ng_source.c revision 125243
1106266Sjulian/* 2106266Sjulian * ng_source.c 3106266Sjulian * 4106266Sjulian * Copyright 2002 Sandvine Inc. 5106266Sjulian * All rights reserved. 6106266Sjulian * 7106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and 8106266Sjulian * redistribution of this software, in source or object code forms, with or 9106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided, 10106266Sjulian * however, that: 11106319Sjulian * 1. Any and all reproductions of the source or object code must include the 12106319Sjulian * copyright notice above and the following disclaimer of warranties; and 13106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc. 14106319Sjulian * trademarks, including the mark "SANDVINE" on advertising, endorsements, 15106319Sjulian * or otherwise except as such appears in the above copyright notice or in 16106266Sjulian * the software. 17106266Sjulian * 18106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM 19106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES, 20106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, 21106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 22106266Sjulian * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR 23106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE 24106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY 25106266Sjulian * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES 26106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 27106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 28106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 29106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 30106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH 33106266Sjulian * DAMAGE. 34106266Sjulian * 35106266Sjulian * Author: Dave Chapeskie <dchapeskie@sandvine.com> 36106266Sjulian */ 37106266Sjulian 38125077Sharti#include <sys/cdefs.h> 39125077Sharti__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 125243 2004-01-30 15:34:57Z harti $"); 40125077Sharti 41106266Sjulian/* 42106266Sjulian * This node is used for high speed packet geneneration. It queues 43106266Sjulian * all data recieved on it's 'input' hook and when told to start via 44106266Sjulian * a control message it sends the packets out it's 'output' hook. In 45106266Sjulian * this way this node can be preloaded with a packet stream which is 46106266Sjulian * continuously sent. 47106266Sjulian * 48106266Sjulian * Currently it just copies the mbufs as required. It could do various 49106266Sjulian * tricks to try and avoid this. Probably the best performance would 50106266Sjulian * be achieved by modifying the appropriate drivers to be told to 51106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 52106266Sjulian * transmit descriptors) under control of this node; perhaps via some 53106266Sjulian * flag in the mbuf or some such. The node would peak at an appropriate 54106266Sjulian * ifnet flag to see if such support is available for the connected 55106266Sjulian * interface. 56106266Sjulian */ 57106266Sjulian 58106266Sjulian#include <sys/param.h> 59106266Sjulian#include <sys/systm.h> 60106266Sjulian#include <sys/errno.h> 61106266Sjulian#include <sys/kernel.h> 62106266Sjulian#include <sys/malloc.h> 63106266Sjulian#include <sys/mbuf.h> 64106266Sjulian#include <sys/socket.h> 65106266Sjulian#include <net/if.h> 66106266Sjulian#include <net/if_var.h> 67106266Sjulian#include <netgraph/ng_message.h> 68106266Sjulian#include <netgraph/netgraph.h> 69106266Sjulian#include <netgraph/ng_parse.h> 70106266Sjulian#include <netgraph/ng_ether.h> 71106266Sjulian#include <netgraph/ng_source.h> 72106266Sjulian 73106266Sjulian#define NG_SOURCE_INTR_TICKS 1 74106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 75106266Sjulian 76106266Sjulian 77106266Sjulian/* Per hook info */ 78106266Sjulianstruct source_hookinfo { 79106266Sjulian hook_p hook; 80106266Sjulian}; 81106266Sjulian 82106266Sjulian/* Per node info */ 83106266Sjulianstruct privdata { 84106266Sjulian node_p node; 85106266Sjulian struct source_hookinfo input; 86106266Sjulian struct source_hookinfo output; 87106266Sjulian struct ng_source_stats stats; 88106319Sjulian struct ifqueue snd_queue; /* packets to send */ 89106266Sjulian struct ifnet *output_ifp; 90106266Sjulian struct callout_handle intr_ch; 91106319Sjulian u_int64_t packets; /* packets to send */ 92106266Sjulian u_int32_t queueOctets; 93106266Sjulian}; 94106266Sjuliantypedef struct privdata *sc_p; 95106266Sjulian 96106266Sjulian/* Node flags */ 97106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 98106266Sjulian 99106266Sjulian/* Netgraph methods */ 100106266Sjulianstatic ng_constructor_t ng_source_constructor; 101106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 102106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 103106266Sjulianstatic ng_newhook_t ng_source_newhook; 104106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 105106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 106106266Sjulian 107106266Sjulian/* Other functions */ 108125243Shartistatic void ng_source_intr(node_p, hook_p, void *, int); 109125029Shartistatic int ng_source_request_output_ifp (sc_p); 110106266Sjulianstatic void ng_source_clr_data (sc_p); 111106266Sjulianstatic void ng_source_start (sc_p); 112106266Sjulianstatic void ng_source_stop (sc_p); 113106266Sjulianstatic int ng_source_send (sc_p, int, int *); 114106435Sjulianstatic int ng_source_store_output_ifp(sc_p sc, 115106435Sjulian struct ng_mesg *msg); 116106266Sjulian 117106266Sjulian 118106266Sjulian/* Parse type for timeval */ 119125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = { 120106266Sjulian { "tv_sec", &ng_parse_int32_type }, 121106266Sjulian { "tv_usec", &ng_parse_int32_type }, 122106266Sjulian { NULL } 123106266Sjulian}; 124106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 125106266Sjulian &ng_parse_struct_type, 126106266Sjulian &ng_source_timeval_type_fields 127106266Sjulian}; 128106266Sjulian 129106266Sjulian/* Parse type for struct ng_source_stats */ 130106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 131106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 132106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 133106266Sjulian &ng_parse_struct_type, 134106266Sjulian &ng_source_stats_type_fields 135106266Sjulian}; 136106266Sjulian 137106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 138106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 139106266Sjulian { 140106266Sjulian NGM_SOURCE_COOKIE, 141106266Sjulian NGM_SOURCE_GET_STATS, 142106266Sjulian "getstats", 143106266Sjulian NULL, 144106266Sjulian &ng_source_stats_type 145106266Sjulian }, 146106266Sjulian { 147106266Sjulian NGM_SOURCE_COOKIE, 148106266Sjulian NGM_SOURCE_CLR_STATS, 149106266Sjulian "clrstats", 150106266Sjulian NULL, 151106266Sjulian NULL 152106266Sjulian }, 153106266Sjulian { 154106266Sjulian NGM_SOURCE_COOKIE, 155106266Sjulian NGM_SOURCE_GETCLR_STATS, 156106266Sjulian "getclrstats", 157106266Sjulian NULL, 158106266Sjulian &ng_source_stats_type 159106266Sjulian }, 160106266Sjulian { 161106266Sjulian NGM_SOURCE_COOKIE, 162106266Sjulian NGM_SOURCE_START, 163106266Sjulian "start", 164106266Sjulian &ng_parse_uint64_type, 165106266Sjulian NULL 166106266Sjulian }, 167106266Sjulian { 168106266Sjulian NGM_SOURCE_COOKIE, 169106266Sjulian NGM_SOURCE_STOP, 170106266Sjulian "stop", 171106266Sjulian NULL, 172106266Sjulian NULL 173106266Sjulian }, 174106266Sjulian { 175106266Sjulian NGM_SOURCE_COOKIE, 176106266Sjulian NGM_SOURCE_CLR_DATA, 177106266Sjulian "clrdata", 178106266Sjulian NULL, 179106266Sjulian NULL 180106266Sjulian }, 181125033Sharti { 182125033Sharti NGM_SOURCE_COOKIE, 183125033Sharti NGM_SOURCE_START_NOW, 184125033Sharti "start_now", 185125033Sharti &ng_parse_uint64_type, 186125033Sharti NULL 187125033Sharti }, 188106266Sjulian { 0 } 189106266Sjulian}; 190106266Sjulian 191106266Sjulian/* Netgraph type descriptor */ 192106266Sjulianstatic struct ng_type ng_source_typestruct = { 193125029Sharti NG_ABI_VERSION, 194106266Sjulian NG_SOURCE_NODE_TYPE, 195106266Sjulian NULL, /* module event handler */ 196106266Sjulian ng_source_constructor, 197106266Sjulian ng_source_rcvmsg, 198106266Sjulian ng_source_rmnode, 199106266Sjulian ng_source_newhook, 200106266Sjulian NULL, /* findhook */ 201125077Sharti NULL, /* connect */ 202106266Sjulian ng_source_rcvdata, /* rcvdata */ 203106266Sjulian ng_source_disconnect, 204106266Sjulian ng_source_cmds 205106266Sjulian}; 206106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 207106266Sjulian 208125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t); 209125032Sharti 210106266Sjulian/* 211106266Sjulian * Node constructor 212106266Sjulian */ 213106266Sjulianstatic int 214106321Sjulianng_source_constructor(node_p node) 215106266Sjulian{ 216106266Sjulian sc_p sc; 217106266Sjulian 218125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 219106266Sjulian if (sc == NULL) 220106266Sjulian return (ENOMEM); 221106266Sjulian 222106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 223106321Sjulian sc->node = node; 224106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 225106321Sjulian callout_handle_init(&sc->intr_ch); /* XXX fix.. will 226106321Sjulian cause problems. */ 227106266Sjulian return (0); 228106266Sjulian} 229106266Sjulian 230106266Sjulian/* 231106266Sjulian * Add a hook 232106266Sjulian */ 233106266Sjulianstatic int 234106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 235106266Sjulian{ 236106321Sjulian sc_p sc; 237106266Sjulian 238106321Sjulian sc = NG_NODE_PRIVATE(node); 239125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 240106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 241106266Sjulian sc->input.hook = hook; 242106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 243106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 244106266Sjulian sc->output.hook = hook; 245106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 246106266Sjulian sc->output_ifp = 0; 247106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 248106266Sjulian } else 249106266Sjulian return (EINVAL); 250106266Sjulian return (0); 251106266Sjulian} 252106266Sjulian 253106266Sjulian/* 254106266Sjulian * Receive a control message 255106266Sjulian */ 256106266Sjulianstatic int 257106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 258106266Sjulian{ 259106321Sjulian sc_p sc; 260106266Sjulian struct ng_mesg *resp = NULL; 261106266Sjulian int error = 0; 262106321Sjulian struct ng_mesg *msg; 263106266Sjulian 264106321Sjulian sc = NG_NODE_PRIVATE(node); 265106321Sjulian NGI_GET_MSG(item, msg); 266125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 267106266Sjulian switch (msg->header.typecookie) { 268106266Sjulian case NGM_SOURCE_COOKIE: 269106435Sjulian if (msg->header.flags & NGF_RESP) { 270106435Sjulian error = EINVAL; 271106435Sjulian break; 272106435Sjulian } 273106266Sjulian switch (msg->header.cmd) { 274106266Sjulian case NGM_SOURCE_GET_STATS: 275106266Sjulian case NGM_SOURCE_CLR_STATS: 276106266Sjulian case NGM_SOURCE_GETCLR_STATS: 277106266Sjulian { 278106266Sjulian struct ng_source_stats *stats; 279106266Sjulian 280106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 281106266Sjulian NG_MKRESPONSE(resp, msg, 282106266Sjulian sizeof(*stats), M_NOWAIT); 283106266Sjulian if (resp == NULL) { 284106266Sjulian error = ENOMEM; 285106266Sjulian goto done; 286106266Sjulian } 287106266Sjulian sc->stats.queueOctets = sc->queueOctets; 288106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 289106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 290106266Sjulian && !timevalisset(&sc->stats.endTime)) { 291106319Sjulian getmicrotime(&sc->stats.elapsedTime); 292106266Sjulian timevalsub(&sc->stats.elapsedTime, 293106319Sjulian &sc->stats.startTime); 294106266Sjulian } 295106319Sjulian stats = (struct ng_source_stats *)resp->data; 296106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 297106266Sjulian } 298106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 299106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 300106266Sjulian } 301106266Sjulian break; 302106266Sjulian case NGM_SOURCE_START: 303106266Sjulian { 304106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 305106266Sjulian if (sc->output.hook == NULL) { 306106319Sjulian printf("%s: start on node with no output hook\n" 307125077Sharti , __func__); 308106266Sjulian error = EINVAL; 309106266Sjulian break; 310106266Sjulian } 311106266Sjulian /* TODO validation of packets */ 312106266Sjulian sc->packets = packets; 313106266Sjulian ng_source_start(sc); 314106266Sjulian } 315106266Sjulian break; 316125033Sharti case NGM_SOURCE_START_NOW: 317125033Sharti { 318125033Sharti u_int64_t packets = *(u_int64_t *)msg->data; 319125033Sharti if (sc->output.hook == NULL) { 320125033Sharti printf("%s: start on node with no output hook\n" 321125077Sharti , __func__); 322125033Sharti error = EINVAL; 323125033Sharti break; 324125033Sharti } 325125033Sharti if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 326125033Sharti error = EBUSY; 327125033Sharti break; 328125033Sharti } 329125033Sharti /* TODO validation of packets */ 330125033Sharti sc->packets = packets; 331125033Sharti sc->output_ifp = NULL; 332125033Sharti 333125033Sharti sc->node->nd_flags |= NG_SOURCE_ACTIVE; 334125033Sharti timevalclear(&sc->stats.elapsedTime); 335125033Sharti timevalclear(&sc->stats.endTime); 336125033Sharti getmicrotime(&sc->stats.startTime); 337125243Sharti sc->intr_ch = ng_timeout(node, NULL, 0, 338125243Sharti ng_source_intr, sc, 0); 339125033Sharti } 340125033Sharti break; 341106266Sjulian case NGM_SOURCE_STOP: 342106266Sjulian ng_source_stop(sc); 343106266Sjulian break; 344106266Sjulian case NGM_SOURCE_CLR_DATA: 345106266Sjulian ng_source_clr_data(sc); 346106266Sjulian break; 347106266Sjulian default: 348106266Sjulian error = EINVAL; 349106266Sjulian break; 350106266Sjulian } 351106266Sjulian break; 352106435Sjulian case NGM_ETHER_COOKIE: 353106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 354106435Sjulian error = EINVAL; 355106435Sjulian break; 356106435Sjulian } 357106435Sjulian switch (msg->header.cmd) { 358106435Sjulian case NGM_ETHER_GET_IFINDEX: 359106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 360106435Sjulian ng_source_set_autosrc(sc, 0); 361106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 362106435Sjulian timevalclear(&sc->stats.elapsedTime); 363106435Sjulian timevalclear(&sc->stats.endTime); 364106435Sjulian getmicrotime(&sc->stats.startTime); 365125243Sharti sc->intr_ch = ng_timeout(node, NULL, 0, 366125243Sharti ng_source_intr, sc, 0); 367106435Sjulian } 368106435Sjulian break; 369106435Sjulian default: 370106435Sjulian error = EINVAL; 371106435Sjulian } 372106435Sjulian break; 373106266Sjulian default: 374106266Sjulian error = EINVAL; 375106266Sjulian break; 376106266Sjulian } 377106266Sjulian 378106266Sjuliandone: 379106321Sjulian /* Take care of synchronous response, if any */ 380106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 381106321Sjulian /* Free the message and return */ 382106321Sjulian NG_FREE_MSG(msg); 383106266Sjulian return (error); 384106266Sjulian} 385106266Sjulian 386106266Sjulian/* 387106266Sjulian * Receive data on a hook 388106266Sjulian * 389106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 390106266Sjulian * If data comes in the output hook, discard it. 391106266Sjulian */ 392106266Sjulianstatic int 393106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 394106266Sjulian{ 395106321Sjulian sc_p sc; 396106321Sjulian struct source_hookinfo *hinfo; 397106266Sjulian int error = 0; 398106321Sjulian struct mbuf *m; 399106266Sjulian 400106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 401106321Sjulian NGI_GET_M(item, m); 402106321Sjulian NG_FREE_ITEM(item); 403106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 404125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 405125077Sharti KASSERT(hinfo != NULL, ("%s: null hook info", __func__)); 406106266Sjulian 407106266Sjulian /* Which hook? */ 408106266Sjulian if (hinfo == &sc->output) { 409106266Sjulian /* discard */ 410106321Sjulian NG_FREE_M(m); 411106266Sjulian return (error); 412106266Sjulian } 413125077Sharti KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__)); 414106266Sjulian 415106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 416125077Sharti printf("%s: mbuf without PKTHDR\n", __func__); 417106321Sjulian NG_FREE_M(m); 418106266Sjulian return (EINVAL); 419106266Sjulian } 420106266Sjulian 421106266Sjulian /* enque packet */ 422106266Sjulian /* XXX should we check IF_QFULL() ? */ 423125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 424106266Sjulian sc->queueOctets += m->m_pkthdr.len; 425106266Sjulian 426106266Sjulian return (0); 427106266Sjulian} 428106266Sjulian 429106266Sjulian/* 430106266Sjulian * Shutdown processing 431106266Sjulian */ 432106266Sjulianstatic int 433106266Sjulianng_source_rmnode(node_p node) 434106266Sjulian{ 435106321Sjulian sc_p sc; 436106266Sjulian 437106321Sjulian sc = NG_NODE_PRIVATE(node); 438125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 439106321Sjulian node->nd_flags |= NG_INVALID; 440106266Sjulian ng_source_stop(sc); 441106266Sjulian ng_source_clr_data(sc); 442106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 443106321Sjulian NG_NODE_UNREF(node); 444125077Sharti free(sc, M_NETGRAPH); 445106266Sjulian return (0); 446106266Sjulian} 447106266Sjulian 448106266Sjulian/* 449106266Sjulian * Hook disconnection 450106266Sjulian */ 451106266Sjulianstatic int 452106266Sjulianng_source_disconnect(hook_p hook) 453106266Sjulian{ 454106321Sjulian struct source_hookinfo *hinfo; 455106319Sjulian sc_p sc; 456106266Sjulian 457106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 458106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 459125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 460106266Sjulian hinfo->hook = NULL; 461106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 462106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 463106266Sjulian return (0); 464106266Sjulian} 465106266Sjulian 466106266Sjulian/* 467106435Sjulian * 468106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 469106435Sjulian * information. 470106266Sjulian */ 471125029Shartistatic int 472106435Sjulianng_source_request_output_ifp(sc_p sc) 473106266Sjulian{ 474106435Sjulian struct ng_mesg *msg; 475106266Sjulian int error = 0; 476106266Sjulian 477106266Sjulian sc->output_ifp = NULL; 478106266Sjulian 479106266Sjulian /* Ask the attached node for the connected interface's index */ 480106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 481106266Sjulian if (msg == NULL) 482106266Sjulian return (ENOBUFS); 483106266Sjulian 484125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 485106435Sjulian return (error); 486106435Sjulian} 487106266Sjulian 488106435Sjulian/* 489106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 490106435Sjulian * reached via our output hook. 491106435Sjulian */ 492106435Sjulianstatic int 493106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 494106435Sjulian{ 495106435Sjulian struct ifnet *ifp; 496106435Sjulian u_int32_t if_index; 497106435Sjulian int s; 498106266Sjulian 499106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 500106266Sjulian return (EINVAL); 501106266Sjulian 502106435Sjulian if_index = *(u_int32_t *)msg->data; 503106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 504106266Sjulian * way of verifying if_index is valid since if_indexlim is 505106266Sjulian * local to if_attach() 506106266Sjulian */ 507108172Shsu IFNET_RLOCK(); 508106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 509106266Sjulian if (ifp->if_index == if_index) 510106266Sjulian break; 511106266Sjulian } 512108172Shsu IFNET_RUNLOCK(); 513106266Sjulian 514106266Sjulian if (ifp == NULL) { 515125077Sharti printf("%s: can't find interface %d\n", __func__, if_index); 516106266Sjulian return (EINVAL); 517106266Sjulian } 518106266Sjulian sc->output_ifp = ifp; 519106266Sjulian 520106266Sjulian#if 1 521106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 522106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 523106266Sjulian * interface with small packets. 524106266Sjulian * XXX we should restore the original value at stop or disconnect 525106266Sjulian */ 526106266Sjulian s = splimp(); /* XXX is this required? */ 527125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 528106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 529106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 530106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 531106266Sjulian } 532106266Sjulian splx(s); 533106266Sjulian#endif 534106266Sjulian return (0); 535106266Sjulian} 536106266Sjulian 537106266Sjulian/* 538106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 539106266Sjulian */ 540106266Sjulianstatic int 541106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 542106266Sjulian{ 543106266Sjulian struct ng_mesg *msg; 544106266Sjulian int error = 0; 545106266Sjulian 546106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 547125077Sharti sizeof (u_int32_t), M_NOWAIT); 548106266Sjulian if (msg == NULL) 549106266Sjulian return(ENOBUFS); 550106266Sjulian 551106266Sjulian *(u_int32_t *)msg->data = flag; 552125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 553106266Sjulian return (error); 554106266Sjulian} 555106266Sjulian 556106266Sjulian/* 557106266Sjulian * Clear out the data we've queued 558106266Sjulian */ 559106266Sjulianstatic void 560106266Sjulianng_source_clr_data (sc_p sc) 561106266Sjulian{ 562106266Sjulian struct mbuf *m; 563106266Sjulian 564106266Sjulian for (;;) { 565125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 566106266Sjulian if (m == NULL) 567106266Sjulian break; 568106266Sjulian NG_FREE_M(m); 569106266Sjulian } 570106266Sjulian sc->queueOctets = 0; 571106266Sjulian} 572106266Sjulian 573106266Sjulian/* 574106266Sjulian * Start sending queued data out the output hook 575106266Sjulian */ 576106266Sjulianstatic void 577106266Sjulianng_source_start (sc_p sc) 578106266Sjulian{ 579106266Sjulian KASSERT(sc->output.hook != NULL, 580125077Sharti ("%s: output hook unconnected", __func__)); 581106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 582106435Sjulian (sc->output_ifp == NULL)) 583106435Sjulian ng_source_request_output_ifp(sc); 584106266Sjulian} 585106266Sjulian 586106266Sjulian/* 587106266Sjulian * Stop sending queued data out the output hook 588106266Sjulian */ 589106266Sjulianstatic void 590106266Sjulianng_source_stop (sc_p sc) 591106266Sjulian{ 592106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 593125243Sharti ng_untimeout(sc->intr_ch, sc->node); 594106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 595106266Sjulian getmicrotime(&sc->stats.endTime); 596106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 597106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 598106266Sjulian /* XXX should set this to the initial value instead */ 599106266Sjulian ng_source_set_autosrc(sc, 1); 600106266Sjulian } 601106266Sjulian} 602106266Sjulian 603106266Sjulian/* 604106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 605106266Sjulian * Sends as many packets as the interface connected to our 606106266Sjulian * output hook is able to enqueue. 607106266Sjulian */ 608106266Sjulianstatic void 609125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 610106266Sjulian{ 611125243Sharti sc_p sc = (sc_p)arg1; 612106266Sjulian struct ifqueue *ifq; 613106266Sjulian int packets; 614106266Sjulian 615125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 616106266Sjulian 617106266Sjulian callout_handle_init(&sc->intr_ch); 618106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 619106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 620106266Sjulian ng_source_stop(sc); 621106266Sjulian return; 622106266Sjulian } 623106266Sjulian 624125033Sharti if (sc->output_ifp != NULL) { 625125033Sharti ifq = &sc->output_ifp->if_snd; 626125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 627125033Sharti } else 628125033Sharti packets = sc->snd_queue.ifq_len; 629125033Sharti 630106266Sjulian ng_source_send(sc, packets, NULL); 631125243Sharti if (sc->packets == 0) 632106266Sjulian ng_source_stop(sc); 633125243Sharti else 634125243Sharti sc->intr_ch = ng_timeout(node, NULL, 0, 635125243Sharti ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 636106266Sjulian} 637106266Sjulian 638106266Sjulian/* 639106266Sjulian * Send packets out our output hook 640106266Sjulian */ 641106266Sjulianstatic int 642106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 643106266Sjulian{ 644106266Sjulian struct ifqueue tmp_queue; 645106266Sjulian struct mbuf *m, *m2; 646106266Sjulian int sent = 0; 647106266Sjulian int error = 0; 648106266Sjulian 649125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 650125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 651106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 652125077Sharti ("%s: inactive node", __func__)); 653106266Sjulian 654106266Sjulian if ((u_int64_t)tosend > sc->packets) 655106266Sjulian tosend = sc->packets; 656106266Sjulian 657106266Sjulian /* Copy the required number of packets to a temporary queue */ 658106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 659106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 660125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 661106266Sjulian if (m == NULL) 662106266Sjulian break; 663106266Sjulian 664106266Sjulian /* duplicate the packet */ 665111119Simp m2 = m_copypacket(m, M_DONTWAIT); 666106266Sjulian if (m2 == NULL) { 667125031Sharti _IF_PREPEND(&sc->snd_queue, m); 668106266Sjulian error = ENOBUFS; 669106266Sjulian break; 670106266Sjulian } 671106266Sjulian 672106266Sjulian /* re-enqueue the original packet for us */ 673125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 674106266Sjulian 675106266Sjulian /* queue the copy for sending at smplimp */ 676125031Sharti _IF_ENQUEUE(&tmp_queue, m2); 677106266Sjulian } 678106266Sjulian 679106266Sjulian sent = 0; 680106266Sjulian for (;;) { 681125031Sharti _IF_DEQUEUE(&tmp_queue, m2); 682106266Sjulian if (m2 == NULL) 683106266Sjulian break; 684106266Sjulian if (error == 0) { 685106266Sjulian ++sent; 686106266Sjulian sc->stats.outFrames++; 687106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 688106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 689125243Sharti if (error) 690125243Sharti printf("%s: error=%d\n", __func__, error); 691106266Sjulian } else { 692106266Sjulian NG_FREE_M(m2); 693106266Sjulian } 694106266Sjulian } 695106266Sjulian 696106266Sjulian sc->packets -= sent; 697106266Sjulian if (sent_p != NULL) 698106266Sjulian *sent_p = sent; 699106266Sjulian return (error); 700106266Sjulian} 701