ng_source.c revision 143387
1106266Sjulian/* 2106266Sjulian * ng_source.c 3139823Simp */ 4139823Simp 5139823Simp/*- 6106266Sjulian * Copyright 2002 Sandvine Inc. 7106266Sjulian * All rights reserved. 8106266Sjulian * 9106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and 10106266Sjulian * redistribution of this software, in source or object code forms, with or 11106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided, 12106266Sjulian * however, that: 13106319Sjulian * 1. Any and all reproductions of the source or object code must include the 14106319Sjulian * copyright notice above and the following disclaimer of warranties; and 15106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc. 16106319Sjulian * trademarks, including the mark "SANDVINE" on advertising, endorsements, 17106319Sjulian * or otherwise except as such appears in the above copyright notice or in 18106266Sjulian * the software. 19106266Sjulian * 20106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM 21106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES, 22106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, 23106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 24106266Sjulian * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR 25106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE 26106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY 27106266Sjulian * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES 28106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH 35106266Sjulian * DAMAGE. 36106266Sjulian * 37106266Sjulian * Author: Dave Chapeskie <dchapeskie@sandvine.com> 38106266Sjulian */ 39106266Sjulian 40125077Sharti#include <sys/cdefs.h> 41125077Sharti__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 143387 2005-03-10 21:50:50Z bmilekic $"); 42125077Sharti 43106266Sjulian/* 44106266Sjulian * This node is used for high speed packet geneneration. It queues 45143387Sbmilekic * all data recieved on its 'input' hook and when told to start via 46143387Sbmilekic * a control message it sends the packets out its 'output' hook. In 47143387Sbmilekic * this way this node can be preloaded with a packet stream which it 48143387Sbmilekic * can then send continuously as fast as possible. 49106266Sjulian * 50106266Sjulian * Currently it just copies the mbufs as required. It could do various 51106266Sjulian * tricks to try and avoid this. Probably the best performance would 52106266Sjulian * be achieved by modifying the appropriate drivers to be told to 53106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 54106266Sjulian * transmit descriptors) under control of this node; perhaps via some 55143387Sbmilekic * flag in the mbuf or some such. The node could peek at an appropriate 56106266Sjulian * ifnet flag to see if such support is available for the connected 57106266Sjulian * interface. 58106266Sjulian */ 59106266Sjulian 60106266Sjulian#include <sys/param.h> 61106266Sjulian#include <sys/systm.h> 62106266Sjulian#include <sys/errno.h> 63106266Sjulian#include <sys/kernel.h> 64106266Sjulian#include <sys/malloc.h> 65106266Sjulian#include <sys/mbuf.h> 66106266Sjulian#include <sys/socket.h> 67106266Sjulian#include <net/if.h> 68106266Sjulian#include <net/if_var.h> 69106266Sjulian#include <netgraph/ng_message.h> 70106266Sjulian#include <netgraph/netgraph.h> 71106266Sjulian#include <netgraph/ng_parse.h> 72106266Sjulian#include <netgraph/ng_ether.h> 73106266Sjulian#include <netgraph/ng_source.h> 74106266Sjulian 75106266Sjulian#define NG_SOURCE_INTR_TICKS 1 76106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 77106266Sjulian 78106266Sjulian/* Per hook info */ 79106266Sjulianstruct source_hookinfo { 80106266Sjulian hook_p hook; 81106266Sjulian}; 82106266Sjulian 83106266Sjulian/* Per node info */ 84106266Sjulianstruct privdata { 85106266Sjulian node_p node; 86106266Sjulian struct source_hookinfo input; 87106266Sjulian struct source_hookinfo output; 88106266Sjulian struct ng_source_stats stats; 89106319Sjulian struct ifqueue snd_queue; /* packets to send */ 90106266Sjulian struct ifnet *output_ifp; 91137138Sglebius struct callout intr_ch; 92106319Sjulian u_int64_t packets; /* packets to send */ 93106266Sjulian u_int32_t queueOctets; 94106266Sjulian}; 95106266Sjuliantypedef struct privdata *sc_p; 96106266Sjulian 97106266Sjulian/* Node flags */ 98106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 99106266Sjulian 100106266Sjulian/* Netgraph methods */ 101106266Sjulianstatic ng_constructor_t ng_source_constructor; 102106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 103106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 104106266Sjulianstatic ng_newhook_t ng_source_newhook; 105106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 106106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 107106266Sjulian 108106266Sjulian/* Other functions */ 109125243Shartistatic void ng_source_intr(node_p, hook_p, void *, int); 110125029Shartistatic int ng_source_request_output_ifp (sc_p); 111106266Sjulianstatic void ng_source_clr_data (sc_p); 112106266Sjulianstatic void ng_source_start (sc_p); 113106266Sjulianstatic void ng_source_stop (sc_p); 114106266Sjulianstatic int ng_source_send (sc_p, int, int *); 115106435Sjulianstatic int ng_source_store_output_ifp(sc_p sc, 116106435Sjulian struct ng_mesg *msg); 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 = { 193129823Sjulian .version = NG_ABI_VERSION, 194129823Sjulian .name = NG_SOURCE_NODE_TYPE, 195129823Sjulian .constructor = ng_source_constructor, 196129823Sjulian .rcvmsg = ng_source_rcvmsg, 197129823Sjulian .shutdown = ng_source_rmnode, 198129823Sjulian .newhook = ng_source_newhook, 199129823Sjulian .rcvdata = ng_source_rcvdata, 200129823Sjulian .disconnect = ng_source_disconnect, 201129823Sjulian .cmdlist = ng_source_cmds, 202106266Sjulian}; 203106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 204106266Sjulian 205125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t); 206125032Sharti 207106266Sjulian/* 208106266Sjulian * Node constructor 209106266Sjulian */ 210106266Sjulianstatic int 211106321Sjulianng_source_constructor(node_p node) 212106266Sjulian{ 213106266Sjulian sc_p sc; 214106266Sjulian 215125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 216106266Sjulian if (sc == NULL) 217106266Sjulian return (ENOMEM); 218106266Sjulian 219106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 220106321Sjulian sc->node = node; 221106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 222137138Sglebius ng_callout_init(&sc->intr_ch); 223137138Sglebius 224106266Sjulian return (0); 225106266Sjulian} 226106266Sjulian 227106266Sjulian/* 228106266Sjulian * Add a hook 229106266Sjulian */ 230106266Sjulianstatic int 231106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 232106266Sjulian{ 233106321Sjulian sc_p sc; 234106266Sjulian 235106321Sjulian sc = NG_NODE_PRIVATE(node); 236125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 237106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 238106266Sjulian sc->input.hook = hook; 239106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 240106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 241106266Sjulian sc->output.hook = hook; 242106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 243106266Sjulian sc->output_ifp = 0; 244106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 245106266Sjulian } else 246106266Sjulian return (EINVAL); 247106266Sjulian return (0); 248106266Sjulian} 249106266Sjulian 250106266Sjulian/* 251106266Sjulian * Receive a control message 252106266Sjulian */ 253106266Sjulianstatic int 254106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 255106266Sjulian{ 256106321Sjulian sc_p sc; 257106266Sjulian struct ng_mesg *resp = NULL; 258106266Sjulian int error = 0; 259106321Sjulian struct ng_mesg *msg; 260106266Sjulian 261106321Sjulian sc = NG_NODE_PRIVATE(node); 262106321Sjulian NGI_GET_MSG(item, msg); 263125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 264106266Sjulian switch (msg->header.typecookie) { 265106266Sjulian case NGM_SOURCE_COOKIE: 266106435Sjulian if (msg->header.flags & NGF_RESP) { 267106435Sjulian error = EINVAL; 268106435Sjulian break; 269106435Sjulian } 270106266Sjulian switch (msg->header.cmd) { 271106266Sjulian case NGM_SOURCE_GET_STATS: 272106266Sjulian case NGM_SOURCE_CLR_STATS: 273106266Sjulian case NGM_SOURCE_GETCLR_STATS: 274106266Sjulian { 275106266Sjulian struct ng_source_stats *stats; 276106266Sjulian 277106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 278106266Sjulian NG_MKRESPONSE(resp, msg, 279106266Sjulian sizeof(*stats), M_NOWAIT); 280106266Sjulian if (resp == NULL) { 281106266Sjulian error = ENOMEM; 282106266Sjulian goto done; 283106266Sjulian } 284106266Sjulian sc->stats.queueOctets = sc->queueOctets; 285106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 286106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 287106266Sjulian && !timevalisset(&sc->stats.endTime)) { 288106319Sjulian getmicrotime(&sc->stats.elapsedTime); 289106266Sjulian timevalsub(&sc->stats.elapsedTime, 290106319Sjulian &sc->stats.startTime); 291106266Sjulian } 292106319Sjulian stats = (struct ng_source_stats *)resp->data; 293106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 294106266Sjulian } 295106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 296106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 297106266Sjulian } 298106266Sjulian break; 299106266Sjulian case NGM_SOURCE_START: 300106266Sjulian { 301106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 302106266Sjulian if (sc->output.hook == NULL) { 303106319Sjulian printf("%s: start on node with no output hook\n" 304125077Sharti , __func__); 305106266Sjulian error = EINVAL; 306106266Sjulian break; 307106266Sjulian } 308106266Sjulian /* TODO validation of packets */ 309106266Sjulian sc->packets = packets; 310106266Sjulian ng_source_start(sc); 311106266Sjulian } 312106266Sjulian break; 313125033Sharti case NGM_SOURCE_START_NOW: 314125033Sharti { 315125033Sharti u_int64_t packets = *(u_int64_t *)msg->data; 316125033Sharti if (sc->output.hook == NULL) { 317125033Sharti printf("%s: start on node with no output hook\n" 318125077Sharti , __func__); 319125033Sharti error = EINVAL; 320125033Sharti break; 321125033Sharti } 322125033Sharti if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 323125033Sharti error = EBUSY; 324125033Sharti break; 325125033Sharti } 326125033Sharti /* TODO validation of packets */ 327125033Sharti sc->packets = packets; 328125033Sharti sc->output_ifp = NULL; 329125033Sharti 330125033Sharti sc->node->nd_flags |= NG_SOURCE_ACTIVE; 331125033Sharti timevalclear(&sc->stats.elapsedTime); 332125033Sharti timevalclear(&sc->stats.endTime); 333125033Sharti getmicrotime(&sc->stats.startTime); 334138268Sglebius ng_callout(&sc->intr_ch, node, NULL, 0, 335125243Sharti ng_source_intr, sc, 0); 336125033Sharti } 337125033Sharti break; 338106266Sjulian case NGM_SOURCE_STOP: 339106266Sjulian ng_source_stop(sc); 340106266Sjulian break; 341106266Sjulian case NGM_SOURCE_CLR_DATA: 342106266Sjulian ng_source_clr_data(sc); 343106266Sjulian break; 344106266Sjulian default: 345106266Sjulian error = EINVAL; 346106266Sjulian break; 347106266Sjulian } 348106266Sjulian break; 349106435Sjulian case NGM_ETHER_COOKIE: 350106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 351106435Sjulian error = EINVAL; 352106435Sjulian break; 353106435Sjulian } 354106435Sjulian switch (msg->header.cmd) { 355106435Sjulian case NGM_ETHER_GET_IFINDEX: 356106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 357106435Sjulian ng_source_set_autosrc(sc, 0); 358106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 359106435Sjulian timevalclear(&sc->stats.elapsedTime); 360106435Sjulian timevalclear(&sc->stats.endTime); 361106435Sjulian getmicrotime(&sc->stats.startTime); 362138268Sglebius ng_callout(&sc->intr_ch, node, NULL, 0, 363125243Sharti ng_source_intr, sc, 0); 364106435Sjulian } 365106435Sjulian break; 366106435Sjulian default: 367106435Sjulian error = EINVAL; 368106435Sjulian } 369106435Sjulian break; 370106266Sjulian default: 371106266Sjulian error = EINVAL; 372106266Sjulian break; 373106266Sjulian } 374106266Sjulian 375106266Sjuliandone: 376106321Sjulian /* Take care of synchronous response, if any */ 377106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 378106321Sjulian /* Free the message and return */ 379106321Sjulian NG_FREE_MSG(msg); 380106266Sjulian return (error); 381106266Sjulian} 382106266Sjulian 383106266Sjulian/* 384106266Sjulian * Receive data on a hook 385106266Sjulian * 386106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 387106266Sjulian * If data comes in the output hook, discard it. 388106266Sjulian */ 389106266Sjulianstatic int 390106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 391106266Sjulian{ 392106321Sjulian sc_p sc; 393106321Sjulian struct source_hookinfo *hinfo; 394106266Sjulian int error = 0; 395106321Sjulian struct mbuf *m; 396106266Sjulian 397106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 398106321Sjulian NGI_GET_M(item, m); 399106321Sjulian NG_FREE_ITEM(item); 400106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 401125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 402125077Sharti KASSERT(hinfo != NULL, ("%s: null hook info", __func__)); 403106266Sjulian 404106266Sjulian /* Which hook? */ 405106266Sjulian if (hinfo == &sc->output) { 406106266Sjulian /* discard */ 407106321Sjulian NG_FREE_M(m); 408106266Sjulian return (error); 409106266Sjulian } 410125077Sharti KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__)); 411106266Sjulian 412106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 413125077Sharti printf("%s: mbuf without PKTHDR\n", __func__); 414106321Sjulian NG_FREE_M(m); 415106266Sjulian return (EINVAL); 416106266Sjulian } 417106266Sjulian 418106266Sjulian /* enque packet */ 419106266Sjulian /* XXX should we check IF_QFULL() ? */ 420125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 421106266Sjulian sc->queueOctets += m->m_pkthdr.len; 422106266Sjulian 423106266Sjulian return (0); 424106266Sjulian} 425106266Sjulian 426106266Sjulian/* 427106266Sjulian * Shutdown processing 428106266Sjulian */ 429106266Sjulianstatic int 430106266Sjulianng_source_rmnode(node_p node) 431106266Sjulian{ 432106321Sjulian sc_p sc; 433106266Sjulian 434106321Sjulian sc = NG_NODE_PRIVATE(node); 435125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 436106266Sjulian ng_source_stop(sc); 437106266Sjulian ng_source_clr_data(sc); 438106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 439106321Sjulian NG_NODE_UNREF(node); 440125077Sharti free(sc, M_NETGRAPH); 441106266Sjulian return (0); 442106266Sjulian} 443106266Sjulian 444106266Sjulian/* 445106266Sjulian * Hook disconnection 446106266Sjulian */ 447106266Sjulianstatic int 448106266Sjulianng_source_disconnect(hook_p hook) 449106266Sjulian{ 450106321Sjulian struct source_hookinfo *hinfo; 451106319Sjulian sc_p sc; 452106266Sjulian 453106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 454106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 455125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 456106266Sjulian hinfo->hook = NULL; 457106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 458106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 459106266Sjulian return (0); 460106266Sjulian} 461106266Sjulian 462106266Sjulian/* 463106435Sjulian * 464106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 465106435Sjulian * information. 466106266Sjulian */ 467125029Shartistatic int 468106435Sjulianng_source_request_output_ifp(sc_p sc) 469106266Sjulian{ 470106435Sjulian struct ng_mesg *msg; 471106266Sjulian int error = 0; 472106266Sjulian 473106266Sjulian sc->output_ifp = NULL; 474106266Sjulian 475106266Sjulian /* Ask the attached node for the connected interface's index */ 476106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 477106266Sjulian if (msg == NULL) 478106266Sjulian return (ENOBUFS); 479106266Sjulian 480125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 481106435Sjulian return (error); 482106435Sjulian} 483106266Sjulian 484106435Sjulian/* 485106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 486106435Sjulian * reached via our output hook. 487106435Sjulian */ 488106435Sjulianstatic int 489106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 490106435Sjulian{ 491106435Sjulian struct ifnet *ifp; 492106435Sjulian u_int32_t if_index; 493106435Sjulian int s; 494106266Sjulian 495106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 496106266Sjulian return (EINVAL); 497106266Sjulian 498106435Sjulian if_index = *(u_int32_t *)msg->data; 499106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 500106266Sjulian * way of verifying if_index is valid since if_indexlim is 501106266Sjulian * local to if_attach() 502106266Sjulian */ 503108172Shsu IFNET_RLOCK(); 504106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 505106266Sjulian if (ifp->if_index == if_index) 506106266Sjulian break; 507106266Sjulian } 508108172Shsu IFNET_RUNLOCK(); 509106266Sjulian 510106266Sjulian if (ifp == NULL) { 511125077Sharti printf("%s: can't find interface %d\n", __func__, if_index); 512106266Sjulian return (EINVAL); 513106266Sjulian } 514106266Sjulian sc->output_ifp = ifp; 515106266Sjulian 516106266Sjulian#if 1 517106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 518106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 519106266Sjulian * interface with small packets. 520106266Sjulian * XXX we should restore the original value at stop or disconnect 521106266Sjulian */ 522106266Sjulian s = splimp(); /* XXX is this required? */ 523125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 524106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 525106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 526106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 527106266Sjulian } 528106266Sjulian splx(s); 529106266Sjulian#endif 530106266Sjulian return (0); 531106266Sjulian} 532106266Sjulian 533106266Sjulian/* 534106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 535106266Sjulian */ 536106266Sjulianstatic int 537106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 538106266Sjulian{ 539106266Sjulian struct ng_mesg *msg; 540106266Sjulian int error = 0; 541106266Sjulian 542106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 543125077Sharti sizeof (u_int32_t), M_NOWAIT); 544106266Sjulian if (msg == NULL) 545106266Sjulian return(ENOBUFS); 546106266Sjulian 547106266Sjulian *(u_int32_t *)msg->data = flag; 548125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 549106266Sjulian return (error); 550106266Sjulian} 551106266Sjulian 552106266Sjulian/* 553106266Sjulian * Clear out the data we've queued 554106266Sjulian */ 555106266Sjulianstatic void 556106266Sjulianng_source_clr_data (sc_p sc) 557106266Sjulian{ 558106266Sjulian struct mbuf *m; 559106266Sjulian 560106266Sjulian for (;;) { 561125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 562106266Sjulian if (m == NULL) 563106266Sjulian break; 564106266Sjulian NG_FREE_M(m); 565106266Sjulian } 566106266Sjulian sc->queueOctets = 0; 567106266Sjulian} 568106266Sjulian 569106266Sjulian/* 570106266Sjulian * Start sending queued data out the output hook 571106266Sjulian */ 572106266Sjulianstatic void 573106266Sjulianng_source_start (sc_p sc) 574106266Sjulian{ 575106266Sjulian KASSERT(sc->output.hook != NULL, 576125077Sharti ("%s: output hook unconnected", __func__)); 577106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 578106435Sjulian (sc->output_ifp == NULL)) 579106435Sjulian ng_source_request_output_ifp(sc); 580106266Sjulian} 581106266Sjulian 582106266Sjulian/* 583106266Sjulian * Stop sending queued data out the output hook 584106266Sjulian */ 585106266Sjulianstatic void 586106266Sjulianng_source_stop (sc_p sc) 587106266Sjulian{ 588106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 589138268Sglebius ng_uncallout(&sc->intr_ch, sc->node); 590106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 591106266Sjulian getmicrotime(&sc->stats.endTime); 592106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 593106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 594106266Sjulian /* XXX should set this to the initial value instead */ 595106266Sjulian ng_source_set_autosrc(sc, 1); 596106266Sjulian } 597106266Sjulian} 598106266Sjulian 599106266Sjulian/* 600106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 601106266Sjulian * Sends as many packets as the interface connected to our 602106266Sjulian * output hook is able to enqueue. 603106266Sjulian */ 604106266Sjulianstatic void 605125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 606106266Sjulian{ 607125243Sharti sc_p sc = (sc_p)arg1; 608106266Sjulian struct ifqueue *ifq; 609106266Sjulian int packets; 610106266Sjulian 611125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 612106266Sjulian 613106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 614106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 615106266Sjulian ng_source_stop(sc); 616106266Sjulian return; 617106266Sjulian } 618106266Sjulian 619125033Sharti if (sc->output_ifp != NULL) { 620141745Sru ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 621125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 622125033Sharti } else 623125033Sharti packets = sc->snd_queue.ifq_len; 624125033Sharti 625106266Sjulian ng_source_send(sc, packets, NULL); 626125243Sharti if (sc->packets == 0) 627106266Sjulian ng_source_stop(sc); 628125243Sharti else 629138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 630137136Sglebius ng_source_intr, sc, 0); 631106266Sjulian} 632106266Sjulian 633106266Sjulian/* 634106266Sjulian * Send packets out our output hook 635106266Sjulian */ 636106266Sjulianstatic int 637106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 638106266Sjulian{ 639106266Sjulian struct ifqueue tmp_queue; 640106266Sjulian struct mbuf *m, *m2; 641106266Sjulian int sent = 0; 642106266Sjulian int error = 0; 643106266Sjulian 644125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 645125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 646106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 647125077Sharti ("%s: inactive node", __func__)); 648106266Sjulian 649106266Sjulian if ((u_int64_t)tosend > sc->packets) 650106266Sjulian tosend = sc->packets; 651106266Sjulian 652106266Sjulian /* Copy the required number of packets to a temporary queue */ 653106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 654106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 655125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 656106266Sjulian if (m == NULL) 657106266Sjulian break; 658106266Sjulian 659106266Sjulian /* duplicate the packet */ 660111119Simp m2 = m_copypacket(m, M_DONTWAIT); 661106266Sjulian if (m2 == NULL) { 662125031Sharti _IF_PREPEND(&sc->snd_queue, m); 663106266Sjulian error = ENOBUFS; 664106266Sjulian break; 665106266Sjulian } 666106266Sjulian 667106266Sjulian /* re-enqueue the original packet for us */ 668125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 669106266Sjulian 670106266Sjulian /* queue the copy for sending at smplimp */ 671125031Sharti _IF_ENQUEUE(&tmp_queue, m2); 672106266Sjulian } 673106266Sjulian 674106266Sjulian sent = 0; 675106266Sjulian for (;;) { 676125031Sharti _IF_DEQUEUE(&tmp_queue, m2); 677106266Sjulian if (m2 == NULL) 678106266Sjulian break; 679106266Sjulian if (error == 0) { 680106266Sjulian ++sent; 681106266Sjulian sc->stats.outFrames++; 682106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 683106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 684125243Sharti if (error) 685125243Sharti printf("%s: error=%d\n", __func__, error); 686106266Sjulian } else { 687106266Sjulian NG_FREE_M(m2); 688106266Sjulian } 689106266Sjulian } 690106266Sjulian 691106266Sjulian sc->packets -= sent; 692106266Sjulian if (sent_p != NULL) 693106266Sjulian *sent_p = sent; 694106266Sjulian return (error); 695106266Sjulian} 696