ng_one2many.c revision 129823
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 129823 2004-05-29 00:51:19Z julian $ 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 .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}; 175NETGRAPH_INIT(one2many, &ng_one2many_typestruct); 176 177/****************************************************************** 178 NETGRAPH NODE METHODS 179******************************************************************/ 180 181/* 182 * Node constructor 183 */ 184static int 185ng_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 NG_NODE_SET_PRIVATE(node, priv); 197 198 /* Done */ 199 return (0); 200} 201 202/* 203 * Method for attaching a new hook 204 */ 205static int 206ng_one2many_newhook(node_p node, hook_p hook, const char *name) 207{ 208 const priv_p priv = NG_NODE_PRIVATE(node); 209 struct ng_one2many_link *link; 210 int linkNum; 211 u_long i; 212 213 /* Which hook? */ 214 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX, 215 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) { 216 const char *cp; 217 char *eptr; 218 219 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX); 220 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 221 return (EINVAL); 222 i = strtoul(cp, &eptr, 10); 223 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS) 224 return (EINVAL); 225 linkNum = (int)i; 226 link = &priv->many[linkNum]; 227 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) { 228 linkNum = NG_ONE2MANY_ONE_LINKNUM; 229 link = &priv->one; 230 } else 231 return (EINVAL); 232 233 /* Is hook already connected? (should never happen) */ 234 if (link->hook != NULL) 235 return (EISCONN); 236 237 /* Setup private info for this link */ 238 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum); 239 link->hook = hook; 240 bzero(&link->stats, sizeof(link->stats)); 241 if (linkNum != NG_ONE2MANY_ONE_LINKNUM) { 242 priv->conf.enabledLinks[linkNum] = 1; /* auto-enable link */ 243 ng_one2many_update_many(priv); 244 } 245 246 /* Done */ 247 return (0); 248} 249 250/* 251 * Receive a control message 252 */ 253static int 254ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook) 255{ 256 const priv_p priv = NG_NODE_PRIVATE(node); 257 struct ng_mesg *resp = NULL; 258 int error = 0; 259 struct ng_mesg *msg; 260 261 NGI_GET_MSG(item, msg); 262 switch (msg->header.typecookie) { 263 case NGM_ONE2MANY_COOKIE: 264 switch (msg->header.cmd) { 265 case NGM_ONE2MANY_SET_CONFIG: 266 { 267 struct ng_one2many_config *conf; 268 int i; 269 270 /* Check that new configuration is valid */ 271 if (msg->header.arglen != sizeof(*conf)) { 272 error = EINVAL; 273 break; 274 } 275 conf = (struct ng_one2many_config *)msg->data; 276 switch (conf->xmitAlg) { 277 case NG_ONE2MANY_XMIT_ROUNDROBIN: 278 case NG_ONE2MANY_XMIT_ALL: 279 break; 280 default: 281 error = EINVAL; 282 break; 283 } 284 switch (conf->failAlg) { 285 case NG_ONE2MANY_FAIL_MANUAL: 286 break; 287 default: 288 error = EINVAL; 289 break; 290 } 291 if (error != 0) 292 break; 293 294 /* Normalized many link enabled bits */ 295 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++) 296 conf->enabledLinks[i] = !!conf->enabledLinks[i]; 297 298 /* Copy config and reset */ 299 bcopy(conf, &priv->conf, sizeof(*conf)); 300 ng_one2many_update_many(priv); 301 break; 302 } 303 case NGM_ONE2MANY_GET_CONFIG: 304 { 305 struct ng_one2many_config *conf; 306 307 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT); 308 if (resp == NULL) { 309 error = ENOMEM; 310 break; 311 } 312 conf = (struct ng_one2many_config *)resp->data; 313 bcopy(&priv->conf, conf, sizeof(priv->conf)); 314 break; 315 } 316 case NGM_ONE2MANY_GET_STATS: 317 case NGM_ONE2MANY_CLR_STATS: 318 case NGM_ONE2MANY_GETCLR_STATS: 319 { 320 struct ng_one2many_link *link; 321 int linkNum; 322 323 /* Get link */ 324 if (msg->header.arglen != sizeof(int32_t)) { 325 error = EINVAL; 326 break; 327 } 328 linkNum = *((int32_t *)msg->data); 329 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) 330 link = &priv->one; 331 else if (linkNum == 0 332 && linkNum < NG_ONE2MANY_MAX_LINKS) { 333 link = &priv->many[linkNum]; 334 } else { 335 error = EINVAL; 336 break; 337 } 338 339 /* Get/clear stats */ 340 if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) { 341 NG_MKRESPONSE(resp, msg, 342 sizeof(link->stats), M_NOWAIT); 343 if (resp == NULL) { 344 error = ENOMEM; 345 break; 346 } 347 bcopy(&link->stats, 348 resp->data, sizeof(link->stats)); 349 } 350 if (msg->header.cmd != NGM_ONE2MANY_GET_STATS) 351 bzero(&link->stats, sizeof(link->stats)); 352 break; 353 } 354 default: 355 error = EINVAL; 356 break; 357 } 358 break; 359 default: 360 error = EINVAL; 361 break; 362 } 363 364 /* Done */ 365 NG_RESPOND_MSG(error, node, item, resp); 366 NG_FREE_MSG(msg); 367 return (error); 368} 369 370/* 371 * Receive data on a hook 372 */ 373static int 374ng_one2many_rcvdata(hook_p hook, item_p item) 375{ 376 const node_p node = NG_HOOK_NODE(hook); 377 const priv_p priv = NG_NODE_PRIVATE(node); 378 struct ng_one2many_link *src; 379 struct ng_one2many_link *dst = NULL; 380 int error = 0; 381 int linkNum; 382 int i; 383 struct mbuf *m; 384 meta_p meta; 385 386 m = NGI_M(item); /* just peaking, mbuf still owned by item */ 387 /* Get link number */ 388 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 389 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM 390 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS), 391 ("%s: linkNum=%d", __func__, linkNum)); 392 393 /* Figure out source link */ 394 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ? 395 &priv->one : &priv->many[linkNum]; 396 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum)); 397 398 /* Update receive stats */ 399 src->stats.recvPackets++; 400 src->stats.recvOctets += m->m_pkthdr.len; 401 402 /* Figure out destination link */ 403 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) { 404 if (priv->numActiveMany == 0) { 405 NG_FREE_ITEM(item); 406 return (ENOTCONN); 407 } 408 switch(priv->conf.xmitAlg) { 409 case NG_ONE2MANY_XMIT_ROUNDROBIN: 410 dst = &priv->many[priv->activeMany[priv->nextMany]]; 411 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany; 412 break; 413 case NG_ONE2MANY_XMIT_ALL: 414 meta = NGI_META(item); /* peek.. */ 415 /* no need to copy data for the 1st one */ 416 dst = &priv->many[priv->activeMany[0]]; 417 418 /* make copies of data and send for all links 419 * except the first one, which we'll do last 420 */ 421 for (i = 1; i < priv->numActiveMany; i++) { 422 meta_p meta2 = NULL; 423 struct mbuf *m2; 424 struct ng_one2many_link *mdst; 425 426 mdst = &priv->many[priv->activeMany[i]]; 427 m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */ 428 if (m2 == NULL) { 429 mdst->stats.memoryFailures++; 430 NG_FREE_ITEM(item); 431 NG_FREE_M(m); 432 return (ENOBUFS); 433 } 434 if (meta != NULL 435 && (meta2 = ng_copy_meta(meta)) == NULL) { 436 mdst->stats.memoryFailures++; 437 m_freem(m2); 438 NG_FREE_ITEM(item); 439 NG_FREE_M(m); 440 return (ENOMEM); 441 } 442 /* Update transmit stats */ 443 mdst->stats.xmitPackets++; 444 mdst->stats.xmitOctets += m->m_pkthdr.len; 445 NG_SEND_DATA(error, mdst->hook, m2, meta2); 446 } 447 break; 448#ifdef INVARIANTS 449 default: 450 panic("%s: invalid xmitAlg", __func__); 451#endif 452 } 453 } else { 454 dst = &priv->one; 455 } 456 457 /* Update transmit stats */ 458 dst->stats.xmitPackets++; 459 dst->stats.xmitOctets += m->m_pkthdr.len; 460 461 /* Deliver packet */ 462 NG_FWD_ITEM_HOOK(error, item, dst->hook); 463 return (error); 464} 465 466/* 467 * Shutdown node 468 */ 469static int 470ng_one2many_shutdown(node_p node) 471{ 472 const priv_p priv = NG_NODE_PRIVATE(node); 473 474 KASSERT(priv->numActiveMany == 0, 475 ("%s: numActiveMany=%d", __func__, priv->numActiveMany)); 476 FREE(priv, M_NETGRAPH); 477 NG_NODE_SET_PRIVATE(node, NULL); 478 NG_NODE_UNREF(node); 479 return (0); 480} 481 482/* 483 * Hook disconnection. 484 */ 485static int 486ng_one2many_disconnect(hook_p hook) 487{ 488 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 489 int linkNum; 490 491 /* Get link number */ 492 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 493 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM 494 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS), 495 ("%s: linkNum=%d", __func__, linkNum)); 496 497 /* Nuke the link */ 498 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) 499 priv->one.hook = NULL; 500 else { 501 priv->many[linkNum].hook = NULL; 502 priv->conf.enabledLinks[linkNum] = 0; 503 ng_one2many_update_many(priv); 504 } 505 506 /* If no hooks left, go away */ 507 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 508 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 509 ng_rmnode_self(NG_HOOK_NODE(hook)); 510 return (0); 511} 512 513/****************************************************************** 514 OTHER FUNCTIONS 515******************************************************************/ 516 517/* 518 * Update internal state after the addition or removal of a "many" link 519 */ 520static void 521ng_one2many_update_many(priv_p priv) 522{ 523 int linkNum; 524 525 /* Update list of which "many" links are up */ 526 priv->numActiveMany = 0; 527 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) { 528 switch (priv->conf.failAlg) { 529 case NG_ONE2MANY_FAIL_MANUAL: 530 if (priv->many[linkNum].hook != NULL 531 && priv->conf.enabledLinks[linkNum]) { 532 priv->activeMany[priv->numActiveMany] = linkNum; 533 priv->numActiveMany++; 534 } 535 break; 536#ifdef INVARIANTS 537 default: 538 panic("%s: invalid failAlg", __func__); 539#endif 540 } 541 } 542 543 /* Update transmit algorithm state */ 544 switch (priv->conf.xmitAlg) { 545 case NG_ONE2MANY_XMIT_ROUNDROBIN: 546 if (priv->numActiveMany > 0) 547 priv->nextMany %= priv->numActiveMany; 548 break; 549 case NG_ONE2MANY_XMIT_ALL: 550 break; 551#ifdef INVARIANTS 552 default: 553 panic("%s: invalid xmitAlg", __func__); 554#endif 555 } 556} 557 558 559