ng_source.c revision 125077
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 125077 2004-01-27 10:45:37Z 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 */ 108106266Sjulianstatic timeout_t ng_source_intr; 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); 337125033Sharti sc->intr_ch = timeout(ng_source_intr, sc, 0); 338125033Sharti } 339125033Sharti break; 340106266Sjulian case NGM_SOURCE_STOP: 341106266Sjulian ng_source_stop(sc); 342106266Sjulian break; 343106266Sjulian case NGM_SOURCE_CLR_DATA: 344106266Sjulian ng_source_clr_data(sc); 345106266Sjulian break; 346106266Sjulian default: 347106266Sjulian error = EINVAL; 348106266Sjulian break; 349106266Sjulian } 350106266Sjulian break; 351106435Sjulian case NGM_ETHER_COOKIE: 352106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 353106435Sjulian error = EINVAL; 354106435Sjulian break; 355106435Sjulian } 356106435Sjulian switch (msg->header.cmd) { 357106435Sjulian case NGM_ETHER_GET_IFINDEX: 358106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 359106435Sjulian ng_source_set_autosrc(sc, 0); 360106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 361106435Sjulian timevalclear(&sc->stats.elapsedTime); 362106435Sjulian timevalclear(&sc->stats.endTime); 363106435Sjulian getmicrotime(&sc->stats.startTime); 364106435Sjulian sc->intr_ch = timeout(ng_source_intr, sc, 0); 365106435Sjulian } 366106435Sjulian break; 367106435Sjulian default: 368106435Sjulian error = EINVAL; 369106435Sjulian } 370106435Sjulian break; 371106266Sjulian default: 372106266Sjulian error = EINVAL; 373106266Sjulian break; 374106266Sjulian } 375106266Sjulian 376106266Sjuliandone: 377106321Sjulian /* Take care of synchronous response, if any */ 378106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 379106321Sjulian /* Free the message and return */ 380106321Sjulian NG_FREE_MSG(msg); 381106266Sjulian return (error); 382106266Sjulian} 383106266Sjulian 384106266Sjulian/* 385106266Sjulian * Receive data on a hook 386106266Sjulian * 387106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 388106266Sjulian * If data comes in the output hook, discard it. 389106266Sjulian */ 390106266Sjulianstatic int 391106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 392106266Sjulian{ 393106321Sjulian sc_p sc; 394106321Sjulian struct source_hookinfo *hinfo; 395106266Sjulian int error = 0; 396106321Sjulian struct mbuf *m; 397106266Sjulian 398106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 399106321Sjulian NGI_GET_M(item, m); 400106321Sjulian NG_FREE_ITEM(item); 401106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 402125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 403125077Sharti KASSERT(hinfo != NULL, ("%s: null hook info", __func__)); 404106266Sjulian 405106266Sjulian /* Which hook? */ 406106266Sjulian if (hinfo == &sc->output) { 407106266Sjulian /* discard */ 408106321Sjulian NG_FREE_M(m); 409106266Sjulian return (error); 410106266Sjulian } 411125077Sharti KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__)); 412106266Sjulian 413106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 414125077Sharti printf("%s: mbuf without PKTHDR\n", __func__); 415106321Sjulian NG_FREE_M(m); 416106266Sjulian return (EINVAL); 417106266Sjulian } 418106266Sjulian 419106266Sjulian /* enque packet */ 420106266Sjulian /* XXX should we check IF_QFULL() ? */ 421125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 422106266Sjulian sc->queueOctets += m->m_pkthdr.len; 423106266Sjulian 424106266Sjulian return (0); 425106266Sjulian} 426106266Sjulian 427106266Sjulian/* 428106266Sjulian * Shutdown processing 429106266Sjulian */ 430106266Sjulianstatic int 431106266Sjulianng_source_rmnode(node_p node) 432106266Sjulian{ 433106321Sjulian sc_p sc; 434106266Sjulian 435106321Sjulian sc = NG_NODE_PRIVATE(node); 436125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 437106321Sjulian node->nd_flags |= NG_INVALID; 438106266Sjulian ng_source_stop(sc); 439106266Sjulian ng_source_clr_data(sc); 440106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 441106321Sjulian NG_NODE_UNREF(node); 442125077Sharti free(sc, M_NETGRAPH); 443106266Sjulian return (0); 444106266Sjulian} 445106266Sjulian 446106266Sjulian/* 447106266Sjulian * Hook disconnection 448106266Sjulian */ 449106266Sjulianstatic int 450106266Sjulianng_source_disconnect(hook_p hook) 451106266Sjulian{ 452106321Sjulian struct source_hookinfo *hinfo; 453106319Sjulian sc_p sc; 454106266Sjulian 455106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 456106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 457125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 458106266Sjulian hinfo->hook = NULL; 459106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 460106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 461106266Sjulian return (0); 462106266Sjulian} 463106266Sjulian 464106266Sjulian/* 465106435Sjulian * 466106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 467106435Sjulian * information. 468106266Sjulian */ 469125029Shartistatic int 470106435Sjulianng_source_request_output_ifp(sc_p sc) 471106266Sjulian{ 472106435Sjulian struct ng_mesg *msg; 473106266Sjulian int error = 0; 474106266Sjulian 475106266Sjulian sc->output_ifp = NULL; 476106266Sjulian 477106266Sjulian /* Ask the attached node for the connected interface's index */ 478106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 479106266Sjulian if (msg == NULL) 480106266Sjulian return (ENOBUFS); 481106266Sjulian 482106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 483106435Sjulian return (error); 484106435Sjulian} 485106266Sjulian 486106435Sjulian/* 487106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 488106435Sjulian * reached via our output hook. 489106435Sjulian */ 490106435Sjulianstatic int 491106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 492106435Sjulian{ 493106435Sjulian struct ifnet *ifp; 494106435Sjulian u_int32_t if_index; 495106435Sjulian int s; 496106266Sjulian 497106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 498106266Sjulian return (EINVAL); 499106266Sjulian 500106435Sjulian if_index = *(u_int32_t *)msg->data; 501106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 502106266Sjulian * way of verifying if_index is valid since if_indexlim is 503106266Sjulian * local to if_attach() 504106266Sjulian */ 505108172Shsu IFNET_RLOCK(); 506106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 507106266Sjulian if (ifp->if_index == if_index) 508106266Sjulian break; 509106266Sjulian } 510108172Shsu IFNET_RUNLOCK(); 511106266Sjulian 512106266Sjulian if (ifp == NULL) { 513125077Sharti printf("%s: can't find interface %d\n", __func__, if_index); 514106266Sjulian return (EINVAL); 515106266Sjulian } 516106266Sjulian sc->output_ifp = ifp; 517106266Sjulian 518106266Sjulian#if 1 519106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 520106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 521106266Sjulian * interface with small packets. 522106266Sjulian * XXX we should restore the original value at stop or disconnect 523106266Sjulian */ 524106266Sjulian s = splimp(); /* XXX is this required? */ 525125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 526106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 527106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 528106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 529106266Sjulian } 530106266Sjulian splx(s); 531106266Sjulian#endif 532106266Sjulian return (0); 533106266Sjulian} 534106266Sjulian 535106266Sjulian/* 536106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 537106266Sjulian */ 538106266Sjulianstatic int 539106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 540106266Sjulian{ 541106266Sjulian struct ng_mesg *msg; 542106266Sjulian int error = 0; 543106266Sjulian 544106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 545125077Sharti sizeof (u_int32_t), M_NOWAIT); 546106266Sjulian if (msg == NULL) 547106266Sjulian return(ENOBUFS); 548106266Sjulian 549106266Sjulian *(u_int32_t *)msg->data = flag; 550106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 551106266Sjulian return (error); 552106266Sjulian} 553106266Sjulian 554106266Sjulian/* 555106266Sjulian * Clear out the data we've queued 556106266Sjulian */ 557106266Sjulianstatic void 558106266Sjulianng_source_clr_data (sc_p sc) 559106266Sjulian{ 560106266Sjulian struct mbuf *m; 561106266Sjulian 562106266Sjulian for (;;) { 563125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 564106266Sjulian if (m == NULL) 565106266Sjulian break; 566106266Sjulian NG_FREE_M(m); 567106266Sjulian } 568106266Sjulian sc->queueOctets = 0; 569106266Sjulian} 570106266Sjulian 571106266Sjulian/* 572106266Sjulian * Start sending queued data out the output hook 573106266Sjulian */ 574106266Sjulianstatic void 575106266Sjulianng_source_start (sc_p sc) 576106266Sjulian{ 577106266Sjulian KASSERT(sc->output.hook != NULL, 578125077Sharti ("%s: output hook unconnected", __func__)); 579106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 580106435Sjulian (sc->output_ifp == NULL)) 581106435Sjulian ng_source_request_output_ifp(sc); 582106266Sjulian} 583106266Sjulian 584106266Sjulian/* 585106266Sjulian * Stop sending queued data out the output hook 586106266Sjulian */ 587106266Sjulianstatic void 588106266Sjulianng_source_stop (sc_p sc) 589106266Sjulian{ 590106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 591106266Sjulian untimeout(ng_source_intr, sc, sc->intr_ch); 592106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 593106266Sjulian getmicrotime(&sc->stats.endTime); 594106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 595106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 596106266Sjulian /* XXX should set this to the initial value instead */ 597106266Sjulian ng_source_set_autosrc(sc, 1); 598106266Sjulian } 599106266Sjulian} 600106266Sjulian 601106266Sjulian/* 602106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 603106266Sjulian * Sends as many packets as the interface connected to our 604106266Sjulian * output hook is able to enqueue. 605106266Sjulian */ 606106266Sjulianstatic void 607106266Sjulianng_source_intr (void *arg) 608106266Sjulian{ 609106321Sjulian sc_p sc = (sc_p) arg; 610106266Sjulian struct ifqueue *ifq; 611106266Sjulian int packets; 612106266Sjulian 613125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 614106266Sjulian 615106266Sjulian callout_handle_init(&sc->intr_ch); 616106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 617106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 618106266Sjulian ng_source_stop(sc); 619106266Sjulian return; 620106266Sjulian } 621106266Sjulian 622125033Sharti if (sc->output_ifp != NULL) { 623125033Sharti ifq = &sc->output_ifp->if_snd; 624125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 625125033Sharti } else 626125033Sharti packets = sc->snd_queue.ifq_len; 627125033Sharti 628106266Sjulian ng_source_send(sc, packets, NULL); 629106266Sjulian if (sc->packets == 0) { 630106266Sjulian int s = splnet(); 631106266Sjulian ng_source_stop(sc); 632106266Sjulian splx(s); 633106266Sjulian } else 634106319Sjulian sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 635106266Sjulian} 636106266Sjulian 637106266Sjulian/* 638106266Sjulian * Send packets out our output hook 639106266Sjulian */ 640106266Sjulianstatic int 641106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 642106266Sjulian{ 643106266Sjulian struct ifqueue tmp_queue; 644106266Sjulian struct mbuf *m, *m2; 645106266Sjulian int sent = 0; 646106266Sjulian int error = 0; 647106266Sjulian int s, s2; 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) { 660106266Sjulian s = splnet(); 661125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 662106266Sjulian splx(s); 663106266Sjulian if (m == NULL) 664106266Sjulian break; 665106266Sjulian 666106266Sjulian /* duplicate the packet */ 667111119Simp m2 = m_copypacket(m, M_DONTWAIT); 668106266Sjulian if (m2 == NULL) { 669106266Sjulian s = splnet(); 670125031Sharti _IF_PREPEND(&sc->snd_queue, m); 671106266Sjulian splx(s); 672106266Sjulian error = ENOBUFS; 673106266Sjulian break; 674106266Sjulian } 675106266Sjulian 676106266Sjulian /* re-enqueue the original packet for us */ 677106266Sjulian s = splnet(); 678125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 679106266Sjulian splx(s); 680106266Sjulian 681106266Sjulian /* queue the copy for sending at smplimp */ 682125031Sharti _IF_ENQUEUE(&tmp_queue, m2); 683106266Sjulian } 684106266Sjulian 685106266Sjulian sent = 0; 686106266Sjulian s = splimp(); 687106266Sjulian for (;;) { 688125031Sharti _IF_DEQUEUE(&tmp_queue, m2); 689106266Sjulian if (m2 == NULL) 690106266Sjulian break; 691106266Sjulian if (error == 0) { 692106266Sjulian ++sent; 693106266Sjulian sc->stats.outFrames++; 694106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 695106266Sjulian s2 = splnet(); 696106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 697106266Sjulian splx(s2); 698106266Sjulian } else { 699106266Sjulian NG_FREE_M(m2); 700106266Sjulian } 701106266Sjulian } 702106266Sjulian splx(s); 703106266Sjulian 704106266Sjulian sc->packets -= sent; 705106266Sjulian if (sent_p != NULL) 706106266Sjulian *sent_p = sent; 707106266Sjulian return (error); 708106266Sjulian} 709