ip_dummynet.c revision 318155
1/*-
2 * Codel/FQ_Codel and PIE/FQ-PIE Code:
3 * Copyright (C) 2016 Centre for Advanced Internet Architectures,
4 *  Swinburne University of Technology, Melbourne, Australia.
5 * Portions of this code were made possible in part by a gift from
6 *  The Comcast Innovation Fund.
7 * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
8 *
9 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
10 * Portions Copyright (c) 2000 Akamba Corp.
11 * All rights reserved
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/10/sys/netpfil/ipfw/ip_dummynet.c 318155 2017-05-10 20:46:59Z marius $");
37
38/*
39 * Configuration and internal object management for dummynet.
40 */
41
42#include "opt_inet6.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/malloc.h>
47#include <sys/mbuf.h>
48#include <sys/kernel.h>
49#include <sys/lock.h>
50#include <sys/module.h>
51#include <sys/priv.h>
52#include <sys/proc.h>
53#include <sys/rwlock.h>
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#include <sys/time.h>
57#include <sys/taskqueue.h>
58#include <net/if.h>	/* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
59#include <netinet/in.h>
60#include <netinet/ip_var.h>	/* ip_output(), IP_FORWARDING */
61#include <netinet/ip_fw.h>
62#include <netinet/ip_dummynet.h>
63
64#include <netpfil/ipfw/ip_fw_private.h>
65#include <netpfil/ipfw/dn_heap.h>
66#include <netpfil/ipfw/ip_dn_private.h>
67#ifdef NEW_AQM
68#include <netpfil/ipfw/dn_aqm.h>
69#endif
70#include <netpfil/ipfw/dn_sched.h>
71
72/* which objects to copy */
73#define DN_C_LINK 	0x01
74#define DN_C_SCH	0x02
75#define DN_C_FLOW	0x04
76#define DN_C_FS		0x08
77#define DN_C_QUEUE	0x10
78
79/* we use this argument in case of a schk_new */
80struct schk_new_arg {
81	struct dn_alg *fp;
82	struct dn_sch *sch;
83};
84
85/*---- callout hooks. ----*/
86static struct callout dn_timeout;
87static int dn_gone;
88static struct task	dn_task;
89static struct taskqueue	*dn_tq = NULL;
90
91static void
92dummynet(void *arg)
93{
94
95	(void)arg;	/* UNUSED */
96	taskqueue_enqueue_fast(dn_tq, &dn_task);
97}
98
99void
100dn_reschedule(void)
101{
102
103	if (dn_gone != 0)
104		return;
105	callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL,
106	    C_HARDCLOCK | C_DIRECT_EXEC);
107}
108/*----- end of callout hooks -----*/
109
110#ifdef NEW_AQM
111/* Return AQM descriptor for given type or name. */
112static struct dn_aqm *
113find_aqm_type(int type, char *name)
114{
115	struct dn_aqm *d;
116
117	SLIST_FOREACH(d, &dn_cfg.aqmlist, next) {
118		if (d->type == type || (name && !strcasecmp(d->name, name)))
119			return d;
120	}
121	return NULL; /* not found */
122}
123#endif
124
125/* Return a scheduler descriptor given the type or name. */
126static struct dn_alg *
127find_sched_type(int type, char *name)
128{
129	struct dn_alg *d;
130
131	SLIST_FOREACH(d, &dn_cfg.schedlist, next) {
132		if (d->type == type || (name && !strcasecmp(d->name, name)))
133			return d;
134	}
135	return NULL; /* not found */
136}
137
138int
139ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
140{
141	int oldv = *v;
142	const char *op = NULL;
143	if (dflt < lo)
144		dflt = lo;
145	if (dflt > hi)
146		dflt = hi;
147	if (oldv < lo) {
148		*v = dflt;
149		op = "Bump";
150	} else if (oldv > hi) {
151		*v = hi;
152		op = "Clamp";
153	} else
154		return *v;
155	if (op && msg)
156		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
157	return *v;
158}
159
160/*---- flow_id mask, hash and compare functions ---*/
161/*
162 * The flow_id includes the 5-tuple, the queue/pipe number
163 * which we store in the extra area in host order,
164 * and for ipv6 also the flow_id6.
165 * XXX see if we want the tos byte (can store in 'flags')
166 */
167static struct ipfw_flow_id *
168flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
169{
170	int is_v6 = IS_IP6_FLOW_ID(id);
171
172	id->dst_port &= mask->dst_port;
173	id->src_port &= mask->src_port;
174	id->proto &= mask->proto;
175	id->extra &= mask->extra;
176	if (is_v6) {
177		APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
178		APPLY_MASK(&id->src_ip6, &mask->src_ip6);
179		id->flow_id6 &= mask->flow_id6;
180	} else {
181		id->dst_ip &= mask->dst_ip;
182		id->src_ip &= mask->src_ip;
183	}
184	return id;
185}
186
187/* computes an OR of two masks, result in dst and also returned */
188static struct ipfw_flow_id *
189flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
190{
191	int is_v6 = IS_IP6_FLOW_ID(dst);
192
193	dst->dst_port |= src->dst_port;
194	dst->src_port |= src->src_port;
195	dst->proto |= src->proto;
196	dst->extra |= src->extra;
197	if (is_v6) {
198#define OR_MASK(_d, _s)                          \
199    (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
200    (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
201    (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
202    (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
203		OR_MASK(&dst->dst_ip6, &src->dst_ip6);
204		OR_MASK(&dst->src_ip6, &src->src_ip6);
205#undef OR_MASK
206		dst->flow_id6 |= src->flow_id6;
207	} else {
208		dst->dst_ip |= src->dst_ip;
209		dst->src_ip |= src->src_ip;
210	}
211	return dst;
212}
213
214static int
215nonzero_mask(struct ipfw_flow_id *m)
216{
217	if (m->dst_port || m->src_port || m->proto || m->extra)
218		return 1;
219	if (IS_IP6_FLOW_ID(m)) {
220		return
221			m->dst_ip6.__u6_addr.__u6_addr32[0] ||
222			m->dst_ip6.__u6_addr.__u6_addr32[1] ||
223			m->dst_ip6.__u6_addr.__u6_addr32[2] ||
224			m->dst_ip6.__u6_addr.__u6_addr32[3] ||
225			m->src_ip6.__u6_addr.__u6_addr32[0] ||
226			m->src_ip6.__u6_addr.__u6_addr32[1] ||
227			m->src_ip6.__u6_addr.__u6_addr32[2] ||
228			m->src_ip6.__u6_addr.__u6_addr32[3] ||
229			m->flow_id6;
230	} else {
231		return m->dst_ip || m->src_ip;
232	}
233}
234
235/* XXX we may want a better hash function */
236static uint32_t
237flow_id_hash(struct ipfw_flow_id *id)
238{
239    uint32_t i;
240
241    if (IS_IP6_FLOW_ID(id)) {
242	uint32_t *d = (uint32_t *)&id->dst_ip6;
243	uint32_t *s = (uint32_t *)&id->src_ip6;
244        i = (d[0]      ) ^ (d[1])       ^
245            (d[2]      ) ^ (d[3])       ^
246            (d[0] >> 15) ^ (d[1] >> 15) ^
247            (d[2] >> 15) ^ (d[3] >> 15) ^
248            (s[0] <<  1) ^ (s[1] <<  1) ^
249            (s[2] <<  1) ^ (s[3] <<  1) ^
250            (s[0] << 16) ^ (s[1] << 16) ^
251            (s[2] << 16) ^ (s[3] << 16) ^
252            (id->dst_port << 1) ^ (id->src_port) ^
253	    (id->extra) ^
254            (id->proto ) ^ (id->flow_id6);
255    } else {
256        i = (id->dst_ip)        ^ (id->dst_ip >> 15) ^
257            (id->src_ip << 1)   ^ (id->src_ip >> 16) ^
258	    (id->extra) ^
259            (id->dst_port << 1) ^ (id->src_port)     ^ (id->proto);
260    }
261    return i;
262}
263
264/* Like bcmp, returns 0 if ids match, 1 otherwise. */
265static int
266flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
267{
268	int is_v6 = IS_IP6_FLOW_ID(id1);
269
270	if (!is_v6) {
271	    if (IS_IP6_FLOW_ID(id2))
272		return 1; /* different address families */
273
274	    return (id1->dst_ip == id2->dst_ip &&
275		    id1->src_ip == id2->src_ip &&
276		    id1->dst_port == id2->dst_port &&
277		    id1->src_port == id2->src_port &&
278		    id1->proto == id2->proto &&
279		    id1->extra == id2->extra) ? 0 : 1;
280	}
281	/* the ipv6 case */
282	return (
283	    !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
284	    !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
285	    id1->dst_port == id2->dst_port &&
286	    id1->src_port == id2->src_port &&
287	    id1->proto == id2->proto &&
288	    id1->extra == id2->extra &&
289	    id1->flow_id6 == id2->flow_id6) ? 0 : 1;
290}
291/*--------- end of flow-id mask, hash and compare ---------*/
292
293/*--- support functions for the qht hashtable ----
294 * Entries are hashed by flow-id
295 */
296static uint32_t
297q_hash(uintptr_t key, int flags, void *arg)
298{
299	/* compute the hash slot from the flow id */
300	struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
301		&((struct dn_queue *)key)->ni.fid :
302		(struct ipfw_flow_id *)key;
303
304	return flow_id_hash(id);
305}
306
307static int
308q_match(void *obj, uintptr_t key, int flags, void *arg)
309{
310	struct dn_queue *o = (struct dn_queue *)obj;
311	struct ipfw_flow_id *id2;
312
313	if (flags & DNHT_KEY_IS_OBJ) {
314		/* compare pointers */
315		id2 = &((struct dn_queue *)key)->ni.fid;
316	} else {
317		id2 = (struct ipfw_flow_id *)key;
318	}
319	return (0 == flow_id_cmp(&o->ni.fid,  id2));
320}
321
322/*
323 * create a new queue instance for the given 'key'.
324 */
325static void *
326q_new(uintptr_t key, int flags, void *arg)
327{
328	struct dn_queue *q, *template = arg;
329	struct dn_fsk *fs = template->fs;
330	int size = sizeof(*q) + fs->sched->fp->q_datalen;
331
332	q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
333	if (q == NULL) {
334		D("no memory for new queue");
335		return NULL;
336	}
337
338	set_oid(&q->ni.oid, DN_QUEUE, size);
339	if (fs->fs.flags & DN_QHT_HASH)
340		q->ni.fid = *(struct ipfw_flow_id *)key;
341	q->fs = fs;
342	q->_si = template->_si;
343	q->_si->q_count++;
344
345	if (fs->sched->fp->new_queue)
346		fs->sched->fp->new_queue(q);
347
348#ifdef NEW_AQM
349	/* call AQM init function after creating a queue*/
350	if (fs->aqmfp && fs->aqmfp->init)
351		if(fs->aqmfp->init(q))
352			D("unable to init AQM for fs %d", fs->fs.fs_nr);
353#endif
354	dn_cfg.queue_count++;
355
356	return q;
357}
358
359/*
360 * Notify schedulers that a queue is going away.
361 * If (flags & DN_DESTROY), also free the packets.
362 * The version for callbacks is called q_delete_cb().
363 */
364static void
365dn_delete_queue(struct dn_queue *q, int flags)
366{
367	struct dn_fsk *fs = q->fs;
368
369#ifdef NEW_AQM
370	/* clean up AQM status for queue 'q'
371	 * cleanup here is called just with MULTIQUEUE
372	 */
373	if (fs && fs->aqmfp && fs->aqmfp->cleanup)
374		fs->aqmfp->cleanup(q);
375#endif
376	// D("fs %p si %p\n", fs, q->_si);
377	/* notify the parent scheduler that the queue is going away */
378	if (fs && fs->sched->fp->free_queue)
379		fs->sched->fp->free_queue(q);
380	q->_si->q_count--;
381	q->_si = NULL;
382	if (flags & DN_DESTROY) {
383		if (q->mq.head)
384			dn_free_pkts(q->mq.head);
385		bzero(q, sizeof(*q));	// safety
386		free(q, M_DUMMYNET);
387		dn_cfg.queue_count--;
388	}
389}
390
391static int
392q_delete_cb(void *q, void *arg)
393{
394	int flags = (int)(uintptr_t)arg;
395	dn_delete_queue(q, flags);
396	return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
397}
398
399/*
400 * calls dn_delete_queue/q_delete_cb on all queues,
401 * which notifies the parent scheduler and possibly drains packets.
402 * flags & DN_DESTROY: drains queues and destroy qht;
403 */
404static void
405qht_delete(struct dn_fsk *fs, int flags)
406{
407	ND("fs %d start flags %d qht %p",
408		fs->fs.fs_nr, flags, fs->qht);
409	if (!fs->qht)
410		return;
411	if (fs->fs.flags & DN_QHT_HASH) {
412		dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
413		if (flags & DN_DESTROY) {
414			dn_ht_free(fs->qht, 0);
415			fs->qht = NULL;
416		}
417	} else {
418		dn_delete_queue((struct dn_queue *)(fs->qht), flags);
419		if (flags & DN_DESTROY)
420			fs->qht = NULL;
421	}
422}
423
424/*
425 * Find and possibly create the queue for a MULTIQUEUE scheduler.
426 * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
427 */
428struct dn_queue *
429ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
430	struct ipfw_flow_id *id)
431{
432	struct dn_queue template;
433
434	template._si = si;
435	template.fs = fs;
436
437	if (fs->fs.flags & DN_QHT_HASH) {
438		struct ipfw_flow_id masked_id;
439		if (fs->qht == NULL) {
440			fs->qht = dn_ht_init(NULL, fs->fs.buckets,
441				offsetof(struct dn_queue, q_next),
442				q_hash, q_match, q_new);
443			if (fs->qht == NULL)
444				return NULL;
445		}
446		masked_id = *id;
447		flow_id_mask(&fs->fsk_mask, &masked_id);
448		return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
449			DNHT_INSERT, &template);
450	} else {
451		if (fs->qht == NULL)
452			fs->qht = q_new(0, 0, &template);
453		return (struct dn_queue *)fs->qht;
454	}
455}
456/*--- end of queue hash table ---*/
457
458/*--- support functions for the sch_inst hashtable ----
459 *
460 * These are hashed by flow-id
461 */
462static uint32_t
463si_hash(uintptr_t key, int flags, void *arg)
464{
465	/* compute the hash slot from the flow id */
466	struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
467		&((struct dn_sch_inst *)key)->ni.fid :
468		(struct ipfw_flow_id *)key;
469
470	return flow_id_hash(id);
471}
472
473static int
474si_match(void *obj, uintptr_t key, int flags, void *arg)
475{
476	struct dn_sch_inst *o = obj;
477	struct ipfw_flow_id *id2;
478
479	id2 = (flags & DNHT_KEY_IS_OBJ) ?
480		&((struct dn_sch_inst *)key)->ni.fid :
481		(struct ipfw_flow_id *)key;
482	return flow_id_cmp(&o->ni.fid,  id2) == 0;
483}
484
485/*
486 * create a new instance for the given 'key'
487 * Allocate memory for instance, delay line and scheduler private data.
488 */
489static void *
490si_new(uintptr_t key, int flags, void *arg)
491{
492	struct dn_schk *s = arg;
493	struct dn_sch_inst *si;
494	int l = sizeof(*si) + s->fp->si_datalen;
495
496	si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
497	if (si == NULL)
498		goto error;
499
500	/* Set length only for the part passed up to userland. */
501	set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
502	set_oid(&(si->dline.oid), DN_DELAY_LINE,
503		sizeof(struct delay_line));
504	/* mark si and dline as outside the event queue */
505	si->ni.oid.id = si->dline.oid.id = -1;
506
507	si->sched = s;
508	si->dline.si = si;
509
510	if (s->fp->new_sched && s->fp->new_sched(si)) {
511		D("new_sched error");
512		goto error;
513	}
514	if (s->sch.flags & DN_HAVE_MASK)
515		si->ni.fid = *(struct ipfw_flow_id *)key;
516
517#ifdef NEW_AQM
518	/* init AQM status for !DN_MULTIQUEUE sched*/
519	if (!(s->fp->flags & DN_MULTIQUEUE))
520		if (s->fs->aqmfp && s->fs->aqmfp->init)
521			if(s->fs->aqmfp->init((struct dn_queue *)(si + 1))) {
522				D("unable to init AQM for fs %d", s->fs->fs.fs_nr);
523				goto error;
524			}
525#endif
526
527	dn_cfg.si_count++;
528	return si;
529
530error:
531	if (si) {
532		bzero(si, sizeof(*si)); // safety
533		free(si, M_DUMMYNET);
534	}
535        return NULL;
536}
537
538/*
539 * Callback from siht to delete all scheduler instances. Remove
540 * si and delay line from the system heap, destroy all queues.
541 * We assume that all flowset have been notified and do not
542 * point to us anymore.
543 */
544static int
545si_destroy(void *_si, void *arg)
546{
547	struct dn_sch_inst *si = _si;
548	struct dn_schk *s = si->sched;
549	struct delay_line *dl = &si->dline;
550
551	if (dl->oid.subtype) /* remove delay line from event heap */
552		heap_extract(&dn_cfg.evheap, dl);
553	dn_free_pkts(dl->mq.head);	/* drain delay line */
554	if (si->kflags & DN_ACTIVE) /* remove si from event heap */
555		heap_extract(&dn_cfg.evheap, si);
556
557#ifdef NEW_AQM
558	/* clean up AQM status for !DN_MULTIQUEUE sched
559	 * Note that all queues belong to fs were cleaned up in fsk_detach.
560	 * When drain_scheduler is called s->fs and q->fs are pointing
561	 * to a correct fs, so we can use fs in this case.
562	 */
563	if (!(s->fp->flags & DN_MULTIQUEUE)) {
564		struct dn_queue *q = (struct dn_queue *)(si + 1);
565		if (q->aqm_status && q->fs->aqmfp)
566			if (q->fs->aqmfp->cleanup)
567				q->fs->aqmfp->cleanup(q);
568	}
569#endif
570	if (s->fp->free_sched)
571		s->fp->free_sched(si);
572	bzero(si, sizeof(*si));	/* safety */
573	free(si, M_DUMMYNET);
574	dn_cfg.si_count--;
575	return DNHT_SCAN_DEL;
576}
577
578/*
579 * Find the scheduler instance for this packet. If we need to apply
580 * a mask, do on a local copy of the flow_id to preserve the original.
581 * Assume siht is always initialized if we have a mask.
582 */
583struct dn_sch_inst *
584ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
585{
586
587	if (s->sch.flags & DN_HAVE_MASK) {
588		struct ipfw_flow_id id_t = *id;
589		flow_id_mask(&s->sch.sched_mask, &id_t);
590		return dn_ht_find(s->siht, (uintptr_t)&id_t,
591			DNHT_INSERT, s);
592	}
593	if (!s->siht)
594		s->siht = si_new(0, 0, s);
595	return (struct dn_sch_inst *)s->siht;
596}
597
598/* callback to flush credit for the scheduler instance */
599static int
600si_reset_credit(void *_si, void *arg)
601{
602	struct dn_sch_inst *si = _si;
603	struct dn_link *p = &si->sched->link;
604
605	si->credit = p->burst + (dn_cfg.io_fast ?  p->bandwidth : 0);
606	return 0;
607}
608
609static void
610schk_reset_credit(struct dn_schk *s)
611{
612	if (s->sch.flags & DN_HAVE_MASK)
613		dn_ht_scan(s->siht, si_reset_credit, NULL);
614	else if (s->siht)
615		si_reset_credit(s->siht, NULL);
616}
617/*---- end of sch_inst hashtable ---------------------*/
618
619/*-------------------------------------------------------
620 * flowset hash (fshash) support. Entries are hashed by fs_nr.
621 * New allocations are put in the fsunlinked list, from which
622 * they are removed when they point to a specific scheduler.
623 */
624static uint32_t
625fsk_hash(uintptr_t key, int flags, void *arg)
626{
627	uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
628		((struct dn_fsk *)key)->fs.fs_nr;
629
630	return ( (i>>8)^(i>>4)^i );
631}
632
633static int
634fsk_match(void *obj, uintptr_t key, int flags, void *arg)
635{
636	struct dn_fsk *fs = obj;
637	int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
638		((struct dn_fsk *)key)->fs.fs_nr;
639
640	return (fs->fs.fs_nr == i);
641}
642
643static void *
644fsk_new(uintptr_t key, int flags, void *arg)
645{
646	struct dn_fsk *fs;
647
648	fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
649	if (fs) {
650		set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
651		dn_cfg.fsk_count++;
652		fs->drain_bucket = 0;
653		SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
654	}
655	return fs;
656}
657
658#ifdef NEW_AQM
659/* callback function for cleaning up AQM queue status belongs to a flowset
660 * connected to scheduler instance '_si' (for !DN_MULTIQUEUE only).
661 */
662static int
663si_cleanup_q(void *_si, void *arg)
664{
665	struct dn_sch_inst *si = _si;
666
667	if (!(si->sched->fp->flags & DN_MULTIQUEUE)) {
668		if (si->sched->fs->aqmfp && si->sched->fs->aqmfp->cleanup)
669			si->sched->fs->aqmfp->cleanup((struct dn_queue *) (si+1));
670	}
671	return 0;
672}
673
674/* callback to clean up queue AQM status.*/
675static int
676q_cleanup_q(void *_q, void *arg)
677{
678	struct dn_queue *q = _q;
679	q->fs->aqmfp->cleanup(q);
680	return 0;
681}
682
683/* Clean up all AQM queues status belongs to flowset 'fs' and then
684 * deconfig AQM for flowset 'fs'
685 */
686static void
687aqm_cleanup_deconfig_fs(struct dn_fsk *fs)
688{
689	struct dn_sch_inst *si;
690
691	/* clean up AQM status for all queues for !DN_MULTIQUEUE sched*/
692	if (fs->fs.fs_nr > DN_MAX_ID) {
693		if (fs->sched && !(fs->sched->fp->flags & DN_MULTIQUEUE)) {
694			if (fs->sched->sch.flags & DN_HAVE_MASK)
695				dn_ht_scan(fs->sched->siht, si_cleanup_q, NULL);
696			else {
697					/* single si i.e. no sched mask */
698					si = (struct dn_sch_inst *) fs->sched->siht;
699					if (si && fs->aqmfp && fs->aqmfp->cleanup)
700						fs->aqmfp->cleanup((struct dn_queue *) (si+1));
701			}
702		}
703	}
704
705	/* clean up AQM status for all queues for DN_MULTIQUEUE sched*/
706	if (fs->sched && fs->sched->fp->flags & DN_MULTIQUEUE && fs->qht) {
707			if (fs->fs.flags & DN_QHT_HASH)
708				dn_ht_scan(fs->qht, q_cleanup_q, NULL);
709			else
710				fs->aqmfp->cleanup((struct dn_queue *)(fs->qht));
711	}
712
713	/* deconfig AQM */
714	if(fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig)
715		fs->aqmfp->deconfig(fs);
716}
717#endif
718
719/*
720 * detach flowset from its current scheduler. Flags as follows:
721 * DN_DETACH removes from the fsk_list
722 * DN_DESTROY deletes individual queues
723 * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
724 */
725static void
726fsk_detach(struct dn_fsk *fs, int flags)
727{
728	if (flags & DN_DELETE_FS)
729		flags |= DN_DESTROY;
730	ND("fs %d from sched %d flags %s %s %s",
731		fs->fs.fs_nr, fs->fs.sched_nr,
732		(flags & DN_DELETE_FS) ? "DEL_FS":"",
733		(flags & DN_DESTROY) ? "DEL":"",
734		(flags & DN_DETACH) ? "DET":"");
735	if (flags & DN_DETACH) { /* detach from the list */
736		struct dn_fsk_head *h;
737		h = fs->sched ? &fs->sched->fsk_list : &dn_cfg.fsu;
738		SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
739	}
740	/* Free the RED parameters, they will be recomputed on
741	 * subsequent attach if needed.
742	 */
743	if (fs->w_q_lookup)
744		free(fs->w_q_lookup, M_DUMMYNET);
745	fs->w_q_lookup = NULL;
746	qht_delete(fs, flags);
747#ifdef NEW_AQM
748	aqm_cleanup_deconfig_fs(fs);
749#endif
750
751	if (fs->sched && fs->sched->fp->free_fsk)
752		fs->sched->fp->free_fsk(fs);
753	fs->sched = NULL;
754	if (flags & DN_DELETE_FS) {
755		bzero(fs, sizeof(*fs));	/* safety */
756		free(fs, M_DUMMYNET);
757		dn_cfg.fsk_count--;
758	} else {
759		SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
760	}
761}
762
763/*
764 * Detach or destroy all flowsets in a list.
765 * flags specifies what to do:
766 * DN_DESTROY:	flush all queues
767 * DN_DELETE_FS:	DN_DESTROY + destroy flowset
768 *	DN_DELETE_FS implies DN_DESTROY
769 */
770static void
771fsk_detach_list(struct dn_fsk_head *h, int flags)
772{
773	struct dn_fsk *fs;
774	int n = 0; /* only for stats */
775
776	ND("head %p flags %x", h, flags);
777	while ((fs = SLIST_FIRST(h))) {
778		SLIST_REMOVE_HEAD(h, sch_chain);
779		n++;
780		fsk_detach(fs, flags);
781	}
782	ND("done %d flowsets", n);
783}
784
785/*
786 * called on 'queue X delete' -- removes the flowset from fshash,
787 * deletes all queues for the flowset, and removes the flowset.
788 */
789static int
790delete_fs(int i, int locked)
791{
792	struct dn_fsk *fs;
793	int err = 0;
794
795	if (!locked)
796		DN_BH_WLOCK();
797	fs = dn_ht_find(dn_cfg.fshash, i, DNHT_REMOVE, NULL);
798	ND("fs %d found %p", i, fs);
799	if (fs) {
800		fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
801		err = 0;
802	} else
803		err = EINVAL;
804	if (!locked)
805		DN_BH_WUNLOCK();
806	return err;
807}
808
809/*----- end of flowset hashtable support -------------*/
810
811/*------------------------------------------------------------
812 * Scheduler hash. When searching by index we pass sched_nr,
813 * otherwise we pass struct dn_sch * which is the first field in
814 * struct dn_schk so we can cast between the two. We use this trick
815 * because in the create phase (but it should be fixed).
816 */
817static uint32_t
818schk_hash(uintptr_t key, int flags, void *_arg)
819{
820	uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
821		((struct dn_schk *)key)->sch.sched_nr;
822	return ( (i>>8)^(i>>4)^i );
823}
824
825static int
826schk_match(void *obj, uintptr_t key, int flags, void *_arg)
827{
828	struct dn_schk *s = (struct dn_schk *)obj;
829	int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
830		((struct dn_schk *)key)->sch.sched_nr;
831	return (s->sch.sched_nr == i);
832}
833
834/*
835 * Create the entry and intialize with the sched hash if needed.
836 * Leave s->fp unset so we can tell whether a dn_ht_find() returns
837 * a new object or a previously existing one.
838 */
839static void *
840schk_new(uintptr_t key, int flags, void *arg)
841{
842	struct schk_new_arg *a = arg;
843	struct dn_schk *s;
844	int l = sizeof(*s) +a->fp->schk_datalen;
845
846	s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
847	if (s == NULL)
848		return NULL;
849	set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
850	s->sch = *a->sch; // copy initial values
851	s->link.link_nr = s->sch.sched_nr;
852	SLIST_INIT(&s->fsk_list);
853	/* initialize the hash table or create the single instance */
854	s->fp = a->fp;	/* si_new needs this */
855	s->drain_bucket = 0;
856	if (s->sch.flags & DN_HAVE_MASK) {
857		s->siht = dn_ht_init(NULL, s->sch.buckets,
858			offsetof(struct dn_sch_inst, si_next),
859			si_hash, si_match, si_new);
860		if (s->siht == NULL) {
861			free(s, M_DUMMYNET);
862			return NULL;
863		}
864	}
865	s->fp = NULL;	/* mark as a new scheduler */
866	dn_cfg.schk_count++;
867	return s;
868}
869
870/*
871 * Callback for sched delete. Notify all attached flowsets to
872 * detach from the scheduler, destroy the internal flowset, and
873 * all instances. The scheduler goes away too.
874 * arg is 0 (only detach flowsets and destroy instances)
875 * DN_DESTROY (detach & delete queues, delete schk)
876 * or DN_DELETE_FS (delete queues and flowsets, delete schk)
877 */
878static int
879schk_delete_cb(void *obj, void *arg)
880{
881	struct dn_schk *s = obj;
882#if 0
883	int a = (int)arg;
884	ND("sched %d arg %s%s",
885		s->sch.sched_nr,
886		a&DN_DESTROY ? "DEL ":"",
887		a&DN_DELETE_FS ? "DEL_FS":"");
888#endif
889	fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
890	/* no more flowset pointing to us now */
891	if (s->sch.flags & DN_HAVE_MASK) {
892		dn_ht_scan(s->siht, si_destroy, NULL);
893		dn_ht_free(s->siht, 0);
894	} else if (s->siht)
895		si_destroy(s->siht, NULL);
896	if (s->profile) {
897		free(s->profile, M_DUMMYNET);
898		s->profile = NULL;
899	}
900	s->siht = NULL;
901	if (s->fp->destroy)
902		s->fp->destroy(s);
903	bzero(s, sizeof(*s));	// safety
904	free(obj, M_DUMMYNET);
905	dn_cfg.schk_count--;
906	return DNHT_SCAN_DEL;
907}
908
909/*
910 * called on a 'sched X delete' command. Deletes a single scheduler.
911 * This is done by removing from the schedhash, unlinking all
912 * flowsets and deleting their traffic.
913 */
914static int
915delete_schk(int i)
916{
917	struct dn_schk *s;
918
919	s = dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
920	ND("%d %p", i, s);
921	if (!s)
922		return EINVAL;
923	delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
924	/* then detach flowsets, delete traffic */
925	schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
926	return 0;
927}
928/*--- end of schk hashtable support ---*/
929
930static int
931copy_obj(char **start, char *end, void *_o, const char *msg, int i)
932{
933	struct dn_id o;
934	union {
935		struct dn_link l;
936		struct dn_schk s;
937	} dn;
938	int have = end - *start;
939
940	memcpy(&o, _o, sizeof(o));
941	if (have < o.len || o.len == 0 || o.type == 0) {
942		D("(WARN) type %d %s %d have %d need %d",
943		    o.type, msg, i, have, o.len);
944		return 1;
945	}
946	ND("type %d %s %d len %d", o.type, msg, i, o.len);
947	if (o.type == DN_LINK) {
948		memcpy(&dn.l, _o, sizeof(dn.l));
949		/* Adjust burst parameter for link */
950		dn.l.burst = div64(dn.l.burst, 8 * hz);
951		dn.l.delay = dn.l.delay * 1000 / hz;
952		memcpy(*start, &dn.l, sizeof(dn.l));
953	} else if (o.type == DN_SCH) {
954		/* Set dn.s.sch.oid.id to the number of instances */
955		memcpy(&dn.s, _o, sizeof(dn.s));
956		dn.s.sch.oid.id = (dn.s.sch.flags & DN_HAVE_MASK) ?
957		    dn_ht_entries(dn.s.siht) : (dn.s.siht ? 1 : 0);
958		memcpy(*start, &dn.s, sizeof(dn.s));
959	} else
960		memcpy(*start, _o, o.len);
961	*start += o.len;
962	return 0;
963}
964
965/* Specific function to copy a queue.
966 * Copies only the user-visible part of a queue (which is in
967 * a struct dn_flow), and sets len accordingly.
968 */
969static int
970copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
971{
972	struct dn_id *o = _o;
973	int have = end - *start;
974	int len = sizeof(struct dn_flow); /* see above comment */
975
976	if (have < len || o->len == 0 || o->type != DN_QUEUE) {
977		D("ERROR type %d %s %d have %d need %d",
978			o->type, msg, i, have, len);
979		return 1;
980	}
981	ND("type %d %s %d len %d", o->type, msg, i, len);
982	memcpy(*start, _o, len);
983	((struct dn_id*)(*start))->len = len;
984	*start += len;
985	return 0;
986}
987
988static int
989copy_q_cb(void *obj, void *arg)
990{
991	struct dn_queue *q = obj;
992	struct copy_args *a = arg;
993	struct dn_flow *ni = (struct dn_flow *)(*a->start);
994        if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
995                return DNHT_SCAN_END;
996        ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
997        ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
998        return 0;
999}
1000
1001static int
1002copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
1003{
1004	if (!fs->qht)
1005		return 0;
1006	if (fs->fs.flags & DN_QHT_HASH)
1007		dn_ht_scan(fs->qht, copy_q_cb, a);
1008	else
1009		copy_q_cb(fs->qht, a);
1010	return 0;
1011}
1012
1013/*
1014 * This routine only copies the initial part of a profile ? XXX
1015 */
1016static int
1017copy_profile(struct copy_args *a, struct dn_profile *p)
1018{
1019	int have = a->end - *a->start;
1020	/* XXX here we check for max length */
1021	int profile_len = sizeof(struct dn_profile) -
1022		ED_MAX_SAMPLES_NO*sizeof(int);
1023
1024	if (p == NULL)
1025		return 0;
1026	if (have < profile_len) {
1027		D("error have %d need %d", have, profile_len);
1028		return 1;
1029	}
1030	memcpy(*a->start, p, profile_len);
1031	((struct dn_id *)(*a->start))->len = profile_len;
1032	*a->start += profile_len;
1033	return 0;
1034}
1035
1036static int
1037copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
1038{
1039	struct dn_fs *ufs = (struct dn_fs *)(*a->start);
1040	if (!fs)
1041		return 0;
1042	ND("flowset %d", fs->fs.fs_nr);
1043	if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
1044		return DNHT_SCAN_END;
1045	ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
1046		dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
1047	if (flags) {	/* copy queues */
1048		copy_q(a, fs, 0);
1049	}
1050	return 0;
1051}
1052
1053static int
1054copy_si_cb(void *obj, void *arg)
1055{
1056	struct dn_sch_inst *si = obj;
1057	struct copy_args *a = arg;
1058	struct dn_flow *ni = (struct dn_flow *)(*a->start);
1059	if (copy_obj(a->start, a->end, &si->ni, "inst",
1060			si->sched->sch.sched_nr))
1061		return DNHT_SCAN_END;
1062	ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
1063	ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
1064	return 0;
1065}
1066
1067static int
1068copy_si(struct copy_args *a, struct dn_schk *s, int flags)
1069{
1070	if (s->sch.flags & DN_HAVE_MASK)
1071		dn_ht_scan(s->siht, copy_si_cb, a);
1072	else if (s->siht)
1073		copy_si_cb(s->siht, a);
1074	return 0;
1075}
1076
1077/*
1078 * compute a list of children of a scheduler and copy up
1079 */
1080static int
1081copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
1082{
1083	struct dn_fsk *fs;
1084	struct dn_id *o;
1085	uint32_t *p;
1086
1087	int n = 0, space = sizeof(*o);
1088	SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1089		if (fs->fs.fs_nr < DN_MAX_ID)
1090			n++;
1091	}
1092	space += n * sizeof(uint32_t);
1093	DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
1094	if (a->end - *(a->start) < space)
1095		return DNHT_SCAN_END;
1096	o = (struct dn_id *)(*(a->start));
1097	o->len = space;
1098	*a->start += o->len;
1099	o->type = DN_TEXT;
1100	p = (uint32_t *)(o+1);
1101	SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
1102		if (fs->fs.fs_nr < DN_MAX_ID)
1103			*p++ = fs->fs.fs_nr;
1104	return 0;
1105}
1106
1107static int
1108copy_data_helper(void *_o, void *_arg)
1109{
1110	struct copy_args *a = _arg;
1111	uint32_t *r = a->extra->r; /* start of first range */
1112	uint32_t *lim;	/* first invalid pointer */
1113	int n;
1114
1115	lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
1116
1117	if (a->type == DN_LINK || a->type == DN_SCH) {
1118		/* pipe|sched show, we receive a dn_schk */
1119		struct dn_schk *s = _o;
1120
1121		n = s->sch.sched_nr;
1122		if (a->type == DN_SCH && n >= DN_MAX_ID)
1123			return 0;	/* not a scheduler */
1124		if (a->type == DN_LINK && n <= DN_MAX_ID)
1125		    return 0;	/* not a pipe */
1126
1127		/* see if the object is within one of our ranges */
1128		for (;r < lim; r += 2) {
1129			if (n < r[0] || n > r[1])
1130				continue;
1131			/* Found a valid entry, copy and we are done */
1132			if (a->flags & DN_C_LINK) {
1133				if (copy_obj(a->start, a->end,
1134				    &s->link, "link", n))
1135					return DNHT_SCAN_END;
1136				if (copy_profile(a, s->profile))
1137					return DNHT_SCAN_END;
1138				if (copy_flowset(a, s->fs, 0))
1139					return DNHT_SCAN_END;
1140			}
1141			if (a->flags & DN_C_SCH) {
1142				if (copy_obj(a->start, a->end,
1143				    &s->sch, "sched", n))
1144					return DNHT_SCAN_END;
1145				/* list all attached flowsets */
1146				if (copy_fsk_list(a, s, 0))
1147					return DNHT_SCAN_END;
1148			}
1149			if (a->flags & DN_C_FLOW)
1150				copy_si(a, s, 0);
1151			break;
1152		}
1153	} else if (a->type == DN_FS) {
1154		/* queue show, skip internal flowsets */
1155		struct dn_fsk *fs = _o;
1156
1157		n = fs->fs.fs_nr;
1158		if (n >= DN_MAX_ID)
1159			return 0;
1160		/* see if the object is within one of our ranges */
1161		for (;r < lim; r += 2) {
1162			if (n < r[0] || n > r[1])
1163				continue;
1164			if (copy_flowset(a, fs, 0))
1165				return DNHT_SCAN_END;
1166			copy_q(a, fs, 0);
1167			break; /* we are done */
1168		}
1169	}
1170	return 0;
1171}
1172
1173static inline struct dn_schk *
1174locate_scheduler(int i)
1175{
1176	return dn_ht_find(dn_cfg.schedhash, i, 0, NULL);
1177}
1178
1179/*
1180 * red parameters are in fixed point arithmetic.
1181 */
1182static int
1183config_red(struct dn_fsk *fs)
1184{
1185	int64_t s, idle, weight, w0;
1186	int t, i;
1187
1188	fs->w_q = fs->fs.w_q;
1189	fs->max_p = fs->fs.max_p;
1190	ND("called");
1191	/* Doing stuff that was in userland */
1192	i = fs->sched->link.bandwidth;
1193	s = (i <= 0) ? 0 :
1194		hz * dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
1195
1196	idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
1197	fs->lookup_step = div64(idle , dn_cfg.red_lookup_depth);
1198	/* fs->lookup_step not scaled, */
1199	if (!fs->lookup_step)
1200		fs->lookup_step = 1;
1201	w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
1202
1203	for (t = fs->lookup_step; t > 1; --t)
1204		weight = SCALE_MUL(weight, w0);
1205	fs->lookup_weight = (int)(weight); // scaled
1206
1207	/* Now doing stuff that was in kerneland */
1208	fs->min_th = SCALE(fs->fs.min_th);
1209	fs->max_th = SCALE(fs->fs.max_th);
1210
1211	if (fs->fs.max_th == fs->fs.min_th)
1212		fs->c_1 = fs->max_p;
1213	else
1214		fs->c_1 = SCALE((int64_t)(fs->max_p)) / (fs->fs.max_th - fs->fs.min_th);
1215	fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
1216
1217	if (fs->fs.flags & DN_IS_GENTLE_RED) {
1218		fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
1219		fs->c_4 = SCALE(1) - 2 * fs->max_p;
1220	}
1221
1222	/* If the lookup table already exist, free and create it again. */
1223	if (fs->w_q_lookup) {
1224		free(fs->w_q_lookup, M_DUMMYNET);
1225		fs->w_q_lookup = NULL;
1226	}
1227	if (dn_cfg.red_lookup_depth == 0) {
1228		printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
1229		    "must be > 0\n");
1230		fs->fs.flags &= ~DN_IS_RED;
1231		fs->fs.flags &= ~DN_IS_GENTLE_RED;
1232		return (EINVAL);
1233	}
1234	fs->lookup_depth = dn_cfg.red_lookup_depth;
1235	fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
1236	    M_DUMMYNET, M_NOWAIT);
1237	if (fs->w_q_lookup == NULL) {
1238		printf("dummynet: sorry, cannot allocate red lookup table\n");
1239		fs->fs.flags &= ~DN_IS_RED;
1240		fs->fs.flags &= ~DN_IS_GENTLE_RED;
1241		return(ENOSPC);
1242	}
1243
1244	/* Fill the lookup table with (1 - w_q)^x */
1245	fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
1246
1247	for (i = 1; i < fs->lookup_depth; i++)
1248		fs->w_q_lookup[i] =
1249		    SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
1250
1251	if (dn_cfg.red_avg_pkt_size < 1)
1252		dn_cfg.red_avg_pkt_size = 512;
1253	fs->avg_pkt_size = dn_cfg.red_avg_pkt_size;
1254	if (dn_cfg.red_max_pkt_size < 1)
1255		dn_cfg.red_max_pkt_size = 1500;
1256	fs->max_pkt_size = dn_cfg.red_max_pkt_size;
1257	ND("exit");
1258	return 0;
1259}
1260
1261/* Scan all flowset attached to this scheduler and update red */
1262static void
1263update_red(struct dn_schk *s)
1264{
1265	struct dn_fsk *fs;
1266	SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
1267		if (fs && (fs->fs.flags & DN_IS_RED))
1268			config_red(fs);
1269	}
1270}
1271
1272/* attach flowset to scheduler s, possibly requeue */
1273static void
1274fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
1275{
1276	ND("remove fs %d from fsunlinked, link to sched %d",
1277		fs->fs.fs_nr, s->sch.sched_nr);
1278	SLIST_REMOVE(&dn_cfg.fsu, fs, dn_fsk, sch_chain);
1279	fs->sched = s;
1280	SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
1281	if (s->fp->new_fsk)
1282		s->fp->new_fsk(fs);
1283	/* XXX compute fsk_mask */
1284	fs->fsk_mask = fs->fs.flow_mask;
1285	if (fs->sched->sch.flags & DN_HAVE_MASK)
1286		flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
1287	if (fs->qht) {
1288		/*
1289		 * we must drain qht according to the old
1290		 * type, and reinsert according to the new one.
1291		 * The requeue is complex -- in general we need to
1292		 * reclassify every single packet.
1293		 * For the time being, let's hope qht is never set
1294		 * when we reach this point.
1295		 */
1296		D("XXX TODO requeue from fs %d to sch %d",
1297			fs->fs.fs_nr, s->sch.sched_nr);
1298		fs->qht = NULL;
1299	}
1300	/* set the new type for qht */
1301	if (nonzero_mask(&fs->fsk_mask))
1302		fs->fs.flags |= DN_QHT_HASH;
1303	else
1304		fs->fs.flags &= ~DN_QHT_HASH;
1305
1306	/* XXX config_red() can fail... */
1307	if (fs->fs.flags & DN_IS_RED)
1308		config_red(fs);
1309}
1310
1311/* update all flowsets which may refer to this scheduler */
1312static void
1313update_fs(struct dn_schk *s)
1314{
1315	struct dn_fsk *fs, *tmp;
1316
1317	SLIST_FOREACH_SAFE(fs, &dn_cfg.fsu, sch_chain, tmp) {
1318		if (s->sch.sched_nr != fs->fs.sched_nr) {
1319			D("fs %d for sch %d not %d still unlinked",
1320				fs->fs.fs_nr, fs->fs.sched_nr,
1321				s->sch.sched_nr);
1322			continue;
1323		}
1324		fsk_attach(fs, s);
1325	}
1326}
1327
1328#ifdef NEW_AQM
1329/* Retrieve AQM configurations to ipfw userland
1330 */
1331static int
1332get_aqm_parms(struct sockopt *sopt)
1333{
1334	struct dn_extra_parms  *ep;
1335	struct dn_fsk *fs;
1336	size_t sopt_valsize;
1337	int l, err = 0;
1338
1339	sopt_valsize = sopt->sopt_valsize;
1340	l = sizeof(*ep);
1341	if (sopt->sopt_valsize < l) {
1342		D("bad len sopt->sopt_valsize %d len %d",
1343			(int) sopt->sopt_valsize , l);
1344		err = EINVAL;
1345		return err;
1346	}
1347	ep = malloc(l, M_DUMMYNET, M_WAITOK);
1348	if(!ep) {
1349		err = ENOMEM ;
1350		return err;
1351	}
1352	do {
1353		err = sooptcopyin(sopt, ep, l, l);
1354		if(err)
1355			break;
1356		sopt->sopt_valsize = sopt_valsize;
1357		if (ep->oid.len < l) {
1358			err = EINVAL;
1359			break;
1360		}
1361
1362		fs = dn_ht_find(dn_cfg.fshash, ep->nr, 0, NULL);
1363		if (!fs) {
1364			D("fs %d not found", ep->nr);
1365			err = EINVAL;
1366			break;
1367		}
1368
1369		if (fs->aqmfp && fs->aqmfp->getconfig) {
1370			if(fs->aqmfp->getconfig(fs, ep)) {
1371				D("Error while trying to get AQM params");
1372				err = EINVAL;
1373				break;
1374			}
1375			ep->oid.len = l;
1376			err = sooptcopyout(sopt, ep, l);
1377		}
1378	}while(0);
1379
1380	free(ep, M_DUMMYNET);
1381	return err;
1382}
1383
1384/* Retrieve AQM configurations to ipfw userland
1385 */
1386static int
1387get_sched_parms(struct sockopt *sopt)
1388{
1389	struct dn_extra_parms  *ep;
1390	struct dn_schk *schk;
1391	size_t sopt_valsize;
1392	int l, err = 0;
1393
1394	sopt_valsize = sopt->sopt_valsize;
1395	l = sizeof(*ep);
1396	if (sopt->sopt_valsize < l) {
1397		D("bad len sopt->sopt_valsize %d len %d",
1398			(int) sopt->sopt_valsize , l);
1399		err = EINVAL;
1400		return err;
1401	}
1402	ep = malloc(l, M_DUMMYNET, M_WAITOK);
1403	if(!ep) {
1404		err = ENOMEM ;
1405		return err;
1406	}
1407	do {
1408		err = sooptcopyin(sopt, ep, l, l);
1409		if(err)
1410			break;
1411		sopt->sopt_valsize = sopt_valsize;
1412		if (ep->oid.len < l) {
1413			err = EINVAL;
1414			break;
1415		}
1416
1417		schk = locate_scheduler(ep->nr);
1418		if (!schk) {
1419			D("sched %d not found", ep->nr);
1420			err = EINVAL;
1421			break;
1422		}
1423
1424		if (schk->fp && schk->fp->getconfig) {
1425			if(schk->fp->getconfig(schk, ep)) {
1426				D("Error while trying to get sched params");
1427				err = EINVAL;
1428				break;
1429			}
1430			ep->oid.len = l;
1431			err = sooptcopyout(sopt, ep, l);
1432		}
1433	}while(0);
1434	free(ep, M_DUMMYNET);
1435
1436	return err;
1437}
1438
1439/* Configure AQM for flowset 'fs'.
1440 * extra parameters are passed from userland.
1441 */
1442static int
1443config_aqm(struct dn_fsk *fs, struct  dn_extra_parms *ep, int busy)
1444{
1445	int err = 0;
1446
1447	do {
1448		/* no configurations */
1449		if (!ep) {
1450			err = 0;
1451			break;
1452		}
1453
1454		/* no AQM for this flowset*/
1455		if (!strcmp(ep->name,"")) {
1456			err = 0;
1457			break;
1458		}
1459		if (ep->oid.len < sizeof(*ep)) {
1460			D("short aqm len %d", ep->oid.len);
1461				err = EINVAL;
1462				break;
1463		}
1464
1465		if (busy) {
1466			D("Unable to configure flowset, flowset busy!");
1467			err = EINVAL;
1468			break;
1469		}
1470
1471		/* deconfigure old aqm if exist */
1472		if (fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig) {
1473			aqm_cleanup_deconfig_fs(fs);
1474		}
1475
1476		if (!(fs->aqmfp = find_aqm_type(0, ep->name))) {
1477			D("AQM functions not found for type %s!", ep->name);
1478			fs->fs.flags &= ~DN_IS_AQM;
1479			err = EINVAL;
1480			break;
1481		} else
1482			fs->fs.flags |= DN_IS_AQM;
1483
1484		if (ep->oid.subtype != DN_AQM_PARAMS) {
1485				D("Wrong subtype");
1486				err = EINVAL;
1487				break;
1488		}
1489
1490		if (fs->aqmfp->config) {
1491			err = fs->aqmfp->config(fs, ep, ep->oid.len);
1492			if (err) {
1493					D("Unable to configure AQM for FS %d", fs->fs.fs_nr );
1494					fs->fs.flags &= ~DN_IS_AQM;
1495					fs->aqmfp = NULL;
1496					break;
1497			}
1498		}
1499	} while(0);
1500
1501	return err;
1502}
1503#endif
1504
1505/*
1506 * Configuration -- to preserve backward compatibility we use
1507 * the following scheme (N is 65536)
1508 *	NUMBER		SCHED	LINK	FLOWSET
1509 *	   1 ..  N-1	(1)WFQ	(2)WFQ	(3)queue
1510 *	 N+1 .. 2N-1	(4)FIFO (5)FIFO	(6)FIFO for sched 1..N-1
1511 *	2N+1 .. 3N-1	--	--	(7)FIFO for sched N+1..2N-1
1512 *
1513 * "pipe i config" configures #1, #2 and #3
1514 * "sched i config" configures #1 and possibly #6
1515 * "queue i config" configures #3
1516 * #1 is configured with 'pipe i config' or 'sched i config'
1517 * #2 is configured with 'pipe i config', and created if not
1518 *	existing with 'sched i config'
1519 * #3 is configured with 'queue i config'
1520 * #4 is automatically configured after #1, can only be FIFO
1521 * #5 is automatically configured after #2
1522 * #6 is automatically created when #1 is !MULTIQUEUE,
1523 *	and can be updated.
1524 * #7 is automatically configured after #2
1525 */
1526
1527/*
1528 * configure a link (and its FIFO instance)
1529 */
1530static int
1531config_link(struct dn_link *p, struct dn_id *arg)
1532{
1533	int i;
1534
1535	if (p->oid.len != sizeof(*p)) {
1536		D("invalid pipe len %d", p->oid.len);
1537		return EINVAL;
1538	}
1539	i = p->link_nr;
1540	if (i <= 0 || i >= DN_MAX_ID)
1541		return EINVAL;
1542	/*
1543	 * The config program passes parameters as follows:
1544	 * bw = bits/second (0 means no limits),
1545	 * delay = ms, must be translated into ticks.
1546	 * qsize = slots/bytes
1547	 * burst ???
1548	 */
1549	p->delay = (p->delay * hz) / 1000;
1550	/* Scale burst size: bytes -> bits * hz */
1551	p->burst *= 8 * hz;
1552
1553	DN_BH_WLOCK();
1554	/* do it twice, base link and FIFO link */
1555	for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1556	    struct dn_schk *s = locate_scheduler(i);
1557	    if (s == NULL) {
1558		DN_BH_WUNLOCK();
1559		D("sched %d not found", i);
1560		return EINVAL;
1561	    }
1562	    /* remove profile if exists */
1563	    if (s->profile) {
1564		free(s->profile, M_DUMMYNET);
1565		s->profile = NULL;
1566	    }
1567	    /* copy all parameters */
1568	    s->link.oid = p->oid;
1569	    s->link.link_nr = i;
1570	    s->link.delay = p->delay;
1571	    if (s->link.bandwidth != p->bandwidth) {
1572		/* XXX bandwidth changes, need to update red params */
1573	    s->link.bandwidth = p->bandwidth;
1574		update_red(s);
1575	    }
1576	    s->link.burst = p->burst;
1577	    schk_reset_credit(s);
1578	}
1579	dn_cfg.id++;
1580	DN_BH_WUNLOCK();
1581	return 0;
1582}
1583
1584/*
1585 * configure a flowset. Can be called from inside with locked=1,
1586 */
1587static struct dn_fsk *
1588config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
1589{
1590	int i;
1591	struct dn_fsk *fs;
1592#ifdef NEW_AQM
1593	struct dn_extra_parms *ep;
1594#endif
1595
1596	if (nfs->oid.len != sizeof(*nfs)) {
1597		D("invalid flowset len %d", nfs->oid.len);
1598		return NULL;
1599	}
1600	i = nfs->fs_nr;
1601	if (i <= 0 || i >= 3*DN_MAX_ID)
1602		return NULL;
1603#ifdef NEW_AQM
1604	ep = NULL;
1605	if (arg != NULL) {
1606		ep = malloc(sizeof(*ep), M_TEMP, locked ? M_NOWAIT : M_WAITOK);
1607		if (ep == NULL)
1608			return (NULL);
1609		memcpy(ep, arg, sizeof(*ep));
1610	}
1611#endif
1612	ND("flowset %d", i);
1613	/* XXX other sanity checks */
1614        if (nfs->flags & DN_QSIZE_BYTES) {
1615		ipdn_bound_var(&nfs->qsize, 16384,
1616		    1500, dn_cfg.byte_limit, NULL); // "queue byte size");
1617        } else {
1618		ipdn_bound_var(&nfs->qsize, 50,
1619		    1, dn_cfg.slot_limit, NULL); // "queue slot size");
1620        }
1621	if (nfs->flags & DN_HAVE_MASK) {
1622		/* make sure we have some buckets */
1623		ipdn_bound_var((int *)&nfs->buckets, dn_cfg.hash_size,
1624			1, dn_cfg.max_hash_size, "flowset buckets");
1625	} else {
1626		nfs->buckets = 1;	/* we only need 1 */
1627	}
1628	if (!locked)
1629		DN_BH_WLOCK();
1630	do { /* exit with break when done */
1631	    struct dn_schk *s;
1632	    int flags = nfs->sched_nr ? DNHT_INSERT : 0;
1633	    int j;
1634	    int oldc = dn_cfg.fsk_count;
1635	    fs = dn_ht_find(dn_cfg.fshash, i, flags, NULL);
1636	    if (fs == NULL) {
1637		D("missing sched for flowset %d", i);
1638	        break;
1639	    }
1640	    /* grab some defaults from the existing one */
1641	    if (nfs->sched_nr == 0) /* reuse */
1642		nfs->sched_nr = fs->fs.sched_nr;
1643	    for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
1644		if (nfs->par[j] == -1) /* reuse */
1645		    nfs->par[j] = fs->fs.par[j];
1646	    }
1647	    if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
1648		ND("flowset %d unchanged", i);
1649#ifdef NEW_AQM
1650		if (ep != NULL) {
1651			/*
1652			 * Reconfigure AQM as the parameters can be changed.
1653			 * We consider the flowset as busy if it has scheduler
1654			 * instance(s).
1655			 */
1656			s = locate_scheduler(nfs->sched_nr);
1657			config_aqm(fs, ep, s != NULL && s->siht != NULL);
1658		}
1659#endif
1660		break; /* no change, nothing to do */
1661	    }
1662	    if (oldc != dn_cfg.fsk_count)	/* new item */
1663		dn_cfg.id++;
1664	    s = locate_scheduler(nfs->sched_nr);
1665	    /* detach from old scheduler if needed, preserving
1666	     * queues if we need to reattach. Then update the
1667	     * configuration, and possibly attach to the new sched.
1668	     */
1669	    DX(2, "fs %d changed sched %d@%p to %d@%p",
1670		fs->fs.fs_nr,
1671		fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
1672	    if (fs->sched) {
1673		int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
1674		flags |= DN_DESTROY; /* XXX temporary */
1675		fsk_detach(fs, flags);
1676	    }
1677	    fs->fs = *nfs; /* copy configuration */
1678#ifdef NEW_AQM
1679			fs->aqmfp = NULL;
1680			if (ep != NULL)
1681				config_aqm(fs, ep, s != NULL &&
1682				    s->siht != NULL);
1683#endif
1684	    if (s != NULL)
1685		fsk_attach(fs, s);
1686	} while (0);
1687	if (!locked)
1688		DN_BH_WUNLOCK();
1689#ifdef NEW_AQM
1690	if (ep != NULL)
1691		free(ep, M_TEMP);
1692#endif
1693	return fs;
1694}
1695
1696/*
1697 * config/reconfig a scheduler and its FIFO variant.
1698 * For !MULTIQUEUE schedulers, also set up the flowset.
1699 *
1700 * On reconfigurations (detected because s->fp is set),
1701 * detach existing flowsets preserving traffic, preserve link,
1702 * and delete the old scheduler creating a new one.
1703 */
1704static int
1705config_sched(struct dn_sch *_nsch, struct dn_id *arg)
1706{
1707	struct dn_schk *s;
1708	struct schk_new_arg a; /* argument for schk_new */
1709	int i;
1710	struct dn_link p;	/* copy of oldlink */
1711	struct dn_profile *pf = NULL;	/* copy of old link profile */
1712	/* Used to preserv mask parameter */
1713	struct ipfw_flow_id new_mask;
1714	int new_buckets = 0;
1715	int new_flags = 0;
1716	int pipe_cmd;
1717	int err = ENOMEM;
1718
1719	a.sch = _nsch;
1720	if (a.sch->oid.len != sizeof(*a.sch)) {
1721		D("bad sched len %d", a.sch->oid.len);
1722		return EINVAL;
1723	}
1724	i = a.sch->sched_nr;
1725	if (i <= 0 || i >= DN_MAX_ID)
1726		return EINVAL;
1727	/* make sure we have some buckets */
1728	if (a.sch->flags & DN_HAVE_MASK)
1729		ipdn_bound_var((int *)&a.sch->buckets, dn_cfg.hash_size,
1730			1, dn_cfg.max_hash_size, "sched buckets");
1731	/* XXX other sanity checks */
1732	bzero(&p, sizeof(p));
1733
1734	pipe_cmd = a.sch->flags & DN_PIPE_CMD;
1735	a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
1736	if (pipe_cmd) {
1737		/* Copy mask parameter */
1738		new_mask = a.sch->sched_mask;
1739		new_buckets = a.sch->buckets;
1740		new_flags = a.sch->flags;
1741	}
1742	DN_BH_WLOCK();
1743again: /* run twice, for wfq and fifo */
1744	/*
1745	 * lookup the type. If not supplied, use the previous one
1746	 * or default to WF2Q+. Otherwise, return an error.
1747	 */
1748	dn_cfg.id++;
1749	a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
1750	if (a.fp != NULL) {
1751		/* found. Lookup or create entry */
1752		s = dn_ht_find(dn_cfg.schedhash, i, DNHT_INSERT, &a);
1753	} else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
1754		/* No type. search existing s* or retry with WF2Q+ */
1755		s = dn_ht_find(dn_cfg.schedhash, i, 0, &a);
1756		if (s != NULL) {
1757			a.fp = s->fp;
1758			/* Scheduler exists, skip to FIFO scheduler
1759			 * if command was pipe config...
1760			 */
1761			if (pipe_cmd)
1762				goto next;
1763		} else {
1764			/* New scheduler, create a wf2q+ with no mask
1765			 * if command was pipe config...
1766			 */
1767			if (pipe_cmd) {
1768				/* clear mask parameter */
1769				bzero(&a.sch->sched_mask, sizeof(new_mask));
1770				a.sch->buckets = 0;
1771				a.sch->flags &= ~DN_HAVE_MASK;
1772			}
1773			a.sch->oid.subtype = DN_SCHED_WF2QP;
1774			goto again;
1775		}
1776	} else {
1777		D("invalid scheduler type %d %s",
1778			a.sch->oid.subtype, a.sch->name);
1779		err = EINVAL;
1780		goto error;
1781	}
1782	/* normalize name and subtype */
1783	a.sch->oid.subtype = a.fp->type;
1784	bzero(a.sch->name, sizeof(a.sch->name));
1785	strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
1786	if (s == NULL) {
1787		D("cannot allocate scheduler %d", i);
1788		goto error;
1789	}
1790	/* restore existing link if any */
1791	if (p.link_nr) {
1792		s->link = p;
1793		if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
1794			s->profile = NULL; /* XXX maybe not needed */
1795		} else {
1796			s->profile = malloc(sizeof(struct dn_profile),
1797					     M_DUMMYNET, M_NOWAIT | M_ZERO);
1798			if (s->profile == NULL) {
1799				D("cannot allocate profile");
1800				goto error; //XXX
1801			}
1802			memcpy(s->profile, pf, sizeof(*pf));
1803		}
1804	}
1805	p.link_nr = 0;
1806	if (s->fp == NULL) {
1807		DX(2, "sched %d new type %s", i, a.fp->name);
1808	} else if (s->fp != a.fp ||
1809			bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
1810		/* already existing. */
1811		DX(2, "sched %d type changed from %s to %s",
1812			i, s->fp->name, a.fp->name);
1813		DX(4, "   type/sub %d/%d -> %d/%d",
1814			s->sch.oid.type, s->sch.oid.subtype,
1815			a.sch->oid.type, a.sch->oid.subtype);
1816		if (s->link.link_nr == 0)
1817			D("XXX WARNING link 0 for sched %d", i);
1818		p = s->link;	/* preserve link */
1819		if (s->profile) {/* preserve profile */
1820			if (!pf)
1821				pf = malloc(sizeof(*pf),
1822				    M_DUMMYNET, M_NOWAIT | M_ZERO);
1823			if (pf)	/* XXX should issue a warning otherwise */
1824				memcpy(pf, s->profile, sizeof(*pf));
1825		}
1826		/* remove from the hash */
1827		dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
1828		/* Detach flowsets, preserve queues. */
1829		// schk_delete_cb(s, NULL);
1830		// XXX temporarily, kill queues
1831		schk_delete_cb(s, (void *)DN_DESTROY);
1832		goto again;
1833	} else {
1834		DX(4, "sched %d unchanged type %s", i, a.fp->name);
1835	}
1836	/* complete initialization */
1837	s->sch = *a.sch;
1838	s->fp = a.fp;
1839	s->cfg = arg;
1840	// XXX schk_reset_credit(s);
1841	/* create the internal flowset if needed,
1842	 * trying to reuse existing ones if available
1843	 */
1844	if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
1845	        s->fs = dn_ht_find(dn_cfg.fshash, i, 0, NULL);
1846		if (!s->fs) {
1847			struct dn_fs fs;
1848			bzero(&fs, sizeof(fs));
1849			set_oid(&fs.oid, DN_FS, sizeof(fs));
1850			fs.fs_nr = i + DN_MAX_ID;
1851			fs.sched_nr = i;
1852			s->fs = config_fs(&fs, NULL, 1 /* locked */);
1853		}
1854		if (!s->fs) {
1855			schk_delete_cb(s, (void *)DN_DESTROY);
1856			D("error creating internal fs for %d", i);
1857			goto error;
1858		}
1859	}
1860	/* call init function after the flowset is created */
1861	if (s->fp->config)
1862		s->fp->config(s);
1863	update_fs(s);
1864next:
1865	if (i < DN_MAX_ID) { /* now configure the FIFO instance */
1866		i += DN_MAX_ID;
1867		if (pipe_cmd) {
1868			/* Restore mask parameter for FIFO */
1869			a.sch->sched_mask = new_mask;
1870			a.sch->buckets = new_buckets;
1871			a.sch->flags = new_flags;
1872		} else {
1873			/* sched config shouldn't modify the FIFO scheduler */
1874			if (dn_ht_find(dn_cfg.schedhash, i, 0, &a) != NULL) {
1875				/* FIFO already exist, don't touch it */
1876				err = 0; /* and this is not an error */
1877				goto error;
1878			}
1879		}
1880		a.sch->sched_nr = i;
1881		a.sch->oid.subtype = DN_SCHED_FIFO;
1882		bzero(a.sch->name, sizeof(a.sch->name));
1883		goto again;
1884	}
1885	err = 0;
1886error:
1887	DN_BH_WUNLOCK();
1888	if (pf)
1889		free(pf, M_DUMMYNET);
1890	return err;
1891}
1892
1893/*
1894 * attach a profile to a link
1895 */
1896static int
1897config_profile(struct dn_profile *pf, struct dn_id *arg)
1898{
1899	struct dn_schk *s;
1900	int i, olen, err = 0;
1901
1902	if (pf->oid.len < sizeof(*pf)) {
1903		D("short profile len %d", pf->oid.len);
1904		return EINVAL;
1905	}
1906	i = pf->link_nr;
1907	if (i <= 0 || i >= DN_MAX_ID)
1908		return EINVAL;
1909	/* XXX other sanity checks */
1910	DN_BH_WLOCK();
1911	for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
1912		s = locate_scheduler(i);
1913
1914		if (s == NULL) {
1915			err = EINVAL;
1916			break;
1917		}
1918		dn_cfg.id++;
1919		/*
1920		 * If we had a profile and the new one does not fit,
1921		 * or it is deleted, then we need to free memory.
1922		 */
1923		if (s->profile && (pf->samples_no == 0 ||
1924		    s->profile->oid.len < pf->oid.len)) {
1925			free(s->profile, M_DUMMYNET);
1926			s->profile = NULL;
1927		}
1928		if (pf->samples_no == 0)
1929			continue;
1930		/*
1931		 * new profile, possibly allocate memory
1932		 * and copy data.
1933		 */
1934		if (s->profile == NULL)
1935			s->profile = malloc(pf->oid.len,
1936				    M_DUMMYNET, M_NOWAIT | M_ZERO);
1937		if (s->profile == NULL) {
1938			D("no memory for profile %d", i);
1939			err = ENOMEM;
1940			break;
1941		}
1942		/* preserve larger length XXX double check */
1943		olen = s->profile->oid.len;
1944		if (olen < pf->oid.len)
1945			olen = pf->oid.len;
1946		memcpy(s->profile, pf, pf->oid.len);
1947		s->profile->oid.len = olen;
1948	}
1949	DN_BH_WUNLOCK();
1950	return err;
1951}
1952
1953/*
1954 * Delete all objects:
1955 */
1956static void
1957dummynet_flush(void)
1958{
1959
1960	/* delete all schedulers and related links/queues/flowsets */
1961	dn_ht_scan(dn_cfg.schedhash, schk_delete_cb,
1962		(void *)(uintptr_t)DN_DELETE_FS);
1963	/* delete all remaining (unlinked) flowsets */
1964	DX(4, "still %d unlinked fs", dn_cfg.fsk_count);
1965	dn_ht_free(dn_cfg.fshash, DNHT_REMOVE);
1966	fsk_detach_list(&dn_cfg.fsu, DN_DELETE_FS);
1967	/* Reinitialize system heap... */
1968	heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
1969}
1970
1971/*
1972 * Main handler for configuration. We are guaranteed to be called
1973 * with an oid which is at least a dn_id.
1974 * - the first object is the command (config, delete, flush, ...)
1975 * - config_link must be issued after the corresponding config_sched
1976 * - parameters (DN_TXT) for an object must preceed the object
1977 *   processed on a config_sched.
1978 */
1979int
1980do_config(void *p, int l)
1981{
1982	struct dn_id o;
1983	union {
1984		struct dn_profile profile;
1985		struct dn_fs fs;
1986		struct dn_link link;
1987		struct dn_sch sched;
1988	} *dn;
1989	struct dn_id *arg;
1990	uintptr_t a;
1991	int err, err2, off;
1992
1993	memcpy(&o, p, sizeof(o));
1994	if (o.id != DN_API_VERSION) {
1995		D("invalid api version got %d need %d", o.id, DN_API_VERSION);
1996		return EINVAL;
1997	}
1998	arg = NULL;
1999	dn = NULL;
2000	for (off = 0; l >= sizeof(o); memcpy(&o, (char *)p + off, sizeof(o))) {
2001		if (o.len < sizeof(o) || l < o.len) {
2002			D("bad len o.len %d len %d", o.len, l);
2003			err = EINVAL;
2004			break;
2005		}
2006		l -= o.len;
2007		err = 0;
2008		switch (o.type) {
2009		default:
2010			D("cmd %d not implemented", o.type);
2011			break;
2012
2013#ifdef EMULATE_SYSCTL
2014		/* sysctl emulation.
2015		 * if we recognize the command, jump to the correct
2016		 * handler and return
2017		 */
2018		case DN_SYSCTL_SET:
2019			err = kesysctl_emu_set(p, l);
2020			return err;
2021#endif
2022
2023		case DN_CMD_CONFIG: /* simply a header */
2024			break;
2025
2026		case DN_CMD_DELETE:
2027			/* the argument is in the first uintptr_t after o */
2028			if (o.len < sizeof(o) + sizeof(a)) {
2029				err = EINVAL;
2030				break;
2031			}
2032			memcpy(&a, (char *)p + off + sizeof(o), sizeof(a));
2033			switch (o.subtype) {
2034			case DN_LINK:
2035				/* delete base and derived schedulers */
2036				DN_BH_WLOCK();
2037				err = delete_schk(a);
2038				err2 = delete_schk(a + DN_MAX_ID);
2039				DN_BH_WUNLOCK();
2040				if (!err)
2041					err = err2;
2042				break;
2043
2044			default:
2045				D("invalid delete type %d", o.subtype);
2046				err = EINVAL;
2047				break;
2048
2049			case DN_FS:
2050				err = (a < 1 || a >= DN_MAX_ID) ?
2051				    EINVAL : delete_fs(a, 0) ;
2052				break;
2053			}
2054			break;
2055
2056		case DN_CMD_FLUSH:
2057			DN_BH_WLOCK();
2058			dummynet_flush();
2059			DN_BH_WUNLOCK();
2060			break;
2061		case DN_TEXT:	/* store argument of next block */
2062			if (arg != NULL)
2063				free(arg, M_TEMP);
2064			arg = malloc(o.len, M_TEMP, M_WAITOK);
2065			memcpy(arg, (char *)p + off, o.len);
2066			break;
2067		case DN_LINK:
2068			if (dn == NULL)
2069				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2070			memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
2071			err = config_link(&dn->link, arg);
2072			break;
2073		case DN_PROFILE:
2074			if (dn == NULL)
2075				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2076			memcpy(&dn->profile, (char *)p + off,
2077			    sizeof(dn->profile));
2078			err = config_profile(&dn->profile, arg);
2079			break;
2080		case DN_SCH:
2081			if (dn == NULL)
2082				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2083			memcpy(&dn->sched, (char *)p + off,
2084			    sizeof(dn->sched));
2085			err = config_sched(&dn->sched, arg);
2086			break;
2087		case DN_FS:
2088			if (dn == NULL)
2089				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2090			memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
2091			err = (NULL == config_fs(&dn->fs, arg, 0));
2092			break;
2093		}
2094		if (err != 0)
2095			break;
2096		off += o.len;
2097	}
2098	if (arg != NULL)
2099		free(arg, M_TEMP);
2100	if (dn != NULL)
2101		free(dn, M_TEMP);
2102	return err;
2103}
2104
2105static int
2106compute_space(struct dn_id *cmd, struct copy_args *a)
2107{
2108	int x = 0, need = 0;
2109	int profile_size = sizeof(struct dn_profile) -
2110		ED_MAX_SAMPLES_NO*sizeof(int);
2111
2112	/* NOTE about compute space:
2113	 * NP 	= dn_cfg.schk_count
2114	 * NSI 	= dn_cfg.si_count
2115	 * NF 	= dn_cfg.fsk_count
2116	 * NQ 	= dn_cfg.queue_count
2117	 * - ipfw pipe show
2118	 *   (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2119	 *                             link, scheduler template, flowset
2120	 *                             integrated in scheduler and header
2121	 *                             for flowset list
2122	 *   (NSI)*(dn_flow) all scheduler instance (includes
2123	 *                              the queue instance)
2124	 * - ipfw sched show
2125	 *   (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
2126	 *                             link, scheduler template, flowset
2127	 *                             integrated in scheduler and header
2128	 *                             for flowset list
2129	 *   (NSI * dn_flow) all scheduler instances
2130	 *   (NF * sizeof(uint_32)) space for flowset list linked to scheduler
2131	 *   (NQ * dn_queue) all queue [XXXfor now not listed]
2132	 * - ipfw queue show
2133	 *   (NF * dn_fs) all flowset
2134	 *   (NQ * dn_queue) all queues
2135	 */
2136	switch (cmd->subtype) {
2137	default:
2138		return -1;
2139	/* XXX where do LINK and SCH differ ? */
2140	/* 'ipfw sched show' could list all queues associated to
2141	 * a scheduler. This feature for now is disabled
2142	 */
2143	case DN_LINK:	/* pipe show */
2144		x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
2145		need += dn_cfg.schk_count *
2146			(sizeof(struct dn_fs) + profile_size) / 2;
2147		need += dn_cfg.fsk_count * sizeof(uint32_t);
2148		break;
2149	case DN_SCH:	/* sched show */
2150		need += dn_cfg.schk_count *
2151			(sizeof(struct dn_fs) + profile_size) / 2;
2152		need += dn_cfg.fsk_count * sizeof(uint32_t);
2153		x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
2154		break;
2155	case DN_FS:	/* queue show */
2156		x = DN_C_FS | DN_C_QUEUE;
2157		break;
2158	case DN_GET_COMPAT:	/* compatibility mode */
2159		need =  dn_compat_calc_size();
2160		break;
2161	}
2162	a->flags = x;
2163	if (x & DN_C_SCH) {
2164		need += dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
2165		/* NOT also, each fs might be attached to a sched */
2166		need += dn_cfg.schk_count * sizeof(struct dn_id) / 2;
2167	}
2168	if (x & DN_C_FS)
2169		need += dn_cfg.fsk_count * sizeof(struct dn_fs);
2170	if (x & DN_C_LINK) {
2171		need += dn_cfg.schk_count * sizeof(struct dn_link) / 2;
2172	}
2173	/*
2174	 * When exporting a queue to userland, only pass up the
2175	 * struct dn_flow, which is the only visible part.
2176	 */
2177
2178	if (x & DN_C_QUEUE)
2179		need += dn_cfg.queue_count * sizeof(struct dn_flow);
2180	if (x & DN_C_FLOW)
2181		need += dn_cfg.si_count * (sizeof(struct dn_flow));
2182	return need;
2183}
2184
2185/*
2186 * If compat != NULL dummynet_get is called in compatibility mode.
2187 * *compat will be the pointer to the buffer to pass to ipfw
2188 */
2189int
2190dummynet_get(struct sockopt *sopt, void **compat)
2191{
2192	int have, i, need, error;
2193	char *start = NULL, *buf;
2194	size_t sopt_valsize;
2195	struct dn_id *cmd;
2196	struct copy_args a;
2197	struct copy_range r;
2198	int l = sizeof(struct dn_id);
2199
2200	bzero(&a, sizeof(a));
2201	bzero(&r, sizeof(r));
2202
2203	/* save and restore original sopt_valsize around copyin */
2204	sopt_valsize = sopt->sopt_valsize;
2205
2206	cmd = &r.o;
2207
2208	if (!compat) {
2209		/* copy at least an oid, and possibly a full object */
2210		error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
2211		sopt->sopt_valsize = sopt_valsize;
2212		if (error)
2213			goto done;
2214		l = cmd->len;
2215#ifdef EMULATE_SYSCTL
2216		/* sysctl emulation. */
2217		if (cmd->type == DN_SYSCTL_GET)
2218			return kesysctl_emu_get(sopt);
2219#endif
2220		if (l > sizeof(r)) {
2221			/* request larger than default, allocate buffer */
2222			cmd = malloc(l,  M_DUMMYNET, M_WAITOK);
2223			error = sooptcopyin(sopt, cmd, l, l);
2224			sopt->sopt_valsize = sopt_valsize;
2225			if (error)
2226				goto done;
2227		}
2228	} else { /* compatibility */
2229		error = 0;
2230		cmd->type = DN_CMD_GET;
2231		cmd->len = sizeof(struct dn_id);
2232		cmd->subtype = DN_GET_COMPAT;
2233		// cmd->id = sopt_valsize;
2234		D("compatibility mode");
2235	}
2236
2237#ifdef NEW_AQM
2238	/* get AQM params */
2239	if(cmd->subtype == DN_AQM_PARAMS) {
2240		error = get_aqm_parms(sopt);
2241		goto done;
2242	/* get Scheduler params */
2243	} else if (cmd->subtype == DN_SCH_PARAMS) {
2244		error = get_sched_parms(sopt);
2245		goto done;
2246	}
2247#endif
2248
2249	a.extra = (struct copy_range *)cmd;
2250	if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
2251		uint32_t *rp = (uint32_t *)(cmd + 1);
2252		cmd->len += 2* sizeof(uint32_t);
2253		rp[0] = 1;
2254		rp[1] = DN_MAX_ID - 1;
2255		if (cmd->subtype == DN_LINK) {
2256			rp[0] += DN_MAX_ID;
2257			rp[1] += DN_MAX_ID;
2258		}
2259	}
2260	/* Count space (under lock) and allocate (outside lock).
2261	 * Exit with lock held if we manage to get enough buffer.
2262	 * Try a few times then give up.
2263	 */
2264	for (have = 0, i = 0; i < 10; i++) {
2265		DN_BH_WLOCK();
2266		need = compute_space(cmd, &a);
2267
2268		/* if there is a range, ignore value from compute_space() */
2269		if (l > sizeof(*cmd))
2270			need = sopt_valsize - sizeof(*cmd);
2271
2272		if (need < 0) {
2273			DN_BH_WUNLOCK();
2274			error = EINVAL;
2275			goto done;
2276		}
2277		need += sizeof(*cmd);
2278		cmd->id = need;
2279		if (have >= need)
2280			break;
2281
2282		DN_BH_WUNLOCK();
2283		if (start)
2284			free(start, M_DUMMYNET);
2285		start = NULL;
2286		if (need > sopt_valsize)
2287			break;
2288
2289		have = need;
2290		start = malloc(have, M_DUMMYNET, M_WAITOK | M_ZERO);
2291	}
2292
2293	if (start == NULL) {
2294		if (compat) {
2295			*compat = NULL;
2296			error =  1; // XXX
2297		} else {
2298			error = sooptcopyout(sopt, cmd, sizeof(*cmd));
2299		}
2300		goto done;
2301	}
2302	ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
2303		"%d:%d si %d, %d:%d queues %d",
2304		dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
2305		dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
2306		dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
2307		dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
2308		dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
2309	sopt->sopt_valsize = sopt_valsize;
2310	a.type = cmd->subtype;
2311
2312	if (compat == NULL) {
2313		memcpy(start, cmd, sizeof(*cmd));
2314		((struct dn_id*)(start))->len = sizeof(struct dn_id);
2315		buf = start + sizeof(*cmd);
2316	} else
2317		buf = start;
2318	a.start = &buf;
2319	a.end = start + have;
2320	/* start copying other objects */
2321	if (compat) {
2322		a.type = DN_COMPAT_PIPE;
2323		dn_ht_scan(dn_cfg.schedhash, copy_data_helper_compat, &a);
2324		a.type = DN_COMPAT_QUEUE;
2325		dn_ht_scan(dn_cfg.fshash, copy_data_helper_compat, &a);
2326	} else if (a.type == DN_FS) {
2327		dn_ht_scan(dn_cfg.fshash, copy_data_helper, &a);
2328	} else {
2329		dn_ht_scan(dn_cfg.schedhash, copy_data_helper, &a);
2330	}
2331	DN_BH_WUNLOCK();
2332
2333	if (compat) {
2334		*compat = start;
2335		sopt->sopt_valsize = buf - start;
2336		/* free() is done by ip_dummynet_compat() */
2337		start = NULL; //XXX hack
2338	} else {
2339		error = sooptcopyout(sopt, start, buf - start);
2340	}
2341done:
2342	if (cmd && cmd != &r.o)
2343		free(cmd, M_DUMMYNET);
2344	if (start)
2345		free(start, M_DUMMYNET);
2346	return error;
2347}
2348
2349/* Callback called on scheduler instance to delete it if idle */
2350static int
2351drain_scheduler_cb(void *_si, void *arg)
2352{
2353	struct dn_sch_inst *si = _si;
2354
2355	if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
2356		return 0;
2357
2358	if (si->sched->fp->flags & DN_MULTIQUEUE) {
2359		if (si->q_count == 0)
2360			return si_destroy(si, NULL);
2361		else
2362			return 0;
2363	} else { /* !DN_MULTIQUEUE */
2364		if ((si+1)->ni.length == 0)
2365			return si_destroy(si, NULL);
2366		else
2367			return 0;
2368	}
2369	return 0; /* unreachable */
2370}
2371
2372/* Callback called on scheduler to check if it has instances */
2373static int
2374drain_scheduler_sch_cb(void *_s, void *arg)
2375{
2376	struct dn_schk *s = _s;
2377
2378	if (s->sch.flags & DN_HAVE_MASK) {
2379		dn_ht_scan_bucket(s->siht, &s->drain_bucket,
2380				drain_scheduler_cb, NULL);
2381		s->drain_bucket++;
2382	} else {
2383		if (s->siht) {
2384			if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
2385				s->siht = NULL;
2386		}
2387	}
2388	return 0;
2389}
2390
2391/* Called every tick, try to delete a 'bucket' of scheduler */
2392void
2393dn_drain_scheduler(void)
2394{
2395	dn_ht_scan_bucket(dn_cfg.schedhash, &dn_cfg.drain_sch,
2396			   drain_scheduler_sch_cb, NULL);
2397	dn_cfg.drain_sch++;
2398}
2399
2400/* Callback called on queue to delete if it is idle */
2401static int
2402drain_queue_cb(void *_q, void *arg)
2403{
2404	struct dn_queue *q = _q;
2405
2406	if (q->ni.length == 0) {
2407		dn_delete_queue(q, DN_DESTROY);
2408		return DNHT_SCAN_DEL; /* queue is deleted */
2409	}
2410
2411	return 0; /* queue isn't deleted */
2412}
2413
2414/* Callback called on flowset used to check if it has queues */
2415static int
2416drain_queue_fs_cb(void *_fs, void *arg)
2417{
2418	struct dn_fsk *fs = _fs;
2419
2420	if (fs->fs.flags & DN_QHT_HASH) {
2421		/* Flowset has a hash table for queues */
2422		dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
2423				drain_queue_cb, NULL);
2424		fs->drain_bucket++;
2425	} else {
2426		/* No hash table for this flowset, null the pointer
2427		 * if the queue is deleted
2428		 */
2429		if (fs->qht) {
2430			if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
2431				fs->qht = NULL;
2432		}
2433	}
2434	return 0;
2435}
2436
2437/* Called every tick, try to delete a 'bucket' of queue */
2438void
2439dn_drain_queue(void)
2440{
2441	/* scan a bucket of flowset */
2442	dn_ht_scan_bucket(dn_cfg.fshash, &dn_cfg.drain_fs,
2443                               drain_queue_fs_cb, NULL);
2444	dn_cfg.drain_fs++;
2445}
2446
2447/*
2448 * Handler for the various dummynet socket options
2449 */
2450static int
2451ip_dn_ctl(struct sockopt *sopt)
2452{
2453	void *p = NULL;
2454	int error, l;
2455
2456	error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
2457	if (error)
2458		return (error);
2459
2460	/* Disallow sets in really-really secure mode. */
2461	if (sopt->sopt_dir == SOPT_SET) {
2462		error =  securelevel_ge(sopt->sopt_td->td_ucred, 3);
2463		if (error)
2464			return (error);
2465	}
2466
2467	switch (sopt->sopt_name) {
2468	default :
2469		D("dummynet: unknown option %d", sopt->sopt_name);
2470		error = EINVAL;
2471		break;
2472
2473	case IP_DUMMYNET_FLUSH:
2474	case IP_DUMMYNET_CONFIGURE:
2475	case IP_DUMMYNET_DEL:	/* remove a pipe or queue */
2476	case IP_DUMMYNET_GET:
2477		D("dummynet: compat option %d", sopt->sopt_name);
2478		error = ip_dummynet_compat(sopt);
2479		break;
2480
2481	case IP_DUMMYNET3 :
2482		if (sopt->sopt_dir == SOPT_GET) {
2483			error = dummynet_get(sopt, NULL);
2484			break;
2485		}
2486		l = sopt->sopt_valsize;
2487		if (l < sizeof(struct dn_id) || l > 12000) {
2488			D("argument len %d invalid", l);
2489			break;
2490		}
2491		p = malloc(l, M_TEMP, M_WAITOK); // XXX can it fail ?
2492		error = sooptcopyin(sopt, p, l, l);
2493		if (error)
2494			break ;
2495		error = do_config(p, l);
2496		break;
2497	}
2498
2499	if (p != NULL)
2500		free(p, M_TEMP);
2501
2502	return error ;
2503}
2504
2505
2506static void
2507ip_dn_init(void)
2508{
2509	if (dn_cfg.init_done)
2510		return;
2511	printf("DUMMYNET %p with IPv6 initialized (100409)\n", curvnet);
2512	dn_cfg.init_done = 1;
2513	/* Set defaults here. MSVC does not accept initializers,
2514	 * and this is also useful for vimages
2515	 */
2516	/* queue limits */
2517	dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
2518	dn_cfg.byte_limit = 1024 * 1024;
2519	dn_cfg.expire = 1;
2520
2521	/* RED parameters */
2522	dn_cfg.red_lookup_depth = 256;	/* default lookup table depth */
2523	dn_cfg.red_avg_pkt_size = 512;	/* default medium packet size */
2524	dn_cfg.red_max_pkt_size = 1500;	/* default max packet size */
2525
2526	/* hash tables */
2527	dn_cfg.max_hash_size = 65536;	/* max in the hash tables */
2528	dn_cfg.hash_size = 64;		/* default hash size */
2529
2530	/* create hash tables for schedulers and flowsets.
2531	 * In both we search by key and by pointer.
2532	 */
2533	dn_cfg.schedhash = dn_ht_init(NULL, dn_cfg.hash_size,
2534		offsetof(struct dn_schk, schk_next),
2535		schk_hash, schk_match, schk_new);
2536	dn_cfg.fshash = dn_ht_init(NULL, dn_cfg.hash_size,
2537		offsetof(struct dn_fsk, fsk_next),
2538		fsk_hash, fsk_match, fsk_new);
2539
2540	/* bucket index to drain object */
2541	dn_cfg.drain_fs = 0;
2542	dn_cfg.drain_sch = 0;
2543
2544	heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
2545	SLIST_INIT(&dn_cfg.fsu);
2546	SLIST_INIT(&dn_cfg.schedlist);
2547
2548	DN_LOCK_INIT();
2549
2550	TASK_INIT(&dn_task, 0, dummynet_task, curvnet);
2551	dn_tq = taskqueue_create_fast("dummynet", M_WAITOK,
2552	    taskqueue_thread_enqueue, &dn_tq);
2553	taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
2554
2555	callout_init(&dn_timeout, 1);
2556	dn_reschedule();
2557
2558	/* Initialize curr_time adjustment mechanics. */
2559	getmicrouptime(&dn_cfg.prev_t);
2560}
2561
2562static void
2563ip_dn_destroy(int last)
2564{
2565	DN_BH_WLOCK();
2566	/* ensure no more callouts are started */
2567	dn_gone = 1;
2568
2569	/* check for last */
2570	if (last) {
2571		ND("removing last instance\n");
2572		ip_dn_ctl_ptr = NULL;
2573		ip_dn_io_ptr = NULL;
2574	}
2575
2576	dummynet_flush();
2577	DN_BH_WUNLOCK();
2578
2579	callout_drain(&dn_timeout);
2580	taskqueue_drain(dn_tq, &dn_task);
2581	taskqueue_free(dn_tq);
2582
2583	dn_ht_free(dn_cfg.schedhash, 0);
2584	dn_ht_free(dn_cfg.fshash, 0);
2585	heap_free(&dn_cfg.evheap);
2586
2587	DN_LOCK_DESTROY();
2588}
2589
2590static int
2591dummynet_modevent(module_t mod, int type, void *data)
2592{
2593
2594	if (type == MOD_LOAD) {
2595		if (ip_dn_io_ptr) {
2596			printf("DUMMYNET already loaded\n");
2597			return EEXIST ;
2598		}
2599		ip_dn_init();
2600		ip_dn_ctl_ptr = ip_dn_ctl;
2601		ip_dn_io_ptr = dummynet_io;
2602		return 0;
2603	} else if (type == MOD_UNLOAD) {
2604		ip_dn_destroy(1 /* last */);
2605		return 0;
2606	} else
2607		return EOPNOTSUPP;
2608}
2609
2610/* modevent helpers for the modules */
2611static int
2612load_dn_sched(struct dn_alg *d)
2613{
2614	struct dn_alg *s;
2615
2616	if (d == NULL)
2617		return 1; /* error */
2618	ip_dn_init();	/* just in case, we need the lock */
2619
2620	/* Check that mandatory funcs exists */
2621	if (d->enqueue == NULL || d->dequeue == NULL) {
2622		D("missing enqueue or dequeue for %s", d->name);
2623		return 1;
2624	}
2625
2626	/* Search if scheduler already exists */
2627	DN_BH_WLOCK();
2628	SLIST_FOREACH(s, &dn_cfg.schedlist, next) {
2629		if (strcmp(s->name, d->name) == 0) {
2630			D("%s already loaded", d->name);
2631			break; /* scheduler already exists */
2632		}
2633	}
2634	if (s == NULL)
2635		SLIST_INSERT_HEAD(&dn_cfg.schedlist, d, next);
2636	DN_BH_WUNLOCK();
2637	D("dn_sched %s %sloaded", d->name, s ? "not ":"");
2638	return s ? 1 : 0;
2639}
2640
2641static int
2642unload_dn_sched(struct dn_alg *s)
2643{
2644	struct dn_alg *tmp, *r;
2645	int err = EINVAL;
2646
2647	ND("called for %s", s->name);
2648
2649	DN_BH_WLOCK();
2650	SLIST_FOREACH_SAFE(r, &dn_cfg.schedlist, next, tmp) {
2651		if (strcmp(s->name, r->name) != 0)
2652			continue;
2653		ND("ref_count = %d", r->ref_count);
2654		err = (r->ref_count != 0) ? EBUSY : 0;
2655		if (err == 0)
2656			SLIST_REMOVE(&dn_cfg.schedlist, r, dn_alg, next);
2657		break;
2658	}
2659	DN_BH_WUNLOCK();
2660	D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
2661	return err;
2662}
2663
2664int
2665dn_sched_modevent(module_t mod, int cmd, void *arg)
2666{
2667	struct dn_alg *sch = arg;
2668
2669	if (cmd == MOD_LOAD)
2670		return load_dn_sched(sch);
2671	else if (cmd == MOD_UNLOAD)
2672		return unload_dn_sched(sch);
2673	else
2674		return EINVAL;
2675}
2676
2677static moduledata_t dummynet_mod = {
2678	"dummynet", dummynet_modevent, NULL
2679};
2680
2681#define	DN_SI_SUB	SI_SUB_PROTO_IFATTACHDOMAIN
2682#define	DN_MODEV_ORD	(SI_ORDER_ANY - 128) /* after ipfw */
2683DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
2684MODULE_DEPEND(dummynet, ipfw, 2, 2, 2);
2685MODULE_VERSION(dummynet, 3);
2686
2687/*
2688 * Starting up. Done in order after dummynet_modevent() has been called.
2689 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2690 */
2691//VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_init, NULL);
2692
2693/*
2694 * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
2695 * after dummynet_modevent() has been called. Not called on reboot.
2696 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2697 * or when the module is unloaded.
2698 */
2699//VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_destroy, NULL);
2700
2701#ifdef NEW_AQM
2702
2703/* modevent helpers for the AQM modules */
2704static int
2705load_dn_aqm(struct dn_aqm *d)
2706{
2707	struct dn_aqm *aqm=NULL;
2708
2709	if (d == NULL)
2710		return 1; /* error */
2711	ip_dn_init();	/* just in case, we need the lock */
2712
2713	/* Check that mandatory funcs exists */
2714	if (d->enqueue == NULL || d->dequeue == NULL) {
2715		D("missing enqueue or dequeue for %s", d->name);
2716		return 1;
2717	}
2718
2719	/* Search if AQM already exists */
2720	DN_BH_WLOCK();
2721	SLIST_FOREACH(aqm, &dn_cfg.aqmlist, next) {
2722		if (strcmp(aqm->name, d->name) == 0) {
2723			D("%s already loaded", d->name);
2724			break; /* AQM already exists */
2725		}
2726	}
2727	if (aqm == NULL)
2728		SLIST_INSERT_HEAD(&dn_cfg.aqmlist, d, next);
2729	DN_BH_WUNLOCK();
2730	D("dn_aqm %s %sloaded", d->name, aqm ? "not ":"");
2731	return aqm ? 1 : 0;
2732}
2733
2734
2735/* Callback to clean up AQM status for queues connected to a flowset
2736 * and then deconfigure the flowset.
2737 * This function is called before an AQM module is unloaded
2738 */
2739static int
2740fs_cleanup(void *_fs, void *arg)
2741{
2742	struct dn_fsk *fs = _fs;
2743	uint32_t type = *(uint32_t *)arg;
2744
2745	if (fs->aqmfp && fs->aqmfp->type == type)
2746		aqm_cleanup_deconfig_fs(fs);
2747
2748	return 0;
2749}
2750
2751static int
2752unload_dn_aqm(struct dn_aqm *aqm)
2753{
2754	struct dn_aqm *tmp, *r;
2755	int err = EINVAL;
2756	err = 0;
2757	ND("called for %s", aqm->name);
2758
2759	DN_BH_WLOCK();
2760
2761	/* clean up AQM status and deconfig flowset */
2762	dn_ht_scan(dn_cfg.fshash, fs_cleanup, &aqm->type);
2763
2764	SLIST_FOREACH_SAFE(r, &dn_cfg.aqmlist, next, tmp) {
2765		if (strcmp(aqm->name, r->name) != 0)
2766			continue;
2767		ND("ref_count = %d", r->ref_count);
2768		err = (r->ref_count != 0 || r->cfg_ref_count != 0) ? EBUSY : 0;
2769		if (err == 0)
2770			SLIST_REMOVE(&dn_cfg.aqmlist, r, dn_aqm, next);
2771		break;
2772	}
2773	DN_BH_WUNLOCK();
2774	D("%s %sunloaded", aqm->name, err ? "not ":"");
2775	if (err)
2776		D("ref_count=%d, cfg_ref_count=%d", r->ref_count, r->cfg_ref_count);
2777	return err;
2778}
2779
2780int
2781dn_aqm_modevent(module_t mod, int cmd, void *arg)
2782{
2783	struct dn_aqm *aqm = arg;
2784
2785	if (cmd == MOD_LOAD)
2786		return load_dn_aqm(aqm);
2787	else if (cmd == MOD_UNLOAD)
2788		return unload_dn_aqm(aqm);
2789	else
2790		return EINVAL;
2791}
2792#endif
2793
2794/* end of file */
2795
2796