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