2 * Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
3 * Copyright (c) 2010 The FreeBSD Foundation
6 * This software was developed by Lawrence Stewart while studying at the Centre
7 * for Advanced Internet Architectures, Swinburne University of Technology,
8 * made possible in part by grants from the FreeBSD Foundation and Cisco
9 * University Research Program Fund at Community Foundation Silicon Valley.
11 * Portions of this software were developed at the Centre for Advanced
12 * Internet Architectures, Swinburne University of Technology, Melbourne,
13 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/hhook.h>
44 #include <sys/khelp.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/module_khelp.h>
50 #include <sys/queue.h>
51 #include <sys/refcount.h>
52 #include <sys/rwlock.h>
53 #include <sys/systm.h>
57 static struct rwlock khelp_list_lock;
58 RW_SYSINIT(khelplistlock, &khelp_list_lock, "helper list lock");
60 static TAILQ_HEAD(helper_head, helper) helpers = TAILQ_HEAD_INITIALIZER(helpers);
62 /* Private function prototypes. */
63 static inline void khelp_remove_osd(struct helper *h, struct osd *hosd);
65 #define KHELP_LIST_WLOCK() rw_wlock(&khelp_list_lock)
66 #define KHELP_LIST_WUNLOCK() rw_wunlock(&khelp_list_lock)
67 #define KHELP_LIST_RLOCK() rw_rlock(&khelp_list_lock)
68 #define KHELP_LIST_RUNLOCK() rw_runlock(&khelp_list_lock)
69 #define KHELP_LIST_LOCK_ASSERT() rw_assert(&khelp_list_lock, RA_LOCKED)
72 khelp_register_helper(struct helper *h)
75 int error, i, inserted;
79 refcount_init(&h->h_refcount, 0);
80 h->h_id = osd_register(OSD_KHELP, NULL, NULL);
82 /* It's only safe to add the hooks after osd_register(). */
83 if (h->h_nhooks > 0) {
84 for (i = 0; i < h->h_nhooks && !error; i++) {
85 /* We don't require the module to assign hook_helper. */
86 h->h_hooks[i].hook_helper = h;
87 error = khelp_add_hhook(&h->h_hooks[i], HHOOK_NOWAIT);
91 for (i--; i >= 0; i--)
92 khelp_remove_hhook(&h->h_hooks[i]);
94 osd_deregister(OSD_KHELP, h->h_id);
101 * Keep list of helpers sorted in descending h_id order. Due to
102 * the way osd_set() works, a sorted list ensures
103 * init_helper_osd() will operate with improved efficiency.
105 TAILQ_FOREACH(tmph, &helpers, h_next) {
106 if (tmph->h_id < h->h_id) {
107 TAILQ_INSERT_BEFORE(tmph, h, h_next);
114 TAILQ_INSERT_TAIL(&helpers, h, h_next);
115 KHELP_LIST_WUNLOCK();
122 khelp_deregister_helper(struct helper *h)
130 if (h->h_refcount > 0)
134 TAILQ_FOREACH(tmph, &helpers, h_next) {
136 TAILQ_REMOVE(&helpers, h, h_next);
142 KHELP_LIST_WUNLOCK();
145 if (h->h_nhooks > 0) {
146 for (i = 0; i < h->h_nhooks; i++)
147 khelp_remove_hhook(&h->h_hooks[i]);
149 osd_deregister(OSD_KHELP, h->h_id);
156 khelp_init_osd(uint32_t classes, struct osd *hosd)
162 KASSERT(hosd != NULL, ("struct osd not initialised!"));
167 TAILQ_FOREACH(h, &helpers, h_next) {
168 /* If helper is correct class and needs to store OSD... */
169 if (h->h_classes & classes && h->h_flags & HELPER_NEEDS_OSD) {
170 hdata = uma_zalloc(h->h_zone, M_NOWAIT);
175 osd_set(OSD_KHELP, hosd, h->h_id, hdata);
176 refcount_acquire(&h->h_refcount);
181 /* Delete OSD that was assigned prior to the error. */
182 TAILQ_FOREACH(h, &helpers, h_next) {
183 if (h->h_classes & classes)
184 khelp_remove_osd(h, hosd);
187 KHELP_LIST_RUNLOCK();
193 khelp_destroy_osd(struct osd *hosd)
198 KASSERT(hosd != NULL, ("struct osd not initialised!"));
204 * Clean up all khelp related OSD.
206 * XXXLAS: Would be nice to use something like osd_exit() here but it
207 * doesn't have the right semantics for this purpose.
209 TAILQ_FOREACH(h, &helpers, h_next)
210 khelp_remove_osd(h, hosd);
211 KHELP_LIST_RUNLOCK();
217 khelp_remove_osd(struct helper *h, struct osd *hosd)
221 if (h->h_flags & HELPER_NEEDS_OSD) {
223 * If the current helper uses OSD and calling osd_get()
224 * on the helper's h_id returns non-NULL, the helper has
225 * OSD attached to 'hosd' which needs to be cleaned up.
227 hdata = osd_get(OSD_KHELP, hosd, h->h_id);
229 uma_zfree(h->h_zone, hdata);
230 osd_del(OSD_KHELP, hosd, h->h_id);
231 refcount_release(&h->h_refcount);
237 khelp_get_osd(struct osd *hosd, int32_t id)
240 return (osd_get(OSD_KHELP, hosd, id));
244 khelp_get_id(char *hname)
252 TAILQ_FOREACH(h, &helpers, h_next) {
253 if (strncmp(h->h_name, hname, HELPER_NAME_MAXLEN) == 0) {
258 KHELP_LIST_RUNLOCK();
264 khelp_add_hhook(struct hookinfo *hki, uint32_t flags)
266 VNET_ITERATOR_DECL(vnet_iter);
272 * XXXLAS: If a helper is dynamically adding a helper hook function at
273 * runtime using this function, we should update the helper's h_hooks
274 * struct member to include the additional hookinfo struct.
277 VNET_LIST_RLOCK_NOSLEEP();
278 VNET_FOREACH(vnet_iter) {
279 CURVNET_SET(vnet_iter);
280 error = hhook_add_hook_lookup(hki, flags);
287 VNET_LIST_RUNLOCK_NOSLEEP();
293 khelp_remove_hhook(struct hookinfo *hki)
295 VNET_ITERATOR_DECL(vnet_iter);
301 * XXXLAS: If a helper is dynamically removing a helper hook function at
302 * runtime using this function, we should update the helper's h_hooks
303 * struct member to remove the defunct hookinfo struct.
306 VNET_LIST_RLOCK_NOSLEEP();
307 VNET_FOREACH(vnet_iter) {
308 CURVNET_SET(vnet_iter);
309 error = hhook_remove_hook_lookup(hki);
316 VNET_LIST_RUNLOCK_NOSLEEP();
322 khelp_modevent(module_t mod, int event_type, void *data)
324 struct khelp_modevent_data *kmd;
327 kmd = (struct khelp_modevent_data *)data;
332 if (kmd->helper->h_flags & HELPER_NEEDS_OSD) {
333 if (kmd->uma_zsize <= 0) {
334 printf("Use KHELP_DECLARE_MOD_UMA() instead!\n");
338 kmd->helper->h_zone = uma_zcreate(kmd->name,
339 kmd->uma_zsize, kmd->umactor, kmd->umadtor, NULL,
341 if (kmd->helper->h_zone == NULL) {
346 strlcpy(kmd->helper->h_name, kmd->name, HELPER_NAME_MAXLEN);
347 kmd->helper->h_hooks = kmd->hooks;
348 kmd->helper->h_nhooks = kmd->nhooks;
349 if (kmd->helper->mod_init != NULL)
350 error = kmd->helper->mod_init();
352 error = khelp_register_helper(kmd->helper);
358 error = khelp_deregister_helper(kmd->helper);
360 if (kmd->helper->h_flags & HELPER_NEEDS_OSD)
361 uma_zdestroy(kmd->helper->h_zone);
362 if (kmd->helper->mod_destroy != NULL)
363 kmd->helper->mod_destroy();
364 } else if (error == ENOENT)
365 /* Do nothing and allow unload if helper not in list. */
367 else if (error == EBUSY)
368 printf("Khelp module \"%s\" can't unload until its "
369 "refcount drops from %d to 0.\n", kmd->name,
370 kmd->helper->h_refcount);
382 * This function is called in two separate situations:
384 * - When the kernel is booting, it is called directly by the SYSINIT framework
385 * to allow Khelp modules which were compiled into the kernel or loaded by the
386 * boot loader to insert their non-virtualised hook functions into the kernel.
388 * - When the kernel is booting or a vnet is created, this function is also
389 * called indirectly through khelp_vnet_init() by the vnet initialisation code.
390 * In this situation, Khelp modules are able to insert their virtualised hook
391 * functions into the virtualised hook points in the vnet which is being
392 * initialised. In the case where the kernel is not compiled with "options
393 * VIMAGE", this step is still run once at boot, but the hook functions get
394 * transparently inserted into the standard unvirtualised network stack.
397 khelp_init(const void *vnet)
404 vinit = vnet != NULL;
407 TAILQ_FOREACH(h, &helpers, h_next) {
408 for (i = 0; i < h->h_nhooks && !error; i++) {
409 htype = h->h_hooks[i].hook_type;
410 hid = h->h_hooks[i].hook_id;
413 * If we're doing a virtualised init (vinit != 0) and
414 * the hook point is virtualised, or we're doing a plain
415 * sysinit at boot and the hook point is not
416 * virtualised, insert the hook.
418 if ((hhook_head_is_virtualised_lookup(htype, hid) ==
419 HHOOK_HEADISINVNET && vinit) ||
420 (!hhook_head_is_virtualised_lookup(htype, hid) &&
422 error = hhook_add_hook_lookup(&h->h_hooks[i],
428 /* Remove any helper's hooks we successfully added. */
429 for (i--; i >= 0; i--)
430 hhook_remove_hook_lookup(&h->h_hooks[i]);
432 printf("%s: Failed to add hooks for helper \"%s\" (%p)",
433 __func__, h->h_name, h);
435 printf(" to vnet %p.\n", vnet);
442 KHELP_LIST_RUNLOCK();
446 * Vnet created and being initialised.
449 khelp_vnet_init(const void *unused __unused)
452 khelp_init(TD_TO_VNET(curthread));
457 * As the kernel boots, allow Khelp modules which were compiled into the kernel
458 * or loaded by the boot loader to insert their non-virtualised hook functions
461 SYSINIT(khelp_init, SI_SUB_PROTO_END, SI_ORDER_FIRST, khelp_init, NULL);
464 * When a vnet is created and being initialised, we need to insert the helper
465 * hook functions for all currently registered Khelp modules into the vnet's
466 * helper hook points. The hhook KPI provides a mechanism for subsystems which
467 * export helper hook points to clean up on vnet shutdown, so we don't need a
468 * VNET_SYSUNINIT for Khelp.
470 VNET_SYSINIT(khelp_vnet_init, SI_SUB_PROTO_END, SI_ORDER_FIRST,
471 khelp_vnet_init, NULL);