6 * Copyright (c) 2000 Whistle Communications, Inc.
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.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_one2many.c,v 1.21 2005/03/11 10:29:38 glebius Exp $
41 * $DragonFly: src/sys/netgraph7/ng_one2many.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
45 * ng_one2many(4) netgraph node type
47 * Packets received on the "one" hook are sent out each of the
48 * "many" hooks accoring to an algorithm. Packets received on any
49 * "many" hook are always delivered to the "one" hook.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/ctype.h>
58 #include <sys/errno.h>
60 #include "ng_message.h"
63 #include "ng_one2many.h"
65 /* Per-link private data */
66 struct ng_one2many_link
{
67 hook_p hook
; /* netgraph hook */
68 struct ng_one2many_link_stats stats
; /* link stats */
71 /* Per-node private data */
72 struct ng_one2many_private
{
73 node_p node
; /* link to node */
74 struct ng_one2many_config conf
; /* node configuration */
75 struct ng_one2many_link one
; /* "one" hook */
76 struct ng_one2many_link many
[NG_ONE2MANY_MAX_LINKS
];
77 u_int16_t nextMany
; /* next round-robin */
78 u_int16_t numActiveMany
; /* # active "many" */
79 u_int16_t activeMany
[NG_ONE2MANY_MAX_LINKS
];
81 typedef struct ng_one2many_private
*priv_p
;
83 /* Netgraph node methods */
84 static ng_constructor_t ng_one2many_constructor
;
85 static ng_rcvmsg_t ng_one2many_rcvmsg
;
86 static ng_shutdown_t ng_one2many_shutdown
;
87 static ng_newhook_t ng_one2many_newhook
;
88 static ng_rcvdata_t ng_one2many_rcvdata
;
89 static ng_disconnect_t ng_one2many_disconnect
;
92 static void ng_one2many_update_many(priv_p priv
);
93 static void ng_one2many_notify(priv_p priv
, uint32_t cmd
);
95 /******************************************************************
97 ******************************************************************/
99 /* Parse type for struct ng_one2many_config */
100 static const struct ng_parse_fixedarray_info
101 ng_one2many_enableLinks_array_type_info
= {
102 &ng_parse_uint8_type
,
103 NG_ONE2MANY_MAX_LINKS
105 static const struct ng_parse_type ng_one2many_enableLinks_array_type
= {
106 &ng_parse_fixedarray_type
,
107 &ng_one2many_enableLinks_array_type_info
,
109 static const struct ng_parse_struct_field ng_one2many_config_type_fields
[]
110 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type
);
111 static const struct ng_parse_type ng_one2many_config_type
= {
112 &ng_parse_struct_type
,
113 &ng_one2many_config_type_fields
116 /* Parse type for struct ng_one2many_link_stats */
117 static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields
[]
118 = NG_ONE2MANY_LINK_STATS_TYPE_INFO
;
119 static const struct ng_parse_type ng_one2many_link_stats_type
= {
120 &ng_parse_struct_type
,
121 &ng_one2many_link_stats_type_fields
124 /* List of commands and how to convert arguments to/from ASCII */
125 static const struct ng_cmdlist ng_one2many_cmdlist
[] = {
128 NGM_ONE2MANY_SET_CONFIG
,
130 &ng_one2many_config_type
,
135 NGM_ONE2MANY_GET_CONFIG
,
138 &ng_one2many_config_type
142 NGM_ONE2MANY_GET_STATS
,
144 &ng_parse_int32_type
,
145 &ng_one2many_link_stats_type
149 NGM_ONE2MANY_CLR_STATS
,
151 &ng_parse_int32_type
,
156 NGM_ONE2MANY_GETCLR_STATS
,
158 &ng_parse_int32_type
,
159 &ng_one2many_link_stats_type
164 /* Node type descriptor */
165 static struct ng_type ng_one2many_typestruct
= {
166 .version
= NG_ABI_VERSION
,
167 .name
= NG_ONE2MANY_NODE_TYPE
,
168 .constructor
= ng_one2many_constructor
,
169 .rcvmsg
= ng_one2many_rcvmsg
,
170 .shutdown
= ng_one2many_shutdown
,
171 .newhook
= ng_one2many_newhook
,
172 .rcvdata
= ng_one2many_rcvdata
,
173 .disconnect
= ng_one2many_disconnect
,
174 .cmdlist
= ng_one2many_cmdlist
,
176 NETGRAPH_INIT(one2many
, &ng_one2many_typestruct
);
178 /******************************************************************
179 NETGRAPH NODE METHODS
180 ******************************************************************/
186 ng_one2many_constructor(node_p node
)
190 /* Allocate and initialize private info */
191 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
194 priv
->conf
.xmitAlg
= NG_ONE2MANY_XMIT_ROUNDROBIN
;
195 priv
->conf
.failAlg
= NG_ONE2MANY_FAIL_MANUAL
;
197 /* cross reference */
198 NG_NODE_SET_PRIVATE(node
, priv
);
206 * Method for attaching a new hook
209 ng_one2many_newhook(node_p node
, hook_p hook
, const char *name
)
211 const priv_p priv
= NG_NODE_PRIVATE(node
);
212 struct ng_one2many_link
*link
;
217 if (strncmp(name
, NG_ONE2MANY_HOOK_MANY_PREFIX
,
218 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX
)) == 0) {
222 cp
= name
+ strlen(NG_ONE2MANY_HOOK_MANY_PREFIX
);
223 if (!isdigit(*cp
) || (cp
[0] == '0' && cp
[1] != '\0'))
225 i
= strtoul(cp
, &eptr
, 10);
226 if (*eptr
!= '\0' || i
< 0 || i
>= NG_ONE2MANY_MAX_LINKS
)
229 link
= &priv
->many
[linkNum
];
230 } else if (strcmp(name
, NG_ONE2MANY_HOOK_ONE
) == 0) {
231 linkNum
= NG_ONE2MANY_ONE_LINKNUM
;
236 /* Is hook already connected? (should never happen) */
237 if (link
->hook
!= NULL
)
240 /* Setup private info for this link */
241 NG_HOOK_SET_PRIVATE(hook
, (void *)(intptr_t)linkNum
);
243 bzero(&link
->stats
, sizeof(link
->stats
));
244 if (linkNum
!= NG_ONE2MANY_ONE_LINKNUM
) {
245 priv
->conf
.enabledLinks
[linkNum
] = 1; /* auto-enable link */
246 ng_one2many_update_many(priv
);
254 * Receive a control message
257 ng_one2many_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
259 const priv_p priv
= NG_NODE_PRIVATE(node
);
260 struct ng_mesg
*resp
= NULL
;
264 NGI_GET_MSG(item
, msg
);
265 switch (msg
->header
.typecookie
) {
266 case NGM_ONE2MANY_COOKIE
:
267 switch (msg
->header
.cmd
) {
268 case NGM_ONE2MANY_SET_CONFIG
:
270 struct ng_one2many_config
*conf
;
273 /* Check that new configuration is valid */
274 if (msg
->header
.arglen
!= sizeof(*conf
)) {
278 conf
= (struct ng_one2many_config
*)msg
->data
;
279 switch (conf
->xmitAlg
) {
280 case NG_ONE2MANY_XMIT_ROUNDROBIN
:
281 case NG_ONE2MANY_XMIT_ALL
:
287 switch (conf
->failAlg
) {
288 case NG_ONE2MANY_FAIL_MANUAL
:
289 case NG_ONE2MANY_FAIL_NOTIFY
:
298 /* Normalized many link enabled bits */
299 for (i
= 0; i
< NG_ONE2MANY_MAX_LINKS
; i
++)
300 conf
->enabledLinks
[i
] = !!conf
->enabledLinks
[i
];
302 /* Copy config and reset */
303 bcopy(conf
, &priv
->conf
, sizeof(*conf
));
304 ng_one2many_update_many(priv
);
307 case NGM_ONE2MANY_GET_CONFIG
:
309 struct ng_one2many_config
*conf
;
311 NG_MKRESPONSE(resp
, msg
, sizeof(*conf
), M_WAITOK
| M_NULLOK
);
316 conf
= (struct ng_one2many_config
*)resp
->data
;
317 bcopy(&priv
->conf
, conf
, sizeof(priv
->conf
));
320 case NGM_ONE2MANY_GET_STATS
:
321 case NGM_ONE2MANY_CLR_STATS
:
322 case NGM_ONE2MANY_GETCLR_STATS
:
324 struct ng_one2many_link
*link
;
328 if (msg
->header
.arglen
!= sizeof(int32_t)) {
332 linkNum
= *((int32_t *)msg
->data
);
333 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
)
335 else if (linkNum
>= 0
336 && linkNum
< NG_ONE2MANY_MAX_LINKS
) {
337 link
= &priv
->many
[linkNum
];
343 /* Get/clear stats */
344 if (msg
->header
.cmd
!= NGM_ONE2MANY_CLR_STATS
) {
345 NG_MKRESPONSE(resp
, msg
,
346 sizeof(link
->stats
), M_WAITOK
| M_NULLOK
);
352 resp
->data
, sizeof(link
->stats
));
354 if (msg
->header
.cmd
!= NGM_ONE2MANY_GET_STATS
)
355 bzero(&link
->stats
, sizeof(link
->stats
));
364 * One of our downstreams notifies us of link change. If we are
365 * configured to listen to these message, then we remove/add
366 * this hook from array of active hooks.
368 case NGM_FLOW_COOKIE
:
372 if (priv
->conf
.failAlg
!= NG_ONE2MANY_FAIL_NOTIFY
)
375 if (lasthook
== NULL
)
378 linkNum
= (intptr_t)NG_HOOK_PRIVATE(lasthook
);
379 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
)
382 KASSERT((linkNum
>= 0 && linkNum
< NG_ONE2MANY_MAX_LINKS
),
383 ("%s: linkNum=%d", __func__
, linkNum
));
385 switch (msg
->header
.cmd
) {
387 priv
->conf
.enabledLinks
[linkNum
] = 1;
388 ng_one2many_update_many(priv
);
390 case NGM_LINK_IS_DOWN
:
391 priv
->conf
.enabledLinks
[linkNum
] = 0;
392 ng_one2many_update_many(priv
);
405 NG_RESPOND_MSG(error
, node
, item
, resp
);
411 * Receive data on a hook
414 ng_one2many_rcvdata(hook_p hook
, item_p item
)
416 const node_p node
= NG_HOOK_NODE(hook
);
417 const priv_p priv
= NG_NODE_PRIVATE(node
);
418 struct ng_one2many_link
*src
;
419 struct ng_one2many_link
*dst
= NULL
;
425 m
= NGI_M(item
); /* just peaking, mbuf still owned by item */
426 /* Get link number */
427 linkNum
= (intptr_t)NG_HOOK_PRIVATE(hook
);
428 KASSERT(linkNum
== NG_ONE2MANY_ONE_LINKNUM
429 || (linkNum
>= 0 && linkNum
< NG_ONE2MANY_MAX_LINKS
),
430 ("%s: linkNum=%d", __func__
, linkNum
));
432 /* Figure out source link */
433 src
= (linkNum
== NG_ONE2MANY_ONE_LINKNUM
) ?
434 &priv
->one
: &priv
->many
[linkNum
];
435 KASSERT(src
->hook
!= NULL
, ("%s: no src%d", __func__
, linkNum
));
437 /* Update receive stats */
438 src
->stats
.recvPackets
++;
439 src
->stats
.recvOctets
+= m
->m_pkthdr
.len
;
441 /* Figure out destination link */
442 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
) {
443 if (priv
->numActiveMany
== 0) {
447 switch(priv
->conf
.xmitAlg
) {
448 case NG_ONE2MANY_XMIT_ROUNDROBIN
:
449 dst
= &priv
->many
[priv
->activeMany
[priv
->nextMany
]];
450 priv
->nextMany
= (priv
->nextMany
+ 1) % priv
->numActiveMany
;
452 case NG_ONE2MANY_XMIT_ALL
:
453 /* no need to copy data for the 1st one */
454 dst
= &priv
->many
[priv
->activeMany
[0]];
456 /* make copies of data and send for all links
457 * except the first one, which we'll do last
459 for (i
= 1; i
< priv
->numActiveMany
; i
++) {
461 struct ng_one2many_link
*mdst
;
463 mdst
= &priv
->many
[priv
->activeMany
[i
]];
464 m2
= m_dup(m
, MB_DONTWAIT
); /* XXX m_copypacket() */
466 mdst
->stats
.memoryFailures
++;
471 /* Update transmit stats */
472 mdst
->stats
.xmitPackets
++;
473 mdst
->stats
.xmitOctets
+= m
->m_pkthdr
.len
;
474 NG_SEND_DATA_ONLY(error
, mdst
->hook
, m2
);
479 panic("%s: invalid xmitAlg", __func__
);
486 /* Update transmit stats */
487 dst
->stats
.xmitPackets
++;
488 dst
->stats
.xmitOctets
+= m
->m_pkthdr
.len
;
491 NG_FWD_ITEM_HOOK(error
, item
, dst
->hook
);
499 ng_one2many_shutdown(node_p node
)
501 const priv_p priv
= NG_NODE_PRIVATE(node
);
503 KASSERT(priv
->numActiveMany
== 0,
504 ("%s: numActiveMany=%d", __func__
, priv
->numActiveMany
));
505 FREE(priv
, M_NETGRAPH
);
506 NG_NODE_SET_PRIVATE(node
, NULL
);
512 * Hook disconnection.
515 ng_one2many_disconnect(hook_p hook
)
517 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
520 /* Get link number */
521 linkNum
= (intptr_t)NG_HOOK_PRIVATE(hook
);
522 KASSERT(linkNum
== NG_ONE2MANY_ONE_LINKNUM
523 || (linkNum
>= 0 && linkNum
< NG_ONE2MANY_MAX_LINKS
),
524 ("%s: linkNum=%d", __func__
, linkNum
));
527 if (linkNum
== NG_ONE2MANY_ONE_LINKNUM
)
528 priv
->one
.hook
= NULL
;
530 priv
->many
[linkNum
].hook
= NULL
;
531 priv
->conf
.enabledLinks
[linkNum
] = 0;
532 ng_one2many_update_many(priv
);
535 /* If no hooks left, go away */
536 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook
)) == 0)
537 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook
))))
538 ng_rmnode_self(NG_HOOK_NODE(hook
));
542 /******************************************************************
544 ******************************************************************/
547 * Update internal state after the addition or removal of a "many" link
550 ng_one2many_update_many(priv_p priv
)
552 uint16_t saveActive
= priv
->numActiveMany
;
555 /* Update list of which "many" links are up */
556 priv
->numActiveMany
= 0;
557 for (linkNum
= 0; linkNum
< NG_ONE2MANY_MAX_LINKS
; linkNum
++) {
558 switch (priv
->conf
.failAlg
) {
559 case NG_ONE2MANY_FAIL_MANUAL
:
560 case NG_ONE2MANY_FAIL_NOTIFY
:
561 if (priv
->many
[linkNum
].hook
!= NULL
562 && priv
->conf
.enabledLinks
[linkNum
]) {
563 priv
->activeMany
[priv
->numActiveMany
] = linkNum
;
564 priv
->numActiveMany
++;
569 panic("%s: invalid failAlg", __func__
);
574 if (priv
->numActiveMany
== 0 && saveActive
> 0)
575 ng_one2many_notify(priv
, NGM_LINK_IS_DOWN
);
577 if (saveActive
== 0 && priv
->numActiveMany
> 0)
578 ng_one2many_notify(priv
, NGM_LINK_IS_UP
);
580 /* Update transmit algorithm state */
581 switch (priv
->conf
.xmitAlg
) {
582 case NG_ONE2MANY_XMIT_ROUNDROBIN
:
583 if (priv
->numActiveMany
> 0)
584 priv
->nextMany
%= priv
->numActiveMany
;
586 case NG_ONE2MANY_XMIT_ALL
:
590 panic("%s: invalid xmitAlg", __func__
);
596 * Notify upstream if we are out of links, or we have at least one link.
599 ng_one2many_notify(priv_p priv
, uint32_t cmd
)
604 if (priv
->one
.hook
== NULL
)
607 NG_MKMESSAGE(msg
, NGM_FLOW_COOKIE
, cmd
, 0, M_WAITOK
| M_NULLOK
);
609 NG_SEND_MSG_HOOK(dummy_error
, priv
->node
, msg
, priv
->one
.hook
, 0);