ng_pipe.c revision 215800
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: head/sys/netgraph/ng_pipe.c 215800 2010-11-24 16:02:58Z zec $ 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 256184205Sdes priv = malloc(sizeof(*priv), M_NG_PIPE, M_ZERO | M_NOWAIT); 257182734Sjulian if (priv == NULL) 258182734Sjulian return (ENOMEM); 259182734Sjulian NG_NODE_SET_PRIVATE(node, priv); 260182734Sjulian 261215800Szec /* Mark node as single-threaded */ 262215800Szec NG_NODE_FORCE_WRITER(node); 263215800Szec 264215800Szec ng_callout_init(&priv->timer); 265215800Szec 266182734Sjulian return (0); 267182734Sjulian} 268182734Sjulian 269182734Sjulian/* Add a hook */ 270182734Sjulianstatic int 271182734Sjulianngp_newhook(node_p node, hook_p hook, const char *name) 272182734Sjulian{ 273182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 274182734Sjulian struct hookinfo *hinfo; 275182734Sjulian 276182734Sjulian if (strcmp(name, NG_PIPE_HOOK_UPPER) == 0) { 277182734Sjulian bzero(&priv->upper, sizeof(priv->upper)); 278182734Sjulian priv->upper.hook = hook; 279182734Sjulian NG_HOOK_SET_PRIVATE(hook, &priv->upper); 280182734Sjulian } else if (strcmp(name, NG_PIPE_HOOK_LOWER) == 0) { 281182734Sjulian bzero(&priv->lower, sizeof(priv->lower)); 282182734Sjulian priv->lower.hook = hook; 283182734Sjulian NG_HOOK_SET_PRIVATE(hook, &priv->lower); 284182734Sjulian } else 285182734Sjulian return (EINVAL); 286182734Sjulian 287182734Sjulian /* Load non-zero initial cfg values */ 288182734Sjulian hinfo = NG_HOOK_PRIVATE(hook); 289182734Sjulian hinfo->cfg.qin_size_limit = 50; 290182734Sjulian hinfo->cfg.fifo = 1; 291182734Sjulian hinfo->cfg.droptail = 1; 292182734Sjulian TAILQ_INIT(&hinfo->fifo_head); 293182734Sjulian TAILQ_INIT(&hinfo->qout_head); 294182734Sjulian return (0); 295182734Sjulian} 296182734Sjulian 297182734Sjulian/* Receive a control message */ 298182734Sjulianstatic int 299182734Sjulianngp_rcvmsg(node_p node, item_p item, hook_p lasthook) 300182734Sjulian{ 301182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 302182734Sjulian struct ng_mesg *resp = NULL; 303182734Sjulian struct ng_mesg *msg; 304182734Sjulian struct ng_pipe_stats *stats; 305182734Sjulian struct ng_pipe_run *run; 306182734Sjulian struct ng_pipe_cfg *cfg; 307182734Sjulian int error = 0; 308182734Sjulian 309182734Sjulian NGI_GET_MSG(item, msg); 310182734Sjulian switch (msg->header.typecookie) { 311182734Sjulian case NGM_PIPE_COOKIE: 312182734Sjulian switch (msg->header.cmd) { 313182734Sjulian case NGM_PIPE_GET_STATS: 314182734Sjulian case NGM_PIPE_CLR_STATS: 315182734Sjulian case NGM_PIPE_GETCLR_STATS: 316182734Sjulian if (msg->header.cmd != NGM_PIPE_CLR_STATS) { 317182734Sjulian NG_MKRESPONSE(resp, msg, 318182734Sjulian sizeof(*stats), M_NOWAIT); 319182734Sjulian if (resp == NULL) { 320182734Sjulian error = ENOMEM; 321182734Sjulian break; 322182734Sjulian } 323215800Szec stats = (struct ng_pipe_stats *) resp->data; 324182734Sjulian bcopy(&priv->upper.stats, &stats->downstream, 325182734Sjulian sizeof(stats->downstream)); 326182734Sjulian bcopy(&priv->lower.stats, &stats->upstream, 327182734Sjulian sizeof(stats->upstream)); 328182734Sjulian } 329182734Sjulian if (msg->header.cmd != NGM_PIPE_GET_STATS) { 330182734Sjulian bzero(&priv->upper.stats, 331182734Sjulian sizeof(priv->upper.stats)); 332182734Sjulian bzero(&priv->lower.stats, 333182734Sjulian sizeof(priv->lower.stats)); 334182734Sjulian } 335182734Sjulian break; 336182734Sjulian case NGM_PIPE_GET_RUN: 337182734Sjulian NG_MKRESPONSE(resp, msg, sizeof(*run), M_NOWAIT); 338182734Sjulian if (resp == NULL) { 339182734Sjulian error = ENOMEM; 340182734Sjulian break; 341182734Sjulian } 342215800Szec run = (struct ng_pipe_run *) resp->data; 343182734Sjulian bcopy(&priv->upper.run, &run->downstream, 344182734Sjulian sizeof(run->downstream)); 345182734Sjulian bcopy(&priv->lower.run, &run->upstream, 346182734Sjulian sizeof(run->upstream)); 347182734Sjulian break; 348182734Sjulian case NGM_PIPE_GET_CFG: 349182734Sjulian NG_MKRESPONSE(resp, msg, sizeof(*cfg), M_NOWAIT); 350182734Sjulian if (resp == NULL) { 351182734Sjulian error = ENOMEM; 352182734Sjulian break; 353182734Sjulian } 354215800Szec cfg = (struct ng_pipe_cfg *) resp->data; 355182734Sjulian bcopy(&priv->upper.cfg, &cfg->downstream, 356182734Sjulian sizeof(cfg->downstream)); 357182734Sjulian bcopy(&priv->lower.cfg, &cfg->upstream, 358182734Sjulian sizeof(cfg->upstream)); 359182734Sjulian cfg->delay = priv->delay; 360182734Sjulian cfg->overhead = priv->overhead; 361182734Sjulian cfg->header_offset = priv->header_offset; 362182734Sjulian if (cfg->upstream.bandwidth == 363182734Sjulian cfg->downstream.bandwidth) { 364182734Sjulian cfg->bandwidth = cfg->upstream.bandwidth; 365182734Sjulian cfg->upstream.bandwidth = 0; 366182734Sjulian cfg->downstream.bandwidth = 0; 367182734Sjulian } else 368182734Sjulian cfg->bandwidth = 0; 369182734Sjulian break; 370182734Sjulian case NGM_PIPE_SET_CFG: 371215800Szec cfg = (struct ng_pipe_cfg *) msg->data; 372182734Sjulian if (msg->header.arglen != sizeof(*cfg)) { 373182734Sjulian error = EINVAL; 374182734Sjulian break; 375182734Sjulian } 376182734Sjulian 377182734Sjulian if (cfg->delay == -1) 378182734Sjulian priv->delay = 0; 379182734Sjulian else if (cfg->delay > 0 && cfg->delay < 10000000) 380182734Sjulian priv->delay = cfg->delay; 381182734Sjulian 382182734Sjulian if (cfg->bandwidth == -1) { 383182734Sjulian priv->upper.cfg.bandwidth = 0; 384182734Sjulian priv->lower.cfg.bandwidth = 0; 385182734Sjulian priv->overhead = 0; 386182734Sjulian } else if (cfg->bandwidth >= 100 && 387182734Sjulian cfg->bandwidth <= 1000000000) { 388182734Sjulian priv->upper.cfg.bandwidth = cfg->bandwidth; 389182734Sjulian priv->lower.cfg.bandwidth = cfg->bandwidth; 390182734Sjulian if (cfg->bandwidth >= 10000000) 391182734Sjulian priv->overhead = 8+4+12; /* Ethernet */ 392182734Sjulian else 393182734Sjulian priv->overhead = 10; /* HDLC */ 394182734Sjulian } 395182734Sjulian 396182734Sjulian if (cfg->overhead == -1) 397182734Sjulian priv->overhead = 0; 398215800Szec else if (cfg->overhead > 0 && 399215800Szec cfg->overhead < MAX_OHSIZE) 400182734Sjulian priv->overhead = cfg->overhead; 401182734Sjulian 402182734Sjulian if (cfg->header_offset == -1) 403182734Sjulian priv->header_offset = 0; 404182734Sjulian else if (cfg->header_offset > 0 && 405182734Sjulian cfg->header_offset < 64) 406182734Sjulian priv->header_offset = cfg->header_offset; 407182734Sjulian 408182734Sjulian parse_cfg(&priv->upper.cfg, &cfg->downstream, 409215800Szec &priv->upper, priv); 410182734Sjulian parse_cfg(&priv->lower.cfg, &cfg->upstream, 411215800Szec &priv->lower, priv); 412182734Sjulian break; 413182734Sjulian default: 414182734Sjulian error = EINVAL; 415182734Sjulian break; 416182734Sjulian } 417182734Sjulian break; 418182734Sjulian default: 419182734Sjulian error = EINVAL; 420182734Sjulian break; 421182734Sjulian } 422182734Sjulian NG_RESPOND_MSG(error, node, item, resp); 423182734Sjulian NG_FREE_MSG(msg); 424182734Sjulian 425182734Sjulian return (error); 426182734Sjulian} 427182734Sjulian 428182734Sjulianstatic void 429182734Sjulianparse_cfg(struct ng_pipe_hookcfg *current, struct ng_pipe_hookcfg *new, 430182734Sjulian struct hookinfo *hinfo, priv_p priv) 431182734Sjulian{ 432182734Sjulian 433182734Sjulian if (new->ber == -1) { 434182734Sjulian current->ber = 0; 435182734Sjulian if (hinfo->ber_p) { 436184205Sdes free(hinfo->ber_p, M_NG_PIPE); 437182734Sjulian hinfo->ber_p = NULL; 438182734Sjulian } 439182734Sjulian } else if (new->ber >= 1 && new->ber <= 1000000000000) { 440182734Sjulian static const uint64_t one = 0x1000000000000; /* = 2^48 */ 441182734Sjulian uint64_t p0, p; 442182734Sjulian uint32_t fsize, i; 443182734Sjulian 444182734Sjulian if (hinfo->ber_p == NULL) 445215800Szec hinfo->ber_p = 446215800Szec malloc((MAX_FSIZE + MAX_OHSIZE) * sizeof(uint64_t), 447215800Szec M_NG_PIPE, M_NOWAIT); 448182734Sjulian current->ber = new->ber; 449182734Sjulian 450182734Sjulian /* 451182734Sjulian * For given BER and each frame size N (in bytes) calculate 452182734Sjulian * the probability P_OK that the frame is clean: 453182734Sjulian * 454182734Sjulian * P_OK(BER,N) = (1 - 1/BER)^(N*8) 455182734Sjulian * 456182734Sjulian * We use a 64-bit fixed-point format with decimal point 457182734Sjulian * positioned between bits 47 and 48. 458182734Sjulian */ 459182734Sjulian p0 = one - one / new->ber; 460182734Sjulian p = one; 461182734Sjulian for (fsize = 0; fsize < MAX_FSIZE + MAX_OHSIZE; fsize++) { 462182734Sjulian hinfo->ber_p[fsize] = p; 463215800Szec for (i = 0; i < 8; i++) 464215800Szec p = (p * (p0 & 0xffff) >> 48) + 465215800Szec (p * ((p0 >> 16) & 0xffff) >> 32) + 466215800Szec (p * (p0 >> 32) >> 16); 467182734Sjulian } 468182734Sjulian } 469182734Sjulian 470182734Sjulian if (new->qin_size_limit == -1) 471182734Sjulian current->qin_size_limit = 0; 472182734Sjulian else if (new->qin_size_limit >= 5) 473182734Sjulian current->qin_size_limit = new->qin_size_limit; 474182734Sjulian 475182734Sjulian if (new->qout_size_limit == -1) 476182734Sjulian current->qout_size_limit = 0; 477182734Sjulian else if (new->qout_size_limit >= 5) 478182734Sjulian current->qout_size_limit = new->qout_size_limit; 479182734Sjulian 480182734Sjulian if (new->duplicate == -1) 481182734Sjulian current->duplicate = 0; 482182734Sjulian else if (new->duplicate > 0 && new->duplicate <= 50) 483182734Sjulian current->duplicate = new->duplicate; 484182734Sjulian 485182734Sjulian if (new->fifo) { 486182734Sjulian current->fifo = 1; 487182734Sjulian current->wfq = 0; 488182734Sjulian current->drr = 0; 489182734Sjulian } 490182734Sjulian 491182734Sjulian if (new->wfq) { 492182734Sjulian current->fifo = 0; 493182734Sjulian current->wfq = 1; 494182734Sjulian current->drr = 0; 495182734Sjulian } 496182734Sjulian 497182734Sjulian if (new->drr) { 498182734Sjulian current->fifo = 0; 499182734Sjulian current->wfq = 0; 500182734Sjulian /* DRR quantum */ 501182734Sjulian if (new->drr >= 32) 502182734Sjulian current->drr = new->drr; 503182734Sjulian else 504182734Sjulian current->drr = 2048; /* default quantum */ 505182734Sjulian } 506182734Sjulian 507182734Sjulian if (new->droptail) { 508182734Sjulian current->droptail = 1; 509182734Sjulian current->drophead = 0; 510182734Sjulian } 511182734Sjulian 512182734Sjulian if (new->drophead) { 513182734Sjulian current->droptail = 0; 514182734Sjulian current->drophead = 1; 515182734Sjulian } 516182734Sjulian 517182734Sjulian if (new->bandwidth == -1) { 518182734Sjulian current->bandwidth = 0; 519182734Sjulian current->fifo = 1; 520182734Sjulian current->wfq = 0; 521182734Sjulian current->drr = 0; 522182734Sjulian } else if (new->bandwidth >= 100 && new->bandwidth <= 1000000000) 523182734Sjulian current->bandwidth = new->bandwidth; 524182734Sjulian 525182734Sjulian if (current->bandwidth | priv->delay | 526182734Sjulian current->duplicate | current->ber) 527182734Sjulian hinfo->noqueue = 0; 528182734Sjulian else 529182734Sjulian hinfo->noqueue = 1; 530182734Sjulian} 531182734Sjulian 532182734Sjulian/* 533182734Sjulian * Compute a hash signature for a packet. This function suffers from the 534182734Sjulian * NIH sindrome, so probably it would be wise to look around what other 535182734Sjulian * folks have found out to be a good and efficient IP hash function... 536182734Sjulian */ 537182734Sjulianstatic int 538182734Sjulianip_hash(struct mbuf *m, int offset) 539182734Sjulian{ 540182734Sjulian u_int64_t i; 541182734Sjulian struct ip *ip = (struct ip *)(mtod(m, u_char *) + offset); 542182734Sjulian 543182734Sjulian if (m->m_len < sizeof(struct ip) + offset || 544182734Sjulian ip->ip_v != 4 || ip->ip_hl << 2 != sizeof(struct ip)) 545182734Sjulian return 0; 546182734Sjulian 547182734Sjulian i = ((u_int64_t) ip->ip_src.s_addr ^ 548182734Sjulian ((u_int64_t) ip->ip_src.s_addr << 13) ^ 549182734Sjulian ((u_int64_t) ip->ip_dst.s_addr << 7) ^ 550182734Sjulian ((u_int64_t) ip->ip_dst.s_addr << 19)); 551182734Sjulian return (i ^ (i >> 32)); 552182734Sjulian} 553182734Sjulian 554182734Sjulian/* 555182734Sjulian * Receive data on a hook - both in upstream and downstream direction. 556182734Sjulian * We put the frame on the inbound queue, and try to initiate dequeuing 557182734Sjulian * sequence immediately. If inbound queue is full, discard one frame 558182734Sjulian * depending on dropping policy (from the head or from the tail of the 559182734Sjulian * queue). 560182734Sjulian */ 561182734Sjulianstatic int 562182734Sjulianngp_rcvdata(hook_p hook, item_p item) 563182734Sjulian{ 564182734Sjulian struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 565182734Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 566182734Sjulian struct timeval uuptime; 567182734Sjulian struct timeval *now = &uuptime; 568182734Sjulian struct ngp_fifo *ngp_f = NULL, *ngp_f1; 569182734Sjulian struct ngp_hdr *ngp_h = NULL; 570182734Sjulian struct mbuf *m; 571215800Szec int hash, plen; 572182734Sjulian int error = 0; 573182734Sjulian 574215800Szec /* 575215800Szec * Shortcut from inbound to outbound hook when neither of 576215800Szec * bandwidth, delay, BER or duplication probability is 577215800Szec * configured, nor we have queued frames to drain. 578215800Szec */ 579215800Szec if (hinfo->run.qin_frames == 0 && hinfo->run.qout_frames == 0 && 580215800Szec hinfo->noqueue) { 581182734Sjulian struct hookinfo *dest; 582182734Sjulian if (hinfo == &priv->lower) 583182734Sjulian dest = &priv->upper; 584182734Sjulian else 585182734Sjulian dest = &priv->lower; 586215800Szec 587215800Szec /* Send the frame. */ 588215800Szec plen = NGI_M(item)->m_pkthdr.len; 589182734Sjulian NG_FWD_ITEM_HOOK(error, item, dest->hook); 590215800Szec 591215800Szec /* Update stats. */ 592215800Szec if (error) { 593215800Szec hinfo->stats.out_disc_frames++; 594215800Szec hinfo->stats.out_disc_octets += plen; 595215800Szec } else { 596215800Szec hinfo->stats.fwd_frames++; 597215800Szec hinfo->stats.fwd_octets += plen; 598215800Szec } 599215800Szec 600215800Szec return (error); 601182734Sjulian } 602182734Sjulian 603182734Sjulian microuptime(now); 604182734Sjulian 605182734Sjulian /* 606215800Szec * If this was an empty queue, update service deadline time. 607182734Sjulian */ 608182734Sjulian if (hinfo->run.qin_frames == 0) { 609182734Sjulian struct timeval *when = &hinfo->qin_utime; 610182734Sjulian if (when->tv_sec < now->tv_sec || (when->tv_sec == now->tv_sec 611182734Sjulian && when->tv_usec < now->tv_usec)) { 612182734Sjulian when->tv_sec = now->tv_sec; 613182734Sjulian when->tv_usec = now->tv_usec; 614182734Sjulian } 615182734Sjulian } 616182734Sjulian 617182734Sjulian /* Populate the packet header */ 618182734Sjulian ngp_h = uma_zalloc(ngp_zone, M_NOWAIT); 619182734Sjulian KASSERT((ngp_h != NULL), ("ngp_h zalloc failed (1)")); 620182734Sjulian NGI_GET_M(item, m); 621182734Sjulian KASSERT(m != NULL, ("NGI_GET_M failed")); 622182734Sjulian ngp_h->m = m; 623182734Sjulian NG_FREE_ITEM(item); 624182734Sjulian 625182734Sjulian if (hinfo->cfg.fifo) 626182734Sjulian hash = 0; /* all packets go into a single FIFO queue */ 627182734Sjulian else 628182734Sjulian hash = ip_hash(m, priv->header_offset); 629182734Sjulian 630182734Sjulian /* Find the appropriate FIFO queue for the packet and enqueue it*/ 631182734Sjulian TAILQ_FOREACH(ngp_f, &hinfo->fifo_head, fifo_le) 632182734Sjulian if (hash == ngp_f->hash) 633182734Sjulian break; 634182734Sjulian if (ngp_f == NULL) { 635182734Sjulian ngp_f = uma_zalloc(ngp_zone, M_NOWAIT); 636182734Sjulian KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (2)")); 637182734Sjulian TAILQ_INIT(&ngp_f->packet_head); 638182734Sjulian ngp_f->hash = hash; 639182734Sjulian ngp_f->packets = 1; 640182734Sjulian ngp_f->rr_deficit = hinfo->cfg.drr; /* DRR quantum */ 641182734Sjulian hinfo->run.fifo_queues++; 642182734Sjulian TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link); 643182734Sjulian FIFO_VTIME_SORT(m->m_pkthdr.len); 644182734Sjulian } else { 645182734Sjulian TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link); 646182734Sjulian ngp_f->packets++; 647182734Sjulian } 648182734Sjulian hinfo->run.qin_frames++; 649182734Sjulian hinfo->run.qin_octets += m->m_pkthdr.len; 650182734Sjulian 651182734Sjulian /* Discard a frame if inbound queue limit has been reached */ 652182734Sjulian if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) { 653182734Sjulian struct mbuf *m1; 654182734Sjulian int longest = 0; 655182734Sjulian 656182734Sjulian /* Find the longest queue */ 657182734Sjulian TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le) 658182734Sjulian if (ngp_f1->packets > longest) { 659182734Sjulian longest = ngp_f1->packets; 660182734Sjulian ngp_f = ngp_f1; 661182734Sjulian } 662182734Sjulian 663182734Sjulian /* Drop a frame from the queue head/tail, depending on cfg */ 664182734Sjulian if (hinfo->cfg.drophead) 665182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 666182734Sjulian else 667182734Sjulian ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head); 668182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 669182734Sjulian m1 = ngp_h->m; 670182734Sjulian uma_zfree(ngp_zone, ngp_h); 671182734Sjulian hinfo->run.qin_octets -= m1->m_pkthdr.len; 672182734Sjulian hinfo->stats.in_disc_octets += m1->m_pkthdr.len; 673182734Sjulian m_freem(m1); 674182734Sjulian if (--(ngp_f->packets) == 0) { 675182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 676182734Sjulian uma_zfree(ngp_zone, ngp_f); 677182734Sjulian hinfo->run.fifo_queues--; 678182734Sjulian } 679182734Sjulian hinfo->run.qin_frames--; 680182734Sjulian hinfo->stats.in_disc_frames++; 681182734Sjulian } else if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) { 682182734Sjulian struct mbuf *m1; 683182734Sjulian int longest = 0; 684182734Sjulian 685182734Sjulian /* Find the longest queue */ 686182734Sjulian TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le) 687182734Sjulian if (ngp_f1->packets > longest) { 688182734Sjulian longest = ngp_f1->packets; 689182734Sjulian ngp_f = ngp_f1; 690182734Sjulian } 691182734Sjulian 692182734Sjulian /* Drop a frame from the queue head/tail, depending on cfg */ 693182734Sjulian if (hinfo->cfg.drophead) 694182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 695182734Sjulian else 696182734Sjulian ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head); 697182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 698182734Sjulian m1 = ngp_h->m; 699182734Sjulian uma_zfree(ngp_zone, ngp_h); 700182734Sjulian hinfo->run.qin_octets -= m1->m_pkthdr.len; 701182734Sjulian hinfo->stats.in_disc_octets += m1->m_pkthdr.len; 702182734Sjulian m_freem(m1); 703182734Sjulian if (--(ngp_f->packets) == 0) { 704182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 705182734Sjulian uma_zfree(ngp_zone, ngp_f); 706182734Sjulian hinfo->run.fifo_queues--; 707182734Sjulian } 708182734Sjulian hinfo->run.qin_frames--; 709182734Sjulian hinfo->stats.in_disc_frames++; 710182734Sjulian } 711182734Sjulian 712182734Sjulian /* 713215800Szec * Try to start the dequeuing process immediately. 714182734Sjulian */ 715182734Sjulian pipe_dequeue(hinfo, now); 716182734Sjulian 717182734Sjulian return (0); 718182734Sjulian} 719182734Sjulian 720182734Sjulian 721182734Sjulian/* 722182734Sjulian * Dequeueing sequence - we basically do the following: 723182734Sjulian * 1) Try to extract the frame from the inbound (bandwidth) queue; 724182734Sjulian * 2) In accordance to BER specified, discard the frame randomly; 725182734Sjulian * 3) If the frame survives BER, prepend it with delay info and move it 726182734Sjulian * to outbound (delay) queue; 727182734Sjulian * 4) Loop to 2) until bandwidth quota for this timeslice is reached, or 728182734Sjulian * inbound queue is flushed completely; 729215800Szec * 5) Dequeue frames from the outbound queue and send them downstream until 730215800Szec * outbound queue is flushed completely, or the next frame in the queue 731215800Szec * is not due to be dequeued yet 732182734Sjulian */ 733182734Sjulianstatic void 734182734Sjulianpipe_dequeue(struct hookinfo *hinfo, struct timeval *now) { 735182734Sjulian static uint64_t rand, oldrand; 736215800Szec const node_p node = NG_HOOK_NODE(hinfo->hook); 737215800Szec const priv_p priv = NG_NODE_PRIVATE(node); 738182734Sjulian struct hookinfo *dest; 739182734Sjulian struct ngp_fifo *ngp_f, *ngp_f1; 740182734Sjulian struct ngp_hdr *ngp_h; 741182734Sjulian struct timeval *when; 742182734Sjulian struct mbuf *m; 743215800Szec int plen, error = 0; 744182734Sjulian 745182734Sjulian /* Which one is the destination hook? */ 746182734Sjulian if (hinfo == &priv->lower) 747182734Sjulian dest = &priv->upper; 748182734Sjulian else 749182734Sjulian dest = &priv->lower; 750182734Sjulian 751182734Sjulian /* Bandwidth queue processing */ 752182734Sjulian while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) { 753182734Sjulian when = &hinfo->qin_utime; 754182734Sjulian if (when->tv_sec > now->tv_sec || (when->tv_sec == now->tv_sec 755182734Sjulian && when->tv_usec > now->tv_usec)) 756182734Sjulian break; 757182734Sjulian 758182734Sjulian ngp_h = TAILQ_FIRST(&ngp_f->packet_head); 759182734Sjulian m = ngp_h->m; 760182734Sjulian 761182734Sjulian /* Deficit Round Robin (DRR) processing */ 762182734Sjulian if (hinfo->cfg.drr) { 763182734Sjulian if (ngp_f->rr_deficit >= m->m_pkthdr.len) { 764182734Sjulian ngp_f->rr_deficit -= m->m_pkthdr.len; 765182734Sjulian } else { 766182734Sjulian ngp_f->rr_deficit += hinfo->cfg.drr; 767182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 768182734Sjulian TAILQ_INSERT_TAIL(&hinfo->fifo_head, 769182734Sjulian ngp_f, fifo_le); 770182734Sjulian continue; 771182734Sjulian } 772182734Sjulian } 773182734Sjulian 774182734Sjulian /* 775182734Sjulian * Either create a duplicate and pass it on, or dequeue 776182734Sjulian * the original packet... 777182734Sjulian */ 778182734Sjulian if (hinfo->cfg.duplicate && 779182734Sjulian random() % 100 <= hinfo->cfg.duplicate) { 780182734Sjulian ngp_h = uma_zalloc(ngp_zone, M_NOWAIT); 781182734Sjulian KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (3)")); 782209723Szec m = m_dup(m, M_NOWAIT); 783209723Szec KASSERT(m != NULL, ("m_dup failed")); 784209723Szec ngp_h->m = m; 785182734Sjulian } else { 786182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 787182734Sjulian hinfo->run.qin_frames--; 788182734Sjulian hinfo->run.qin_octets -= m->m_pkthdr.len; 789182734Sjulian ngp_f->packets--; 790182734Sjulian } 791182734Sjulian 792182734Sjulian /* Calculate the serialization delay */ 793182734Sjulian if (hinfo->cfg.bandwidth) { 794215800Szec hinfo->qin_utime.tv_usec += 795215800Szec ((uint64_t) m->m_pkthdr.len + priv->overhead ) * 796215800Szec 8000000 / hinfo->cfg.bandwidth; 797182734Sjulian hinfo->qin_utime.tv_sec += 798215800Szec hinfo->qin_utime.tv_usec / 1000000; 799182734Sjulian hinfo->qin_utime.tv_usec = 800215800Szec hinfo->qin_utime.tv_usec % 1000000; 801182734Sjulian } 802182734Sjulian when = &ngp_h->when; 803182734Sjulian when->tv_sec = hinfo->qin_utime.tv_sec; 804182734Sjulian when->tv_usec = hinfo->qin_utime.tv_usec; 805182734Sjulian 806182734Sjulian /* Sort / rearrange inbound queues */ 807182734Sjulian if (ngp_f->packets) { 808182734Sjulian if (hinfo->cfg.wfq) { 809182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 810182734Sjulian FIFO_VTIME_SORT(TAILQ_FIRST( 811182734Sjulian &ngp_f->packet_head)->m->m_pkthdr.len) 812182734Sjulian } 813182734Sjulian } else { 814182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 815182734Sjulian uma_zfree(ngp_zone, ngp_f); 816182734Sjulian hinfo->run.fifo_queues--; 817182734Sjulian } 818182734Sjulian 819182734Sjulian /* Randomly discard the frame, according to BER setting */ 820207426Strasz if (hinfo->cfg.ber) { 821207426Strasz oldrand = rand; 822207426Strasz rand = random(); 823207426Strasz if (((oldrand ^ rand) << 17) >= 824207426Strasz hinfo->ber_p[priv->overhead + m->m_pkthdr.len]) { 825207426Strasz hinfo->stats.out_disc_frames++; 826207426Strasz hinfo->stats.out_disc_octets += m->m_pkthdr.len; 827207426Strasz uma_zfree(ngp_zone, ngp_h); 828207426Strasz m_freem(m); 829207426Strasz continue; 830207426Strasz } 831182734Sjulian } 832182734Sjulian 833182734Sjulian /* Discard frame if outbound queue size limit exceeded */ 834182734Sjulian if (hinfo->cfg.qout_size_limit && 835182734Sjulian hinfo->run.qout_frames>=hinfo->cfg.qout_size_limit) { 836182734Sjulian hinfo->stats.out_disc_frames++; 837182734Sjulian hinfo->stats.out_disc_octets += m->m_pkthdr.len; 838182734Sjulian uma_zfree(ngp_zone, ngp_h); 839182734Sjulian m_freem(m); 840182734Sjulian continue; 841182734Sjulian } 842182734Sjulian 843182734Sjulian /* Calculate the propagation delay */ 844182734Sjulian when->tv_usec += priv->delay; 845182734Sjulian when->tv_sec += when->tv_usec / 1000000; 846182734Sjulian when->tv_usec = when->tv_usec % 1000000; 847182734Sjulian 848182734Sjulian /* Put the frame into the delay queue */ 849182734Sjulian TAILQ_INSERT_TAIL(&hinfo->qout_head, ngp_h, ngp_link); 850182734Sjulian hinfo->run.qout_frames++; 851182734Sjulian hinfo->run.qout_octets += m->m_pkthdr.len; 852182734Sjulian } 853182734Sjulian 854182734Sjulian /* Delay queue processing */ 855182734Sjulian while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) { 856182734Sjulian when = &ngp_h->when; 857215800Szec m = ngp_h->m; 858182734Sjulian if (when->tv_sec > now->tv_sec || 859182734Sjulian (when->tv_sec == now->tv_sec && 860182734Sjulian when->tv_usec > now->tv_usec)) 861182734Sjulian break; 862182734Sjulian 863182734Sjulian /* Update outbound queue stats */ 864215800Szec plen = m->m_pkthdr.len; 865182734Sjulian hinfo->run.qout_frames--; 866215800Szec hinfo->run.qout_octets -= plen; 867182734Sjulian 868182734Sjulian /* Dequeue the packet from qout */ 869182734Sjulian TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link); 870182734Sjulian uma_zfree(ngp_zone, ngp_h); 871182734Sjulian 872215800Szec NG_SEND_DATA(error, dest->hook, m, meta); 873215800Szec if (error) { 874215800Szec hinfo->stats.out_disc_frames++; 875215800Szec hinfo->stats.out_disc_octets += plen; 876215800Szec } else { 877215800Szec hinfo->stats.fwd_frames++; 878215800Szec hinfo->stats.fwd_octets += plen; 879215800Szec } 880182734Sjulian } 881182734Sjulian 882215800Szec if ((hinfo->run.qin_frames != 0 || hinfo->run.qout_frames != 0) && 883215800Szec !priv->timer_scheduled) { 884215800Szec ng_callout(&priv->timer, node, NULL, 1, ngp_callout, NULL, 0); 885215800Szec priv->timer_scheduled = 1; 886182734Sjulian } 887182734Sjulian} 888182734Sjulian 889182734Sjulian/* 890215800Szec * This routine is called on every clock tick. We poll connected hooks 891182734Sjulian * for queued frames by calling pipe_dequeue(). 892182734Sjulian */ 893182734Sjulianstatic void 894215800Szecngp_callout(node_p node, hook_p hook, void *arg1, int arg2) 895182734Sjulian{ 896215800Szec const priv_p priv = NG_NODE_PRIVATE(node); 897215800Szec struct timeval now; 898182734Sjulian 899215800Szec priv->timer_scheduled = 0; 900182734Sjulian microuptime(&now); 901215800Szec if (priv->upper.hook != NULL) 902215800Szec pipe_dequeue(&priv->upper, &now); 903215800Szec if (priv->lower.hook != NULL) 904215800Szec pipe_dequeue(&priv->lower, &now); 905182734Sjulian} 906182734Sjulian 907182734Sjulian/* 908182734Sjulian * Shutdown processing 909182734Sjulian * 910182734Sjulian * This is tricky. If we have both a lower and upper hook, then we 911182734Sjulian * probably want to extricate ourselves and leave the two peers 912182734Sjulian * still linked to each other. Otherwise we should just shut down as 913182734Sjulian * a normal node would. 914182734Sjulian */ 915182734Sjulianstatic int 916182734Sjulianngp_shutdown(node_p node) 917182734Sjulian{ 918182734Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 919182734Sjulian 920215800Szec if (priv->timer_scheduled) 921215800Szec ng_uncallout(&priv->timer, node); 922182734Sjulian if (priv->lower.hook && priv->upper.hook) 923182734Sjulian ng_bypass(priv->lower.hook, priv->upper.hook); 924182734Sjulian else { 925182734Sjulian if (priv->upper.hook != NULL) 926182734Sjulian ng_rmhook_self(priv->upper.hook); 927182734Sjulian if (priv->lower.hook != NULL) 928182734Sjulian ng_rmhook_self(priv->lower.hook); 929182734Sjulian } 930182734Sjulian NG_NODE_UNREF(node); 931184205Sdes free(priv, M_NG_PIPE); 932182734Sjulian return (0); 933182734Sjulian} 934182734Sjulian 935182734Sjulian 936182734Sjulian/* 937182734Sjulian * Hook disconnection 938182734Sjulian */ 939182734Sjulianstatic int 940182734Sjulianngp_disconnect(hook_p hook) 941182734Sjulian{ 942182734Sjulian struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 943182734Sjulian struct ngp_fifo *ngp_f; 944182734Sjulian struct ngp_hdr *ngp_h; 945182734Sjulian 946182734Sjulian KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__)); 947182734Sjulian hinfo->hook = NULL; 948182734Sjulian 949182734Sjulian /* Flush all fifo queues associated with the hook */ 950182734Sjulian while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) { 951182734Sjulian while ((ngp_h = TAILQ_FIRST(&ngp_f->packet_head))) { 952182734Sjulian TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link); 953182734Sjulian m_freem(ngp_h->m); 954182734Sjulian uma_zfree(ngp_zone, ngp_h); 955182734Sjulian } 956182734Sjulian TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le); 957182734Sjulian uma_zfree(ngp_zone, ngp_f); 958182734Sjulian } 959182734Sjulian 960182734Sjulian /* Flush the delay queue */ 961182734Sjulian while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) { 962182734Sjulian TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link); 963182734Sjulian m_freem(ngp_h->m); 964182734Sjulian uma_zfree(ngp_zone, ngp_h); 965182734Sjulian } 966182734Sjulian 967182734Sjulian /* Release the packet loss probability table (BER) */ 968182734Sjulian if (hinfo->ber_p) 969184205Sdes free(hinfo->ber_p, M_NG_PIPE); 970182734Sjulian 971182734Sjulian return (0); 972182734Sjulian} 973182734Sjulian 974182734Sjulianstatic int 975182734Sjulianngp_modevent(module_t mod, int type, void *unused) 976182734Sjulian{ 977182734Sjulian int error = 0; 978182734Sjulian 979182734Sjulian switch (type) { 980182734Sjulian case MOD_LOAD: 981182734Sjulian ngp_zone = uma_zcreate("ng_pipe", max(sizeof(struct ngp_hdr), 982182734Sjulian sizeof (struct ngp_fifo)), NULL, NULL, NULL, NULL, 983182734Sjulian UMA_ALIGN_PTR, 0); 984182734Sjulian if (ngp_zone == NULL) 985182734Sjulian panic("ng_pipe: couldn't allocate descriptor zone"); 986182734Sjulian break; 987182734Sjulian case MOD_UNLOAD: 988182734Sjulian uma_zdestroy(ngp_zone); 989182734Sjulian break; 990182734Sjulian default: 991182734Sjulian error = EOPNOTSUPP; 992182734Sjulian break; 993182734Sjulian } 994182734Sjulian 995182734Sjulian return (error); 996182734Sjulian} 997