ng_source.c revision 209728
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: head/sys/netgraph/ng_source.c 209728 2010-07-06 16:42:11Z emaste $"); 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> 71196019Srwatson#include <net/vnet.h> 72106266Sjulian#include <netgraph/ng_message.h> 73106266Sjulian#include <netgraph/netgraph.h> 74106266Sjulian#include <netgraph/ng_parse.h> 75106266Sjulian#include <netgraph/ng_ether.h> 76106266Sjulian#include <netgraph/ng_source.h> 77106266Sjulian 78106266Sjulian#define NG_SOURCE_INTR_TICKS 1 79106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024) 80106266Sjulian 81167156Semaste#define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off))) 82167156Semaste 83106266Sjulian/* Per node info */ 84106266Sjulianstruct privdata { 85106266Sjulian node_p node; 86144674Sglebius hook_p input; 87144674Sglebius hook_p output; 88106266Sjulian struct ng_source_stats stats; 89106319Sjulian struct ifqueue snd_queue; /* packets to send */ 90167160Semaste struct mbuf *last_packet; /* last pkt in queue */ 91106266Sjulian struct ifnet *output_ifp; 92137138Sglebius struct callout intr_ch; 93144674Sglebius uint64_t packets; /* packets to send */ 94144674Sglebius uint32_t queueOctets; 95167156Semaste struct ng_source_embed_info embed_timestamp; 96167160Semaste struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS]; 97106266Sjulian}; 98106266Sjuliantypedef struct privdata *sc_p; 99106266Sjulian 100106266Sjulian/* Node flags */ 101106266Sjulian#define NG_SOURCE_ACTIVE (NGF_TYPE1) 102106266Sjulian 103106266Sjulian/* Netgraph methods */ 104106266Sjulianstatic ng_constructor_t ng_source_constructor; 105106266Sjulianstatic ng_rcvmsg_t ng_source_rcvmsg; 106106266Sjulianstatic ng_shutdown_t ng_source_rmnode; 107106266Sjulianstatic ng_newhook_t ng_source_newhook; 108144674Sglebiusstatic ng_connect_t ng_source_connect; 109106266Sjulianstatic ng_rcvdata_t ng_source_rcvdata; 110106266Sjulianstatic ng_disconnect_t ng_source_disconnect; 111106266Sjulian 112106266Sjulian/* Other functions */ 113125243Shartistatic void ng_source_intr(node_p, hook_p, void *, int); 114106266Sjulianstatic void ng_source_clr_data (sc_p); 115144674Sglebiusstatic int ng_source_start (sc_p, uint64_t); 116106266Sjulianstatic void ng_source_stop (sc_p); 117106266Sjulianstatic int ng_source_send (sc_p, int, int *); 118144674Sglebiusstatic int ng_source_store_output_ifp(sc_p, char *); 119167156Semastestatic void ng_source_packet_mod(sc_p, struct mbuf *, 120167156Semaste int, int, caddr_t, int); 121167160Semastestatic void ng_source_mod_counter(sc_p sc, 122167160Semaste struct ng_source_embed_cnt_info *cnt, 123167160Semaste struct mbuf *m, int increment); 124167156Semastestatic int ng_source_dup_mod(sc_p, struct mbuf *, 125167156Semaste struct mbuf **); 126106266Sjulian 127106266Sjulian/* Parse type for timeval */ 128125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = { 129106266Sjulian { "tv_sec", &ng_parse_int32_type }, 130106266Sjulian { "tv_usec", &ng_parse_int32_type }, 131106266Sjulian { NULL } 132106266Sjulian}; 133106266Sjulianconst struct ng_parse_type ng_source_timeval_type = { 134106266Sjulian &ng_parse_struct_type, 135106266Sjulian &ng_source_timeval_type_fields 136106266Sjulian}; 137106266Sjulian 138106266Sjulian/* Parse type for struct ng_source_stats */ 139106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[] 140106266Sjulian = NG_SOURCE_STATS_TYPE_INFO; 141106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = { 142106266Sjulian &ng_parse_struct_type, 143106266Sjulian &ng_source_stats_type_fields 144106266Sjulian}; 145106266Sjulian 146167156Semaste/* Parse type for struct ng_source_embed_info */ 147167156Semastestatic const struct ng_parse_struct_field ng_source_embed_type_fields[] = 148167156Semaste NG_SOURCE_EMBED_TYPE_INFO; 149167156Semastestatic const struct ng_parse_type ng_source_embed_type = { 150167156Semaste &ng_parse_struct_type, 151167156Semaste &ng_source_embed_type_fields 152167156Semaste}; 153167156Semaste 154167160Semaste/* Parse type for struct ng_source_embed_cnt_info */ 155167160Semastestatic const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] = 156167160Semaste NG_SOURCE_EMBED_CNT_TYPE_INFO; 157167160Semastestatic const struct ng_parse_type ng_source_embed_cnt_type = { 158167160Semaste &ng_parse_struct_type, 159167160Semaste &ng_source_embed_cnt_type_fields 160167160Semaste}; 161167160Semaste 162106266Sjulian/* List of commands and how to convert arguments to/from ASCII */ 163106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = { 164106266Sjulian { 165106266Sjulian NGM_SOURCE_COOKIE, 166106266Sjulian NGM_SOURCE_GET_STATS, 167106266Sjulian "getstats", 168106266Sjulian NULL, 169106266Sjulian &ng_source_stats_type 170106266Sjulian }, 171106266Sjulian { 172106266Sjulian NGM_SOURCE_COOKIE, 173106266Sjulian NGM_SOURCE_CLR_STATS, 174106266Sjulian "clrstats", 175106266Sjulian NULL, 176106266Sjulian NULL 177106266Sjulian }, 178106266Sjulian { 179106266Sjulian NGM_SOURCE_COOKIE, 180106266Sjulian NGM_SOURCE_GETCLR_STATS, 181106266Sjulian "getclrstats", 182106266Sjulian NULL, 183106266Sjulian &ng_source_stats_type 184106266Sjulian }, 185106266Sjulian { 186106266Sjulian NGM_SOURCE_COOKIE, 187106266Sjulian NGM_SOURCE_START, 188106266Sjulian "start", 189106266Sjulian &ng_parse_uint64_type, 190106266Sjulian NULL 191106266Sjulian }, 192106266Sjulian { 193106266Sjulian NGM_SOURCE_COOKIE, 194106266Sjulian NGM_SOURCE_STOP, 195106266Sjulian "stop", 196106266Sjulian NULL, 197106266Sjulian NULL 198106266Sjulian }, 199106266Sjulian { 200106266Sjulian NGM_SOURCE_COOKIE, 201106266Sjulian NGM_SOURCE_CLR_DATA, 202106266Sjulian "clrdata", 203106266Sjulian NULL, 204106266Sjulian NULL 205106266Sjulian }, 206125033Sharti { 207125033Sharti NGM_SOURCE_COOKIE, 208144674Sglebius NGM_SOURCE_SETIFACE, 209144674Sglebius "setiface", 210144674Sglebius &ng_parse_string_type, 211125033Sharti NULL 212125033Sharti }, 213153690Sglebius { 214153690Sglebius NGM_SOURCE_COOKIE, 215153690Sglebius NGM_SOURCE_SETPPS, 216153690Sglebius "setpps", 217153690Sglebius &ng_parse_uint32_type, 218153690Sglebius NULL 219153690Sglebius }, 220167156Semaste { 221167156Semaste NGM_SOURCE_COOKIE, 222167156Semaste NGM_SOURCE_SET_TIMESTAMP, 223167156Semaste "settimestamp", 224167156Semaste &ng_source_embed_type, 225167156Semaste NULL 226167156Semaste }, 227167156Semaste { 228167156Semaste NGM_SOURCE_COOKIE, 229167156Semaste NGM_SOURCE_GET_TIMESTAMP, 230167156Semaste "gettimestamp", 231167156Semaste NULL, 232167156Semaste &ng_source_embed_type 233167156Semaste }, 234167160Semaste { 235167160Semaste NGM_SOURCE_COOKIE, 236167160Semaste NGM_SOURCE_SET_COUNTER, 237167160Semaste "setcounter", 238167160Semaste &ng_source_embed_cnt_type, 239167160Semaste NULL 240167160Semaste }, 241167160Semaste { 242167160Semaste NGM_SOURCE_COOKIE, 243167160Semaste NGM_SOURCE_GET_COUNTER, 244167160Semaste "getcounter", 245167160Semaste &ng_parse_uint8_type, 246167160Semaste &ng_source_embed_cnt_type 247167160Semaste }, 248106266Sjulian { 0 } 249106266Sjulian}; 250106266Sjulian 251106266Sjulian/* Netgraph type descriptor */ 252106266Sjulianstatic struct ng_type ng_source_typestruct = { 253129823Sjulian .version = NG_ABI_VERSION, 254129823Sjulian .name = NG_SOURCE_NODE_TYPE, 255129823Sjulian .constructor = ng_source_constructor, 256129823Sjulian .rcvmsg = ng_source_rcvmsg, 257129823Sjulian .shutdown = ng_source_rmnode, 258129823Sjulian .newhook = ng_source_newhook, 259144674Sglebius .connect = ng_source_connect, 260129823Sjulian .rcvdata = ng_source_rcvdata, 261129823Sjulian .disconnect = ng_source_disconnect, 262129823Sjulian .cmdlist = ng_source_cmds, 263106266Sjulian}; 264106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct); 265106266Sjulian 266144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t); 267125032Sharti 268106266Sjulian/* 269106266Sjulian * Node constructor 270106266Sjulian */ 271106266Sjulianstatic int 272106321Sjulianng_source_constructor(node_p node) 273106266Sjulian{ 274106266Sjulian sc_p sc; 275106266Sjulian 276125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 277106266Sjulian if (sc == NULL) 278106266Sjulian return (ENOMEM); 279106266Sjulian 280106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 281106321Sjulian sc->node = node; 282106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 283137138Sglebius ng_callout_init(&sc->intr_ch); 284137138Sglebius 285106266Sjulian return (0); 286106266Sjulian} 287106266Sjulian 288106266Sjulian/* 289106266Sjulian * Add a hook 290106266Sjulian */ 291106266Sjulianstatic int 292106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 293106266Sjulian{ 294144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 295106266Sjulian 296106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 297144674Sglebius sc->input = hook; 298106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 299144674Sglebius sc->output = hook; 300106266Sjulian sc->output_ifp = 0; 301106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 302106266Sjulian } else 303106266Sjulian return (EINVAL); 304144674Sglebius 305106266Sjulian return (0); 306106266Sjulian} 307106266Sjulian 308106266Sjulian/* 309144674Sglebius * Hook has been added 310144674Sglebius */ 311144674Sglebiusstatic int 312144674Sglebiusng_source_connect(hook_p hook) 313144674Sglebius{ 314144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 315144674Sglebius struct ng_mesg *msg; 316144674Sglebius int dummy_error = 0; 317144674Sglebius 318144674Sglebius /* 319144674Sglebius * If this is "output" hook, then request information 320144674Sglebius * from our downstream. 321144674Sglebius */ 322144674Sglebius if (hook == sc->output) { 323144674Sglebius NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME, 324144674Sglebius 0, M_NOWAIT); 325144674Sglebius if (msg == NULL) 326144674Sglebius return (ENOBUFS); 327144674Sglebius 328144674Sglebius /* 329144674Sglebius * Our hook and peer hook have HK_INVALID flag set, 330144674Sglebius * so we can't use NG_SEND_MSG_HOOK() macro here. 331144674Sglebius */ 332144674Sglebius NG_SEND_MSG_ID(dummy_error, sc->node, msg, 333144674Sglebius NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node)); 334144674Sglebius } 335144674Sglebius 336144674Sglebius return (0); 337144674Sglebius} 338144674Sglebius 339144674Sglebius/* 340106266Sjulian * Receive a control message 341106266Sjulian */ 342106266Sjulianstatic int 343106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 344106266Sjulian{ 345144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 346144674Sglebius struct ng_mesg *msg, *resp = NULL; 347106266Sjulian int error = 0; 348106266Sjulian 349106321Sjulian NGI_GET_MSG(item, msg); 350144674Sglebius 351106266Sjulian switch (msg->header.typecookie) { 352106266Sjulian case NGM_SOURCE_COOKIE: 353106435Sjulian if (msg->header.flags & NGF_RESP) { 354106435Sjulian error = EINVAL; 355106435Sjulian break; 356106435Sjulian } 357106266Sjulian switch (msg->header.cmd) { 358106266Sjulian case NGM_SOURCE_GET_STATS: 359106266Sjulian case NGM_SOURCE_CLR_STATS: 360106266Sjulian case NGM_SOURCE_GETCLR_STATS: 361106266Sjulian { 362106266Sjulian struct ng_source_stats *stats; 363106266Sjulian 364106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 365106266Sjulian NG_MKRESPONSE(resp, msg, 366106266Sjulian sizeof(*stats), M_NOWAIT); 367106266Sjulian if (resp == NULL) { 368106266Sjulian error = ENOMEM; 369106266Sjulian goto done; 370106266Sjulian } 371106266Sjulian sc->stats.queueOctets = sc->queueOctets; 372106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 373106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 374106266Sjulian && !timevalisset(&sc->stats.endTime)) { 375106319Sjulian getmicrotime(&sc->stats.elapsedTime); 376106266Sjulian timevalsub(&sc->stats.elapsedTime, 377106319Sjulian &sc->stats.startTime); 378106266Sjulian } 379106319Sjulian stats = (struct ng_source_stats *)resp->data; 380106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 381106266Sjulian } 382106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 383106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 384106266Sjulian } 385106266Sjulian break; 386106266Sjulian case NGM_SOURCE_START: 387106266Sjulian { 388144674Sglebius uint64_t packets; 389144674Sglebius 390144674Sglebius if (msg->header.arglen != sizeof(uint64_t)) { 391106266Sjulian error = EINVAL; 392106266Sjulian break; 393106266Sjulian } 394125033Sharti 395144674Sglebius packets = *(uint64_t *)msg->data; 396144674Sglebius 397144674Sglebius error = ng_source_start(sc, packets); 398144674Sglebius 399144674Sglebius break; 400125033Sharti } 401106266Sjulian case NGM_SOURCE_STOP: 402106266Sjulian ng_source_stop(sc); 403106266Sjulian break; 404106266Sjulian case NGM_SOURCE_CLR_DATA: 405106266Sjulian ng_source_clr_data(sc); 406106266Sjulian break; 407144674Sglebius case NGM_SOURCE_SETIFACE: 408144674Sglebius { 409144674Sglebius char *ifname = (char *)msg->data; 410144674Sglebius 411144674Sglebius if (msg->header.arglen < 2) { 412144674Sglebius error = EINVAL; 413144674Sglebius break; 414144674Sglebius } 415144674Sglebius 416144674Sglebius ng_source_store_output_ifp(sc, ifname); 417144674Sglebius break; 418144674Sglebius } 419153690Sglebius case NGM_SOURCE_SETPPS: 420153690Sglebius { 421153690Sglebius uint32_t pps; 422153690Sglebius 423153690Sglebius if (msg->header.arglen != sizeof(uint32_t)) { 424153690Sglebius error = EINVAL; 425153690Sglebius break; 426153690Sglebius } 427153690Sglebius 428153690Sglebius pps = *(uint32_t *)msg->data; 429153690Sglebius 430153690Sglebius sc->stats.maxPps = pps; 431153690Sglebius 432153690Sglebius break; 433153690Sglebius } 434167156Semaste case NGM_SOURCE_SET_TIMESTAMP: 435167156Semaste { 436167156Semaste struct ng_source_embed_info *embed; 437167156Semaste 438167168Semaste if (msg->header.arglen != sizeof(*embed)) { 439167168Semaste error = EINVAL; 440167168Semaste goto done; 441167168Semaste } 442167156Semaste embed = (struct ng_source_embed_info *)msg->data; 443167156Semaste bcopy(embed, &sc->embed_timestamp, sizeof(*embed)); 444167156Semaste 445167156Semaste break; 446167156Semaste } 447167156Semaste case NGM_SOURCE_GET_TIMESTAMP: 448167156Semaste { 449167156Semaste struct ng_source_embed_info *embed; 450167156Semaste 451167156Semaste NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT); 452167156Semaste if (resp == NULL) { 453167156Semaste error = ENOMEM; 454167156Semaste goto done; 455167156Semaste } 456167156Semaste embed = (struct ng_source_embed_info *)resp->data; 457167156Semaste bcopy(&sc->embed_timestamp, embed, sizeof(*embed)); 458167156Semaste 459167156Semaste break; 460167156Semaste } 461167160Semaste case NGM_SOURCE_SET_COUNTER: 462167160Semaste { 463167160Semaste struct ng_source_embed_cnt_info *embed; 464167160Semaste 465167168Semaste if (msg->header.arglen != sizeof(*embed)) { 466167168Semaste error = EINVAL; 467167168Semaste goto done; 468167168Semaste } 469167160Semaste embed = (struct ng_source_embed_cnt_info *)msg->data; 470167160Semaste if (embed->index >= NG_SOURCE_COUNTERS || 471167160Semaste !(embed->width == 1 || embed->width == 2 || 472167160Semaste embed->width == 4)) { 473167160Semaste error = EINVAL; 474167160Semaste goto done; 475167160Semaste } 476167160Semaste bcopy(embed, &sc->embed_counter[embed->index], 477167160Semaste sizeof(*embed)); 478167160Semaste 479167160Semaste break; 480167160Semaste } 481167160Semaste case NGM_SOURCE_GET_COUNTER: 482167160Semaste { 483167160Semaste uint8_t index = *(uint8_t *)msg->data; 484167160Semaste struct ng_source_embed_cnt_info *embed; 485167160Semaste 486167160Semaste if (index >= NG_SOURCE_COUNTERS) { 487167160Semaste error = EINVAL; 488167160Semaste goto done; 489167160Semaste } 490167160Semaste NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT); 491167160Semaste if (resp == NULL) { 492167160Semaste error = ENOMEM; 493167160Semaste goto done; 494167160Semaste } 495167160Semaste embed = (struct ng_source_embed_cnt_info *)resp->data; 496167160Semaste bcopy(&sc->embed_counter[index], embed, sizeof(*embed)); 497167160Semaste 498167160Semaste break; 499167160Semaste } 500106266Sjulian default: 501106266Sjulian error = EINVAL; 502106266Sjulian break; 503106266Sjulian } 504106266Sjulian break; 505106435Sjulian case NGM_ETHER_COOKIE: 506106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 507106435Sjulian error = EINVAL; 508106435Sjulian break; 509106435Sjulian } 510106435Sjulian switch (msg->header.cmd) { 511144674Sglebius case NGM_ETHER_GET_IFNAME: 512144674Sglebius { 513144674Sglebius char *ifname = (char *)msg->data; 514144674Sglebius 515144674Sglebius if (msg->header.arglen < 2) { 516144674Sglebius error = EINVAL; 517144674Sglebius break; 518144674Sglebius } 519144674Sglebius 520144674Sglebius if (ng_source_store_output_ifp(sc, ifname) == 0) 521106435Sjulian ng_source_set_autosrc(sc, 0); 522106435Sjulian break; 523144674Sglebius } 524106435Sjulian default: 525106435Sjulian error = EINVAL; 526106435Sjulian } 527106435Sjulian break; 528106266Sjulian default: 529106266Sjulian error = EINVAL; 530106266Sjulian break; 531106266Sjulian } 532106266Sjulian 533106266Sjuliandone: 534144674Sglebius /* Take care of synchronous response, if any. */ 535106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 536144674Sglebius /* Free the message and return. */ 537106321Sjulian NG_FREE_MSG(msg); 538106266Sjulian return (error); 539106266Sjulian} 540106266Sjulian 541106266Sjulian/* 542106266Sjulian * Receive data on a hook 543106266Sjulian * 544106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 545106266Sjulian * If data comes in the output hook, discard it. 546106266Sjulian */ 547106266Sjulianstatic int 548106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 549106266Sjulian{ 550144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 551144674Sglebius struct mbuf *m; 552106266Sjulian int error = 0; 553106266Sjulian 554106321Sjulian NGI_GET_M(item, m); 555106321Sjulian NG_FREE_ITEM(item); 556106266Sjulian 557106266Sjulian /* Which hook? */ 558144674Sglebius if (hook == sc->output) { 559106266Sjulian /* discard */ 560106321Sjulian NG_FREE_M(m); 561106266Sjulian return (error); 562106266Sjulian } 563144674Sglebius KASSERT(hook == sc->input, ("%s: no hook!", __func__)); 564106266Sjulian 565144674Sglebius /* Enqueue packet. */ 566106266Sjulian /* XXX should we check IF_QFULL() ? */ 567125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 568106266Sjulian sc->queueOctets += m->m_pkthdr.len; 569167160Semaste sc->last_packet = m; 570106266Sjulian 571106266Sjulian return (0); 572106266Sjulian} 573106266Sjulian 574106266Sjulian/* 575106266Sjulian * Shutdown processing 576106266Sjulian */ 577106266Sjulianstatic int 578106266Sjulianng_source_rmnode(node_p node) 579106266Sjulian{ 580144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 581106266Sjulian 582106266Sjulian ng_source_stop(sc); 583106266Sjulian ng_source_clr_data(sc); 584106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 585106321Sjulian NG_NODE_UNREF(node); 586125077Sharti free(sc, M_NETGRAPH); 587144674Sglebius 588106266Sjulian return (0); 589106266Sjulian} 590106266Sjulian 591106266Sjulian/* 592106266Sjulian * Hook disconnection 593106266Sjulian */ 594106266Sjulianstatic int 595106266Sjulianng_source_disconnect(hook_p hook) 596106266Sjulian{ 597106319Sjulian sc_p sc; 598106266Sjulian 599106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 600125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 601144674Sglebius if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output) 602106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 603106266Sjulian return (0); 604106266Sjulian} 605106266Sjulian 606106266Sjulian/* 607106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 608106435Sjulian * reached via our output hook. 609106435Sjulian */ 610106435Sjulianstatic int 611144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname) 612106435Sjulian{ 613106435Sjulian struct ifnet *ifp; 614106435Sjulian int s; 615106266Sjulian 616144674Sglebius ifp = ifunit(ifname); 617106266Sjulian 618106266Sjulian if (ifp == NULL) { 619183225Szec printf("%s: can't find interface %s\n", __func__, ifname); 620106266Sjulian return (EINVAL); 621106266Sjulian } 622106266Sjulian sc->output_ifp = ifp; 623106266Sjulian 624106266Sjulian#if 1 625106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 626106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 627106266Sjulian * interface with small packets. 628106266Sjulian * XXX we should restore the original value at stop or disconnect 629106266Sjulian */ 630106266Sjulian s = splimp(); /* XXX is this required? */ 631125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 632106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 633106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 634106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 635106266Sjulian } 636106266Sjulian splx(s); 637106266Sjulian#endif 638106266Sjulian return (0); 639106266Sjulian} 640106266Sjulian 641106266Sjulian/* 642106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 643106266Sjulian */ 644106266Sjulianstatic int 645144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag) 646106266Sjulian{ 647106266Sjulian struct ng_mesg *msg; 648106266Sjulian int error = 0; 649106266Sjulian 650106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 651144674Sglebius sizeof (uint32_t), M_NOWAIT); 652106266Sjulian if (msg == NULL) 653106266Sjulian return(ENOBUFS); 654106266Sjulian 655144674Sglebius *(uint32_t *)msg->data = flag; 656144674Sglebius NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0); 657106266Sjulian return (error); 658106266Sjulian} 659106266Sjulian 660106266Sjulian/* 661106266Sjulian * Clear out the data we've queued 662106266Sjulian */ 663106266Sjulianstatic void 664106266Sjulianng_source_clr_data (sc_p sc) 665106266Sjulian{ 666106266Sjulian struct mbuf *m; 667106266Sjulian 668106266Sjulian for (;;) { 669125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 670106266Sjulian if (m == NULL) 671106266Sjulian break; 672106266Sjulian NG_FREE_M(m); 673106266Sjulian } 674106266Sjulian sc->queueOctets = 0; 675167168Semaste sc->last_packet = NULL; 676106266Sjulian} 677106266Sjulian 678106266Sjulian/* 679106266Sjulian * Start sending queued data out the output hook 680106266Sjulian */ 681144674Sglebiusstatic int 682144674Sglebiusng_source_start(sc_p sc, uint64_t packets) 683106266Sjulian{ 684144674Sglebius if (sc->output_ifp == NULL) { 685144674Sglebius printf("ng_source: start without iface configured\n"); 686144674Sglebius return (ENXIO); 687144674Sglebius } 688144674Sglebius 689144674Sglebius if (sc->node->nd_flags & NG_SOURCE_ACTIVE) 690144674Sglebius return (EBUSY); 691144674Sglebius 692144674Sglebius sc->node->nd_flags |= NG_SOURCE_ACTIVE; 693144674Sglebius 694144674Sglebius sc->packets = packets; 695144674Sglebius timevalclear(&sc->stats.elapsedTime); 696144674Sglebius timevalclear(&sc->stats.endTime); 697144674Sglebius getmicrotime(&sc->stats.startTime); 698153690Sglebius getmicrotime(&sc->stats.lastTime); 699144674Sglebius ng_callout(&sc->intr_ch, sc->node, NULL, 0, 700144674Sglebius ng_source_intr, sc, 0); 701144674Sglebius 702144674Sglebius return (0); 703106266Sjulian} 704106266Sjulian 705106266Sjulian/* 706106266Sjulian * Stop sending queued data out the output hook 707106266Sjulian */ 708106266Sjulianstatic void 709144674Sglebiusng_source_stop(sc_p sc) 710106266Sjulian{ 711144674Sglebius ng_uncallout(&sc->intr_ch, sc->node); 712144674Sglebius sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 713144674Sglebius getmicrotime(&sc->stats.endTime); 714144674Sglebius sc->stats.elapsedTime = sc->stats.endTime; 715144674Sglebius timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 716106266Sjulian} 717106266Sjulian 718106266Sjulian/* 719106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 720106266Sjulian * Sends as many packets as the interface connected to our 721106266Sjulian * output hook is able to enqueue. 722106266Sjulian */ 723106266Sjulianstatic void 724125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 725106266Sjulian{ 726125243Sharti sc_p sc = (sc_p)arg1; 727106266Sjulian struct ifqueue *ifq; 728106266Sjulian int packets; 729106266Sjulian 730125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 731106266Sjulian 732144674Sglebius if (sc->packets == 0 || sc->output == NULL 733106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 734106266Sjulian ng_source_stop(sc); 735106266Sjulian return; 736106266Sjulian } 737106266Sjulian 738125033Sharti if (sc->output_ifp != NULL) { 739141745Sru ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 740125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 741125033Sharti } else 742125033Sharti packets = sc->snd_queue.ifq_len; 743125033Sharti 744153690Sglebius if (sc->stats.maxPps != 0) { 745153690Sglebius struct timeval now, elapsed; 746153690Sglebius uint64_t usec; 747153690Sglebius int maxpkt; 748153690Sglebius 749153690Sglebius getmicrotime(&now); 750153690Sglebius elapsed = now; 751153690Sglebius timevalsub(&elapsed, &sc->stats.lastTime); 752153690Sglebius usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; 753153690Sglebius maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; 754153690Sglebius sc->stats.lastTime = now; 755153690Sglebius if (packets > maxpkt) 756153690Sglebius packets = maxpkt; 757153690Sglebius } 758153690Sglebius 759106266Sjulian ng_source_send(sc, packets, NULL); 760125243Sharti if (sc->packets == 0) 761106266Sjulian ng_source_stop(sc); 762125243Sharti else 763138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 764137136Sglebius ng_source_intr, sc, 0); 765106266Sjulian} 766106266Sjulian 767106266Sjulian/* 768154707Sglebius * Send packets out our output hook. 769106266Sjulian */ 770106266Sjulianstatic int 771154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p) 772106266Sjulian{ 773106266Sjulian struct mbuf *m, *m2; 774154707Sglebius int sent; 775106266Sjulian int error = 0; 776106266Sjulian 777125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 778106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 779125077Sharti ("%s: inactive node", __func__)); 780106266Sjulian 781144674Sglebius if ((uint64_t)tosend > sc->packets) 782106266Sjulian tosend = sc->packets; 783106266Sjulian 784154707Sglebius /* Go through the queue sending packets one by one. */ 785106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 786125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 787106266Sjulian if (m == NULL) 788106266Sjulian break; 789106266Sjulian 790167156Semaste /* Duplicate and modify the packet. */ 791167156Semaste error = ng_source_dup_mod(sc, m, &m2); 792167156Semaste if (error) { 793167156Semaste if (error == ENOBUFS) 794167156Semaste _IF_PREPEND(&sc->snd_queue, m); 795167156Semaste else 796167156Semaste _IF_ENQUEUE(&sc->snd_queue, m); 797106266Sjulian break; 798106266Sjulian } 799106266Sjulian 800144674Sglebius /* Re-enqueue the original packet for us. */ 801125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 802106266Sjulian 803154707Sglebius sc->stats.outFrames++; 804154707Sglebius sc->stats.outOctets += m2->m_pkthdr.len; 805154707Sglebius NG_SEND_DATA_ONLY(error, sc->output, m2); 806154707Sglebius if (error) 807106266Sjulian break; 808106266Sjulian } 809106266Sjulian 810106266Sjulian sc->packets -= sent; 811106266Sjulian if (sent_p != NULL) 812106266Sjulian *sent_p = sent; 813106266Sjulian return (error); 814106266Sjulian} 815167156Semaste 816167156Semaste/* 817167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset' 818167156Semaste * to data in 'cp'. 819167156Semaste * 820167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf. 821167156Semaste */ 822167156Semastestatic void 823167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp, 824167156Semaste int flags) 825167156Semaste{ 826167156Semaste if (len == 0) 827167156Semaste return; 828167156Semaste 829167156Semaste /* Can't modify beyond end of packet. */ 830167156Semaste /* TODO: Pad packet for this case. */ 831167156Semaste if (offset + len > m->m_len) 832167156Semaste return; 833167156Semaste 834167156Semaste bcopy(cp, mtod_off(m, offset, caddr_t), len); 835167156Semaste} 836167156Semaste 837167160Semastestatic void 838167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt, 839167160Semaste struct mbuf *m, int increment) 840167160Semaste{ 841167160Semaste caddr_t cp; 842167160Semaste uint32_t val; 843167160Semaste 844167160Semaste val = htonl(cnt->next_val); 845167160Semaste cp = (caddr_t)&val + sizeof(val) - cnt->width; 846167160Semaste ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags); 847167160Semaste 848167160Semaste if (increment) { 849167160Semaste cnt->next_val += increment; 850167160Semaste 851167160Semaste if (increment > 0 && cnt->next_val > cnt->max_val) { 852167160Semaste cnt->next_val = cnt->min_val - 1 + 853167160Semaste (cnt->next_val - cnt->max_val); 854167160Semaste if (cnt->next_val > cnt->max_val) 855167160Semaste cnt->next_val = cnt->max_val; 856167160Semaste } else if (increment < 0 && cnt->next_val < cnt->min_val) { 857167160Semaste cnt->next_val = cnt->max_val + 1 + 858167160Semaste (cnt->next_val - cnt->min_val); 859167160Semaste if (cnt->next_val < cnt->min_val) 860167160Semaste cnt->next_val = cnt->max_val; 861167160Semaste } 862167160Semaste } 863167160Semaste} 864167160Semaste 865167156Semastestatic int 866167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr) 867167156Semaste{ 868167156Semaste struct mbuf *m; 869167160Semaste struct ng_source_embed_cnt_info *cnt; 870167156Semaste struct ng_source_embed_info *ts; 871167156Semaste int modify; 872167156Semaste int error = 0; 873167160Semaste int i, increment; 874167156Semaste 875167156Semaste /* Are we going to modify packets? */ 876167156Semaste modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE; 877167160Semaste for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i) 878167160Semaste modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE; 879167156Semaste 880167156Semaste /* Duplicate the packet. */ 881167156Semaste if (modify) 882167156Semaste m = m_dup(m0, M_DONTWAIT); 883167156Semaste else 884167156Semaste m = m_copypacket(m0, M_DONTWAIT); 885167156Semaste if (m == NULL) { 886167156Semaste error = ENOBUFS; 887167156Semaste goto done; 888167156Semaste } 889167156Semaste *m_ptr = m; 890167156Semaste 891167156Semaste if (!modify) 892167156Semaste goto done; 893167156Semaste 894167156Semaste /* Modify the copied packet for sending. */ 895167156Semaste KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__)); 896167156Semaste 897167160Semaste for (i = 0; i < NG_SOURCE_COUNTERS; ++i) { 898167160Semaste cnt = &sc->embed_counter[i]; 899167160Semaste if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) { 900167160Semaste if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 || 901167160Semaste sc->last_packet == m0) 902167160Semaste increment = cnt->increment; 903167160Semaste else 904167160Semaste increment = 0; 905167160Semaste ng_source_mod_counter(sc, cnt, m, increment); 906167160Semaste } 907167160Semaste } 908167160Semaste 909167156Semaste ts = &sc->embed_timestamp; 910167156Semaste if (ts->flags & NGM_SOURCE_EMBED_ENABLE) { 911167156Semaste struct timeval now; 912167156Semaste getmicrotime(&now); 913167156Semaste now.tv_sec = htonl(now.tv_sec); 914167156Semaste now.tv_usec = htonl(now.tv_usec); 915167156Semaste ng_source_packet_mod(sc, m, ts->offset, sizeof (now), 916167156Semaste (caddr_t)&now, ts->flags); 917167156Semaste } 918167156Semaste 919167156Semastedone: 920167156Semaste return(error); 921167156Semaste} 922