1 2 /* 3 * netgraph.h 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Julian Elischer <julian@freebsd.org> 38 * 39 * $FreeBSD: src/sys/netgraph/netgraph.h,v 1.6.2.7 2002/04/14 23:31:08 julian Exp $ 40 * $Whistle: netgraph.h,v 1.29 1999/11/01 07:56:13 julian Exp $ 41 */ 42 43 #ifndef _NETGRAPH_NETGRAPH_H_ 44 #define _NETGRAPH_NETGRAPH_H_ 45 46 #ifndef _SYS_QUEUE_H_ 47 #include <sys/queue.h> 48 #endif 49 #ifndef _SYS_MODULE_H_ 50 #include <sys/module.h> 51 #endif 52 #ifndef _SYS_MBUF_H_ 53 #include <sys/mbuf.h> 54 #endif 55 #ifndef _NETGRAPH_NG_MESSAGE_H_ 56 #include <netgraph/ng_message.h> 57 #endif 58 59 #ifndef _KERNEL 60 #error "This file should not be included in user level programs" 61 #endif 62 63 #define NG_ABI_VERSION NG_VERSION 64 65 /* 66 * Structure of a hook 67 */ 68 struct ng_hook { 69 char *name; /* what this node knows this link as */ 70 void *private; /* node dependant ID for this hook */ 71 int flags; /* info about this hook/link */ 72 int refs; /* dont actually free this till 0 */ 73 struct ng_hook *peer; /* the other end of this link */ 74 struct ng_node *node; /* The node this hook is attached to */ 75 LIST_ENTRY(ng_hook) hooks; /* linked list of all hooks on node */ 76 }; 77 typedef struct ng_hook *hook_p; 78 79 /* Flags for a hook */ 80 #define HK_INVALID 0x0001 /* don't trust it! */ 81 82 void ng_unref_hook(hook_p hook); /* don't move this */ 83 #define NG_HOOK_REF(hook) atomic_add_int(&(hook)->refs, 1) 84 #define NG_HOOK_NAME(hook) ((hook)->name) 85 #define NG_HOOK_UNREF(hook) ng_unref_hook(hook) 86 #define NG_HOOK_SET_PRIVATE(hook, val) do {(hook)->private = val;} while (0) 87 #define NG_HOOK_SET_RCVMSG(hook, val) do {(hook)->rcvmsg = val;} while (0) 88 #define NG_HOOK_SET_RCVDATA(hook, val) do {(hook)->rcvdata = val;} while (0) 89 #define NG_HOOK_PRIVATE(hook) ((hook)->private) 90 #define NG_HOOK_NOT_VALID(hook) ((hook)->flags & HK_INVALID) 91 #define NG_HOOK_IS_VALID(hook) (!((hook)->flags & HK_INVALID)) 92 #define NG_HOOK_NODE(hook) ((hook)->node) /* only rvalue! */ 93 #define NG_HOOK_PEER(hook) ((hook)->peer) /* only rvalue! */ 94 95 /* Some shortcuts */ 96 #define NG_PEER_NODE(hook) NG_HOOK_NODE(NG_HOOK_PEER(hook)) 97 #define NG_PEER_HOOK_NAME(hook) NG_HOOK_NAME(NG_HOOK_PEER(hook)) 98 #define NG_PEER_NODE_NAME(hook) NG_NODE_NAME(NG_PEER_NODE(hook)) 99 100 /* 101 * Structure of a node 102 */ 103 struct ng_node { 104 char *name; /* optional globally unique name */ 105 struct ng_type *type; /* the installed 'type' */ 106 int flags; /* see below for bit definitions */ 107 int sleepers; /* #procs sleeping on this node */ 108 int refs; /* number of references to this node */ 109 int numhooks; /* number of hooks */ 110 int colour; /* for graph colouring algorithms */ 111 void *private; /* node type dependant node ID */ 112 ng_ID_t ID; /* Unique per node */ 113 LIST_HEAD(hooks, ng_hook) hooks; /* linked list of node hooks */ 114 LIST_ENTRY(ng_node) nodes; /* linked list of all nodes */ 115 LIST_ENTRY(ng_node) idnodes; /* ID hash collision list */ 116 }; 117 typedef struct ng_node *node_p; 118 119 /* Flags for a node */ 120 #define NG_INVALID 0x001 /* free when all sleepers and refs go to 0 */ 121 #define NG_BUSY 0x002 /* callers should sleep or wait */ 122 #define NG_TOUCHED 0x004 /* to avoid cycles when 'flooding' */ 123 #define NGF_TYPE1 0x10000000 /* reserved for type specific storage */ 124 #define NGF_TYPE2 0x20000000 /* reserved for type specific storage */ 125 #define NGF_TYPE3 0x40000000 /* reserved for type specific storage */ 126 #define NGF_TYPE4 0x80000000 /* reserved for type specific storage */ 127 128 void ng_unref_node(node_p node); /* don't move this */ 129 #define NG_NODE_NAME(node) ((node)->name + 0) 130 #define NG_NODE_HAS_NAME(node) ((node)->name[0] + 0) 131 #define NG_NODE_ID(node) ((node)->ID + 0) 132 #define NG_NODE_REF(node) atomic_add_int(&(node)->refs, 1) 133 #define NG_NODE_UNREF(node) ng_unref(node) 134 #define NG_NODE_SET_PRIVATE(node, val) do {(node)->private = val;} while (0) 135 #define NG_NODE_PRIVATE(node) ((node)->private) 136 #define NG_NODE_IS_VALID(node) (!((node)->flags & NG_INVALID)) 137 #define NG_NODE_NOT_VALID(node) ((node)->flags & NG_INVALID) 138 #define NG_NODE_NUMHOOKS(node) ((node)->numhooks + 0) /* rvalue */ 139 140 /* 141 * The structure that holds meta_data about a data packet (e.g. priority) 142 * Nodes might add or subtract options as needed if there is room. 143 * They might reallocate the struct to make more room if they need to. 144 * Meta-data is still experimental. 145 */ 146 struct meta_field_header { 147 u_long cookie; /* cookie for the field. Skip fields you don't 148 * know about (same cookie as in messgaes) */ 149 u_short type; /* field ID */ 150 u_short len; /* total len of this field including extra 151 * data */ 152 char data[0]; /* data starts here */ 153 }; 154 155 /* To zero out an option 'in place' set it's cookie to this */ 156 #define NGM_INVALID_COOKIE 865455152 157 158 /* This part of the metadata is always present if the pointer is non NULL */ 159 struct ng_meta { 160 char priority; /* -ve is less priority, 0 is default */ 161 char discardability; /* higher is less valuable.. discard first */ 162 u_short allocated_len; /* amount malloc'd */ 163 u_short used_len; /* sum of all fields, options etc. */ 164 u_short flags; /* see below.. generic flags */ 165 struct meta_field_header options[0]; /* add as (if) needed */ 166 }; 167 typedef struct ng_meta *meta_p; 168 169 /* Flags for meta-data */ 170 #define NGMF_TEST 0x01 /* discard at the last moment before sending */ 171 #define NGMF_TRACE 0x02 /* trace when handing this data to a node */ 172 173 /* node method definitions */ 174 typedef int ng_constructor_t(node_p *node); 175 typedef int ng_rcvmsg_t(node_p node, struct ng_mesg *msg, 176 const char *retaddr, struct ng_mesg **resp); 177 typedef int ng_shutdown_t(node_p node); 178 typedef int ng_newhook_t(node_p node, hook_p hook, const char *name); 179 typedef hook_p ng_findhook_t(node_p node, const char *name); 180 typedef int ng_connect_t(hook_p hook); 181 typedef int ng_rcvdata_t(hook_p hook, struct mbuf *m, meta_p meta); 182 typedef int ng_disconnect_t(hook_p hook); 183 184 /* 185 * Command list -- each node type specifies the command that it knows 186 * how to convert between ASCII and binary using an array of these. 187 * The last element in the array must be a terminator with cookie=0. 188 */ 189 190 struct ng_cmdlist { 191 u_int32_t cookie; /* command typecookie */ 192 int cmd; /* command number */ 193 const char *name; /* command name */ 194 const struct ng_parse_type *mesgType; /* args if !NGF_RESP */ 195 const struct ng_parse_type *respType; /* args if NGF_RESP */ 196 }; 197 198 /* 199 * Structure of a node type 200 */ 201 struct ng_type { 202 203 u_int32_t version; /* must equal NG_VERSION */ 204 const char *name; /* Unique type name */ 205 modeventhand_t mod_event; /* Module event handler (optional) */ 206 ng_constructor_t *constructor; /* Node constructor */ 207 ng_rcvmsg_t *rcvmsg; /* control messages come here */ 208 ng_shutdown_t *shutdown; /* reset, and free resources */ 209 ng_newhook_t *newhook; /* first notification of new hook */ 210 ng_findhook_t *findhook; /* only if you have lots of hooks */ 211 ng_connect_t *connect; /* final notification of new hook */ 212 ng_rcvdata_t *rcvdata; /* date comes here */ 213 ng_rcvdata_t *rcvdataq; /* or here if being queued */ 214 ng_disconnect_t *disconnect; /* notify on disconnect */ 215 216 const struct ng_cmdlist *cmdlist; /* commands we can convert */ 217 218 /* R/W data private to the base netgraph code DON'T TOUCH! */ 219 LIST_ENTRY(ng_type) types; /* linked list of all types */ 220 int refs; /* number of instances */ 221 }; 222 223 /* Send data packet with meta-data */ 224 #define NG_SEND_DATA(error, hook, m, a) \ 225 do { \ 226 (error) = ng_send_data((hook), (m), (a)); \ 227 (m) = NULL; \ 228 (a) = NULL; \ 229 } while (0) 230 231 #define NG_SEND_DATA_ONLY(error, hook, m) \ 232 do { \ 233 (error) = ng_send_data((hook), (m), NULL); \ 234 (m) = NULL; \ 235 } while (0) 236 237 /* Send queued data packet with meta-data */ 238 #define NG_SEND_DATAQ(error, hook, m, a) \ 239 do { \ 240 (error) = ng_send_dataq((hook), (m), (a)); \ 241 (m) = NULL; \ 242 (a) = NULL; \ 243 } while (0) 244 245 #define NG_RESPOND_MSG(error, here, retaddr, resp, rptr) \ 246 do { \ 247 if (rptr) { \ 248 *rptr = resp; \ 249 } else if (resp) { \ 250 if (retaddr) { \ 251 error = ng_queue_msg(here, resp, retaddr); \ 252 } else { \ 253 kfree(resp, M_NETGRAPH); \ 254 } \ 255 } \ 256 } while (0) 257 258 #define NG_FREE_MSG(msg) \ 259 do { \ 260 if ((msg)) { \ 261 kfree((msg), M_NETGRAPH); \ 262 (msg) = NULL; \ 263 } \ 264 } while (0) 265 266 #define NG_FREE_META(a) \ 267 do { \ 268 if ((a)) { \ 269 kfree((a), M_NETGRAPH); \ 270 (a) = NULL; \ 271 } \ 272 } while (0) 273 274 #define NG_FREE_M(m) \ 275 do { \ 276 if ((m)) { \ 277 m_freem((m)); \ 278 (m) = NULL; \ 279 } \ 280 } while (0) 281 282 /* Free any data packet and/or meta-data */ 283 #define NG_FREE_DATA(m, a) \ 284 do { \ 285 NG_FREE_M((m)); \ 286 NG_FREE_META((a)); \ 287 } while (0) 288 289 /* 290 * Use the NETGRAPH_INIT() macro to link a node type into the 291 * netgraph system. This works for types compiled into the kernel 292 * as well as KLD modules. The first argument should be the type 293 * name (eg, echo) and the second a pointer to the type struct. 294 * 295 * If a different link time is desired, e.g., a device driver that 296 * needs to install its netgraph type before probing, use the 297 * NETGRAPH_INIT_ORDERED() macro instead. Deivce drivers probably 298 * want to use SI_SUB_DRIVERS instead of SI_SUB_PSEUDO. 299 */ 300 301 #define NETGRAPH_INIT_ORDERED(typename, typestructp, sub, order) \ 302 static moduledata_t ng_##typename##_mod = { \ 303 "ng_" #typename, \ 304 ng_mod_event, \ 305 (typestructp) \ 306 }; \ 307 DECLARE_MODULE(ng_##typename, ng_##typename##_mod, sub, order); \ 308 MODULE_VERSION(ng_##typename, NG_ABI_VERSION); \ 309 MODULE_DEPEND(ng_##typename, netgraph, NG_ABI_VERSION, \ 310 NG_ABI_VERSION, \ 311 NG_ABI_VERSION) 312 #define NETGRAPH_INIT(tn, tp) \ 313 NETGRAPH_INIT_ORDERED(tn, tp, SI_SUB_PSEUDO, SI_ORDER_ANY) 314 315 /* Special malloc() type for netgraph structs and ctrl messages */ 316 #ifdef MALLOC_DECLARE 317 MALLOC_DECLARE(M_NETGRAPH); 318 #endif 319 320 /* declare the base of the netgraph sysctl hierarchy */ 321 /* but only if this file cares about sysctls */ 322 #ifdef SYSCTL_DECL 323 SYSCTL_DECL(_net_graph); 324 #endif 325 326 int ng_bypass(hook_p hook1, hook_p hook2); 327 void ng_cutlinks(node_p node); 328 int ng_con_nodes(node_p node, 329 const char *name, node_p node2, const char *name2); 330 meta_p ng_copy_meta(meta_p meta); 331 void ng_destroy_hook(hook_p hook); 332 hook_p ng_findhook(node_p node, const char *name); 333 node_p ng_findname(node_p node, const char *name); 334 struct ng_type *ng_findtype(const char *type); 335 int ng_make_node(const char *type, node_p *nodepp); 336 int ng_make_node_common(struct ng_type *typep, node_p *nodep); 337 int ng_mkpeer(node_p node, const char *name, const char *name2, char *type); 338 int ng_mod_event(module_t mod, int what, void *arg); 339 int ng_name_node(node_p node, const char *name); 340 int ng_newtype(struct ng_type *tp); 341 ng_ID_t ng_node2ID(node_p node); 342 int ng_path2node(node_p here, const char *path, node_p *dest, char **rtnp); 343 int ng_path_parse(char *addr, char **node, char **path, char **hook); 344 int ng_queue_data(hook_p hook, struct mbuf *m, meta_p meta); 345 int ng_queue_msg(node_p here, struct ng_mesg *msg, const char *address); 346 void ng_release_node(node_p node); 347 void ng_rmnode(node_p node); 348 int ng_send_data(hook_p hook, struct mbuf *m, meta_p meta); 349 int ng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta); 350 int ng_send_msg(node_p here, struct ng_mesg *msg, 351 const char *address, struct ng_mesg **resp); 352 void ng_unname(node_p node); 353 void ng_unref(node_p node); 354 int ng_wait_node(node_p node, char *msg); 355 356 #endif /* _NETGRAPH_NETGRAPH_H_ */ 357