xref: /freebsd/sys/netgraph/ng_one2many.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * ng_one2many.c
3  */
4 
5 /*-
6  * Copyright (c) 2000 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: Archie Cobbs <archie@freebsd.org>
39  *
40  * $FreeBSD$
41  */
42 
43 /*
44  * ng_one2many(4) netgraph node type
45  *
46  * Packets received on the "one" hook are sent out each of the
47  * "many" hooks accoring to an algorithm. Packets received on any
48  * "many" hook are always delivered to the "one" hook.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/ctype.h>
56 #include <sys/mbuf.h>
57 #include <sys/errno.h>
58 
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_one2many.h>
63 
64 /* Per-link private data */
65 struct ng_one2many_link {
66 	hook_p				hook;	/* netgraph hook */
67 	struct ng_one2many_link_stats	stats;	/* link stats */
68 };
69 
70 /* Per-node private data */
71 struct ng_one2many_private {
72 	node_p				node;		/* link to node */
73 	struct ng_one2many_config	conf;		/* node configuration */
74 	struct ng_one2many_link		one;		/* "one" hook */
75 	struct ng_one2many_link		many[NG_ONE2MANY_MAX_LINKS];
76 	u_int16_t			nextMany;	/* next round-robin */
77 	u_int16_t			numActiveMany;	/* # active "many" */
78 	u_int16_t			activeMany[NG_ONE2MANY_MAX_LINKS];
79 };
80 typedef struct ng_one2many_private *priv_p;
81 
82 /* Netgraph node methods */
83 static ng_constructor_t	ng_one2many_constructor;
84 static ng_rcvmsg_t	ng_one2many_rcvmsg;
85 static ng_shutdown_t	ng_one2many_shutdown;
86 static ng_newhook_t	ng_one2many_newhook;
87 static ng_rcvdata_t	ng_one2many_rcvdata;
88 static ng_disconnect_t	ng_one2many_disconnect;
89 
90 /* Other functions */
91 static void		ng_one2many_update_many(priv_p priv);
92 static void		ng_one2many_notify(priv_p priv, uint32_t cmd);
93 
94 /******************************************************************
95 		    NETGRAPH PARSE TYPES
96 ******************************************************************/
97 
98 /* Parse type for struct ng_one2many_config */
99 static const struct ng_parse_fixedarray_info
100     ng_one2many_enableLinks_array_type_info = {
101 	&ng_parse_uint8_type,
102 	NG_ONE2MANY_MAX_LINKS
103 };
104 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
105 	&ng_parse_fixedarray_type,
106 	&ng_one2many_enableLinks_array_type_info,
107 };
108 static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
109 	= NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
110 static const struct ng_parse_type ng_one2many_config_type = {
111 	&ng_parse_struct_type,
112 	&ng_one2many_config_type_fields
113 };
114 
115 /* Parse type for struct ng_one2many_link_stats */
116 static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
117 	= NG_ONE2MANY_LINK_STATS_TYPE_INFO;
118 static const struct ng_parse_type ng_one2many_link_stats_type = {
119 	&ng_parse_struct_type,
120 	&ng_one2many_link_stats_type_fields
121 };
122 
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
125 	{
126 	  NGM_ONE2MANY_COOKIE,
127 	  NGM_ONE2MANY_SET_CONFIG,
128 	  "setconfig",
129 	  &ng_one2many_config_type,
130 	  NULL
131 	},
132 	{
133 	  NGM_ONE2MANY_COOKIE,
134 	  NGM_ONE2MANY_GET_CONFIG,
135 	  "getconfig",
136 	  NULL,
137 	  &ng_one2many_config_type
138 	},
139 	{
140 	  NGM_ONE2MANY_COOKIE,
141 	  NGM_ONE2MANY_GET_STATS,
142 	  "getstats",
143 	  &ng_parse_int32_type,
144 	  &ng_one2many_link_stats_type
145 	},
146 	{
147 	  NGM_ONE2MANY_COOKIE,
148 	  NGM_ONE2MANY_CLR_STATS,
149 	  "clrstats",
150 	  &ng_parse_int32_type,
151 	  NULL,
152 	},
153 	{
154 	  NGM_ONE2MANY_COOKIE,
155 	  NGM_ONE2MANY_GETCLR_STATS,
156 	  "getclrstats",
157 	  &ng_parse_int32_type,
158 	  &ng_one2many_link_stats_type
159 	},
160 	{ 0 }
161 };
162 
163 /* Node type descriptor */
164 static struct ng_type ng_one2many_typestruct = {
165 	.version =	NG_ABI_VERSION,
166 	.name =		NG_ONE2MANY_NODE_TYPE,
167 	.constructor =	ng_one2many_constructor,
168 	.rcvmsg =	ng_one2many_rcvmsg,
169 	.shutdown =	ng_one2many_shutdown,
170 	.newhook =	ng_one2many_newhook,
171 	.rcvdata =	ng_one2many_rcvdata,
172 	.disconnect =	ng_one2many_disconnect,
173 	.cmdlist =	ng_one2many_cmdlist,
174 };
175 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
176 
177 /******************************************************************
178 		    NETGRAPH NODE METHODS
179 ******************************************************************/
180 
181 /*
182  * Node constructor
183  */
184 static int
185 ng_one2many_constructor(node_p node)
186 {
187 	priv_p priv;
188 
189 	/* Allocate and initialize private info */
190 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
191 	if (priv == NULL)
192 		return (ENOMEM);
193 	priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
194 	priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
195 
196 	/* cross reference */
197 	NG_NODE_SET_PRIVATE(node, priv);
198 	priv->node = node;
199 
200 	/* Done */
201 	return (0);
202 }
203 
204 /*
205  * Method for attaching a new hook
206  */
207 static	int
208 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
209 {
210 	const priv_p priv = NG_NODE_PRIVATE(node);
211 	struct ng_one2many_link *link;
212 	int linkNum;
213 	u_long i;
214 
215 	/* Which hook? */
216 	if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
217 	    strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
218 		const char *cp;
219 		char *eptr;
220 
221 		cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
222 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
223 			return (EINVAL);
224 		i = strtoul(cp, &eptr, 10);
225 		if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
226 			return (EINVAL);
227 		linkNum = (int)i;
228 		link = &priv->many[linkNum];
229 	} else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
230 		linkNum = NG_ONE2MANY_ONE_LINKNUM;
231 		link = &priv->one;
232 	} else
233 		return (EINVAL);
234 
235 	/* Is hook already connected? (should never happen) */
236 	if (link->hook != NULL)
237 		return (EISCONN);
238 
239 	/* Setup private info for this link */
240 	NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
241 	link->hook = hook;
242 	bzero(&link->stats, sizeof(link->stats));
243 	if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
244 		priv->conf.enabledLinks[linkNum] = 1;	/* auto-enable link */
245 		ng_one2many_update_many(priv);
246 	}
247 
248 	/* Done */
249 	return (0);
250 }
251 
252 /*
253  * Receive a control message
254  */
255 static int
256 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
257 {
258 	const priv_p priv = NG_NODE_PRIVATE(node);
259 	struct ng_mesg *resp = NULL;
260 	int error = 0;
261 	struct ng_mesg *msg;
262 
263 	NGI_GET_MSG(item, msg);
264 	switch (msg->header.typecookie) {
265 	case NGM_ONE2MANY_COOKIE:
266 		switch (msg->header.cmd) {
267 		case NGM_ONE2MANY_SET_CONFIG:
268 		    {
269 			struct ng_one2many_config *conf;
270 			int i;
271 
272 			/* Check that new configuration is valid */
273 			if (msg->header.arglen != sizeof(*conf)) {
274 				error = EINVAL;
275 				break;
276 			}
277 			conf = (struct ng_one2many_config *)msg->data;
278 			switch (conf->xmitAlg) {
279 			case NG_ONE2MANY_XMIT_ROUNDROBIN:
280 			case NG_ONE2MANY_XMIT_ALL:
281 				break;
282 			default:
283 				error = EINVAL;
284 				break;
285 			}
286 			switch (conf->failAlg) {
287 			case NG_ONE2MANY_FAIL_MANUAL:
288 			case NG_ONE2MANY_FAIL_NOTIFY:
289 				break;
290 			default:
291 				error = EINVAL;
292 				break;
293 			}
294 			if (error != 0)
295 				break;
296 
297 			/* Normalized many link enabled bits */
298 			for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
299 				conf->enabledLinks[i] = !!conf->enabledLinks[i];
300 
301 			/* Copy config and reset */
302 			bcopy(conf, &priv->conf, sizeof(*conf));
303 			ng_one2many_update_many(priv);
304 			break;
305 		    }
306 		case NGM_ONE2MANY_GET_CONFIG:
307 		    {
308 			struct ng_one2many_config *conf;
309 
310 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
311 			if (resp == NULL) {
312 				error = ENOMEM;
313 				break;
314 			}
315 			conf = (struct ng_one2many_config *)resp->data;
316 			bcopy(&priv->conf, conf, sizeof(priv->conf));
317 			break;
318 		    }
319 		case NGM_ONE2MANY_GET_STATS:
320 		case NGM_ONE2MANY_CLR_STATS:
321 		case NGM_ONE2MANY_GETCLR_STATS:
322 		    {
323 			struct ng_one2many_link *link;
324 			int linkNum;
325 
326 			/* Get link */
327 			if (msg->header.arglen != sizeof(int32_t)) {
328 				error = EINVAL;
329 				break;
330 			}
331 			linkNum = *((int32_t *)msg->data);
332 			if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
333 				link = &priv->one;
334 			else if (linkNum >= 0
335 			    && linkNum < NG_ONE2MANY_MAX_LINKS) {
336 				link = &priv->many[linkNum];
337 			} else {
338 				error = EINVAL;
339 				break;
340 			}
341 
342 			/* Get/clear stats */
343 			if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
344 				NG_MKRESPONSE(resp, msg,
345 				    sizeof(link->stats), M_NOWAIT);
346 				if (resp == NULL) {
347 					error = ENOMEM;
348 					break;
349 				}
350 				bcopy(&link->stats,
351 				    resp->data, sizeof(link->stats));
352 			}
353 			if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
354 				bzero(&link->stats, sizeof(link->stats));
355 			break;
356 		    }
357 		default:
358 			error = EINVAL;
359 			break;
360 		}
361 		break;
362 	/*
363 	 * One of our downstreams notifies us of link change. If we are
364 	 * configured to listen to these message, then we remove/add
365 	 * this hook from array of active hooks.
366 	 */
367 	case NGM_FLOW_COOKIE:
368 	    {
369 		int linkNum;
370 
371 		if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
372 			break;
373 
374 		if (lasthook == NULL)
375 			break;
376 
377 		linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
378 		if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
379 			break;
380 
381 		KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
382 		    ("%s: linkNum=%d", __func__, linkNum));
383 
384 		switch (msg->header.cmd) {
385 		case NGM_LINK_IS_UP:
386 			priv->conf.enabledLinks[linkNum] = 1;
387 			ng_one2many_update_many(priv);
388 			break;
389 		case NGM_LINK_IS_DOWN:
390 			priv->conf.enabledLinks[linkNum] = 0;
391 			ng_one2many_update_many(priv);
392 			break;
393 		default:
394 			break;
395 		}
396 		break;
397 	    }
398 	default:
399 		error = EINVAL;
400 		break;
401 	}
402 
403 	/* Done */
404 	NG_RESPOND_MSG(error, node, item, resp);
405 	NG_FREE_MSG(msg);
406 	return (error);
407 }
408 
409 /*
410  * Receive data on a hook
411  */
412 static int
413 ng_one2many_rcvdata(hook_p hook, item_p item)
414 {
415 	const node_p node = NG_HOOK_NODE(hook);
416 	const priv_p priv = NG_NODE_PRIVATE(node);
417 	struct ng_one2many_link *src;
418 	struct ng_one2many_link *dst = NULL;
419 	int error = 0;
420 	int linkNum;
421 	int i;
422 	struct mbuf *m;
423 
424 	m = NGI_M(item); /* just peaking, mbuf still owned by item */
425 	/* Get link number */
426 	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
427 	KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
428 	    || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
429 	    ("%s: linkNum=%d", __func__, linkNum));
430 
431 	/* Figure out source link */
432 	src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
433 	    &priv->one : &priv->many[linkNum];
434 	KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
435 
436 	/* Update receive stats */
437 	src->stats.recvPackets++;
438 	src->stats.recvOctets += m->m_pkthdr.len;
439 
440 	/* Figure out destination link */
441 	if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
442 		if (priv->numActiveMany == 0) {
443 			NG_FREE_ITEM(item);
444 			return (ENOTCONN);
445 		}
446 		switch(priv->conf.xmitAlg) {
447 		case NG_ONE2MANY_XMIT_ROUNDROBIN:
448 			dst = &priv->many[priv->activeMany[priv->nextMany]];
449 			priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
450 			break;
451 		case NG_ONE2MANY_XMIT_ALL:
452 			/* no need to copy data for the 1st one */
453 			dst = &priv->many[priv->activeMany[0]];
454 
455 			/* make copies of data and send for all links
456 			 * except the first one, which we'll do last
457 			 */
458 			for (i = 1; i < priv->numActiveMany; i++) {
459 				struct mbuf *m2;
460 				struct ng_one2many_link *mdst;
461 
462 				mdst = &priv->many[priv->activeMany[i]];
463 				m2 = m_dup(m, M_DONTWAIT);        /* XXX m_copypacket() */
464 				if (m2 == NULL) {
465 					mdst->stats.memoryFailures++;
466 					NG_FREE_ITEM(item);
467 					NG_FREE_M(m);
468 					return (ENOBUFS);
469 				}
470 				/* Update transmit stats */
471 				mdst->stats.xmitPackets++;
472 				mdst->stats.xmitOctets += m->m_pkthdr.len;
473 				NG_SEND_DATA_ONLY(error, mdst->hook, m2);
474 			}
475 			break;
476 #ifdef INVARIANTS
477 		default:
478 			panic("%s: invalid xmitAlg", __func__);
479 #endif
480 		}
481 	} else {
482 		dst = &priv->one;
483 	}
484 
485 	/* Update transmit stats */
486 	dst->stats.xmitPackets++;
487 	dst->stats.xmitOctets += m->m_pkthdr.len;
488 
489 	/* Deliver packet */
490 	NG_FWD_ITEM_HOOK(error, item, dst->hook);
491 	return (error);
492 }
493 
494 /*
495  * Shutdown node
496  */
497 static int
498 ng_one2many_shutdown(node_p node)
499 {
500 	const priv_p priv = NG_NODE_PRIVATE(node);
501 
502 	KASSERT(priv->numActiveMany == 0,
503 	    ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
504 	FREE(priv, M_NETGRAPH);
505 	NG_NODE_SET_PRIVATE(node, NULL);
506 	NG_NODE_UNREF(node);
507 	return (0);
508 }
509 
510 /*
511  * Hook disconnection.
512  */
513 static int
514 ng_one2many_disconnect(hook_p hook)
515 {
516 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
517 	int linkNum;
518 
519 	/* Get link number */
520 	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
521 	KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
522 	    || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
523 	    ("%s: linkNum=%d", __func__, linkNum));
524 
525 	/* Nuke the link */
526 	if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
527 		priv->one.hook = NULL;
528 	else {
529 		priv->many[linkNum].hook = NULL;
530 		priv->conf.enabledLinks[linkNum] = 0;
531 		ng_one2many_update_many(priv);
532 	}
533 
534 	/* If no hooks left, go away */
535 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
536 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
537 		ng_rmnode_self(NG_HOOK_NODE(hook));
538 	return (0);
539 }
540 
541 /******************************************************************
542 		    	OTHER FUNCTIONS
543 ******************************************************************/
544 
545 /*
546  * Update internal state after the addition or removal of a "many" link
547  */
548 static void
549 ng_one2many_update_many(priv_p priv)
550 {
551 	uint16_t saveActive = priv->numActiveMany;
552 	int linkNum;
553 
554 	/* Update list of which "many" links are up */
555 	priv->numActiveMany = 0;
556 	for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
557 		switch (priv->conf.failAlg) {
558 		case NG_ONE2MANY_FAIL_MANUAL:
559 		case NG_ONE2MANY_FAIL_NOTIFY:
560 			if (priv->many[linkNum].hook != NULL
561 			    && priv->conf.enabledLinks[linkNum]) {
562 				priv->activeMany[priv->numActiveMany] = linkNum;
563 				priv->numActiveMany++;
564 			}
565 			break;
566 #ifdef INVARIANTS
567 		default:
568 			panic("%s: invalid failAlg", __func__);
569 #endif
570 		}
571 	}
572 
573 	if (priv->numActiveMany == 0 && saveActive > 0)
574 		ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
575 
576 	if (saveActive == 0 && priv->numActiveMany > 0)
577 		ng_one2many_notify(priv, NGM_LINK_IS_UP);
578 
579 	/* Update transmit algorithm state */
580 	switch (priv->conf.xmitAlg) {
581 	case NG_ONE2MANY_XMIT_ROUNDROBIN:
582 		if (priv->numActiveMany > 0)
583 			priv->nextMany %= priv->numActiveMany;
584 		break;
585 	case NG_ONE2MANY_XMIT_ALL:
586 		break;
587 #ifdef INVARIANTS
588 	default:
589 		panic("%s: invalid xmitAlg", __func__);
590 #endif
591 	}
592 }
593 
594 /*
595  * Notify upstream if we are out of links, or we have at least one link.
596  */
597 static void
598 ng_one2many_notify(priv_p priv, uint32_t cmd)
599 {
600 	struct ng_mesg *msg;
601 	int dummy_error = 0;
602 
603 	if (priv->one.hook == NULL)
604 		return;
605 
606 	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
607 	if (msg != NULL)
608 		NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);
609 }
610