1206360Sjoel/*- 2215800Szec * Copyright (c) 2004-2010 University of Zagreb 3182734Sjulian * Copyright (c) 2007-2008 FreeBSD Foundation 4182734Sjulian * 5182734Sjulian * This software was developed by the University of Zagreb and the 6182734Sjulian * FreeBSD Foundation under sponsorship by the Stichting NLnet and the 7182734Sjulian * FreeBSD Foundation. 8182734Sjulian * 9182734Sjulian * Redistribution and use in source and binary forms, with or without 10182734Sjulian * modification, are permitted provided that the following conditions 11182734Sjulian * are met: 12182734Sjulian * 1. Redistributions of source code must retain the above copyright 13182734Sjulian * notice, this list of conditions and the following disclaimer. 14182734Sjulian * 2. Redistributions in binary form must reproduce the above copyright 15182734Sjulian * notice, this list of conditions and the following disclaimer in the 16182734Sjulian * documentation and/or other materials provided with the distribution. 17182734Sjulian * 18182734Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19182734Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20182734Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21182734Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22182734Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23182734Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24182734Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25182734Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26182734Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27182734Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28182734Sjulian * SUCH DAMAGE. 29182734Sjulian * 30182734Sjulian * $FreeBSD$ 31182734Sjulian */ 32182734Sjulian 33182734Sjulian/* 34182734Sjulian * This node permits simple traffic shaping by emulating bandwidth 35182734Sjulian * and delay, as well as random packet losses. 36182734Sjulian * The node has two hooks, upper and lower. Traffic flowing from upper to 37182734Sjulian * lower hook is referenced as downstream, and vice versa. Parameters for 38182734Sjulian * both directions can be set separately, except for delay. 39182734Sjulian */ 40182734Sjulian 41182734Sjulian 42182734Sjulian#include <sys/param.h> 43182734Sjulian#include <sys/errno.h> 44182734Sjulian#include <sys/systm.h> 45182734Sjulian#include <sys/kernel.h> 46182734Sjulian#include <sys/malloc.h> 47182734Sjulian#include <sys/mbuf.h> 48182734Sjulian#include <sys/time.h> 49182734Sjulian 50182734Sjulian#include <vm/uma.h> 51182734Sjulian 52196019Srwatson#include <net/vnet.h> 53196019Srwatson 54182734Sjulian#include <netinet/in.h> 55182734Sjulian#include <netinet/in_systm.h> 56182734Sjulian#include <netinet/ip.h> 57182734Sjulian 58182734Sjulian#include <netgraph/ng_message.h> 59182734Sjulian#include <netgraph/netgraph.h> 60182734Sjulian#include <netgraph/ng_parse.h> 61182734Sjulian#include <netgraph/ng_pipe.h> 62182734Sjulian 63182734Sjulianstatic MALLOC_DEFINE(M_NG_PIPE, "ng_pipe", "ng_pipe"); 64182734Sjulian 65182734Sjulian/* Packet header struct */ 66182734Sjulianstruct ngp_hdr { 67182734Sjulian TAILQ_ENTRY(ngp_hdr) ngp_link; /* next pkt in queue */ 68182734Sjulian struct timeval when; /* this packet's due time */ 69182734Sjulian struct mbuf *m; /* ptr to the packet data */ 70182734Sjulian}; 71182734SjulianTAILQ_HEAD(p_head, ngp_hdr); 72182734Sjulian 73182734Sjulian/* FIFO queue struct */ 74182734Sjulianstruct ngp_fifo { 75182734Sjulian TAILQ_ENTRY(ngp_fifo) fifo_le; /* list of active queues only */ 76182734Sjulian struct p_head packet_head; /* FIFO queue head */ 77182734Sjulian u_int32_t hash; /* flow signature */ 78182734Sjulian struct timeval vtime; /* virtual time, for WFQ */ 79182734Sjulian u_int32_t rr_deficit; /* for DRR */ 80182734Sjulian u_int32_t packets; /* # of packets in this queue */ 81182734Sjulian}; 82182734Sjulian 83182734Sjulian/* Per hook info */ 84182734Sjulianstruct hookinfo { 85182734Sjulian hook_p hook; 86182734Sjulian int noqueue; /* bypass any processing */ 87182734Sjulian TAILQ_HEAD(, ngp_fifo) fifo_head; /* FIFO queues */ 88182734Sjulian TAILQ_HEAD(, ngp_hdr) qout_head; /* delay queue head */ 89182734Sjulian struct timeval qin_utime; 90182734Sjulian struct ng_pipe_hookcfg cfg; 91182734Sjulian struct ng_pipe_hookrun run; 92182734Sjulian struct ng_pipe_hookstat stats; 93182734Sjulian uint64_t *ber_p; /* loss_p(BER,psize) map */ 94182734Sjulian}; 95182734Sjulian 96182734Sjulian/* Per node info */ 97182734Sjulianstruct node_priv { 98182734Sjulian u_int64_t delay; 99182734Sjulian u_int32_t overhead; 100182734Sjulian u_int32_t header_offset; 101182734Sjulian struct hookinfo lower; 102182734Sjulian struct hookinfo upper; 103215800Szec struct callout timer; 104215800Szec int timer_scheduled; 105182734Sjulian}; 106182734Sjuliantypedef struct node_priv *priv_p; 107182734Sjulian 108182734Sjulian/* Macro for calculating the virtual time for packet dequeueing in WFQ */ 109182734Sjulian#define FIFO_VTIME_SORT(plen) \ 110182734Sjulian if (hinfo->cfg.wfq && hinfo->cfg.bandwidth) { \ 111182734Sjulian ngp_f->vtime.tv_usec = now->tv_usec + ((uint64_t) (plen) \ 112182734Sjulian + priv->overhead ) * hinfo->run.fifo_queues * \ 113182734Sjulian 8000000 / hinfo->cfg.bandwidth; \ 114182734Sjulian ngp_f->vtime.tv_sec = now->tv_sec + \ 115182734Sjulian ngp_f->vtime.tv_usec / 1000000; \ 116182734Sjulian ngp_f->vtime.tv_usec = ngp_f->vtime.tv_usec % 1000000; \ 117182734Sjulian TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le) \ 118182734Sjulian if (ngp_f1->vtime.tv_sec > ngp_f->vtime.tv_sec || \ 119182734Sjulian (ngp_f1->vtime.tv_sec == ngp_f->vtime.tv_sec && \ 120182734Sjulian ngp_f1->vtime.tv_usec > ngp_f->vtime.tv_usec)) \ 121182734Sjulian break; \ 122182734Sjulian if (ngp_f1 == NULL) \ 123182734Sjulian TAILQ_INSERT_TAIL(&hinfo->fifo_head, ngp_f, fifo_le); \ 124182734Sjulian else \ 125182734Sjulian TAILQ_INSERT_BEFORE(ngp_f1, ngp_f, fifo_le); \ 126182734Sjulian } else \ 127182734Sjulian TAILQ_INSERT_TAIL(&hinfo->fifo_head, ngp_f, fifo_le); \ 128182734Sjulian 129182734Sjulian 130182734Sjulianstatic void parse_cfg(struct ng_pipe_hookcfg *, struct ng_pipe_hookcfg *, 131182734Sjulian struct hookinfo *, priv_p); 132182734Sjulianstatic void pipe_dequeue(struct hookinfo *, struct timeval *); 133215800Szecstatic void ngp_callout(node_p, hook_p, void *, int); 134182734Sjulianstatic int ngp_modevent(module_t, int, void *); 135182734Sjulian 136182734Sjulian/* zone for storing ngp_hdr-s */ 137182734Sjulianstatic uma_zone_t ngp_zone; 138182734Sjulian 139182734Sjulian/* Netgraph methods */ 140182734Sjulianstatic ng_constructor_t ngp_constructor; 141182734Sjulianstatic ng_rcvmsg_t ngp_rcvmsg; 142182734Sjulianstatic ng_shutdown_t ngp_shutdown; 143182734Sjulianstatic ng_newhook_t ngp_newhook; 144182734Sjulianstatic ng_rcvdata_t ngp_rcvdata; 145182734Sjulianstatic ng_disconnect_t ngp_disconnect; 146182734Sjulian 147182734Sjulian/* Parse type for struct ng_pipe_hookstat */ 148182734Sjulianstatic const struct ng_parse_struct_field 149182734Sjulian ng_pipe_hookstat_type_fields[] = NG_PIPE_HOOKSTAT_INFO; 150182734Sjulianstatic const struct ng_parse_type ng_pipe_hookstat_type = { 151182734Sjulian &ng_parse_struct_type, 152182734Sjulian &ng_pipe_hookstat_type_fields 153182734Sjulian}; 154182734Sjulian 155182734Sjulian/* Parse type for struct ng_pipe_stats */ 156182734Sjulianstatic const struct ng_parse_struct_field ng_pipe_stats_type_fields[] = 157182734Sjulian NG_PIPE_STATS_INFO(&ng_pipe_hookstat_type); 158182734Sjulianstatic const struct ng_parse_type ng_pipe_stats_type = { 159182734Sjulian &ng_parse_struct_type, 160182734Sjulian &ng_pipe_stats_type_fields 161182734Sjulian}; 162182734Sjulian 163182734Sjulian/* Parse type for struct ng_pipe_hookrun */ 164182734Sjulianstatic const struct ng_parse_struct_field 165182734Sjulian ng_pipe_hookrun_type_fields[] = NG_PIPE_HOOKRUN_INFO; 166182734Sjulianstatic const struct ng_parse_type ng_pipe_hookrun_type = { 167182734Sjulian &ng_parse_struct_type, 168182734Sjulian &ng_pipe_hookrun_type_fields 169182734Sjulian}; 170182734Sjulian 171182734Sjulian/* Parse type for struct ng_pipe_run */ 172182734Sjulianstatic const struct ng_parse_struct_field 173182734Sjulian ng_pipe_run_type_fields[] = NG_PIPE_RUN_INFO(&ng_pipe_hookrun_type); 174182734Sjulianstatic const struct ng_parse_type ng_pipe_run_type = { 175182734Sjulian &ng_parse_struct_type, 176182734Sjulian &ng_pipe_run_type_fields 177182734Sjulian}; 178182734Sjulian 179182734Sjulian/* Parse type for struct ng_pipe_hookcfg */ 180182734Sjulianstatic const struct ng_parse_struct_field 181182734Sjulian ng_pipe_hookcfg_type_fields[] = NG_PIPE_HOOKCFG_INFO; 182182734Sjulianstatic const struct ng_parse_type ng_pipe_hookcfg_type = { 183182734Sjulian &ng_parse_struct_type, 184182734Sjulian &ng_pipe_hookcfg_type_fields 185182734Sjulian}; 186182734Sjulian 187182734Sjulian/* Parse type for struct ng_pipe_cfg */ 188182734Sjulianstatic const struct ng_parse_struct_field 189182734Sjulian ng_pipe_cfg_type_fields[] = NG_PIPE_CFG_INFO(&ng_pipe_hookcfg_type); 190182734Sjulianstatic const struct ng_parse_type ng_pipe_cfg_type = { 191182734Sjulian &ng_parse_struct_type, 192182734Sjulian &ng_pipe_cfg_type_fields 193182734Sjulian}; 194182734Sjulian 195182734Sjulian/* List of commands and how to convert arguments to/from ASCII */ 196182734Sjulianstatic const struct ng_cmdlist ngp_cmds[] = { 197182734Sjulian { 198182734Sjulian .cookie = NGM_PIPE_COOKIE, 199182734Sjulian .cmd = NGM_PIPE_GET_STATS, 200182734Sjulian .name = "getstats", 201182734Sjulian .respType = &ng_pipe_stats_type 202182734Sjulian }, 203182734Sjulian { 204182734Sjulian .cookie = NGM_PIPE_COOKIE, 205182734Sjulian .cmd = NGM_PIPE_CLR_STATS, 206182734Sjulian .name = "clrstats" 207182734Sjulian }, 208182734Sjulian { 209182734Sjulian .cookie = NGM_PIPE_COOKIE, 210182734Sjulian .cmd = NGM_PIPE_GETCLR_STATS, 211182734Sjulian .name = "getclrstats", 212182734Sjulian .respType = &ng_pipe_stats_type 213182734Sjulian }, 214182734Sjulian { 215182734Sjulian .cookie = NGM_PIPE_COOKIE, 216182734Sjulian .cmd = NGM_PIPE_GET_RUN, 217182734Sjulian .name = "getrun", 218182734Sjulian .respType = &ng_pipe_run_type 219182734Sjulian }, 220182734Sjulian { 221182734Sjulian .cookie = NGM_PIPE_COOKIE, 222182734Sjulian .cmd = NGM_PIPE_GET_CFG, 223182734Sjulian .name = "getcfg", 224182734Sjulian .respType = &ng_pipe_cfg_type 225182734Sjulian }, 226182734Sjulian { 227182734Sjulian .cookie = NGM_PIPE_COOKIE, 228182734Sjulian .cmd = NGM_PIPE_SET_CFG, 229182734Sjulian .name = "setcfg", 230182734Sjulian .mesgType = &ng_pipe_cfg_type, 231182734Sjulian }, 232182734Sjulian { 0 } 233182734Sjulian}; 234182734Sjulian 235182734Sjulian/* Netgraph type descriptor */ 236182734Sjulianstatic struct ng_type ng_pipe_typestruct = { 237182734Sjulian .version = NG_ABI_VERSION, 238182734Sjulian .name = NG_PIPE_NODE_TYPE, 239182734Sjulian .mod_event = ngp_modevent, 240182734Sjulian .constructor = ngp_constructor, 241182734Sjulian .shutdown = ngp_shutdown, 242182734Sjulian .rcvmsg = ngp_rcvmsg, 243182734Sjulian .newhook = ngp_newhook, 244182734Sjulian .rcvdata = ngp_rcvdata, 245182734Sjulian .disconnect = ngp_disconnect, 246182734Sjulian .cmdlist = ngp_cmds 247182734Sjulian}; 248182734SjulianNETGRAPH_INIT(pipe, &ng_pipe_typestruct); 249182734Sjulian 250182734Sjulian/* Node constructor */ 251182734Sjulianstatic int 252182734Sjulianngp_constructor(node_p node) 253182734Sjulian{ 254182734Sjulian priv_p priv; 255182734Sjulian 256220768Sglebius priv = malloc(sizeof(*priv), M_NG_PIPE, M_ZERO | M_WAITOK); 257182734Sjulian NG_NODE_SET_PRIVATE(node, priv); 258182734Sjulian 259215800Szec /* Mark node as single-threaded */ 260215800Szec NG_NODE_FORCE_WRITER(node); 261215800Szec 262215800Szec ng_callout_init(&priv->timer); 263215800Szec 264182734Sjulian return (0); 265182734Sjulian} 266182734Sjulian 267182734Sjulian/* Add a hook */ 268182734Sjulianstatic int 269182734Sjulianngp_newhook(node_p node, hook_p hook, const char *name) 270182734Sjulian{ 271182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 272182734Sjulian struct hookinfo *hinfo; 273182734Sjulian 274182734Sjulian if (strcmp(name, NG_PIPE_HOOK_UPPER) == 0) { 275182734Sjulian bzero(&priv->upper, sizeof(priv->upper)); 276182734Sjulian priv->upper.hook = hook; 277182734Sjulian NG_HOOK_SET_PRIVATE(hook, &priv->upper); 278182734Sjulian } else if (strcmp(name, NG_PIPE_HOOK_LOWER) == 0) { 279182734Sjulian bzero(&priv->lower, sizeof(priv->lower)); 280182734Sjulian priv->lower.hook = hook; 281182734Sjulian NG_HOOK_SET_PRIVATE(hook, &priv->lower); 282182734Sjulian } else 283182734Sjulian return (EINVAL); 284182734Sjulian 285182734Sjulian /* Load non-zero initial cfg values */ 286182734Sjulian hinfo = NG_HOOK_PRIVATE(hook); 287182734Sjulian hinfo->cfg.qin_size_limit = 50; 288182734Sjulian hinfo->cfg.fifo = 1; 289182734Sjulian hinfo->cfg.droptail = 1; 290182734Sjulian TAILQ_INIT(&hinfo->fifo_head); 291182734Sjulian TAILQ_INIT(&hinfo->qout_head); 292182734Sjulian return (0); 293182734Sjulian} 294182734Sjulian 295182734Sjulian/* Receive a control message */ 296182734Sjulianstatic int 297182734Sjulianngp_rcvmsg(node_p node, item_p item, hook_p lasthook) 298182734Sjulian{ 299182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 300182734Sjulian struct ng_mesg *resp = NULL; 301222257Szec struct ng_mesg *msg, *flow_msg; 302182734Sjulian struct ng_pipe_stats *stats; 303182734Sjulian struct ng_pipe_run *run; 304182734Sjulian struct ng_pipe_cfg *cfg; 305182734Sjulian int error = 0; 306222257Szec int prev_down, now_down, cmd; 307182734Sjulian 308182734Sjulian NGI_GET_MSG(item, msg); 309182734Sjulian switch (msg->header.typecookie) { 310182734Sjulian case NGM_PIPE_COOKIE: 311182734Sjulian switch (msg->header.cmd) { 312182734Sjulian case NGM_PIPE_GET_STATS: 313182734Sjulian case NGM_PIPE_CLR_STATS: 314182734Sjulian case NGM_PIPE_GETCLR_STATS: 315182734Sjulian if (msg->header.cmd != NGM_PIPE_CLR_STATS) { 316182734Sjulian NG_MKRESPONSE(resp, msg, 317182734Sjulian sizeof(*stats), M_NOWAIT); 318182734Sjulian if (resp == NULL) { 319182734Sjulian error = ENOMEM; 320182734Sjulian break; 321182734Sjulian } 322215800Szec stats = (struct ng_pipe_stats *) resp->data; 323182734Sjulian bcopy(&priv->upper.stats, &stats->downstream, 324182734Sjulian sizeof(stats->downstream)); 325182734Sjulian bcopy(&priv->lower.stats, &stats->upstream, 326182734Sjulian sizeof(stats->upstream)); 327182734Sjulian } 328182734Sjulian if (msg->header.cmd != NGM_PIPE_GET_STATS) { 329182734Sjulian bzero(&priv->upper.stats, 330182734Sjulian sizeof(priv->upper.stats)); 331182734Sjulian bzero(&priv->lower.stats, 332182734Sjulian sizeof(priv->lower.stats)); 333182734Sjulian } 334182734Sjulian break; 335182734Sjulian case NGM_PIPE_GET_RUN: 336182734Sjulian NG_MKRESPONSE(resp, msg, sizeof(*run), M_NOWAIT); 337182734Sjulian if (resp == NULL) { 338182734Sjulian error = ENOMEM; 339182734Sjulian break; 340182734Sjulian } 341215800Szec run = (struct ng_pipe_run *) resp->data; 342182734Sjulian bcopy(&priv->upper.run, &run->downstream, 343182734Sjulian sizeof(run->downstream)); 344182734Sjulian bcopy(&priv->lower.run, &run->upstream, 345182734Sjulian sizeof(run->upstream)); 346182734Sjulian break; 347182734Sjulian case NGM_PIPE_GET_CFG: 348182734Sjulian NG_MKRESPONSE(resp, msg, sizeof(*cfg), M_NOWAIT); 349182734Sjulian if (resp == NULL) { 350182734Sjulian error = ENOMEM; 351182734Sjulian break; 352182734Sjulian } 353215800Szec cfg = (struct ng_pipe_cfg *) resp->data; 354182734Sjulian bcopy(&priv->upper.cfg, &cfg->downstream, 355182734Sjulian sizeof(cfg->downstream)); 356182734Sjulian bcopy(&priv->lower.cfg, &cfg->upstream, 357182734Sjulian sizeof(cfg->upstream)); 358182734Sjulian cfg->delay = priv->delay; 359182734Sjulian cfg->overhead = priv->overhead; 360182734Sjulian cfg->header_offset = priv->header_offset; 361182734Sjulian if (cfg->upstream.bandwidth == 362182734Sjulian cfg->downstream.bandwidth) { 363182734Sjulian cfg->bandwidth = cfg->upstream.bandwidth; 364182734Sjulian cfg->upstream.bandwidth = 0; 365182734Sjulian cfg->downstream.bandwidth = 0; 366182734Sjulian } else 367182734Sjulian cfg->bandwidth = 0; 368182734Sjulian break; 369182734Sjulian case NGM_PIPE_SET_CFG: 370215800Szec cfg = (struct ng_pipe_cfg *) msg->data; 371182734Sjulian if (msg->header.arglen != sizeof(*cfg)) { 372182734Sjulian error = EINVAL; 373182734Sjulian break; 374182734Sjulian } 375182734Sjulian 376182734Sjulian if (cfg->delay == -1) 377182734Sjulian priv->delay = 0; 378182734Sjulian else if (cfg->delay > 0 && cfg->delay < 10000000) 379182734Sjulian priv->delay = cfg->delay; 380182734Sjulian 381182734Sjulian if (cfg->bandwidth == -1) { 382182734Sjulian priv->upper.cfg.bandwidth = 0; 383182734Sjulian priv->lower.cfg.bandwidth = 0; 384182734Sjulian priv->overhead = 0; 385182734Sjulian } else if (cfg->bandwidth >= 100 && 386182734Sjulian cfg->bandwidth <= 1000000000) { 387182734Sjulian priv->upper.cfg.bandwidth = cfg->bandwidth; 388182734Sjulian priv->lower.cfg.bandwidth = cfg->bandwidth; 389182734Sjulian if (cfg->bandwidth >= 10000000) 390182734Sjulian priv->overhead = 8+4+12; /* Ethernet */ 391182734Sjulian else 392182734Sjulian priv->overhead = 10; /* HDLC */ 393182734Sjulian } 394182734Sjulian 395182734Sjulian if (cfg->overhead == -1) 396182734Sjulian priv->overhead = 0; 397215800Szec else if (cfg->overhead > 0 && 398215800Szec cfg->overhead < MAX_OHSIZE) 399182734Sjulian priv->overhead = cfg->overhead; 400182734Sjulian 401182734Sjulian if (cfg->header_offset == -1) 402182734Sjulian priv->header_offset = 0; 403182734Sjulian else if (cfg->header_offset > 0 && 404182734Sjulian cfg->header_offset < 64) 405182734Sjulian priv->header_offset = cfg->header_offset; 406182734Sjulian 407222257Szec prev_down = priv->upper.cfg.ber == 1 || 408222257Szec priv->lower.cfg.ber == 1; 409182734Sjulian parse_cfg(&priv->upper.cfg, &cfg->downstream, 410215800Szec &priv->upper, priv); 411182734Sjulian parse_cfg(&priv->lower.cfg, &cfg->upstream, 412215800Szec &priv->lower, priv); 413222257Szec now_down = priv->upper.cfg.ber == 1 || 414222257Szec priv->lower.cfg.ber == 1; 415222257Szec 416222257Szec if (prev_down != now_down) { 417222257Szec if (now_down) 418222257Szec cmd = NGM_LINK_IS_DOWN; 419222257Szec else 420222257Szec cmd = NGM_LINK_IS_UP; 421222257Szec 422222257Szec if (priv->lower.hook != NULL) { 423222257Szec NG_MKMESSAGE(flow_msg, NGM_FLOW_COOKIE, 424222257Szec cmd, 0, M_NOWAIT); 425222257Szec if (flow_msg != NULL) 426222257Szec NG_SEND_MSG_HOOK(error, node, 427222257Szec flow_msg, priv->lower.hook, 428222257Szec 0); 429222257Szec } 430222257Szec if (priv->upper.hook != NULL) { 431222257Szec NG_MKMESSAGE(flow_msg, NGM_FLOW_COOKIE, 432222257Szec cmd, 0, M_NOWAIT); 433222257Szec if (flow_msg != NULL) 434222257Szec NG_SEND_MSG_HOOK(error, node, 435222257Szec flow_msg, priv->upper.hook, 436222257Szec 0); 437222257Szec } 438222257Szec } 439182734Sjulian break; 440182734Sjulian default: 441182734Sjulian error = EINVAL; 442182734Sjulian break; 443182734Sjulian } 444182734Sjulian break; 445182734Sjulian default: 446182734Sjulian error = EINVAL; 447182734Sjulian break; 448182734Sjulian } 449182734Sjulian NG_RESPOND_MSG(error, node, item, resp); 450182734Sjulian NG_FREE_MSG(msg); 451182734Sjulian 452182734Sjulian return (error); 453182734Sjulian} 454182734Sjulian 455182734Sjulianstatic void 456182734Sjulianparse_cfg(struct ng_pipe_hookcfg *current, struct ng_pipe_hookcfg *new, 457182734Sjulian struct hookinfo *hinfo, priv_p priv) 458182734Sjulian{ 459182734Sjulian 460182734Sjulian if (new->ber == -1) { 461182734Sjulian current->ber = 0; 462182734Sjulian if (hinfo->ber_p) { 463184205Sdes free(hinfo->ber_p, M_NG_PIPE); 464182734Sjulian hinfo->ber_p = NULL; 465182734Sjulian } 466182734Sjulian } else if (new->ber >= 1 && new->ber <= 1000000000000) { 467182734Sjulian static const uint64_t one = 0x1000000000000; /* = 2^48 */ 468182734Sjulian uint64_t p0, p; 469182734Sjulian uint32_t fsize, i; 470182734Sjulian 471182734Sjulian if (hinfo->ber_p == NULL) 472215800Szec hinfo->ber_p = 473215800Szec malloc((MAX_FSIZE + MAX_OHSIZE) * sizeof(uint64_t), 474215800Szec M_NG_PIPE, M_NOWAIT); 475182734Sjulian current->ber = new->ber; 476182734Sjulian 477182734Sjulian /* 478182734Sjulian * For given BER and each frame size N (in bytes) calculate 479182734Sjulian * the probability P_OK that the frame is clean: 480182734Sjulian * 481182734Sjulian * P_OK(BER,N) = (1 - 1/BER)^(N*8) 482182734Sjulian * 483182734Sjulian * We use a 64-bit fixed-point format with decimal point 484182734Sjulian * positioned between bits 47 and 48. 485182734Sjulian */ 486182734Sjulian p0 = one - one / new->ber; 487182734Sjulian p = one; 488182734Sjulian for (fsize = 0; fsize < MAX_FSIZE + MAX_OHSIZE; fsize++) { 489182734Sjulian hinfo->ber_p[fsize] = p; 490215800Szec for (i = 0; i < 8; i++) 491215800Szec p = (p * (p0 & 0xffff) >> 48) + 492215800Szec (p * ((p0 >> 16) & 0xffff) >> 32) + 493215800Szec (p * (p0 >> 32) >> 16); 494182734Sjulian } 495182734Sjulian } 496182734Sjulian 497182734Sjulian if (new->qin_size_limit == -1) 498182734Sjulian current->qin_size_limit = 0; 499182734Sjulian else if (new->qin_size_limit >= 5) 500182734Sjulian current->qin_size_limit = new->qin_size_limit; 501182734Sjulian 502182734Sjulian if (new->qout_size_limit == -1) 503182734Sjulian current->qout_size_limit = 0; 504182734Sjulian else if (new->qout_size_limit >= 5) 505182734Sjulian current->qout_size_limit = new->qout_size_limit; 506182734Sjulian 507182734Sjulian if (new->duplicate == -1) 508182734Sjulian current->duplicate = 0; 509182734Sjulian else if (new->duplicate > 0 && new->duplicate <= 50) 510182734Sjulian current->duplicate = new->duplicate; 511182734Sjulian 512182734Sjulian if (new->fifo) { 513182734Sjulian current->fifo = 1; 514182734Sjulian current->wfq = 0; 515182734Sjulian current->drr = 0; 516182734Sjulian } 517182734Sjulian 518182734Sjulian if (new->wfq) { 519182734Sjulian current->fifo = 0; 520182734Sjulian current->wfq = 1; 521182734Sjulian current->drr = 0; 522182734Sjulian } 523182734Sjulian 524182734Sjulian if (new->drr) { 525182734Sjulian current->fifo = 0; 526182734Sjulian current->wfq = 0; 527182734Sjulian /* DRR quantum */ 528182734Sjulian if (new->drr >= 32) 529182734Sjulian current->drr = new->drr; 530182734Sjulian else 531182734Sjulian current->drr = 2048; /* default quantum */ 532182734Sjulian } 533182734Sjulian 534182734Sjulian if (new->droptail) { 535182734Sjulian current->droptail = 1; 536182734Sjulian current->drophead = 0; 537182734Sjulian } 538182734Sjulian 539182734Sjulian if (new->drophead) { 540182734Sjulian current->droptail = 0; 541182734Sjulian current->drophead = 1; 542182734Sjulian } 543182734Sjulian 544182734Sjulian if (new->bandwidth == -1) { 545182734Sjulian current->bandwidth = 0; 546182734Sjulian current->fifo = 1; 547182734Sjulian current->wfq = 0; 548182734Sjulian current->drr = 0; 549182734Sjulian } else if (new->bandwidth >= 100 && new->bandwidth <= 1000000000) 550182734Sjulian current->bandwidth = new->bandwidth; 551182734Sjulian 552182734Sjulian if (current->bandwidth | priv->delay | 553182734Sjulian current->duplicate | current->ber) 554182734Sjulian hinfo->noqueue = 0; 555182734Sjulian else 556182734Sjulian hinfo->noqueue = 1; 557182734Sjulian} 558182734Sjulian 559182734Sjulian/* 560182734Sjulian * Compute a hash signature for a packet. This function suffers from the 561182734Sjulian * NIH sindrome, so probably it would be wise to look around what other 562182734Sjulian * folks have found out to be a good and efficient IP hash function... 563182734Sjulian */ 564182734Sjulianstatic int 565182734Sjulianip_hash(struct mbuf *m, int offset) 566182734Sjulian{ 567182734Sjulian u_int64_t i; 568182734Sjulian struct ip *ip = (struct ip *)(mtod(m, u_char *) + offset); 569182734Sjulian 570182734Sjulian if (m->m_len < sizeof(struct ip) + offset || 571182734Sjulian ip->ip_v != 4 || ip->ip_hl << 2 != sizeof(struct ip)) 572182734Sjulian return 0; 573182734Sjulian 574182734Sjulian i = ((u_int64_t) ip->ip_src.s_addr ^ 575182734Sjulian ((u_int64_t) ip->ip_src.s_addr << 13) ^ 576182734Sjulian ((u_int64_t) ip->ip_dst.s_addr << 7) ^ 577182734Sjulian ((u_int64_t) ip->ip_dst.s_addr << 19)); 578182734Sjulian return (i ^ (i >> 32)); 579182734Sjulian} 580182734Sjulian 581182734Sjulian/* 582182734Sjulian * Receive data on a hook - both in upstream and downstream direction. 583182734Sjulian * We put the frame on the inbound queue, and try to initiate dequeuing 584182734Sjulian * sequence immediately. If inbound queue is full, discard one frame 585182734Sjulian * depending on dropping policy (from the head or from the tail of the 586182734Sjulian * queue). 587182734Sjulian */ 588182734Sjulianstatic int 589182734Sjulianngp_rcvdata(hook_p hook, item_p item) 590182734Sjulian{ 591182734Sjulian struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 592182734Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 593182734Sjulian struct timeval uuptime; 594182734Sjulian struct timeval *now = &uuptime; 595182734Sjulian struct ngp_fifo *ngp_f = NULL, *ngp_f1; 596182734Sjulian struct ngp_hdr *ngp_h = NULL; 597182734Sjulian struct mbuf *m; 598215800Szec int hash, plen; 599182734Sjulian int error = 0; 600182734Sjulian 601215800Szec /* 602215800Szec * Shortcut from inbound to outbound hook when neither of 603215800Szec * bandwidth, delay, BER or duplication probability is 604215800Szec * configured, nor we have queued frames to drain. 605215800Szec */ 606215800Szec if (hinfo->run.qin_frames == 0 && hinfo->run.qout_frames == 0 && 607215800Szec hinfo->noqueue) { 608182734Sjulian struct hookinfo *dest; 609182734Sjulian if (hinfo == &priv->lower) 610182734Sjulian dest = &priv->upper; 611182734Sjulian else 612182734Sjulian dest = &priv->lower; 613215800Szec 614215800Szec /* Send the frame. */ 615215800Szec plen = NGI_M(item)->m_pkthdr.len; 616182734Sjulian NG_FWD_ITEM_HOOK(error, item, dest->hook); 617215800Szec 618215800Szec /* Update stats. */ 619215800Szec if (error) { 620215800Szec hinfo->stats.out_disc_frames++; 621215800Szec hinfo->stats.out_disc_octets += plen; 622215800Szec } else { 623215800Szec hinfo->stats.fwd_frames++; 624215800Szec hinfo->stats.fwd_octets += plen; 625215800Szec } 626215800Szec 627215800Szec return (error); 628182734Sjulian } 629182734Sjulian 630182734Sjulian microuptime(now); 631182734Sjulian 632182734Sjulian /* 633215800Szec * If this was an empty queue, update service deadline time. 634182734Sjulian */ 635182734Sjulian if (hinfo->run.qin_frames == 0) { 636182734Sjulian struct timeval *when = &hinfo->qin_utime; 637182734Sjulian if (when->tv_sec < now->tv_sec || (when->tv_sec == now->tv_sec 638182734Sjulian && when->tv_usec < now->tv_usec)) { 639182734Sjulian when->tv_sec = now->tv_sec; 640182734Sjulian when->tv_usec = now->tv_usec; 641182734Sjulian } 642182734Sjulian } 643182734Sjulian 644182734Sjulian /* Populate the packet header */ 645182734Sjulian ngp_h = uma_zalloc(ngp_zone, M_NOWAIT); 646182734Sjulian KASSERT((ngp_h != NULL), ("ngp_h zalloc failed (1)")); 647182734Sjulian NGI_GET_M(item, m); 648182734Sjulian KASSERT(m != NULL, ("NGI_GET_M failed")); 649182734Sjulian ngp_h->m = m; 650182734Sjulian NG_FREE_ITEM(item); 651182734Sjulian 652182734Sjulian if (hinfo->cfg.fifo) 653182734Sjulian hash = 0; /* all packets go into a single FIFO queue */ 654182734Sjulian else 655182734Sjulian hash = ip_hash(m, priv->header_offset); 656182734Sjulian 657182734Sjulian /* Find the appropriate FIFO queue for the packet and enqueue it*/ 658182734Sjulian TAILQ_FOREACH(ngp_f, &hinfo->fifo_head, fifo_le) 659182734Sjulian if (hash == ngp_f->hash) 660182734Sjulian break; 661182734Sjulian if (ngp_f == NULL) { 662182734Sjulian ngp_f = uma_zalloc(ngp_zone, M_NOWAIT); 663182734Sjulian KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (2)")); 664182734Sjulian TAILQ_INIT(&ngp_f->packet_head); 665182734Sjulian ngp_f->hash = hash; 666182734Sjulian ngp_f->packets = 1; 667182734Sjulian ngp_f->rr_deficit = hinfo->cfg.drr; /* DRR quantum */ 668182734Sjulian hinfo->run.fifo_queues++; 669182734Sjulian TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link); 670182734Sjulian FIFO_VTIME_SORT(m->m_pkthdr.len); 671182734Sjulian } else { 672182734Sjulian TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link); 673182734Sjulian ngp_f->packets++; 674182734Sjulian } 675182734Sjulian hinfo->run.qin_frames++; 676182734Sjulian hinfo->run.qin_octets += m->m_pkthdr.len; 677182734Sjulian 678182734Sjulian /* Discard a frame if inbound queue limit has been reached */ 679182734Sjulian if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) { 680182734Sjulian struct mbuf *m1; 681182734Sjulian int longest = 0; 682182734Sjulian 683182734Sjulian /* Find the longest queue */ 684182734Sjulian TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le) 685182734Sjulian if (ngp_f1->packets > longest) { 686182734Sjulian longest = ngp_f1->packets; 687182734Sjulian ngp_f = ngp_f1; 688182734Sjulian } 689182734Sjulian 690182734Sjulian /* Drop a frame from the queue head/tail, depending on cfg */ 691182734Sjulian if (hinfo->cfg.drophead) 692182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 693182734Sjulian else 694182734Sjulian ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head); 695182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 696182734Sjulian m1 = ngp_h->m; 697182734Sjulian uma_zfree(ngp_zone, ngp_h); 698182734Sjulian hinfo->run.qin_octets -= m1->m_pkthdr.len; 699182734Sjulian hinfo->stats.in_disc_octets += m1->m_pkthdr.len; 700182734Sjulian m_freem(m1); 701182734Sjulian if (--(ngp_f->packets) == 0) { 702182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 703182734Sjulian uma_zfree(ngp_zone, ngp_f); 704182734Sjulian hinfo->run.fifo_queues--; 705182734Sjulian } 706182734Sjulian hinfo->run.qin_frames--; 707182734Sjulian hinfo->stats.in_disc_frames++; 708182734Sjulian } else if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) { 709182734Sjulian struct mbuf *m1; 710182734Sjulian int longest = 0; 711182734Sjulian 712182734Sjulian /* Find the longest queue */ 713182734Sjulian TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le) 714182734Sjulian if (ngp_f1->packets > longest) { 715182734Sjulian longest = ngp_f1->packets; 716182734Sjulian ngp_f = ngp_f1; 717182734Sjulian } 718182734Sjulian 719182734Sjulian /* Drop a frame from the queue head/tail, depending on cfg */ 720182734Sjulian if (hinfo->cfg.drophead) 721182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 722182734Sjulian else 723182734Sjulian ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head); 724182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 725182734Sjulian m1 = ngp_h->m; 726182734Sjulian uma_zfree(ngp_zone, ngp_h); 727182734Sjulian hinfo->run.qin_octets -= m1->m_pkthdr.len; 728182734Sjulian hinfo->stats.in_disc_octets += m1->m_pkthdr.len; 729182734Sjulian m_freem(m1); 730182734Sjulian if (--(ngp_f->packets) == 0) { 731182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 732182734Sjulian uma_zfree(ngp_zone, ngp_f); 733182734Sjulian hinfo->run.fifo_queues--; 734182734Sjulian } 735182734Sjulian hinfo->run.qin_frames--; 736182734Sjulian hinfo->stats.in_disc_frames++; 737182734Sjulian } 738182734Sjulian 739182734Sjulian /* 740215800Szec * Try to start the dequeuing process immediately. 741182734Sjulian */ 742182734Sjulian pipe_dequeue(hinfo, now); 743182734Sjulian 744182734Sjulian return (0); 745182734Sjulian} 746182734Sjulian 747182734Sjulian 748182734Sjulian/* 749182734Sjulian * Dequeueing sequence - we basically do the following: 750182734Sjulian * 1) Try to extract the frame from the inbound (bandwidth) queue; 751182734Sjulian * 2) In accordance to BER specified, discard the frame randomly; 752182734Sjulian * 3) If the frame survives BER, prepend it with delay info and move it 753182734Sjulian * to outbound (delay) queue; 754182734Sjulian * 4) Loop to 2) until bandwidth quota for this timeslice is reached, or 755182734Sjulian * inbound queue is flushed completely; 756215800Szec * 5) Dequeue frames from the outbound queue and send them downstream until 757215800Szec * outbound queue is flushed completely, or the next frame in the queue 758215800Szec * is not due to be dequeued yet 759182734Sjulian */ 760182734Sjulianstatic void 761182734Sjulianpipe_dequeue(struct hookinfo *hinfo, struct timeval *now) { 762182734Sjulian static uint64_t rand, oldrand; 763215800Szec const node_p node = NG_HOOK_NODE(hinfo->hook); 764215800Szec const priv_p priv = NG_NODE_PRIVATE(node); 765182734Sjulian struct hookinfo *dest; 766182734Sjulian struct ngp_fifo *ngp_f, *ngp_f1; 767182734Sjulian struct ngp_hdr *ngp_h; 768182734Sjulian struct timeval *when; 769182734Sjulian struct mbuf *m; 770215800Szec int plen, error = 0; 771182734Sjulian 772182734Sjulian /* Which one is the destination hook? */ 773182734Sjulian if (hinfo == &priv->lower) 774182734Sjulian dest = &priv->upper; 775182734Sjulian else 776182734Sjulian dest = &priv->lower; 777182734Sjulian 778182734Sjulian /* Bandwidth queue processing */ 779182734Sjulian while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) { 780182734Sjulian when = &hinfo->qin_utime; 781182734Sjulian if (when->tv_sec > now->tv_sec || (when->tv_sec == now->tv_sec 782182734Sjulian && when->tv_usec > now->tv_usec)) 783182734Sjulian break; 784182734Sjulian 785182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 786182734Sjulian m = ngp_h->m; 787182734Sjulian 788182734Sjulian /* Deficit Round Robin (DRR) processing */ 789182734Sjulian if (hinfo->cfg.drr) { 790182734Sjulian if (ngp_f->rr_deficit >= m->m_pkthdr.len) { 791182734Sjulian ngp_f->rr_deficit -= m->m_pkthdr.len; 792182734Sjulian } else { 793182734Sjulian ngp_f->rr_deficit += hinfo->cfg.drr; 794182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 795182734Sjulian TAILQ_INSERT_TAIL(&hinfo->fifo_head, 796182734Sjulian ngp_f, fifo_le); 797182734Sjulian continue; 798182734Sjulian } 799182734Sjulian } 800182734Sjulian 801182734Sjulian /* 802182734Sjulian * Either create a duplicate and pass it on, or dequeue 803182734Sjulian * the original packet... 804182734Sjulian */ 805182734Sjulian if (hinfo->cfg.duplicate && 806182734Sjulian random() % 100 <= hinfo->cfg.duplicate) { 807182734Sjulian ngp_h = uma_zalloc(ngp_zone, M_NOWAIT); 808182734Sjulian KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (3)")); 809209723Szec m = m_dup(m, M_NOWAIT); 810209723Szec KASSERT(m != NULL, ("m_dup failed")); 811209723Szec ngp_h->m = m; 812182734Sjulian } else { 813182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 814182734Sjulian hinfo->run.qin_frames--; 815182734Sjulian hinfo->run.qin_octets -= m->m_pkthdr.len; 816182734Sjulian ngp_f->packets--; 817182734Sjulian } 818182734Sjulian 819182734Sjulian /* Calculate the serialization delay */ 820182734Sjulian if (hinfo->cfg.bandwidth) { 821215800Szec hinfo->qin_utime.tv_usec += 822215800Szec ((uint64_t) m->m_pkthdr.len + priv->overhead ) * 823215800Szec 8000000 / hinfo->cfg.bandwidth; 824182734Sjulian hinfo->qin_utime.tv_sec += 825215800Szec hinfo->qin_utime.tv_usec / 1000000; 826182734Sjulian hinfo->qin_utime.tv_usec = 827215800Szec hinfo->qin_utime.tv_usec % 1000000; 828182734Sjulian } 829182734Sjulian when = &ngp_h->when; 830182734Sjulian when->tv_sec = hinfo->qin_utime.tv_sec; 831182734Sjulian when->tv_usec = hinfo->qin_utime.tv_usec; 832182734Sjulian 833182734Sjulian /* Sort / rearrange inbound queues */ 834182734Sjulian if (ngp_f->packets) { 835182734Sjulian if (hinfo->cfg.wfq) { 836182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 837182734Sjulian FIFO_VTIME_SORT(TAILQ_FIRST( 838182734Sjulian &ngp_f->packet_head)->m->m_pkthdr.len) 839182734Sjulian } 840182734Sjulian } else { 841182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 842182734Sjulian uma_zfree(ngp_zone, ngp_f); 843182734Sjulian hinfo->run.fifo_queues--; 844182734Sjulian } 845182734Sjulian 846182734Sjulian /* Randomly discard the frame, according to BER setting */ 847207426Strasz if (hinfo->cfg.ber) { 848207426Strasz oldrand = rand; 849207426Strasz rand = random(); 850207426Strasz if (((oldrand ^ rand) << 17) >= 851207426Strasz hinfo->ber_p[priv->overhead + m->m_pkthdr.len]) { 852207426Strasz hinfo->stats.out_disc_frames++; 853207426Strasz hinfo->stats.out_disc_octets += m->m_pkthdr.len; 854207426Strasz uma_zfree(ngp_zone, ngp_h); 855207426Strasz m_freem(m); 856207426Strasz continue; 857207426Strasz } 858182734Sjulian } 859182734Sjulian 860182734Sjulian /* Discard frame if outbound queue size limit exceeded */ 861182734Sjulian if (hinfo->cfg.qout_size_limit && 862182734Sjulian hinfo->run.qout_frames>=hinfo->cfg.qout_size_limit) { 863182734Sjulian hinfo->stats.out_disc_frames++; 864182734Sjulian hinfo->stats.out_disc_octets += m->m_pkthdr.len; 865182734Sjulian uma_zfree(ngp_zone, ngp_h); 866182734Sjulian m_freem(m); 867182734Sjulian continue; 868182734Sjulian } 869182734Sjulian 870182734Sjulian /* Calculate the propagation delay */ 871182734Sjulian when->tv_usec += priv->delay; 872182734Sjulian when->tv_sec += when->tv_usec / 1000000; 873182734Sjulian when->tv_usec = when->tv_usec % 1000000; 874182734Sjulian 875182734Sjulian /* Put the frame into the delay queue */ 876182734Sjulian TAILQ_INSERT_TAIL(&hinfo->qout_head, ngp_h, ngp_link); 877182734Sjulian hinfo->run.qout_frames++; 878182734Sjulian hinfo->run.qout_octets += m->m_pkthdr.len; 879182734Sjulian } 880182734Sjulian 881182734Sjulian /* Delay queue processing */ 882182734Sjulian while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) { 883182734Sjulian when = &ngp_h->when; 884215800Szec m = ngp_h->m; 885182734Sjulian if (when->tv_sec > now->tv_sec || 886182734Sjulian (when->tv_sec == now->tv_sec && 887182734Sjulian when->tv_usec > now->tv_usec)) 888182734Sjulian break; 889182734Sjulian 890182734Sjulian /* Update outbound queue stats */ 891215800Szec plen = m->m_pkthdr.len; 892182734Sjulian hinfo->run.qout_frames--; 893215800Szec hinfo->run.qout_octets -= plen; 894182734Sjulian 895182734Sjulian /* Dequeue the packet from qout */ 896182734Sjulian TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link); 897182734Sjulian uma_zfree(ngp_zone, ngp_h); 898182734Sjulian 899215800Szec NG_SEND_DATA(error, dest->hook, m, meta); 900215800Szec if (error) { 901215800Szec hinfo->stats.out_disc_frames++; 902215800Szec hinfo->stats.out_disc_octets += plen; 903215800Szec } else { 904215800Szec hinfo->stats.fwd_frames++; 905215800Szec hinfo->stats.fwd_octets += plen; 906215800Szec } 907182734Sjulian } 908182734Sjulian 909215800Szec if ((hinfo->run.qin_frames != 0 || hinfo->run.qout_frames != 0) && 910215800Szec !priv->timer_scheduled) { 911215800Szec ng_callout(&priv->timer, node, NULL, 1, ngp_callout, NULL, 0); 912215800Szec priv->timer_scheduled = 1; 913182734Sjulian } 914182734Sjulian} 915182734Sjulian 916182734Sjulian/* 917215800Szec * This routine is called on every clock tick. We poll connected hooks 918182734Sjulian * for queued frames by calling pipe_dequeue(). 919182734Sjulian */ 920182734Sjulianstatic void 921215800Szecngp_callout(node_p node, hook_p hook, void *arg1, int arg2) 922182734Sjulian{ 923215800Szec const priv_p priv = NG_NODE_PRIVATE(node); 924215800Szec struct timeval now; 925182734Sjulian 926215800Szec priv->timer_scheduled = 0; 927182734Sjulian microuptime(&now); 928215800Szec if (priv->upper.hook != NULL) 929215800Szec pipe_dequeue(&priv->upper, &now); 930215800Szec if (priv->lower.hook != NULL) 931215800Szec pipe_dequeue(&priv->lower, &now); 932182734Sjulian} 933182734Sjulian 934182734Sjulian/* 935182734Sjulian * Shutdown processing 936182734Sjulian * 937182734Sjulian * This is tricky. If we have both a lower and upper hook, then we 938182734Sjulian * probably want to extricate ourselves and leave the two peers 939182734Sjulian * still linked to each other. Otherwise we should just shut down as 940182734Sjulian * a normal node would. 941182734Sjulian */ 942182734Sjulianstatic int 943182734Sjulianngp_shutdown(node_p node) 944182734Sjulian{ 945182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 946182734Sjulian 947215800Szec if (priv->timer_scheduled) 948215800Szec ng_uncallout(&priv->timer, node); 949182734Sjulian if (priv->lower.hook && priv->upper.hook) 950182734Sjulian ng_bypass(priv->lower.hook, priv->upper.hook); 951182734Sjulian else { 952182734Sjulian if (priv->upper.hook != NULL) 953182734Sjulian ng_rmhook_self(priv->upper.hook); 954182734Sjulian if (priv->lower.hook != NULL) 955182734Sjulian ng_rmhook_self(priv->lower.hook); 956182734Sjulian } 957182734Sjulian NG_NODE_UNREF(node); 958184205Sdes free(priv, M_NG_PIPE); 959182734Sjulian return (0); 960182734Sjulian} 961182734Sjulian 962182734Sjulian 963182734Sjulian/* 964182734Sjulian * Hook disconnection 965182734Sjulian */ 966182734Sjulianstatic int 967182734Sjulianngp_disconnect(hook_p hook) 968182734Sjulian{ 969182734Sjulian struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 970182734Sjulian struct ngp_fifo *ngp_f; 971182734Sjulian struct ngp_hdr *ngp_h; 972182734Sjulian 973182734Sjulian KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__)); 974182734Sjulian hinfo->hook = NULL; 975182734Sjulian 976182734Sjulian /* Flush all fifo queues associated with the hook */ 977182734Sjulian while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) { 978182734Sjulian while ((ngp_h = TAILQ_FIRST(&ngp_f->packet_head))) { 979182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 980182734Sjulian m_freem(ngp_h->m); 981182734Sjulian uma_zfree(ngp_zone, ngp_h); 982182734Sjulian } 983182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 984182734Sjulian uma_zfree(ngp_zone, ngp_f); 985182734Sjulian } 986182734Sjulian 987182734Sjulian /* Flush the delay queue */ 988182734Sjulian while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) { 989182734Sjulian TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link); 990182734Sjulian m_freem(ngp_h->m); 991182734Sjulian uma_zfree(ngp_zone, ngp_h); 992182734Sjulian } 993182734Sjulian 994182734Sjulian /* Release the packet loss probability table (BER) */ 995182734Sjulian if (hinfo->ber_p) 996184205Sdes free(hinfo->ber_p, M_NG_PIPE); 997182734Sjulian 998182734Sjulian return (0); 999182734Sjulian} 1000182734Sjulian 1001182734Sjulianstatic int 1002182734Sjulianngp_modevent(module_t mod, int type, void *unused) 1003182734Sjulian{ 1004182734Sjulian int error = 0; 1005182734Sjulian 1006182734Sjulian switch (type) { 1007182734Sjulian case MOD_LOAD: 1008182734Sjulian ngp_zone = uma_zcreate("ng_pipe", max(sizeof(struct ngp_hdr), 1009182734Sjulian sizeof (struct ngp_fifo)), NULL, NULL, NULL, NULL, 1010182734Sjulian UMA_ALIGN_PTR, 0); 1011182734Sjulian if (ngp_zone == NULL) 1012182734Sjulian panic("ng_pipe: couldn't allocate descriptor zone"); 1013182734Sjulian break; 1014182734Sjulian case MOD_UNLOAD: 1015182734Sjulian uma_zdestroy(ngp_zone); 1016182734Sjulian break; 1017182734Sjulian default: 1018182734Sjulian error = EOPNOTSUPP; 1019182734Sjulian break; 1020182734Sjulian } 1021182734Sjulian 1022182734Sjulian return (error); 1023182734Sjulian} 1024