2 .\" Copyright (c) 2010-2011 The FreeBSD Foundation
4 .\" This documentation was written at the Centre for Advanced Internet
5 .\" Architectures, Swinburne University of Technology, Melbourne, Australia by
6 .\" David Hayes and Lawrence Stewart under sponsorship from the FreeBSD
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 .\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 .Nm hhook_head_register ,
38 .Nm hhook_head_deregister ,
39 .Nm hhook_head_deregister_lookup ,
42 .Nm HHOOKS_RUN_LOOKUP_IF
43 .Nd Helper Hook Framework
47 .Fn "\*(lp*hhook_func_t\*(rp" "int32_t hhook_type" "int32_t hhook_id" \
48 "void *udata" "void *ctx_data" "void *hdata" "struct osd *hosd"
49 .Fn "int hhook_head_register" "int32_t hhook_type" "int32_t hhook_id" \
50 "struct hhook_head **hhh" "uint32_t flags"
51 .Fn "int hhook_head_deregister" "struct hhook_head *hhh"
52 .Fn "int hhook_head_deregister_lookup" "int32_t hhook_type" "int32_t hhook_id"
53 .Fn "void hhook_run_hooks" "struct hhook_head *hhh" "void *ctx_data" \
55 .Fn HHOOKS_RUN_IF "hhh" "ctx_data" "hosd"
56 .Fn HHOOKS_RUN_LOOKUP_IF "hhook_type" "hhook_id" "ctx_data" "hosd"
59 provides a framework for managing and running arbitrary hook functions at
60 defined hook points within the kernel.
61 The KPI was inspired by
63 and in many respects can be thought of as a more generic superset of pfil.
69 frameworks are tightly integrated.
70 Khelp is responsible for registering and deregistering Khelp module hook
74 The KPI functions used by
76 to do this are not documented here as they are not relevant to consumers wishing
77 to instantiate hook points.
78 .Ss Information for Khelp Module Implementors
79 Khelp modules indirectly interact with
81 by defining appropriate hook functions for insertion into hook points.
82 Hook functions must conform to the
84 function pointer declaration
92 arguments identify the hook point which has called into the hook function.
93 These are useful when a single hook function is registered for multiple hook
94 points and wants to know which hook point has called into it.
98 defines and subsystems which export hook points are responsible for defining
101 value in appropriate header files.
105 argument will be passed to the hook function if it was specified in the
107 at hook registration time.
111 argument contains context specific data from the hook point call site.
112 The data type passed is subsystem dependent.
116 argument is a pointer to the persistent per-object storage allocated for use by
117 the module if required.
118 The pointer will only ever be NULL if the module did not request per-object
123 argument can be used with the
127 function to access data belonging to a different Khelp module.
129 Khelp modules instruct the Khelp framework to register their hook functions with
132 .Vt "struct hookinfo"
133 per hook point, which contains the following members:
134 .Bd -literal -offset indent
136 hhook_func_t hook_func;
137 struct helper *hook_helper;
144 Khelp modules are responsible for setting all members of the struct except
146 which is handled by the Khelp framework.
147 .Ss Creating and Managing Hook Points
148 Kernel subsystems that wish to provide
150 points typically need to make four and possibly five key changes to their
156 mappings in an appropriate subsystem header.
158 Register each hook point with the
159 .Fn hhook_head_register
160 function during initialisation of the subsystem.
162 Select or create a standardised data type to pass to hook functions as
168 .Fn HHOOKS_RUN_IF_LOOKUP
169 at the point in the subsystem's code where the hook point should be executed.
171 If the subsystem can be dynamically added/removed at runtime, each hook
172 point registered with the
173 .Fn hhook_head_register
174 function when the subsystem was initialised needs to be deregistered with the
175 .Fn hhook_head_deregister
177 .Fn hhook_head_deregister_lookup
178 functions when the subsystem is being deinitialised prior to removal.
182 .Fn hhook_head_register
183 function registers a hook point with the
188 argument defines the high level type for the hook point.
189 Valid types are defined in
191 and new types should be added as required.
194 argument specifies a unique, subsystem specific identifier for the hook point.
197 argument will, if not NULL, be used to store a reference to the
198 .Vt struct hhook_head
199 created as part of the registration process.
200 Subsystems will generally want to store a local copy of the
201 .Vt struct hhook_head
202 so that they can use the
204 macro to instantiate hook points.
205 The HHOOK_WAITOK flag may be passed in via the
209 is allowed to sleep waiting for memory to become available.
210 If the hook point is within a virtualised subsystem (e.g. the network stack),
211 the HHOOK_HEADISINVNET flag should be passed in via the
214 .Vt struct hhook_head
215 created during the registration process will be added to a virtualised list.
218 .Fn hhook_head_deregister
219 function deregisters a previously registered hook point from the
224 argument is the pointer to the
225 .Vt struct hhook_head
227 .Fn hhoook_head_register
228 when the hook point was registered.
231 .Fn hhook_head_deregister_lookup
232 function can be used instead of
233 .Fn hhook_head_deregister
234 in situations where the caller does not have a cached copy of the
235 .Vt struct hhook_head
236 and wants to deregister a hook point using the appropriate
244 function should normally not be called directly and should instead be called
248 However, there may be circumstances where it is preferable to call the function
249 directly, and so it is documented here for completeness.
252 argument references the
254 point to call all registered hook functions for.
257 argument specifies a pointer to the contextual hook point data to pass into the
261 argument should be the pointer to the appropriate object's
263 if the subsystem provides the ability for Khelp modules to associate per-object
265 Subsystems which do not should pass NULL.
269 macro is the preferred way to implement hook points.
272 function if at least one hook function is registered for the hook point.
273 By checking for registered hook functions, the macro minimises the cost
274 associated with adding hook points to frequently used code paths by reducing to
275 a simple if test in the common case where no hook functions are registered.
276 The arguments are as described for the
281 .Fn HHOOKS_RUN_IF_LOOKUP
282 macro performs the same function as the
284 macro, but performs an additional step to look up the
285 .Vt struct hhook_head
291 It should not be used except in code paths which are infrequently executed
292 because of the reference counting overhead associated with the look up.
293 .Sh IMPLEMENTATION NOTES
295 .Vt struct hhook_head
296 protects its internal list of hook functions with a
300 is called directly or indirectly via the
303 .Fn HHOOKS_RUN_IF_LOOKUP
304 macros, a non-sleepable read lock will be acquired and held across the calls to
305 all registered hook functions.
307 .Fn hhook_head_register
308 returns 0 if no errors occurred.
309 It returns EEXIST if a hook point with the same
313 is already registered.
314 It returns EINVAL if the HHOOK_HEADISINVNET flag is not set in
316 because the implementation does not yet support hook points in non-virtualised
319 section for details).
322 failed to allocate memory for the new
323 .Vt struct hhook_head .
325 .Fn hhook_head_deregister
327 .Fn hhook_head_deregister_lookup
328 return 0 if no errors occurred.
329 They return ENOENT if
332 They return EBUSY if the reference count of
336 A well commented example Khelp module can be found at:
337 .Pa /usr/share/examples/kld/khelp/h_example.c
341 implementation provides two
343 points which are called for packets sent/received when a connection is in the
345 Search for HHOOK in the following files:
346 .Pa sys/netinet/tcp_var.h ,
347 .Pa sys/netinet/tcp_input.c ,
348 .Pa sys/netinet/tcp_output.c
350 .Pa sys/netinet/tcp_subr.c .
354 Development and testing of this software were made possible in part by grants
355 from the FreeBSD Foundation and Cisco University Research Program Fund at
356 Community Foundation Silicon Valley.
360 framework first appeared in
365 framework was first released in 2010 by Lawrence Stewart whilst studying at
366 Swinburne University of Technology's Centre for Advanced Internet Architectures,
367 Melbourne, Australia.
368 More details are available at:
370 http://caia.swin.edu.au/urp/newtcp/
375 framework was written by
376 .An Lawrence Stewart Aq Mt lstewart@FreeBSD.org .
378 This manual page was written by
379 .An David Hayes Aq Mt david.hayes@ieee.org
381 .An Lawrence Stewart Aq Mt lstewart@FreeBSD.org .