ng_source.c revision 125032
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 * $FreeBSD: head/sys/netgraph/ng_source.c 125032 2004-01-26 14:48:21Z harti $ 38106266Sjulian */ 39106266Sjulian 40106266Sjulian/* 41106266Sjulian * This node is used for high speed packet geneneration. It queues 42106266Sjulian * all data recieved on it's 'input' hook and when told to start via 43106266Sjulian * a control message it sends the packets out it's 'output' hook. In 44106266Sjulian * this way this node can be preloaded with a packet stream which is 45106266Sjulian * continuously sent. 46106266Sjulian * 47106266Sjulian * Currently it just copies the mbufs as required. It could do various 48106266Sjulian * tricks to try and avoid this. Probably the best performance would 49106266Sjulian * be achieved by modifying the appropriate drivers to be told to 50106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 51106266Sjulian * transmit descriptors) under control of this node; perhaps via some 52106266Sjulian * flag in the mbuf or some such. The node would peak at an appropriate 53106266Sjulian * ifnet flag to see if such support is available for the connected 54106266Sjulian * interface. 55106266Sjulian */ 56106266Sjulian 57106266Sjulian#include <sys/param.h> 58106266Sjulian#include <sys/systm.h> 59106266Sjulian#include <sys/errno.h> 60106266Sjulian#include <sys/kernel.h> 61106266Sjulian#include <sys/malloc.h> 62106266Sjulian#include <sys/mbuf.h> 63106266Sjulian#include <sys/socket.h> 64106266Sjulian#include <net/if.h> 65106266Sjulian#include <net/if_var.h> 66106266Sjulian#include <netgraph/ng_message.h> 67106266Sjulian#include <netgraph/netgraph.h> 68106266Sjulian#include <netgraph/ng_parse.h> 69106266Sjulian#include <netgraph/ng_ether.h> 70106266Sjulian#include <netgraph/ng_source.h> 71106266Sjulian 72106266Sjulian#define NG_SOURCE_INTR_TICKS 1 73106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 74106266Sjulian 75106266Sjulian 76106266Sjulian/* Per hook info */ 77106266Sjulianstruct source_hookinfo { 78106266Sjulian hook_p hook; 79106266Sjulian}; 80106266Sjulian 81106266Sjulian/* Per node info */ 82106266Sjulianstruct privdata { 83106266Sjulian node_p node; 84106266Sjulian struct source_hookinfo input; 85106266Sjulian struct source_hookinfo output; 86106266Sjulian struct ng_source_stats stats; 87106319Sjulian struct ifqueue snd_queue; /* packets to send */ 88106266Sjulian struct ifnet *output_ifp; 89106266Sjulian struct callout_handle intr_ch; 90106319Sjulian u_int64_t packets; /* packets to send */ 91106266Sjulian u_int32_t queueOctets; 92106266Sjulian}; 93106266Sjuliantypedef struct privdata *sc_p; 94106266Sjulian 95106266Sjulian/* Node flags */ 96106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 97106266Sjulian 98106266Sjulian/* XXX */ 99106266Sjulian#if 1 100106266Sjulian#undef KASSERT 101106266Sjulian#define KASSERT(expr,msg) do { \ 102106266Sjulian if (!(expr)) { \ 103106266Sjulian printf msg ; \ 104106266Sjulian panic("Assertion"); \ 105106266Sjulian } \ 106106266Sjulian } while(0) 107106266Sjulian#endif 108106266Sjulian 109106266Sjulian/* Netgraph methods */ 110106266Sjulianstatic ng_constructor_t ng_source_constructor; 111106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 112106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 113106266Sjulianstatic ng_newhook_t ng_source_newhook; 114106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 115106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 116106266Sjulian 117106266Sjulian/* Other functions */ 118106266Sjulianstatic timeout_t ng_source_intr; 119125029Shartistatic int ng_source_request_output_ifp (sc_p); 120106266Sjulianstatic void ng_source_clr_data (sc_p); 121106266Sjulianstatic void ng_source_start (sc_p); 122106266Sjulianstatic void ng_source_stop (sc_p); 123106266Sjulianstatic int ng_source_send (sc_p, int, int *); 124106435Sjulianstatic int ng_source_store_output_ifp(sc_p sc, 125106435Sjulian struct ng_mesg *msg); 126106266Sjulian 127106266Sjulian 128106266Sjulian/* Parse type for timeval */ 129106266Sjulianstatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = 130106266Sjulian{ 131106266Sjulian { "tv_sec", &ng_parse_int32_type }, 132106266Sjulian { "tv_usec", &ng_parse_int32_type }, 133106266Sjulian { NULL } 134106266Sjulian}; 135106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 136106266Sjulian &ng_parse_struct_type, 137106266Sjulian &ng_source_timeval_type_fields 138106266Sjulian}; 139106266Sjulian 140106266Sjulian/* Parse type for struct ng_source_stats */ 141106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 142106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 143106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 144106266Sjulian &ng_parse_struct_type, 145106266Sjulian &ng_source_stats_type_fields 146106266Sjulian}; 147106266Sjulian 148106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 149106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 150106266Sjulian { 151106266Sjulian NGM_SOURCE_COOKIE, 152106266Sjulian NGM_SOURCE_GET_STATS, 153106266Sjulian "getstats", 154106266Sjulian NULL, 155106266Sjulian &ng_source_stats_type 156106266Sjulian }, 157106266Sjulian { 158106266Sjulian NGM_SOURCE_COOKIE, 159106266Sjulian NGM_SOURCE_CLR_STATS, 160106266Sjulian "clrstats", 161106266Sjulian NULL, 162106266Sjulian NULL 163106266Sjulian }, 164106266Sjulian { 165106266Sjulian NGM_SOURCE_COOKIE, 166106266Sjulian NGM_SOURCE_GETCLR_STATS, 167106266Sjulian "getclrstats", 168106266Sjulian NULL, 169106266Sjulian &ng_source_stats_type 170106266Sjulian }, 171106266Sjulian { 172106266Sjulian NGM_SOURCE_COOKIE, 173106266Sjulian NGM_SOURCE_START, 174106266Sjulian "start", 175106266Sjulian &ng_parse_uint64_type, 176106266Sjulian NULL 177106266Sjulian }, 178106266Sjulian { 179106266Sjulian NGM_SOURCE_COOKIE, 180106266Sjulian NGM_SOURCE_STOP, 181106266Sjulian "stop", 182106266Sjulian NULL, 183106266Sjulian NULL 184106266Sjulian }, 185106266Sjulian { 186106266Sjulian NGM_SOURCE_COOKIE, 187106266Sjulian NGM_SOURCE_CLR_DATA, 188106266Sjulian "clrdata", 189106266Sjulian NULL, 190106266Sjulian NULL 191106266Sjulian }, 192106266Sjulian { 0 } 193106266Sjulian}; 194106266Sjulian 195106266Sjulian/* Netgraph type descriptor */ 196106266Sjulianstatic struct ng_type ng_source_typestruct = { 197125029Sharti NG_ABI_VERSION, 198106266Sjulian NG_SOURCE_NODE_TYPE, 199106266Sjulian NULL, /* module event handler */ 200106266Sjulian ng_source_constructor, 201106266Sjulian ng_source_rcvmsg, 202106266Sjulian ng_source_rmnode, 203106266Sjulian ng_source_newhook, 204106266Sjulian NULL, /* findhook */ 205106266Sjulian NULL, 206106266Sjulian ng_source_rcvdata, /* rcvdata */ 207106266Sjulian ng_source_disconnect, 208106266Sjulian ng_source_cmds 209106266Sjulian}; 210106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 211106266Sjulian 212125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t); 213125032Sharti 214106266Sjulian/* 215106266Sjulian * Node constructor 216106266Sjulian */ 217106266Sjulianstatic int 218106321Sjulianng_source_constructor(node_p node) 219106266Sjulian{ 220106266Sjulian sc_p sc; 221106266Sjulian 222125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 223106266Sjulian if (sc == NULL) 224106266Sjulian return (ENOMEM); 225106266Sjulian 226106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 227106321Sjulian sc->node = node; 228106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 229106321Sjulian callout_handle_init(&sc->intr_ch); /* XXX fix.. will 230106321Sjulian cause problems. */ 231106266Sjulian return (0); 232106266Sjulian} 233106266Sjulian 234106266Sjulian/* 235106266Sjulian * Add a hook 236106266Sjulian */ 237106266Sjulianstatic int 238106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 239106266Sjulian{ 240106321Sjulian sc_p sc; 241106266Sjulian 242106321Sjulian sc = NG_NODE_PRIVATE(node); 243106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 244106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 245106266Sjulian sc->input.hook = hook; 246106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 247106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 248106266Sjulian sc->output.hook = hook; 249106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 250106266Sjulian sc->output_ifp = 0; 251106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 252106266Sjulian } else 253106266Sjulian return (EINVAL); 254106266Sjulian return (0); 255106266Sjulian} 256106266Sjulian 257106266Sjulian/* 258106266Sjulian * Receive a control message 259106266Sjulian */ 260106266Sjulianstatic int 261106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 262106266Sjulian{ 263106321Sjulian sc_p sc; 264106266Sjulian struct ng_mesg *resp = NULL; 265106266Sjulian int error = 0; 266106321Sjulian struct ng_mesg *msg; 267106266Sjulian 268106321Sjulian sc = NG_NODE_PRIVATE(node); 269106321Sjulian NGI_GET_MSG(item, msg); 270106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 271106266Sjulian switch (msg->header.typecookie) { 272106266Sjulian case NGM_SOURCE_COOKIE: 273106435Sjulian if (msg->header.flags & NGF_RESP) { 274106435Sjulian error = EINVAL; 275106435Sjulian break; 276106435Sjulian } 277106266Sjulian switch (msg->header.cmd) { 278106266Sjulian case NGM_SOURCE_GET_STATS: 279106266Sjulian case NGM_SOURCE_CLR_STATS: 280106266Sjulian case NGM_SOURCE_GETCLR_STATS: 281106266Sjulian { 282106266Sjulian struct ng_source_stats *stats; 283106266Sjulian 284106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 285106266Sjulian NG_MKRESPONSE(resp, msg, 286106266Sjulian sizeof(*stats), M_NOWAIT); 287106266Sjulian if (resp == NULL) { 288106266Sjulian error = ENOMEM; 289106266Sjulian goto done; 290106266Sjulian } 291106266Sjulian sc->stats.queueOctets = sc->queueOctets; 292106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 293106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 294106266Sjulian && !timevalisset(&sc->stats.endTime)) { 295106319Sjulian getmicrotime(&sc->stats.elapsedTime); 296106266Sjulian timevalsub(&sc->stats.elapsedTime, 297106319Sjulian &sc->stats.startTime); 298106266Sjulian } 299106319Sjulian stats = (struct ng_source_stats *)resp->data; 300106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 301106266Sjulian } 302106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 303106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 304106266Sjulian } 305106266Sjulian break; 306106266Sjulian case NGM_SOURCE_START: 307106266Sjulian { 308106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 309106266Sjulian if (sc->output.hook == NULL) { 310106319Sjulian printf("%s: start on node with no output hook\n" 311106319Sjulian , __FUNCTION__); 312106266Sjulian error = EINVAL; 313106266Sjulian break; 314106266Sjulian } 315106266Sjulian /* TODO validation of packets */ 316106266Sjulian sc->packets = packets; 317106266Sjulian ng_source_start(sc); 318106266Sjulian } 319106266Sjulian break; 320106266Sjulian case NGM_SOURCE_STOP: 321106266Sjulian ng_source_stop(sc); 322106266Sjulian break; 323106266Sjulian case NGM_SOURCE_CLR_DATA: 324106266Sjulian ng_source_clr_data(sc); 325106266Sjulian break; 326106266Sjulian default: 327106266Sjulian error = EINVAL; 328106266Sjulian break; 329106266Sjulian } 330106266Sjulian break; 331106435Sjulian case NGM_ETHER_COOKIE: 332106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 333106435Sjulian error = EINVAL; 334106435Sjulian break; 335106435Sjulian } 336106435Sjulian switch (msg->header.cmd) { 337106435Sjulian case NGM_ETHER_GET_IFINDEX: 338106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 339106435Sjulian ng_source_set_autosrc(sc, 0); 340106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 341106435Sjulian timevalclear(&sc->stats.elapsedTime); 342106435Sjulian timevalclear(&sc->stats.endTime); 343106435Sjulian getmicrotime(&sc->stats.startTime); 344106435Sjulian sc->intr_ch = timeout(ng_source_intr, sc, 0); 345106435Sjulian } 346106435Sjulian break; 347106435Sjulian default: 348106435Sjulian error = EINVAL; 349106435Sjulian } 350106435Sjulian break; 351106266Sjulian default: 352106266Sjulian error = EINVAL; 353106266Sjulian break; 354106266Sjulian } 355106266Sjulian 356106266Sjuliandone: 357106321Sjulian /* Take care of synchronous response, if any */ 358106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 359106321Sjulian /* Free the message and return */ 360106321Sjulian NG_FREE_MSG(msg); 361106266Sjulian return (error); 362106266Sjulian} 363106266Sjulian 364106266Sjulian/* 365106266Sjulian * Receive data on a hook 366106266Sjulian * 367106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 368106266Sjulian * If data comes in the output hook, discard it. 369106266Sjulian */ 370106266Sjulianstatic int 371106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 372106266Sjulian{ 373106321Sjulian sc_p sc; 374106321Sjulian struct source_hookinfo *hinfo; 375106266Sjulian int error = 0; 376106321Sjulian struct mbuf *m; 377106266Sjulian 378106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 379106321Sjulian NGI_GET_M(item, m); 380106321Sjulian NG_FREE_ITEM(item); 381106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 382106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 383106266Sjulian KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__)); 384106266Sjulian 385106266Sjulian /* Which hook? */ 386106266Sjulian if (hinfo == &sc->output) { 387106266Sjulian /* discard */ 388106321Sjulian NG_FREE_M(m); 389106266Sjulian return (error); 390106266Sjulian } 391106266Sjulian KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__)); 392106266Sjulian 393106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 394106266Sjulian printf("%s: mbuf without PKTHDR\n", __FUNCTION__); 395106321Sjulian NG_FREE_M(m); 396106266Sjulian return (EINVAL); 397106266Sjulian } 398106266Sjulian 399106266Sjulian /* enque packet */ 400106266Sjulian /* XXX should we check IF_QFULL() ? */ 401125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 402106266Sjulian sc->queueOctets += m->m_pkthdr.len; 403106266Sjulian 404106266Sjulian return (0); 405106266Sjulian} 406106266Sjulian 407106266Sjulian/* 408106266Sjulian * Shutdown processing 409106266Sjulian */ 410106266Sjulianstatic int 411106266Sjulianng_source_rmnode(node_p node) 412106266Sjulian{ 413106321Sjulian sc_p sc; 414106266Sjulian 415106321Sjulian sc = NG_NODE_PRIVATE(node); 416106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 417106321Sjulian node->nd_flags |= NG_INVALID; 418106266Sjulian ng_source_stop(sc); 419106266Sjulian ng_source_clr_data(sc); 420106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 421106321Sjulian NG_NODE_UNREF(node); 422106266Sjulian FREE(sc, M_NETGRAPH); 423106266Sjulian return (0); 424106266Sjulian} 425106266Sjulian 426106266Sjulian/* 427106266Sjulian * Hook disconnection 428106266Sjulian */ 429106266Sjulianstatic int 430106266Sjulianng_source_disconnect(hook_p hook) 431106266Sjulian{ 432106321Sjulian struct source_hookinfo *hinfo; 433106319Sjulian sc_p sc; 434106266Sjulian 435106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 436106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 437106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 438106266Sjulian hinfo->hook = NULL; 439106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 440106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 441106266Sjulian return (0); 442106266Sjulian} 443106266Sjulian 444106266Sjulian/* 445106435Sjulian * 446106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 447106435Sjulian * information. 448106266Sjulian */ 449125029Shartistatic int 450106435Sjulianng_source_request_output_ifp(sc_p sc) 451106266Sjulian{ 452106435Sjulian struct ng_mesg *msg; 453106266Sjulian int error = 0; 454106266Sjulian 455106266Sjulian sc->output_ifp = NULL; 456106266Sjulian 457106266Sjulian /* Ask the attached node for the connected interface's index */ 458106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 459106266Sjulian if (msg == NULL) 460106266Sjulian return (ENOBUFS); 461106266Sjulian 462106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 463106435Sjulian return (error); 464106435Sjulian} 465106266Sjulian 466106435Sjulian/* 467106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 468106435Sjulian * reached via our output hook. 469106435Sjulian */ 470106435Sjulianstatic int 471106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 472106435Sjulian{ 473106435Sjulian struct ifnet *ifp; 474106435Sjulian u_int32_t if_index; 475106435Sjulian int s; 476106266Sjulian 477106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 478106266Sjulian return (EINVAL); 479106266Sjulian 480106435Sjulian if_index = *(u_int32_t *)msg->data; 481106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 482106266Sjulian * way of verifying if_index is valid since if_indexlim is 483106266Sjulian * local to if_attach() 484106266Sjulian */ 485108172Shsu IFNET_RLOCK(); 486106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 487106266Sjulian if (ifp->if_index == if_index) 488106266Sjulian break; 489106266Sjulian } 490108172Shsu IFNET_RUNLOCK(); 491106266Sjulian 492106266Sjulian if (ifp == NULL) { 493106319Sjulian printf("%s: can't find interface %d\n", __FUNCTION__, if_index); 494106266Sjulian return (EINVAL); 495106266Sjulian } 496106266Sjulian sc->output_ifp = ifp; 497106266Sjulian 498106266Sjulian#if 1 499106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 500106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 501106266Sjulian * interface with small packets. 502106266Sjulian * XXX we should restore the original value at stop or disconnect 503106266Sjulian */ 504106266Sjulian s = splimp(); /* XXX is this required? */ 505106266Sjulian if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) 506106266Sjulian { 507106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 508106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 509106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 510106266Sjulian } 511106266Sjulian splx(s); 512106266Sjulian#endif 513106266Sjulian return (0); 514106266Sjulian} 515106266Sjulian 516106266Sjulian/* 517106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 518106266Sjulian */ 519106266Sjulianstatic int 520106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 521106266Sjulian{ 522106266Sjulian struct ng_mesg *msg; 523106266Sjulian int error = 0; 524106266Sjulian 525106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 526106266Sjulian sizeof (u_int32_t), M_NOWAIT); 527106266Sjulian if (msg == NULL) 528106266Sjulian return(ENOBUFS); 529106266Sjulian 530106266Sjulian *(u_int32_t *)msg->data = flag; 531106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 532106266Sjulian return (error); 533106266Sjulian} 534106266Sjulian 535106266Sjulian/* 536106266Sjulian * Clear out the data we've queued 537106266Sjulian */ 538106266Sjulianstatic void 539106266Sjulianng_source_clr_data (sc_p sc) 540106266Sjulian{ 541106266Sjulian struct mbuf *m; 542106266Sjulian 543106266Sjulian for (;;) { 544125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 545106266Sjulian if (m == NULL) 546106266Sjulian break; 547106266Sjulian NG_FREE_M(m); 548106266Sjulian } 549106266Sjulian sc->queueOctets = 0; 550106266Sjulian} 551106266Sjulian 552106266Sjulian/* 553106266Sjulian * Start sending queued data out the output hook 554106266Sjulian */ 555106266Sjulianstatic void 556106266Sjulianng_source_start (sc_p sc) 557106266Sjulian{ 558106266Sjulian KASSERT(sc->output.hook != NULL, 559106266Sjulian ("%s: output hook unconnected", __FUNCTION__)); 560106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 561106435Sjulian (sc->output_ifp == NULL)) 562106435Sjulian ng_source_request_output_ifp(sc); 563106266Sjulian} 564106266Sjulian 565106266Sjulian/* 566106266Sjulian * Stop sending queued data out the output hook 567106266Sjulian */ 568106266Sjulianstatic void 569106266Sjulianng_source_stop (sc_p sc) 570106266Sjulian{ 571106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 572106266Sjulian untimeout(ng_source_intr, sc, sc->intr_ch); 573106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 574106266Sjulian getmicrotime(&sc->stats.endTime); 575106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 576106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 577106266Sjulian /* XXX should set this to the initial value instead */ 578106266Sjulian ng_source_set_autosrc(sc, 1); 579106266Sjulian } 580106266Sjulian} 581106266Sjulian 582106266Sjulian/* 583106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 584106266Sjulian * Sends as many packets as the interface connected to our 585106266Sjulian * output hook is able to enqueue. 586106266Sjulian */ 587106266Sjulianstatic void 588106266Sjulianng_source_intr (void *arg) 589106266Sjulian{ 590106321Sjulian sc_p sc = (sc_p) arg; 591106266Sjulian struct ifqueue *ifq; 592106266Sjulian int packets; 593106266Sjulian 594106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 595106266Sjulian 596106266Sjulian callout_handle_init(&sc->intr_ch); 597106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 598106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 599106266Sjulian ng_source_stop(sc); 600106266Sjulian return; 601106266Sjulian } 602106266Sjulian 603106266Sjulian ifq = &sc->output_ifp->if_snd; 604106266Sjulian packets = ifq->ifq_maxlen - ifq->ifq_len; 605106266Sjulian ng_source_send(sc, packets, NULL); 606106266Sjulian if (sc->packets == 0) { 607106266Sjulian int s = splnet(); 608106266Sjulian ng_source_stop(sc); 609106266Sjulian splx(s); 610106266Sjulian } else 611106319Sjulian sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 612106266Sjulian} 613106266Sjulian 614106266Sjulian/* 615106266Sjulian * Send packets out our output hook 616106266Sjulian */ 617106266Sjulianstatic int 618106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 619106266Sjulian{ 620106266Sjulian struct ifqueue tmp_queue; 621106266Sjulian struct mbuf *m, *m2; 622106266Sjulian int sent = 0; 623106266Sjulian int error = 0; 624106266Sjulian int s, s2; 625106266Sjulian 626106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 627106266Sjulian KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__)); 628106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 629106266Sjulian ("%s: inactive node", __FUNCTION__)); 630106266Sjulian 631106266Sjulian if ((u_int64_t)tosend > sc->packets) 632106266Sjulian tosend = sc->packets; 633106266Sjulian 634106266Sjulian /* Copy the required number of packets to a temporary queue */ 635106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 636106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 637106266Sjulian s = splnet(); 638125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 639106266Sjulian splx(s); 640106266Sjulian if (m == NULL) 641106266Sjulian break; 642106266Sjulian 643106266Sjulian /* duplicate the packet */ 644111119Simp m2 = m_copypacket(m, M_DONTWAIT); 645106266Sjulian if (m2 == NULL) { 646106266Sjulian s = splnet(); 647125031Sharti _IF_PREPEND(&sc->snd_queue, m); 648106266Sjulian splx(s); 649106266Sjulian error = ENOBUFS; 650106266Sjulian break; 651106266Sjulian } 652106266Sjulian 653106266Sjulian /* re-enqueue the original packet for us */ 654106266Sjulian s = splnet(); 655125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 656106266Sjulian splx(s); 657106266Sjulian 658106266Sjulian /* queue the copy for sending at smplimp */ 659125031Sharti _IF_ENQUEUE(&tmp_queue, m2); 660106266Sjulian } 661106266Sjulian 662106266Sjulian sent = 0; 663106266Sjulian s = splimp(); 664106266Sjulian for (;;) { 665125031Sharti _IF_DEQUEUE(&tmp_queue, m2); 666106266Sjulian if (m2 == NULL) 667106266Sjulian break; 668106266Sjulian if (error == 0) { 669106266Sjulian ++sent; 670106266Sjulian sc->stats.outFrames++; 671106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 672106266Sjulian s2 = splnet(); 673106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 674106266Sjulian splx(s2); 675106266Sjulian } else { 676106266Sjulian NG_FREE_M(m2); 677106266Sjulian } 678106266Sjulian } 679106266Sjulian splx(s); 680106266Sjulian 681106266Sjulian sc->packets -= sent; 682106266Sjulian if (sent_p != NULL) 683106266Sjulian *sent_p = sent; 684106266Sjulian return (error); 685106266Sjulian} 686