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