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