1106266Sjulian/* 2106266Sjulian * ng_source.c 3139823Simp */ 4139823Simp 5139823Simp/*- 6144674Sglebius * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org> 7106266Sjulian * Copyright 2002 Sandvine Inc. 8106266Sjulian * All rights reserved. 9106266Sjulian * 10106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and 11106266Sjulian * redistribution of this software, in source or object code forms, with or 12106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided, 13106266Sjulian * however, that: 14106319Sjulian * 1. Any and all reproductions of the source or object code must include the 15106319Sjulian * copyright notice above and the following disclaimer of warranties; and 16106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc. 17106319Sjulian * trademarks, including the mark "SANDVINE" on advertising, endorsements, 18106319Sjulian * or otherwise except as such appears in the above copyright notice or in 19106266Sjulian * the software. 20106266Sjulian * 21106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM 22106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES, 23106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, 24106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 25106266Sjulian * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR 26106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE 27106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY 28106266Sjulian * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES 29106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH 36106266Sjulian * DAMAGE. 37106266Sjulian * 38209728Semaste * Author: Dave Chapeskie 39106266Sjulian */ 40106266Sjulian 41125077Sharti#include <sys/cdefs.h> 42125077Sharti__FBSDID("$FreeBSD: stable/11/sys/netgraph/ng_source.c 369769 2021-05-09 13:47:05Z git2svn $"); 43125077Sharti 44106266Sjulian/* 45106266Sjulian * This node is used for high speed packet geneneration. It queues 46298813Spfg * all data received on its 'input' hook and when told to start via 47143387Sbmilekic * a control message it sends the packets out its 'output' hook. In 48143387Sbmilekic * this way this node can be preloaded with a packet stream which it 49143387Sbmilekic * can then send continuously as fast as possible. 50106266Sjulian * 51106266Sjulian * Currently it just copies the mbufs as required. It could do various 52106266Sjulian * tricks to try and avoid this. Probably the best performance would 53106266Sjulian * be achieved by modifying the appropriate drivers to be told to 54106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same 55106266Sjulian * transmit descriptors) under control of this node; perhaps via some 56143387Sbmilekic * flag in the mbuf or some such. The node could peek at an appropriate 57106266Sjulian * ifnet flag to see if such support is available for the connected 58106266Sjulian * interface. 59106266Sjulian */ 60106266Sjulian 61106266Sjulian#include <sys/param.h> 62106266Sjulian#include <sys/systm.h> 63106266Sjulian#include <sys/errno.h> 64106266Sjulian#include <sys/kernel.h> 65106266Sjulian#include <sys/malloc.h> 66106266Sjulian#include <sys/mbuf.h> 67106266Sjulian#include <sys/socket.h> 68144674Sglebius#include <sys/syslog.h> 69106266Sjulian#include <net/if.h> 70106266Sjulian#include <net/if_var.h> 71106266Sjulian#include <netgraph/ng_message.h> 72106266Sjulian#include <netgraph/netgraph.h> 73106266Sjulian#include <netgraph/ng_parse.h> 74106266Sjulian#include <netgraph/ng_ether.h> 75106266Sjulian#include <netgraph/ng_source.h> 76106266Sjulian 77106266Sjulian#define NG_SOURCE_INTR_TICKS 1 78106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 79106266Sjulian 80167156Semaste#define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off))) 81167156Semaste 82106266Sjulian/* Per node info */ 83106266Sjulianstruct privdata { 84106266Sjulian node_p node; 85144674Sglebius hook_p input; 86144674Sglebius hook_p output; 87106266Sjulian struct ng_source_stats stats; 88369769Sgit2svn struct mbufq snd_queue; /* packets to send */ 89167160Semaste struct mbuf *last_packet; /* last pkt in queue */ 90106266Sjulian struct ifnet *output_ifp; 91137138Sglebius struct callout intr_ch; 92144674Sglebius uint64_t packets; /* packets to send */ 93144674Sglebius uint32_t queueOctets; 94167156Semaste struct ng_source_embed_info embed_timestamp; 95167160Semaste struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS]; 96106266Sjulian}; 97106266Sjuliantypedef struct privdata *sc_p; 98106266Sjulian 99106266Sjulian/* Node flags */ 100106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 101106266Sjulian 102106266Sjulian/* Netgraph methods */ 103106266Sjulianstatic ng_constructor_t ng_source_constructor; 104106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 105106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 106106266Sjulianstatic ng_newhook_t ng_source_newhook; 107144674Sglebiusstatic ng_connect_t ng_source_connect; 108106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 109106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 110106266Sjulian 111106266Sjulian/* Other functions */ 112125243Shartistatic void ng_source_intr(node_p, hook_p, void *, int); 113106266Sjulianstatic void ng_source_clr_data (sc_p); 114144674Sglebiusstatic int ng_source_start (sc_p, uint64_t); 115106266Sjulianstatic void ng_source_stop (sc_p); 116106266Sjulianstatic int ng_source_send (sc_p, int, int *); 117144674Sglebiusstatic int ng_source_store_output_ifp(sc_p, char *); 118167156Semastestatic void ng_source_packet_mod(sc_p, struct mbuf *, 119167156Semaste int, int, caddr_t, int); 120167160Semastestatic void ng_source_mod_counter(sc_p sc, 121167160Semaste struct ng_source_embed_cnt_info *cnt, 122167160Semaste struct mbuf *m, int increment); 123167156Semastestatic int ng_source_dup_mod(sc_p, struct mbuf *, 124167156Semaste struct mbuf **); 125106266Sjulian 126106266Sjulian/* Parse type for timeval */ 127125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = { 128343022Seugen#ifdef __i386__ 129106266Sjulian { "tv_sec", &ng_parse_int32_type }, 130343022Seugen#else 131343022Seugen { "tv_sec", &ng_parse_int64_type }, 132343022Seugen#endif 133343022Seugen#ifdef __LP64__ 134343022Seugen { "tv_usec", &ng_parse_int64_type }, 135343022Seugen#else 136106266Sjulian { "tv_usec", &ng_parse_int32_type }, 137343022Seugen#endif 138106266Sjulian { NULL } 139106266Sjulian}; 140106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 141106266Sjulian &ng_parse_struct_type, 142106266Sjulian &ng_source_timeval_type_fields 143106266Sjulian}; 144106266Sjulian 145106266Sjulian/* Parse type for struct ng_source_stats */ 146106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 147106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 148106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 149106266Sjulian &ng_parse_struct_type, 150106266Sjulian &ng_source_stats_type_fields 151106266Sjulian}; 152106266Sjulian 153167156Semaste/* Parse type for struct ng_source_embed_info */ 154167156Semastestatic const struct ng_parse_struct_field ng_source_embed_type_fields[] = 155167156Semaste NG_SOURCE_EMBED_TYPE_INFO; 156167156Semastestatic const struct ng_parse_type ng_source_embed_type = { 157167156Semaste &ng_parse_struct_type, 158167156Semaste &ng_source_embed_type_fields 159167156Semaste}; 160167156Semaste 161167160Semaste/* Parse type for struct ng_source_embed_cnt_info */ 162167160Semastestatic const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] = 163167160Semaste NG_SOURCE_EMBED_CNT_TYPE_INFO; 164167160Semastestatic const struct ng_parse_type ng_source_embed_cnt_type = { 165167160Semaste &ng_parse_struct_type, 166167160Semaste &ng_source_embed_cnt_type_fields 167167160Semaste}; 168167160Semaste 169106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 170106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 171106266Sjulian { 172106266Sjulian NGM_SOURCE_COOKIE, 173106266Sjulian NGM_SOURCE_GET_STATS, 174106266Sjulian "getstats", 175106266Sjulian NULL, 176106266Sjulian &ng_source_stats_type 177106266Sjulian }, 178106266Sjulian { 179106266Sjulian NGM_SOURCE_COOKIE, 180106266Sjulian NGM_SOURCE_CLR_STATS, 181106266Sjulian "clrstats", 182106266Sjulian NULL, 183106266Sjulian NULL 184106266Sjulian }, 185106266Sjulian { 186106266Sjulian NGM_SOURCE_COOKIE, 187106266Sjulian NGM_SOURCE_GETCLR_STATS, 188106266Sjulian "getclrstats", 189106266Sjulian NULL, 190106266Sjulian &ng_source_stats_type 191106266Sjulian }, 192106266Sjulian { 193106266Sjulian NGM_SOURCE_COOKIE, 194106266Sjulian NGM_SOURCE_START, 195106266Sjulian "start", 196106266Sjulian &ng_parse_uint64_type, 197106266Sjulian NULL 198106266Sjulian }, 199106266Sjulian { 200106266Sjulian NGM_SOURCE_COOKIE, 201106266Sjulian NGM_SOURCE_STOP, 202106266Sjulian "stop", 203106266Sjulian NULL, 204106266Sjulian NULL 205106266Sjulian }, 206106266Sjulian { 207106266Sjulian NGM_SOURCE_COOKIE, 208106266Sjulian NGM_SOURCE_CLR_DATA, 209106266Sjulian "clrdata", 210106266Sjulian NULL, 211106266Sjulian NULL 212106266Sjulian }, 213125033Sharti { 214125033Sharti NGM_SOURCE_COOKIE, 215144674Sglebius NGM_SOURCE_SETIFACE, 216144674Sglebius "setiface", 217144674Sglebius &ng_parse_string_type, 218125033Sharti NULL 219125033Sharti }, 220153690Sglebius { 221153690Sglebius NGM_SOURCE_COOKIE, 222153690Sglebius NGM_SOURCE_SETPPS, 223153690Sglebius "setpps", 224153690Sglebius &ng_parse_uint32_type, 225153690Sglebius NULL 226153690Sglebius }, 227167156Semaste { 228167156Semaste NGM_SOURCE_COOKIE, 229167156Semaste NGM_SOURCE_SET_TIMESTAMP, 230167156Semaste "settimestamp", 231167156Semaste &ng_source_embed_type, 232167156Semaste NULL 233167156Semaste }, 234167156Semaste { 235167156Semaste NGM_SOURCE_COOKIE, 236167156Semaste NGM_SOURCE_GET_TIMESTAMP, 237167156Semaste "gettimestamp", 238167156Semaste NULL, 239167156Semaste &ng_source_embed_type 240167156Semaste }, 241167160Semaste { 242167160Semaste NGM_SOURCE_COOKIE, 243167160Semaste NGM_SOURCE_SET_COUNTER, 244167160Semaste "setcounter", 245167160Semaste &ng_source_embed_cnt_type, 246167160Semaste NULL 247167160Semaste }, 248167160Semaste { 249167160Semaste NGM_SOURCE_COOKIE, 250167160Semaste NGM_SOURCE_GET_COUNTER, 251167160Semaste "getcounter", 252167160Semaste &ng_parse_uint8_type, 253167160Semaste &ng_source_embed_cnt_type 254167160Semaste }, 255106266Sjulian { 0 } 256106266Sjulian}; 257106266Sjulian 258106266Sjulian/* Netgraph type descriptor */ 259106266Sjulianstatic struct ng_type ng_source_typestruct = { 260129823Sjulian .version = NG_ABI_VERSION, 261129823Sjulian .name = NG_SOURCE_NODE_TYPE, 262129823Sjulian .constructor = ng_source_constructor, 263129823Sjulian .rcvmsg = ng_source_rcvmsg, 264129823Sjulian .shutdown = ng_source_rmnode, 265129823Sjulian .newhook = ng_source_newhook, 266144674Sglebius .connect = ng_source_connect, 267129823Sjulian .rcvdata = ng_source_rcvdata, 268129823Sjulian .disconnect = ng_source_disconnect, 269129823Sjulian .cmdlist = ng_source_cmds, 270106266Sjulian}; 271106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 272106266Sjulian 273144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t); 274125032Sharti 275106266Sjulian/* 276106266Sjulian * Node constructor 277106266Sjulian */ 278106266Sjulianstatic int 279106321Sjulianng_source_constructor(node_p node) 280106266Sjulian{ 281106266Sjulian sc_p sc; 282106266Sjulian 283220768Sglebius sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 284106266Sjulian 285106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 286106321Sjulian sc->node = node; 287369769Sgit2svn mbufq_init(&sc->snd_queue, 2048); 288137138Sglebius ng_callout_init(&sc->intr_ch); 289137138Sglebius 290106266Sjulian return (0); 291106266Sjulian} 292106266Sjulian 293106266Sjulian/* 294106266Sjulian * Add a hook 295106266Sjulian */ 296106266Sjulianstatic int 297106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 298106266Sjulian{ 299144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 300106266Sjulian 301106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 302144674Sglebius sc->input = hook; 303106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 304144674Sglebius sc->output = hook; 305298075Spfg sc->output_ifp = NULL; 306106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 307106266Sjulian } else 308106266Sjulian return (EINVAL); 309144674Sglebius 310106266Sjulian return (0); 311106266Sjulian} 312106266Sjulian 313106266Sjulian/* 314144674Sglebius * Hook has been added 315144674Sglebius */ 316144674Sglebiusstatic int 317144674Sglebiusng_source_connect(hook_p hook) 318144674Sglebius{ 319144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 320144674Sglebius struct ng_mesg *msg; 321144674Sglebius int dummy_error = 0; 322144674Sglebius 323144674Sglebius /* 324144674Sglebius * If this is "output" hook, then request information 325144674Sglebius * from our downstream. 326144674Sglebius */ 327144674Sglebius if (hook == sc->output) { 328144674Sglebius NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME, 329144674Sglebius 0, M_NOWAIT); 330144674Sglebius if (msg == NULL) 331144674Sglebius return (ENOBUFS); 332144674Sglebius 333144674Sglebius /* 334144674Sglebius * Our hook and peer hook have HK_INVALID flag set, 335144674Sglebius * so we can't use NG_SEND_MSG_HOOK() macro here. 336144674Sglebius */ 337144674Sglebius NG_SEND_MSG_ID(dummy_error, sc->node, msg, 338144674Sglebius NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node)); 339144674Sglebius } 340144674Sglebius 341144674Sglebius return (0); 342144674Sglebius} 343144674Sglebius 344144674Sglebius/* 345106266Sjulian * Receive a control message 346106266Sjulian */ 347106266Sjulianstatic int 348106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 349106266Sjulian{ 350144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 351144674Sglebius struct ng_mesg *msg, *resp = NULL; 352106266Sjulian int error = 0; 353106266Sjulian 354106321Sjulian NGI_GET_MSG(item, msg); 355144674Sglebius 356106266Sjulian switch (msg->header.typecookie) { 357106266Sjulian case NGM_SOURCE_COOKIE: 358106435Sjulian if (msg->header.flags & NGF_RESP) { 359106435Sjulian error = EINVAL; 360106435Sjulian break; 361106435Sjulian } 362106266Sjulian switch (msg->header.cmd) { 363106266Sjulian case NGM_SOURCE_GET_STATS: 364106266Sjulian case NGM_SOURCE_CLR_STATS: 365106266Sjulian case NGM_SOURCE_GETCLR_STATS: 366106266Sjulian { 367106266Sjulian struct ng_source_stats *stats; 368106266Sjulian 369106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 370106266Sjulian NG_MKRESPONSE(resp, msg, 371106266Sjulian sizeof(*stats), M_NOWAIT); 372106266Sjulian if (resp == NULL) { 373106266Sjulian error = ENOMEM; 374106266Sjulian goto done; 375106266Sjulian } 376106266Sjulian sc->stats.queueOctets = sc->queueOctets; 377369769Sgit2svn sc->stats.queueFrames = mbufq_len(&sc->snd_queue); 378106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 379106266Sjulian && !timevalisset(&sc->stats.endTime)) { 380106319Sjulian getmicrotime(&sc->stats.elapsedTime); 381106266Sjulian timevalsub(&sc->stats.elapsedTime, 382106319Sjulian &sc->stats.startTime); 383106266Sjulian } 384106319Sjulian stats = (struct ng_source_stats *)resp->data; 385106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 386106266Sjulian } 387106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 388106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 389106266Sjulian } 390106266Sjulian break; 391106266Sjulian case NGM_SOURCE_START: 392106266Sjulian { 393144674Sglebius uint64_t packets; 394144674Sglebius 395144674Sglebius if (msg->header.arglen != sizeof(uint64_t)) { 396106266Sjulian error = EINVAL; 397106266Sjulian break; 398106266Sjulian } 399125033Sharti 400144674Sglebius packets = *(uint64_t *)msg->data; 401144674Sglebius 402144674Sglebius error = ng_source_start(sc, packets); 403144674Sglebius 404144674Sglebius break; 405125033Sharti } 406106266Sjulian case NGM_SOURCE_STOP: 407106266Sjulian ng_source_stop(sc); 408106266Sjulian break; 409106266Sjulian case NGM_SOURCE_CLR_DATA: 410106266Sjulian ng_source_clr_data(sc); 411106266Sjulian break; 412144674Sglebius case NGM_SOURCE_SETIFACE: 413144674Sglebius { 414144674Sglebius char *ifname = (char *)msg->data; 415144674Sglebius 416144674Sglebius if (msg->header.arglen < 2) { 417144674Sglebius error = EINVAL; 418144674Sglebius break; 419144674Sglebius } 420144674Sglebius 421144674Sglebius ng_source_store_output_ifp(sc, ifname); 422144674Sglebius break; 423144674Sglebius } 424153690Sglebius case NGM_SOURCE_SETPPS: 425153690Sglebius { 426153690Sglebius uint32_t pps; 427153690Sglebius 428153690Sglebius if (msg->header.arglen != sizeof(uint32_t)) { 429153690Sglebius error = EINVAL; 430153690Sglebius break; 431153690Sglebius } 432153690Sglebius 433153690Sglebius pps = *(uint32_t *)msg->data; 434153690Sglebius 435153690Sglebius sc->stats.maxPps = pps; 436153690Sglebius 437153690Sglebius break; 438153690Sglebius } 439167156Semaste case NGM_SOURCE_SET_TIMESTAMP: 440167156Semaste { 441167156Semaste struct ng_source_embed_info *embed; 442167156Semaste 443167168Semaste if (msg->header.arglen != sizeof(*embed)) { 444167168Semaste error = EINVAL; 445167168Semaste goto done; 446167168Semaste } 447167156Semaste embed = (struct ng_source_embed_info *)msg->data; 448167156Semaste bcopy(embed, &sc->embed_timestamp, sizeof(*embed)); 449167156Semaste 450167156Semaste break; 451167156Semaste } 452167156Semaste case NGM_SOURCE_GET_TIMESTAMP: 453167156Semaste { 454167156Semaste struct ng_source_embed_info *embed; 455167156Semaste 456243882Sglebius NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT); 457167156Semaste if (resp == NULL) { 458167156Semaste error = ENOMEM; 459167156Semaste goto done; 460167156Semaste } 461167156Semaste embed = (struct ng_source_embed_info *)resp->data; 462167156Semaste bcopy(&sc->embed_timestamp, embed, sizeof(*embed)); 463167156Semaste 464167156Semaste break; 465167156Semaste } 466167160Semaste case NGM_SOURCE_SET_COUNTER: 467167160Semaste { 468167160Semaste struct ng_source_embed_cnt_info *embed; 469167160Semaste 470167168Semaste if (msg->header.arglen != sizeof(*embed)) { 471167168Semaste error = EINVAL; 472167168Semaste goto done; 473167168Semaste } 474167160Semaste embed = (struct ng_source_embed_cnt_info *)msg->data; 475167160Semaste if (embed->index >= NG_SOURCE_COUNTERS || 476167160Semaste !(embed->width == 1 || embed->width == 2 || 477167160Semaste embed->width == 4)) { 478167160Semaste error = EINVAL; 479167160Semaste goto done; 480167160Semaste } 481167160Semaste bcopy(embed, &sc->embed_counter[embed->index], 482167160Semaste sizeof(*embed)); 483167160Semaste 484167160Semaste break; 485167160Semaste } 486167160Semaste case NGM_SOURCE_GET_COUNTER: 487167160Semaste { 488167160Semaste uint8_t index = *(uint8_t *)msg->data; 489167160Semaste struct ng_source_embed_cnt_info *embed; 490167160Semaste 491167160Semaste if (index >= NG_SOURCE_COUNTERS) { 492167160Semaste error = EINVAL; 493167160Semaste goto done; 494167160Semaste } 495243882Sglebius NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT); 496167160Semaste if (resp == NULL) { 497167160Semaste error = ENOMEM; 498167160Semaste goto done; 499167160Semaste } 500167160Semaste embed = (struct ng_source_embed_cnt_info *)resp->data; 501167160Semaste bcopy(&sc->embed_counter[index], embed, sizeof(*embed)); 502167160Semaste 503167160Semaste break; 504167160Semaste } 505106266Sjulian default: 506106266Sjulian error = EINVAL; 507106266Sjulian break; 508106266Sjulian } 509106266Sjulian break; 510106435Sjulian case NGM_ETHER_COOKIE: 511106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 512106435Sjulian error = EINVAL; 513106435Sjulian break; 514106435Sjulian } 515106435Sjulian switch (msg->header.cmd) { 516144674Sglebius case NGM_ETHER_GET_IFNAME: 517144674Sglebius { 518144674Sglebius char *ifname = (char *)msg->data; 519144674Sglebius 520144674Sglebius if (msg->header.arglen < 2) { 521144674Sglebius error = EINVAL; 522144674Sglebius break; 523144674Sglebius } 524144674Sglebius 525144674Sglebius if (ng_source_store_output_ifp(sc, ifname) == 0) 526106435Sjulian ng_source_set_autosrc(sc, 0); 527106435Sjulian break; 528144674Sglebius } 529106435Sjulian default: 530106435Sjulian error = EINVAL; 531106435Sjulian } 532106435Sjulian break; 533106266Sjulian default: 534106266Sjulian error = EINVAL; 535106266Sjulian break; 536106266Sjulian } 537106266Sjulian 538106266Sjuliandone: 539144674Sglebius /* Take care of synchronous response, if any. */ 540106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 541144674Sglebius /* Free the message and return. */ 542106321Sjulian NG_FREE_MSG(msg); 543106266Sjulian return (error); 544106266Sjulian} 545106266Sjulian 546106266Sjulian/* 547106266Sjulian * Receive data on a hook 548106266Sjulian * 549106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 550106266Sjulian * If data comes in the output hook, discard it. 551106266Sjulian */ 552106266Sjulianstatic int 553106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 554106266Sjulian{ 555144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 556144674Sglebius struct mbuf *m; 557106266Sjulian int error = 0; 558106266Sjulian 559106321Sjulian NGI_GET_M(item, m); 560106321Sjulian NG_FREE_ITEM(item); 561106266Sjulian 562106266Sjulian /* Which hook? */ 563144674Sglebius if (hook == sc->output) { 564106266Sjulian /* discard */ 565106321Sjulian NG_FREE_M(m); 566106266Sjulian return (error); 567106266Sjulian } 568144674Sglebius KASSERT(hook == sc->input, ("%s: no hook!", __func__)); 569106266Sjulian 570369768Sdonner /* Enqueue packet if the queue isn't full. */ 571369769Sgit2svn error = mbufq_enqueue(&sc->snd_queue, m); 572369769Sgit2svn if (error) { 573369768Sdonner NG_FREE_M(m); 574369769Sgit2svn return (error); 575369768Sdonner } 576106266Sjulian sc->queueOctets += m->m_pkthdr.len; 577167160Semaste sc->last_packet = m; 578106266Sjulian 579106266Sjulian return (0); 580106266Sjulian} 581106266Sjulian 582106266Sjulian/* 583106266Sjulian * Shutdown processing 584106266Sjulian */ 585106266Sjulianstatic int 586106266Sjulianng_source_rmnode(node_p node) 587106266Sjulian{ 588144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 589106266Sjulian 590106266Sjulian ng_source_stop(sc); 591106266Sjulian ng_source_clr_data(sc); 592106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 593106321Sjulian NG_NODE_UNREF(node); 594125077Sharti free(sc, M_NETGRAPH); 595144674Sglebius 596106266Sjulian return (0); 597106266Sjulian} 598106266Sjulian 599106266Sjulian/* 600106266Sjulian * Hook disconnection 601106266Sjulian */ 602106266Sjulianstatic int 603106266Sjulianng_source_disconnect(hook_p hook) 604106266Sjulian{ 605106319Sjulian sc_p sc; 606106266Sjulian 607106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 608125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 609144674Sglebius if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output) 610106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 611106266Sjulian return (0); 612106266Sjulian} 613106266Sjulian 614106266Sjulian/* 615218909Sbrucec * Set sc->output_ifp to point to the struct ifnet of the interface 616106435Sjulian * reached via our output hook. 617106435Sjulian */ 618106435Sjulianstatic int 619144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname) 620106435Sjulian{ 621106435Sjulian struct ifnet *ifp; 622106266Sjulian 623144674Sglebius ifp = ifunit(ifname); 624106266Sjulian 625106266Sjulian if (ifp == NULL) { 626183225Szec printf("%s: can't find interface %s\n", __func__, ifname); 627106266Sjulian return (EINVAL); 628106266Sjulian } 629106266Sjulian sc->output_ifp = ifp; 630106266Sjulian 631106266Sjulian#if 1 632106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 633106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 634106266Sjulian * interface with small packets. 635106266Sjulian * XXX we should restore the original value at stop or disconnect 636106266Sjulian */ 637125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 638106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 639106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 640106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 641106266Sjulian } 642106266Sjulian#endif 643106266Sjulian return (0); 644106266Sjulian} 645106266Sjulian 646106266Sjulian/* 647106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 648106266Sjulian */ 649106266Sjulianstatic int 650144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag) 651106266Sjulian{ 652106266Sjulian struct ng_mesg *msg; 653106266Sjulian int error = 0; 654106266Sjulian 655106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 656144674Sglebius sizeof (uint32_t), M_NOWAIT); 657106266Sjulian if (msg == NULL) 658106266Sjulian return(ENOBUFS); 659106266Sjulian 660144674Sglebius *(uint32_t *)msg->data = flag; 661144674Sglebius NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0); 662106266Sjulian return (error); 663106266Sjulian} 664106266Sjulian 665106266Sjulian/* 666106266Sjulian * Clear out the data we've queued 667106266Sjulian */ 668106266Sjulianstatic void 669106266Sjulianng_source_clr_data (sc_p sc) 670106266Sjulian{ 671106266Sjulian struct mbuf *m; 672106266Sjulian 673106266Sjulian for (;;) { 674369769Sgit2svn m = mbufq_dequeue(&sc->snd_queue); 675106266Sjulian if (m == NULL) 676106266Sjulian break; 677106266Sjulian NG_FREE_M(m); 678106266Sjulian } 679106266Sjulian sc->queueOctets = 0; 680167168Semaste sc->last_packet = NULL; 681106266Sjulian} 682106266Sjulian 683106266Sjulian/* 684106266Sjulian * Start sending queued data out the output hook 685106266Sjulian */ 686144674Sglebiusstatic int 687144674Sglebiusng_source_start(sc_p sc, uint64_t packets) 688106266Sjulian{ 689144674Sglebius if (sc->output_ifp == NULL) { 690144674Sglebius printf("ng_source: start without iface configured\n"); 691144674Sglebius return (ENXIO); 692144674Sglebius } 693144674Sglebius 694144674Sglebius if (sc->node->nd_flags & NG_SOURCE_ACTIVE) 695144674Sglebius return (EBUSY); 696144674Sglebius 697144674Sglebius sc->node->nd_flags |= NG_SOURCE_ACTIVE; 698144674Sglebius 699144674Sglebius sc->packets = packets; 700144674Sglebius timevalclear(&sc->stats.elapsedTime); 701144674Sglebius timevalclear(&sc->stats.endTime); 702144674Sglebius getmicrotime(&sc->stats.startTime); 703153690Sglebius getmicrotime(&sc->stats.lastTime); 704144674Sglebius ng_callout(&sc->intr_ch, sc->node, NULL, 0, 705144674Sglebius ng_source_intr, sc, 0); 706144674Sglebius 707144674Sglebius return (0); 708106266Sjulian} 709106266Sjulian 710106266Sjulian/* 711106266Sjulian * Stop sending queued data out the output hook 712106266Sjulian */ 713106266Sjulianstatic void 714144674Sglebiusng_source_stop(sc_p sc) 715106266Sjulian{ 716144674Sglebius ng_uncallout(&sc->intr_ch, sc->node); 717144674Sglebius sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 718144674Sglebius getmicrotime(&sc->stats.endTime); 719144674Sglebius sc->stats.elapsedTime = sc->stats.endTime; 720144674Sglebius timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 721106266Sjulian} 722106266Sjulian 723106266Sjulian/* 724106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 725106266Sjulian * Sends as many packets as the interface connected to our 726106266Sjulian * output hook is able to enqueue. 727106266Sjulian */ 728106266Sjulianstatic void 729125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 730106266Sjulian{ 731125243Sharti sc_p sc = (sc_p)arg1; 732106266Sjulian struct ifqueue *ifq; 733106266Sjulian int packets; 734106266Sjulian 735125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 736106266Sjulian 737144674Sglebius if (sc->packets == 0 || sc->output == NULL 738106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 739106266Sjulian ng_source_stop(sc); 740106266Sjulian return; 741106266Sjulian } 742106266Sjulian 743125033Sharti if (sc->output_ifp != NULL) { 744141745Sru ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 745125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 746125033Sharti } else 747369769Sgit2svn packets = mbufq_len(&sc->snd_queue); 748125033Sharti 749153690Sglebius if (sc->stats.maxPps != 0) { 750153690Sglebius struct timeval now, elapsed; 751153690Sglebius uint64_t usec; 752153690Sglebius int maxpkt; 753153690Sglebius 754153690Sglebius getmicrotime(&now); 755153690Sglebius elapsed = now; 756153690Sglebius timevalsub(&elapsed, &sc->stats.lastTime); 757153690Sglebius usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; 758153690Sglebius maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; 759153690Sglebius sc->stats.lastTime = now; 760153690Sglebius if (packets > maxpkt) 761153690Sglebius packets = maxpkt; 762153690Sglebius } 763153690Sglebius 764106266Sjulian ng_source_send(sc, packets, NULL); 765125243Sharti if (sc->packets == 0) 766106266Sjulian ng_source_stop(sc); 767125243Sharti else 768138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 769137136Sglebius ng_source_intr, sc, 0); 770106266Sjulian} 771106266Sjulian 772106266Sjulian/* 773154707Sglebius * Send packets out our output hook. 774106266Sjulian */ 775106266Sjulianstatic int 776154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p) 777106266Sjulian{ 778106266Sjulian struct mbuf *m, *m2; 779154707Sglebius int sent; 780106266Sjulian int error = 0; 781106266Sjulian 782125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 783106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 784125077Sharti ("%s: inactive node", __func__)); 785106266Sjulian 786144674Sglebius if ((uint64_t)tosend > sc->packets) 787106266Sjulian tosend = sc->packets; 788106266Sjulian 789154707Sglebius /* Go through the queue sending packets one by one. */ 790106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 791369769Sgit2svn m = mbufq_dequeue(&sc->snd_queue); 792106266Sjulian if (m == NULL) 793106266Sjulian break; 794106266Sjulian 795167156Semaste /* Duplicate and modify the packet. */ 796167156Semaste error = ng_source_dup_mod(sc, m, &m2); 797167156Semaste if (error) { 798167156Semaste if (error == ENOBUFS) 799369769Sgit2svn mbufq_prepend(&sc->snd_queue, m); 800167156Semaste else 801369769Sgit2svn (void)mbufq_enqueue(&sc->snd_queue, m); 802106266Sjulian break; 803106266Sjulian } 804106266Sjulian 805369769Sgit2svn /* 806369769Sgit2svn * Re-enqueue the original packet for us. The queue 807369769Sgit2svn * has a free slot, because we dequeued the packet 808369769Sgit2svn * above and this callout function runs under WRITER 809369769Sgit2svn * lock. 810369769Sgit2svn */ 811369769Sgit2svn error = mbufq_enqueue(&sc->snd_queue, m); 812369769Sgit2svn KASSERT(error == 0, ("%s: re-enqueue packet failed", __func__)); 813106266Sjulian 814154707Sglebius sc->stats.outFrames++; 815154707Sglebius sc->stats.outOctets += m2->m_pkthdr.len; 816154707Sglebius NG_SEND_DATA_ONLY(error, sc->output, m2); 817154707Sglebius if (error) 818106266Sjulian break; 819106266Sjulian } 820106266Sjulian 821106266Sjulian sc->packets -= sent; 822106266Sjulian if (sent_p != NULL) 823106266Sjulian *sent_p = sent; 824106266Sjulian return (error); 825106266Sjulian} 826167156Semaste 827167156Semaste/* 828167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset' 829167156Semaste * to data in 'cp'. 830167156Semaste * 831167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf. 832167156Semaste */ 833167156Semastestatic void 834167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp, 835167156Semaste int flags) 836167156Semaste{ 837167156Semaste if (len == 0) 838167156Semaste return; 839167156Semaste 840167156Semaste /* Can't modify beyond end of packet. */ 841167156Semaste /* TODO: Pad packet for this case. */ 842167156Semaste if (offset + len > m->m_len) 843167156Semaste return; 844167156Semaste 845167156Semaste bcopy(cp, mtod_off(m, offset, caddr_t), len); 846167156Semaste} 847167156Semaste 848167160Semastestatic void 849167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt, 850167160Semaste struct mbuf *m, int increment) 851167160Semaste{ 852167160Semaste caddr_t cp; 853167160Semaste uint32_t val; 854167160Semaste 855167160Semaste val = htonl(cnt->next_val); 856167160Semaste cp = (caddr_t)&val + sizeof(val) - cnt->width; 857167160Semaste ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags); 858167160Semaste 859167160Semaste if (increment) { 860167160Semaste cnt->next_val += increment; 861167160Semaste 862167160Semaste if (increment > 0 && cnt->next_val > cnt->max_val) { 863167160Semaste cnt->next_val = cnt->min_val - 1 + 864167160Semaste (cnt->next_val - cnt->max_val); 865167160Semaste if (cnt->next_val > cnt->max_val) 866167160Semaste cnt->next_val = cnt->max_val; 867167160Semaste } else if (increment < 0 && cnt->next_val < cnt->min_val) { 868167160Semaste cnt->next_val = cnt->max_val + 1 + 869167160Semaste (cnt->next_val - cnt->min_val); 870167160Semaste if (cnt->next_val < cnt->min_val) 871167160Semaste cnt->next_val = cnt->max_val; 872167160Semaste } 873167160Semaste } 874167160Semaste} 875167160Semaste 876167156Semastestatic int 877167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr) 878167156Semaste{ 879167156Semaste struct mbuf *m; 880167160Semaste struct ng_source_embed_cnt_info *cnt; 881167156Semaste struct ng_source_embed_info *ts; 882167156Semaste int modify; 883167156Semaste int error = 0; 884167160Semaste int i, increment; 885167156Semaste 886167156Semaste /* Are we going to modify packets? */ 887167156Semaste modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE; 888167160Semaste for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i) 889167160Semaste modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE; 890167156Semaste 891167156Semaste /* Duplicate the packet. */ 892167156Semaste if (modify) 893243882Sglebius m = m_dup(m0, M_NOWAIT); 894167156Semaste else 895243882Sglebius m = m_copypacket(m0, M_NOWAIT); 896167156Semaste if (m == NULL) { 897167156Semaste error = ENOBUFS; 898167156Semaste goto done; 899167156Semaste } 900167156Semaste *m_ptr = m; 901167156Semaste 902167156Semaste if (!modify) 903167156Semaste goto done; 904167156Semaste 905167156Semaste /* Modify the copied packet for sending. */ 906167156Semaste KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__)); 907167156Semaste 908167160Semaste for (i = 0; i < NG_SOURCE_COUNTERS; ++i) { 909167160Semaste cnt = &sc->embed_counter[i]; 910167160Semaste if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) { 911167160Semaste if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 || 912167160Semaste sc->last_packet == m0) 913167160Semaste increment = cnt->increment; 914167160Semaste else 915167160Semaste increment = 0; 916167160Semaste ng_source_mod_counter(sc, cnt, m, increment); 917167160Semaste } 918167160Semaste } 919167160Semaste 920167156Semaste ts = &sc->embed_timestamp; 921167156Semaste if (ts->flags & NGM_SOURCE_EMBED_ENABLE) { 922167156Semaste struct timeval now; 923167156Semaste getmicrotime(&now); 924167156Semaste now.tv_sec = htonl(now.tv_sec); 925167156Semaste now.tv_usec = htonl(now.tv_usec); 926167156Semaste ng_source_packet_mod(sc, m, ts->offset, sizeof (now), 927167156Semaste (caddr_t)&now, ts->flags); 928167156Semaste } 929167156Semaste 930167156Semastedone: 931167156Semaste return(error); 932167156Semaste} 933