2 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
3 * Portions Copyright (c) 2000 Akamba Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Configuration and internal object management for dummynet.
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/rwlock.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
50 #include <sys/taskqueue.h>
51 #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
52 #include <netinet/in.h>
53 #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */
54 #include <netinet/ip_fw.h>
55 #include <netinet/ipfw/ip_fw_private.h>
56 #include <netinet/ipfw/dn_heap.h>
57 #include <netinet/ip_dummynet.h>
58 #include <netinet/ipfw/ip_dn_private.h>
59 #include <netinet/ipfw/dn_sched.h>
61 /* which objects to copy */
62 #define DN_C_LINK 0x01
64 #define DN_C_FLOW 0x04
66 #define DN_C_QUEUE 0x10
68 /* we use this argument in case of a schk_new */
74 /*---- callout hooks. ----*/
75 static struct callout dn_timeout;
76 static struct task dn_task;
77 static struct taskqueue *dn_tq = NULL;
80 dummynet(void * __unused unused)
83 taskqueue_enqueue(dn_tq, &dn_task);
89 callout_reset(&dn_timeout, 1, dummynet, NULL);
91 /*----- end of callout hooks -----*/
93 /* Return a scheduler descriptor given the type or name. */
94 static struct dn_alg *
95 find_sched_type(int type, char *name)
99 SLIST_FOREACH(d, &dn_cfg.schedlist, next) {
100 if (d->type == type || (name && !strcmp(d->name, name)))
103 return NULL; /* not found */
107 ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
110 const char *op = NULL;
118 } else if (oldv > hi) {
124 printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
128 /*---- flow_id mask, hash and compare functions ---*/
130 * The flow_id includes the 5-tuple, the queue/pipe number
131 * which we store in the extra area in host order,
132 * and for ipv6 also the flow_id6.
133 * XXX see if we want the tos byte (can store in 'flags')
135 static struct ipfw_flow_id *
136 flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
138 int is_v6 = IS_IP6_FLOW_ID(id);
140 id->dst_port &= mask->dst_port;
141 id->src_port &= mask->src_port;
142 id->proto &= mask->proto;
143 id->extra &= mask->extra;
145 APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
146 APPLY_MASK(&id->src_ip6, &mask->src_ip6);
147 id->flow_id6 &= mask->flow_id6;
149 id->dst_ip &= mask->dst_ip;
150 id->src_ip &= mask->src_ip;
155 /* computes an OR of two masks, result in dst and also returned */
156 static struct ipfw_flow_id *
157 flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
159 int is_v6 = IS_IP6_FLOW_ID(dst);
161 dst->dst_port |= src->dst_port;
162 dst->src_port |= src->src_port;
163 dst->proto |= src->proto;
164 dst->extra |= src->extra;
166 #define OR_MASK(_d, _s) \
167 (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
168 (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
169 (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
170 (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
171 OR_MASK(&dst->dst_ip6, &src->dst_ip6);
172 OR_MASK(&dst->src_ip6, &src->src_ip6);
174 dst->flow_id6 |= src->flow_id6;
176 dst->dst_ip |= src->dst_ip;
177 dst->src_ip |= src->src_ip;
183 nonzero_mask(struct ipfw_flow_id *m)
185 if (m->dst_port || m->src_port || m->proto || m->extra)
187 if (IS_IP6_FLOW_ID(m)) {
189 m->dst_ip6.__u6_addr.__u6_addr32[0] ||
190 m->dst_ip6.__u6_addr.__u6_addr32[1] ||
191 m->dst_ip6.__u6_addr.__u6_addr32[2] ||
192 m->dst_ip6.__u6_addr.__u6_addr32[3] ||
193 m->src_ip6.__u6_addr.__u6_addr32[0] ||
194 m->src_ip6.__u6_addr.__u6_addr32[1] ||
195 m->src_ip6.__u6_addr.__u6_addr32[2] ||
196 m->src_ip6.__u6_addr.__u6_addr32[3] ||
199 return m->dst_ip || m->src_ip;
203 /* XXX we may want a better hash function */
205 flow_id_hash(struct ipfw_flow_id *id)
209 if (IS_IP6_FLOW_ID(id)) {
210 uint32_t *d = (uint32_t *)&id->dst_ip6;
211 uint32_t *s = (uint32_t *)&id->src_ip6;
212 i = (d[0] ) ^ (d[1]) ^
214 (d[0] >> 15) ^ (d[1] >> 15) ^
215 (d[2] >> 15) ^ (d[3] >> 15) ^
216 (s[0] << 1) ^ (s[1] << 1) ^
217 (s[2] << 1) ^ (s[3] << 1) ^
218 (s[0] << 16) ^ (s[1] << 16) ^
219 (s[2] << 16) ^ (s[3] << 16) ^
220 (id->dst_port << 1) ^ (id->src_port) ^
222 (id->proto ) ^ (id->flow_id6);
224 i = (id->dst_ip) ^ (id->dst_ip >> 15) ^
225 (id->src_ip << 1) ^ (id->src_ip >> 16) ^
227 (id->dst_port << 1) ^ (id->src_port) ^ (id->proto);
232 /* Like bcmp, returns 0 if ids match, 1 otherwise. */
234 flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
236 int is_v6 = IS_IP6_FLOW_ID(id1);
239 if (IS_IP6_FLOW_ID(id2))
240 return 1; /* different address families */
242 return (id1->dst_ip == id2->dst_ip &&
243 id1->src_ip == id2->src_ip &&
244 id1->dst_port == id2->dst_port &&
245 id1->src_port == id2->src_port &&
246 id1->proto == id2->proto &&
247 id1->extra == id2->extra) ? 0 : 1;
251 !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
252 !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
253 id1->dst_port == id2->dst_port &&
254 id1->src_port == id2->src_port &&
255 id1->proto == id2->proto &&
256 id1->extra == id2->extra &&
257 id1->flow_id6 == id2->flow_id6) ? 0 : 1;
259 /*--------- end of flow-id mask, hash and compare ---------*/
261 /*--- support functions for the qht hashtable ----
262 * Entries are hashed by flow-id
265 q_hash(uintptr_t key, int flags, void *arg)
267 /* compute the hash slot from the flow id */
268 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
269 &((struct dn_queue *)key)->ni.fid :
270 (struct ipfw_flow_id *)key;
272 return flow_id_hash(id);
276 q_match(void *obj, uintptr_t key, int flags, void *arg)
278 struct dn_queue *o = (struct dn_queue *)obj;
279 struct ipfw_flow_id *id2;
281 if (flags & DNHT_KEY_IS_OBJ) {
282 /* compare pointers */
283 id2 = &((struct dn_queue *)key)->ni.fid;
285 id2 = (struct ipfw_flow_id *)key;
287 return (0 == flow_id_cmp(&o->ni.fid, id2));
291 * create a new queue instance for the given 'key'.
294 q_new(uintptr_t key, int flags, void *arg)
296 struct dn_queue *q, *template = arg;
297 struct dn_fsk *fs = template->fs;
298 int size = sizeof(*q) + fs->sched->fp->q_datalen;
300 q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
302 D("no memory for new queue");
306 set_oid(&q->ni.oid, DN_QUEUE, size);
307 if (fs->fs.flags & DN_QHT_HASH)
308 q->ni.fid = *(struct ipfw_flow_id *)key;
310 q->_si = template->_si;
313 if (fs->sched->fp->new_queue)
314 fs->sched->fp->new_queue(q);
315 dn_cfg.queue_count++;
320 * Notify schedulers that a queue is going away.
321 * If (flags & DN_DESTROY), also free the packets.
322 * The version for callbacks is called q_delete_cb().
325 dn_delete_queue(struct dn_queue *q, int flags)
327 struct dn_fsk *fs = q->fs;
329 // D("fs %p si %p\n", fs, q->_si);
330 /* notify the parent scheduler that the queue is going away */
331 if (fs && fs->sched->fp->free_queue)
332 fs->sched->fp->free_queue(q);
335 if (flags & DN_DESTROY) {
337 dn_free_pkts(q->mq.head);
338 bzero(q, sizeof(*q)); // safety
340 dn_cfg.queue_count--;
345 q_delete_cb(void *q, void *arg)
347 int flags = (int)(uintptr_t)arg;
348 dn_delete_queue(q, flags);
349 return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
353 * calls dn_delete_queue/q_delete_cb on all queues,
354 * which notifies the parent scheduler and possibly drains packets.
355 * flags & DN_DESTROY: drains queues and destroy qht;
358 qht_delete(struct dn_fsk *fs, int flags)
360 ND("fs %d start flags %d qht %p",
361 fs->fs.fs_nr, flags, fs->qht);
364 if (fs->fs.flags & DN_QHT_HASH) {
365 dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
366 if (flags & DN_DESTROY) {
367 dn_ht_free(fs->qht, 0);
371 dn_delete_queue((struct dn_queue *)(fs->qht), flags);
372 if (flags & DN_DESTROY)
378 * Find and possibly create the queue for a MULTIQUEUE scheduler.
379 * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
382 ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
383 struct ipfw_flow_id *id)
385 struct dn_queue template;
390 if (fs->fs.flags & DN_QHT_HASH) {
391 struct ipfw_flow_id masked_id;
392 if (fs->qht == NULL) {
393 fs->qht = dn_ht_init(NULL, fs->fs.buckets,
394 offsetof(struct dn_queue, q_next),
395 q_hash, q_match, q_new);
400 flow_id_mask(&fs->fsk_mask, &masked_id);
401 return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
402 DNHT_INSERT, &template);
405 fs->qht = q_new(0, 0, &template);
406 return (struct dn_queue *)fs->qht;
409 /*--- end of queue hash table ---*/
411 /*--- support functions for the sch_inst hashtable ----
413 * These are hashed by flow-id
416 si_hash(uintptr_t key, int flags, void *arg)
418 /* compute the hash slot from the flow id */
419 struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
420 &((struct dn_sch_inst *)key)->ni.fid :
421 (struct ipfw_flow_id *)key;
423 return flow_id_hash(id);
427 si_match(void *obj, uintptr_t key, int flags, void *arg)
429 struct dn_sch_inst *o = obj;
430 struct ipfw_flow_id *id2;
432 id2 = (flags & DNHT_KEY_IS_OBJ) ?
433 &((struct dn_sch_inst *)key)->ni.fid :
434 (struct ipfw_flow_id *)key;
435 return flow_id_cmp(&o->ni.fid, id2) == 0;
439 * create a new instance for the given 'key'
440 * Allocate memory for instance, delay line and scheduler private data.
443 si_new(uintptr_t key, int flags, void *arg)
445 struct dn_schk *s = arg;
446 struct dn_sch_inst *si;
447 int l = sizeof(*si) + s->fp->si_datalen;
449 si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
453 /* Set length only for the part passed up to userland. */
454 set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
455 set_oid(&(si->dline.oid), DN_DELAY_LINE,
456 sizeof(struct delay_line));
457 /* mark si and dline as outside the event queue */
458 si->ni.oid.id = si->dline.oid.id = -1;
463 if (s->fp->new_sched && s->fp->new_sched(si)) {
464 D("new_sched error");
467 if (s->sch.flags & DN_HAVE_MASK)
468 si->ni.fid = *(struct ipfw_flow_id *)key;
475 bzero(si, sizeof(*si)); // safety
476 free(si, M_DUMMYNET);
482 * Callback from siht to delete all scheduler instances. Remove
483 * si and delay line from the system heap, destroy all queues.
484 * We assume that all flowset have been notified and do not
485 * point to us anymore.
488 si_destroy(void *_si, void *arg)
490 struct dn_sch_inst *si = _si;
491 struct dn_schk *s = si->sched;
492 struct delay_line *dl = &si->dline;
494 if (dl->oid.subtype) /* remove delay line from event heap */
495 heap_extract(&dn_cfg.evheap, dl);
496 dn_free_pkts(dl->mq.head); /* drain delay line */
497 if (si->kflags & DN_ACTIVE) /* remove si from event heap */
498 heap_extract(&dn_cfg.evheap, si);
499 if (s->fp->free_sched)
500 s->fp->free_sched(si);
501 bzero(si, sizeof(*si)); /* safety */
502 free(si, M_DUMMYNET);
504 return DNHT_SCAN_DEL;
508 * Find the scheduler instance for this packet. If we need to apply
509 * a mask, do on a local copy of the flow_id to preserve the original.
510 * Assume siht is always initialized if we have a mask.
513 ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
516 if (s->sch.flags & DN_HAVE_MASK) {
517 struct ipfw_flow_id id_t = *id;
518 flow_id_mask(&s->sch.sched_mask, &id_t);
519 return dn_ht_find(s->siht, (uintptr_t)&id_t,
523 s->siht = si_new(0, 0, s);
524 return (struct dn_sch_inst *)s->siht;
527 /* callback to flush credit for the scheduler instance */
529 si_reset_credit(void *_si, void *arg)
531 struct dn_sch_inst *si = _si;
532 struct dn_link *p = &si->sched->link;
534 si->credit = p->burst + (dn_cfg.io_fast ? p->bandwidth : 0);
539 schk_reset_credit(struct dn_schk *s)
541 if (s->sch.flags & DN_HAVE_MASK)
542 dn_ht_scan(s->siht, si_reset_credit, NULL);
544 si_reset_credit(s->siht, NULL);
546 /*---- end of sch_inst hashtable ---------------------*/
548 /*-------------------------------------------------------
549 * flowset hash (fshash) support. Entries are hashed by fs_nr.
550 * New allocations are put in the fsunlinked list, from which
551 * they are removed when they point to a specific scheduler.
554 fsk_hash(uintptr_t key, int flags, void *arg)
556 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
557 ((struct dn_fsk *)key)->fs.fs_nr;
559 return ( (i>>8)^(i>>4)^i );
563 fsk_match(void *obj, uintptr_t key, int flags, void *arg)
565 struct dn_fsk *fs = obj;
566 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
567 ((struct dn_fsk *)key)->fs.fs_nr;
569 return (fs->fs.fs_nr == i);
573 fsk_new(uintptr_t key, int flags, void *arg)
577 fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
579 set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
581 fs->drain_bucket = 0;
582 SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
588 * detach flowset from its current scheduler. Flags as follows:
589 * DN_DETACH removes from the fsk_list
590 * DN_DESTROY deletes individual queues
591 * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
594 fsk_detach(struct dn_fsk *fs, int flags)
596 if (flags & DN_DELETE_FS)
598 ND("fs %d from sched %d flags %s %s %s",
599 fs->fs.fs_nr, fs->fs.sched_nr,
600 (flags & DN_DELETE_FS) ? "DEL_FS":"",
601 (flags & DN_DESTROY) ? "DEL":"",
602 (flags & DN_DETACH) ? "DET":"");
603 if (flags & DN_DETACH) { /* detach from the list */
604 struct dn_fsk_head *h;
605 h = fs->sched ? &fs->sched->fsk_list : &dn_cfg.fsu;
606 SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
608 /* Free the RED parameters, they will be recomputed on
609 * subsequent attach if needed.
612 free(fs->w_q_lookup, M_DUMMYNET);
613 fs->w_q_lookup = NULL;
614 qht_delete(fs, flags);
615 if (fs->sched && fs->sched->fp->free_fsk)
616 fs->sched->fp->free_fsk(fs);
618 if (flags & DN_DELETE_FS) {
619 bzero(fs, sizeof(fs)); /* safety */
620 free(fs, M_DUMMYNET);
623 SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
628 * Detach or destroy all flowsets in a list.
629 * flags specifies what to do:
630 * DN_DESTROY: flush all queues
631 * DN_DELETE_FS: DN_DESTROY + destroy flowset
632 * DN_DELETE_FS implies DN_DESTROY
635 fsk_detach_list(struct dn_fsk_head *h, int flags)
638 int n = 0; /* only for stats */
640 ND("head %p flags %x", h, flags);
641 while ((fs = SLIST_FIRST(h))) {
642 SLIST_REMOVE_HEAD(h, sch_chain);
644 fsk_detach(fs, flags);
646 ND("done %d flowsets", n);
650 * called on 'queue X delete' -- removes the flowset from fshash,
651 * deletes all queues for the flowset, and removes the flowset.
654 delete_fs(int i, int locked)
661 fs = dn_ht_find(dn_cfg.fshash, i, DNHT_REMOVE, NULL);
662 ND("fs %d found %p", i, fs);
664 fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
673 /*----- end of flowset hashtable support -------------*/
675 /*------------------------------------------------------------
676 * Scheduler hash. When searching by index we pass sched_nr,
677 * otherwise we pass struct dn_sch * which is the first field in
678 * struct dn_schk so we can cast between the two. We use this trick
679 * because in the create phase (but it should be fixed).
682 schk_hash(uintptr_t key, int flags, void *_arg)
684 uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
685 ((struct dn_schk *)key)->sch.sched_nr;
686 return ( (i>>8)^(i>>4)^i );
690 schk_match(void *obj, uintptr_t key, int flags, void *_arg)
692 struct dn_schk *s = (struct dn_schk *)obj;
693 int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
694 ((struct dn_schk *)key)->sch.sched_nr;
695 return (s->sch.sched_nr == i);
699 * Create the entry and intialize with the sched hash if needed.
700 * Leave s->fp unset so we can tell whether a dn_ht_find() returns
701 * a new object or a previously existing one.
704 schk_new(uintptr_t key, int flags, void *arg)
706 struct schk_new_arg *a = arg;
708 int l = sizeof(*s) +a->fp->schk_datalen;
710 s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
713 set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
714 s->sch = *a->sch; // copy initial values
715 s->link.link_nr = s->sch.sched_nr;
716 SLIST_INIT(&s->fsk_list);
717 /* initialize the hash table or create the single instance */
718 s->fp = a->fp; /* si_new needs this */
720 if (s->sch.flags & DN_HAVE_MASK) {
721 s->siht = dn_ht_init(NULL, s->sch.buckets,
722 offsetof(struct dn_sch_inst, si_next),
723 si_hash, si_match, si_new);
724 if (s->siht == NULL) {
729 s->fp = NULL; /* mark as a new scheduler */
735 * Callback for sched delete. Notify all attached flowsets to
736 * detach from the scheduler, destroy the internal flowset, and
737 * all instances. The scheduler goes away too.
738 * arg is 0 (only detach flowsets and destroy instances)
739 * DN_DESTROY (detach & delete queues, delete schk)
740 * or DN_DELETE_FS (delete queues and flowsets, delete schk)
743 schk_delete_cb(void *obj, void *arg)
745 struct dn_schk *s = obj;
748 ND("sched %d arg %s%s",
750 a&DN_DESTROY ? "DEL ":"",
751 a&DN_DELETE_FS ? "DEL_FS":"");
753 fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
754 /* no more flowset pointing to us now */
755 if (s->sch.flags & DN_HAVE_MASK) {
756 dn_ht_scan(s->siht, si_destroy, NULL);
757 dn_ht_free(s->siht, 0);
759 si_destroy(s->siht, NULL);
761 free(s->profile, M_DUMMYNET);
767 bzero(s, sizeof(*s)); // safety
768 free(obj, M_DUMMYNET);
770 return DNHT_SCAN_DEL;
774 * called on a 'sched X delete' command. Deletes a single scheduler.
775 * This is done by removing from the schedhash, unlinking all
776 * flowsets and deleting their traffic.
783 s = dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
787 delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
788 /* then detach flowsets, delete traffic */
789 schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
792 /*--- end of schk hashtable support ---*/
795 copy_obj(char **start, char *end, void *_o, const char *msg, int i)
797 struct dn_id *o = _o;
798 int have = end - *start;
800 if (have < o->len || o->len == 0 || o->type == 0) {
801 D("(WARN) type %d %s %d have %d need %d",
802 o->type, msg, i, have, o->len);
805 ND("type %d %s %d len %d", o->type, msg, i, o->len);
806 bcopy(_o, *start, o->len);
807 if (o->type == DN_LINK) {
808 /* Adjust burst parameter for link */
809 struct dn_link *l = (struct dn_link *)*start;
810 l->burst = div64(l->burst, 8 * hz);
811 l->delay = l->delay * 1000 / hz;
812 } else if (o->type == DN_SCH) {
813 /* Set id->id to the number of instances */
814 struct dn_schk *s = _o;
815 struct dn_id *id = (struct dn_id *)(*start);
816 id->id = (s->sch.flags & DN_HAVE_MASK) ?
817 dn_ht_entries(s->siht) : (s->siht ? 1 : 0);
823 /* Specific function to copy a queue.
824 * Copies only the user-visible part of a queue (which is in
825 * a struct dn_flow), and sets len accordingly.
828 copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
830 struct dn_id *o = _o;
831 int have = end - *start;
832 int len = sizeof(struct dn_flow); /* see above comment */
834 if (have < len || o->len == 0 || o->type != DN_QUEUE) {
835 D("ERROR type %d %s %d have %d need %d",
836 o->type, msg, i, have, len);
839 ND("type %d %s %d len %d", o->type, msg, i, len);
840 bcopy(_o, *start, len);
841 ((struct dn_id*)(*start))->len = len;
847 copy_q_cb(void *obj, void *arg)
849 struct dn_queue *q = obj;
850 struct copy_args *a = arg;
851 struct dn_flow *ni = (struct dn_flow *)(*a->start);
852 if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
853 return DNHT_SCAN_END;
854 ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
855 ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
860 copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
864 if (fs->fs.flags & DN_QHT_HASH)
865 dn_ht_scan(fs->qht, copy_q_cb, a);
867 copy_q_cb(fs->qht, a);
872 * This routine only copies the initial part of a profile ? XXX
875 copy_profile(struct copy_args *a, struct dn_profile *p)
877 int have = a->end - *a->start;
878 /* XXX here we check for max length */
879 int profile_len = sizeof(struct dn_profile) -
880 ED_MAX_SAMPLES_NO*sizeof(int);
884 if (have < profile_len) {
885 D("error have %d need %d", have, profile_len);
888 bcopy(p, *a->start, profile_len);
889 ((struct dn_id *)(*a->start))->len = profile_len;
890 *a->start += profile_len;
895 copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
897 struct dn_fs *ufs = (struct dn_fs *)(*a->start);
900 ND("flowset %d", fs->fs.fs_nr);
901 if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
902 return DNHT_SCAN_END;
903 ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
904 dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
905 if (flags) { /* copy queues */
912 copy_si_cb(void *obj, void *arg)
914 struct dn_sch_inst *si = obj;
915 struct copy_args *a = arg;
916 struct dn_flow *ni = (struct dn_flow *)(*a->start);
917 if (copy_obj(a->start, a->end, &si->ni, "inst",
918 si->sched->sch.sched_nr))
919 return DNHT_SCAN_END;
920 ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
921 ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
926 copy_si(struct copy_args *a, struct dn_schk *s, int flags)
928 if (s->sch.flags & DN_HAVE_MASK)
929 dn_ht_scan(s->siht, copy_si_cb, a);
931 copy_si_cb(s->siht, a);
936 * compute a list of children of a scheduler and copy up
939 copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
945 int n = 0, space = sizeof(*o);
946 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
947 if (fs->fs.fs_nr < DN_MAX_ID)
950 space += n * sizeof(uint32_t);
951 DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
952 if (a->end - *(a->start) < space)
953 return DNHT_SCAN_END;
954 o = (struct dn_id *)(*(a->start));
958 p = (uint32_t *)(o+1);
959 SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
960 if (fs->fs.fs_nr < DN_MAX_ID)
966 copy_data_helper(void *_o, void *_arg)
968 struct copy_args *a = _arg;
969 uint32_t *r = a->extra->r; /* start of first range */
970 uint32_t *lim; /* first invalid pointer */
973 lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
975 if (a->type == DN_LINK || a->type == DN_SCH) {
976 /* pipe|sched show, we receive a dn_schk */
977 struct dn_schk *s = _o;
980 if (a->type == DN_SCH && n >= DN_MAX_ID)
981 return 0; /* not a scheduler */
982 if (a->type == DN_LINK && n <= DN_MAX_ID)
983 return 0; /* not a pipe */
985 /* see if the object is within one of our ranges */
986 for (;r < lim; r += 2) {
987 if (n < r[0] || n > r[1])
989 /* Found a valid entry, copy and we are done */
990 if (a->flags & DN_C_LINK) {
991 if (copy_obj(a->start, a->end,
992 &s->link, "link", n))
993 return DNHT_SCAN_END;
994 if (copy_profile(a, s->profile))
995 return DNHT_SCAN_END;
996 if (copy_flowset(a, s->fs, 0))
997 return DNHT_SCAN_END;
999 if (a->flags & DN_C_SCH) {
1000 if (copy_obj(a->start, a->end,
1001 &s->sch, "sched", n))
1002 return DNHT_SCAN_END;
1003 /* list all attached flowsets */
1004 if (copy_fsk_list(a, s, 0))
1005 return DNHT_SCAN_END;
1007 if (a->flags & DN_C_FLOW)
1011 } else if (a->type == DN_FS) {
1012 /* queue show, skip internal flowsets */
1013 struct dn_fsk *fs = _o;
1018 /* see if the object is within one of our ranges */
1019 for (;r < lim; r += 2) {
1020 if (n < r[0] || n > r[1])
1022 if (copy_flowset(a, fs, 0))
1023 return DNHT_SCAN_END;
1025 break; /* we are done */
1031 static inline struct dn_schk *
1032 locate_scheduler(int i)
1034 return dn_ht_find(dn_cfg.schedhash, i, 0, NULL);
1038 * red parameters are in fixed point arithmetic.
1041 config_red(struct dn_fsk *fs)
1043 int64_t s, idle, weight, w0;
1046 fs->w_q = fs->fs.w_q;
1047 fs->max_p = fs->fs.max_p;
1049 /* Doing stuff that was in userland */
1050 i = fs->sched->link.bandwidth;
1052 hz * dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
1054 idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
1055 fs->lookup_step = div64(idle , dn_cfg.red_lookup_depth);
1056 /* fs->lookup_step not scaled, */
1057 if (!fs->lookup_step)
1058 fs->lookup_step = 1;
1059 w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
1061 for (t = fs->lookup_step; t > 1; --t)
1062 weight = SCALE_MUL(weight, w0);
1063 fs->lookup_weight = (int)(weight); // scaled
1065 /* Now doing stuff that was in kerneland */
1066 fs->min_th = SCALE(fs->fs.min_th);
1067 fs->max_th = SCALE(fs->fs.max_th);
1069 fs->c_1 = fs->max_p / (fs->fs.max_th - fs->fs.min_th);
1070 fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
1072 if (fs->fs.flags & DN_IS_GENTLE_RED) {
1073 fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
1074 fs->c_4 = SCALE(1) - 2 * fs->max_p;
1077 /* If the lookup table already exist, free and create it again. */
1078 if (fs->w_q_lookup) {
1079 free(fs->w_q_lookup, M_DUMMYNET);
1080 fs->w_q_lookup = NULL;
1082 if (dn_cfg.red_lookup_depth == 0) {
1083 printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
1085 fs->fs.flags &= ~DN_IS_RED;
1086 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1089 fs->lookup_depth = dn_cfg.red_lookup_depth;
1090 fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
1091 M_DUMMYNET, M_NOWAIT);
1092 if (fs->w_q_lookup == NULL) {
1093 printf("dummynet: sorry, cannot allocate red lookup table\n");
1094 fs->fs.flags &= ~DN_IS_RED;
1095 fs->fs.flags &= ~DN_IS_GENTLE_RED;
1099 /* Fill the lookup table with (1 - w_q)^x */
1100 fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
1102 for (i = 1; i < fs->lookup_depth; i++)
1104 SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
1106 if (dn_cfg.red_avg_pkt_size < 1)
1107 dn_cfg.red_avg_pkt_size = 512;
1108 fs->avg_pkt_size = dn_cfg.red_avg_pkt_size;
1109 if (dn_cfg.red_max_pkt_size < 1)
1110 dn_cfg.red_max_pkt_size = 1500;
1111 fs->max_pkt_size = dn_cfg.red_max_pkt_size;
1116 /* Scan all flowset attached to this scheduler and update red */
1118 update_red(struct dn_schk *s)
1121 SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1122 if (fs && (fs->fs.flags & DN_IS_RED))
1127 /* attach flowset to scheduler s, possibly requeue */
1129 fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
1131 ND("remove fs %d from fsunlinked, link to sched %d",
1132 fs->fs.fs_nr, s->sch.sched_nr);
1133 SLIST_REMOVE(&dn_cfg.fsu, fs, dn_fsk, sch_chain);
1135 SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
1138 /* XXX compute fsk_mask */
1139 fs->fsk_mask = fs->fs.flow_mask;
1140 if (fs->sched->sch.flags & DN_HAVE_MASK)
1141 flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
1144 * we must drain qht according to the old
1145 * type, and reinsert according to the new one.
1146 * The requeue is complex -- in general we need to
1147 * reclassify every single packet.
1148 * For the time being, let's hope qht is never set
1149 * when we reach this point.
1151 D("XXX TODO requeue from fs %d to sch %d",
1152 fs->fs.fs_nr, s->sch.sched_nr);
1155 /* set the new type for qht */
1156 if (nonzero_mask(&fs->fsk_mask))
1157 fs->fs.flags |= DN_QHT_HASH;
1159 fs->fs.flags &= ~DN_QHT_HASH;
1161 /* XXX config_red() can fail... */
1162 if (fs->fs.flags & DN_IS_RED)
1166 /* update all flowsets which may refer to this scheduler */
1168 update_fs(struct dn_schk *s)
1170 struct dn_fsk *fs, *tmp;
1172 SLIST_FOREACH_SAFE(fs, &dn_cfg.fsu, sch_chain, tmp) {
1173 if (s->sch.sched_nr != fs->fs.sched_nr) {
1174 D("fs %d for sch %d not %d still unlinked",
1175 fs->fs.fs_nr, fs->fs.sched_nr,
1184 * Configuration -- to preserve backward compatibility we use
1185 * the following scheme (N is 65536)
1186 * NUMBER SCHED LINK FLOWSET
1187 * 1 .. N-1 (1)WFQ (2)WFQ (3)queue
1188 * N+1 .. 2N-1 (4)FIFO (5)FIFO (6)FIFO for sched 1..N-1
1189 * 2N+1 .. 3N-1 -- -- (7)FIFO for sched N+1..2N-1
1191 * "pipe i config" configures #1, #2 and #3
1192 * "sched i config" configures #1 and possibly #6
1193 * "queue i config" configures #3
1194 * #1 is configured with 'pipe i config' or 'sched i config'
1195 * #2 is configured with 'pipe i config', and created if not
1196 * existing with 'sched i config'
1197 * #3 is configured with 'queue i config'
1198 * #4 is automatically configured after #1, can only be FIFO
1199 * #5 is automatically configured after #2
1200 * #6 is automatically created when #1 is !MULTIQUEUE,
1201 * and can be updated.
1202 * #7 is automatically configured after #2
1206 * configure a link (and its FIFO instance)
1209 config_link(struct dn_link *p, struct dn_id *arg)
1213 if (p->oid.len != sizeof(*p)) {
1214 D("invalid pipe len %d", p->oid.len);
1218 if (i <= 0 || i >= DN_MAX_ID)
1221 * The config program passes parameters as follows:
1222 * bw = bits/second (0 means no limits),
1223 * delay = ms, must be translated into ticks.
1224 * qsize = slots/bytes
1227 p->delay = (p->delay * hz) / 1000;
1228 /* Scale burst size: bytes -> bits * hz */
1232 /* do it twice, base link and FIFO link */
1233 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1234 struct dn_schk *s = locate_scheduler(i);
1237 D("sched %d not found", i);
1240 /* remove profile if exists */
1242 free(s->profile, M_DUMMYNET);
1245 /* copy all parameters */
1246 s->link.oid = p->oid;
1247 s->link.link_nr = i;
1248 s->link.delay = p->delay;
1249 if (s->link.bandwidth != p->bandwidth) {
1250 /* XXX bandwidth changes, need to update red params */
1251 s->link.bandwidth = p->bandwidth;
1254 s->link.burst = p->burst;
1255 schk_reset_credit(s);
1263 * configure a flowset. Can be called from inside with locked=1,
1265 static struct dn_fsk *
1266 config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
1271 if (nfs->oid.len != sizeof(*nfs)) {
1272 D("invalid flowset len %d", nfs->oid.len);
1276 if (i <= 0 || i >= 3*DN_MAX_ID)
1278 ND("flowset %d", i);
1279 /* XXX other sanity checks */
1280 if (nfs->flags & DN_QSIZE_BYTES) {
1281 ipdn_bound_var(&nfs->qsize, 16384,
1282 1500, dn_cfg.byte_limit, NULL); // "queue byte size");
1284 ipdn_bound_var(&nfs->qsize, 50,
1285 1, dn_cfg.slot_limit, NULL); // "queue slot size");
1287 if (nfs->flags & DN_HAVE_MASK) {
1288 /* make sure we have some buckets */
1289 ipdn_bound_var(&nfs->buckets, dn_cfg.hash_size,
1290 1, dn_cfg.max_hash_size, "flowset buckets");
1292 nfs->buckets = 1; /* we only need 1 */
1296 do { /* exit with break when done */
1298 int flags = nfs->sched_nr ? DNHT_INSERT : 0;
1300 int oldc = dn_cfg.fsk_count;
1301 fs = dn_ht_find(dn_cfg.fshash, i, flags, NULL);
1303 D("missing sched for flowset %d", i);
1306 /* grab some defaults from the existing one */
1307 if (nfs->sched_nr == 0) /* reuse */
1308 nfs->sched_nr = fs->fs.sched_nr;
1309 for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
1310 if (nfs->par[j] == -1) /* reuse */
1311 nfs->par[j] = fs->fs.par[j];
1313 if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
1314 ND("flowset %d unchanged", i);
1315 break; /* no change, nothing to do */
1317 if (oldc != dn_cfg.fsk_count) /* new item */
1319 s = locate_scheduler(nfs->sched_nr);
1320 /* detach from old scheduler if needed, preserving
1321 * queues if we need to reattach. Then update the
1322 * configuration, and possibly attach to the new sched.
1324 DX(2, "fs %d changed sched %d@%p to %d@%p",
1326 fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
1328 int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
1329 flags |= DN_DESTROY; /* XXX temporary */
1330 fsk_detach(fs, flags);
1332 fs->fs = *nfs; /* copy configuration */
1342 * config/reconfig a scheduler and its FIFO variant.
1343 * For !MULTIQUEUE schedulers, also set up the flowset.
1345 * On reconfigurations (detected because s->fp is set),
1346 * detach existing flowsets preserving traffic, preserve link,
1347 * and delete the old scheduler creating a new one.
1350 config_sched(struct dn_sch *_nsch, struct dn_id *arg)
1353 struct schk_new_arg a; /* argument for schk_new */
1355 struct dn_link p; /* copy of oldlink */
1356 struct dn_profile *pf = NULL; /* copy of old link profile */
1357 /* Used to preserv mask parameter */
1358 struct ipfw_flow_id new_mask;
1359 int new_buckets = 0;
1365 if (a.sch->oid.len != sizeof(*a.sch)) {
1366 D("bad sched len %d", a.sch->oid.len);
1369 i = a.sch->sched_nr;
1370 if (i <= 0 || i >= DN_MAX_ID)
1372 /* make sure we have some buckets */
1373 if (a.sch->flags & DN_HAVE_MASK)
1374 ipdn_bound_var(&a.sch->buckets, dn_cfg.hash_size,
1375 1, dn_cfg.max_hash_size, "sched buckets");
1376 /* XXX other sanity checks */
1377 bzero(&p, sizeof(p));
1379 pipe_cmd = a.sch->flags & DN_PIPE_CMD;
1380 a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
1382 /* Copy mask parameter */
1383 new_mask = a.sch->sched_mask;
1384 new_buckets = a.sch->buckets;
1385 new_flags = a.sch->flags;
1388 again: /* run twice, for wfq and fifo */
1390 * lookup the type. If not supplied, use the previous one
1391 * or default to WF2Q+. Otherwise, return an error.
1394 a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
1396 /* found. Lookup or create entry */
1397 s = dn_ht_find(dn_cfg.schedhash, i, DNHT_INSERT, &a);
1398 } else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
1399 /* No type. search existing s* or retry with WF2Q+ */
1400 s = dn_ht_find(dn_cfg.schedhash, i, 0, &a);
1403 /* Scheduler exists, skip to FIFO scheduler
1404 * if command was pipe config...
1409 /* New scheduler, create a wf2q+ with no mask
1410 * if command was pipe config...
1413 /* clear mask parameter */
1414 bzero(&a.sch->sched_mask, sizeof(new_mask));
1416 a.sch->flags &= ~DN_HAVE_MASK;
1418 a.sch->oid.subtype = DN_SCHED_WF2QP;
1422 D("invalid scheduler type %d %s",
1423 a.sch->oid.subtype, a.sch->name);
1427 /* normalize name and subtype */
1428 a.sch->oid.subtype = a.fp->type;
1429 bzero(a.sch->name, sizeof(a.sch->name));
1430 strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
1432 D("cannot allocate scheduler %d", i);
1435 /* restore existing link if any */
1438 if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
1439 s->profile = NULL; /* XXX maybe not needed */
1441 s->profile = malloc(sizeof(struct dn_profile),
1442 M_DUMMYNET, M_NOWAIT | M_ZERO);
1443 if (s->profile == NULL) {
1444 D("cannot allocate profile");
1447 bcopy(pf, s->profile, sizeof(*pf));
1451 if (s->fp == NULL) {
1452 DX(2, "sched %d new type %s", i, a.fp->name);
1453 } else if (s->fp != a.fp ||
1454 bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
1455 /* already existing. */
1456 DX(2, "sched %d type changed from %s to %s",
1457 i, s->fp->name, a.fp->name);
1458 DX(4, " type/sub %d/%d -> %d/%d",
1459 s->sch.oid.type, s->sch.oid.subtype,
1460 a.sch->oid.type, a.sch->oid.subtype);
1461 if (s->link.link_nr == 0)
1462 D("XXX WARNING link 0 for sched %d", i);
1463 p = s->link; /* preserve link */
1464 if (s->profile) {/* preserve profile */
1466 pf = malloc(sizeof(*pf),
1467 M_DUMMYNET, M_NOWAIT | M_ZERO);
1468 if (pf) /* XXX should issue a warning otherwise */
1469 bcopy(s->profile, pf, sizeof(*pf));
1471 /* remove from the hash */
1472 dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
1473 /* Detach flowsets, preserve queues. */
1474 // schk_delete_cb(s, NULL);
1475 // XXX temporarily, kill queues
1476 schk_delete_cb(s, (void *)DN_DESTROY);
1479 DX(4, "sched %d unchanged type %s", i, a.fp->name);
1481 /* complete initialization */
1485 // XXX schk_reset_credit(s);
1486 /* create the internal flowset if needed,
1487 * trying to reuse existing ones if available
1489 if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
1490 s->fs = dn_ht_find(dn_cfg.fshash, i, 0, NULL);
1493 bzero(&fs, sizeof(fs));
1494 set_oid(&fs.oid, DN_FS, sizeof(fs));
1495 fs.fs_nr = i + DN_MAX_ID;
1497 s->fs = config_fs(&fs, NULL, 1 /* locked */);
1500 schk_delete_cb(s, (void *)DN_DESTROY);
1501 D("error creating internal fs for %d", i);
1505 /* call init function after the flowset is created */
1510 if (i < DN_MAX_ID) { /* now configure the FIFO instance */
1513 /* Restore mask parameter for FIFO */
1514 a.sch->sched_mask = new_mask;
1515 a.sch->buckets = new_buckets;
1516 a.sch->flags = new_flags;
1518 /* sched config shouldn't modify the FIFO scheduler */
1519 if (dn_ht_find(dn_cfg.schedhash, i, 0, &a) != NULL) {
1520 /* FIFO already exist, don't touch it */
1521 err = 0; /* and this is not an error */
1525 a.sch->sched_nr = i;
1526 a.sch->oid.subtype = DN_SCHED_FIFO;
1527 bzero(a.sch->name, sizeof(a.sch->name));
1534 free(pf, M_DUMMYNET);
1539 * attach a profile to a link
1542 config_profile(struct dn_profile *pf, struct dn_id *arg)
1545 int i, olen, err = 0;
1547 if (pf->oid.len < sizeof(*pf)) {
1548 D("short profile len %d", pf->oid.len);
1552 if (i <= 0 || i >= DN_MAX_ID)
1554 /* XXX other sanity checks */
1556 for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1557 s = locate_scheduler(i);
1565 * If we had a profile and the new one does not fit,
1566 * or it is deleted, then we need to free memory.
1568 if (s->profile && (pf->samples_no == 0 ||
1569 s->profile->oid.len < pf->oid.len)) {
1570 free(s->profile, M_DUMMYNET);
1573 if (pf->samples_no == 0)
1576 * new profile, possibly allocate memory
1579 if (s->profile == NULL)
1580 s->profile = malloc(pf->oid.len,
1581 M_DUMMYNET, M_NOWAIT | M_ZERO);
1582 if (s->profile == NULL) {
1583 D("no memory for profile %d", i);
1587 /* preserve larger length XXX double check */
1588 olen = s->profile->oid.len;
1589 if (olen < pf->oid.len)
1591 bcopy(pf, s->profile, pf->oid.len);
1592 s->profile->oid.len = olen;
1599 * Delete all objects:
1602 dummynet_flush(void)
1605 /* delete all schedulers and related links/queues/flowsets */
1606 dn_ht_scan(dn_cfg.schedhash, schk_delete_cb,
1607 (void *)(uintptr_t)DN_DELETE_FS);
1608 /* delete all remaining (unlinked) flowsets */
1609 DX(4, "still %d unlinked fs", dn_cfg.fsk_count);
1610 dn_ht_free(dn_cfg.fshash, DNHT_REMOVE);
1611 fsk_detach_list(&dn_cfg.fsu, DN_DELETE_FS);
1612 /* Reinitialize system heap... */
1613 heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
1617 * Main handler for configuration. We are guaranteed to be called
1618 * with an oid which is at least a dn_id.
1619 * - the first object is the command (config, delete, flush, ...)
1620 * - config_link must be issued after the corresponding config_sched
1621 * - parameters (DN_TXT) for an object must preceed the object
1622 * processed on a config_sched.
1625 do_config(void *p, int l)
1627 struct dn_id *next, *o;
1628 int err = 0, err2 = 0;
1629 struct dn_id *arg = NULL;
1633 if (o->id != DN_API_VERSION) {
1634 D("invalid api version got %d need %d",
1635 o->id, DN_API_VERSION);
1638 for (; l >= sizeof(*o); o = next) {
1639 struct dn_id *prev = arg;
1640 if (o->len < sizeof(*o) || l < o->len) {
1641 D("bad len o->len %d len %d", o->len, l);
1646 next = (struct dn_id *)((char *)o + o->len);
1650 D("cmd %d not implemented", o->type);
1653 #ifdef EMULATE_SYSCTL
1654 /* sysctl emulation.
1655 * if we recognize the command, jump to the correct
1656 * handler and return
1659 err = kesysctl_emu_set(p, l);
1663 case DN_CMD_CONFIG: /* simply a header */
1667 /* the argument is in the first uintptr_t after o */
1668 a = (uintptr_t *)(o+1);
1669 if (o->len < sizeof(*o) + sizeof(*a)) {
1673 switch (o->subtype) {
1675 /* delete base and derived schedulers */
1677 err = delete_schk(*a);
1678 err2 = delete_schk(*a + DN_MAX_ID);
1685 D("invalid delete type %d",
1691 err = (*a <1 || *a >= DN_MAX_ID) ?
1692 EINVAL : delete_fs(*a, 0) ;
1702 case DN_TEXT: /* store argument the next block */
1707 err = config_link((struct dn_link *)o, arg);
1710 err = config_profile((struct dn_profile *)o, arg);
1713 err = config_sched((struct dn_sch *)o, arg);
1716 err = (NULL==config_fs((struct dn_fs *)o, arg, 0));
1728 compute_space(struct dn_id *cmd, struct copy_args *a)
1730 int x = 0, need = 0;
1731 int profile_size = sizeof(struct dn_profile) -
1732 ED_MAX_SAMPLES_NO*sizeof(int);
1734 /* NOTE about compute space:
1735 * NP = dn_cfg.schk_count
1736 * NSI = dn_cfg.si_count
1737 * NF = dn_cfg.fsk_count
1738 * NQ = dn_cfg.queue_count
1740 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
1741 * link, scheduler template, flowset
1742 * integrated in scheduler and header
1744 * (NSI)*(dn_flow) all scheduler instance (includes
1745 * the queue instance)
1747 * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
1748 * link, scheduler template, flowset
1749 * integrated in scheduler and header
1751 * (NSI * dn_flow) all scheduler instances
1752 * (NF * sizeof(uint_32)) space for flowset list linked to scheduler
1753 * (NQ * dn_queue) all queue [XXXfor now not listed]
1755 * (NF * dn_fs) all flowset
1756 * (NQ * dn_queue) all queues
1758 switch (cmd->subtype) {
1761 /* XXX where do LINK and SCH differ ? */
1762 /* 'ipfw sched show' could list all queues associated to
1763 * a scheduler. This feature for now is disabled
1765 case DN_LINK: /* pipe show */
1766 x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
1767 need += dn_cfg.schk_count *
1768 (sizeof(struct dn_fs) + profile_size) / 2;
1769 need += dn_cfg.fsk_count * sizeof(uint32_t);
1771 case DN_SCH: /* sched show */
1772 need += dn_cfg.schk_count *
1773 (sizeof(struct dn_fs) + profile_size) / 2;
1774 need += dn_cfg.fsk_count * sizeof(uint32_t);
1775 x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
1777 case DN_FS: /* queue show */
1778 x = DN_C_FS | DN_C_QUEUE;
1780 case DN_GET_COMPAT: /* compatibility mode */
1781 need = dn_compat_calc_size();
1786 need += dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
1787 /* NOT also, each fs might be attached to a sched */
1788 need += dn_cfg.schk_count * sizeof(struct dn_id) / 2;
1791 need += dn_cfg.fsk_count * sizeof(struct dn_fs);
1792 if (x & DN_C_LINK) {
1793 need += dn_cfg.schk_count * sizeof(struct dn_link) / 2;
1796 * When exporting a queue to userland, only pass up the
1797 * struct dn_flow, which is the only visible part.
1801 need += dn_cfg.queue_count * sizeof(struct dn_flow);
1803 need += dn_cfg.si_count * (sizeof(struct dn_flow));
1808 * If compat != NULL dummynet_get is called in compatibility mode.
1809 * *compat will be the pointer to the buffer to pass to ipfw
1812 dummynet_get(struct sockopt *sopt, void **compat)
1814 int have, i, need, error;
1815 char *start = NULL, *buf;
1816 size_t sopt_valsize;
1819 struct copy_range r;
1820 int l = sizeof(struct dn_id);
1822 bzero(&a, sizeof(a));
1823 bzero(&r, sizeof(r));
1825 /* save and restore original sopt_valsize around copyin */
1826 sopt_valsize = sopt->sopt_valsize;
1831 /* copy at least an oid, and possibly a full object */
1832 error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
1833 sopt->sopt_valsize = sopt_valsize;
1837 #ifdef EMULATE_SYSCTL
1838 /* sysctl emulation. */
1839 if (cmd->type == DN_SYSCTL_GET)
1840 return kesysctl_emu_get(sopt);
1842 if (l > sizeof(r)) {
1843 /* request larger than default, allocate buffer */
1844 cmd = malloc(l, M_DUMMYNET, M_WAITOK);
1845 error = sooptcopyin(sopt, cmd, l, l);
1846 sopt->sopt_valsize = sopt_valsize;
1850 } else { /* compatibility */
1852 cmd->type = DN_CMD_GET;
1853 cmd->len = sizeof(struct dn_id);
1854 cmd->subtype = DN_GET_COMPAT;
1855 // cmd->id = sopt_valsize;
1856 D("compatibility mode");
1858 a.extra = (struct copy_range *)cmd;
1859 if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
1860 uint32_t *rp = (uint32_t *)(cmd + 1);
1861 cmd->len += 2* sizeof(uint32_t);
1863 rp[1] = DN_MAX_ID - 1;
1864 if (cmd->subtype == DN_LINK) {
1869 /* Count space (under lock) and allocate (outside lock).
1870 * Exit with lock held if we manage to get enough buffer.
1871 * Try a few times then give up.
1873 for (have = 0, i = 0; i < 10; i++) {
1875 need = compute_space(cmd, &a);
1877 /* if there is a range, ignore value from compute_space() */
1878 if (l > sizeof(*cmd))
1879 need = sopt_valsize - sizeof(*cmd);
1886 need += sizeof(*cmd);
1893 free(start, M_DUMMYNET);
1895 if (need > sopt_valsize)
1899 start = malloc(have, M_DUMMYNET, M_WAITOK | M_ZERO);
1902 if (start == NULL) {
1907 error = sooptcopyout(sopt, cmd, sizeof(*cmd));
1911 ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
1912 "%d:%d si %d, %d:%d queues %d",
1913 dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
1914 dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
1915 dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
1916 dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
1917 dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
1918 sopt->sopt_valsize = sopt_valsize;
1919 a.type = cmd->subtype;
1921 if (compat == NULL) {
1922 bcopy(cmd, start, sizeof(*cmd));
1923 ((struct dn_id*)(start))->len = sizeof(struct dn_id);
1924 buf = start + sizeof(*cmd);
1928 a.end = start + have;
1929 /* start copying other objects */
1931 a.type = DN_COMPAT_PIPE;
1932 dn_ht_scan(dn_cfg.schedhash, copy_data_helper_compat, &a);
1933 a.type = DN_COMPAT_QUEUE;
1934 dn_ht_scan(dn_cfg.fshash, copy_data_helper_compat, &a);
1935 } else if (a.type == DN_FS) {
1936 dn_ht_scan(dn_cfg.fshash, copy_data_helper, &a);
1938 dn_ht_scan(dn_cfg.schedhash, copy_data_helper, &a);
1944 sopt->sopt_valsize = buf - start;
1945 /* free() is done by ip_dummynet_compat() */
1946 start = NULL; //XXX hack
1948 error = sooptcopyout(sopt, start, buf - start);
1951 if (cmd && cmd != &r.o)
1952 free(cmd, M_DUMMYNET);
1954 free(start, M_DUMMYNET);
1958 /* Callback called on scheduler instance to delete it if idle */
1960 drain_scheduler_cb(void *_si, void *arg)
1962 struct dn_sch_inst *si = _si;
1964 if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
1967 if (si->sched->fp->flags & DN_MULTIQUEUE) {
1968 if (si->q_count == 0)
1969 return si_destroy(si, NULL);
1972 } else { /* !DN_MULTIQUEUE */
1973 if ((si+1)->ni.length == 0)
1974 return si_destroy(si, NULL);
1978 return 0; /* unreachable */
1981 /* Callback called on scheduler to check if it has instances */
1983 drain_scheduler_sch_cb(void *_s, void *arg)
1985 struct dn_schk *s = _s;
1987 if (s->sch.flags & DN_HAVE_MASK) {
1988 dn_ht_scan_bucket(s->siht, &s->drain_bucket,
1989 drain_scheduler_cb, NULL);
1993 if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
2000 /* Called every tick, try to delete a 'bucket' of scheduler */
2002 dn_drain_scheduler(void)
2004 dn_ht_scan_bucket(dn_cfg.schedhash, &dn_cfg.drain_sch,
2005 drain_scheduler_sch_cb, NULL);
2009 /* Callback called on queue to delete if it is idle */
2011 drain_queue_cb(void *_q, void *arg)
2013 struct dn_queue *q = _q;
2015 if (q->ni.length == 0) {
2016 dn_delete_queue(q, DN_DESTROY);
2017 return DNHT_SCAN_DEL; /* queue is deleted */
2020 return 0; /* queue isn't deleted */
2023 /* Callback called on flowset used to check if it has queues */
2025 drain_queue_fs_cb(void *_fs, void *arg)
2027 struct dn_fsk *fs = _fs;
2029 if (fs->fs.flags & DN_QHT_HASH) {
2030 /* Flowset has a hash table for queues */
2031 dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
2032 drain_queue_cb, NULL);
2035 /* No hash table for this flowset, null the pointer
2036 * if the queue is deleted
2039 if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
2046 /* Called every tick, try to delete a 'bucket' of queue */
2048 dn_drain_queue(void)
2050 /* scan a bucket of flowset */
2051 dn_ht_scan_bucket(dn_cfg.fshash, &dn_cfg.drain_fs,
2052 drain_queue_fs_cb, NULL);
2057 * Handler for the various dummynet socket options
2060 ip_dn_ctl(struct sockopt *sopt)
2065 error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
2069 /* Disallow sets in really-really secure mode. */
2070 if (sopt->sopt_dir == SOPT_SET) {
2071 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2076 switch (sopt->sopt_name) {
2078 D("dummynet: unknown option %d", sopt->sopt_name);
2082 case IP_DUMMYNET_FLUSH:
2083 case IP_DUMMYNET_CONFIGURE:
2084 case IP_DUMMYNET_DEL: /* remove a pipe or queue */
2085 case IP_DUMMYNET_GET:
2086 D("dummynet: compat option %d", sopt->sopt_name);
2087 error = ip_dummynet_compat(sopt);
2091 if (sopt->sopt_dir == SOPT_GET) {
2092 error = dummynet_get(sopt, NULL);
2095 l = sopt->sopt_valsize;
2096 if (l < sizeof(struct dn_id) || l > 12000) {
2097 D("argument len %d invalid", l);
2100 p = malloc(l, M_TEMP, M_WAITOK); // XXX can it fail ?
2101 error = sooptcopyin(sopt, p, l, l);
2104 error = do_config(p, l);
2118 if (dn_cfg.init_done)
2120 printf("DUMMYNET %p with IPv6 initialized (100409)\n", curvnet);
2121 dn_cfg.init_done = 1;
2122 /* Set defaults here. MSVC does not accept initializers,
2123 * and this is also useful for vimages
2126 dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
2127 dn_cfg.byte_limit = 1024 * 1024;
2130 /* RED parameters */
2131 dn_cfg.red_lookup_depth = 256; /* default lookup table depth */
2132 dn_cfg.red_avg_pkt_size = 512; /* default medium packet size */
2133 dn_cfg.red_max_pkt_size = 1500; /* default max packet size */
2136 dn_cfg.max_hash_size = 65536; /* max in the hash tables */
2137 dn_cfg.hash_size = 64; /* default hash size */
2139 /* create hash tables for schedulers and flowsets.
2140 * In both we search by key and by pointer.
2142 dn_cfg.schedhash = dn_ht_init(NULL, dn_cfg.hash_size,
2143 offsetof(struct dn_schk, schk_next),
2144 schk_hash, schk_match, schk_new);
2145 dn_cfg.fshash = dn_ht_init(NULL, dn_cfg.hash_size,
2146 offsetof(struct dn_fsk, fsk_next),
2147 fsk_hash, fsk_match, fsk_new);
2149 /* bucket index to drain object */
2150 dn_cfg.drain_fs = 0;
2151 dn_cfg.drain_sch = 0;
2153 heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
2154 SLIST_INIT(&dn_cfg.fsu);
2155 SLIST_INIT(&dn_cfg.schedlist);
2159 TASK_INIT(&dn_task, 0, dummynet_task, curvnet);
2160 dn_tq = taskqueue_create("dummynet", M_WAITOK,
2161 taskqueue_thread_enqueue, &dn_tq);
2162 taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
2164 callout_init(&dn_timeout, CALLOUT_MPSAFE);
2165 callout_reset(&dn_timeout, 1, dummynet, NULL);
2167 /* Initialize curr_time adjustment mechanics. */
2168 getmicrouptime(&dn_cfg.prev_t);
2173 ip_dn_destroy(int last)
2175 callout_drain(&dn_timeout);
2179 ND("removing last instance\n");
2180 ip_dn_ctl_ptr = NULL;
2181 ip_dn_io_ptr = NULL;
2186 taskqueue_drain(dn_tq, &dn_task);
2187 taskqueue_free(dn_tq);
2189 dn_ht_free(dn_cfg.schedhash, 0);
2190 dn_ht_free(dn_cfg.fshash, 0);
2191 heap_free(&dn_cfg.evheap);
2195 #endif /* KLD_MODULE */
2198 dummynet_modevent(module_t mod, int type, void *data)
2201 if (type == MOD_LOAD) {
2203 printf("DUMMYNET already loaded\n");
2207 ip_dn_ctl_ptr = ip_dn_ctl;
2208 ip_dn_io_ptr = dummynet_io;
2210 } else if (type == MOD_UNLOAD) {
2211 #if !defined(KLD_MODULE)
2212 printf("dummynet statically compiled, cannot unload\n");
2215 ip_dn_destroy(1 /* last */);
2222 /* modevent helpers for the modules */
2224 load_dn_sched(struct dn_alg *d)
2229 return 1; /* error */
2230 ip_dn_init(); /* just in case, we need the lock */
2232 /* Check that mandatory funcs exists */
2233 if (d->enqueue == NULL || d->dequeue == NULL) {
2234 D("missing enqueue or dequeue for %s", d->name);
2238 /* Search if scheduler already exists */
2240 SLIST_FOREACH(s, &dn_cfg.schedlist, next) {
2241 if (strcmp(s->name, d->name) == 0) {
2242 D("%s already loaded", d->name);
2243 break; /* scheduler already exists */
2247 SLIST_INSERT_HEAD(&dn_cfg.schedlist, d, next);
2249 D("dn_sched %s %sloaded", d->name, s ? "not ":"");
2254 unload_dn_sched(struct dn_alg *s)
2256 struct dn_alg *tmp, *r;
2259 ND("called for %s", s->name);
2262 SLIST_FOREACH_SAFE(r, &dn_cfg.schedlist, next, tmp) {
2263 if (strcmp(s->name, r->name) != 0)
2265 ND("ref_count = %d", r->ref_count);
2266 err = (r->ref_count != 0) ? EBUSY : 0;
2268 SLIST_REMOVE(&dn_cfg.schedlist, r, dn_alg, next);
2272 D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
2277 dn_sched_modevent(module_t mod, int cmd, void *arg)
2279 struct dn_alg *sch = arg;
2281 if (cmd == MOD_LOAD)
2282 return load_dn_sched(sch);
2283 else if (cmd == MOD_UNLOAD)
2284 return unload_dn_sched(sch);
2289 static moduledata_t dummynet_mod = {
2290 "dummynet", dummynet_modevent, NULL
2293 #define DN_SI_SUB SI_SUB_PROTO_IFATTACHDOMAIN
2294 #define DN_MODEV_ORD (SI_ORDER_ANY - 128) /* after ipfw */
2295 DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
2296 MODULE_DEPEND(dummynet, ipfw, 2, 2, 2);
2297 MODULE_VERSION(dummynet, 3);
2300 * Starting up. Done in order after dummynet_modevent() has been called.
2301 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2303 //VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_init, NULL);
2306 * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
2307 * after dummynet_modevent() has been called. Not called on reboot.
2308 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2309 * or when the module is unloaded.
2311 //VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_destroy, NULL);