2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Codel/FQ_Codel and PIE/FQ-PIE Code:
5 * Copyright (C) 2016 Centre for Advanced Internet Architectures,
6 * Swinburne University of Technology, Melbourne, Australia.
7 * Portions of this code were made possible in part by a gift from
8 * The Comcast Innovation Fund.
9 * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
11 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
12 * Portions Copyright (c) 2000 Akamba Corp.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * Configuration and internal object management for dummynet.
44 #include "opt_inet6.h"
46 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
57 #include <sys/rwlock.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
61 #include <sys/taskqueue.h>
62 #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
63 #include <netinet/in.h>
64 #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */
65 #include <netinet/ip_fw.h>
66 #include <netinet/ip_dummynet.h>
69 #include <netpfil/ipfw/ip_fw_private.h>
70 #include <netpfil/ipfw/dn_heap.h>
71 #include <netpfil/ipfw/ip_dn_private.h>
73 #include <netpfil/ipfw/dn_aqm.h>
75 #include <netpfil/ipfw/dn_sched.h>
77 /* which objects to copy */
78 #define DN_C_LINK 0x01
80 #define DN_C_FLOW 0x04
82 #define DN_C_QUEUE 0x10
84 /* we use this argument in case of a schk_new */
90 /*---- callout hooks. ----*/
91 static struct callout dn_timeout;
92 static int dn_tasks_started = 0;
94 static struct task dn_task;
95 static struct taskqueue *dn_tq = NULL;
97 /* global scheduler list */
99 CK_LIST_HEAD(, dn_alg) schedlist;
101 CK_LIST_HEAD(, dn_aqm) aqmlist; /* list of AQMs */
108 (void)arg; /* UNUSED */
109 taskqueue_enqueue(dn_tq, &dn_task);
113 dummynet_sched_lock(void)
115 mtx_lock(&sched_mtx);
119 dummynet_sched_unlock(void)
121 mtx_unlock(&sched_mtx);
130 callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL,
131 C_HARDCLOCK | C_DIRECT_EXEC);
133 /*----- end of callout hooks -----*/
136 /* Return AQM descriptor for given type or name. */
137 static struct dn_aqm *
138 find_aqm_type(int type, char *name)
144 CK_LIST_FOREACH(d, &aqmlist, next) {
145 if (d->type == type || (name && !strcasecmp(d->name, name)))
148 return NULL; /* not found */
152 /* Return a scheduler descriptor given the type or name. */
153 static struct dn_alg *
154 find_sched_type(int type, char *name)
160 CK_LIST_FOREACH(d, &schedlist, next) {
161 if (d->type == type || (name && !strcasecmp(d->name, name)))
164 return NULL; /* not found */
168 ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
171 const char *op = NULL;
179 } else if (oldv > hi) {
184 if (op && msg && bootverbose)
185 printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
189 /*---- flow_id mask, hash and compare functions ---*/
191 * The flow_id includes the 5-tuple, the queue/pipe number
192 * which we store in the extra area in host order,
193 * and for ipv6 also the flow_id6.
194 * XXX see if we want the tos byte (can store in 'flags')
196 static struct ipfw_flow_id *
197 flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
199 int is_v6 = IS_IP6_FLOW_ID(id);
201 id->dst_port &= mask->dst_port;
202 id->src_port &= mask->src_port;
203 id->proto &= mask->proto;
204 id->extra &= mask->extra;
206 APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
207 APPLY_MASK(&id->src_ip6, &mask->src_ip6);
208 id->flow_id6 &= mask->flow_id6;
210 id->dst_ip &= mask->dst_ip;
211 id->src_ip &= mask->src_ip;
216 /* computes an OR of two masks, result in dst and also returned */
217 static struct ipfw_flow_id *
218 flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
220 int is_v6 = IS_IP6_FLOW_ID(dst);
222 dst->dst_port |= src->dst_port;
223 dst->src_port |= src->src_port;
224 dst->proto |= src->proto;
225 dst->extra |= src->extra;
227 #define OR_MASK(_d, _s) \
228 (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
229 (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
230 (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
231 (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
232 OR_MASK(&dst->dst_ip6, &src->dst_ip6);
233 OR_MASK(&dst->src_ip6, &src->src_ip6);
235 dst->flow_id6 |= src->flow_id6;
237 dst->dst_ip |= src->dst_ip;
238 dst->src_ip |= src->src_ip;
244 nonzero_mask(struct ipfw_flow_id *m)
246 if (m->dst_port || m->src_port || m->proto || m->extra)
248 if (IS_IP6_FLOW_ID(m)) {
250 m->dst_ip6.__u6_addr.__u6_addr32[0] ||
251 m->dst_ip6.__u6_addr.__u6_addr32[1] ||
252 m->dst_ip6.__u6_addr.__u6_addr32[2] ||
253 m->dst_ip6.__u6_addr.__u6_addr32[3] ||
254 m->src_ip6.__u6_addr.__u6_addr32[0] ||
255 m->src_ip6.__u6_addr.__u6_addr32[1] ||
256 m->src_ip6.__u6_addr.__u6_addr32[2] ||
257 m->src_ip6.__u6_addr.__u6_addr32[3] ||
260 return m->dst_ip || m->src_ip;
264 /* XXX we may want a better hash function */
266 flow_id_hash(struct ipfw_flow_id *id)
270 if (IS_IP6_FLOW_ID(id)) {
271 uint32_t *d = (uint32_t *)&id->dst_ip6;
272 uint32_t *s = (uint32_t *)&id->src_ip6;
273 i = (d[0] ) ^ (d[1]) ^
275 (d[0] >> 15) ^ (d[1] >> 15) ^
276 (d[2] >> 15) ^ (d[3] >> 15) ^
277 (s[0] << 1) ^ (s[1] << 1) ^
278 (s[2] << 1) ^ (s[3] << 1) ^
279 (s[0] << 16) ^ (s[1] << 16) ^
280 (s[2] << 16) ^ (s[3] << 16) ^
281 (id->dst_port << 1) ^ (id->src_port) ^
283 (id->proto ) ^ (id->flow_id6);
285 i = (id->dst_ip) ^ (id->dst_ip >> 15) ^
286 (id->src_ip << 1) ^ (id->src_ip >> 16) ^
288 (id->dst_port << 1) ^ (id->src_port) ^ (id->proto);
293 /* Like bcmp, returns 0 if ids match, 1 otherwise. */
295 flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
297 int is_v6 = IS_IP6_FLOW_ID(id1);
300 if (IS_IP6_FLOW_ID(id2))
301 return 1; /* different address families */
303 return (id1->dst_ip == id2->dst_ip &&
304 id1->src_ip == id2->src_ip &&
305 id1->dst_port == id2->dst_port &&
306 id1->src_port == id2->src_port &&
307 id1->proto == id2->proto &&
308 id1->extra == id2->extra) ? 0 : 1;
312 !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
313 !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
314 id1->dst_port == id2->dst_port &&
315 id1->src_port == id2->src_port &&
316 id1->proto == id2->proto &&
317 id1->extra == id2->extra &&
318 id1->flow_id6 == id2->flow_id6) ? 0 : 1;
320 /*--------- end of flow-id mask, hash and compare ---------*/
322 /*--- support functions for the qht hashtable ----
323 * Entries are hashed by flow-id
326 q_hash(uintptr_t key, int flags, void *arg)
328 /* compute the hash slot from the flow id */
329 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
330 &((struct dn_queue *)key)->ni.fid :
331 (struct ipfw_flow_id *)key;
333 return flow_id_hash(id);
337 q_match(void *obj, uintptr_t key, int flags, void *arg)
339 struct dn_queue *o = (struct dn_queue *)obj;
340 struct ipfw_flow_id *id2;
342 if (flags & DNHT_KEY_IS_OBJ) {
343 /* compare pointers */
344 id2 = &((struct dn_queue *)key)->ni.fid;
346 id2 = (struct ipfw_flow_id *)key;
348 return (0 == flow_id_cmp(&o->ni.fid, id2));
352 * create a new queue instance for the given 'key'.
355 q_new(uintptr_t key, int flags, void *arg)
357 struct dn_queue *q, *template = arg;
358 struct dn_fsk *fs = template->fs;
359 int size = sizeof(*q) + fs->sched->fp->q_datalen;
361 q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
363 D("no memory for new queue");
367 set_oid(&q->ni.oid, DN_QUEUE, size);
368 if (fs->fs.flags & DN_QHT_HASH)
369 q->ni.fid = *(struct ipfw_flow_id *)key;
371 q->_si = template->_si;
374 if (fs->sched->fp->new_queue)
375 fs->sched->fp->new_queue(q);
378 /* call AQM init function after creating a queue*/
379 if (fs->aqmfp && fs->aqmfp->init)
380 if(fs->aqmfp->init(q))
381 D("unable to init AQM for fs %d", fs->fs.fs_nr);
383 V_dn_cfg.queue_count++;
389 * Notify schedulers that a queue is going away.
390 * If (flags & DN_DESTROY), also free the packets.
391 * The version for callbacks is called q_delete_cb().
394 dn_delete_queue(struct dn_queue *q, int flags)
396 struct dn_fsk *fs = q->fs;
399 /* clean up AQM status for queue 'q'
400 * cleanup here is called just with MULTIQUEUE
402 if (fs && fs->aqmfp && fs->aqmfp->cleanup)
403 fs->aqmfp->cleanup(q);
405 // D("fs %p si %p\n", fs, q->_si);
406 /* notify the parent scheduler that the queue is going away */
407 if (fs && fs->sched->fp->free_queue)
408 fs->sched->fp->free_queue(q);
411 if (flags & DN_DESTROY) {
413 dn_free_pkts(q->mq.head);
414 bzero(q, sizeof(*q)); // safety
416 V_dn_cfg.queue_count--;
421 q_delete_cb(void *q, void *arg)
423 int flags = (int)(uintptr_t)arg;
424 dn_delete_queue(q, flags);
425 return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
429 * calls dn_delete_queue/q_delete_cb on all queues,
430 * which notifies the parent scheduler and possibly drains packets.
431 * flags & DN_DESTROY: drains queues and destroy qht;
434 qht_delete(struct dn_fsk *fs, int flags)
436 ND("fs %d start flags %d qht %p",
437 fs->fs.fs_nr, flags, fs->qht);
440 if (fs->fs.flags & DN_QHT_HASH) {
441 dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
442 if (flags & DN_DESTROY) {
443 dn_ht_free(fs->qht, 0);
447 dn_delete_queue((struct dn_queue *)(fs->qht), flags);
448 if (flags & DN_DESTROY)
454 * Find and possibly create the queue for a MULTIQUEUE scheduler.
455 * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
458 ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
459 struct ipfw_flow_id *id)
461 struct dn_queue template;
466 if (fs->fs.flags & DN_QHT_HASH) {
467 struct ipfw_flow_id masked_id;
468 if (fs->qht == NULL) {
469 fs->qht = dn_ht_init(NULL, fs->fs.buckets,
470 offsetof(struct dn_queue, q_next),
471 q_hash, q_match, q_new);
476 flow_id_mask(&fs->fsk_mask, &masked_id);
477 return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
478 DNHT_INSERT, &template);
481 fs->qht = q_new(0, 0, &template);
482 return (struct dn_queue *)fs->qht;
485 /*--- end of queue hash table ---*/
487 /*--- support functions for the sch_inst hashtable ----
489 * These are hashed by flow-id
492 si_hash(uintptr_t key, int flags, void *arg)
494 /* compute the hash slot from the flow id */
495 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
496 &((struct dn_sch_inst *)key)->ni.fid :
497 (struct ipfw_flow_id *)key;
499 return flow_id_hash(id);
503 si_match(void *obj, uintptr_t key, int flags, void *arg)
505 struct dn_sch_inst *o = obj;
506 struct ipfw_flow_id *id2;
508 id2 = (flags & DNHT_KEY_IS_OBJ) ?
509 &((struct dn_sch_inst *)key)->ni.fid :
510 (struct ipfw_flow_id *)key;
511 return flow_id_cmp(&o->ni.fid, id2) == 0;
515 * create a new instance for the given 'key'
516 * Allocate memory for instance, delay line and scheduler private data.
519 si_new(uintptr_t key, int flags, void *arg)
521 struct dn_schk *s = arg;
522 struct dn_sch_inst *si;
523 int l = sizeof(*si) + s->fp->si_datalen;
525 si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
529 /* Set length only for the part passed up to userland. */
530 set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
531 set_oid(&(si->dline.oid), DN_DELAY_LINE,
532 sizeof(struct delay_line));
533 /* mark si and dline as outside the event queue */
534 si->ni.oid.id = si->dline.oid.id = -1;
539 if (s->fp->new_sched && s->fp->new_sched(si)) {
540 D("new_sched error");
543 if (s->sch.flags & DN_HAVE_MASK)
544 si->ni.fid = *(struct ipfw_flow_id *)key;
547 /* init AQM status for !DN_MULTIQUEUE sched*/
548 if (!(s->fp->flags & DN_MULTIQUEUE))
549 if (s->fs->aqmfp && s->fs->aqmfp->init)
550 if(s->fs->aqmfp->init((struct dn_queue *)(si + 1))) {
551 D("unable to init AQM for fs %d", s->fs->fs.fs_nr);
561 bzero(si, sizeof(*si)); // safety
562 free(si, M_DUMMYNET);
568 * Callback from siht to delete all scheduler instances. Remove
569 * si and delay line from the system heap, destroy all queues.
570 * We assume that all flowset have been notified and do not
571 * point to us anymore.
574 si_destroy(void *_si, void *arg)
576 struct dn_sch_inst *si = _si;
577 struct dn_schk *s = si->sched;
578 struct delay_line *dl = &si->dline;
580 if (dl->oid.subtype) /* remove delay line from event heap */
581 heap_extract(&V_dn_cfg.evheap, dl);
582 dn_free_pkts(dl->mq.head); /* drain delay line */
583 if (si->kflags & DN_ACTIVE) /* remove si from event heap */
584 heap_extract(&V_dn_cfg.evheap, si);
587 /* clean up AQM status for !DN_MULTIQUEUE sched
588 * Note that all queues belong to fs were cleaned up in fsk_detach.
589 * When drain_scheduler is called s->fs and q->fs are pointing
590 * to a correct fs, so we can use fs in this case.
592 if (!(s->fp->flags & DN_MULTIQUEUE)) {
593 struct dn_queue *q = (struct dn_queue *)(si + 1);
594 if (q->aqm_status && q->fs->aqmfp)
595 if (q->fs->aqmfp->cleanup)
596 q->fs->aqmfp->cleanup(q);
599 if (s->fp->free_sched)
600 s->fp->free_sched(si);
601 bzero(si, sizeof(*si)); /* safety */
602 free(si, M_DUMMYNET);
604 return DNHT_SCAN_DEL;
608 * Find the scheduler instance for this packet. If we need to apply
609 * a mask, do on a local copy of the flow_id to preserve the original.
610 * Assume siht is always initialized if we have a mask.
613 ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
616 if (s->sch.flags & DN_HAVE_MASK) {
617 struct ipfw_flow_id id_t = *id;
618 flow_id_mask(&s->sch.sched_mask, &id_t);
619 return dn_ht_find(s->siht, (uintptr_t)&id_t,
623 s->siht = si_new(0, 0, s);
624 return (struct dn_sch_inst *)s->siht;
627 /* callback to flush credit for the scheduler instance */
629 si_reset_credit(void *_si, void *arg)
631 struct dn_sch_inst *si = _si;
632 struct dn_link *p = &si->sched->link;
634 si->credit = p->burst + (V_dn_cfg.io_fast ? p->bandwidth : 0);
639 schk_reset_credit(struct dn_schk *s)
641 if (s->sch.flags & DN_HAVE_MASK)
642 dn_ht_scan(s->siht, si_reset_credit, NULL);
644 si_reset_credit(s->siht, NULL);
646 /*---- end of sch_inst hashtable ---------------------*/
648 /*-------------------------------------------------------
649 * flowset hash (fshash) support. Entries are hashed by fs_nr.
650 * New allocations are put in the fsunlinked list, from which
651 * they are removed when they point to a specific scheduler.
654 fsk_hash(uintptr_t key, int flags, void *arg)
656 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
657 ((struct dn_fsk *)key)->fs.fs_nr;
659 return ( (i>>8)^(i>>4)^i );
663 fsk_match(void *obj, uintptr_t key, int flags, void *arg)
665 struct dn_fsk *fs = obj;
666 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
667 ((struct dn_fsk *)key)->fs.fs_nr;
669 return (fs->fs.fs_nr == i);
673 fsk_new(uintptr_t key, int flags, void *arg)
677 fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
679 set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
680 V_dn_cfg.fsk_count++;
681 fs->drain_bucket = 0;
682 SLIST_INSERT_HEAD(&V_dn_cfg.fsu, fs, sch_chain);
688 /* callback function for cleaning up AQM queue status belongs to a flowset
689 * connected to scheduler instance '_si' (for !DN_MULTIQUEUE only).
692 si_cleanup_q(void *_si, void *arg)
694 struct dn_sch_inst *si = _si;
696 if (!(si->sched->fp->flags & DN_MULTIQUEUE)) {
697 if (si->sched->fs->aqmfp && si->sched->fs->aqmfp->cleanup)
698 si->sched->fs->aqmfp->cleanup((struct dn_queue *) (si+1));
703 /* callback to clean up queue AQM status.*/
705 q_cleanup_q(void *_q, void *arg)
707 struct dn_queue *q = _q;
708 q->fs->aqmfp->cleanup(q);
712 /* Clean up all AQM queues status belongs to flowset 'fs' and then
713 * deconfig AQM for flowset 'fs'
716 aqm_cleanup_deconfig_fs(struct dn_fsk *fs)
718 struct dn_sch_inst *si;
720 /* clean up AQM status for all queues for !DN_MULTIQUEUE sched*/
721 if (fs->fs.fs_nr > DN_MAX_ID) {
722 if (fs->sched && !(fs->sched->fp->flags & DN_MULTIQUEUE)) {
723 if (fs->sched->sch.flags & DN_HAVE_MASK)
724 dn_ht_scan(fs->sched->siht, si_cleanup_q, NULL);
726 /* single si i.e. no sched mask */
727 si = (struct dn_sch_inst *) fs->sched->siht;
728 if (si && fs->aqmfp && fs->aqmfp->cleanup)
729 fs->aqmfp->cleanup((struct dn_queue *) (si+1));
734 /* clean up AQM status for all queues for DN_MULTIQUEUE sched*/
735 if (fs->sched && fs->sched->fp->flags & DN_MULTIQUEUE && fs->qht) {
736 if (fs->fs.flags & DN_QHT_HASH)
737 dn_ht_scan(fs->qht, q_cleanup_q, NULL);
739 fs->aqmfp->cleanup((struct dn_queue *)(fs->qht));
743 if(fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig)
744 fs->aqmfp->deconfig(fs);
749 * detach flowset from its current scheduler. Flags as follows:
750 * DN_DETACH removes from the fsk_list
751 * DN_DESTROY deletes individual queues
752 * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
755 fsk_detach(struct dn_fsk *fs, int flags)
757 if (flags & DN_DELETE_FS)
759 ND("fs %d from sched %d flags %s %s %s",
760 fs->fs.fs_nr, fs->fs.sched_nr,
761 (flags & DN_DELETE_FS) ? "DEL_FS":"",
762 (flags & DN_DESTROY) ? "DEL":"",
763 (flags & DN_DETACH) ? "DET":"");
764 if (flags & DN_DETACH) { /* detach from the list */
765 struct dn_fsk_head *h;
766 h = fs->sched ? &fs->sched->fsk_list : &V_dn_cfg.fsu;
767 SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
769 /* Free the RED parameters, they will be recomputed on
770 * subsequent attach if needed.
772 free(fs->w_q_lookup, M_DUMMYNET);
773 fs->w_q_lookup = NULL;
774 qht_delete(fs, flags);
776 aqm_cleanup_deconfig_fs(fs);
779 if (fs->sched && fs->sched->fp->free_fsk)
780 fs->sched->fp->free_fsk(fs);
782 if (flags & DN_DELETE_FS) {
783 bzero(fs, sizeof(*fs)); /* safety */
784 free(fs, M_DUMMYNET);
785 V_dn_cfg.fsk_count--;
787 SLIST_INSERT_HEAD(&V_dn_cfg.fsu, fs, sch_chain);
792 * Detach or destroy all flowsets in a list.
793 * flags specifies what to do:
794 * DN_DESTROY: flush all queues
795 * DN_DELETE_FS: DN_DESTROY + destroy flowset
796 * DN_DELETE_FS implies DN_DESTROY
799 fsk_detach_list(struct dn_fsk_head *h, int flags)
802 int n = 0; /* only for stats */
804 ND("head %p flags %x", h, flags);
805 while ((fs = SLIST_FIRST(h))) {
806 SLIST_REMOVE_HEAD(h, sch_chain);
808 fsk_detach(fs, flags);
810 ND("done %d flowsets", n);
814 * called on 'queue X delete' -- removes the flowset from fshash,
815 * deletes all queues for the flowset, and removes the flowset.
818 delete_fs(int i, int locked)
825 fs = dn_ht_find(V_dn_cfg.fshash, i, DNHT_REMOVE, NULL);
826 ND("fs %d found %p", i, fs);
828 fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
837 /*----- end of flowset hashtable support -------------*/
839 /*------------------------------------------------------------
840 * Scheduler hash. When searching by index we pass sched_nr,
841 * otherwise we pass struct dn_sch * which is the first field in
842 * struct dn_schk so we can cast between the two. We use this trick
843 * because in the create phase (but it should be fixed).
846 schk_hash(uintptr_t key, int flags, void *_arg)
848 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
849 ((struct dn_schk *)key)->sch.sched_nr;
850 return ( (i>>8)^(i>>4)^i );
854 schk_match(void *obj, uintptr_t key, int flags, void *_arg)
856 struct dn_schk *s = (struct dn_schk *)obj;
857 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
858 ((struct dn_schk *)key)->sch.sched_nr;
859 return (s->sch.sched_nr == i);
863 * Create the entry and intialize with the sched hash if needed.
864 * Leave s->fp unset so we can tell whether a dn_ht_find() returns
865 * a new object or a previously existing one.
868 schk_new(uintptr_t key, int flags, void *arg)
870 struct schk_new_arg *a = arg;
872 int l = sizeof(*s) +a->fp->schk_datalen;
874 s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
877 set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
878 s->sch = *a->sch; // copy initial values
879 s->link.link_nr = s->sch.sched_nr;
880 SLIST_INIT(&s->fsk_list);
881 /* initialize the hash table or create the single instance */
882 s->fp = a->fp; /* si_new needs this */
884 if (s->sch.flags & DN_HAVE_MASK) {
885 s->siht = dn_ht_init(NULL, s->sch.buckets,
886 offsetof(struct dn_sch_inst, si_next),
887 si_hash, si_match, si_new);
888 if (s->siht == NULL) {
893 s->fp = NULL; /* mark as a new scheduler */
894 V_dn_cfg.schk_count++;
899 * Callback for sched delete. Notify all attached flowsets to
900 * detach from the scheduler, destroy the internal flowset, and
901 * all instances. The scheduler goes away too.
902 * arg is 0 (only detach flowsets and destroy instances)
903 * DN_DESTROY (detach & delete queues, delete schk)
904 * or DN_DELETE_FS (delete queues and flowsets, delete schk)
907 schk_delete_cb(void *obj, void *arg)
909 struct dn_schk *s = obj;
912 ND("sched %d arg %s%s",
914 a&DN_DESTROY ? "DEL ":"",
915 a&DN_DELETE_FS ? "DEL_FS":"");
917 fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
918 /* no more flowset pointing to us now */
919 if (s->sch.flags & DN_HAVE_MASK) {
920 dn_ht_scan(s->siht, si_destroy, NULL);
921 dn_ht_free(s->siht, 0);
923 si_destroy(s->siht, NULL);
925 free(s->profile, M_DUMMYNET);
930 bzero(s, sizeof(*s)); // safety
931 free(obj, M_DUMMYNET);
932 V_dn_cfg.schk_count--;
933 return DNHT_SCAN_DEL;
937 * called on a 'sched X delete' command. Deletes a single scheduler.
938 * This is done by removing from the schedhash, unlinking all
939 * flowsets and deleting their traffic.
946 s = dn_ht_find(V_dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
950 delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
951 /* then detach flowsets, delete traffic */
952 schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
955 /*--- end of schk hashtable support ---*/
958 copy_obj(char **start, char *end, void *_o, const char *msg, int i)
965 int have = end - *start;
967 memcpy(&o, _o, sizeof(o));
968 if (have < o.len || o.len == 0 || o.type == 0) {
969 D("(WARN) type %d %s %d have %d need %d",
970 o.type, msg, i, have, o.len);
973 ND("type %d %s %d len %d", o.type, msg, i, o.len);
974 if (o.type == DN_LINK) {
975 memcpy(&dn.l, _o, sizeof(dn.l));
976 /* Adjust burst parameter for link */
977 dn.l.burst = div64(dn.l.burst, 8 * hz);
978 dn.l.delay = dn.l.delay * 1000 / hz;
979 memcpy(*start, &dn.l, sizeof(dn.l));
980 } else if (o.type == DN_SCH) {
981 /* Set dn.s.sch.oid.id to the number of instances */
982 memcpy(&dn.s, _o, sizeof(dn.s));
983 dn.s.sch.oid.id = (dn.s.sch.flags & DN_HAVE_MASK) ?
984 dn_ht_entries(dn.s.siht) : (dn.s.siht ? 1 : 0);
985 memcpy(*start, &dn.s, sizeof(dn.s));
987 memcpy(*start, _o, o.len);
992 /* Specific function to copy a queue.
993 * Copies only the user-visible part of a queue (which is in
994 * a struct dn_flow), and sets len accordingly.
997 copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
999 struct dn_id *o = _o;
1000 int have = end - *start;
1001 int len = sizeof(struct dn_flow); /* see above comment */
1003 if (have < len || o->len == 0 || o->type != DN_QUEUE) {
1004 D("ERROR type %d %s %d have %d need %d",
1005 o->type, msg, i, have, len);
1008 ND("type %d %s %d len %d", o->type, msg, i, len);
1009 memcpy(*start, _o, len);
1010 ((struct dn_id*)(*start))->len = len;
1016 copy_q_cb(void *obj, void *arg)
1018 struct dn_queue *q = obj;
1019 struct copy_args *a = arg;
1020 struct dn_flow *ni = (struct dn_flow *)(*a->start);
1021 if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
1022 return DNHT_SCAN_END;
1023 ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
1024 ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
1029 copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
1033 if (fs->fs.flags & DN_QHT_HASH)
1034 dn_ht_scan(fs->qht, copy_q_cb, a);
1036 copy_q_cb(fs->qht, a);
1041 * This routine only copies the initial part of a profile ? XXX
1044 copy_profile(struct copy_args *a, struct dn_profile *p)
1046 int have = a->end - *a->start;
1047 /* XXX here we check for max length */
1048 int profile_len = sizeof(struct dn_profile) -
1049 ED_MAX_SAMPLES_NO*sizeof(int);
1053 if (have < profile_len) {
1054 D("error have %d need %d", have, profile_len);
1057 memcpy(*a->start, p, profile_len);
1058 ((struct dn_id *)(*a->start))->len = profile_len;
1059 *a->start += profile_len;
1064 copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
1066 struct dn_fs *ufs = (struct dn_fs *)(*a->start);
1069 ND("flowset %d", fs->fs.fs_nr);
1070 if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
1071 return DNHT_SCAN_END;
1072 ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
1073 dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
1074 if (flags) { /* copy queues */
1081 copy_si_cb(void *obj, void *arg)
1083 struct dn_sch_inst *si = obj;
1084 struct copy_args *a = arg;
1085 struct dn_flow *ni = (struct dn_flow *)(*a->start);
1086 if (copy_obj(a->start, a->end, &si->ni, "inst",
1087 si->sched->sch.sched_nr))
1088 return DNHT_SCAN_END;
1089 ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
1090 ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
1095 copy_si(struct copy_args *a, struct dn_schk *s, int flags)
1097 if (s->sch.flags & DN_HAVE_MASK)
1098 dn_ht_scan(s->siht, copy_si_cb, a);
1100 copy_si_cb(s->siht, a);
1105 * compute a list of children of a scheduler and copy up
1108 copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
1114 int n = 0, space = sizeof(*o);
1115 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1116 if (fs->fs.fs_nr < DN_MAX_ID)
1119 space += n * sizeof(uint32_t);
1120 DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
1121 if (a->end - *(a->start) < space)
1122 return DNHT_SCAN_END;
1123 o = (struct dn_id *)(*(a->start));
1125 *a->start += o->len;
1127 p = (uint32_t *)(o+1);
1128 SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
1129 if (fs->fs.fs_nr < DN_MAX_ID)
1130 *p++ = fs->fs.fs_nr;
1135 copy_data_helper(void *_o, void *_arg)
1137 struct copy_args *a = _arg;
1138 uint32_t *r = a->extra->r; /* start of first range */
1139 uint32_t *lim; /* first invalid pointer */
1142 lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
1144 if (a->type == DN_LINK || a->type == DN_SCH) {
1145 /* pipe|sched show, we receive a dn_schk */
1146 struct dn_schk *s = _o;
1148 n = s->sch.sched_nr;
1149 if (a->type == DN_SCH && n >= DN_MAX_ID)
1150 return 0; /* not a scheduler */
1151 if (a->type == DN_LINK && n <= DN_MAX_ID)
1152 return 0; /* not a pipe */
1154 /* see if the object is within one of our ranges */
1155 for (;r < lim; r += 2) {
1156 if (n < r[0] || n > r[1])
1158 /* Found a valid entry, copy and we are done */
1159 if (a->flags & DN_C_LINK) {
1160 if (copy_obj(a->start, a->end,
1161 &s->link, "link", n))
1162 return DNHT_SCAN_END;
1163 if (copy_profile(a, s->profile))
1164 return DNHT_SCAN_END;
1165 if (copy_flowset(a, s->fs, 0))
1166 return DNHT_SCAN_END;
1168 if (a->flags & DN_C_SCH) {
1169 if (copy_obj(a->start, a->end,
1170 &s->sch, "sched", n))
1171 return DNHT_SCAN_END;
1172 /* list all attached flowsets */
1173 if (copy_fsk_list(a, s, 0))
1174 return DNHT_SCAN_END;
1176 if (a->flags & DN_C_FLOW)
1180 } else if (a->type == DN_FS) {
1181 /* queue show, skip internal flowsets */
1182 struct dn_fsk *fs = _o;
1187 /* see if the object is within one of our ranges */
1188 for (;r < lim; r += 2) {
1189 if (n < r[0] || n > r[1])
1191 if (copy_flowset(a, fs, 0))
1192 return DNHT_SCAN_END;
1194 break; /* we are done */
1200 static inline struct dn_schk *
1201 locate_scheduler(int i)
1203 return dn_ht_find(V_dn_cfg.schedhash, i, 0, NULL);
1207 * red parameters are in fixed point arithmetic.
1210 config_red(struct dn_fsk *fs)
1212 int64_t s, idle, weight, w0;
1215 fs->w_q = fs->fs.w_q;
1216 fs->max_p = fs->fs.max_p;
1218 /* Doing stuff that was in userland */
1219 i = fs->sched->link.bandwidth;
1221 hz * V_dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
1223 idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
1224 fs->lookup_step = div64(idle , V_dn_cfg.red_lookup_depth);
1225 /* fs->lookup_step not scaled, */
1226 if (!fs->lookup_step)
1227 fs->lookup_step = 1;
1228 w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
1230 for (t = fs->lookup_step; t > 1; --t)
1231 weight = SCALE_MUL(weight, w0);
1232 fs->lookup_weight = (int)(weight); // scaled
1234 /* Now doing stuff that was in kerneland */
1235 fs->min_th = SCALE(fs->fs.min_th);
1236 fs->max_th = SCALE(fs->fs.max_th);
1238 if (fs->fs.max_th == fs->fs.min_th)
1239 fs->c_1 = fs->max_p;
1241 fs->c_1 = SCALE((int64_t)(fs->max_p)) / (fs->fs.max_th - fs->fs.min_th);
1242 fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
1244 if (fs->fs.flags & DN_IS_GENTLE_RED) {
1245 fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
1246 fs->c_4 = SCALE(1) - 2 * fs->max_p;
1249 /* If the lookup table already exist, free and create it again. */
1250 free(fs->w_q_lookup, M_DUMMYNET);
1251 fs->w_q_lookup = NULL;
1252 if (V_dn_cfg.red_lookup_depth == 0) {
1253 printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
1255 fs->fs.flags &= ~DN_IS_RED;
1256 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1259 fs->lookup_depth = V_dn_cfg.red_lookup_depth;
1260 fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
1261 M_DUMMYNET, M_NOWAIT);
1262 if (fs->w_q_lookup == NULL) {
1263 printf("dummynet: sorry, cannot allocate red lookup table\n");
1264 fs->fs.flags &= ~DN_IS_RED;
1265 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1269 /* Fill the lookup table with (1 - w_q)^x */
1270 fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
1272 for (i = 1; i < fs->lookup_depth; i++)
1274 SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
1276 if (V_dn_cfg.red_avg_pkt_size < 1)
1277 V_dn_cfg.red_avg_pkt_size = 512;
1278 fs->avg_pkt_size = V_dn_cfg.red_avg_pkt_size;
1279 if (V_dn_cfg.red_max_pkt_size < 1)
1280 V_dn_cfg.red_max_pkt_size = 1500;
1281 fs->max_pkt_size = V_dn_cfg.red_max_pkt_size;
1286 /* Scan all flowset attached to this scheduler and update red */
1288 update_red(struct dn_schk *s)
1291 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1292 if (fs && (fs->fs.flags & DN_IS_RED))
1297 /* attach flowset to scheduler s, possibly requeue */
1299 fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
1301 ND("remove fs %d from fsunlinked, link to sched %d",
1302 fs->fs.fs_nr, s->sch.sched_nr);
1303 SLIST_REMOVE(&V_dn_cfg.fsu, fs, dn_fsk, sch_chain);
1305 SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
1308 /* XXX compute fsk_mask */
1309 fs->fsk_mask = fs->fs.flow_mask;
1310 if (fs->sched->sch.flags & DN_HAVE_MASK)
1311 flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
1314 * we must drain qht according to the old
1315 * type, and reinsert according to the new one.
1316 * The requeue is complex -- in general we need to
1317 * reclassify every single packet.
1318 * For the time being, let's hope qht is never set
1319 * when we reach this point.
1321 D("XXX TODO requeue from fs %d to sch %d",
1322 fs->fs.fs_nr, s->sch.sched_nr);
1325 /* set the new type for qht */
1326 if (nonzero_mask(&fs->fsk_mask))
1327 fs->fs.flags |= DN_QHT_HASH;
1329 fs->fs.flags &= ~DN_QHT_HASH;
1331 /* XXX config_red() can fail... */
1332 if (fs->fs.flags & DN_IS_RED)
1336 /* update all flowsets which may refer to this scheduler */
1338 update_fs(struct dn_schk *s)
1340 struct dn_fsk *fs, *tmp;
1342 SLIST_FOREACH_SAFE(fs, &V_dn_cfg.fsu, sch_chain, tmp) {
1343 if (s->sch.sched_nr != fs->fs.sched_nr) {
1344 D("fs %d for sch %d not %d still unlinked",
1345 fs->fs.fs_nr, fs->fs.sched_nr,
1354 /* Retrieve AQM configurations to ipfw userland
1357 get_aqm_parms(struct sockopt *sopt)
1359 struct dn_extra_parms *ep;
1361 size_t sopt_valsize;
1364 sopt_valsize = sopt->sopt_valsize;
1366 if (sopt->sopt_valsize < l) {
1367 D("bad len sopt->sopt_valsize %d len %d",
1368 (int) sopt->sopt_valsize , l);
1372 ep = malloc(l, M_DUMMYNET, M_NOWAIT);
1378 err = sooptcopyin(sopt, ep, l, l);
1381 sopt->sopt_valsize = sopt_valsize;
1382 if (ep->oid.len < l) {
1387 fs = dn_ht_find(V_dn_cfg.fshash, ep->nr, 0, NULL);
1389 D("fs %d not found", ep->nr);
1394 if (fs->aqmfp && fs->aqmfp->getconfig) {
1395 if(fs->aqmfp->getconfig(fs, ep)) {
1396 D("Error while trying to get AQM params");
1401 err = sooptcopyout(sopt, ep, l);
1405 free(ep, M_DUMMYNET);
1409 /* Retrieve AQM configurations to ipfw userland
1412 get_sched_parms(struct sockopt *sopt)
1414 struct dn_extra_parms *ep;
1415 struct dn_schk *schk;
1416 size_t sopt_valsize;
1419 sopt_valsize = sopt->sopt_valsize;
1421 if (sopt->sopt_valsize < l) {
1422 D("bad len sopt->sopt_valsize %d len %d",
1423 (int) sopt->sopt_valsize , l);
1427 ep = malloc(l, M_DUMMYNET, M_NOWAIT);
1433 err = sooptcopyin(sopt, ep, l, l);
1436 sopt->sopt_valsize = sopt_valsize;
1437 if (ep->oid.len < l) {
1442 schk = locate_scheduler(ep->nr);
1444 D("sched %d not found", ep->nr);
1449 if (schk->fp && schk->fp->getconfig) {
1450 if(schk->fp->getconfig(schk, ep)) {
1451 D("Error while trying to get sched params");
1456 err = sooptcopyout(sopt, ep, l);
1459 free(ep, M_DUMMYNET);
1464 /* Configure AQM for flowset 'fs'.
1465 * extra parameters are passed from userland.
1468 config_aqm(struct dn_fsk *fs, struct dn_extra_parms *ep, int busy)
1475 /* no configurations */
1481 /* no AQM for this flowset*/
1482 if (!strcmp(ep->name,"")) {
1486 if (ep->oid.len < sizeof(*ep)) {
1487 D("short aqm len %d", ep->oid.len);
1493 D("Unable to configure flowset, flowset busy!");
1498 /* deconfigure old aqm if exist */
1499 if (fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig) {
1500 aqm_cleanup_deconfig_fs(fs);
1503 if (!(fs->aqmfp = find_aqm_type(0, ep->name))) {
1504 D("AQM functions not found for type %s!", ep->name);
1505 fs->fs.flags &= ~DN_IS_AQM;
1509 fs->fs.flags |= DN_IS_AQM;
1511 if (ep->oid.subtype != DN_AQM_PARAMS) {
1517 if (fs->aqmfp->config) {
1518 err = fs->aqmfp->config(fs, ep, ep->oid.len);
1520 D("Unable to configure AQM for FS %d", fs->fs.fs_nr );
1521 fs->fs.flags &= ~DN_IS_AQM;
1533 * Configuration -- to preserve backward compatibility we use
1534 * the following scheme (N is 65536)
1535 * NUMBER SCHED LINK FLOWSET
1536 * 1 .. N-1 (1)WFQ (2)WFQ (3)queue
1537 * N+1 .. 2N-1 (4)FIFO (5)FIFO (6)FIFO for sched 1..N-1
1538 * 2N+1 .. 3N-1 -- -- (7)FIFO for sched N+1..2N-1
1540 * "pipe i config" configures #1, #2 and #3
1541 * "sched i config" configures #1 and possibly #6
1542 * "queue i config" configures #3
1543 * #1 is configured with 'pipe i config' or 'sched i config'
1544 * #2 is configured with 'pipe i config', and created if not
1545 * existing with 'sched i config'
1546 * #3 is configured with 'queue i config'
1547 * #4 is automatically configured after #1, can only be FIFO
1548 * #5 is automatically configured after #2
1549 * #6 is automatically created when #1 is !MULTIQUEUE,
1550 * and can be updated.
1551 * #7 is automatically configured after #2
1555 * configure a link (and its FIFO instance)
1558 config_link(struct dn_link *p, struct dn_id *arg)
1562 if (p->oid.len != sizeof(*p)) {
1563 D("invalid pipe len %d", p->oid.len);
1567 if (i <= 0 || i >= DN_MAX_ID)
1570 * The config program passes parameters as follows:
1571 * bw = bits/second (0 means no limits),
1572 * delay = ms, must be translated into ticks.
1573 * qsize = slots/bytes
1576 p->delay = (p->delay * hz) / 1000;
1577 /* Scale burst size: bytes -> bits * hz */
1581 /* do it twice, base link and FIFO link */
1582 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1583 struct dn_schk *s = locate_scheduler(i);
1586 D("sched %d not found", i);
1589 /* remove profile if exists */
1590 free(s->profile, M_DUMMYNET);
1593 /* copy all parameters */
1594 s->link.oid = p->oid;
1595 s->link.link_nr = i;
1596 s->link.delay = p->delay;
1597 if (s->link.bandwidth != p->bandwidth) {
1598 /* XXX bandwidth changes, need to update red params */
1599 s->link.bandwidth = p->bandwidth;
1602 s->link.burst = p->burst;
1603 schk_reset_credit(s);
1611 * configure a flowset. Can be called from inside with locked=1,
1613 static struct dn_fsk *
1614 config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
1619 struct dn_extra_parms *ep;
1622 if (nfs->oid.len != sizeof(*nfs)) {
1623 D("invalid flowset len %d", nfs->oid.len);
1627 if (i <= 0 || i >= 3*DN_MAX_ID)
1632 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
1635 memcpy(ep, arg, sizeof(*ep));
1638 ND("flowset %d", i);
1639 /* XXX other sanity checks */
1640 if (nfs->flags & DN_QSIZE_BYTES) {
1641 ipdn_bound_var(&nfs->qsize, 16384,
1642 1500, V_dn_cfg.byte_limit, NULL); // "queue byte size");
1644 ipdn_bound_var(&nfs->qsize, 50,
1645 1, V_dn_cfg.slot_limit, NULL); // "queue slot size");
1647 if (nfs->flags & DN_HAVE_MASK) {
1648 /* make sure we have some buckets */
1649 ipdn_bound_var((int *)&nfs->buckets, V_dn_cfg.hash_size,
1650 1, V_dn_cfg.max_hash_size, "flowset buckets");
1652 nfs->buckets = 1; /* we only need 1 */
1656 do { /* exit with break when done */
1658 int flags = nfs->sched_nr ? DNHT_INSERT : 0;
1660 int oldc = V_dn_cfg.fsk_count;
1661 fs = dn_ht_find(V_dn_cfg.fshash, i, flags, NULL);
1663 D("missing sched for flowset %d", i);
1666 /* grab some defaults from the existing one */
1667 if (nfs->sched_nr == 0) /* reuse */
1668 nfs->sched_nr = fs->fs.sched_nr;
1669 for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
1670 if (nfs->par[j] == -1) /* reuse */
1671 nfs->par[j] = fs->fs.par[j];
1673 if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
1674 ND("flowset %d unchanged", i);
1678 * Reconfigure AQM as the parameters can be changed.
1679 * We consider the flowset as busy if it has scheduler
1682 s = locate_scheduler(nfs->sched_nr);
1683 config_aqm(fs, ep, s != NULL && s->siht != NULL);
1686 break; /* no change, nothing to do */
1688 if (oldc != V_dn_cfg.fsk_count) /* new item */
1690 s = locate_scheduler(nfs->sched_nr);
1691 /* detach from old scheduler if needed, preserving
1692 * queues if we need to reattach. Then update the
1693 * configuration, and possibly attach to the new sched.
1695 DX(2, "fs %d changed sched %d@%p to %d@%p",
1697 fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
1699 int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
1700 flags |= DN_DESTROY; /* XXX temporary */
1701 fsk_detach(fs, flags);
1703 fs->fs = *nfs; /* copy configuration */
1707 config_aqm(fs, ep, s != NULL &&
1722 * config/reconfig a scheduler and its FIFO variant.
1723 * For !MULTIQUEUE schedulers, also set up the flowset.
1725 * On reconfigurations (detected because s->fp is set),
1726 * detach existing flowsets preserving traffic, preserve link,
1727 * and delete the old scheduler creating a new one.
1730 config_sched(struct dn_sch *_nsch, struct dn_id *arg)
1733 struct schk_new_arg a; /* argument for schk_new */
1735 struct dn_link p; /* copy of oldlink */
1736 struct dn_profile *pf = NULL; /* copy of old link profile */
1737 /* Used to preserv mask parameter */
1738 struct ipfw_flow_id new_mask;
1739 int new_buckets = 0;
1747 if (a.sch->oid.len != sizeof(*a.sch)) {
1748 D("bad sched len %d", a.sch->oid.len);
1751 i = a.sch->sched_nr;
1752 if (i <= 0 || i >= DN_MAX_ID)
1754 /* make sure we have some buckets */
1755 if (a.sch->flags & DN_HAVE_MASK)
1756 ipdn_bound_var((int *)&a.sch->buckets, V_dn_cfg.hash_size,
1757 1, V_dn_cfg.max_hash_size, "sched buckets");
1758 /* XXX other sanity checks */
1759 bzero(&p, sizeof(p));
1761 pipe_cmd = a.sch->flags & DN_PIPE_CMD;
1762 a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
1764 /* Copy mask parameter */
1765 new_mask = a.sch->sched_mask;
1766 new_buckets = a.sch->buckets;
1767 new_flags = a.sch->flags;
1770 again: /* run twice, for wfq and fifo */
1772 * lookup the type. If not supplied, use the previous one
1773 * or default to WF2Q+. Otherwise, return an error.
1776 a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
1778 /* found. Lookup or create entry */
1779 s = dn_ht_find(V_dn_cfg.schedhash, i, DNHT_INSERT, &a);
1780 } else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
1781 /* No type. search existing s* or retry with WF2Q+ */
1782 s = dn_ht_find(V_dn_cfg.schedhash, i, 0, &a);
1785 /* Scheduler exists, skip to FIFO scheduler
1786 * if command was pipe config...
1791 /* New scheduler, create a wf2q+ with no mask
1792 * if command was pipe config...
1795 /* clear mask parameter */
1796 bzero(&a.sch->sched_mask, sizeof(new_mask));
1798 a.sch->flags &= ~DN_HAVE_MASK;
1800 a.sch->oid.subtype = DN_SCHED_WF2QP;
1804 D("invalid scheduler type %d %s",
1805 a.sch->oid.subtype, a.sch->name);
1809 /* normalize name and subtype */
1810 a.sch->oid.subtype = a.fp->type;
1811 bzero(a.sch->name, sizeof(a.sch->name));
1812 strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
1814 D("cannot allocate scheduler %d", i);
1817 /* restore existing link if any */
1820 if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
1821 s->profile = NULL; /* XXX maybe not needed */
1823 s->profile = malloc(sizeof(struct dn_profile),
1824 M_DUMMYNET, M_NOWAIT | M_ZERO);
1825 if (s->profile == NULL) {
1826 D("cannot allocate profile");
1829 memcpy(s->profile, pf, sizeof(*pf));
1833 if (s->fp == NULL) {
1834 DX(2, "sched %d new type %s", i, a.fp->name);
1835 } else if (s->fp != a.fp ||
1836 bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
1837 /* already existing. */
1838 DX(2, "sched %d type changed from %s to %s",
1839 i, s->fp->name, a.fp->name);
1840 DX(4, " type/sub %d/%d -> %d/%d",
1841 s->sch.oid.type, s->sch.oid.subtype,
1842 a.sch->oid.type, a.sch->oid.subtype);
1843 if (s->link.link_nr == 0)
1844 D("XXX WARNING link 0 for sched %d", i);
1845 p = s->link; /* preserve link */
1846 if (s->profile) {/* preserve profile */
1848 pf = malloc(sizeof(*pf),
1849 M_DUMMYNET, M_NOWAIT | M_ZERO);
1850 if (pf) /* XXX should issue a warning otherwise */
1851 memcpy(pf, s->profile, sizeof(*pf));
1853 /* remove from the hash */
1854 dn_ht_find(V_dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
1855 /* Detach flowsets, preserve queues. */
1856 // schk_delete_cb(s, NULL);
1857 // XXX temporarily, kill queues
1858 schk_delete_cb(s, (void *)DN_DESTROY);
1861 DX(4, "sched %d unchanged type %s", i, a.fp->name);
1863 /* complete initialization */
1867 // XXX schk_reset_credit(s);
1868 /* create the internal flowset if needed,
1869 * trying to reuse existing ones if available
1871 if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
1872 s->fs = dn_ht_find(V_dn_cfg.fshash, i, 0, NULL);
1875 bzero(&fs, sizeof(fs));
1876 set_oid(&fs.oid, DN_FS, sizeof(fs));
1877 fs.fs_nr = i + DN_MAX_ID;
1879 s->fs = config_fs(&fs, NULL, 1 /* locked */);
1882 schk_delete_cb(s, (void *)DN_DESTROY);
1883 D("error creating internal fs for %d", i);
1887 /* call init function after the flowset is created */
1892 if (i < DN_MAX_ID) { /* now configure the FIFO instance */
1895 /* Restore mask parameter for FIFO */
1896 a.sch->sched_mask = new_mask;
1897 a.sch->buckets = new_buckets;
1898 a.sch->flags = new_flags;
1900 /* sched config shouldn't modify the FIFO scheduler */
1901 if (dn_ht_find(V_dn_cfg.schedhash, i, 0, &a) != NULL) {
1902 /* FIFO already exist, don't touch it */
1903 err = 0; /* and this is not an error */
1907 a.sch->sched_nr = i;
1908 a.sch->oid.subtype = DN_SCHED_FIFO;
1909 bzero(a.sch->name, sizeof(a.sch->name));
1915 free(pf, M_DUMMYNET);
1920 * attach a profile to a link
1923 config_profile(struct dn_profile *pf, struct dn_id *arg)
1926 int i, olen, err = 0;
1928 if (pf->oid.len < sizeof(*pf)) {
1929 D("short profile len %d", pf->oid.len);
1933 if (i <= 0 || i >= DN_MAX_ID)
1935 /* XXX other sanity checks */
1937 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1938 s = locate_scheduler(i);
1946 * If we had a profile and the new one does not fit,
1947 * or it is deleted, then we need to free memory.
1949 if (s->profile && (pf->samples_no == 0 ||
1950 s->profile->oid.len < pf->oid.len)) {
1951 free(s->profile, M_DUMMYNET);
1954 if (pf->samples_no == 0)
1957 * new profile, possibly allocate memory
1960 if (s->profile == NULL)
1961 s->profile = malloc(pf->oid.len,
1962 M_DUMMYNET, M_NOWAIT | M_ZERO);
1963 if (s->profile == NULL) {
1964 D("no memory for profile %d", i);
1968 /* preserve larger length XXX double check */
1969 olen = s->profile->oid.len;
1970 if (olen < pf->oid.len)
1972 memcpy(s->profile, pf, pf->oid.len);
1973 s->profile->oid.len = olen;
1980 * Delete all objects:
1983 dummynet_flush(void)
1986 /* delete all schedulers and related links/queues/flowsets */
1987 dn_ht_scan(V_dn_cfg.schedhash, schk_delete_cb,
1988 (void *)(uintptr_t)DN_DELETE_FS);
1989 /* delete all remaining (unlinked) flowsets */
1990 DX(4, "still %d unlinked fs", V_dn_cfg.fsk_count);
1991 dn_ht_free(V_dn_cfg.fshash, DNHT_REMOVE);
1992 fsk_detach_list(&V_dn_cfg.fsu, DN_DELETE_FS);
1993 /* Reinitialize system heap... */
1994 heap_init(&V_dn_cfg.evheap, 16, offsetof(struct dn_id, id));
1998 * Main handler for configuration. We are guaranteed to be called
1999 * with an oid which is at least a dn_id.
2000 * - the first object is the command (config, delete, flush, ...)
2001 * - config_link must be issued after the corresponding config_sched
2002 * - parameters (DN_TXT) for an object must precede the object
2003 * processed on a config_sched.
2006 do_config(void *p, size_t l)
2010 struct dn_profile profile;
2012 struct dn_link link;
2013 struct dn_sch sched;
2019 memcpy(&o, p, sizeof(o));
2020 if (o.id != DN_API_VERSION) {
2021 D("invalid api version got %d need %d", o.id, DN_API_VERSION);
2027 while (l >= sizeof(o)) {
2028 memcpy(&o, (char *)p + off, sizeof(o));
2029 if (o.len < sizeof(o) || l < o.len) {
2030 D("bad len o.len %d len %zu", o.len, l);
2038 D("cmd %d not implemented", o.type);
2041 #ifdef EMULATE_SYSCTL
2042 /* sysctl emulation.
2043 * if we recognize the command, jump to the correct
2044 * handler and return
2047 err = kesysctl_emu_set(p, l);
2051 case DN_CMD_CONFIG: /* simply a header */
2055 /* the argument is in the first uintptr_t after o */
2056 if (o.len < sizeof(o) + sizeof(a)) {
2060 memcpy(&a, (char *)p + off + sizeof(o), sizeof(a));
2061 switch (o.subtype) {
2063 /* delete base and derived schedulers */
2065 err = delete_schk(a);
2066 err2 = delete_schk(a + DN_MAX_ID);
2073 D("invalid delete type %d", o.subtype);
2078 err = (a < 1 || a >= DN_MAX_ID) ?
2079 EINVAL : delete_fs(a, 0) ;
2089 case DN_TEXT: /* store argument of next block */
2091 arg = malloc(o.len, M_TEMP, M_NOWAIT);
2096 memcpy(arg, (char *)p + off, o.len);
2100 dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
2105 memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
2106 err = config_link(&dn->link, arg);
2110 dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
2115 memcpy(&dn->profile, (char *)p + off,
2116 sizeof(dn->profile));
2117 err = config_profile(&dn->profile, arg);
2121 dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
2126 memcpy(&dn->sched, (char *)p + off,
2128 err = config_sched(&dn->sched, arg);
2132 dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
2137 memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
2138 err = (NULL == config_fs(&dn->fs, arg, 0));
2151 compute_space(struct dn_id *cmd, struct copy_args *a)
2153 int x = 0, need = 0;
2154 int profile_size = sizeof(struct dn_profile) -
2155 ED_MAX_SAMPLES_NO*sizeof(int);
2157 /* NOTE about compute space:
2158 * NP = V_dn_cfg.schk_count
2159 * NSI = V_dn_cfg.si_count
2160 * NF = V_dn_cfg.fsk_count
2161 * NQ = V_dn_cfg.queue_count
2163 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2164 * link, scheduler template, flowset
2165 * integrated in scheduler and header
2167 * (NSI)*(dn_flow) all scheduler instance (includes
2168 * the queue instance)
2170 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2171 * link, scheduler template, flowset
2172 * integrated in scheduler and header
2174 * (NSI * dn_flow) all scheduler instances
2175 * (NF * sizeof(uint_32)) space for flowset list linked to scheduler
2176 * (NQ * dn_queue) all queue [XXXfor now not listed]
2178 * (NF * dn_fs) all flowset
2179 * (NQ * dn_queue) all queues
2181 switch (cmd->subtype) {
2184 /* XXX where do LINK and SCH differ ? */
2185 /* 'ipfw sched show' could list all queues associated to
2186 * a scheduler. This feature for now is disabled
2188 case DN_LINK: /* pipe show */
2189 x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
2190 need += V_dn_cfg.schk_count *
2191 (sizeof(struct dn_fs) + profile_size) / 2;
2192 need += V_dn_cfg.fsk_count * sizeof(uint32_t);
2194 case DN_SCH: /* sched show */
2195 need += V_dn_cfg.schk_count *
2196 (sizeof(struct dn_fs) + profile_size) / 2;
2197 need += V_dn_cfg.fsk_count * sizeof(uint32_t);
2198 x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
2200 case DN_FS: /* queue show */
2201 x = DN_C_FS | DN_C_QUEUE;
2203 case DN_GET_COMPAT: /* compatibility mode */
2204 need = dn_compat_calc_size();
2209 need += V_dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
2210 /* NOT also, each fs might be attached to a sched */
2211 need += V_dn_cfg.schk_count * sizeof(struct dn_id) / 2;
2214 need += V_dn_cfg.fsk_count * sizeof(struct dn_fs);
2215 if (x & DN_C_LINK) {
2216 need += V_dn_cfg.schk_count * sizeof(struct dn_link) / 2;
2219 * When exporting a queue to userland, only pass up the
2220 * struct dn_flow, which is the only visible part.
2224 need += V_dn_cfg.queue_count * sizeof(struct dn_flow);
2226 need += V_dn_cfg.si_count * (sizeof(struct dn_flow));
2231 * If compat != NULL dummynet_get is called in compatibility mode.
2232 * *compat will be the pointer to the buffer to pass to ipfw
2235 dummynet_get(struct sockopt *sopt, void **compat)
2237 int have, i, need, error;
2238 char *start = NULL, *buf;
2239 size_t sopt_valsize;
2242 struct copy_range r;
2243 int l = sizeof(struct dn_id);
2245 bzero(&a, sizeof(a));
2246 bzero(&r, sizeof(r));
2248 /* save and restore original sopt_valsize around copyin */
2249 sopt_valsize = sopt->sopt_valsize;
2254 /* copy at least an oid, and possibly a full object */
2255 error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
2256 sopt->sopt_valsize = sopt_valsize;
2260 #ifdef EMULATE_SYSCTL
2261 /* sysctl emulation. */
2262 if (cmd->type == DN_SYSCTL_GET)
2263 return kesysctl_emu_get(sopt);
2265 if (l > sizeof(r)) {
2266 /* request larger than default, allocate buffer */
2267 cmd = malloc(l, M_DUMMYNET, M_NOWAIT);
2272 error = sooptcopyin(sopt, cmd, l, l);
2273 sopt->sopt_valsize = sopt_valsize;
2277 } else { /* compatibility */
2279 cmd->type = DN_CMD_GET;
2280 cmd->len = sizeof(struct dn_id);
2281 cmd->subtype = DN_GET_COMPAT;
2282 // cmd->id = sopt_valsize;
2283 D("compatibility mode");
2287 /* get AQM params */
2288 if(cmd->subtype == DN_AQM_PARAMS) {
2289 error = get_aqm_parms(sopt);
2291 /* get Scheduler params */
2292 } else if (cmd->subtype == DN_SCH_PARAMS) {
2293 error = get_sched_parms(sopt);
2298 a.extra = (struct copy_range *)cmd;
2299 if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
2300 uint32_t *rp = (uint32_t *)(cmd + 1);
2301 cmd->len += 2* sizeof(uint32_t);
2303 rp[1] = DN_MAX_ID - 1;
2304 if (cmd->subtype == DN_LINK) {
2309 /* Count space (under lock) and allocate (outside lock).
2310 * Exit with lock held if we manage to get enough buffer.
2311 * Try a few times then give up.
2313 for (have = 0, i = 0; i < 10; i++) {
2315 need = compute_space(cmd, &a);
2317 /* if there is a range, ignore value from compute_space() */
2318 if (l > sizeof(*cmd))
2319 need = sopt_valsize - sizeof(*cmd);
2326 need += sizeof(*cmd);
2332 free(start, M_DUMMYNET);
2334 if (need > sopt_valsize)
2338 start = malloc(have, M_DUMMYNET, M_NOWAIT | M_ZERO);
2341 if (start == NULL) {
2346 error = sooptcopyout(sopt, cmd, sizeof(*cmd));
2350 ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
2351 "%d:%d si %d, %d:%d queues %d",
2352 V_dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
2353 V_dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
2354 V_dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
2355 V_dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
2356 V_dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
2357 sopt->sopt_valsize = sopt_valsize;
2358 a.type = cmd->subtype;
2360 if (compat == NULL) {
2361 memcpy(start, cmd, sizeof(*cmd));
2362 ((struct dn_id*)(start))->len = sizeof(struct dn_id);
2363 buf = start + sizeof(*cmd);
2367 a.end = start + have;
2368 /* start copying other objects */
2370 a.type = DN_COMPAT_PIPE;
2371 dn_ht_scan(V_dn_cfg.schedhash, copy_data_helper_compat, &a);
2372 a.type = DN_COMPAT_QUEUE;
2373 dn_ht_scan(V_dn_cfg.fshash, copy_data_helper_compat, &a);
2374 } else if (a.type == DN_FS) {
2375 dn_ht_scan(V_dn_cfg.fshash, copy_data_helper, &a);
2377 dn_ht_scan(V_dn_cfg.schedhash, copy_data_helper, &a);
2383 sopt->sopt_valsize = buf - start;
2384 /* free() is done by ip_dummynet_compat() */
2385 start = NULL; //XXX hack
2387 error = sooptcopyout(sopt, start, buf - start);
2391 free(cmd, M_DUMMYNET);
2392 free(start, M_DUMMYNET);
2396 /* Callback called on scheduler instance to delete it if idle */
2398 drain_scheduler_cb(void *_si, void *arg)
2400 struct dn_sch_inst *si = _si;
2402 if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
2405 if (si->sched->fp->flags & DN_MULTIQUEUE) {
2406 if (si->q_count == 0)
2407 return si_destroy(si, NULL);
2410 } else { /* !DN_MULTIQUEUE */
2411 if ((si+1)->ni.length == 0)
2412 return si_destroy(si, NULL);
2416 return 0; /* unreachable */
2419 /* Callback called on scheduler to check if it has instances */
2421 drain_scheduler_sch_cb(void *_s, void *arg)
2423 struct dn_schk *s = _s;
2425 if (s->sch.flags & DN_HAVE_MASK) {
2426 dn_ht_scan_bucket(s->siht, &s->drain_bucket,
2427 drain_scheduler_cb, NULL);
2431 if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
2438 /* Called every tick, try to delete a 'bucket' of scheduler */
2440 dn_drain_scheduler(void)
2442 dn_ht_scan_bucket(V_dn_cfg.schedhash, &V_dn_cfg.drain_sch,
2443 drain_scheduler_sch_cb, NULL);
2444 V_dn_cfg.drain_sch++;
2447 /* Callback called on queue to delete if it is idle */
2449 drain_queue_cb(void *_q, void *arg)
2451 struct dn_queue *q = _q;
2453 if (q->ni.length == 0) {
2454 dn_delete_queue(q, DN_DESTROY);
2455 return DNHT_SCAN_DEL; /* queue is deleted */
2458 return 0; /* queue isn't deleted */
2461 /* Callback called on flowset used to check if it has queues */
2463 drain_queue_fs_cb(void *_fs, void *arg)
2465 struct dn_fsk *fs = _fs;
2467 if (fs->fs.flags & DN_QHT_HASH) {
2468 /* Flowset has a hash table for queues */
2469 dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
2470 drain_queue_cb, NULL);
2473 /* No hash table for this flowset, null the pointer
2474 * if the queue is deleted
2477 if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
2484 /* Called every tick, try to delete a 'bucket' of queue */
2486 dn_drain_queue(void)
2488 /* scan a bucket of flowset */
2489 dn_ht_scan_bucket(V_dn_cfg.fshash, &V_dn_cfg.drain_fs,
2490 drain_queue_fs_cb, NULL);
2491 V_dn_cfg.drain_fs++;
2495 * Handler for the various dummynet socket options
2498 ip_dn_ctl(struct sockopt *sopt)
2500 struct epoch_tracker et;
2505 error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
2509 /* Disallow sets in really-really secure mode. */
2510 if (sopt->sopt_dir == SOPT_SET) {
2511 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2516 NET_EPOCH_ENTER(et);
2518 switch (sopt->sopt_name) {
2520 D("dummynet: unknown option %d", sopt->sopt_name);
2524 case IP_DUMMYNET_FLUSH:
2525 case IP_DUMMYNET_CONFIGURE:
2526 case IP_DUMMYNET_DEL: /* remove a pipe or queue */
2527 case IP_DUMMYNET_GET:
2528 D("dummynet: compat option %d", sopt->sopt_name);
2529 error = ip_dummynet_compat(sopt);
2533 if (sopt->sopt_dir == SOPT_GET) {
2534 error = dummynet_get(sopt, NULL);
2537 l = sopt->sopt_valsize;
2538 if (l < sizeof(struct dn_id) || l > 12000) {
2539 D("argument len %zu invalid", l);
2542 p = malloc(l, M_TEMP, M_NOWAIT);
2547 error = sooptcopyin(sopt, p, l, l);
2549 error = do_config(p, l);
2561 ip_dn_vnet_init(void)
2563 if (V_dn_cfg.init_done)
2565 V_dn_cfg.init_done = 1;
2566 /* Set defaults here. MSVC does not accept initializers,
2567 * and this is also useful for vimages
2570 V_dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
2571 V_dn_cfg.byte_limit = 1024 * 1024;
2572 V_dn_cfg.expire = 1;
2574 /* RED parameters */
2575 V_dn_cfg.red_lookup_depth = 256; /* default lookup table depth */
2576 V_dn_cfg.red_avg_pkt_size = 512; /* default medium packet size */
2577 V_dn_cfg.red_max_pkt_size = 1500; /* default max packet size */
2580 V_dn_cfg.max_hash_size = 65536; /* max in the hash tables */
2581 V_dn_cfg.hash_size = 64; /* default hash size */
2583 /* create hash tables for schedulers and flowsets.
2584 * In both we search by key and by pointer.
2586 V_dn_cfg.schedhash = dn_ht_init(NULL, V_dn_cfg.hash_size,
2587 offsetof(struct dn_schk, schk_next),
2588 schk_hash, schk_match, schk_new);
2589 V_dn_cfg.fshash = dn_ht_init(NULL, V_dn_cfg.hash_size,
2590 offsetof(struct dn_fsk, fsk_next),
2591 fsk_hash, fsk_match, fsk_new);
2593 /* bucket index to drain object */
2594 V_dn_cfg.drain_fs = 0;
2595 V_dn_cfg.drain_sch = 0;
2597 heap_init(&V_dn_cfg.evheap, 16, offsetof(struct dn_id, id));
2598 SLIST_INIT(&V_dn_cfg.fsu);
2602 /* Initialize curr_time adjustment mechanics. */
2603 getmicrouptime(&V_dn_cfg.prev_t);
2607 ip_dn_vnet_destroy(void)
2613 dn_ht_free(V_dn_cfg.schedhash, 0);
2614 dn_ht_free(V_dn_cfg.fshash, 0);
2615 heap_free(&V_dn_cfg.evheap);
2623 if (dn_tasks_started)
2626 mtx_init(&sched_mtx, "dn_sched", NULL, MTX_DEF);
2628 dn_tasks_started = 1;
2629 TASK_INIT(&dn_task, 0, dummynet_task, NULL);
2630 dn_tq = taskqueue_create_fast("dummynet", M_WAITOK,
2631 taskqueue_thread_enqueue, &dn_tq);
2632 taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
2634 CK_LIST_INIT(&schedlist);
2635 callout_init(&dn_timeout, 1);
2640 ip_dn_destroy(int last)
2642 /* ensure no more callouts are started */
2645 /* check for last */
2647 ND("removing last instance\n");
2648 ip_dn_ctl_ptr = NULL;
2649 ip_dn_io_ptr = NULL;
2652 callout_drain(&dn_timeout);
2653 taskqueue_drain(dn_tq, &dn_task);
2654 taskqueue_free(dn_tq);
2658 dummynet_modevent(module_t mod, int type, void *data)
2661 if (type == MOD_LOAD) {
2663 printf("DUMMYNET already loaded\n");
2667 ip_dn_ctl_ptr = ip_dn_ctl;
2668 ip_dn_io_ptr = dummynet_io;
2670 } else if (type == MOD_UNLOAD) {
2671 ip_dn_destroy(1 /* last */);
2677 /* modevent helpers for the modules */
2679 load_dn_sched(struct dn_alg *d)
2684 return 1; /* error */
2685 ip_dn_init(); /* just in case, we need the lock */
2687 /* Check that mandatory funcs exists */
2688 if (d->enqueue == NULL || d->dequeue == NULL) {
2689 D("missing enqueue or dequeue for %s", d->name);
2693 /* Search if scheduler already exists */
2694 mtx_lock(&sched_mtx);
2695 CK_LIST_FOREACH(s, &schedlist, next) {
2696 if (strcmp(s->name, d->name) == 0) {
2697 D("%s already loaded", d->name);
2698 break; /* scheduler already exists */
2702 CK_LIST_INSERT_HEAD(&schedlist, d, next);
2703 mtx_unlock(&sched_mtx);
2704 D("dn_sched %s %sloaded", d->name, s ? "not ":"");
2709 unload_dn_sched(struct dn_alg *s)
2711 struct dn_alg *tmp, *r;
2714 ND("called for %s", s->name);
2716 mtx_lock(&sched_mtx);
2717 CK_LIST_FOREACH_SAFE(r, &schedlist, next, tmp) {
2718 if (strcmp(s->name, r->name) != 0)
2720 ND("ref_count = %d", r->ref_count);
2721 err = (r->ref_count != 0) ? EBUSY : 0;
2723 CK_LIST_REMOVE(r, next);
2726 mtx_unlock(&sched_mtx);
2728 D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
2733 dn_sched_modevent(module_t mod, int cmd, void *arg)
2735 struct dn_alg *sch = arg;
2737 if (cmd == MOD_LOAD)
2738 return load_dn_sched(sch);
2739 else if (cmd == MOD_UNLOAD)
2740 return unload_dn_sched(sch);
2745 static moduledata_t dummynet_mod = {
2746 "dummynet", dummynet_modevent, NULL
2749 #define DN_SI_SUB SI_SUB_PROTO_FIREWALL
2750 #define DN_MODEV_ORD (SI_ORDER_ANY - 128) /* after ipfw */
2751 DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
2752 MODULE_VERSION(dummynet, 3);
2755 * Starting up. Done in order after dummynet_modevent() has been called.
2756 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2758 VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_vnet_init, NULL);
2761 * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
2762 * after dummynet_modevent() has been called. Not called on reboot.
2763 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2764 * or when the module is unloaded.
2766 VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_vnet_destroy, NULL);
2770 /* modevent helpers for the AQM modules */
2772 load_dn_aqm(struct dn_aqm *d)
2774 struct dn_aqm *aqm=NULL;
2777 return 1; /* error */
2778 ip_dn_init(); /* just in case, we need the lock */
2780 /* Check that mandatory funcs exists */
2781 if (d->enqueue == NULL || d->dequeue == NULL) {
2782 D("missing enqueue or dequeue for %s", d->name);
2786 mtx_lock(&sched_mtx);
2788 /* Search if AQM already exists */
2789 CK_LIST_FOREACH(aqm, &aqmlist, next) {
2790 if (strcmp(aqm->name, d->name) == 0) {
2791 D("%s already loaded", d->name);
2792 break; /* AQM already exists */
2796 CK_LIST_INSERT_HEAD(&aqmlist, d, next);
2798 mtx_unlock(&sched_mtx);
2800 D("dn_aqm %s %sloaded", d->name, aqm ? "not ":"");
2804 /* Callback to clean up AQM status for queues connected to a flowset
2805 * and then deconfigure the flowset.
2806 * This function is called before an AQM module is unloaded
2809 fs_cleanup(void *_fs, void *arg)
2811 struct dn_fsk *fs = _fs;
2812 uint32_t type = *(uint32_t *)arg;
2814 if (fs->aqmfp && fs->aqmfp->type == type)
2815 aqm_cleanup_deconfig_fs(fs);
2821 unload_dn_aqm(struct dn_aqm *aqm)
2823 struct dn_aqm *tmp, *r;
2826 ND("called for %s", aqm->name);
2828 /* clean up AQM status and deconfig flowset */
2829 dn_ht_scan(V_dn_cfg.fshash, fs_cleanup, &aqm->type);
2831 mtx_lock(&sched_mtx);
2833 CK_LIST_FOREACH_SAFE(r, &aqmlist, next, tmp) {
2834 if (strcmp(aqm->name, r->name) != 0)
2836 ND("ref_count = %d", r->ref_count);
2837 err = (r->ref_count != 0 || r->cfg_ref_count != 0) ? EBUSY : 0;
2839 CK_LIST_REMOVE(r, next);
2843 mtx_unlock(&sched_mtx);
2846 D("%s %sunloaded", aqm->name, err ? "not ":"");
2848 D("ref_count=%d, cfg_ref_count=%d", r->ref_count, r->cfg_ref_count);
2853 dn_aqm_modevent(module_t mod, int cmd, void *arg)
2855 struct dn_aqm *aqm = arg;
2857 if (cmd == MOD_LOAD)
2858 return load_dn_aqm(aqm);
2859 else if (cmd == MOD_UNLOAD)
2860 return unload_dn_aqm(aqm);