1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2015 Joyent, Inc. All rights reserved.
  24  * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
  25  * Copyright 2014 Igor Kozhukhov <ikozhukhov@gmail.com>.
  26  * Copyright 2016, Joyent, Inc.
  27  */
  28 
  29 #ifndef _SYS_ZONE_H
  30 #define _SYS_ZONE_H
  31 
  32 #include <sys/types.h>
  33 #include <sys/mutex.h>
  34 #include <sys/param.h>
  35 #include <sys/rctl.h>
  36 #include <sys/ipc_rctl.h>
  37 #include <sys/pset.h>
  38 #include <sys/tsol/label.h>
  39 #include <sys/cred.h>
  40 #include <sys/netstack.h>
  41 #include <sys/uadmin.h>
  42 #include <sys/ksynch.h>
  43 #include <sys/socket_impl.h>
  44 #include <netinet/in.h>
  45 
  46 #ifdef  __cplusplus
  47 extern "C" {
  48 #endif
  49 
  50 /*
  51  * NOTE
  52  *
  53  * The contents of this file are private to the implementation of
  54  * Solaris and are subject to change at any time without notice.
  55  * Applications and drivers using these interfaces may fail to
  56  * run on future releases.
  57  */
  58 
  59 /* Available both in kernel and for user space */
  60 
  61 /* zone id restrictions and special ids */
  62 #define MAX_ZONEID      9999
  63 #define MIN_USERZONEID  1       /* lowest user-creatable zone ID */
  64 #define MIN_ZONEID      0       /* minimum zone ID on system */
  65 #define GLOBAL_ZONEID   0
  66 #define ZONEID_WIDTH    4       /* for printf */
  67 
  68 /*
  69  * Special zoneid_t token to refer to all zones.
  70  */
  71 #define ALL_ZONES       (-1)
  72 
  73 /* system call subcodes */
  74 #define ZONE_CREATE             0
  75 #define ZONE_DESTROY            1
  76 #define ZONE_GETATTR            2
  77 #define ZONE_ENTER              3
  78 #define ZONE_LIST               4
  79 #define ZONE_SHUTDOWN           5
  80 #define ZONE_LOOKUP             6
  81 #define ZONE_BOOT               7
  82 #define ZONE_VERSION            8
  83 #define ZONE_SETATTR            9
  84 #define ZONE_ADD_DATALINK       10
  85 #define ZONE_DEL_DATALINK       11
  86 #define ZONE_CHECK_DATALINK     12
  87 #define ZONE_LIST_DATALINK      13
  88 
  89 /* zone attributes */
  90 #define ZONE_ATTR_ROOT          1
  91 #define ZONE_ATTR_NAME          2
  92 #define ZONE_ATTR_STATUS        3
  93 #define ZONE_ATTR_PRIVSET       4
  94 #define ZONE_ATTR_UNIQID        5
  95 #define ZONE_ATTR_POOLID        6
  96 #define ZONE_ATTR_INITPID       7
  97 #define ZONE_ATTR_SLBL          8
  98 #define ZONE_ATTR_INITNAME      9
  99 #define ZONE_ATTR_BOOTARGS      10
 100 #define ZONE_ATTR_BRAND         11
 101 #define ZONE_ATTR_PMCAP_NOVER   12
 102 #define ZONE_ATTR_SCHED_CLASS   13
 103 #define ZONE_ATTR_FLAGS         14
 104 #define ZONE_ATTR_HOSTID        15
 105 #define ZONE_ATTR_FS_ALLOWED    16
 106 #define ZONE_ATTR_NETWORK       17
 107 #define ZONE_ATTR_DID           18
 108 #define ZONE_ATTR_PMCAP_PAGEOUT 19
 109 #define ZONE_ATTR_INITNORESTART 20
 110 #define ZONE_ATTR_PG_FLT_DELAY  21
 111 #define ZONE_ATTR_RSS           22
 112 #define ZONE_ATTR_APP_SVC_CT    23
 113 #define ZONE_ATTR_SCHED_FIXEDHI 24
 114 
 115 /* Start of the brand-specific attribute namespace */
 116 #define ZONE_ATTR_BRAND_ATTRS   32768
 117 
 118 #define ZONE_FS_ALLOWED_MAX     1024
 119 
 120 #define ZONE_EVENT_CHANNEL      "com.sun:zones:status"
 121 #define ZONE_EVENT_STATUS_CLASS "status"
 122 #define ZONE_EVENT_STATUS_SUBCLASS      "change"
 123 
 124 #define ZONE_EVENT_UNINITIALIZED        "uninitialized"
 125 #define ZONE_EVENT_INITIALIZED          "initialized"
 126 #define ZONE_EVENT_READY                "ready"
 127 #define ZONE_EVENT_RUNNING              "running"
 128 #define ZONE_EVENT_SHUTTING_DOWN        "shutting_down"
 129 
 130 #define ZONE_CB_NAME            "zonename"
 131 #define ZONE_CB_NEWSTATE        "newstate"
 132 #define ZONE_CB_OLDSTATE        "oldstate"
 133 #define ZONE_CB_TIMESTAMP       "when"
 134 #define ZONE_CB_ZONEID          "zoneid"
 135 
 136 /*
 137  * Exit values that may be returned by scripts or programs invoked by various
 138  * zone commands.
 139  *
 140  * These are defined as:
 141  *
 142  *      ZONE_SUBPROC_OK
 143  *      ===============
 144  *      The subprocess completed successfully.
 145  *
 146  *      ZONE_SUBPROC_USAGE
 147  *      ==================
 148  *      The subprocess failed with a usage message, or a usage message should
 149  *      be output in its behalf.
 150  *
 151  *      ZONE_SUBPROC_NOTCOMPLETE
 152  *      ========================
 153  *      The subprocess did not complete, but the actions performed by the
 154  *      subprocess require no recovery actions by the user.
 155  *
 156  *      For example, if the subprocess were called by "zoneadm install," the
 157  *      installation of the zone did not succeed but the user need not perform
 158  *      a "zoneadm uninstall" before attempting another install.
 159  *
 160  *      ZONE_SUBPROC_FATAL
 161  *      ==================
 162  *      The subprocess failed in a fatal manner, usually one that will require
 163  *      some type of recovery action by the user.
 164  *
 165  *      For example, if the subprocess were called by "zoneadm install," the
 166  *      installation of the zone did not succeed and the user will need to
 167  *      perform a "zoneadm uninstall" before another install attempt is
 168  *      possible.
 169  *
 170  *      The non-success exit values are large to avoid accidental collision
 171  *      with values used internally by some commands (e.g. "Z_ERR" and
 172  *      "Z_USAGE" as used by zoneadm.)
 173  */
 174 #define ZONE_SUBPROC_OK                 0
 175 #define ZONE_SUBPROC_USAGE              253
 176 #define ZONE_SUBPROC_NOTCOMPLETE        254
 177 #define ZONE_SUBPROC_FATAL              255
 178 
 179 #ifdef _SYSCALL32
 180 typedef struct {
 181         caddr32_t zone_name;
 182         caddr32_t zone_root;
 183         caddr32_t zone_privs;
 184         size32_t zone_privssz;
 185         caddr32_t rctlbuf;
 186         size32_t rctlbufsz;
 187         caddr32_t extended_error;
 188         caddr32_t zfsbuf;
 189         size32_t  zfsbufsz;
 190         int match;                      /* match level */
 191         uint32_t doi;                   /* DOI for label */
 192         caddr32_t label;                /* label associated with zone */
 193         int flags;
 194 } zone_def32;
 195 #endif
 196 typedef struct {
 197         const char *zone_name;
 198         const char *zone_root;
 199         const struct priv_set *zone_privs;
 200         size_t zone_privssz;
 201         const char *rctlbuf;
 202         size_t rctlbufsz;
 203         int *extended_error;
 204         const char *zfsbuf;
 205         size_t zfsbufsz;
 206         int match;                      /* match level */
 207         uint32_t doi;                   /* DOI for label */
 208         const bslabel_t *label;         /* label associated with zone */
 209         int flags;
 210 } zone_def;
 211 
 212 /* extended error information */
 213 #define ZE_UNKNOWN      0       /* No extended error info */
 214 #define ZE_CHROOTED     1       /* tried to zone_create from chroot */
 215 #define ZE_AREMOUNTS    2       /* there are mounts within the zone */
 216 #define ZE_LABELINUSE   3       /* label is already in use by some other zone */
 217 
 218 /*
 219  * zone_status values
 220  *
 221  * You must modify zone_status_names in mdb(1M)'s genunix module
 222  * (genunix/zone.c) when you modify this enum.
 223  */
 224 typedef enum {
 225         ZONE_IS_UNINITIALIZED = 0,
 226         ZONE_IS_INITIALIZED,
 227         ZONE_IS_READY,
 228         ZONE_IS_BOOTING,
 229         ZONE_IS_RUNNING,
 230         ZONE_IS_SHUTTING_DOWN,
 231         ZONE_IS_EMPTY,
 232         ZONE_IS_DOWN,
 233         ZONE_IS_DYING,
 234         ZONE_IS_DEAD
 235 } zone_status_t;
 236 #define ZONE_MIN_STATE          ZONE_IS_UNINITIALIZED
 237 #define ZONE_MAX_STATE          ZONE_IS_DEAD
 238 
 239 /*
 240  * Valid commands which may be issued by zoneadm to zoneadmd.  The kernel also
 241  * communicates with zoneadmd, but only uses Z_REBOOT and Z_HALT.
 242  */
 243 typedef enum zone_cmd {
 244         Z_READY, Z_BOOT, Z_FORCEBOOT, Z_REBOOT, Z_HALT, Z_NOTE_UNINSTALLING,
 245         Z_MOUNT, Z_FORCEMOUNT, Z_UNMOUNT, Z_SHUTDOWN
 246 } zone_cmd_t;
 247 
 248 /*
 249  * The structure of a request to zoneadmd.
 250  */
 251 typedef struct zone_cmd_arg {
 252         uint64_t        uniqid;         /* unique "generation number" */
 253         zone_cmd_t      cmd;            /* requested action */
 254         int             status;         /* init status on shutdown */
 255         uint32_t        debug;          /* enable brand hook debug */
 256         char locale[MAXPATHLEN];        /* locale in which to render messages */
 257         char bootbuf[BOOTARGS_MAX];     /* arguments passed to zone_boot() */
 258         /* Needed for 32/64 zoneadm -> zoneadmd door arg size check. */
 259         int             pad;
 260 } zone_cmd_arg_t;
 261 
 262 /*
 263  * Structure of zoneadmd's response to a request.  A NULL return value means
 264  * the caller should attempt to restart zoneadmd and retry.
 265  */
 266 typedef struct zone_cmd_rval {
 267         int rval;                       /* return value of request */
 268         char errbuf[1]; /* variable-sized buffer containing error messages */
 269 } zone_cmd_rval_t;
 270 
 271 /*
 272  * The zone support infrastructure uses the zone name as a component
 273  * of unix domain (AF_UNIX) sockets, which are limited to 108 characters
 274  * in length, so ZONENAME_MAX is limited by that.
 275  */
 276 #define ZONENAME_MAX            64
 277 
 278 #define GLOBAL_ZONENAME         "global"
 279 
 280 /*
 281  * Extended Regular expression (see regex(5)) which matches all valid zone
 282  * names.
 283  */
 284 #define ZONENAME_REGEXP         "[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}"
 285 
 286 /*
 287  * Where the zones support infrastructure places temporary files.
 288  */
 289 #define ZONES_TMPDIR            "/var/run/zones"
 290 
 291 /*
 292  * The path to the door used by clients to communicate with zoneadmd.
 293  */
 294 #define ZONE_DOOR_PATH          ZONES_TMPDIR "/%s.zoneadmd_door"
 295 
 296 
 297 /* zone_flags */
 298 /*
 299  * Threads that read or write the following flag must hold zone_lock.
 300  */
 301 #define ZF_REFCOUNTS_LOGGED     0x1     /* a thread logged the zone's refs */
 302 
 303 /*
 304  * The following threads are set when the zone is created and never changed.
 305  * Threads that test for these flags don't have to hold zone_lock.
 306  */
 307 #define ZF_HASHED_LABEL         0x2     /* zone has a unique label */
 308 #define ZF_IS_SCRATCH           0x4     /* scratch zone */
 309 #define ZF_NET_EXCL             0x8     /* Zone has an exclusive IP stack */
 310 
 311 
 312 /* zone_create flags */
 313 #define ZCF_NET_EXCL            0x1     /* Create a zone with exclusive IP */
 314 
 315 /* zone network properties */
 316 #define ZONE_NETWORK_ADDRESS    1
 317 #define ZONE_NETWORK_DEFROUTER  2
 318 
 319 #define ZONE_NET_ADDRNAME       "address"
 320 #define ZONE_NET_RTRNAME        "route"
 321 
 322 typedef struct zone_net_data {
 323         int zn_type;
 324         int zn_len;
 325         datalink_id_t zn_linkid;
 326         uint8_t zn_val[1];
 327 } zone_net_data_t;
 328 
 329 
 330 #ifdef _KERNEL
 331 
 332 /*
 333  * We need to protect the definition of 'list_t' from userland applications and
 334  * libraries which may be defining ther own versions.
 335  */
 336 #include <sys/list.h>
 337 #include <sys/loadavg.h>
 338 
 339 #define GLOBAL_ZONEUNIQID       0       /* uniqid of the global zone */
 340 
 341 struct pool;
 342 struct brand;
 343 
 344 /*
 345  * Each of these constants identifies a kernel subsystem that acquires and
 346  * releases zone references.  Each subsystem that invokes
 347  * zone_hold_ref() and zone_rele_ref() should specify the
 348  * zone_ref_subsys_t constant associated with the subsystem.  Tracked holds
 349  * help users and developers quickly identify subsystems that stall zone
 350  * shutdowns indefinitely.
 351  *
 352  * NOTE: You must modify zone_ref_subsys_names in usr/src/uts/common/os/zone.c
 353  * when you modify this enumeration.
 354  */
 355 typedef enum zone_ref_subsys {
 356         ZONE_REF_NFS,                   /* NFS */
 357         ZONE_REF_NFSV4,                 /* NFSv4 */
 358         ZONE_REF_SMBFS,                 /* SMBFS */
 359         ZONE_REF_MNTFS,                 /* MNTFS */
 360         ZONE_REF_LOFI,                  /* LOFI devices */
 361         ZONE_REF_VFS,                   /* VFS infrastructure */
 362         ZONE_REF_IPC,                   /* IPC infrastructure */
 363         ZONE_REF_NUM_SUBSYS             /* This must be the last entry. */
 364 } zone_ref_subsys_t;
 365 
 366 /*
 367  * zone_ref represents a general-purpose references to a zone.  Each zone's
 368  * references are linked into the zone's zone_t::zone_ref_list.  This allows
 369  * debuggers to walk zones' references.
 370  */
 371 typedef struct zone_ref {
 372         struct zone     *zref_zone; /* the zone to which the reference refers */
 373         list_node_t     zref_linkage; /* linkage for zone_t::zone_ref_list */
 374 } zone_ref_t;
 375 
 376 /*
 377  * Structure to record list of ZFS datasets exported to a zone.
 378  */
 379 typedef struct zone_dataset {
 380         char            *zd_dataset;
 381         list_node_t     zd_linkage;
 382 } zone_dataset_t;
 383 
 384 /*
 385  * structure for zone kstats
 386  */
 387 typedef struct zone_kstat {
 388         kstat_named_t zk_zonename;
 389         kstat_named_t zk_usage;
 390         kstat_named_t zk_value;
 391 } zone_kstat_t;
 392 
 393 struct cpucap;
 394 
 395 typedef struct {
 396         hrtime_t        cycle_start;
 397         uint_t          cycle_cnt;
 398         hrtime_t        zone_avg_cnt;
 399 } sys_zio_cntr_t;
 400 
 401 typedef struct {
 402         kstat_named_t   zv_zonename;
 403         kstat_named_t   zv_nread;
 404         kstat_named_t   zv_reads;
 405         kstat_named_t   zv_rtime;
 406         kstat_named_t   zv_rlentime;
 407         kstat_named_t   zv_rcnt;
 408         kstat_named_t   zv_nwritten;
 409         kstat_named_t   zv_writes;
 410         kstat_named_t   zv_wtime;
 411         kstat_named_t   zv_wlentime;
 412         kstat_named_t   zv_wcnt;
 413         kstat_named_t   zv_10ms_ops;
 414         kstat_named_t   zv_100ms_ops;
 415         kstat_named_t   zv_1s_ops;
 416         kstat_named_t   zv_10s_ops;
 417         kstat_named_t   zv_delay_cnt;
 418         kstat_named_t   zv_delay_time;
 419 } zone_vfs_kstat_t;
 420 
 421 typedef struct {
 422         kstat_named_t   zz_zonename;
 423         kstat_named_t   zz_nread;
 424         kstat_named_t   zz_reads;
 425         kstat_named_t   zz_rtime;
 426         kstat_named_t   zz_rlentime;
 427         kstat_named_t   zz_nwritten;
 428         kstat_named_t   zz_writes;
 429         kstat_named_t   zz_waittime;
 430 } zone_zfs_kstat_t;
 431 
 432 typedef struct {
 433         kstat_named_t   zm_zonename;
 434         kstat_named_t   zm_rss;
 435         kstat_named_t   zm_phys_cap;
 436         kstat_named_t   zm_swap;
 437         kstat_named_t   zm_swap_cap;
 438         kstat_named_t   zm_nover;
 439         kstat_named_t   zm_pagedout;
 440         kstat_named_t   zm_pgpgin;
 441         kstat_named_t   zm_anonpgin;
 442         kstat_named_t   zm_execpgin;
 443         kstat_named_t   zm_fspgin;
 444         kstat_named_t   zm_anon_alloc_fail;
 445         kstat_named_t   zm_pf_throttle;
 446         kstat_named_t   zm_pf_throttle_usec;
 447 } zone_mcap_kstat_t;
 448 
 449 typedef struct {
 450         kstat_named_t   zm_zonename;    /* full name, kstat truncates name */
 451         kstat_named_t   zm_utime;
 452         kstat_named_t   zm_stime;
 453         kstat_named_t   zm_wtime;
 454         kstat_named_t   zm_avenrun1;
 455         kstat_named_t   zm_avenrun5;
 456         kstat_named_t   zm_avenrun15;
 457         kstat_named_t   zm_ffcap;
 458         kstat_named_t   zm_ffnoproc;
 459         kstat_named_t   zm_ffnomem;
 460         kstat_named_t   zm_ffmisc;
 461         kstat_named_t   zm_nested_intp;
 462         kstat_named_t   zm_init_pid;
 463         kstat_named_t   zm_boot_time;
 464 } zone_misc_kstat_t;
 465 
 466 typedef struct zone {
 467         /*
 468          * zone_name is never modified once set.
 469          */
 470         char            *zone_name;     /* zone's configuration name */
 471         /*
 472          * zone_nodename and zone_domain are never freed once allocated.
 473          */
 474         char            *zone_nodename; /* utsname.nodename equivalent */
 475         char            *zone_domain;   /* srpc_domain equivalent */
 476         /*
 477          * zone_hostid is used for per-zone hostid emulation.
 478          * Currently it isn't modified after it's set (so no locks protect
 479          * accesses), but that might have to change when we allow
 480          * administrators to change running zones' properties.
 481          *
 482          * The global zone's zone_hostid must always be HW_INVALID_HOSTID so
 483          * that zone_get_hostid() will function correctly.
 484          */
 485         uint32_t        zone_hostid;    /* zone's hostid, HW_INVALID_HOSTID */
 486                                         /* if not emulated */
 487         /*
 488          * zone_lock protects the following fields of a zone_t:
 489          *      zone_ref
 490          *      zone_cred_ref
 491          *      zone_subsys_ref
 492          *      zone_ref_list
 493          *      zone_ntasks
 494          *      zone_flags
 495          *      zone_zsd
 496          *      zone_pfexecd
 497          */
 498         kmutex_t        zone_lock;
 499         /*
 500          * zone_linkage is the zone's linkage into the active or
 501          * death-row list.  The field is protected by zonehash_lock.
 502          */
 503         list_node_t     zone_linkage;
 504         zoneid_t        zone_id;        /* ID of zone */
 505         uint_t          zone_ref;       /* count of zone_hold()s on zone */
 506         uint_t          zone_cred_ref;  /* count of zone_hold_cred()s on zone */
 507         /*
 508          * Fixed-sized array of subsystem-specific reference counts
 509          * The sum of all of the counts must be less than or equal to zone_ref.
 510          * The array is indexed by the counts' subsystems' zone_ref_subsys_t
 511          * constants.
 512          */
 513         uint_t          zone_subsys_ref[ZONE_REF_NUM_SUBSYS];
 514         list_t          zone_ref_list;  /* list of zone_ref_t structs */
 515         /*
 516          * zone_rootvp and zone_rootpath can never be modified once set.
 517          */
 518         struct vnode    *zone_rootvp;   /* zone's root vnode */
 519         char            *zone_rootpath; /* Path to zone's root + '/' */
 520         ushort_t        zone_flags;     /* misc flags */
 521         zone_status_t   zone_status;    /* protected by zone_status_lock */
 522         uint_t          zone_ntasks;    /* number of tasks executing in zone */
 523         kmutex_t        zone_nlwps_lock; /* protects zone_nlwps, and *_nlwps */
 524                                         /* counters in projects and tasks */
 525                                         /* that are within the zone */
 526         rctl_qty_t      zone_nlwps;     /* number of lwps in zone */
 527         rctl_qty_t      zone_nlwps_ctl; /* protected by zone_rctls->rcs_lock */
 528         rctl_qty_t      zone_shmmax;    /* System V shared memory usage */
 529         ipc_rqty_t      zone_ipc;       /* System V IPC id resource usage */
 530 
 531         uint_t          zone_rootpathlen; /* strlen(zone_rootpath) + 1 */
 532         uint32_t        zone_shares;    /* FSS shares allocated to zone */
 533         rctl_set_t      *zone_rctls;    /* zone-wide (zone.*) rctls */
 534         kmutex_t        zone_mem_lock;  /* protects zone_locked_mem and */
 535                                         /* kpd_locked_mem for all */
 536                                         /* projects in zone. */
 537                                         /* Also protects zone_max_swap */
 538                                         /* grab after p_lock, before rcs_lock */
 539         rctl_qty_t      zone_locked_mem;        /* bytes of locked memory in */
 540                                                 /* zone */
 541         rctl_qty_t      zone_locked_mem_ctl;    /* Current locked memory */
 542                                                 /* limit.  Protected by */
 543                                                 /* zone_rctls->rcs_lock */
 544         rctl_qty_t      zone_max_swap; /* bytes of swap reserved by zone */
 545         rctl_qty_t      zone_max_swap_ctl;      /* current swap limit. */
 546                                                 /* Protected by */
 547                                                 /* zone_rctls->rcs_lock */
 548         kmutex_t        zone_rctl_lock; /* protects zone_max_lofi */
 549         rctl_qty_t      zone_max_lofi; /* lofi devs for zone */
 550         rctl_qty_t      zone_max_lofi_ctl;      /* current lofi limit. */
 551                                                 /* Protected by */
 552                                                 /* zone_rctls->rcs_lock */
 553         list_t          zone_zsd;       /* list of Zone-Specific Data values */
 554         kcondvar_t      zone_cv;        /* used to signal state changes */
 555         struct proc     *zone_zsched;   /* Dummy kernel "zsched" process */
 556         pid_t           zone_proc_initpid; /* pid of "init" for this zone */
 557         char            *zone_initname;         /* fs path to 'init' */
 558         int             zone_init_status;       /* init's exit status */
 559         int             zone_boot_err;  /* for zone_boot() if boot fails */
 560         char            *zone_bootargs; /* arguments passed via zone_boot() */
 561         rctl_qty_t      zone_phys_mem_ctl;      /* current phys. memory limit */
 562         /*
 563          * zone_kthreads is protected by zone_status_lock.
 564          */
 565         kthread_t       *zone_kthreads; /* kernel threads in zone */
 566         struct priv_set *zone_privset;  /* limit set for zone */
 567         /*
 568          * zone_vfslist is protected by vfs_list_lock().
 569          */
 570         struct vfs      *zone_vfslist;  /* list of FS's mounted in zone */
 571         uint64_t        zone_uniqid;    /* unique zone generation number */
 572         struct cred     *zone_kcred;    /* kcred-like, zone-limited cred */
 573         /*
 574          * zone_pool is protected by pool_lock().
 575          */
 576         struct pool     *zone_pool;     /* pool the zone is bound to */
 577         hrtime_t        zone_pool_mod;  /* last pool bind modification time */
 578         /* zone_psetid is protected by cpu_lock */
 579         psetid_t        zone_psetid;    /* pset the zone is bound to */
 580 
 581         time_t          zone_boot_time; /* Similar to boot_time */
 582 
 583         /*
 584          * The following two can be read without holding any locks.  They are
 585          * updated under cpu_lock.
 586          */
 587         int             zone_ncpus;  /* zone's idea of ncpus */
 588         int             zone_ncpus_online; /* zone's idea of ncpus_online */
 589         /*
 590          * List of ZFS datasets exported to this zone.
 591          */
 592         list_t          zone_datasets;  /* list of datasets */
 593 
 594         ts_label_t      *zone_slabel;   /* zone sensitivity label */
 595         int             zone_match;     /* require label match for packets */
 596         tsol_mlp_list_t zone_mlps;      /* MLPs on zone-private addresses */
 597 
 598         boolean_t       zone_restart_init;      /* Restart init if it dies? */
 599         boolean_t       zone_reboot_on_init_exit; /* Reboot if init dies? */
 600         boolean_t       zone_setup_app_contract; /* setup contract? */
 601         struct brand    *zone_brand;            /* zone's brand */
 602         void            *zone_brand_data;       /* store brand specific data */
 603         id_t            zone_defaultcid;        /* dflt scheduling class id */
 604         boolean_t       zone_fixed_hipri;       /* fixed sched. hi prio */
 605         kstat_t         *zone_swapresv_kstat;
 606         kstat_t         *zone_lockedmem_kstat;
 607         /*
 608          * zone_dl_list is protected by zone_lock
 609          */
 610         list_t          zone_dl_list;
 611         netstack_t      *zone_netstack;
 612         struct cpucap   *zone_cpucap;   /* CPU caps data */
 613 
 614         /*
 615          * Data and counters used for ZFS fair-share disk IO.
 616          */
 617         rctl_qty_t      zone_zfs_io_pri;        /* ZFS IO priority */
 618         uint_t          zone_zfs_queued[2];     /* sync I/O enqueued count */
 619         uint64_t        zone_zfs_weight;        /* used to prevent starvation */
 620         uint64_t        zone_io_util;           /* IO utilization metric */
 621         boolean_t       zone_io_util_above_avg; /* IO util percent > avg. */
 622         uint16_t        zone_io_delay;          /* IO delay on logical r/w */
 623         kmutex_t        zone_stg_io_lock;       /* protects IO window data */
 624         sys_zio_cntr_t  zone_rd_ops;            /* Counters for ZFS reads, */
 625         sys_zio_cntr_t  zone_wr_ops;            /* writes and */
 626         sys_zio_cntr_t  zone_lwr_ops;           /* logical writes. */
 627 
 628         /*
 629          * kstats and counters for I/O ops and bytes.
 630          */
 631         kmutex_t        zone_io_lock;           /* protects I/O statistics */
 632         kstat_t         *zone_io_ksp;
 633         kstat_io_t      *zone_io_kiop;
 634 
 635         /*
 636          * kstats and counters for VFS ops and bytes.
 637          */
 638         kmutex_t        zone_vfs_lock;          /* protects VFS statistics */
 639         kstat_t         *zone_vfs_ksp;
 640         kstat_io_t      zone_vfs_rwstats;
 641         zone_vfs_kstat_t *zone_vfs_stats;
 642 
 643         /*
 644          * kstats for ZFS I/O ops and bytes.
 645          */
 646         kmutex_t        zone_zfs_lock;          /* protects ZFS statistics */
 647         kstat_io_t      zone_zfs_rwstats;
 648         zone_zfs_kstat_t *zone_zfs_stats;
 649 
 650         /*
 651          * Solaris Auditing per-zone audit context
 652          */
 653         struct au_kcontext      *zone_audit_kctxt;
 654         /*
 655          * For private use by mntfs.
 656          */
 657         struct mntelem  *zone_mntfs_db;
 658         krwlock_t       zone_mntfs_db_lock;
 659 
 660         struct klpd_reg         *zone_pfexecd;
 661 
 662         char            *zone_fs_allowed;
 663         rctl_qty_t      zone_nprocs;    /* number of processes in the zone */
 664         rctl_qty_t      zone_nprocs_ctl;        /* current limit protected by */
 665                                                 /* zone_rctls->rcs_lock */
 666         kstat_t         *zone_nprocs_kstat;
 667 
 668         /*
 669          * kstats and counters for physical memory capping.
 670          */
 671         rctl_qty_t      zone_phys_mem;  /* current bytes of phys. mem. (RSS) */
 672         kstat_t         *zone_physmem_kstat;
 673         uint64_t        zone_mcap_nover;        /* # of times over phys. cap */
 674         uint64_t        zone_mcap_pagedout;     /* bytes of mem. paged out */
 675         kmutex_t        zone_mcap_lock; /* protects mcap statistics */
 676         kstat_t         *zone_mcap_ksp;
 677         zone_mcap_kstat_t *zone_mcap_stats;
 678         uint64_t        zone_pgpgin;            /* pages paged in */
 679         uint64_t        zone_anonpgin;          /* anon pages paged in */
 680         uint64_t        zone_execpgin;          /* exec pages paged in */
 681         uint64_t        zone_fspgin;            /* fs pages paged in */
 682         uint64_t        zone_anon_alloc_fail;   /* cnt of anon alloc fails */
 683         uint64_t        zone_pf_throttle;       /* cnt of page flt throttles */
 684         uint64_t        zone_pf_throttle_usec;  /* time of page flt throttles */
 685 
 686         /* Num usecs to throttle page fault when zone is over phys. mem cap */
 687         uint32_t        zone_pg_flt_delay;
 688 
 689         /*
 690          * Misc. kstats and counters for zone cpu-usage aggregation.
 691          * The zone_Xtime values are the sum of the micro-state accounting
 692          * values for all threads that are running or have run in the zone.
 693          * This is tracked in msacct.c as threads change state.
 694          * The zone_stime is the sum of the LMS_SYSTEM times.
 695          * The zone_utime is the sum of the LMS_USER times.
 696          * The zone_wtime is the sum of the LMS_WAIT_CPU times.
 697          * As with per-thread micro-state accounting values, these values are
 698          * not scaled to nanosecs.  The scaling is done by the
 699          * zone_misc_kstat_update function when kstats are requested.
 700          */
 701         kmutex_t        zone_misc_lock;         /* protects misc statistics */
 702         kstat_t         *zone_misc_ksp;
 703         zone_misc_kstat_t *zone_misc_stats;
 704         uint64_t        zone_stime;             /* total system time */
 705         uint64_t        zone_utime;             /* total user time */
 706         uint64_t        zone_wtime;             /* total time waiting in runq */
 707         /* fork-fail kstat tracking */
 708         uint32_t        zone_ffcap;             /* hit an rctl cap */
 709         uint32_t        zone_ffnoproc;          /* get proc/lwp error */
 710         uint32_t        zone_ffnomem;           /* as_dup/memory error */
 711         uint32_t        zone_ffmisc;            /* misc. other error */
 712 
 713         uint32_t        zone_nested_intp;       /* nested interp. kstat */
 714 
 715         struct loadavg_s zone_loadavg;          /* loadavg for this zone */
 716         uint64_t        zone_hp_avenrun[3];     /* high-precision avenrun */
 717         int             zone_avenrun[3];        /* FSCALED avg. run queue len */
 718 
 719         /*
 720          * FSS stats updated once per second by fss_decay_usage.
 721          */
 722         uint32_t        zone_fss_gen;           /* FSS generation cntr */
 723         uint64_t        zone_run_ticks;         /* tot # of ticks running */
 724 
 725         /*
 726          * DTrace-private per-zone state
 727          */
 728         int             zone_dtrace_getf;       /* # of unprivileged getf()s */
 729 
 730         /*
 731          * Synchronization primitives used to synchronize between mounts and
 732          * zone creation/destruction.
 733          */
 734         int             zone_mounts_in_progress;
 735         kcondvar_t      zone_mount_cv;
 736         kmutex_t        zone_mount_lock;
 737 } zone_t;
 738 
 739 /*
 740  * Special value of zone_psetid to indicate that pools are disabled.
 741  */
 742 #define ZONE_PS_INVAL   PS_MYID
 743 
 744 
 745 extern zone_t zone0;
 746 extern zone_t *global_zone;
 747 extern uint_t maxzones;
 748 extern rctl_hndl_t rc_zone_nlwps;
 749 extern rctl_hndl_t rc_zone_nprocs;
 750 
 751 extern long zone(int, void *, void *, void *, void *);
 752 extern void zone_zsd_init(void);
 753 extern void zone_init(void);
 754 extern void zone_hold(zone_t *);
 755 extern void zone_rele(zone_t *);
 756 extern void zone_init_ref(zone_ref_t *);
 757 extern void zone_hold_ref(zone_t *, zone_ref_t *, zone_ref_subsys_t);
 758 extern void zone_rele_ref(zone_ref_t *, zone_ref_subsys_t);
 759 extern void zone_cred_hold(zone_t *);
 760 extern void zone_cred_rele(zone_t *);
 761 extern void zone_task_hold(zone_t *);
 762 extern void zone_task_rele(zone_t *);
 763 extern zone_t *zone_find_by_id(zoneid_t);
 764 extern zone_t *zone_find_by_label(const ts_label_t *);
 765 extern zone_t *zone_find_by_name(char *);
 766 extern zone_t *zone_find_by_any_path(const char *, boolean_t);
 767 extern zone_t *zone_find_by_path(const char *);
 768 extern zoneid_t getzoneid(void);
 769 extern zone_t *zone_find_by_id_nolock(zoneid_t);
 770 extern int zone_datalink_walk(zoneid_t, int (*)(datalink_id_t, void *), void *);
 771 extern int zone_check_datalink(zoneid_t *, datalink_id_t);
 772 extern void zone_loadavg_update();
 773 
 774 /*
 775  * Zone-specific data (ZSD) APIs
 776  */
 777 /*
 778  * The following is what code should be initializing its zone_key_t to if it
 779  * calls zone_getspecific() without necessarily knowing that zone_key_create()
 780  * has been called on the key.
 781  */
 782 #define ZONE_KEY_UNINITIALIZED  0
 783 
 784 typedef uint_t zone_key_t;
 785 
 786 extern void     zone_key_create(zone_key_t *, void *(*)(zoneid_t),
 787     void (*)(zoneid_t, void *), void (*)(zoneid_t, void *));
 788 extern int      zone_key_delete(zone_key_t);
 789 extern void     *zone_getspecific(zone_key_t, zone_t *);
 790 extern int      zone_setspecific(zone_key_t, zone_t *, const void *);
 791 
 792 /*
 793  * The definition of a zsd_entry is truly private to zone.c and is only
 794  * placed here so it can be shared with mdb.
 795  *
 796  * State maintained for each zone times each registered key, which tracks
 797  * the state of the create, shutdown and destroy callbacks.
 798  *
 799  * zsd_flags is used to keep track of pending actions to avoid holding locks
 800  * when calling the create/shutdown/destroy callbacks, since doing so
 801  * could lead to deadlocks.
 802  */
 803 struct zsd_entry {
 804         zone_key_t              zsd_key;        /* Key used to lookup value */
 805         void                    *zsd_data;      /* Caller-managed value */
 806         /*
 807          * Callbacks to be executed when a zone is created, shutdown, and
 808          * destroyed, respectively.
 809          */
 810         void                    *(*zsd_create)(zoneid_t);
 811         void                    (*zsd_shutdown)(zoneid_t, void *);
 812         void                    (*zsd_destroy)(zoneid_t, void *);
 813         list_node_t             zsd_linkage;
 814         uint16_t                zsd_flags;      /* See below */
 815         kcondvar_t              zsd_cv;
 816 };
 817 
 818 /*
 819  * zsd_flags
 820  */
 821 #define ZSD_CREATE_NEEDED       0x0001
 822 #define ZSD_CREATE_INPROGRESS   0x0002
 823 #define ZSD_CREATE_COMPLETED    0x0004
 824 #define ZSD_SHUTDOWN_NEEDED     0x0010
 825 #define ZSD_SHUTDOWN_INPROGRESS 0x0020
 826 #define ZSD_SHUTDOWN_COMPLETED  0x0040
 827 #define ZSD_DESTROY_NEEDED      0x0100
 828 #define ZSD_DESTROY_INPROGRESS  0x0200
 829 #define ZSD_DESTROY_COMPLETED   0x0400
 830 
 831 #define ZSD_CREATE_ALL  \
 832         (ZSD_CREATE_NEEDED|ZSD_CREATE_INPROGRESS|ZSD_CREATE_COMPLETED)
 833 #define ZSD_SHUTDOWN_ALL        \
 834         (ZSD_SHUTDOWN_NEEDED|ZSD_SHUTDOWN_INPROGRESS|ZSD_SHUTDOWN_COMPLETED)
 835 #define ZSD_DESTROY_ALL \
 836         (ZSD_DESTROY_NEEDED|ZSD_DESTROY_INPROGRESS|ZSD_DESTROY_COMPLETED)
 837 
 838 #define ZSD_ALL_INPROGRESS \
 839         (ZSD_CREATE_INPROGRESS|ZSD_SHUTDOWN_INPROGRESS|ZSD_DESTROY_INPROGRESS)
 840 
 841 /*
 842  * Macros to help with zone visibility restrictions.
 843  */
 844 
 845 /*
 846  * Is process in the global zone?
 847  */
 848 #define INGLOBALZONE(p) \
 849         ((p)->p_zone == global_zone)
 850 
 851 /*
 852  * Can process view objects in given zone?
 853  */
 854 #define HASZONEACCESS(p, zoneid) \
 855         ((p)->p_zone->zone_id == (zoneid) || INGLOBALZONE(p))
 856 
 857 /*
 858  * Convenience macro to see if a resolved path is visible from within a
 859  * given zone.
 860  *
 861  * The basic idea is that the first (zone_rootpathlen - 1) bytes of the
 862  * two strings must be equal.  Since the rootpathlen has a trailing '/',
 863  * we want to skip everything in the path up to (but not including) the
 864  * trailing '/'.
 865  */
 866 #define ZONE_PATH_VISIBLE(path, zone) \
 867         (strncmp((path), (zone)->zone_rootpath,              \
 868             (zone)->zone_rootpathlen - 1) == 0)
 869 
 870 /*
 871  * Convenience macro to go from the global view of a path to that seen
 872  * from within said zone.  It is the responsibility of the caller to
 873  * ensure that the path is a resolved one (ie, no '..'s or '.'s), and is
 874  * in fact visible from within the zone.
 875  */
 876 #define ZONE_PATH_TRANSLATE(path, zone) \
 877         (ASSERT(ZONE_PATH_VISIBLE(path, zone)), \
 878         (path) + (zone)->zone_rootpathlen - 2)
 879 
 880 /*
 881  * Special processes visible in all zones.
 882  */
 883 #define ZONE_SPECIALPID(x)       ((x) == 0 || (x) == 1)
 884 
 885 /*
 886  * Zone-safe version of thread_create() to be used when the caller wants to
 887  * create a kernel thread to run within the current zone's context.
 888  */
 889 extern kthread_t *zthread_create(caddr_t, size_t, void (*)(), void *, size_t,
 890     pri_t);
 891 extern void zthread_exit(void);
 892 
 893 /*
 894  * Functions for an external observer to register interest in a zone's status
 895  * change.  Observers will be woken up when the zone status equals the status
 896  * argument passed in (in the case of zone_status_timedwait, the function may
 897  * also return because of a timeout; zone_status_wait_sig may return early due
 898  * to a signal being delivered; zone_status_timedwait_sig may return for any of
 899  * the above reasons).
 900  *
 901  * Otherwise these behave identically to cv_timedwait(), cv_wait(), and
 902  * cv_wait_sig() respectively.
 903  */
 904 extern clock_t zone_status_timedwait(zone_t *, clock_t, zone_status_t);
 905 extern clock_t zone_status_timedwait_sig(zone_t *, clock_t, zone_status_t);
 906 extern void zone_status_wait(zone_t *, zone_status_t);
 907 extern int zone_status_wait_sig(zone_t *, zone_status_t);
 908 
 909 /*
 910  * Get the status  of the zone (at the time it was called).  The state may
 911  * have progressed by the time it is returned.
 912  */
 913 extern zone_status_t zone_status_get(zone_t *);
 914 
 915 /*
 916  * Safely get the hostid of the specified zone (defaults to machine's hostid
 917  * if the specified zone doesn't emulate a hostid).  Passing NULL retrieves
 918  * the global zone's (i.e., physical system's) hostid.
 919  */
 920 extern uint32_t zone_get_hostid(zone_t *);
 921 
 922 /*
 923  * Get the "kcred" credentials corresponding to the given zone.
 924  */
 925 extern struct cred *zone_get_kcred(zoneid_t);
 926 
 927 /*
 928  * Get/set the pool the zone is currently bound to.
 929  */
 930 extern struct pool *zone_pool_get(zone_t *);
 931 extern void zone_pool_set(zone_t *, struct pool *);
 932 
 933 /*
 934  * Get/set the pset the zone is currently using.
 935  */
 936 extern psetid_t zone_pset_get(zone_t *);
 937 extern void zone_pset_set(zone_t *, psetid_t);
 938 
 939 /*
 940  * Get the number of cpus/online-cpus visible from the given zone.
 941  */
 942 extern int zone_ncpus_get(zone_t *);
 943 extern int zone_ncpus_online_get(zone_t *);
 944 
 945 /*
 946  * Returns true if the named pool/dataset is visible in the current zone.
 947  */
 948 extern int zone_dataset_visible(const char *, int *);
 949 extern int zone_dataset_visible_inzone(zone_t *, const char *, int *);
 950 
 951 /*
 952  * zone version of kadmin()
 953  */
 954 extern int zone_kadmin(int, int, const char *, cred_t *);
 955 extern void zone_shutdown_global(void);
 956 
 957 extern void mount_in_progress(zone_t *);
 958 extern void mount_completed(zone_t *);
 959 
 960 extern int zone_walk(int (*)(zone_t *, void *), void *);
 961 
 962 extern rctl_hndl_t rc_zone_locked_mem;
 963 extern rctl_hndl_t rc_zone_max_swap;
 964 extern rctl_hndl_t rc_zone_phys_mem;
 965 extern rctl_hndl_t rc_zone_max_lofi;
 966 
 967 #endif  /* _KERNEL */
 968 
 969 #ifdef  __cplusplus
 970 }
 971 #endif
 972 
 973 #endif  /* _SYS_ZONE_H */