xref: /titanic_52/usr/src/uts/common/sys/hook.h (revision d14abf155341d55053c76eeec58b787a456b753b)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file includes definitions of kernel hook framework components
28  */
29 
30 #ifndef _SYS_HOOK_H
31 #define	_SYS_HOOK_H
32 
33 #include <sys/queue.h>
34 #include <sys/netstack.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Definition exposed to hook provider and consumer
42  */
43 
44 #define	HOOK_VERSION	1
45 
46 typedef uintptr_t hook_data_t;
47 
48 struct hook_event_int;
49 typedef struct hook_event_int *hook_event_token_t;
50 struct hook_int;
51 typedef struct hook_int *hook_token_t;
52 
53 typedef int (* hook_func_t)(hook_event_token_t, hook_data_t, void *);
54 
55 /*
56  * A hook_notify_cmd_t is given as an argument to functions called as part of
57  * the notify callbacks that have been registered firing.
58  */
59 typedef enum hook_notify_cmd_e {
60 	HN_NONE = 0,
61 	HN_REGISTER = 1,
62 	HN_UNREGISTER = 2
63 } hook_notify_cmd_t;
64 
65 /*
66  *
67  */
68 typedef enum hook_hint_e {
69 	HH_NONE = 0,
70 	HH_FIRST,
71 	HH_LAST,
72 	HH_BEFORE,
73 	HH_AFTER
74 } hook_hint_t;
75 
76 /*
77  * Hook
78  */
79 typedef struct hook_s {
80 	int		h_version;
81 	hook_func_t	h_func;		/* callback func */
82 	char		*h_name;	/* name of this hook */
83 	uint_t		h_flags;	/* extra hook properties */
84 	hook_hint_t	h_hint;		/* What type of hint is hintvalue */
85 	uintptr_t	h_hintvalue;
86 	void		*h_arg;		/* value to pass back into the hook */
87 } hook_t;
88 
89 #define	HOOK_INIT(x, fn, r, a)		\
90 	do {					\
91 		(x) = hook_alloc(HOOK_VERSION);	\
92 		(x)->h_func = (fn);		\
93 		(x)->h_name = (r);		\
94 		(x)->h_flags = 0;		\
95 		(x)->h_hint = HH_NONE;		\
96 		(x)->h_hintvalue = 0;		\
97 		(x)->h_arg = (a);		\
98 		_NOTE(CONSTCOND)		\
99 	} while (0)
100 
101 /*
102  * Family
103  */
104 typedef struct hook_family_s {
105 	int		hf_version;	/* version number */
106 	char		*hf_name;	/* family name */
107 } hook_family_t;
108 
109 #define	HOOK_FAMILY_INIT(x, y)			\
110 	do {					\
111 		(x)->hf_version = HOOK_VERSION;	\
112 		(x)->hf_name = (y);		\
113 		_NOTE(CONSTCOND)		\
114 	} while (0)
115 
116 /*
117  * Event
118  */
119 typedef struct hook_event_s {
120 	int		he_version;
121 	char		*he_name;	/* name of this hook list */
122 	int		he_flags;	/* 1 = multiple entries allowed */
123 	boolean_t	he_interested;	/* true if callback exist */
124 } hook_event_t;
125 
126 #define	HOOK_RDONLY	0x1		/* Callbacks must not change data */
127 					/* Multiple callbacks are allowed */
128 
129 #define	HOOK_EVENT_INIT(x, y)			\
130 	do {					\
131 		(x)->he_version = HOOK_VERSION;	\
132 		(x)->he_name = (y);		\
133 		(x)->he_flags = 0;		\
134 		(x)->he_interested = B_FALSE;	\
135 		_NOTE(CONSTCOND)		\
136 	} while (0)
137 
138 typedef int (* hook_notify_fn_t)(hook_notify_cmd_t, void *, const char *,
139     const char *, const char *);
140 
141 extern	hook_t		*hook_alloc(const int version);
142 extern	void		hook_free(hook_t *);
143 
144 #ifdef	__cplusplus
145 }
146 #endif
147 
148 #endif /* _SYS_HOOK_H */
149