xref: /illumos-gate/usr/src/uts/common/sys/zone.h (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_ZONE_H
27 #define	_SYS_ZONE_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <sys/mutex.h>
33 #include <sys/param.h>
34 #include <sys/rctl.h>
35 #include <sys/pset.h>
36 #include <sys/tsol/label.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * NOTE
44  *
45  * The contents of this file are private to the implementation of
46  * Solaris and are subject to change at any time without notice.
47  * Applications and drivers using these interfaces may fail to
48  * run on future releases.
49  */
50 
51 /* Available both in kernel and for user space */
52 
53 /* zone id restrictions and special ids */
54 #define	MAX_ZONEID	9999
55 #define	MIN_USERZONEID	1	/* lowest user-creatable zone ID */
56 #define	MIN_ZONEID	0	/* minimum zone ID on system */
57 #define	GLOBAL_ZONEID	0
58 #define	ZONEID_WIDTH	4	/* for printf */
59 
60 /*
61  * Special zoneid_t token to refer to all zones.
62  */
63 #define	ALL_ZONES	(-1)
64 
65 /* system call subcodes */
66 #define	ZONE_CREATE	0
67 #define	ZONE_DESTROY	1
68 #define	ZONE_GETATTR	2
69 #define	ZONE_ENTER	3
70 #define	ZONE_LIST	4
71 #define	ZONE_SHUTDOWN	5
72 #define	ZONE_LOOKUP	6
73 #define	ZONE_BOOT	7
74 #define	ZONE_VERSION	8
75 
76 /* zone attributes */
77 #define	ZONE_ATTR_ROOT		1
78 #define	ZONE_ATTR_NAME		2
79 #define	ZONE_ATTR_STATUS	3
80 #define	ZONE_ATTR_PRIVSET	4
81 #define	ZONE_ATTR_UNIQID	5
82 #define	ZONE_ATTR_POOLID	6
83 #define	ZONE_ATTR_INITPID	7
84 #define	ZONE_ATTR_SLBL		8
85 
86 #define	ZONE_EVENT_CHANNEL	"com.sun:zones:status"
87 #define	ZONE_EVENT_STATUS_CLASS	"status"
88 #define	ZONE_EVENT_STATUS_SUBCLASS	"change"
89 
90 #define	ZONE_EVENT_UNINITIALIZED	"uninitialized"
91 #define	ZONE_EVENT_READY		"ready"
92 #define	ZONE_EVENT_RUNNING		"running"
93 #define	ZONE_EVENT_SHUTTING_DOWN	"shutting_down"
94 
95 #define	ZONE_CB_NAME		"zonename"
96 #define	ZONE_CB_NEWSTATE	"newstate"
97 #define	ZONE_CB_OLDSTATE	"oldstate"
98 #define	ZONE_CB_TIMESTAMP	"when"
99 #define	ZONE_CB_ZONEID		"zoneid"
100 
101 #ifdef _SYSCALL32
102 typedef struct {
103 	caddr32_t zone_name;
104 	caddr32_t zone_root;
105 	caddr32_t zone_privs;
106 	size32_t zone_privssz;
107 	caddr32_t rctlbuf;
108 	size32_t rctlbufsz;
109 	caddr32_t extended_error;
110 	caddr32_t zfsbuf;
111 	size32_t  zfsbufsz;
112 	int match;
113 	int doi;
114 	caddr32_t label;
115 } zone_def32;
116 #endif
117 typedef struct {
118 	const char *zone_name;
119 	const char *zone_root;
120 	const struct priv_set *zone_privs;
121 	size_t zone_privssz;
122 	const char *rctlbuf;
123 	size_t rctlbufsz;
124 	int *extended_error;
125 	const char *zfsbuf;
126 	size_t zfsbufsz;
127 	int match;			/* match level */
128 	int doi;			/* DOI for label */
129 	const bslabel_t *label;		/* label associated with zone */
130 } zone_def;
131 
132 /* extended error information */
133 #define	ZE_UNKNOWN	0	/* No extended error info */
134 #define	ZE_CHROOTED	1	/* tried to zone_create from chroot */
135 #define	ZE_AREMOUNTS	2	/* there are mounts within the zone */
136 
137 /* zone_status */
138 typedef enum {
139 	ZONE_IS_UNINITIALIZED = 0,
140 	ZONE_IS_READY,
141 	ZONE_IS_BOOTING,
142 	ZONE_IS_RUNNING,
143 	ZONE_IS_SHUTTING_DOWN,
144 	ZONE_IS_EMPTY,
145 	ZONE_IS_DOWN,
146 	ZONE_IS_DYING,
147 	ZONE_IS_DEAD
148 } zone_status_t;
149 #define	ZONE_MIN_STATE		ZONE_IS_UNINITIALIZED
150 #define	ZONE_MAX_STATE		ZONE_IS_DEAD
151 
152 /*
153  * Valid commands which may be issued by zoneadm to zoneadmd.  The kernel also
154  * communicates with zoneadmd, but only uses Z_REBOOT and Z_HALT.
155  */
156 typedef enum zone_cmd {
157 	Z_READY, Z_BOOT, Z_REBOOT, Z_HALT, Z_NOTE_UNINSTALLING,
158 	Z_MOUNT, Z_UNMOUNT
159 } zone_cmd_t;
160 
161 #define	ZONEBOOTARGS_MAX	257	/* uadmin()'s buffer is 257 bytes. */
162 
163 /*
164  * The structure of a request to zoneadmd.
165  */
166 typedef struct zone_cmd_arg {
167 	uint64_t	uniqid;		/* unique "generation number" */
168 	zone_cmd_t	cmd;		/* requested action */
169 	char locale[MAXPATHLEN];	/* locale in which to render messages */
170 	char bootbuf[ZONEBOOTARGS_MAX];	/* arguments passed to zone_boot() */
171 } zone_cmd_arg_t;
172 
173 /*
174  * Structure of zoneadmd's response to a request.  A NULL return value means
175  * the caller should attempt to restart zoneadmd and retry.
176  */
177 typedef struct zone_cmd_rval {
178 	int rval;			/* return value of request */
179 	char errbuf[1];	/* variable-sized buffer containing error messages */
180 } zone_cmd_rval_t;
181 
182 /*
183  * The zone support infrastructure uses the zone name as a component
184  * of unix domain (AF_UNIX) sockets, which are limited to 108 characters
185  * in length, so ZONENAME_MAX is limited by that.
186  */
187 #define	ZONENAME_MAX		64
188 
189 #define	GLOBAL_ZONENAME		"global"
190 
191 /*
192  * Extended Regular expression (see regex(5)) which matches all valid zone
193  * names.
194  */
195 #define	ZONENAME_REGEXP		"[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}"
196 
197 /*
198  * Where the zones support infrastructure places temporary files.
199  */
200 #define	ZONES_TMPDIR		"/var/run/zones"
201 
202 /*
203  * The path to the door used by clients to communicate with zoneadmd.
204  */
205 #define	ZONE_DOOR_PATH		ZONES_TMPDIR "/%s.zoneadmd_door"
206 
207 #ifdef _KERNEL
208 /*
209  * We need to protect the definition of 'list_t' from userland applications and
210  * libraries which may be defining ther own versions.
211  */
212 #include <sys/list.h>
213 
214 #define	GLOBAL_ZONEUNIQID	0	/* uniqid of the global zone */
215 
216 /* zone_flags */
217 #define	ZF_DESTROYED		0x1	/* ZSD destructor callbacks run */
218 
219 struct pool;
220 
221 /*
222  * Structure to record list of ZFS datasets exported to a zone.
223  */
224 typedef struct zone_dataset {
225 	char		*zd_dataset;
226 	list_node_t	zd_linkage;
227 } zone_dataset_t;
228 
229 typedef struct zone {
230 	/*
231 	 * zone_name is never modified once set.
232 	 */
233 	char		*zone_name;	/* zone's configuration name */
234 	/*
235 	 * zone_nodename and zone_domain are never freed once allocated.
236 	 */
237 	char		*zone_nodename;	/* utsname.nodename equivalent */
238 	char		*zone_domain;	/* srpc_domain equivalent */
239 	/*
240 	 * zone_lock protects the following fields of a zone_t:
241 	 * 	zone_ref
242 	 * 	zone_cred_ref
243 	 * 	zone_ntasks
244 	 * 	zone_flags
245 	 * 	zone_zsd
246 	 */
247 	kmutex_t	zone_lock;
248 	/*
249 	 * zone_linkage is the zone's linkage into the active or
250 	 * death-row list.  The field is protected by zonehash_lock.
251 	 */
252 	list_node_t	zone_linkage;
253 	zoneid_t	zone_id;	/* ID of zone */
254 	uint_t		zone_ref;	/* count of zone_hold()s on zone */
255 	uint_t		zone_cred_ref;	/* count of zone_hold_cred()s on zone */
256 	/*
257 	 * zone_rootvp and zone_rootpath can never be modified once set.
258 	 */
259 	struct vnode	*zone_rootvp;	/* zone's root vnode */
260 	char		*zone_rootpath;	/* Path to zone's root + '/' */
261 	ushort_t	zone_flags;	/* misc flags */
262 	zone_status_t	zone_status;	/* protected by zone_status_lock */
263 	uint_t		zone_ntasks;	/* number of tasks executing in zone */
264 	kmutex_t	zone_nlwps_lock; /* protects zone_nlwps, and *_nlwps */
265 					/* counters in projects and tasks */
266 					/* that are within the zone */
267 	rctl_qty_t	zone_nlwps;	/* number of lwps in zone */
268 	rctl_qty_t	zone_nlwps_ctl; /* protected by zone_rctls->rcs_lock */
269 
270 	uint_t		zone_rootpathlen; /* strlen(zone_rootpath) + 1 */
271 	uint32_t	zone_shares;	/* FSS shares allocated to zone */
272 	rctl_set_t	*zone_rctls;	/* zone-wide (zone.*) rctls */
273 	list_t		zone_zsd;	/* list of Zone-Specific Data values */
274 	kcondvar_t	zone_cv;	/* used to signal state changes */
275 	struct proc	*zone_zsched;	/* Dummy kernel "zsched" process */
276 	pid_t		zone_proc_initpid; /* pid of "init" for this zone */
277 	int		zone_boot_err;  /* for zone_boot() if boot fails */
278 	char		*zone_bootargs;	/* arguments passed via zone_boot() */
279 	/*
280 	 * zone_kthreads is protected by zone_status_lock.
281 	 */
282 	kthread_t	*zone_kthreads;	/* kernel threads in zone */
283 	struct priv_set	*zone_privset;	/* limit set for zone */
284 	/*
285 	 * zone_vfslist is protected by vfs_list_lock().
286 	 */
287 	struct vfs	*zone_vfslist;	/* list of FS's mounted in zone */
288 	uint64_t	zone_uniqid;	/* unique zone generation number */
289 	struct cred	*zone_kcred;	/* kcred-like, zone-limited cred */
290 	/*
291 	 * zone_pool is protected by pool_lock().
292 	 */
293 	struct pool	*zone_pool;	/* pool the zone is bound to */
294 	hrtime_t	zone_pool_mod;	/* last pool bind modification time */
295 	/* zone_psetid is protected by cpu_lock */
296 	psetid_t	zone_psetid;	/* pset the zone is bound to */
297 	/*
298 	 * The following two can be read without holding any locks.  They are
299 	 * updated under cpu_lock.
300 	 */
301 	int		zone_ncpus;  /* zone's idea of ncpus */
302 	int		zone_ncpus_online; /* zone's idea of ncpus_online */
303 	/*
304 	 * List of ZFS datasets exported to this zone.
305 	 */
306 	list_t		zone_datasets;	/* list of datasets */
307 
308 	ts_label_t	*zone_slabel;
309 	int		zone_match;
310 	tsol_mlp_list_t zone_mlps;	/* MLPs on zone-private addresses */
311 } zone_t;
312 
313 /*
314  * Special value of zone_psetid to indicate that pools are disabled.
315  */
316 #define	ZONE_PS_INVAL	PS_MYID
317 
318 extern zone_t zone0;
319 extern zone_t *global_zone;
320 extern uint_t maxzones;
321 extern rctl_hndl_t rc_zone_nlwps;
322 
323 extern const char * const zone_initname;
324 
325 extern long zone(int, void *, void *, void *, void *);
326 extern void zone_zsd_init(void);
327 extern void zone_init(void);
328 extern void zone_hold(zone_t *);
329 extern void zone_rele(zone_t *);
330 extern void zone_cred_hold(zone_t *);
331 extern void zone_cred_rele(zone_t *);
332 extern void zone_task_hold(zone_t *);
333 extern void zone_task_rele(zone_t *);
334 extern zone_t *zone_find_by_id(zoneid_t);
335 extern zone_t *zone_find_by_label(const ts_label_t *);
336 extern zone_t *zone_find_by_name(char *);
337 extern zone_t *zone_find_by_path(const char *);
338 extern zone_t *zone_find_by_any_path(const char *, boolean_t);
339 extern zoneid_t getzoneid(void);
340 
341 /*
342  * Zone-specific data (ZSD) APIs
343  */
344 /*
345  * The following is what code should be initializing its zone_key_t to if it
346  * calls zone_getspecific() without necessarily knowing that zone_key_create()
347  * has been called on the key.
348  */
349 #define	ZONE_KEY_UNINITIALIZED	0
350 
351 typedef uint_t zone_key_t;
352 
353 extern void	zone_key_create(zone_key_t *, void *(*)(zoneid_t),
354     void (*)(zoneid_t, void *), void (*)(zoneid_t, void *));
355 extern int 	zone_key_delete(zone_key_t);
356 extern void	*zone_getspecific(zone_key_t, zone_t *);
357 extern int	zone_setspecific(zone_key_t, zone_t *, const void *);
358 
359 /*
360  * The definition of a zsd_entry is truly private to zone.c and is only
361  * placed here so it can be shared with mdb.
362  */
363 struct zsd_entry {
364 	zone_key_t		zsd_key;	/* Key used to lookup value */
365 	void			*zsd_data;	/* Caller-managed value */
366 	/*
367 	 * Callbacks to be executed when a zone is created, shutdown, and
368 	 * destroyed, respectively.
369 	 */
370 	void			*(*zsd_create)(zoneid_t);
371 	void			(*zsd_shutdown)(zoneid_t, void *);
372 	void			(*zsd_destroy)(zoneid_t, void *);
373 	list_node_t		zsd_linkage;
374 };
375 
376 /*
377  * Macros to help with zone visibility restrictions.
378  */
379 
380 /*
381  * Is process in the global zone?
382  */
383 #define	INGLOBALZONE(p) \
384 	((p)->p_zone == global_zone)
385 
386 /*
387  * Can process view objects in given zone?
388  */
389 #define	HASZONEACCESS(p, zoneid) \
390 	((p)->p_zone->zone_id == (zoneid) || INGLOBALZONE(p))
391 
392 /*
393  * Convenience macro to see if a resolved path is visible from within a
394  * given zone.
395  *
396  * The basic idea is that the first (zone_rootpathlen - 1) bytes of the
397  * two strings must be equal.  Since the rootpathlen has a trailing '/',
398  * we want to skip everything in the path up to (but not including) the
399  * trailing '/'.
400  */
401 #define	ZONE_PATH_VISIBLE(path, zone) \
402 	(strncmp((path), (zone)->zone_rootpath,		\
403 	    (zone)->zone_rootpathlen - 1) == 0)
404 
405 /*
406  * Convenience macro to go from the global view of a path to that seen
407  * from within said zone.  It is the responsibility of the caller to
408  * ensure that the path is a resolved one (ie, no '..'s or '.'s), and is
409  * in fact visible from within the zone.
410  */
411 #define	ZONE_PATH_TRANSLATE(path, zone)	\
412 	(ASSERT(ZONE_PATH_VISIBLE(path, zone)),	\
413 	(path) + (zone)->zone_rootpathlen - 2)
414 
415 /*
416  * Special processes visible in all zones.
417  */
418 #define	ZONE_SPECIALPID(x)	 ((x) == 0 || (x) == 1)
419 
420 /*
421  * Zone-safe version of thread_create() to be used when the caller wants to
422  * create a kernel thread to run within the current zone's context.
423  */
424 extern kthread_t *zthread_create(caddr_t, size_t, void (*)(), void *, size_t,
425     pri_t);
426 extern void zthread_exit(void);
427 
428 /*
429  * Functions for an external observer to register interest in a zone's status
430  * change.  Observers will be woken up when the zone status equals the status
431  * argument passed in (in the case of zone_status_timedwait, the function may
432  * also return because of a timeout; zone_status_wait_sig may return early due
433  * to a signal being delivered; zone_status_timedwait_sig may return for any of
434  * the above reasons).
435  *
436  * Otherwise these behave identically to cv_timedwait(), cv_wait(), and
437  * cv_wait_sig() respectively.
438  */
439 extern clock_t zone_status_timedwait(zone_t *, clock_t, zone_status_t);
440 extern clock_t zone_status_timedwait_sig(zone_t *, clock_t, zone_status_t);
441 extern void zone_status_wait(zone_t *, zone_status_t);
442 extern int zone_status_wait_sig(zone_t *, zone_status_t);
443 
444 /*
445  * Get the status  of the zone (at the time it was called).  The state may
446  * have progressed by the time it is returned.
447  */
448 extern zone_status_t zone_status_get(zone_t *);
449 
450 /*
451  * Get the "kcred" credentials corresponding to the given zone.
452  */
453 extern struct cred *zone_get_kcred(zoneid_t);
454 
455 /*
456  * Get/set the pool the zone is currently bound to.
457  */
458 extern struct pool *zone_pool_get(zone_t *);
459 extern void zone_pool_set(zone_t *, struct pool *);
460 
461 /*
462  * Get/set the pset the zone is currently using.
463  */
464 extern psetid_t zone_pset_get(zone_t *);
465 extern void zone_pset_set(zone_t *, psetid_t);
466 
467 /*
468  * Get the number of cpus/online-cpus visible from the given zone.
469  */
470 extern int zone_ncpus_get(zone_t *);
471 extern int zone_ncpus_online_get(zone_t *);
472 
473 /*
474  * Returns true if the named pool/dataset is visible in the current zone.
475  */
476 extern int zone_dataset_visible(const char *, int *);
477 
478 /*
479  * zone version of uadmin()
480  */
481 extern int zone_uadmin(int, int, struct cred *);
482 extern void zone_shutdown_global(void);
483 
484 extern void mount_in_progress(void);
485 extern void mount_completed(void);
486 
487 extern int zone_walk(int (*)(zone_t *, void *), void *);
488 
489 #endif	/* _KERNEL */
490 
491 #ifdef	__cplusplus
492 }
493 #endif
494 
495 #endif	/* _SYS_ZONE_H */
496