ng_source.c revision 125030
1/* 2 * ng_source.c 3 * 4 * Copyright 2002 Sandvine Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or 9 * without modifications are expressly permitted by Sandvine Inc.; provided, 10 * however, that: 11 * 1. Any and all reproductions of the source or object code must include the 12 * copyright notice above and the following disclaimer of warranties; and 13 * 2. No rights are granted, in any manner or form, to use Sandvine Inc. 14 * trademarks, including the mark "SANDVINE" on advertising, endorsements, 15 * or otherwise except as such appears in the above copyright notice or in 16 * the software. 17 * 18 * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM 19 * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES, 20 * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, 21 * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 22 * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR 23 * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE 24 * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY 25 * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES 26 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 27 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 28 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 29 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH 33 * DAMAGE. 34 * 35 * Author: Dave Chapeskie <dchapeskie@sandvine.com> 36 * 37 * $FreeBSD: head/sys/netgraph/ng_source.c 125030 2004-01-26 14:44:36Z harti $ 38 */ 39 40/* 41 * This node is used for high speed packet geneneration. It queues 42 * all data recieved on it's 'input' hook and when told to start via 43 * a control message it sends the packets out it's 'output' hook. In 44 * this way this node can be preloaded with a packet stream which is 45 * continuously sent. 46 * 47 * Currently it just copies the mbufs as required. It could do various 48 * tricks to try and avoid this. Probably the best performance would 49 * be achieved by modifying the appropriate drivers to be told to 50 * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 51 * transmit descriptors) under control of this node; perhaps via some 52 * flag in the mbuf or some such. The node would peak at an appropriate 53 * ifnet flag to see if such support is available for the connected 54 * interface. 55 */ 56 57#include <sys/param.h> 58#include <sys/systm.h> 59#include <sys/errno.h> 60#include <sys/kernel.h> 61#include <sys/malloc.h> 62#include <sys/mbuf.h> 63#include <sys/socket.h> 64#include <net/if.h> 65#include <net/if_var.h> 66#include <netgraph/ng_message.h> 67#include <netgraph/netgraph.h> 68#include <netgraph/ng_parse.h> 69#include <netgraph/ng_ether.h> 70#include <netgraph/ng_source.h> 71 72#define NG_SOURCE_INTR_TICKS 1 73#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 74 75 76/* Per hook info */ 77struct source_hookinfo { 78 hook_p hook; 79}; 80 81/* Per node info */ 82struct privdata { 83 node_p node; 84 struct source_hookinfo input; 85 struct source_hookinfo output; 86 struct ng_source_stats stats; 87 struct ifqueue snd_queue; /* packets to send */ 88 struct ifnet *output_ifp; 89 struct callout_handle intr_ch; 90 u_int64_t packets; /* packets to send */ 91 u_int32_t queueOctets; 92}; 93typedef struct privdata *sc_p; 94 95/* Node flags */ 96#define NG_SOURCE_ACTIVE (NGF_TYPE1) 97 98/* XXX */ 99#if 1 100#undef KASSERT 101#define KASSERT(expr,msg) do { \ 102 if (!(expr)) { \ 103 printf msg ; \ 104 panic("Assertion"); \ 105 } \ 106 } while(0) 107#endif 108 109/* Netgraph methods */ 110static ng_constructor_t ng_source_constructor; 111static ng_rcvmsg_t ng_source_rcvmsg; 112static ng_shutdown_t ng_source_rmnode; 113static ng_newhook_t ng_source_newhook; 114static ng_rcvdata_t ng_source_rcvdata; 115static ng_disconnect_t ng_source_disconnect; 116 117/* Other functions */ 118static timeout_t ng_source_intr; 119static int ng_source_request_output_ifp (sc_p); 120static void ng_source_clr_data (sc_p); 121static void ng_source_start (sc_p); 122static void ng_source_stop (sc_p); 123static int ng_source_send (sc_p, int, int *); 124static int ng_source_store_output_ifp(sc_p sc, 125 struct ng_mesg *msg); 126 127 128/* Parse type for timeval */ 129static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = 130{ 131 { "tv_sec", &ng_parse_int32_type }, 132 { "tv_usec", &ng_parse_int32_type }, 133 { NULL } 134}; 135const struct ng_parse_type ng_source_timeval_type = { 136 &ng_parse_struct_type, 137 &ng_source_timeval_type_fields 138}; 139 140/* Parse type for struct ng_source_stats */ 141static const struct ng_parse_struct_field ng_source_stats_type_fields[] 142 = NG_SOURCE_STATS_TYPE_INFO; 143static const struct ng_parse_type ng_source_stats_type = { 144 &ng_parse_struct_type, 145 &ng_source_stats_type_fields 146}; 147 148/* List of commands and how to convert arguments to/from ASCII */ 149static const struct ng_cmdlist ng_source_cmds[] = { 150 { 151 NGM_SOURCE_COOKIE, 152 NGM_SOURCE_GET_STATS, 153 "getstats", 154 NULL, 155 &ng_source_stats_type 156 }, 157 { 158 NGM_SOURCE_COOKIE, 159 NGM_SOURCE_CLR_STATS, 160 "clrstats", 161 NULL, 162 NULL 163 }, 164 { 165 NGM_SOURCE_COOKIE, 166 NGM_SOURCE_GETCLR_STATS, 167 "getclrstats", 168 NULL, 169 &ng_source_stats_type 170 }, 171 { 172 NGM_SOURCE_COOKIE, 173 NGM_SOURCE_START, 174 "start", 175 &ng_parse_uint64_type, 176 NULL 177 }, 178 { 179 NGM_SOURCE_COOKIE, 180 NGM_SOURCE_STOP, 181 "stop", 182 NULL, 183 NULL 184 }, 185 { 186 NGM_SOURCE_COOKIE, 187 NGM_SOURCE_CLR_DATA, 188 "clrdata", 189 NULL, 190 NULL 191 }, 192 { 0 } 193}; 194 195/* Netgraph type descriptor */ 196static struct ng_type ng_source_typestruct = { 197 NG_ABI_VERSION, 198 NG_SOURCE_NODE_TYPE, 199 NULL, /* module event handler */ 200 ng_source_constructor, 201 ng_source_rcvmsg, 202 ng_source_rmnode, 203 ng_source_newhook, 204 NULL, /* findhook */ 205 NULL, 206 ng_source_rcvdata, /* rcvdata */ 207 ng_source_disconnect, 208 ng_source_cmds 209}; 210NETGRAPH_INIT(source, &ng_source_typestruct); 211 212/* 213 * Node constructor 214 */ 215static int 216ng_source_constructor(node_p node) 217{ 218 sc_p sc; 219 220 sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 221 if (sc == NULL) 222 return (ENOMEM); 223 224 NG_NODE_SET_PRIVATE(node, sc); 225 sc->node = node; 226 sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 227 callout_handle_init(&sc->intr_ch); /* XXX fix.. will 228 cause problems. */ 229 return (0); 230} 231 232/* 233 * Add a hook 234 */ 235static int 236ng_source_newhook(node_p node, hook_p hook, const char *name) 237{ 238 sc_p sc; 239 240 sc = NG_NODE_PRIVATE(node); 241 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 242 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 243 sc->input.hook = hook; 244 NG_HOOK_SET_PRIVATE(hook, &sc->input); 245 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 246 sc->output.hook = hook; 247 NG_HOOK_SET_PRIVATE(hook, &sc->output); 248 sc->output_ifp = 0; 249 bzero(&sc->stats, sizeof(sc->stats)); 250 } else 251 return (EINVAL); 252 return (0); 253} 254 255/* 256 * Receive a control message 257 */ 258static int 259ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 260{ 261 sc_p sc; 262 struct ng_mesg *resp = NULL; 263 int error = 0; 264 struct ng_mesg *msg; 265 266 sc = NG_NODE_PRIVATE(node); 267 NGI_GET_MSG(item, msg); 268 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 269 switch (msg->header.typecookie) { 270 case NGM_SOURCE_COOKIE: 271 if (msg->header.flags & NGF_RESP) { 272 error = EINVAL; 273 break; 274 } 275 switch (msg->header.cmd) { 276 case NGM_SOURCE_GET_STATS: 277 case NGM_SOURCE_CLR_STATS: 278 case NGM_SOURCE_GETCLR_STATS: 279 { 280 struct ng_source_stats *stats; 281 282 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 283 NG_MKRESPONSE(resp, msg, 284 sizeof(*stats), M_NOWAIT); 285 if (resp == NULL) { 286 error = ENOMEM; 287 goto done; 288 } 289 sc->stats.queueOctets = sc->queueOctets; 290 sc->stats.queueFrames = sc->snd_queue.ifq_len; 291 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 292 && !timevalisset(&sc->stats.endTime)) { 293 getmicrotime(&sc->stats.elapsedTime); 294 timevalsub(&sc->stats.elapsedTime, 295 &sc->stats.startTime); 296 } 297 stats = (struct ng_source_stats *)resp->data; 298 bcopy(&sc->stats, stats, sizeof(* stats)); 299 } 300 if (msg->header.cmd != NGM_SOURCE_GET_STATS) 301 bzero(&sc->stats, sizeof(sc->stats)); 302 } 303 break; 304 case NGM_SOURCE_START: 305 { 306 u_int64_t packets = *(u_int64_t *)msg->data; 307 if (sc->output.hook == NULL) { 308 printf("%s: start on node with no output hook\n" 309 , __FUNCTION__); 310 error = EINVAL; 311 break; 312 } 313 /* TODO validation of packets */ 314 sc->packets = packets; 315 ng_source_start(sc); 316 } 317 break; 318 case NGM_SOURCE_STOP: 319 ng_source_stop(sc); 320 break; 321 case NGM_SOURCE_CLR_DATA: 322 ng_source_clr_data(sc); 323 break; 324 default: 325 error = EINVAL; 326 break; 327 } 328 break; 329 case NGM_ETHER_COOKIE: 330 if (!(msg->header.flags & NGF_RESP)) { 331 error = EINVAL; 332 break; 333 } 334 switch (msg->header.cmd) { 335 case NGM_ETHER_GET_IFINDEX: 336 if (ng_source_store_output_ifp(sc, msg) == 0) { 337 ng_source_set_autosrc(sc, 0); 338 sc->node->nd_flags |= NG_SOURCE_ACTIVE; 339 timevalclear(&sc->stats.elapsedTime); 340 timevalclear(&sc->stats.endTime); 341 getmicrotime(&sc->stats.startTime); 342 sc->intr_ch = timeout(ng_source_intr, sc, 0); 343 } 344 break; 345 default: 346 error = EINVAL; 347 } 348 break; 349 default: 350 error = EINVAL; 351 break; 352 } 353 354done: 355 /* Take care of synchronous response, if any */ 356 NG_RESPOND_MSG(error, node, item, resp); 357 /* Free the message and return */ 358 NG_FREE_MSG(msg); 359 return (error); 360} 361 362/* 363 * Receive data on a hook 364 * 365 * If data comes in the input hook, enqueue it on the send queue. 366 * If data comes in the output hook, discard it. 367 */ 368static int 369ng_source_rcvdata(hook_p hook, item_p item) 370{ 371 sc_p sc; 372 struct source_hookinfo *hinfo; 373 int error = 0; 374 struct mbuf *m; 375 376 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 377 NGI_GET_M(item, m); 378 NG_FREE_ITEM(item); 379 hinfo = NG_HOOK_PRIVATE(hook); 380 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 381 KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__)); 382 383 /* Which hook? */ 384 if (hinfo == &sc->output) { 385 /* discard */ 386 NG_FREE_M(m); 387 return (error); 388 } 389 KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__)); 390 391 if ((m->m_flags & M_PKTHDR) == 0) { 392 printf("%s: mbuf without PKTHDR\n", __FUNCTION__); 393 NG_FREE_M(m); 394 return (EINVAL); 395 } 396 397 /* enque packet */ 398 /* XXX should we check IF_QFULL() ? */ 399 IF_ENQUEUE(&sc->snd_queue, m); 400 sc->queueOctets += m->m_pkthdr.len; 401 402 return (0); 403} 404 405/* 406 * Shutdown processing 407 */ 408static int 409ng_source_rmnode(node_p node) 410{ 411 sc_p sc; 412 413 sc = NG_NODE_PRIVATE(node); 414 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 415 node->nd_flags |= NG_INVALID; 416 ng_source_stop(sc); 417 ng_source_clr_data(sc); 418 NG_NODE_SET_PRIVATE(node, NULL); 419 NG_NODE_UNREF(node); 420 FREE(sc, M_NETGRAPH); 421 return (0); 422} 423 424/* 425 * Hook disconnection 426 */ 427static int 428ng_source_disconnect(hook_p hook) 429{ 430 struct source_hookinfo *hinfo; 431 sc_p sc; 432 433 hinfo = NG_HOOK_PRIVATE(hook); 434 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 435 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 436 hinfo->hook = NULL; 437 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 438 ng_rmnode_self(NG_HOOK_NODE(hook)); 439 return (0); 440} 441 442/* 443 * 444 * Ask out neighbour on the output hook side to send us it's interface 445 * information. 446 */ 447static int 448ng_source_request_output_ifp(sc_p sc) 449{ 450 struct ng_mesg *msg; 451 int error = 0; 452 453 sc->output_ifp = NULL; 454 455 /* Ask the attached node for the connected interface's index */ 456 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 457 if (msg == NULL) 458 return (ENOBUFS); 459 460 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 461 return (error); 462} 463 464/* 465 * Set sc->output_ifp to point to the the struct ifnet of the interface 466 * reached via our output hook. 467 */ 468static int 469ng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 470{ 471 struct ifnet *ifp; 472 u_int32_t if_index; 473 int s; 474 475 if (msg->header.arglen < sizeof(u_int32_t)) 476 return (EINVAL); 477 478 if_index = *(u_int32_t *)msg->data; 479 /* Could use ifindex2ifnet[if_index] except that we have no 480 * way of verifying if_index is valid since if_indexlim is 481 * local to if_attach() 482 */ 483 IFNET_RLOCK(); 484 TAILQ_FOREACH(ifp, &ifnet, if_link) { 485 if (ifp->if_index == if_index) 486 break; 487 } 488 IFNET_RUNLOCK(); 489 490 if (ifp == NULL) { 491 printf("%s: can't find interface %d\n", __FUNCTION__, if_index); 492 return (EINVAL); 493 } 494 sc->output_ifp = ifp; 495 496#if 1 497 /* XXX mucking with a drivers ifqueue size is ugly but we need it 498 * to queue a lot of packets to get close to line rate on a gigabit 499 * interface with small packets. 500 * XXX we should restore the original value at stop or disconnect 501 */ 502 s = splimp(); /* XXX is this required? */ 503 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) 504 { 505 printf("ng_source: changing ifq_maxlen from %d to %d\n", 506 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 507 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 508 } 509 splx(s); 510#endif 511 return (0); 512} 513 514/* 515 * Set the attached ethernet node's ethernet source address override flag. 516 */ 517static int 518ng_source_set_autosrc(sc_p sc, u_int32_t flag) 519{ 520 struct ng_mesg *msg; 521 int error = 0; 522 523 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 524 sizeof (u_int32_t), M_NOWAIT); 525 if (msg == NULL) 526 return(ENOBUFS); 527 528 *(u_int32_t *)msg->data = flag; 529 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 530 return (error); 531} 532 533/* 534 * Clear out the data we've queued 535 */ 536static void 537ng_source_clr_data (sc_p sc) 538{ 539 struct mbuf *m; 540 541 for (;;) { 542 IF_DEQUEUE(&sc->snd_queue, m); 543 if (m == NULL) 544 break; 545 NG_FREE_M(m); 546 } 547 sc->queueOctets = 0; 548} 549 550/* 551 * Start sending queued data out the output hook 552 */ 553static void 554ng_source_start (sc_p sc) 555{ 556 KASSERT(sc->output.hook != NULL, 557 ("%s: output hook unconnected", __FUNCTION__)); 558 if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 559 (sc->output_ifp == NULL)) 560 ng_source_request_output_ifp(sc); 561} 562 563/* 564 * Stop sending queued data out the output hook 565 */ 566static void 567ng_source_stop (sc_p sc) 568{ 569 if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 570 untimeout(ng_source_intr, sc, sc->intr_ch); 571 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 572 getmicrotime(&sc->stats.endTime); 573 sc->stats.elapsedTime = sc->stats.endTime; 574 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 575 /* XXX should set this to the initial value instead */ 576 ng_source_set_autosrc(sc, 1); 577 } 578} 579 580/* 581 * While active called every NG_SOURCE_INTR_TICKS ticks. 582 * Sends as many packets as the interface connected to our 583 * output hook is able to enqueue. 584 */ 585static void 586ng_source_intr (void *arg) 587{ 588 sc_p sc = (sc_p) arg; 589 struct ifqueue *ifq; 590 int packets; 591 592 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 593 594 callout_handle_init(&sc->intr_ch); 595 if (sc->packets == 0 || sc->output.hook == NULL 596 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 597 ng_source_stop(sc); 598 return; 599 } 600 601 ifq = &sc->output_ifp->if_snd; 602 packets = ifq->ifq_maxlen - ifq->ifq_len; 603 ng_source_send(sc, packets, NULL); 604 if (sc->packets == 0) { 605 int s = splnet(); 606 ng_source_stop(sc); 607 splx(s); 608 } else 609 sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 610} 611 612/* 613 * Send packets out our output hook 614 */ 615static int 616ng_source_send (sc_p sc, int tosend, int *sent_p) 617{ 618 struct ifqueue tmp_queue; 619 struct mbuf *m, *m2; 620 int sent = 0; 621 int error = 0; 622 int s, s2; 623 624 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 625 KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__)); 626 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 627 ("%s: inactive node", __FUNCTION__)); 628 629 if ((u_int64_t)tosend > sc->packets) 630 tosend = sc->packets; 631 632 /* Copy the required number of packets to a temporary queue */ 633 bzero (&tmp_queue, sizeof (tmp_queue)); 634 for (sent = 0; error == 0 && sent < tosend; ++sent) { 635 s = splnet(); 636 IF_DEQUEUE(&sc->snd_queue, m); 637 splx(s); 638 if (m == NULL) 639 break; 640 641 /* duplicate the packet */ 642 m2 = m_copypacket(m, M_DONTWAIT); 643 if (m2 == NULL) { 644 s = splnet(); 645 IF_PREPEND(&sc->snd_queue, m); 646 splx(s); 647 error = ENOBUFS; 648 break; 649 } 650 651 /* re-enqueue the original packet for us */ 652 s = splnet(); 653 IF_ENQUEUE(&sc->snd_queue, m); 654 splx(s); 655 656 /* queue the copy for sending at smplimp */ 657 IF_ENQUEUE(&tmp_queue, m2); 658 } 659 660 sent = 0; 661 s = splimp(); 662 for (;;) { 663 IF_DEQUEUE(&tmp_queue, m2); 664 if (m2 == NULL) 665 break; 666 if (error == 0) { 667 ++sent; 668 sc->stats.outFrames++; 669 sc->stats.outOctets += m2->m_pkthdr.len; 670 s2 = splnet(); 671 NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 672 splx(s2); 673 } else { 674 NG_FREE_M(m2); 675 } 676 } 677 splx(s); 678 679 sc->packets -= sent; 680 if (sent_p != NULL) 681 *sent_p = sent; 682 return (error); 683} 684