5 * Copyright (c) 2000 Whistle Communications, Inc.
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.
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
37 * Author: Archie Cobbs <archie@FreeBSD.org>
39 * $FreeBSD: src/sys/netgraph/ng_one2many.c,v 1.1.2.2 2002/07/02 23:44:02 archie Exp $
43 * ng_one2many(4) netgraph node type
45 * Packets received on the "one" hook are sent out each of the
46 * "many" hooks in round-robin fashion. Packets received on any
47 * "many" hook are always delivered to the "one" hook.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/ctype.h>
56 #include <sys/errno.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include "ng_one2many.h"
63 /* Per-link private data */
64 struct ng_one2many_link
{
65 hook_p hook
; /* netgraph hook */
66 struct ng_one2many_link_stats stats
; /* link stats */
69 /* Per-node private data */
70 struct ng_one2many_private
{
71 struct ng_one2many_config conf
; /* node configuration */
72 struct ng_one2many_link one
; /* "one" hook */
73 struct ng_one2many_link many
[NG_ONE2MANY_MAX_LINKS
];
74 u_int16_t nextMany
; /* next round-robin */
75 u_int16_t numActiveMany
; /* # active "many" */
76 u_int16_t activeMany
[NG_ONE2MANY_MAX_LINKS
];
78 typedef struct ng_one2many_private
*priv_p
;
80 /* Netgraph node methods */
81 static ng_constructor_t ng_one2many_constructor
;
82 static ng_rcvmsg_t ng_one2many_rcvmsg
;
83 static ng_shutdown_t ng_one2many_rmnode
;
84 static ng_newhook_t ng_one2many_newhook
;
85 static ng_rcvdata_t ng_one2many_rcvdata
;
86 static ng_disconnect_t ng_one2many_disconnect
;
89 static void ng_one2many_update_many(priv_p priv
);
91 /* Store each hook's link number in the private field */
92 #define LINK_NUM(hook) (*(int16_t *)(&(hook)->private))
94 /******************************************************************
96 ******************************************************************/
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
104 static const struct ng_parse_type ng_one2many_enableLinks_array_type
= {
105 &ng_parse_fixedarray_type
,
106 &ng_one2many_enableLinks_array_type_info
,
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
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
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_one2many_cmdlist
[] = {
127 NGM_ONE2MANY_SET_CONFIG
,
129 &ng_one2many_config_type
,
134 NGM_ONE2MANY_GET_CONFIG
,
137 &ng_one2many_config_type
141 NGM_ONE2MANY_GET_STATS
,
143 &ng_parse_int32_type
,
144 &ng_one2many_link_stats_type
148 NGM_ONE2MANY_CLR_STATS
,
150 &ng_parse_int32_type
,
155 NGM_ONE2MANY_GETCLR_STATS
,
157 &ng_parse_int32_type
,
158 &ng_one2many_link_stats_type
163 /* Node type descriptor */
164 static struct ng_type ng_one2many_typestruct
= {
166 NG_ONE2MANY_NODE_TYPE
,
168 ng_one2many_constructor
,
176 ng_one2many_disconnect
,
179 NETGRAPH_INIT(one2many
, &ng_one2many_typestruct
);
181 /******************************************************************
182 NETGRAPH NODE METHODS
183 ******************************************************************/
189 ng_one2many_constructor(node_p
*nodep
)
194 /* Allocate and initialize private info */
195 priv
= kmalloc(sizeof(*priv
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
198 priv
->conf
.xmitAlg
= NG_ONE2MANY_XMIT_ROUNDROBIN
;
199 priv
->conf
.failAlg
= NG_ONE2MANY_FAIL_MANUAL
;
201 /* Call superclass constructor */
202 if ((error
= ng_make_node_common(&ng_one2many_typestruct
, nodep
))) {
203 kfree(priv
, M_NETGRAPH
);
206 (*nodep
)->private = priv
;
213 * Method for attaching a new hook
216 ng_one2many_newhook(node_p node
, hook_p hook
, const char *name
)
218 const priv_p priv
= node
->private;
219 struct ng_one2many_link
*link
;
224 if (strncmp(name
, NG_ONE2MANY_HOOK_MANY_PREFIX
,
225 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX
)) == 0) {
229 cp
= name
+ strlen(NG_ONE2MANY_HOOK_MANY_PREFIX
);
230 if (!isdigit(*cp
) || (cp
[0] == '0' && cp
[1] != '\0'))
232 i
= strtoul(cp
, &eptr
, 10);
233 if (*eptr
!= '\0' || i
< 0 || i
>= NG_ONE2MANY_MAX_LINKS
)
236 link
= &priv
->many
[linkNum
];
237 } else if (strcmp(name
, NG_ONE2MANY_HOOK_ONE
) == 0) {
238 linkNum
= NG_ONE2MANY_ONE_LINKNUM
;
243 /* Is hook already connected? (should never happen) */
244 if (link
->hook
!= NULL
)
247 /* Setup private info for this link */
248 LINK_NUM(hook
) = linkNum
;
250 bzero(&link
->stats
, sizeof(link
->stats
));
251 if (linkNum
!= NG_ONE2MANY_ONE_LINKNUM
) {
252 priv
->conf
.enabledLinks
[linkNum
] = 1; /* auto-enable link */
253 ng_one2many_update_many(priv
);
261 * Receive a control message
264 ng_one2many_rcvmsg(node_p node
, struct ng_mesg
*msg
,
265 const char *retaddr
, struct ng_mesg
**rptr
)
267 const priv_p priv
= node
->private;
268 struct ng_mesg
*resp
= NULL
;
271 switch (msg
->header
.typecookie
) {
272 case NGM_ONE2MANY_COOKIE
:
273 switch (msg
->header
.cmd
) {
274 case NGM_ONE2MANY_SET_CONFIG
:
276 struct ng_one2many_config
*conf
;
279 /* Check that new configuration is valid */
280 if (msg
->header
.arglen
!= sizeof(*conf
)) {
284 conf
= (struct ng_one2many_config
*)msg
->data
;
285 switch (conf
->xmitAlg
) {
286 case NG_ONE2MANY_XMIT_ROUNDROBIN
:
292 switch (conf
->failAlg
) {
293 case NG_ONE2MANY_FAIL_MANUAL
:
302 /* Normalized many link enabled bits */
303 for (i
= 0; i
< NG_ONE2MANY_MAX_LINKS
; i
++)
304 conf
->enabledLinks
[i
] = !!conf
->enabledLinks
[i
];
306 /* Copy config and reset */
307 bcopy(conf
, &priv
->conf
, sizeof(*conf
));
308 ng_one2many_update_many(priv
);
311 case NGM_ONE2MANY_GET_CONFIG
:
313 struct ng_one2many_config
*conf
;
315 NG_MKRESPONSE(resp
, msg
, sizeof(*conf
), M_NOWAIT
);
320 conf
= (struct ng_one2many_config
*)resp
->data
;
321 bcopy(&priv
->conf
, conf
, sizeof(priv
->conf
));
324 case NGM_ONE2MANY_GET_STATS
:
325 case NGM_ONE2MANY_CLR_STATS
:
326 case NGM_ONE2MANY_GETCLR_STATS
:
328 struct ng_one2many_link
*link
;
332 if (msg
->header
.arglen
!= sizeof(int32_t)) {
336 linkNum
= *((int32_t *)msg
->data
);
337 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
)
339 else if (linkNum
== 0
340 && linkNum
< NG_ONE2MANY_MAX_LINKS
) {
341 link
= &priv
->many
[linkNum
];
347 /* Get/clear stats */
348 if (msg
->header
.cmd
!= NGM_ONE2MANY_CLR_STATS
) {
349 NG_MKRESPONSE(resp
, msg
,
350 sizeof(link
->stats
), M_NOWAIT
);
356 resp
->data
, sizeof(link
->stats
));
358 if (msg
->header
.cmd
!= NGM_ONE2MANY_GET_STATS
)
359 bzero(&link
->stats
, sizeof(link
->stats
));
375 else if (resp
!= NULL
)
376 kfree(resp
, M_NETGRAPH
);
377 kfree(msg
, M_NETGRAPH
);
382 * Receive data on a hook
385 ng_one2many_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
387 const node_p node
= hook
->node
;
388 const priv_p priv
= node
->private;
389 struct ng_one2many_link
*src
;
390 struct ng_one2many_link
*dst
;
394 /* Get link number */
395 linkNum
= LINK_NUM(hook
);
396 KASSERT(linkNum
== NG_ONE2MANY_ONE_LINKNUM
397 || (linkNum
>= 0 && linkNum
< NG_ONE2MANY_MAX_LINKS
),
398 ("%s: linkNum=%d", __func__
, linkNum
));
400 /* Figure out source link */
401 src
= (linkNum
== NG_ONE2MANY_ONE_LINKNUM
) ?
402 &priv
->one
: &priv
->many
[linkNum
];
403 KASSERT(src
->hook
!= NULL
, ("%s: no src%d", __func__
, linkNum
));
405 /* Update receive stats */
406 src
->stats
.recvPackets
++;
407 src
->stats
.recvOctets
+= m
->m_pkthdr
.len
;
409 /* Figure out destination link */
410 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
) {
411 if (priv
->numActiveMany
== 0) {
412 NG_FREE_DATA(m
, meta
);
415 dst
= &priv
->many
[priv
->activeMany
[priv
->nextMany
]];
416 priv
->nextMany
= (priv
->nextMany
+ 1) % priv
->numActiveMany
;
420 /* Update transmit stats */
421 dst
->stats
.xmitPackets
++;
422 dst
->stats
.xmitOctets
+= m
->m_pkthdr
.len
;
425 NG_SEND_DATA(error
, dst
->hook
, m
, meta
);
433 ng_one2many_rmnode(node_p node
)
435 const priv_p priv
= node
->private;
439 KASSERT(priv
->numActiveMany
== 0,
440 ("%s: numActiveMany=%d", __func__
, priv
->numActiveMany
));
441 kfree(priv
, M_NETGRAPH
);
442 node
->private = NULL
;
448 * Hook disconnection.
451 ng_one2many_disconnect(hook_p hook
)
453 const priv_p priv
= hook
->node
->private;
456 /* Get link number */
457 linkNum
= LINK_NUM(hook
);
458 KASSERT(linkNum
== NG_ONE2MANY_ONE_LINKNUM
459 || (linkNum
>= 0 && linkNum
< NG_ONE2MANY_MAX_LINKS
),
460 ("%s: linkNum=%d", __func__
, linkNum
));
463 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
)
464 priv
->one
.hook
= NULL
;
466 priv
->many
[linkNum
].hook
= NULL
;
467 priv
->conf
.enabledLinks
[linkNum
] = 0;
468 ng_one2many_update_many(priv
);
471 /* If no hooks left, go away */
472 if (hook
->node
->numhooks
== 0)
473 ng_rmnode(hook
->node
);
477 /******************************************************************
479 ******************************************************************/
482 * Update internal state after the addition or removal of a "many" link
485 ng_one2many_update_many(priv_p priv
)
489 /* Update list of which "many" links are up */
490 priv
->numActiveMany
= 0;
491 for (linkNum
= 0; linkNum
< NG_ONE2MANY_MAX_LINKS
; linkNum
++) {
492 switch (priv
->conf
.failAlg
) {
493 case NG_ONE2MANY_FAIL_MANUAL
:
494 if (priv
->many
[linkNum
].hook
!= NULL
495 && priv
->conf
.enabledLinks
[linkNum
]) {
496 priv
->activeMany
[priv
->numActiveMany
] = linkNum
;
497 priv
->numActiveMany
++;
502 panic("%s: invalid failAlg", __func__
);
507 /* Update transmit algorithm state */
508 switch (priv
->conf
.xmitAlg
) {
509 case NG_ONE2MANY_XMIT_ROUNDROBIN
:
510 if (priv
->numActiveMany
> 0)
511 priv
->nextMany
%= priv
->numActiveMany
;
515 panic("%s: invalid xmitAlg", __func__
);