ng_one2many.c (126012) | ng_one2many.c (126035) |
---|---|
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 --- 22 unchanged lines hidden (view full) --- 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 --- 22 unchanged lines hidden (view full) --- 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 126012 2004-02-19 17:04:23Z pjd $ | 39 * $FreeBSD: head/sys/netgraph/ng_one2many.c 126035 2004-02-20 08:26:27Z pjd $ |
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> | 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/socket.h> 57#include <sys/sockio.h> | |
58#include <sys/errno.h> 59 | 56#include <sys/errno.h> 57 |
60#include <net/if.h> 61#include <net/if_media.h> 62 | |
63#include <netgraph/ng_message.h> 64#include <netgraph/netgraph.h> 65#include <netgraph/ng_parse.h> | 58#include <netgraph/ng_message.h> 59#include <netgraph/netgraph.h> 60#include <netgraph/ng_parse.h> |
66#include <netgraph/ng_ether.h> | |
67#include <netgraph/ng_one2many.h> 68 69/* Per-link private data */ 70struct ng_one2many_link { 71 hook_p hook; /* netgraph hook */ 72 struct ng_one2many_link_stats stats; /* link stats */ 73}; 74 75/* Per-node private data */ 76struct ng_one2many_private { 77 struct ng_one2many_config conf; /* node configuration */ 78 struct ng_one2many_link one; /* "one" hook */ 79 struct ng_one2many_link many[NG_ONE2MANY_MAX_LINKS]; 80 u_int16_t nextMany; /* next round-robin */ 81 u_int16_t numActiveMany; /* # active "many" */ 82 u_int16_t activeMany[NG_ONE2MANY_MAX_LINKS]; | 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]; |
83 struct callout_handle callout; | |
84}; 85typedef struct ng_one2many_private *priv_p; 86 87/* Netgraph node methods */ 88static ng_constructor_t ng_one2many_constructor; 89static ng_rcvmsg_t ng_one2many_rcvmsg; 90static ng_shutdown_t ng_one2many_shutdown; 91static ng_newhook_t ng_one2many_newhook; 92static ng_rcvdata_t ng_one2many_rcvdata; 93static ng_disconnect_t ng_one2many_disconnect; 94 95/* Other functions */ | 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 */ |
96static void ng_one2many_scan(node_p node, hook_p hook __unused, 97 void *arg1 __unused, int arg2 __unused); | |
98static void ng_one2many_update_many(priv_p priv); 99 100/* Store each hook's link number in the private field */ 101#define LINK_NUM(hook) (*(int16_t *)(&(hook)->private)) 102 103/****************************************************************** 104 NETGRAPH PARSE TYPES 105******************************************************************/ --- 93 unchanged lines hidden (view full) --- 199 priv_p priv; 200 201 /* Allocate and initialize private info */ 202 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 203 if (priv == NULL) 204 return (ENOMEM); 205 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN; 206 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL; | 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******************************************************************/ --- 93 unchanged lines hidden (view full) --- 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; |
207 priv->conf.interval = 5; | |
208 209 NG_NODE_SET_PRIVATE(node, priv); 210 | 198 199 NG_NODE_SET_PRIVATE(node, priv); 200 |
211 if (priv->conf.failAlg == NG_ONE2MANY_FAIL_IFACE_LINK) { 212 priv->callout = ng_timeout(node, NULL, priv->conf.interval * hz, 213 ng_one2many_scan, NULL, 0); 214 } 215 | |
216 /* Done */ 217 return (0); 218} 219 220/* 221 * Method for attaching a new hook 222 */ 223static int --- 72 unchanged lines hidden (view full) --- 296 case NG_ONE2MANY_XMIT_ALL: 297 break; 298 default: 299 error = EINVAL; 300 break; 301 } 302 switch (conf->failAlg) { 303 case NG_ONE2MANY_FAIL_MANUAL: | 201 /* Done */ 202 return (0); 203} 204 205/* 206 * Method for attaching a new hook 207 */ 208static int --- 72 unchanged lines hidden (view full) --- 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: |
304 case NG_ONE2MANY_FAIL_IFACE_LINK: | |
305 break; 306 default: 307 error = EINVAL; 308 break; 309 } 310 if (error != 0) 311 break; 312 313 /* Normalized many link enabled bits */ 314 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++) 315 conf->enabledLinks[i] = !!conf->enabledLinks[i]; 316 317 /* Copy config and reset */ 318 bcopy(conf, &priv->conf, sizeof(*conf)); 319 ng_one2many_update_many(priv); | 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); |
320 321 ng_untimeout(priv->callout, node); 322 if (priv->conf.failAlg == NG_ONE2MANY_FAIL_IFACE_LINK) { 323 priv->callout = ng_timeout(node, NULL, 324 priv->conf.interval * hz, ng_one2many_scan, 325 NULL, 0); 326 } | |
327 break; 328 } 329 case NGM_ONE2MANY_GET_CONFIG: 330 { 331 struct ng_one2many_config *conf; 332 333 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT); 334 if (resp == NULL) { --- 159 unchanged lines hidden (view full) --- 494 */ 495static int 496ng_one2many_shutdown(node_p node) 497{ 498 const priv_p priv = NG_NODE_PRIVATE(node); 499 500 KASSERT(priv->numActiveMany == 0, 501 ("%s: numActiveMany=%d", __func__, priv->numActiveMany)); | 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) { --- 159 unchanged lines hidden (view full) --- 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)); |
502 if (priv->conf.failAlg == NG_ONE2MANY_FAIL_IFACE_LINK) 503 ng_untimeout(priv->callout, node); | |
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. --- 25 unchanged lines hidden (view full) --- 537 ng_rmnode_self(NG_HOOK_NODE(hook)); 538 return (0); 539} 540 541/****************************************************************** 542 OTHER FUNCTIONS 543******************************************************************/ 544 | 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. --- 25 unchanged lines hidden (view full) --- 512 ng_rmnode_self(NG_HOOK_NODE(hook)); 513 return (0); 514} 515 516/****************************************************************** 517 OTHER FUNCTIONS 518******************************************************************/ 519 |
545#if 0 | |
546/* | 520/* |
547 * Get interface name. 548 */ 549static const char * 550ng_one2many_ifname(struct ng_one2many_link *link) 551{ 552 node_p node; 553 554 node = link->hook->hk_peer->hk_node; 555 if (strcmp(node->nd_type->name, "ether") != 0) 556 return ("unknown"); 557 return (node->nd_name); 558} 559#endif 560 561/* 562 * Check if interface related to given node is active. 563 */ 564static int 565ng_one2many_active(struct ng_one2many_link *link) 566{ 567 struct ng_ether_private *ethpriv; 568 struct ifmediareq ifmr; 569 struct ifnet *ifp; 570 node_p node; 571 int error; 572 573 node = link->hook->hk_peer->hk_node; 574 if (strcmp(node->nd_type->name, "ether") != 0) 575 return (0); 576 ethpriv = NG_NODE_PRIVATE(node); 577 ifp = ethpriv->ifp; 578 bzero(&ifmr, sizeof(ifmr)); 579 error = ifp->if_ioctl(ifp, SIOCGIFMEDIA, (char *)&ifmr); 580 if (error != 0) 581 return (0); 582 if ((ifmr.ifm_status & IFM_ACTIVE) == 0) 583 return (0); 584 return (1); 585} 586 587/* 588 * Check every priv->conf.interval seconds for active links. 589 */ 590static void 591ng_one2many_scan(node_p node, hook_p hook __unused, void *arg1 __unused, 592 int arg2 __unused) 593{ 594 const priv_p priv = NG_NODE_PRIVATE(node); 595 596 ng_one2many_update_many(priv); 597 598 priv->callout = ng_timeout(node, NULL, priv->conf.interval * hz, 599 ng_one2many_scan, NULL, 0); 600} 601 602/* | |
603 * Update internal state after the addition or removal of a "many" link 604 */ 605static void 606ng_one2many_update_many(priv_p priv) 607{ 608 int linkNum; 609 610 /* Update list of which "many" links are up */ 611 priv->numActiveMany = 0; 612 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) { 613 switch (priv->conf.failAlg) { 614 case NG_ONE2MANY_FAIL_MANUAL: 615 if (priv->many[linkNum].hook != NULL 616 && priv->conf.enabledLinks[linkNum]) { 617 priv->activeMany[priv->numActiveMany] = linkNum; 618 priv->numActiveMany++; 619 } 620 break; | 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; |
621 case NG_ONE2MANY_FAIL_IFACE_LINK: 622 if (priv->many[linkNum].hook != NULL && 623 ng_one2many_active(&priv->many[linkNum])) { 624 priv->activeMany[priv->numActiveMany] = linkNum; 625 priv->numActiveMany++; 626 } 627 break; | |
628#ifdef INVARIANTS 629 default: 630 panic("%s: invalid failAlg", __func__); 631#endif 632 } 633 } 634 635 /* Update transmit algorithm state */ --- 15 unchanged lines hidden --- | 539#ifdef INVARIANTS 540 default: 541 panic("%s: invalid failAlg", __func__); 542#endif 543 } 544 } 545 546 /* Update transmit algorithm state */ --- 15 unchanged lines hidden --- |