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