xref: /titanic_51/usr/src/uts/common/sys/zone.h (revision 1cc553493b17fa6a6770261bbfeb258f354ebf48)
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;			/* match level */
113 	uint32_t doi;			/* DOI for label */
114 	caddr32_t label;		/* label associated with zone */
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 	uint32_t 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 #define	ZF_HASHED_LABEL		0x2	/* zone has a unique label */
219 #define	ZF_IS_SCRATCH		0x4	/* scratch zone */
220 
221 struct pool;
222 
223 /*
224  * Structure to record list of ZFS datasets exported to a zone.
225  */
226 typedef struct zone_dataset {
227 	char		*zd_dataset;
228 	list_node_t	zd_linkage;
229 } zone_dataset_t;
230 
231 typedef struct zone {
232 	/*
233 	 * zone_name is never modified once set.
234 	 */
235 	char		*zone_name;	/* zone's configuration name */
236 	/*
237 	 * zone_nodename and zone_domain are never freed once allocated.
238 	 */
239 	char		*zone_nodename;	/* utsname.nodename equivalent */
240 	char		*zone_domain;	/* srpc_domain equivalent */
241 	/*
242 	 * zone_lock protects the following fields of a zone_t:
243 	 * 	zone_ref
244 	 * 	zone_cred_ref
245 	 * 	zone_ntasks
246 	 * 	zone_flags
247 	 * 	zone_zsd
248 	 */
249 	kmutex_t	zone_lock;
250 	/*
251 	 * zone_linkage is the zone's linkage into the active or
252 	 * death-row list.  The field is protected by zonehash_lock.
253 	 */
254 	list_node_t	zone_linkage;
255 	zoneid_t	zone_id;	/* ID of zone */
256 	uint_t		zone_ref;	/* count of zone_hold()s on zone */
257 	uint_t		zone_cred_ref;	/* count of zone_hold_cred()s on zone */
258 	/*
259 	 * zone_rootvp and zone_rootpath can never be modified once set.
260 	 */
261 	struct vnode	*zone_rootvp;	/* zone's root vnode */
262 	char		*zone_rootpath;	/* Path to zone's root + '/' */
263 	ushort_t	zone_flags;	/* misc flags */
264 	zone_status_t	zone_status;	/* protected by zone_status_lock */
265 	uint_t		zone_ntasks;	/* number of tasks executing in zone */
266 	kmutex_t	zone_nlwps_lock; /* protects zone_nlwps, and *_nlwps */
267 					/* counters in projects and tasks */
268 					/* that are within the zone */
269 	rctl_qty_t	zone_nlwps;	/* number of lwps in zone */
270 	rctl_qty_t	zone_nlwps_ctl; /* protected by zone_rctls->rcs_lock */
271 
272 	uint_t		zone_rootpathlen; /* strlen(zone_rootpath) + 1 */
273 	uint32_t	zone_shares;	/* FSS shares allocated to zone */
274 	rctl_set_t	*zone_rctls;	/* zone-wide (zone.*) rctls */
275 	list_t		zone_zsd;	/* list of Zone-Specific Data values */
276 	kcondvar_t	zone_cv;	/* used to signal state changes */
277 	struct proc	*zone_zsched;	/* Dummy kernel "zsched" process */
278 	pid_t		zone_proc_initpid; /* pid of "init" for this zone */
279 	int		zone_boot_err;  /* for zone_boot() if boot fails */
280 	char		*zone_bootargs;	/* arguments passed via zone_boot() */
281 	/*
282 	 * zone_kthreads is protected by zone_status_lock.
283 	 */
284 	kthread_t	*zone_kthreads;	/* kernel threads in zone */
285 	struct priv_set	*zone_privset;	/* limit set for zone */
286 	/*
287 	 * zone_vfslist is protected by vfs_list_lock().
288 	 */
289 	struct vfs	*zone_vfslist;	/* list of FS's mounted in zone */
290 	uint64_t	zone_uniqid;	/* unique zone generation number */
291 	struct cred	*zone_kcred;	/* kcred-like, zone-limited cred */
292 	/*
293 	 * zone_pool is protected by pool_lock().
294 	 */
295 	struct pool	*zone_pool;	/* pool the zone is bound to */
296 	hrtime_t	zone_pool_mod;	/* last pool bind modification time */
297 	/* zone_psetid is protected by cpu_lock */
298 	psetid_t	zone_psetid;	/* pset the zone is bound to */
299 	/*
300 	 * The following two can be read without holding any locks.  They are
301 	 * updated under cpu_lock.
302 	 */
303 	int		zone_ncpus;  /* zone's idea of ncpus */
304 	int		zone_ncpus_online; /* zone's idea of ncpus_online */
305 	/*
306 	 * List of ZFS datasets exported to this zone.
307 	 */
308 	list_t		zone_datasets;	/* list of datasets */
309 
310 	ts_label_t	*zone_slabel;	/* zone sensitivity label */
311 	int		zone_match;	/* require label match for packets */
312 	tsol_mlp_list_t zone_mlps;	/* MLPs on zone-private addresses */
313 } zone_t;
314 
315 /*
316  * Special value of zone_psetid to indicate that pools are disabled.
317  */
318 #define	ZONE_PS_INVAL	PS_MYID
319 
320 extern zone_t zone0;
321 extern zone_t *global_zone;
322 extern uint_t maxzones;
323 extern rctl_hndl_t rc_zone_nlwps;
324 
325 extern const char * const zone_initname;
326 
327 extern long zone(int, void *, void *, void *, void *);
328 extern void zone_zsd_init(void);
329 extern void zone_init(void);
330 extern void zone_hold(zone_t *);
331 extern void zone_rele(zone_t *);
332 extern void zone_cred_hold(zone_t *);
333 extern void zone_cred_rele(zone_t *);
334 extern void zone_task_hold(zone_t *);
335 extern void zone_task_rele(zone_t *);
336 extern zone_t *zone_find_by_id(zoneid_t);
337 extern zone_t *zone_find_by_label(const ts_label_t *);
338 extern zone_t *zone_find_by_name(char *);
339 extern zone_t *zone_find_by_any_path(const char *, boolean_t);
340 extern zone_t *zone_find_by_path(const char *);
341 extern zoneid_t getzoneid(void);
342 
343 /*
344  * Zone-specific data (ZSD) APIs
345  */
346 /*
347  * The following is what code should be initializing its zone_key_t to if it
348  * calls zone_getspecific() without necessarily knowing that zone_key_create()
349  * has been called on the key.
350  */
351 #define	ZONE_KEY_UNINITIALIZED	0
352 
353 typedef uint_t zone_key_t;
354 
355 extern void	zone_key_create(zone_key_t *, void *(*)(zoneid_t),
356     void (*)(zoneid_t, void *), void (*)(zoneid_t, void *));
357 extern int 	zone_key_delete(zone_key_t);
358 extern void	*zone_getspecific(zone_key_t, zone_t *);
359 extern int	zone_setspecific(zone_key_t, zone_t *, const void *);
360 
361 /*
362  * The definition of a zsd_entry is truly private to zone.c and is only
363  * placed here so it can be shared with mdb.
364  */
365 struct zsd_entry {
366 	zone_key_t		zsd_key;	/* Key used to lookup value */
367 	void			*zsd_data;	/* Caller-managed value */
368 	/*
369 	 * Callbacks to be executed when a zone is created, shutdown, and
370 	 * destroyed, respectively.
371 	 */
372 	void			*(*zsd_create)(zoneid_t);
373 	void			(*zsd_shutdown)(zoneid_t, void *);
374 	void			(*zsd_destroy)(zoneid_t, void *);
375 	list_node_t		zsd_linkage;
376 };
377 
378 /*
379  * Macros to help with zone visibility restrictions.
380  */
381 
382 /*
383  * Is process in the global zone?
384  */
385 #define	INGLOBALZONE(p) \
386 	((p)->p_zone == global_zone)
387 
388 /*
389  * Can process view objects in given zone?
390  */
391 #define	HASZONEACCESS(p, zoneid) \
392 	((p)->p_zone->zone_id == (zoneid) || INGLOBALZONE(p))
393 
394 /*
395  * Convenience macro to see if a resolved path is visible from within a
396  * given zone.
397  *
398  * The basic idea is that the first (zone_rootpathlen - 1) bytes of the
399  * two strings must be equal.  Since the rootpathlen has a trailing '/',
400  * we want to skip everything in the path up to (but not including) the
401  * trailing '/'.
402  */
403 #define	ZONE_PATH_VISIBLE(path, zone) \
404 	(strncmp((path), (zone)->zone_rootpath,		\
405 	    (zone)->zone_rootpathlen - 1) == 0)
406 
407 /*
408  * Convenience macro to go from the global view of a path to that seen
409  * from within said zone.  It is the responsibility of the caller to
410  * ensure that the path is a resolved one (ie, no '..'s or '.'s), and is
411  * in fact visible from within the zone.
412  */
413 #define	ZONE_PATH_TRANSLATE(path, zone)	\
414 	(ASSERT(ZONE_PATH_VISIBLE(path, zone)),	\
415 	(path) + (zone)->zone_rootpathlen - 2)
416 
417 /*
418  * Special processes visible in all zones.
419  */
420 #define	ZONE_SPECIALPID(x)	 ((x) == 0 || (x) == 1)
421 
422 /*
423  * Zone-safe version of thread_create() to be used when the caller wants to
424  * create a kernel thread to run within the current zone's context.
425  */
426 extern kthread_t *zthread_create(caddr_t, size_t, void (*)(), void *, size_t,
427     pri_t);
428 extern void zthread_exit(void);
429 
430 /*
431  * Functions for an external observer to register interest in a zone's status
432  * change.  Observers will be woken up when the zone status equals the status
433  * argument passed in (in the case of zone_status_timedwait, the function may
434  * also return because of a timeout; zone_status_wait_sig may return early due
435  * to a signal being delivered; zone_status_timedwait_sig may return for any of
436  * the above reasons).
437  *
438  * Otherwise these behave identically to cv_timedwait(), cv_wait(), and
439  * cv_wait_sig() respectively.
440  */
441 extern clock_t zone_status_timedwait(zone_t *, clock_t, zone_status_t);
442 extern clock_t zone_status_timedwait_sig(zone_t *, clock_t, zone_status_t);
443 extern void zone_status_wait(zone_t *, zone_status_t);
444 extern int zone_status_wait_sig(zone_t *, zone_status_t);
445 
446 /*
447  * Get the status  of the zone (at the time it was called).  The state may
448  * have progressed by the time it is returned.
449  */
450 extern zone_status_t zone_status_get(zone_t *);
451 
452 /*
453  * Get the "kcred" credentials corresponding to the given zone.
454  */
455 extern struct cred *zone_get_kcred(zoneid_t);
456 
457 /*
458  * Get/set the pool the zone is currently bound to.
459  */
460 extern struct pool *zone_pool_get(zone_t *);
461 extern void zone_pool_set(zone_t *, struct pool *);
462 
463 /*
464  * Get/set the pset the zone is currently using.
465  */
466 extern psetid_t zone_pset_get(zone_t *);
467 extern void zone_pset_set(zone_t *, psetid_t);
468 
469 /*
470  * Get the number of cpus/online-cpus visible from the given zone.
471  */
472 extern int zone_ncpus_get(zone_t *);
473 extern int zone_ncpus_online_get(zone_t *);
474 
475 /*
476  * Returns true if the named pool/dataset is visible in the current zone.
477  */
478 extern int zone_dataset_visible(const char *, int *);
479 
480 /*
481  * zone version of uadmin()
482  */
483 extern int zone_uadmin(int, int, struct cred *);
484 extern void zone_shutdown_global(void);
485 
486 extern void mount_in_progress(void);
487 extern void mount_completed(void);
488 
489 extern int zone_walk(int (*)(zone_t *, void *), void *);
490 
491 #endif	/* _KERNEL */
492 
493 #ifdef	__cplusplus
494 }
495 #endif
496 
497 #endif	/* _SYS_ZONE_H */
498