ng_source.c revision 167156
1221828Sgrehan/* 2221828Sgrehan * ng_source.c 3221828Sgrehan */ 4221828Sgrehan 5221828Sgrehan/*- 6221828Sgrehan * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org> 7221828Sgrehan * Copyright 2002 Sandvine Inc. 8221828Sgrehan * All rights reserved. 9221828Sgrehan * 10221828Sgrehan * Subject to the following obligations and disclaimer of warranty, use and 11221828Sgrehan * redistribution of this software, in source or object code forms, with or 12221828Sgrehan * without modifications are expressly permitted by Sandvine Inc.; provided, 13221828Sgrehan * however, that: 14221828Sgrehan * 1. Any and all reproductions of the source or object code must include the 15221828Sgrehan * copyright notice above and the following disclaimer of warranties; and 16221828Sgrehan * 2. No rights are granted, in any manner or form, to use Sandvine Inc. 17221828Sgrehan * trademarks, including the mark "SANDVINE" on advertising, endorsements, 18221828Sgrehan * or otherwise except as such appears in the above copyright notice or in 19221828Sgrehan * the software. 20221828Sgrehan * 21221828Sgrehan * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM 22221828Sgrehan * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES, 23221828Sgrehan * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, 24221828Sgrehan * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 25221828Sgrehan * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR 26221828Sgrehan * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE 27221828Sgrehan * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY 28221828Sgrehan * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES 29221828Sgrehan * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30221828Sgrehan * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31221828Sgrehan * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32221828Sgrehan * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33221828Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34221828Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35221828Sgrehan * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH 36221828Sgrehan * DAMAGE. 37221828Sgrehan * 38221828Sgrehan * Author: Dave Chapeskie <dchapeskie@sandvine.com> 39221828Sgrehan */ 40221828Sgrehan 41221828Sgrehan#include <sys/cdefs.h> 42221828Sgrehan__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 167156 2007-03-01 23:16:17Z emaste $"); 43221828Sgrehan 44221828Sgrehan/* 45221828Sgrehan * This node is used for high speed packet geneneration. It queues 46221828Sgrehan * all data recieved on its 'input' hook and when told to start via 47221828Sgrehan * a control message it sends the packets out its 'output' hook. In 48221828Sgrehan * this way this node can be preloaded with a packet stream which it 49221828Sgrehan * can then send continuously as fast as possible. 50221828Sgrehan * 51221828Sgrehan * Currently it just copies the mbufs as required. It could do various 52221828Sgrehan * tricks to try and avoid this. Probably the best performance would 53221828Sgrehan * be achieved by modifying the appropriate drivers to be told to 54221828Sgrehan * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 55221828Sgrehan * transmit descriptors) under control of this node; perhaps via some 56221828Sgrehan * flag in the mbuf or some such. The node could peek at an appropriate 57221828Sgrehan * ifnet flag to see if such support is available for the connected 58221828Sgrehan * interface. 59221828Sgrehan */ 60221828Sgrehan 61221828Sgrehan#include <sys/param.h> 62221828Sgrehan#include <sys/systm.h> 63221828Sgrehan#include <sys/errno.h> 64221828Sgrehan#include <sys/kernel.h> 65221828Sgrehan#include <sys/malloc.h> 66221828Sgrehan#include <sys/mbuf.h> 67221828Sgrehan#include <sys/socket.h> 68221828Sgrehan#include <sys/syslog.h> 69221828Sgrehan#include <net/if.h> 70221828Sgrehan#include <net/if_var.h> 71221828Sgrehan#include <netgraph/ng_message.h> 72221828Sgrehan#include <netgraph/netgraph.h> 73221828Sgrehan#include <netgraph/ng_parse.h> 74221828Sgrehan#include <netgraph/ng_ether.h> 75221828Sgrehan#include <netgraph/ng_source.h> 76221828Sgrehan 77221828Sgrehan#define NG_SOURCE_INTR_TICKS 1 78221828Sgrehan#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 79221828Sgrehan 80221828Sgrehan#define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off))) 81221828Sgrehan 82221828Sgrehan/* Per node info */ 83221828Sgrehanstruct privdata { 84221828Sgrehan node_p node; 85221828Sgrehan hook_p input; 86221828Sgrehan hook_p output; 87221828Sgrehan struct ng_source_stats stats; 88221828Sgrehan struct ifqueue snd_queue; /* packets to send */ 89221828Sgrehan struct ifnet *output_ifp; 90221828Sgrehan struct callout intr_ch; 91221828Sgrehan uint64_t packets; /* packets to send */ 92221828Sgrehan uint32_t queueOctets; 93221828Sgrehan struct ng_source_embed_info embed_timestamp; 94221828Sgrehan}; 95221828Sgrehantypedef struct privdata *sc_p; 96221828Sgrehan 97221828Sgrehan/* Node flags */ 98221828Sgrehan#define NG_SOURCE_ACTIVE (NGF_TYPE1) 99221828Sgrehan 100221828Sgrehan/* Netgraph methods */ 101221828Sgrehanstatic ng_constructor_t ng_source_constructor; 102221828Sgrehanstatic ng_rcvmsg_t ng_source_rcvmsg; 103221828Sgrehanstatic ng_shutdown_t ng_source_rmnode; 104221828Sgrehanstatic ng_newhook_t ng_source_newhook; 105221828Sgrehanstatic ng_connect_t ng_source_connect; 106221828Sgrehanstatic ng_rcvdata_t ng_source_rcvdata; 107221828Sgrehanstatic ng_disconnect_t ng_source_disconnect; 108221828Sgrehan 109221828Sgrehan/* Other functions */ 110221828Sgrehanstatic void ng_source_intr(node_p, hook_p, void *, int); 111221828Sgrehanstatic void ng_source_clr_data (sc_p); 112221828Sgrehanstatic int ng_source_start (sc_p, uint64_t); 113221828Sgrehanstatic void ng_source_stop (sc_p); 114221828Sgrehanstatic int ng_source_send (sc_p, int, int *); 115221828Sgrehanstatic int ng_source_store_output_ifp(sc_p, char *); 116241178Sneelstatic void ng_source_packet_mod(sc_p, struct mbuf *, 117241178Sneel int, int, caddr_t, int); 118221828Sgrehanstatic int ng_source_dup_mod(sc_p, struct mbuf *, 119221828Sgrehan struct mbuf **); 120221828Sgrehan 121239700Sgrehan/* Parse type for timeval */ 122239700Sgrehanstatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = { 123239700Sgrehan { "tv_sec", &ng_parse_int32_type }, 124239700Sgrehan { "tv_usec", &ng_parse_int32_type }, 125239700Sgrehan { NULL } 126239700Sgrehan}; 127239700Sgrehanconst struct ng_parse_type ng_source_timeval_type = { 128239700Sgrehan &ng_parse_struct_type, 129239700Sgrehan &ng_source_timeval_type_fields 130239700Sgrehan}; 131239700Sgrehan 132239700Sgrehan/* Parse type for struct ng_source_stats */ 133239700Sgrehanstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 134239700Sgrehan = NG_SOURCE_STATS_TYPE_INFO; 135239700Sgrehanstatic const struct ng_parse_type ng_source_stats_type = { 136239700Sgrehan &ng_parse_struct_type, 137239700Sgrehan &ng_source_stats_type_fields 138239700Sgrehan}; 139239700Sgrehan 140239700Sgrehan/* Parse type for struct ng_source_embed_info */ 141239700Sgrehanstatic const struct ng_parse_struct_field ng_source_embed_type_fields[] = 142239700Sgrehan NG_SOURCE_EMBED_TYPE_INFO; 143239700Sgrehanstatic const struct ng_parse_type ng_source_embed_type = { 144239700Sgrehan &ng_parse_struct_type, 145221828Sgrehan &ng_source_embed_type_fields 146241179Sneel}; 147221828Sgrehan 148221828Sgrehan/* List of commands and how to convert arguments to/from ASCII */ 149221828Sgrehanstatic const struct ng_cmdlist ng_source_cmds[] = { 150221828Sgrehan { 151221828Sgrehan NGM_SOURCE_COOKIE, 152221828Sgrehan NGM_SOURCE_GET_STATS, 153221828Sgrehan "getstats", 154221828Sgrehan NULL, 155221828Sgrehan &ng_source_stats_type 156221828Sgrehan }, 157221828Sgrehan { 158221828Sgrehan NGM_SOURCE_COOKIE, 159221828Sgrehan NGM_SOURCE_CLR_STATS, 160221828Sgrehan "clrstats", 161221828Sgrehan NULL, 162221828Sgrehan NULL 163221828Sgrehan }, 164221828Sgrehan { 165221828Sgrehan NGM_SOURCE_COOKIE, 166221828Sgrehan NGM_SOURCE_GETCLR_STATS, 167221828Sgrehan "getclrstats", 168221828Sgrehan NULL, 169221828Sgrehan &ng_source_stats_type 170221828Sgrehan }, 171221828Sgrehan { 172221828Sgrehan NGM_SOURCE_COOKIE, 173221828Sgrehan NGM_SOURCE_START, 174221828Sgrehan "start", 175221828Sgrehan &ng_parse_uint64_type, 176221828Sgrehan NULL 177221828Sgrehan }, 178221828Sgrehan { 179221828Sgrehan NGM_SOURCE_COOKIE, 180221828Sgrehan NGM_SOURCE_STOP, 181221828Sgrehan "stop", 182221828Sgrehan NULL, 183221828Sgrehan NULL 184221828Sgrehan }, 185221828Sgrehan { 186221828Sgrehan NGM_SOURCE_COOKIE, 187221828Sgrehan NGM_SOURCE_CLR_DATA, 188221828Sgrehan "clrdata", 189221828Sgrehan NULL, 190221828Sgrehan NULL 191221828Sgrehan }, 192221828Sgrehan { 193221828Sgrehan NGM_SOURCE_COOKIE, 194221828Sgrehan NGM_SOURCE_SETIFACE, 195221828Sgrehan "setiface", 196221828Sgrehan &ng_parse_string_type, 197221828Sgrehan NULL 198221828Sgrehan }, 199221828Sgrehan { 200221828Sgrehan NGM_SOURCE_COOKIE, 201221828Sgrehan NGM_SOURCE_SETPPS, 202221828Sgrehan "setpps", 203221828Sgrehan &ng_parse_uint32_type, 204221828Sgrehan NULL 205221828Sgrehan }, 206221828Sgrehan { 207221828Sgrehan NGM_SOURCE_COOKIE, 208221828Sgrehan NGM_SOURCE_SET_TIMESTAMP, 209221828Sgrehan "settimestamp", 210221828Sgrehan &ng_source_embed_type, 211221828Sgrehan NULL 212221828Sgrehan }, 213221828Sgrehan { 214221828Sgrehan NGM_SOURCE_COOKIE, 215221828Sgrehan NGM_SOURCE_GET_TIMESTAMP, 216221828Sgrehan "gettimestamp", 217221828Sgrehan NULL, 218221828Sgrehan &ng_source_embed_type 219221828Sgrehan }, 220221828Sgrehan { 0 } 221221828Sgrehan}; 222221828Sgrehan 223221828Sgrehan/* Netgraph type descriptor */ 224221828Sgrehanstatic struct ng_type ng_source_typestruct = { 225221828Sgrehan .version = NG_ABI_VERSION, 226221828Sgrehan .name = NG_SOURCE_NODE_TYPE, 227221828Sgrehan .constructor = ng_source_constructor, 228221828Sgrehan .rcvmsg = ng_source_rcvmsg, 229221828Sgrehan .shutdown = ng_source_rmnode, 230221828Sgrehan .newhook = ng_source_newhook, 231221828Sgrehan .connect = ng_source_connect, 232221828Sgrehan .rcvdata = ng_source_rcvdata, 233221828Sgrehan .disconnect = ng_source_disconnect, 234221828Sgrehan .cmdlist = ng_source_cmds, 235221828Sgrehan}; 236221828SgrehanNETGRAPH_INIT(source, &ng_source_typestruct); 237221828Sgrehan 238221828Sgrehanstatic int ng_source_set_autosrc(sc_p, uint32_t); 239221828Sgrehan 240221828Sgrehan/* 241221828Sgrehan * Node constructor 242221828Sgrehan */ 243221828Sgrehanstatic int 244221828Sgrehanng_source_constructor(node_p node) 245221828Sgrehan{ 246221828Sgrehan sc_p sc; 247221828Sgrehan 248221828Sgrehan sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 249221828Sgrehan if (sc == NULL) 250221828Sgrehan return (ENOMEM); 251221828Sgrehan 252221828Sgrehan NG_NODE_SET_PRIVATE(node, sc); 253221828Sgrehan sc->node = node; 254221828Sgrehan sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 255221828Sgrehan ng_callout_init(&sc->intr_ch); 256221828Sgrehan 257221828Sgrehan return (0); 258221828Sgrehan} 259221828Sgrehan 260221828Sgrehan/* 261221828Sgrehan * Add a hook 262221828Sgrehan */ 263221828Sgrehanstatic int 264221828Sgrehanng_source_newhook(node_p node, hook_p hook, const char *name) 265221828Sgrehan{ 266221828Sgrehan sc_p sc = NG_NODE_PRIVATE(node); 267221828Sgrehan 268221828Sgrehan if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 269221828Sgrehan sc->input = hook; 270221828Sgrehan } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 271221828Sgrehan sc->output = hook; 272221828Sgrehan sc->output_ifp = 0; 273221828Sgrehan bzero(&sc->stats, sizeof(sc->stats)); 274221828Sgrehan } else 275221828Sgrehan return (EINVAL); 276221828Sgrehan 277221828Sgrehan return (0); 278221828Sgrehan} 279221828Sgrehan 280221828Sgrehan/* 281221828Sgrehan * Hook has been added 282221828Sgrehan */ 283221828Sgrehanstatic int 284221828Sgrehanng_source_connect(hook_p hook) 285221828Sgrehan{ 286221828Sgrehan sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 287221828Sgrehan struct ng_mesg *msg; 288221828Sgrehan int dummy_error = 0; 289221828Sgrehan 290221828Sgrehan /* 291221828Sgrehan * If this is "output" hook, then request information 292221828Sgrehan * from our downstream. 293221828Sgrehan */ 294221828Sgrehan if (hook == sc->output) { 295221828Sgrehan NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME, 296221828Sgrehan 0, M_NOWAIT); 297221828Sgrehan if (msg == NULL) 298221828Sgrehan return (ENOBUFS); 299221828Sgrehan 300221828Sgrehan /* 301221828Sgrehan * Our hook and peer hook have HK_INVALID flag set, 302221828Sgrehan * so we can't use NG_SEND_MSG_HOOK() macro here. 303221828Sgrehan */ 304221828Sgrehan NG_SEND_MSG_ID(dummy_error, sc->node, msg, 305221828Sgrehan NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node)); 306221828Sgrehan } 307221828Sgrehan 308221828Sgrehan return (0); 309221828Sgrehan} 310221828Sgrehan 311221828Sgrehan/* 312221828Sgrehan * Receive a control message 313221828Sgrehan */ 314221828Sgrehanstatic int 315221828Sgrehanng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 316221828Sgrehan{ 317221828Sgrehan sc_p sc = NG_NODE_PRIVATE(node); 318221828Sgrehan struct ng_mesg *msg, *resp = NULL; 319221828Sgrehan int error = 0; 320221828Sgrehan 321221828Sgrehan NGI_GET_MSG(item, msg); 322221828Sgrehan 323221828Sgrehan switch (msg->header.typecookie) { 324221828Sgrehan case NGM_SOURCE_COOKIE: 325221828Sgrehan if (msg->header.flags & NGF_RESP) { 326221828Sgrehan error = EINVAL; 327221828Sgrehan break; 328221828Sgrehan } 329221828Sgrehan switch (msg->header.cmd) { 330221828Sgrehan case NGM_SOURCE_GET_STATS: 331221828Sgrehan case NGM_SOURCE_CLR_STATS: 332239042Sneel case NGM_SOURCE_GETCLR_STATS: 333239042Sneel { 334221828Sgrehan struct ng_source_stats *stats; 335221828Sgrehan 336221828Sgrehan if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 337239042Sneel NG_MKRESPONSE(resp, msg, 338221828Sgrehan sizeof(*stats), M_NOWAIT); 339221828Sgrehan if (resp == NULL) { 340221828Sgrehan error = ENOMEM; 341239026Sneel goto done; 342239026Sneel } 343239026Sneel sc->stats.queueOctets = sc->queueOctets; 344239026Sneel sc->stats.queueFrames = sc->snd_queue.ifq_len; 345239026Sneel if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 346239026Sneel && !timevalisset(&sc->stats.endTime)) { 347239026Sneel getmicrotime(&sc->stats.elapsedTime); 348239026Sneel timevalsub(&sc->stats.elapsedTime, 349239026Sneel &sc->stats.startTime); 350239026Sneel } 351221828Sgrehan stats = (struct ng_source_stats *)resp->data; 352221828Sgrehan bcopy(&sc->stats, stats, sizeof(* stats)); 353221828Sgrehan } 354221828Sgrehan if (msg->header.cmd != NGM_SOURCE_GET_STATS) 355221828Sgrehan bzero(&sc->stats, sizeof(sc->stats)); 356221828Sgrehan } 357221828Sgrehan break; 358221828Sgrehan case NGM_SOURCE_START: 359221828Sgrehan { 360221828Sgrehan uint64_t packets; 361221828Sgrehan 362221828Sgrehan if (msg->header.arglen != sizeof(uint64_t)) { 363221828Sgrehan error = EINVAL; 364221828Sgrehan break; 365221828Sgrehan } 366221828Sgrehan 367221828Sgrehan packets = *(uint64_t *)msg->data; 368221828Sgrehan 369221828Sgrehan error = ng_source_start(sc, packets); 370221828Sgrehan 371221828Sgrehan break; 372221828Sgrehan } 373241486Sneel case NGM_SOURCE_STOP: 374241486Sneel ng_source_stop(sc); 375241486Sneel break; 376241486Sneel case NGM_SOURCE_CLR_DATA: 377241486Sneel ng_source_clr_data(sc); 378241486Sneel break; 379241486Sneel case NGM_SOURCE_SETIFACE: 380241486Sneel { 381241486Sneel char *ifname = (char *)msg->data; 382241486Sneel 383241486Sneel if (msg->header.arglen < 2) { 384221828Sgrehan error = EINVAL; 385221828Sgrehan break; 386221828Sgrehan } 387221828Sgrehan 388221828Sgrehan ng_source_store_output_ifp(sc, ifname); 389221828Sgrehan break; 390221828Sgrehan } 391221828Sgrehan case NGM_SOURCE_SETPPS: 392221828Sgrehan { 393221828Sgrehan uint32_t pps; 394221828Sgrehan 395221828Sgrehan if (msg->header.arglen != sizeof(uint32_t)) { 396221828Sgrehan error = EINVAL; 397241486Sneel break; 398241486Sneel } 399241486Sneel 400241486Sneel pps = *(uint32_t *)msg->data; 401241486Sneel 402241486Sneel sc->stats.maxPps = pps; 403241486Sneel 404241486Sneel break; 405241486Sneel } 406241486Sneel case NGM_SOURCE_SET_TIMESTAMP: 407241486Sneel { 408241486Sneel struct ng_source_embed_info *embed; 409241486Sneel 410221828Sgrehan embed = (struct ng_source_embed_info *)msg->data; 411221828Sgrehan bcopy(embed, &sc->embed_timestamp, sizeof(*embed)); 412221828Sgrehan 413221828Sgrehan break; 414221828Sgrehan } 415221828Sgrehan case NGM_SOURCE_GET_TIMESTAMP: 416221828Sgrehan { 417221828Sgrehan struct ng_source_embed_info *embed; 418221828Sgrehan 419221828Sgrehan NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT); 420221828Sgrehan if (resp == NULL) { 421221828Sgrehan error = ENOMEM; 422221828Sgrehan goto done; 423221828Sgrehan } 424221828Sgrehan embed = (struct ng_source_embed_info *)resp->data; 425221828Sgrehan bcopy(&sc->embed_timestamp, embed, sizeof(*embed)); 426221828Sgrehan 427221828Sgrehan break; 428221828Sgrehan } 429221828Sgrehan default: 430221828Sgrehan error = EINVAL; 431221828Sgrehan break; 432221828Sgrehan } 433221828Sgrehan break; 434221828Sgrehan case NGM_ETHER_COOKIE: 435221828Sgrehan if (!(msg->header.flags & NGF_RESP)) { 436221828Sgrehan error = EINVAL; 437221828Sgrehan break; 438221828Sgrehan } 439221828Sgrehan switch (msg->header.cmd) { 440221828Sgrehan case NGM_ETHER_GET_IFNAME: 441221828Sgrehan { 442221828Sgrehan char *ifname = (char *)msg->data; 443221828Sgrehan 444221828Sgrehan if (msg->header.arglen < 2) { 445221828Sgrehan error = EINVAL; 446221828Sgrehan break; 447221828Sgrehan } 448221828Sgrehan 449221828Sgrehan if (ng_source_store_output_ifp(sc, ifname) == 0) 450221828Sgrehan ng_source_set_autosrc(sc, 0); 451221828Sgrehan break; 452221828Sgrehan } 453221828Sgrehan default: 454221828Sgrehan error = EINVAL; 455221828Sgrehan } 456221828Sgrehan break; 457221828Sgrehan default: 458221828Sgrehan error = EINVAL; 459221828Sgrehan break; 460221828Sgrehan } 461221828Sgrehan 462221828Sgrehandone: 463221828Sgrehan /* Take care of synchronous response, if any. */ 464221828Sgrehan NG_RESPOND_MSG(error, node, item, resp); 465221828Sgrehan /* Free the message and return. */ 466221828Sgrehan NG_FREE_MSG(msg); 467221828Sgrehan return (error); 468221828Sgrehan} 469221828Sgrehan 470221828Sgrehan/* 471221828Sgrehan * Receive data on a hook 472221828Sgrehan * 473221828Sgrehan * If data comes in the input hook, enqueue it on the send queue. 474221828Sgrehan * If data comes in the output hook, discard it. 475221828Sgrehan */ 476221828Sgrehanstatic int 477221828Sgrehanng_source_rcvdata(hook_p hook, item_p item) 478221828Sgrehan{ 479221828Sgrehan sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 480221828Sgrehan struct mbuf *m; 481221828Sgrehan int error = 0; 482221828Sgrehan 483221828Sgrehan NGI_GET_M(item, m); 484221828Sgrehan NG_FREE_ITEM(item); 485221828Sgrehan 486221828Sgrehan /* Which hook? */ 487221828Sgrehan if (hook == sc->output) { 488221828Sgrehan /* discard */ 489221828Sgrehan NG_FREE_M(m); 490221828Sgrehan return (error); 491221828Sgrehan } 492221828Sgrehan KASSERT(hook == sc->input, ("%s: no hook!", __func__)); 493221828Sgrehan 494221828Sgrehan /* Enqueue packet. */ 495221828Sgrehan /* XXX should we check IF_QFULL() ? */ 496221828Sgrehan _IF_ENQUEUE(&sc->snd_queue, m); 497221828Sgrehan sc->queueOctets += m->m_pkthdr.len; 498221828Sgrehan 499221828Sgrehan return (0); 500234761Sgrehan} 501234761Sgrehan 502234761Sgrehan/* 503234761Sgrehan * Shutdown processing 504234761Sgrehan */ 505234761Sgrehanstatic int 506234761Sgrehanng_source_rmnode(node_p node) 507234761Sgrehan{ 508234761Sgrehan sc_p sc = NG_NODE_PRIVATE(node); 509234761Sgrehan 510234761Sgrehan ng_source_stop(sc); 511234761Sgrehan ng_source_clr_data(sc); 512234761Sgrehan NG_NODE_SET_PRIVATE(node, NULL); 513234761Sgrehan NG_NODE_UNREF(node); 514234761Sgrehan free(sc, M_NETGRAPH); 515234761Sgrehan 516234761Sgrehan return (0); 517234761Sgrehan} 518234761Sgrehan 519221828Sgrehan/* 520221828Sgrehan * Hook disconnection 521221828Sgrehan */ 522221828Sgrehanstatic int 523221828Sgrehanng_source_disconnect(hook_p hook) 524221828Sgrehan{ 525221828Sgrehan sc_p sc; 526221828Sgrehan 527221828Sgrehan sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 528221828Sgrehan KASSERT(sc != NULL, ("%s: null node private", __func__)); 529221828Sgrehan if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output) 530221828Sgrehan ng_rmnode_self(NG_HOOK_NODE(hook)); 531221828Sgrehan return (0); 532221828Sgrehan} 533221828Sgrehan 534221828Sgrehan/* 535221828Sgrehan * Set sc->output_ifp to point to the the struct ifnet of the interface 536221828Sgrehan * reached via our output hook. 537221828Sgrehan */ 538221828Sgrehanstatic int 539221828Sgrehanng_source_store_output_ifp(sc_p sc, char *ifname) 540221828Sgrehan{ 541221828Sgrehan struct ifnet *ifp; 542221828Sgrehan int s; 543221828Sgrehan 544221828Sgrehan ifp = ifunit(ifname); 545221828Sgrehan 546221828Sgrehan if (ifp == NULL) { 547221828Sgrehan printf("%s: can't find interface %d\n", __func__, if_index); 548221828Sgrehan return (EINVAL); 549221828Sgrehan } 550221828Sgrehan sc->output_ifp = ifp; 551221828Sgrehan 552240922Sneel#if 1 553240922Sneel /* XXX mucking with a drivers ifqueue size is ugly but we need it 554240922Sneel * to queue a lot of packets to get close to line rate on a gigabit 555240922Sneel * interface with small packets. 556240922Sneel * XXX we should restore the original value at stop or disconnect 557240922Sneel */ 558240922Sneel s = splimp(); /* XXX is this required? */ 559240922Sneel if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 560240922Sneel printf("ng_source: changing ifq_maxlen from %d to %d\n", 561240922Sneel ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 562240922Sneel ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 563240922Sneel } 564240922Sneel splx(s); 565240922Sneel#endif 566240922Sneel return (0); 567240922Sneel} 568240922Sneel 569240922Sneel/* 570240922Sneel * Set the attached ethernet node's ethernet source address override flag. 571240922Sneel */ 572240922Sneelstatic int 573240922Sneelng_source_set_autosrc(sc_p sc, uint32_t flag) 574240922Sneel{ 575240922Sneel struct ng_mesg *msg; 576240922Sneel int error = 0; 577240922Sneel 578240922Sneel NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 579240922Sneel sizeof (uint32_t), M_NOWAIT); 580240922Sneel if (msg == NULL) 581221828Sgrehan return(ENOBUFS); 582221828Sgrehan 583221828Sgrehan *(uint32_t *)msg->data = flag; 584221828Sgrehan NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0); 585221828Sgrehan return (error); 586221828Sgrehan} 587221828Sgrehan 588221828Sgrehan/* 589221828Sgrehan * Clear out the data we've queued 590221828Sgrehan */ 591221828Sgrehanstatic void 592221828Sgrehanng_source_clr_data (sc_p sc) 593221828Sgrehan{ 594221828Sgrehan struct mbuf *m; 595221828Sgrehan 596221828Sgrehan for (;;) { 597221828Sgrehan _IF_DEQUEUE(&sc->snd_queue, m); 598221828Sgrehan if (m == NULL) 599221828Sgrehan break; 600221828Sgrehan NG_FREE_M(m); 601221828Sgrehan } 602221828Sgrehan sc->queueOctets = 0; 603221828Sgrehan} 604221828Sgrehan 605221828Sgrehan/* 606221828Sgrehan * Start sending queued data out the output hook 607221828Sgrehan */ 608221828Sgrehanstatic int 609221828Sgrehanng_source_start(sc_p sc, uint64_t packets) 610221828Sgrehan{ 611239025Sneel if (sc->output_ifp == NULL) { 612221828Sgrehan printf("ng_source: start without iface configured\n"); 613221828Sgrehan return (ENXIO); 614221828Sgrehan } 615221828Sgrehan 616221828Sgrehan if (sc->node->nd_flags & NG_SOURCE_ACTIVE) 617221828Sgrehan return (EBUSY); 618221828Sgrehan 619221828Sgrehan sc->node->nd_flags |= NG_SOURCE_ACTIVE; 620221828Sgrehan 621221828Sgrehan sc->packets = packets; 622221828Sgrehan timevalclear(&sc->stats.elapsedTime); 623221828Sgrehan timevalclear(&sc->stats.endTime); 624221828Sgrehan getmicrotime(&sc->stats.startTime); 625221828Sgrehan getmicrotime(&sc->stats.lastTime); 626221828Sgrehan ng_callout(&sc->intr_ch, sc->node, NULL, 0, 627221828Sgrehan ng_source_intr, sc, 0); 628221828Sgrehan 629221828Sgrehan return (0); 630221828Sgrehan} 631221828Sgrehan 632221828Sgrehan/* 633221828Sgrehan * Stop sending queued data out the output hook 634221828Sgrehan */ 635221828Sgrehanstatic void 636221828Sgrehanng_source_stop(sc_p sc) 637221828Sgrehan{ 638221828Sgrehan ng_uncallout(&sc->intr_ch, sc->node); 639221828Sgrehan sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 640221828Sgrehan getmicrotime(&sc->stats.endTime); 641221828Sgrehan sc->stats.elapsedTime = sc->stats.endTime; 642221828Sgrehan timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 643221828Sgrehan} 644221828Sgrehan 645221828Sgrehan/* 646221828Sgrehan * While active called every NG_SOURCE_INTR_TICKS ticks. 647221828Sgrehan * Sends as many packets as the interface connected to our 648221828Sgrehan * output hook is able to enqueue. 649221828Sgrehan */ 650221828Sgrehanstatic void 651221828Sgrehanng_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 652221828Sgrehan{ 653221828Sgrehan sc_p sc = (sc_p)arg1; 654221828Sgrehan struct ifqueue *ifq; 655221828Sgrehan int packets; 656221828Sgrehan 657221828Sgrehan KASSERT(sc != NULL, ("%s: null node private", __func__)); 658221828Sgrehan 659221828Sgrehan if (sc->packets == 0 || sc->output == NULL 660221828Sgrehan || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 661221828Sgrehan ng_source_stop(sc); 662221828Sgrehan return; 663221828Sgrehan } 664221828Sgrehan 665221828Sgrehan if (sc->output_ifp != NULL) { 666221828Sgrehan ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 667221828Sgrehan packets = ifq->ifq_maxlen - ifq->ifq_len; 668221828Sgrehan } else 669221828Sgrehan packets = sc->snd_queue.ifq_len; 670221828Sgrehan 671221828Sgrehan if (sc->stats.maxPps != 0) { 672221828Sgrehan struct timeval now, elapsed; 673221828Sgrehan uint64_t usec; 674221828Sgrehan int maxpkt; 675221828Sgrehan 676221828Sgrehan getmicrotime(&now); 677221828Sgrehan elapsed = now; 678221828Sgrehan timevalsub(&elapsed, &sc->stats.lastTime); 679221828Sgrehan usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; 680221828Sgrehan maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; 681221828Sgrehan sc->stats.lastTime = now; 682221828Sgrehan if (packets > maxpkt) 683221828Sgrehan packets = maxpkt; 684221828Sgrehan } 685221828Sgrehan 686221828Sgrehan ng_source_send(sc, packets, NULL); 687221828Sgrehan if (sc->packets == 0) 688221828Sgrehan ng_source_stop(sc); 689221828Sgrehan else 690221828Sgrehan ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 691221828Sgrehan ng_source_intr, sc, 0); 692221828Sgrehan} 693221828Sgrehan 694221828Sgrehan/* 695221828Sgrehan * Send packets out our output hook. 696221828Sgrehan */ 697221828Sgrehanstatic int 698221828Sgrehanng_source_send(sc_p sc, int tosend, int *sent_p) 699221828Sgrehan{ 700221828Sgrehan struct mbuf *m, *m2; 701221828Sgrehan int sent; 702221828Sgrehan int error = 0; 703221828Sgrehan 704221828Sgrehan KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 705221828Sgrehan KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 706221828Sgrehan ("%s: inactive node", __func__)); 707221828Sgrehan 708221828Sgrehan if ((uint64_t)tosend > sc->packets) 709221828Sgrehan tosend = sc->packets; 710221828Sgrehan 711221828Sgrehan /* Go through the queue sending packets one by one. */ 712221828Sgrehan for (sent = 0; error == 0 && sent < tosend; ++sent) { 713221828Sgrehan _IF_DEQUEUE(&sc->snd_queue, m); 714221828Sgrehan if (m == NULL) 715221828Sgrehan break; 716221828Sgrehan 717221828Sgrehan /* Duplicate and modify the packet. */ 718221828Sgrehan error = ng_source_dup_mod(sc, m, &m2); 719221828Sgrehan if (error) { 720221828Sgrehan if (error == ENOBUFS) 721221828Sgrehan _IF_PREPEND(&sc->snd_queue, m); 722221828Sgrehan else 723221828Sgrehan _IF_ENQUEUE(&sc->snd_queue, m); 724221828Sgrehan break; 725221828Sgrehan } 726221828Sgrehan 727221828Sgrehan /* Re-enqueue the original packet for us. */ 728221828Sgrehan _IF_ENQUEUE(&sc->snd_queue, m); 729221828Sgrehan 730221828Sgrehan sc->stats.outFrames++; 731221828Sgrehan sc->stats.outOctets += m2->m_pkthdr.len; 732221828Sgrehan NG_SEND_DATA_ONLY(error, sc->output, m2); 733221828Sgrehan if (error) 734221828Sgrehan break; 735221828Sgrehan } 736221828Sgrehan 737 sc->packets -= sent; 738 if (sent_p != NULL) 739 *sent_p = sent; 740 return (error); 741} 742 743/* 744 * Modify packet in 'm' by changing 'len' bytes starting at 'offset' 745 * to data in 'cp'. 746 * 747 * The packet data in 'm' must be in a contiguous buffer in a single mbuf. 748 */ 749static void 750ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp, 751 int flags) 752{ 753 if (len == 0) 754 return; 755 756 /* Can't modify beyond end of packet. */ 757 /* TODO: Pad packet for this case. */ 758 if (offset + len > m->m_len) 759 return; 760 761 bcopy(cp, mtod_off(m, offset, caddr_t), len); 762} 763 764static int 765ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr) 766{ 767 struct mbuf *m; 768 struct ng_source_embed_info *ts; 769 int modify; 770 int error = 0; 771 772 /* Are we going to modify packets? */ 773 modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE; 774 775 /* Duplicate the packet. */ 776 if (modify) 777 m = m_dup(m0, M_DONTWAIT); 778 else 779 m = m_copypacket(m0, M_DONTWAIT); 780 if (m == NULL) { 781 error = ENOBUFS; 782 goto done; 783 } 784 *m_ptr = m; 785 786 if (!modify) 787 goto done; 788 789 /* Modify the copied packet for sending. */ 790 KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__)); 791 792 ts = &sc->embed_timestamp; 793 if (ts->flags & NGM_SOURCE_EMBED_ENABLE) { 794 struct timeval now; 795 getmicrotime(&now); 796 now.tv_sec = htonl(now.tv_sec); 797 now.tv_usec = htonl(now.tv_usec); 798 ng_source_packet_mod(sc, m, ts->offset, sizeof (now), 799 (caddr_t)&now, ts->flags); 800 } 801 802done: 803 return(error); 804} 805