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 * $FreeBSD: releng/5.0/sys/netinet/ip_dummynet.c 106935 2002-11-14 23:46:04Z sam $
28 */
29
30 #define DEB(x)
31 #define DDB(x) x
32
33 /*
34 * This module implements IP dummynet, a bandwidth limiter/delay emulator
35 * used in conjunction with the ipfw package.
36 * Description of the data structures used is in ip_dummynet.h
37 * Here you mainly find the following blocks of code:
38 * + variable declarations;
39 * + heap management functions;
40 * + scheduler and dummynet functions;
41 * + configuration and initialization.
42 *
43 * NOTA BENE: critical sections are protected by splimp()/splx()
44 * pairs. One would think that splnet() is enough as for most of
45 * the netinet code, but it is not so because when used with
46 * bridging, dummynet is invoked at splimp().
47 *
48 * Most important Changes:
49 *
50 * 011004: KLDable
51 * 010124: Fixed WF2Q behaviour
52 * 010122: Fixed spl protection.
53 * 000601: WF2Q support
54 * 000106: large rewrite, use heaps to handle very many pipes.
55 * 980513: initial release
56 *
57 * include files marked with XXX are probably not needed
58 */
59
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/proc.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/time.h>
70 #include <sys/sysctl.h>
71 #include <net/if.h>
72 #include <net/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_fw.h>
78 #include <netinet/ip_dummynet.h>
79 #include <netinet/ip_var.h>
80
81 #include <netinet/if_ether.h> /* for struct arpcom */
82 #include <net/bridge.h>
83
84 /*
85 * We keep a private variable for the simulation time, but we could
86 * probably use an existing one ("softticks" in sys/kern/kern_timer.c)
87 */
88 static dn_key curr_time = 0 ; /* current simulation time */
89
90 static int dn_hash_size = 64 ; /* default hash size */
91
92 /* statistics on number of queue searches and search steps */
93 static int searches, search_steps ;
94 static int pipe_expire = 1 ; /* expire queue if empty */
95 static int dn_max_ratio = 16 ; /* max queues/buckets ratio */
96
97 static int red_lookup_depth = 256; /* RED - default lookup table depth */
98 static int red_avg_pkt_size = 512; /* RED - default medium packet size */
99 static int red_max_pkt_size = 1500; /* RED - default max packet size */
100
101 /*
102 * Three heaps contain queues and pipes that the scheduler handles:
103 *
104 * ready_heap contains all dn_flow_queue related to fixed-rate pipes.
105 *
106 * wfq_ready_heap contains the pipes associated with WF2Q flows
107 *
108 * extract_heap contains pipes associated with delay lines.
109 *
110 */
111
112 MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");
113
114 static struct dn_heap ready_heap, extract_heap, wfq_ready_heap ;
115
116 static int heap_init(struct dn_heap *h, int size) ;
117 static int heap_insert (struct dn_heap *h, dn_key key1, void *p);
118 static void heap_extract(struct dn_heap *h, void *obj);
119
120 static void transmit_event(struct dn_pipe *pipe);
121 static void ready_event(struct dn_flow_queue *q);
122
123 static struct dn_pipe *all_pipes = NULL ; /* list of all pipes */
124 static struct dn_flow_set *all_flow_sets = NULL ;/* list of all flow_sets */
125
126 static struct callout_handle dn_timeout;
127
128 #ifdef SYSCTL_NODE
129 SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet,
130 CTLFLAG_RW, 0, "Dummynet");
131 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size,
132 CTLFLAG_RW, &dn_hash_size, 0, "Default hash table size");
133 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, curr_time,
134 CTLFLAG_RD, &curr_time, 0, "Current tick");
135 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, ready_heap,
136 CTLFLAG_RD, &ready_heap.size, 0, "Size of ready heap");
137 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, extract_heap,
138 CTLFLAG_RD, &extract_heap.size, 0, "Size of extract heap");
139 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, searches,
140 CTLFLAG_RD, &searches, 0, "Number of queue searches");
141 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, search_steps,
142 CTLFLAG_RD, &search_steps, 0, "Number of queue search steps");
143 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire,
144 CTLFLAG_RW, &pipe_expire, 0, "Expire queue if empty");
145 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, max_chain_len,
146 CTLFLAG_RW, &dn_max_ratio, 0,
147 "Max ratio between dynamic queues and buckets");
148 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
149 CTLFLAG_RD, &red_lookup_depth, 0, "Depth of RED lookup table");
150 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
151 CTLFLAG_RD, &red_avg_pkt_size, 0, "RED Medium packet size");
152 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
153 CTLFLAG_RD, &red_max_pkt_size, 0, "RED Max packet size");
154 #endif
155
156 static int config_pipe(struct dn_pipe *p);
157 static int ip_dn_ctl(struct sockopt *sopt);
158
159 static void rt_unref(struct rtentry *);
160 static void dummynet(void *);
161 static void dummynet_flush(void);
162 void dummynet_drain(void);
163 static ip_dn_io_t dummynet_io;
164 static void dn_rule_delete(void *);
165
166 int if_tx_rdy(struct ifnet *ifp);
167
168 static void
169 rt_unref(struct rtentry *rt)
170 {
171 if (rt == NULL)
172 return ;
173 if (rt->rt_refcnt <= 0)
174 printf("-- warning, refcnt now %ld, decreasing\n", rt->rt_refcnt);
175 RTFREE(rt);
176 }
177
178 /*
179 * Heap management functions.
180 *
181 * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
182 * Some macros help finding parent/children so we can optimize them.
183 *
184 * heap_init() is called to expand the heap when needed.
185 * Increment size in blocks of 16 entries.
186 * XXX failure to allocate a new element is a pretty bad failure
187 * as we basically stall a whole queue forever!!
188 * Returns 1 on error, 0 on success
189 */
190 #define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 )
191 #define HEAP_LEFT(x) ( 2*(x) + 1 )
192 #define HEAP_IS_LEFT(x) ( (x) & 1 )
193 #define HEAP_RIGHT(x) ( 2*(x) + 2 )
194 #define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
195 #define HEAP_INCREMENT 15
196
197 static int
198 heap_init(struct dn_heap *h, int new_size)
199 {
200 struct dn_heap_entry *p;
201
202 if (h->size >= new_size ) {
203 printf("heap_init, Bogus call, have %d want %d\n",
204 h->size, new_size);
205 return 0 ;
206 }
207 new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT ;
208 p = malloc(new_size * sizeof(*p), M_DUMMYNET, M_DONTWAIT );
209 if (p == NULL) {
210 printf(" heap_init, resize %d failed\n", new_size );
211 return 1 ; /* error */
212 }
213 if (h->size > 0) {
214 bcopy(h->p, p, h->size * sizeof(*p) );
215 free(h->p, M_DUMMYNET);
216 }
217 h->p = p ;
218 h->size = new_size ;
219 return 0 ;
220 }
221
222 /*
223 * Insert element in heap. Normally, p != NULL, we insert p in
224 * a new position and bubble up. If p == NULL, then the element is
225 * already in place, and key is the position where to start the
226 * bubble-up.
227 * Returns 1 on failure (cannot allocate new heap entry)
228 *
229 * If offset > 0 the position (index, int) of the element in the heap is
230 * also stored in the element itself at the given offset in bytes.
231 */
232 #define SET_OFFSET(heap, node) \
233 if (heap->offset > 0) \
234 *((int *)((char *)(heap->p[node].object) + heap->offset)) = node ;
235 /*
236 * RESET_OFFSET is used for sanity checks. It sets offset to an invalid value.
237 */
238 #define RESET_OFFSET(heap, node) \
239 if (heap->offset > 0) \
240 *((int *)((char *)(heap->p[node].object) + heap->offset)) = -1 ;
241 static int
242 heap_insert(struct dn_heap *h, dn_key key1, void *p)
243 {
244 int son = h->elements ;
245
246 if (p == NULL) /* data already there, set starting point */
247 son = key1 ;
248 else { /* insert new element at the end, possibly resize */
249 son = h->elements ;
250 if (son == h->size) /* need resize... */
251 if (heap_init(h, h->elements+1) )
252 return 1 ; /* failure... */
253 h->p[son].object = p ;
254 h->p[son].key = key1 ;
255 h->elements++ ;
256 }
257 while (son > 0) { /* bubble up */
258 int father = HEAP_FATHER(son) ;
259 struct dn_heap_entry tmp ;
260
261 if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
262 break ; /* found right position */
263 /* son smaller than father, swap and repeat */
264 HEAP_SWAP(h->p[son], h->p[father], tmp) ;
265 SET_OFFSET(h, son);
266 son = father ;
267 }
268 SET_OFFSET(h, son);
269 return 0 ;
270 }
271
272 /*
273 * remove top element from heap, or obj if obj != NULL
274 */
275 static void
276 heap_extract(struct dn_heap *h, void *obj)
277 {
278 int child, father, max = h->elements - 1 ;
279
280 if (max < 0) {
281 printf("warning, extract from empty heap 0x%p\n", h);
282 return ;
283 }
284 father = 0 ; /* default: move up smallest child */
285 if (obj != NULL) { /* extract specific element, index is at offset */
286 if (h->offset <= 0)
287 panic("*** heap_extract from middle not supported on this heap!!!\n");
288 father = *((int *)((char *)obj + h->offset)) ;
289 if (father < 0 || father >= h->elements) {
290 printf("dummynet: heap_extract, father %d out of bound 0..%d\n",
291 father, h->elements);
292 panic("heap_extract");
293 }
294 }
295 RESET_OFFSET(h, father);
296 child = HEAP_LEFT(father) ; /* left child */
297 while (child <= max) { /* valid entry */
298 if (child != max && DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
299 child = child+1 ; /* take right child, otherwise left */
300 h->p[father] = h->p[child] ;
301 SET_OFFSET(h, father);
302 father = child ;
303 child = HEAP_LEFT(child) ; /* left child for next loop */
304 }
305 h->elements-- ;
306 if (father != max) {
307 /*
308 * Fill hole with last entry and bubble up, reusing the insert code
309 */
310 h->p[father] = h->p[max] ;
311 heap_insert(h, father, NULL); /* this one cannot fail */
312 }
313 }
314
315 #if 0
316 /*
317 * change object position and update references
318 * XXX this one is never used!
319 */
320 static void
321 heap_move(struct dn_heap *h, dn_key new_key, void *object)
322 {
323 int temp;
324 int i ;
325 int max = h->elements-1 ;
326 struct dn_heap_entry buf ;
327
328 if (h->offset <= 0)
329 panic("cannot move items on this heap");
330
331 i = *((int *)((char *)object + h->offset));
332 if (DN_KEY_LT(new_key, h->p[i].key) ) { /* must move up */
333 h->p[i].key = new_key ;
334 for (; i>0 && DN_KEY_LT(new_key, h->p[(temp = HEAP_FATHER(i))].key) ;
335 i = temp ) { /* bubble up */
336 HEAP_SWAP(h->p[i], h->p[temp], buf) ;
337 SET_OFFSET(h, i);
338 }
339 } else { /* must move down */
340 h->p[i].key = new_key ;
341 while ( (temp = HEAP_LEFT(i)) <= max ) { /* found left child */
342 if ((temp != max) && DN_KEY_GT(h->p[temp].key, h->p[temp+1].key))
343 temp++ ; /* select child with min key */
344 if (DN_KEY_GT(new_key, h->p[temp].key)) { /* go down */
345 HEAP_SWAP(h->p[i], h->p[temp], buf) ;
346 SET_OFFSET(h, i);
347 } else
348 break ;
349 i = temp ;
350 }
351 }
352 SET_OFFSET(h, i);
353 }
354 #endif /* heap_move, unused */
355
356 /*
357 * heapify() will reorganize data inside an array to maintain the
358 * heap property. It is needed when we delete a bunch of entries.
359 */
360 static void
361 heapify(struct dn_heap *h)
362 {
363 int i ;
364
365 for (i = 0 ; i < h->elements ; i++ )
366 heap_insert(h, i , NULL) ;
367 }
368
369 /*
370 * cleanup the heap and free data structure
371 */
372 static void
373 heap_free(struct dn_heap *h)
374 {
375 if (h->size >0 )
376 free(h->p, M_DUMMYNET);
377 bzero(h, sizeof(*h) );
378 }
379
380 /*
381 * --- end of heap management functions ---
382 */
383
384 /*
385 * Scheduler functions:
386 *
387 * transmit_event() is called when the delay-line needs to enter
388 * the scheduler, either because of existing pkts getting ready,
389 * or new packets entering the queue. The event handled is the delivery
390 * time of the packet.
391 *
392 * ready_event() does something similar with fixed-rate queues, and the
393 * event handled is the finish time of the head pkt.
394 *
395 * wfq_ready_event() does something similar with WF2Q queues, and the
396 * event handled is the start time of the head pkt.
397 *
398 * In all cases, we make sure that the data structures are consistent
399 * before passing pkts out, because this might trigger recursive
400 * invocations of the procedures.
401 */
402 static void
403 transmit_event(struct dn_pipe *pipe)
404 {
405 struct dn_pkt *pkt ;
406
407 while ( (pkt = pipe->head) && DN_KEY_LEQ(pkt->output_time, curr_time) ) {
408 /*
409 * first unlink, then call procedures, since ip_input() can invoke
410 * ip_output() and viceversa, thus causing nested calls
411 */
412 pipe->head = DN_NEXT(pkt) ;
413
414 /*
415 * The actual mbuf is preceded by a struct dn_pkt, resembling an mbuf
416 * (NOT A REAL one, just a small block of malloc'ed memory) with
417 * m_type = MT_TAG, m_flags = PACKET_TAG_DUMMYNET
418 * dn_m (m_next) = actual mbuf to be processed by ip_input/output
419 * and some other fields.
420 * The block IS FREED HERE because it contains parameters passed
421 * to the called routine.
422 */
423 switch (pkt->dn_dir) {
424 case DN_TO_IP_OUT:
425 (void)ip_output((struct mbuf *)pkt, NULL, NULL, 0, NULL, NULL);
426 rt_unref (pkt->ro.ro_rt) ;
427 break ;
428
429 case DN_TO_IP_IN :
430 ip_input((struct mbuf *)pkt) ;
431 break ;
432
433 case DN_TO_BDG_FWD :
434 if (!BDG_LOADED) {
435 /* somebody unloaded the bridge module. Drop pkt */
436 printf("-- dropping bridged packet trapped in pipe--\n");
437 m_freem(pkt->dn_m);
438 break;
439 } /* fallthrough */
440 case DN_TO_ETH_DEMUX:
441 {
442 struct mbuf *m = (struct mbuf *)pkt ;
443
444 if (pkt->dn_m->m_len < ETHER_HDR_LEN &&
445 (pkt->dn_m = m_pullup(pkt->dn_m, ETHER_HDR_LEN)) == NULL) {
446 printf("dummynet/bridge: pullup fail, dropping pkt\n");
447 break;
448 }
449 /*
450 * bdg_forward() wants a pointer to the pseudo-mbuf-header, but
451 * on return it will supply the pointer to the actual packet
452 * (originally pkt->dn_m, but could be something else now) if
453 * it has not consumed it.
454 */
455 if (pkt->dn_dir == DN_TO_BDG_FWD) {
456 /*
457 * same as ether_input, make eh be a pointer into the mbuf
458 */
459 m = bdg_forward_ptr(m, pkt->ifp);
460 if (m)
461 m_freem(m);
462 } else
463 ether_demux(NULL, m); /* which consumes the mbuf */
464 }
465 break ;
466 case DN_TO_ETH_OUT:
467 ether_output_frame(pkt->ifp, (struct mbuf *)pkt);
468 break;
469
470 default:
471 printf("dummynet: bad switch %d!\n", pkt->dn_dir);
472 m_freem(pkt->dn_m);
473 break ;
474 }
475 free(pkt, M_DUMMYNET);
476 }
477 /* if there are leftover packets, put into the heap for next event */
478 if ( (pkt = pipe->head) )
479 heap_insert(&extract_heap, pkt->output_time, pipe ) ;
480 /* XXX should check errors on heap_insert, by draining the
481 * whole pipe p and hoping in the future we are more successful
482 */
483 }
484
485 /*
486 * the following macro computes how many ticks we have to wait
487 * before being able to transmit a packet. The credit is taken from
488 * either a pipe (WF2Q) or a flow_queue (per-flow queueing)
489 */
490 #define SET_TICKS(pkt, q, p) \
491 (pkt->dn_m->m_pkthdr.len*8*hz - (q)->numbytes + p->bandwidth - 1 ) / \
492 p->bandwidth ;
493
494 /*
495 * extract pkt from queue, compute output time (could be now)
496 * and put into delay line (p_queue)
497 */
498 static void
499 move_pkt(struct dn_pkt *pkt, struct dn_flow_queue *q,
500 struct dn_pipe *p, int len)
501 {
502 q->head = DN_NEXT(pkt) ;
503 q->len-- ;
504 q->len_bytes -= len ;
505
506 pkt->output_time = curr_time + p->delay ;
507
508 if (p->head == NULL)
509 p->head = pkt;
510 else
511 DN_NEXT(p->tail) = pkt;
512 p->tail = pkt;
513 DN_NEXT(p->tail) = NULL;
514 }
515
516 /*
517 * ready_event() is invoked every time the queue must enter the
518 * scheduler, either because the first packet arrives, or because
519 * a previously scheduled event fired.
520 * On invokation, drain as many pkts as possible (could be 0) and then
521 * if there are leftover packets reinsert the pkt in the scheduler.
522 */
523 static void
524 ready_event(struct dn_flow_queue *q)
525 {
526 struct dn_pkt *pkt;
527 struct dn_pipe *p = q->fs->pipe ;
528 int p_was_empty ;
529
530 if (p == NULL) {
531 printf("ready_event- pipe is gone\n");
532 return ;
533 }
534 p_was_empty = (p->head == NULL) ;
535
536 /*
537 * schedule fixed-rate queues linked to this pipe:
538 * Account for the bw accumulated since last scheduling, then
539 * drain as many pkts as allowed by q->numbytes and move to
540 * the delay line (in p) computing output time.
541 * bandwidth==0 (no limit) means we can drain the whole queue,
542 * setting len_scaled = 0 does the job.
543 */
544 q->numbytes += ( curr_time - q->sched_time ) * p->bandwidth;
545 while ( (pkt = q->head) != NULL ) {
546 int len = pkt->dn_m->m_pkthdr.len;
547 int len_scaled = p->bandwidth ? len*8*hz : 0 ;
548 if (len_scaled > q->numbytes )
549 break ;
550 q->numbytes -= len_scaled ;
551 move_pkt(pkt, q, p, len);
552 }
553 /*
554 * If we have more packets queued, schedule next ready event
555 * (can only occur when bandwidth != 0, otherwise we would have
556 * flushed the whole queue in the previous loop).
557 * To this purpose we record the current time and compute how many
558 * ticks to go for the finish time of the packet.
559 */
560 if ( (pkt = q->head) != NULL ) { /* this implies bandwidth != 0 */
561 dn_key t = SET_TICKS(pkt, q, p); /* ticks i have to wait */
562 q->sched_time = curr_time ;
563 heap_insert(&ready_heap, curr_time + t, (void *)q );
564 /* XXX should check errors on heap_insert, and drain the whole
565 * queue on error hoping next time we are luckier.
566 */
567 } else /* RED needs to know when the queue becomes empty */
568 q->q_time = curr_time;
569 /*
570 * If the delay line was empty call transmit_event(p) now.
571 * Otherwise, the scheduler will take care of it.
572 */
573 if (p_was_empty)
574 transmit_event(p);
575 }
576
577 /*
578 * Called when we can transmit packets on WF2Q queues. Take pkts out of
579 * the queues at their start time, and enqueue into the delay line.
580 * Packets are drained until p->numbytes < 0. As long as
581 * len_scaled >= p->numbytes, the packet goes into the delay line
582 * with a deadline p->delay. For the last packet, if p->numbytes<0,
583 * there is an additional delay.
584 */
585 static void
586 ready_event_wfq(struct dn_pipe *p)
587 {
588 int p_was_empty = (p->head == NULL) ;
589 struct dn_heap *sch = &(p->scheduler_heap);
590 struct dn_heap *neh = &(p->not_eligible_heap) ;
591
592 if (p->if_name[0] == 0) /* tx clock is simulated */
593 p->numbytes += ( curr_time - p->sched_time ) * p->bandwidth;
594 else { /* tx clock is for real, the ifq must be empty or this is a NOP */
595 if (p->ifp && p->ifp->if_snd.ifq_head != NULL)
596 return ;
597 else {
598 DEB(printf("pipe %d ready from %s --\n",
599 p->pipe_nr, p->if_name);)
600 }
601 }
602
603 /*
604 * While we have backlogged traffic AND credit, we need to do
605 * something on the queue.
606 */
607 while ( p->numbytes >=0 && (sch->elements>0 || neh->elements >0) ) {
608 if (sch->elements > 0) { /* have some eligible pkts to send out */
609 struct dn_flow_queue *q = sch->p[0].object ;
610 struct dn_pkt *pkt = q->head;
611 struct dn_flow_set *fs = q->fs;
612 u_int64_t len = pkt->dn_m->m_pkthdr.len;
613 int len_scaled = p->bandwidth ? len*8*hz : 0 ;
614
615 heap_extract(sch, NULL); /* remove queue from heap */
616 p->numbytes -= len_scaled ;
617 move_pkt(pkt, q, p, len);
618
619 p->V += (len<<MY_M) / p->sum ; /* update V */
620 q->S = q->F ; /* update start time */
621 if (q->len == 0) { /* Flow not backlogged any more */
622 fs->backlogged-- ;
623 heap_insert(&(p->idle_heap), q->F, q);
624 } else { /* still backlogged */
625 /*
626 * update F and position in backlogged queue, then
627 * put flow in not_eligible_heap (we will fix this later).
628 */
629 len = (q->head)->dn_m->m_pkthdr.len;
630 q->F += (len<<MY_M)/(u_int64_t) fs->weight ;
631 if (DN_KEY_LEQ(q->S, p->V))
632 heap_insert(neh, q->S, q);
633 else
634 heap_insert(sch, q->F, q);
635 }
636 }
637 /*
638 * now compute V = max(V, min(S_i)). Remember that all elements in sch
639 * have by definition S_i <= V so if sch is not empty, V is surely
640 * the max and we must not update it. Conversely, if sch is empty
641 * we only need to look at neh.
642 */
643 if (sch->elements == 0 && neh->elements > 0)
644 p->V = MAX64 ( p->V, neh->p[0].key );
645 /* move from neh to sch any packets that have become eligible */
646 while (neh->elements > 0 && DN_KEY_LEQ(neh->p[0].key, p->V) ) {
647 struct dn_flow_queue *q = neh->p[0].object ;
648 heap_extract(neh, NULL);
649 heap_insert(sch, q->F, q);
650 }
651
652 if (p->if_name[0] != '\0') {/* tx clock is from a real thing */
653 p->numbytes = -1 ; /* mark not ready for I/O */
654 break ;
655 }
656 }
657 if (sch->elements == 0 && neh->elements == 0 && p->numbytes >= 0
658 && p->idle_heap.elements > 0) {
659 /*
660 * no traffic and no events scheduled. We can get rid of idle-heap.
661 */
662 int i ;
663
664 for (i = 0 ; i < p->idle_heap.elements ; i++) {
665 struct dn_flow_queue *q = p->idle_heap.p[i].object ;
666
667 q->F = 0 ;
668 q->S = q->F + 1 ;
669 }
670 p->sum = 0 ;
671 p->V = 0 ;
672 p->idle_heap.elements = 0 ;
673 }
674 /*
675 * If we are getting clocks from dummynet (not a real interface) and
676 * If we are under credit, schedule the next ready event.
677 * Also fix the delivery time of the last packet.
678 */
679 if (p->if_name[0]==0 && p->numbytes < 0) { /* this implies bandwidth >0 */
680 dn_key t=0 ; /* number of ticks i have to wait */
681
682 if (p->bandwidth > 0)
683 t = ( p->bandwidth -1 - p->numbytes) / p->bandwidth ;
684 p->tail->output_time += t ;
685 p->sched_time = curr_time ;
686 heap_insert(&wfq_ready_heap, curr_time + t, (void *)p);
687 /* XXX should check errors on heap_insert, and drain the whole
688 * queue on error hoping next time we are luckier.
689 */
690 }
691 /*
692 * If the delay line was empty call transmit_event(p) now.
693 * Otherwise, the scheduler will take care of it.
694 */
695 if (p_was_empty)
696 transmit_event(p);
697 }
698
699 /*
700 * This is called once per tick, or HZ times per second. It is used to
701 * increment the current tick counter and schedule expired events.
702 */
703 static void
704 dummynet(void * __unused unused)
705 {
706 void *p ; /* generic parameter to handler */
707 struct dn_heap *h ;
708 int s ;
709 struct dn_heap *heaps[3];
710 int i;
711 struct dn_pipe *pe ;
712
713 heaps[0] = &ready_heap ; /* fixed-rate queues */
714 heaps[1] = &wfq_ready_heap ; /* wfq queues */
715 heaps[2] = &extract_heap ; /* delay line */
716 s = splimp(); /* see note on top, splnet() is not enough */
717 curr_time++ ;
718 for (i=0; i < 3 ; i++) {
719 h = heaps[i];
720 while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time) ) {
721 DDB(if (h->p[0].key > curr_time)
722 printf("-- dummynet: warning, heap %d is %d ticks late\n",
723 i, (int)(curr_time - h->p[0].key));)
724 p = h->p[0].object ; /* store a copy before heap_extract */
725 heap_extract(h, NULL); /* need to extract before processing */
726 if (i == 0)
727 ready_event(p) ;
728 else if (i == 1) {
729 struct dn_pipe *pipe = p;
730 if (pipe->if_name[0] != '\0')
731 printf("*** bad ready_event_wfq for pipe %s\n",
732 pipe->if_name);
733 else
734 ready_event_wfq(p) ;
735 } else
736 transmit_event(p);
737 }
738 }
739 /* sweep pipes trying to expire idle flow_queues */
740 for (pe = all_pipes; pe ; pe = pe->next )
741 if (pe->idle_heap.elements > 0 &&
742 DN_KEY_LT(pe->idle_heap.p[0].key, pe->V) ) {
743 struct dn_flow_queue *q = pe->idle_heap.p[0].object ;
744
745 heap_extract(&(pe->idle_heap), NULL);
746 q->S = q->F + 1 ; /* mark timestamp as invalid */
747 pe->sum -= q->fs->weight ;
748 }
749 splx(s);
750 dn_timeout = timeout(dummynet, NULL, 1);
751 }
752
753 /*
754 * called by an interface when tx_rdy occurs.
755 */
756 int
757 if_tx_rdy(struct ifnet *ifp)
758 {
759 struct dn_pipe *p;
760
761 for (p = all_pipes; p ; p = p->next )
762 if (p->ifp == ifp)
763 break ;
764 if (p == NULL) {
765 char buf[32];
766 sprintf(buf, "%s%d",ifp->if_name, ifp->if_unit);
767 for (p = all_pipes; p ; p = p->next )
768 if (!strcmp(p->if_name, buf) ) {
769 p->ifp = ifp ;
770 DEB(printf("++ tx rdy from %s (now found)\n", buf);)
771 break ;
772 }
773 }
774 if (p != NULL) {
775 DEB(printf("++ tx rdy from %s%d - qlen %d\n", ifp->if_name,
776 ifp->if_unit, ifp->if_snd.ifq_len);)
777 p->numbytes = 0 ; /* mark ready for I/O */
778 ready_event_wfq(p);
779 }
780 return 0;
781 }
782
783 /*
784 * Unconditionally expire empty queues in case of shortage.
785 * Returns the number of queues freed.
786 */
787 static int
788 expire_queues(struct dn_flow_set *fs)
789 {
790 struct dn_flow_queue *q, *prev ;
791 int i, initial_elements = fs->rq_elements ;
792
793 if (fs->last_expired == time_second)
794 return 0 ;
795 fs->last_expired = time_second ;
796 for (i = 0 ; i <= fs->rq_size ; i++) /* last one is overflow */
797 for (prev=NULL, q = fs->rq[i] ; q != NULL ; )
798 if (q->head != NULL || q->S != q->F+1) {
799 prev = q ;
800 q = q->next ;
801 } else { /* entry is idle, expire it */
802 struct dn_flow_queue *old_q = q ;
803
804 if (prev != NULL)
805 prev->next = q = q->next ;
806 else
807 fs->rq[i] = q = q->next ;
808 fs->rq_elements-- ;
809 free(old_q, M_DUMMYNET);
810 }
811 return initial_elements - fs->rq_elements ;
812 }
813
814 /*
815 * If room, create a new queue and put at head of slot i;
816 * otherwise, create or use the default queue.
817 */
818 static struct dn_flow_queue *
819 create_queue(struct dn_flow_set *fs, int i)
820 {
821 struct dn_flow_queue *q ;
822
823 if (fs->rq_elements > fs->rq_size * dn_max_ratio &&
824 expire_queues(fs) == 0) {
825 /*
826 * No way to get room, use or create overflow queue.
827 */
828 i = fs->rq_size ;
829 if ( fs->rq[i] != NULL )
830 return fs->rq[i] ;
831 }
832 q = malloc(sizeof(*q), M_DUMMYNET, M_DONTWAIT | M_ZERO);
833 if (q == NULL) {
834 printf("sorry, cannot allocate queue for new flow\n");
835 return NULL ;
836 }
837 q->fs = fs ;
838 q->hash_slot = i ;
839 q->next = fs->rq[i] ;
840 q->S = q->F + 1; /* hack - mark timestamp as invalid */
841 fs->rq[i] = q ;
842 fs->rq_elements++ ;
843 return q ;
844 }
845
846 /*
847 * Given a flow_set and a pkt in last_pkt, find a matching queue
848 * after appropriate masking. The queue is moved to front
849 * so that further searches take less time.
850 */
851 static struct dn_flow_queue *
852 find_queue(struct dn_flow_set *fs, struct ipfw_flow_id *id)
853 {
854 int i = 0 ; /* we need i and q for new allocations */
855 struct dn_flow_queue *q, *prev;
856
857 if ( !(fs->flags_fs & DN_HAVE_FLOW_MASK) )
858 q = fs->rq[0] ;
859 else {
860 /* first, do the masking */
861 id->dst_ip &= fs->flow_mask.dst_ip ;
862 id->src_ip &= fs->flow_mask.src_ip ;
863 id->dst_port &= fs->flow_mask.dst_port ;
864 id->src_port &= fs->flow_mask.src_port ;
865 id->proto &= fs->flow_mask.proto ;
866 id->flags = 0 ; /* we don't care about this one */
867 /* then, hash function */
868 i = ( (id->dst_ip) & 0xffff ) ^
869 ( (id->dst_ip >> 15) & 0xffff ) ^
870 ( (id->src_ip << 1) & 0xffff ) ^
871 ( (id->src_ip >> 16 ) & 0xffff ) ^
872 (id->dst_port << 1) ^ (id->src_port) ^
873 (id->proto );
874 i = i % fs->rq_size ;
875 /* finally, scan the current list for a match */
876 searches++ ;
877 for (prev=NULL, q = fs->rq[i] ; q ; ) {
878 search_steps++;
879 if (id->dst_ip == q->id.dst_ip &&
880 id->src_ip == q->id.src_ip &&
881 id->dst_port == q->id.dst_port &&
882 id->src_port == q->id.src_port &&
883 id->proto == q->id.proto &&
884 id->flags == q->id.flags)
885 break ; /* found */
886 else if (pipe_expire && q->head == NULL && q->S == q->F+1 ) {
887 /* entry is idle and not in any heap, expire it */
888 struct dn_flow_queue *old_q = q ;
889
890 if (prev != NULL)
891 prev->next = q = q->next ;
892 else
893 fs->rq[i] = q = q->next ;
894 fs->rq_elements-- ;
895 free(old_q, M_DUMMYNET);
896 continue ;
897 }
898 prev = q ;
899 q = q->next ;
900 }
901 if (q && prev != NULL) { /* found and not in front */
902 prev->next = q->next ;
903 q->next = fs->rq[i] ;
904 fs->rq[i] = q ;
905 }
906 }
907 if (q == NULL) { /* no match, need to allocate a new entry */
908 q = create_queue(fs, i);
909 if (q != NULL)
910 q->id = *id ;
911 }
912 return q ;
913 }
914
915 static int
916 red_drops(struct dn_flow_set *fs, struct dn_flow_queue *q, int len)
917 {
918 /*
919 * RED algorithm
920 *
921 * RED calculates the average queue size (avg) using a low-pass filter
922 * with an exponential weighted (w_q) moving average:
923 * avg <- (1-w_q) * avg + w_q * q_size
924 * where q_size is the queue length (measured in bytes or * packets).
925 *
926 * If q_size == 0, we compute the idle time for the link, and set
927 * avg = (1 - w_q)^(idle/s)
928 * where s is the time needed for transmitting a medium-sized packet.
929 *
930 * Now, if avg < min_th the packet is enqueued.
931 * If avg > max_th the packet is dropped. Otherwise, the packet is
932 * dropped with probability P function of avg.
933 *
934 */
935
936 int64_t p_b = 0;
937 /* queue in bytes or packets ? */
938 u_int q_size = (fs->flags_fs & DN_QSIZE_IS_BYTES) ? q->len_bytes : q->len;
939
940 DEB(printf("\n%d q: %2u ", (int) curr_time, q_size);)
941
942 /* average queue size estimation */
943 if (q_size != 0) {
944 /*
945 * queue is not empty, avg <- avg + (q_size - avg) * w_q
946 */
947 int diff = SCALE(q_size) - q->avg;
948 int64_t v = SCALE_MUL((int64_t) diff, (int64_t) fs->w_q);
949
950 q->avg += (int) v;
951 } else {
952 /*
953 * queue is empty, find for how long the queue has been
954 * empty and use a lookup table for computing
955 * (1 - * w_q)^(idle_time/s) where s is the time to send a
956 * (small) packet.
957 * XXX check wraps...
958 */
959 if (q->avg) {
960 u_int t = (curr_time - q->q_time) / fs->lookup_step;
961
962 q->avg = (t < fs->lookup_depth) ?
963 SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
964 }
965 }
966 DEB(printf("avg: %u ", SCALE_VAL(q->avg));)
967
968 /* should i drop ? */
969
970 if (q->avg < fs->min_th) {
971 q->count = -1;
972 return 0; /* accept packet ; */
973 }
974 if (q->avg >= fs->max_th) { /* average queue >= max threshold */
975 if (fs->flags_fs & DN_IS_GENTLE_RED) {
976 /*
977 * According to Gentle-RED, if avg is greater than max_th the
978 * packet is dropped with a probability
979 * p_b = c_3 * avg - c_4
980 * where c_3 = (1 - max_p) / max_th, and c_4 = 1 - 2 * max_p
981 */
982 p_b = SCALE_MUL((int64_t) fs->c_3, (int64_t) q->avg) - fs->c_4;
983 } else {
984 q->count = -1;
985 printf("- drop");
986 return 1 ;
987 }
988 } else if (q->avg > fs->min_th) {
989 /*
990 * we compute p_b using the linear dropping function p_b = c_1 *
991 * avg - c_2, where c_1 = max_p / (max_th - min_th), and c_2 =
992 * max_p * min_th / (max_th - min_th)
993 */
994 p_b = SCALE_MUL((int64_t) fs->c_1, (int64_t) q->avg) - fs->c_2;
995 }
996 if (fs->flags_fs & DN_QSIZE_IS_BYTES)
997 p_b = (p_b * len) / fs->max_pkt_size;
998 if (++q->count == 0)
999 q->random = random() & 0xffff;
1000 else {
1001 /*
1002 * q->count counts packets arrived since last drop, so a greater
1003 * value of q->count means a greater packet drop probability.
1004 */
1005 if (SCALE_MUL(p_b, SCALE((int64_t) q->count)) > q->random) {
1006 q->count = 0;
1007 DEB(printf("- red drop");)
1008 /* after a drop we calculate a new random value */
1009 q->random = random() & 0xffff;
1010 return 1; /* drop */
1011 }
1012 }
1013 /* end of RED algorithm */
1014 return 0 ; /* accept */
1015 }
1016
1017 static __inline
1018 struct dn_flow_set *
1019 locate_flowset(int pipe_nr, struct ip_fw *rule)
1020 {
1021 #if IPFW2
1022 struct dn_flow_set *fs;
1023 ipfw_insn *cmd = rule->cmd + rule->act_ofs;
1024
1025 if (cmd->opcode == O_LOG)
1026 cmd += F_LEN(cmd);
1027 fs = ((ipfw_insn_pipe *)cmd)->pipe_ptr;
1028
1029 if (fs != NULL)
1030 return fs;
1031
1032 if (cmd->opcode == O_QUEUE)
1033 #else /* !IPFW2 */
1034 struct dn_flow_set *fs = NULL ;
1035
1036 if ( (rule->fw_flg & IP_FW_F_COMMAND) == IP_FW_F_QUEUE )
1037 #endif /* !IPFW2 */
1038 for (fs=all_flow_sets; fs && fs->fs_nr != pipe_nr; fs=fs->next)
1039 ;
1040 else {
1041 struct dn_pipe *p1;
1042 for (p1 = all_pipes; p1 && p1->pipe_nr != pipe_nr; p1 = p1->next)
1043 ;
1044 if (p1 != NULL)
1045 fs = &(p1->fs) ;
1046 }
1047 /* record for the future */
1048 #if IPFW2
1049 ((ipfw_insn_pipe *)cmd)->pipe_ptr = fs;
1050 #else
1051 if (fs != NULL)
1052 rule->pipe_ptr = fs;
1053 #endif
1054 return fs ;
1055 }
1056
1057 /*
1058 * dummynet hook for packets. Below 'pipe' is a pipe or a queue
1059 * depending on whether WF2Q or fixed bw is used.
1060 *
1061 * pipe_nr pipe or queue the packet is destined for.
1062 * dir where shall we send the packet after dummynet.
1063 * m the mbuf with the packet
1064 * ifp the 'ifp' parameter from the caller.
1065 * NULL in ip_input, destination interface in ip_output,
1066 * real_dst in bdg_forward
1067 * ro route parameter (only used in ip_output, NULL otherwise)
1068 * dst destination address, only used by ip_output
1069 * rule matching rule, in case of multiple passes
1070 * flags flags from the caller, only used in ip_output
1071 *
1072 */
1073 static int
1074 dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
1075 {
1076 struct dn_pkt *pkt;
1077 struct dn_flow_set *fs;
1078 struct dn_pipe *pipe ;
1079 u_int64_t len = m->m_pkthdr.len ;
1080 struct dn_flow_queue *q = NULL ;
1081 int s = splimp();
1082 int is_pipe;
1083 #if IPFW2
1084 ipfw_insn *cmd = fwa->rule->cmd + fwa->rule->act_ofs;
1085
1086 if (cmd->opcode == O_LOG)
1087 cmd += F_LEN(cmd);
1088 is_pipe = (cmd->opcode == O_PIPE);
1089 #else
1090 is_pipe = (fwa->rule->fw_flg & IP_FW_F_COMMAND) == IP_FW_F_PIPE;
1091 #endif
1092
1093 pipe_nr &= 0xffff ;
1094
1095 /*
1096 * this is a dummynet rule, so we expect a O_PIPE or O_QUEUE rule
1097 */
1098 fs = locate_flowset(pipe_nr, fwa->rule);
1099 if (fs == NULL)
1100 goto dropit ; /* this queue/pipe does not exist! */
1101 pipe = fs->pipe ;
1102 if (pipe == NULL) { /* must be a queue, try find a matching pipe */
1103 for (pipe = all_pipes; pipe && pipe->pipe_nr != fs->parent_nr;
1104 pipe = pipe->next)
1105 ;
1106 if (pipe != NULL)
1107 fs->pipe = pipe ;
1108 else {
1109 printf("No pipe %d for queue %d, drop pkt\n",
1110 fs->parent_nr, fs->fs_nr);
1111 goto dropit ;
1112 }
1113 }
1114 q = find_queue(fs, &(fwa->f_id));
1115 if ( q == NULL )
1116 goto dropit ; /* cannot allocate queue */
1117 /*
1118 * update statistics, then check reasons to drop pkt
1119 */
1120 q->tot_bytes += len ;
1121 q->tot_pkts++ ;
1122 if ( fs->plr && random() < fs->plr )
1123 goto dropit ; /* random pkt drop */
1124 if ( fs->flags_fs & DN_QSIZE_IS_BYTES) {
1125 if (q->len_bytes > fs->qsize)
1126 goto dropit ; /* queue size overflow */
1127 } else {
1128 if (q->len >= fs->qsize)
1129 goto dropit ; /* queue count overflow */
1130 }
1131 if ( fs->flags_fs & DN_IS_RED && red_drops(fs, q, len) )
1132 goto dropit ;
1133
1134 /* XXX expensive to zero, see if we can remove it*/
1135 pkt = (struct dn_pkt *)malloc(sizeof (*pkt), M_DUMMYNET, M_NOWAIT|M_ZERO);
1136 if ( pkt == NULL )
1137 goto dropit ; /* cannot allocate packet header */
1138 /* ok, i can handle the pkt now... */
1139 /* build and enqueue packet + parameters */
1140 pkt->hdr.mh_type = MT_TAG;
1141 pkt->hdr.mh_flags = PACKET_TAG_DUMMYNET;
1142 pkt->rule = fwa->rule ;
1143 DN_NEXT(pkt) = NULL;
1144 pkt->dn_m = m;
1145 pkt->dn_dir = dir ;
1146
1147 pkt->ifp = fwa->oif;
1148 if (dir == DN_TO_IP_OUT) {
1149 /*
1150 * We need to copy *ro because for ICMP pkts (and maybe others)
1151 * the caller passed a pointer into the stack; dst might also be
1152 * a pointer into *ro so it needs to be updated.
1153 */
1154 pkt->ro = *(fwa->ro);
1155 if (fwa->ro->ro_rt)
1156 fwa->ro->ro_rt->rt_refcnt++ ;
1157 if (fwa->dst == (struct sockaddr_in *)&fwa->ro->ro_dst) /* dst points into ro */
1158 fwa->dst = (struct sockaddr_in *)&(pkt->ro.ro_dst) ;
1159
1160 pkt->dn_dst = fwa->dst;
1161 pkt->flags = fwa->flags;
1162 }
1163 if (q->head == NULL)
1164 q->head = pkt;
1165 else
1166 DN_NEXT(q->tail) = pkt;
1167 q->tail = pkt;
1168 q->len++;
1169 q->len_bytes += len ;
1170
1171 if ( q->head != pkt ) /* flow was not idle, we are done */
1172 goto done;
1173 /*
1174 * If we reach this point the flow was previously idle, so we need
1175 * to schedule it. This involves different actions for fixed-rate or
1176 * WF2Q queues.
1177 */
1178 if (is_pipe) {
1179 /*
1180 * Fixed-rate queue: just insert into the ready_heap.
1181 */
1182 dn_key t = 0 ;
1183 if (pipe->bandwidth)
1184 t = SET_TICKS(pkt, q, pipe);
1185 q->sched_time = curr_time ;
1186 if (t == 0) /* must process it now */
1187 ready_event( q );
1188 else
1189 heap_insert(&ready_heap, curr_time + t , q );
1190 } else {
1191 /*
1192 * WF2Q. First, compute start time S: if the flow was idle (S=F+1)
1193 * set S to the virtual time V for the controlling pipe, and update
1194 * the sum of weights for the pipe; otherwise, remove flow from
1195 * idle_heap and set S to max(F,V).
1196 * Second, compute finish time F = S + len/weight.
1197 * Third, if pipe was idle, update V=max(S, V).
1198 * Fourth, count one more backlogged flow.
1199 */
1200 if (DN_KEY_GT(q->S, q->F)) { /* means timestamps are invalid */
1201 q->S = pipe->V ;
1202 pipe->sum += fs->weight ; /* add weight of new queue */
1203 } else {
1204 heap_extract(&(pipe->idle_heap), q);
1205 q->S = MAX64(q->F, pipe->V ) ;
1206 }
1207 q->F = q->S + ( len<<MY_M )/(u_int64_t) fs->weight;
1208
1209 if (pipe->not_eligible_heap.elements == 0 &&
1210 pipe->scheduler_heap.elements == 0)
1211 pipe->V = MAX64 ( q->S, pipe->V );
1212 fs->backlogged++ ;
1213 /*
1214 * Look at eligibility. A flow is not eligibile if S>V (when
1215 * this happens, it means that there is some other flow already
1216 * scheduled for the same pipe, so the scheduler_heap cannot be
1217 * empty). If the flow is not eligible we just store it in the
1218 * not_eligible_heap. Otherwise, we store in the scheduler_heap
1219 * and possibly invoke ready_event_wfq() right now if there is
1220 * leftover credit.
1221 * Note that for all flows in scheduler_heap (SCH), S_i <= V,
1222 * and for all flows in not_eligible_heap (NEH), S_i > V .
1223 * So when we need to compute max( V, min(S_i) ) forall i in SCH+NEH,
1224 * we only need to look into NEH.
1225 */
1226 if (DN_KEY_GT(q->S, pipe->V) ) { /* not eligible */
1227 if (pipe->scheduler_heap.elements == 0)
1228 printf("++ ouch! not eligible but empty scheduler!\n");
1229 heap_insert(&(pipe->not_eligible_heap), q->S, q);
1230 } else {
1231 heap_insert(&(pipe->scheduler_heap), q->F, q);
1232 if (pipe->numbytes >= 0) { /* pipe is idle */
1233 if (pipe->scheduler_heap.elements != 1)
1234 printf("*** OUCH! pipe should have been idle!\n");
1235 DEB(printf("Waking up pipe %d at %d\n",
1236 pipe->pipe_nr, (int)(q->F >> MY_M)); )
1237 pipe->sched_time = curr_time ;
1238 ready_event_wfq(pipe);
1239 }
1240 }
1241 }
1242 done:
1243 splx(s);
1244 return 0;
1245
1246 dropit:
1247 splx(s);
1248 if (q)
1249 q->drops++ ;
1250 m_freem(m);
1251 return ( (fs && (fs->flags_fs & DN_NOERROR)) ? 0 : ENOBUFS);
1252 }
1253
1254 /*
1255 * Below, the rt_unref is only needed when (pkt->dn_dir == DN_TO_IP_OUT)
1256 * Doing this would probably save us the initial bzero of dn_pkt
1257 */
1258 #define DN_FREE_PKT(pkt) { \
1259 struct dn_pkt *n = pkt ; \
1260 rt_unref ( n->ro.ro_rt ) ; \
1261 m_freem(n->dn_m); \
1262 pkt = DN_NEXT(n) ; \
1263 free(n, M_DUMMYNET) ; }
1264
1265 /*
1266 * Dispose all packets and flow_queues on a flow_set.
1267 * If all=1, also remove red lookup table and other storage,
1268 * including the descriptor itself.
1269 * For the one in dn_pipe MUST also cleanup ready_heap...
1270 */
1271 static void
1272 purge_flow_set(struct dn_flow_set *fs, int all)
1273 {
1274 struct dn_pkt *pkt ;
1275 struct dn_flow_queue *q, *qn ;
1276 int i ;
1277
1278 for (i = 0 ; i <= fs->rq_size ; i++ ) {
1279 for (q = fs->rq[i] ; q ; q = qn ) {
1280 for (pkt = q->head ; pkt ; )
1281 DN_FREE_PKT(pkt) ;
1282 qn = q->next ;
1283 free(q, M_DUMMYNET);
1284 }
1285 fs->rq[i] = NULL ;
1286 }
1287 fs->rq_elements = 0 ;
1288 if (all) {
1289 /* RED - free lookup table */
1290 if (fs->w_q_lookup)
1291 free(fs->w_q_lookup, M_DUMMYNET);
1292 if (fs->rq)
1293 free(fs->rq, M_DUMMYNET);
1294 /* if this fs is not part of a pipe, free it */
1295 if (fs->pipe && fs != &(fs->pipe->fs) )
1296 free(fs, M_DUMMYNET);
1297 }
1298 }
1299
1300 /*
1301 * Dispose all packets queued on a pipe (not a flow_set).
1302 * Also free all resources associated to a pipe, which is about
1303 * to be deleted.
1304 */
1305 static void
1306 purge_pipe(struct dn_pipe *pipe)
1307 {
1308 struct dn_pkt *pkt ;
1309
1310 purge_flow_set( &(pipe->fs), 1 );
1311
1312 for (pkt = pipe->head ; pkt ; )
1313 DN_FREE_PKT(pkt) ;
1314
1315 heap_free( &(pipe->scheduler_heap) );
1316 heap_free( &(pipe->not_eligible_heap) );
1317 heap_free( &(pipe->idle_heap) );
1318 }
1319
1320 /*
1321 * Delete all pipes and heaps returning memory. Must also
1322 * remove references from all ipfw rules to all pipes.
1323 */
1324 static void
1325 dummynet_flush()
1326 {
1327 struct dn_pipe *curr_p, *p ;
1328 struct dn_flow_set *fs, *curr_fs;
1329 int s ;
1330
1331 s = splimp() ;
1332
1333 /* remove all references to pipes ...*/
1334 flush_pipe_ptrs(NULL);
1335 /* prevent future matches... */
1336 p = all_pipes ;
1337 all_pipes = NULL ;
1338 fs = all_flow_sets ;
1339 all_flow_sets = NULL ;
1340 /* and free heaps so we don't have unwanted events */
1341 heap_free(&ready_heap);
1342 heap_free(&wfq_ready_heap);
1343 heap_free(&extract_heap);
1344 splx(s) ;
1345 /*
1346 * Now purge all queued pkts and delete all pipes
1347 */
1348 /* scan and purge all flow_sets. */
1349 for ( ; fs ; ) {
1350 curr_fs = fs ;
1351 fs = fs->next ;
1352 purge_flow_set(curr_fs, 1);
1353 }
1354 for ( ; p ; ) {
1355 purge_pipe(p);
1356 curr_p = p ;
1357 p = p->next ;
1358 free(curr_p, M_DUMMYNET);
1359 }
1360 }
1361
1362
1363 extern struct ip_fw *ip_fw_default_rule ;
1364 static void
1365 dn_rule_delete_fs(struct dn_flow_set *fs, void *r)
1366 {
1367 int i ;
1368 struct dn_flow_queue *q ;
1369 struct dn_pkt *pkt ;
1370
1371 for (i = 0 ; i <= fs->rq_size ; i++) /* last one is ovflow */
1372 for (q = fs->rq[i] ; q ; q = q->next )
1373 for (pkt = q->head ; pkt ; pkt = DN_NEXT(pkt) )
1374 if (pkt->rule == r)
1375 pkt->rule = ip_fw_default_rule ;
1376 }
1377 /*
1378 * when a firewall rule is deleted, scan all queues and remove the flow-id
1379 * from packets matching this rule.
1380 */
1381 void
1382 dn_rule_delete(void *r)
1383 {
1384 struct dn_pipe *p ;
1385 struct dn_pkt *pkt ;
1386 struct dn_flow_set *fs ;
1387
1388 /*
1389 * If the rule references a queue (dn_flow_set), then scan
1390 * the flow set, otherwise scan pipes. Should do either, but doing
1391 * both does not harm.
1392 */
1393 for ( fs = all_flow_sets ; fs ; fs = fs->next )
1394 dn_rule_delete_fs(fs, r);
1395 for ( p = all_pipes ; p ; p = p->next ) {
1396 fs = &(p->fs) ;
1397 dn_rule_delete_fs(fs, r);
1398 for (pkt = p->head ; pkt ; pkt = DN_NEXT(pkt) )
1399 if (pkt->rule == r)
1400 pkt->rule = ip_fw_default_rule ;
1401 }
1402 }
1403
1404 /*
1405 * setup RED parameters
1406 */
1407 static int
1408 config_red(struct dn_flow_set *p, struct dn_flow_set * x)
1409 {
1410 int i;
1411
1412 x->w_q = p->w_q;
1413 x->min_th = SCALE(p->min_th);
1414 x->max_th = SCALE(p->max_th);
1415 x->max_p = p->max_p;
1416
1417 x->c_1 = p->max_p / (p->max_th - p->min_th);
1418 x->c_2 = SCALE_MUL(x->c_1, SCALE(p->min_th));
1419 if (x->flags_fs & DN_IS_GENTLE_RED) {
1420 x->c_3 = (SCALE(1) - p->max_p) / p->max_th;
1421 x->c_4 = (SCALE(1) - 2 * p->max_p);
1422 }
1423
1424 /* if the lookup table already exist, free and create it again */
1425 if (x->w_q_lookup) {
1426 free(x->w_q_lookup, M_DUMMYNET);
1427 x->w_q_lookup = NULL ;
1428 }
1429 if (red_lookup_depth == 0) {
1430 printf("\nnet.inet.ip.dummynet.red_lookup_depth must be > 0");
1431 free(x, M_DUMMYNET);
1432 return EINVAL;
1433 }
1434 x->lookup_depth = red_lookup_depth;
1435 x->w_q_lookup = (u_int *) malloc(x->lookup_depth * sizeof(int),
1436 M_DUMMYNET, M_DONTWAIT);
1437 if (x->w_q_lookup == NULL) {
1438 printf("sorry, cannot allocate red lookup table\n");
1439 free(x, M_DUMMYNET);
1440 return ENOSPC;
1441 }
1442
1443 /* fill the lookup table with (1 - w_q)^x */
1444 x->lookup_step = p->lookup_step ;
1445 x->lookup_weight = p->lookup_weight ;
1446 x->w_q_lookup[0] = SCALE(1) - x->w_q;
1447 for (i = 1; i < x->lookup_depth; i++)
1448 x->w_q_lookup[i] = SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight);
1449 if (red_avg_pkt_size < 1)
1450 red_avg_pkt_size = 512 ;
1451 x->avg_pkt_size = red_avg_pkt_size ;
1452 if (red_max_pkt_size < 1)
1453 red_max_pkt_size = 1500 ;
1454 x->max_pkt_size = red_max_pkt_size ;
1455 return 0 ;
1456 }
1457
1458 static int
1459 alloc_hash(struct dn_flow_set *x, struct dn_flow_set *pfs)
1460 {
1461 if (x->flags_fs & DN_HAVE_FLOW_MASK) { /* allocate some slots */
1462 int l = pfs->rq_size;
1463
1464 if (l == 0)
1465 l = dn_hash_size;
1466 if (l < 4)
1467 l = 4;
1468 else if (l > DN_MAX_HASH_SIZE)
1469 l = DN_MAX_HASH_SIZE;
1470 x->rq_size = l;
1471 } else /* one is enough for null mask */
1472 x->rq_size = 1;
1473 x->rq = malloc((1 + x->rq_size) * sizeof(struct dn_flow_queue *),
1474 M_DUMMYNET, M_DONTWAIT | M_ZERO);
1475 if (x->rq == NULL) {
1476 printf("sorry, cannot allocate queue\n");
1477 return ENOSPC;
1478 }
1479 x->rq_elements = 0;
1480 return 0 ;
1481 }
1482
1483 static void
1484 set_fs_parms(struct dn_flow_set *x, struct dn_flow_set *src)
1485 {
1486 x->flags_fs = src->flags_fs;
1487 x->qsize = src->qsize;
1488 x->plr = src->plr;
1489 x->flow_mask = src->flow_mask;
1490 if (x->flags_fs & DN_QSIZE_IS_BYTES) {
1491 if (x->qsize > 1024*1024)
1492 x->qsize = 1024*1024 ;
1493 } else {
1494 if (x->qsize == 0)
1495 x->qsize = 50 ;
1496 if (x->qsize > 100)
1497 x->qsize = 50 ;
1498 }
1499 /* configuring RED */
1500 if ( x->flags_fs & DN_IS_RED )
1501 config_red(src, x) ; /* XXX should check errors */
1502 }
1503
1504 /*
1505 * setup pipe or queue parameters.
1506 */
1507
1508 static int
1509 config_pipe(struct dn_pipe *p)
1510 {
1511 int s ;
1512 struct dn_flow_set *pfs = &(p->fs);
1513
1514 /*
1515 * The config program passes parameters as follows:
1516 * bw = bits/second (0 means no limits),
1517 * delay = ms, must be translated into ticks.
1518 * qsize = slots/bytes
1519 */
1520 p->delay = ( p->delay * hz ) / 1000 ;
1521 /* We need either a pipe number or a flow_set number */
1522 if (p->pipe_nr == 0 && pfs->fs_nr == 0)
1523 return EINVAL ;
1524 if (p->pipe_nr != 0 && pfs->fs_nr != 0)
1525 return EINVAL ;
1526 if (p->pipe_nr != 0) { /* this is a pipe */
1527 struct dn_pipe *x, *a, *b;
1528 /* locate pipe */
1529 for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
1530 a = b , b = b->next) ;
1531
1532 if (b == NULL || b->pipe_nr != p->pipe_nr) { /* new pipe */
1533 x = malloc(sizeof(struct dn_pipe), M_DUMMYNET, M_DONTWAIT | M_ZERO);
1534 if (x == NULL) {
1535 printf("ip_dummynet.c: no memory for new pipe\n");
1536 return ENOSPC;
1537 }
1538 x->pipe_nr = p->pipe_nr;
1539 x->fs.pipe = x ;
1540 /* idle_heap is the only one from which we extract from the middle.
1541 */
1542 x->idle_heap.size = x->idle_heap.elements = 0 ;
1543 x->idle_heap.offset=OFFSET_OF(struct dn_flow_queue, heap_pos);
1544 } else
1545 x = b;
1546
1547 x->bandwidth = p->bandwidth ;
1548 x->numbytes = 0; /* just in case... */
1549 bcopy(p->if_name, x->if_name, sizeof(p->if_name) );
1550 x->ifp = NULL ; /* reset interface ptr */
1551 x->delay = p->delay ;
1552 set_fs_parms(&(x->fs), pfs);
1553
1554
1555 if ( x->fs.rq == NULL ) { /* a new pipe */
1556 s = alloc_hash(&(x->fs), pfs) ;
1557 if (s) {
1558 free(x, M_DUMMYNET);
1559 return s ;
1560 }
1561 s = splimp() ;
1562 x->next = b ;
1563 if (a == NULL)
1564 all_pipes = x ;
1565 else
1566 a->next = x ;
1567 splx(s);
1568 }
1569 } else { /* config queue */
1570 struct dn_flow_set *x, *a, *b ;
1571
1572 /* locate flow_set */
1573 for (a=NULL, b=all_flow_sets ; b && b->fs_nr < pfs->fs_nr ;
1574 a = b , b = b->next) ;
1575
1576 if (b == NULL || b->fs_nr != pfs->fs_nr) { /* new */
1577 if (pfs->parent_nr == 0) /* need link to a pipe */
1578 return EINVAL ;
1579 x = malloc(sizeof(struct dn_flow_set),M_DUMMYNET,M_DONTWAIT|M_ZERO);
1580 if (x == NULL) {
1581 printf("ip_dummynet.c: no memory for new flow_set\n");
1582 return ENOSPC;
1583 }
1584 x->fs_nr = pfs->fs_nr;
1585 x->parent_nr = pfs->parent_nr;
1586 x->weight = pfs->weight ;
1587 if (x->weight == 0)
1588 x->weight = 1 ;
1589 else if (x->weight > 100)
1590 x->weight = 100 ;
1591 } else {
1592 /* Change parent pipe not allowed; must delete and recreate */
1593 if (pfs->parent_nr != 0 && b->parent_nr != pfs->parent_nr)
1594 return EINVAL ;
1595 x = b;
1596 }
1597 set_fs_parms(x, pfs);
1598
1599 if ( x->rq == NULL ) { /* a new flow_set */
1600 s = alloc_hash(x, pfs) ;
1601 if (s) {
1602 free(x, M_DUMMYNET);
1603 return s ;
1604 }
1605 s = splimp() ;
1606 x->next = b;
1607 if (a == NULL)
1608 all_flow_sets = x;
1609 else
1610 a->next = x;
1611 splx(s);
1612 }
1613 }
1614 return 0 ;
1615 }
1616
1617 /*
1618 * Helper function to remove from a heap queues which are linked to
1619 * a flow_set about to be deleted.
1620 */
1621 static void
1622 fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs)
1623 {
1624 int i = 0, found = 0 ;
1625 for (; i < h->elements ;)
1626 if ( ((struct dn_flow_queue *)h->p[i].object)->fs == fs) {
1627 h->elements-- ;
1628 h->p[i] = h->p[h->elements] ;
1629 found++ ;
1630 } else
1631 i++ ;
1632 if (found)
1633 heapify(h);
1634 }
1635
1636 /*
1637 * helper function to remove a pipe from a heap (can be there at most once)
1638 */
1639 static void
1640 pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p)
1641 {
1642 if (h->elements > 0) {
1643 int i = 0 ;
1644 for (i=0; i < h->elements ; i++ ) {
1645 if (h->p[i].object == p) { /* found it */
1646 h->elements-- ;
1647 h->p[i] = h->p[h->elements] ;
1648 heapify(h);
1649 break ;
1650 }
1651 }
1652 }
1653 }
1654
1655 /*
1656 * drain all queues. Called in case of severe mbuf shortage.
1657 */
1658 void
1659 dummynet_drain()
1660 {
1661 struct dn_flow_set *fs;
1662 struct dn_pipe *p;
1663 struct dn_pkt *pkt;
1664
1665 heap_free(&ready_heap);
1666 heap_free(&wfq_ready_heap);
1667 heap_free(&extract_heap);
1668 /* remove all references to this pipe from flow_sets */
1669 for (fs = all_flow_sets; fs; fs= fs->next )
1670 purge_flow_set(fs, 0);
1671
1672 for (p = all_pipes; p; p= p->next ) {
1673 purge_flow_set(&(p->fs), 0);
1674 for (pkt = p->head ; pkt ; )
1675 DN_FREE_PKT(pkt) ;
1676 p->head = p->tail = NULL ;
1677 }
1678 }
1679
1680 /*
1681 * Fully delete a pipe or a queue, cleaning up associated info.
1682 */
1683 static int
1684 delete_pipe(struct dn_pipe *p)
1685 {
1686 int s ;
1687
1688 if (p->pipe_nr == 0 && p->fs.fs_nr == 0)
1689 return EINVAL ;
1690 if (p->pipe_nr != 0 && p->fs.fs_nr != 0)
1691 return EINVAL ;
1692 if (p->pipe_nr != 0) { /* this is an old-style pipe */
1693 struct dn_pipe *a, *b;
1694 struct dn_flow_set *fs;
1695
1696 /* locate pipe */
1697 for (a = NULL , b = all_pipes ; b && b->pipe_nr < p->pipe_nr ;
1698 a = b , b = b->next) ;
1699 if (b == NULL || (b->pipe_nr != p->pipe_nr) )
1700 return EINVAL ; /* not found */
1701
1702 s = splimp() ;
1703
1704 /* unlink from list of pipes */
1705 if (a == NULL)
1706 all_pipes = b->next ;
1707 else
1708 a->next = b->next ;
1709 /* remove references to this pipe from the ip_fw rules. */
1710 flush_pipe_ptrs(&(b->fs));
1711
1712 /* remove all references to this pipe from flow_sets */
1713 for (fs = all_flow_sets; fs; fs= fs->next )
1714 if (fs->pipe == b) {
1715 printf("++ ref to pipe %d from fs %d\n",
1716 p->pipe_nr, fs->fs_nr);
1717 fs->pipe = NULL ;
1718 purge_flow_set(fs, 0);
1719 }
1720 fs_remove_from_heap(&ready_heap, &(b->fs));
1721 purge_pipe(b); /* remove all data associated to this pipe */
1722 /* remove reference to here from extract_heap and wfq_ready_heap */
1723 pipe_remove_from_heap(&extract_heap, b);
1724 pipe_remove_from_heap(&wfq_ready_heap, b);
1725 splx(s);
1726 free(b, M_DUMMYNET);
1727 } else { /* this is a WF2Q queue (dn_flow_set) */
1728 struct dn_flow_set *a, *b;
1729
1730 /* locate set */
1731 for (a = NULL, b = all_flow_sets ; b && b->fs_nr < p->fs.fs_nr ;
1732 a = b , b = b->next) ;
1733 if (b == NULL || (b->fs_nr != p->fs.fs_nr) )
1734 return EINVAL ; /* not found */
1735
1736 s = splimp() ;
1737 if (a == NULL)
1738 all_flow_sets = b->next ;
1739 else
1740 a->next = b->next ;
1741 /* remove references to this flow_set from the ip_fw rules. */
1742 flush_pipe_ptrs(b);
1743
1744 if (b->pipe != NULL) {
1745 /* Update total weight on parent pipe and cleanup parent heaps */
1746 b->pipe->sum -= b->weight * b->backlogged ;
1747 fs_remove_from_heap(&(b->pipe->not_eligible_heap), b);
1748 fs_remove_from_heap(&(b->pipe->scheduler_heap), b);
1749 #if 1 /* XXX should i remove from idle_heap as well ? */
1750 fs_remove_from_heap(&(b->pipe->idle_heap), b);
1751 #endif
1752 }
1753 purge_flow_set(b, 1);
1754 splx(s);
1755 }
1756 return 0 ;
1757 }
1758
1759 /*
1760 * helper function used to copy data from kernel in DUMMYNET_GET
1761 */
1762 static char *
1763 dn_copy_set(struct dn_flow_set *set, char *bp)
1764 {
1765 int i, copied = 0 ;
1766 struct dn_flow_queue *q, *qp = (struct dn_flow_queue *)bp;
1767
1768 for (i = 0 ; i <= set->rq_size ; i++)
1769 for (q = set->rq[i] ; q ; q = q->next, qp++ ) {
1770 if (q->hash_slot != i)
1771 printf("++ at %d: wrong slot (have %d, "
1772 "should be %d)\n", copied, q->hash_slot, i);
1773 if (q->fs != set)
1774 printf("++ at %d: wrong fs ptr (have %p, should be %p)\n",
1775 i, q->fs, set);
1776 copied++ ;
1777 bcopy(q, qp, sizeof( *q ) );
1778 /* cleanup pointers */
1779 qp->next = NULL ;
1780 qp->head = qp->tail = NULL ;
1781 qp->fs = NULL ;
1782 }
1783 if (copied != set->rq_elements)
1784 printf("++ wrong count, have %d should be %d\n",
1785 copied, set->rq_elements);
1786 return (char *)qp ;
1787 }
1788
1789 static int
1790 dummynet_get(struct sockopt *sopt)
1791 {
1792 char *buf, *bp ; /* bp is the "copy-pointer" */
1793 size_t size ;
1794 struct dn_flow_set *set ;
1795 struct dn_pipe *p ;
1796 int s, error=0 ;
1797
1798 s = splimp();
1799 /*
1800 * compute size of data structures: list of pipes and flow_sets.
1801 */
1802 for (p = all_pipes, size = 0 ; p ; p = p->next )
1803 size += sizeof( *p ) +
1804 p->fs.rq_elements * sizeof(struct dn_flow_queue);
1805 for (set = all_flow_sets ; set ; set = set->next )
1806 size += sizeof ( *set ) +
1807 set->rq_elements * sizeof(struct dn_flow_queue);
1808 buf = malloc(size, M_TEMP, M_DONTWAIT);
1809 if (buf == 0) {
1810 splx(s);
1811 return ENOBUFS ;
1812 }
1813 for (p = all_pipes, bp = buf ; p ; p = p->next ) {
1814 struct dn_pipe *pipe_bp = (struct dn_pipe *)bp ;
1815
1816 /*
1817 * copy pipe descriptor into *bp, convert delay back to ms,
1818 * then copy the flow_set descriptor(s) one at a time.
1819 * After each flow_set, copy the queue descriptor it owns.
1820 */
1821 bcopy(p, bp, sizeof( *p ) );
1822 pipe_bp->delay = (pipe_bp->delay * 1000) / hz ;
1823 /*
1824 * XXX the following is a hack based on ->next being the
1825 * first field in dn_pipe and dn_flow_set. The correct
1826 * solution would be to move the dn_flow_set to the beginning
1827 * of struct dn_pipe.
1828 */
1829 pipe_bp->next = (struct dn_pipe *)DN_IS_PIPE ;
1830 /* clean pointers */
1831 pipe_bp->head = pipe_bp->tail = NULL ;
1832 pipe_bp->fs.next = NULL ;
1833 pipe_bp->fs.pipe = NULL ;
1834 pipe_bp->fs.rq = NULL ;
1835
1836 bp += sizeof( *p ) ;
1837 bp = dn_copy_set( &(p->fs), bp );
1838 }
1839 for (set = all_flow_sets ; set ; set = set->next ) {
1840 struct dn_flow_set *fs_bp = (struct dn_flow_set *)bp ;
1841 bcopy(set, bp, sizeof( *set ) );
1842 /* XXX same hack as above */
1843 fs_bp->next = (struct dn_flow_set *)DN_IS_QUEUE ;
1844 fs_bp->pipe = NULL ;
1845 fs_bp->rq = NULL ;
1846 bp += sizeof( *set ) ;
1847 bp = dn_copy_set( set, bp );
1848 }
1849 splx(s);
1850 error = sooptcopyout(sopt, buf, size);
1851 free(buf, M_TEMP);
1852 return error ;
1853 }
1854
1855 /*
1856 * Handler for the various dummynet socket options (get, flush, config, del)
1857 */
1858 static int
1859 ip_dn_ctl(struct sockopt *sopt)
1860 {
1861 int error = 0 ;
1862 struct dn_pipe *p, tmp_pipe;
1863
1864 /* Disallow sets in really-really secure mode. */
1865 if (sopt->sopt_dir == SOPT_SET) {
1866 #if __FreeBSD_version >= 500034
1867 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
1868 if (error)
1869 return (error);
1870 #else
1871 if (securelevel >= 3)
1872 return (EPERM);
1873 #endif
1874 }
1875
1876 switch (sopt->sopt_name) {
1877 default :
1878 printf("ip_dn_ctl -- unknown option %d", sopt->sopt_name);
1879 return EINVAL ;
1880
1881 case IP_DUMMYNET_GET :
1882 error = dummynet_get(sopt);
1883 break ;
1884
1885 case IP_DUMMYNET_FLUSH :
1886 dummynet_flush() ;
1887 break ;
1888
1889 case IP_DUMMYNET_CONFIGURE :
1890 p = &tmp_pipe ;
1891 error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
1892 if (error)
1893 break ;
1894 error = config_pipe(p);
1895 break ;
1896
1897 case IP_DUMMYNET_DEL : /* remove a pipe or queue */
1898 p = &tmp_pipe ;
1899 error = sooptcopyin(sopt, p, sizeof *p, sizeof *p);
1900 if (error)
1901 break ;
1902
1903 error = delete_pipe(p);
1904 break ;
1905 }
1906 return error ;
1907 }
1908
1909 static void
1910 ip_dn_init(void)
1911 {
1912 printf("DUMMYNET initialized (011031)\n");
1913 all_pipes = NULL ;
1914 all_flow_sets = NULL ;
1915 ready_heap.size = ready_heap.elements = 0 ;
1916 ready_heap.offset = 0 ;
1917
1918 wfq_ready_heap.size = wfq_ready_heap.elements = 0 ;
1919 wfq_ready_heap.offset = 0 ;
1920
1921 extract_heap.size = extract_heap.elements = 0 ;
1922 extract_heap.offset = 0 ;
1923 ip_dn_ctl_ptr = ip_dn_ctl;
1924 ip_dn_io_ptr = dummynet_io;
1925 ip_dn_ruledel_ptr = dn_rule_delete;
1926 bzero(&dn_timeout, sizeof(struct callout_handle));
1927 dn_timeout = timeout(dummynet, NULL, 1);
1928 }
1929
1930 static int
1931 dummynet_modevent(module_t mod, int type, void *data)
1932 {
1933 int s;
1934 switch (type) {
1935 case MOD_LOAD:
1936 s = splimp();
1937 if (DUMMYNET_LOADED) {
1938 splx(s);
1939 printf("DUMMYNET already loaded\n");
1940 return EEXIST ;
1941 }
1942 ip_dn_init();
1943 splx(s);
1944 break;
1945
1946 case MOD_UNLOAD:
1947 #if !defined(KLD_MODULE)
1948 printf("dummynet statically compiled, cannot unload\n");
1949 return EINVAL ;
1950 #else
1951 s = splimp();
1952 untimeout(dummynet, NULL, dn_timeout);
1953 dummynet_flush();
1954 ip_dn_ctl_ptr = NULL;
1955 ip_dn_io_ptr = NULL;
1956 ip_dn_ruledel_ptr = NULL;
1957 splx(s);
1958 #endif
1959 break ;
1960 default:
1961 break ;
1962 }
1963 return 0 ;
1964 }
1965
1966 static moduledata_t dummynet_mod = {
1967 "dummynet",
1968 dummynet_modevent,
1969 NULL
1970 };
1971 DECLARE_MODULE(dummynet, dummynet_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1972 MODULE_DEPEND(dummynet, ipfw, 1, 1, 1);
1973 MODULE_VERSION(dummynet, 1);
Cache object: eef24ecbb0d72b1140932b815a0cc8fd
|