Deleted Added
full compact
ng_one2many.c (106665) ng_one2many.c (108107)
1
2/*
3 * ng_one2many.c
4 *
5 * Copyright (c) 2000 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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.
18 *
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
35 * OF SUCH DAMAGE.
36 *
37 * Author: Archie Cobbs <archie@freebsd.org>
38 *
1
2/*
3 * ng_one2many.c
4 *
5 * Copyright (c) 2000 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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.
18 *
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
35 * OF SUCH DAMAGE.
36 *
37 * Author: Archie Cobbs <archie@freebsd.org>
38 *
39 * $FreeBSD: head/sys/netgraph/ng_one2many.c 106665 2002-11-08 21:13:18Z jhb $
39 * $FreeBSD: head/sys/netgraph/ng_one2many.c 108107 2002-12-19 22:58:27Z bmilekic $
40 */
41
42/*
43 * ng_one2many(4) netgraph node type
44 *
45 * Packets received on the "one" hook are sent out each of the
46 * "many" hooks accoring to an algorithm. Packets received on any
47 * "many" hook are always delivered to the "one" hook.
48 */
49
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>
55#include <sys/mbuf.h>
56#include <sys/errno.h>
57
58#include <netgraph/ng_message.h>
59#include <netgraph/netgraph.h>
60#include <netgraph/ng_parse.h>
61#include <netgraph/ng_one2many.h>
62
63/* Per-link private data */
64struct ng_one2many_link {
65 hook_p hook; /* netgraph hook */
66 struct ng_one2many_link_stats stats; /* link stats */
67};
68
69/* Per-node private data */
70struct 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];
77};
78typedef struct ng_one2many_private *priv_p;
79
80/* Netgraph node methods */
81static ng_constructor_t ng_one2many_constructor;
82static ng_rcvmsg_t ng_one2many_rcvmsg;
83static ng_shutdown_t ng_one2many_shutdown;
84static ng_newhook_t ng_one2many_newhook;
85static ng_rcvdata_t ng_one2many_rcvdata;
86static ng_disconnect_t ng_one2many_disconnect;
87
88/* Other functions */
89static void ng_one2many_update_many(priv_p priv);
90
91/* Store each hook's link number in the private field */
92#define LINK_NUM(hook) (*(int16_t *)(&(hook)->private))
93
94/******************************************************************
95 NETGRAPH PARSE TYPES
96******************************************************************/
97
98/* Parse type for struct ng_one2many_config */
99static const struct ng_parse_fixedarray_info
100 ng_one2many_enableLinks_array_type_info = {
101 &ng_parse_uint8_type,
102 NG_ONE2MANY_MAX_LINKS
103};
104static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
105 &ng_parse_fixedarray_type,
106 &ng_one2many_enableLinks_array_type_info,
107};
108static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
109 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
110static 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 */
116static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
117 = NG_ONE2MANY_LINK_STATS_TYPE_INFO;
118static 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 */
124static 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 */
164static struct ng_type ng_one2many_typestruct = {
165 NG_ABI_VERSION,
166 NG_ONE2MANY_NODE_TYPE,
167 NULL,
168 ng_one2many_constructor,
169 ng_one2many_rcvmsg,
170 ng_one2many_shutdown,
171 ng_one2many_newhook,
172 NULL,
173 NULL,
174 ng_one2many_rcvdata,
175 ng_one2many_disconnect,
176 ng_one2many_cmdlist,
177};
178NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
179
180/******************************************************************
181 NETGRAPH NODE METHODS
182******************************************************************/
183
184/*
185 * Node constructor
186 */
187static int
188ng_one2many_constructor(node_p node)
189{
190 priv_p priv;
191
192 /* Allocate and initialize private info */
193 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
194 if (priv == NULL)
195 return (ENOMEM);
196 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
197 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
198
199 NG_NODE_SET_PRIVATE(node, priv);
200
201 /* Done */
202 return (0);
203}
204
205/*
206 * Method for attaching a new hook
207 */
208static int
209ng_one2many_newhook(node_p node, hook_p hook, const char *name)
210{
211 const priv_p priv = NG_NODE_PRIVATE(node);
212 struct ng_one2many_link *link;
213 int linkNum;
214 u_long i;
215
216 /* Which hook? */
217 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
218 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
219 const char *cp;
220 char *eptr;
221
222 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
223 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
224 return (EINVAL);
225 i = strtoul(cp, &eptr, 10);
226 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
227 return (EINVAL);
228 linkNum = (int)i;
229 link = &priv->many[linkNum];
230 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
231 linkNum = NG_ONE2MANY_ONE_LINKNUM;
232 link = &priv->one;
233 } else
234 return (EINVAL);
235
236 /* Is hook already connected? (should never happen) */
237 if (link->hook != NULL)
238 return (EISCONN);
239
240 /* Setup private info for this link */
241 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
242 link->hook = hook;
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);
247 }
248
249 /* Done */
250 return (0);
251}
252
253/*
254 * Receive a control message
255 */
256static int
257ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
258{
259 const priv_p priv = NG_NODE_PRIVATE(node);
260 struct ng_mesg *resp = NULL;
261 int error = 0;
262 struct ng_mesg *msg;
263
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:
269 {
270 struct ng_one2many_config *conf;
271 int i;
272
273 /* Check that new configuration is valid */
274 if (msg->header.arglen != sizeof(*conf)) {
275 error = EINVAL;
276 break;
277 }
278 conf = (struct ng_one2many_config *)msg->data;
279 switch (conf->xmitAlg) {
280 case NG_ONE2MANY_XMIT_ROUNDROBIN:
281 case NG_ONE2MANY_XMIT_ALL:
282 break;
283 default:
284 error = EINVAL;
285 break;
286 }
287 switch (conf->failAlg) {
288 case NG_ONE2MANY_FAIL_MANUAL:
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 default:
363 error = EINVAL;
364 break;
365 }
366
367 /* Done */
368 NG_RESPOND_MSG(error, node, item, resp);
369 NG_FREE_MSG(msg);
370 return (error);
371}
372
373/*
374 * Receive data on a hook
375 */
376static int
377ng_one2many_rcvdata(hook_p hook, item_p item)
378{
379 const node_p node = NG_HOOK_NODE(hook);
380 const priv_p priv = NG_NODE_PRIVATE(node);
381 struct ng_one2many_link *src;
382 struct ng_one2many_link *dst = NULL;
383 int error = 0;
384 int linkNum;
385 int i;
386 struct mbuf *m;
387 meta_p meta;
388
389 m = NGI_M(item); /* just peaking, mbuf still owned by item */
390 /* Get link number */
391 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
392 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
393 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
394 ("%s: linkNum=%d", __func__, linkNum));
395
396 /* Figure out source link */
397 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
398 &priv->one : &priv->many[linkNum];
399 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
400
401 /* Update receive stats */
402 src->stats.recvPackets++;
403 src->stats.recvOctets += m->m_pkthdr.len;
404
405 /* Figure out destination link */
406 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
407 if (priv->numActiveMany == 0) {
408 NG_FREE_ITEM(item);
409 return (ENOTCONN);
410 }
411 switch(priv->conf.xmitAlg) {
412 case NG_ONE2MANY_XMIT_ROUNDROBIN:
413 dst = &priv->many[priv->activeMany[priv->nextMany]];
414 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
415 break;
416 case NG_ONE2MANY_XMIT_ALL:
417 meta = NGI_META(item); /* peek.. */
418 /* no need to copy data for the 1st one */
419 dst = &priv->many[priv->activeMany[0]];
420
421 /* make copies of data and send for all links
422 * except the first one, which we'll do last
423 */
424 for (i = 1; i < priv->numActiveMany; i++) {
425 meta_p meta2 = NULL;
426 struct mbuf *m2;
427 struct ng_one2many_link *mdst;
428
429 mdst = &priv->many[priv->activeMany[i]];
40 */
41
42/*
43 * ng_one2many(4) netgraph node type
44 *
45 * Packets received on the "one" hook are sent out each of the
46 * "many" hooks accoring to an algorithm. Packets received on any
47 * "many" hook are always delivered to the "one" hook.
48 */
49
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>
55#include <sys/mbuf.h>
56#include <sys/errno.h>
57
58#include <netgraph/ng_message.h>
59#include <netgraph/netgraph.h>
60#include <netgraph/ng_parse.h>
61#include <netgraph/ng_one2many.h>
62
63/* Per-link private data */
64struct ng_one2many_link {
65 hook_p hook; /* netgraph hook */
66 struct ng_one2many_link_stats stats; /* link stats */
67};
68
69/* Per-node private data */
70struct 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];
77};
78typedef struct ng_one2many_private *priv_p;
79
80/* Netgraph node methods */
81static ng_constructor_t ng_one2many_constructor;
82static ng_rcvmsg_t ng_one2many_rcvmsg;
83static ng_shutdown_t ng_one2many_shutdown;
84static ng_newhook_t ng_one2many_newhook;
85static ng_rcvdata_t ng_one2many_rcvdata;
86static ng_disconnect_t ng_one2many_disconnect;
87
88/* Other functions */
89static void ng_one2many_update_many(priv_p priv);
90
91/* Store each hook's link number in the private field */
92#define LINK_NUM(hook) (*(int16_t *)(&(hook)->private))
93
94/******************************************************************
95 NETGRAPH PARSE TYPES
96******************************************************************/
97
98/* Parse type for struct ng_one2many_config */
99static const struct ng_parse_fixedarray_info
100 ng_one2many_enableLinks_array_type_info = {
101 &ng_parse_uint8_type,
102 NG_ONE2MANY_MAX_LINKS
103};
104static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
105 &ng_parse_fixedarray_type,
106 &ng_one2many_enableLinks_array_type_info,
107};
108static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
109 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
110static 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 */
116static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
117 = NG_ONE2MANY_LINK_STATS_TYPE_INFO;
118static 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 */
124static 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 */
164static struct ng_type ng_one2many_typestruct = {
165 NG_ABI_VERSION,
166 NG_ONE2MANY_NODE_TYPE,
167 NULL,
168 ng_one2many_constructor,
169 ng_one2many_rcvmsg,
170 ng_one2many_shutdown,
171 ng_one2many_newhook,
172 NULL,
173 NULL,
174 ng_one2many_rcvdata,
175 ng_one2many_disconnect,
176 ng_one2many_cmdlist,
177};
178NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
179
180/******************************************************************
181 NETGRAPH NODE METHODS
182******************************************************************/
183
184/*
185 * Node constructor
186 */
187static int
188ng_one2many_constructor(node_p node)
189{
190 priv_p priv;
191
192 /* Allocate and initialize private info */
193 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
194 if (priv == NULL)
195 return (ENOMEM);
196 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
197 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
198
199 NG_NODE_SET_PRIVATE(node, priv);
200
201 /* Done */
202 return (0);
203}
204
205/*
206 * Method for attaching a new hook
207 */
208static int
209ng_one2many_newhook(node_p node, hook_p hook, const char *name)
210{
211 const priv_p priv = NG_NODE_PRIVATE(node);
212 struct ng_one2many_link *link;
213 int linkNum;
214 u_long i;
215
216 /* Which hook? */
217 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
218 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
219 const char *cp;
220 char *eptr;
221
222 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
223 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
224 return (EINVAL);
225 i = strtoul(cp, &eptr, 10);
226 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
227 return (EINVAL);
228 linkNum = (int)i;
229 link = &priv->many[linkNum];
230 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
231 linkNum = NG_ONE2MANY_ONE_LINKNUM;
232 link = &priv->one;
233 } else
234 return (EINVAL);
235
236 /* Is hook already connected? (should never happen) */
237 if (link->hook != NULL)
238 return (EISCONN);
239
240 /* Setup private info for this link */
241 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
242 link->hook = hook;
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);
247 }
248
249 /* Done */
250 return (0);
251}
252
253/*
254 * Receive a control message
255 */
256static int
257ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
258{
259 const priv_p priv = NG_NODE_PRIVATE(node);
260 struct ng_mesg *resp = NULL;
261 int error = 0;
262 struct ng_mesg *msg;
263
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:
269 {
270 struct ng_one2many_config *conf;
271 int i;
272
273 /* Check that new configuration is valid */
274 if (msg->header.arglen != sizeof(*conf)) {
275 error = EINVAL;
276 break;
277 }
278 conf = (struct ng_one2many_config *)msg->data;
279 switch (conf->xmitAlg) {
280 case NG_ONE2MANY_XMIT_ROUNDROBIN:
281 case NG_ONE2MANY_XMIT_ALL:
282 break;
283 default:
284 error = EINVAL;
285 break;
286 }
287 switch (conf->failAlg) {
288 case NG_ONE2MANY_FAIL_MANUAL:
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 default:
363 error = EINVAL;
364 break;
365 }
366
367 /* Done */
368 NG_RESPOND_MSG(error, node, item, resp);
369 NG_FREE_MSG(msg);
370 return (error);
371}
372
373/*
374 * Receive data on a hook
375 */
376static int
377ng_one2many_rcvdata(hook_p hook, item_p item)
378{
379 const node_p node = NG_HOOK_NODE(hook);
380 const priv_p priv = NG_NODE_PRIVATE(node);
381 struct ng_one2many_link *src;
382 struct ng_one2many_link *dst = NULL;
383 int error = 0;
384 int linkNum;
385 int i;
386 struct mbuf *m;
387 meta_p meta;
388
389 m = NGI_M(item); /* just peaking, mbuf still owned by item */
390 /* Get link number */
391 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
392 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
393 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
394 ("%s: linkNum=%d", __func__, linkNum));
395
396 /* Figure out source link */
397 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
398 &priv->one : &priv->many[linkNum];
399 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
400
401 /* Update receive stats */
402 src->stats.recvPackets++;
403 src->stats.recvOctets += m->m_pkthdr.len;
404
405 /* Figure out destination link */
406 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
407 if (priv->numActiveMany == 0) {
408 NG_FREE_ITEM(item);
409 return (ENOTCONN);
410 }
411 switch(priv->conf.xmitAlg) {
412 case NG_ONE2MANY_XMIT_ROUNDROBIN:
413 dst = &priv->many[priv->activeMany[priv->nextMany]];
414 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
415 break;
416 case NG_ONE2MANY_XMIT_ALL:
417 meta = NGI_META(item); /* peek.. */
418 /* no need to copy data for the 1st one */
419 dst = &priv->many[priv->activeMany[0]];
420
421 /* make copies of data and send for all links
422 * except the first one, which we'll do last
423 */
424 for (i = 1; i < priv->numActiveMany; i++) {
425 meta_p meta2 = NULL;
426 struct mbuf *m2;
427 struct ng_one2many_link *mdst;
428
429 mdst = &priv->many[priv->activeMany[i]];
430 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */
430 m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */
431 if (m2 == NULL) {
432 mdst->stats.memoryFailures++;
433 NG_FREE_ITEM(item);
434 NG_FREE_M(m);
435 return (ENOBUFS);
436 }
437 if (meta != NULL
438 && (meta2 = ng_copy_meta(meta)) == NULL) {
439 mdst->stats.memoryFailures++;
440 m_freem(m2);
441 NG_FREE_ITEM(item);
442 NG_FREE_M(m);
443 return (ENOMEM);
444 }
445 /* Update transmit stats */
446 mdst->stats.xmitPackets++;
447 mdst->stats.xmitOctets += m->m_pkthdr.len;
448 NG_SEND_DATA(error, mdst->hook, m2, meta2);
449 }
450 break;
451#ifdef INVARIANTS
452 default:
453 panic("%s: invalid xmitAlg", __func__);
454#endif
455 }
456 } else {
457 dst = &priv->one;
458 }
459
460 /* Update transmit stats */
461 dst->stats.xmitPackets++;
462 dst->stats.xmitOctets += m->m_pkthdr.len;
463
464 /* Deliver packet */
465 NG_FWD_ITEM_HOOK(error, item, dst->hook);
466 return (error);
467}
468
469/*
470 * Shutdown node
471 */
472static int
473ng_one2many_shutdown(node_p node)
474{
475 const priv_p priv = NG_NODE_PRIVATE(node);
476
477 KASSERT(priv->numActiveMany == 0,
478 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
479 FREE(priv, M_NETGRAPH);
480 NG_NODE_SET_PRIVATE(node, NULL);
481 NG_NODE_UNREF(node);
482 return (0);
483}
484
485/*
486 * Hook disconnection.
487 */
488static int
489ng_one2many_disconnect(hook_p hook)
490{
491 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
492 int linkNum;
493
494 /* Get link number */
495 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
496 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
497 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
498 ("%s: linkNum=%d", __func__, linkNum));
499
500 /* Nuke the link */
501 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
502 priv->one.hook = NULL;
503 else {
504 priv->many[linkNum].hook = NULL;
505 priv->conf.enabledLinks[linkNum] = 0;
506 ng_one2many_update_many(priv);
507 }
508
509 /* If no hooks left, go away */
510 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
511 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
512 ng_rmnode_self(NG_HOOK_NODE(hook));
513 return (0);
514}
515
516/******************************************************************
517 OTHER FUNCTIONS
518******************************************************************/
519
520/*
521 * Update internal state after the addition or removal of a "many" link
522 */
523static void
524ng_one2many_update_many(priv_p priv)
525{
526 int linkNum;
527
528 /* Update list of which "many" links are up */
529 priv->numActiveMany = 0;
530 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
531 switch (priv->conf.failAlg) {
532 case NG_ONE2MANY_FAIL_MANUAL:
533 if (priv->many[linkNum].hook != NULL
534 && priv->conf.enabledLinks[linkNum]) {
535 priv->activeMany[priv->numActiveMany] = linkNum;
536 priv->numActiveMany++;
537 }
538 break;
539#ifdef INVARIANTS
540 default:
541 panic("%s: invalid failAlg", __func__);
542#endif
543 }
544 }
545
546 /* Update transmit algorithm state */
547 switch (priv->conf.xmitAlg) {
548 case NG_ONE2MANY_XMIT_ROUNDROBIN:
549 if (priv->numActiveMany > 0)
550 priv->nextMany %= priv->numActiveMany;
551 break;
552 case NG_ONE2MANY_XMIT_ALL:
553 break;
554#ifdef INVARIANTS
555 default:
556 panic("%s: invalid xmitAlg", __func__);
557#endif
558 }
559}
560
561
431 if (m2 == NULL) {
432 mdst->stats.memoryFailures++;
433 NG_FREE_ITEM(item);
434 NG_FREE_M(m);
435 return (ENOBUFS);
436 }
437 if (meta != NULL
438 && (meta2 = ng_copy_meta(meta)) == NULL) {
439 mdst->stats.memoryFailures++;
440 m_freem(m2);
441 NG_FREE_ITEM(item);
442 NG_FREE_M(m);
443 return (ENOMEM);
444 }
445 /* Update transmit stats */
446 mdst->stats.xmitPackets++;
447 mdst->stats.xmitOctets += m->m_pkthdr.len;
448 NG_SEND_DATA(error, mdst->hook, m2, meta2);
449 }
450 break;
451#ifdef INVARIANTS
452 default:
453 panic("%s: invalid xmitAlg", __func__);
454#endif
455 }
456 } else {
457 dst = &priv->one;
458 }
459
460 /* Update transmit stats */
461 dst->stats.xmitPackets++;
462 dst->stats.xmitOctets += m->m_pkthdr.len;
463
464 /* Deliver packet */
465 NG_FWD_ITEM_HOOK(error, item, dst->hook);
466 return (error);
467}
468
469/*
470 * Shutdown node
471 */
472static int
473ng_one2many_shutdown(node_p node)
474{
475 const priv_p priv = NG_NODE_PRIVATE(node);
476
477 KASSERT(priv->numActiveMany == 0,
478 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
479 FREE(priv, M_NETGRAPH);
480 NG_NODE_SET_PRIVATE(node, NULL);
481 NG_NODE_UNREF(node);
482 return (0);
483}
484
485/*
486 * Hook disconnection.
487 */
488static int
489ng_one2many_disconnect(hook_p hook)
490{
491 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
492 int linkNum;
493
494 /* Get link number */
495 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
496 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
497 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
498 ("%s: linkNum=%d", __func__, linkNum));
499
500 /* Nuke the link */
501 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
502 priv->one.hook = NULL;
503 else {
504 priv->many[linkNum].hook = NULL;
505 priv->conf.enabledLinks[linkNum] = 0;
506 ng_one2many_update_many(priv);
507 }
508
509 /* If no hooks left, go away */
510 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
511 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
512 ng_rmnode_self(NG_HOOK_NODE(hook));
513 return (0);
514}
515
516/******************************************************************
517 OTHER FUNCTIONS
518******************************************************************/
519
520/*
521 * Update internal state after the addition or removal of a "many" link
522 */
523static void
524ng_one2many_update_many(priv_p priv)
525{
526 int linkNum;
527
528 /* Update list of which "many" links are up */
529 priv->numActiveMany = 0;
530 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
531 switch (priv->conf.failAlg) {
532 case NG_ONE2MANY_FAIL_MANUAL:
533 if (priv->many[linkNum].hook != NULL
534 && priv->conf.enabledLinks[linkNum]) {
535 priv->activeMany[priv->numActiveMany] = linkNum;
536 priv->numActiveMany++;
537 }
538 break;
539#ifdef INVARIANTS
540 default:
541 panic("%s: invalid failAlg", __func__);
542#endif
543 }
544 }
545
546 /* Update transmit algorithm state */
547 switch (priv->conf.xmitAlg) {
548 case NG_ONE2MANY_XMIT_ROUNDROBIN:
549 if (priv->numActiveMany > 0)
550 priv->nextMany %= priv->numActiveMany;
551 break;
552 case NG_ONE2MANY_XMIT_ALL:
553 break;
554#ifdef INVARIANTS
555 default:
556 panic("%s: invalid xmitAlg", __func__);
557#endif
558 }
559}
560
561