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