ng_source.c revision 167160
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 * 38106266Sjulian * Author: Dave Chapeskie <dchapeskie@sandvine.com> 39106266Sjulian */ 40106266Sjulian 41125077Sharti#include <sys/cdefs.h> 42125077Sharti__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 167160 2007-03-02 01:44:04Z 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> 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 275125030Sharti sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 276106266Sjulian if (sc == NULL) 277106266Sjulian return (ENOMEM); 278106266Sjulian 279106321Sjulian NG_NODE_SET_PRIVATE(node, sc); 280106321Sjulian sc->node = node; 281106266Sjulian sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */ 282137138Sglebius ng_callout_init(&sc->intr_ch); 283137138Sglebius 284106266Sjulian return (0); 285106266Sjulian} 286106266Sjulian 287106266Sjulian/* 288106266Sjulian * Add a hook 289106266Sjulian */ 290106266Sjulianstatic int 291106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name) 292106266Sjulian{ 293144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 294106266Sjulian 295106266Sjulian if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) { 296144674Sglebius sc->input = hook; 297106266Sjulian } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) { 298144674Sglebius sc->output = hook; 299106266Sjulian sc->output_ifp = 0; 300106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 301106266Sjulian } else 302106266Sjulian return (EINVAL); 303144674Sglebius 304106266Sjulian return (0); 305106266Sjulian} 306106266Sjulian 307106266Sjulian/* 308144674Sglebius * Hook has been added 309144674Sglebius */ 310144674Sglebiusstatic int 311144674Sglebiusng_source_connect(hook_p hook) 312144674Sglebius{ 313144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 314144674Sglebius struct ng_mesg *msg; 315144674Sglebius int dummy_error = 0; 316144674Sglebius 317144674Sglebius /* 318144674Sglebius * If this is "output" hook, then request information 319144674Sglebius * from our downstream. 320144674Sglebius */ 321144674Sglebius if (hook == sc->output) { 322144674Sglebius NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME, 323144674Sglebius 0, M_NOWAIT); 324144674Sglebius if (msg == NULL) 325144674Sglebius return (ENOBUFS); 326144674Sglebius 327144674Sglebius /* 328144674Sglebius * Our hook and peer hook have HK_INVALID flag set, 329144674Sglebius * so we can't use NG_SEND_MSG_HOOK() macro here. 330144674Sglebius */ 331144674Sglebius NG_SEND_MSG_ID(dummy_error, sc->node, msg, 332144674Sglebius NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node)); 333144674Sglebius } 334144674Sglebius 335144674Sglebius return (0); 336144674Sglebius} 337144674Sglebius 338144674Sglebius/* 339106266Sjulian * Receive a control message 340106266Sjulian */ 341106266Sjulianstatic int 342106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) 343106266Sjulian{ 344144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 345144674Sglebius struct ng_mesg *msg, *resp = NULL; 346106266Sjulian int error = 0; 347106266Sjulian 348106321Sjulian NGI_GET_MSG(item, msg); 349144674Sglebius 350106266Sjulian switch (msg->header.typecookie) { 351106266Sjulian case NGM_SOURCE_COOKIE: 352106435Sjulian if (msg->header.flags & NGF_RESP) { 353106435Sjulian error = EINVAL; 354106435Sjulian break; 355106435Sjulian } 356106266Sjulian switch (msg->header.cmd) { 357106266Sjulian case NGM_SOURCE_GET_STATS: 358106266Sjulian case NGM_SOURCE_CLR_STATS: 359106266Sjulian case NGM_SOURCE_GETCLR_STATS: 360106266Sjulian { 361106266Sjulian struct ng_source_stats *stats; 362106266Sjulian 363106266Sjulian if (msg->header.cmd != NGM_SOURCE_CLR_STATS) { 364106266Sjulian NG_MKRESPONSE(resp, msg, 365106266Sjulian sizeof(*stats), M_NOWAIT); 366106266Sjulian if (resp == NULL) { 367106266Sjulian error = ENOMEM; 368106266Sjulian goto done; 369106266Sjulian } 370106266Sjulian sc->stats.queueOctets = sc->queueOctets; 371106319Sjulian sc->stats.queueFrames = sc->snd_queue.ifq_len; 372106321Sjulian if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) 373106266Sjulian && !timevalisset(&sc->stats.endTime)) { 374106319Sjulian getmicrotime(&sc->stats.elapsedTime); 375106266Sjulian timevalsub(&sc->stats.elapsedTime, 376106319Sjulian &sc->stats.startTime); 377106266Sjulian } 378106319Sjulian stats = (struct ng_source_stats *)resp->data; 379106266Sjulian bcopy(&sc->stats, stats, sizeof(* stats)); 380106266Sjulian } 381106266Sjulian if (msg->header.cmd != NGM_SOURCE_GET_STATS) 382106266Sjulian bzero(&sc->stats, sizeof(sc->stats)); 383106266Sjulian } 384106266Sjulian break; 385106266Sjulian case NGM_SOURCE_START: 386106266Sjulian { 387144674Sglebius uint64_t packets; 388144674Sglebius 389144674Sglebius if (msg->header.arglen != sizeof(uint64_t)) { 390106266Sjulian error = EINVAL; 391106266Sjulian break; 392106266Sjulian } 393125033Sharti 394144674Sglebius packets = *(uint64_t *)msg->data; 395144674Sglebius 396144674Sglebius error = ng_source_start(sc, packets); 397144674Sglebius 398144674Sglebius break; 399125033Sharti } 400106266Sjulian case NGM_SOURCE_STOP: 401106266Sjulian ng_source_stop(sc); 402106266Sjulian break; 403106266Sjulian case NGM_SOURCE_CLR_DATA: 404106266Sjulian ng_source_clr_data(sc); 405106266Sjulian break; 406144674Sglebius case NGM_SOURCE_SETIFACE: 407144674Sglebius { 408144674Sglebius char *ifname = (char *)msg->data; 409144674Sglebius 410144674Sglebius if (msg->header.arglen < 2) { 411144674Sglebius error = EINVAL; 412144674Sglebius break; 413144674Sglebius } 414144674Sglebius 415144674Sglebius ng_source_store_output_ifp(sc, ifname); 416144674Sglebius break; 417144674Sglebius } 418153690Sglebius case NGM_SOURCE_SETPPS: 419153690Sglebius { 420153690Sglebius uint32_t pps; 421153690Sglebius 422153690Sglebius if (msg->header.arglen != sizeof(uint32_t)) { 423153690Sglebius error = EINVAL; 424153690Sglebius break; 425153690Sglebius } 426153690Sglebius 427153690Sglebius pps = *(uint32_t *)msg->data; 428153690Sglebius 429153690Sglebius sc->stats.maxPps = pps; 430153690Sglebius 431153690Sglebius break; 432153690Sglebius } 433167156Semaste case NGM_SOURCE_SET_TIMESTAMP: 434167156Semaste { 435167156Semaste struct ng_source_embed_info *embed; 436167156Semaste 437167156Semaste embed = (struct ng_source_embed_info *)msg->data; 438167156Semaste bcopy(embed, &sc->embed_timestamp, sizeof(*embed)); 439167156Semaste 440167156Semaste break; 441167156Semaste } 442167156Semaste case NGM_SOURCE_GET_TIMESTAMP: 443167156Semaste { 444167156Semaste struct ng_source_embed_info *embed; 445167156Semaste 446167156Semaste NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT); 447167156Semaste if (resp == NULL) { 448167156Semaste error = ENOMEM; 449167156Semaste goto done; 450167156Semaste } 451167156Semaste embed = (struct ng_source_embed_info *)resp->data; 452167156Semaste bcopy(&sc->embed_timestamp, embed, sizeof(*embed)); 453167156Semaste 454167156Semaste break; 455167156Semaste } 456167160Semaste case NGM_SOURCE_SET_COUNTER: 457167160Semaste { 458167160Semaste struct ng_source_embed_cnt_info *embed; 459167160Semaste 460167160Semaste embed = (struct ng_source_embed_cnt_info *)msg->data; 461167160Semaste if (embed->index >= NG_SOURCE_COUNTERS || 462167160Semaste !(embed->width == 1 || embed->width == 2 || 463167160Semaste embed->width == 4)) { 464167160Semaste error = EINVAL; 465167160Semaste goto done; 466167160Semaste } 467167160Semaste bcopy(embed, &sc->embed_counter[embed->index], 468167160Semaste sizeof(*embed)); 469167160Semaste 470167160Semaste break; 471167160Semaste } 472167160Semaste case NGM_SOURCE_GET_COUNTER: 473167160Semaste { 474167160Semaste uint8_t index = *(uint8_t *)msg->data; 475167160Semaste struct ng_source_embed_cnt_info *embed; 476167160Semaste 477167160Semaste if (index >= NG_SOURCE_COUNTERS) { 478167160Semaste error = EINVAL; 479167160Semaste goto done; 480167160Semaste } 481167160Semaste NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT); 482167160Semaste if (resp == NULL) { 483167160Semaste error = ENOMEM; 484167160Semaste goto done; 485167160Semaste } 486167160Semaste embed = (struct ng_source_embed_cnt_info *)resp->data; 487167160Semaste bcopy(&sc->embed_counter[index], embed, sizeof(*embed)); 488167160Semaste 489167160Semaste break; 490167160Semaste } 491106266Sjulian default: 492106266Sjulian error = EINVAL; 493106266Sjulian break; 494106266Sjulian } 495106266Sjulian break; 496106435Sjulian case NGM_ETHER_COOKIE: 497106435Sjulian if (!(msg->header.flags & NGF_RESP)) { 498106435Sjulian error = EINVAL; 499106435Sjulian break; 500106435Sjulian } 501106435Sjulian switch (msg->header.cmd) { 502144674Sglebius case NGM_ETHER_GET_IFNAME: 503144674Sglebius { 504144674Sglebius char *ifname = (char *)msg->data; 505144674Sglebius 506144674Sglebius if (msg->header.arglen < 2) { 507144674Sglebius error = EINVAL; 508144674Sglebius break; 509144674Sglebius } 510144674Sglebius 511144674Sglebius if (ng_source_store_output_ifp(sc, ifname) == 0) 512106435Sjulian ng_source_set_autosrc(sc, 0); 513106435Sjulian break; 514144674Sglebius } 515106435Sjulian default: 516106435Sjulian error = EINVAL; 517106435Sjulian } 518106435Sjulian break; 519106266Sjulian default: 520106266Sjulian error = EINVAL; 521106266Sjulian break; 522106266Sjulian } 523106266Sjulian 524106266Sjuliandone: 525144674Sglebius /* Take care of synchronous response, if any. */ 526106321Sjulian NG_RESPOND_MSG(error, node, item, resp); 527144674Sglebius /* Free the message and return. */ 528106321Sjulian NG_FREE_MSG(msg); 529106266Sjulian return (error); 530106266Sjulian} 531106266Sjulian 532106266Sjulian/* 533106266Sjulian * Receive data on a hook 534106266Sjulian * 535106266Sjulian * If data comes in the input hook, enqueue it on the send queue. 536106266Sjulian * If data comes in the output hook, discard it. 537106266Sjulian */ 538106266Sjulianstatic int 539106321Sjulianng_source_rcvdata(hook_p hook, item_p item) 540106266Sjulian{ 541144674Sglebius sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 542144674Sglebius struct mbuf *m; 543106266Sjulian int error = 0; 544106266Sjulian 545106321Sjulian NGI_GET_M(item, m); 546106321Sjulian NG_FREE_ITEM(item); 547106266Sjulian 548106266Sjulian /* Which hook? */ 549144674Sglebius if (hook == sc->output) { 550106266Sjulian /* discard */ 551106321Sjulian NG_FREE_M(m); 552106266Sjulian return (error); 553106266Sjulian } 554144674Sglebius KASSERT(hook == sc->input, ("%s: no hook!", __func__)); 555106266Sjulian 556144674Sglebius /* Enqueue packet. */ 557106266Sjulian /* XXX should we check IF_QFULL() ? */ 558125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 559106266Sjulian sc->queueOctets += m->m_pkthdr.len; 560167160Semaste sc->last_packet = m; 561106266Sjulian 562106266Sjulian return (0); 563106266Sjulian} 564106266Sjulian 565106266Sjulian/* 566106266Sjulian * Shutdown processing 567106266Sjulian */ 568106266Sjulianstatic int 569106266Sjulianng_source_rmnode(node_p node) 570106266Sjulian{ 571144674Sglebius sc_p sc = NG_NODE_PRIVATE(node); 572106266Sjulian 573106266Sjulian ng_source_stop(sc); 574106266Sjulian ng_source_clr_data(sc); 575106321Sjulian NG_NODE_SET_PRIVATE(node, NULL); 576106321Sjulian NG_NODE_UNREF(node); 577125077Sharti free(sc, M_NETGRAPH); 578144674Sglebius 579106266Sjulian return (0); 580106266Sjulian} 581106266Sjulian 582106266Sjulian/* 583106266Sjulian * Hook disconnection 584106266Sjulian */ 585106266Sjulianstatic int 586106266Sjulianng_source_disconnect(hook_p hook) 587106266Sjulian{ 588106319Sjulian sc_p sc; 589106266Sjulian 590106321Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 591125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 592144674Sglebius if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output) 593106321Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 594106266Sjulian return (0); 595106266Sjulian} 596106266Sjulian 597106266Sjulian/* 598106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface 599106435Sjulian * reached via our output hook. 600106435Sjulian */ 601106435Sjulianstatic int 602144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname) 603106435Sjulian{ 604106435Sjulian struct ifnet *ifp; 605106435Sjulian int s; 606106266Sjulian 607144674Sglebius ifp = ifunit(ifname); 608106266Sjulian 609106266Sjulian if (ifp == NULL) { 610125077Sharti printf("%s: can't find interface %d\n", __func__, if_index); 611106266Sjulian return (EINVAL); 612106266Sjulian } 613106266Sjulian sc->output_ifp = ifp; 614106266Sjulian 615106266Sjulian#if 1 616106266Sjulian /* XXX mucking with a drivers ifqueue size is ugly but we need it 617106266Sjulian * to queue a lot of packets to get close to line rate on a gigabit 618106266Sjulian * interface with small packets. 619106266Sjulian * XXX we should restore the original value at stop or disconnect 620106266Sjulian */ 621106266Sjulian s = splimp(); /* XXX is this required? */ 622125077Sharti if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) { 623106266Sjulian printf("ng_source: changing ifq_maxlen from %d to %d\n", 624106319Sjulian ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN); 625106266Sjulian ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN; 626106266Sjulian } 627106266Sjulian splx(s); 628106266Sjulian#endif 629106266Sjulian return (0); 630106266Sjulian} 631106266Sjulian 632106266Sjulian/* 633106266Sjulian * Set the attached ethernet node's ethernet source address override flag. 634106266Sjulian */ 635106266Sjulianstatic int 636144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag) 637106266Sjulian{ 638106266Sjulian struct ng_mesg *msg; 639106266Sjulian int error = 0; 640106266Sjulian 641106266Sjulian NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC, 642144674Sglebius sizeof (uint32_t), M_NOWAIT); 643106266Sjulian if (msg == NULL) 644106266Sjulian return(ENOBUFS); 645106266Sjulian 646144674Sglebius *(uint32_t *)msg->data = flag; 647144674Sglebius NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0); 648106266Sjulian return (error); 649106266Sjulian} 650106266Sjulian 651106266Sjulian/* 652106266Sjulian * Clear out the data we've queued 653106266Sjulian */ 654106266Sjulianstatic void 655106266Sjulianng_source_clr_data (sc_p sc) 656106266Sjulian{ 657106266Sjulian struct mbuf *m; 658106266Sjulian 659106266Sjulian for (;;) { 660125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 661106266Sjulian if (m == NULL) 662106266Sjulian break; 663106266Sjulian NG_FREE_M(m); 664106266Sjulian } 665106266Sjulian sc->queueOctets = 0; 666167160Semaste sc->last_packet = 0; 667106266Sjulian} 668106266Sjulian 669106266Sjulian/* 670106266Sjulian * Start sending queued data out the output hook 671106266Sjulian */ 672144674Sglebiusstatic int 673144674Sglebiusng_source_start(sc_p sc, uint64_t packets) 674106266Sjulian{ 675144674Sglebius if (sc->output_ifp == NULL) { 676144674Sglebius printf("ng_source: start without iface configured\n"); 677144674Sglebius return (ENXIO); 678144674Sglebius } 679144674Sglebius 680144674Sglebius if (sc->node->nd_flags & NG_SOURCE_ACTIVE) 681144674Sglebius return (EBUSY); 682144674Sglebius 683144674Sglebius sc->node->nd_flags |= NG_SOURCE_ACTIVE; 684144674Sglebius 685144674Sglebius sc->packets = packets; 686144674Sglebius timevalclear(&sc->stats.elapsedTime); 687144674Sglebius timevalclear(&sc->stats.endTime); 688144674Sglebius getmicrotime(&sc->stats.startTime); 689153690Sglebius getmicrotime(&sc->stats.lastTime); 690144674Sglebius ng_callout(&sc->intr_ch, sc->node, NULL, 0, 691144674Sglebius ng_source_intr, sc, 0); 692144674Sglebius 693144674Sglebius return (0); 694106266Sjulian} 695106266Sjulian 696106266Sjulian/* 697106266Sjulian * Stop sending queued data out the output hook 698106266Sjulian */ 699106266Sjulianstatic void 700144674Sglebiusng_source_stop(sc_p sc) 701106266Sjulian{ 702144674Sglebius ng_uncallout(&sc->intr_ch, sc->node); 703144674Sglebius sc->node->nd_flags &= ~NG_SOURCE_ACTIVE; 704144674Sglebius getmicrotime(&sc->stats.endTime); 705144674Sglebius sc->stats.elapsedTime = sc->stats.endTime; 706144674Sglebius timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime); 707106266Sjulian} 708106266Sjulian 709106266Sjulian/* 710106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks. 711106266Sjulian * Sends as many packets as the interface connected to our 712106266Sjulian * output hook is able to enqueue. 713106266Sjulian */ 714106266Sjulianstatic void 715125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2) 716106266Sjulian{ 717125243Sharti sc_p sc = (sc_p)arg1; 718106266Sjulian struct ifqueue *ifq; 719106266Sjulian int packets; 720106266Sjulian 721125077Sharti KASSERT(sc != NULL, ("%s: null node private", __func__)); 722106266Sjulian 723144674Sglebius if (sc->packets == 0 || sc->output == NULL 724106321Sjulian || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) { 725106266Sjulian ng_source_stop(sc); 726106266Sjulian return; 727106266Sjulian } 728106266Sjulian 729125033Sharti if (sc->output_ifp != NULL) { 730141745Sru ifq = (struct ifqueue *)&sc->output_ifp->if_snd; 731125033Sharti packets = ifq->ifq_maxlen - ifq->ifq_len; 732125033Sharti } else 733125033Sharti packets = sc->snd_queue.ifq_len; 734125033Sharti 735153690Sglebius if (sc->stats.maxPps != 0) { 736153690Sglebius struct timeval now, elapsed; 737153690Sglebius uint64_t usec; 738153690Sglebius int maxpkt; 739153690Sglebius 740153690Sglebius getmicrotime(&now); 741153690Sglebius elapsed = now; 742153690Sglebius timevalsub(&elapsed, &sc->stats.lastTime); 743153690Sglebius usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; 744153690Sglebius maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; 745153690Sglebius sc->stats.lastTime = now; 746153690Sglebius if (packets > maxpkt) 747153690Sglebius packets = maxpkt; 748153690Sglebius } 749153690Sglebius 750106266Sjulian ng_source_send(sc, packets, NULL); 751125243Sharti if (sc->packets == 0) 752106266Sjulian ng_source_stop(sc); 753125243Sharti else 754138268Sglebius ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS, 755137136Sglebius ng_source_intr, sc, 0); 756106266Sjulian} 757106266Sjulian 758106266Sjulian/* 759154707Sglebius * Send packets out our output hook. 760106266Sjulian */ 761106266Sjulianstatic int 762154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p) 763106266Sjulian{ 764106266Sjulian struct mbuf *m, *m2; 765154707Sglebius int sent; 766106266Sjulian int error = 0; 767106266Sjulian 768125077Sharti KASSERT(tosend >= 0, ("%s: negative tosend param", __func__)); 769106321Sjulian KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE, 770125077Sharti ("%s: inactive node", __func__)); 771106266Sjulian 772144674Sglebius if ((uint64_t)tosend > sc->packets) 773106266Sjulian tosend = sc->packets; 774106266Sjulian 775154707Sglebius /* Go through the queue sending packets one by one. */ 776106266Sjulian for (sent = 0; error == 0 && sent < tosend; ++sent) { 777125031Sharti _IF_DEQUEUE(&sc->snd_queue, m); 778106266Sjulian if (m == NULL) 779106266Sjulian break; 780106266Sjulian 781167156Semaste /* Duplicate and modify the packet. */ 782167156Semaste error = ng_source_dup_mod(sc, m, &m2); 783167156Semaste if (error) { 784167156Semaste if (error == ENOBUFS) 785167156Semaste _IF_PREPEND(&sc->snd_queue, m); 786167156Semaste else 787167156Semaste _IF_ENQUEUE(&sc->snd_queue, m); 788106266Sjulian break; 789106266Sjulian } 790106266Sjulian 791144674Sglebius /* Re-enqueue the original packet for us. */ 792125031Sharti _IF_ENQUEUE(&sc->snd_queue, m); 793106266Sjulian 794154707Sglebius sc->stats.outFrames++; 795154707Sglebius sc->stats.outOctets += m2->m_pkthdr.len; 796154707Sglebius NG_SEND_DATA_ONLY(error, sc->output, m2); 797154707Sglebius if (error) 798106266Sjulian break; 799106266Sjulian } 800106266Sjulian 801106266Sjulian sc->packets -= sent; 802106266Sjulian if (sent_p != NULL) 803106266Sjulian *sent_p = sent; 804106266Sjulian return (error); 805106266Sjulian} 806167156Semaste 807167156Semaste/* 808167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset' 809167156Semaste * to data in 'cp'. 810167156Semaste * 811167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf. 812167156Semaste */ 813167156Semastestatic void 814167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp, 815167156Semaste int flags) 816167156Semaste{ 817167156Semaste if (len == 0) 818167156Semaste return; 819167156Semaste 820167156Semaste /* Can't modify beyond end of packet. */ 821167156Semaste /* TODO: Pad packet for this case. */ 822167156Semaste if (offset + len > m->m_len) 823167156Semaste return; 824167156Semaste 825167156Semaste bcopy(cp, mtod_off(m, offset, caddr_t), len); 826167156Semaste} 827167156Semaste 828167160Semastestatic void 829167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt, 830167160Semaste struct mbuf *m, int increment) 831167160Semaste{ 832167160Semaste caddr_t cp; 833167160Semaste uint32_t val; 834167160Semaste 835167160Semaste val = htonl(cnt->next_val); 836167160Semaste cp = (caddr_t)&val + sizeof(val) - cnt->width; 837167160Semaste ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags); 838167160Semaste 839167160Semaste if (increment) { 840167160Semaste cnt->next_val += increment; 841167160Semaste 842167160Semaste if (increment > 0 && cnt->next_val > cnt->max_val) { 843167160Semaste cnt->next_val = cnt->min_val - 1 + 844167160Semaste (cnt->next_val - cnt->max_val); 845167160Semaste if (cnt->next_val > cnt->max_val) 846167160Semaste cnt->next_val = cnt->max_val; 847167160Semaste } else if (increment < 0 && cnt->next_val < cnt->min_val) { 848167160Semaste cnt->next_val = cnt->max_val + 1 + 849167160Semaste (cnt->next_val - cnt->min_val); 850167160Semaste if (cnt->next_val < cnt->min_val) 851167160Semaste cnt->next_val = cnt->max_val; 852167160Semaste } 853167160Semaste } 854167160Semaste} 855167160Semaste 856167156Semastestatic int 857167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr) 858167156Semaste{ 859167156Semaste struct mbuf *m; 860167160Semaste struct ng_source_embed_cnt_info *cnt; 861167156Semaste struct ng_source_embed_info *ts; 862167156Semaste int modify; 863167156Semaste int error = 0; 864167160Semaste int i, increment; 865167156Semaste 866167156Semaste /* Are we going to modify packets? */ 867167156Semaste modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE; 868167160Semaste for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i) 869167160Semaste modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE; 870167156Semaste 871167156Semaste /* Duplicate the packet. */ 872167156Semaste if (modify) 873167156Semaste m = m_dup(m0, M_DONTWAIT); 874167156Semaste else 875167156Semaste m = m_copypacket(m0, M_DONTWAIT); 876167156Semaste if (m == NULL) { 877167156Semaste error = ENOBUFS; 878167156Semaste goto done; 879167156Semaste } 880167156Semaste *m_ptr = m; 881167156Semaste 882167156Semaste if (!modify) 883167156Semaste goto done; 884167156Semaste 885167156Semaste /* Modify the copied packet for sending. */ 886167156Semaste KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__)); 887167156Semaste 888167160Semaste for (i = 0; i < NG_SOURCE_COUNTERS; ++i) { 889167160Semaste cnt = &sc->embed_counter[i]; 890167160Semaste if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) { 891167160Semaste if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 || 892167160Semaste sc->last_packet == m0) 893167160Semaste increment = cnt->increment; 894167160Semaste else 895167160Semaste increment = 0; 896167160Semaste ng_source_mod_counter(sc, cnt, m, increment); 897167160Semaste } 898167160Semaste } 899167160Semaste 900167156Semaste ts = &sc->embed_timestamp; 901167156Semaste if (ts->flags & NGM_SOURCE_EMBED_ENABLE) { 902167156Semaste struct timeval now; 903167156Semaste getmicrotime(&now); 904167156Semaste now.tv_sec = htonl(now.tv_sec); 905167156Semaste now.tv_usec = htonl(now.tv_usec); 906167156Semaste ng_source_packet_mod(sc, m, ts->offset, sizeof (now), 907167156Semaste (caddr_t)&now, ts->flags); 908167156Semaste } 909167156Semaste 910167156Semastedone: 911167156Semaste return(error); 912167156Semaste} 913