I'm growing tired of having to add #include lines for header files that
[dragonfly.git] / sys / netgraph / netgraph.h
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  * $DragonFly: src/sys/netgraph/netgraph.h,v 1.3 2006/05/20 02:42:11 dillon Exp $
41  * $Whistle: netgraph.h,v 1.29 1999/11/01 07:56:13 julian Exp $
42  */
43
44 #ifndef _NETGRAPH_NETGRAPH_H_
45 #define _NETGRAPH_NETGRAPH_H_
46
47 #ifndef _SYS_QUEUE_H_
48 #include <sys/queue.h>
49 #endif
50 #ifndef _SYS_MALLOC_H_
51 #include <sys/malloc.h>
52 #endif
53 #ifndef _SYS_MODULE_H_
54 #include <sys/module.h>
55 #endif
56 #ifndef _SYS_MBUF_H_
57 #include <sys/mbuf.h>
58 #endif
59 #ifndef _NETGRAPH_NG_MESSAGE_H_
60 #include <netgraph/ng_message.h>
61 #endif
62
63 #ifndef _KERNEL
64 #error "This file should not be included in user level programs"
65 #endif
66
67 #define NG_ABI_VERSION NG_VERSION
68
69 /*
70  * Structure of a hook
71  */
72 struct ng_hook {
73         char   *name;           /* what this node knows this link as */
74         void   *private;        /* node dependant ID for this hook */
75         int     flags;          /* info about this hook/link */
76         int     refs;           /* dont actually free this till 0 */
77         struct  ng_hook *peer;  /* the other end of this link */
78         struct  ng_node *node;  /* The node this hook is attached to */
79         LIST_ENTRY(ng_hook) hooks;      /* linked list of all hooks on node */
80 };
81 typedef struct ng_hook *hook_p;
82
83 /* Flags for a hook */
84 #define HK_INVALID              0x0001  /* don't trust it! */
85
86 void ng_unref_hook(hook_p hook); /* don't move this */
87 #define NG_HOOK_REF(hook)       atomic_add_int(&(hook)->refs, 1)
88 #define NG_HOOK_NAME(hook)      ((hook)->name)
89 #define NG_HOOK_UNREF(hook)     ng_unref_hook(hook)
90 #define NG_HOOK_SET_PRIVATE(hook, val)  do {(hook)->private = val;} while (0)
91 #define NG_HOOK_SET_RCVMSG(hook, val)   do {(hook)->rcvmsg = val;} while (0)
92 #define NG_HOOK_SET_RCVDATA(hook, val)  do {(hook)->rcvdata = val;} while (0)
93 #define NG_HOOK_PRIVATE(hook)   ((hook)->private)
94 #define NG_HOOK_NOT_VALID(hook) ((hook)->flags & HK_INVALID)
95 #define NG_HOOK_IS_VALID(hook)  (!((hook)->flags & HK_INVALID))
96 #define NG_HOOK_NODE(hook)      ((hook)->node) /* only rvalue! */
97 #define NG_HOOK_PEER(hook)      ((hook)->peer) /* only rvalue! */
98
99 /* Some shortcuts */
100 #define NG_PEER_NODE(hook)      NG_HOOK_NODE(NG_HOOK_PEER(hook))
101 #define NG_PEER_HOOK_NAME(hook) NG_HOOK_NAME(NG_HOOK_PEER(hook))
102 #define NG_PEER_NODE_NAME(hook) NG_NODE_NAME(NG_PEER_NODE(hook))
103
104 /*
105  * Structure of a node
106  */
107 struct ng_node {
108         char   *name;           /* optional globally unique name */
109         struct  ng_type *type;  /* the installed 'type' */
110         int     flags;          /* see below for bit definitions */
111         int     sleepers;       /* #procs sleeping on this node */
112         int     refs;           /* number of references to this node */
113         int     numhooks;       /* number of hooks */
114         int     colour;         /* for graph colouring algorithms */
115         void   *private;        /* node type dependant node ID */
116         ng_ID_t         ID;     /* Unique per node */
117         LIST_HEAD(hooks, ng_hook) hooks;        /* linked list of node hooks */
118         LIST_ENTRY(ng_node)       nodes;        /* linked list of all nodes */
119         LIST_ENTRY(ng_node)       idnodes;      /* ID hash collision list */
120 };
121 typedef struct ng_node *node_p;
122
123 /* Flags for a node */
124 #define NG_INVALID      0x001   /* free when all sleepers and refs go to 0 */
125 #define NG_BUSY         0x002   /* callers should sleep or wait */
126 #define NG_TOUCHED      0x004   /* to avoid cycles when 'flooding' */
127 #define NGF_TYPE1       0x10000000      /* reserved for type specific storage */
128 #define NGF_TYPE2       0x20000000      /* reserved for type specific storage */
129 #define NGF_TYPE3       0x40000000      /* reserved for type specific storage */
130 #define NGF_TYPE4       0x80000000      /* reserved for type specific storage */
131
132 void    ng_unref_node(node_p node); /* don't move this */
133 #define NG_NODE_NAME(node)      ((node)->name + 0)
134 #define NG_NODE_HAS_NAME(node)  ((node)->name[0] + 0)
135 #define NG_NODE_ID(node)        ((node)->ID + 0)
136 #define NG_NODE_REF(node)       atomic_add_int(&(node)->refs, 1)
137 #define NG_NODE_UNREF(node)     ng_unref(node)
138 #define NG_NODE_SET_PRIVATE(node, val)  do {(node)->private = val;} while (0)
139 #define NG_NODE_PRIVATE(node)   ((node)->private)
140 #define NG_NODE_IS_VALID(node)  (!((node)->flags & NG_INVALID))
141 #define NG_NODE_NOT_VALID(node) ((node)->flags & NG_INVALID)
142 #define NG_NODE_NUMHOOKS(node)  ((node)->numhooks + 0) /* rvalue */
143
144 /*
145  * The structure that holds meta_data about a data packet (e.g. priority)
146  * Nodes might add or subtract options as needed if there is room.
147  * They might reallocate the struct to make more room if they need to.
148  * Meta-data is still experimental.
149  */
150 struct meta_field_header {
151         u_long  cookie;         /* cookie for the field. Skip fields you don't
152                                  * know about (same cookie as in messgaes) */
153         u_short type;           /* field ID */
154         u_short len;            /* total len of this field including extra
155                                  * data */
156         char    data[0];        /* data starts here */
157 };
158
159 /* To zero out an option 'in place' set it's cookie to this */
160 #define NGM_INVALID_COOKIE      865455152
161
162 /* This part of the metadata is always present if the pointer is non NULL */
163 struct ng_meta {
164         char    priority;       /* -ve is less priority,  0 is default */
165         char    discardability; /* higher is less valuable.. discard first */
166         u_short allocated_len;  /* amount malloc'd */
167         u_short used_len;       /* sum of all fields, options etc. */
168         u_short flags;          /* see below.. generic flags */
169         struct meta_field_header options[0];    /* add as (if) needed */
170 };
171 typedef struct ng_meta *meta_p;
172
173 /* Flags for meta-data */
174 #define NGMF_TEST       0x01    /* discard at the last moment before sending */
175 #define NGMF_TRACE      0x02    /* trace when handing this data to a node */
176
177 /* node method definitions */
178 typedef int     ng_constructor_t(node_p *node);
179 typedef int     ng_rcvmsg_t(node_p node, struct ng_mesg *msg,
180                         const char *retaddr, struct ng_mesg **resp);
181 typedef int     ng_shutdown_t(node_p node);
182 typedef int     ng_newhook_t(node_p node, hook_p hook, const char *name);
183 typedef hook_p  ng_findhook_t(node_p node, const char *name);
184 typedef int     ng_connect_t(hook_p hook);
185 typedef int     ng_rcvdata_t(hook_p hook, struct mbuf *m, meta_p meta);
186 typedef int     ng_disconnect_t(hook_p hook);
187
188 /*
189  * Command list -- each node type specifies the command that it knows
190  * how to convert between ASCII and binary using an array of these.
191  * The last element in the array must be a terminator with cookie=0.
192  */
193
194 struct ng_cmdlist {
195         u_int32_t                       cookie;         /* command typecookie */
196         int                             cmd;            /* command number */
197         const char                      *name;          /* command name */
198         const struct ng_parse_type      *mesgType;      /* args if !NGF_RESP */
199         const struct ng_parse_type      *respType;      /* args if NGF_RESP */
200 };
201
202 /*
203  * Structure of a node type
204  */
205 struct ng_type {
206
207         u_int32_t       version;        /* must equal NG_VERSION */
208         const char      *name;          /* Unique type name */
209         modeventhand_t  mod_event;      /* Module event handler (optional) */
210         ng_constructor_t *constructor;  /* Node constructor */
211         ng_rcvmsg_t     *rcvmsg;        /* control messages come here */
212         ng_shutdown_t   *shutdown;      /* reset, and free resources */
213         ng_newhook_t    *newhook;       /* first notification of new hook */
214         ng_findhook_t   *findhook;      /* only if you have lots of hooks */
215         ng_connect_t    *connect;       /* final notification of new hook */
216         ng_rcvdata_t    *rcvdata;       /* date comes here */
217         ng_rcvdata_t    *rcvdataq;      /* or here if being queued */
218         ng_disconnect_t *disconnect;    /* notify on disconnect */
219
220         const struct    ng_cmdlist *cmdlist;    /* commands we can convert */
221
222         /* R/W data private to the base netgraph code DON'T TOUCH! */
223         LIST_ENTRY(ng_type) types;              /* linked list of all types */
224         int                 refs;               /* number of instances */
225 };
226
227 /* Send data packet with meta-data */
228 #define NG_SEND_DATA(error, hook, m, a)                                 \
229         do {                                                            \
230                 (error) = ng_send_data((hook), (m), (a));               \
231                 (m) = NULL;                                             \
232                 (a) = NULL;                                             \
233         } while (0)
234
235 #define NG_SEND_DATA_ONLY(error, hook, m)                               \
236         do {                                                            \
237                 (error) = ng_send_data((hook), (m), NULL);              \
238                 (m) = NULL;                                             \
239         } while (0)
240
241 /* Send  queued data packet with meta-data */
242 #define NG_SEND_DATAQ(error, hook, m, a)                                \
243         do {                                                            \
244                 (error) = ng_send_dataq((hook), (m), (a));              \
245                 (m) = NULL;                                             \
246                 (a) = NULL;                                             \
247         } while (0)
248
249 #define NG_RESPOND_MSG(error, here, retaddr, resp, rptr)                \
250         do {                                                            \
251                 if (rptr) {                                             \
252                         *rptr = resp;                                   \
253                 } else if (resp) {                                      \
254                         if (retaddr) {                                  \
255                                 error = ng_queue_msg(here, resp, retaddr); \
256                         } else {                                        \
257                                 FREE(resp, M_NETGRAPH);                 \
258                         }                                               \
259                 }                                                       \
260         } while (0)
261
262 #define NG_FREE_MSG(msg)                                                \
263         do {                                                            \
264                 if ((msg)) {                                            \
265                         FREE((msg), M_NETGRAPH);                        \
266                         (msg) = NULL;                                   \
267                 }                                                       \
268         } while (0)
269
270 #define NG_FREE_META(a)                                                 \
271         do {                                                            \
272                 if ((a)) {                                              \
273                         FREE((a), M_NETGRAPH);                          \
274                         (a) = NULL;                                     \
275                 }                                                       \
276         } while (0)
277
278 #define NG_FREE_M(m)                                                    \
279         do {                                                            \
280                 if ((m)) {                                              \
281                         m_freem((m));                                   \
282                         (m) = NULL;                                     \
283                 }                                                       \
284         } while (0)
285
286 /* Free any data packet and/or meta-data */
287 #define NG_FREE_DATA(m, a)                                              \
288         do {                                                            \
289                 NG_FREE_M((m));                                         \
290                 NG_FREE_META((a));                                      \
291         } while (0)
292
293 /*
294  * Use the NETGRAPH_INIT() macro to link a node type into the
295  * netgraph system. This works for types compiled into the kernel
296  * as well as KLD modules. The first argument should be the type
297  * name (eg, echo) and the second a pointer to the type struct.
298  *
299  * If a different link time is desired, e.g., a device driver that
300  * needs to install its netgraph type before probing, use the
301  * NETGRAPH_INIT_ORDERED() macro instead. Deivce drivers probably
302  * want to use SI_SUB_DRIVERS instead of SI_SUB_PSEUDO.
303  */
304
305 #define NETGRAPH_INIT_ORDERED(typename, typestructp, sub, order)        \
306 static moduledata_t ng_##typename##_mod = {                             \
307         "ng_" #typename,                                                \
308         ng_mod_event,                                                   \
309         (typestructp)                                                   \
310 };                                                                      \
311 DECLARE_MODULE(ng_##typename, ng_##typename##_mod, sub, order)
312
313 #define NETGRAPH_INIT(tn, tp)                                           \
314         NETGRAPH_INIT_ORDERED(tn, tp, SI_SUB_PSEUDO, SI_ORDER_ANY)
315
316 /* Special malloc() type for netgraph structs and ctrl messages */
317 MALLOC_DECLARE(M_NETGRAPH);
318
319 /* declare the base of the netgraph sysctl hierarchy */
320 /* but only if this file cares about sysctls */
321 #ifdef  SYSCTL_DECL
322 SYSCTL_DECL(_net_graph);
323 #endif
324
325 int     ng_bypass(hook_p hook1, hook_p hook2);
326 void    ng_cutlinks(node_p node);
327 int     ng_con_nodes(node_p node,
328              const char *name, node_p node2, const char *name2);
329 meta_p  ng_copy_meta(meta_p meta);
330 void    ng_destroy_hook(hook_p hook);
331 hook_p  ng_findhook(node_p node, const char *name);
332 node_p  ng_findname(node_p node, const char *name);
333 struct  ng_type *ng_findtype(const char *type);
334 int     ng_make_node(const char *type, node_p *nodepp);
335 int     ng_make_node_common(struct ng_type *typep, node_p *nodep);
336 int     ng_mkpeer(node_p node, const char *name, const char *name2, char *type);
337 int     ng_mod_event(module_t mod, int what, void *arg);
338 int     ng_name_node(node_p node, const char *name);
339 int     ng_newtype(struct ng_type *tp);
340 ng_ID_t ng_node2ID(node_p node);
341 int     ng_path2node(node_p here, const char *path, node_p *dest, char **rtnp);
342 int     ng_path_parse(char *addr, char **node, char **path, char **hook);
343 int     ng_queue_data(hook_p hook, struct mbuf *m, meta_p meta);
344 int     ng_queue_msg(node_p here, struct ng_mesg *msg, const char *address);
345 void    ng_release_node(node_p node);
346 void    ng_rmnode(node_p node);
347 int     ng_send_data(hook_p hook, struct mbuf *m, meta_p meta);
348 int     ng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta);
349 int     ng_send_msg(node_p here, struct ng_mesg *msg,
350             const char *address, struct ng_mesg **resp);
351 void    ng_unname(node_p node);
352 void    ng_unref(node_p node);
353 int     ng_wait_node(node_p node, char *msg);
354
355 #endif /* _NETGRAPH_NETGRAPH_H_ */
356