ng_source.c revision 108107
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 108107 2002-12-19 22:58:27Z bmilekic $ 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; 119106435Sjulianstatic void 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 = { 197106266Sjulian NG_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 220106266Sjulian MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT); 221106266Sjulian if (sc == NULL) 222106266Sjulian return (ENOMEM); 223106266Sjulian bzero(sc, sizeof(*sc)); 224106266Sjulian 225106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 226106321Sjulian sc->node = node; 227106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 228106321Sjulian callout_handle_init(&sc->intr_ch); /* XXX fix.. will 229106321Sjulian cause problems. */ 230106266Sjulian return (0); 231106266Sjulian} 232106266Sjulian 233106266Sjulian/* 234106266Sjulian * Add a hook 235106266Sjulian */ 236106266Sjulianstatic int 237106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 238106266Sjulian{ 239106321Sjulian sc_p sc; 240106266Sjulian 241106321Sjulian sc = NG_NODE_PRIVATE(node); 242106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 243106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 244106266Sjulian sc->input.hook = hook; 245106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->input); 246106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 247106266Sjulian sc->output.hook = hook; 248106321Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->output); 249106266Sjulian sc->output_ifp = 0; 250106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 251106266Sjulian } else 252106266Sjulian return (EINVAL); 253106266Sjulian return (0); 254106266Sjulian} 255106266Sjulian 256106266Sjulian/* 257106266Sjulian * Receive a control message 258106266Sjulian */ 259106266Sjulianstatic int 260106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 261106266Sjulian{ 262106321Sjulian sc_p sc; 263106266Sjulian struct ng_mesg *resp = NULL; 264106266Sjulian int error = 0; 265106321Sjulian struct ng_mesg *msg; 266106266Sjulian 267106321Sjulian sc = NG_NODE_PRIVATE(node); 268106321Sjulian NGI_GET_MSG(item, msg); 269106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 270106266Sjulian switch (msg->header.typecookie) { 271106266Sjulian case NGM_SOURCE_COOKIE: 272106435Sjulian if (msg->header.flags & NGF_RESP) { 273106435Sjulian error = EINVAL; 274106435Sjulian break; 275106435Sjulian } 276106266Sjulian switch (msg->header.cmd) { 277106266Sjulian case NGM_SOURCE_GET_STATS: 278106266Sjulian case NGM_SOURCE_CLR_STATS: 279106266Sjulian case NGM_SOURCE_GETCLR_STATS: 280106266Sjulian { 281106266Sjulian struct ng_source_stats *stats; 282106266Sjulian 283106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 284106266Sjulian NG_MKRESPONSE(resp, msg, 285106266Sjulian sizeof(*stats), M_NOWAIT); 286106266Sjulian if (resp == NULL) { 287106266Sjulian error = ENOMEM; 288106266Sjulian goto done; 289106266Sjulian } 290106266Sjulian sc->stats.queueOctets = sc->queueOctets; 291106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 292106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 293106266Sjulian && !timevalisset(&sc->stats.endTime)) { 294106319Sjulian getmicrotime(&sc->stats.elapsedTime); 295106266Sjulian timevalsub(&sc->stats.elapsedTime, 296106319Sjulian &sc->stats.startTime); 297106266Sjulian } 298106319Sjulian stats = (struct ng_source_stats *)resp->data; 299106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 300106266Sjulian } 301106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 302106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 303106266Sjulian } 304106266Sjulian break; 305106266Sjulian case NGM_SOURCE_START: 306106266Sjulian { 307106266Sjulian u_int64_t packets = *(u_int64_t *)msg->data; 308106266Sjulian if (sc->output.hook == NULL) { 309106319Sjulian printf("%s: start on node with no output hook\n" 310106319Sjulian , __FUNCTION__); 311106266Sjulian error = EINVAL; 312106266Sjulian break; 313106266Sjulian } 314106266Sjulian /* TODO validation of packets */ 315106266Sjulian sc->packets = packets; 316106266Sjulian ng_source_start(sc); 317106266Sjulian } 318106266Sjulian break; 319106266Sjulian case NGM_SOURCE_STOP: 320106266Sjulian ng_source_stop(sc); 321106266Sjulian break; 322106266Sjulian case NGM_SOURCE_CLR_DATA: 323106266Sjulian ng_source_clr_data(sc); 324106266Sjulian break; 325106266Sjulian default: 326106266Sjulian error = EINVAL; 327106266Sjulian break; 328106266Sjulian } 329106266Sjulian break; 330106435Sjulian case NGM_ETHER_COOKIE: 331106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 332106435Sjulian error = EINVAL; 333106435Sjulian break; 334106435Sjulian } 335106435Sjulian switch (msg->header.cmd) { 336106435Sjulian case NGM_ETHER_GET_IFINDEX: 337106435Sjulian if (ng_source_store_output_ifp(sc, msg) == 0) { 338106435Sjulian ng_source_set_autosrc(sc, 0); 339106435Sjulian sc->node->nd_flags |= NG_SOURCE_ACTIVE; 340106435Sjulian timevalclear(&sc->stats.elapsedTime); 341106435Sjulian timevalclear(&sc->stats.endTime); 342106435Sjulian getmicrotime(&sc->stats.startTime); 343106435Sjulian sc->intr_ch = timeout(ng_source_intr, sc, 0); 344106435Sjulian } 345106435Sjulian break; 346106435Sjulian default: 347106435Sjulian error = EINVAL; 348106435Sjulian } 349106435Sjulian break; 350106266Sjulian default: 351106266Sjulian error = EINVAL; 352106266Sjulian break; 353106266Sjulian } 354106266Sjulian 355106266Sjuliandone: 356106321Sjulian /* Take care of synchronous response, if any */ 357106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 358106321Sjulian /* Free the message and return */ 359106321Sjulian NG_FREE_MSG(msg); 360106266Sjulian return (error); 361106266Sjulian} 362106266Sjulian 363106266Sjulian/* 364106266Sjulian * Receive data on a hook 365106266Sjulian * 366106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 367106266Sjulian * If data comes in the output hook, discard it. 368106266Sjulian */ 369106266Sjulianstatic int 370106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 371106266Sjulian{ 372106321Sjulian sc_p sc; 373106321Sjulian struct source_hookinfo *hinfo; 374106266Sjulian int error = 0; 375106321Sjulian struct mbuf *m; 376106266Sjulian 377106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 378106321Sjulian NGI_GET_M(item, m); 379106321Sjulian NG_FREE_ITEM(item); 380106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 381106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 382106266Sjulian KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__)); 383106266Sjulian 384106266Sjulian /* Which hook? */ 385106266Sjulian if (hinfo == &sc->output) { 386106266Sjulian /* discard */ 387106321Sjulian NG_FREE_M(m); 388106266Sjulian return (error); 389106266Sjulian } 390106266Sjulian KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__)); 391106266Sjulian 392106266Sjulian if ((m->m_flags & M_PKTHDR) == 0) { 393106266Sjulian printf("%s: mbuf without PKTHDR\n", __FUNCTION__); 394106321Sjulian NG_FREE_M(m); 395106266Sjulian return (EINVAL); 396106266Sjulian } 397106266Sjulian 398106266Sjulian /* enque packet */ 399106266Sjulian /* XXX should we check IF_QFULL() ? */ 400106266Sjulian IF_ENQUEUE(&sc->snd_queue, m); 401106266Sjulian sc->queueOctets += m->m_pkthdr.len; 402106266Sjulian 403106266Sjulian return (0); 404106266Sjulian} 405106266Sjulian 406106266Sjulian/* 407106266Sjulian * Shutdown processing 408106266Sjulian */ 409106266Sjulianstatic int 410106266Sjulianng_source_rmnode(node_p node) 411106266Sjulian{ 412106321Sjulian sc_p sc; 413106266Sjulian 414106321Sjulian sc = NG_NODE_PRIVATE(node); 415106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 416106321Sjulian node->nd_flags |= NG_INVALID; 417106266Sjulian ng_source_stop(sc); 418106266Sjulian ng_source_clr_data(sc); 419106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 420106321Sjulian NG_NODE_UNREF(node); 421106266Sjulian FREE(sc, M_NETGRAPH); 422106266Sjulian return (0); 423106266Sjulian} 424106266Sjulian 425106266Sjulian/* 426106266Sjulian * Hook disconnection 427106266Sjulian */ 428106266Sjulianstatic int 429106266Sjulianng_source_disconnect(hook_p hook) 430106266Sjulian{ 431106321Sjulian struct source_hookinfo *hinfo; 432106319Sjulian sc_p sc; 433106266Sjulian 434106321Sjulian hinfo = NG_HOOK_PRIVATE(hook); 435106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 436106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 437106266Sjulian hinfo->hook = NULL; 438106321Sjulian if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output) 439106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 440106266Sjulian return (0); 441106266Sjulian} 442106266Sjulian 443106266Sjulian/* 444106435Sjulian * 445106435Sjulian * Ask out neighbour on the output hook side to send us it's interface 446106435Sjulian * information. 447106266Sjulian */ 448106435Sjulianstatic void 449106435Sjulianng_source_request_output_ifp(sc_p sc) 450106266Sjulian{ 451106435Sjulian struct ng_mesg *msg; 452106266Sjulian int error = 0; 453106266Sjulian 454106266Sjulian sc->output_ifp = NULL; 455106266Sjulian 456106266Sjulian /* Ask the attached node for the connected interface's index */ 457106319Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT); 458106266Sjulian if (msg == NULL) 459106266Sjulian return (ENOBUFS); 460106266Sjulian 461106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 462106435Sjulian return (error); 463106435Sjulian} 464106266Sjulian 465106435Sjulian/* 466106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 467106435Sjulian * reached via our output hook. 468106435Sjulian */ 469106435Sjulianstatic int 470106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg) 471106435Sjulian{ 472106435Sjulian struct ifnet *ifp; 473106435Sjulian u_int32_t if_index; 474106435Sjulian int s; 475106266Sjulian 476106435Sjulian if (msg->header.arglen < sizeof(u_int32_t)) 477106266Sjulian return (EINVAL); 478106266Sjulian 479106435Sjulian if_index = *(u_int32_t *)msg->data; 480106266Sjulian /* Could use ifindex2ifnet[if_index] except that we have no 481106266Sjulian * way of verifying if_index is valid since if_indexlim is 482106266Sjulian * local to if_attach() 483106266Sjulian */ 484106266Sjulian TAILQ_FOREACH(ifp, &ifnet, if_link) { 485106266Sjulian if (ifp->if_index == if_index) 486106266Sjulian break; 487106266Sjulian } 488106266Sjulian 489106266Sjulian if (ifp == NULL) { 490106319Sjulian printf("%s: can't find interface %d\n", __FUNCTION__, if_index); 491106266Sjulian return (EINVAL); 492106266Sjulian } 493106266Sjulian sc->output_ifp = ifp; 494106266Sjulian 495106266Sjulian#if 1 496106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 497106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 498106266Sjulian * interface with small packets. 499106266Sjulian * XXX we should restore the original value at stop or disconnect 500106266Sjulian */ 501106266Sjulian s = splimp(); /* XXX is this required? */ 502106266Sjulian if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) 503106266Sjulian { 504106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 505106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 506106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 507106266Sjulian } 508106266Sjulian splx(s); 509106266Sjulian#endif 510106266Sjulian return (0); 511106266Sjulian} 512106266Sjulian 513106266Sjulian/* 514106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 515106266Sjulian */ 516106266Sjulianstatic int 517106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag) 518106266Sjulian{ 519106266Sjulian struct ng_mesg *msg; 520106266Sjulian int error = 0; 521106266Sjulian 522106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 523106266Sjulian sizeof (u_int32_t), M_NOWAIT); 524106266Sjulian if (msg == NULL) 525106266Sjulian return(ENOBUFS); 526106266Sjulian 527106266Sjulian *(u_int32_t *)msg->data = flag; 528106321Sjulian NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL); 529106266Sjulian return (error); 530106266Sjulian} 531106266Sjulian 532106266Sjulian/* 533106266Sjulian * Clear out the data we've queued 534106266Sjulian */ 535106266Sjulianstatic void 536106266Sjulianng_source_clr_data (sc_p sc) 537106266Sjulian{ 538106266Sjulian struct mbuf *m; 539106266Sjulian 540106266Sjulian for (;;) { 541106266Sjulian IF_DEQUEUE(&sc->snd_queue, m); 542106266Sjulian if (m == NULL) 543106266Sjulian break; 544106266Sjulian NG_FREE_M(m); 545106266Sjulian } 546106266Sjulian sc->queueOctets = 0; 547106266Sjulian} 548106266Sjulian 549106266Sjulian/* 550106266Sjulian * Start sending queued data out the output hook 551106266Sjulian */ 552106266Sjulianstatic void 553106266Sjulianng_source_start (sc_p sc) 554106266Sjulian{ 555106266Sjulian KASSERT(sc->output.hook != NULL, 556106266Sjulian ("%s: output hook unconnected", __FUNCTION__)); 557106435Sjulian if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) && 558106435Sjulian (sc->output_ifp == NULL)) 559106435Sjulian ng_source_request_output_ifp(sc); 560106266Sjulian} 561106266Sjulian 562106266Sjulian/* 563106266Sjulian * Stop sending queued data out the output hook 564106266Sjulian */ 565106266Sjulianstatic void 566106266Sjulianng_source_stop (sc_p sc) 567106266Sjulian{ 568106321Sjulian if (sc->node->nd_flags & NG_SOURCE_ACTIVE) { 569106266Sjulian untimeout(ng_source_intr, sc, sc->intr_ch); 570106321Sjulian sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 571106266Sjulian getmicrotime(&sc->stats.endTime); 572106266Sjulian sc->stats.elapsedTime = sc->stats.endTime; 573106266Sjulian timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 574106266Sjulian /* XXX should set this to the initial value instead */ 575106266Sjulian ng_source_set_autosrc(sc, 1); 576106266Sjulian } 577106266Sjulian} 578106266Sjulian 579106266Sjulian/* 580106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 581106266Sjulian * Sends as many packets as the interface connected to our 582106266Sjulian * output hook is able to enqueue. 583106266Sjulian */ 584106266Sjulianstatic void 585106266Sjulianng_source_intr (void *arg) 586106266Sjulian{ 587106321Sjulian sc_p sc = (sc_p) arg; 588106266Sjulian struct ifqueue *ifq; 589106266Sjulian int packets; 590106266Sjulian 591106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 592106266Sjulian 593106266Sjulian callout_handle_init(&sc->intr_ch); 594106266Sjulian if (sc->packets == 0 || sc->output.hook == NULL 595106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 596106266Sjulian ng_source_stop(sc); 597106266Sjulian return; 598106266Sjulian } 599106266Sjulian 600106266Sjulian ifq = &sc->output_ifp->if_snd; 601106266Sjulian packets = ifq->ifq_maxlen - ifq->ifq_len; 602106266Sjulian ng_source_send(sc, packets, NULL); 603106266Sjulian if (sc->packets == 0) { 604106266Sjulian int s = splnet(); 605106266Sjulian ng_source_stop(sc); 606106266Sjulian splx(s); 607106266Sjulian } else 608106319Sjulian sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS); 609106266Sjulian} 610106266Sjulian 611106266Sjulian/* 612106266Sjulian * Send packets out our output hook 613106266Sjulian */ 614106266Sjulianstatic int 615106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p) 616106266Sjulian{ 617106266Sjulian struct ifqueue tmp_queue; 618106266Sjulian struct mbuf *m, *m2; 619106266Sjulian int sent = 0; 620106266Sjulian int error = 0; 621106266Sjulian int s, s2; 622106266Sjulian 623106266Sjulian KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__)); 624106266Sjulian KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__)); 625106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 626106266Sjulian ("%s: inactive node", __FUNCTION__)); 627106266Sjulian 628106266Sjulian if ((u_int64_t)tosend > sc->packets) 629106266Sjulian tosend = sc->packets; 630106266Sjulian 631106266Sjulian /* Copy the required number of packets to a temporary queue */ 632106266Sjulian bzero (&tmp_queue, sizeof (tmp_queue)); 633106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 634106266Sjulian s = splnet(); 635106266Sjulian IF_DEQUEUE(&sc->snd_queue, m); 636106266Sjulian splx(s); 637106266Sjulian if (m == NULL) 638106266Sjulian break; 639106266Sjulian 640106266Sjulian /* duplicate the packet */ 641108107Sbmilekic m2 = m_copypacket(m, M_DONTWAIT); 642106266Sjulian if (m2 == NULL) { 643106266Sjulian s = splnet(); 644106266Sjulian IF_PREPEND(&sc->snd_queue, m); 645106266Sjulian splx(s); 646106266Sjulian error = ENOBUFS; 647106266Sjulian break; 648106266Sjulian } 649106266Sjulian 650106266Sjulian /* re-enqueue the original packet for us */ 651106266Sjulian s = splnet(); 652106266Sjulian IF_ENQUEUE(&sc->snd_queue, m); 653106266Sjulian splx(s); 654106266Sjulian 655106266Sjulian /* queue the copy for sending at smplimp */ 656106266Sjulian IF_ENQUEUE(&tmp_queue, m2); 657106266Sjulian } 658106266Sjulian 659106266Sjulian sent = 0; 660106266Sjulian s = splimp(); 661106266Sjulian for (;;) { 662106266Sjulian IF_DEQUEUE(&tmp_queue, m2); 663106266Sjulian if (m2 == NULL) 664106266Sjulian break; 665106266Sjulian if (error == 0) { 666106266Sjulian ++sent; 667106266Sjulian sc->stats.outFrames++; 668106266Sjulian sc->stats.outOctets += m2->m_pkthdr.len; 669106266Sjulian s2 = splnet(); 670106266Sjulian NG_SEND_DATA_ONLY(error, sc->output.hook, m2); 671106266Sjulian splx(s2); 672106266Sjulian } else { 673106266Sjulian NG_FREE_M(m2); 674106266Sjulian } 675106266Sjulian } 676106266Sjulian splx(s); 677106266Sjulian 678106266Sjulian sc->packets -= sent; 679106266Sjulian if (sent_p != NULL) 680106266Sjulian *sent_p = sent; 681106266Sjulian return (error); 682106266Sjulian} 683