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