1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Copyright (c) 1998-2000 Luigi Rizzo, Universita` di Pisa
|
|
|
|
* Portions Copyright (c) 2000 Akamba Corp.
|
|
|
|
* All rights reserved
|
1998-09-12 22:03:21 +00:00
|
|
|
*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1998-09-12 22:03:21 +00:00
|
|
|
*
|
2000-01-08 11:24:46 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1998-09-12 22:03:21 +00:00
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
#define DEB(x)
|
|
|
|
#define DDB(x) x
|
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
|
|
|
* This module implements IP dummynet, a bandwidth limiter/delay emulator
|
|
|
|
* used in conjunction with the ipfw package.
|
|
|
|
*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Most important Changes:
|
1998-09-12 22:03:21 +00:00
|
|
|
*
|
2000-01-08 11:24:46 +00:00
|
|
|
* 000106: large rewrite, use heaps to handle very many pipes.
|
1998-09-12 22:03:21 +00:00
|
|
|
* 980513: initial release
|
2000-01-08 11:24:46 +00:00
|
|
|
*
|
|
|
|
* include files marked with XXX are probably not needed
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/queue.h> /* XXX */
|
|
|
|
#include <sys/kernel.h>
|
1999-04-20 13:32:06 +00:00
|
|
|
#include <sys/module.h>
|
1998-09-12 22:03:21 +00:00
|
|
|
#include <sys/socket.h>
|
1998-12-14 18:09:13 +00:00
|
|
|
#include <sys/socketvar.h>
|
1998-09-12 22:03:21 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_fw.h>
|
|
|
|
#include <netinet/ip_dummynet.h>
|
1999-01-12 12:27:54 +00:00
|
|
|
#include <netinet/ip_var.h>
|
1998-09-12 22:03:21 +00:00
|
|
|
|
1999-03-24 12:43:39 +00:00
|
|
|
#include "opt_bdg.h"
|
1998-09-12 22:03:21 +00:00
|
|
|
#ifdef BRIDGE
|
|
|
|
#include <netinet/if_ether.h> /* for struct arpcom */
|
|
|
|
#include <net/bridge.h>
|
|
|
|
#endif
|
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* we keep a private variable for the simulation time, but probably
|
|
|
|
* it would be better to use the already existing one "softticks"
|
|
|
|
* (in sys/kern/kern_timer.c)
|
|
|
|
*/
|
|
|
|
static dn_key curr_time = 0 ; /* current simulation time */
|
|
|
|
|
|
|
|
static int dn_hash_size = 64 ; /* default hash size */
|
|
|
|
|
|
|
|
/* statistics on number of queue searches and search steps */
|
|
|
|
static int searches, search_steps ;
|
2000-02-10 14:17:40 +00:00
|
|
|
static int pipe_expire = 1 ; /* expire queue if empty */
|
2000-01-08 11:24:46 +00:00
|
|
|
|
|
|
|
static struct dn_heap ready_heap, extract_heap ;
|
|
|
|
static int heap_init(struct dn_heap *h, int size) ;
|
|
|
|
static int heap_insert (struct dn_heap *h, dn_key key1, void *p);
|
|
|
|
static void heap_extract(struct dn_heap *h);
|
|
|
|
static void transmit_event(struct dn_pipe *pipe);
|
|
|
|
static void ready_event(struct dn_flow_queue *q);
|
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
static struct dn_pipe *all_pipes = NULL ; /* list of all pipes */
|
|
|
|
|
|
|
|
#ifdef SYSCTL_NODE
|
2000-01-08 11:24:46 +00:00
|
|
|
SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet,
|
|
|
|
CTLFLAG_RW, 0, "Dummynet");
|
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size,
|
2000-02-10 14:17:40 +00:00
|
|
|
CTLFLAG_RW, &dn_hash_size, 0, "Default hash table size");
|
2000-01-08 11:24:46 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, curr_time,
|
|
|
|
CTLFLAG_RD, &curr_time, 0, "Current tick");
|
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, ready_heap,
|
|
|
|
CTLFLAG_RD, &ready_heap.size, 0, "Size of ready heap");
|
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, extract_heap,
|
|
|
|
CTLFLAG_RD, &extract_heap.size, 0, "Size of extract heap");
|
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, searches,
|
|
|
|
CTLFLAG_RD, &searches, 0, "Number of queue searches");
|
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, search_steps,
|
|
|
|
CTLFLAG_RD, &search_steps, 0, "Number of queue search steps");
|
2000-02-10 14:17:40 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire,
|
|
|
|
CTLFLAG_RW, &pipe_expire, 0, "Expire queue if empty");
|
1998-09-12 22:03:21 +00:00
|
|
|
#endif
|
|
|
|
|
1998-12-14 18:09:13 +00:00
|
|
|
static int ip_dn_ctl(struct sockopt *sopt);
|
1998-09-12 22:03:21 +00:00
|
|
|
|
1999-01-11 11:08:07 +00:00
|
|
|
static void rt_unref(struct rtentry *);
|
1999-01-12 12:27:54 +00:00
|
|
|
static void dummynet(void *);
|
|
|
|
static void dummynet_flush(void);
|
1998-09-12 22:03:21 +00:00
|
|
|
|
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* ip_fw_chain is used when deleting a pipe, because ipfw rules can
|
1998-09-12 22:03:21 +00:00
|
|
|
* hold references to the pipe.
|
|
|
|
*/
|
|
|
|
extern LIST_HEAD (ip_fw_head, ip_fw_chain) ip_fw_chain;
|
|
|
|
|
1999-01-11 11:08:07 +00:00
|
|
|
static void
|
|
|
|
rt_unref(struct rtentry *rt)
|
|
|
|
{
|
|
|
|
if (rt == NULL)
|
|
|
|
return ;
|
|
|
|
if (rt->rt_refcnt <= 0)
|
1999-08-17 22:10:00 +00:00
|
|
|
printf("-- warning, refcnt now %ld, decreasing\n", rt->rt_refcnt);
|
1999-01-11 11:08:07 +00:00
|
|
|
RTFREE(rt);
|
|
|
|
}
|
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Heap management functions.
|
|
|
|
*
|
|
|
|
* In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
|
|
|
|
* Some macros help finding parent/children so we can optimize them.
|
2000-02-10 14:17:40 +00:00
|
|
|
*
|
2000-01-08 11:24:46 +00:00
|
|
|
* heap_init() is called to expand the heap when needed.
|
|
|
|
* Increment size in blocks of 256 entries (which make one 4KB page)
|
|
|
|
* XXX failure to allocate a new element is a pretty bad failure
|
|
|
|
* as we basically stall a whole queue forever!!
|
|
|
|
* Returns 1 on error, 0 on success
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
#define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 )
|
|
|
|
#define HEAP_LEFT(x) ( 2*(x) + 1 )
|
|
|
|
#define HEAP_IS_LEFT(x) ( (x) & 1 )
|
|
|
|
#define HEAP_RIGHT(x) ( 2*(x) + 1 )
|
|
|
|
#define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
|
|
|
|
#define HEAP_INCREMENT 255
|
|
|
|
|
|
|
|
static int
|
|
|
|
heap_init(struct dn_heap *h, int new_size)
|
|
|
|
{
|
|
|
|
struct dn_heap_entry *p;
|
|
|
|
|
|
|
|
if (h->size >= new_size ) {
|
|
|
|
printf("heap_init, Bogus call, have %d want %d\n",
|
|
|
|
h->size, new_size);
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT ;
|
|
|
|
p = malloc(new_size * sizeof(*p), M_IPFW, M_DONTWAIT );
|
|
|
|
if (p == NULL) {
|
|
|
|
printf(" heap_init, resize %d failed\n", new_size );
|
|
|
|
return 1 ; /* error */
|
|
|
|
}
|
|
|
|
if (h->size > 0) {
|
|
|
|
bcopy(h->p, p, h->size * sizeof(*p) );
|
|
|
|
free(h->p, M_IPFW);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
h->p = p ;
|
|
|
|
h->size = new_size ;
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert element in heap. Normally, p != NULL, we insert p in
|
|
|
|
* a new position and bubble up. If p == NULL, then the element is
|
|
|
|
* already in place, and key is the position where to start the
|
|
|
|
* bubble-up.
|
|
|
|
* Returns 1 on failure (cannot allocate new heap entry)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
heap_insert(struct dn_heap *h, dn_key key1, void *p)
|
|
|
|
{
|
|
|
|
int son = h->elements ;
|
|
|
|
|
|
|
|
if (p == NULL) /* data already there, set starting point */
|
|
|
|
son = key1 ;
|
|
|
|
else { /* insert new element at the end, possibly resize */
|
|
|
|
son = h->elements ;
|
|
|
|
if (son == h->size) /* need resize... */
|
|
|
|
if (heap_init(h, h->elements+1) )
|
|
|
|
return 1 ; /* failure... */
|
|
|
|
h->p[son].object = p ;
|
|
|
|
h->p[son].key = key1 ;
|
|
|
|
h->elements++ ;
|
|
|
|
}
|
|
|
|
while (son > 0) { /* bubble up */
|
|
|
|
int father = HEAP_FATHER(son) ;
|
|
|
|
struct dn_heap_entry tmp ;
|
|
|
|
|
|
|
|
if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
|
|
|
|
break ; /* found right position */
|
|
|
|
/* son smaller than father, swap and try again */
|
|
|
|
HEAP_SWAP(h->p[son], h->p[father], tmp) ;
|
|
|
|
son = father ;
|
|
|
|
}
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove top element from heap
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
heap_extract(struct dn_heap *h)
|
|
|
|
{
|
|
|
|
int child, father, max = h->elements - 1 ;
|
|
|
|
if (max < 0)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
/* move up smallest child */
|
|
|
|
father = 0 ;
|
|
|
|
child = HEAP_LEFT(father) ; /* left child */
|
|
|
|
while (child <= max) { /* valid entry */
|
|
|
|
if (child != max && DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
|
|
|
|
child = child+1 ; /* take right child, otherwise left */
|
|
|
|
h->p[father] = h->p[child] ;
|
|
|
|
father = child ;
|
|
|
|
child = HEAP_LEFT(child) ; /* left child for next loop */
|
|
|
|
}
|
|
|
|
h->elements-- ;
|
|
|
|
if (father != max) {
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Fill hole with last entry and bubble up, reusing the insert code
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
h->p[father] = h->p[max] ;
|
|
|
|
heap_insert(h, father, NULL); /* this one cannot fail */
|
|
|
|
}
|
|
|
|
}
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* heapify() will reorganize data inside an array to maintain the
|
|
|
|
* heap property. It is needed when we delete a bunch of entries.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
heapify(struct dn_heap *h)
|
|
|
|
{
|
|
|
|
int father, i ;
|
|
|
|
struct dn_heap_entry tmp ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
for (i = h->elements - 1 ; i > 0 ; i-- ) {
|
|
|
|
father = HEAP_FATHER(i) ;
|
|
|
|
if ( DN_KEY_LT(h->p[i].key, h->p[father].key) )
|
|
|
|
HEAP_SWAP(h->p[father], h->p[i], tmp) ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* --- end of heap management functions ---
|
|
|
|
*/
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* Scheduler functions -- transmit_event(), ready_event()
|
|
|
|
*
|
|
|
|
* transmit_event() is called when the delay-line needs to enter
|
|
|
|
* the scheduler, either because of existing pkts getting ready,
|
|
|
|
* or new packets entering the queue. The event handled is the delivery
|
|
|
|
* time of the packet.
|
|
|
|
*
|
|
|
|
* ready_event() does something similar with flow queues, and the
|
|
|
|
* event handled is the finish time of the head pkt.
|
|
|
|
*
|
|
|
|
* In both cases, we make sure that the data structures are consistent
|
|
|
|
* before passing pkts out, because this might trigger recursive
|
|
|
|
* invocations of the procedures.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
transmit_event(struct dn_pipe *pipe)
|
|
|
|
{
|
|
|
|
struct dn_pkt *pkt ;
|
|
|
|
|
|
|
|
while ( (pkt = pipe->p.head) && DN_KEY_LEQ(pkt->output_time, curr_time) ) {
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* first unlink, then call procedures, since ip_input() can invoke
|
|
|
|
* ip_output() and viceversa, thus causing nested calls
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
pipe->p.head = DN_NEXT(pkt) ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* The actual mbuf is preceded by a struct dn_pkt, resembling an mbuf
|
|
|
|
* (NOT A REAL one, just a small block of malloc'ed memory) with
|
|
|
|
* m_type = MT_DUMMYNET
|
|
|
|
* m_next = actual mbuf to be processed by ip_input/output
|
|
|
|
* m_data = the matching rule
|
|
|
|
* and some other fields.
|
|
|
|
* The block IS FREED HERE because it contains parameters passed
|
|
|
|
* to the called routine.
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
|
|
|
switch (pkt->dn_dir) {
|
2000-01-08 11:24:46 +00:00
|
|
|
case DN_TO_IP_OUT:
|
|
|
|
(void)ip_output((struct mbuf *)pkt, NULL, NULL, 0, NULL);
|
|
|
|
rt_unref (pkt->ro.ro_rt) ;
|
1998-09-12 22:03:21 +00:00
|
|
|
break ;
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
case DN_TO_IP_IN :
|
|
|
|
ip_input((struct mbuf *)pkt) ;
|
|
|
|
break ;
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
#ifdef BRIDGE
|
1999-05-04 07:30:08 +00:00
|
|
|
case DN_TO_BDG_FWD : {
|
|
|
|
struct mbuf *m = (struct mbuf *)pkt ;
|
|
|
|
bdg_forward(&m, pkt->ifp);
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
}
|
1998-09-12 22:03:21 +00:00
|
|
|
break ;
|
|
|
|
#endif
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
default:
|
|
|
|
printf("dummynet: bad switch %d!\n", pkt->dn_dir);
|
|
|
|
m_freem(pkt->dn_m);
|
|
|
|
break ;
|
|
|
|
}
|
1999-05-04 16:20:33 +00:00
|
|
|
FREE(pkt, M_IPFW);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
/* if there are leftover packets, put into the heap for next event */
|
|
|
|
if ( (pkt = pipe->p.head) )
|
|
|
|
heap_insert(&extract_heap, pkt->output_time, pipe ) ;
|
|
|
|
/* XXX should check errors on heap_insert, by draining the
|
|
|
|
* whole pipe p and hoping in the future we are more successful
|
|
|
|
*/
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* ready_event() is invoked every time the queue must enter the
|
|
|
|
* scheduler, either because the first packet arrives, or because
|
|
|
|
* a previously scheduled event fired.
|
|
|
|
* On invokation, drain as many pkts as possible (could be 0) and then
|
|
|
|
* if there are leftover packets reinsert the pkt in the scheduler.
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
static void
|
|
|
|
ready_event(struct dn_flow_queue *q)
|
1998-09-12 22:03:21 +00:00
|
|
|
{
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_pkt *pkt;
|
|
|
|
struct dn_pipe *p = q->p ;
|
|
|
|
int p_was_empty = (p->p.head == NULL) ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
while ( (pkt = q->r.head) != NULL ) {
|
|
|
|
int len = pkt->dn_m->m_pkthdr.len;
|
|
|
|
int len_scaled = p->bandwidth ? len*8*hz : 0 ;
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* bandwidth==0 (no limit) means we can drain as many pkts as
|
|
|
|
* needed from the queue. Setting len_scaled = 0 does the job.
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
if (len_scaled > q->numbytes )
|
|
|
|
break ;
|
|
|
|
/*
|
|
|
|
* extract pkt from queue, compute output time (could be now)
|
|
|
|
* and put into delay line (p_queue)
|
|
|
|
*/
|
|
|
|
q->numbytes -= len_scaled ;
|
|
|
|
q->r.head = DN_NEXT(pkt) ;
|
|
|
|
q->len-- ;
|
|
|
|
q->len_bytes -= len ;
|
|
|
|
|
|
|
|
pkt->output_time = curr_time + p->delay ;
|
|
|
|
|
|
|
|
if (p->p.head == NULL)
|
|
|
|
p->p.head = pkt;
|
|
|
|
else
|
|
|
|
DN_NEXT(p->p.tail) = pkt;
|
|
|
|
p->p.tail = pkt;
|
|
|
|
DN_NEXT(p->p.tail) = NULL;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* If we have more packets queued, schedule next ready event
|
|
|
|
* (can only occur when bandwidth != 0, otherwise we would have
|
|
|
|
* flushed the whole queue in the previous loop).
|
|
|
|
* To this purpose compute how many ticks to go for the next
|
|
|
|
* event, accounting for packet size and residual credit. This means
|
|
|
|
* we compute the finish time of the packet.
|
|
|
|
*/
|
|
|
|
if ( (pkt = q->r.head) != NULL ) { /* this implies bandwidth != 0 */
|
|
|
|
dn_key t ;
|
|
|
|
t = (pkt->dn_m->m_pkthdr.len*8*hz - q->numbytes + p->bandwidth - 1 ) /
|
|
|
|
p->bandwidth ;
|
|
|
|
q->numbytes += t * p->bandwidth ;
|
|
|
|
heap_insert(&ready_heap, curr_time + t, (void *)q );
|
|
|
|
/* XXX should check errors on heap_insert, and drain the whole
|
|
|
|
* queue on error hoping next time we are luckier.
|
|
|
|
*/
|
|
|
|
}
|
2000-02-04 16:45:33 +00:00
|
|
|
/*
|
|
|
|
* If the delay line was empty call transmit_event(p) now.
|
|
|
|
* Otherwise, the scheduler will take care of it.
|
|
|
|
*/
|
|
|
|
if (p_was_empty)
|
|
|
|
transmit_event(p);
|
2000-01-08 11:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is called once per tick, or HZ times per second. It is used to
|
|
|
|
* increment the current tick counter and schedule expired events.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dummynet(void * __unused unused)
|
|
|
|
{
|
|
|
|
void *p ; /* generic parameter to handler */
|
|
|
|
struct dn_heap *h ;
|
|
|
|
int s ;
|
|
|
|
|
|
|
|
s = splnet(); /* avoid network interrupts... */
|
|
|
|
curr_time++ ;
|
|
|
|
h = &ready_heap ;
|
|
|
|
while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time) ) {
|
|
|
|
/*
|
|
|
|
* XXX if the event is late, we should probably credit the queue
|
|
|
|
* by q->p->bandwidth * (delta_ticks). On the other hand, i dont
|
|
|
|
* think this can ever occur with this code (i.e. curr_time will
|
|
|
|
* still be incremented by one at each tick. Things might be
|
|
|
|
* different if we were using the counter from the high priority
|
|
|
|
* timer.
|
|
|
|
*/
|
|
|
|
if (h->p[0].key != curr_time)
|
|
|
|
printf("-- dummynet: warning, event is %d ticks late\n",
|
|
|
|
curr_time - h->p[0].key);
|
|
|
|
p = h->p[0].object ;
|
|
|
|
heap_extract(h); /* need to extract before processing */
|
|
|
|
ready_event(p) ;
|
|
|
|
}
|
|
|
|
h = &extract_heap ;
|
|
|
|
while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time) ) {
|
|
|
|
if (h->p[0].key != curr_time) /* XXX same as above */
|
|
|
|
printf("-- dummynet: warning, event is %d ticks late\n",
|
|
|
|
curr_time - h->p[0].key);
|
|
|
|
p = h->p[0].object ;
|
|
|
|
heap_extract(&extract_heap);
|
|
|
|
transmit_event(p);
|
|
|
|
}
|
1999-08-11 14:37:58 +00:00
|
|
|
splx(s);
|
2000-01-08 11:24:46 +00:00
|
|
|
timeout(dummynet, NULL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-02-10 14:17:40 +00:00
|
|
|
* Given a pipe and a pkt in last_pkt, find a matching queue
|
2000-01-08 11:24:46 +00:00
|
|
|
* after appropriate masking. The queue is moved to front
|
|
|
|
* so that further searches take less time.
|
|
|
|
* XXX if the queue is longer than some threshold should consider
|
|
|
|
* purging old unused entries. They will get in the way every time
|
|
|
|
* we have a new flow.
|
|
|
|
*/
|
|
|
|
static struct dn_flow_queue *
|
|
|
|
find_queue(struct dn_pipe *pipe)
|
|
|
|
{
|
|
|
|
int i = 0 ; /* we need i and q for new allocations */
|
|
|
|
struct dn_flow_queue *q, *prev;
|
|
|
|
|
|
|
|
if ( !(pipe->flags & DN_HAVE_FLOW_MASK) )
|
|
|
|
q = pipe->rq[0] ;
|
|
|
|
else {
|
|
|
|
/* first, do the masking */
|
2000-02-10 14:17:40 +00:00
|
|
|
last_pkt.dst_ip &= pipe->flow_mask.dst_ip ;
|
|
|
|
last_pkt.src_ip &= pipe->flow_mask.src_ip ;
|
|
|
|
last_pkt.dst_port &= pipe->flow_mask.dst_port ;
|
|
|
|
last_pkt.src_port &= pipe->flow_mask.src_port ;
|
|
|
|
last_pkt.proto &= pipe->flow_mask.proto ;
|
2000-02-11 13:23:14 +00:00
|
|
|
last_pkt.flags = 0 ; /* we don't care about this one */
|
2000-01-08 11:24:46 +00:00
|
|
|
/* then, hash function */
|
2000-02-10 14:17:40 +00:00
|
|
|
i = ( (last_pkt.dst_ip) & 0xffff ) ^
|
|
|
|
( (last_pkt.dst_ip >> 15) & 0xffff ) ^
|
|
|
|
( (last_pkt.src_ip << 1) & 0xffff ) ^
|
|
|
|
( (last_pkt.src_ip >> 16 ) & 0xffff ) ^
|
|
|
|
(last_pkt.dst_port << 1) ^ (last_pkt.src_port) ^
|
|
|
|
(last_pkt.proto );
|
2000-01-08 11:24:46 +00:00
|
|
|
i = i % pipe->rq_size ;
|
|
|
|
/* finally, scan the current list for a match */
|
|
|
|
searches++ ;
|
2000-02-10 14:17:40 +00:00
|
|
|
for (prev=NULL, q = pipe->rq[i] ; q ; ) {
|
2000-01-08 11:24:46 +00:00
|
|
|
search_steps++;
|
2000-02-10 14:17:40 +00:00
|
|
|
if (bcmp(&last_pkt, &(q->id), sizeof(q->id) ) == 0)
|
2000-01-08 11:24:46 +00:00
|
|
|
break ; /* found */
|
2000-02-10 14:17:40 +00:00
|
|
|
else if (pipe_expire && q->r.head == NULL) {
|
|
|
|
/* entry is idle, expire it */
|
|
|
|
struct dn_flow_queue *old_q = q ;
|
|
|
|
|
|
|
|
if (prev != NULL)
|
|
|
|
prev->next = q = q->next ;
|
|
|
|
else
|
|
|
|
pipe->rq[i] = q = q->next ;
|
|
|
|
pipe->rq_elements-- ;
|
|
|
|
free(old_q, M_IPFW);
|
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
prev = q ;
|
|
|
|
q = q->next ;
|
2000-01-08 11:24:46 +00:00
|
|
|
}
|
|
|
|
if (q && prev != NULL) { /* found and not in front */
|
|
|
|
prev->next = q->next ;
|
|
|
|
q->next = pipe->rq[i] ;
|
|
|
|
pipe->rq[i] = q ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (q == NULL) { /* no match, need to allocate a new entry */
|
|
|
|
q = malloc(sizeof(*q), M_IPFW, M_DONTWAIT) ;
|
|
|
|
if (q == NULL) {
|
|
|
|
printf("sorry, cannot allocate new flow\n");
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
bzero(q, sizeof(*q) ); /* needed */
|
2000-02-10 14:17:40 +00:00
|
|
|
q->id = last_pkt ;
|
2000-01-08 11:24:46 +00:00
|
|
|
q->p = pipe ;
|
|
|
|
q->hash_slot = i ;
|
|
|
|
q->next = pipe->rq[i] ;
|
|
|
|
pipe->rq[i] = q ;
|
|
|
|
pipe->rq_elements++ ;
|
|
|
|
DEB(printf("++ new queue (%d) for 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
|
|
|
|
pipe->rq_elements,
|
2000-02-10 14:17:40 +00:00
|
|
|
last_pkt.src_ip, last_pkt.src_port,
|
|
|
|
last_pkt.dst_ip, last_pkt.dst_port); )
|
2000-01-08 11:24:46 +00:00
|
|
|
}
|
|
|
|
return q ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dummynet hook for packets.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dummynet_io(int pipe_nr, int dir,
|
1999-05-04 07:30:08 +00:00
|
|
|
struct mbuf *m, struct ifnet *ifp, struct route *ro,
|
|
|
|
struct sockaddr_in *dst,
|
1999-12-22 19:13:38 +00:00
|
|
|
struct ip_fw_chain *rule, int flags)
|
1998-09-12 22:03:21 +00:00
|
|
|
{
|
|
|
|
struct dn_pkt *pkt;
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_pipe *p;
|
1999-03-26 14:15:59 +00:00
|
|
|
int len = m->m_pkthdr.len ;
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_flow_queue *q = NULL ;
|
|
|
|
int s ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
s = splimp();
|
|
|
|
/* XXX check the spl protection. It might be unnecessary since we
|
|
|
|
* run this at splnet() already.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEB(printf("-- last_pkt dst 0x%08x/0x%04x src 0x%08x/0x%04x\n",
|
2000-02-10 14:17:40 +00:00
|
|
|
last_pkt.dst_ip, last_pkt.dst_port,
|
|
|
|
last_pkt.src_ip, last_pkt.src_port);)
|
1998-09-12 22:03:21 +00:00
|
|
|
|
|
|
|
pipe_nr &= 0xffff ;
|
|
|
|
/*
|
|
|
|
* locate pipe. First time is expensive, next have direct access.
|
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
if ( (p = rule->rule->pipe_ptr) == NULL ) {
|
|
|
|
for (p = all_pipes; p && p->pipe_nr != pipe_nr; p = p->next)
|
1998-09-12 22:03:21 +00:00
|
|
|
;
|
2000-01-08 11:24:46 +00:00
|
|
|
if (p == NULL)
|
|
|
|
goto dropit ; /* this pipe does not exist! */
|
|
|
|
rule->rule->pipe_ptr = p ; /* record pipe ptr for the future */
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
q = find_queue(p);
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* update statistics, then do various check on reasons to drop pkt
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
if ( q == NULL )
|
|
|
|
goto dropit ; /* cannot allocate queue */
|
|
|
|
q->tot_bytes += len ;
|
|
|
|
q->tot_pkts++ ;
|
|
|
|
if ( p->plr && random() < p->plr )
|
|
|
|
goto dropit ; /* random pkt drop */
|
|
|
|
if ( p->queue_size && q->len >= p->queue_size)
|
|
|
|
goto dropit ; /* queue count overflow */
|
|
|
|
if ( p->queue_size_bytes && len + q->len_bytes > p->queue_size_bytes)
|
|
|
|
goto dropit ; /* queue size overflow */
|
|
|
|
/*
|
|
|
|
* can implement RED drops here if needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pkt = (struct dn_pkt *)malloc(sizeof (*pkt), M_IPFW, M_NOWAIT) ;
|
|
|
|
if ( pkt == NULL )
|
|
|
|
goto dropit ; /* cannot allocate packet header */
|
|
|
|
/* ok, i can handle the pkt now... */
|
|
|
|
bzero(pkt, sizeof(*pkt) ); /* XXX expensive, see if we can remove it*/
|
|
|
|
/* build and enqueue packet + parameters */
|
1998-09-12 22:03:21 +00:00
|
|
|
pkt->hdr.mh_type = MT_DUMMYNET ;
|
|
|
|
(struct ip_fw_chain *)pkt->hdr.mh_data = rule ;
|
2000-01-08 11:24:46 +00:00
|
|
|
DN_NEXT(pkt) = NULL;
|
1998-09-12 22:03:21 +00:00
|
|
|
pkt->dn_m = m;
|
|
|
|
pkt->dn_dir = dir ;
|
|
|
|
|
|
|
|
pkt->ifp = ifp;
|
|
|
|
if (dir == DN_TO_IP_OUT) {
|
1999-05-04 07:30:08 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* We need to copy *ro because for ICMP pkts (and maybe others)
|
|
|
|
* the caller passed a pointer into the stack; and, dst might
|
|
|
|
* also be a pointer into *ro so it needs to be updated.
|
1999-05-04 07:30:08 +00:00
|
|
|
*/
|
|
|
|
pkt->ro = *ro;
|
1998-09-12 22:03:21 +00:00
|
|
|
if (ro->ro_rt)
|
|
|
|
ro->ro_rt->rt_refcnt++ ; /* XXX */
|
1999-05-06 22:08:57 +00:00
|
|
|
if (dst == (struct sockaddr_in *)&ro->ro_dst) /* dst points into ro */
|
|
|
|
dst = (struct sockaddr_in *)&(pkt->ro.ro_dst) ;
|
1999-05-04 07:30:08 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
pkt->dn_dst = dst;
|
|
|
|
pkt->flags = flags ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
if (q->r.head == NULL)
|
|
|
|
q->r.head = pkt;
|
1998-09-12 22:03:21 +00:00
|
|
|
else
|
2000-01-08 11:24:46 +00:00
|
|
|
DN_NEXT(q->r.tail) = pkt;
|
|
|
|
q->r.tail = pkt;
|
|
|
|
q->len++;
|
|
|
|
q->len_bytes += len ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* If queue was empty (this is first pkt) then call ready_event()
|
|
|
|
* now to make the pkt go out at the right time. Otherwise we are done,
|
|
|
|
* as there must be a ready event already scheduled.
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
if (q->r.head == pkt) /* r_queue was empty */
|
|
|
|
ready_event( q );
|
1999-08-11 14:37:58 +00:00
|
|
|
splx(s);
|
1998-09-12 22:03:21 +00:00
|
|
|
return 0;
|
2000-01-08 11:24:46 +00:00
|
|
|
|
|
|
|
dropit:
|
|
|
|
splx(s);
|
|
|
|
if (q)
|
|
|
|
q->drops++ ;
|
|
|
|
m_freem(m);
|
|
|
|
return 0 ; /* XXX should I return an error ? */
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* below, the rt_unref is only needed when (pkt->dn_dir == DN_TO_IP_OUT)
|
|
|
|
* Doing this would probably save us the initial bzero of dn_pkt
|
|
|
|
*/
|
|
|
|
#define DN_FREE_PKT(pkt) { \
|
|
|
|
struct dn_pkt *n = pkt ; \
|
|
|
|
rt_unref ( n->ro.ro_rt ) ; \
|
|
|
|
m_freem(n->dn_m); \
|
|
|
|
pkt = DN_NEXT(n) ; \
|
|
|
|
free(n, M_IPFW) ; }
|
1998-09-12 22:03:21 +00:00
|
|
|
/*
|
|
|
|
* dispose all packets queued on a pipe
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
purge_pipe(struct dn_pipe *pipe)
|
|
|
|
{
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_pkt *pkt ;
|
|
|
|
struct dn_flow_queue *q, *qn ;
|
|
|
|
int i ;
|
|
|
|
|
|
|
|
for (i = 0 ; i < pipe->rq_size ; i++ )
|
|
|
|
for (q = pipe->rq[i] ; q ; q = qn ) {
|
|
|
|
for (pkt = q->r.head ; pkt ; )
|
|
|
|
DN_FREE_PKT(pkt) ;
|
|
|
|
qn = q->next ;
|
|
|
|
free(q, M_IPFW);
|
|
|
|
}
|
|
|
|
for (pkt = pipe->p.head ; pkt ; )
|
|
|
|
DN_FREE_PKT(pkt) ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Delete all pipes and heaps returning memory. Must also
|
|
|
|
* remove references from all ipfw rules to all pipes.
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dummynet_flush()
|
|
|
|
{
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_pipe *curr_p, *p ;
|
|
|
|
struct ip_fw_chain *chain ;
|
|
|
|
int s ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
s = splnet() ;
|
|
|
|
|
|
|
|
/* remove all references to pipes ...*/
|
|
|
|
for (chain= ip_fw_chain.lh_first ; chain; chain = chain->chain.le_next)
|
|
|
|
chain->rule->pipe_ptr = NULL ;
|
|
|
|
/* prevent future matches... */
|
|
|
|
p = all_pipes ;
|
|
|
|
all_pipes = NULL ;
|
|
|
|
/* and free heaps so we don't have unwanted events */
|
|
|
|
if (ready_heap.size >0 )
|
|
|
|
free(ready_heap.p, M_IPFW);
|
|
|
|
ready_heap.elements = ready_heap.size = 0 ;
|
|
|
|
if (extract_heap.size >0 )
|
|
|
|
free(extract_heap.p, M_IPFW);
|
|
|
|
extract_heap.elements = extract_heap.size = 0 ;
|
1998-09-12 22:03:21 +00:00
|
|
|
splx(s) ;
|
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* Now purge all queued pkts and delete all pipes
|
1998-09-12 22:03:21 +00:00
|
|
|
*/
|
|
|
|
for ( ; p ; ) {
|
|
|
|
purge_pipe(p);
|
2000-01-08 11:24:46 +00:00
|
|
|
curr_p = p ;
|
1998-09-12 22:03:21 +00:00
|
|
|
p = p->next ;
|
2000-01-08 11:24:46 +00:00
|
|
|
free(curr_p->rq, M_IPFW);
|
|
|
|
free(curr_p, M_IPFW);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern struct ip_fw_chain *ip_fw_default_rule ;
|
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* when a firewall rule is deleted, scan all queues and remove the flow-id
|
1998-09-12 22:03:21 +00:00
|
|
|
* from packets matching this rule.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dn_rule_delete(void *r)
|
|
|
|
{
|
1999-01-11 11:08:07 +00:00
|
|
|
struct dn_pipe *p ;
|
2000-01-08 11:24:46 +00:00
|
|
|
struct dn_flow_queue *q ;
|
|
|
|
struct dn_pkt *pkt ;
|
|
|
|
int i ;
|
1999-01-11 11:08:07 +00:00
|
|
|
|
|
|
|
for ( p = all_pipes ; p ; p = p->next ) {
|
2000-01-08 11:24:46 +00:00
|
|
|
for (i = 0 ; i < p->rq_size ; i++)
|
|
|
|
for (q = p->rq[i] ; q ; q = q->next )
|
|
|
|
for (pkt = q->r.head ; pkt ; pkt = DN_NEXT(pkt) )
|
|
|
|
if (pkt->hdr.mh_data == r)
|
|
|
|
pkt->hdr.mh_data = (void *)ip_fw_default_rule ;
|
|
|
|
for (pkt = p->p.head ; pkt ; pkt = DN_NEXT(pkt) )
|
|
|
|
if (pkt->hdr.mh_data == r)
|
|
|
|
pkt->hdr.mh_data = (void *)ip_fw_default_rule ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
1998-12-14 18:09:13 +00:00
|
|
|
}
|
1998-09-12 22:03:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* handler for the various dummynet socket options
|
|
|
|
* (get, flush, config, del)
|
|
|
|
*/
|
|
|
|
static int
|
1998-12-14 18:09:13 +00:00
|
|
|
ip_dn_ctl(struct sockopt *sopt)
|
1998-09-12 22:03:21 +00:00
|
|
|
{
|
1998-12-14 18:09:13 +00:00
|
|
|
int error = 0 ;
|
|
|
|
size_t size ;
|
2000-01-08 11:24:46 +00:00
|
|
|
char *buf, *bp ; /* bp is the "copy-pointer" */
|
1998-12-31 07:35:49 +00:00
|
|
|
struct dn_pipe *p, tmp_pipe ;
|
1998-12-14 18:09:13 +00:00
|
|
|
|
|
|
|
struct dn_pipe *x, *a, *b ;
|
|
|
|
|
|
|
|
/* Disallow sets in really-really secure mode. */
|
|
|
|
if (sopt->sopt_dir == SOPT_SET && securelevel >= 3)
|
|
|
|
return (EPERM);
|
|
|
|
|
|
|
|
switch (sopt->sopt_name) {
|
|
|
|
default :
|
|
|
|
panic("ip_dn_ctl -- unknown option");
|
|
|
|
|
|
|
|
case IP_DUMMYNET_GET :
|
|
|
|
for (p = all_pipes, size = 0 ; p ; p = p->next )
|
2000-01-08 11:24:46 +00:00
|
|
|
size += sizeof( *p ) +
|
|
|
|
p->rq_elements * sizeof(struct dn_flow_queue);
|
1998-12-14 18:09:13 +00:00
|
|
|
buf = malloc(size, M_TEMP, M_WAITOK);
|
|
|
|
if (buf == 0) {
|
|
|
|
error = ENOBUFS ;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
for (p = all_pipes, bp = buf ; p ; p = p->next ) {
|
2000-01-08 11:24:46 +00:00
|
|
|
int i ;
|
|
|
|
struct dn_pipe *pipe_bp = (struct dn_pipe *)bp ;
|
|
|
|
struct dn_flow_queue *q;
|
1998-12-14 18:09:13 +00:00
|
|
|
|
1999-05-04 07:30:08 +00:00
|
|
|
/*
|
2000-01-08 11:24:46 +00:00
|
|
|
* copy the pipe descriptor into *bp, convert delay back to ms,
|
|
|
|
* then copy the queue descriptor(s) one at a time.
|
1999-05-04 07:30:08 +00:00
|
|
|
*/
|
2000-01-08 11:24:46 +00:00
|
|
|
bcopy(p, bp, sizeof( *p ) );
|
|
|
|
pipe_bp->delay = (pipe_bp->delay * 1000) / hz ;
|
1998-12-14 18:09:13 +00:00
|
|
|
bp += sizeof( *p ) ;
|
2000-01-08 11:24:46 +00:00
|
|
|
for (i = 0 ; i < p->rq_size ; i++)
|
|
|
|
for (q = p->rq[i] ; q ; q = q->next, bp += sizeof(*q) )
|
|
|
|
bcopy(q, bp, sizeof( *q ) );
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
1998-12-14 18:09:13 +00:00
|
|
|
error = sooptcopyout(sopt, buf, size);
|
|
|
|
FREE(buf, M_TEMP);
|
|
|
|
break ;
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-12-14 18:09:13 +00:00
|
|
|
case IP_DUMMYNET_FLUSH :
|
2000-01-08 11:24:46 +00:00
|
|
|
dummynet_flush() ;
|
1998-12-14 18:09:13 +00:00
|
|
|
break ;
|
2000-01-08 11:24:46 +00:00
|
|
|
|
1998-12-14 18:09:13 +00:00
|
|
|
case IP_DUMMYNET_CONFIGURE :
|
|
|
|
p = &tmp_pipe ;
|
|
|
|
error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
|
|
|
|
if (error)
|
|
|
|
break ;
|
2000-01-08 11:24:46 +00:00
|
|
|
/*
|
|
|
|
* The config program passes parameters as follows:
|
|
|
|
* bandwidth = bits/second (0 means no limits);
|
|
|
|
* delay = millisec., must be translated into ticks.
|
|
|
|
* queue_size = slots (0 means no limit)
|
|
|
|
* queue_size_bytes = bytes (0 means no limit)
|
|
|
|
* only one can be set, must be bound-checked
|
|
|
|
*/
|
|
|
|
p->delay = ( p->delay * hz ) / 1000 ;
|
|
|
|
if (p->queue_size == 0 && p->queue_size_bytes == 0)
|
|
|
|
p->queue_size = 50 ;
|
|
|
|
if (p->queue_size != 0 ) /* buffers are prevailing */
|
|
|
|
p->queue_size_bytes = 0 ;
|
|
|
|
if (p->queue_size > 100)
|
|
|
|
p->queue_size = 50 ;
|
|
|
|
if (p->queue_size_bytes > 1024*1024)
|
|
|
|
p->queue_size_bytes = 1024*1024 ;
|
|
|
|
for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
|
1998-09-12 22:03:21 +00:00
|
|
|
a = b , b = b->next) ;
|
2000-01-08 11:24:46 +00:00
|
|
|
if (b && b->pipe_nr == p->pipe_nr) {
|
|
|
|
b->bandwidth = p->bandwidth ;
|
|
|
|
b->delay = p->delay ;
|
|
|
|
b->queue_size = p->queue_size ;
|
|
|
|
b->queue_size_bytes = p->queue_size_bytes ;
|
|
|
|
b->plr = p->plr ;
|
|
|
|
b->flow_mask = p->flow_mask ;
|
|
|
|
b->flags = p->flags ;
|
|
|
|
} else { /* completely new pipe */
|
|
|
|
int s ;
|
|
|
|
x = malloc(sizeof(struct dn_pipe), M_IPFW, M_DONTWAIT) ;
|
|
|
|
if (x == NULL) {
|
|
|
|
printf("ip_dummynet.c: no memory for new pipe\n");
|
1998-12-14 18:09:13 +00:00
|
|
|
error = ENOSPC ;
|
|
|
|
break ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
bzero(x, sizeof(*x) );
|
|
|
|
x->bandwidth = p->bandwidth ;
|
|
|
|
x->delay = p->delay ;
|
|
|
|
x->pipe_nr = p->pipe_nr ;
|
|
|
|
x->queue_size = p->queue_size ;
|
|
|
|
x->queue_size_bytes = p->queue_size_bytes ;
|
|
|
|
x->plr = p->plr ;
|
|
|
|
x->flow_mask = p->flow_mask ;
|
|
|
|
x->flags = p->flags ;
|
|
|
|
if (x->flags & DN_HAVE_FLOW_MASK) {/* allocate some slots */
|
|
|
|
int l = p->rq_size ;
|
|
|
|
if (l == 0)
|
|
|
|
l = dn_hash_size ;
|
|
|
|
if (l < 4)
|
|
|
|
l = 4 ;
|
|
|
|
else if (l > 1024)
|
|
|
|
l = 1024 ;
|
|
|
|
x->rq_size = l ;
|
|
|
|
} else /* one is enough for null mask */
|
|
|
|
x->rq_size = 1 ;
|
|
|
|
x->rq = malloc(x->rq_size * sizeof(struct dn_flow_queue *),
|
|
|
|
M_IPFW, M_DONTWAIT) ;
|
|
|
|
if (x->rq == NULL ) {
|
|
|
|
printf("sorry, cannot allocate queue\n");
|
|
|
|
free(x, M_IPFW);
|
|
|
|
error = ENOSPC ;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
bzero(x->rq, x->rq_size * sizeof(struct dn_flow_queue *) );
|
|
|
|
x->rq_elements = 0 ;
|
|
|
|
|
|
|
|
s = splnet() ;
|
|
|
|
x->next = b ;
|
|
|
|
if (a == NULL)
|
|
|
|
all_pipes = x ;
|
|
|
|
else
|
|
|
|
a->next = x ;
|
|
|
|
splx(s);
|
|
|
|
}
|
1998-12-14 18:09:13 +00:00
|
|
|
break ;
|
|
|
|
|
|
|
|
case IP_DUMMYNET_DEL :
|
|
|
|
p = &tmp_pipe ;
|
|
|
|
error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
|
|
|
|
if (error)
|
|
|
|
break ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
|
1998-09-12 22:03:21 +00:00
|
|
|
a = b , b = b->next) ;
|
2000-01-08 11:24:46 +00:00
|
|
|
if (b && b->pipe_nr == p->pipe_nr) { /* found pipe */
|
|
|
|
int s ;
|
|
|
|
struct ip_fw_chain *chain ;
|
|
|
|
|
|
|
|
s = splnet() ;
|
|
|
|
chain = ip_fw_chain.lh_first;
|
|
|
|
|
|
|
|
if (a == NULL)
|
|
|
|
all_pipes = b->next ;
|
|
|
|
else
|
|
|
|
a->next = b->next ;
|
|
|
|
/*
|
|
|
|
* remove references to this pipe from the ip_fw rules.
|
|
|
|
*/
|
|
|
|
for (; chain; chain = chain->chain.le_next)
|
|
|
|
if (chain->rule->pipe_ptr == b)
|
|
|
|
chain->rule->pipe_ptr = NULL ;
|
|
|
|
/* remove all references to b from heaps */
|
|
|
|
if (ready_heap.elements > 0) {
|
|
|
|
struct dn_heap *h = &ready_heap ;
|
|
|
|
int i = 0, found = 0 ;
|
|
|
|
while ( i < h->elements ) {
|
|
|
|
if (((struct dn_flow_queue *)(h->p[i].object))->p == b) {
|
|
|
|
/* found one */
|
|
|
|
h->elements-- ;
|
|
|
|
h->p[i] = h->p[h->elements] ;
|
|
|
|
found++ ;
|
|
|
|
} else
|
|
|
|
i++ ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
if (found)
|
|
|
|
heapify(h);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
if (extract_heap.elements > 0) {
|
|
|
|
struct dn_heap *h = &extract_heap ;
|
|
|
|
int i = 0, found = 0 ;
|
|
|
|
while ( i < h->elements ) {
|
|
|
|
if (h->p[i].object == b) { /* found one */
|
|
|
|
h->elements-- ;
|
|
|
|
h->p[i] = h->p[h->elements] ;
|
|
|
|
found++ ;
|
|
|
|
} else
|
|
|
|
i++ ;
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
heapify(h);
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
purge_pipe(b); /* remove pkts from here */
|
|
|
|
free(b->rq, M_IPFW);
|
|
|
|
free(b, M_IPFW);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
break ;
|
|
|
|
}
|
1998-12-14 18:09:13 +00:00
|
|
|
return error ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
1999-04-20 13:32:06 +00:00
|
|
|
static void
|
1998-09-12 22:03:21 +00:00
|
|
|
ip_dn_init(void)
|
|
|
|
{
|
2000-01-08 11:24:46 +00:00
|
|
|
printf("DUMMYNET initialized (000106)\n");
|
1998-09-12 22:03:21 +00:00
|
|
|
all_pipes = NULL ;
|
2000-01-08 11:24:46 +00:00
|
|
|
ready_heap.size = ready_heap.elements = 0 ;
|
|
|
|
extract_heap.size = extract_heap.elements = 0 ;
|
1998-09-12 22:03:21 +00:00
|
|
|
ip_dn_ctl_ptr = ip_dn_ctl;
|
2000-01-08 11:24:46 +00:00
|
|
|
timeout(dummynet, NULL, 1);
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
2000-01-08 11:24:46 +00:00
|
|
|
static ip_dn_ctl_t *old_dn_ctl_ptr ;
|
1998-09-12 22:03:21 +00:00
|
|
|
|
|
|
|
static int
|
1999-04-17 11:09:08 +00:00
|
|
|
dummynet_modevent(module_t mod, int type, void *data)
|
1998-09-12 22:03:21 +00:00
|
|
|
{
|
2000-01-08 11:24:46 +00:00
|
|
|
int s ;
|
1999-04-17 11:09:08 +00:00
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
s = splnet();
|
|
|
|
old_dn_ctl_ptr = ip_dn_ctl_ptr;
|
|
|
|
ip_dn_init();
|
|
|
|
splx(s);
|
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
s = splnet();
|
2000-01-08 11:24:46 +00:00
|
|
|
ip_dn_ctl_ptr = old_dn_ctl_ptr;
|
1999-04-17 11:09:08 +00:00
|
|
|
splx(s);
|
|
|
|
dummynet_flush();
|
2000-01-08 11:24:46 +00:00
|
|
|
break ;
|
1999-04-17 11:09:08 +00:00
|
|
|
default:
|
2000-01-08 11:24:46 +00:00
|
|
|
break ;
|
1999-04-17 11:09:08 +00:00
|
|
|
}
|
2000-01-08 11:24:46 +00:00
|
|
|
return 0 ;
|
1998-09-12 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
1999-04-17 11:09:08 +00:00
|
|
|
static moduledata_t dummynet_mod = {
|
|
|
|
"dummynet",
|
|
|
|
dummynet_modevent,
|
|
|
|
NULL
|
2000-01-08 11:24:46 +00:00
|
|
|
} ;
|
1999-04-20 13:32:06 +00:00
|
|
|
DECLARE_MODULE(dummynet, dummynet_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
|