ng_source.c revision 139823
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 139823 2005-01-07 01:45:51Z imp $"); 42125077Sharti 43106266Sjulian/* 44106266Sjulian * This node is used for high speed packet geneneration. It queues 45106266Sjulian * all data recieved on it's 'input' hook and when told to start via 46106266Sjulian * a control message it sends the packets out it's 'output' hook. In 47106266Sjulian * this way this node can be preloaded with a packet stream which is 48106266Sjulian * continuously sent. 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 55106266Sjulian * flag in the mbuf or some such. The node would peak 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 79106266Sjulian/* Per hook info */ 80106266Sjulianstruct source_hookinfo { 81106266Sjulian hook_p hook; 82106266Sjulian}; 83106266Sjulian 84106266Sjulian/* Per node info */ 85106266Sjulianstruct privdata { 86106266Sjulian node_p node; 87106266Sjulian struct source_hookinfo input; 88106266Sjulian struct source_hookinfo output; 89106266Sjulian struct ng_source_stats stats; 90106319Sjulian struct ifqueue snd_queue; /* packets to send */ 91106266Sjulian struct ifnet *output_ifp; 92137138Sglebius struct callout intr_ch; 93106319Sjulian u_int64_t packets; /* packets to send */ 94106266Sjulian u_int32_t queueOctets; 95106266Sjulian}; 96106266Sjuliantypedef struct privdata *sc_p; 97106266Sjulian 98106266Sjulian/* Node flags */ 99106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 100106266Sjulian 101106266Sjulian/* Netgraph methods */ 102106266Sjulianstatic ng_constructor_t ng_source_constructor; 103106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 104106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 105106266Sjulianstatic ng_newhook_t ng_source_newhook; 106106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 107106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 108106266Sjulian 109106266Sjulian/* Other functions */ 110125243Shartistatic void ng_source_intr(node_p, hook_p, void *, int); 111125029Shartistatic int ng_source_request_output_ifp (sc_p); 112106266Sjulianstatic void ng_source_clr_data (sc_p); 113106266Sjulianstatic void ng_source_start (sc_p); 114106266Sjulianstatic void ng_source_stop (sc_p); 115106266Sjulianstatic int ng_source_send (sc_p, int, int *); 116106435Sjulianstatic int ng_source_store_output_ifp(sc_p sc, 117106435Sjulian struct ng_mesg *msg); 118106266Sjulian 119106266Sjulian 120106266Sjulian/* Parse type for timeval */ 121125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = { 122106266Sjulian { "tv_sec", &ng_parse_int32_type }, 123106266Sjulian { "tv_usec", &ng_parse_int32_type }, 124106266Sjulian { NULL } 125106266Sjulian}; 126106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 127106266Sjulian &ng_parse_struct_type, 128106266Sjulian &ng_source_timeval_type_fields 129106266Sjulian}; 130106266Sjulian 131106266Sjulian/* Parse type for struct ng_source_stats */ 132106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 133106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 134106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 135106266Sjulian &ng_parse_struct_type, 136106266Sjulian &ng_source_stats_type_fields 137106266Sjulian}; 138106266Sjulian 139106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 140106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 141106266Sjulian { 142106266Sjulian NGM_SOURCE_COOKIE, 143106266Sjulian NGM_SOURCE_GET_STATS, 144106266Sjulian "getstats", 145106266Sjulian NULL, 146106266Sjulian &ng_source_stats_type 147106266Sjulian }, 148106266Sjulian { 149106266Sjulian NGM_SOURCE_COOKIE, 150106266Sjulian NGM_SOURCE_CLR_STATS, 151106266Sjulian "clrstats", 152106266Sjulian NULL, 153106266Sjulian NULL 154106266Sjulian }, 155106266Sjulian { 156106266Sjulian NGM_SOURCE_COOKIE, 157106266Sjulian NGM_SOURCE_GETCLR_STATS, 158106266Sjulian "getclrstats", 159106266Sjulian NULL, 160106266Sjulian &ng_source_stats_type 161106266Sjulian }, 162106266Sjulian { 163106266Sjulian NGM_SOURCE_COOKIE, 164106266Sjulian NGM_SOURCE_START, 165106266Sjulian "start", 166106266Sjulian &ng_parse_uint64_type, 167106266Sjulian NULL 168106266Sjulian }, 169106266Sjulian { 170106266Sjulian NGM_SOURCE_COOKIE, 171106266Sjulian NGM_SOURCE_STOP, 172106266Sjulian "stop", 173106266Sjulian NULL, 174106266Sjulian NULL 175106266Sjulian }, 176106266Sjulian { 177106266Sjulian NGM_SOURCE_COOKIE, 178106266Sjulian NGM_SOURCE_CLR_DATA, 179106266Sjulian "clrdata", 180106266Sjulian NULL, 181106266Sjulian NULL 182106266Sjulian }, 183125033Sharti { 184125033Sharti NGM_SOURCE_COOKIE, 185125033Sharti NGM_SOURCE_START_NOW, 186125033Sharti "start_now", 187125033Sharti &ng_parse_uint64_type, 188125033Sharti NULL 189125033Sharti }, 190106266Sjulian { 0 } 191106266Sjulian}; 192106266Sjulian 193106266Sjulian/* Netgraph type descriptor */ 194106266Sjulianstatic struct ng_type ng_source_typestruct = { 195129823Sjulian .version = NG_ABI_VERSION, 196129823Sjulian .name = NG_SOURCE_NODE_TYPE, 197129823Sjulian .constructor = ng_source_constructor, 198129823Sjulian .rcvmsg = ng_source_rcvmsg, 199129823Sjulian .shutdown = ng_source_rmnode, 200129823Sjulian .newhook = ng_source_newhook, 201129823Sjulian .rcvdata = ng_source_rcvdata, 202129823Sjulian .disconnect = ng_source_disconnect, 203129823Sjulian .cmdlist = ng_source_cmds, 204106266Sjulian}; 205106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 206106266Sjulian 207125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t); 208125032Sharti 209106266Sjulian/* 210106266Sjulian * Node constructor 211106266Sjulian */ 212106266Sjulianstatic int 213106321Sjulianng_source_constructor(node_p node) 214106266Sjulian{ 215106266Sjulian sc_p sc; 216106266Sjulian 217125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 218106266Sjulian if (sc == NULL) 219106266Sjulian return (ENOMEM); 220106266Sjulian 221106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 222106321Sjulian sc->node = node; 223106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 224137138Sglebius ng_callout_init(&sc->intr_ch); 225137138Sglebius 226106266Sjulian return (0); 227106266Sjulian} 228106266Sjulian 229106266Sjulian/* 230106266Sjulian * Add a hook 231106266Sjulian */ 232106266Sjulianstatic int 233106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 234106266Sjulian{ 235106321Sjulian sc_p sc; 236106266Sjulian 237106321Sjulian sc = NG_NODE_PRIVATE(node); 238125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 239106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 240106266Sjulian sc->input.hook = hook; 241106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 242106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 243106266Sjulian sc->output.hook = hook; 244106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 245106266Sjulian sc->output_ifp = 0; 246106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 247106266Sjulian } else 248106266Sjulian return (EINVAL); 249106266Sjulian return (0); 250106266Sjulian} 251106266Sjulian 252106266Sjulian/* 253106266Sjulian * Receive a control message 254106266Sjulian */ 255106266Sjulianstatic int 256106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 257106266Sjulian{ 258106321Sjulian sc_p sc; 259106266Sjulian struct ng_mesg *resp = NULL; 260106266Sjulian int error = 0; 261106321Sjulian struct ng_mesg *msg; 262106266Sjulian 263106321Sjulian sc = NG_NODE_PRIVATE(node); 264106321Sjulian NGI_GET_MSG(item, msg); 265125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 266106266Sjulian switch (msg->header.typecookie) { 267106266Sjulian case NGM_SOURCE_COOKIE: 268106435Sjulian if (msg->header.flags & NGF_RESP) { 269106435Sjulian error = EINVAL; 270106435Sjulian break; 271106435Sjulian } 272106266Sjulian switch (msg->header.cmd) { 273106266Sjulian case NGM_SOURCE_GET_STATS: 274106266Sjulian case NGM_SOURCE_CLR_STATS: 275106266Sjulian case NGM_SOURCE_GETCLR_STATS: 276106266Sjulian { 277106266Sjulian struct ng_source_stats *stats; 278106266Sjulian 279106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 280106266Sjulian NG_MKRESPONSE(resp, msg, 281106266Sjulian sizeof(*stats), M_NOWAIT); 282106266Sjulian if (resp == NULL) { 283106266Sjulian error = ENOMEM; 284106266Sjulian goto done; 285106266Sjulian } 286106266Sjulian sc->stats.queueOctets = sc->queueOctets; 287106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 288106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 289106266Sjulian && !timevalisset(&sc->stats.endTime)) { 290106319Sjulian getmicrotime(&sc->stats.elapsedTime); 291106266Sjulian timevalsub(&sc->stats.elapsedTime, 292106319Sjulian &sc->stats.startTime); 293106266Sjulian } 294106319Sjulian stats = (struct ng_source_stats *)resp->data; 295106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 296106266Sjulian } 297106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 298106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 299106266Sjulian } 300106266Sjulian break; 301106266Sjulian case NGM_SOURCE_START: 302106266Sjulian { 303106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 304106266Sjulian if (sc->output.hook == NULL) { 305106319Sjulian printf("%s: start on node with no output hook\n" 306125077Sharti , __func__); 307106266Sjulian error = EINVAL; 308106266Sjulian break; 309106266Sjulian } 310106266Sjulian /* TODO validation of packets */ 311106266Sjulian sc->packets = packets; 312106266Sjulian ng_source_start(sc); 313106266Sjulian } 314106266Sjulian break; 315125033Sharti case NGM_SOURCE_START_NOW: 316125033Sharti { 317125033Sharti u_int64_t packets = *(u_int64_t *)msg->data; 318125033Sharti if (sc->output.hook == NULL) { 319125033Sharti printf("%s: start on node with no output hook\n" 320125077Sharti , __func__); 321125033Sharti error = EINVAL; 322125033Sharti break; 323125033Sharti } 324125033Sharti if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 325125033Sharti error = EBUSY; 326125033Sharti break; 327125033Sharti } 328125033Sharti /* TODO validation of packets */ 329125033Sharti sc->packets = packets; 330125033Sharti sc->output_ifp = NULL; 331125033Sharti 332125033Sharti sc->node->nd_flags |= NG_SOURCE_ACTIVE; 333125033Sharti timevalclear(&sc->stats.elapsedTime); 334125033Sharti timevalclear(&sc->stats.endTime); 335125033Sharti getmicrotime(&sc->stats.startTime); 336138268Sglebius ng_callout(&sc->intr_ch, node, NULL, 0, 337125243Sharti 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); 364138268Sglebius ng_callout(&sc->intr_ch, node, NULL, 0, 365125243Sharti ng_source_intr, sc, 0); 366106435Sjulian } 367106435Sjulian break; 368106435Sjulian default: 369106435Sjulian error = EINVAL; 370106435Sjulian } 371106435Sjulian break; 372106266Sjulian default: 373106266Sjulian error = EINVAL; 374106266Sjulian break; 375106266Sjulian } 376106266Sjulian 377106266Sjuliandone: 378106321Sjulian /* Take care of synchronous response, if any */ 379106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 380106321Sjulian /* Free the message and return */ 381106321Sjulian NG_FREE_MSG(msg); 382106266Sjulian return (error); 383106266Sjulian} 384106266Sjulian 385106266Sjulian/* 386106266Sjulian * Receive data on a hook 387106266Sjulian * 388106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 389106266Sjulian * If data comes in the output hook, discard it. 390106266Sjulian */ 391106266Sjulianstatic int 392106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 393106266Sjulian{ 394106321Sjulian sc_p sc; 395106321Sjulian struct source_hookinfo *hinfo; 396106266Sjulian int error = 0; 397106321Sjulian struct mbuf *m; 398106266Sjulian 399106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 400106321Sjulian NGI_GET_M(item, m); 401106321Sjulian NG_FREE_ITEM(item); 402106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 403125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 404125077Sharti KASSERT(hinfo != NULL, ("%s: null hook info", __func__)); 405106266Sjulian 406106266Sjulian /* Which hook? */ 407106266Sjulian if (hinfo == &sc->output) { 408106266Sjulian /* discard */ 409106321Sjulian NG_FREE_M(m); 410106266Sjulian return (error); 411106266Sjulian } 412125077Sharti KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__)); 413106266Sjulian 414106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 415125077Sharti printf("%s: mbuf without PKTHDR\n", __func__); 416106321Sjulian NG_FREE_M(m); 417106266Sjulian return (EINVAL); 418106266Sjulian } 419106266Sjulian 420106266Sjulian /* enque packet */ 421106266Sjulian /* XXX should we check IF_QFULL() ? */ 422125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 423106266Sjulian sc->queueOctets += m->m_pkthdr.len; 424106266Sjulian 425106266Sjulian return (0); 426106266Sjulian} 427106266Sjulian 428106266Sjulian/* 429106266Sjulian * Shutdown processing 430106266Sjulian */ 431106266Sjulianstatic int 432106266Sjulianng_source_rmnode(node_p node) 433106266Sjulian{ 434106321Sjulian sc_p sc; 435106266Sjulian 436106321Sjulian sc = NG_NODE_PRIVATE(node); 437125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 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 482125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 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; 550125078Sharti NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0); 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) { 591138268Sglebius ng_uncallout(&sc->intr_ch, sc->node); 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 607125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 608106266Sjulian{ 609125243Sharti sc_p sc = (sc_p)arg1; 610106266Sjulian struct ifqueue *ifq; 611106266Sjulian int packets; 612106266Sjulian 613125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 614106266Sjulian 615106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 616106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 617106266Sjulian ng_source_stop(sc); 618106266Sjulian return; 619106266Sjulian } 620106266Sjulian 621125033Sharti if (sc->output_ifp != NULL) { 622125033Sharti ifq = &sc->output_ifp->if_snd; 623125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 624125033Sharti } else 625125033Sharti packets = sc->snd_queue.ifq_len; 626125033Sharti 627106266Sjulian ng_source_send(sc, packets, NULL); 628125243Sharti if (sc->packets == 0) 629106266Sjulian ng_source_stop(sc); 630125243Sharti else 631138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 632137136Sglebius ng_source_intr, sc, 0); 633106266Sjulian} 634106266Sjulian 635106266Sjulian/* 636106266Sjulian * Send packets out our output hook 637106266Sjulian */ 638106266Sjulianstatic int 639106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 640106266Sjulian{ 641106266Sjulian struct ifqueue tmp_queue; 642106266Sjulian struct mbuf *m, *m2; 643106266Sjulian int sent = 0; 644106266Sjulian int error = 0; 645106266Sjulian 646125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 647125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 648106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 649125077Sharti ("%s: inactive node", __func__)); 650106266Sjulian 651106266Sjulian if ((u_int64_t)tosend > sc->packets) 652106266Sjulian tosend = sc->packets; 653106266Sjulian 654106266Sjulian /* Copy the required number of packets to a temporary queue */ 655106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 656106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 657125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 658106266Sjulian if (m == NULL) 659106266Sjulian break; 660106266Sjulian 661106266Sjulian /* duplicate the packet */ 662111119Simp m2 = m_copypacket(m, M_DONTWAIT); 663106266Sjulian if (m2 == NULL) { 664125031Sharti _IF_PREPEND(&sc->snd_queue, m); 665106266Sjulian error = ENOBUFS; 666106266Sjulian break; 667106266Sjulian } 668106266Sjulian 669106266Sjulian /* re-enqueue the original packet for us */ 670125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 671106266Sjulian 672106266Sjulian /* queue the copy for sending at smplimp */ 673125031Sharti _IF_ENQUEUE(&tmp_queue, m2); 674106266Sjulian } 675106266Sjulian 676106266Sjulian sent = 0; 677106266Sjulian for (;;) { 678125031Sharti _IF_DEQUEUE(&tmp_queue, m2); 679106266Sjulian if (m2 == NULL) 680106266Sjulian break; 681106266Sjulian if (error == 0) { 682106266Sjulian ++sent; 683106266Sjulian sc->stats.outFrames++; 684106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 685106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 686125243Sharti if (error) 687125243Sharti printf("%s: error=%d\n", __func__, error); 688106266Sjulian } else { 689106266Sjulian NG_FREE_M(m2); 690106266Sjulian } 691106266Sjulian } 692106266Sjulian 693106266Sjulian sc->packets -= sent; 694106266Sjulian if (sent_p != NULL) 695106266Sjulian *sent_p = sent; 696106266Sjulian return (error); 697106266Sjulian} 698