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