The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_dummynet.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1998-2002 Luigi Rizzo, Universita` di Pisa
    3  * Portions Copyright (c) 2000 Akamba Corp.
    4  * All rights reserved
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #define DUMMYNET_DEBUG
   32 
   33 #include "opt_inet6.h"
   34 
   35 /*
   36  * This module implements IP dummynet, a bandwidth limiter/delay emulator
   37  * used in conjunction with the ipfw package.
   38  * Description of the data structures used is in ip_dummynet.h
   39  * Here you mainly find the following blocks of code:
   40  *  + variable declarations;
   41  *  + heap management functions;
   42  *  + scheduler and dummynet functions;
   43  *  + configuration and initialization.
   44  *
   45  * NOTA BENE: critical sections are protected by the "dummynet lock".
   46  *
   47  * Most important Changes:
   48  *
   49  * 011004: KLDable
   50  * 010124: Fixed WF2Q behaviour
   51  * 010122: Fixed spl protection.
   52  * 000601: WF2Q support
   53  * 000106: large rewrite, use heaps to handle very many pipes.
   54  * 980513:      initial release
   55  *
   56  * include files marked with XXX are probably not needed
   57  */
   58 
   59 #include <sys/limits.h>
   60 #include <sys/param.h>
   61 #include <sys/systm.h>
   62 #include <sys/malloc.h>
   63 #include <sys/mbuf.h>
   64 #include <sys/kernel.h>
   65 #include <sys/module.h>
   66 #include <sys/priv.h>
   67 #include <sys/proc.h>
   68 #include <sys/socket.h>
   69 #include <sys/socketvar.h>
   70 #include <sys/time.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/taskqueue.h>
   73 #include <net/if.h>
   74 #include <net/netisr.h>
   75 #include <net/route.h>
   76 #include <netinet/in.h>
   77 #include <netinet/in_systm.h>
   78 #include <netinet/in_var.h>
   79 #include <netinet/ip.h>
   80 #include <netinet/ip_fw.h>
   81 #include <netinet/ip_dummynet.h>
   82 #include <netinet/ip_var.h>
   83 
   84 #include <netinet/if_ether.h> /* for struct arpcom */
   85 
   86 #include <netinet/ip6.h>       /* for ip6_input, ip6_output prototypes */
   87 #include <netinet6/ip6_var.h>
   88 
   89 /*
   90  * We keep a private variable for the simulation time, but we could
   91  * probably use an existing one ("softticks" in sys/kern/kern_timeout.c)
   92  */
   93 static dn_key curr_time = 0 ; /* current simulation time */
   94 
   95 static int dn_hash_size = 64 ;  /* default hash size */
   96 
   97 /* statistics on number of queue searches and search steps */
   98 static long searches, search_steps ;
   99 static int pipe_expire = 1 ;   /* expire queue if empty */
  100 static int dn_max_ratio = 16 ; /* max queues/buckets ratio */
  101 
  102 static long pipe_slot_limit = 100; /* Foot shooting limit for pipe queues. */
  103 static long pipe_byte_limit = 1024 * 1024;
  104 
  105 static int red_lookup_depth = 256;      /* RED - default lookup table depth */
  106 static int red_avg_pkt_size = 512;      /* RED - default medium packet size */
  107 static int red_max_pkt_size = 1500;     /* RED - default max packet size */
  108 
  109 static struct timeval prev_t, t;
  110 static long tick_last;                  /* Last tick duration (usec). */
  111 static long tick_delta;                 /* Last vs standard tick diff (usec). */
  112 static long tick_delta_sum;             /* Accumulated tick difference (usec).*/
  113 static long tick_adjustment;            /* Tick adjustments done. */
  114 static long tick_lost;                  /* Lost(coalesced) ticks number. */
  115 /* Adjusted vs non-adjusted curr_time difference (ticks). */
  116 static long tick_diff;
  117 
  118 static int              io_fast;
  119 static unsigned long    io_pkt;
  120 static unsigned long    io_pkt_fast;
  121 static unsigned long    io_pkt_drop;
  122 
  123 /*
  124  * Three heaps contain queues and pipes that the scheduler handles:
  125  *
  126  * ready_heap contains all dn_flow_queue related to fixed-rate pipes.
  127  *
  128  * wfq_ready_heap contains the pipes associated with WF2Q flows
  129  *
  130  * extract_heap contains pipes associated with delay lines.
  131  *
  132  */
  133 
  134 MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");
  135 
  136 static struct dn_heap ready_heap, extract_heap, wfq_ready_heap ;
  137 
  138 static int      heap_init(struct dn_heap *h, int size);
  139 static int      heap_insert (struct dn_heap *h, dn_key key1, void *p);
  140 static void     heap_extract(struct dn_heap *h, void *obj);
  141 static void     transmit_event(struct dn_pipe *pipe, struct mbuf **head,
  142                     struct mbuf **tail);
  143 static void     ready_event(struct dn_flow_queue *q, struct mbuf **head,
  144                     struct mbuf **tail);
  145 static void     ready_event_wfq(struct dn_pipe *p, struct mbuf **head,
  146                     struct mbuf **tail);
  147 
  148 #define HASHSIZE        16
  149 #define HASH(num)       ((((num) >> 8) ^ ((num) >> 4) ^ (num)) & 0x0f)
  150 static struct dn_pipe_head      pipehash[HASHSIZE];     /* all pipes */
  151 static struct dn_flow_set_head  flowsethash[HASHSIZE];  /* all flowsets */
  152 
  153 static struct callout dn_timeout;
  154 
  155 extern  void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
  156 
  157 #ifdef SYSCTL_NODE
  158 SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet, CTLFLAG_RW, 0, "Dummynet");
  159 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size,
  160     CTLFLAG_RW, &dn_hash_size, 0, "Default hash table size");
  161 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, curr_time,
  162     CTLFLAG_RD, &curr_time, 0, "Current tick");
  163 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, ready_heap,
  164     CTLFLAG_RD, &ready_heap.size, 0, "Size of ready heap");
  165 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, extract_heap,
  166     CTLFLAG_RD, &extract_heap.size, 0, "Size of extract heap");
  167 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, searches,
  168     CTLFLAG_RD, &searches, 0, "Number of queue searches");
  169 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, search_steps,
  170     CTLFLAG_RD, &search_steps, 0, "Number of queue search steps");
  171 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire,
  172     CTLFLAG_RW, &pipe_expire, 0, "Expire queue if empty");
  173 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, max_chain_len,
  174     CTLFLAG_RW, &dn_max_ratio, 0,
  175     "Max ratio between dynamic queues and buckets");
  176 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
  177     CTLFLAG_RD, &red_lookup_depth, 0, "Depth of RED lookup table");
  178 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
  179     CTLFLAG_RD, &red_avg_pkt_size, 0, "RED Medium packet size");
  180 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
  181     CTLFLAG_RD, &red_max_pkt_size, 0, "RED Max packet size");
  182 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta,
  183     CTLFLAG_RD, &tick_delta, 0, "Last vs standard tick difference (usec).");
  184 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta_sum,
  185     CTLFLAG_RD, &tick_delta_sum, 0, "Accumulated tick difference (usec).");
  186 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_adjustment,
  187     CTLFLAG_RD, &tick_adjustment, 0, "Tick adjustments done.");
  188 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_diff,
  189     CTLFLAG_RD, &tick_diff, 0,
  190     "Adjusted vs non-adjusted curr_time difference (ticks).");
  191 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_lost,
  192     CTLFLAG_RD, &tick_lost, 0,
  193     "Number of ticks coalesced by dummynet taskqueue.");
  194 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, io_fast,
  195     CTLFLAG_RW, &io_fast, 0, "Enable fast dummynet io.");
  196 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt,
  197     CTLFLAG_RD, &io_pkt, 0,
  198     "Number of packets passed to dummynet.");
  199 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_fast,
  200     CTLFLAG_RD, &io_pkt_fast, 0,
  201     "Number of packets bypassed dummynet scheduler.");
  202 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_drop,
  203     CTLFLAG_RD, &io_pkt_drop, 0,
  204     "Number of packets dropped by dummynet.");
  205 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, pipe_slot_limit,
  206     CTLFLAG_RW, &pipe_slot_limit, 0, "Upper limit in slots for pipe queue.");
  207 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, pipe_byte_limit,
  208     CTLFLAG_RW, &pipe_byte_limit, 0, "Upper limit in bytes for pipe queue.");
  209 #endif
  210 
  211 #ifdef DUMMYNET_DEBUG
  212 int     dummynet_debug = 0;
  213 #ifdef SYSCTL_NODE
  214 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug, CTLFLAG_RW, &dummynet_debug,
  215             0, "control debugging printfs");
  216 #endif
  217 #define DPRINTF(X)      if (dummynet_debug) printf X
  218 #else
  219 #define DPRINTF(X)
  220 #endif
  221 
  222 static struct task      dn_task;
  223 static struct taskqueue *dn_tq = NULL;
  224 static void dummynet_task(void *, int);
  225 
  226 static struct mtx dummynet_mtx;
  227 #define DUMMYNET_LOCK_INIT() \
  228         mtx_init(&dummynet_mtx, "dummynet", NULL, MTX_DEF)
  229 #define DUMMYNET_LOCK_DESTROY() mtx_destroy(&dummynet_mtx)
  230 #define DUMMYNET_LOCK()         mtx_lock(&dummynet_mtx)
  231 #define DUMMYNET_UNLOCK()       mtx_unlock(&dummynet_mtx)
  232 #define DUMMYNET_LOCK_ASSERT()  mtx_assert(&dummynet_mtx, MA_OWNED)
  233 
  234 static int      config_pipe(struct dn_pipe *p);
  235 static int      ip_dn_ctl(struct sockopt *sopt);
  236 
  237 static void     dummynet(void *);
  238 static void     dummynet_flush(void);
  239 static void     dummynet_send(struct mbuf *);
  240 void            dummynet_drain(void);
  241 static ip_dn_io_t dummynet_io;
  242 static void     dn_rule_delete(void *);
  243 
  244 /*
  245  * Heap management functions.
  246  *
  247  * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
  248  * Some macros help finding parent/children so we can optimize them.
  249  *
  250  * heap_init() is called to expand the heap when needed.
  251  * Increment size in blocks of 16 entries.
  252  * XXX failure to allocate a new element is a pretty bad failure
  253  * as we basically stall a whole queue forever!!
  254  * Returns 1 on error, 0 on success
  255  */
  256 #define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 )
  257 #define HEAP_LEFT(x) ( 2*(x) + 1 )
  258 #define HEAP_IS_LEFT(x) ( (x) & 1 )
  259 #define HEAP_RIGHT(x) ( 2*(x) + 2 )
  260 #define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
  261 #define HEAP_INCREMENT  15
  262 
  263 static int
  264 heap_init(struct dn_heap *h, int new_size)
  265 {
  266     struct dn_heap_entry *p;
  267 
  268     if (h->size >= new_size ) {
  269         printf("dummynet: %s, Bogus call, have %d want %d\n", __func__,
  270                 h->size, new_size);
  271         return 0 ;
  272     }
  273     new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT ;
  274     p = malloc(new_size * sizeof(*p), M_DUMMYNET, M_NOWAIT);
  275     if (p == NULL) {
  276         printf("dummynet: %s, resize %d failed\n", __func__, new_size );
  277         return 1 ; /* error */
  278     }
  279     if (h->size > 0) {
  280         bcopy(h->p, p, h->size * sizeof(*p) );
  281         free(h->p, M_DUMMYNET);
  282     }
  283     h->p = p ;
  284     h->size = new_size ;
  285     return 0 ;
  286 }
  287 
  288 /*
  289  * Insert element in heap. Normally, p != NULL, we insert p in
  290  * a new position and bubble up. If p == NULL, then the element is
  291  * already in place, and key is the position where to start the
  292  * bubble-up.
  293  * Returns 1 on failure (cannot allocate new heap entry)
  294  *
  295  * If offset > 0 the position (index, int) of the element in the heap is
  296  * also stored in the element itself at the given offset in bytes.
  297  */
  298 #define SET_OFFSET(heap, node) \
  299     if (heap->offset > 0) \
  300             *((int *)((char *)(heap->p[node].object) + heap->offset)) = node ;
  301 /*
  302  * RESET_OFFSET is used for sanity checks. It sets offset to an invalid value.
  303  */
  304 #define RESET_OFFSET(heap, node) \
  305     if (heap->offset > 0) \
  306             *((int *)((char *)(heap->p[node].object) + heap->offset)) = -1 ;
  307 static int
  308 heap_insert(struct dn_heap *h, dn_key key1, void *p)
  309 {
  310     int son = h->elements ;
  311 
  312     if (p == NULL)      /* data already there, set starting point */
  313         son = key1 ;
  314     else {              /* insert new element at the end, possibly resize */
  315         son = h->elements ;
  316         if (son == h->size) /* need resize... */
  317             if (heap_init(h, h->elements+1) )
  318                 return 1 ; /* failure... */
  319         h->p[son].object = p ;
  320         h->p[son].key = key1 ;
  321         h->elements++ ;
  322     }
  323     while (son > 0) {                           /* bubble up */
  324         int father = HEAP_FATHER(son) ;
  325         struct dn_heap_entry tmp  ;
  326 
  327         if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
  328             break ; /* found right position */
  329         /* son smaller than father, swap and repeat */
  330         HEAP_SWAP(h->p[son], h->p[father], tmp) ;
  331         SET_OFFSET(h, son);
  332         son = father ;
  333     }
  334     SET_OFFSET(h, son);
  335     return 0 ;
  336 }
  337 
  338 /*
  339  * remove top element from heap, or obj if obj != NULL
  340  */
  341 static void
  342 heap_extract(struct dn_heap *h, void *obj)
  343 {
  344     int child, father, max = h->elements - 1 ;
  345 
  346     if (max < 0) {
  347         printf("dummynet: warning, extract from empty heap 0x%p\n", h);
  348         return ;
  349     }
  350     father = 0 ; /* default: move up smallest child */
  351     if (obj != NULL) { /* extract specific element, index is at offset */
  352         if (h->offset <= 0)
  353             panic("dummynet: heap_extract from middle not supported on this heap!!!\n");
  354         father = *((int *)((char *)obj + h->offset)) ;
  355         if (father < 0 || father >= h->elements) {
  356             printf("dummynet: heap_extract, father %d out of bound 0..%d\n",
  357                 father, h->elements);
  358             panic("dummynet: heap_extract");
  359         }
  360     }
  361     RESET_OFFSET(h, father);
  362     child = HEAP_LEFT(father) ;         /* left child */
  363     while (child <= max) {              /* valid entry */
  364         if (child != max && DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
  365             child = child+1 ;           /* take right child, otherwise left */
  366         h->p[father] = h->p[child] ;
  367         SET_OFFSET(h, father);
  368         father = child ;
  369         child = HEAP_LEFT(child) ;   /* left child for next loop */
  370     }
  371     h->elements-- ;
  372     if (father != max) {
  373         /*
  374          * Fill hole with last entry and bubble up, reusing the insert code
  375          */
  376         h->p[father] = h->p[max] ;
  377         heap_insert(h, father, NULL); /* this one cannot fail */
  378     }
  379 }
  380 
  381 #if 0
  382 /*
  383  * change object position and update references
  384  * XXX this one is never used!
  385  */
  386 static void
  387 heap_move(struct dn_heap *h, dn_key new_key, void *object)
  388 {
  389     int temp;
  390     int i ;
  391     int max = h->elements-1 ;
  392     struct dn_heap_entry buf ;
  393 
  394     if (h->offset <= 0)
  395         panic("cannot move items on this heap");
  396 
  397     i = *((int *)((char *)object + h->offset));
  398     if (DN_KEY_LT(new_key, h->p[i].key) ) { /* must move up */
  399         h->p[i].key = new_key ;
  400         for (; i>0 && DN_KEY_LT(new_key, h->p[(temp = HEAP_FATHER(i))].key) ;
  401                  i = temp ) { /* bubble up */
  402             HEAP_SWAP(h->p[i], h->p[temp], buf) ;
  403             SET_OFFSET(h, i);
  404         }
  405     } else {            /* must move down */
  406         h->p[i].key = new_key ;
  407         while ( (temp = HEAP_LEFT(i)) <= max ) { /* found left child */
  408             if ((temp != max) && DN_KEY_GT(h->p[temp].key, h->p[temp+1].key))
  409                 temp++ ; /* select child with min key */
  410             if (DN_KEY_GT(new_key, h->p[temp].key)) { /* go down */
  411                 HEAP_SWAP(h->p[i], h->p[temp], buf) ;
  412                 SET_OFFSET(h, i);
  413             } else
  414                 break ;
  415             i = temp ;
  416         }
  417     }
  418     SET_OFFSET(h, i);
  419 }
  420 #endif /* heap_move, unused */
  421 
  422 /*
  423  * heapify() will reorganize data inside an array to maintain the
  424  * heap property. It is needed when we delete a bunch of entries.
  425  */
  426 static void
  427 heapify(struct dn_heap *h)
  428 {
  429     int i ;
  430 
  431     for (i = 0 ; i < h->elements ; i++ )
  432         heap_insert(h, i , NULL) ;
  433 }
  434 
  435 /*
  436  * cleanup the heap and free data structure
  437  */
  438 static void
  439 heap_free(struct dn_heap *h)
  440 {
  441     if (h->size >0 )
  442         free(h->p, M_DUMMYNET);
  443     bzero(h, sizeof(*h) );
  444 }
  445 
  446 /*
  447  * --- end of heap management functions ---
  448  */
  449 
  450 /*
  451  * Return the mbuf tag holding the dummynet state.  As an optimization
  452  * this is assumed to be the first tag on the list.  If this turns out
  453  * wrong we'll need to search the list.
  454  */
  455 static struct dn_pkt_tag *
  456 dn_tag_get(struct mbuf *m)
  457 {
  458     struct m_tag *mtag = m_tag_first(m);
  459     KASSERT(mtag != NULL &&
  460             mtag->m_tag_cookie == MTAG_ABI_COMPAT &&
  461             mtag->m_tag_id == PACKET_TAG_DUMMYNET,
  462             ("packet on dummynet queue w/o dummynet tag!"));
  463     return (struct dn_pkt_tag *)(mtag+1);
  464 }
  465 
  466 /*
  467  * Scheduler functions:
  468  *
  469  * transmit_event() is called when the delay-line needs to enter
  470  * the scheduler, either because of existing pkts getting ready,
  471  * or new packets entering the queue. The event handled is the delivery
  472  * time of the packet.
  473  *
  474  * ready_event() does something similar with fixed-rate queues, and the
  475  * event handled is the finish time of the head pkt.
  476  *
  477  * wfq_ready_event() does something similar with WF2Q queues, and the
  478  * event handled is the start time of the head pkt.
  479  *
  480  * In all cases, we make sure that the data structures are consistent
  481  * before passing pkts out, because this might trigger recursive
  482  * invocations of the procedures.
  483  */
  484 static void
  485 transmit_event(struct dn_pipe *pipe, struct mbuf **head, struct mbuf **tail)
  486 {
  487         struct mbuf *m;
  488         struct dn_pkt_tag *pkt;
  489 
  490         DUMMYNET_LOCK_ASSERT();
  491 
  492         while ((m = pipe->head) != NULL) {
  493                 pkt = dn_tag_get(m);
  494                 if (!DN_KEY_LEQ(pkt->output_time, curr_time))
  495                         break;
  496 
  497                 pipe->head = m->m_nextpkt;
  498                 if (*tail != NULL)
  499                         (*tail)->m_nextpkt = m;
  500                 else
  501                         *head = m;
  502                 *tail = m;
  503         }
  504         if (*tail != NULL)
  505                 (*tail)->m_nextpkt = NULL;
  506 
  507         /* If there are leftover packets, put into the heap for next event. */
  508         if ((m = pipe->head) != NULL) {
  509                 pkt = dn_tag_get(m);
  510                 /*
  511                  * XXX Should check errors on heap_insert, by draining the
  512                  * whole pipe p and hoping in the future we are more successful.
  513                  */
  514                 heap_insert(&extract_heap, pkt->output_time, pipe);
  515         }
  516 }
  517 
  518 /*
  519  * the following macro computes how many ticks we have to wait
  520  * before being able to transmit a packet. The credit is taken from
  521  * either a pipe (WF2Q) or a flow_queue (per-flow queueing)
  522  */
  523 #define SET_TICKS(_m, q, p)     \
  524     ((_m)->m_pkthdr.len * 8 * hz - (q)->numbytes + p->bandwidth - 1) / \
  525     p->bandwidth;
  526 
  527 /*
  528  * extract pkt from queue, compute output time (could be now)
  529  * and put into delay line (p_queue)
  530  */
  531 static void
  532 move_pkt(struct mbuf *pkt, struct dn_flow_queue *q, struct dn_pipe *p,
  533     int len)
  534 {
  535     struct dn_pkt_tag *dt = dn_tag_get(pkt);
  536 
  537     q->head = pkt->m_nextpkt ;
  538     q->len-- ;
  539     q->len_bytes -= len ;
  540 
  541     dt->output_time = curr_time + p->delay ;
  542 
  543     if (p->head == NULL)
  544         p->head = pkt;
  545     else
  546         p->tail->m_nextpkt = pkt;
  547     p->tail = pkt;
  548     p->tail->m_nextpkt = NULL;
  549 }
  550 
  551 /*
  552  * ready_event() is invoked every time the queue must enter the
  553  * scheduler, either because the first packet arrives, or because
  554  * a previously scheduled event fired.
  555  * On invokation, drain as many pkts as possible (could be 0) and then
  556  * if there are leftover packets reinsert the pkt in the scheduler.
  557  */
  558 static void
  559 ready_event(struct dn_flow_queue *q, struct mbuf **head, struct mbuf **tail)
  560 {
  561         struct mbuf *pkt;
  562         struct dn_pipe *p = q->fs->pipe;
  563         int p_was_empty;
  564 
  565         DUMMYNET_LOCK_ASSERT();
  566 
  567         if (p == NULL) {
  568                 printf("dummynet: ready_event- pipe is gone\n");
  569                 return;
  570         }
  571         p_was_empty = (p->head == NULL);
  572 
  573         /*
  574          * Schedule fixed-rate queues linked to this pipe:
  575          * account for the bw accumulated since last scheduling, then
  576          * drain as many pkts as allowed by q->numbytes and move to
  577          * the delay line (in p) computing output time.
  578          * bandwidth==0 (no limit) means we can drain the whole queue,
  579          * setting len_scaled = 0 does the job.
  580          */
  581         q->numbytes += (curr_time - q->sched_time) * p->bandwidth;
  582         while ((pkt = q->head) != NULL) {
  583                 int len = pkt->m_pkthdr.len;
  584                 int len_scaled = p->bandwidth ? len * 8 * hz : 0;
  585 
  586                 if (len_scaled > q->numbytes)
  587                         break;
  588                 q->numbytes -= len_scaled;
  589                 move_pkt(pkt, q, p, len);
  590         }
  591         /*
  592          * If we have more packets queued, schedule next ready event
  593          * (can only occur when bandwidth != 0, otherwise we would have
  594          * flushed the whole queue in the previous loop).
  595          * To this purpose we record the current time and compute how many
  596          * ticks to go for the finish time of the packet.
  597          */
  598         if ((pkt = q->head) != NULL) {  /* this implies bandwidth != 0 */
  599                 dn_key t = SET_TICKS(pkt, q, p); /* ticks i have to wait */
  600 
  601                 q->sched_time = curr_time;
  602                 heap_insert(&ready_heap, curr_time + t, (void *)q);
  603                 /*
  604                  * XXX Should check errors on heap_insert, and drain the whole
  605                  * queue on error hoping next time we are luckier.
  606                  */
  607         } else          /* RED needs to know when the queue becomes empty. */
  608                 q->q_time = curr_time;
  609 
  610         /*
  611          * If the delay line was empty call transmit_event() now.
  612          * Otherwise, the scheduler will take care of it.
  613          */
  614         if (p_was_empty)
  615                 transmit_event(p, head, tail);
  616 }
  617 
  618 /*
  619  * Called when we can transmit packets on WF2Q queues. Take pkts out of
  620  * the queues at their start time, and enqueue into the delay line.
  621  * Packets are drained until p->numbytes < 0. As long as
  622  * len_scaled >= p->numbytes, the packet goes into the delay line
  623  * with a deadline p->delay. For the last packet, if p->numbytes < 0,
  624  * there is an additional delay.
  625  */
  626 static void
  627 ready_event_wfq(struct dn_pipe *p, struct mbuf **head, struct mbuf **tail)
  628 {
  629         int p_was_empty = (p->head == NULL);
  630         struct dn_heap *sch = &(p->scheduler_heap);
  631         struct dn_heap *neh = &(p->not_eligible_heap);
  632         int64_t p_numbytes = p->numbytes;
  633 
  634         DUMMYNET_LOCK_ASSERT();
  635 
  636         if (p->if_name[0] == 0)         /* tx clock is simulated */
  637                 /*
  638                  * Since result may not fit into p->numbytes (32bit) we
  639                  * are using 64bit var here.
  640                  */
  641                 p_numbytes += (curr_time - p->sched_time) * p->bandwidth;
  642         else {  /*
  643                  * tx clock is for real,
  644                  * the ifq must be empty or this is a NOP.
  645                  */
  646                 if (p->ifp && p->ifp->if_snd.ifq_head != NULL)
  647                         return;
  648                 else {
  649                         DPRINTF(("dummynet: pipe %d ready from %s --\n",
  650                             p->pipe_nr, p->if_name));
  651                 }
  652         }
  653 
  654         /*
  655          * While we have backlogged traffic AND credit, we need to do
  656          * something on the queue.
  657          */
  658         while (p_numbytes >= 0 && (sch->elements > 0 || neh->elements > 0)) {
  659                 if (sch->elements > 0) {
  660                         /* Have some eligible pkts to send out. */
  661                         struct dn_flow_queue *q = sch->p[0].object;
  662                         struct mbuf *pkt = q->head;
  663                         struct dn_flow_set *fs = q->fs;
  664                         uint64_t len = pkt->m_pkthdr.len;
  665                         int len_scaled = p->bandwidth ? len * 8 * hz : 0;
  666 
  667                         heap_extract(sch, NULL); /* Remove queue from heap. */
  668                         p_numbytes -= len_scaled;
  669                         move_pkt(pkt, q, p, len);
  670 
  671                         p->V += (len << MY_M) / p->sum; /* Update V. */
  672                         q->S = q->F;                    /* Update start time. */
  673                         if (q->len == 0) {
  674                                 /* Flow not backlogged any more. */
  675                                 fs->backlogged--;
  676                                 heap_insert(&(p->idle_heap), q->F, q);
  677                         } else {
  678                                 /* Still backlogged. */
  679 
  680                                 /*
  681                                  * Update F and position in backlogged queue,
  682                                  * then put flow in not_eligible_heap
  683                                  * (we will fix this later).
  684                                  */
  685                                 len = (q->head)->m_pkthdr.len;
  686                                 q->F += (len << MY_M) / (uint64_t)fs->weight;
  687                                 if (DN_KEY_LEQ(q->S, p->V))
  688                                         heap_insert(neh, q->S, q);
  689                                 else
  690                                         heap_insert(sch, q->F, q);
  691                         }
  692                 }
  693                 /*
  694                  * Now compute V = max(V, min(S_i)). Remember that all elements
  695                  * in sch have by definition S_i <= V so if sch is not empty,
  696                  * V is surely the max and we must not update it. Conversely,
  697                  * if sch is empty we only need to look at neh.
  698                  */
  699                 if (sch->elements == 0 && neh->elements > 0)
  700                         p->V = MAX64(p->V, neh->p[0].key);
  701                 /* Move from neh to sch any packets that have become eligible */
  702                 while (neh->elements > 0 && DN_KEY_LEQ(neh->p[0].key, p->V)) {
  703                         struct dn_flow_queue *q = neh->p[0].object;
  704                         heap_extract(neh, NULL);
  705                         heap_insert(sch, q->F, q);
  706                 }
  707 
  708                 if (p->if_name[0] != '\0') { /* Tx clock is from a real thing */
  709                         p_numbytes = -1;        /* Mark not ready for I/O. */
  710                         break;
  711                 }
  712         }
  713         if (sch->elements == 0 && neh->elements == 0 && p_numbytes >= 0 &&
  714             p->idle_heap.elements > 0) {
  715                 /*
  716                  * No traffic and no events scheduled.
  717                  * We can get rid of idle-heap.
  718                  */
  719                 int i;
  720 
  721                 for (i = 0; i < p->idle_heap.elements; i++) {
  722                         struct dn_flow_queue *q = p->idle_heap.p[i].object;
  723 
  724                         q->F = 0;
  725                         q->S = q->F + 1;
  726                 }
  727                 p->sum = 0;
  728                 p->V = 0;
  729                 p->idle_heap.elements = 0;
  730         }
  731         /*
  732          * If we are getting clocks from dummynet (not a real interface) and
  733          * If we are under credit, schedule the next ready event.
  734          * Also fix the delivery time of the last packet.
  735          */
  736         if (p->if_name[0]==0 && p_numbytes < 0) { /* This implies bw > 0. */
  737                 dn_key t = 0;           /* Number of ticks i have to wait. */
  738 
  739                 if (p->bandwidth > 0)
  740                         t = (p->bandwidth - 1 - p_numbytes) / p->bandwidth;
  741                 dn_tag_get(p->tail)->output_time += t;
  742                 p->sched_time = curr_time;
  743                 heap_insert(&wfq_ready_heap, curr_time + t, (void *)p);
  744                 /*
  745                  * XXX Should check errors on heap_insert, and drain the whole
  746                  * queue on error hoping next time we are luckier.
  747                  */
  748         }
  749 
  750         /* Fit (adjust if necessary) 64bit result into 32bit variable. */
  751         if (p_numbytes > INT_MAX)
  752                 p->numbytes = INT_MAX;
  753         else if (p_numbytes < INT_MIN)
  754                 p->numbytes = INT_MIN;
  755         else
  756                 p->numbytes = p_numbytes;
  757 
  758         /*
  759          * If the delay line was empty call transmit_event() now.
  760          * Otherwise, the scheduler will take care of it.
  761          */
  762         if (p_was_empty)
  763                 transmit_event(p, head, tail);
  764 }
  765 
  766 /*
  767  * This is called one tick, after previous run. It is used to
  768  * schedule next run.
  769  */
  770 static void
  771 dummynet(void * __unused unused)
  772 {
  773 
  774         taskqueue_enqueue(dn_tq, &dn_task);
  775 }
  776 
  777 /*
  778  * The main dummynet processing function.
  779  */
  780 static void
  781 dummynet_task(void *context, int pending)
  782 {
  783         struct mbuf *head = NULL, *tail = NULL;
  784         struct dn_pipe *pipe;
  785         struct dn_heap *heaps[3];
  786         struct dn_heap *h;
  787         void *p;        /* generic parameter to handler */
  788         int i;
  789 
  790         DUMMYNET_LOCK();
  791 
  792         heaps[0] = &ready_heap;                 /* fixed-rate queues */
  793         heaps[1] = &wfq_ready_heap;             /* wfq queues */
  794         heaps[2] = &extract_heap;               /* delay line */
  795 
  796         /* Update number of lost(coalesced) ticks. */
  797         tick_lost += pending - 1;
  798  
  799         getmicrouptime(&t);
  800         /* Last tick duration (usec). */
  801         tick_last = (t.tv_sec - prev_t.tv_sec) * 1000000 +
  802             (t.tv_usec - prev_t.tv_usec);
  803         /* Last tick vs standard tick difference (usec). */
  804         tick_delta = (tick_last * hz - 1000000) / hz;
  805         /* Accumulated tick difference (usec). */
  806         tick_delta_sum += tick_delta;
  807  
  808         prev_t = t;
  809  
  810         /*
  811          * Adjust curr_time if accumulated tick difference greater than
  812          * 'standard' tick. Since curr_time should be monotonically increasing,
  813          * we do positive adjustment as required and throttle curr_time in
  814          * case of negative adjustment.
  815          */
  816         curr_time++;
  817         if (tick_delta_sum - tick >= 0) {
  818                 int diff = tick_delta_sum / tick;
  819  
  820                 curr_time += diff;
  821                 tick_diff += diff;
  822                 tick_delta_sum %= tick;
  823                 tick_adjustment++;
  824         } else if (tick_delta_sum + tick <= 0) {
  825                 curr_time--;
  826                 tick_diff--;
  827                 tick_delta_sum += tick;
  828                 tick_adjustment++;
  829         }
  830 
  831         for (i = 0; i < 3; i++) {
  832                 h = heaps[i];
  833                 while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time)) {
  834                         if (h->p[0].key > curr_time)
  835                                 printf("dummynet: warning, "
  836                                     "heap %d is %d ticks late\n",
  837                                     i, (int)(curr_time - h->p[0].key));
  838                         /* store a copy before heap_extract */
  839                         p = h->p[0].object;
  840                         /* need to extract before processing */
  841                         heap_extract(h, NULL);
  842                         if (i == 0)
  843                                 ready_event(p, &head, &tail);
  844                         else if (i == 1) {
  845                                 struct dn_pipe *pipe = p;
  846                                 if (pipe->if_name[0] != '\0')
  847                                         printf("dummynet: bad ready_event_wfq "
  848                                             "for pipe %s\n", pipe->if_name);
  849                                 else
  850                                         ready_event_wfq(p, &head, &tail);
  851                         } else
  852                                 transmit_event(p, &head, &tail);
  853                 }
  854         }
  855 
  856         /* Sweep pipes trying to expire idle flow_queues. */
  857         for (i = 0; i < HASHSIZE; i++)
  858                 SLIST_FOREACH(pipe, &pipehash[i], next)
  859                         if (pipe->idle_heap.elements > 0 &&
  860                             DN_KEY_LT(pipe->idle_heap.p[0].key, pipe->V)) {
  861                                 struct dn_flow_queue *q =
  862                                     pipe->idle_heap.p[0].object;
  863 
  864                                 heap_extract(&(pipe->idle_heap), NULL);
  865                                 /* Mark timestamp as invalid. */
  866                                 q->S = q->F + 1;
  867                                 pipe->sum -= q->fs->weight;
  868                         }
  869 
  870         DUMMYNET_UNLOCK();
  871 
  872         if (head != NULL)
  873                 dummynet_send(head);
  874 
  875         callout_reset(&dn_timeout, 1, dummynet, NULL);
  876 }
  877 
  878 static void
  879 dummynet_send(struct mbuf *m)
  880 {
  881         struct dn_pkt_tag *pkt;
  882         struct mbuf *n;
  883         struct ip *ip;
  884 
  885         for (; m != NULL; m = n) {
  886                 n = m->m_nextpkt;
  887                 m->m_nextpkt = NULL;
  888                 pkt = dn_tag_get(m);
  889                 switch (pkt->dn_dir) {
  890                 case DN_TO_IP_OUT:
  891                         ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
  892                         break ;
  893                   case DN_TO_IP_IN :
  894                         ip = mtod(m, struct ip *);
  895                         ip->ip_len = htons(ip->ip_len);
  896                         ip->ip_off = htons(ip->ip_off);
  897                         netisr_dispatch(NETISR_IP, m);
  898                         break;
  899 #ifdef INET6
  900                 case DN_TO_IP6_IN:
  901                         netisr_dispatch(NETISR_IPV6, m);
  902                         break;
  903 
  904                 case DN_TO_IP6_OUT:
  905                         ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
  906                         break;
  907 #endif
  908                 case DN_TO_IFB_FWD:
  909                         if (bridge_dn_p != NULL)
  910                                 ((*bridge_dn_p)(m, pkt->ifp));
  911                         else
  912                                 printf("dummynet: if_bridge not loaded\n");
  913 
  914                         break;
  915                 case DN_TO_ETH_DEMUX:
  916                         /*
  917                          * The Ethernet code assumes the Ethernet header is
  918                          * contiguous in the first mbuf header.
  919                          * Insure this is true.
  920                          */
  921                         if (m->m_len < ETHER_HDR_LEN &&
  922                             (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
  923                                 printf("dummynet/ether: pullup failed, "
  924                                     "dropping packet\n");
  925                                 break;
  926                         }
  927                         ether_demux(m->m_pkthdr.rcvif, m);
  928                         break;
  929                 case DN_TO_ETH_OUT:
  930                         ether_output_frame(pkt->ifp, m);
  931                         break;
  932                 default:
  933                         printf("dummynet: bad switch %d!\n", pkt->dn_dir);
  934                         m_freem(m);
  935                         break;
  936                 }
  937         }
  938 }
  939 
  940 /*
  941  * Unconditionally expire empty queues in case of shortage.
  942  * Returns the number of queues freed.
  943  */
  944 static int
  945 expire_queues(struct dn_flow_set *fs)
  946 {
  947     struct dn_flow_queue *q, *prev ;
  948     int i, initial_elements = fs->rq_elements ;
  949 
  950     if (fs->last_expired == time_uptime)
  951         return 0 ;
  952     fs->last_expired = time_uptime ;
  953     for (i = 0 ; i <= fs->rq_size ; i++) /* last one is overflow */
  954         for (prev=NULL, q = fs->rq[i] ; q != NULL ; )
  955             if (q->head != NULL || q->S != q->F+1) {
  956                 prev = q ;
  957                 q = q->next ;
  958             } else { /* entry is idle, expire it */
  959                 struct dn_flow_queue *old_q = q ;
  960 
  961                 if (prev != NULL)
  962                     prev->next = q = q->next ;
  963                 else
  964                     fs->rq[i] = q = q->next ;
  965                 fs->rq_elements-- ;
  966                 free(old_q, M_DUMMYNET);
  967             }
  968     return initial_elements - fs->rq_elements ;
  969 }
  970 
  971 /*
  972  * If room, create a new queue and put at head of slot i;
  973  * otherwise, create or use the default queue.
  974  */
  975 static struct dn_flow_queue *
  976 create_queue(struct dn_flow_set *fs, int i)
  977 {
  978         struct dn_flow_queue *q;
  979 
  980         if (fs->rq_elements > fs->rq_size * dn_max_ratio &&
  981             expire_queues(fs) == 0) {
  982                 /* No way to get room, use or create overflow queue. */
  983                 i = fs->rq_size;
  984                 if (fs->rq[i] != NULL)
  985                     return fs->rq[i];
  986         }
  987         q = malloc(sizeof(*q), M_DUMMYNET, M_NOWAIT | M_ZERO);
  988         if (q == NULL) {
  989                 printf("dummynet: sorry, cannot allocate queue for new flow\n");
  990                 return (NULL);
  991         }
  992         q->fs = fs;
  993         q->hash_slot = i;
  994         q->next = fs->rq[i];
  995         q->S = q->F + 1;        /* hack - mark timestamp as invalid. */
  996         q->numbytes = io_fast ? fs->pipe->bandwidth : 0;
  997         fs->rq[i] = q;
  998         fs->rq_elements++;
  999         return (q);
 1000 }
 1001 
 1002 /*
 1003  * Given a flow_set and a pkt in last_pkt, find a matching queue
 1004  * after appropriate masking. The queue is moved to front
 1005  * so that further searches take less time.
 1006  */
 1007 static struct dn_flow_queue *
 1008 find_queue(struct dn_flow_set *fs, struct ipfw_flow_id *id)
 1009 {
 1010     int i = 0 ; /* we need i and q for new allocations */
 1011     struct dn_flow_queue *q, *prev;
 1012     int is_v6 = IS_IP6_FLOW_ID(id);
 1013 
 1014     if ( !(fs->flags_fs & DN_HAVE_FLOW_MASK) )
 1015         q = fs->rq[0] ;
 1016     else {
 1017         /* first, do the masking, then hash */
 1018         id->dst_port &= fs->flow_mask.dst_port ;
 1019         id->src_port &= fs->flow_mask.src_port ;
 1020         id->proto &= fs->flow_mask.proto ;
 1021         id->flags = 0 ; /* we don't care about this one */
 1022         if (is_v6) {
 1023             APPLY_MASK(&id->dst_ip6, &fs->flow_mask.dst_ip6);
 1024             APPLY_MASK(&id->src_ip6, &fs->flow_mask.src_ip6);
 1025             id->flow_id6 &= fs->flow_mask.flow_id6;
 1026 
 1027             i = ((id->dst_ip6.__u6_addr.__u6_addr32[0]) & 0xffff)^
 1028                 ((id->dst_ip6.__u6_addr.__u6_addr32[1]) & 0xffff)^
 1029                 ((id->dst_ip6.__u6_addr.__u6_addr32[2]) & 0xffff)^
 1030                 ((id->dst_ip6.__u6_addr.__u6_addr32[3]) & 0xffff)^
 1031 
 1032                 ((id->dst_ip6.__u6_addr.__u6_addr32[0] >> 15) & 0xffff)^
 1033                 ((id->dst_ip6.__u6_addr.__u6_addr32[1] >> 15) & 0xffff)^
 1034                 ((id->dst_ip6.__u6_addr.__u6_addr32[2] >> 15) & 0xffff)^
 1035                 ((id->dst_ip6.__u6_addr.__u6_addr32[3] >> 15) & 0xffff)^
 1036 
 1037                 ((id->src_ip6.__u6_addr.__u6_addr32[0] << 1) & 0xfffff)^
 1038                 ((id->src_ip6.__u6_addr.__u6_addr32[1] << 1) & 0xfffff)^
 1039                 ((id->src_ip6.__u6_addr.__u6_addr32[2] << 1) & 0xfffff)^
 1040                 ((id->src_ip6.__u6_addr.__u6_addr32[3] << 1) & 0xfffff)^
 1041 
 1042                 ((id->src_ip6.__u6_addr.__u6_addr32[0] << 16) & 0xffff)^
 1043                 ((id->src_ip6.__u6_addr.__u6_addr32[1] << 16) & 0xffff)^
 1044                 ((id->src_ip6.__u6_addr.__u6_addr32[2] << 16) & 0xffff)^
 1045                 ((id->src_ip6.__u6_addr.__u6_addr32[3] << 16) & 0xffff)^
 1046 
 1047                 (id->dst_port << 1) ^ (id->src_port) ^
 1048                 (id->proto ) ^
 1049                 (id->flow_id6);
 1050         } else {
 1051             id->dst_ip &= fs->flow_mask.dst_ip ;
 1052             id->src_ip &= fs->flow_mask.src_ip ;
 1053 
 1054             i = ( (id->dst_ip) & 0xffff ) ^
 1055                 ( (id->dst_ip >> 15) & 0xffff ) ^
 1056                 ( (id->src_ip << 1) & 0xffff ) ^
 1057                 ( (id->src_ip >> 16 ) & 0xffff ) ^
 1058                 (id->dst_port << 1) ^ (id->src_port) ^
 1059                 (id->proto );
 1060         }
 1061         i = i % fs->rq_size ;
 1062         /* finally, scan the current list for a match */
 1063         searches++ ;
 1064         for (prev=NULL, q = fs->rq[i] ; q ; ) {
 1065             search_steps++;
 1066             if (is_v6 &&
 1067                     IN6_ARE_ADDR_EQUAL(&id->dst_ip6,&q->id.dst_ip6) &&  
 1068                     IN6_ARE_ADDR_EQUAL(&id->src_ip6,&q->id.src_ip6) &&  
 1069                     id->dst_port == q->id.dst_port &&
 1070                     id->src_port == q->id.src_port &&
 1071                     id->proto == q->id.proto &&
 1072                     id->flags == q->id.flags &&
 1073                     id->flow_id6 == q->id.flow_id6)
 1074                 break ; /* found */
 1075 
 1076             if (!is_v6 && id->dst_ip == q->id.dst_ip &&
 1077                     id->src_ip == q->id.src_ip &&
 1078                     id->dst_port == q->id.dst_port &&
 1079                     id->src_port == q->id.src_port &&
 1080                     id->proto == q->id.proto &&
 1081                     id->flags == q->id.flags)
 1082                 break ; /* found */
 1083 
 1084             /* No match. Check if we can expire the entry */
 1085             if (pipe_expire && q->head == NULL && q->S == q->F+1 ) {
 1086                 /* entry is idle and not in any heap, expire it */
 1087                 struct dn_flow_queue *old_q = q ;
 1088 
 1089                 if (prev != NULL)
 1090                     prev->next = q = q->next ;
 1091                 else
 1092                     fs->rq[i] = q = q->next ;
 1093                 fs->rq_elements-- ;
 1094                 free(old_q, M_DUMMYNET);
 1095                 continue ;
 1096             }
 1097             prev = q ;
 1098             q = q->next ;
 1099         }
 1100         if (q && prev != NULL) { /* found and not in front */
 1101             prev->next = q->next ;
 1102             q->next = fs->rq[i] ;
 1103             fs->rq[i] = q ;
 1104         }
 1105     }
 1106     if (q == NULL) { /* no match, need to allocate a new entry */
 1107         q = create_queue(fs, i);
 1108         if (q != NULL)
 1109         q->id = *id ;
 1110     }
 1111     return q ;
 1112 }
 1113 
 1114 static int
 1115 red_drops(struct dn_flow_set *fs, struct dn_flow_queue *q, int len)
 1116 {
 1117         /*
 1118          * RED algorithm
 1119          *
 1120          * RED calculates the average queue size (avg) using a low-pass filter
 1121          * with an exponential weighted (w_q) moving average:
 1122          *      avg  <-  (1-w_q) * avg + w_q * q_size
 1123          * where q_size is the queue length (measured in bytes or * packets).
 1124          *
 1125          * If q_size == 0, we compute the idle time for the link, and set
 1126          *      avg = (1 - w_q)^(idle/s)
 1127          * where s is the time needed for transmitting a medium-sized packet.
 1128          *
 1129          * Now, if avg < min_th the packet is enqueued.
 1130          * If avg > max_th the packet is dropped. Otherwise, the packet is
 1131          * dropped with probability P function of avg.
 1132          */
 1133 
 1134         int64_t p_b = 0;
 1135 
 1136         /* Queue in bytes or packets? */
 1137         u_int q_size = (fs->flags_fs & DN_QSIZE_IS_BYTES) ?
 1138             q->len_bytes : q->len;
 1139 
 1140         DPRINTF(("\ndummynet: %d q: %2u ", (int)curr_time, q_size));
 1141 
 1142         /* Average queue size estimation. */
 1143         if (q_size != 0) {
 1144                 /* Queue is not empty, avg <- avg + (q_size - avg) * w_q */
 1145                 int diff = SCALE(q_size) - q->avg;
 1146                 int64_t v = SCALE_MUL((int64_t)diff, (int64_t)fs->w_q);
 1147 
 1148                 q->avg += (int)v;
 1149         } else {
 1150                 /*
 1151                  * Queue is empty, find for how long the queue has been
 1152                  * empty and use a lookup table for computing
 1153                  * (1 - * w_q)^(idle_time/s) where s is the time to send a
 1154                  * (small) packet.
 1155                  * XXX check wraps...
 1156                  */
 1157                 if (q->avg) {
 1158                         u_int t = ((uint32_t)curr_time - q->q_time) /
 1159                             fs->lookup_step;
 1160 
 1161                         q->avg = (t < fs->lookup_depth) ?
 1162                             SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
 1163                 }
 1164         }
 1165         DPRINTF(("dummynet: avg: %u ", SCALE_VAL(q->avg)));
 1166 
 1167         /* Should i drop? */
 1168         if (q->avg < fs->min_th) {
 1169                 q->count = -1;
 1170                 return (0);     /* accept packet */
 1171         }
 1172         if (q->avg >= fs->max_th) {     /* average queue >=  max threshold */
 1173                 if (fs->flags_fs & DN_IS_GENTLE_RED) {
 1174                         /*
 1175                          * According to Gentle-RED, if avg is greater than
 1176                          * max_th the packet is dropped with a probability
 1177                          *       p_b = c_3 * avg - c_4
 1178                          * where c_3 = (1 - max_p) / max_th
 1179                          *       c_4 = 1 - 2 * max_p
 1180                          */
 1181                         p_b = SCALE_MUL((int64_t)fs->c_3, (int64_t)q->avg) -
 1182                             fs->c_4;
 1183                 } else {
 1184                         q->count = -1;
 1185                         DPRINTF(("dummynet: - drop"));
 1186                         return (1);
 1187                 }
 1188         } else if (q->avg > fs->min_th) {
 1189                 /*
 1190                  * We compute p_b using the linear dropping function
 1191                  *       p_b = c_1 * avg - c_2
 1192                  * where c_1 = max_p / (max_th - min_th)
 1193                  *       c_2 = max_p * min_th / (max_th - min_th)
 1194                  */
 1195                 p_b = SCALE_MUL((int64_t)fs->c_1, (int64_t)q->avg) - fs->c_2;
 1196         }
 1197 
 1198         if (fs->flags_fs & DN_QSIZE_IS_BYTES)
 1199                 p_b = (p_b * len) / fs->max_pkt_size;
 1200         if (++q->count == 0)
 1201                 q->random = random() & 0xffff;
 1202         else {
 1203                 /*
 1204                  * q->count counts packets arrived since last drop, so a greater
 1205                  * value of q->count means a greater packet drop probability.
 1206                  */
 1207                 if (SCALE_MUL(p_b, SCALE((int64_t)q->count)) > q->random) {
 1208                         q->count = 0;
 1209                         DPRINTF(("dummynet: - red drop"));
 1210                         /* After a drop we calculate a new random value. */
 1211                         q->random = random() & 0xffff;
 1212                         return (1);     /* drop */
 1213                 }
 1214         }
 1215         /* End of RED algorithm. */
 1216 
 1217         return (0);     /* accept */
 1218 }
 1219 
 1220 static __inline struct dn_flow_set *
 1221 locate_flowset(int fs_nr)
 1222 {
 1223         struct dn_flow_set *fs;
 1224 
 1225         SLIST_FOREACH(fs, &flowsethash[HASH(fs_nr)], next)
 1226                 if (fs->fs_nr == fs_nr)
 1227                         return (fs);
 1228 
 1229         return (NULL);
 1230 }
 1231 
 1232 static __inline struct dn_pipe *
 1233 locate_pipe(int pipe_nr)
 1234 {
 1235         struct dn_pipe *pipe;
 1236 
 1237         SLIST_FOREACH(pipe, &pipehash[HASH(pipe_nr)], next)
 1238                 if (pipe->pipe_nr == pipe_nr)
 1239                         return (pipe);
 1240 
 1241         return (NULL);
 1242 }
 1243 
 1244 /*
 1245  * dummynet hook for packets. Below 'pipe' is a pipe or a queue
 1246  * depending on whether WF2Q or fixed bw is used.
 1247  *
 1248  * pipe_nr      pipe or queue the packet is destined for.
 1249  * dir          where shall we send the packet after dummynet.
 1250  * m            the mbuf with the packet
 1251  * ifp          the 'ifp' parameter from the caller.
 1252  *              NULL in ip_input, destination interface in ip_output,
 1253  * rule         matching rule, in case of multiple passes
 1254  */
 1255 static int
 1256 dummynet_io(struct mbuf **m0, int dir, struct ip_fw_args *fwa)
 1257 {
 1258         struct mbuf *m = *m0, *head = NULL, *tail = NULL;
 1259         struct dn_pkt_tag *pkt;
 1260         struct m_tag *mtag;
 1261         struct dn_flow_set *fs = NULL;
 1262         struct dn_pipe *pipe;
 1263         uint64_t len = m->m_pkthdr.len;
 1264         struct dn_flow_queue *q = NULL;
 1265         int is_pipe;
 1266         ipfw_insn *cmd = ACTION_PTR(fwa->rule);
 1267 
 1268         KASSERT(m->m_nextpkt == NULL,
 1269             ("dummynet_io: mbuf queue passed to dummynet"));
 1270 
 1271         if (cmd->opcode == O_LOG)
 1272                 cmd += F_LEN(cmd);
 1273         if (cmd->opcode == O_ALTQ)
 1274                 cmd += F_LEN(cmd);
 1275         if (cmd->opcode == O_TAG)
 1276                 cmd += F_LEN(cmd);
 1277         is_pipe = (cmd->opcode == O_PIPE);
 1278 
 1279         DUMMYNET_LOCK();
 1280         io_pkt++;
 1281         /*
 1282          * This is a dummynet rule, so we expect an O_PIPE or O_QUEUE rule.
 1283          *
 1284          * XXXGL: probably the pipe->fs and fs->pipe logic here
 1285          * below can be simplified.
 1286          */
 1287         if (is_pipe) {
 1288                 pipe = locate_pipe(fwa->cookie);
 1289                 if (pipe != NULL)
 1290                         fs = &(pipe->fs);
 1291         } else
 1292                 fs = locate_flowset(fwa->cookie);
 1293 
 1294         if (fs == NULL)
 1295                 goto dropit;    /* This queue/pipe does not exist! */
 1296         pipe = fs->pipe;
 1297         if (pipe == NULL) {     /* Must be a queue, try find a matching pipe. */
 1298                 pipe = locate_pipe(fs->parent_nr);
 1299                 if (pipe != NULL)
 1300                         fs->pipe = pipe;
 1301                 else {
 1302                         printf("dummynet: no pipe %d for queue %d, drop pkt\n",
 1303                             fs->parent_nr, fs->fs_nr);
 1304                         goto dropit;
 1305                 }
 1306         }
 1307         q = find_queue(fs, &(fwa->f_id));
 1308         if (q == NULL)
 1309                 goto dropit;            /* Cannot allocate queue. */
 1310 
 1311         /* Update statistics, then check reasons to drop pkt. */
 1312         q->tot_bytes += len;
 1313         q->tot_pkts++;
 1314         if (fs->plr && random() < fs->plr)
 1315                 goto dropit;            /* Random pkt drop. */
 1316         if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
 1317                 if (q->len_bytes > fs->qsize)
 1318                         goto dropit;    /* Queue size overflow. */
 1319         } else {
 1320                 if (q->len >= fs->qsize)
 1321                         goto dropit;    /* Queue count overflow. */
 1322         }
 1323         if (fs->flags_fs & DN_IS_RED && red_drops(fs, q, len))
 1324                 goto dropit;
 1325 
 1326         /* XXX expensive to zero, see if we can remove it. */
 1327         mtag = m_tag_get(PACKET_TAG_DUMMYNET,
 1328             sizeof(struct dn_pkt_tag), M_NOWAIT | M_ZERO);
 1329         if (mtag == NULL)
 1330                 goto dropit;            /* Cannot allocate packet header. */
 1331         m_tag_prepend(m, mtag);         /* Attach to mbuf chain. */
 1332 
 1333         pkt = (struct dn_pkt_tag *)(mtag + 1);
 1334         /*
 1335          * Ok, i can handle the pkt now...
 1336          * Build and enqueue packet + parameters.
 1337          */
 1338         pkt->rule = fwa->rule;
 1339         pkt->dn_dir = dir;
 1340 
 1341         pkt->ifp = fwa->oif;
 1342 
 1343         if (q->head == NULL)
 1344                 q->head = m;
 1345         else
 1346                 q->tail->m_nextpkt = m;
 1347         q->tail = m;
 1348         q->len++;
 1349         q->len_bytes += len;
 1350 
 1351         if (q->head != m)               /* Flow was not idle, we are done. */
 1352                 goto done;
 1353 
 1354         if (q->q_time < (uint32_t)curr_time)
 1355                 q->numbytes = io_fast ? fs->pipe->bandwidth : 0;
 1356         q->q_time = curr_time;
 1357 
 1358         /*
 1359          * If we reach this point the flow was previously idle, so we need
 1360          * to schedule it. This involves different actions for fixed-rate or
 1361          * WF2Q queues.
 1362          */
 1363         if (is_pipe) {
 1364                 /* Fixed-rate queue: just insert into the ready_heap. */
 1365                 dn_key t = 0;
 1366 
 1367                 if (pipe->bandwidth && m->m_pkthdr.len * 8 * hz > q->numbytes)
 1368                         t = SET_TICKS(m, q, pipe);
 1369                 q->sched_time = curr_time;
 1370                 if (t == 0)             /* Must process it now. */
 1371                         ready_event(q, &head, &tail);
 1372                 else
 1373                         heap_insert(&ready_heap, curr_time + t , q);
 1374         } else {
 1375                 /*
 1376                  * WF2Q. First, compute start time S: if the flow was
 1377                  * idle (S = F + 1) set S to the virtual time V for the
 1378                  * controlling pipe, and update the sum of weights for the pipe;
 1379                  * otherwise, remove flow from idle_heap and set S to max(F,V).
 1380                  * Second, compute finish time F = S + len / weight.
 1381                  * Third, if pipe was idle, update V = max(S, V).
 1382                  * Fourth, count one more backlogged flow.
 1383                  */
 1384                 if (DN_KEY_GT(q->S, q->F)) { /* Means timestamps are invalid. */
 1385                         q->S = pipe->V;
 1386                         pipe->sum += fs->weight; /* Add weight of new queue. */
 1387                 } else {
 1388                         heap_extract(&(pipe->idle_heap), q);
 1389                         q->S = MAX64(q->F, pipe->V);
 1390                 }
 1391                 q->F = q->S + (len << MY_M) / (uint64_t)fs->weight;
 1392 
 1393                 if (pipe->not_eligible_heap.elements == 0 &&
 1394                     pipe->scheduler_heap.elements == 0)
 1395                         pipe->V = MAX64(q->S, pipe->V);
 1396                 fs->backlogged++;
 1397                 /*
 1398                  * Look at eligibility. A flow is not eligibile if S>V (when
 1399                  * this happens, it means that there is some other flow already
 1400                  * scheduled for the same pipe, so the scheduler_heap cannot be
 1401                  * empty). If the flow is not eligible we just store it in the
 1402                  * not_eligible_heap. Otherwise, we store in the scheduler_heap
 1403                  * and possibly invoke ready_event_wfq() right now if there is
 1404                  * leftover credit.
 1405                  * Note that for all flows in scheduler_heap (SCH), S_i <= V,
 1406                  * and for all flows in not_eligible_heap (NEH), S_i > V.
 1407                  * So when we need to compute max(V, min(S_i)) forall i in
 1408                  * SCH+NEH, we only need to look into NEH.
 1409                  */
 1410                 if (DN_KEY_GT(q->S, pipe->V)) {         /* Not eligible. */
 1411                         if (pipe->scheduler_heap.elements == 0)
 1412                                 printf("dummynet: ++ ouch! not eligible but empty scheduler!\n");
 1413                         heap_insert(&(pipe->not_eligible_heap), q->S, q);
 1414                 } else {
 1415                         heap_insert(&(pipe->scheduler_heap), q->F, q);
 1416                         if (pipe->numbytes >= 0) {       /* Pipe is idle. */
 1417                                 if (pipe->scheduler_heap.elements != 1)
 1418                                         printf("dummynet: OUCH! pipe should have been idle!\n");
 1419                                 DPRINTF(("dummynet: waking up pipe %d at %d\n",
 1420                                     pipe->pipe_nr, (int)(q->F >> MY_M)));
 1421                                 pipe->sched_time = curr_time;
 1422                                 ready_event_wfq(pipe, &head, &tail);
 1423                         }
 1424                 }
 1425         }
 1426 done:
 1427         if (head == m && dir != DN_TO_IFB_FWD && dir != DN_TO_ETH_DEMUX &&
 1428             dir != DN_TO_ETH_OUT) {     /* Fast io. */
 1429                 io_pkt_fast++;
 1430                 if (m->m_nextpkt != NULL)
 1431                         printf("dummynet: fast io: pkt chain detected!\n");
 1432                 head = m->m_nextpkt = NULL;
 1433         } else
 1434                 *m0 = NULL;             /* Normal io. */
 1435 
 1436         DUMMYNET_UNLOCK();
 1437         if (head != NULL)
 1438                 dummynet_send(head);
 1439         return (0);
 1440 
 1441 dropit:
 1442         io_pkt_drop++;
 1443         if (q)
 1444                 q->drops++;
 1445         DUMMYNET_UNLOCK();
 1446         m_freem(m);
 1447         *m0 = NULL;
 1448         return ((fs && (fs->flags_fs & DN_NOERROR)) ? 0 : ENOBUFS);
 1449 }
 1450 
 1451 /*
 1452  * Below, the rt_unref is only needed when (pkt->dn_dir == DN_TO_IP_OUT)
 1453  * Doing this would probably save us the initial bzero of dn_pkt
 1454  */
 1455 #define DN_FREE_PKT(_m) do {                            \
 1456         m_freem(_m);                                    \
 1457 } while (0)
 1458 
 1459 /*
 1460  * Dispose all packets and flow_queues on a flow_set.
 1461  * If all=1, also remove red lookup table and other storage,
 1462  * including the descriptor itself.
 1463  * For the one in dn_pipe MUST also cleanup ready_heap...
 1464  */
 1465 static void
 1466 purge_flow_set(struct dn_flow_set *fs, int all)
 1467 {
 1468         struct dn_flow_queue *q, *qn;
 1469         int i;
 1470 
 1471         DUMMYNET_LOCK_ASSERT();
 1472 
 1473         for (i = 0; i <= fs->rq_size; i++) {
 1474                 for (q = fs->rq[i]; q != NULL; q = qn) {
 1475                         struct mbuf *m, *mnext;
 1476 
 1477                         mnext = q->head;
 1478                         while ((m = mnext) != NULL) {
 1479                                 mnext = m->m_nextpkt;
 1480                                 DN_FREE_PKT(m);
 1481                         }
 1482                         qn = q->next;
 1483                         free(q, M_DUMMYNET);
 1484                 }
 1485                 fs->rq[i] = NULL;
 1486         }
 1487 
 1488         fs->rq_elements = 0;
 1489         if (all) {
 1490                 /* RED - free lookup table. */
 1491                 if (fs->w_q_lookup != NULL)
 1492                         free(fs->w_q_lookup, M_DUMMYNET);
 1493                 if (fs->rq != NULL)
 1494                         free(fs->rq, M_DUMMYNET);
 1495                 /* If this fs is not part of a pipe, free it. */
 1496                 if (fs->pipe == NULL || fs != &(fs->pipe->fs))
 1497                         free(fs, M_DUMMYNET);
 1498         }
 1499 }
 1500 
 1501 /*
 1502  * Dispose all packets queued on a pipe (not a flow_set).
 1503  * Also free all resources associated to a pipe, which is about
 1504  * to be deleted.
 1505  */
 1506 static void
 1507 purge_pipe(struct dn_pipe *pipe)
 1508 {
 1509     struct mbuf *m, *mnext;
 1510 
 1511     purge_flow_set( &(pipe->fs), 1 );
 1512 
 1513     mnext = pipe->head;
 1514     while ((m = mnext) != NULL) {
 1515         mnext = m->m_nextpkt;
 1516         DN_FREE_PKT(m);
 1517     }
 1518 
 1519     heap_free( &(pipe->scheduler_heap) );
 1520     heap_free( &(pipe->not_eligible_heap) );
 1521     heap_free( &(pipe->idle_heap) );
 1522 }
 1523 
 1524 /*
 1525  * Delete all pipes and heaps returning memory. Must also
 1526  * remove references from all ipfw rules to all pipes.
 1527  */
 1528 static void
 1529 dummynet_flush(void)
 1530 {
 1531         struct dn_pipe *pipe, *pipe1;
 1532         struct dn_flow_set *fs, *fs1;
 1533         int i;
 1534 
 1535         DUMMYNET_LOCK();
 1536         /* Free heaps so we don't have unwanted events. */
 1537         heap_free(&ready_heap);
 1538         heap_free(&wfq_ready_heap);
 1539         heap_free(&extract_heap);
 1540 
 1541         /*
 1542          * Now purge all queued pkts and delete all pipes.
 1543          *
 1544          * XXXGL: can we merge the for(;;) cycles into one or not?
 1545          */
 1546         for (i = 0; i < HASHSIZE; i++)
 1547                 SLIST_FOREACH_SAFE(fs, &flowsethash[i], next, fs1) {
 1548                         SLIST_REMOVE(&flowsethash[i], fs, dn_flow_set, next);
 1549                         purge_flow_set(fs, 1);
 1550                 }
 1551         for (i = 0; i < HASHSIZE; i++)
 1552                 SLIST_FOREACH_SAFE(pipe, &pipehash[i], next, pipe1) {
 1553                         SLIST_REMOVE(&pipehash[i], pipe, dn_pipe, next);
 1554                         purge_pipe(pipe);
 1555                         free(pipe, M_DUMMYNET);
 1556                 }
 1557         DUMMYNET_UNLOCK();
 1558 }
 1559 
 1560 extern struct ip_fw *ip_fw_default_rule ;
 1561 static void
 1562 dn_rule_delete_fs(struct dn_flow_set *fs, void *r)
 1563 {
 1564     int i ;
 1565     struct dn_flow_queue *q ;
 1566     struct mbuf *m ;
 1567 
 1568     for (i = 0 ; i <= fs->rq_size ; i++) /* last one is ovflow */
 1569         for (q = fs->rq[i] ; q ; q = q->next )
 1570             for (m = q->head ; m ; m = m->m_nextpkt ) {
 1571                 struct dn_pkt_tag *pkt = dn_tag_get(m) ;
 1572                 if (pkt->rule == r)
 1573                     pkt->rule = ip_fw_default_rule ;
 1574             }
 1575 }
 1576 /*
 1577  * when a firewall rule is deleted, scan all queues and remove the flow-id
 1578  * from packets matching this rule.
 1579  */
 1580 void
 1581 dn_rule_delete(void *r)
 1582 {
 1583     struct dn_pipe *pipe;
 1584     struct dn_flow_set *fs;
 1585     struct dn_pkt_tag *pkt;
 1586     struct mbuf *m;
 1587     int i;
 1588 
 1589     DUMMYNET_LOCK();
 1590     /*
 1591      * If the rule references a queue (dn_flow_set), then scan
 1592      * the flow set, otherwise scan pipes. Should do either, but doing
 1593      * both does not harm.
 1594      */
 1595     for (i = 0; i < HASHSIZE; i++)
 1596         SLIST_FOREACH(fs, &flowsethash[i], next)
 1597                 dn_rule_delete_fs(fs, r);
 1598 
 1599     for (i = 0; i < HASHSIZE; i++)
 1600         SLIST_FOREACH(pipe, &pipehash[i], next) {
 1601                 fs = &(pipe->fs);
 1602                 dn_rule_delete_fs(fs, r);
 1603                 for (m = pipe->head ; m ; m = m->m_nextpkt ) {
 1604                         pkt = dn_tag_get(m);
 1605                         if (pkt->rule == r)
 1606                                 pkt->rule = ip_fw_default_rule;
 1607                 }
 1608         }
 1609     DUMMYNET_UNLOCK();
 1610 }
 1611 
 1612 /*
 1613  * setup RED parameters
 1614  */
 1615 static int
 1616 config_red(struct dn_flow_set *p, struct dn_flow_set *x)
 1617 {
 1618         int i;
 1619 
 1620         x->w_q = p->w_q;
 1621         x->min_th = SCALE(p->min_th);
 1622         x->max_th = SCALE(p->max_th);
 1623         x->max_p = p->max_p;
 1624 
 1625         x->c_1 = p->max_p / (p->max_th - p->min_th);
 1626         x->c_2 = SCALE_MUL(x->c_1, SCALE(p->min_th));
 1627 
 1628         if (x->flags_fs & DN_IS_GENTLE_RED) {
 1629                 x->c_3 = (SCALE(1) - p->max_p) / p->max_th;
 1630                 x->c_4 = SCALE(1) - 2 * p->max_p;
 1631         }
 1632 
 1633         /* If the lookup table already exist, free and create it again. */
 1634         if (x->w_q_lookup) {
 1635                 free(x->w_q_lookup, M_DUMMYNET);
 1636                 x->w_q_lookup = NULL;
 1637         }
 1638         if (red_lookup_depth == 0) {
 1639                 printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
 1640                     "must be > 0\n");
 1641                 free(x, M_DUMMYNET);
 1642                 return (EINVAL);
 1643         }
 1644         x->lookup_depth = red_lookup_depth;
 1645         x->w_q_lookup = (u_int *)malloc(x->lookup_depth * sizeof(int),
 1646             M_DUMMYNET, M_NOWAIT);
 1647         if (x->w_q_lookup == NULL) {
 1648                 printf("dummynet: sorry, cannot allocate red lookup table\n");
 1649                 free(x, M_DUMMYNET);
 1650                 return(ENOSPC);
 1651         }
 1652 
 1653         /* Fill the lookup table with (1 - w_q)^x */
 1654         x->lookup_step = p->lookup_step;
 1655         x->lookup_weight = p->lookup_weight;
 1656         x->w_q_lookup[0] = SCALE(1) - x->w_q;
 1657 
 1658         for (i = 1; i < x->lookup_depth; i++)
 1659                 x->w_q_lookup[i] =
 1660                     SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight);
 1661 
 1662         if (red_avg_pkt_size < 1)
 1663                 red_avg_pkt_size = 512;
 1664         x->avg_pkt_size = red_avg_pkt_size;
 1665         if (red_max_pkt_size < 1)
 1666                 red_max_pkt_size = 1500;
 1667         x->max_pkt_size = red_max_pkt_size;
 1668         return (0);
 1669 }
 1670 
 1671 static int
 1672 alloc_hash(struct dn_flow_set *x, struct dn_flow_set *pfs)
 1673 {
 1674     if (x->flags_fs & DN_HAVE_FLOW_MASK) {     /* allocate some slots */
 1675         int l = pfs->rq_size;
 1676 
 1677         if (l == 0)
 1678             l = dn_hash_size;
 1679         if (l < 4)
 1680             l = 4;
 1681         else if (l > DN_MAX_HASH_SIZE)
 1682             l = DN_MAX_HASH_SIZE;
 1683         x->rq_size = l;
 1684     } else                  /* one is enough for null mask */
 1685         x->rq_size = 1;
 1686     x->rq = malloc((1 + x->rq_size) * sizeof(struct dn_flow_queue *),
 1687             M_DUMMYNET, M_NOWAIT | M_ZERO);
 1688     if (x->rq == NULL) {
 1689         printf("dummynet: sorry, cannot allocate queue\n");
 1690         return (ENOMEM);
 1691     }
 1692     x->rq_elements = 0;
 1693     return 0 ;
 1694 }
 1695 
 1696 static void
 1697 set_fs_parms(struct dn_flow_set *x, struct dn_flow_set *src)
 1698 {
 1699         x->flags_fs = src->flags_fs;
 1700         x->qsize = src->qsize;
 1701         x->plr = src->plr;
 1702         x->flow_mask = src->flow_mask;
 1703         if (x->flags_fs & DN_QSIZE_IS_BYTES) {
 1704                 if (x->qsize > pipe_byte_limit)
 1705                         x->qsize = 1024 * 1024;
 1706         } else {
 1707                 if (x->qsize == 0)
 1708                         x->qsize = 50;
 1709                 if (x->qsize > pipe_slot_limit)
 1710                         x->qsize = 50;
 1711         }
 1712         /* Configuring RED. */
 1713         if (x->flags_fs & DN_IS_RED)
 1714                 config_red(src, x);     /* XXX should check errors */
 1715 }
 1716 
 1717 /*
 1718  * Setup pipe or queue parameters.
 1719  */
 1720 static int
 1721 config_pipe(struct dn_pipe *p)
 1722 {
 1723         struct dn_flow_set *pfs = &(p->fs);
 1724         struct dn_flow_queue *q;
 1725         int i, error;
 1726 
 1727         /*
 1728          * The config program passes parameters as follows:
 1729          * bw = bits/second (0 means no limits),
 1730          * delay = ms, must be translated into ticks.
 1731          * qsize = slots/bytes
 1732          */
 1733         p->delay = (p->delay * hz) / 1000;
 1734         /* We need either a pipe number or a flow_set number. */
 1735         if (p->pipe_nr == 0 && pfs->fs_nr == 0)
 1736                 return (EINVAL);
 1737         if (p->pipe_nr != 0 && pfs->fs_nr != 0)
 1738                 return (EINVAL);
 1739         if (p->pipe_nr != 0) {                  /* this is a pipe */
 1740                 struct dn_pipe *pipe;
 1741 
 1742                 DUMMYNET_LOCK();
 1743                 pipe = locate_pipe(p->pipe_nr); /* locate pipe */
 1744 
 1745                 if (pipe == NULL) {             /* new pipe */
 1746                         pipe = malloc(sizeof(struct dn_pipe), M_DUMMYNET,
 1747                             M_NOWAIT | M_ZERO);
 1748                         if (pipe == NULL) {
 1749                                 DUMMYNET_UNLOCK();
 1750                                 printf("dummynet: no memory for new pipe\n");
 1751                                 return (ENOMEM);
 1752                         }
 1753                         pipe->pipe_nr = p->pipe_nr;
 1754                         pipe->fs.pipe = pipe;
 1755                         /*
 1756                          * idle_heap is the only one from which
 1757                          * we extract from the middle.
 1758                          */
 1759                         pipe->idle_heap.size = pipe->idle_heap.elements = 0;
 1760                         pipe->idle_heap.offset =
 1761                             offsetof(struct dn_flow_queue, heap_pos);
 1762                 } else
 1763                         /* Flush accumulated credit for all queues. */
 1764                         for (i = 0; i <= pipe->fs.rq_size; i++)
 1765                                 for (q = pipe->fs.rq[i]; q; q = q->next)
 1766                                         q->numbytes = io_fast ? p->bandwidth : 0;
 1767 
 1768                 pipe->bandwidth = p->bandwidth;
 1769                 pipe->numbytes = 0;             /* just in case... */
 1770                 bcopy(p->if_name, pipe->if_name, sizeof(p->if_name));
 1771                 pipe->ifp = NULL;               /* reset interface ptr */
 1772                 pipe->delay = p->delay;
 1773                 set_fs_parms(&(pipe->fs), pfs);
 1774 
 1775                 if (pipe->fs.rq == NULL) {      /* a new pipe */
 1776                         error = alloc_hash(&(pipe->fs), pfs);
 1777                         if (error) {
 1778                                 DUMMYNET_UNLOCK();
 1779                                 free(pipe, M_DUMMYNET);
 1780                                 return (error);
 1781                         }
 1782                         SLIST_INSERT_HEAD(&pipehash[HASH(pipe->pipe_nr)],
 1783                             pipe, next);
 1784                 }
 1785                 DUMMYNET_UNLOCK();
 1786         } else {                                /* config queue */
 1787                 struct dn_flow_set *fs;
 1788 
 1789                 DUMMYNET_LOCK();
 1790                 fs = locate_flowset(pfs->fs_nr); /* locate flow_set */
 1791 
 1792                 if (fs == NULL) {               /* new */
 1793                         if (pfs->parent_nr == 0) { /* need link to a pipe */
 1794                                 DUMMYNET_UNLOCK();
 1795                                 return (EINVAL);
 1796                         }
 1797                         fs = malloc(sizeof(struct dn_flow_set), M_DUMMYNET,
 1798                             M_NOWAIT | M_ZERO);
 1799                         if (fs == NULL) {
 1800                                 DUMMYNET_UNLOCK();
 1801                                 printf(
 1802                                     "dummynet: no memory for new flow_set\n");
 1803                                 return (ENOMEM);
 1804                         }
 1805                         fs->fs_nr = pfs->fs_nr;
 1806                         fs->parent_nr = pfs->parent_nr;
 1807                         fs->weight = pfs->weight;
 1808                         if (fs->weight == 0)
 1809                                 fs->weight = 1;
 1810                         else if (fs->weight > 100)
 1811                                 fs->weight = 100;
 1812                 } else {
 1813                         /*
 1814                          * Change parent pipe not allowed;
 1815                          * must delete and recreate.
 1816                          */
 1817                         if (pfs->parent_nr != 0 &&
 1818                             fs->parent_nr != pfs->parent_nr) {
 1819                                 DUMMYNET_UNLOCK();
 1820                                 return (EINVAL);
 1821                         }
 1822                 }
 1823 
 1824                 set_fs_parms(fs, pfs);
 1825 
 1826                 if (fs->rq == NULL) {           /* a new flow_set */
 1827                         error = alloc_hash(fs, pfs);
 1828                         if (error) {
 1829                                 DUMMYNET_UNLOCK();
 1830                                 free(fs, M_DUMMYNET);
 1831                                 return (error);
 1832                         }
 1833                         SLIST_INSERT_HEAD(&flowsethash[HASH(fs->fs_nr)],
 1834                             fs, next);
 1835                 }
 1836                 DUMMYNET_UNLOCK();
 1837         }
 1838         return (0);
 1839 }
 1840 
 1841 /*
 1842  * Helper function to remove from a heap queues which are linked to
 1843  * a flow_set about to be deleted.
 1844  */
 1845 static void
 1846 fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs)
 1847 {
 1848     int i = 0, found = 0 ;
 1849     for (; i < h->elements ;)
 1850         if ( ((struct dn_flow_queue *)h->p[i].object)->fs == fs) {
 1851             h->elements-- ;
 1852             h->p[i] = h->p[h->elements] ;
 1853             found++ ;
 1854         } else
 1855             i++ ;
 1856     if (found)
 1857         heapify(h);
 1858 }
 1859 
 1860 /*
 1861  * helper function to remove a pipe from a heap (can be there at most once)
 1862  */
 1863 static void
 1864 pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p)
 1865 {
 1866     if (h->elements > 0) {
 1867         int i = 0 ;
 1868         for (i=0; i < h->elements ; i++ ) {
 1869             if (h->p[i].object == p) { /* found it */
 1870                 h->elements-- ;
 1871                 h->p[i] = h->p[h->elements] ;
 1872                 heapify(h);
 1873                 break ;
 1874             }
 1875         }
 1876     }
 1877 }
 1878 
 1879 /*
 1880  * drain all queues. Called in case of severe mbuf shortage.
 1881  */
 1882 void
 1883 dummynet_drain(void)
 1884 {
 1885     struct dn_flow_set *fs;
 1886     struct dn_pipe *pipe;
 1887     struct mbuf *m, *mnext;
 1888     int i;
 1889 
 1890     DUMMYNET_LOCK_ASSERT();
 1891 
 1892     heap_free(&ready_heap);
 1893     heap_free(&wfq_ready_heap);
 1894     heap_free(&extract_heap);
 1895     /* remove all references to this pipe from flow_sets */
 1896     for (i = 0; i < HASHSIZE; i++)
 1897         SLIST_FOREACH(fs, &flowsethash[i], next)
 1898                 purge_flow_set(fs, 0);
 1899 
 1900     for (i = 0; i < HASHSIZE; i++) {
 1901         SLIST_FOREACH(pipe, &pipehash[i], next) {
 1902                 purge_flow_set(&(pipe->fs), 0);
 1903 
 1904                 mnext = pipe->head;
 1905                 while ((m = mnext) != NULL) {
 1906                         mnext = m->m_nextpkt;
 1907                         DN_FREE_PKT(m);
 1908                 }
 1909                 pipe->head = pipe->tail = NULL;
 1910         }
 1911     }
 1912 }
 1913 
 1914 /*
 1915  * Fully delete a pipe or a queue, cleaning up associated info.
 1916  */
 1917 static int
 1918 delete_pipe(struct dn_pipe *p)
 1919 {
 1920 
 1921     if (p->pipe_nr == 0 && p->fs.fs_nr == 0)
 1922         return EINVAL ;
 1923     if (p->pipe_nr != 0 && p->fs.fs_nr != 0)
 1924         return EINVAL ;
 1925     if (p->pipe_nr != 0) { /* this is an old-style pipe */
 1926         struct dn_pipe *pipe;
 1927         struct dn_flow_set *fs;
 1928         int i;
 1929 
 1930         DUMMYNET_LOCK();
 1931         pipe = locate_pipe(p->pipe_nr); /* locate pipe */
 1932 
 1933         if (pipe == NULL) {
 1934             DUMMYNET_UNLOCK();
 1935             return (ENOENT);    /* not found */
 1936         }
 1937 
 1938         /* Unlink from list of pipes. */
 1939         SLIST_REMOVE(&pipehash[HASH(pipe->pipe_nr)], pipe, dn_pipe, next);
 1940 
 1941         /* Remove all references to this pipe from flow_sets. */
 1942         for (i = 0; i < HASHSIZE; i++)
 1943             SLIST_FOREACH(fs, &flowsethash[i], next)
 1944                 if (fs->pipe == pipe) {
 1945                         printf("dummynet: ++ ref to pipe %d from fs %d\n",
 1946                             p->pipe_nr, fs->fs_nr);
 1947                         fs->pipe = NULL ;
 1948                         purge_flow_set(fs, 0);
 1949                 }
 1950         fs_remove_from_heap(&ready_heap, &(pipe->fs));
 1951         purge_pipe(pipe); /* remove all data associated to this pipe */
 1952         /* remove reference to here from extract_heap and wfq_ready_heap */
 1953         pipe_remove_from_heap(&extract_heap, pipe);
 1954         pipe_remove_from_heap(&wfq_ready_heap, pipe);
 1955         DUMMYNET_UNLOCK();
 1956 
 1957         free(pipe, M_DUMMYNET);
 1958     } else { /* this is a WF2Q queue (dn_flow_set) */
 1959         struct dn_flow_set *fs;
 1960 
 1961         DUMMYNET_LOCK();
 1962         fs = locate_flowset(p->fs.fs_nr); /* locate set */
 1963 
 1964         if (fs == NULL) {
 1965             DUMMYNET_UNLOCK();
 1966             return (ENOENT); /* not found */
 1967         }
 1968 
 1969         /* Unlink from list of flowsets. */
 1970         SLIST_REMOVE( &flowsethash[HASH(fs->fs_nr)], fs, dn_flow_set, next);
 1971 
 1972         if (fs->pipe != NULL) {
 1973             /* Update total weight on parent pipe and cleanup parent heaps. */
 1974             fs->pipe->sum -= fs->weight * fs->backlogged ;
 1975             fs_remove_from_heap(&(fs->pipe->not_eligible_heap), fs);
 1976             fs_remove_from_heap(&(fs->pipe->scheduler_heap), fs);
 1977 #if 1   /* XXX should i remove from idle_heap as well ? */
 1978             fs_remove_from_heap(&(fs->pipe->idle_heap), fs);
 1979 #endif
 1980         }
 1981         purge_flow_set(fs, 1);
 1982         DUMMYNET_UNLOCK();
 1983     }
 1984     return 0 ;
 1985 }
 1986 
 1987 /*
 1988  * helper function used to copy data from kernel in DUMMYNET_GET
 1989  */
 1990 static char *
 1991 dn_copy_set(struct dn_flow_set *set, char *bp)
 1992 {
 1993     int i, copied = 0 ;
 1994     struct dn_flow_queue *q, *qp = (struct dn_flow_queue *)bp;
 1995 
 1996     DUMMYNET_LOCK_ASSERT();
 1997 
 1998     for (i = 0 ; i <= set->rq_size ; i++)
 1999         for (q = set->rq[i] ; q ; q = q->next, qp++ ) {
 2000             if (q->hash_slot != i)
 2001                 printf("dummynet: ++ at %d: wrong slot (have %d, "
 2002                     "should be %d)\n", copied, q->hash_slot, i);
 2003             if (q->fs != set)
 2004                 printf("dummynet: ++ at %d: wrong fs ptr (have %p, should be %p)\n",
 2005                         i, q->fs, set);
 2006             copied++ ;
 2007             bcopy(q, qp, sizeof( *q ) );
 2008             /* cleanup pointers */
 2009             qp->next = NULL ;
 2010             qp->head = qp->tail = NULL ;
 2011             qp->fs = NULL ;
 2012         }
 2013     if (copied != set->rq_elements)
 2014         printf("dummynet: ++ wrong count, have %d should be %d\n",
 2015             copied, set->rq_elements);
 2016     return (char *)qp ;
 2017 }
 2018 
 2019 static size_t
 2020 dn_calc_size(void)
 2021 {
 2022     struct dn_flow_set *fs;
 2023     struct dn_pipe *pipe;
 2024     size_t size = 0;
 2025     int i;
 2026 
 2027     DUMMYNET_LOCK_ASSERT();
 2028     /*
 2029      * Compute size of data structures: list of pipes and flow_sets.
 2030      */
 2031     for (i = 0; i < HASHSIZE; i++) {
 2032         SLIST_FOREACH(pipe, &pipehash[i], next)
 2033                 size += sizeof(*pipe) +
 2034                     pipe->fs.rq_elements * sizeof(struct dn_flow_queue);
 2035         SLIST_FOREACH(fs, &flowsethash[i], next)
 2036                 size += sizeof (*fs) +
 2037                     fs->rq_elements * sizeof(struct dn_flow_queue);
 2038     }
 2039     return size;
 2040 }
 2041 
 2042 static int
 2043 dummynet_get(struct sockopt *sopt)
 2044 {
 2045     char *buf, *bp ; /* bp is the "copy-pointer" */
 2046     size_t size ;
 2047     struct dn_flow_set *fs;
 2048     struct dn_pipe *pipe;
 2049     int error=0, i ;
 2050 
 2051     /* XXX lock held too long */
 2052     DUMMYNET_LOCK();
 2053     /*
 2054      * XXX: Ugly, but we need to allocate memory with M_WAITOK flag and we
 2055      *      cannot use this flag while holding a mutex.
 2056      */
 2057     for (i = 0; i < 10; i++) {
 2058         size = dn_calc_size();
 2059         DUMMYNET_UNLOCK();
 2060         buf = malloc(size, M_TEMP, M_WAITOK);
 2061         DUMMYNET_LOCK();
 2062         if (size == dn_calc_size())
 2063                 break;
 2064         free(buf, M_TEMP);
 2065         buf = NULL;
 2066     }
 2067     if (buf == NULL) {
 2068         DUMMYNET_UNLOCK();
 2069         return ENOBUFS ;
 2070     }
 2071     bp = buf;
 2072     for (i = 0; i < HASHSIZE; i++)
 2073         SLIST_FOREACH(pipe, &pipehash[i], next) {
 2074                 struct dn_pipe *pipe_bp = (struct dn_pipe *)bp;
 2075 
 2076                 /*
 2077                  * Copy pipe descriptor into *bp, convert delay back to ms,
 2078                  * then copy the flow_set descriptor(s) one at a time.
 2079                  * After each flow_set, copy the queue descriptor it owns.
 2080                  */
 2081                 bcopy(pipe, bp, sizeof(*pipe));
 2082                 pipe_bp->delay = (pipe_bp->delay * 1000) / hz;
 2083                 /*
 2084                  * XXX the following is a hack based on ->next being the
 2085                  * first field in dn_pipe and dn_flow_set. The correct
 2086                  * solution would be to move the dn_flow_set to the beginning
 2087                  * of struct dn_pipe.
 2088                  */
 2089                 pipe_bp->next.sle_next = (struct dn_pipe *)DN_IS_PIPE;
 2090                 /* Clean pointers. */
 2091                 pipe_bp->head = pipe_bp->tail = NULL;
 2092                 pipe_bp->fs.next.sle_next = NULL;
 2093                 pipe_bp->fs.pipe = NULL;
 2094                 pipe_bp->fs.rq = NULL;
 2095 
 2096                 bp += sizeof(*pipe) ;
 2097                 bp = dn_copy_set(&(pipe->fs), bp);
 2098         }
 2099 
 2100     for (i = 0; i < HASHSIZE; i++)
 2101         SLIST_FOREACH(fs, &flowsethash[i], next) {
 2102                 struct dn_flow_set *fs_bp = (struct dn_flow_set *)bp;
 2103 
 2104                 bcopy(fs, bp, sizeof(*fs));
 2105                 /* XXX same hack as above */
 2106                 fs_bp->next.sle_next = (struct dn_flow_set *)DN_IS_QUEUE;
 2107                 fs_bp->pipe = NULL;
 2108                 fs_bp->rq = NULL;
 2109                 bp += sizeof(*fs);
 2110                 bp = dn_copy_set(fs, bp);
 2111         }
 2112 
 2113     DUMMYNET_UNLOCK();
 2114 
 2115     error = sooptcopyout(sopt, buf, size);
 2116     free(buf, M_TEMP);
 2117     return error ;
 2118 }
 2119 
 2120 /*
 2121  * Handler for the various dummynet socket options (get, flush, config, del)
 2122  */
 2123 static int
 2124 ip_dn_ctl(struct sockopt *sopt)
 2125 {
 2126     int error = 0 ;
 2127     struct dn_pipe *p, tmp_pipe;
 2128 
 2129     error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
 2130     if (error)
 2131         return (error);
 2132 
 2133     /* Disallow sets in really-really secure mode. */
 2134     if (sopt->sopt_dir == SOPT_SET) {
 2135 #if __FreeBSD_version >= 500034
 2136         error =  securelevel_ge(sopt->sopt_td->td_ucred, 3);
 2137         if (error)
 2138             return (error);
 2139 #else
 2140         if (securelevel >= 3)
 2141             return (EPERM);
 2142 #endif
 2143     }
 2144 
 2145     switch (sopt->sopt_name) {
 2146     default :
 2147         printf("dummynet: -- unknown option %d", sopt->sopt_name);
 2148         return EINVAL ;
 2149 
 2150     case IP_DUMMYNET_GET :
 2151         error = dummynet_get(sopt);
 2152         break ;
 2153 
 2154     case IP_DUMMYNET_FLUSH :
 2155         dummynet_flush() ;
 2156         break ;
 2157 
 2158     case IP_DUMMYNET_CONFIGURE :
 2159         p = &tmp_pipe ;
 2160         error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
 2161         if (error)
 2162             break ;
 2163         error = config_pipe(p);
 2164         break ;
 2165 
 2166     case IP_DUMMYNET_DEL :      /* remove a pipe or queue */
 2167         p = &tmp_pipe ;
 2168         error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
 2169         if (error)
 2170             break ;
 2171 
 2172         error = delete_pipe(p);
 2173         break ;
 2174     }
 2175     return error ;
 2176 }
 2177 
 2178 static void
 2179 ip_dn_init(void)
 2180 {
 2181         int i;
 2182 
 2183         if (bootverbose)
 2184                 printf("DUMMYNET with IPv6 initialized (040826)\n");
 2185 
 2186         DUMMYNET_LOCK_INIT();
 2187 
 2188         for (i = 0; i < HASHSIZE; i++) {
 2189                 SLIST_INIT(&pipehash[i]);
 2190                 SLIST_INIT(&flowsethash[i]);
 2191         }
 2192         ready_heap.size = ready_heap.elements = 0;
 2193         ready_heap.offset = 0;
 2194 
 2195         wfq_ready_heap.size = wfq_ready_heap.elements = 0;
 2196         wfq_ready_heap.offset = 0;
 2197 
 2198         extract_heap.size = extract_heap.elements = 0;
 2199         extract_heap.offset = 0;
 2200 
 2201         ip_dn_ctl_ptr = ip_dn_ctl;
 2202         ip_dn_io_ptr = dummynet_io;
 2203         ip_dn_ruledel_ptr = dn_rule_delete;
 2204 
 2205         TASK_INIT(&dn_task, 0, dummynet_task, NULL);
 2206         dn_tq = taskqueue_create_fast("dummynet", M_NOWAIT,
 2207             taskqueue_thread_enqueue, &dn_tq);
 2208         taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
 2209 
 2210         callout_init(&dn_timeout, CALLOUT_MPSAFE);
 2211         callout_reset(&dn_timeout, 1, dummynet, NULL);
 2212 
 2213         /* Initialize curr_time adjustment mechanics. */
 2214         getmicrouptime(&prev_t);
 2215 }
 2216 
 2217 #ifdef KLD_MODULE
 2218 static void
 2219 ip_dn_destroy(void)
 2220 {
 2221         ip_dn_ctl_ptr = NULL;
 2222         ip_dn_io_ptr = NULL;
 2223         ip_dn_ruledel_ptr = NULL;
 2224 
 2225         DUMMYNET_LOCK();
 2226         callout_stop(&dn_timeout);
 2227         DUMMYNET_UNLOCK();
 2228         taskqueue_drain(dn_tq, &dn_task);
 2229         taskqueue_free(dn_tq);
 2230 
 2231         dummynet_flush();
 2232 
 2233         DUMMYNET_LOCK_DESTROY();
 2234 }
 2235 #endif /* KLD_MODULE */
 2236 
 2237 static int
 2238 dummynet_modevent(module_t mod, int type, void *data)
 2239 {
 2240 
 2241         switch (type) {
 2242         case MOD_LOAD:
 2243                 if (DUMMYNET_LOADED) {
 2244                     printf("DUMMYNET already loaded\n");
 2245                     return EEXIST ;
 2246                 }
 2247                 ip_dn_init();
 2248                 break;
 2249 
 2250         case MOD_UNLOAD:
 2251 #if !defined(KLD_MODULE)
 2252                 printf("dummynet statically compiled, cannot unload\n");
 2253                 return EINVAL ;
 2254 #else
 2255                 ip_dn_destroy();
 2256 #endif
 2257                 break ;
 2258         default:
 2259                 return EOPNOTSUPP;
 2260                 break ;
 2261         }
 2262         return 0 ;
 2263 }
 2264 
 2265 static moduledata_t dummynet_mod = {
 2266         "dummynet",
 2267         dummynet_modevent,
 2268         NULL
 2269 };
 2270 DECLARE_MODULE(dummynet, dummynet_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
 2271 MODULE_DEPEND(dummynet, ipfw, 2, 2, 2);
 2272 MODULE_VERSION(dummynet, 1);

Cache object: b7981b27773d085304b9cb1f6cbf374e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.