xref: /onnv-gate/usr/src/uts/common/sys/hook_impl.h (revision 12263:18746354f914)
12958Sdr146992 /*
22958Sdr146992  * CDDL HEADER START
32958Sdr146992  *
42958Sdr146992  * The contents of this file are subject to the terms of the
52958Sdr146992  * Common Development and Distribution License (the "License").
62958Sdr146992  * You may not use this file except in compliance with the License.
72958Sdr146992  *
82958Sdr146992  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92958Sdr146992  * or http://www.opensolaris.org/os/licensing.
102958Sdr146992  * See the License for the specific language governing permissions
112958Sdr146992  * and limitations under the License.
122958Sdr146992  *
132958Sdr146992  * When distributing Covered Code, include this CDDL HEADER in each
142958Sdr146992  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152958Sdr146992  * If applicable, add the following below this CDDL HEADER, with the
162958Sdr146992  * fields enclosed by brackets "[]" replaced with your own identifying
172958Sdr146992  * information: Portions Copyright [yyyy] [name of copyright owner]
182958Sdr146992  *
192958Sdr146992  * CDDL HEADER END
202958Sdr146992  */
212958Sdr146992 /*
227513SDarren.Reed@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
232958Sdr146992  * Use is subject to license terms.
242958Sdr146992  */
252958Sdr146992 
262958Sdr146992 /*
272958Sdr146992  * This file include internal used definition and data structure of hooks
282958Sdr146992  */
292958Sdr146992 
302958Sdr146992 #ifndef _SYS_HOOK_IMPL_H
312958Sdr146992 #define	_SYS_HOOK_IMPL_H
322958Sdr146992 
332958Sdr146992 #include <sys/hook.h>
342958Sdr146992 #include <sys/condvar_impl.h>
353448Sdh155122 #include <sys/netstack.h>
362958Sdr146992 
372958Sdr146992 #ifdef	__cplusplus
382958Sdr146992 extern "C" {
392958Sdr146992 #endif
402958Sdr146992 
417513SDarren.Reed@Sun.COM typedef enum fwflag_e {
427513SDarren.Reed@Sun.COM 	FWF_NONE		= 0x00,
437513SDarren.Reed@Sun.COM 	FWF_DESTROY_ACTIVE	= 0x01,
447513SDarren.Reed@Sun.COM 	FWF_ADD_ACTIVE		= 0x04,
457513SDarren.Reed@Sun.COM 	FWF_DEL_ACTIVE		= 0x08,
467513SDarren.Reed@Sun.COM 	FWF_DESTROY_WANTED	= 0x10,
477513SDarren.Reed@Sun.COM 	FWF_ADD_WANTED		= 0x40,
487513SDarren.Reed@Sun.COM 	FWF_DEL_WANTED		= 0x80,
497513SDarren.Reed@Sun.COM 	FWF_NOT_READY		= 0x100
507513SDarren.Reed@Sun.COM } fwflag_t;
517513SDarren.Reed@Sun.COM 
52*12263SDarren.Reed@Sun.COM #define	FWF_ADD_WAIT_MASK	(FWF_ADD_ACTIVE|FWF_DEL_ACTIVE|FWF_ADD_WANTED)
53*12263SDarren.Reed@Sun.COM #define	FWF_DEL_WAIT_MASK	(FWF_ADD_ACTIVE|FWF_DEL_ACTIVE|\
547513SDarren.Reed@Sun.COM     FWF_ADD_WANTED|FWF_DEL_WANTED)
557513SDarren.Reed@Sun.COM #define	FWF_UNSAFE		(FWF_DESTROY_ACTIVE|FWF_NOT_READY)
567513SDarren.Reed@Sun.COM #define	FWF_DESTROY		(FWF_DESTROY_ACTIVE|FWF_DESTROY_WANTED)
577513SDarren.Reed@Sun.COM #define	FWF_DESTROY_OK(x)	((x)->fw_flags == FWF_DESTROY_WANTED)
587513SDarren.Reed@Sun.COM 
597513SDarren.Reed@Sun.COM typedef struct flagwait_s {
607513SDarren.Reed@Sun.COM 	kcondvar_t	fw_cv;
617513SDarren.Reed@Sun.COM 	kmutex_t	fw_lock;
627513SDarren.Reed@Sun.COM 	uint32_t	fw_flags;
637513SDarren.Reed@Sun.COM 	cvwaitlock_t	*fw_owner;
647513SDarren.Reed@Sun.COM } flagwait_t;
657513SDarren.Reed@Sun.COM 
667513SDarren.Reed@Sun.COM 
672958Sdr146992 /*
682958Sdr146992  * The following diagram describes the linking together of data structures
692958Sdr146992  * used in this implementation of callback hooks.  The start of it all is
702958Sdr146992  * the "familylist" variable in hook.c.  The relationships between data
712958Sdr146992  * structures is:
722958Sdr146992  * - there is a list of hook families;
732958Sdr146992  * - each hook family can have a list of hook events;
742958Sdr146992  * - each hook_event_t must be uniquely associated with one family and event;
752958Sdr146992  * - each hook event can have a list of registered hooks to call.
762958Sdr146992  *
772958Sdr146992  *   familylist                    +--------------+
782958Sdr146992  *       |                         | hook_event_t |<--\
792958Sdr146992  *       |                         +--------------+   |
802958Sdr146992  *       V                                            |
812958Sdr146992  * +-------------------+       ->+------------------+ |     ->+--------------+
822958Sdr146992  * | hook_family_int_t |      /  | hook_event_int_t | |    /  | hook_int_t   |
832958Sdr146992  * | +---------------+ |     /   |                  | /   /   | +----------+ |
842958Sdr146992  * | | hook_family_t | |    /    | hei_event---------/   /    | | hook_t   | |
857513SDarren.Reed@Sun.COM  * | +---------------+ |   /     | hei_nhead----------\ /     | +----------+ |
867513SDarren.Reed@Sun.COM  * |                   |  /      |                  |  X      |              |
877513SDarren.Reed@Sun.COM  * | hfi_head------------/       | hei_head-----------/ \     | hi_entry--\  |
887513SDarren.Reed@Sun.COM  * | hfi_entry--\      |         | hei_entry--\     |   |     +-----------|--+
897513SDarren.Reed@Sun.COM  * +------------|------+         +------------|-----+   |                 |
907513SDarren.Reed@Sun.COM  *              |                             |         |                 |
917513SDarren.Reed@Sun.COM  *              V                             V         |                 V
927513SDarren.Reed@Sun.COM  * +-------------------+         +------------------+   |     +--------------+
937513SDarren.Reed@Sun.COM  * | hook_family_int_t |         | hook_event_int_t |   |     | hook_int_t   |
947513SDarren.Reed@Sun.COM  *                                                      V
957513SDarren.Reed@Sun.COM  *                                             +--------------+
967513SDarren.Reed@Sun.COM  *                                             |
972958Sdr146992  * ...
982958Sdr146992  */
992958Sdr146992 
1007513SDarren.Reed@Sun.COM typedef struct hook_hook_kstat {
1017513SDarren.Reed@Sun.COM 	kstat_named_t			hook_version;
1027513SDarren.Reed@Sun.COM 	kstat_named_t			hook_flags;
1037513SDarren.Reed@Sun.COM 	kstat_named_t			hook_hint;
1047513SDarren.Reed@Sun.COM 	kstat_named_t			hook_hintvalue;
1057513SDarren.Reed@Sun.COM 	kstat_named_t			hook_position;
1067513SDarren.Reed@Sun.COM 	kstat_named_t			hook_hits;
1077513SDarren.Reed@Sun.COM } hook_hook_kstat_t;
1087513SDarren.Reed@Sun.COM 
1092958Sdr146992 /*
1102958Sdr146992  * hook_int: internal storage of hook
1112958Sdr146992  */
1122958Sdr146992 typedef struct hook_int {
1137513SDarren.Reed@Sun.COM 	TAILQ_ENTRY(hook_int)		hi_entry;
1147513SDarren.Reed@Sun.COM 	hook_t				hi_hook;
1157513SDarren.Reed@Sun.COM 	hook_hook_kstat_t		hi_kstats;
1167513SDarren.Reed@Sun.COM 	kstat_t				*hi_kstatp;
1177513SDarren.Reed@Sun.COM 	char				*hi_ksname;
1187513SDarren.Reed@Sun.COM 	cvwaitlock_t			hi_notify_lock;
1192958Sdr146992 } hook_int_t;
1202958Sdr146992 
1212958Sdr146992 /*
1227513SDarren.Reed@Sun.COM  * hook_int_head: tail queue of hook_int
1232958Sdr146992  */
1242958Sdr146992 TAILQ_HEAD(hook_int_head, hook_int);
1252958Sdr146992 typedef struct hook_int_head hook_int_head_t;
1262958Sdr146992 
1277513SDarren.Reed@Sun.COM 
1287513SDarren.Reed@Sun.COM typedef struct hook_notify {
1297513SDarren.Reed@Sun.COM 	TAILQ_ENTRY(hook_notify)	hn_entry;
1307513SDarren.Reed@Sun.COM 	hook_notify_fn_t		hn_func;
1317513SDarren.Reed@Sun.COM 	void				*hn_arg;
1327513SDarren.Reed@Sun.COM 	uint32_t			hn_flags;
1337513SDarren.Reed@Sun.COM } hook_notify_t;
1347513SDarren.Reed@Sun.COM 
1357513SDarren.Reed@Sun.COM TAILQ_HEAD(hook_notify_head, hook_notify);
1367513SDarren.Reed@Sun.COM typedef struct hook_notify_head hook_notify_head_t;
1377513SDarren.Reed@Sun.COM 
1387513SDarren.Reed@Sun.COM 
1397513SDarren.Reed@Sun.COM typedef struct hook_event_kstat {
1407513SDarren.Reed@Sun.COM 	kstat_named_t			hooks_added;
1417513SDarren.Reed@Sun.COM 	kstat_named_t			hooks_removed;
1427513SDarren.Reed@Sun.COM 	kstat_named_t			events;
1437513SDarren.Reed@Sun.COM } hook_event_kstat_t;
1447513SDarren.Reed@Sun.COM 
1452958Sdr146992 /*
1462958Sdr146992  * hook_event_int: internal storage of hook_event
1472958Sdr146992  */
1482958Sdr146992 typedef struct hook_event_int {
1492958Sdr146992 	cvwaitlock_t			hei_lock;
1502958Sdr146992 	SLIST_ENTRY(hook_event_int)	hei_entry;
1512958Sdr146992 	hook_event_t			*hei_event;
1522958Sdr146992 	hook_int_head_t			hei_head;
1537513SDarren.Reed@Sun.COM 	kstat_t				*hei_kstatp;
1547513SDarren.Reed@Sun.COM 	hook_event_kstat_t		hei_kstats;
1557513SDarren.Reed@Sun.COM 	hook_notify_head_t		hei_nhead;
1567513SDarren.Reed@Sun.COM 	flagwait_t			hei_waiter;
1577513SDarren.Reed@Sun.COM 	boolean_t			hei_condemned;
1587915SDarren.Reed@Sun.COM 	boolean_t			hei_shutdown;
1592958Sdr146992 } hook_event_int_t;
1602958Sdr146992 
1612958Sdr146992 /*
1622958Sdr146992  * hook_event_int_head: singly-linked list of hook_event_int
1632958Sdr146992  */
1642958Sdr146992 SLIST_HEAD(hook_event_int_head, hook_event_int);
1652958Sdr146992 typedef struct hook_event_int_head hook_event_int_head_t;
1662958Sdr146992 
1672958Sdr146992 /*
1682958Sdr146992  * hook_family_int: internal storage of hook_family
1692958Sdr146992  */
1702958Sdr146992 typedef struct hook_family_int {
1717513SDarren.Reed@Sun.COM 	cvwaitlock_t			hfi_lock;
1722958Sdr146992 	SLIST_ENTRY(hook_family_int)	hfi_entry;
1732958Sdr146992 	hook_event_int_head_t		hfi_head;
1742958Sdr146992 	hook_family_t 			hfi_family;
1757513SDarren.Reed@Sun.COM 	kstat_t				*hfi_kstat;
1767513SDarren.Reed@Sun.COM 	struct hook_stack		*hfi_stack;
1777513SDarren.Reed@Sun.COM 	hook_notify_head_t		hfi_nhead;
1787513SDarren.Reed@Sun.COM 	flagwait_t			hfi_waiter;
1797513SDarren.Reed@Sun.COM 	boolean_t			hfi_condemned;
1807915SDarren.Reed@Sun.COM 	boolean_t			hfi_shutdown;
1812958Sdr146992 } hook_family_int_t;
1822958Sdr146992 
1832958Sdr146992 /*
1842958Sdr146992  * hook_family_int_head: singly-linked list of hook_family
1852958Sdr146992  */
1862958Sdr146992 SLIST_HEAD(hook_family_int_head, hook_family_int);
1872958Sdr146992 typedef struct hook_family_int_head hook_family_int_head_t;
1882958Sdr146992 
1892958Sdr146992 /*
1903448Sdh155122  * hook stack instances
1913448Sdh155122  */
1923448Sdh155122 struct hook_stack {
1937513SDarren.Reed@Sun.COM 	cvwaitlock_t			hks_lock;
1947513SDarren.Reed@Sun.COM 	SLIST_ENTRY(hook_stack)		hks_entry;
1957513SDarren.Reed@Sun.COM 	hook_family_int_head_t		hks_familylist;	/* family list head */
1967513SDarren.Reed@Sun.COM 	netstack_t			*hks_netstack;
1977513SDarren.Reed@Sun.COM 	netstackid_t			hks_netstackid;
1987513SDarren.Reed@Sun.COM 	hook_notify_head_t		hks_nhead;
1997513SDarren.Reed@Sun.COM 	int				hks_shutdown;
2007513SDarren.Reed@Sun.COM 	flagwait_t			hks_waiter;
2013448Sdh155122 };
2023448Sdh155122 typedef struct hook_stack hook_stack_t;
2037513SDarren.Reed@Sun.COM SLIST_HEAD(hook_stack_head, hook_stack);
2047513SDarren.Reed@Sun.COM typedef struct hook_stack_head hook_stack_head_t;
2053448Sdh155122 
2063448Sdh155122 /*
2072958Sdr146992  * Names of hooks families currently defined by Solaris
2082958Sdr146992  */
2092958Sdr146992 #define	Hn_ARP	"arp"
2102958Sdr146992 #define	Hn_IPV4	"inet"
2112958Sdr146992 #define	Hn_IPV6	"inet6"
2122958Sdr146992 
2137513SDarren.Reed@Sun.COM extern int hook_run(hook_family_int_t *, hook_event_token_t, hook_data_t);
2147513SDarren.Reed@Sun.COM extern int hook_register(hook_family_int_t *, char *, hook_t *);
2157513SDarren.Reed@Sun.COM 
2167513SDarren.Reed@Sun.COM extern int hook_unregister(hook_family_int_t *, char *, hook_t *);
2172958Sdr146992 extern hook_event_int_t *hook_event_add(hook_family_int_t *, hook_event_t *);
2187513SDarren.Reed@Sun.COM extern int hook_event_notify_register(hook_family_int_t *, char *,
2197513SDarren.Reed@Sun.COM     hook_notify_fn_t, void *);
2207513SDarren.Reed@Sun.COM extern int hook_event_notify_unregister(hook_family_int_t *, char *,
2217513SDarren.Reed@Sun.COM     hook_notify_fn_t);
2222958Sdr146992 extern int hook_event_remove(hook_family_int_t *, hook_event_t *);
2237915SDarren.Reed@Sun.COM extern int hook_event_shutdown(hook_family_int_t *, hook_event_t *);
2247513SDarren.Reed@Sun.COM 
225*12263SDarren.Reed@Sun.COM extern hook_family_int_t *hook_family_add(hook_family_t *, hook_stack_t *,
226*12263SDarren.Reed@Sun.COM     void **);
2277513SDarren.Reed@Sun.COM extern int hook_family_notify_register(hook_family_int_t *, hook_notify_fn_t,
2287513SDarren.Reed@Sun.COM     void *);
2297513SDarren.Reed@Sun.COM extern int hook_family_notify_unregister(hook_family_int_t *, hook_notify_fn_t);
2307513SDarren.Reed@Sun.COM extern int hook_family_remove(hook_family_int_t *);
2317915SDarren.Reed@Sun.COM extern int hook_family_shutdown(hook_family_int_t *);
2327513SDarren.Reed@Sun.COM 
2337513SDarren.Reed@Sun.COM extern int hook_stack_notify_register(netstackid_t, hook_notify_fn_t, void *);
2347513SDarren.Reed@Sun.COM extern int hook_stack_notify_unregister(netstackid_t, hook_notify_fn_t);
2357513SDarren.Reed@Sun.COM 
2362958Sdr146992 
2372958Sdr146992 #ifdef	__cplusplus
2382958Sdr146992 }
2392958Sdr146992 #endif
2402958Sdr146992 
2412958Sdr146992 #endif /* _SYS_HOOK_IMPL_H */
242