1216615Slstewart/*- 2251770Slstewart * Copyright (c) 2010,2013 Lawrence Stewart <lstewart@freebsd.org> 3216615Slstewart * Copyright (c) 2010 The FreeBSD Foundation 4216615Slstewart * All rights reserved. 5216615Slstewart * 6216615Slstewart * This software was developed by Lawrence Stewart while studying at the Centre 7220560Slstewart * for Advanced Internet Architectures, Swinburne University of Technology, made 8220560Slstewart * possible in part by grants from the FreeBSD Foundation and Cisco University 9220560Slstewart * Research Program Fund at Community Foundation Silicon Valley. 10216615Slstewart * 11216615Slstewart * Portions of this software were developed at the Centre for Advanced 12216615Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne, 13216615Slstewart * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. 14216615Slstewart * 15216615Slstewart * Redistribution and use in source and binary forms, with or without 16216615Slstewart * modification, are permitted provided that the following conditions 17216615Slstewart * are met: 18216615Slstewart * 1. Redistributions of source code must retain the above copyright 19216615Slstewart * notice, this list of conditions and the following disclaimer. 20216615Slstewart * 2. Redistributions in binary form must reproduce the above copyright 21216615Slstewart * notice, this list of conditions and the following disclaimer in the 22216615Slstewart * documentation and/or other materials provided with the distribution. 23216615Slstewart * 24216615Slstewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25216615Slstewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26216615Slstewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27216615Slstewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28216615Slstewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29216615Slstewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30216615Slstewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31216615Slstewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32216615Slstewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33216615Slstewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34216615Slstewart * SUCH DAMAGE. 35216615Slstewart * 36216615Slstewart * $FreeBSD$ 37216615Slstewart */ 38216615Slstewart 39216615Slstewart/* 40216615Slstewart * A KPI modelled on the pfil framework for instantiating helper hook points 41216615Slstewart * within the kernel for use by Khelp modules. Originally released as part of 42220560Slstewart * the NewTCP research project at Swinburne University of Technology's Centre 43220560Slstewart * for Advanced Internet Architectures, Melbourne, Australia, which was made 44220560Slstewart * possible in part by a grant from the Cisco University Research Program Fund 45220560Slstewart * at Community Foundation Silicon Valley. More details are available at: 46216615Slstewart * http://caia.swin.edu.au/urp/newtcp/ 47216615Slstewart */ 48216615Slstewart 49216615Slstewart#ifndef _SYS_HHOOK_H_ 50216615Slstewart#define _SYS_HHOOK_H_ 51216615Slstewart 52216615Slstewart/* XXXLAS: Is there a way around this? */ 53216615Slstewart#include <sys/lock.h> 54216615Slstewart#include <sys/rmlock.h> 55216615Slstewart 56216615Slstewart/* hhook_head flags. */ 57216615Slstewart#define HHH_ISINVNET 0x00000001 /* Is the hook point in a vnet? */ 58216615Slstewart 59216615Slstewart/* Flags common to all register functions. */ 60216615Slstewart#define HHOOK_WAITOK 0x00000001 /* Sleeping allowed. */ 61216615Slstewart#define HHOOK_NOWAIT 0x00000002 /* Sleeping disallowed. */ 62216615Slstewart/* Flags only relevant to hhook_head_register() and hhook_head_is_virtual(). */ 63216615Slstewart#define HHOOK_HEADISINVNET 0x00000100 /* Public proxy for HHH_ISINVNET. */ 64216615Slstewart 65216615Slstewart/* Helper hook types. */ 66216615Slstewart#define HHOOK_TYPE_TCP 1 67216615Slstewart 68216615Slstewartstruct helper; 69216615Slstewartstruct osd; 70216615Slstewart 71216615Slstewart/* Signature for helper hook functions. */ 72216615Slstewarttypedef int (*hhook_func_t)(int32_t hhook_type, int32_t hhook_id, void *udata, 73216615Slstewart void *ctx_data, void *hdata, struct osd *hosd); 74216615Slstewart 75216615Slstewart/* 76216615Slstewart * Information required to add/remove a helper hook function to/from a helper 77216615Slstewart * hook point. 78216615Slstewart */ 79216615Slstewartstruct hookinfo { 80216615Slstewart hhook_func_t hook_func; 81216615Slstewart struct helper *hook_helper; 82216615Slstewart void *hook_udata; 83216615Slstewart int32_t hook_id; 84216615Slstewart int32_t hook_type; 85216615Slstewart}; 86216615Slstewart 87216615Slstewart/* 88216615Slstewart * Ideally this would be private but we need access to the hhh_nhooks member 89216615Slstewart * variable in order to make the HHOOKS_RUN_IF() macro low impact. 90216615Slstewart */ 91216615Slstewartstruct hhook_head { 92216615Slstewart STAILQ_HEAD(hhook_list, hhook) hhh_hooks; 93216615Slstewart struct rmlock hhh_lock; 94251732Slstewart uintptr_t hhh_vid; 95216615Slstewart int32_t hhh_id; 96216615Slstewart int32_t hhh_nhooks; 97216615Slstewart int32_t hhh_type; 98216615Slstewart uint32_t hhh_flags; 99216615Slstewart volatile uint32_t hhh_refcount; 100216615Slstewart LIST_ENTRY(hhook_head) hhh_next; 101251732Slstewart LIST_ENTRY(hhook_head) hhh_vnext; 102216615Slstewart}; 103216615Slstewart 104216615Slstewart/* Public KPI functions. */ 105216615Slstewartvoid hhook_run_hooks(struct hhook_head *hhh, void *ctx_data, struct osd *hosd); 106216615Slstewart 107216615Slstewartint hhook_add_hook(struct hhook_head *hhh, struct hookinfo *hki, 108216615Slstewart uint32_t flags); 109216615Slstewart 110216615Slstewartint hhook_add_hook_lookup(struct hookinfo *hki, uint32_t flags); 111216615Slstewart 112216615Slstewartint hhook_remove_hook(struct hhook_head *hhh, struct hookinfo *hki); 113216615Slstewart 114216615Slstewartint hhook_remove_hook_lookup(struct hookinfo *hki); 115216615Slstewart 116216615Slstewartint hhook_head_register(int32_t hhook_type, int32_t hhook_id, 117216615Slstewart struct hhook_head **hhh, uint32_t flags); 118216615Slstewart 119216615Slstewartint hhook_head_deregister(struct hhook_head *hhh); 120216615Slstewart 121216615Slstewartint hhook_head_deregister_lookup(int32_t hhook_type, int32_t hhook_id); 122216615Slstewart 123216615Slstewartstruct hhook_head * hhook_head_get(int32_t hhook_type, int32_t hhook_id); 124216615Slstewart 125216615Slstewartvoid hhook_head_release(struct hhook_head *hhh); 126216615Slstewart 127216615Slstewartuint32_t hhook_head_is_virtualised(struct hhook_head *hhh); 128216615Slstewart 129216615Slstewartuint32_t hhook_head_is_virtualised_lookup(int32_t hook_type, int32_t hook_id); 130216615Slstewart 131216615Slstewart/* 132216615Slstewart * A wrapper around hhook_run_hooks() that only calls the function if at least 133216615Slstewart * one helper hook function is registered for the specified helper hook point. 134216615Slstewart */ 135216615Slstewart#define HHOOKS_RUN_IF(hhh, ctx_data, hosd) do { \ 136216615Slstewart if (hhh != NULL && hhh->hhh_nhooks > 0) \ 137216615Slstewart hhook_run_hooks(hhh, ctx_data, hosd); \ 138216615Slstewart} while (0) 139216615Slstewart 140216615Slstewart/* 141216615Slstewart * WARNING: This macro should only be used in code paths that execute 142216615Slstewart * infrequently, otherwise the refcounting overhead would be excessive. 143216615Slstewart * 144216615Slstewart * A similar wrapper to HHOOKS_RUN_IF() for situations where the caller prefers 145216615Slstewart * not to lookup and store the appropriate hhook_head pointer themselves. 146216615Slstewart */ 147216615Slstewart#define HHOOKS_RUN_LOOKUP_IF(hhook_type, hhook_id, ctx_data, hosd) do { \ 148216615Slstewart struct hhook_head *_hhh; \ 149216615Slstewart \ 150216615Slstewart _hhh = hhook_head_get(hhook_type, hhook_id); \ 151216615Slstewart if (_hhh != NULL) { \ 152216615Slstewart if (_hhh->hhh_nhooks > 0) \ 153216615Slstewart hhook_run_hooks(_hhh, ctx_data, hosd); \ 154216615Slstewart hhook_head_release(_hhh); \ 155216615Slstewart } \ 156216615Slstewart} while (0) 157216615Slstewart 158216615Slstewart#endif /* _SYS_HHOOK_H_ */ 159