ng_source.c revision 125030
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 125030 2004-01-26 14:44:36Z 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 212106266Sjulian/* 213106266Sjulian * Node constructor 214106266Sjulian */ 215106266Sjulianstatic int 216106321Sjulianng_source_constructor(node_p node) 217106266Sjulian{ 218106266Sjulian sc_p sc; 219106266Sjulian 220125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 221106266Sjulian if (sc == NULL) 222106266Sjulian return (ENOMEM); 223106266Sjulian 224106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 225106321Sjulian sc->node = node; 226106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 227106321Sjulian callout_handle_init(&sc->intr_ch); /* XXX fix.. will 228106321Sjulian cause problems. */ 229106266Sjulian return (0); 230106266Sjulian} 231106266Sjulian 232106266Sjulian/* 233106266Sjulian * Add a hook 234106266Sjulian */ 235106266Sjulianstatic int 236106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 237106266Sjulian{ 238106321Sjulian sc_p sc; 239106266Sjulian 240106321Sjulian sc = NG_NODE_PRIVATE(node); 241106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 242106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 243106266Sjulian sc->input.hook = hook; 244106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 245106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 246106266Sjulian sc->output.hook = hook; 247106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 248106266Sjulian sc->output_ifp = 0; 249106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 250106266Sjulian } else 251106266Sjulian return (EINVAL); 252106266Sjulian return (0); 253106266Sjulian} 254106266Sjulian 255106266Sjulian/* 256106266Sjulian * Receive a control message 257106266Sjulian */ 258106266Sjulianstatic int 259106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 260106266Sjulian{ 261106321Sjulian sc_p sc; 262106266Sjulian struct ng_mesg *resp = NULL; 263106266Sjulian int error = 0; 264106321Sjulian struct ng_mesg *msg; 265106266Sjulian 266106321Sjulian sc = NG_NODE_PRIVATE(node); 267106321Sjulian NGI_GET_MSG(item, msg); 268106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 269106266Sjulian switch (msg->header.typecookie) { 270106266Sjulian case NGM_SOURCE_COOKIE: 271106435Sjulian if (msg->header.flags & NGF_RESP) { 272106435Sjulian error = EINVAL; 273106435Sjulian break; 274106435Sjulian } 275106266Sjulian switch (msg->header.cmd) { 276106266Sjulian case NGM_SOURCE_GET_STATS: 277106266Sjulian case NGM_SOURCE_CLR_STATS: 278106266Sjulian case NGM_SOURCE_GETCLR_STATS: 279106266Sjulian { 280106266Sjulian struct ng_source_stats *stats; 281106266Sjulian 282106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 283106266Sjulian NG_MKRESPONSE(resp, msg, 284106266Sjulian sizeof(*stats), M_NOWAIT); 285106266Sjulian if (resp == NULL) { 286106266Sjulian error = ENOMEM; 287106266Sjulian goto done; 288106266Sjulian } 289106266Sjulian sc->stats.queueOctets = sc->queueOctets; 290106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 291106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 292106266Sjulian && !timevalisset(&sc->stats.endTime)) { 293106319Sjulian getmicrotime(&sc->stats.elapsedTime); 294106266Sjulian timevalsub(&sc->stats.elapsedTime, 295106319Sjulian &sc->stats.startTime); 296106266Sjulian } 297106319Sjulian stats = (struct ng_source_stats *)resp->data; 298106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 299106266Sjulian } 300106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 301106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 302106266Sjulian } 303106266Sjulian break; 304106266Sjulian case NGM_SOURCE_START: 305106266Sjulian { 306106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 307106266Sjulian if (sc->output.hook == NULL) { 308106319Sjulian printf("%s: start on node with no output hook\n" 309106319Sjulian , __FUNCTION__); 310106266Sjulian error = EINVAL; 311106266Sjulian break; 312106266Sjulian } 313106266Sjulian /* TODO validation of packets */ 314106266Sjulian sc->packets = packets; 315106266Sjulian ng_source_start(sc); 316106266Sjulian } 317106266Sjulian break; 318106266Sjulian case NGM_SOURCE_STOP: 319106266Sjulian ng_source_stop(sc); 320106266Sjulian break; 321106266Sjulian case NGM_SOURCE_CLR_DATA: 322106266Sjulian ng_source_clr_data(sc); 323106266Sjulian break; 324106266Sjulian default: 325106266Sjulian error = EINVAL; 326106266Sjulian break; 327106266Sjulian } 328106266Sjulian break; 329106435Sjulian case NGM_ETHER_COOKIE: 330106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 331106435Sjulian error = EINVAL; 332106435Sjulian break; 333106435Sjulian } 334106435Sjulian switch (msg->header.cmd) { 335106435Sjulian case NGM_ETHER_GET_IFINDEX: 336106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 337106435Sjulian ng_source_set_autosrc(sc, 0); 338106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 339106435Sjulian timevalclear(&sc->stats.elapsedTime); 340106435Sjulian timevalclear(&sc->stats.endTime); 341106435Sjulian getmicrotime(&sc->stats.startTime); 342106435Sjulian sc->intr_ch = timeout(ng_source_intr, sc, 0); 343106435Sjulian } 344106435Sjulian break; 345106435Sjulian default: 346106435Sjulian error = EINVAL; 347106435Sjulian } 348106435Sjulian break; 349106266Sjulian default: 350106266Sjulian error = EINVAL; 351106266Sjulian break; 352106266Sjulian } 353106266Sjulian 354106266Sjuliandone: 355106321Sjulian /* Take care of synchronous response, if any */ 356106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 357106321Sjulian /* Free the message and return */ 358106321Sjulian NG_FREE_MSG(msg); 359106266Sjulian return (error); 360106266Sjulian} 361106266Sjulian 362106266Sjulian/* 363106266Sjulian * Receive data on a hook 364106266Sjulian * 365106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 366106266Sjulian * If data comes in the output hook, discard it. 367106266Sjulian */ 368106266Sjulianstatic int 369106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 370106266Sjulian{ 371106321Sjulian sc_p sc; 372106321Sjulian struct source_hookinfo *hinfo; 373106266Sjulian int error = 0; 374106321Sjulian struct mbuf *m; 375106266Sjulian 376106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 377106321Sjulian NGI_GET_M(item, m); 378106321Sjulian NG_FREE_ITEM(item); 379106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 380106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 381106266Sjulian KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__)); 382106266Sjulian 383106266Sjulian /* Which hook? */ 384106266Sjulian if (hinfo == &sc->output) { 385106266Sjulian /* discard */ 386106321Sjulian NG_FREE_M(m); 387106266Sjulian return (error); 388106266Sjulian } 389106266Sjulian KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__)); 390106266Sjulian 391106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 392106266Sjulian printf("%s: mbuf without PKTHDR\n", __FUNCTION__); 393106321Sjulian NG_FREE_M(m); 394106266Sjulian return (EINVAL); 395106266Sjulian } 396106266Sjulian 397106266Sjulian /* enque packet */ 398106266Sjulian /* XXX should we check IF_QFULL() ? */ 399106266Sjulian IF_ENQUEUE(&sc->snd_queue, m); 400106266Sjulian sc->queueOctets += m->m_pkthdr.len; 401106266Sjulian 402106266Sjulian return (0); 403106266Sjulian} 404106266Sjulian 405106266Sjulian/* 406106266Sjulian * Shutdown processing 407106266Sjulian */ 408106266Sjulianstatic int 409106266Sjulianng_source_rmnode(node_p node) 410106266Sjulian{ 411106321Sjulian sc_p sc; 412106266Sjulian 413106321Sjulian sc = NG_NODE_PRIVATE(node); 414106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 415106321Sjulian node->nd_flags |= NG_INVALID; 416106266Sjulian ng_source_stop(sc); 417106266Sjulian ng_source_clr_data(sc); 418106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 419106321Sjulian NG_NODE_UNREF(node); 420106266Sjulian FREE(sc, M_NETGRAPH); 421106266Sjulian return (0); 422106266Sjulian} 423106266Sjulian 424106266Sjulian/* 425106266Sjulian * Hook disconnection 426106266Sjulian */ 427106266Sjulianstatic int 428106266Sjulianng_source_disconnect(hook_p hook) 429106266Sjulian{ 430106321Sjulian struct source_hookinfo *hinfo; 431106319Sjulian sc_p sc; 432106266Sjulian 433106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 434106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 435106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 436106266Sjulian hinfo->hook = NULL; 437106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 438106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 439106266Sjulian return (0); 440106266Sjulian} 441106266Sjulian 442106266Sjulian/* 443106435Sjulian * 444106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 445106435Sjulian * information. 446106266Sjulian */ 447125029Shartistatic int 448106435Sjulianng_source_request_output_ifp(sc_p sc) 449106266Sjulian{ 450106435Sjulian struct ng_mesg *msg; 451106266Sjulian int error = 0; 452106266Sjulian 453106266Sjulian sc->output_ifp = NULL; 454106266Sjulian 455106266Sjulian /* Ask the attached node for the connected interface's index */ 456106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 457106266Sjulian if (msg == NULL) 458106266Sjulian return (ENOBUFS); 459106266Sjulian 460106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 461106435Sjulian return (error); 462106435Sjulian} 463106266Sjulian 464106435Sjulian/* 465106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 466106435Sjulian * reached via our output hook. 467106435Sjulian */ 468106435Sjulianstatic int 469106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 470106435Sjulian{ 471106435Sjulian struct ifnet *ifp; 472106435Sjulian u_int32_t if_index; 473106435Sjulian int s; 474106266Sjulian 475106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 476106266Sjulian return (EINVAL); 477106266Sjulian 478106435Sjulian if_index = *(u_int32_t *)msg->data; 479106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 480106266Sjulian * way of verifying if_index is valid since if_indexlim is 481106266Sjulian * local to if_attach() 482106266Sjulian */ 483108172Shsu IFNET_RLOCK(); 484106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 485106266Sjulian if (ifp->if_index == if_index) 486106266Sjulian break; 487106266Sjulian } 488108172Shsu IFNET_RUNLOCK(); 489106266Sjulian 490106266Sjulian if (ifp == NULL) { 491106319Sjulian printf("%s: can't find interface %d\n", __FUNCTION__, if_index); 492106266Sjulian return (EINVAL); 493106266Sjulian } 494106266Sjulian sc->output_ifp = ifp; 495106266Sjulian 496106266Sjulian#if 1 497106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 498106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 499106266Sjulian * interface with small packets. 500106266Sjulian * XXX we should restore the original value at stop or disconnect 501106266Sjulian */ 502106266Sjulian s = splimp(); /* XXX is this required? */ 503106266Sjulian if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) 504106266Sjulian { 505106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 506106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 507106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 508106266Sjulian } 509106266Sjulian splx(s); 510106266Sjulian#endif 511106266Sjulian return (0); 512106266Sjulian} 513106266Sjulian 514106266Sjulian/* 515106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 516106266Sjulian */ 517106266Sjulianstatic int 518106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 519106266Sjulian{ 520106266Sjulian struct ng_mesg *msg; 521106266Sjulian int error = 0; 522106266Sjulian 523106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 524106266Sjulian sizeof (u_int32_t), M_NOWAIT); 525106266Sjulian if (msg == NULL) 526106266Sjulian return(ENOBUFS); 527106266Sjulian 528106266Sjulian *(u_int32_t *)msg->data = flag; 529106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 530106266Sjulian return (error); 531106266Sjulian} 532106266Sjulian 533106266Sjulian/* 534106266Sjulian * Clear out the data we've queued 535106266Sjulian */ 536106266Sjulianstatic void 537106266Sjulianng_source_clr_data (sc_p sc) 538106266Sjulian{ 539106266Sjulian struct mbuf *m; 540106266Sjulian 541106266Sjulian for (;;) { 542106266Sjulian IF_DEQUEUE(&sc->snd_queue, m); 543106266Sjulian if (m == NULL) 544106266Sjulian break; 545106266Sjulian NG_FREE_M(m); 546106266Sjulian } 547106266Sjulian sc->queueOctets = 0; 548106266Sjulian} 549106266Sjulian 550106266Sjulian/* 551106266Sjulian * Start sending queued data out the output hook 552106266Sjulian */ 553106266Sjulianstatic void 554106266Sjulianng_source_start (sc_p sc) 555106266Sjulian{ 556106266Sjulian KASSERT(sc->output.hook != NULL, 557106266Sjulian ("%s: output hook unconnected", __FUNCTION__)); 558106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 559106435Sjulian (sc->output_ifp == NULL)) 560106435Sjulian ng_source_request_output_ifp(sc); 561106266Sjulian} 562106266Sjulian 563106266Sjulian/* 564106266Sjulian * Stop sending queued data out the output hook 565106266Sjulian */ 566106266Sjulianstatic void 567106266Sjulianng_source_stop (sc_p sc) 568106266Sjulian{ 569106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 570106266Sjulian untimeout(ng_source_intr, sc, sc->intr_ch); 571106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 572106266Sjulian getmicrotime(&sc->stats.endTime); 573106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 574106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 575106266Sjulian /* XXX should set this to the initial value instead */ 576106266Sjulian ng_source_set_autosrc(sc, 1); 577106266Sjulian } 578106266Sjulian} 579106266Sjulian 580106266Sjulian/* 581106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 582106266Sjulian * Sends as many packets as the interface connected to our 583106266Sjulian * output hook is able to enqueue. 584106266Sjulian */ 585106266Sjulianstatic void 586106266Sjulianng_source_intr (void *arg) 587106266Sjulian{ 588106321Sjulian sc_p sc = (sc_p) arg; 589106266Sjulian struct ifqueue *ifq; 590106266Sjulian int packets; 591106266Sjulian 592106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 593106266Sjulian 594106266Sjulian callout_handle_init(&sc->intr_ch); 595106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 596106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 597106266Sjulian ng_source_stop(sc); 598106266Sjulian return; 599106266Sjulian } 600106266Sjulian 601106266Sjulian ifq = &sc->output_ifp->if_snd; 602106266Sjulian packets = ifq->ifq_maxlen - ifq->ifq_len; 603106266Sjulian ng_source_send(sc, packets, NULL); 604106266Sjulian if (sc->packets == 0) { 605106266Sjulian int s = splnet(); 606106266Sjulian ng_source_stop(sc); 607106266Sjulian splx(s); 608106266Sjulian } else 609106319Sjulian sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 610106266Sjulian} 611106266Sjulian 612106266Sjulian/* 613106266Sjulian * Send packets out our output hook 614106266Sjulian */ 615106266Sjulianstatic int 616106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 617106266Sjulian{ 618106266Sjulian struct ifqueue tmp_queue; 619106266Sjulian struct mbuf *m, *m2; 620106266Sjulian int sent = 0; 621106266Sjulian int error = 0; 622106266Sjulian int s, s2; 623106266Sjulian 624106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 625106266Sjulian KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__)); 626106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 627106266Sjulian ("%s: inactive node", __FUNCTION__)); 628106266Sjulian 629106266Sjulian if ((u_int64_t)tosend > sc->packets) 630106266Sjulian tosend = sc->packets; 631106266Sjulian 632106266Sjulian /* Copy the required number of packets to a temporary queue */ 633106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 634106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 635106266Sjulian s = splnet(); 636106266Sjulian IF_DEQUEUE(&sc->snd_queue, m); 637106266Sjulian splx(s); 638106266Sjulian if (m == NULL) 639106266Sjulian break; 640106266Sjulian 641106266Sjulian /* duplicate the packet */ 642111119Simp m2 = m_copypacket(m, M_DONTWAIT); 643106266Sjulian if (m2 == NULL) { 644106266Sjulian s = splnet(); 645106266Sjulian IF_PREPEND(&sc->snd_queue, m); 646106266Sjulian splx(s); 647106266Sjulian error = ENOBUFS; 648106266Sjulian break; 649106266Sjulian } 650106266Sjulian 651106266Sjulian /* re-enqueue the original packet for us */ 652106266Sjulian s = splnet(); 653106266Sjulian IF_ENQUEUE(&sc->snd_queue, m); 654106266Sjulian splx(s); 655106266Sjulian 656106266Sjulian /* queue the copy for sending at smplimp */ 657106266Sjulian IF_ENQUEUE(&tmp_queue, m2); 658106266Sjulian } 659106266Sjulian 660106266Sjulian sent = 0; 661106266Sjulian s = splimp(); 662106266Sjulian for (;;) { 663106266Sjulian IF_DEQUEUE(&tmp_queue, m2); 664106266Sjulian if (m2 == NULL) 665106266Sjulian break; 666106266Sjulian if (error == 0) { 667106266Sjulian ++sent; 668106266Sjulian sc->stats.outFrames++; 669106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 670106266Sjulian s2 = splnet(); 671106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 672106266Sjulian splx(s2); 673106266Sjulian } else { 674106266Sjulian NG_FREE_M(m2); 675106266Sjulian } 676106266Sjulian } 677106266Sjulian splx(s); 678106266Sjulian 679106266Sjulian sc->packets -= sent; 680106266Sjulian if (sent_p != NULL) 681106266Sjulian *sent_p = sent; 682106266Sjulian return (error); 683106266Sjulian} 684