ng_source.c revision 106321
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 106321 2002-11-02 02:29:43Z julian $ 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_get_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 *); 124 125 126/* Parse type for timeval */ 127static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = 128{ 129 { "tv_sec", &ng_parse_int32_type }, 130 { "tv_usec", &ng_parse_int32_type }, 131 { NULL } 132}; 133const struct ng_parse_type ng_source_timeval_type = { 134 &ng_parse_struct_type, 135 &ng_source_timeval_type_fields 136}; 137 138/* Parse type for struct ng_source_stats */ 139static const struct ng_parse_struct_field ng_source_stats_type_fields[] 140 = NG_SOURCE_STATS_TYPE_INFO; 141static const struct ng_parse_type ng_source_stats_type = { 142 &ng_parse_struct_type, 143 &ng_source_stats_type_fields 144}; 145 146/* List of commands and how to convert arguments to/from ASCII */ 147static const struct ng_cmdlist ng_source_cmds[] = { 148 { 149 NGM_SOURCE_COOKIE, 150 NGM_SOURCE_GET_STATS, 151 "getstats", 152 NULL, 153 &ng_source_stats_type 154 }, 155 { 156 NGM_SOURCE_COOKIE, 157 NGM_SOURCE_CLR_STATS, 158 "clrstats", 159 NULL, 160 NULL 161 }, 162 { 163 NGM_SOURCE_COOKIE, 164 NGM_SOURCE_GETCLR_STATS, 165 "getclrstats", 166 NULL, 167 &ng_source_stats_type 168 }, 169 { 170 NGM_SOURCE_COOKIE, 171 NGM_SOURCE_START, 172 "start", 173 &ng_parse_uint64_type, 174 NULL 175 }, 176 { 177 NGM_SOURCE_COOKIE, 178 NGM_SOURCE_STOP, 179 "stop", 180 NULL, 181 NULL 182 }, 183 { 184 NGM_SOURCE_COOKIE, 185 NGM_SOURCE_CLR_DATA, 186 "clrdata", 187 NULL, 188 NULL 189 }, 190 { 0 } 191}; 192 193/* Netgraph type descriptor */ 194static struct ng_type ng_source_typestruct = { 195 NG_VERSION, 196 NG_SOURCE_NODE_TYPE, 197 NULL, /* module event handler */ 198 ng_source_constructor, 199 ng_source_rcvmsg, 200 ng_source_rmnode, 201 ng_source_newhook, 202 NULL, /* findhook */ 203 NULL, 204 ng_source_rcvdata, /* rcvdata */ 205 ng_source_disconnect, 206 ng_source_cmds 207}; 208NETGRAPH_INIT(source, &ng_source_typestruct); 209 210/* 211 * Node constructor 212 */ 213static int 214ng_source_constructor(node_p node) 215{ 216 sc_p sc; 217 218 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT); 219 if (sc == NULL) 220 return (ENOMEM); 221 bzero(sc, sizeof(*sc)); 222 223 NG_NODE_SET_PRIVATE(node, sc); 224 sc->node = node; 225 sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 226 callout_handle_init(&sc->intr_ch); /* XXX fix.. will 227 cause problems. */ 228 return (0); 229} 230 231/* 232 * Add a hook 233 */ 234static int 235ng_source_newhook(node_p node, hook_p hook, const char *name) 236{ 237 sc_p sc; 238 239 sc = NG_NODE_PRIVATE(node); 240 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 241 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 242 sc->input.hook = hook; 243 NG_HOOK_SET_PRIVATE(hook, &sc->input); 244 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 245 sc->output.hook = hook; 246 NG_HOOK_SET_PRIVATE(hook, &sc->output); 247 sc->output_ifp = 0; 248 bzero(&sc->stats, sizeof(sc->stats)); 249 } else 250 return (EINVAL); 251 return (0); 252} 253 254/* 255 * Receive a control message 256 */ 257static int 258ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 259{ 260 sc_p sc; 261 struct ng_mesg *resp = NULL; 262 int error = 0; 263 struct ng_mesg *msg; 264 265 sc = NG_NODE_PRIVATE(node); 266 NGI_GET_MSG(item, msg); 267 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 268 switch (msg->header.typecookie) { 269 case NGM_SOURCE_COOKIE: 270 switch (msg->header.cmd) { 271 case NGM_SOURCE_GET_STATS: 272 case NGM_SOURCE_CLR_STATS: 273 case NGM_SOURCE_GETCLR_STATS: 274 { 275 struct ng_source_stats *stats; 276 277 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 278 NG_MKRESPONSE(resp, msg, 279 sizeof(*stats), M_NOWAIT); 280 if (resp == NULL) { 281 error = ENOMEM; 282 goto done; 283 } 284 sc->stats.queueOctets = sc->queueOctets; 285 sc->stats.queueFrames = sc->snd_queue.ifq_len; 286 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 287 && !timevalisset(&sc->stats.endTime)) { 288 getmicrotime(&sc->stats.elapsedTime); 289 timevalsub(&sc->stats.elapsedTime, 290 &sc->stats.startTime); 291 } 292 stats = (struct ng_source_stats *)resp->data; 293 bcopy(&sc->stats, stats, sizeof(* stats)); 294 } 295 if (msg->header.cmd != NGM_SOURCE_GET_STATS) 296 bzero(&sc->stats, sizeof(sc->stats)); 297 } 298 break; 299 case NGM_SOURCE_START: 300 { 301 u_int64_t packets = *(u_int64_t *)msg->data; 302 if (sc->output.hook == NULL) { 303 printf("%s: start on node with no output hook\n" 304 , __FUNCTION__); 305 error = EINVAL; 306 break; 307 } 308 /* TODO validation of packets */ 309 sc->packets = packets; 310 ng_source_start(sc); 311 } 312 break; 313 case NGM_SOURCE_STOP: 314 ng_source_stop(sc); 315 break; 316 case NGM_SOURCE_CLR_DATA: 317 ng_source_clr_data(sc); 318 break; 319 default: 320 error = EINVAL; 321 break; 322 } 323 break; 324 default: 325 error = EINVAL; 326 break; 327 } 328 329done: 330 /* Take care of synchronous response, if any */ 331 NG_RESPOND_MSG(error, node, item, resp); 332 /* Free the message and return */ 333 NG_FREE_MSG(msg); 334 return (error); 335} 336 337/* 338 * Receive data on a hook 339 * 340 * If data comes in the input hook, enqueue it on the send queue. 341 * If data comes in the output hook, discard it. 342 */ 343static int 344ng_source_rcvdata(hook_p hook, item_p item) 345{ 346 sc_p sc; 347 struct source_hookinfo *hinfo; 348 int error = 0; 349 struct mbuf *m; 350 351 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 352 NGI_GET_M(item, m); 353 NG_FREE_ITEM(item); 354 hinfo = NG_HOOK_PRIVATE(hook); 355 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 356 KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__)); 357 358 /* Which hook? */ 359 if (hinfo == &sc->output) { 360 /* discard */ 361 NG_FREE_M(m); 362 return (error); 363 } 364 KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__)); 365 366 if ((m->m_flags & M_PKTHDR) == 0) { 367 printf("%s: mbuf without PKTHDR\n", __FUNCTION__); 368 NG_FREE_M(m); 369 return (EINVAL); 370 } 371 372 /* enque packet */ 373 /* XXX should we check IF_QFULL() ? */ 374 IF_ENQUEUE(&sc->snd_queue, m); 375 sc->queueOctets += m->m_pkthdr.len; 376 377 return (0); 378} 379 380/* 381 * Shutdown processing 382 */ 383static int 384ng_source_rmnode(node_p node) 385{ 386 sc_p sc; 387 388 sc = NG_NODE_PRIVATE(node); 389 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 390 node->nd_flags |= NG_INVALID; 391 ng_source_stop(sc); 392 ng_source_clr_data(sc); 393 NG_NODE_SET_PRIVATE(node, NULL); 394 NG_NODE_UNREF(node); 395 FREE(sc, M_NETGRAPH); 396 return (0); 397} 398 399/* 400 * Hook disconnection 401 */ 402static int 403ng_source_disconnect(hook_p hook) 404{ 405 struct source_hookinfo *hinfo; 406 sc_p sc; 407 408 hinfo = NG_HOOK_PRIVATE(hook); 409 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 410 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 411 hinfo->hook = NULL; 412 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 413 ng_rmnode_self(NG_HOOK_NODE(hook)); 414 return (0); 415} 416 417/* 418 * Set sc->output_ifp to point to the the struct ifnet of the interface 419 * reached via our output hook. 420 */ 421static int 422ng_source_get_output_ifp(sc_p sc) 423{ 424 struct ng_mesg *msg, *rsp; 425 struct ifnet *ifp; 426 u_int32_t if_index; 427 int error = 0; 428 int s; 429 430 sc->output_ifp = NULL; 431 432 /* Ask the attached node for the connected interface's index */ 433 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 434 if (msg == NULL) 435 return (ENOBUFS); 436 437 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 438 /* error = ng_send_msg(sc->node, msg, NG_SOURCE_HOOK_OUTPUT, &rsp); */ 439#warn "oiks" 440 if (error != 0) 441 return (error); 442 443 if (rsp == NULL) 444 return (EINVAL); 445 446 if (rsp->header.arglen < sizeof(u_int32_t)) 447 return (EINVAL); 448 449 if_index = *(u_int32_t *)rsp->data; 450 /* Could use ifindex2ifnet[if_index] except that we have no 451 * way of verifying if_index is valid since if_indexlim is 452 * local to if_attach() 453 */ 454 TAILQ_FOREACH(ifp, &ifnet, if_link) { 455 if (ifp->if_index == if_index) 456 break; 457 } 458 459 if (ifp == NULL) { 460 printf("%s: can't find interface %d\n", __FUNCTION__, if_index); 461 return (EINVAL); 462 } 463 sc->output_ifp = ifp; 464 465#if 1 466 /* XXX mucking with a drivers ifqueue size is ugly but we need it 467 * to queue a lot of packets to get close to line rate on a gigabit 468 * interface with small packets. 469 * XXX we should restore the original value at stop or disconnect 470 */ 471 s = splimp(); /* XXX is this required? */ 472 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) 473 { 474 printf("ng_source: changing ifq_maxlen from %d to %d\n", 475 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 476 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 477 } 478 splx(s); 479#endif 480 return (0); 481} 482 483/* 484 * Set the attached ethernet node's ethernet source address override flag. 485 */ 486static int 487ng_source_set_autosrc(sc_p sc, u_int32_t flag) 488{ 489 struct ng_mesg *msg; 490 int error = 0; 491 492 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 493 sizeof (u_int32_t), M_NOWAIT); 494 if (msg == NULL) 495 return(ENOBUFS); 496 497 *(u_int32_t *)msg->data = flag; 498 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 499 return (error); 500} 501 502/* 503 * Clear out the data we've queued 504 */ 505static void 506ng_source_clr_data (sc_p sc) 507{ 508 struct mbuf *m; 509 510 for (;;) { 511 IF_DEQUEUE(&sc->snd_queue, m); 512 if (m == NULL) 513 break; 514 NG_FREE_M(m); 515 } 516 sc->queueOctets = 0; 517} 518 519/* 520 * Start sending queued data out the output hook 521 */ 522static void 523ng_source_start (sc_p sc) 524{ 525 KASSERT(sc->output.hook != NULL, 526 ("%s: output hook unconnected", __FUNCTION__)); 527 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 528 if (sc->output_ifp == NULL && ng_source_get_output_ifp(sc) != 0) 529 return; 530 ng_source_set_autosrc(sc, 0); 531 sc->node->nd_flags |= NG_SOURCE_ACTIVE; 532 timevalclear(&sc->stats.elapsedTime); 533 timevalclear(&sc->stats.endTime); 534 getmicrotime(&sc->stats.startTime); 535 sc->intr_ch = timeout(ng_source_intr, sc, 0); 536 } 537} 538 539/* 540 * Stop sending queued data out the output hook 541 */ 542static void 543ng_source_stop (sc_p sc) 544{ 545 if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 546 untimeout(ng_source_intr, sc, sc->intr_ch); 547 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 548 getmicrotime(&sc->stats.endTime); 549 sc->stats.elapsedTime = sc->stats.endTime; 550 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 551 /* XXX should set this to the initial value instead */ 552 ng_source_set_autosrc(sc, 1); 553 } 554} 555 556/* 557 * While active called every NG_SOURCE_INTR_TICKS ticks. 558 * Sends as many packets as the interface connected to our 559 * output hook is able to enqueue. 560 */ 561static void 562ng_source_intr (void *arg) 563{ 564 sc_p sc = (sc_p) arg; 565 struct ifqueue *ifq; 566 int packets; 567 568 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 569 570 callout_handle_init(&sc->intr_ch); 571 if (sc->packets == 0 || sc->output.hook == NULL 572 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 573 ng_source_stop(sc); 574 return; 575 } 576 577 ifq = &sc->output_ifp->if_snd; 578 packets = ifq->ifq_maxlen - ifq->ifq_len; 579 ng_source_send(sc, packets, NULL); 580 if (sc->packets == 0) { 581 int s = splnet(); 582 ng_source_stop(sc); 583 splx(s); 584 } else 585 sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 586} 587 588/* 589 * Send packets out our output hook 590 */ 591static int 592ng_source_send (sc_p sc, int tosend, int *sent_p) 593{ 594 struct ifqueue tmp_queue; 595 struct mbuf *m, *m2; 596 int sent = 0; 597 int error = 0; 598 int s, s2; 599 600 KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 601 KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__)); 602 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 603 ("%s: inactive node", __FUNCTION__)); 604 605 if ((u_int64_t)tosend > sc->packets) 606 tosend = sc->packets; 607 608 /* Copy the required number of packets to a temporary queue */ 609 bzero (&tmp_queue, sizeof (tmp_queue)); 610 for (sent = 0; error == 0 && sent < tosend; ++sent) { 611 s = splnet(); 612 IF_DEQUEUE(&sc->snd_queue, m); 613 splx(s); 614 if (m == NULL) 615 break; 616 617 /* duplicate the packet */ 618 m2 = m_copypacket(m, M_NOWAIT); 619 if (m2 == NULL) { 620 s = splnet(); 621 IF_PREPEND(&sc->snd_queue, m); 622 splx(s); 623 error = ENOBUFS; 624 break; 625 } 626 627 /* re-enqueue the original packet for us */ 628 s = splnet(); 629 IF_ENQUEUE(&sc->snd_queue, m); 630 splx(s); 631 632 /* queue the copy for sending at smplimp */ 633 IF_ENQUEUE(&tmp_queue, m2); 634 } 635 636 sent = 0; 637 s = splimp(); 638 for (;;) { 639 IF_DEQUEUE(&tmp_queue, m2); 640 if (m2 == NULL) 641 break; 642 if (error == 0) { 643 ++sent; 644 sc->stats.outFrames++; 645 sc->stats.outOctets += m2->m_pkthdr.len; 646 s2 = splnet(); 647 NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 648 splx(s2); 649 } else { 650 NG_FREE_M(m2); 651 } 652 } 653 splx(s); 654 655 sc->packets -= sent; 656 if (sent_p != NULL) 657 *sent_p = sent; 658 return (error); 659} 660