2 * Codel/FQ_Codel and PIE/FQ-PIE Code:
3 * Copyright (C) 2016 Centre for Advanced Internet Architectures,
4 * Swinburne University of Technology, Melbourne, Australia.
5 * Portions of this code were made possible in part by a gift from
6 * The Comcast Innovation Fund.
7 * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
9 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
10 * Portions Copyright (c) 2000 Akamba Corp.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Configuration and internal object management for dummynet.
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
54 #include <sys/rwlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <sys/taskqueue.h>
59 #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
60 #include <netinet/in.h>
61 #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */
62 #include <netinet/ip_fw.h>
63 #include <netinet/ip_dummynet.h>
65 #include <netpfil/ipfw/ip_fw_private.h>
66 #include <netpfil/ipfw/dn_heap.h>
67 #include <netpfil/ipfw/ip_dn_private.h>
69 #include <netpfil/ipfw/dn_aqm.h>
71 #include <netpfil/ipfw/dn_sched.h>
73 /* which objects to copy */
74 #define DN_C_LINK 0x01
76 #define DN_C_FLOW 0x04
78 #define DN_C_QUEUE 0x10
80 /* we use this argument in case of a schk_new */
86 /*---- callout hooks. ----*/
87 static struct callout dn_timeout;
89 static struct task dn_task;
90 static struct taskqueue *dn_tq = NULL;
96 (void)arg; /* UNUSED */
97 taskqueue_enqueue(dn_tq, &dn_task);
106 callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL,
107 C_HARDCLOCK | C_DIRECT_EXEC);
109 /*----- end of callout hooks -----*/
112 /* Return AQM descriptor for given type or name. */
113 static struct dn_aqm *
114 find_aqm_type(int type, char *name)
118 SLIST_FOREACH(d, &dn_cfg.aqmlist, next) {
119 if (d->type == type || (name && !strcasecmp(d->name, name)))
122 return NULL; /* not found */
126 /* Return a scheduler descriptor given the type or name. */
127 static struct dn_alg *
128 find_sched_type(int type, char *name)
132 SLIST_FOREACH(d, &dn_cfg.schedlist, next) {
133 if (d->type == type || (name && !strcasecmp(d->name, name)))
136 return NULL; /* not found */
140 ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
143 const char *op = NULL;
151 } else if (oldv > hi) {
157 printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
161 /*---- flow_id mask, hash and compare functions ---*/
163 * The flow_id includes the 5-tuple, the queue/pipe number
164 * which we store in the extra area in host order,
165 * and for ipv6 also the flow_id6.
166 * XXX see if we want the tos byte (can store in 'flags')
168 static struct ipfw_flow_id *
169 flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
171 int is_v6 = IS_IP6_FLOW_ID(id);
173 id->dst_port &= mask->dst_port;
174 id->src_port &= mask->src_port;
175 id->proto &= mask->proto;
176 id->extra &= mask->extra;
178 APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
179 APPLY_MASK(&id->src_ip6, &mask->src_ip6);
180 id->flow_id6 &= mask->flow_id6;
182 id->dst_ip &= mask->dst_ip;
183 id->src_ip &= mask->src_ip;
188 /* computes an OR of two masks, result in dst and also returned */
189 static struct ipfw_flow_id *
190 flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
192 int is_v6 = IS_IP6_FLOW_ID(dst);
194 dst->dst_port |= src->dst_port;
195 dst->src_port |= src->src_port;
196 dst->proto |= src->proto;
197 dst->extra |= src->extra;
199 #define OR_MASK(_d, _s) \
200 (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
201 (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
202 (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
203 (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
204 OR_MASK(&dst->dst_ip6, &src->dst_ip6);
205 OR_MASK(&dst->src_ip6, &src->src_ip6);
207 dst->flow_id6 |= src->flow_id6;
209 dst->dst_ip |= src->dst_ip;
210 dst->src_ip |= src->src_ip;
216 nonzero_mask(struct ipfw_flow_id *m)
218 if (m->dst_port || m->src_port || m->proto || m->extra)
220 if (IS_IP6_FLOW_ID(m)) {
222 m->dst_ip6.__u6_addr.__u6_addr32[0] ||
223 m->dst_ip6.__u6_addr.__u6_addr32[1] ||
224 m->dst_ip6.__u6_addr.__u6_addr32[2] ||
225 m->dst_ip6.__u6_addr.__u6_addr32[3] ||
226 m->src_ip6.__u6_addr.__u6_addr32[0] ||
227 m->src_ip6.__u6_addr.__u6_addr32[1] ||
228 m->src_ip6.__u6_addr.__u6_addr32[2] ||
229 m->src_ip6.__u6_addr.__u6_addr32[3] ||
232 return m->dst_ip || m->src_ip;
236 /* XXX we may want a better hash function */
238 flow_id_hash(struct ipfw_flow_id *id)
242 if (IS_IP6_FLOW_ID(id)) {
243 uint32_t *d = (uint32_t *)&id->dst_ip6;
244 uint32_t *s = (uint32_t *)&id->src_ip6;
245 i = (d[0] ) ^ (d[1]) ^
247 (d[0] >> 15) ^ (d[1] >> 15) ^
248 (d[2] >> 15) ^ (d[3] >> 15) ^
249 (s[0] << 1) ^ (s[1] << 1) ^
250 (s[2] << 1) ^ (s[3] << 1) ^
251 (s[0] << 16) ^ (s[1] << 16) ^
252 (s[2] << 16) ^ (s[3] << 16) ^
253 (id->dst_port << 1) ^ (id->src_port) ^
255 (id->proto ) ^ (id->flow_id6);
257 i = (id->dst_ip) ^ (id->dst_ip >> 15) ^
258 (id->src_ip << 1) ^ (id->src_ip >> 16) ^
260 (id->dst_port << 1) ^ (id->src_port) ^ (id->proto);
265 /* Like bcmp, returns 0 if ids match, 1 otherwise. */
267 flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
269 int is_v6 = IS_IP6_FLOW_ID(id1);
272 if (IS_IP6_FLOW_ID(id2))
273 return 1; /* different address families */
275 return (id1->dst_ip == id2->dst_ip &&
276 id1->src_ip == id2->src_ip &&
277 id1->dst_port == id2->dst_port &&
278 id1->src_port == id2->src_port &&
279 id1->proto == id2->proto &&
280 id1->extra == id2->extra) ? 0 : 1;
284 !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
285 !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
286 id1->dst_port == id2->dst_port &&
287 id1->src_port == id2->src_port &&
288 id1->proto == id2->proto &&
289 id1->extra == id2->extra &&
290 id1->flow_id6 == id2->flow_id6) ? 0 : 1;
292 /*--------- end of flow-id mask, hash and compare ---------*/
294 /*--- support functions for the qht hashtable ----
295 * Entries are hashed by flow-id
298 q_hash(uintptr_t key, int flags, void *arg)
300 /* compute the hash slot from the flow id */
301 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
302 &((struct dn_queue *)key)->ni.fid :
303 (struct ipfw_flow_id *)key;
305 return flow_id_hash(id);
309 q_match(void *obj, uintptr_t key, int flags, void *arg)
311 struct dn_queue *o = (struct dn_queue *)obj;
312 struct ipfw_flow_id *id2;
314 if (flags & DNHT_KEY_IS_OBJ) {
315 /* compare pointers */
316 id2 = &((struct dn_queue *)key)->ni.fid;
318 id2 = (struct ipfw_flow_id *)key;
320 return (0 == flow_id_cmp(&o->ni.fid, id2));
324 * create a new queue instance for the given 'key'.
327 q_new(uintptr_t key, int flags, void *arg)
329 struct dn_queue *q, *template = arg;
330 struct dn_fsk *fs = template->fs;
331 int size = sizeof(*q) + fs->sched->fp->q_datalen;
333 q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
335 D("no memory for new queue");
339 set_oid(&q->ni.oid, DN_QUEUE, size);
340 if (fs->fs.flags & DN_QHT_HASH)
341 q->ni.fid = *(struct ipfw_flow_id *)key;
343 q->_si = template->_si;
346 if (fs->sched->fp->new_queue)
347 fs->sched->fp->new_queue(q);
350 /* call AQM init function after creating a queue*/
351 if (fs->aqmfp && fs->aqmfp->init)
352 if(fs->aqmfp->init(q))
353 D("unable to init AQM for fs %d", fs->fs.fs_nr);
355 dn_cfg.queue_count++;
361 * Notify schedulers that a queue is going away.
362 * If (flags & DN_DESTROY), also free the packets.
363 * The version for callbacks is called q_delete_cb().
366 dn_delete_queue(struct dn_queue *q, int flags)
368 struct dn_fsk *fs = q->fs;
371 /* clean up AQM status for queue 'q'
372 * cleanup here is called just with MULTIQUEUE
374 if (fs && fs->aqmfp && fs->aqmfp->cleanup)
375 fs->aqmfp->cleanup(q);
377 // D("fs %p si %p\n", fs, q->_si);
378 /* notify the parent scheduler that the queue is going away */
379 if (fs && fs->sched->fp->free_queue)
380 fs->sched->fp->free_queue(q);
383 if (flags & DN_DESTROY) {
385 dn_free_pkts(q->mq.head);
386 bzero(q, sizeof(*q)); // safety
388 dn_cfg.queue_count--;
393 q_delete_cb(void *q, void *arg)
395 int flags = (int)(uintptr_t)arg;
396 dn_delete_queue(q, flags);
397 return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
401 * calls dn_delete_queue/q_delete_cb on all queues,
402 * which notifies the parent scheduler and possibly drains packets.
403 * flags & DN_DESTROY: drains queues and destroy qht;
406 qht_delete(struct dn_fsk *fs, int flags)
408 ND("fs %d start flags %d qht %p",
409 fs->fs.fs_nr, flags, fs->qht);
412 if (fs->fs.flags & DN_QHT_HASH) {
413 dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
414 if (flags & DN_DESTROY) {
415 dn_ht_free(fs->qht, 0);
419 dn_delete_queue((struct dn_queue *)(fs->qht), flags);
420 if (flags & DN_DESTROY)
426 * Find and possibly create the queue for a MULTIQUEUE scheduler.
427 * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
430 ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
431 struct ipfw_flow_id *id)
433 struct dn_queue template;
438 if (fs->fs.flags & DN_QHT_HASH) {
439 struct ipfw_flow_id masked_id;
440 if (fs->qht == NULL) {
441 fs->qht = dn_ht_init(NULL, fs->fs.buckets,
442 offsetof(struct dn_queue, q_next),
443 q_hash, q_match, q_new);
448 flow_id_mask(&fs->fsk_mask, &masked_id);
449 return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
450 DNHT_INSERT, &template);
453 fs->qht = q_new(0, 0, &template);
454 return (struct dn_queue *)fs->qht;
457 /*--- end of queue hash table ---*/
459 /*--- support functions for the sch_inst hashtable ----
461 * These are hashed by flow-id
464 si_hash(uintptr_t key, int flags, void *arg)
466 /* compute the hash slot from the flow id */
467 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
468 &((struct dn_sch_inst *)key)->ni.fid :
469 (struct ipfw_flow_id *)key;
471 return flow_id_hash(id);
475 si_match(void *obj, uintptr_t key, int flags, void *arg)
477 struct dn_sch_inst *o = obj;
478 struct ipfw_flow_id *id2;
480 id2 = (flags & DNHT_KEY_IS_OBJ) ?
481 &((struct dn_sch_inst *)key)->ni.fid :
482 (struct ipfw_flow_id *)key;
483 return flow_id_cmp(&o->ni.fid, id2) == 0;
487 * create a new instance for the given 'key'
488 * Allocate memory for instance, delay line and scheduler private data.
491 si_new(uintptr_t key, int flags, void *arg)
493 struct dn_schk *s = arg;
494 struct dn_sch_inst *si;
495 int l = sizeof(*si) + s->fp->si_datalen;
497 si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
501 /* Set length only for the part passed up to userland. */
502 set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
503 set_oid(&(si->dline.oid), DN_DELAY_LINE,
504 sizeof(struct delay_line));
505 /* mark si and dline as outside the event queue */
506 si->ni.oid.id = si->dline.oid.id = -1;
511 if (s->fp->new_sched && s->fp->new_sched(si)) {
512 D("new_sched error");
515 if (s->sch.flags & DN_HAVE_MASK)
516 si->ni.fid = *(struct ipfw_flow_id *)key;
519 /* init AQM status for !DN_MULTIQUEUE sched*/
520 if (!(s->fp->flags & DN_MULTIQUEUE))
521 if (s->fs->aqmfp && s->fs->aqmfp->init)
522 if(s->fs->aqmfp->init((struct dn_queue *)(si + 1))) {
523 D("unable to init AQM for fs %d", s->fs->fs.fs_nr);
533 bzero(si, sizeof(*si)); // safety
534 free(si, M_DUMMYNET);
540 * Callback from siht to delete all scheduler instances. Remove
541 * si and delay line from the system heap, destroy all queues.
542 * We assume that all flowset have been notified and do not
543 * point to us anymore.
546 si_destroy(void *_si, void *arg)
548 struct dn_sch_inst *si = _si;
549 struct dn_schk *s = si->sched;
550 struct delay_line *dl = &si->dline;
552 if (dl->oid.subtype) /* remove delay line from event heap */
553 heap_extract(&dn_cfg.evheap, dl);
554 dn_free_pkts(dl->mq.head); /* drain delay line */
555 if (si->kflags & DN_ACTIVE) /* remove si from event heap */
556 heap_extract(&dn_cfg.evheap, si);
559 /* clean up AQM status for !DN_MULTIQUEUE sched
560 * Note that all queues belong to fs were cleaned up in fsk_detach.
561 * When drain_scheduler is called s->fs and q->fs are pointing
562 * to a correct fs, so we can use fs in this case.
564 if (!(s->fp->flags & DN_MULTIQUEUE)) {
565 struct dn_queue *q = (struct dn_queue *)(si + 1);
566 if (q->aqm_status && q->fs->aqmfp)
567 if (q->fs->aqmfp->cleanup)
568 q->fs->aqmfp->cleanup(q);
571 if (s->fp->free_sched)
572 s->fp->free_sched(si);
573 bzero(si, sizeof(*si)); /* safety */
574 free(si, M_DUMMYNET);
576 return DNHT_SCAN_DEL;
580 * Find the scheduler instance for this packet. If we need to apply
581 * a mask, do on a local copy of the flow_id to preserve the original.
582 * Assume siht is always initialized if we have a mask.
585 ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
588 if (s->sch.flags & DN_HAVE_MASK) {
589 struct ipfw_flow_id id_t = *id;
590 flow_id_mask(&s->sch.sched_mask, &id_t);
591 return dn_ht_find(s->siht, (uintptr_t)&id_t,
595 s->siht = si_new(0, 0, s);
596 return (struct dn_sch_inst *)s->siht;
599 /* callback to flush credit for the scheduler instance */
601 si_reset_credit(void *_si, void *arg)
603 struct dn_sch_inst *si = _si;
604 struct dn_link *p = &si->sched->link;
606 si->credit = p->burst + (dn_cfg.io_fast ? p->bandwidth : 0);
611 schk_reset_credit(struct dn_schk *s)
613 if (s->sch.flags & DN_HAVE_MASK)
614 dn_ht_scan(s->siht, si_reset_credit, NULL);
616 si_reset_credit(s->siht, NULL);
618 /*---- end of sch_inst hashtable ---------------------*/
620 /*-------------------------------------------------------
621 * flowset hash (fshash) support. Entries are hashed by fs_nr.
622 * New allocations are put in the fsunlinked list, from which
623 * they are removed when they point to a specific scheduler.
626 fsk_hash(uintptr_t key, int flags, void *arg)
628 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
629 ((struct dn_fsk *)key)->fs.fs_nr;
631 return ( (i>>8)^(i>>4)^i );
635 fsk_match(void *obj, uintptr_t key, int flags, void *arg)
637 struct dn_fsk *fs = obj;
638 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
639 ((struct dn_fsk *)key)->fs.fs_nr;
641 return (fs->fs.fs_nr == i);
645 fsk_new(uintptr_t key, int flags, void *arg)
649 fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
651 set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
653 fs->drain_bucket = 0;
654 SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
660 /* callback function for cleaning up AQM queue status belongs to a flowset
661 * connected to scheduler instance '_si' (for !DN_MULTIQUEUE only).
664 si_cleanup_q(void *_si, void *arg)
666 struct dn_sch_inst *si = _si;
668 if (!(si->sched->fp->flags & DN_MULTIQUEUE)) {
669 if (si->sched->fs->aqmfp && si->sched->fs->aqmfp->cleanup)
670 si->sched->fs->aqmfp->cleanup((struct dn_queue *) (si+1));
675 /* callback to clean up queue AQM status.*/
677 q_cleanup_q(void *_q, void *arg)
679 struct dn_queue *q = _q;
680 q->fs->aqmfp->cleanup(q);
684 /* Clean up all AQM queues status belongs to flowset 'fs' and then
685 * deconfig AQM for flowset 'fs'
688 aqm_cleanup_deconfig_fs(struct dn_fsk *fs)
690 struct dn_sch_inst *si;
692 /* clean up AQM status for all queues for !DN_MULTIQUEUE sched*/
693 if (fs->fs.fs_nr > DN_MAX_ID) {
694 if (fs->sched && !(fs->sched->fp->flags & DN_MULTIQUEUE)) {
695 if (fs->sched->sch.flags & DN_HAVE_MASK)
696 dn_ht_scan(fs->sched->siht, si_cleanup_q, NULL);
698 /* single si i.e. no sched mask */
699 si = (struct dn_sch_inst *) fs->sched->siht;
700 if (si && fs->aqmfp && fs->aqmfp->cleanup)
701 fs->aqmfp->cleanup((struct dn_queue *) (si+1));
706 /* clean up AQM status for all queues for DN_MULTIQUEUE sched*/
707 if (fs->sched && fs->sched->fp->flags & DN_MULTIQUEUE && fs->qht) {
708 if (fs->fs.flags & DN_QHT_HASH)
709 dn_ht_scan(fs->qht, q_cleanup_q, NULL);
711 fs->aqmfp->cleanup((struct dn_queue *)(fs->qht));
715 if(fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig)
716 fs->aqmfp->deconfig(fs);
721 * detach flowset from its current scheduler. Flags as follows:
722 * DN_DETACH removes from the fsk_list
723 * DN_DESTROY deletes individual queues
724 * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
727 fsk_detach(struct dn_fsk *fs, int flags)
729 if (flags & DN_DELETE_FS)
731 ND("fs %d from sched %d flags %s %s %s",
732 fs->fs.fs_nr, fs->fs.sched_nr,
733 (flags & DN_DELETE_FS) ? "DEL_FS":"",
734 (flags & DN_DESTROY) ? "DEL":"",
735 (flags & DN_DETACH) ? "DET":"");
736 if (flags & DN_DETACH) { /* detach from the list */
737 struct dn_fsk_head *h;
738 h = fs->sched ? &fs->sched->fsk_list : &dn_cfg.fsu;
739 SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
741 /* Free the RED parameters, they will be recomputed on
742 * subsequent attach if needed.
745 free(fs->w_q_lookup, M_DUMMYNET);
746 fs->w_q_lookup = NULL;
747 qht_delete(fs, flags);
749 aqm_cleanup_deconfig_fs(fs);
752 if (fs->sched && fs->sched->fp->free_fsk)
753 fs->sched->fp->free_fsk(fs);
755 if (flags & DN_DELETE_FS) {
756 bzero(fs, sizeof(*fs)); /* safety */
757 free(fs, M_DUMMYNET);
760 SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
765 * Detach or destroy all flowsets in a list.
766 * flags specifies what to do:
767 * DN_DESTROY: flush all queues
768 * DN_DELETE_FS: DN_DESTROY + destroy flowset
769 * DN_DELETE_FS implies DN_DESTROY
772 fsk_detach_list(struct dn_fsk_head *h, int flags)
775 int n = 0; /* only for stats */
777 ND("head %p flags %x", h, flags);
778 while ((fs = SLIST_FIRST(h))) {
779 SLIST_REMOVE_HEAD(h, sch_chain);
781 fsk_detach(fs, flags);
783 ND("done %d flowsets", n);
787 * called on 'queue X delete' -- removes the flowset from fshash,
788 * deletes all queues for the flowset, and removes the flowset.
791 delete_fs(int i, int locked)
798 fs = dn_ht_find(dn_cfg.fshash, i, DNHT_REMOVE, NULL);
799 ND("fs %d found %p", i, fs);
801 fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
810 /*----- end of flowset hashtable support -------------*/
812 /*------------------------------------------------------------
813 * Scheduler hash. When searching by index we pass sched_nr,
814 * otherwise we pass struct dn_sch * which is the first field in
815 * struct dn_schk so we can cast between the two. We use this trick
816 * because in the create phase (but it should be fixed).
819 schk_hash(uintptr_t key, int flags, void *_arg)
821 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
822 ((struct dn_schk *)key)->sch.sched_nr;
823 return ( (i>>8)^(i>>4)^i );
827 schk_match(void *obj, uintptr_t key, int flags, void *_arg)
829 struct dn_schk *s = (struct dn_schk *)obj;
830 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
831 ((struct dn_schk *)key)->sch.sched_nr;
832 return (s->sch.sched_nr == i);
836 * Create the entry and intialize with the sched hash if needed.
837 * Leave s->fp unset so we can tell whether a dn_ht_find() returns
838 * a new object or a previously existing one.
841 schk_new(uintptr_t key, int flags, void *arg)
843 struct schk_new_arg *a = arg;
845 int l = sizeof(*s) +a->fp->schk_datalen;
847 s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
850 set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
851 s->sch = *a->sch; // copy initial values
852 s->link.link_nr = s->sch.sched_nr;
853 SLIST_INIT(&s->fsk_list);
854 /* initialize the hash table or create the single instance */
855 s->fp = a->fp; /* si_new needs this */
857 if (s->sch.flags & DN_HAVE_MASK) {
858 s->siht = dn_ht_init(NULL, s->sch.buckets,
859 offsetof(struct dn_sch_inst, si_next),
860 si_hash, si_match, si_new);
861 if (s->siht == NULL) {
866 s->fp = NULL; /* mark as a new scheduler */
872 * Callback for sched delete. Notify all attached flowsets to
873 * detach from the scheduler, destroy the internal flowset, and
874 * all instances. The scheduler goes away too.
875 * arg is 0 (only detach flowsets and destroy instances)
876 * DN_DESTROY (detach & delete queues, delete schk)
877 * or DN_DELETE_FS (delete queues and flowsets, delete schk)
880 schk_delete_cb(void *obj, void *arg)
882 struct dn_schk *s = obj;
885 ND("sched %d arg %s%s",
887 a&DN_DESTROY ? "DEL ":"",
888 a&DN_DELETE_FS ? "DEL_FS":"");
890 fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
891 /* no more flowset pointing to us now */
892 if (s->sch.flags & DN_HAVE_MASK) {
893 dn_ht_scan(s->siht, si_destroy, NULL);
894 dn_ht_free(s->siht, 0);
896 si_destroy(s->siht, NULL);
898 free(s->profile, M_DUMMYNET);
904 bzero(s, sizeof(*s)); // safety
905 free(obj, M_DUMMYNET);
907 return DNHT_SCAN_DEL;
911 * called on a 'sched X delete' command. Deletes a single scheduler.
912 * This is done by removing from the schedhash, unlinking all
913 * flowsets and deleting their traffic.
920 s = dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
924 delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
925 /* then detach flowsets, delete traffic */
926 schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
929 /*--- end of schk hashtable support ---*/
932 copy_obj(char **start, char *end, void *_o, const char *msg, int i)
939 int have = end - *start;
941 memcpy(&o, _o, sizeof(o));
942 if (have < o.len || o.len == 0 || o.type == 0) {
943 D("(WARN) type %d %s %d have %d need %d",
944 o.type, msg, i, have, o.len);
947 ND("type %d %s %d len %d", o.type, msg, i, o.len);
948 if (o.type == DN_LINK) {
949 memcpy(&dn.l, _o, sizeof(dn.l));
950 /* Adjust burst parameter for link */
951 dn.l.burst = div64(dn.l.burst, 8 * hz);
952 dn.l.delay = dn.l.delay * 1000 / hz;
953 memcpy(*start, &dn.l, sizeof(dn.l));
954 } else if (o.type == DN_SCH) {
955 /* Set dn.s.sch.oid.id to the number of instances */
956 memcpy(&dn.s, _o, sizeof(dn.s));
957 dn.s.sch.oid.id = (dn.s.sch.flags & DN_HAVE_MASK) ?
958 dn_ht_entries(dn.s.siht) : (dn.s.siht ? 1 : 0);
959 memcpy(*start, &dn.s, sizeof(dn.s));
961 memcpy(*start, _o, o.len);
966 /* Specific function to copy a queue.
967 * Copies only the user-visible part of a queue (which is in
968 * a struct dn_flow), and sets len accordingly.
971 copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
973 struct dn_id *o = _o;
974 int have = end - *start;
975 int len = sizeof(struct dn_flow); /* see above comment */
977 if (have < len || o->len == 0 || o->type != DN_QUEUE) {
978 D("ERROR type %d %s %d have %d need %d",
979 o->type, msg, i, have, len);
982 ND("type %d %s %d len %d", o->type, msg, i, len);
983 memcpy(*start, _o, len);
984 ((struct dn_id*)(*start))->len = len;
990 copy_q_cb(void *obj, void *arg)
992 struct dn_queue *q = obj;
993 struct copy_args *a = arg;
994 struct dn_flow *ni = (struct dn_flow *)(*a->start);
995 if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
996 return DNHT_SCAN_END;
997 ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
998 ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
1003 copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
1007 if (fs->fs.flags & DN_QHT_HASH)
1008 dn_ht_scan(fs->qht, copy_q_cb, a);
1010 copy_q_cb(fs->qht, a);
1015 * This routine only copies the initial part of a profile ? XXX
1018 copy_profile(struct copy_args *a, struct dn_profile *p)
1020 int have = a->end - *a->start;
1021 /* XXX here we check for max length */
1022 int profile_len = sizeof(struct dn_profile) -
1023 ED_MAX_SAMPLES_NO*sizeof(int);
1027 if (have < profile_len) {
1028 D("error have %d need %d", have, profile_len);
1031 memcpy(*a->start, p, profile_len);
1032 ((struct dn_id *)(*a->start))->len = profile_len;
1033 *a->start += profile_len;
1038 copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
1040 struct dn_fs *ufs = (struct dn_fs *)(*a->start);
1043 ND("flowset %d", fs->fs.fs_nr);
1044 if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
1045 return DNHT_SCAN_END;
1046 ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
1047 dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
1048 if (flags) { /* copy queues */
1055 copy_si_cb(void *obj, void *arg)
1057 struct dn_sch_inst *si = obj;
1058 struct copy_args *a = arg;
1059 struct dn_flow *ni = (struct dn_flow *)(*a->start);
1060 if (copy_obj(a->start, a->end, &si->ni, "inst",
1061 si->sched->sch.sched_nr))
1062 return DNHT_SCAN_END;
1063 ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
1064 ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
1069 copy_si(struct copy_args *a, struct dn_schk *s, int flags)
1071 if (s->sch.flags & DN_HAVE_MASK)
1072 dn_ht_scan(s->siht, copy_si_cb, a);
1074 copy_si_cb(s->siht, a);
1079 * compute a list of children of a scheduler and copy up
1082 copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
1088 int n = 0, space = sizeof(*o);
1089 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1090 if (fs->fs.fs_nr < DN_MAX_ID)
1093 space += n * sizeof(uint32_t);
1094 DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
1095 if (a->end - *(a->start) < space)
1096 return DNHT_SCAN_END;
1097 o = (struct dn_id *)(*(a->start));
1099 *a->start += o->len;
1101 p = (uint32_t *)(o+1);
1102 SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
1103 if (fs->fs.fs_nr < DN_MAX_ID)
1104 *p++ = fs->fs.fs_nr;
1109 copy_data_helper(void *_o, void *_arg)
1111 struct copy_args *a = _arg;
1112 uint32_t *r = a->extra->r; /* start of first range */
1113 uint32_t *lim; /* first invalid pointer */
1116 lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
1118 if (a->type == DN_LINK || a->type == DN_SCH) {
1119 /* pipe|sched show, we receive a dn_schk */
1120 struct dn_schk *s = _o;
1122 n = s->sch.sched_nr;
1123 if (a->type == DN_SCH && n >= DN_MAX_ID)
1124 return 0; /* not a scheduler */
1125 if (a->type == DN_LINK && n <= DN_MAX_ID)
1126 return 0; /* not a pipe */
1128 /* see if the object is within one of our ranges */
1129 for (;r < lim; r += 2) {
1130 if (n < r[0] || n > r[1])
1132 /* Found a valid entry, copy and we are done */
1133 if (a->flags & DN_C_LINK) {
1134 if (copy_obj(a->start, a->end,
1135 &s->link, "link", n))
1136 return DNHT_SCAN_END;
1137 if (copy_profile(a, s->profile))
1138 return DNHT_SCAN_END;
1139 if (copy_flowset(a, s->fs, 0))
1140 return DNHT_SCAN_END;
1142 if (a->flags & DN_C_SCH) {
1143 if (copy_obj(a->start, a->end,
1144 &s->sch, "sched", n))
1145 return DNHT_SCAN_END;
1146 /* list all attached flowsets */
1147 if (copy_fsk_list(a, s, 0))
1148 return DNHT_SCAN_END;
1150 if (a->flags & DN_C_FLOW)
1154 } else if (a->type == DN_FS) {
1155 /* queue show, skip internal flowsets */
1156 struct dn_fsk *fs = _o;
1161 /* see if the object is within one of our ranges */
1162 for (;r < lim; r += 2) {
1163 if (n < r[0] || n > r[1])
1165 if (copy_flowset(a, fs, 0))
1166 return DNHT_SCAN_END;
1168 break; /* we are done */
1174 static inline struct dn_schk *
1175 locate_scheduler(int i)
1177 return dn_ht_find(dn_cfg.schedhash, i, 0, NULL);
1181 * red parameters are in fixed point arithmetic.
1184 config_red(struct dn_fsk *fs)
1186 int64_t s, idle, weight, w0;
1189 fs->w_q = fs->fs.w_q;
1190 fs->max_p = fs->fs.max_p;
1192 /* Doing stuff that was in userland */
1193 i = fs->sched->link.bandwidth;
1195 hz * dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
1197 idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
1198 fs->lookup_step = div64(idle , dn_cfg.red_lookup_depth);
1199 /* fs->lookup_step not scaled, */
1200 if (!fs->lookup_step)
1201 fs->lookup_step = 1;
1202 w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
1204 for (t = fs->lookup_step; t > 1; --t)
1205 weight = SCALE_MUL(weight, w0);
1206 fs->lookup_weight = (int)(weight); // scaled
1208 /* Now doing stuff that was in kerneland */
1209 fs->min_th = SCALE(fs->fs.min_th);
1210 fs->max_th = SCALE(fs->fs.max_th);
1212 if (fs->fs.max_th == fs->fs.min_th)
1213 fs->c_1 = fs->max_p;
1215 fs->c_1 = SCALE((int64_t)(fs->max_p)) / (fs->fs.max_th - fs->fs.min_th);
1216 fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
1218 if (fs->fs.flags & DN_IS_GENTLE_RED) {
1219 fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
1220 fs->c_4 = SCALE(1) - 2 * fs->max_p;
1223 /* If the lookup table already exist, free and create it again. */
1224 if (fs->w_q_lookup) {
1225 free(fs->w_q_lookup, M_DUMMYNET);
1226 fs->w_q_lookup = NULL;
1228 if (dn_cfg.red_lookup_depth == 0) {
1229 printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
1231 fs->fs.flags &= ~DN_IS_RED;
1232 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1235 fs->lookup_depth = dn_cfg.red_lookup_depth;
1236 fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
1237 M_DUMMYNET, M_NOWAIT);
1238 if (fs->w_q_lookup == NULL) {
1239 printf("dummynet: sorry, cannot allocate red lookup table\n");
1240 fs->fs.flags &= ~DN_IS_RED;
1241 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1245 /* Fill the lookup table with (1 - w_q)^x */
1246 fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
1248 for (i = 1; i < fs->lookup_depth; i++)
1250 SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
1252 if (dn_cfg.red_avg_pkt_size < 1)
1253 dn_cfg.red_avg_pkt_size = 512;
1254 fs->avg_pkt_size = dn_cfg.red_avg_pkt_size;
1255 if (dn_cfg.red_max_pkt_size < 1)
1256 dn_cfg.red_max_pkt_size = 1500;
1257 fs->max_pkt_size = dn_cfg.red_max_pkt_size;
1262 /* Scan all flowset attached to this scheduler and update red */
1264 update_red(struct dn_schk *s)
1267 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1268 if (fs && (fs->fs.flags & DN_IS_RED))
1273 /* attach flowset to scheduler s, possibly requeue */
1275 fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
1277 ND("remove fs %d from fsunlinked, link to sched %d",
1278 fs->fs.fs_nr, s->sch.sched_nr);
1279 SLIST_REMOVE(&dn_cfg.fsu, fs, dn_fsk, sch_chain);
1281 SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
1284 /* XXX compute fsk_mask */
1285 fs->fsk_mask = fs->fs.flow_mask;
1286 if (fs->sched->sch.flags & DN_HAVE_MASK)
1287 flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
1290 * we must drain qht according to the old
1291 * type, and reinsert according to the new one.
1292 * The requeue is complex -- in general we need to
1293 * reclassify every single packet.
1294 * For the time being, let's hope qht is never set
1295 * when we reach this point.
1297 D("XXX TODO requeue from fs %d to sch %d",
1298 fs->fs.fs_nr, s->sch.sched_nr);
1301 /* set the new type for qht */
1302 if (nonzero_mask(&fs->fsk_mask))
1303 fs->fs.flags |= DN_QHT_HASH;
1305 fs->fs.flags &= ~DN_QHT_HASH;
1307 /* XXX config_red() can fail... */
1308 if (fs->fs.flags & DN_IS_RED)
1312 /* update all flowsets which may refer to this scheduler */
1314 update_fs(struct dn_schk *s)
1316 struct dn_fsk *fs, *tmp;
1318 SLIST_FOREACH_SAFE(fs, &dn_cfg.fsu, sch_chain, tmp) {
1319 if (s->sch.sched_nr != fs->fs.sched_nr) {
1320 D("fs %d for sch %d not %d still unlinked",
1321 fs->fs.fs_nr, fs->fs.sched_nr,
1330 /* Retrieve AQM configurations to ipfw userland
1333 get_aqm_parms(struct sockopt *sopt)
1335 struct dn_extra_parms *ep;
1337 size_t sopt_valsize;
1340 sopt_valsize = sopt->sopt_valsize;
1342 if (sopt->sopt_valsize < l) {
1343 D("bad len sopt->sopt_valsize %d len %d",
1344 (int) sopt->sopt_valsize , l);
1348 ep = malloc(l, M_DUMMYNET, M_WAITOK);
1354 err = sooptcopyin(sopt, ep, l, l);
1357 sopt->sopt_valsize = sopt_valsize;
1358 if (ep->oid.len < l) {
1363 fs = dn_ht_find(dn_cfg.fshash, ep->nr, 0, NULL);
1365 D("fs %d not found", ep->nr);
1370 if (fs->aqmfp && fs->aqmfp->getconfig) {
1371 if(fs->aqmfp->getconfig(fs, ep)) {
1372 D("Error while trying to get AQM params");
1377 err = sooptcopyout(sopt, ep, l);
1381 free(ep, M_DUMMYNET);
1385 /* Retrieve AQM configurations to ipfw userland
1388 get_sched_parms(struct sockopt *sopt)
1390 struct dn_extra_parms *ep;
1391 struct dn_schk *schk;
1392 size_t sopt_valsize;
1395 sopt_valsize = sopt->sopt_valsize;
1397 if (sopt->sopt_valsize < l) {
1398 D("bad len sopt->sopt_valsize %d len %d",
1399 (int) sopt->sopt_valsize , l);
1403 ep = malloc(l, M_DUMMYNET, M_WAITOK);
1409 err = sooptcopyin(sopt, ep, l, l);
1412 sopt->sopt_valsize = sopt_valsize;
1413 if (ep->oid.len < l) {
1418 schk = locate_scheduler(ep->nr);
1420 D("sched %d not found", ep->nr);
1425 if (schk->fp && schk->fp->getconfig) {
1426 if(schk->fp->getconfig(schk, ep)) {
1427 D("Error while trying to get sched params");
1432 err = sooptcopyout(sopt, ep, l);
1435 free(ep, M_DUMMYNET);
1440 /* Configure AQM for flowset 'fs'.
1441 * extra parameters are passed from userland.
1444 config_aqm(struct dn_fsk *fs, struct dn_extra_parms *ep, int busy)
1449 /* no configurations */
1455 /* no AQM for this flowset*/
1456 if (!strcmp(ep->name,"")) {
1460 if (ep->oid.len < sizeof(*ep)) {
1461 D("short aqm len %d", ep->oid.len);
1467 D("Unable to configure flowset, flowset busy!");
1472 /* deconfigure old aqm if exist */
1473 if (fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig) {
1474 aqm_cleanup_deconfig_fs(fs);
1477 if (!(fs->aqmfp = find_aqm_type(0, ep->name))) {
1478 D("AQM functions not found for type %s!", ep->name);
1479 fs->fs.flags &= ~DN_IS_AQM;
1483 fs->fs.flags |= DN_IS_AQM;
1485 if (ep->oid.subtype != DN_AQM_PARAMS) {
1491 if (fs->aqmfp->config) {
1492 err = fs->aqmfp->config(fs, ep, ep->oid.len);
1494 D("Unable to configure AQM for FS %d", fs->fs.fs_nr );
1495 fs->fs.flags &= ~DN_IS_AQM;
1507 * Configuration -- to preserve backward compatibility we use
1508 * the following scheme (N is 65536)
1509 * NUMBER SCHED LINK FLOWSET
1510 * 1 .. N-1 (1)WFQ (2)WFQ (3)queue
1511 * N+1 .. 2N-1 (4)FIFO (5)FIFO (6)FIFO for sched 1..N-1
1512 * 2N+1 .. 3N-1 -- -- (7)FIFO for sched N+1..2N-1
1514 * "pipe i config" configures #1, #2 and #3
1515 * "sched i config" configures #1 and possibly #6
1516 * "queue i config" configures #3
1517 * #1 is configured with 'pipe i config' or 'sched i config'
1518 * #2 is configured with 'pipe i config', and created if not
1519 * existing with 'sched i config'
1520 * #3 is configured with 'queue i config'
1521 * #4 is automatically configured after #1, can only be FIFO
1522 * #5 is automatically configured after #2
1523 * #6 is automatically created when #1 is !MULTIQUEUE,
1524 * and can be updated.
1525 * #7 is automatically configured after #2
1529 * configure a link (and its FIFO instance)
1532 config_link(struct dn_link *p, struct dn_id *arg)
1536 if (p->oid.len != sizeof(*p)) {
1537 D("invalid pipe len %d", p->oid.len);
1541 if (i <= 0 || i >= DN_MAX_ID)
1544 * The config program passes parameters as follows:
1545 * bw = bits/second (0 means no limits),
1546 * delay = ms, must be translated into ticks.
1547 * qsize = slots/bytes
1550 p->delay = (p->delay * hz) / 1000;
1551 /* Scale burst size: bytes -> bits * hz */
1555 /* do it twice, base link and FIFO link */
1556 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1557 struct dn_schk *s = locate_scheduler(i);
1560 D("sched %d not found", i);
1563 /* remove profile if exists */
1565 free(s->profile, M_DUMMYNET);
1568 /* copy all parameters */
1569 s->link.oid = p->oid;
1570 s->link.link_nr = i;
1571 s->link.delay = p->delay;
1572 if (s->link.bandwidth != p->bandwidth) {
1573 /* XXX bandwidth changes, need to update red params */
1574 s->link.bandwidth = p->bandwidth;
1577 s->link.burst = p->burst;
1578 schk_reset_credit(s);
1586 * configure a flowset. Can be called from inside with locked=1,
1588 static struct dn_fsk *
1589 config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
1594 struct dn_extra_parms *ep;
1597 if (nfs->oid.len != sizeof(*nfs)) {
1598 D("invalid flowset len %d", nfs->oid.len);
1602 if (i <= 0 || i >= 3*DN_MAX_ID)
1607 ep = malloc(sizeof(*ep), M_TEMP, locked ? M_NOWAIT : M_WAITOK);
1610 memcpy(ep, arg, sizeof(*ep));
1613 ND("flowset %d", i);
1614 /* XXX other sanity checks */
1615 if (nfs->flags & DN_QSIZE_BYTES) {
1616 ipdn_bound_var(&nfs->qsize, 16384,
1617 1500, dn_cfg.byte_limit, NULL); // "queue byte size");
1619 ipdn_bound_var(&nfs->qsize, 50,
1620 1, dn_cfg.slot_limit, NULL); // "queue slot size");
1622 if (nfs->flags & DN_HAVE_MASK) {
1623 /* make sure we have some buckets */
1624 ipdn_bound_var((int *)&nfs->buckets, dn_cfg.hash_size,
1625 1, dn_cfg.max_hash_size, "flowset buckets");
1627 nfs->buckets = 1; /* we only need 1 */
1631 do { /* exit with break when done */
1633 int flags = nfs->sched_nr ? DNHT_INSERT : 0;
1635 int oldc = dn_cfg.fsk_count;
1636 fs = dn_ht_find(dn_cfg.fshash, i, flags, NULL);
1638 D("missing sched for flowset %d", i);
1641 /* grab some defaults from the existing one */
1642 if (nfs->sched_nr == 0) /* reuse */
1643 nfs->sched_nr = fs->fs.sched_nr;
1644 for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
1645 if (nfs->par[j] == -1) /* reuse */
1646 nfs->par[j] = fs->fs.par[j];
1648 if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
1649 ND("flowset %d unchanged", i);
1653 * Reconfigure AQM as the parameters can be changed.
1654 * We consider the flowset as busy if it has scheduler
1657 s = locate_scheduler(nfs->sched_nr);
1658 config_aqm(fs, ep, s != NULL && s->siht != NULL);
1661 break; /* no change, nothing to do */
1663 if (oldc != dn_cfg.fsk_count) /* new item */
1665 s = locate_scheduler(nfs->sched_nr);
1666 /* detach from old scheduler if needed, preserving
1667 * queues if we need to reattach. Then update the
1668 * configuration, and possibly attach to the new sched.
1670 DX(2, "fs %d changed sched %d@%p to %d@%p",
1672 fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
1674 int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
1675 flags |= DN_DESTROY; /* XXX temporary */
1676 fsk_detach(fs, flags);
1678 fs->fs = *nfs; /* copy configuration */
1682 config_aqm(fs, ep, s != NULL &&
1698 * config/reconfig a scheduler and its FIFO variant.
1699 * For !MULTIQUEUE schedulers, also set up the flowset.
1701 * On reconfigurations (detected because s->fp is set),
1702 * detach existing flowsets preserving traffic, preserve link,
1703 * and delete the old scheduler creating a new one.
1706 config_sched(struct dn_sch *_nsch, struct dn_id *arg)
1709 struct schk_new_arg a; /* argument for schk_new */
1711 struct dn_link p; /* copy of oldlink */
1712 struct dn_profile *pf = NULL; /* copy of old link profile */
1713 /* Used to preserv mask parameter */
1714 struct ipfw_flow_id new_mask;
1715 int new_buckets = 0;
1721 if (a.sch->oid.len != sizeof(*a.sch)) {
1722 D("bad sched len %d", a.sch->oid.len);
1725 i = a.sch->sched_nr;
1726 if (i <= 0 || i >= DN_MAX_ID)
1728 /* make sure we have some buckets */
1729 if (a.sch->flags & DN_HAVE_MASK)
1730 ipdn_bound_var((int *)&a.sch->buckets, dn_cfg.hash_size,
1731 1, dn_cfg.max_hash_size, "sched buckets");
1732 /* XXX other sanity checks */
1733 bzero(&p, sizeof(p));
1735 pipe_cmd = a.sch->flags & DN_PIPE_CMD;
1736 a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
1738 /* Copy mask parameter */
1739 new_mask = a.sch->sched_mask;
1740 new_buckets = a.sch->buckets;
1741 new_flags = a.sch->flags;
1744 again: /* run twice, for wfq and fifo */
1746 * lookup the type. If not supplied, use the previous one
1747 * or default to WF2Q+. Otherwise, return an error.
1750 a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
1752 /* found. Lookup or create entry */
1753 s = dn_ht_find(dn_cfg.schedhash, i, DNHT_INSERT, &a);
1754 } else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
1755 /* No type. search existing s* or retry with WF2Q+ */
1756 s = dn_ht_find(dn_cfg.schedhash, i, 0, &a);
1759 /* Scheduler exists, skip to FIFO scheduler
1760 * if command was pipe config...
1765 /* New scheduler, create a wf2q+ with no mask
1766 * if command was pipe config...
1769 /* clear mask parameter */
1770 bzero(&a.sch->sched_mask, sizeof(new_mask));
1772 a.sch->flags &= ~DN_HAVE_MASK;
1774 a.sch->oid.subtype = DN_SCHED_WF2QP;
1778 D("invalid scheduler type %d %s",
1779 a.sch->oid.subtype, a.sch->name);
1783 /* normalize name and subtype */
1784 a.sch->oid.subtype = a.fp->type;
1785 bzero(a.sch->name, sizeof(a.sch->name));
1786 strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
1788 D("cannot allocate scheduler %d", i);
1791 /* restore existing link if any */
1794 if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
1795 s->profile = NULL; /* XXX maybe not needed */
1797 s->profile = malloc(sizeof(struct dn_profile),
1798 M_DUMMYNET, M_NOWAIT | M_ZERO);
1799 if (s->profile == NULL) {
1800 D("cannot allocate profile");
1803 memcpy(s->profile, pf, sizeof(*pf));
1807 if (s->fp == NULL) {
1808 DX(2, "sched %d new type %s", i, a.fp->name);
1809 } else if (s->fp != a.fp ||
1810 bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
1811 /* already existing. */
1812 DX(2, "sched %d type changed from %s to %s",
1813 i, s->fp->name, a.fp->name);
1814 DX(4, " type/sub %d/%d -> %d/%d",
1815 s->sch.oid.type, s->sch.oid.subtype,
1816 a.sch->oid.type, a.sch->oid.subtype);
1817 if (s->link.link_nr == 0)
1818 D("XXX WARNING link 0 for sched %d", i);
1819 p = s->link; /* preserve link */
1820 if (s->profile) {/* preserve profile */
1822 pf = malloc(sizeof(*pf),
1823 M_DUMMYNET, M_NOWAIT | M_ZERO);
1824 if (pf) /* XXX should issue a warning otherwise */
1825 memcpy(pf, s->profile, sizeof(*pf));
1827 /* remove from the hash */
1828 dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
1829 /* Detach flowsets, preserve queues. */
1830 // schk_delete_cb(s, NULL);
1831 // XXX temporarily, kill queues
1832 schk_delete_cb(s, (void *)DN_DESTROY);
1835 DX(4, "sched %d unchanged type %s", i, a.fp->name);
1837 /* complete initialization */
1841 // XXX schk_reset_credit(s);
1842 /* create the internal flowset if needed,
1843 * trying to reuse existing ones if available
1845 if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
1846 s->fs = dn_ht_find(dn_cfg.fshash, i, 0, NULL);
1849 bzero(&fs, sizeof(fs));
1850 set_oid(&fs.oid, DN_FS, sizeof(fs));
1851 fs.fs_nr = i + DN_MAX_ID;
1853 s->fs = config_fs(&fs, NULL, 1 /* locked */);
1856 schk_delete_cb(s, (void *)DN_DESTROY);
1857 D("error creating internal fs for %d", i);
1861 /* call init function after the flowset is created */
1866 if (i < DN_MAX_ID) { /* now configure the FIFO instance */
1869 /* Restore mask parameter for FIFO */
1870 a.sch->sched_mask = new_mask;
1871 a.sch->buckets = new_buckets;
1872 a.sch->flags = new_flags;
1874 /* sched config shouldn't modify the FIFO scheduler */
1875 if (dn_ht_find(dn_cfg.schedhash, i, 0, &a) != NULL) {
1876 /* FIFO already exist, don't touch it */
1877 err = 0; /* and this is not an error */
1881 a.sch->sched_nr = i;
1882 a.sch->oid.subtype = DN_SCHED_FIFO;
1883 bzero(a.sch->name, sizeof(a.sch->name));
1890 free(pf, M_DUMMYNET);
1895 * attach a profile to a link
1898 config_profile(struct dn_profile *pf, struct dn_id *arg)
1901 int i, olen, err = 0;
1903 if (pf->oid.len < sizeof(*pf)) {
1904 D("short profile len %d", pf->oid.len);
1908 if (i <= 0 || i >= DN_MAX_ID)
1910 /* XXX other sanity checks */
1912 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1913 s = locate_scheduler(i);
1921 * If we had a profile and the new one does not fit,
1922 * or it is deleted, then we need to free memory.
1924 if (s->profile && (pf->samples_no == 0 ||
1925 s->profile->oid.len < pf->oid.len)) {
1926 free(s->profile, M_DUMMYNET);
1929 if (pf->samples_no == 0)
1932 * new profile, possibly allocate memory
1935 if (s->profile == NULL)
1936 s->profile = malloc(pf->oid.len,
1937 M_DUMMYNET, M_NOWAIT | M_ZERO);
1938 if (s->profile == NULL) {
1939 D("no memory for profile %d", i);
1943 /* preserve larger length XXX double check */
1944 olen = s->profile->oid.len;
1945 if (olen < pf->oid.len)
1947 memcpy(s->profile, pf, pf->oid.len);
1948 s->profile->oid.len = olen;
1955 * Delete all objects:
1958 dummynet_flush(void)
1961 /* delete all schedulers and related links/queues/flowsets */
1962 dn_ht_scan(dn_cfg.schedhash, schk_delete_cb,
1963 (void *)(uintptr_t)DN_DELETE_FS);
1964 /* delete all remaining (unlinked) flowsets */
1965 DX(4, "still %d unlinked fs", dn_cfg.fsk_count);
1966 dn_ht_free(dn_cfg.fshash, DNHT_REMOVE);
1967 fsk_detach_list(&dn_cfg.fsu, DN_DELETE_FS);
1968 /* Reinitialize system heap... */
1969 heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
1973 * Main handler for configuration. We are guaranteed to be called
1974 * with an oid which is at least a dn_id.
1975 * - the first object is the command (config, delete, flush, ...)
1976 * - config_link must be issued after the corresponding config_sched
1977 * - parameters (DN_TXT) for an object must precede the object
1978 * processed on a config_sched.
1981 do_config(void *p, int l)
1985 struct dn_profile profile;
1987 struct dn_link link;
1988 struct dn_sch sched;
1994 memcpy(&o, p, sizeof(o));
1995 if (o.id != DN_API_VERSION) {
1996 D("invalid api version got %d need %d", o.id, DN_API_VERSION);
2001 for (off = 0; l >= sizeof(o); memcpy(&o, (char *)p + off, sizeof(o))) {
2002 if (o.len < sizeof(o) || l < o.len) {
2003 D("bad len o.len %d len %d", o.len, l);
2011 D("cmd %d not implemented", o.type);
2014 #ifdef EMULATE_SYSCTL
2015 /* sysctl emulation.
2016 * if we recognize the command, jump to the correct
2017 * handler and return
2020 err = kesysctl_emu_set(p, l);
2024 case DN_CMD_CONFIG: /* simply a header */
2028 /* the argument is in the first uintptr_t after o */
2029 if (o.len < sizeof(o) + sizeof(a)) {
2033 memcpy(&a, (char *)p + off + sizeof(o), sizeof(a));
2034 switch (o.subtype) {
2036 /* delete base and derived schedulers */
2038 err = delete_schk(a);
2039 err2 = delete_schk(a + DN_MAX_ID);
2046 D("invalid delete type %d", o.subtype);
2051 err = (a < 1 || a >= DN_MAX_ID) ?
2052 EINVAL : delete_fs(a, 0) ;
2062 case DN_TEXT: /* store argument of next block */
2065 arg = malloc(o.len, M_TEMP, M_WAITOK);
2066 memcpy(arg, (char *)p + off, o.len);
2070 dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2071 memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
2072 err = config_link(&dn->link, arg);
2076 dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2077 memcpy(&dn->profile, (char *)p + off,
2078 sizeof(dn->profile));
2079 err = config_profile(&dn->profile, arg);
2083 dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2084 memcpy(&dn->sched, (char *)p + off,
2086 err = config_sched(&dn->sched, arg);
2090 dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2091 memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
2092 err = (NULL == config_fs(&dn->fs, arg, 0));
2107 compute_space(struct dn_id *cmd, struct copy_args *a)
2109 int x = 0, need = 0;
2110 int profile_size = sizeof(struct dn_profile) -
2111 ED_MAX_SAMPLES_NO*sizeof(int);
2113 /* NOTE about compute space:
2114 * NP = dn_cfg.schk_count
2115 * NSI = dn_cfg.si_count
2116 * NF = dn_cfg.fsk_count
2117 * NQ = dn_cfg.queue_count
2119 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2120 * link, scheduler template, flowset
2121 * integrated in scheduler and header
2123 * (NSI)*(dn_flow) all scheduler instance (includes
2124 * the queue instance)
2126 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2127 * link, scheduler template, flowset
2128 * integrated in scheduler and header
2130 * (NSI * dn_flow) all scheduler instances
2131 * (NF * sizeof(uint_32)) space for flowset list linked to scheduler
2132 * (NQ * dn_queue) all queue [XXXfor now not listed]
2134 * (NF * dn_fs) all flowset
2135 * (NQ * dn_queue) all queues
2137 switch (cmd->subtype) {
2140 /* XXX where do LINK and SCH differ ? */
2141 /* 'ipfw sched show' could list all queues associated to
2142 * a scheduler. This feature for now is disabled
2144 case DN_LINK: /* pipe show */
2145 x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
2146 need += dn_cfg.schk_count *
2147 (sizeof(struct dn_fs) + profile_size) / 2;
2148 need += dn_cfg.fsk_count * sizeof(uint32_t);
2150 case DN_SCH: /* sched show */
2151 need += dn_cfg.schk_count *
2152 (sizeof(struct dn_fs) + profile_size) / 2;
2153 need += dn_cfg.fsk_count * sizeof(uint32_t);
2154 x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
2156 case DN_FS: /* queue show */
2157 x = DN_C_FS | DN_C_QUEUE;
2159 case DN_GET_COMPAT: /* compatibility mode */
2160 need = dn_compat_calc_size();
2165 need += dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
2166 /* NOT also, each fs might be attached to a sched */
2167 need += dn_cfg.schk_count * sizeof(struct dn_id) / 2;
2170 need += dn_cfg.fsk_count * sizeof(struct dn_fs);
2171 if (x & DN_C_LINK) {
2172 need += dn_cfg.schk_count * sizeof(struct dn_link) / 2;
2175 * When exporting a queue to userland, only pass up the
2176 * struct dn_flow, which is the only visible part.
2180 need += dn_cfg.queue_count * sizeof(struct dn_flow);
2182 need += dn_cfg.si_count * (sizeof(struct dn_flow));
2187 * If compat != NULL dummynet_get is called in compatibility mode.
2188 * *compat will be the pointer to the buffer to pass to ipfw
2191 dummynet_get(struct sockopt *sopt, void **compat)
2193 int have, i, need, error;
2194 char *start = NULL, *buf;
2195 size_t sopt_valsize;
2198 struct copy_range r;
2199 int l = sizeof(struct dn_id);
2201 bzero(&a, sizeof(a));
2202 bzero(&r, sizeof(r));
2204 /* save and restore original sopt_valsize around copyin */
2205 sopt_valsize = sopt->sopt_valsize;
2210 /* copy at least an oid, and possibly a full object */
2211 error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
2212 sopt->sopt_valsize = sopt_valsize;
2216 #ifdef EMULATE_SYSCTL
2217 /* sysctl emulation. */
2218 if (cmd->type == DN_SYSCTL_GET)
2219 return kesysctl_emu_get(sopt);
2221 if (l > sizeof(r)) {
2222 /* request larger than default, allocate buffer */
2223 cmd = malloc(l, M_DUMMYNET, M_WAITOK);
2224 error = sooptcopyin(sopt, cmd, l, l);
2225 sopt->sopt_valsize = sopt_valsize;
2229 } else { /* compatibility */
2231 cmd->type = DN_CMD_GET;
2232 cmd->len = sizeof(struct dn_id);
2233 cmd->subtype = DN_GET_COMPAT;
2234 // cmd->id = sopt_valsize;
2235 D("compatibility mode");
2239 /* get AQM params */
2240 if(cmd->subtype == DN_AQM_PARAMS) {
2241 error = get_aqm_parms(sopt);
2243 /* get Scheduler params */
2244 } else if (cmd->subtype == DN_SCH_PARAMS) {
2245 error = get_sched_parms(sopt);
2250 a.extra = (struct copy_range *)cmd;
2251 if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
2252 uint32_t *rp = (uint32_t *)(cmd + 1);
2253 cmd->len += 2* sizeof(uint32_t);
2255 rp[1] = DN_MAX_ID - 1;
2256 if (cmd->subtype == DN_LINK) {
2261 /* Count space (under lock) and allocate (outside lock).
2262 * Exit with lock held if we manage to get enough buffer.
2263 * Try a few times then give up.
2265 for (have = 0, i = 0; i < 10; i++) {
2267 need = compute_space(cmd, &a);
2269 /* if there is a range, ignore value from compute_space() */
2270 if (l > sizeof(*cmd))
2271 need = sopt_valsize - sizeof(*cmd);
2278 need += sizeof(*cmd);
2285 free(start, M_DUMMYNET);
2287 if (need > sopt_valsize)
2291 start = malloc(have, M_DUMMYNET, M_WAITOK | M_ZERO);
2294 if (start == NULL) {
2299 error = sooptcopyout(sopt, cmd, sizeof(*cmd));
2303 ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
2304 "%d:%d si %d, %d:%d queues %d",
2305 dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
2306 dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
2307 dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
2308 dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
2309 dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
2310 sopt->sopt_valsize = sopt_valsize;
2311 a.type = cmd->subtype;
2313 if (compat == NULL) {
2314 memcpy(start, cmd, sizeof(*cmd));
2315 ((struct dn_id*)(start))->len = sizeof(struct dn_id);
2316 buf = start + sizeof(*cmd);
2320 a.end = start + have;
2321 /* start copying other objects */
2323 a.type = DN_COMPAT_PIPE;
2324 dn_ht_scan(dn_cfg.schedhash, copy_data_helper_compat, &a);
2325 a.type = DN_COMPAT_QUEUE;
2326 dn_ht_scan(dn_cfg.fshash, copy_data_helper_compat, &a);
2327 } else if (a.type == DN_FS) {
2328 dn_ht_scan(dn_cfg.fshash, copy_data_helper, &a);
2330 dn_ht_scan(dn_cfg.schedhash, copy_data_helper, &a);
2336 sopt->sopt_valsize = buf - start;
2337 /* free() is done by ip_dummynet_compat() */
2338 start = NULL; //XXX hack
2340 error = sooptcopyout(sopt, start, buf - start);
2343 if (cmd && cmd != &r.o)
2344 free(cmd, M_DUMMYNET);
2346 free(start, M_DUMMYNET);
2350 /* Callback called on scheduler instance to delete it if idle */
2352 drain_scheduler_cb(void *_si, void *arg)
2354 struct dn_sch_inst *si = _si;
2356 if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
2359 if (si->sched->fp->flags & DN_MULTIQUEUE) {
2360 if (si->q_count == 0)
2361 return si_destroy(si, NULL);
2364 } else { /* !DN_MULTIQUEUE */
2365 if ((si+1)->ni.length == 0)
2366 return si_destroy(si, NULL);
2370 return 0; /* unreachable */
2373 /* Callback called on scheduler to check if it has instances */
2375 drain_scheduler_sch_cb(void *_s, void *arg)
2377 struct dn_schk *s = _s;
2379 if (s->sch.flags & DN_HAVE_MASK) {
2380 dn_ht_scan_bucket(s->siht, &s->drain_bucket,
2381 drain_scheduler_cb, NULL);
2385 if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
2392 /* Called every tick, try to delete a 'bucket' of scheduler */
2394 dn_drain_scheduler(void)
2396 dn_ht_scan_bucket(dn_cfg.schedhash, &dn_cfg.drain_sch,
2397 drain_scheduler_sch_cb, NULL);
2401 /* Callback called on queue to delete if it is idle */
2403 drain_queue_cb(void *_q, void *arg)
2405 struct dn_queue *q = _q;
2407 if (q->ni.length == 0) {
2408 dn_delete_queue(q, DN_DESTROY);
2409 return DNHT_SCAN_DEL; /* queue is deleted */
2412 return 0; /* queue isn't deleted */
2415 /* Callback called on flowset used to check if it has queues */
2417 drain_queue_fs_cb(void *_fs, void *arg)
2419 struct dn_fsk *fs = _fs;
2421 if (fs->fs.flags & DN_QHT_HASH) {
2422 /* Flowset has a hash table for queues */
2423 dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
2424 drain_queue_cb, NULL);
2427 /* No hash table for this flowset, null the pointer
2428 * if the queue is deleted
2431 if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
2438 /* Called every tick, try to delete a 'bucket' of queue */
2440 dn_drain_queue(void)
2442 /* scan a bucket of flowset */
2443 dn_ht_scan_bucket(dn_cfg.fshash, &dn_cfg.drain_fs,
2444 drain_queue_fs_cb, NULL);
2449 * Handler for the various dummynet socket options
2452 ip_dn_ctl(struct sockopt *sopt)
2457 error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
2461 /* Disallow sets in really-really secure mode. */
2462 if (sopt->sopt_dir == SOPT_SET) {
2463 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2468 switch (sopt->sopt_name) {
2470 D("dummynet: unknown option %d", sopt->sopt_name);
2474 case IP_DUMMYNET_FLUSH:
2475 case IP_DUMMYNET_CONFIGURE:
2476 case IP_DUMMYNET_DEL: /* remove a pipe or queue */
2477 case IP_DUMMYNET_GET:
2478 D("dummynet: compat option %d", sopt->sopt_name);
2479 error = ip_dummynet_compat(sopt);
2483 if (sopt->sopt_dir == SOPT_GET) {
2484 error = dummynet_get(sopt, NULL);
2487 l = sopt->sopt_valsize;
2488 if (l < sizeof(struct dn_id) || l > 12000) {
2489 D("argument len %d invalid", l);
2492 p = malloc(l, M_TEMP, M_WAITOK); // XXX can it fail ?
2493 error = sooptcopyin(sopt, p, l, l);
2496 error = do_config(p, l);
2510 if (dn_cfg.init_done)
2512 printf("DUMMYNET %p with IPv6 initialized (100409)\n", curvnet);
2513 dn_cfg.init_done = 1;
2514 /* Set defaults here. MSVC does not accept initializers,
2515 * and this is also useful for vimages
2518 dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
2519 dn_cfg.byte_limit = 1024 * 1024;
2522 /* RED parameters */
2523 dn_cfg.red_lookup_depth = 256; /* default lookup table depth */
2524 dn_cfg.red_avg_pkt_size = 512; /* default medium packet size */
2525 dn_cfg.red_max_pkt_size = 1500; /* default max packet size */
2528 dn_cfg.max_hash_size = 65536; /* max in the hash tables */
2529 dn_cfg.hash_size = 64; /* default hash size */
2531 /* create hash tables for schedulers and flowsets.
2532 * In both we search by key and by pointer.
2534 dn_cfg.schedhash = dn_ht_init(NULL, dn_cfg.hash_size,
2535 offsetof(struct dn_schk, schk_next),
2536 schk_hash, schk_match, schk_new);
2537 dn_cfg.fshash = dn_ht_init(NULL, dn_cfg.hash_size,
2538 offsetof(struct dn_fsk, fsk_next),
2539 fsk_hash, fsk_match, fsk_new);
2541 /* bucket index to drain object */
2542 dn_cfg.drain_fs = 0;
2543 dn_cfg.drain_sch = 0;
2545 heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
2546 SLIST_INIT(&dn_cfg.fsu);
2547 SLIST_INIT(&dn_cfg.schedlist);
2551 TASK_INIT(&dn_task, 0, dummynet_task, curvnet);
2552 dn_tq = taskqueue_create_fast("dummynet", M_WAITOK,
2553 taskqueue_thread_enqueue, &dn_tq);
2554 taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
2556 callout_init(&dn_timeout, 1);
2559 /* Initialize curr_time adjustment mechanics. */
2560 getmicrouptime(&dn_cfg.prev_t);
2564 ip_dn_destroy(int last)
2567 /* ensure no more callouts are started */
2570 /* check for last */
2572 ND("removing last instance\n");
2573 ip_dn_ctl_ptr = NULL;
2574 ip_dn_io_ptr = NULL;
2580 callout_drain(&dn_timeout);
2581 taskqueue_drain(dn_tq, &dn_task);
2582 taskqueue_free(dn_tq);
2584 dn_ht_free(dn_cfg.schedhash, 0);
2585 dn_ht_free(dn_cfg.fshash, 0);
2586 heap_free(&dn_cfg.evheap);
2592 dummynet_modevent(module_t mod, int type, void *data)
2595 if (type == MOD_LOAD) {
2597 printf("DUMMYNET already loaded\n");
2601 ip_dn_ctl_ptr = ip_dn_ctl;
2602 ip_dn_io_ptr = dummynet_io;
2604 } else if (type == MOD_UNLOAD) {
2605 ip_dn_destroy(1 /* last */);
2611 /* modevent helpers for the modules */
2613 load_dn_sched(struct dn_alg *d)
2618 return 1; /* error */
2619 ip_dn_init(); /* just in case, we need the lock */
2621 /* Check that mandatory funcs exists */
2622 if (d->enqueue == NULL || d->dequeue == NULL) {
2623 D("missing enqueue or dequeue for %s", d->name);
2627 /* Search if scheduler already exists */
2629 SLIST_FOREACH(s, &dn_cfg.schedlist, next) {
2630 if (strcmp(s->name, d->name) == 0) {
2631 D("%s already loaded", d->name);
2632 break; /* scheduler already exists */
2636 SLIST_INSERT_HEAD(&dn_cfg.schedlist, d, next);
2638 D("dn_sched %s %sloaded", d->name, s ? "not ":"");
2643 unload_dn_sched(struct dn_alg *s)
2645 struct dn_alg *tmp, *r;
2648 ND("called for %s", s->name);
2651 SLIST_FOREACH_SAFE(r, &dn_cfg.schedlist, next, tmp) {
2652 if (strcmp(s->name, r->name) != 0)
2654 ND("ref_count = %d", r->ref_count);
2655 err = (r->ref_count != 0) ? EBUSY : 0;
2657 SLIST_REMOVE(&dn_cfg.schedlist, r, dn_alg, next);
2661 D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
2666 dn_sched_modevent(module_t mod, int cmd, void *arg)
2668 struct dn_alg *sch = arg;
2670 if (cmd == MOD_LOAD)
2671 return load_dn_sched(sch);
2672 else if (cmd == MOD_UNLOAD)
2673 return unload_dn_sched(sch);
2678 static moduledata_t dummynet_mod = {
2679 "dummynet", dummynet_modevent, NULL
2682 #define DN_SI_SUB SI_SUB_PROTO_FIREWALL
2683 #define DN_MODEV_ORD (SI_ORDER_ANY - 128) /* after ipfw */
2684 DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
2685 MODULE_DEPEND(dummynet, ipfw, 3, 3, 3);
2686 MODULE_VERSION(dummynet, 3);
2689 * Starting up. Done in order after dummynet_modevent() has been called.
2690 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2692 //VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_init, NULL);
2695 * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
2696 * after dummynet_modevent() has been called. Not called on reboot.
2697 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2698 * or when the module is unloaded.
2700 //VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_destroy, NULL);
2704 /* modevent helpers for the AQM modules */
2706 load_dn_aqm(struct dn_aqm *d)
2708 struct dn_aqm *aqm=NULL;
2711 return 1; /* error */
2712 ip_dn_init(); /* just in case, we need the lock */
2714 /* Check that mandatory funcs exists */
2715 if (d->enqueue == NULL || d->dequeue == NULL) {
2716 D("missing enqueue or dequeue for %s", d->name);
2720 /* Search if AQM already exists */
2722 SLIST_FOREACH(aqm, &dn_cfg.aqmlist, next) {
2723 if (strcmp(aqm->name, d->name) == 0) {
2724 D("%s already loaded", d->name);
2725 break; /* AQM already exists */
2729 SLIST_INSERT_HEAD(&dn_cfg.aqmlist, d, next);
2731 D("dn_aqm %s %sloaded", d->name, aqm ? "not ":"");
2736 /* Callback to clean up AQM status for queues connected to a flowset
2737 * and then deconfigure the flowset.
2738 * This function is called before an AQM module is unloaded
2741 fs_cleanup(void *_fs, void *arg)
2743 struct dn_fsk *fs = _fs;
2744 uint32_t type = *(uint32_t *)arg;
2746 if (fs->aqmfp && fs->aqmfp->type == type)
2747 aqm_cleanup_deconfig_fs(fs);
2753 unload_dn_aqm(struct dn_aqm *aqm)
2755 struct dn_aqm *tmp, *r;
2758 ND("called for %s", aqm->name);
2762 /* clean up AQM status and deconfig flowset */
2763 dn_ht_scan(dn_cfg.fshash, fs_cleanup, &aqm->type);
2765 SLIST_FOREACH_SAFE(r, &dn_cfg.aqmlist, next, tmp) {
2766 if (strcmp(aqm->name, r->name) != 0)
2768 ND("ref_count = %d", r->ref_count);
2769 err = (r->ref_count != 0 || r->cfg_ref_count != 0) ? EBUSY : 0;
2771 SLIST_REMOVE(&dn_cfg.aqmlist, r, dn_aqm, next);
2775 D("%s %sunloaded", aqm->name, err ? "not ":"");
2777 D("ref_count=%d, cfg_ref_count=%d", r->ref_count, r->cfg_ref_count);
2782 dn_aqm_modevent(module_t mod, int cmd, void *arg)
2784 struct dn_aqm *aqm = arg;
2786 if (cmd == MOD_LOAD)
2787 return load_dn_aqm(aqm);
2788 else if (cmd == MOD_UNLOAD)
2789 return unload_dn_aqm(aqm);