#define DUMMYNET_DEBUG
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/route.h>
#include <net/kpi_protocol.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>
#include <netinet/ip_var.h>
#if BRIDGE
#include <netinet/if_ether.h>
#include <net/bridge.h>
#endif
static dn_key curr_time = 0 ;
static int dn_hash_size = 64 ;
static int searches, search_steps ;
static int pipe_expire = 1 ;
static int dn_max_ratio = 16 ;
static int red_lookup_depth = 256;
static int red_avg_pkt_size = 512;
static int red_max_pkt_size = 1500;
static struct dn_heap ready_heap, extract_heap, wfq_ready_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, void *obj);
static void transmit_event(struct dn_pipe *pipe);
static void ready_event(struct dn_flow_queue *q);
static struct dn_pipe *all_pipes = NULL ;
static struct dn_flow_set *all_flow_sets = NULL ;
#ifdef SYSCTL_NODE
SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet,
CTLFLAG_RW, 0, "Dummynet");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size,
CTLFLAG_RW, &dn_hash_size, 0, "Default hash table size");
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");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire,
CTLFLAG_RW, &pipe_expire, 0, "Expire queue if empty");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, max_chain_len,
CTLFLAG_RW, &dn_max_ratio, 0,
"Max ratio between dynamic queues and buckets");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
CTLFLAG_RD, &red_lookup_depth, 0, "Depth of RED lookup table");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
CTLFLAG_RD, &red_avg_pkt_size, 0, "RED Medium packet size");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
CTLFLAG_RD, &red_max_pkt_size, 0, "RED Max packet size");
#endif
#ifdef DUMMYNET_DEBUG
int dummynet_debug = 0;
#ifdef SYSCTL_NODE
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug, CTLFLAG_RW, &dummynet_debug,
0, "control debugging printfs");
#endif
#define DPRINTF(X) if (dummynet_debug) printf X
#else
#define DPRINTF(X)
#endif
lck_grp_t *dn_mutex_grp;
lck_grp_attr_t *dn_mutex_grp_attr;
lck_attr_t *dn_mutex_attr;
lck_mtx_t *dn_mutex;
static int config_pipe(struct dn_pipe *p);
static int ip_dn_ctl(struct sockopt *sopt);
static void dummynet(void *);
static void dummynet_flush(void);
void dummynet_drain(void);
static ip_dn_io_t dummynet_io;
static void dn_rule_delete(void *);
int if_tx_rdy(struct ifnet *ifp);
extern lck_mtx_t *rt_mtx;
#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) + 2 )
#define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
#define HEAP_INCREMENT 15
static int
heap_init(struct dn_heap *h, int new_size)
{
struct dn_heap_entry *p;
if (h->size >= new_size ) {
printf("dummynet: 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_DUMMYNET, M_DONTWAIT );
if (p == NULL) {
printf("dummynet: heap_init, resize %d failed\n", new_size );
return 1 ;
}
if (h->size > 0) {
bcopy(h->p, p, h->size * sizeof(*p) );
FREE(h->p, M_DUMMYNET);
}
h->p = p ;
h->size = new_size ;
return 0 ;
}
#define SET_OFFSET(heap, node) \
if (heap->offset > 0) \
*((int *)((char *)(heap->p[node].object) + heap->offset)) = node ;
#define RESET_OFFSET(heap, node) \
if (heap->offset > 0) \
*((int *)((char *)(heap->p[node].object) + heap->offset)) = -1 ;
static int
heap_insert(struct dn_heap *h, dn_key key1, void *p)
{
int son = h->elements ;
if (p == NULL)
son = key1 ;
else {
son = h->elements ;
if (son == h->size)
if (heap_init(h, h->elements+1) )
return 1 ;
h->p[son].object = p ;
h->p[son].key = key1 ;
h->elements++ ;
}
while (son > 0) {
int father = HEAP_FATHER(son) ;
struct dn_heap_entry tmp ;
if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
break ;
HEAP_SWAP(h->p[son], h->p[father], tmp) ;
SET_OFFSET(h, son);
son = father ;
}
SET_OFFSET(h, son);
return 0 ;
}
static void
heap_extract(struct dn_heap *h, void *obj)
{
int child, father, max = h->elements - 1 ;
if (max < 0) {
printf("dummynet: warning, extract from empty heap 0x%p\n", h);
return ;
}
father = 0 ;
if (obj != NULL) {
if (h->offset <= 0)
panic("dummynet: heap_extract from middle not supported on this heap!!!\n");
father = *((int *)((char *)obj + h->offset)) ;
if (father < 0 || father >= h->elements) {
printf("dummynet: heap_extract, father %d out of bound 0..%d\n",
father, h->elements);
panic("dummynet: heap_extract");
}
}
RESET_OFFSET(h, father);
child = HEAP_LEFT(father) ;
while (child <= max) {
if (child != max && DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
child = child+1 ;
h->p[father] = h->p[child] ;
SET_OFFSET(h, father);
father = child ;
child = HEAP_LEFT(child) ;
}
h->elements-- ;
if (father != max) {
h->p[father] = h->p[max] ;
heap_insert(h, father, NULL);
}
}
#if 0
static void
heap_move(struct dn_heap *h, dn_key new_key, void *object)
{
int temp;
int i ;
int max = h->elements-1 ;
struct dn_heap_entry buf ;
if (h->offset <= 0)
panic("cannot move items on this heap");
i = *((int *)((char *)object + h->offset));
if (DN_KEY_LT(new_key, h->p[i].key) ) {
h->p[i].key = new_key ;
for (; i>0 && DN_KEY_LT(new_key, h->p[(temp = HEAP_FATHER(i))].key) ;
i = temp ) {
HEAP_SWAP(h->p[i], h->p[temp], buf) ;
SET_OFFSET(h, i);
}
} else {
h->p[i].key = new_key ;
while ( (temp = HEAP_LEFT(i)) <= max ) {
if ((temp != max) && DN_KEY_GT(h->p[temp].key, h->p[temp+1].key))
temp++ ;
if (DN_KEY_GT(new_key, h->p[temp].key)) {
HEAP_SWAP(h->p[i], h->p[temp], buf) ;
SET_OFFSET(h, i);
} else
break ;
i = temp ;
}
}
SET_OFFSET(h, i);
}
#endif
static void
heapify(struct dn_heap *h)
{
int i ;
for (i = 0 ; i < h->elements ; i++ )
heap_insert(h, i , NULL) ;
}
static void
heap_free(struct dn_heap *h)
{
if (h->size >0 )
FREE(h->p, M_DUMMYNET);
bzero(h, sizeof(*h) );
}
static struct dn_pkt_tag *
dn_tag_get(struct mbuf *m)
{
struct m_tag *mtag = m_tag_first(m);
return (struct dn_pkt_tag *)(mtag+1);
}
static void
transmit_event(struct dn_pipe *pipe)
{
struct mbuf *m ;
struct dn_pkt_tag *pkt ;
struct ip *ip;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
while ( (m = pipe->head) ) {
pkt = dn_tag_get(m);
if ( !DN_KEY_LEQ(pkt->output_time, curr_time) )
break;
pipe->head = m->m_nextpkt ;
m->m_nextpkt = NULL;
lck_mtx_unlock(dn_mutex);
switch (pkt->dn_dir) {
case DN_TO_IP_OUT: {
struct route tmp_rt = pkt->ro;
(void)ip_output(m, NULL, NULL, pkt->flags, NULL);
if (tmp_rt.ro_rt) {
rtfree(tmp_rt.ro_rt);
}
break ;
}
case DN_TO_IP_IN :
ip = mtod(m, struct ip *);
ip->ip_len = htons(ip->ip_len);
ip->ip_off = htons(ip->ip_off);
proto_inject(PF_INET, m);
break ;
#if BRIDGE
case DN_TO_BDG_FWD :
if (BDG_LOADED) {
if (m->m_len < ETHER_HDR_LEN &&
(m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
printf("dummynet/bridge: pullup fail, dropping pkt\n");
break;
}
m = bdg_forward_ptr(m, pkt->ifp);
} else {
printf("dummynet: dropping bridged packet trapped in pipe\n");
}
if (m)
m_freem(m);
break;
#endif
default:
printf("dummynet: bad switch %d!\n", pkt->dn_dir);
m_freem(m);
break ;
}
lck_mtx_lock(dn_mutex);
}
if ( (m = pipe->head) ) {
pkt = dn_tag_get(m);
heap_insert(&extract_heap, pkt->output_time, pipe);
}
}
#define SET_TICKS(_m, q, p) \
((_m)->m_pkthdr.len*8*hz - (q)->numbytes + p->bandwidth - 1 ) / \
p->bandwidth ;
static void
move_pkt(struct mbuf *pkt, struct dn_flow_queue *q,
struct dn_pipe *p, int len)
{
struct dn_pkt_tag *dt = dn_tag_get(pkt);
q->head = pkt->m_nextpkt ;
q->len-- ;
q->len_bytes -= len ;
dt->output_time = curr_time + p->delay ;
if (p->head == NULL)
p->head = pkt;
else
p->tail->m_nextpkt = pkt;
p->tail = pkt;
p->tail->m_nextpkt = NULL;
}
static void
ready_event(struct dn_flow_queue *q)
{
struct mbuf *pkt;
struct dn_pipe *p = q->fs->pipe ;
int p_was_empty ;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
if (p == NULL) {
printf("dummynet: ready_event- pipe is gone\n");
return ;
}
p_was_empty = (p->head == NULL) ;
q->numbytes += ( curr_time - q->sched_time ) * p->bandwidth;
while ( (pkt = q->head) != NULL ) {
int len = pkt->m_pkthdr.len;
int len_scaled = p->bandwidth ? len*8*hz : 0 ;
if (len_scaled > q->numbytes )
break ;
q->numbytes -= len_scaled ;
move_pkt(pkt, q, p, len);
}
if ( (pkt = q->head) != NULL ) {
dn_key t = SET_TICKS(pkt, q, p);
q->sched_time = curr_time ;
heap_insert(&ready_heap, curr_time + t, (void *)q );
} else {
q->q_time = curr_time;
q->numbytes = 0;
}
if (p_was_empty)
transmit_event(p);
}
static void
ready_event_wfq(struct dn_pipe *p)
{
int p_was_empty = (p->head == NULL) ;
struct dn_heap *sch = &(p->scheduler_heap);
struct dn_heap *neh = &(p->not_eligible_heap) ;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
if (p->if_name[0] == 0)
p->numbytes += ( curr_time - p->sched_time ) * p->bandwidth;
else {
if (p->ifp && p->ifp->if_snd.ifq_head != NULL)
return ;
else {
DPRINTF(("dummynet: pipe %d ready from %s --\n",
p->pipe_nr, p->if_name));
}
}
while ( p->numbytes >=0 && (sch->elements>0 || neh->elements >0) ) {
if (sch->elements > 0) {
struct dn_flow_queue *q = sch->p[0].object ;
struct mbuf *pkt = q->head;
struct dn_flow_set *fs = q->fs;
u_int64_t len = pkt->m_pkthdr.len;
int len_scaled = p->bandwidth ? len*8*hz : 0 ;
heap_extract(sch, NULL);
p->numbytes -= len_scaled ;
move_pkt(pkt, q, p, len);
p->V += (len<<MY_M) / p->sum ;
q->S = q->F ;
if (q->len == 0) {
fs->backlogged-- ;
heap_insert(&(p->idle_heap), q->F, q);
} else {
len = (q->head)->m_pkthdr.len;
q->F += (len<<MY_M)/(u_int64_t) fs->weight ;
if (DN_KEY_LEQ(q->S, p->V))
heap_insert(neh, q->S, q);
else
heap_insert(sch, q->F, q);
}
}
if (sch->elements == 0 && neh->elements > 0)
p->V = MAX64 ( p->V, neh->p[0].key );
while (neh->elements > 0 && DN_KEY_LEQ(neh->p[0].key, p->V) ) {
struct dn_flow_queue *q = neh->p[0].object ;
heap_extract(neh, NULL);
heap_insert(sch, q->F, q);
}
if (p->if_name[0] != '\0') {
p->numbytes = -1 ;
break ;
}
}
if (sch->elements == 0 && neh->elements == 0 && p->numbytes >= 0
&& p->idle_heap.elements > 0) {
int i ;
for (i = 0 ; i < p->idle_heap.elements ; i++) {
struct dn_flow_queue *q = p->idle_heap.p[i].object ;
q->F = 0 ;
q->S = q->F + 1 ;
}
p->sum = 0 ;
p->V = 0 ;
p->idle_heap.elements = 0 ;
}
if (p->if_name[0]==0 && p->numbytes < 0) {
dn_key t=0 ;
if (p->bandwidth > 0)
t = ( p->bandwidth -1 - p->numbytes) / p->bandwidth ;
dn_tag_get(p->tail)->output_time += t ;
p->sched_time = curr_time ;
heap_insert(&wfq_ready_heap, curr_time + t, (void *)p);
}
if (p_was_empty)
transmit_event(p);
}
static void
dummynet(void * __unused unused)
{
void *p ;
struct dn_heap *h ;
struct dn_heap *heaps[3];
int i;
struct dn_pipe *pe ;
heaps[0] = &ready_heap ;
heaps[1] = &wfq_ready_heap ;
heaps[2] = &extract_heap ;
lck_mtx_lock(dn_mutex);
curr_time++ ;
for (i=0; i < 3 ; i++) {
h = heaps[i];
while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time) ) {
if (h->p[0].key > curr_time)
printf("dummynet: warning, heap %d is %d ticks late\n",
i, (int)(curr_time - h->p[0].key));
p = h->p[0].object ;
heap_extract(h, NULL);
if (i == 0)
ready_event(p) ;
else if (i == 1) {
struct dn_pipe *pipe = p;
if (pipe->if_name[0] != '\0')
printf("dummynet: bad ready_event_wfq for pipe %s\n",
pipe->if_name);
else
ready_event_wfq(p) ;
} else
transmit_event(p);
}
}
for (pe = all_pipes; pe ; pe = pe->next )
if (pe->idle_heap.elements > 0 &&
DN_KEY_LT(pe->idle_heap.p[0].key, pe->V) ) {
struct dn_flow_queue *q = pe->idle_heap.p[0].object ;
heap_extract(&(pe->idle_heap), NULL);
q->S = q->F + 1 ;
pe->sum -= q->fs->weight ;
}
lck_mtx_unlock(dn_mutex);
timeout(dummynet, NULL, 1);
}
int
if_tx_rdy(struct ifnet *ifp)
{
struct dn_pipe *p;
lck_mtx_lock(dn_mutex);
for (p = all_pipes; p ; p = p->next )
if (p->ifp == ifp)
break ;
if (p == NULL) {
char buf[32];
sprintf(buf, "%s%d",ifp->if_name, ifp->if_unit);
for (p = all_pipes; p ; p = p->next )
if (!strcmp(p->if_name, buf) ) {
p->ifp = ifp ;
DPRINTF(("dummynet: ++ tx rdy from %s (now found)\n", buf));
break ;
}
}
if (p != NULL) {
DPRINTF(("dummynet: ++ tx rdy from %s%d - qlen %d\n", ifp->if_name,
ifp->if_unit, ifp->if_snd.ifq_len));
p->numbytes = 0 ;
ready_event_wfq(p);
}
lck_mtx_lock(dn_mutex);
return 0;
}
static int
expire_queues(struct dn_flow_set *fs)
{
struct dn_flow_queue *q, *prev ;
int i, initial_elements = fs->rq_elements ;
struct timeval timenow;
getmicrotime(&timenow);
if (fs->last_expired == timenow.tv_sec)
return 0 ;
fs->last_expired = timenow.tv_sec ;
for (i = 0 ; i <= fs->rq_size ; i++)
for (prev=NULL, q = fs->rq[i] ; q != NULL ; )
if (q->head != NULL || q->S != q->F+1) {
prev = q ;
q = q->next ;
} else {
struct dn_flow_queue *old_q = q ;
if (prev != NULL)
prev->next = q = q->next ;
else
fs->rq[i] = q = q->next ;
fs->rq_elements-- ;
FREE(old_q, M_DUMMYNET);
}
return initial_elements - fs->rq_elements ;
}
static struct dn_flow_queue *
create_queue(struct dn_flow_set *fs, int i)
{
struct dn_flow_queue *q ;
if (fs->rq_elements > fs->rq_size * dn_max_ratio &&
expire_queues(fs) == 0) {
i = fs->rq_size ;
if ( fs->rq[i] != NULL )
return fs->rq[i] ;
}
q = _MALLOC(sizeof(*q), M_DUMMYNET, M_DONTWAIT | M_ZERO);
if (q == NULL) {
printf("dummynet: sorry, cannot allocate queue for new flow\n");
return NULL ;
}
q->fs = fs ;
q->hash_slot = i ;
q->next = fs->rq[i] ;
q->S = q->F + 1;
fs->rq[i] = q ;
fs->rq_elements++ ;
return q ;
}
static struct dn_flow_queue *
find_queue(struct dn_flow_set *fs, struct ipfw_flow_id *id)
{
int i = 0 ;
struct dn_flow_queue *q, *prev;
if ( !(fs->flags_fs & DN_HAVE_FLOW_MASK) )
q = fs->rq[0] ;
else {
id->dst_ip &= fs->flow_mask.dst_ip ;
id->src_ip &= fs->flow_mask.src_ip ;
id->dst_port &= fs->flow_mask.dst_port ;
id->src_port &= fs->flow_mask.src_port ;
id->proto &= fs->flow_mask.proto ;
id->flags = 0 ;
i = ( (id->dst_ip) & 0xffff ) ^
( (id->dst_ip >> 15) & 0xffff ) ^
( (id->src_ip << 1) & 0xffff ) ^
( (id->src_ip >> 16 ) & 0xffff ) ^
(id->dst_port << 1) ^ (id->src_port) ^
(id->proto );
i = i % fs->rq_size ;
searches++ ;
for (prev=NULL, q = fs->rq[i] ; q ; ) {
search_steps++;
if (id->dst_ip == q->id.dst_ip &&
id->src_ip == q->id.src_ip &&
id->dst_port == q->id.dst_port &&
id->src_port == q->id.src_port &&
id->proto == q->id.proto &&
id->flags == q->id.flags)
break ;
else if (pipe_expire && q->head == NULL && q->S == q->F+1 ) {
struct dn_flow_queue *old_q = q ;
if (prev != NULL)
prev->next = q = q->next ;
else
fs->rq[i] = q = q->next ;
fs->rq_elements-- ;
FREE(old_q, M_DUMMYNET);
continue ;
}
prev = q ;
q = q->next ;
}
if (q && prev != NULL) {
prev->next = q->next ;
q->next = fs->rq[i] ;
fs->rq[i] = q ;
}
}
if (q == NULL) {
q = create_queue(fs, i);
if (q != NULL)
q->id = *id ;
}
return q ;
}
static int
red_drops(struct dn_flow_set *fs, struct dn_flow_queue *q, int len)
{
int64_t p_b = 0;
u_int q_size = (fs->flags_fs & DN_QSIZE_IS_BYTES) ? q->len_bytes : q->len;
DPRINTF(("\ndummynet: %d q: %2u ", (int) curr_time, q_size));
if (q_size != 0) {
int diff = SCALE(q_size) - q->avg;
int64_t v = SCALE_MUL((int64_t) diff, (int64_t) fs->w_q);
q->avg += (int) v;
} else {
if (q->avg) {
u_int t = (curr_time - q->q_time) / fs->lookup_step;
q->avg = (t < fs->lookup_depth) ?
SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
}
}
DPRINTF(("dummynet: avg: %u ", SCALE_VAL(q->avg)));
if (q->avg < fs->min_th) {
q->count = -1;
return 0;
}
if (q->avg >= fs->max_th) {
if (fs->flags_fs & DN_IS_GENTLE_RED) {
p_b = SCALE_MUL((int64_t) fs->c_3, (int64_t) q->avg) - fs->c_4;
} else {
q->count = -1;
DPRINTF(("dummynet: - drop"));
return 1 ;
}
} else if (q->avg > fs->min_th) {
p_b = SCALE_MUL((int64_t) fs->c_1, (int64_t) q->avg) - fs->c_2;
}
if (fs->flags_fs & DN_QSIZE_IS_BYTES)
p_b = (p_b * len) / fs->max_pkt_size;
if (++q->count == 0)
q->random = random() & 0xffff;
else {
if (SCALE_MUL(p_b, SCALE((int64_t) q->count)) > q->random) {
q->count = 0;
DPRINTF(("dummynet: - red drop"));
q->random = random() & 0xffff;
return 1;
}
}
return 0 ;
}
static __inline
struct dn_flow_set *
locate_flowset(int pipe_nr, struct ip_fw *rule)
{
struct dn_flow_set *fs;
ipfw_insn *cmd = rule->cmd + rule->act_ofs;
if (cmd->opcode == O_LOG)
cmd += F_LEN(cmd);
bcopy(& ((ipfw_insn_pipe *)cmd)->pipe_ptr, &fs, sizeof(fs));
if (fs != NULL)
return fs;
if (cmd->opcode == O_QUEUE) {
for (fs=all_flow_sets; fs && fs->fs_nr != pipe_nr; fs=fs->next)
;
}
else {
struct dn_pipe *p1;
for (p1 = all_pipes; p1 && p1->pipe_nr != pipe_nr; p1 = p1->next)
;
if (p1 != NULL)
fs = &(p1->fs) ;
}
bcopy(&fs, & ((ipfw_insn_pipe *)cmd)->pipe_ptr, sizeof(fs));
return fs ;
}
static int
dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
{
struct dn_pkt_tag *pkt;
struct m_tag *mtag;
struct dn_flow_set *fs;
struct dn_pipe *pipe ;
u_int64_t len = m->m_pkthdr.len ;
struct dn_flow_queue *q = NULL ;
int is_pipe;
#if IPFW2
ipfw_insn *cmd = fwa->rule->cmd + fwa->rule->act_ofs;
if (cmd->opcode == O_LOG)
cmd += F_LEN(cmd);
is_pipe = (cmd->opcode == O_PIPE);
#else
is_pipe = (fwa->rule->fw_flg & IP_FW_F_COMMAND) == IP_FW_F_PIPE;
#endif
pipe_nr &= 0xffff ;
lck_mtx_lock(dn_mutex);
fs = locate_flowset(pipe_nr, fwa->rule);
if (fs == NULL)
goto dropit ;
pipe = fs->pipe ;
if (pipe == NULL) {
for (pipe = all_pipes; pipe && pipe->pipe_nr != fs->parent_nr;
pipe = pipe->next)
;
if (pipe != NULL)
fs->pipe = pipe ;
else {
printf("dummynet: no pipe %d for queue %d, drop pkt\n",
fs->parent_nr, fs->fs_nr);
goto dropit ;
}
}
q = find_queue(fs, &(fwa->f_id));
if ( q == NULL )
goto dropit ;
q->tot_bytes += len ;
q->tot_pkts++ ;
if ( fs->plr && random() < fs->plr )
goto dropit ;
if ( fs->flags_fs & DN_QSIZE_IS_BYTES) {
if (q->len_bytes > fs->qsize)
goto dropit ;
} else {
if (q->len >= fs->qsize)
goto dropit ;
}
if ( fs->flags_fs & DN_IS_RED && red_drops(fs, q, len) )
goto dropit ;
mtag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET,
sizeof(struct dn_pkt_tag), M_NOWAIT);
if ( mtag == NULL )
goto dropit ;
m_tag_prepend(m, mtag);
pkt = (struct dn_pkt_tag *)(mtag+1);
bzero(pkt, sizeof(struct dn_pkt_tag));
pkt->rule = fwa->rule ;
pkt->dn_dir = dir ;
pkt->ifp = fwa->oif;
if (dir == DN_TO_IP_OUT) {
lck_mtx_lock(rt_mtx);
pkt->ro = *(fwa->ro);
if (fwa->ro->ro_rt)
fwa->ro->ro_rt->rt_refcnt++ ;
if (fwa->dst == (struct sockaddr_in *)&fwa->ro->ro_dst)
fwa->dst = (struct sockaddr_in *)&(pkt->ro.ro_dst) ;
lck_mtx_unlock(rt_mtx);
pkt->dn_dst = fwa->dst;
pkt->flags = fwa->flags;
}
if (q->head == NULL)
q->head = m;
else
q->tail->m_nextpkt = m;
q->tail = m;
q->len++;
q->len_bytes += len ;
if ( q->head != m )
goto done;
if (is_pipe) {
dn_key t = 0 ;
if (pipe->bandwidth)
t = SET_TICKS(m, q, pipe);
q->sched_time = curr_time ;
if (t == 0)
ready_event( q );
else
heap_insert(&ready_heap, curr_time + t , q );
} else {
if (DN_KEY_GT(q->S, q->F)) {
q->S = pipe->V ;
pipe->sum += fs->weight ;
} else {
heap_extract(&(pipe->idle_heap), q);
q->S = MAX64(q->F, pipe->V ) ;
}
q->F = q->S + ( len<<MY_M )/(u_int64_t) fs->weight;
if (pipe->not_eligible_heap.elements == 0 &&
pipe->scheduler_heap.elements == 0)
pipe->V = MAX64 ( q->S, pipe->V );
fs->backlogged++ ;
if (DN_KEY_GT(q->S, pipe->V) ) {
if (pipe->scheduler_heap.elements == 0)
printf("dummynet: ++ ouch! not eligible but empty scheduler!\n");
heap_insert(&(pipe->not_eligible_heap), q->S, q);
} else {
heap_insert(&(pipe->scheduler_heap), q->F, q);
if (pipe->numbytes >= 0) {
if (pipe->scheduler_heap.elements != 1)
printf("dummynet: OUCH! pipe should have been idle!\n");
DPRINTF(("dummynet: waking up pipe %d at %d\n",
pipe->pipe_nr, (int)(q->F >> MY_M)));
pipe->sched_time = curr_time ;
ready_event_wfq(pipe);
}
}
}
done:
lck_mtx_unlock(dn_mutex);
return 0;
dropit:
if (q)
q->drops++ ;
lck_mtx_unlock(dn_mutex);
m_freem(m);
return ( (fs && (fs->flags_fs & DN_NOERROR)) ? 0 : ENOBUFS);
}
#define DN_FREE_PKT(_m) do { \
struct m_tag *tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET, NULL); \
if (tag) { \
struct dn_pkt_tag *n = (struct dn_pkt_tag *)(tag+1); \
if (n->ro.ro_rt) \
rtfree(n->ro.ro_rt); \
} \
m_tag_delete(_m, tag); \
m_freem(_m); \
} while (0)
static void
purge_flow_set(struct dn_flow_set *fs, int all)
{
struct dn_flow_queue *q, *qn ;
int i ;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
for (i = 0 ; i <= fs->rq_size ; i++ ) {
for (q = fs->rq[i] ; q ; q = qn ) {
struct mbuf *m, *mnext;
mnext = q->head;
while ((m = mnext) != NULL) {
mnext = m->m_nextpkt;
DN_FREE_PKT(m);
}
qn = q->next ;
FREE(q, M_DUMMYNET);
}
fs->rq[i] = NULL ;
}
fs->rq_elements = 0 ;
if (all) {
if (fs->w_q_lookup)
FREE(fs->w_q_lookup, M_DUMMYNET);
if (fs->rq)
FREE(fs->rq, M_DUMMYNET);
if (fs->pipe && fs != &(fs->pipe->fs) )
FREE(fs, M_DUMMYNET);
}
}
static void
purge_pipe(struct dn_pipe *pipe)
{
struct mbuf *m, *mnext;
purge_flow_set( &(pipe->fs), 1 );
mnext = pipe->head;
while ((m = mnext) != NULL) {
mnext = m->m_nextpkt;
DN_FREE_PKT(m);
}
heap_free( &(pipe->scheduler_heap) );
heap_free( &(pipe->not_eligible_heap) );
heap_free( &(pipe->idle_heap) );
}
static void
dummynet_flush()
{
struct dn_pipe *curr_p, *p ;
struct dn_flow_set *fs, *curr_fs;
lck_mtx_lock(dn_mutex);
flush_pipe_ptrs(NULL);
p = all_pipes ;
all_pipes = NULL ;
fs = all_flow_sets ;
all_flow_sets = NULL ;
heap_free(&ready_heap);
heap_free(&wfq_ready_heap);
heap_free(&extract_heap);
for ( ; fs ; ) {
curr_fs = fs ;
fs = fs->next ;
purge_flow_set(curr_fs, 1);
}
for ( ; p ; ) {
purge_pipe(p);
curr_p = p ;
p = p->next ;
FREE(curr_p, M_DUMMYNET);
}
lck_mtx_unlock(dn_mutex);
}
extern struct ip_fw *ip_fw_default_rule ;
static void
dn_rule_delete_fs(struct dn_flow_set *fs, void *r)
{
int i ;
struct dn_flow_queue *q ;
struct mbuf *m ;
for (i = 0 ; i <= fs->rq_size ; i++)
for (q = fs->rq[i] ; q ; q = q->next )
for (m = q->head ; m ; m = m->m_nextpkt ) {
struct dn_pkt_tag *pkt = dn_tag_get(m) ;
if (pkt->rule == r)
pkt->rule = ip_fw_default_rule ;
}
}
void
dn_rule_delete(void *r)
{
struct dn_pipe *p ;
struct dn_flow_set *fs ;
struct dn_pkt_tag *pkt ;
struct mbuf *m ;
lck_mtx_lock(dn_mutex);
for ( fs = all_flow_sets ; fs ; fs = fs->next )
dn_rule_delete_fs(fs, r);
for ( p = all_pipes ; p ; p = p->next ) {
fs = &(p->fs) ;
dn_rule_delete_fs(fs, r);
for (m = p->head ; m ; m = m->m_nextpkt ) {
pkt = dn_tag_get(m) ;
if (pkt->rule == r)
pkt->rule = ip_fw_default_rule ;
}
}
lck_mtx_unlock(dn_mutex);
}
static int
config_red(struct dn_flow_set *p, struct dn_flow_set * x)
{
int i;
x->w_q = p->w_q;
x->min_th = SCALE(p->min_th);
x->max_th = SCALE(p->max_th);
x->max_p = p->max_p;
x->c_1 = p->max_p / (p->max_th - p->min_th);
x->c_2 = SCALE_MUL(x->c_1, SCALE(p->min_th));
if (x->flags_fs & DN_IS_GENTLE_RED) {
x->c_3 = (SCALE(1) - p->max_p) / p->max_th;
x->c_4 = (SCALE(1) - 2 * p->max_p);
}
if (x->w_q_lookup) {
FREE(x->w_q_lookup, M_DUMMYNET);
x->w_q_lookup = NULL ;
}
if (red_lookup_depth == 0) {
printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth must be > 0\n");
FREE(x, M_DUMMYNET);
return EINVAL;
}
x->lookup_depth = red_lookup_depth;
x->w_q_lookup = (u_int *) _MALLOC(x->lookup_depth * sizeof(int),
M_DUMMYNET, M_DONTWAIT);
if (x->w_q_lookup == NULL) {
printf("dummynet: sorry, cannot allocate red lookup table\n");
FREE(x, M_DUMMYNET);
return ENOSPC;
}
x->lookup_step = p->lookup_step ;
x->lookup_weight = p->lookup_weight ;
x->w_q_lookup[0] = SCALE(1) - x->w_q;
for (i = 1; i < x->lookup_depth; i++)
x->w_q_lookup[i] = SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight);
if (red_avg_pkt_size < 1)
red_avg_pkt_size = 512 ;
x->avg_pkt_size = red_avg_pkt_size ;
if (red_max_pkt_size < 1)
red_max_pkt_size = 1500 ;
x->max_pkt_size = red_max_pkt_size ;
return 0 ;
}
static int
alloc_hash(struct dn_flow_set *x, struct dn_flow_set *pfs)
{
if (x->flags_fs & DN_HAVE_FLOW_MASK) {
int l = pfs->rq_size;
if (l == 0)
l = dn_hash_size;
if (l < 4)
l = 4;
else if (l > DN_MAX_HASH_SIZE)
l = DN_MAX_HASH_SIZE;
x->rq_size = l;
} else
x->rq_size = 1;
x->rq = _MALLOC((1 + x->rq_size) * sizeof(struct dn_flow_queue *),
M_DUMMYNET, M_DONTWAIT | M_ZERO);
if (x->rq == NULL) {
printf("dummynet: sorry, cannot allocate queue\n");
return ENOSPC;
}
x->rq_elements = 0;
return 0 ;
}
static void
set_fs_parms(struct dn_flow_set *x, struct dn_flow_set *src)
{
x->flags_fs = src->flags_fs;
x->qsize = src->qsize;
x->plr = src->plr;
x->flow_mask = src->flow_mask;
if (x->flags_fs & DN_QSIZE_IS_BYTES) {
if (x->qsize > 1024*1024)
x->qsize = 1024*1024 ;
} else {
if (x->qsize == 0)
x->qsize = 50 ;
if (x->qsize > 100)
x->qsize = 50 ;
}
if ( x->flags_fs & DN_IS_RED )
config_red(src, x) ;
}
static int
config_pipe(struct dn_pipe *p)
{
int i, r;
struct dn_flow_set *pfs = &(p->fs);
struct dn_flow_queue *q;
p->delay = ( p->delay * hz ) / 1000 ;
if (p->pipe_nr == 0 && pfs->fs_nr == 0)
return EINVAL ;
if (p->pipe_nr != 0 && pfs->fs_nr != 0)
return EINVAL ;
if (p->pipe_nr != 0) {
struct dn_pipe *x, *a, *b;
lck_mtx_lock(dn_mutex);
for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
a = b , b = b->next) ;
if (b == NULL || b->pipe_nr != p->pipe_nr) {
x = _MALLOC(sizeof(struct dn_pipe), M_DUMMYNET, M_DONTWAIT | M_ZERO) ;
if (x == NULL) {
lck_mtx_unlock(dn_mutex);
printf("dummynet: no memory for new pipe\n");
return ENOSPC;
}
x->pipe_nr = p->pipe_nr;
x->fs.pipe = x ;
x->idle_heap.size = x->idle_heap.elements = 0 ;
x->idle_heap.offset=OFFSET_OF(struct dn_flow_queue, heap_pos);
} else {
x = b;
for (i = 0; i <= x->fs.rq_size; i++)
for (q = x->fs.rq[i]; q; q = q->next)
q->numbytes = 0;
}
x->bandwidth = p->bandwidth ;
x->numbytes = 0;
bcopy(p->if_name, x->if_name, sizeof(p->if_name) );
x->ifp = NULL ;
x->delay = p->delay ;
set_fs_parms(&(x->fs), pfs);
if ( x->fs.rq == NULL ) {
r = alloc_hash(&(x->fs), pfs) ;
if (r) {
lck_mtx_unlock(dn_mutex);
FREE(x, M_DUMMYNET);
return r ;
}
x->next = b ;
if (a == NULL)
all_pipes = x ;
else
a->next = x ;
}
lck_mtx_unlock(dn_mutex);
} else {
struct dn_flow_set *x, *a, *b ;
lck_mtx_lock(dn_mutex);
for (a=NULL, b=all_flow_sets ; b && b->fs_nr < pfs->fs_nr ;
a = b , b = b->next) ;
if (b == NULL || b->fs_nr != pfs->fs_nr) {
if (pfs->parent_nr == 0) {
lck_mtx_unlock(dn_mutex);
return EINVAL ;
}
x = _MALLOC(sizeof(struct dn_flow_set), M_DUMMYNET, M_DONTWAIT | M_ZERO);
if (x == NULL) {
lck_mtx_unlock(dn_mutex);
printf("dummynet: no memory for new flow_set\n");
return ENOSPC;
}
x->fs_nr = pfs->fs_nr;
x->parent_nr = pfs->parent_nr;
x->weight = pfs->weight ;
if (x->weight == 0)
x->weight = 1 ;
else if (x->weight > 100)
x->weight = 100 ;
} else {
if (pfs->parent_nr != 0 && b->parent_nr != pfs->parent_nr) {
lck_mtx_unlock(dn_mutex);
return EINVAL ;
}
x = b;
}
set_fs_parms(x, pfs);
if ( x->rq == NULL ) {
r = alloc_hash(x, pfs) ;
if (r) {
lck_mtx_unlock(dn_mutex);
FREE(x, M_DUMMYNET);
return r ;
}
x->next = b;
if (a == NULL)
all_flow_sets = x;
else
a->next = x;
}
lck_mtx_unlock(dn_mutex);
}
return 0 ;
}
static void
fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs)
{
int i = 0, found = 0 ;
for (; i < h->elements ;)
if ( ((struct dn_flow_queue *)h->p[i].object)->fs == fs) {
h->elements-- ;
h->p[i] = h->p[h->elements] ;
found++ ;
} else
i++ ;
if (found)
heapify(h);
}
static void
pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p)
{
if (h->elements > 0) {
int i = 0 ;
for (i=0; i < h->elements ; i++ ) {
if (h->p[i].object == p) {
h->elements-- ;
h->p[i] = h->p[h->elements] ;
heapify(h);
break ;
}
}
}
}
void
dummynet_drain()
{
struct dn_flow_set *fs;
struct dn_pipe *p;
struct mbuf *m, *mnext;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
heap_free(&ready_heap);
heap_free(&wfq_ready_heap);
heap_free(&extract_heap);
for (fs = all_flow_sets; fs; fs= fs->next )
purge_flow_set(fs, 0);
for (p = all_pipes; p; p= p->next ) {
purge_flow_set(&(p->fs), 0);
mnext = p->head;
while ((m = mnext) != NULL) {
mnext = m->m_nextpkt;
DN_FREE_PKT(m);
}
p->head = p->tail = NULL ;
}
}
static int
delete_pipe(struct dn_pipe *p)
{
if (p->pipe_nr == 0 && p->fs.fs_nr == 0)
return EINVAL ;
if (p->pipe_nr != 0 && p->fs.fs_nr != 0)
return EINVAL ;
if (p->pipe_nr != 0) {
struct dn_pipe *a, *b;
struct dn_flow_set *fs;
lck_mtx_lock(dn_mutex);
for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
a = b , b = b->next) ;
if (b == NULL || (b->pipe_nr != p->pipe_nr) ) {
lck_mtx_unlock(dn_mutex);
return EINVAL ;
}
if (a == NULL)
all_pipes = b->next ;
else
a->next = b->next ;
flush_pipe_ptrs(&(b->fs));
for (fs = all_flow_sets; fs; fs= fs->next )
if (fs->pipe == b) {
printf("dummynet: ++ ref to pipe %d from fs %d\n",
p->pipe_nr, fs->fs_nr);
fs->pipe = NULL ;
purge_flow_set(fs, 0);
}
fs_remove_from_heap(&ready_heap, &(b->fs));
purge_pipe(b);
pipe_remove_from_heap(&extract_heap, b);
pipe_remove_from_heap(&wfq_ready_heap, b);
lck_mtx_unlock(dn_mutex);
FREE(b, M_DUMMYNET);
} else {
struct dn_flow_set *a, *b;
lck_mtx_lock(dn_mutex);
for (a = NULL, b = all_flow_sets ; b && b->fs_nr < p->fs.fs_nr ;
a = b , b = b->next) ;
if (b == NULL || (b->fs_nr != p->fs.fs_nr) ) {
lck_mtx_unlock(dn_mutex);
return EINVAL ;
}
if (a == NULL)
all_flow_sets = b->next ;
else
a->next = b->next ;
flush_pipe_ptrs(b);
if (b->pipe != NULL) {
b->pipe->sum -= b->weight * b->backlogged ;
fs_remove_from_heap(&(b->pipe->not_eligible_heap), b);
fs_remove_from_heap(&(b->pipe->scheduler_heap), b);
#if 1
fs_remove_from_heap(&(b->pipe->idle_heap), b);
#endif
}
purge_flow_set(b, 1);
lck_mtx_unlock(dn_mutex);
}
return 0 ;
}
static char *
dn_copy_set(struct dn_flow_set *set, char *bp)
{
int i, copied = 0 ;
struct dn_flow_queue *q, *qp = (struct dn_flow_queue *)bp;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
for (i = 0 ; i <= set->rq_size ; i++)
for (q = set->rq[i] ; q ; q = q->next, qp++ ) {
if (q->hash_slot != i)
printf("dummynet: ++ at %d: wrong slot (have %d, "
"should be %d)\n", copied, q->hash_slot, i);
if (q->fs != set)
printf("dummynet: ++ at %d: wrong fs ptr (have %p, should be %p)\n",
i, q->fs, set);
copied++ ;
bcopy(q, qp, sizeof( *q ) );
qp->next = NULL ;
qp->head = qp->tail = NULL ;
qp->fs = NULL ;
}
if (copied != set->rq_elements)
printf("dummynet: ++ wrong count, have %d should be %d\n",
copied, set->rq_elements);
return (char *)qp ;
}
static size_t
dn_calc_size(void)
{
struct dn_flow_set *set ;
struct dn_pipe *p ;
size_t size ;
lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
for (p = all_pipes, size = 0 ; p ; p = p->next )
size += sizeof( *p ) +
p->fs.rq_elements * sizeof(struct dn_flow_queue);
for (set = all_flow_sets ; set ; set = set->next )
size += sizeof ( *set ) +
set->rq_elements * sizeof(struct dn_flow_queue);
return size ;
}
static int
dummynet_get(struct sockopt *sopt)
{
char *buf, *bp ;
size_t size ;
struct dn_flow_set *set ;
struct dn_pipe *p ;
int error=0, i ;
lck_mtx_lock(dn_mutex);
for (i = 0; i < 10; i++) {
size = dn_calc_size();
lck_mtx_unlock(dn_mutex);
buf = _MALLOC(size, M_TEMP, M_WAITOK);
lck_mtx_lock(dn_mutex);
if (size == dn_calc_size())
break;
FREE(buf, M_TEMP);
buf = NULL;
}
if (buf == NULL) {
lck_mtx_unlock(dn_mutex);
return ENOBUFS ;
}
for (p = all_pipes, bp = buf ; p ; p = p->next ) {
struct dn_pipe *pipe_bp = (struct dn_pipe *)bp ;
bcopy(p, bp, sizeof( *p ) );
pipe_bp->delay = (pipe_bp->delay * 1000) / hz ;
pipe_bp->next = (struct dn_pipe *)DN_IS_PIPE ;
pipe_bp->head = pipe_bp->tail = NULL ;
pipe_bp->fs.next = NULL ;
pipe_bp->fs.pipe = NULL ;
pipe_bp->fs.rq = NULL ;
bp += sizeof( *p ) ;
bp = dn_copy_set( &(p->fs), bp );
}
for (set = all_flow_sets ; set ; set = set->next ) {
struct dn_flow_set *fs_bp = (struct dn_flow_set *)bp ;
bcopy(set, bp, sizeof( *set ) );
fs_bp->next = (struct dn_flow_set *)DN_IS_QUEUE ;
fs_bp->pipe = NULL ;
fs_bp->rq = NULL ;
bp += sizeof( *set ) ;
bp = dn_copy_set( set, bp );
}
lck_mtx_unlock(dn_mutex);
error = sooptcopyout(sopt, buf, size);
FREE(buf, M_TEMP);
return error ;
}
static int
ip_dn_ctl(struct sockopt *sopt)
{
int error = 0 ;
struct dn_pipe *p, tmp_pipe;
if (sopt->sopt_dir == SOPT_SET && securelevel >= 3)
return (EPERM);
switch (sopt->sopt_name) {
default :
printf("dummynet: -- unknown option %d", sopt->sopt_name);
return EINVAL ;
case IP_DUMMYNET_GET :
error = dummynet_get(sopt);
break ;
case IP_DUMMYNET_FLUSH :
dummynet_flush() ;
break ;
case IP_DUMMYNET_CONFIGURE :
p = &tmp_pipe ;
error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
if (error)
break ;
error = config_pipe(p);
break ;
case IP_DUMMYNET_DEL :
p = &tmp_pipe ;
error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
if (error)
break ;
error = delete_pipe(p);
break ;
}
return error ;
}
void
ip_dn_init(void)
{
dn_mutex_grp_attr = lck_grp_attr_alloc_init();
dn_mutex_grp = lck_grp_alloc_init("dn", dn_mutex_grp_attr);
dn_mutex_attr = lck_attr_alloc_init();
lck_attr_setdefault(dn_mutex_attr);
if ((dn_mutex = lck_mtx_alloc_init(dn_mutex_grp, dn_mutex_attr)) == NULL) {
printf("ip_dn_init: can't alloc dn_mutex\n");
return;
}
all_pipes = NULL ;
all_flow_sets = NULL ;
ready_heap.size = ready_heap.elements = 0 ;
ready_heap.offset = 0 ;
wfq_ready_heap.size = wfq_ready_heap.elements = 0 ;
wfq_ready_heap.offset = 0 ;
extract_heap.size = extract_heap.elements = 0 ;
extract_heap.offset = 0 ;
ip_dn_ctl_ptr = ip_dn_ctl;
ip_dn_io_ptr = dummynet_io;
ip_dn_ruledel_ptr = dn_rule_delete;
timeout(dummynet, NULL, 1);
}