xref: /dflybsd-src/sys/netgraph7/ng_message.h (revision d54592ee9e96c920b951af2e00cd72c0081ccae3)
1 /*
2  * ng_message.h
3  */
4 
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/ng_message.h,v 1.29 2006/10/17 11:01:20 glebius Exp $
41  * $DragonFly: src/sys/netgraph7/ng_message.h,v 1.2 2008/06/26 23:05:35 dillon Exp $
42  * $Whistle: ng_message.h,v 1.12 1999/01/25 01:17:44 archie Exp $
43  */
44 
45 #ifndef _NETGRAPH_NG_MESSAGE_H_
46 #define _NETGRAPH_NG_MESSAGE_H_
47 
48 /* ASCII string size limits */
49 #define	NG_TYPESIZ	32	/* max type name len (including null) */
50 #define	NG_HOOKSIZ	32	/* max hook name len (including null) */
51 #define	NG_NODESIZ	32	/* max node name len (including null) */
52 #define	NG_PATHSIZ	512	/* max path len (including null) */
53 #define	NG_CMDSTRSIZ	32	/* max command string (including null) */
54 
55 #ifndef BURN_BRIDGES
56 /* don't use these - they will go away */
57 #define NG_TYPELEN	(NG_TYPESIZ - 1)
58 #define NG_HOOKLEN	(NG_HOOKSIZ - 1)
59 #define NG_NODELEN	(NG_NODESIZ - 1)
60 #define NG_PATHLEN	(NG_PATHSIZ - 1)
61 #define NG_CMDSTRLEN	(NG_CMDSTRSIZ - 1)
62 #endif
63 
64 #define NG_TEXTRESPONSE 1024	/* allow this length for a text response */
65 
66 /* A netgraph message */
67 struct ng_mesg {
68 	struct	ng_msghdr {
69 		u_char		version;		/*  == NGM_VERSION */
70 		u_char		spare;			/* pad to 4 bytes */
71 		u_int16_t	spare2;
72 		u_int32_t	arglen;			/* length of data */
73 		u_int32_t	cmd;			/* command identifier */
74 		u_int32_t	flags;			/* message status */
75 		u_int32_t	token;			/* match with reply */
76 		u_int32_t	typecookie;		/* node's type cookie */
77 		u_char		cmdstr[NG_CMDSTRSIZ];	/* cmd string + \0 */
78 	} header;
79 	char	data[];			/* placeholder for actual data */
80 };
81 
82 /* This command is guaranteed to not alter data (or'd into the command). */
83 #define NGM_READONLY	0x10000000
84 /* This command is guaranteed to have a reply (or'd into the command). */
85 #define NGM_HASREPLY	0x20000000
86 
87 /* Keep this in sync with the above structure definition */
88 #define NG_GENERIC_NG_MESG_INFO(dtype)	{			\
89 	  { "version",		&ng_parse_uint8_type	},	\
90 	  { "spare",		&ng_parse_uint8_type	},	\
91 	  { "spare2",		&ng_parse_uint16_type	},	\
92 	  { "arglen",		&ng_parse_uint32_type	},	\
93 	  { "cmd",		&ng_parse_uint32_type	},	\
94 	  { "flags",		&ng_parse_hint32_type	},	\
95 	  { "token",		&ng_parse_uint32_type	},	\
96 	  { "typecookie",	&ng_parse_uint32_type	},	\
97 	  { "cmdstr",		&ng_parse_cmdbuf_type	},	\
98 	  { "data",		(dtype)			},	\
99 	  { NULL }						\
100 }
101 
102 /*
103  * Netgraph message header compatibility field
104  * Interfaces within the kernel are defined by a different
105  * value (see NG_ABI_VERSION in netgraph.h)
106  */
107 #define NG_VERSION	8
108 
109 /* Flags field flags */
110 #define NGF_ORIG	0x00000000	/* the msg is the original request */
111 #define NGF_RESP	0x00000001	/* the message is a response */
112 
113 /* Type of a unique node ID. */
114 #define ng_ID_t uint32_t
115 
116 /*
117  * Here we describe the "generic" messages that all nodes inherently
118  * understand. With the exception of NGM_TEXT_STATUS, these are handled
119  * automatically by the base netgraph code.
120  */
121 
122 /* Generic message type cookie */
123 #define NGM_GENERIC_COOKIE	1137070366
124 
125 /* Generic messages defined for this type cookie. */
126 enum {
127 	NGM_SHUTDOWN	= 1,	/* Shut down node. */
128 	NGM_MKPEER	= 2,	/* Create and attach a peer node. */
129 	NGM_CONNECT	= 3,	/* Connect two nodes. */
130 	NGM_NAME	= 4,	/* Give a node a name. */
131 	NGM_RMHOOK	= 5,	/* Break a connection between two nodes. */
132 
133 	/* Get nodeinfo for target. */
134 	NGM_NODEINFO	= (6|NGM_READONLY|NGM_HASREPLY),
135 	/* Get list of hooks on node. */
136 	NGM_LISTHOOKS	= (7|NGM_READONLY|NGM_HASREPLY),
137 	/* List globally named nodes. */
138 	NGM_LISTNAMES	= (8|NGM_READONLY|NGM_HASREPLY),
139 	/* List all nodes. */
140 	NGM_LISTNODES	= (9|NGM_READONLY|NGM_HASREPLY),
141 	/* List installed node types. */
142 	NGM_LISTTYPES	= (10|NGM_READONLY|NGM_HASREPLY),
143 	/* (optional) Get text status. */
144 	NGM_TEXT_STATUS	= (11|NGM_READONLY|NGM_HASREPLY),
145 	/* Convert struct ng_mesg to ASCII. */
146 	NGM_BINARY2ASCII= (12|NGM_READONLY|NGM_HASREPLY),
147 	/* Convert ASCII to struct ng_mesg. */
148 	NGM_ASCII2BINARY= (13|NGM_READONLY|NGM_HASREPLY),
149 	/* (optional) Get/set text config. */
150 	NGM_TEXT_CONFIG	= 14,
151 };
152 
153 /*
154  * Flow control and intra node control messages.
155  * These are routed between nodes to allow flow control and to allow
156  * events to be passed around the graph.
157  * There will be some form of default handling for these but I
158  * do not yet know what it is..
159  */
160 
161 /* Generic message type cookie */
162 #define NGM_FLOW_COOKIE	851672669 /* temp for debugging */
163 
164 /* Upstream messages */
165 #define NGM_LINK_IS_UP		32	/* e.g. carrier found - no data */
166 #define NGM_LINK_IS_DOWN	33	/* carrier lost, includes queue state */
167 #define NGM_HIGH_WATER_PASSED	34	/* includes queue state */
168 #define NGM_LOW_WATER_PASSED	35	/* includes queue state */
169 #define NGM_SYNC_QUEUE_STATE	36	/* sync response from sending packet */
170 
171 /* Downstream messages */
172 #define NGM_DROP_LINK		41	/* drop DTR, etc. - stay in the graph */
173 #define NGM_RAISE_LINK		42	/* if you previously dropped it */
174 #define NGM_FLUSH_QUEUE		43	/* no data */
175 #define NGM_GET_BANDWIDTH	(44|NGM_READONLY)	/* either real or measured */
176 #define NGM_SET_XMIT_Q_LIMITS	45	/* includes queue state */
177 #define NGM_GET_XMIT_Q_LIMITS	(46|NGM_READONLY)	/* returns queue state */
178 #define NGM_MICROMANAGE		47	/* We want sync. queue state
179 						reply for each packet sent */
180 #define NGM_SET_FLOW_MANAGER	48	/* send flow control here */
181 /* Structure used for NGM_MKPEER */
182 struct ngm_mkpeer {
183 	char	type[NG_TYPESIZ];		/* peer type */
184 	char	ourhook[NG_HOOKSIZ];		/* hook name */
185 	char	peerhook[NG_HOOKSIZ];		/* peer hook name */
186 };
187 
188 /* Keep this in sync with the above structure definition */
189 #define NG_GENERIC_MKPEER_INFO()	{			\
190 	  { "type",		&ng_parse_typebuf_type	},	\
191 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
192 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
193 	  { NULL }						\
194 }
195 
196 /* Structure used for NGM_CONNECT */
197 struct ngm_connect {
198 	char	path[NG_PATHSIZ];		/* peer path */
199 	char	ourhook[NG_HOOKSIZ];		/* hook name */
200 	char	peerhook[NG_HOOKSIZ];		/* peer hook name */
201 };
202 
203 /* Keep this in sync with the above structure definition */
204 #define NG_GENERIC_CONNECT_INFO()	{			\
205 	  { "path",		&ng_parse_pathbuf_type	},	\
206 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
207 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
208 	  { NULL }						\
209 }
210 
211 /* Structure used for NGM_NAME */
212 struct ngm_name {
213 	char	name[NG_NODESIZ];			/* node name */
214 };
215 
216 /* Keep this in sync with the above structure definition */
217 #define NG_GENERIC_NAME_INFO()	{				\
218 	  { "name",		&ng_parse_nodebuf_type	},	\
219 	  { NULL }						\
220 }
221 
222 /* Structure used for NGM_RMHOOK */
223 struct ngm_rmhook {
224 	char	ourhook[NG_HOOKSIZ];		/* hook name */
225 };
226 
227 /* Keep this in sync with the above structure definition */
228 #define NG_GENERIC_RMHOOK_INFO()	{			\
229 	  { "hook",		&ng_parse_hookbuf_type	},	\
230 	  { NULL }						\
231 }
232 
233 /* Structure used for NGM_NODEINFO */
234 struct nodeinfo {
235 	char		name[NG_NODESIZ];	/* node name (if any) */
236         char    	type[NG_TYPESIZ];	/* peer type */
237 	ng_ID_t		id;			/* unique identifier */
238 	u_int32_t	hooks;			/* number of active hooks */
239 };
240 
241 /* Keep this in sync with the above structure definition */
242 #define NG_GENERIC_NODEINFO_INFO()	{			\
243 	  { "name",		&ng_parse_nodebuf_type	},	\
244 	  { "type",		&ng_parse_typebuf_type	},	\
245 	  { "id",		&ng_parse_hint32_type	},	\
246 	  { "hooks",		&ng_parse_uint32_type	},	\
247 	  { NULL }						\
248 }
249 
250 /* Structure used for NGM_LISTHOOKS */
251 struct linkinfo {
252 	char		ourhook[NG_HOOKSIZ];	/* hook name */
253 	char		peerhook[NG_HOOKSIZ];	/* peer hook */
254 	struct nodeinfo	nodeinfo;
255 };
256 
257 /* Keep this in sync with the above structure definition */
258 #define NG_GENERIC_LINKINFO_INFO(nitype)	{		\
259 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
260 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
261 	  { "nodeinfo",		(nitype)		},	\
262 	  { NULL }						\
263 }
264 
265 struct hooklist {
266 	struct nodeinfo nodeinfo;		/* node information */
267 	struct linkinfo link[];			/* info about each hook */
268 };
269 
270 /* Keep this in sync with the above structure definition */
271 #define NG_GENERIC_HOOKLIST_INFO(nitype,litype)	{		\
272 	  { "nodeinfo",		(nitype)		},	\
273 	  { "linkinfo",		(litype)		},	\
274 	  { NULL }						\
275 }
276 
277 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES */
278 struct namelist {
279 	u_int32_t	numnames;
280 	struct nodeinfo	nodeinfo[];
281 };
282 
283 /* Keep this in sync with the above structure definition */
284 #define NG_GENERIC_LISTNODES_INFO(niarraytype)	{		\
285 	  { "numnames",		&ng_parse_uint32_type	},	\
286 	  { "nodeinfo",		(niarraytype)		},	\
287 	  { NULL }						\
288 }
289 
290 /* Structure used for NGM_LISTTYPES */
291 struct typeinfo {
292 	char		type_name[NG_TYPESIZ];	/* name of type */
293 	u_int32_t	numnodes;		/* number alive */
294 };
295 
296 /* Keep this in sync with the above structure definition */
297 #define NG_GENERIC_TYPEINFO_INFO()		{		\
298 	  { "typename",		&ng_parse_typebuf_type	},	\
299 	  { "numnodes",		&ng_parse_uint32_type	},	\
300 	  { NULL }						\
301 }
302 
303 struct typelist {
304 	u_int32_t	numtypes;
305 	struct typeinfo	typeinfo[];
306 };
307 
308 /* Keep this in sync with the above structure definition */
309 #define NG_GENERIC_TYPELIST_INFO(tiarraytype)	{		\
310 	  { "numtypes",		&ng_parse_uint32_type	},	\
311 	  { "typeinfo",		(tiarraytype)		},	\
312 	  { NULL }						\
313 }
314 
315 struct ngm_bandwidth {
316 	u_int64_t	nominal_in;
317 	u_int64_t	seen_in;
318 	u_int64_t	nominal_out;
319 	u_int64_t	seen_out;
320 };
321 
322 /* Keep this in sync with the above structure definition */
323 #define NG_GENERIC_BANDWIDTH_INFO()	{			\
324 	  { "nominal_in",	&ng_parse_uint64_type	},	\
325 	  { "seen_in",		&ng_parse_uint64_type	},	\
326 	  { "nominal_out",	&ng_parse_uint64_type	},	\
327 	  { "seen_out",		&ng_parse_uint64_type	},	\
328 	  { NULL }						\
329 }
330 
331 /*
332  * Information about a node's 'output' queue.
333  * This is NOT the netgraph input queueing mechanism,
334  * but rather any queue the node may implement internally
335  * This has to consider ALTQ if we are to work with it.
336  * As far as I can see, ALTQ counts PACKETS, not bytes.
337  * If ALTQ has several queues and one has passed a watermark
338  * we should have the priority of that queue be real (and not -1)
339  * XXX ALTQ stuff is just an idea.....
340  */
341 struct ngm_queue_state {
342 	u_int queue_priority; /* maybe only low-pri is full. -1 = all*/
343 	u_int	max_queuelen_bytes;
344 	u_int	max_queuelen_packets;
345 	u_int	low_watermark;
346 	u_int	high_watermark;
347 	u_int	current;
348 };
349 
350 /* Keep this in sync with the above structure definition */
351 #define NG_GENERIC_QUEUE_INFO()	{				\
352 	  { "max_queuelen_bytes", &ng_parse_uint_type	},	\
353 	  { "max_queuelen_packets", &ng_parse_uint_type	},	\
354 	  { "high_watermark",	&ng_parse_uint_type	},	\
355 	  { "low_watermark",	&ng_parse_uint_type	},	\
356 	  { "current",		&ng_parse_uint_type	},	\
357 	  { NULL }						\
358 }
359 
360 /* Tell a node who to send async flow control info to. */
361 struct flow_manager {
362 	ng_ID_t		id;			/* unique identifier */
363 };
364 
365 /* Keep this in sync with the above structure definition */
366 #define NG_GENERIC_FLOW_MANAGER_INFO()	{			\
367 	  { "id",		&ng_parse_hint32_type	},	\
368 	  { NULL }						\
369 }
370 
371 
372 /*
373  * For netgraph nodes that are somehow associated with file descriptors
374  * (e.g., a device that has a /dev entry and is also a netgraph node),
375  * we define a generic ioctl for requesting the corresponding nodeinfo
376  * structure and for assigning a name (if there isn't one already).
377  *
378  * For these to you need to also #include <sys/ioccom.h>.
379  */
380 
381 #define NGIOCGINFO	_IOR('N', 40, struct nodeinfo)	/* get node info */
382 #define NGIOCSETNAME	_IOW('N', 41, struct ngm_name)	/* set node name */
383 
384 #ifdef _KERNEL
385 /*
386  * Allocate and initialize a netgraph message "msg" with "len"
387  * extra bytes of argument. Sets "msg" to NULL if fails.
388  * Does not initialize token.
389  */
390 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how)			\
391 	do {								\
392 	  MALLOC((msg), struct ng_mesg *, sizeof(struct ng_mesg)	\
393 	    + (len), M_NETGRAPH_MSG, (how) | M_ZERO);			\
394 	  if ((msg) == NULL)						\
395 	    break;							\
396 	  (msg)->header.version = NG_VERSION;				\
397 	  (msg)->header.typecookie = (cookie);				\
398 	  (msg)->header.cmd = (cmdid);					\
399 	  (msg)->header.arglen = (len);					\
400 	  strncpy((msg)->header.cmdstr, #cmdid,				\
401 	    sizeof((msg)->header.cmdstr) - 1);				\
402 	} while (0)
403 
404 /*
405  * Allocate and initialize a response "rsp" to a message "msg"
406  * with "len" extra bytes of argument. Sets "rsp" to NULL if fails.
407  */
408 #define NG_MKRESPONSE(rsp, msg, len, how)				\
409 	do {								\
410 	  MALLOC((rsp), struct ng_mesg *, sizeof(struct ng_mesg)	\
411 	    + (len), M_NETGRAPH_MSG, (how) | M_ZERO);			\
412 	  if ((rsp) == NULL)						\
413 	    break;							\
414 	  (rsp)->header.version = NG_VERSION;				\
415 	  (rsp)->header.arglen = (len);					\
416 	  (rsp)->header.token = (msg)->header.token;			\
417 	  (rsp)->header.typecookie = (msg)->header.typecookie;		\
418 	  (rsp)->header.cmd = (msg)->header.cmd;			\
419 	  bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr,		\
420 	    sizeof((rsp)->header.cmdstr));				\
421 	  (rsp)->header.flags |= NGF_RESP;				\
422 	} while (0)
423 
424 /*
425  * Make a copy of message. Sets "copy" to NULL if fails.
426  */
427 #define	NG_COPYMESSAGE(copy, msg, how)					\
428 	do {								\
429 	  MALLOC((copy), struct ng_mesg *, sizeof(struct ng_mesg) +	\
430 	    (msg)->header.arglen, M_NETGRAPH_MSG, (how) | M_ZERO);	\
431 	  if ((copy) == NULL)						\
432 	    break;							\
433 	  (copy)->header.version = NG_VERSION;				\
434 	  (copy)->header.arglen = (msg)->header.arglen;			\
435 	  (copy)->header.token = (msg)->header.token;			\
436 	  (copy)->header.typecookie = (msg)->header.typecookie;		\
437 	  (copy)->header.cmd = (msg)->header.cmd;			\
438 	  (copy)->header.flags = (msg)->header.flags;			\
439 	  bcopy((msg)->header.cmdstr, (copy)->header.cmdstr,		\
440 	    sizeof((copy)->header.cmdstr));				\
441 	  if ((msg)->header.arglen > 0)					\
442 	    bcopy((msg)->data, (copy)->data, (msg)->header.arglen);	\
443 	} while (0)
444 
445 #endif /* _KERNEL */
446 
447 #endif /* _NETGRAPH_NG_MESSAGE_H_ */
448