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