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: releng/10.2/sys/netgraph/ng_source.c 243882 2012-12-05 08:04:20Z glebius $"); 43125077Sharti 44106266Sjulian/* 45106266Sjulian * This node is used for high speed packet geneneration. It queues 46143387Sbmilekic * all data recieved 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; 88106319Sjulian struct ifqueue 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[] = { 128106266Sjulian { "tv_sec", &ng_parse_int32_type }, 129106266Sjulian { "tv_usec", &ng_parse_int32_type }, 130106266Sjulian { NULL } 131106266Sjulian}; 132106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 133106266Sjulian &ng_parse_struct_type, 134106266Sjulian &ng_source_timeval_type_fields 135106266Sjulian}; 136106266Sjulian 137106266Sjulian/* Parse type for struct ng_source_stats */ 138106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 139106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 140106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 141106266Sjulian &ng_parse_struct_type, 142106266Sjulian &ng_source_stats_type_fields 143106266Sjulian}; 144106266Sjulian 145167156Semaste/* Parse type for struct ng_source_embed_info */ 146167156Semastestatic const struct ng_parse_struct_field ng_source_embed_type_fields[] = 147167156Semaste NG_SOURCE_EMBED_TYPE_INFO; 148167156Semastestatic const struct ng_parse_type ng_source_embed_type = { 149167156Semaste &ng_parse_struct_type, 150167156Semaste &ng_source_embed_type_fields 151167156Semaste}; 152167156Semaste 153167160Semaste/* Parse type for struct ng_source_embed_cnt_info */ 154167160Semastestatic const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] = 155167160Semaste NG_SOURCE_EMBED_CNT_TYPE_INFO; 156167160Semastestatic const struct ng_parse_type ng_source_embed_cnt_type = { 157167160Semaste &ng_parse_struct_type, 158167160Semaste &ng_source_embed_cnt_type_fields 159167160Semaste}; 160167160Semaste 161106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 162106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 163106266Sjulian { 164106266Sjulian NGM_SOURCE_COOKIE, 165106266Sjulian NGM_SOURCE_GET_STATS, 166106266Sjulian "getstats", 167106266Sjulian NULL, 168106266Sjulian &ng_source_stats_type 169106266Sjulian }, 170106266Sjulian { 171106266Sjulian NGM_SOURCE_COOKIE, 172106266Sjulian NGM_SOURCE_CLR_STATS, 173106266Sjulian "clrstats", 174106266Sjulian NULL, 175106266Sjulian NULL 176106266Sjulian }, 177106266Sjulian { 178106266Sjulian NGM_SOURCE_COOKIE, 179106266Sjulian NGM_SOURCE_GETCLR_STATS, 180106266Sjulian "getclrstats", 181106266Sjulian NULL, 182106266Sjulian &ng_source_stats_type 183106266Sjulian }, 184106266Sjulian { 185106266Sjulian NGM_SOURCE_COOKIE, 186106266Sjulian NGM_SOURCE_START, 187106266Sjulian "start", 188106266Sjulian &ng_parse_uint64_type, 189106266Sjulian NULL 190106266Sjulian }, 191106266Sjulian { 192106266Sjulian NGM_SOURCE_COOKIE, 193106266Sjulian NGM_SOURCE_STOP, 194106266Sjulian "stop", 195106266Sjulian NULL, 196106266Sjulian NULL 197106266Sjulian }, 198106266Sjulian { 199106266Sjulian NGM_SOURCE_COOKIE, 200106266Sjulian NGM_SOURCE_CLR_DATA, 201106266Sjulian "clrdata", 202106266Sjulian NULL, 203106266Sjulian NULL 204106266Sjulian }, 205125033Sharti { 206125033Sharti NGM_SOURCE_COOKIE, 207144674Sglebius NGM_SOURCE_SETIFACE, 208144674Sglebius "setiface", 209144674Sglebius &ng_parse_string_type, 210125033Sharti NULL 211125033Sharti }, 212153690Sglebius { 213153690Sglebius NGM_SOURCE_COOKIE, 214153690Sglebius NGM_SOURCE_SETPPS, 215153690Sglebius "setpps", 216153690Sglebius &ng_parse_uint32_type, 217153690Sglebius NULL 218153690Sglebius }, 219167156Semaste { 220167156Semaste NGM_SOURCE_COOKIE, 221167156Semaste NGM_SOURCE_SET_TIMESTAMP, 222167156Semaste "settimestamp", 223167156Semaste &ng_source_embed_type, 224167156Semaste NULL 225167156Semaste }, 226167156Semaste { 227167156Semaste NGM_SOURCE_COOKIE, 228167156Semaste NGM_SOURCE_GET_TIMESTAMP, 229167156Semaste "gettimestamp", 230167156Semaste NULL, 231167156Semaste &ng_source_embed_type 232167156Semaste }, 233167160Semaste { 234167160Semaste NGM_SOURCE_COOKIE, 235167160Semaste NGM_SOURCE_SET_COUNTER, 236167160Semaste "setcounter", 237167160Semaste &ng_source_embed_cnt_type, 238167160Semaste NULL 239167160Semaste }, 240167160Semaste { 241167160Semaste NGM_SOURCE_COOKIE, 242167160Semaste NGM_SOURCE_GET_COUNTER, 243167160Semaste "getcounter", 244167160Semaste &ng_parse_uint8_type, 245167160Semaste &ng_source_embed_cnt_type 246167160Semaste }, 247106266Sjulian { 0 } 248106266Sjulian}; 249106266Sjulian 250106266Sjulian/* Netgraph type descriptor */ 251106266Sjulianstatic struct ng_type ng_source_typestruct = { 252129823Sjulian .version = NG_ABI_VERSION, 253129823Sjulian .name = NG_SOURCE_NODE_TYPE, 254129823Sjulian .constructor = ng_source_constructor, 255129823Sjulian .rcvmsg = ng_source_rcvmsg, 256129823Sjulian .shutdown = ng_source_rmnode, 257129823Sjulian .newhook = ng_source_newhook, 258144674Sglebius .connect = ng_source_connect, 259129823Sjulian .rcvdata = ng_source_rcvdata, 260129823Sjulian .disconnect = ng_source_disconnect, 261129823Sjulian .cmdlist = ng_source_cmds, 262106266Sjulian}; 263106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 264106266Sjulian 265144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t); 266125032Sharti 267106266Sjulian/* 268106266Sjulian * Node constructor 269106266Sjulian */ 270106266Sjulianstatic int 271106321Sjulianng_source_constructor(node_p node) 272106266Sjulian{ 273106266Sjulian sc_p sc; 274106266Sjulian 275220768Sglebius sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 276106266Sjulian 277106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 278106321Sjulian sc->node = node; 279106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 280137138Sglebius ng_callout_init(&sc->intr_ch); 281137138Sglebius 282106266Sjulian return (0); 283106266Sjulian} 284106266Sjulian 285106266Sjulian/* 286106266Sjulian * Add a hook 287106266Sjulian */ 288106266Sjulianstatic int 289106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 290106266Sjulian{ 291144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 292106266Sjulian 293106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 294144674Sglebius sc->input = hook; 295106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 296144674Sglebius sc->output = hook; 297106266Sjulian sc->output_ifp = 0; 298106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 299106266Sjulian } else 300106266Sjulian return (EINVAL); 301144674Sglebius 302106266Sjulian return (0); 303106266Sjulian} 304106266Sjulian 305106266Sjulian/* 306144674Sglebius * Hook has been added 307144674Sglebius */ 308144674Sglebiusstatic int 309144674Sglebiusng_source_connect(hook_p hook) 310144674Sglebius{ 311144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 312144674Sglebius struct ng_mesg *msg; 313144674Sglebius int dummy_error = 0; 314144674Sglebius 315144674Sglebius /* 316144674Sglebius * If this is "output" hook, then request information 317144674Sglebius * from our downstream. 318144674Sglebius */ 319144674Sglebius if (hook == sc->output) { 320144674Sglebius NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME, 321144674Sglebius 0, M_NOWAIT); 322144674Sglebius if (msg == NULL) 323144674Sglebius return (ENOBUFS); 324144674Sglebius 325144674Sglebius /* 326144674Sglebius * Our hook and peer hook have HK_INVALID flag set, 327144674Sglebius * so we can't use NG_SEND_MSG_HOOK() macro here. 328144674Sglebius */ 329144674Sglebius NG_SEND_MSG_ID(dummy_error, sc->node, msg, 330144674Sglebius NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node)); 331144674Sglebius } 332144674Sglebius 333144674Sglebius return (0); 334144674Sglebius} 335144674Sglebius 336144674Sglebius/* 337106266Sjulian * Receive a control message 338106266Sjulian */ 339106266Sjulianstatic int 340106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 341106266Sjulian{ 342144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 343144674Sglebius struct ng_mesg *msg, *resp = NULL; 344106266Sjulian int error = 0; 345106266Sjulian 346106321Sjulian NGI_GET_MSG(item, msg); 347144674Sglebius 348106266Sjulian switch (msg->header.typecookie) { 349106266Sjulian case NGM_SOURCE_COOKIE: 350106435Sjulian if (msg->header.flags & NGF_RESP) { 351106435Sjulian error = EINVAL; 352106435Sjulian break; 353106435Sjulian } 354106266Sjulian switch (msg->header.cmd) { 355106266Sjulian case NGM_SOURCE_GET_STATS: 356106266Sjulian case NGM_SOURCE_CLR_STATS: 357106266Sjulian case NGM_SOURCE_GETCLR_STATS: 358106266Sjulian { 359106266Sjulian struct ng_source_stats *stats; 360106266Sjulian 361106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 362106266Sjulian NG_MKRESPONSE(resp, msg, 363106266Sjulian sizeof(*stats), M_NOWAIT); 364106266Sjulian if (resp == NULL) { 365106266Sjulian error = ENOMEM; 366106266Sjulian goto done; 367106266Sjulian } 368106266Sjulian sc->stats.queueOctets = sc->queueOctets; 369106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 370106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 371106266Sjulian && !timevalisset(&sc->stats.endTime)) { 372106319Sjulian getmicrotime(&sc->stats.elapsedTime); 373106266Sjulian timevalsub(&sc->stats.elapsedTime, 374106319Sjulian &sc->stats.startTime); 375106266Sjulian } 376106319Sjulian stats = (struct ng_source_stats *)resp->data; 377106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 378106266Sjulian } 379106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 380106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 381106266Sjulian } 382106266Sjulian break; 383106266Sjulian case NGM_SOURCE_START: 384106266Sjulian { 385144674Sglebius uint64_t packets; 386144674Sglebius 387144674Sglebius if (msg->header.arglen != sizeof(uint64_t)) { 388106266Sjulian error = EINVAL; 389106266Sjulian break; 390106266Sjulian } 391125033Sharti 392144674Sglebius packets = *(uint64_t *)msg->data; 393144674Sglebius 394144674Sglebius error = ng_source_start(sc, packets); 395144674Sglebius 396144674Sglebius break; 397125033Sharti } 398106266Sjulian case NGM_SOURCE_STOP: 399106266Sjulian ng_source_stop(sc); 400106266Sjulian break; 401106266Sjulian case NGM_SOURCE_CLR_DATA: 402106266Sjulian ng_source_clr_data(sc); 403106266Sjulian break; 404144674Sglebius case NGM_SOURCE_SETIFACE: 405144674Sglebius { 406144674Sglebius char *ifname = (char *)msg->data; 407144674Sglebius 408144674Sglebius if (msg->header.arglen < 2) { 409144674Sglebius error = EINVAL; 410144674Sglebius break; 411144674Sglebius } 412144674Sglebius 413144674Sglebius ng_source_store_output_ifp(sc, ifname); 414144674Sglebius break; 415144674Sglebius } 416153690Sglebius case NGM_SOURCE_SETPPS: 417153690Sglebius { 418153690Sglebius uint32_t pps; 419153690Sglebius 420153690Sglebius if (msg->header.arglen != sizeof(uint32_t)) { 421153690Sglebius error = EINVAL; 422153690Sglebius break; 423153690Sglebius } 424153690Sglebius 425153690Sglebius pps = *(uint32_t *)msg->data; 426153690Sglebius 427153690Sglebius sc->stats.maxPps = pps; 428153690Sglebius 429153690Sglebius break; 430153690Sglebius } 431167156Semaste case NGM_SOURCE_SET_TIMESTAMP: 432167156Semaste { 433167156Semaste struct ng_source_embed_info *embed; 434167156Semaste 435167168Semaste if (msg->header.arglen != sizeof(*embed)) { 436167168Semaste error = EINVAL; 437167168Semaste goto done; 438167168Semaste } 439167156Semaste embed = (struct ng_source_embed_info *)msg->data; 440167156Semaste bcopy(embed, &sc->embed_timestamp, sizeof(*embed)); 441167156Semaste 442167156Semaste break; 443167156Semaste } 444167156Semaste case NGM_SOURCE_GET_TIMESTAMP: 445167156Semaste { 446167156Semaste struct ng_source_embed_info *embed; 447167156Semaste 448243882Sglebius NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT); 449167156Semaste if (resp == NULL) { 450167156Semaste error = ENOMEM; 451167156Semaste goto done; 452167156Semaste } 453167156Semaste embed = (struct ng_source_embed_info *)resp->data; 454167156Semaste bcopy(&sc->embed_timestamp, embed, sizeof(*embed)); 455167156Semaste 456167156Semaste break; 457167156Semaste } 458167160Semaste case NGM_SOURCE_SET_COUNTER: 459167160Semaste { 460167160Semaste struct ng_source_embed_cnt_info *embed; 461167160Semaste 462167168Semaste if (msg->header.arglen != sizeof(*embed)) { 463167168Semaste error = EINVAL; 464167168Semaste goto done; 465167168Semaste } 466167160Semaste embed = (struct ng_source_embed_cnt_info *)msg->data; 467167160Semaste if (embed->index >= NG_SOURCE_COUNTERS || 468167160Semaste !(embed->width == 1 || embed->width == 2 || 469167160Semaste embed->width == 4)) { 470167160Semaste error = EINVAL; 471167160Semaste goto done; 472167160Semaste } 473167160Semaste bcopy(embed, &sc->embed_counter[embed->index], 474167160Semaste sizeof(*embed)); 475167160Semaste 476167160Semaste break; 477167160Semaste } 478167160Semaste case NGM_SOURCE_GET_COUNTER: 479167160Semaste { 480167160Semaste uint8_t index = *(uint8_t *)msg->data; 481167160Semaste struct ng_source_embed_cnt_info *embed; 482167160Semaste 483167160Semaste if (index >= NG_SOURCE_COUNTERS) { 484167160Semaste error = EINVAL; 485167160Semaste goto done; 486167160Semaste } 487243882Sglebius NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT); 488167160Semaste if (resp == NULL) { 489167160Semaste error = ENOMEM; 490167160Semaste goto done; 491167160Semaste } 492167160Semaste embed = (struct ng_source_embed_cnt_info *)resp->data; 493167160Semaste bcopy(&sc->embed_counter[index], embed, sizeof(*embed)); 494167160Semaste 495167160Semaste break; 496167160Semaste } 497106266Sjulian default: 498106266Sjulian error = EINVAL; 499106266Sjulian break; 500106266Sjulian } 501106266Sjulian break; 502106435Sjulian case NGM_ETHER_COOKIE: 503106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 504106435Sjulian error = EINVAL; 505106435Sjulian break; 506106435Sjulian } 507106435Sjulian switch (msg->header.cmd) { 508144674Sglebius case NGM_ETHER_GET_IFNAME: 509144674Sglebius { 510144674Sglebius char *ifname = (char *)msg->data; 511144674Sglebius 512144674Sglebius if (msg->header.arglen < 2) { 513144674Sglebius error = EINVAL; 514144674Sglebius break; 515144674Sglebius } 516144674Sglebius 517144674Sglebius if (ng_source_store_output_ifp(sc, ifname) == 0) 518106435Sjulian ng_source_set_autosrc(sc, 0); 519106435Sjulian break; 520144674Sglebius } 521106435Sjulian default: 522106435Sjulian error = EINVAL; 523106435Sjulian } 524106435Sjulian break; 525106266Sjulian default: 526106266Sjulian error = EINVAL; 527106266Sjulian break; 528106266Sjulian } 529106266Sjulian 530106266Sjuliandone: 531144674Sglebius /* Take care of synchronous response, if any. */ 532106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 533144674Sglebius /* Free the message and return. */ 534106321Sjulian NG_FREE_MSG(msg); 535106266Sjulian return (error); 536106266Sjulian} 537106266Sjulian 538106266Sjulian/* 539106266Sjulian * Receive data on a hook 540106266Sjulian * 541106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 542106266Sjulian * If data comes in the output hook, discard it. 543106266Sjulian */ 544106266Sjulianstatic int 545106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 546106266Sjulian{ 547144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 548144674Sglebius struct mbuf *m; 549106266Sjulian int error = 0; 550106266Sjulian 551106321Sjulian NGI_GET_M(item, m); 552106321Sjulian NG_FREE_ITEM(item); 553106266Sjulian 554106266Sjulian /* Which hook? */ 555144674Sglebius if (hook == sc->output) { 556106266Sjulian /* discard */ 557106321Sjulian NG_FREE_M(m); 558106266Sjulian return (error); 559106266Sjulian } 560144674Sglebius KASSERT(hook == sc->input, ("%s: no hook!", __func__)); 561106266Sjulian 562144674Sglebius /* Enqueue packet. */ 563106266Sjulian /* XXX should we check IF_QFULL() ? */ 564125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 565106266Sjulian sc->queueOctets += m->m_pkthdr.len; 566167160Semaste sc->last_packet = m; 567106266Sjulian 568106266Sjulian return (0); 569106266Sjulian} 570106266Sjulian 571106266Sjulian/* 572106266Sjulian * Shutdown processing 573106266Sjulian */ 574106266Sjulianstatic int 575106266Sjulianng_source_rmnode(node_p node) 576106266Sjulian{ 577144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 578106266Sjulian 579106266Sjulian ng_source_stop(sc); 580106266Sjulian ng_source_clr_data(sc); 581106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 582106321Sjulian NG_NODE_UNREF(node); 583125077Sharti free(sc, M_NETGRAPH); 584144674Sglebius 585106266Sjulian return (0); 586106266Sjulian} 587106266Sjulian 588106266Sjulian/* 589106266Sjulian * Hook disconnection 590106266Sjulian */ 591106266Sjulianstatic int 592106266Sjulianng_source_disconnect(hook_p hook) 593106266Sjulian{ 594106319Sjulian sc_p sc; 595106266Sjulian 596106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 597125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 598144674Sglebius if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output) 599106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 600106266Sjulian return (0); 601106266Sjulian} 602106266Sjulian 603106266Sjulian/* 604218909Sbrucec * Set sc->output_ifp to point to the struct ifnet of the interface 605106435Sjulian * reached via our output hook. 606106435Sjulian */ 607106435Sjulianstatic int 608144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname) 609106435Sjulian{ 610106435Sjulian struct ifnet *ifp; 611106266Sjulian 612144674Sglebius ifp = ifunit(ifname); 613106266Sjulian 614106266Sjulian if (ifp == NULL) { 615183225Szec printf("%s: can't find interface %s\n", __func__, ifname); 616106266Sjulian return (EINVAL); 617106266Sjulian } 618106266Sjulian sc->output_ifp = ifp; 619106266Sjulian 620106266Sjulian#if 1 621106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 622106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 623106266Sjulian * interface with small packets. 624106266Sjulian * XXX we should restore the original value at stop or disconnect 625106266Sjulian */ 626125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 627106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 628106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 629106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 630106266Sjulian } 631106266Sjulian#endif 632106266Sjulian return (0); 633106266Sjulian} 634106266Sjulian 635106266Sjulian/* 636106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 637106266Sjulian */ 638106266Sjulianstatic int 639144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag) 640106266Sjulian{ 641106266Sjulian struct ng_mesg *msg; 642106266Sjulian int error = 0; 643106266Sjulian 644106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 645144674Sglebius sizeof (uint32_t), M_NOWAIT); 646106266Sjulian if (msg == NULL) 647106266Sjulian return(ENOBUFS); 648106266Sjulian 649144674Sglebius *(uint32_t *)msg->data = flag; 650144674Sglebius NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0); 651106266Sjulian return (error); 652106266Sjulian} 653106266Sjulian 654106266Sjulian/* 655106266Sjulian * Clear out the data we've queued 656106266Sjulian */ 657106266Sjulianstatic void 658106266Sjulianng_source_clr_data (sc_p sc) 659106266Sjulian{ 660106266Sjulian struct mbuf *m; 661106266Sjulian 662106266Sjulian for (;;) { 663125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 664106266Sjulian if (m == NULL) 665106266Sjulian break; 666106266Sjulian NG_FREE_M(m); 667106266Sjulian } 668106266Sjulian sc->queueOctets = 0; 669167168Semaste sc->last_packet = NULL; 670106266Sjulian} 671106266Sjulian 672106266Sjulian/* 673106266Sjulian * Start sending queued data out the output hook 674106266Sjulian */ 675144674Sglebiusstatic int 676144674Sglebiusng_source_start(sc_p sc, uint64_t packets) 677106266Sjulian{ 678144674Sglebius if (sc->output_ifp == NULL) { 679144674Sglebius printf("ng_source: start without iface configured\n"); 680144674Sglebius return (ENXIO); 681144674Sglebius } 682144674Sglebius 683144674Sglebius if (sc->node->nd_flags & NG_SOURCE_ACTIVE) 684144674Sglebius return (EBUSY); 685144674Sglebius 686144674Sglebius sc->node->nd_flags |= NG_SOURCE_ACTIVE; 687144674Sglebius 688144674Sglebius sc->packets = packets; 689144674Sglebius timevalclear(&sc->stats.elapsedTime); 690144674Sglebius timevalclear(&sc->stats.endTime); 691144674Sglebius getmicrotime(&sc->stats.startTime); 692153690Sglebius getmicrotime(&sc->stats.lastTime); 693144674Sglebius ng_callout(&sc->intr_ch, sc->node, NULL, 0, 694144674Sglebius ng_source_intr, sc, 0); 695144674Sglebius 696144674Sglebius return (0); 697106266Sjulian} 698106266Sjulian 699106266Sjulian/* 700106266Sjulian * Stop sending queued data out the output hook 701106266Sjulian */ 702106266Sjulianstatic void 703144674Sglebiusng_source_stop(sc_p sc) 704106266Sjulian{ 705144674Sglebius ng_uncallout(&sc->intr_ch, sc->node); 706144674Sglebius sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 707144674Sglebius getmicrotime(&sc->stats.endTime); 708144674Sglebius sc->stats.elapsedTime = sc->stats.endTime; 709144674Sglebius timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 710106266Sjulian} 711106266Sjulian 712106266Sjulian/* 713106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 714106266Sjulian * Sends as many packets as the interface connected to our 715106266Sjulian * output hook is able to enqueue. 716106266Sjulian */ 717106266Sjulianstatic void 718125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 719106266Sjulian{ 720125243Sharti sc_p sc = (sc_p)arg1; 721106266Sjulian struct ifqueue *ifq; 722106266Sjulian int packets; 723106266Sjulian 724125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 725106266Sjulian 726144674Sglebius if (sc->packets == 0 || sc->output == NULL 727106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 728106266Sjulian ng_source_stop(sc); 729106266Sjulian return; 730106266Sjulian } 731106266Sjulian 732125033Sharti if (sc->output_ifp != NULL) { 733141745Sru ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 734125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 735125033Sharti } else 736125033Sharti packets = sc->snd_queue.ifq_len; 737125033Sharti 738153690Sglebius if (sc->stats.maxPps != 0) { 739153690Sglebius struct timeval now, elapsed; 740153690Sglebius uint64_t usec; 741153690Sglebius int maxpkt; 742153690Sglebius 743153690Sglebius getmicrotime(&now); 744153690Sglebius elapsed = now; 745153690Sglebius timevalsub(&elapsed, &sc->stats.lastTime); 746153690Sglebius usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; 747153690Sglebius maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; 748153690Sglebius sc->stats.lastTime = now; 749153690Sglebius if (packets > maxpkt) 750153690Sglebius packets = maxpkt; 751153690Sglebius } 752153690Sglebius 753106266Sjulian ng_source_send(sc, packets, NULL); 754125243Sharti if (sc->packets == 0) 755106266Sjulian ng_source_stop(sc); 756125243Sharti else 757138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 758137136Sglebius ng_source_intr, sc, 0); 759106266Sjulian} 760106266Sjulian 761106266Sjulian/* 762154707Sglebius * Send packets out our output hook. 763106266Sjulian */ 764106266Sjulianstatic int 765154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p) 766106266Sjulian{ 767106266Sjulian struct mbuf *m, *m2; 768154707Sglebius int sent; 769106266Sjulian int error = 0; 770106266Sjulian 771125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 772106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 773125077Sharti ("%s: inactive node", __func__)); 774106266Sjulian 775144674Sglebius if ((uint64_t)tosend > sc->packets) 776106266Sjulian tosend = sc->packets; 777106266Sjulian 778154707Sglebius /* Go through the queue sending packets one by one. */ 779106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 780125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 781106266Sjulian if (m == NULL) 782106266Sjulian break; 783106266Sjulian 784167156Semaste /* Duplicate and modify the packet. */ 785167156Semaste error = ng_source_dup_mod(sc, m, &m2); 786167156Semaste if (error) { 787167156Semaste if (error == ENOBUFS) 788167156Semaste _IF_PREPEND(&sc->snd_queue, m); 789167156Semaste else 790167156Semaste _IF_ENQUEUE(&sc->snd_queue, m); 791106266Sjulian break; 792106266Sjulian } 793106266Sjulian 794144674Sglebius /* Re-enqueue the original packet for us. */ 795125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 796106266Sjulian 797154707Sglebius sc->stats.outFrames++; 798154707Sglebius sc->stats.outOctets += m2->m_pkthdr.len; 799154707Sglebius NG_SEND_DATA_ONLY(error, sc->output, m2); 800154707Sglebius if (error) 801106266Sjulian break; 802106266Sjulian } 803106266Sjulian 804106266Sjulian sc->packets -= sent; 805106266Sjulian if (sent_p != NULL) 806106266Sjulian *sent_p = sent; 807106266Sjulian return (error); 808106266Sjulian} 809167156Semaste 810167156Semaste/* 811167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset' 812167156Semaste * to data in 'cp'. 813167156Semaste * 814167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf. 815167156Semaste */ 816167156Semastestatic void 817167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp, 818167156Semaste int flags) 819167156Semaste{ 820167156Semaste if (len == 0) 821167156Semaste return; 822167156Semaste 823167156Semaste /* Can't modify beyond end of packet. */ 824167156Semaste /* TODO: Pad packet for this case. */ 825167156Semaste if (offset + len > m->m_len) 826167156Semaste return; 827167156Semaste 828167156Semaste bcopy(cp, mtod_off(m, offset, caddr_t), len); 829167156Semaste} 830167156Semaste 831167160Semastestatic void 832167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt, 833167160Semaste struct mbuf *m, int increment) 834167160Semaste{ 835167160Semaste caddr_t cp; 836167160Semaste uint32_t val; 837167160Semaste 838167160Semaste val = htonl(cnt->next_val); 839167160Semaste cp = (caddr_t)&val + sizeof(val) - cnt->width; 840167160Semaste ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags); 841167160Semaste 842167160Semaste if (increment) { 843167160Semaste cnt->next_val += increment; 844167160Semaste 845167160Semaste if (increment > 0 && cnt->next_val > cnt->max_val) { 846167160Semaste cnt->next_val = cnt->min_val - 1 + 847167160Semaste (cnt->next_val - cnt->max_val); 848167160Semaste if (cnt->next_val > cnt->max_val) 849167160Semaste cnt->next_val = cnt->max_val; 850167160Semaste } else if (increment < 0 && cnt->next_val < cnt->min_val) { 851167160Semaste cnt->next_val = cnt->max_val + 1 + 852167160Semaste (cnt->next_val - cnt->min_val); 853167160Semaste if (cnt->next_val < cnt->min_val) 854167160Semaste cnt->next_val = cnt->max_val; 855167160Semaste } 856167160Semaste } 857167160Semaste} 858167160Semaste 859167156Semastestatic int 860167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr) 861167156Semaste{ 862167156Semaste struct mbuf *m; 863167160Semaste struct ng_source_embed_cnt_info *cnt; 864167156Semaste struct ng_source_embed_info *ts; 865167156Semaste int modify; 866167156Semaste int error = 0; 867167160Semaste int i, increment; 868167156Semaste 869167156Semaste /* Are we going to modify packets? */ 870167156Semaste modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE; 871167160Semaste for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i) 872167160Semaste modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE; 873167156Semaste 874167156Semaste /* Duplicate the packet. */ 875167156Semaste if (modify) 876243882Sglebius m = m_dup(m0, M_NOWAIT); 877167156Semaste else 878243882Sglebius m = m_copypacket(m0, M_NOWAIT); 879167156Semaste if (m == NULL) { 880167156Semaste error = ENOBUFS; 881167156Semaste goto done; 882167156Semaste } 883167156Semaste *m_ptr = m; 884167156Semaste 885167156Semaste if (!modify) 886167156Semaste goto done; 887167156Semaste 888167156Semaste /* Modify the copied packet for sending. */ 889167156Semaste KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__)); 890167156Semaste 891167160Semaste for (i = 0; i < NG_SOURCE_COUNTERS; ++i) { 892167160Semaste cnt = &sc->embed_counter[i]; 893167160Semaste if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) { 894167160Semaste if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 || 895167160Semaste sc->last_packet == m0) 896167160Semaste increment = cnt->increment; 897167160Semaste else 898167160Semaste increment = 0; 899167160Semaste ng_source_mod_counter(sc, cnt, m, increment); 900167160Semaste } 901167160Semaste } 902167160Semaste 903167156Semaste ts = &sc->embed_timestamp; 904167156Semaste if (ts->flags & NGM_SOURCE_EMBED_ENABLE) { 905167156Semaste struct timeval now; 906167156Semaste getmicrotime(&now); 907167156Semaste now.tv_sec = htonl(now.tv_sec); 908167156Semaste now.tv_usec = htonl(now.tv_usec); 909167156Semaste ng_source_packet_mod(sc, m, ts->offset, sizeof (now), 910167156Semaste (caddr_t)&now, ts->flags); 911167156Semaste } 912167156Semaste 913167156Semastedone: 914167156Semaste return(error); 915167156Semaste} 916