xref: /freebsd/sys/netpfil/ipfw/ip_dummynet.c (revision 0ac43d9728a7916453a4d8f1cf09e5f47c7038fd)
13b3a8eb9SGleb Smirnoff /*-
291336b40SDon Lewis  * Codel/FQ_Codel and PIE/FQ-PIE Code:
391336b40SDon Lewis  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
491336b40SDon Lewis  *  Swinburne University of Technology, Melbourne, Australia.
591336b40SDon Lewis  * Portions of this code were made possible in part by a gift from
691336b40SDon Lewis  *  The Comcast Innovation Fund.
791336b40SDon Lewis  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
891336b40SDon Lewis  *
93b3a8eb9SGleb Smirnoff  * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
103b3a8eb9SGleb Smirnoff  * Portions Copyright (c) 2000 Akamba Corp.
113b3a8eb9SGleb Smirnoff  * All rights reserved
123b3a8eb9SGleb Smirnoff  *
133b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
143b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
153b3a8eb9SGleb Smirnoff  * are met:
163b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
173b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
183b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
193b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
203b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
213b3a8eb9SGleb Smirnoff  *
223b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
233b3a8eb9SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253b3a8eb9SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
263b3a8eb9SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273b3a8eb9SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283b3a8eb9SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293b3a8eb9SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313b3a8eb9SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323b3a8eb9SGleb Smirnoff  * SUCH DAMAGE.
333b3a8eb9SGleb Smirnoff  */
343b3a8eb9SGleb Smirnoff 
353b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
363b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
373b3a8eb9SGleb Smirnoff 
383b3a8eb9SGleb Smirnoff /*
393b3a8eb9SGleb Smirnoff  * Configuration and internal object management for dummynet.
403b3a8eb9SGleb Smirnoff  */
413b3a8eb9SGleb Smirnoff 
423b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
433b3a8eb9SGleb Smirnoff 
443b3a8eb9SGleb Smirnoff #include <sys/param.h>
453b3a8eb9SGleb Smirnoff #include <sys/systm.h>
463b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
473b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
483b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
493b3a8eb9SGleb Smirnoff #include <sys/lock.h>
503b3a8eb9SGleb Smirnoff #include <sys/module.h>
51eedc7fd9SGleb Smirnoff #include <sys/mutex.h>
523b3a8eb9SGleb Smirnoff #include <sys/priv.h>
533b3a8eb9SGleb Smirnoff #include <sys/proc.h>
543b3a8eb9SGleb Smirnoff #include <sys/rwlock.h>
553b3a8eb9SGleb Smirnoff #include <sys/socket.h>
563b3a8eb9SGleb Smirnoff #include <sys/socketvar.h>
573b3a8eb9SGleb Smirnoff #include <sys/time.h>
583b3a8eb9SGleb Smirnoff #include <sys/taskqueue.h>
593b3a8eb9SGleb Smirnoff #include <net/if.h>	/* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
603b3a8eb9SGleb Smirnoff #include <netinet/in.h>
613b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h>	/* ip_output(), IP_FORWARDING */
623b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h>
633b3a8eb9SGleb Smirnoff #include <netinet/ip_dummynet.h>
643b3a8eb9SGleb Smirnoff 
653b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_fw_private.h>
663b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_heap.h>
673b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_dn_private.h>
6891336b40SDon Lewis #ifdef NEW_AQM
6991336b40SDon Lewis #include <netpfil/ipfw/dn_aqm.h>
7091336b40SDon Lewis #endif
713b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_sched.h>
723b3a8eb9SGleb Smirnoff 
733b3a8eb9SGleb Smirnoff /* which objects to copy */
743b3a8eb9SGleb Smirnoff #define DN_C_LINK 	0x01
753b3a8eb9SGleb Smirnoff #define DN_C_SCH	0x02
763b3a8eb9SGleb Smirnoff #define DN_C_FLOW	0x04
773b3a8eb9SGleb Smirnoff #define DN_C_FS		0x08
783b3a8eb9SGleb Smirnoff #define DN_C_QUEUE	0x10
793b3a8eb9SGleb Smirnoff 
803b3a8eb9SGleb Smirnoff /* we use this argument in case of a schk_new */
813b3a8eb9SGleb Smirnoff struct schk_new_arg {
823b3a8eb9SGleb Smirnoff 	struct dn_alg *fp;
833b3a8eb9SGleb Smirnoff 	struct dn_sch *sch;
843b3a8eb9SGleb Smirnoff };
853b3a8eb9SGleb Smirnoff 
863b3a8eb9SGleb Smirnoff /*---- callout hooks. ----*/
873b3a8eb9SGleb Smirnoff static struct callout dn_timeout;
88c8cfbc06SHans Petter Selasky static int dn_gone;
893b3a8eb9SGleb Smirnoff static struct task	dn_task;
903b3a8eb9SGleb Smirnoff static struct taskqueue	*dn_tq = NULL;
913b3a8eb9SGleb Smirnoff 
923b3a8eb9SGleb Smirnoff static void
933b3a8eb9SGleb Smirnoff dummynet(void *arg)
943b3a8eb9SGleb Smirnoff {
953b3a8eb9SGleb Smirnoff 
963b3a8eb9SGleb Smirnoff 	(void)arg;	/* UNUSED */
97cbc4d2dbSJohn Baldwin 	taskqueue_enqueue(dn_tq, &dn_task);
983b3a8eb9SGleb Smirnoff }
993b3a8eb9SGleb Smirnoff 
1003b3a8eb9SGleb Smirnoff void
1013b3a8eb9SGleb Smirnoff dn_reschedule(void)
1023b3a8eb9SGleb Smirnoff {
1035f4fc3dbSAlexander Motin 
104c8cfbc06SHans Petter Selasky 	if (dn_gone != 0)
105c8cfbc06SHans Petter Selasky 		return;
1065f4fc3dbSAlexander Motin 	callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL,
1075f4fc3dbSAlexander Motin 	    C_HARDCLOCK | C_DIRECT_EXEC);
1083b3a8eb9SGleb Smirnoff }
1093b3a8eb9SGleb Smirnoff /*----- end of callout hooks -----*/
1103b3a8eb9SGleb Smirnoff 
11191336b40SDon Lewis #ifdef NEW_AQM
11291336b40SDon Lewis /* Return AQM descriptor for given type or name. */
11391336b40SDon Lewis static struct dn_aqm *
11491336b40SDon Lewis find_aqm_type(int type, char *name)
11591336b40SDon Lewis {
11691336b40SDon Lewis 	struct dn_aqm *d;
11791336b40SDon Lewis 
11891336b40SDon Lewis 	SLIST_FOREACH(d, &dn_cfg.aqmlist, next) {
11991336b40SDon Lewis 		if (d->type == type || (name && !strcasecmp(d->name, name)))
12091336b40SDon Lewis 			return d;
12191336b40SDon Lewis 	}
12291336b40SDon Lewis 	return NULL; /* not found */
12391336b40SDon Lewis }
12491336b40SDon Lewis #endif
12591336b40SDon Lewis 
1263b3a8eb9SGleb Smirnoff /* Return a scheduler descriptor given the type or name. */
1273b3a8eb9SGleb Smirnoff static struct dn_alg *
1283b3a8eb9SGleb Smirnoff find_sched_type(int type, char *name)
1293b3a8eb9SGleb Smirnoff {
1303b3a8eb9SGleb Smirnoff 	struct dn_alg *d;
1313b3a8eb9SGleb Smirnoff 
1323b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(d, &dn_cfg.schedlist, next) {
1333b3a8eb9SGleb Smirnoff 		if (d->type == type || (name && !strcasecmp(d->name, name)))
1343b3a8eb9SGleb Smirnoff 			return d;
1353b3a8eb9SGleb Smirnoff 	}
1363b3a8eb9SGleb Smirnoff 	return NULL; /* not found */
1373b3a8eb9SGleb Smirnoff }
1383b3a8eb9SGleb Smirnoff 
1393b3a8eb9SGleb Smirnoff int
1403b3a8eb9SGleb Smirnoff ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
1413b3a8eb9SGleb Smirnoff {
1423b3a8eb9SGleb Smirnoff 	int oldv = *v;
1433b3a8eb9SGleb Smirnoff 	const char *op = NULL;
1443b3a8eb9SGleb Smirnoff 	if (dflt < lo)
1453b3a8eb9SGleb Smirnoff 		dflt = lo;
1463b3a8eb9SGleb Smirnoff 	if (dflt > hi)
1473b3a8eb9SGleb Smirnoff 		dflt = hi;
1483b3a8eb9SGleb Smirnoff 	if (oldv < lo) {
1493b3a8eb9SGleb Smirnoff 		*v = dflt;
1503b3a8eb9SGleb Smirnoff 		op = "Bump";
1513b3a8eb9SGleb Smirnoff 	} else if (oldv > hi) {
1523b3a8eb9SGleb Smirnoff 		*v = hi;
1533b3a8eb9SGleb Smirnoff 		op = "Clamp";
1543b3a8eb9SGleb Smirnoff 	} else
1553b3a8eb9SGleb Smirnoff 		return *v;
1563b3a8eb9SGleb Smirnoff 	if (op && msg)
1573b3a8eb9SGleb Smirnoff 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
1583b3a8eb9SGleb Smirnoff 	return *v;
1593b3a8eb9SGleb Smirnoff }
1603b3a8eb9SGleb Smirnoff 
1613b3a8eb9SGleb Smirnoff /*---- flow_id mask, hash and compare functions ---*/
1623b3a8eb9SGleb Smirnoff /*
1633b3a8eb9SGleb Smirnoff  * The flow_id includes the 5-tuple, the queue/pipe number
1643b3a8eb9SGleb Smirnoff  * which we store in the extra area in host order,
1653b3a8eb9SGleb Smirnoff  * and for ipv6 also the flow_id6.
1663b3a8eb9SGleb Smirnoff  * XXX see if we want the tos byte (can store in 'flags')
1673b3a8eb9SGleb Smirnoff  */
1683b3a8eb9SGleb Smirnoff static struct ipfw_flow_id *
1693b3a8eb9SGleb Smirnoff flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
1703b3a8eb9SGleb Smirnoff {
1713b3a8eb9SGleb Smirnoff 	int is_v6 = IS_IP6_FLOW_ID(id);
1723b3a8eb9SGleb Smirnoff 
1733b3a8eb9SGleb Smirnoff 	id->dst_port &= mask->dst_port;
1743b3a8eb9SGleb Smirnoff 	id->src_port &= mask->src_port;
1753b3a8eb9SGleb Smirnoff 	id->proto &= mask->proto;
1763b3a8eb9SGleb Smirnoff 	id->extra &= mask->extra;
1773b3a8eb9SGleb Smirnoff 	if (is_v6) {
1783b3a8eb9SGleb Smirnoff 		APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
1793b3a8eb9SGleb Smirnoff 		APPLY_MASK(&id->src_ip6, &mask->src_ip6);
1803b3a8eb9SGleb Smirnoff 		id->flow_id6 &= mask->flow_id6;
1813b3a8eb9SGleb Smirnoff 	} else {
1823b3a8eb9SGleb Smirnoff 		id->dst_ip &= mask->dst_ip;
1833b3a8eb9SGleb Smirnoff 		id->src_ip &= mask->src_ip;
1843b3a8eb9SGleb Smirnoff 	}
1853b3a8eb9SGleb Smirnoff 	return id;
1863b3a8eb9SGleb Smirnoff }
1873b3a8eb9SGleb Smirnoff 
1883b3a8eb9SGleb Smirnoff /* computes an OR of two masks, result in dst and also returned */
1893b3a8eb9SGleb Smirnoff static struct ipfw_flow_id *
1903b3a8eb9SGleb Smirnoff flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
1913b3a8eb9SGleb Smirnoff {
1923b3a8eb9SGleb Smirnoff 	int is_v6 = IS_IP6_FLOW_ID(dst);
1933b3a8eb9SGleb Smirnoff 
1943b3a8eb9SGleb Smirnoff 	dst->dst_port |= src->dst_port;
1953b3a8eb9SGleb Smirnoff 	dst->src_port |= src->src_port;
1963b3a8eb9SGleb Smirnoff 	dst->proto |= src->proto;
1973b3a8eb9SGleb Smirnoff 	dst->extra |= src->extra;
1983b3a8eb9SGleb Smirnoff 	if (is_v6) {
1993b3a8eb9SGleb Smirnoff #define OR_MASK(_d, _s)                          \
2003b3a8eb9SGleb Smirnoff     (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
2013b3a8eb9SGleb Smirnoff     (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
2023b3a8eb9SGleb Smirnoff     (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
2033b3a8eb9SGleb Smirnoff     (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
2043b3a8eb9SGleb Smirnoff 		OR_MASK(&dst->dst_ip6, &src->dst_ip6);
2053b3a8eb9SGleb Smirnoff 		OR_MASK(&dst->src_ip6, &src->src_ip6);
2063b3a8eb9SGleb Smirnoff #undef OR_MASK
2073b3a8eb9SGleb Smirnoff 		dst->flow_id6 |= src->flow_id6;
2083b3a8eb9SGleb Smirnoff 	} else {
2093b3a8eb9SGleb Smirnoff 		dst->dst_ip |= src->dst_ip;
2103b3a8eb9SGleb Smirnoff 		dst->src_ip |= src->src_ip;
2113b3a8eb9SGleb Smirnoff 	}
2123b3a8eb9SGleb Smirnoff 	return dst;
2133b3a8eb9SGleb Smirnoff }
2143b3a8eb9SGleb Smirnoff 
2153b3a8eb9SGleb Smirnoff static int
2163b3a8eb9SGleb Smirnoff nonzero_mask(struct ipfw_flow_id *m)
2173b3a8eb9SGleb Smirnoff {
2183b3a8eb9SGleb Smirnoff 	if (m->dst_port || m->src_port || m->proto || m->extra)
2193b3a8eb9SGleb Smirnoff 		return 1;
2203b3a8eb9SGleb Smirnoff 	if (IS_IP6_FLOW_ID(m)) {
2213b3a8eb9SGleb Smirnoff 		return
2223b3a8eb9SGleb Smirnoff 			m->dst_ip6.__u6_addr.__u6_addr32[0] ||
2233b3a8eb9SGleb Smirnoff 			m->dst_ip6.__u6_addr.__u6_addr32[1] ||
2243b3a8eb9SGleb Smirnoff 			m->dst_ip6.__u6_addr.__u6_addr32[2] ||
2253b3a8eb9SGleb Smirnoff 			m->dst_ip6.__u6_addr.__u6_addr32[3] ||
2263b3a8eb9SGleb Smirnoff 			m->src_ip6.__u6_addr.__u6_addr32[0] ||
2273b3a8eb9SGleb Smirnoff 			m->src_ip6.__u6_addr.__u6_addr32[1] ||
2283b3a8eb9SGleb Smirnoff 			m->src_ip6.__u6_addr.__u6_addr32[2] ||
2293b3a8eb9SGleb Smirnoff 			m->src_ip6.__u6_addr.__u6_addr32[3] ||
2303b3a8eb9SGleb Smirnoff 			m->flow_id6;
2313b3a8eb9SGleb Smirnoff 	} else {
2323b3a8eb9SGleb Smirnoff 		return m->dst_ip || m->src_ip;
2333b3a8eb9SGleb Smirnoff 	}
2343b3a8eb9SGleb Smirnoff }
2353b3a8eb9SGleb Smirnoff 
2363b3a8eb9SGleb Smirnoff /* XXX we may want a better hash function */
2373b3a8eb9SGleb Smirnoff static uint32_t
2383b3a8eb9SGleb Smirnoff flow_id_hash(struct ipfw_flow_id *id)
2393b3a8eb9SGleb Smirnoff {
2403b3a8eb9SGleb Smirnoff     uint32_t i;
2413b3a8eb9SGleb Smirnoff 
2423b3a8eb9SGleb Smirnoff     if (IS_IP6_FLOW_ID(id)) {
2433b3a8eb9SGleb Smirnoff 	uint32_t *d = (uint32_t *)&id->dst_ip6;
2443b3a8eb9SGleb Smirnoff 	uint32_t *s = (uint32_t *)&id->src_ip6;
2453b3a8eb9SGleb Smirnoff         i = (d[0]      ) ^ (d[1])       ^
2463b3a8eb9SGleb Smirnoff             (d[2]      ) ^ (d[3])       ^
2473b3a8eb9SGleb Smirnoff             (d[0] >> 15) ^ (d[1] >> 15) ^
2483b3a8eb9SGleb Smirnoff             (d[2] >> 15) ^ (d[3] >> 15) ^
2493b3a8eb9SGleb Smirnoff             (s[0] <<  1) ^ (s[1] <<  1) ^
2503b3a8eb9SGleb Smirnoff             (s[2] <<  1) ^ (s[3] <<  1) ^
2513b3a8eb9SGleb Smirnoff             (s[0] << 16) ^ (s[1] << 16) ^
2523b3a8eb9SGleb Smirnoff             (s[2] << 16) ^ (s[3] << 16) ^
2533b3a8eb9SGleb Smirnoff             (id->dst_port << 1) ^ (id->src_port) ^
2543b3a8eb9SGleb Smirnoff 	    (id->extra) ^
2553b3a8eb9SGleb Smirnoff             (id->proto ) ^ (id->flow_id6);
2563b3a8eb9SGleb Smirnoff     } else {
2573b3a8eb9SGleb Smirnoff         i = (id->dst_ip)        ^ (id->dst_ip >> 15) ^
2583b3a8eb9SGleb Smirnoff             (id->src_ip << 1)   ^ (id->src_ip >> 16) ^
2593b3a8eb9SGleb Smirnoff 	    (id->extra) ^
2603b3a8eb9SGleb Smirnoff             (id->dst_port << 1) ^ (id->src_port)     ^ (id->proto);
2613b3a8eb9SGleb Smirnoff     }
2623b3a8eb9SGleb Smirnoff     return i;
2633b3a8eb9SGleb Smirnoff }
2643b3a8eb9SGleb Smirnoff 
2653b3a8eb9SGleb Smirnoff /* Like bcmp, returns 0 if ids match, 1 otherwise. */
2663b3a8eb9SGleb Smirnoff static int
2673b3a8eb9SGleb Smirnoff flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
2683b3a8eb9SGleb Smirnoff {
2693b3a8eb9SGleb Smirnoff 	int is_v6 = IS_IP6_FLOW_ID(id1);
2703b3a8eb9SGleb Smirnoff 
2713b3a8eb9SGleb Smirnoff 	if (!is_v6) {
2723b3a8eb9SGleb Smirnoff 	    if (IS_IP6_FLOW_ID(id2))
2733b3a8eb9SGleb Smirnoff 		return 1; /* different address families */
2743b3a8eb9SGleb Smirnoff 
2753b3a8eb9SGleb Smirnoff 	    return (id1->dst_ip == id2->dst_ip &&
2763b3a8eb9SGleb Smirnoff 		    id1->src_ip == id2->src_ip &&
2773b3a8eb9SGleb Smirnoff 		    id1->dst_port == id2->dst_port &&
2783b3a8eb9SGleb Smirnoff 		    id1->src_port == id2->src_port &&
2793b3a8eb9SGleb Smirnoff 		    id1->proto == id2->proto &&
2803b3a8eb9SGleb Smirnoff 		    id1->extra == id2->extra) ? 0 : 1;
2813b3a8eb9SGleb Smirnoff 	}
2823b3a8eb9SGleb Smirnoff 	/* the ipv6 case */
2833b3a8eb9SGleb Smirnoff 	return (
2843b3a8eb9SGleb Smirnoff 	    !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
2853b3a8eb9SGleb Smirnoff 	    !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
2863b3a8eb9SGleb Smirnoff 	    id1->dst_port == id2->dst_port &&
2873b3a8eb9SGleb Smirnoff 	    id1->src_port == id2->src_port &&
2883b3a8eb9SGleb Smirnoff 	    id1->proto == id2->proto &&
2893b3a8eb9SGleb Smirnoff 	    id1->extra == id2->extra &&
2903b3a8eb9SGleb Smirnoff 	    id1->flow_id6 == id2->flow_id6) ? 0 : 1;
2913b3a8eb9SGleb Smirnoff }
2923b3a8eb9SGleb Smirnoff /*--------- end of flow-id mask, hash and compare ---------*/
2933b3a8eb9SGleb Smirnoff 
2943b3a8eb9SGleb Smirnoff /*--- support functions for the qht hashtable ----
2953b3a8eb9SGleb Smirnoff  * Entries are hashed by flow-id
2963b3a8eb9SGleb Smirnoff  */
2973b3a8eb9SGleb Smirnoff static uint32_t
2983b3a8eb9SGleb Smirnoff q_hash(uintptr_t key, int flags, void *arg)
2993b3a8eb9SGleb Smirnoff {
3003b3a8eb9SGleb Smirnoff 	/* compute the hash slot from the flow id */
3013b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
3023b3a8eb9SGleb Smirnoff 		&((struct dn_queue *)key)->ni.fid :
3033b3a8eb9SGleb Smirnoff 		(struct ipfw_flow_id *)key;
3043b3a8eb9SGleb Smirnoff 
3053b3a8eb9SGleb Smirnoff 	return flow_id_hash(id);
3063b3a8eb9SGleb Smirnoff }
3073b3a8eb9SGleb Smirnoff 
3083b3a8eb9SGleb Smirnoff static int
3093b3a8eb9SGleb Smirnoff q_match(void *obj, uintptr_t key, int flags, void *arg)
3103b3a8eb9SGleb Smirnoff {
3113b3a8eb9SGleb Smirnoff 	struct dn_queue *o = (struct dn_queue *)obj;
3123b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id2;
3133b3a8eb9SGleb Smirnoff 
3143b3a8eb9SGleb Smirnoff 	if (flags & DNHT_KEY_IS_OBJ) {
3153b3a8eb9SGleb Smirnoff 		/* compare pointers */
3163b3a8eb9SGleb Smirnoff 		id2 = &((struct dn_queue *)key)->ni.fid;
3173b3a8eb9SGleb Smirnoff 	} else {
3183b3a8eb9SGleb Smirnoff 		id2 = (struct ipfw_flow_id *)key;
3193b3a8eb9SGleb Smirnoff 	}
3203b3a8eb9SGleb Smirnoff 	return (0 == flow_id_cmp(&o->ni.fid,  id2));
3213b3a8eb9SGleb Smirnoff }
3223b3a8eb9SGleb Smirnoff 
3233b3a8eb9SGleb Smirnoff /*
3243b3a8eb9SGleb Smirnoff  * create a new queue instance for the given 'key'.
3253b3a8eb9SGleb Smirnoff  */
3263b3a8eb9SGleb Smirnoff static void *
3273b3a8eb9SGleb Smirnoff q_new(uintptr_t key, int flags, void *arg)
3283b3a8eb9SGleb Smirnoff {
3293b3a8eb9SGleb Smirnoff 	struct dn_queue *q, *template = arg;
3303b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = template->fs;
3313b3a8eb9SGleb Smirnoff 	int size = sizeof(*q) + fs->sched->fp->q_datalen;
3323b3a8eb9SGleb Smirnoff 
3333b3a8eb9SGleb Smirnoff 	q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
3343b3a8eb9SGleb Smirnoff 	if (q == NULL) {
3353b3a8eb9SGleb Smirnoff 		D("no memory for new queue");
3363b3a8eb9SGleb Smirnoff 		return NULL;
3373b3a8eb9SGleb Smirnoff 	}
3383b3a8eb9SGleb Smirnoff 
3393b3a8eb9SGleb Smirnoff 	set_oid(&q->ni.oid, DN_QUEUE, size);
3403b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QHT_HASH)
3413b3a8eb9SGleb Smirnoff 		q->ni.fid = *(struct ipfw_flow_id *)key;
3423b3a8eb9SGleb Smirnoff 	q->fs = fs;
3433b3a8eb9SGleb Smirnoff 	q->_si = template->_si;
3443b3a8eb9SGleb Smirnoff 	q->_si->q_count++;
3453b3a8eb9SGleb Smirnoff 
3463b3a8eb9SGleb Smirnoff 	if (fs->sched->fp->new_queue)
3473b3a8eb9SGleb Smirnoff 		fs->sched->fp->new_queue(q);
34891336b40SDon Lewis 
34991336b40SDon Lewis #ifdef NEW_AQM
35091336b40SDon Lewis 	/* call AQM init function after creating a queue*/
35191336b40SDon Lewis 	if (fs->aqmfp && fs->aqmfp->init)
35291336b40SDon Lewis 		if(fs->aqmfp->init(q))
35391336b40SDon Lewis 			D("unable to init AQM for fs %d", fs->fs.fs_nr);
35491336b40SDon Lewis #endif
3553b3a8eb9SGleb Smirnoff 	dn_cfg.queue_count++;
35691336b40SDon Lewis 
3573b3a8eb9SGleb Smirnoff 	return q;
3583b3a8eb9SGleb Smirnoff }
3593b3a8eb9SGleb Smirnoff 
3603b3a8eb9SGleb Smirnoff /*
3613b3a8eb9SGleb Smirnoff  * Notify schedulers that a queue is going away.
3623b3a8eb9SGleb Smirnoff  * If (flags & DN_DESTROY), also free the packets.
3633b3a8eb9SGleb Smirnoff  * The version for callbacks is called q_delete_cb().
3643b3a8eb9SGleb Smirnoff  */
3653b3a8eb9SGleb Smirnoff static void
3663b3a8eb9SGleb Smirnoff dn_delete_queue(struct dn_queue *q, int flags)
3673b3a8eb9SGleb Smirnoff {
3683b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = q->fs;
3693b3a8eb9SGleb Smirnoff 
37091336b40SDon Lewis #ifdef NEW_AQM
37191336b40SDon Lewis 	/* clean up AQM status for queue 'q'
37291336b40SDon Lewis 	 * cleanup here is called just with MULTIQUEUE
37391336b40SDon Lewis 	 */
37491336b40SDon Lewis 	if (fs && fs->aqmfp && fs->aqmfp->cleanup)
37591336b40SDon Lewis 		fs->aqmfp->cleanup(q);
37691336b40SDon Lewis #endif
3773b3a8eb9SGleb Smirnoff 	// D("fs %p si %p\n", fs, q->_si);
3783b3a8eb9SGleb Smirnoff 	/* notify the parent scheduler that the queue is going away */
3793b3a8eb9SGleb Smirnoff 	if (fs && fs->sched->fp->free_queue)
3803b3a8eb9SGleb Smirnoff 		fs->sched->fp->free_queue(q);
3813b3a8eb9SGleb Smirnoff 	q->_si->q_count--;
3823b3a8eb9SGleb Smirnoff 	q->_si = NULL;
3833b3a8eb9SGleb Smirnoff 	if (flags & DN_DESTROY) {
3843b3a8eb9SGleb Smirnoff 		if (q->mq.head)
3853b3a8eb9SGleb Smirnoff 			dn_free_pkts(q->mq.head);
3863b3a8eb9SGleb Smirnoff 		bzero(q, sizeof(*q));	// safety
3873b3a8eb9SGleb Smirnoff 		free(q, M_DUMMYNET);
3883b3a8eb9SGleb Smirnoff 		dn_cfg.queue_count--;
3893b3a8eb9SGleb Smirnoff 	}
3903b3a8eb9SGleb Smirnoff }
3913b3a8eb9SGleb Smirnoff 
3923b3a8eb9SGleb Smirnoff static int
3933b3a8eb9SGleb Smirnoff q_delete_cb(void *q, void *arg)
3943b3a8eb9SGleb Smirnoff {
3953b3a8eb9SGleb Smirnoff 	int flags = (int)(uintptr_t)arg;
3963b3a8eb9SGleb Smirnoff 	dn_delete_queue(q, flags);
3973b3a8eb9SGleb Smirnoff 	return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
3983b3a8eb9SGleb Smirnoff }
3993b3a8eb9SGleb Smirnoff 
4003b3a8eb9SGleb Smirnoff /*
4013b3a8eb9SGleb Smirnoff  * calls dn_delete_queue/q_delete_cb on all queues,
4023b3a8eb9SGleb Smirnoff  * which notifies the parent scheduler and possibly drains packets.
4033b3a8eb9SGleb Smirnoff  * flags & DN_DESTROY: drains queues and destroy qht;
4043b3a8eb9SGleb Smirnoff  */
4053b3a8eb9SGleb Smirnoff static void
4063b3a8eb9SGleb Smirnoff qht_delete(struct dn_fsk *fs, int flags)
4073b3a8eb9SGleb Smirnoff {
4083b3a8eb9SGleb Smirnoff 	ND("fs %d start flags %d qht %p",
4093b3a8eb9SGleb Smirnoff 		fs->fs.fs_nr, flags, fs->qht);
4103b3a8eb9SGleb Smirnoff 	if (!fs->qht)
4113b3a8eb9SGleb Smirnoff 		return;
4123b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QHT_HASH) {
4133b3a8eb9SGleb Smirnoff 		dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
4143b3a8eb9SGleb Smirnoff 		if (flags & DN_DESTROY) {
4153b3a8eb9SGleb Smirnoff 			dn_ht_free(fs->qht, 0);
4163b3a8eb9SGleb Smirnoff 			fs->qht = NULL;
4173b3a8eb9SGleb Smirnoff 		}
4183b3a8eb9SGleb Smirnoff 	} else {
4193b3a8eb9SGleb Smirnoff 		dn_delete_queue((struct dn_queue *)(fs->qht), flags);
4203b3a8eb9SGleb Smirnoff 		if (flags & DN_DESTROY)
4213b3a8eb9SGleb Smirnoff 			fs->qht = NULL;
4223b3a8eb9SGleb Smirnoff 	}
4233b3a8eb9SGleb Smirnoff }
4243b3a8eb9SGleb Smirnoff 
4253b3a8eb9SGleb Smirnoff /*
4263b3a8eb9SGleb Smirnoff  * Find and possibly create the queue for a MULTIQUEUE scheduler.
4273b3a8eb9SGleb Smirnoff  * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
4283b3a8eb9SGleb Smirnoff  */
4293b3a8eb9SGleb Smirnoff struct dn_queue *
4303b3a8eb9SGleb Smirnoff ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
4313b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id)
4323b3a8eb9SGleb Smirnoff {
4333b3a8eb9SGleb Smirnoff 	struct dn_queue template;
4343b3a8eb9SGleb Smirnoff 
4353b3a8eb9SGleb Smirnoff 	template._si = si;
4363b3a8eb9SGleb Smirnoff 	template.fs = fs;
4373b3a8eb9SGleb Smirnoff 
4383b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QHT_HASH) {
4393b3a8eb9SGleb Smirnoff 		struct ipfw_flow_id masked_id;
4403b3a8eb9SGleb Smirnoff 		if (fs->qht == NULL) {
4413b3a8eb9SGleb Smirnoff 			fs->qht = dn_ht_init(NULL, fs->fs.buckets,
4423b3a8eb9SGleb Smirnoff 				offsetof(struct dn_queue, q_next),
4433b3a8eb9SGleb Smirnoff 				q_hash, q_match, q_new);
4443b3a8eb9SGleb Smirnoff 			if (fs->qht == NULL)
4453b3a8eb9SGleb Smirnoff 				return NULL;
4463b3a8eb9SGleb Smirnoff 		}
4473b3a8eb9SGleb Smirnoff 		masked_id = *id;
4483b3a8eb9SGleb Smirnoff 		flow_id_mask(&fs->fsk_mask, &masked_id);
4493b3a8eb9SGleb Smirnoff 		return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
4503b3a8eb9SGleb Smirnoff 			DNHT_INSERT, &template);
4513b3a8eb9SGleb Smirnoff 	} else {
4523b3a8eb9SGleb Smirnoff 		if (fs->qht == NULL)
4533b3a8eb9SGleb Smirnoff 			fs->qht = q_new(0, 0, &template);
4543b3a8eb9SGleb Smirnoff 		return (struct dn_queue *)fs->qht;
4553b3a8eb9SGleb Smirnoff 	}
4563b3a8eb9SGleb Smirnoff }
4573b3a8eb9SGleb Smirnoff /*--- end of queue hash table ---*/
4583b3a8eb9SGleb Smirnoff 
4593b3a8eb9SGleb Smirnoff /*--- support functions for the sch_inst hashtable ----
4603b3a8eb9SGleb Smirnoff  *
4613b3a8eb9SGleb Smirnoff  * These are hashed by flow-id
4623b3a8eb9SGleb Smirnoff  */
4633b3a8eb9SGleb Smirnoff static uint32_t
4643b3a8eb9SGleb Smirnoff si_hash(uintptr_t key, int flags, void *arg)
4653b3a8eb9SGleb Smirnoff {
4663b3a8eb9SGleb Smirnoff 	/* compute the hash slot from the flow id */
4673b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
4683b3a8eb9SGleb Smirnoff 		&((struct dn_sch_inst *)key)->ni.fid :
4693b3a8eb9SGleb Smirnoff 		(struct ipfw_flow_id *)key;
4703b3a8eb9SGleb Smirnoff 
4713b3a8eb9SGleb Smirnoff 	return flow_id_hash(id);
4723b3a8eb9SGleb Smirnoff }
4733b3a8eb9SGleb Smirnoff 
4743b3a8eb9SGleb Smirnoff static int
4753b3a8eb9SGleb Smirnoff si_match(void *obj, uintptr_t key, int flags, void *arg)
4763b3a8eb9SGleb Smirnoff {
4773b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *o = obj;
4783b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id2;
4793b3a8eb9SGleb Smirnoff 
4803b3a8eb9SGleb Smirnoff 	id2 = (flags & DNHT_KEY_IS_OBJ) ?
4813b3a8eb9SGleb Smirnoff 		&((struct dn_sch_inst *)key)->ni.fid :
4823b3a8eb9SGleb Smirnoff 		(struct ipfw_flow_id *)key;
4833b3a8eb9SGleb Smirnoff 	return flow_id_cmp(&o->ni.fid,  id2) == 0;
4843b3a8eb9SGleb Smirnoff }
4853b3a8eb9SGleb Smirnoff 
4863b3a8eb9SGleb Smirnoff /*
4873b3a8eb9SGleb Smirnoff  * create a new instance for the given 'key'
4883b3a8eb9SGleb Smirnoff  * Allocate memory for instance, delay line and scheduler private data.
4893b3a8eb9SGleb Smirnoff  */
4903b3a8eb9SGleb Smirnoff static void *
4913b3a8eb9SGleb Smirnoff si_new(uintptr_t key, int flags, void *arg)
4923b3a8eb9SGleb Smirnoff {
4933b3a8eb9SGleb Smirnoff 	struct dn_schk *s = arg;
4943b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si;
4953b3a8eb9SGleb Smirnoff 	int l = sizeof(*si) + s->fp->si_datalen;
4963b3a8eb9SGleb Smirnoff 
4973b3a8eb9SGleb Smirnoff 	si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
4983b3a8eb9SGleb Smirnoff 	if (si == NULL)
4993b3a8eb9SGleb Smirnoff 		goto error;
5003b3a8eb9SGleb Smirnoff 
5013b3a8eb9SGleb Smirnoff 	/* Set length only for the part passed up to userland. */
5023b3a8eb9SGleb Smirnoff 	set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
5033b3a8eb9SGleb Smirnoff 	set_oid(&(si->dline.oid), DN_DELAY_LINE,
5043b3a8eb9SGleb Smirnoff 		sizeof(struct delay_line));
5053b3a8eb9SGleb Smirnoff 	/* mark si and dline as outside the event queue */
5063b3a8eb9SGleb Smirnoff 	si->ni.oid.id = si->dline.oid.id = -1;
5073b3a8eb9SGleb Smirnoff 
5083b3a8eb9SGleb Smirnoff 	si->sched = s;
5093b3a8eb9SGleb Smirnoff 	si->dline.si = si;
5103b3a8eb9SGleb Smirnoff 
5113b3a8eb9SGleb Smirnoff 	if (s->fp->new_sched && s->fp->new_sched(si)) {
5123b3a8eb9SGleb Smirnoff 		D("new_sched error");
5133b3a8eb9SGleb Smirnoff 		goto error;
5143b3a8eb9SGleb Smirnoff 	}
5153b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK)
5163b3a8eb9SGleb Smirnoff 		si->ni.fid = *(struct ipfw_flow_id *)key;
5173b3a8eb9SGleb Smirnoff 
51891336b40SDon Lewis #ifdef NEW_AQM
51991336b40SDon Lewis 	/* init AQM status for !DN_MULTIQUEUE sched*/
52091336b40SDon Lewis 	if (!(s->fp->flags & DN_MULTIQUEUE))
52191336b40SDon Lewis 		if (s->fs->aqmfp && s->fs->aqmfp->init)
52291336b40SDon Lewis 			if(s->fs->aqmfp->init((struct dn_queue *)(si + 1))) {
52391336b40SDon Lewis 				D("unable to init AQM for fs %d", s->fs->fs.fs_nr);
52491336b40SDon Lewis 				goto error;
52591336b40SDon Lewis 			}
52691336b40SDon Lewis #endif
52791336b40SDon Lewis 
5283b3a8eb9SGleb Smirnoff 	dn_cfg.si_count++;
5293b3a8eb9SGleb Smirnoff 	return si;
5303b3a8eb9SGleb Smirnoff 
5313b3a8eb9SGleb Smirnoff error:
5323b3a8eb9SGleb Smirnoff 	if (si) {
5333b3a8eb9SGleb Smirnoff 		bzero(si, sizeof(*si)); // safety
5343b3a8eb9SGleb Smirnoff 		free(si, M_DUMMYNET);
5353b3a8eb9SGleb Smirnoff 	}
5363b3a8eb9SGleb Smirnoff         return NULL;
5373b3a8eb9SGleb Smirnoff }
5383b3a8eb9SGleb Smirnoff 
5393b3a8eb9SGleb Smirnoff /*
5403b3a8eb9SGleb Smirnoff  * Callback from siht to delete all scheduler instances. Remove
5413b3a8eb9SGleb Smirnoff  * si and delay line from the system heap, destroy all queues.
5423b3a8eb9SGleb Smirnoff  * We assume that all flowset have been notified and do not
5433b3a8eb9SGleb Smirnoff  * point to us anymore.
5443b3a8eb9SGleb Smirnoff  */
5453b3a8eb9SGleb Smirnoff static int
5463b3a8eb9SGleb Smirnoff si_destroy(void *_si, void *arg)
5473b3a8eb9SGleb Smirnoff {
5483b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si = _si;
5493b3a8eb9SGleb Smirnoff 	struct dn_schk *s = si->sched;
5503b3a8eb9SGleb Smirnoff 	struct delay_line *dl = &si->dline;
5513b3a8eb9SGleb Smirnoff 
5523b3a8eb9SGleb Smirnoff 	if (dl->oid.subtype) /* remove delay line from event heap */
5533b3a8eb9SGleb Smirnoff 		heap_extract(&dn_cfg.evheap, dl);
5543b3a8eb9SGleb Smirnoff 	dn_free_pkts(dl->mq.head);	/* drain delay line */
5553b3a8eb9SGleb Smirnoff 	if (si->kflags & DN_ACTIVE) /* remove si from event heap */
5563b3a8eb9SGleb Smirnoff 		heap_extract(&dn_cfg.evheap, si);
55791336b40SDon Lewis 
55891336b40SDon Lewis #ifdef NEW_AQM
55991336b40SDon Lewis 	/* clean up AQM status for !DN_MULTIQUEUE sched
56091336b40SDon Lewis 	 * Note that all queues belong to fs were cleaned up in fsk_detach.
56191336b40SDon Lewis 	 * When drain_scheduler is called s->fs and q->fs are pointing
56291336b40SDon Lewis 	 * to a correct fs, so we can use fs in this case.
56391336b40SDon Lewis 	 */
56491336b40SDon Lewis 	if (!(s->fp->flags & DN_MULTIQUEUE)) {
56591336b40SDon Lewis 		struct dn_queue *q = (struct dn_queue *)(si + 1);
56691336b40SDon Lewis 		if (q->aqm_status && q->fs->aqmfp)
56791336b40SDon Lewis 			if (q->fs->aqmfp->cleanup)
56891336b40SDon Lewis 				q->fs->aqmfp->cleanup(q);
56991336b40SDon Lewis 	}
57091336b40SDon Lewis #endif
5713b3a8eb9SGleb Smirnoff 	if (s->fp->free_sched)
5723b3a8eb9SGleb Smirnoff 		s->fp->free_sched(si);
5733b3a8eb9SGleb Smirnoff 	bzero(si, sizeof(*si));	/* safety */
5743b3a8eb9SGleb Smirnoff 	free(si, M_DUMMYNET);
5753b3a8eb9SGleb Smirnoff 	dn_cfg.si_count--;
5763b3a8eb9SGleb Smirnoff 	return DNHT_SCAN_DEL;
5773b3a8eb9SGleb Smirnoff }
5783b3a8eb9SGleb Smirnoff 
5793b3a8eb9SGleb Smirnoff /*
5803b3a8eb9SGleb Smirnoff  * Find the scheduler instance for this packet. If we need to apply
5813b3a8eb9SGleb Smirnoff  * a mask, do on a local copy of the flow_id to preserve the original.
5823b3a8eb9SGleb Smirnoff  * Assume siht is always initialized if we have a mask.
5833b3a8eb9SGleb Smirnoff  */
5843b3a8eb9SGleb Smirnoff struct dn_sch_inst *
5853b3a8eb9SGleb Smirnoff ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
5863b3a8eb9SGleb Smirnoff {
5873b3a8eb9SGleb Smirnoff 
5883b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK) {
5893b3a8eb9SGleb Smirnoff 		struct ipfw_flow_id id_t = *id;
5903b3a8eb9SGleb Smirnoff 		flow_id_mask(&s->sch.sched_mask, &id_t);
5913b3a8eb9SGleb Smirnoff 		return dn_ht_find(s->siht, (uintptr_t)&id_t,
5923b3a8eb9SGleb Smirnoff 			DNHT_INSERT, s);
5933b3a8eb9SGleb Smirnoff 	}
5943b3a8eb9SGleb Smirnoff 	if (!s->siht)
5953b3a8eb9SGleb Smirnoff 		s->siht = si_new(0, 0, s);
5963b3a8eb9SGleb Smirnoff 	return (struct dn_sch_inst *)s->siht;
5973b3a8eb9SGleb Smirnoff }
5983b3a8eb9SGleb Smirnoff 
5993b3a8eb9SGleb Smirnoff /* callback to flush credit for the scheduler instance */
6003b3a8eb9SGleb Smirnoff static int
6013b3a8eb9SGleb Smirnoff si_reset_credit(void *_si, void *arg)
6023b3a8eb9SGleb Smirnoff {
6033b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si = _si;
6043b3a8eb9SGleb Smirnoff 	struct dn_link *p = &si->sched->link;
6053b3a8eb9SGleb Smirnoff 
6063b3a8eb9SGleb Smirnoff 	si->credit = p->burst + (dn_cfg.io_fast ?  p->bandwidth : 0);
6073b3a8eb9SGleb Smirnoff 	return 0;
6083b3a8eb9SGleb Smirnoff }
6093b3a8eb9SGleb Smirnoff 
6103b3a8eb9SGleb Smirnoff static void
6113b3a8eb9SGleb Smirnoff schk_reset_credit(struct dn_schk *s)
6123b3a8eb9SGleb Smirnoff {
6133b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK)
6143b3a8eb9SGleb Smirnoff 		dn_ht_scan(s->siht, si_reset_credit, NULL);
6153b3a8eb9SGleb Smirnoff 	else if (s->siht)
6163b3a8eb9SGleb Smirnoff 		si_reset_credit(s->siht, NULL);
6173b3a8eb9SGleb Smirnoff }
6183b3a8eb9SGleb Smirnoff /*---- end of sch_inst hashtable ---------------------*/
6193b3a8eb9SGleb Smirnoff 
6203b3a8eb9SGleb Smirnoff /*-------------------------------------------------------
6213b3a8eb9SGleb Smirnoff  * flowset hash (fshash) support. Entries are hashed by fs_nr.
6223b3a8eb9SGleb Smirnoff  * New allocations are put in the fsunlinked list, from which
6233b3a8eb9SGleb Smirnoff  * they are removed when they point to a specific scheduler.
6243b3a8eb9SGleb Smirnoff  */
6253b3a8eb9SGleb Smirnoff static uint32_t
6263b3a8eb9SGleb Smirnoff fsk_hash(uintptr_t key, int flags, void *arg)
6273b3a8eb9SGleb Smirnoff {
6283b3a8eb9SGleb Smirnoff 	uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
6293b3a8eb9SGleb Smirnoff 		((struct dn_fsk *)key)->fs.fs_nr;
6303b3a8eb9SGleb Smirnoff 
6313b3a8eb9SGleb Smirnoff 	return ( (i>>8)^(i>>4)^i );
6323b3a8eb9SGleb Smirnoff }
6333b3a8eb9SGleb Smirnoff 
6343b3a8eb9SGleb Smirnoff static int
6353b3a8eb9SGleb Smirnoff fsk_match(void *obj, uintptr_t key, int flags, void *arg)
6363b3a8eb9SGleb Smirnoff {
6373b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = obj;
6383b3a8eb9SGleb Smirnoff 	int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
6393b3a8eb9SGleb Smirnoff 		((struct dn_fsk *)key)->fs.fs_nr;
6403b3a8eb9SGleb Smirnoff 
6413b3a8eb9SGleb Smirnoff 	return (fs->fs.fs_nr == i);
6423b3a8eb9SGleb Smirnoff }
6433b3a8eb9SGleb Smirnoff 
6443b3a8eb9SGleb Smirnoff static void *
6453b3a8eb9SGleb Smirnoff fsk_new(uintptr_t key, int flags, void *arg)
6463b3a8eb9SGleb Smirnoff {
6473b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
6483b3a8eb9SGleb Smirnoff 
6493b3a8eb9SGleb Smirnoff 	fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
6503b3a8eb9SGleb Smirnoff 	if (fs) {
6513b3a8eb9SGleb Smirnoff 		set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
6523b3a8eb9SGleb Smirnoff 		dn_cfg.fsk_count++;
6533b3a8eb9SGleb Smirnoff 		fs->drain_bucket = 0;
6543b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
6553b3a8eb9SGleb Smirnoff 	}
6563b3a8eb9SGleb Smirnoff 	return fs;
6573b3a8eb9SGleb Smirnoff }
6583b3a8eb9SGleb Smirnoff 
65991336b40SDon Lewis #ifdef NEW_AQM
66091336b40SDon Lewis /* callback function for cleaning up AQM queue status belongs to a flowset
66191336b40SDon Lewis  * connected to scheduler instance '_si' (for !DN_MULTIQUEUE only).
66291336b40SDon Lewis  */
66391336b40SDon Lewis static int
66491336b40SDon Lewis si_cleanup_q(void *_si, void *arg)
66591336b40SDon Lewis {
66691336b40SDon Lewis 	struct dn_sch_inst *si = _si;
66791336b40SDon Lewis 
66891336b40SDon Lewis 	if (!(si->sched->fp->flags & DN_MULTIQUEUE)) {
66991336b40SDon Lewis 		if (si->sched->fs->aqmfp && si->sched->fs->aqmfp->cleanup)
67091336b40SDon Lewis 			si->sched->fs->aqmfp->cleanup((struct dn_queue *) (si+1));
67191336b40SDon Lewis 	}
67291336b40SDon Lewis 	return 0;
67391336b40SDon Lewis }
67491336b40SDon Lewis 
67591336b40SDon Lewis /* callback to clean up queue AQM status.*/
67691336b40SDon Lewis static int
67791336b40SDon Lewis q_cleanup_q(void *_q, void *arg)
67891336b40SDon Lewis {
67991336b40SDon Lewis 	struct dn_queue *q = _q;
68091336b40SDon Lewis 	q->fs->aqmfp->cleanup(q);
68191336b40SDon Lewis 	return 0;
68291336b40SDon Lewis }
68391336b40SDon Lewis 
68491336b40SDon Lewis /* Clean up all AQM queues status belongs to flowset 'fs' and then
68591336b40SDon Lewis  * deconfig AQM for flowset 'fs'
68691336b40SDon Lewis  */
68791336b40SDon Lewis static void
68891336b40SDon Lewis aqm_cleanup_deconfig_fs(struct dn_fsk *fs)
68991336b40SDon Lewis {
69091336b40SDon Lewis 	struct dn_sch_inst *si;
69191336b40SDon Lewis 
69291336b40SDon Lewis 	/* clean up AQM status for all queues for !DN_MULTIQUEUE sched*/
69391336b40SDon Lewis 	if (fs->fs.fs_nr > DN_MAX_ID) {
69491336b40SDon Lewis 		if (fs->sched && !(fs->sched->fp->flags & DN_MULTIQUEUE)) {
69591336b40SDon Lewis 			if (fs->sched->sch.flags & DN_HAVE_MASK)
69691336b40SDon Lewis 				dn_ht_scan(fs->sched->siht, si_cleanup_q, NULL);
69791336b40SDon Lewis 			else {
69891336b40SDon Lewis 					/* single si i.e. no sched mask */
69991336b40SDon Lewis 					si = (struct dn_sch_inst *) fs->sched->siht;
70091336b40SDon Lewis 					if (si && fs->aqmfp && fs->aqmfp->cleanup)
70191336b40SDon Lewis 						fs->aqmfp->cleanup((struct dn_queue *) (si+1));
70291336b40SDon Lewis 			}
70391336b40SDon Lewis 		}
70491336b40SDon Lewis 	}
70591336b40SDon Lewis 
70691336b40SDon Lewis 	/* clean up AQM status for all queues for DN_MULTIQUEUE sched*/
70791336b40SDon Lewis 	if (fs->sched && fs->sched->fp->flags & DN_MULTIQUEUE && fs->qht) {
70891336b40SDon Lewis 			if (fs->fs.flags & DN_QHT_HASH)
70991336b40SDon Lewis 				dn_ht_scan(fs->qht, q_cleanup_q, NULL);
71091336b40SDon Lewis 			else
71191336b40SDon Lewis 				fs->aqmfp->cleanup((struct dn_queue *)(fs->qht));
71291336b40SDon Lewis 	}
71391336b40SDon Lewis 
71491336b40SDon Lewis 	/* deconfig AQM */
71591336b40SDon Lewis 	if(fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig)
71691336b40SDon Lewis 		fs->aqmfp->deconfig(fs);
71791336b40SDon Lewis }
71891336b40SDon Lewis #endif
71991336b40SDon Lewis 
7203b3a8eb9SGleb Smirnoff /*
7213b3a8eb9SGleb Smirnoff  * detach flowset from its current scheduler. Flags as follows:
7223b3a8eb9SGleb Smirnoff  * DN_DETACH removes from the fsk_list
7233b3a8eb9SGleb Smirnoff  * DN_DESTROY deletes individual queues
7243b3a8eb9SGleb Smirnoff  * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
7253b3a8eb9SGleb Smirnoff  */
7263b3a8eb9SGleb Smirnoff static void
7273b3a8eb9SGleb Smirnoff fsk_detach(struct dn_fsk *fs, int flags)
7283b3a8eb9SGleb Smirnoff {
7293b3a8eb9SGleb Smirnoff 	if (flags & DN_DELETE_FS)
7303b3a8eb9SGleb Smirnoff 		flags |= DN_DESTROY;
7313b3a8eb9SGleb Smirnoff 	ND("fs %d from sched %d flags %s %s %s",
7323b3a8eb9SGleb Smirnoff 		fs->fs.fs_nr, fs->fs.sched_nr,
7333b3a8eb9SGleb Smirnoff 		(flags & DN_DELETE_FS) ? "DEL_FS":"",
7343b3a8eb9SGleb Smirnoff 		(flags & DN_DESTROY) ? "DEL":"",
7353b3a8eb9SGleb Smirnoff 		(flags & DN_DETACH) ? "DET":"");
7363b3a8eb9SGleb Smirnoff 	if (flags & DN_DETACH) { /* detach from the list */
7373b3a8eb9SGleb Smirnoff 		struct dn_fsk_head *h;
7383b3a8eb9SGleb Smirnoff 		h = fs->sched ? &fs->sched->fsk_list : &dn_cfg.fsu;
7393b3a8eb9SGleb Smirnoff 		SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
7403b3a8eb9SGleb Smirnoff 	}
7413b3a8eb9SGleb Smirnoff 	/* Free the RED parameters, they will be recomputed on
7423b3a8eb9SGleb Smirnoff 	 * subsequent attach if needed.
7433b3a8eb9SGleb Smirnoff 	 */
7443b3a8eb9SGleb Smirnoff 	if (fs->w_q_lookup)
7453b3a8eb9SGleb Smirnoff 		free(fs->w_q_lookup, M_DUMMYNET);
7463b3a8eb9SGleb Smirnoff 	fs->w_q_lookup = NULL;
7473b3a8eb9SGleb Smirnoff 	qht_delete(fs, flags);
74891336b40SDon Lewis #ifdef NEW_AQM
74991336b40SDon Lewis 	aqm_cleanup_deconfig_fs(fs);
75091336b40SDon Lewis #endif
75191336b40SDon Lewis 
7523b3a8eb9SGleb Smirnoff 	if (fs->sched && fs->sched->fp->free_fsk)
7533b3a8eb9SGleb Smirnoff 		fs->sched->fp->free_fsk(fs);
7543b3a8eb9SGleb Smirnoff 	fs->sched = NULL;
7553b3a8eb9SGleb Smirnoff 	if (flags & DN_DELETE_FS) {
756578acad3SEitan Adler 		bzero(fs, sizeof(*fs));	/* safety */
7573b3a8eb9SGleb Smirnoff 		free(fs, M_DUMMYNET);
7583b3a8eb9SGleb Smirnoff 		dn_cfg.fsk_count--;
7593b3a8eb9SGleb Smirnoff 	} else {
7603b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain);
7613b3a8eb9SGleb Smirnoff 	}
7623b3a8eb9SGleb Smirnoff }
7633b3a8eb9SGleb Smirnoff 
7643b3a8eb9SGleb Smirnoff /*
7653b3a8eb9SGleb Smirnoff  * Detach or destroy all flowsets in a list.
7663b3a8eb9SGleb Smirnoff  * flags specifies what to do:
7673b3a8eb9SGleb Smirnoff  * DN_DESTROY:	flush all queues
7683b3a8eb9SGleb Smirnoff  * DN_DELETE_FS:	DN_DESTROY + destroy flowset
7693b3a8eb9SGleb Smirnoff  *	DN_DELETE_FS implies DN_DESTROY
7703b3a8eb9SGleb Smirnoff  */
7713b3a8eb9SGleb Smirnoff static void
7723b3a8eb9SGleb Smirnoff fsk_detach_list(struct dn_fsk_head *h, int flags)
7733b3a8eb9SGleb Smirnoff {
7743b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
7753b3a8eb9SGleb Smirnoff 	int n = 0; /* only for stats */
7763b3a8eb9SGleb Smirnoff 
7773b3a8eb9SGleb Smirnoff 	ND("head %p flags %x", h, flags);
7783b3a8eb9SGleb Smirnoff 	while ((fs = SLIST_FIRST(h))) {
7793b3a8eb9SGleb Smirnoff 		SLIST_REMOVE_HEAD(h, sch_chain);
7803b3a8eb9SGleb Smirnoff 		n++;
7813b3a8eb9SGleb Smirnoff 		fsk_detach(fs, flags);
7823b3a8eb9SGleb Smirnoff 	}
7833b3a8eb9SGleb Smirnoff 	ND("done %d flowsets", n);
7843b3a8eb9SGleb Smirnoff }
7853b3a8eb9SGleb Smirnoff 
7863b3a8eb9SGleb Smirnoff /*
7873b3a8eb9SGleb Smirnoff  * called on 'queue X delete' -- removes the flowset from fshash,
7883b3a8eb9SGleb Smirnoff  * deletes all queues for the flowset, and removes the flowset.
7893b3a8eb9SGleb Smirnoff  */
7903b3a8eb9SGleb Smirnoff static int
7913b3a8eb9SGleb Smirnoff delete_fs(int i, int locked)
7923b3a8eb9SGleb Smirnoff {
7933b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
7943b3a8eb9SGleb Smirnoff 	int err = 0;
7953b3a8eb9SGleb Smirnoff 
7963b3a8eb9SGleb Smirnoff 	if (!locked)
7973b3a8eb9SGleb Smirnoff 		DN_BH_WLOCK();
7983b3a8eb9SGleb Smirnoff 	fs = dn_ht_find(dn_cfg.fshash, i, DNHT_REMOVE, NULL);
7993b3a8eb9SGleb Smirnoff 	ND("fs %d found %p", i, fs);
8003b3a8eb9SGleb Smirnoff 	if (fs) {
8013b3a8eb9SGleb Smirnoff 		fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
8023b3a8eb9SGleb Smirnoff 		err = 0;
8033b3a8eb9SGleb Smirnoff 	} else
8043b3a8eb9SGleb Smirnoff 		err = EINVAL;
8053b3a8eb9SGleb Smirnoff 	if (!locked)
8063b3a8eb9SGleb Smirnoff 		DN_BH_WUNLOCK();
8073b3a8eb9SGleb Smirnoff 	return err;
8083b3a8eb9SGleb Smirnoff }
8093b3a8eb9SGleb Smirnoff 
8103b3a8eb9SGleb Smirnoff /*----- end of flowset hashtable support -------------*/
8113b3a8eb9SGleb Smirnoff 
8123b3a8eb9SGleb Smirnoff /*------------------------------------------------------------
8133b3a8eb9SGleb Smirnoff  * Scheduler hash. When searching by index we pass sched_nr,
8143b3a8eb9SGleb Smirnoff  * otherwise we pass struct dn_sch * which is the first field in
8153b3a8eb9SGleb Smirnoff  * struct dn_schk so we can cast between the two. We use this trick
8163b3a8eb9SGleb Smirnoff  * because in the create phase (but it should be fixed).
8173b3a8eb9SGleb Smirnoff  */
8183b3a8eb9SGleb Smirnoff static uint32_t
8193b3a8eb9SGleb Smirnoff schk_hash(uintptr_t key, int flags, void *_arg)
8203b3a8eb9SGleb Smirnoff {
8213b3a8eb9SGleb Smirnoff 	uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
8223b3a8eb9SGleb Smirnoff 		((struct dn_schk *)key)->sch.sched_nr;
8233b3a8eb9SGleb Smirnoff 	return ( (i>>8)^(i>>4)^i );
8243b3a8eb9SGleb Smirnoff }
8253b3a8eb9SGleb Smirnoff 
8263b3a8eb9SGleb Smirnoff static int
8273b3a8eb9SGleb Smirnoff schk_match(void *obj, uintptr_t key, int flags, void *_arg)
8283b3a8eb9SGleb Smirnoff {
8293b3a8eb9SGleb Smirnoff 	struct dn_schk *s = (struct dn_schk *)obj;
8303b3a8eb9SGleb Smirnoff 	int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
8313b3a8eb9SGleb Smirnoff 		((struct dn_schk *)key)->sch.sched_nr;
8323b3a8eb9SGleb Smirnoff 	return (s->sch.sched_nr == i);
8333b3a8eb9SGleb Smirnoff }
8343b3a8eb9SGleb Smirnoff 
8353b3a8eb9SGleb Smirnoff /*
8363b3a8eb9SGleb Smirnoff  * Create the entry and intialize with the sched hash if needed.
8373b3a8eb9SGleb Smirnoff  * Leave s->fp unset so we can tell whether a dn_ht_find() returns
8383b3a8eb9SGleb Smirnoff  * a new object or a previously existing one.
8393b3a8eb9SGleb Smirnoff  */
8403b3a8eb9SGleb Smirnoff static void *
8413b3a8eb9SGleb Smirnoff schk_new(uintptr_t key, int flags, void *arg)
8423b3a8eb9SGleb Smirnoff {
8433b3a8eb9SGleb Smirnoff 	struct schk_new_arg *a = arg;
8443b3a8eb9SGleb Smirnoff 	struct dn_schk *s;
8453b3a8eb9SGleb Smirnoff 	int l = sizeof(*s) +a->fp->schk_datalen;
8463b3a8eb9SGleb Smirnoff 
8473b3a8eb9SGleb Smirnoff 	s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
8483b3a8eb9SGleb Smirnoff 	if (s == NULL)
8493b3a8eb9SGleb Smirnoff 		return NULL;
8503b3a8eb9SGleb Smirnoff 	set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
8513b3a8eb9SGleb Smirnoff 	s->sch = *a->sch; // copy initial values
8523b3a8eb9SGleb Smirnoff 	s->link.link_nr = s->sch.sched_nr;
8533b3a8eb9SGleb Smirnoff 	SLIST_INIT(&s->fsk_list);
8543b3a8eb9SGleb Smirnoff 	/* initialize the hash table or create the single instance */
8553b3a8eb9SGleb Smirnoff 	s->fp = a->fp;	/* si_new needs this */
8563b3a8eb9SGleb Smirnoff 	s->drain_bucket = 0;
8573b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK) {
8583b3a8eb9SGleb Smirnoff 		s->siht = dn_ht_init(NULL, s->sch.buckets,
8593b3a8eb9SGleb Smirnoff 			offsetof(struct dn_sch_inst, si_next),
8603b3a8eb9SGleb Smirnoff 			si_hash, si_match, si_new);
8613b3a8eb9SGleb Smirnoff 		if (s->siht == NULL) {
8623b3a8eb9SGleb Smirnoff 			free(s, M_DUMMYNET);
8633b3a8eb9SGleb Smirnoff 			return NULL;
8643b3a8eb9SGleb Smirnoff 		}
8653b3a8eb9SGleb Smirnoff 	}
8663b3a8eb9SGleb Smirnoff 	s->fp = NULL;	/* mark as a new scheduler */
8673b3a8eb9SGleb Smirnoff 	dn_cfg.schk_count++;
8683b3a8eb9SGleb Smirnoff 	return s;
8693b3a8eb9SGleb Smirnoff }
8703b3a8eb9SGleb Smirnoff 
8713b3a8eb9SGleb Smirnoff /*
8723b3a8eb9SGleb Smirnoff  * Callback for sched delete. Notify all attached flowsets to
8733b3a8eb9SGleb Smirnoff  * detach from the scheduler, destroy the internal flowset, and
8743b3a8eb9SGleb Smirnoff  * all instances. The scheduler goes away too.
8753b3a8eb9SGleb Smirnoff  * arg is 0 (only detach flowsets and destroy instances)
8763b3a8eb9SGleb Smirnoff  * DN_DESTROY (detach & delete queues, delete schk)
8773b3a8eb9SGleb Smirnoff  * or DN_DELETE_FS (delete queues and flowsets, delete schk)
8783b3a8eb9SGleb Smirnoff  */
8793b3a8eb9SGleb Smirnoff static int
8803b3a8eb9SGleb Smirnoff schk_delete_cb(void *obj, void *arg)
8813b3a8eb9SGleb Smirnoff {
8823b3a8eb9SGleb Smirnoff 	struct dn_schk *s = obj;
8833b3a8eb9SGleb Smirnoff #if 0
8843b3a8eb9SGleb Smirnoff 	int a = (int)arg;
8853b3a8eb9SGleb Smirnoff 	ND("sched %d arg %s%s",
8863b3a8eb9SGleb Smirnoff 		s->sch.sched_nr,
8873b3a8eb9SGleb Smirnoff 		a&DN_DESTROY ? "DEL ":"",
8883b3a8eb9SGleb Smirnoff 		a&DN_DELETE_FS ? "DEL_FS":"");
8893b3a8eb9SGleb Smirnoff #endif
8903b3a8eb9SGleb Smirnoff 	fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
8913b3a8eb9SGleb Smirnoff 	/* no more flowset pointing to us now */
8923b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK) {
8933b3a8eb9SGleb Smirnoff 		dn_ht_scan(s->siht, si_destroy, NULL);
8943b3a8eb9SGleb Smirnoff 		dn_ht_free(s->siht, 0);
8953b3a8eb9SGleb Smirnoff 	} else if (s->siht)
8963b3a8eb9SGleb Smirnoff 		si_destroy(s->siht, NULL);
8973b3a8eb9SGleb Smirnoff 	if (s->profile) {
8983b3a8eb9SGleb Smirnoff 		free(s->profile, M_DUMMYNET);
8993b3a8eb9SGleb Smirnoff 		s->profile = NULL;
9003b3a8eb9SGleb Smirnoff 	}
9013b3a8eb9SGleb Smirnoff 	s->siht = NULL;
9023b3a8eb9SGleb Smirnoff 	if (s->fp->destroy)
9033b3a8eb9SGleb Smirnoff 		s->fp->destroy(s);
9043b3a8eb9SGleb Smirnoff 	bzero(s, sizeof(*s));	// safety
9053b3a8eb9SGleb Smirnoff 	free(obj, M_DUMMYNET);
9063b3a8eb9SGleb Smirnoff 	dn_cfg.schk_count--;
9073b3a8eb9SGleb Smirnoff 	return DNHT_SCAN_DEL;
9083b3a8eb9SGleb Smirnoff }
9093b3a8eb9SGleb Smirnoff 
9103b3a8eb9SGleb Smirnoff /*
9113b3a8eb9SGleb Smirnoff  * called on a 'sched X delete' command. Deletes a single scheduler.
9123b3a8eb9SGleb Smirnoff  * This is done by removing from the schedhash, unlinking all
9133b3a8eb9SGleb Smirnoff  * flowsets and deleting their traffic.
9143b3a8eb9SGleb Smirnoff  */
9153b3a8eb9SGleb Smirnoff static int
9163b3a8eb9SGleb Smirnoff delete_schk(int i)
9173b3a8eb9SGleb Smirnoff {
9183b3a8eb9SGleb Smirnoff 	struct dn_schk *s;
9193b3a8eb9SGleb Smirnoff 
9203b3a8eb9SGleb Smirnoff 	s = dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
9213b3a8eb9SGleb Smirnoff 	ND("%d %p", i, s);
9223b3a8eb9SGleb Smirnoff 	if (!s)
9233b3a8eb9SGleb Smirnoff 		return EINVAL;
9243b3a8eb9SGleb Smirnoff 	delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
9253b3a8eb9SGleb Smirnoff 	/* then detach flowsets, delete traffic */
9263b3a8eb9SGleb Smirnoff 	schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
9273b3a8eb9SGleb Smirnoff 	return 0;
9283b3a8eb9SGleb Smirnoff }
9293b3a8eb9SGleb Smirnoff /*--- end of schk hashtable support ---*/
9303b3a8eb9SGleb Smirnoff 
9313b3a8eb9SGleb Smirnoff static int
9323b3a8eb9SGleb Smirnoff copy_obj(char **start, char *end, void *_o, const char *msg, int i)
9333b3a8eb9SGleb Smirnoff {
934*0ac43d97SMarius Strobl 	struct dn_id o;
935*0ac43d97SMarius Strobl 	union {
936*0ac43d97SMarius Strobl 		struct dn_link l;
937*0ac43d97SMarius Strobl 		struct dn_schk s;
938*0ac43d97SMarius Strobl 	} dn;
9393b3a8eb9SGleb Smirnoff 	int have = end - *start;
9403b3a8eb9SGleb Smirnoff 
941*0ac43d97SMarius Strobl 	memcpy(&o, _o, sizeof(o));
942*0ac43d97SMarius Strobl 	if (have < o.len || o.len == 0 || o.type == 0) {
9433b3a8eb9SGleb Smirnoff 		D("(WARN) type %d %s %d have %d need %d",
944*0ac43d97SMarius Strobl 		    o.type, msg, i, have, o.len);
9453b3a8eb9SGleb Smirnoff 		return 1;
9463b3a8eb9SGleb Smirnoff 	}
947*0ac43d97SMarius Strobl 	ND("type %d %s %d len %d", o.type, msg, i, o.len);
948*0ac43d97SMarius Strobl 	if (o.type == DN_LINK) {
949*0ac43d97SMarius Strobl 		memcpy(&dn.l, _o, sizeof(dn.l));
9503b3a8eb9SGleb Smirnoff 		/* Adjust burst parameter for link */
951*0ac43d97SMarius Strobl 		dn.l.burst = div64(dn.l.burst, 8 * hz);
952*0ac43d97SMarius Strobl 		dn.l.delay = dn.l.delay * 1000 / hz;
953*0ac43d97SMarius Strobl 		memcpy(*start, &dn.l, sizeof(dn.l));
954*0ac43d97SMarius Strobl 	} else if (o.type == DN_SCH) {
955*0ac43d97SMarius Strobl 		/* Set dn.s.sch.oid.id to the number of instances */
956*0ac43d97SMarius Strobl 		memcpy(&dn.s, _o, sizeof(dn.s));
957*0ac43d97SMarius Strobl 		dn.s.sch.oid.id = (dn.s.sch.flags & DN_HAVE_MASK) ?
958*0ac43d97SMarius Strobl 		    dn_ht_entries(dn.s.siht) : (dn.s.siht ? 1 : 0);
959*0ac43d97SMarius Strobl 		memcpy(*start, &dn.s, sizeof(dn.s));
960*0ac43d97SMarius Strobl 	} else
961*0ac43d97SMarius Strobl 		memcpy(*start, _o, o.len);
962*0ac43d97SMarius Strobl 	*start += o.len;
9633b3a8eb9SGleb Smirnoff 	return 0;
9643b3a8eb9SGleb Smirnoff }
9653b3a8eb9SGleb Smirnoff 
9663b3a8eb9SGleb Smirnoff /* Specific function to copy a queue.
9673b3a8eb9SGleb Smirnoff  * Copies only the user-visible part of a queue (which is in
9683b3a8eb9SGleb Smirnoff  * a struct dn_flow), and sets len accordingly.
9693b3a8eb9SGleb Smirnoff  */
9703b3a8eb9SGleb Smirnoff static int
9713b3a8eb9SGleb Smirnoff copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
9723b3a8eb9SGleb Smirnoff {
9733b3a8eb9SGleb Smirnoff 	struct dn_id *o = _o;
9743b3a8eb9SGleb Smirnoff 	int have = end - *start;
9753b3a8eb9SGleb Smirnoff 	int len = sizeof(struct dn_flow); /* see above comment */
9763b3a8eb9SGleb Smirnoff 
9773b3a8eb9SGleb Smirnoff 	if (have < len || o->len == 0 || o->type != DN_QUEUE) {
9783b3a8eb9SGleb Smirnoff 		D("ERROR type %d %s %d have %d need %d",
9793b3a8eb9SGleb Smirnoff 			o->type, msg, i, have, len);
9803b3a8eb9SGleb Smirnoff 		return 1;
9813b3a8eb9SGleb Smirnoff 	}
9823b3a8eb9SGleb Smirnoff 	ND("type %d %s %d len %d", o->type, msg, i, len);
983*0ac43d97SMarius Strobl 	memcpy(*start, _o, len);
9843b3a8eb9SGleb Smirnoff 	((struct dn_id*)(*start))->len = len;
9853b3a8eb9SGleb Smirnoff 	*start += len;
9863b3a8eb9SGleb Smirnoff 	return 0;
9873b3a8eb9SGleb Smirnoff }
9883b3a8eb9SGleb Smirnoff 
9893b3a8eb9SGleb Smirnoff static int
9903b3a8eb9SGleb Smirnoff copy_q_cb(void *obj, void *arg)
9913b3a8eb9SGleb Smirnoff {
9923b3a8eb9SGleb Smirnoff 	struct dn_queue *q = obj;
9933b3a8eb9SGleb Smirnoff 	struct copy_args *a = arg;
9943b3a8eb9SGleb Smirnoff 	struct dn_flow *ni = (struct dn_flow *)(*a->start);
9953b3a8eb9SGleb Smirnoff         if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
9963b3a8eb9SGleb Smirnoff                 return DNHT_SCAN_END;
9973b3a8eb9SGleb Smirnoff         ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
9983b3a8eb9SGleb Smirnoff         ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
9993b3a8eb9SGleb Smirnoff         return 0;
10003b3a8eb9SGleb Smirnoff }
10013b3a8eb9SGleb Smirnoff 
10023b3a8eb9SGleb Smirnoff static int
10033b3a8eb9SGleb Smirnoff copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
10043b3a8eb9SGleb Smirnoff {
10053b3a8eb9SGleb Smirnoff 	if (!fs->qht)
10063b3a8eb9SGleb Smirnoff 		return 0;
10073b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QHT_HASH)
10083b3a8eb9SGleb Smirnoff 		dn_ht_scan(fs->qht, copy_q_cb, a);
10093b3a8eb9SGleb Smirnoff 	else
10103b3a8eb9SGleb Smirnoff 		copy_q_cb(fs->qht, a);
10113b3a8eb9SGleb Smirnoff 	return 0;
10123b3a8eb9SGleb Smirnoff }
10133b3a8eb9SGleb Smirnoff 
10143b3a8eb9SGleb Smirnoff /*
10153b3a8eb9SGleb Smirnoff  * This routine only copies the initial part of a profile ? XXX
10163b3a8eb9SGleb Smirnoff  */
10173b3a8eb9SGleb Smirnoff static int
10183b3a8eb9SGleb Smirnoff copy_profile(struct copy_args *a, struct dn_profile *p)
10193b3a8eb9SGleb Smirnoff {
10203b3a8eb9SGleb Smirnoff 	int have = a->end - *a->start;
10213b3a8eb9SGleb Smirnoff 	/* XXX here we check for max length */
10223b3a8eb9SGleb Smirnoff 	int profile_len = sizeof(struct dn_profile) -
10233b3a8eb9SGleb Smirnoff 		ED_MAX_SAMPLES_NO*sizeof(int);
10243b3a8eb9SGleb Smirnoff 
10253b3a8eb9SGleb Smirnoff 	if (p == NULL)
10263b3a8eb9SGleb Smirnoff 		return 0;
10273b3a8eb9SGleb Smirnoff 	if (have < profile_len) {
10283b3a8eb9SGleb Smirnoff 		D("error have %d need %d", have, profile_len);
10293b3a8eb9SGleb Smirnoff 		return 1;
10303b3a8eb9SGleb Smirnoff 	}
1031*0ac43d97SMarius Strobl 	memcpy(*a->start, p, profile_len);
10323b3a8eb9SGleb Smirnoff 	((struct dn_id *)(*a->start))->len = profile_len;
10333b3a8eb9SGleb Smirnoff 	*a->start += profile_len;
10343b3a8eb9SGleb Smirnoff 	return 0;
10353b3a8eb9SGleb Smirnoff }
10363b3a8eb9SGleb Smirnoff 
10373b3a8eb9SGleb Smirnoff static int
10383b3a8eb9SGleb Smirnoff copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
10393b3a8eb9SGleb Smirnoff {
10403b3a8eb9SGleb Smirnoff 	struct dn_fs *ufs = (struct dn_fs *)(*a->start);
10413b3a8eb9SGleb Smirnoff 	if (!fs)
10423b3a8eb9SGleb Smirnoff 		return 0;
10433b3a8eb9SGleb Smirnoff 	ND("flowset %d", fs->fs.fs_nr);
10443b3a8eb9SGleb Smirnoff 	if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
10453b3a8eb9SGleb Smirnoff 		return DNHT_SCAN_END;
10463b3a8eb9SGleb Smirnoff 	ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
10473b3a8eb9SGleb Smirnoff 		dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
10483b3a8eb9SGleb Smirnoff 	if (flags) {	/* copy queues */
10493b3a8eb9SGleb Smirnoff 		copy_q(a, fs, 0);
10503b3a8eb9SGleb Smirnoff 	}
10513b3a8eb9SGleb Smirnoff 	return 0;
10523b3a8eb9SGleb Smirnoff }
10533b3a8eb9SGleb Smirnoff 
10543b3a8eb9SGleb Smirnoff static int
10553b3a8eb9SGleb Smirnoff copy_si_cb(void *obj, void *arg)
10563b3a8eb9SGleb Smirnoff {
10573b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si = obj;
10583b3a8eb9SGleb Smirnoff 	struct copy_args *a = arg;
10593b3a8eb9SGleb Smirnoff 	struct dn_flow *ni = (struct dn_flow *)(*a->start);
10603b3a8eb9SGleb Smirnoff 	if (copy_obj(a->start, a->end, &si->ni, "inst",
10613b3a8eb9SGleb Smirnoff 			si->sched->sch.sched_nr))
10623b3a8eb9SGleb Smirnoff 		return DNHT_SCAN_END;
10633b3a8eb9SGleb Smirnoff 	ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
10643b3a8eb9SGleb Smirnoff 	ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
10653b3a8eb9SGleb Smirnoff 	return 0;
10663b3a8eb9SGleb Smirnoff }
10673b3a8eb9SGleb Smirnoff 
10683b3a8eb9SGleb Smirnoff static int
10693b3a8eb9SGleb Smirnoff copy_si(struct copy_args *a, struct dn_schk *s, int flags)
10703b3a8eb9SGleb Smirnoff {
10713b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK)
10723b3a8eb9SGleb Smirnoff 		dn_ht_scan(s->siht, copy_si_cb, a);
10733b3a8eb9SGleb Smirnoff 	else if (s->siht)
10743b3a8eb9SGleb Smirnoff 		copy_si_cb(s->siht, a);
10753b3a8eb9SGleb Smirnoff 	return 0;
10763b3a8eb9SGleb Smirnoff }
10773b3a8eb9SGleb Smirnoff 
10783b3a8eb9SGleb Smirnoff /*
10793b3a8eb9SGleb Smirnoff  * compute a list of children of a scheduler and copy up
10803b3a8eb9SGleb Smirnoff  */
10813b3a8eb9SGleb Smirnoff static int
10823b3a8eb9SGleb Smirnoff copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
10833b3a8eb9SGleb Smirnoff {
10843b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
10853b3a8eb9SGleb Smirnoff 	struct dn_id *o;
10863b3a8eb9SGleb Smirnoff 	uint32_t *p;
10873b3a8eb9SGleb Smirnoff 
10883b3a8eb9SGleb Smirnoff 	int n = 0, space = sizeof(*o);
10893b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
10903b3a8eb9SGleb Smirnoff 		if (fs->fs.fs_nr < DN_MAX_ID)
10913b3a8eb9SGleb Smirnoff 			n++;
10923b3a8eb9SGleb Smirnoff 	}
10933b3a8eb9SGleb Smirnoff 	space += n * sizeof(uint32_t);
10943b3a8eb9SGleb Smirnoff 	DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
10953b3a8eb9SGleb Smirnoff 	if (a->end - *(a->start) < space)
10963b3a8eb9SGleb Smirnoff 		return DNHT_SCAN_END;
10973b3a8eb9SGleb Smirnoff 	o = (struct dn_id *)(*(a->start));
10983b3a8eb9SGleb Smirnoff 	o->len = space;
10993b3a8eb9SGleb Smirnoff 	*a->start += o->len;
11003b3a8eb9SGleb Smirnoff 	o->type = DN_TEXT;
11013b3a8eb9SGleb Smirnoff 	p = (uint32_t *)(o+1);
11023b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
11033b3a8eb9SGleb Smirnoff 		if (fs->fs.fs_nr < DN_MAX_ID)
11043b3a8eb9SGleb Smirnoff 			*p++ = fs->fs.fs_nr;
11053b3a8eb9SGleb Smirnoff 	return 0;
11063b3a8eb9SGleb Smirnoff }
11073b3a8eb9SGleb Smirnoff 
11083b3a8eb9SGleb Smirnoff static int
11093b3a8eb9SGleb Smirnoff copy_data_helper(void *_o, void *_arg)
11103b3a8eb9SGleb Smirnoff {
11113b3a8eb9SGleb Smirnoff 	struct copy_args *a = _arg;
11123b3a8eb9SGleb Smirnoff 	uint32_t *r = a->extra->r; /* start of first range */
11133b3a8eb9SGleb Smirnoff 	uint32_t *lim;	/* first invalid pointer */
11143b3a8eb9SGleb Smirnoff 	int n;
11153b3a8eb9SGleb Smirnoff 
11163b3a8eb9SGleb Smirnoff 	lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
11173b3a8eb9SGleb Smirnoff 
11183b3a8eb9SGleb Smirnoff 	if (a->type == DN_LINK || a->type == DN_SCH) {
11193b3a8eb9SGleb Smirnoff 		/* pipe|sched show, we receive a dn_schk */
11203b3a8eb9SGleb Smirnoff 		struct dn_schk *s = _o;
11213b3a8eb9SGleb Smirnoff 
11223b3a8eb9SGleb Smirnoff 		n = s->sch.sched_nr;
11233b3a8eb9SGleb Smirnoff 		if (a->type == DN_SCH && n >= DN_MAX_ID)
11243b3a8eb9SGleb Smirnoff 			return 0;	/* not a scheduler */
11253b3a8eb9SGleb Smirnoff 		if (a->type == DN_LINK && n <= DN_MAX_ID)
11263b3a8eb9SGleb Smirnoff 		    return 0;	/* not a pipe */
11273b3a8eb9SGleb Smirnoff 
11283b3a8eb9SGleb Smirnoff 		/* see if the object is within one of our ranges */
11293b3a8eb9SGleb Smirnoff 		for (;r < lim; r += 2) {
11303b3a8eb9SGleb Smirnoff 			if (n < r[0] || n > r[1])
11313b3a8eb9SGleb Smirnoff 				continue;
11323b3a8eb9SGleb Smirnoff 			/* Found a valid entry, copy and we are done */
11333b3a8eb9SGleb Smirnoff 			if (a->flags & DN_C_LINK) {
11343b3a8eb9SGleb Smirnoff 				if (copy_obj(a->start, a->end,
11353b3a8eb9SGleb Smirnoff 				    &s->link, "link", n))
11363b3a8eb9SGleb Smirnoff 					return DNHT_SCAN_END;
11373b3a8eb9SGleb Smirnoff 				if (copy_profile(a, s->profile))
11383b3a8eb9SGleb Smirnoff 					return DNHT_SCAN_END;
11393b3a8eb9SGleb Smirnoff 				if (copy_flowset(a, s->fs, 0))
11403b3a8eb9SGleb Smirnoff 					return DNHT_SCAN_END;
11413b3a8eb9SGleb Smirnoff 			}
11423b3a8eb9SGleb Smirnoff 			if (a->flags & DN_C_SCH) {
11433b3a8eb9SGleb Smirnoff 				if (copy_obj(a->start, a->end,
11443b3a8eb9SGleb Smirnoff 				    &s->sch, "sched", n))
11453b3a8eb9SGleb Smirnoff 					return DNHT_SCAN_END;
11463b3a8eb9SGleb Smirnoff 				/* list all attached flowsets */
11473b3a8eb9SGleb Smirnoff 				if (copy_fsk_list(a, s, 0))
11483b3a8eb9SGleb Smirnoff 					return DNHT_SCAN_END;
11493b3a8eb9SGleb Smirnoff 			}
11503b3a8eb9SGleb Smirnoff 			if (a->flags & DN_C_FLOW)
11513b3a8eb9SGleb Smirnoff 				copy_si(a, s, 0);
11523b3a8eb9SGleb Smirnoff 			break;
11533b3a8eb9SGleb Smirnoff 		}
11543b3a8eb9SGleb Smirnoff 	} else if (a->type == DN_FS) {
11553b3a8eb9SGleb Smirnoff 		/* queue show, skip internal flowsets */
11563b3a8eb9SGleb Smirnoff 		struct dn_fsk *fs = _o;
11573b3a8eb9SGleb Smirnoff 
11583b3a8eb9SGleb Smirnoff 		n = fs->fs.fs_nr;
11593b3a8eb9SGleb Smirnoff 		if (n >= DN_MAX_ID)
11603b3a8eb9SGleb Smirnoff 			return 0;
11613b3a8eb9SGleb Smirnoff 		/* see if the object is within one of our ranges */
11623b3a8eb9SGleb Smirnoff 		for (;r < lim; r += 2) {
11633b3a8eb9SGleb Smirnoff 			if (n < r[0] || n > r[1])
11643b3a8eb9SGleb Smirnoff 				continue;
11653b3a8eb9SGleb Smirnoff 			if (copy_flowset(a, fs, 0))
11663b3a8eb9SGleb Smirnoff 				return DNHT_SCAN_END;
11673b3a8eb9SGleb Smirnoff 			copy_q(a, fs, 0);
11683b3a8eb9SGleb Smirnoff 			break; /* we are done */
11693b3a8eb9SGleb Smirnoff 		}
11703b3a8eb9SGleb Smirnoff 	}
11713b3a8eb9SGleb Smirnoff 	return 0;
11723b3a8eb9SGleb Smirnoff }
11733b3a8eb9SGleb Smirnoff 
11743b3a8eb9SGleb Smirnoff static inline struct dn_schk *
11753b3a8eb9SGleb Smirnoff locate_scheduler(int i)
11763b3a8eb9SGleb Smirnoff {
11773b3a8eb9SGleb Smirnoff 	return dn_ht_find(dn_cfg.schedhash, i, 0, NULL);
11783b3a8eb9SGleb Smirnoff }
11793b3a8eb9SGleb Smirnoff 
11803b3a8eb9SGleb Smirnoff /*
11813b3a8eb9SGleb Smirnoff  * red parameters are in fixed point arithmetic.
11823b3a8eb9SGleb Smirnoff  */
11833b3a8eb9SGleb Smirnoff static int
11843b3a8eb9SGleb Smirnoff config_red(struct dn_fsk *fs)
11853b3a8eb9SGleb Smirnoff {
11863b3a8eb9SGleb Smirnoff 	int64_t s, idle, weight, w0;
11873b3a8eb9SGleb Smirnoff 	int t, i;
11883b3a8eb9SGleb Smirnoff 
11893b3a8eb9SGleb Smirnoff 	fs->w_q = fs->fs.w_q;
11903b3a8eb9SGleb Smirnoff 	fs->max_p = fs->fs.max_p;
11913b3a8eb9SGleb Smirnoff 	ND("called");
11923b3a8eb9SGleb Smirnoff 	/* Doing stuff that was in userland */
11933b3a8eb9SGleb Smirnoff 	i = fs->sched->link.bandwidth;
11943b3a8eb9SGleb Smirnoff 	s = (i <= 0) ? 0 :
11953b3a8eb9SGleb Smirnoff 		hz * dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
11963b3a8eb9SGleb Smirnoff 
11973b3a8eb9SGleb Smirnoff 	idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
11983b3a8eb9SGleb Smirnoff 	fs->lookup_step = div64(idle , dn_cfg.red_lookup_depth);
11993b3a8eb9SGleb Smirnoff 	/* fs->lookup_step not scaled, */
12003b3a8eb9SGleb Smirnoff 	if (!fs->lookup_step)
12013b3a8eb9SGleb Smirnoff 		fs->lookup_step = 1;
12023b3a8eb9SGleb Smirnoff 	w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
12033b3a8eb9SGleb Smirnoff 
12043b3a8eb9SGleb Smirnoff 	for (t = fs->lookup_step; t > 1; --t)
12053b3a8eb9SGleb Smirnoff 		weight = SCALE_MUL(weight, w0);
12063b3a8eb9SGleb Smirnoff 	fs->lookup_weight = (int)(weight); // scaled
12073b3a8eb9SGleb Smirnoff 
12083b3a8eb9SGleb Smirnoff 	/* Now doing stuff that was in kerneland */
12093b3a8eb9SGleb Smirnoff 	fs->min_th = SCALE(fs->fs.min_th);
12103b3a8eb9SGleb Smirnoff 	fs->max_th = SCALE(fs->fs.max_th);
12113b3a8eb9SGleb Smirnoff 
1212fc5e1956SHiren Panchasara 	if (fs->fs.max_th == fs->fs.min_th)
1213fc5e1956SHiren Panchasara 		fs->c_1 = fs->max_p;
1214fc5e1956SHiren Panchasara 	else
1215fc5e1956SHiren Panchasara 		fs->c_1 = SCALE((int64_t)(fs->max_p)) / (fs->fs.max_th - fs->fs.min_th);
12163b3a8eb9SGleb Smirnoff 	fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
12173b3a8eb9SGleb Smirnoff 
12183b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_IS_GENTLE_RED) {
12193b3a8eb9SGleb Smirnoff 		fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
12203b3a8eb9SGleb Smirnoff 		fs->c_4 = SCALE(1) - 2 * fs->max_p;
12213b3a8eb9SGleb Smirnoff 	}
12223b3a8eb9SGleb Smirnoff 
12233b3a8eb9SGleb Smirnoff 	/* If the lookup table already exist, free and create it again. */
12243b3a8eb9SGleb Smirnoff 	if (fs->w_q_lookup) {
12253b3a8eb9SGleb Smirnoff 		free(fs->w_q_lookup, M_DUMMYNET);
12263b3a8eb9SGleb Smirnoff 		fs->w_q_lookup = NULL;
12273b3a8eb9SGleb Smirnoff 	}
12283b3a8eb9SGleb Smirnoff 	if (dn_cfg.red_lookup_depth == 0) {
12293b3a8eb9SGleb Smirnoff 		printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
12303b3a8eb9SGleb Smirnoff 		    "must be > 0\n");
12313b3a8eb9SGleb Smirnoff 		fs->fs.flags &= ~DN_IS_RED;
12323b3a8eb9SGleb Smirnoff 		fs->fs.flags &= ~DN_IS_GENTLE_RED;
12333b3a8eb9SGleb Smirnoff 		return (EINVAL);
12343b3a8eb9SGleb Smirnoff 	}
12353b3a8eb9SGleb Smirnoff 	fs->lookup_depth = dn_cfg.red_lookup_depth;
12363b3a8eb9SGleb Smirnoff 	fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
12373b3a8eb9SGleb Smirnoff 	    M_DUMMYNET, M_NOWAIT);
12383b3a8eb9SGleb Smirnoff 	if (fs->w_q_lookup == NULL) {
12393b3a8eb9SGleb Smirnoff 		printf("dummynet: sorry, cannot allocate red lookup table\n");
12403b3a8eb9SGleb Smirnoff 		fs->fs.flags &= ~DN_IS_RED;
12413b3a8eb9SGleb Smirnoff 		fs->fs.flags &= ~DN_IS_GENTLE_RED;
12423b3a8eb9SGleb Smirnoff 		return(ENOSPC);
12433b3a8eb9SGleb Smirnoff 	}
12443b3a8eb9SGleb Smirnoff 
12453b3a8eb9SGleb Smirnoff 	/* Fill the lookup table with (1 - w_q)^x */
12463b3a8eb9SGleb Smirnoff 	fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
12473b3a8eb9SGleb Smirnoff 
12483b3a8eb9SGleb Smirnoff 	for (i = 1; i < fs->lookup_depth; i++)
12493b3a8eb9SGleb Smirnoff 		fs->w_q_lookup[i] =
12503b3a8eb9SGleb Smirnoff 		    SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
12513b3a8eb9SGleb Smirnoff 
12523b3a8eb9SGleb Smirnoff 	if (dn_cfg.red_avg_pkt_size < 1)
12533b3a8eb9SGleb Smirnoff 		dn_cfg.red_avg_pkt_size = 512;
12543b3a8eb9SGleb Smirnoff 	fs->avg_pkt_size = dn_cfg.red_avg_pkt_size;
12553b3a8eb9SGleb Smirnoff 	if (dn_cfg.red_max_pkt_size < 1)
12563b3a8eb9SGleb Smirnoff 		dn_cfg.red_max_pkt_size = 1500;
12573b3a8eb9SGleb Smirnoff 	fs->max_pkt_size = dn_cfg.red_max_pkt_size;
12583b3a8eb9SGleb Smirnoff 	ND("exit");
12593b3a8eb9SGleb Smirnoff 	return 0;
12603b3a8eb9SGleb Smirnoff }
12613b3a8eb9SGleb Smirnoff 
12623b3a8eb9SGleb Smirnoff /* Scan all flowset attached to this scheduler and update red */
12633b3a8eb9SGleb Smirnoff static void
12643b3a8eb9SGleb Smirnoff update_red(struct dn_schk *s)
12653b3a8eb9SGleb Smirnoff {
12663b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
12673b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
12683b3a8eb9SGleb Smirnoff 		if (fs && (fs->fs.flags & DN_IS_RED))
12693b3a8eb9SGleb Smirnoff 			config_red(fs);
12703b3a8eb9SGleb Smirnoff 	}
12713b3a8eb9SGleb Smirnoff }
12723b3a8eb9SGleb Smirnoff 
12733b3a8eb9SGleb Smirnoff /* attach flowset to scheduler s, possibly requeue */
12743b3a8eb9SGleb Smirnoff static void
12753b3a8eb9SGleb Smirnoff fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
12763b3a8eb9SGleb Smirnoff {
12773b3a8eb9SGleb Smirnoff 	ND("remove fs %d from fsunlinked, link to sched %d",
12783b3a8eb9SGleb Smirnoff 		fs->fs.fs_nr, s->sch.sched_nr);
12793b3a8eb9SGleb Smirnoff 	SLIST_REMOVE(&dn_cfg.fsu, fs, dn_fsk, sch_chain);
12803b3a8eb9SGleb Smirnoff 	fs->sched = s;
12813b3a8eb9SGleb Smirnoff 	SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
12823b3a8eb9SGleb Smirnoff 	if (s->fp->new_fsk)
12833b3a8eb9SGleb Smirnoff 		s->fp->new_fsk(fs);
12843b3a8eb9SGleb Smirnoff 	/* XXX compute fsk_mask */
12853b3a8eb9SGleb Smirnoff 	fs->fsk_mask = fs->fs.flow_mask;
12863b3a8eb9SGleb Smirnoff 	if (fs->sched->sch.flags & DN_HAVE_MASK)
12873b3a8eb9SGleb Smirnoff 		flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
12883b3a8eb9SGleb Smirnoff 	if (fs->qht) {
12893b3a8eb9SGleb Smirnoff 		/*
12903b3a8eb9SGleb Smirnoff 		 * we must drain qht according to the old
12913b3a8eb9SGleb Smirnoff 		 * type, and reinsert according to the new one.
12923b3a8eb9SGleb Smirnoff 		 * The requeue is complex -- in general we need to
12933b3a8eb9SGleb Smirnoff 		 * reclassify every single packet.
12943b3a8eb9SGleb Smirnoff 		 * For the time being, let's hope qht is never set
12953b3a8eb9SGleb Smirnoff 		 * when we reach this point.
12963b3a8eb9SGleb Smirnoff 		 */
12973b3a8eb9SGleb Smirnoff 		D("XXX TODO requeue from fs %d to sch %d",
12983b3a8eb9SGleb Smirnoff 			fs->fs.fs_nr, s->sch.sched_nr);
12993b3a8eb9SGleb Smirnoff 		fs->qht = NULL;
13003b3a8eb9SGleb Smirnoff 	}
13013b3a8eb9SGleb Smirnoff 	/* set the new type for qht */
13023b3a8eb9SGleb Smirnoff 	if (nonzero_mask(&fs->fsk_mask))
13033b3a8eb9SGleb Smirnoff 		fs->fs.flags |= DN_QHT_HASH;
13043b3a8eb9SGleb Smirnoff 	else
13053b3a8eb9SGleb Smirnoff 		fs->fs.flags &= ~DN_QHT_HASH;
13063b3a8eb9SGleb Smirnoff 
13073b3a8eb9SGleb Smirnoff 	/* XXX config_red() can fail... */
13083b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_IS_RED)
13093b3a8eb9SGleb Smirnoff 		config_red(fs);
13103b3a8eb9SGleb Smirnoff }
13113b3a8eb9SGleb Smirnoff 
13123b3a8eb9SGleb Smirnoff /* update all flowsets which may refer to this scheduler */
13133b3a8eb9SGleb Smirnoff static void
13143b3a8eb9SGleb Smirnoff update_fs(struct dn_schk *s)
13153b3a8eb9SGleb Smirnoff {
13163b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs, *tmp;
13173b3a8eb9SGleb Smirnoff 
13183b3a8eb9SGleb Smirnoff 	SLIST_FOREACH_SAFE(fs, &dn_cfg.fsu, sch_chain, tmp) {
13193b3a8eb9SGleb Smirnoff 		if (s->sch.sched_nr != fs->fs.sched_nr) {
13203b3a8eb9SGleb Smirnoff 			D("fs %d for sch %d not %d still unlinked",
13213b3a8eb9SGleb Smirnoff 				fs->fs.fs_nr, fs->fs.sched_nr,
13223b3a8eb9SGleb Smirnoff 				s->sch.sched_nr);
13233b3a8eb9SGleb Smirnoff 			continue;
13243b3a8eb9SGleb Smirnoff 		}
13253b3a8eb9SGleb Smirnoff 		fsk_attach(fs, s);
13263b3a8eb9SGleb Smirnoff 	}
13273b3a8eb9SGleb Smirnoff }
13283b3a8eb9SGleb Smirnoff 
132991336b40SDon Lewis #ifdef NEW_AQM
133091336b40SDon Lewis /* Retrieve AQM configurations to ipfw userland
133191336b40SDon Lewis  */
133291336b40SDon Lewis static int
133391336b40SDon Lewis get_aqm_parms(struct sockopt *sopt)
133491336b40SDon Lewis {
133591336b40SDon Lewis 	struct dn_extra_parms  *ep;
133691336b40SDon Lewis 	struct dn_fsk *fs;
133791336b40SDon Lewis 	size_t sopt_valsize;
133891336b40SDon Lewis 	int l, err = 0;
133991336b40SDon Lewis 
134091336b40SDon Lewis 	sopt_valsize = sopt->sopt_valsize;
134191336b40SDon Lewis 	l = sizeof(*ep);
134291336b40SDon Lewis 	if (sopt->sopt_valsize < l) {
134391336b40SDon Lewis 		D("bad len sopt->sopt_valsize %d len %d",
134491336b40SDon Lewis 			(int) sopt->sopt_valsize , l);
134591336b40SDon Lewis 		err = EINVAL;
134691336b40SDon Lewis 		return err;
134791336b40SDon Lewis 	}
134891336b40SDon Lewis 	ep = malloc(l, M_DUMMYNET, M_WAITOK);
134991336b40SDon Lewis 	if(!ep) {
135091336b40SDon Lewis 		err = ENOMEM ;
135191336b40SDon Lewis 		return err;
135291336b40SDon Lewis 	}
135391336b40SDon Lewis 	do {
135491336b40SDon Lewis 		err = sooptcopyin(sopt, ep, l, l);
135591336b40SDon Lewis 		if(err)
135691336b40SDon Lewis 			break;
135791336b40SDon Lewis 		sopt->sopt_valsize = sopt_valsize;
135891336b40SDon Lewis 		if (ep->oid.len < l) {
135991336b40SDon Lewis 			err = EINVAL;
136091336b40SDon Lewis 			break;
136191336b40SDon Lewis 		}
136291336b40SDon Lewis 
136391336b40SDon Lewis 		fs = dn_ht_find(dn_cfg.fshash, ep->nr, 0, NULL);
136491336b40SDon Lewis 		if (!fs) {
136591336b40SDon Lewis 			D("fs %d not found", ep->nr);
136691336b40SDon Lewis 			err = EINVAL;
136791336b40SDon Lewis 			break;
136891336b40SDon Lewis 		}
136991336b40SDon Lewis 
137091336b40SDon Lewis 		if (fs->aqmfp && fs->aqmfp->getconfig) {
137191336b40SDon Lewis 			if(fs->aqmfp->getconfig(fs, ep)) {
137291336b40SDon Lewis 				D("Error while trying to get AQM params");
137391336b40SDon Lewis 				err = EINVAL;
137491336b40SDon Lewis 				break;
137591336b40SDon Lewis 			}
137691336b40SDon Lewis 			ep->oid.len = l;
137791336b40SDon Lewis 			err = sooptcopyout(sopt, ep, l);
137891336b40SDon Lewis 		}
137991336b40SDon Lewis 	}while(0);
138091336b40SDon Lewis 
138191336b40SDon Lewis 	free(ep, M_DUMMYNET);
138291336b40SDon Lewis 	return err;
138391336b40SDon Lewis }
138491336b40SDon Lewis 
138591336b40SDon Lewis /* Retrieve AQM configurations to ipfw userland
138691336b40SDon Lewis  */
138791336b40SDon Lewis static int
138891336b40SDon Lewis get_sched_parms(struct sockopt *sopt)
138991336b40SDon Lewis {
139091336b40SDon Lewis 	struct dn_extra_parms  *ep;
139191336b40SDon Lewis 	struct dn_schk *schk;
139291336b40SDon Lewis 	size_t sopt_valsize;
139391336b40SDon Lewis 	int l, err = 0;
139491336b40SDon Lewis 
139591336b40SDon Lewis 	sopt_valsize = sopt->sopt_valsize;
139691336b40SDon Lewis 	l = sizeof(*ep);
139791336b40SDon Lewis 	if (sopt->sopt_valsize < l) {
139891336b40SDon Lewis 		D("bad len sopt->sopt_valsize %d len %d",
139991336b40SDon Lewis 			(int) sopt->sopt_valsize , l);
140091336b40SDon Lewis 		err = EINVAL;
140191336b40SDon Lewis 		return err;
140291336b40SDon Lewis 	}
140391336b40SDon Lewis 	ep = malloc(l, M_DUMMYNET, M_WAITOK);
140491336b40SDon Lewis 	if(!ep) {
140591336b40SDon Lewis 		err = ENOMEM ;
140691336b40SDon Lewis 		return err;
140791336b40SDon Lewis 	}
140891336b40SDon Lewis 	do {
140991336b40SDon Lewis 		err = sooptcopyin(sopt, ep, l, l);
141091336b40SDon Lewis 		if(err)
141191336b40SDon Lewis 			break;
141291336b40SDon Lewis 		sopt->sopt_valsize = sopt_valsize;
141391336b40SDon Lewis 		if (ep->oid.len < l) {
141491336b40SDon Lewis 			err = EINVAL;
141591336b40SDon Lewis 			break;
141691336b40SDon Lewis 		}
141791336b40SDon Lewis 
141891336b40SDon Lewis 		schk = locate_scheduler(ep->nr);
141991336b40SDon Lewis 		if (!schk) {
142091336b40SDon Lewis 			D("sched %d not found", ep->nr);
142191336b40SDon Lewis 			err = EINVAL;
142291336b40SDon Lewis 			break;
142391336b40SDon Lewis 		}
142491336b40SDon Lewis 
142591336b40SDon Lewis 		if (schk->fp && schk->fp->getconfig) {
142691336b40SDon Lewis 			if(schk->fp->getconfig(schk, ep)) {
142791336b40SDon Lewis 				D("Error while trying to get sched params");
142891336b40SDon Lewis 				err = EINVAL;
142991336b40SDon Lewis 				break;
143091336b40SDon Lewis 			}
143191336b40SDon Lewis 			ep->oid.len = l;
143291336b40SDon Lewis 			err = sooptcopyout(sopt, ep, l);
143391336b40SDon Lewis 		}
143491336b40SDon Lewis 	}while(0);
143591336b40SDon Lewis 	free(ep, M_DUMMYNET);
143691336b40SDon Lewis 
143791336b40SDon Lewis 	return err;
143891336b40SDon Lewis }
143991336b40SDon Lewis 
144091336b40SDon Lewis /* Configure AQM for flowset 'fs'.
144191336b40SDon Lewis  * extra parameters are passed from userland.
144291336b40SDon Lewis  */
144391336b40SDon Lewis static int
144491336b40SDon Lewis config_aqm(struct dn_fsk *fs, struct  dn_extra_parms *ep, int busy)
144591336b40SDon Lewis {
144691336b40SDon Lewis 	int err = 0;
144791336b40SDon Lewis 
144891336b40SDon Lewis 	do {
144991336b40SDon Lewis 		/* no configurations */
145091336b40SDon Lewis 		if (!ep) {
145191336b40SDon Lewis 			err = 0;
145291336b40SDon Lewis 			break;
145391336b40SDon Lewis 		}
145491336b40SDon Lewis 
145591336b40SDon Lewis 		/* no AQM for this flowset*/
145691336b40SDon Lewis 		if (!strcmp(ep->name,"")) {
145791336b40SDon Lewis 			err = 0;
145891336b40SDon Lewis 			break;
145991336b40SDon Lewis 		}
146091336b40SDon Lewis 		if (ep->oid.len < sizeof(*ep)) {
146191336b40SDon Lewis 			D("short aqm len %d", ep->oid.len);
146291336b40SDon Lewis 				err = EINVAL;
146391336b40SDon Lewis 				break;
146491336b40SDon Lewis 		}
146591336b40SDon Lewis 
146691336b40SDon Lewis 		if (busy) {
146791336b40SDon Lewis 			D("Unable to configure flowset, flowset busy!");
146891336b40SDon Lewis 			err = EINVAL;
146991336b40SDon Lewis 			break;
147091336b40SDon Lewis 		}
147191336b40SDon Lewis 
147291336b40SDon Lewis 		/* deconfigure old aqm if exist */
147391336b40SDon Lewis 		if (fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig) {
147491336b40SDon Lewis 			aqm_cleanup_deconfig_fs(fs);
147591336b40SDon Lewis 		}
147691336b40SDon Lewis 
147791336b40SDon Lewis 		if (!(fs->aqmfp = find_aqm_type(0, ep->name))) {
147891336b40SDon Lewis 			D("AQM functions not found for type %s!", ep->name);
147991336b40SDon Lewis 			fs->fs.flags &= ~DN_IS_AQM;
148091336b40SDon Lewis 			err = EINVAL;
148191336b40SDon Lewis 			break;
148291336b40SDon Lewis 		} else
148391336b40SDon Lewis 			fs->fs.flags |= DN_IS_AQM;
148491336b40SDon Lewis 
148591336b40SDon Lewis 		if (ep->oid.subtype != DN_AQM_PARAMS) {
148691336b40SDon Lewis 				D("Wrong subtype");
148791336b40SDon Lewis 				err = EINVAL;
148891336b40SDon Lewis 				break;
148991336b40SDon Lewis 		}
149091336b40SDon Lewis 
149191336b40SDon Lewis 		if (fs->aqmfp->config) {
149291336b40SDon Lewis 			err = fs->aqmfp->config(fs, ep, ep->oid.len);
149391336b40SDon Lewis 			if (err) {
149491336b40SDon Lewis 					D("Unable to configure AQM for FS %d", fs->fs.fs_nr );
149591336b40SDon Lewis 					fs->fs.flags &= ~DN_IS_AQM;
149691336b40SDon Lewis 					fs->aqmfp = NULL;
149791336b40SDon Lewis 					break;
149891336b40SDon Lewis 			}
149991336b40SDon Lewis 		}
150091336b40SDon Lewis 	} while(0);
150191336b40SDon Lewis 
150291336b40SDon Lewis 	return err;
150391336b40SDon Lewis }
150491336b40SDon Lewis #endif
150591336b40SDon Lewis 
15063b3a8eb9SGleb Smirnoff /*
15073b3a8eb9SGleb Smirnoff  * Configuration -- to preserve backward compatibility we use
15083b3a8eb9SGleb Smirnoff  * the following scheme (N is 65536)
15093b3a8eb9SGleb Smirnoff  *	NUMBER		SCHED	LINK	FLOWSET
15103b3a8eb9SGleb Smirnoff  *	   1 ..  N-1	(1)WFQ	(2)WFQ	(3)queue
15113b3a8eb9SGleb Smirnoff  *	 N+1 .. 2N-1	(4)FIFO (5)FIFO	(6)FIFO for sched 1..N-1
15123b3a8eb9SGleb Smirnoff  *	2N+1 .. 3N-1	--	--	(7)FIFO for sched N+1..2N-1
15133b3a8eb9SGleb Smirnoff  *
15143b3a8eb9SGleb Smirnoff  * "pipe i config" configures #1, #2 and #3
15153b3a8eb9SGleb Smirnoff  * "sched i config" configures #1 and possibly #6
15163b3a8eb9SGleb Smirnoff  * "queue i config" configures #3
15173b3a8eb9SGleb Smirnoff  * #1 is configured with 'pipe i config' or 'sched i config'
15183b3a8eb9SGleb Smirnoff  * #2 is configured with 'pipe i config', and created if not
15193b3a8eb9SGleb Smirnoff  *	existing with 'sched i config'
15203b3a8eb9SGleb Smirnoff  * #3 is configured with 'queue i config'
15213b3a8eb9SGleb Smirnoff  * #4 is automatically configured after #1, can only be FIFO
15223b3a8eb9SGleb Smirnoff  * #5 is automatically configured after #2
15233b3a8eb9SGleb Smirnoff  * #6 is automatically created when #1 is !MULTIQUEUE,
15243b3a8eb9SGleb Smirnoff  *	and can be updated.
15253b3a8eb9SGleb Smirnoff  * #7 is automatically configured after #2
15263b3a8eb9SGleb Smirnoff  */
15273b3a8eb9SGleb Smirnoff 
15283b3a8eb9SGleb Smirnoff /*
15293b3a8eb9SGleb Smirnoff  * configure a link (and its FIFO instance)
15303b3a8eb9SGleb Smirnoff  */
15313b3a8eb9SGleb Smirnoff static int
15323b3a8eb9SGleb Smirnoff config_link(struct dn_link *p, struct dn_id *arg)
15333b3a8eb9SGleb Smirnoff {
15343b3a8eb9SGleb Smirnoff 	int i;
15353b3a8eb9SGleb Smirnoff 
15363b3a8eb9SGleb Smirnoff 	if (p->oid.len != sizeof(*p)) {
15373b3a8eb9SGleb Smirnoff 		D("invalid pipe len %d", p->oid.len);
15383b3a8eb9SGleb Smirnoff 		return EINVAL;
15393b3a8eb9SGleb Smirnoff 	}
15403b3a8eb9SGleb Smirnoff 	i = p->link_nr;
15413b3a8eb9SGleb Smirnoff 	if (i <= 0 || i >= DN_MAX_ID)
15423b3a8eb9SGleb Smirnoff 		return EINVAL;
15433b3a8eb9SGleb Smirnoff 	/*
15443b3a8eb9SGleb Smirnoff 	 * The config program passes parameters as follows:
15453b3a8eb9SGleb Smirnoff 	 * bw = bits/second (0 means no limits),
15463b3a8eb9SGleb Smirnoff 	 * delay = ms, must be translated into ticks.
15473b3a8eb9SGleb Smirnoff 	 * qsize = slots/bytes
15483b3a8eb9SGleb Smirnoff 	 * burst ???
15493b3a8eb9SGleb Smirnoff 	 */
15503b3a8eb9SGleb Smirnoff 	p->delay = (p->delay * hz) / 1000;
15513b3a8eb9SGleb Smirnoff 	/* Scale burst size: bytes -> bits * hz */
15523b3a8eb9SGleb Smirnoff 	p->burst *= 8 * hz;
15533b3a8eb9SGleb Smirnoff 
15543b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
15553b3a8eb9SGleb Smirnoff 	/* do it twice, base link and FIFO link */
15563b3a8eb9SGleb Smirnoff 	for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
15573b3a8eb9SGleb Smirnoff 	    struct dn_schk *s = locate_scheduler(i);
15583b3a8eb9SGleb Smirnoff 	    if (s == NULL) {
15593b3a8eb9SGleb Smirnoff 		DN_BH_WUNLOCK();
15603b3a8eb9SGleb Smirnoff 		D("sched %d not found", i);
15613b3a8eb9SGleb Smirnoff 		return EINVAL;
15623b3a8eb9SGleb Smirnoff 	    }
15633b3a8eb9SGleb Smirnoff 	    /* remove profile if exists */
15643b3a8eb9SGleb Smirnoff 	    if (s->profile) {
15653b3a8eb9SGleb Smirnoff 		free(s->profile, M_DUMMYNET);
15663b3a8eb9SGleb Smirnoff 		s->profile = NULL;
15673b3a8eb9SGleb Smirnoff 	    }
15683b3a8eb9SGleb Smirnoff 	    /* copy all parameters */
15693b3a8eb9SGleb Smirnoff 	    s->link.oid = p->oid;
15703b3a8eb9SGleb Smirnoff 	    s->link.link_nr = i;
15713b3a8eb9SGleb Smirnoff 	    s->link.delay = p->delay;
15723b3a8eb9SGleb Smirnoff 	    if (s->link.bandwidth != p->bandwidth) {
15733b3a8eb9SGleb Smirnoff 		/* XXX bandwidth changes, need to update red params */
15743b3a8eb9SGleb Smirnoff 	    s->link.bandwidth = p->bandwidth;
15753b3a8eb9SGleb Smirnoff 		update_red(s);
15763b3a8eb9SGleb Smirnoff 	    }
15773b3a8eb9SGleb Smirnoff 	    s->link.burst = p->burst;
15783b3a8eb9SGleb Smirnoff 	    schk_reset_credit(s);
15793b3a8eb9SGleb Smirnoff 	}
15803b3a8eb9SGleb Smirnoff 	dn_cfg.id++;
15813b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
15823b3a8eb9SGleb Smirnoff 	return 0;
15833b3a8eb9SGleb Smirnoff }
15843b3a8eb9SGleb Smirnoff 
15853b3a8eb9SGleb Smirnoff /*
15863b3a8eb9SGleb Smirnoff  * configure a flowset. Can be called from inside with locked=1,
15873b3a8eb9SGleb Smirnoff  */
15883b3a8eb9SGleb Smirnoff static struct dn_fsk *
15893b3a8eb9SGleb Smirnoff config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
15903b3a8eb9SGleb Smirnoff {
15913b3a8eb9SGleb Smirnoff 	int i;
15923b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs;
1593*0ac43d97SMarius Strobl #ifdef NEW_AQM
1594*0ac43d97SMarius Strobl 	struct dn_extra_parms *ep;
1595*0ac43d97SMarius Strobl #endif
15963b3a8eb9SGleb Smirnoff 
15973b3a8eb9SGleb Smirnoff 	if (nfs->oid.len != sizeof(*nfs)) {
15983b3a8eb9SGleb Smirnoff 		D("invalid flowset len %d", nfs->oid.len);
15993b3a8eb9SGleb Smirnoff 		return NULL;
16003b3a8eb9SGleb Smirnoff 	}
16013b3a8eb9SGleb Smirnoff 	i = nfs->fs_nr;
16023b3a8eb9SGleb Smirnoff 	if (i <= 0 || i >= 3*DN_MAX_ID)
16033b3a8eb9SGleb Smirnoff 		return NULL;
1604*0ac43d97SMarius Strobl #ifdef NEW_AQM
1605*0ac43d97SMarius Strobl 	ep = NULL;
1606*0ac43d97SMarius Strobl 	if (arg != NULL) {
1607*0ac43d97SMarius Strobl 		ep = malloc(sizeof(*ep), M_TEMP, locked ? M_NOWAIT : M_WAITOK);
1608*0ac43d97SMarius Strobl 		if (ep == NULL)
1609*0ac43d97SMarius Strobl 			return (NULL);
1610*0ac43d97SMarius Strobl 		memcpy(ep, arg, sizeof(*ep));
1611*0ac43d97SMarius Strobl 	}
1612*0ac43d97SMarius Strobl #endif
16133b3a8eb9SGleb Smirnoff 	ND("flowset %d", i);
16143b3a8eb9SGleb Smirnoff 	/* XXX other sanity checks */
16153b3a8eb9SGleb Smirnoff         if (nfs->flags & DN_QSIZE_BYTES) {
16163b3a8eb9SGleb Smirnoff 		ipdn_bound_var(&nfs->qsize, 16384,
16173b3a8eb9SGleb Smirnoff 		    1500, dn_cfg.byte_limit, NULL); // "queue byte size");
16183b3a8eb9SGleb Smirnoff         } else {
16193b3a8eb9SGleb Smirnoff 		ipdn_bound_var(&nfs->qsize, 50,
16203b3a8eb9SGleb Smirnoff 		    1, dn_cfg.slot_limit, NULL); // "queue slot size");
16213b3a8eb9SGleb Smirnoff         }
16223b3a8eb9SGleb Smirnoff 	if (nfs->flags & DN_HAVE_MASK) {
16233b3a8eb9SGleb Smirnoff 		/* make sure we have some buckets */
16243b3a8eb9SGleb Smirnoff 		ipdn_bound_var((int *)&nfs->buckets, dn_cfg.hash_size,
16253b3a8eb9SGleb Smirnoff 			1, dn_cfg.max_hash_size, "flowset buckets");
16263b3a8eb9SGleb Smirnoff 	} else {
16273b3a8eb9SGleb Smirnoff 		nfs->buckets = 1;	/* we only need 1 */
16283b3a8eb9SGleb Smirnoff 	}
16293b3a8eb9SGleb Smirnoff 	if (!locked)
16303b3a8eb9SGleb Smirnoff 		DN_BH_WLOCK();
16313b3a8eb9SGleb Smirnoff 	do { /* exit with break when done */
16323b3a8eb9SGleb Smirnoff 	    struct dn_schk *s;
16333b3a8eb9SGleb Smirnoff 	    int flags = nfs->sched_nr ? DNHT_INSERT : 0;
16343b3a8eb9SGleb Smirnoff 	    int j;
16353b3a8eb9SGleb Smirnoff 	    int oldc = dn_cfg.fsk_count;
16363b3a8eb9SGleb Smirnoff 	    fs = dn_ht_find(dn_cfg.fshash, i, flags, NULL);
16373b3a8eb9SGleb Smirnoff 	    if (fs == NULL) {
16383b3a8eb9SGleb Smirnoff 		D("missing sched for flowset %d", i);
16393b3a8eb9SGleb Smirnoff 	        break;
16403b3a8eb9SGleb Smirnoff 	    }
16413b3a8eb9SGleb Smirnoff 	    /* grab some defaults from the existing one */
16423b3a8eb9SGleb Smirnoff 	    if (nfs->sched_nr == 0) /* reuse */
16433b3a8eb9SGleb Smirnoff 		nfs->sched_nr = fs->fs.sched_nr;
16443b3a8eb9SGleb Smirnoff 	    for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
16453b3a8eb9SGleb Smirnoff 		if (nfs->par[j] == -1) /* reuse */
16463b3a8eb9SGleb Smirnoff 		    nfs->par[j] = fs->fs.par[j];
16473b3a8eb9SGleb Smirnoff 	    }
16483b3a8eb9SGleb Smirnoff 	    if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
16493b3a8eb9SGleb Smirnoff 		ND("flowset %d unchanged", i);
165091336b40SDon Lewis #ifdef NEW_AQM
1651*0ac43d97SMarius Strobl 		if (ep != NULL) {
1652*0ac43d97SMarius Strobl 			/*
1653*0ac43d97SMarius Strobl 			 * Reconfigure AQM as the parameters can be changed.
1654*0ac43d97SMarius Strobl 			 * We consider the flowset as busy if it has scheduler
1655*0ac43d97SMarius Strobl 			 * instance(s).
165691336b40SDon Lewis 			 */
165791336b40SDon Lewis 			s = locate_scheduler(nfs->sched_nr);
1658*0ac43d97SMarius Strobl 			config_aqm(fs, ep, s != NULL && s->siht != NULL);
1659*0ac43d97SMarius Strobl 		}
166091336b40SDon Lewis #endif
16613b3a8eb9SGleb Smirnoff 		break; /* no change, nothing to do */
16623b3a8eb9SGleb Smirnoff 	    }
16633b3a8eb9SGleb Smirnoff 	    if (oldc != dn_cfg.fsk_count)	/* new item */
16643b3a8eb9SGleb Smirnoff 		dn_cfg.id++;
16653b3a8eb9SGleb Smirnoff 	    s = locate_scheduler(nfs->sched_nr);
16663b3a8eb9SGleb Smirnoff 	    /* detach from old scheduler if needed, preserving
16673b3a8eb9SGleb Smirnoff 	     * queues if we need to reattach. Then update the
16683b3a8eb9SGleb Smirnoff 	     * configuration, and possibly attach to the new sched.
16693b3a8eb9SGleb Smirnoff 	     */
16703b3a8eb9SGleb Smirnoff 	    DX(2, "fs %d changed sched %d@%p to %d@%p",
16713b3a8eb9SGleb Smirnoff 		fs->fs.fs_nr,
16723b3a8eb9SGleb Smirnoff 		fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
16733b3a8eb9SGleb Smirnoff 	    if (fs->sched) {
16743b3a8eb9SGleb Smirnoff 		int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
16753b3a8eb9SGleb Smirnoff 		flags |= DN_DESTROY; /* XXX temporary */
16763b3a8eb9SGleb Smirnoff 		fsk_detach(fs, flags);
16773b3a8eb9SGleb Smirnoff 	    }
16783b3a8eb9SGleb Smirnoff 	    fs->fs = *nfs; /* copy configuration */
167991336b40SDon Lewis #ifdef NEW_AQM
168091336b40SDon Lewis 			fs->aqmfp = NULL;
1681*0ac43d97SMarius Strobl 			if (ep != NULL)
1682*0ac43d97SMarius Strobl 				config_aqm(fs, ep, s != NULL &&
1683*0ac43d97SMarius Strobl 				    s->siht != NULL);
168491336b40SDon Lewis #endif
16853b3a8eb9SGleb Smirnoff 	    if (s != NULL)
16863b3a8eb9SGleb Smirnoff 		fsk_attach(fs, s);
16873b3a8eb9SGleb Smirnoff 	} while (0);
16883b3a8eb9SGleb Smirnoff 	if (!locked)
16893b3a8eb9SGleb Smirnoff 		DN_BH_WUNLOCK();
1690*0ac43d97SMarius Strobl #ifdef NEW_AQM
1691*0ac43d97SMarius Strobl 	if (ep != NULL)
1692*0ac43d97SMarius Strobl 		free(ep, M_TEMP);
1693*0ac43d97SMarius Strobl #endif
16943b3a8eb9SGleb Smirnoff 	return fs;
16953b3a8eb9SGleb Smirnoff }
16963b3a8eb9SGleb Smirnoff 
16973b3a8eb9SGleb Smirnoff /*
16983b3a8eb9SGleb Smirnoff  * config/reconfig a scheduler and its FIFO variant.
16993b3a8eb9SGleb Smirnoff  * For !MULTIQUEUE schedulers, also set up the flowset.
17003b3a8eb9SGleb Smirnoff  *
17013b3a8eb9SGleb Smirnoff  * On reconfigurations (detected because s->fp is set),
17023b3a8eb9SGleb Smirnoff  * detach existing flowsets preserving traffic, preserve link,
17033b3a8eb9SGleb Smirnoff  * and delete the old scheduler creating a new one.
17043b3a8eb9SGleb Smirnoff  */
17053b3a8eb9SGleb Smirnoff static int
17063b3a8eb9SGleb Smirnoff config_sched(struct dn_sch *_nsch, struct dn_id *arg)
17073b3a8eb9SGleb Smirnoff {
17083b3a8eb9SGleb Smirnoff 	struct dn_schk *s;
17093b3a8eb9SGleb Smirnoff 	struct schk_new_arg a; /* argument for schk_new */
17103b3a8eb9SGleb Smirnoff 	int i;
17113b3a8eb9SGleb Smirnoff 	struct dn_link p;	/* copy of oldlink */
17123b3a8eb9SGleb Smirnoff 	struct dn_profile *pf = NULL;	/* copy of old link profile */
17133b3a8eb9SGleb Smirnoff 	/* Used to preserv mask parameter */
17143b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id new_mask;
17153b3a8eb9SGleb Smirnoff 	int new_buckets = 0;
17163b3a8eb9SGleb Smirnoff 	int new_flags = 0;
17173b3a8eb9SGleb Smirnoff 	int pipe_cmd;
17183b3a8eb9SGleb Smirnoff 	int err = ENOMEM;
17193b3a8eb9SGleb Smirnoff 
17203b3a8eb9SGleb Smirnoff 	a.sch = _nsch;
17213b3a8eb9SGleb Smirnoff 	if (a.sch->oid.len != sizeof(*a.sch)) {
17223b3a8eb9SGleb Smirnoff 		D("bad sched len %d", a.sch->oid.len);
17233b3a8eb9SGleb Smirnoff 		return EINVAL;
17243b3a8eb9SGleb Smirnoff 	}
17253b3a8eb9SGleb Smirnoff 	i = a.sch->sched_nr;
17263b3a8eb9SGleb Smirnoff 	if (i <= 0 || i >= DN_MAX_ID)
17273b3a8eb9SGleb Smirnoff 		return EINVAL;
17283b3a8eb9SGleb Smirnoff 	/* make sure we have some buckets */
17293b3a8eb9SGleb Smirnoff 	if (a.sch->flags & DN_HAVE_MASK)
17303b3a8eb9SGleb Smirnoff 		ipdn_bound_var((int *)&a.sch->buckets, dn_cfg.hash_size,
17313b3a8eb9SGleb Smirnoff 			1, dn_cfg.max_hash_size, "sched buckets");
17323b3a8eb9SGleb Smirnoff 	/* XXX other sanity checks */
17333b3a8eb9SGleb Smirnoff 	bzero(&p, sizeof(p));
17343b3a8eb9SGleb Smirnoff 
17353b3a8eb9SGleb Smirnoff 	pipe_cmd = a.sch->flags & DN_PIPE_CMD;
17363b3a8eb9SGleb Smirnoff 	a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
17373b3a8eb9SGleb Smirnoff 	if (pipe_cmd) {
17383b3a8eb9SGleb Smirnoff 		/* Copy mask parameter */
17393b3a8eb9SGleb Smirnoff 		new_mask = a.sch->sched_mask;
17403b3a8eb9SGleb Smirnoff 		new_buckets = a.sch->buckets;
17413b3a8eb9SGleb Smirnoff 		new_flags = a.sch->flags;
17423b3a8eb9SGleb Smirnoff 	}
17433b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
17443b3a8eb9SGleb Smirnoff again: /* run twice, for wfq and fifo */
17453b3a8eb9SGleb Smirnoff 	/*
17463b3a8eb9SGleb Smirnoff 	 * lookup the type. If not supplied, use the previous one
17473b3a8eb9SGleb Smirnoff 	 * or default to WF2Q+. Otherwise, return an error.
17483b3a8eb9SGleb Smirnoff 	 */
17493b3a8eb9SGleb Smirnoff 	dn_cfg.id++;
17503b3a8eb9SGleb Smirnoff 	a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
17513b3a8eb9SGleb Smirnoff 	if (a.fp != NULL) {
17523b3a8eb9SGleb Smirnoff 		/* found. Lookup or create entry */
17533b3a8eb9SGleb Smirnoff 		s = dn_ht_find(dn_cfg.schedhash, i, DNHT_INSERT, &a);
17543b3a8eb9SGleb Smirnoff 	} else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
17553b3a8eb9SGleb Smirnoff 		/* No type. search existing s* or retry with WF2Q+ */
17563b3a8eb9SGleb Smirnoff 		s = dn_ht_find(dn_cfg.schedhash, i, 0, &a);
17573b3a8eb9SGleb Smirnoff 		if (s != NULL) {
17583b3a8eb9SGleb Smirnoff 			a.fp = s->fp;
17593b3a8eb9SGleb Smirnoff 			/* Scheduler exists, skip to FIFO scheduler
17603b3a8eb9SGleb Smirnoff 			 * if command was pipe config...
17613b3a8eb9SGleb Smirnoff 			 */
17623b3a8eb9SGleb Smirnoff 			if (pipe_cmd)
17633b3a8eb9SGleb Smirnoff 				goto next;
17643b3a8eb9SGleb Smirnoff 		} else {
17653b3a8eb9SGleb Smirnoff 			/* New scheduler, create a wf2q+ with no mask
17663b3a8eb9SGleb Smirnoff 			 * if command was pipe config...
17673b3a8eb9SGleb Smirnoff 			 */
17683b3a8eb9SGleb Smirnoff 			if (pipe_cmd) {
17693b3a8eb9SGleb Smirnoff 				/* clear mask parameter */
17703b3a8eb9SGleb Smirnoff 				bzero(&a.sch->sched_mask, sizeof(new_mask));
17713b3a8eb9SGleb Smirnoff 				a.sch->buckets = 0;
17723b3a8eb9SGleb Smirnoff 				a.sch->flags &= ~DN_HAVE_MASK;
17733b3a8eb9SGleb Smirnoff 			}
17743b3a8eb9SGleb Smirnoff 			a.sch->oid.subtype = DN_SCHED_WF2QP;
17753b3a8eb9SGleb Smirnoff 			goto again;
17763b3a8eb9SGleb Smirnoff 		}
17773b3a8eb9SGleb Smirnoff 	} else {
17783b3a8eb9SGleb Smirnoff 		D("invalid scheduler type %d %s",
17793b3a8eb9SGleb Smirnoff 			a.sch->oid.subtype, a.sch->name);
17803b3a8eb9SGleb Smirnoff 		err = EINVAL;
17813b3a8eb9SGleb Smirnoff 		goto error;
17823b3a8eb9SGleb Smirnoff 	}
17833b3a8eb9SGleb Smirnoff 	/* normalize name and subtype */
17843b3a8eb9SGleb Smirnoff 	a.sch->oid.subtype = a.fp->type;
17853b3a8eb9SGleb Smirnoff 	bzero(a.sch->name, sizeof(a.sch->name));
17863b3a8eb9SGleb Smirnoff 	strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
17873b3a8eb9SGleb Smirnoff 	if (s == NULL) {
17883b3a8eb9SGleb Smirnoff 		D("cannot allocate scheduler %d", i);
17893b3a8eb9SGleb Smirnoff 		goto error;
17903b3a8eb9SGleb Smirnoff 	}
17913b3a8eb9SGleb Smirnoff 	/* restore existing link if any */
17923b3a8eb9SGleb Smirnoff 	if (p.link_nr) {
17933b3a8eb9SGleb Smirnoff 		s->link = p;
17943b3a8eb9SGleb Smirnoff 		if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
17953b3a8eb9SGleb Smirnoff 			s->profile = NULL; /* XXX maybe not needed */
17963b3a8eb9SGleb Smirnoff 		} else {
17973b3a8eb9SGleb Smirnoff 			s->profile = malloc(sizeof(struct dn_profile),
17983b3a8eb9SGleb Smirnoff 					     M_DUMMYNET, M_NOWAIT | M_ZERO);
17993b3a8eb9SGleb Smirnoff 			if (s->profile == NULL) {
18003b3a8eb9SGleb Smirnoff 				D("cannot allocate profile");
18013b3a8eb9SGleb Smirnoff 				goto error; //XXX
18023b3a8eb9SGleb Smirnoff 			}
1803*0ac43d97SMarius Strobl 			memcpy(s->profile, pf, sizeof(*pf));
18043b3a8eb9SGleb Smirnoff 		}
18053b3a8eb9SGleb Smirnoff 	}
18063b3a8eb9SGleb Smirnoff 	p.link_nr = 0;
18073b3a8eb9SGleb Smirnoff 	if (s->fp == NULL) {
18083b3a8eb9SGleb Smirnoff 		DX(2, "sched %d new type %s", i, a.fp->name);
18093b3a8eb9SGleb Smirnoff 	} else if (s->fp != a.fp ||
18103b3a8eb9SGleb Smirnoff 			bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
18113b3a8eb9SGleb Smirnoff 		/* already existing. */
18123b3a8eb9SGleb Smirnoff 		DX(2, "sched %d type changed from %s to %s",
18133b3a8eb9SGleb Smirnoff 			i, s->fp->name, a.fp->name);
18143b3a8eb9SGleb Smirnoff 		DX(4, "   type/sub %d/%d -> %d/%d",
18153b3a8eb9SGleb Smirnoff 			s->sch.oid.type, s->sch.oid.subtype,
18163b3a8eb9SGleb Smirnoff 			a.sch->oid.type, a.sch->oid.subtype);
18173b3a8eb9SGleb Smirnoff 		if (s->link.link_nr == 0)
18183b3a8eb9SGleb Smirnoff 			D("XXX WARNING link 0 for sched %d", i);
18193b3a8eb9SGleb Smirnoff 		p = s->link;	/* preserve link */
18203b3a8eb9SGleb Smirnoff 		if (s->profile) {/* preserve profile */
18213b3a8eb9SGleb Smirnoff 			if (!pf)
18223b3a8eb9SGleb Smirnoff 				pf = malloc(sizeof(*pf),
18233b3a8eb9SGleb Smirnoff 				    M_DUMMYNET, M_NOWAIT | M_ZERO);
18243b3a8eb9SGleb Smirnoff 			if (pf)	/* XXX should issue a warning otherwise */
1825*0ac43d97SMarius Strobl 				memcpy(pf, s->profile, sizeof(*pf));
18263b3a8eb9SGleb Smirnoff 		}
18273b3a8eb9SGleb Smirnoff 		/* remove from the hash */
18283b3a8eb9SGleb Smirnoff 		dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
18293b3a8eb9SGleb Smirnoff 		/* Detach flowsets, preserve queues. */
18303b3a8eb9SGleb Smirnoff 		// schk_delete_cb(s, NULL);
18313b3a8eb9SGleb Smirnoff 		// XXX temporarily, kill queues
18323b3a8eb9SGleb Smirnoff 		schk_delete_cb(s, (void *)DN_DESTROY);
18333b3a8eb9SGleb Smirnoff 		goto again;
18343b3a8eb9SGleb Smirnoff 	} else {
18353b3a8eb9SGleb Smirnoff 		DX(4, "sched %d unchanged type %s", i, a.fp->name);
18363b3a8eb9SGleb Smirnoff 	}
18373b3a8eb9SGleb Smirnoff 	/* complete initialization */
18383b3a8eb9SGleb Smirnoff 	s->sch = *a.sch;
18393b3a8eb9SGleb Smirnoff 	s->fp = a.fp;
18403b3a8eb9SGleb Smirnoff 	s->cfg = arg;
18413b3a8eb9SGleb Smirnoff 	// XXX schk_reset_credit(s);
18423b3a8eb9SGleb Smirnoff 	/* create the internal flowset if needed,
18433b3a8eb9SGleb Smirnoff 	 * trying to reuse existing ones if available
18443b3a8eb9SGleb Smirnoff 	 */
18453b3a8eb9SGleb Smirnoff 	if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
18463b3a8eb9SGleb Smirnoff 	        s->fs = dn_ht_find(dn_cfg.fshash, i, 0, NULL);
18473b3a8eb9SGleb Smirnoff 		if (!s->fs) {
18483b3a8eb9SGleb Smirnoff 			struct dn_fs fs;
18493b3a8eb9SGleb Smirnoff 			bzero(&fs, sizeof(fs));
18503b3a8eb9SGleb Smirnoff 			set_oid(&fs.oid, DN_FS, sizeof(fs));
18513b3a8eb9SGleb Smirnoff 			fs.fs_nr = i + DN_MAX_ID;
18523b3a8eb9SGleb Smirnoff 			fs.sched_nr = i;
18533b3a8eb9SGleb Smirnoff 			s->fs = config_fs(&fs, NULL, 1 /* locked */);
18543b3a8eb9SGleb Smirnoff 		}
18553b3a8eb9SGleb Smirnoff 		if (!s->fs) {
18563b3a8eb9SGleb Smirnoff 			schk_delete_cb(s, (void *)DN_DESTROY);
18573b3a8eb9SGleb Smirnoff 			D("error creating internal fs for %d", i);
18583b3a8eb9SGleb Smirnoff 			goto error;
18593b3a8eb9SGleb Smirnoff 		}
18603b3a8eb9SGleb Smirnoff 	}
18613b3a8eb9SGleb Smirnoff 	/* call init function after the flowset is created */
18623b3a8eb9SGleb Smirnoff 	if (s->fp->config)
18633b3a8eb9SGleb Smirnoff 		s->fp->config(s);
18643b3a8eb9SGleb Smirnoff 	update_fs(s);
18653b3a8eb9SGleb Smirnoff next:
18663b3a8eb9SGleb Smirnoff 	if (i < DN_MAX_ID) { /* now configure the FIFO instance */
18673b3a8eb9SGleb Smirnoff 		i += DN_MAX_ID;
18683b3a8eb9SGleb Smirnoff 		if (pipe_cmd) {
18693b3a8eb9SGleb Smirnoff 			/* Restore mask parameter for FIFO */
18703b3a8eb9SGleb Smirnoff 			a.sch->sched_mask = new_mask;
18713b3a8eb9SGleb Smirnoff 			a.sch->buckets = new_buckets;
18723b3a8eb9SGleb Smirnoff 			a.sch->flags = new_flags;
18733b3a8eb9SGleb Smirnoff 		} else {
18743b3a8eb9SGleb Smirnoff 			/* sched config shouldn't modify the FIFO scheduler */
18753b3a8eb9SGleb Smirnoff 			if (dn_ht_find(dn_cfg.schedhash, i, 0, &a) != NULL) {
18763b3a8eb9SGleb Smirnoff 				/* FIFO already exist, don't touch it */
18773b3a8eb9SGleb Smirnoff 				err = 0; /* and this is not an error */
18783b3a8eb9SGleb Smirnoff 				goto error;
18793b3a8eb9SGleb Smirnoff 			}
18803b3a8eb9SGleb Smirnoff 		}
18813b3a8eb9SGleb Smirnoff 		a.sch->sched_nr = i;
18823b3a8eb9SGleb Smirnoff 		a.sch->oid.subtype = DN_SCHED_FIFO;
18833b3a8eb9SGleb Smirnoff 		bzero(a.sch->name, sizeof(a.sch->name));
18843b3a8eb9SGleb Smirnoff 		goto again;
18853b3a8eb9SGleb Smirnoff 	}
18863b3a8eb9SGleb Smirnoff 	err = 0;
18873b3a8eb9SGleb Smirnoff error:
18883b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
18893b3a8eb9SGleb Smirnoff 	if (pf)
18903b3a8eb9SGleb Smirnoff 		free(pf, M_DUMMYNET);
18913b3a8eb9SGleb Smirnoff 	return err;
18923b3a8eb9SGleb Smirnoff }
18933b3a8eb9SGleb Smirnoff 
18943b3a8eb9SGleb Smirnoff /*
18953b3a8eb9SGleb Smirnoff  * attach a profile to a link
18963b3a8eb9SGleb Smirnoff  */
18973b3a8eb9SGleb Smirnoff static int
18983b3a8eb9SGleb Smirnoff config_profile(struct dn_profile *pf, struct dn_id *arg)
18993b3a8eb9SGleb Smirnoff {
19003b3a8eb9SGleb Smirnoff 	struct dn_schk *s;
19013b3a8eb9SGleb Smirnoff 	int i, olen, err = 0;
19023b3a8eb9SGleb Smirnoff 
19033b3a8eb9SGleb Smirnoff 	if (pf->oid.len < sizeof(*pf)) {
19043b3a8eb9SGleb Smirnoff 		D("short profile len %d", pf->oid.len);
19053b3a8eb9SGleb Smirnoff 		return EINVAL;
19063b3a8eb9SGleb Smirnoff 	}
19073b3a8eb9SGleb Smirnoff 	i = pf->link_nr;
19083b3a8eb9SGleb Smirnoff 	if (i <= 0 || i >= DN_MAX_ID)
19093b3a8eb9SGleb Smirnoff 		return EINVAL;
19103b3a8eb9SGleb Smirnoff 	/* XXX other sanity checks */
19113b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
19123b3a8eb9SGleb Smirnoff 	for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
19133b3a8eb9SGleb Smirnoff 		s = locate_scheduler(i);
19143b3a8eb9SGleb Smirnoff 
19153b3a8eb9SGleb Smirnoff 		if (s == NULL) {
19163b3a8eb9SGleb Smirnoff 			err = EINVAL;
19173b3a8eb9SGleb Smirnoff 			break;
19183b3a8eb9SGleb Smirnoff 		}
19193b3a8eb9SGleb Smirnoff 		dn_cfg.id++;
19203b3a8eb9SGleb Smirnoff 		/*
19213b3a8eb9SGleb Smirnoff 		 * If we had a profile and the new one does not fit,
19223b3a8eb9SGleb Smirnoff 		 * or it is deleted, then we need to free memory.
19233b3a8eb9SGleb Smirnoff 		 */
19243b3a8eb9SGleb Smirnoff 		if (s->profile && (pf->samples_no == 0 ||
19253b3a8eb9SGleb Smirnoff 		    s->profile->oid.len < pf->oid.len)) {
19263b3a8eb9SGleb Smirnoff 			free(s->profile, M_DUMMYNET);
19273b3a8eb9SGleb Smirnoff 			s->profile = NULL;
19283b3a8eb9SGleb Smirnoff 		}
19293b3a8eb9SGleb Smirnoff 		if (pf->samples_no == 0)
19303b3a8eb9SGleb Smirnoff 			continue;
19313b3a8eb9SGleb Smirnoff 		/*
19323b3a8eb9SGleb Smirnoff 		 * new profile, possibly allocate memory
19333b3a8eb9SGleb Smirnoff 		 * and copy data.
19343b3a8eb9SGleb Smirnoff 		 */
19353b3a8eb9SGleb Smirnoff 		if (s->profile == NULL)
19363b3a8eb9SGleb Smirnoff 			s->profile = malloc(pf->oid.len,
19373b3a8eb9SGleb Smirnoff 				    M_DUMMYNET, M_NOWAIT | M_ZERO);
19383b3a8eb9SGleb Smirnoff 		if (s->profile == NULL) {
19393b3a8eb9SGleb Smirnoff 			D("no memory for profile %d", i);
19403b3a8eb9SGleb Smirnoff 			err = ENOMEM;
19413b3a8eb9SGleb Smirnoff 			break;
19423b3a8eb9SGleb Smirnoff 		}
19433b3a8eb9SGleb Smirnoff 		/* preserve larger length XXX double check */
19443b3a8eb9SGleb Smirnoff 		olen = s->profile->oid.len;
19453b3a8eb9SGleb Smirnoff 		if (olen < pf->oid.len)
19463b3a8eb9SGleb Smirnoff 			olen = pf->oid.len;
1947*0ac43d97SMarius Strobl 		memcpy(s->profile, pf, pf->oid.len);
19483b3a8eb9SGleb Smirnoff 		s->profile->oid.len = olen;
19493b3a8eb9SGleb Smirnoff 	}
19503b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
19513b3a8eb9SGleb Smirnoff 	return err;
19523b3a8eb9SGleb Smirnoff }
19533b3a8eb9SGleb Smirnoff 
19543b3a8eb9SGleb Smirnoff /*
19553b3a8eb9SGleb Smirnoff  * Delete all objects:
19563b3a8eb9SGleb Smirnoff  */
19573b3a8eb9SGleb Smirnoff static void
19583b3a8eb9SGleb Smirnoff dummynet_flush(void)
19593b3a8eb9SGleb Smirnoff {
19603b3a8eb9SGleb Smirnoff 
19613b3a8eb9SGleb Smirnoff 	/* delete all schedulers and related links/queues/flowsets */
19623b3a8eb9SGleb Smirnoff 	dn_ht_scan(dn_cfg.schedhash, schk_delete_cb,
19633b3a8eb9SGleb Smirnoff 		(void *)(uintptr_t)DN_DELETE_FS);
19643b3a8eb9SGleb Smirnoff 	/* delete all remaining (unlinked) flowsets */
19653b3a8eb9SGleb Smirnoff 	DX(4, "still %d unlinked fs", dn_cfg.fsk_count);
19663b3a8eb9SGleb Smirnoff 	dn_ht_free(dn_cfg.fshash, DNHT_REMOVE);
19673b3a8eb9SGleb Smirnoff 	fsk_detach_list(&dn_cfg.fsu, DN_DELETE_FS);
19683b3a8eb9SGleb Smirnoff 	/* Reinitialize system heap... */
19693b3a8eb9SGleb Smirnoff 	heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
19703b3a8eb9SGleb Smirnoff }
19713b3a8eb9SGleb Smirnoff 
19723b3a8eb9SGleb Smirnoff /*
19733b3a8eb9SGleb Smirnoff  * Main handler for configuration. We are guaranteed to be called
19743b3a8eb9SGleb Smirnoff  * with an oid which is at least a dn_id.
19753b3a8eb9SGleb Smirnoff  * - the first object is the command (config, delete, flush, ...)
19763b3a8eb9SGleb Smirnoff  * - config_link must be issued after the corresponding config_sched
1977a4641f4eSPedro F. Giffuni  * - parameters (DN_TXT) for an object must precede the object
19783b3a8eb9SGleb Smirnoff  *   processed on a config_sched.
19793b3a8eb9SGleb Smirnoff  */
19803b3a8eb9SGleb Smirnoff int
19813b3a8eb9SGleb Smirnoff do_config(void *p, int l)
19823b3a8eb9SGleb Smirnoff {
1983*0ac43d97SMarius Strobl 	struct dn_id o;
1984*0ac43d97SMarius Strobl 	union {
1985*0ac43d97SMarius Strobl 		struct dn_profile profile;
1986*0ac43d97SMarius Strobl 		struct dn_fs fs;
1987*0ac43d97SMarius Strobl 		struct dn_link link;
1988*0ac43d97SMarius Strobl 		struct dn_sch sched;
1989*0ac43d97SMarius Strobl 	} *dn;
1990*0ac43d97SMarius Strobl 	struct dn_id *arg;
1991*0ac43d97SMarius Strobl 	uintptr_t a;
1992*0ac43d97SMarius Strobl 	int err, err2, off;
19933b3a8eb9SGleb Smirnoff 
1994*0ac43d97SMarius Strobl 	memcpy(&o, p, sizeof(o));
1995*0ac43d97SMarius Strobl 	if (o.id != DN_API_VERSION) {
1996*0ac43d97SMarius Strobl 		D("invalid api version got %d need %d", o.id, DN_API_VERSION);
19973b3a8eb9SGleb Smirnoff 		return EINVAL;
19983b3a8eb9SGleb Smirnoff 	}
1999*0ac43d97SMarius Strobl 	arg = NULL;
2000*0ac43d97SMarius Strobl 	dn = NULL;
2001*0ac43d97SMarius Strobl 	for (off = 0; l >= sizeof(o); memcpy(&o, (char *)p + off, sizeof(o))) {
2002*0ac43d97SMarius Strobl 		if (o.len < sizeof(o) || l < o.len) {
2003*0ac43d97SMarius Strobl 			D("bad len o.len %d len %d", o.len, l);
20043b3a8eb9SGleb Smirnoff 			err = EINVAL;
20053b3a8eb9SGleb Smirnoff 			break;
20063b3a8eb9SGleb Smirnoff 		}
2007*0ac43d97SMarius Strobl 		l -= o.len;
20083b3a8eb9SGleb Smirnoff 		err = 0;
2009*0ac43d97SMarius Strobl 		switch (o.type) {
20103b3a8eb9SGleb Smirnoff 		default:
2011*0ac43d97SMarius Strobl 			D("cmd %d not implemented", o.type);
20123b3a8eb9SGleb Smirnoff 			break;
20133b3a8eb9SGleb Smirnoff 
20143b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL
20153b3a8eb9SGleb Smirnoff 		/* sysctl emulation.
20163b3a8eb9SGleb Smirnoff 		 * if we recognize the command, jump to the correct
20173b3a8eb9SGleb Smirnoff 		 * handler and return
20183b3a8eb9SGleb Smirnoff 		 */
20193b3a8eb9SGleb Smirnoff 		case DN_SYSCTL_SET:
20203b3a8eb9SGleb Smirnoff 			err = kesysctl_emu_set(p, l);
20213b3a8eb9SGleb Smirnoff 			return err;
20223b3a8eb9SGleb Smirnoff #endif
20233b3a8eb9SGleb Smirnoff 
20243b3a8eb9SGleb Smirnoff 		case DN_CMD_CONFIG: /* simply a header */
20253b3a8eb9SGleb Smirnoff 			break;
20263b3a8eb9SGleb Smirnoff 
20273b3a8eb9SGleb Smirnoff 		case DN_CMD_DELETE:
20283b3a8eb9SGleb Smirnoff 			/* the argument is in the first uintptr_t after o */
2029*0ac43d97SMarius Strobl 			if (o.len < sizeof(o) + sizeof(a)) {
20303b3a8eb9SGleb Smirnoff 				err = EINVAL;
20313b3a8eb9SGleb Smirnoff 				break;
20323b3a8eb9SGleb Smirnoff 			}
2033*0ac43d97SMarius Strobl 			memcpy(&a, (char *)p + off + sizeof(o), sizeof(a));
2034*0ac43d97SMarius Strobl 			switch (o.subtype) {
20353b3a8eb9SGleb Smirnoff 			case DN_LINK:
20363b3a8eb9SGleb Smirnoff 				/* delete base and derived schedulers */
20373b3a8eb9SGleb Smirnoff 				DN_BH_WLOCK();
2038*0ac43d97SMarius Strobl 				err = delete_schk(a);
2039*0ac43d97SMarius Strobl 				err2 = delete_schk(a + DN_MAX_ID);
20403b3a8eb9SGleb Smirnoff 				DN_BH_WUNLOCK();
20413b3a8eb9SGleb Smirnoff 				if (!err)
20423b3a8eb9SGleb Smirnoff 					err = err2;
20433b3a8eb9SGleb Smirnoff 				break;
20443b3a8eb9SGleb Smirnoff 
20453b3a8eb9SGleb Smirnoff 			default:
2046*0ac43d97SMarius Strobl 				D("invalid delete type %d", o.subtype);
20473b3a8eb9SGleb Smirnoff 				err = EINVAL;
20483b3a8eb9SGleb Smirnoff 				break;
20493b3a8eb9SGleb Smirnoff 
20503b3a8eb9SGleb Smirnoff 			case DN_FS:
2051*0ac43d97SMarius Strobl 				err = (a < 1 || a >= DN_MAX_ID) ?
2052*0ac43d97SMarius Strobl 				    EINVAL : delete_fs(a, 0) ;
20533b3a8eb9SGleb Smirnoff 				break;
20543b3a8eb9SGleb Smirnoff 			}
20553b3a8eb9SGleb Smirnoff 			break;
20563b3a8eb9SGleb Smirnoff 
20573b3a8eb9SGleb Smirnoff 		case DN_CMD_FLUSH:
20583b3a8eb9SGleb Smirnoff 			DN_BH_WLOCK();
20593b3a8eb9SGleb Smirnoff 			dummynet_flush();
20603b3a8eb9SGleb Smirnoff 			DN_BH_WUNLOCK();
20613b3a8eb9SGleb Smirnoff 			break;
2062*0ac43d97SMarius Strobl 		case DN_TEXT:	/* store argument of next block */
2063*0ac43d97SMarius Strobl 			if (arg != NULL)
2064*0ac43d97SMarius Strobl 				free(arg, M_TEMP);
2065*0ac43d97SMarius Strobl 			arg = malloc(o.len, M_TEMP, M_WAITOK);
2066*0ac43d97SMarius Strobl 			memcpy(arg, (char *)p + off, o.len);
20673b3a8eb9SGleb Smirnoff 			break;
20683b3a8eb9SGleb Smirnoff 		case DN_LINK:
2069*0ac43d97SMarius Strobl 			if (dn == NULL)
2070*0ac43d97SMarius Strobl 				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2071*0ac43d97SMarius Strobl 			memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
2072*0ac43d97SMarius Strobl 			err = config_link(&dn->link, arg);
20733b3a8eb9SGleb Smirnoff 			break;
20743b3a8eb9SGleb Smirnoff 		case DN_PROFILE:
2075*0ac43d97SMarius Strobl 			if (dn == NULL)
2076*0ac43d97SMarius Strobl 				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2077*0ac43d97SMarius Strobl 			memcpy(&dn->profile, (char *)p + off,
2078*0ac43d97SMarius Strobl 			    sizeof(dn->profile));
2079*0ac43d97SMarius Strobl 			err = config_profile(&dn->profile, arg);
20803b3a8eb9SGleb Smirnoff 			break;
20813b3a8eb9SGleb Smirnoff 		case DN_SCH:
2082*0ac43d97SMarius Strobl 			if (dn == NULL)
2083*0ac43d97SMarius Strobl 				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2084*0ac43d97SMarius Strobl 			memcpy(&dn->sched, (char *)p + off,
2085*0ac43d97SMarius Strobl 			    sizeof(dn->sched));
2086*0ac43d97SMarius Strobl 			err = config_sched(&dn->sched, arg);
20873b3a8eb9SGleb Smirnoff 			break;
20883b3a8eb9SGleb Smirnoff 		case DN_FS:
2089*0ac43d97SMarius Strobl 			if (dn == NULL)
2090*0ac43d97SMarius Strobl 				dn = malloc(sizeof(*dn), M_TEMP, M_WAITOK);
2091*0ac43d97SMarius Strobl 			memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
2092*0ac43d97SMarius Strobl 			err = (NULL == config_fs(&dn->fs, arg, 0));
20933b3a8eb9SGleb Smirnoff 			break;
20943b3a8eb9SGleb Smirnoff 		}
20953b3a8eb9SGleb Smirnoff 		if (err != 0)
20963b3a8eb9SGleb Smirnoff 			break;
2097*0ac43d97SMarius Strobl 		off += o.len;
20983b3a8eb9SGleb Smirnoff 	}
2099*0ac43d97SMarius Strobl 	if (arg != NULL)
2100*0ac43d97SMarius Strobl 		free(arg, M_TEMP);
2101*0ac43d97SMarius Strobl 	if (dn != NULL)
2102*0ac43d97SMarius Strobl 		free(dn, M_TEMP);
21033b3a8eb9SGleb Smirnoff 	return err;
21043b3a8eb9SGleb Smirnoff }
21053b3a8eb9SGleb Smirnoff 
21063b3a8eb9SGleb Smirnoff static int
21073b3a8eb9SGleb Smirnoff compute_space(struct dn_id *cmd, struct copy_args *a)
21083b3a8eb9SGleb Smirnoff {
21093b3a8eb9SGleb Smirnoff 	int x = 0, need = 0;
21103b3a8eb9SGleb Smirnoff 	int profile_size = sizeof(struct dn_profile) -
21113b3a8eb9SGleb Smirnoff 		ED_MAX_SAMPLES_NO*sizeof(int);
21123b3a8eb9SGleb Smirnoff 
21133b3a8eb9SGleb Smirnoff 	/* NOTE about compute space:
21143b3a8eb9SGleb Smirnoff 	 * NP 	= dn_cfg.schk_count
21153b3a8eb9SGleb Smirnoff 	 * NSI 	= dn_cfg.si_count
21163b3a8eb9SGleb Smirnoff 	 * NF 	= dn_cfg.fsk_count
21173b3a8eb9SGleb Smirnoff 	 * NQ 	= dn_cfg.queue_count
21183b3a8eb9SGleb Smirnoff 	 * - ipfw pipe show
21193b3a8eb9SGleb Smirnoff 	 *   (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
21203b3a8eb9SGleb Smirnoff 	 *                             link, scheduler template, flowset
21213b3a8eb9SGleb Smirnoff 	 *                             integrated in scheduler and header
21223b3a8eb9SGleb Smirnoff 	 *                             for flowset list
21233b3a8eb9SGleb Smirnoff 	 *   (NSI)*(dn_flow) all scheduler instance (includes
21243b3a8eb9SGleb Smirnoff 	 *                              the queue instance)
21253b3a8eb9SGleb Smirnoff 	 * - ipfw sched show
21263b3a8eb9SGleb Smirnoff 	 *   (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
21273b3a8eb9SGleb Smirnoff 	 *                             link, scheduler template, flowset
21283b3a8eb9SGleb Smirnoff 	 *                             integrated in scheduler and header
21293b3a8eb9SGleb Smirnoff 	 *                             for flowset list
21303b3a8eb9SGleb Smirnoff 	 *   (NSI * dn_flow) all scheduler instances
21313b3a8eb9SGleb Smirnoff 	 *   (NF * sizeof(uint_32)) space for flowset list linked to scheduler
21323b3a8eb9SGleb Smirnoff 	 *   (NQ * dn_queue) all queue [XXXfor now not listed]
21333b3a8eb9SGleb Smirnoff 	 * - ipfw queue show
21343b3a8eb9SGleb Smirnoff 	 *   (NF * dn_fs) all flowset
21353b3a8eb9SGleb Smirnoff 	 *   (NQ * dn_queue) all queues
21363b3a8eb9SGleb Smirnoff 	 */
21373b3a8eb9SGleb Smirnoff 	switch (cmd->subtype) {
21383b3a8eb9SGleb Smirnoff 	default:
21393b3a8eb9SGleb Smirnoff 		return -1;
21403b3a8eb9SGleb Smirnoff 	/* XXX where do LINK and SCH differ ? */
21413b3a8eb9SGleb Smirnoff 	/* 'ipfw sched show' could list all queues associated to
21423b3a8eb9SGleb Smirnoff 	 * a scheduler. This feature for now is disabled
21433b3a8eb9SGleb Smirnoff 	 */
21443b3a8eb9SGleb Smirnoff 	case DN_LINK:	/* pipe show */
21453b3a8eb9SGleb Smirnoff 		x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
21463b3a8eb9SGleb Smirnoff 		need += dn_cfg.schk_count *
21473b3a8eb9SGleb Smirnoff 			(sizeof(struct dn_fs) + profile_size) / 2;
21483b3a8eb9SGleb Smirnoff 		need += dn_cfg.fsk_count * sizeof(uint32_t);
21493b3a8eb9SGleb Smirnoff 		break;
21503b3a8eb9SGleb Smirnoff 	case DN_SCH:	/* sched show */
21513b3a8eb9SGleb Smirnoff 		need += dn_cfg.schk_count *
21523b3a8eb9SGleb Smirnoff 			(sizeof(struct dn_fs) + profile_size) / 2;
21533b3a8eb9SGleb Smirnoff 		need += dn_cfg.fsk_count * sizeof(uint32_t);
21543b3a8eb9SGleb Smirnoff 		x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
21553b3a8eb9SGleb Smirnoff 		break;
21563b3a8eb9SGleb Smirnoff 	case DN_FS:	/* queue show */
21573b3a8eb9SGleb Smirnoff 		x = DN_C_FS | DN_C_QUEUE;
21583b3a8eb9SGleb Smirnoff 		break;
21593b3a8eb9SGleb Smirnoff 	case DN_GET_COMPAT:	/* compatibility mode */
21603b3a8eb9SGleb Smirnoff 		need =  dn_compat_calc_size();
21613b3a8eb9SGleb Smirnoff 		break;
21623b3a8eb9SGleb Smirnoff 	}
21633b3a8eb9SGleb Smirnoff 	a->flags = x;
21643b3a8eb9SGleb Smirnoff 	if (x & DN_C_SCH) {
21653b3a8eb9SGleb Smirnoff 		need += dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
21663b3a8eb9SGleb Smirnoff 		/* NOT also, each fs might be attached to a sched */
21673b3a8eb9SGleb Smirnoff 		need += dn_cfg.schk_count * sizeof(struct dn_id) / 2;
21683b3a8eb9SGleb Smirnoff 	}
21693b3a8eb9SGleb Smirnoff 	if (x & DN_C_FS)
21703b3a8eb9SGleb Smirnoff 		need += dn_cfg.fsk_count * sizeof(struct dn_fs);
21713b3a8eb9SGleb Smirnoff 	if (x & DN_C_LINK) {
21723b3a8eb9SGleb Smirnoff 		need += dn_cfg.schk_count * sizeof(struct dn_link) / 2;
21733b3a8eb9SGleb Smirnoff 	}
21743b3a8eb9SGleb Smirnoff 	/*
21753b3a8eb9SGleb Smirnoff 	 * When exporting a queue to userland, only pass up the
21763b3a8eb9SGleb Smirnoff 	 * struct dn_flow, which is the only visible part.
21773b3a8eb9SGleb Smirnoff 	 */
21783b3a8eb9SGleb Smirnoff 
21793b3a8eb9SGleb Smirnoff 	if (x & DN_C_QUEUE)
21803b3a8eb9SGleb Smirnoff 		need += dn_cfg.queue_count * sizeof(struct dn_flow);
21813b3a8eb9SGleb Smirnoff 	if (x & DN_C_FLOW)
21823b3a8eb9SGleb Smirnoff 		need += dn_cfg.si_count * (sizeof(struct dn_flow));
21833b3a8eb9SGleb Smirnoff 	return need;
21843b3a8eb9SGleb Smirnoff }
21853b3a8eb9SGleb Smirnoff 
21863b3a8eb9SGleb Smirnoff /*
21873b3a8eb9SGleb Smirnoff  * If compat != NULL dummynet_get is called in compatibility mode.
21883b3a8eb9SGleb Smirnoff  * *compat will be the pointer to the buffer to pass to ipfw
21893b3a8eb9SGleb Smirnoff  */
21903b3a8eb9SGleb Smirnoff int
21913b3a8eb9SGleb Smirnoff dummynet_get(struct sockopt *sopt, void **compat)
21923b3a8eb9SGleb Smirnoff {
21933b3a8eb9SGleb Smirnoff 	int have, i, need, error;
21943b3a8eb9SGleb Smirnoff 	char *start = NULL, *buf;
21953b3a8eb9SGleb Smirnoff 	size_t sopt_valsize;
21963b3a8eb9SGleb Smirnoff 	struct dn_id *cmd;
21973b3a8eb9SGleb Smirnoff 	struct copy_args a;
21983b3a8eb9SGleb Smirnoff 	struct copy_range r;
21993b3a8eb9SGleb Smirnoff 	int l = sizeof(struct dn_id);
22003b3a8eb9SGleb Smirnoff 
22013b3a8eb9SGleb Smirnoff 	bzero(&a, sizeof(a));
22023b3a8eb9SGleb Smirnoff 	bzero(&r, sizeof(r));
22033b3a8eb9SGleb Smirnoff 
22043b3a8eb9SGleb Smirnoff 	/* save and restore original sopt_valsize around copyin */
22053b3a8eb9SGleb Smirnoff 	sopt_valsize = sopt->sopt_valsize;
22063b3a8eb9SGleb Smirnoff 
22073b3a8eb9SGleb Smirnoff 	cmd = &r.o;
22083b3a8eb9SGleb Smirnoff 
22093b3a8eb9SGleb Smirnoff 	if (!compat) {
22103b3a8eb9SGleb Smirnoff 		/* copy at least an oid, and possibly a full object */
22113b3a8eb9SGleb Smirnoff 		error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
22123b3a8eb9SGleb Smirnoff 		sopt->sopt_valsize = sopt_valsize;
22133b3a8eb9SGleb Smirnoff 		if (error)
22143b3a8eb9SGleb Smirnoff 			goto done;
22153b3a8eb9SGleb Smirnoff 		l = cmd->len;
22163b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL
22173b3a8eb9SGleb Smirnoff 		/* sysctl emulation. */
22183b3a8eb9SGleb Smirnoff 		if (cmd->type == DN_SYSCTL_GET)
22193b3a8eb9SGleb Smirnoff 			return kesysctl_emu_get(sopt);
22203b3a8eb9SGleb Smirnoff #endif
22213b3a8eb9SGleb Smirnoff 		if (l > sizeof(r)) {
22223b3a8eb9SGleb Smirnoff 			/* request larger than default, allocate buffer */
22233b3a8eb9SGleb Smirnoff 			cmd = malloc(l,  M_DUMMYNET, M_WAITOK);
22243b3a8eb9SGleb Smirnoff 			error = sooptcopyin(sopt, cmd, l, l);
22253b3a8eb9SGleb Smirnoff 			sopt->sopt_valsize = sopt_valsize;
22263b3a8eb9SGleb Smirnoff 			if (error)
22273b3a8eb9SGleb Smirnoff 				goto done;
22283b3a8eb9SGleb Smirnoff 		}
22293b3a8eb9SGleb Smirnoff 	} else { /* compatibility */
22303b3a8eb9SGleb Smirnoff 		error = 0;
22313b3a8eb9SGleb Smirnoff 		cmd->type = DN_CMD_GET;
22323b3a8eb9SGleb Smirnoff 		cmd->len = sizeof(struct dn_id);
22333b3a8eb9SGleb Smirnoff 		cmd->subtype = DN_GET_COMPAT;
22343b3a8eb9SGleb Smirnoff 		// cmd->id = sopt_valsize;
22353b3a8eb9SGleb Smirnoff 		D("compatibility mode");
22363b3a8eb9SGleb Smirnoff 	}
223791336b40SDon Lewis 
223891336b40SDon Lewis #ifdef NEW_AQM
223991336b40SDon Lewis 	/* get AQM params */
224091336b40SDon Lewis 	if(cmd->subtype == DN_AQM_PARAMS) {
224191336b40SDon Lewis 		error = get_aqm_parms(sopt);
224291336b40SDon Lewis 		goto done;
224391336b40SDon Lewis 	/* get Scheduler params */
224491336b40SDon Lewis 	} else if (cmd->subtype == DN_SCH_PARAMS) {
224591336b40SDon Lewis 		error = get_sched_parms(sopt);
224691336b40SDon Lewis 		goto done;
224791336b40SDon Lewis 	}
224891336b40SDon Lewis #endif
224991336b40SDon Lewis 
22503b3a8eb9SGleb Smirnoff 	a.extra = (struct copy_range *)cmd;
22513b3a8eb9SGleb Smirnoff 	if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
22523b3a8eb9SGleb Smirnoff 		uint32_t *rp = (uint32_t *)(cmd + 1);
22533b3a8eb9SGleb Smirnoff 		cmd->len += 2* sizeof(uint32_t);
22543b3a8eb9SGleb Smirnoff 		rp[0] = 1;
22553b3a8eb9SGleb Smirnoff 		rp[1] = DN_MAX_ID - 1;
22563b3a8eb9SGleb Smirnoff 		if (cmd->subtype == DN_LINK) {
22573b3a8eb9SGleb Smirnoff 			rp[0] += DN_MAX_ID;
22583b3a8eb9SGleb Smirnoff 			rp[1] += DN_MAX_ID;
22593b3a8eb9SGleb Smirnoff 		}
22603b3a8eb9SGleb Smirnoff 	}
22613b3a8eb9SGleb Smirnoff 	/* Count space (under lock) and allocate (outside lock).
22623b3a8eb9SGleb Smirnoff 	 * Exit with lock held if we manage to get enough buffer.
22633b3a8eb9SGleb Smirnoff 	 * Try a few times then give up.
22643b3a8eb9SGleb Smirnoff 	 */
22653b3a8eb9SGleb Smirnoff 	for (have = 0, i = 0; i < 10; i++) {
22663b3a8eb9SGleb Smirnoff 		DN_BH_WLOCK();
22673b3a8eb9SGleb Smirnoff 		need = compute_space(cmd, &a);
22683b3a8eb9SGleb Smirnoff 
22693b3a8eb9SGleb Smirnoff 		/* if there is a range, ignore value from compute_space() */
22703b3a8eb9SGleb Smirnoff 		if (l > sizeof(*cmd))
22713b3a8eb9SGleb Smirnoff 			need = sopt_valsize - sizeof(*cmd);
22723b3a8eb9SGleb Smirnoff 
22733b3a8eb9SGleb Smirnoff 		if (need < 0) {
22743b3a8eb9SGleb Smirnoff 			DN_BH_WUNLOCK();
22753b3a8eb9SGleb Smirnoff 			error = EINVAL;
22763b3a8eb9SGleb Smirnoff 			goto done;
22773b3a8eb9SGleb Smirnoff 		}
22783b3a8eb9SGleb Smirnoff 		need += sizeof(*cmd);
22793b3a8eb9SGleb Smirnoff 		cmd->id = need;
22803b3a8eb9SGleb Smirnoff 		if (have >= need)
22813b3a8eb9SGleb Smirnoff 			break;
22823b3a8eb9SGleb Smirnoff 
22833b3a8eb9SGleb Smirnoff 		DN_BH_WUNLOCK();
22843b3a8eb9SGleb Smirnoff 		if (start)
22853b3a8eb9SGleb Smirnoff 			free(start, M_DUMMYNET);
22863b3a8eb9SGleb Smirnoff 		start = NULL;
22873b3a8eb9SGleb Smirnoff 		if (need > sopt_valsize)
22883b3a8eb9SGleb Smirnoff 			break;
22893b3a8eb9SGleb Smirnoff 
22903b3a8eb9SGleb Smirnoff 		have = need;
22913b3a8eb9SGleb Smirnoff 		start = malloc(have, M_DUMMYNET, M_WAITOK | M_ZERO);
22923b3a8eb9SGleb Smirnoff 	}
22933b3a8eb9SGleb Smirnoff 
22943b3a8eb9SGleb Smirnoff 	if (start == NULL) {
22953b3a8eb9SGleb Smirnoff 		if (compat) {
22963b3a8eb9SGleb Smirnoff 			*compat = NULL;
22973b3a8eb9SGleb Smirnoff 			error =  1; // XXX
22983b3a8eb9SGleb Smirnoff 		} else {
22993b3a8eb9SGleb Smirnoff 			error = sooptcopyout(sopt, cmd, sizeof(*cmd));
23003b3a8eb9SGleb Smirnoff 		}
23013b3a8eb9SGleb Smirnoff 		goto done;
23023b3a8eb9SGleb Smirnoff 	}
23033b3a8eb9SGleb Smirnoff 	ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
23043b3a8eb9SGleb Smirnoff 		"%d:%d si %d, %d:%d queues %d",
23053b3a8eb9SGleb Smirnoff 		dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
23063b3a8eb9SGleb Smirnoff 		dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
23073b3a8eb9SGleb Smirnoff 		dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
23083b3a8eb9SGleb Smirnoff 		dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
23093b3a8eb9SGleb Smirnoff 		dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
23103b3a8eb9SGleb Smirnoff 	sopt->sopt_valsize = sopt_valsize;
23113b3a8eb9SGleb Smirnoff 	a.type = cmd->subtype;
23123b3a8eb9SGleb Smirnoff 
23133b3a8eb9SGleb Smirnoff 	if (compat == NULL) {
2314*0ac43d97SMarius Strobl 		memcpy(start, cmd, sizeof(*cmd));
23153b3a8eb9SGleb Smirnoff 		((struct dn_id*)(start))->len = sizeof(struct dn_id);
23163b3a8eb9SGleb Smirnoff 		buf = start + sizeof(*cmd);
23173b3a8eb9SGleb Smirnoff 	} else
23183b3a8eb9SGleb Smirnoff 		buf = start;
23193b3a8eb9SGleb Smirnoff 	a.start = &buf;
23203b3a8eb9SGleb Smirnoff 	a.end = start + have;
23213b3a8eb9SGleb Smirnoff 	/* start copying other objects */
23223b3a8eb9SGleb Smirnoff 	if (compat) {
23233b3a8eb9SGleb Smirnoff 		a.type = DN_COMPAT_PIPE;
23243b3a8eb9SGleb Smirnoff 		dn_ht_scan(dn_cfg.schedhash, copy_data_helper_compat, &a);
23253b3a8eb9SGleb Smirnoff 		a.type = DN_COMPAT_QUEUE;
23263b3a8eb9SGleb Smirnoff 		dn_ht_scan(dn_cfg.fshash, copy_data_helper_compat, &a);
23273b3a8eb9SGleb Smirnoff 	} else if (a.type == DN_FS) {
23283b3a8eb9SGleb Smirnoff 		dn_ht_scan(dn_cfg.fshash, copy_data_helper, &a);
23293b3a8eb9SGleb Smirnoff 	} else {
23303b3a8eb9SGleb Smirnoff 		dn_ht_scan(dn_cfg.schedhash, copy_data_helper, &a);
23313b3a8eb9SGleb Smirnoff 	}
23323b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
23333b3a8eb9SGleb Smirnoff 
23343b3a8eb9SGleb Smirnoff 	if (compat) {
23353b3a8eb9SGleb Smirnoff 		*compat = start;
23363b3a8eb9SGleb Smirnoff 		sopt->sopt_valsize = buf - start;
23373b3a8eb9SGleb Smirnoff 		/* free() is done by ip_dummynet_compat() */
23383b3a8eb9SGleb Smirnoff 		start = NULL; //XXX hack
23393b3a8eb9SGleb Smirnoff 	} else {
23403b3a8eb9SGleb Smirnoff 		error = sooptcopyout(sopt, start, buf - start);
23413b3a8eb9SGleb Smirnoff 	}
23423b3a8eb9SGleb Smirnoff done:
23433b3a8eb9SGleb Smirnoff 	if (cmd && cmd != &r.o)
23443b3a8eb9SGleb Smirnoff 		free(cmd, M_DUMMYNET);
23453b3a8eb9SGleb Smirnoff 	if (start)
23463b3a8eb9SGleb Smirnoff 		free(start, M_DUMMYNET);
23473b3a8eb9SGleb Smirnoff 	return error;
23483b3a8eb9SGleb Smirnoff }
23493b3a8eb9SGleb Smirnoff 
23503b3a8eb9SGleb Smirnoff /* Callback called on scheduler instance to delete it if idle */
23513b3a8eb9SGleb Smirnoff static int
23523b3a8eb9SGleb Smirnoff drain_scheduler_cb(void *_si, void *arg)
23533b3a8eb9SGleb Smirnoff {
23543b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si = _si;
23553b3a8eb9SGleb Smirnoff 
23563b3a8eb9SGleb Smirnoff 	if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
23573b3a8eb9SGleb Smirnoff 		return 0;
23583b3a8eb9SGleb Smirnoff 
23593b3a8eb9SGleb Smirnoff 	if (si->sched->fp->flags & DN_MULTIQUEUE) {
23603b3a8eb9SGleb Smirnoff 		if (si->q_count == 0)
23613b3a8eb9SGleb Smirnoff 			return si_destroy(si, NULL);
23623b3a8eb9SGleb Smirnoff 		else
23633b3a8eb9SGleb Smirnoff 			return 0;
23643b3a8eb9SGleb Smirnoff 	} else { /* !DN_MULTIQUEUE */
23653b3a8eb9SGleb Smirnoff 		if ((si+1)->ni.length == 0)
23663b3a8eb9SGleb Smirnoff 			return si_destroy(si, NULL);
23673b3a8eb9SGleb Smirnoff 		else
23683b3a8eb9SGleb Smirnoff 			return 0;
23693b3a8eb9SGleb Smirnoff 	}
23703b3a8eb9SGleb Smirnoff 	return 0; /* unreachable */
23713b3a8eb9SGleb Smirnoff }
23723b3a8eb9SGleb Smirnoff 
23733b3a8eb9SGleb Smirnoff /* Callback called on scheduler to check if it has instances */
23743b3a8eb9SGleb Smirnoff static int
23753b3a8eb9SGleb Smirnoff drain_scheduler_sch_cb(void *_s, void *arg)
23763b3a8eb9SGleb Smirnoff {
23773b3a8eb9SGleb Smirnoff 	struct dn_schk *s = _s;
23783b3a8eb9SGleb Smirnoff 
23793b3a8eb9SGleb Smirnoff 	if (s->sch.flags & DN_HAVE_MASK) {
23803b3a8eb9SGleb Smirnoff 		dn_ht_scan_bucket(s->siht, &s->drain_bucket,
23813b3a8eb9SGleb Smirnoff 				drain_scheduler_cb, NULL);
23823b3a8eb9SGleb Smirnoff 		s->drain_bucket++;
23833b3a8eb9SGleb Smirnoff 	} else {
23843b3a8eb9SGleb Smirnoff 		if (s->siht) {
23853b3a8eb9SGleb Smirnoff 			if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
23863b3a8eb9SGleb Smirnoff 				s->siht = NULL;
23873b3a8eb9SGleb Smirnoff 		}
23883b3a8eb9SGleb Smirnoff 	}
23893b3a8eb9SGleb Smirnoff 	return 0;
23903b3a8eb9SGleb Smirnoff }
23913b3a8eb9SGleb Smirnoff 
23923b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of scheduler */
23933b3a8eb9SGleb Smirnoff void
23943b3a8eb9SGleb Smirnoff dn_drain_scheduler(void)
23953b3a8eb9SGleb Smirnoff {
23963b3a8eb9SGleb Smirnoff 	dn_ht_scan_bucket(dn_cfg.schedhash, &dn_cfg.drain_sch,
23973b3a8eb9SGleb Smirnoff 			   drain_scheduler_sch_cb, NULL);
23983b3a8eb9SGleb Smirnoff 	dn_cfg.drain_sch++;
23993b3a8eb9SGleb Smirnoff }
24003b3a8eb9SGleb Smirnoff 
24013b3a8eb9SGleb Smirnoff /* Callback called on queue to delete if it is idle */
24023b3a8eb9SGleb Smirnoff static int
24033b3a8eb9SGleb Smirnoff drain_queue_cb(void *_q, void *arg)
24043b3a8eb9SGleb Smirnoff {
24053b3a8eb9SGleb Smirnoff 	struct dn_queue *q = _q;
24063b3a8eb9SGleb Smirnoff 
24073b3a8eb9SGleb Smirnoff 	if (q->ni.length == 0) {
24083b3a8eb9SGleb Smirnoff 		dn_delete_queue(q, DN_DESTROY);
24093b3a8eb9SGleb Smirnoff 		return DNHT_SCAN_DEL; /* queue is deleted */
24103b3a8eb9SGleb Smirnoff 	}
24113b3a8eb9SGleb Smirnoff 
24123b3a8eb9SGleb Smirnoff 	return 0; /* queue isn't deleted */
24133b3a8eb9SGleb Smirnoff }
24143b3a8eb9SGleb Smirnoff 
24153b3a8eb9SGleb Smirnoff /* Callback called on flowset used to check if it has queues */
24163b3a8eb9SGleb Smirnoff static int
24173b3a8eb9SGleb Smirnoff drain_queue_fs_cb(void *_fs, void *arg)
24183b3a8eb9SGleb Smirnoff {
24193b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = _fs;
24203b3a8eb9SGleb Smirnoff 
24213b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QHT_HASH) {
24223b3a8eb9SGleb Smirnoff 		/* Flowset has a hash table for queues */
24233b3a8eb9SGleb Smirnoff 		dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
24243b3a8eb9SGleb Smirnoff 				drain_queue_cb, NULL);
24253b3a8eb9SGleb Smirnoff 		fs->drain_bucket++;
24263b3a8eb9SGleb Smirnoff 	} else {
24273b3a8eb9SGleb Smirnoff 		/* No hash table for this flowset, null the pointer
24283b3a8eb9SGleb Smirnoff 		 * if the queue is deleted
24293b3a8eb9SGleb Smirnoff 		 */
24303b3a8eb9SGleb Smirnoff 		if (fs->qht) {
24313b3a8eb9SGleb Smirnoff 			if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
24323b3a8eb9SGleb Smirnoff 				fs->qht = NULL;
24333b3a8eb9SGleb Smirnoff 		}
24343b3a8eb9SGleb Smirnoff 	}
24353b3a8eb9SGleb Smirnoff 	return 0;
24363b3a8eb9SGleb Smirnoff }
24373b3a8eb9SGleb Smirnoff 
24383b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of queue */
24393b3a8eb9SGleb Smirnoff void
24403b3a8eb9SGleb Smirnoff dn_drain_queue(void)
24413b3a8eb9SGleb Smirnoff {
24423b3a8eb9SGleb Smirnoff 	/* scan a bucket of flowset */
24433b3a8eb9SGleb Smirnoff 	dn_ht_scan_bucket(dn_cfg.fshash, &dn_cfg.drain_fs,
24443b3a8eb9SGleb Smirnoff                                drain_queue_fs_cb, NULL);
24453b3a8eb9SGleb Smirnoff 	dn_cfg.drain_fs++;
24463b3a8eb9SGleb Smirnoff }
24473b3a8eb9SGleb Smirnoff 
24483b3a8eb9SGleb Smirnoff /*
24493b3a8eb9SGleb Smirnoff  * Handler for the various dummynet socket options
24503b3a8eb9SGleb Smirnoff  */
24513b3a8eb9SGleb Smirnoff static int
24523b3a8eb9SGleb Smirnoff ip_dn_ctl(struct sockopt *sopt)
24533b3a8eb9SGleb Smirnoff {
24543b3a8eb9SGleb Smirnoff 	void *p = NULL;
24553b3a8eb9SGleb Smirnoff 	int error, l;
24563b3a8eb9SGleb Smirnoff 
24573b3a8eb9SGleb Smirnoff 	error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
24583b3a8eb9SGleb Smirnoff 	if (error)
24593b3a8eb9SGleb Smirnoff 		return (error);
24603b3a8eb9SGleb Smirnoff 
24613b3a8eb9SGleb Smirnoff 	/* Disallow sets in really-really secure mode. */
24623b3a8eb9SGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET) {
24633b3a8eb9SGleb Smirnoff 		error =  securelevel_ge(sopt->sopt_td->td_ucred, 3);
24643b3a8eb9SGleb Smirnoff 		if (error)
24653b3a8eb9SGleb Smirnoff 			return (error);
24663b3a8eb9SGleb Smirnoff 	}
24673b3a8eb9SGleb Smirnoff 
24683b3a8eb9SGleb Smirnoff 	switch (sopt->sopt_name) {
24693b3a8eb9SGleb Smirnoff 	default :
24703b3a8eb9SGleb Smirnoff 		D("dummynet: unknown option %d", sopt->sopt_name);
24713b3a8eb9SGleb Smirnoff 		error = EINVAL;
24723b3a8eb9SGleb Smirnoff 		break;
24733b3a8eb9SGleb Smirnoff 
24743b3a8eb9SGleb Smirnoff 	case IP_DUMMYNET_FLUSH:
24753b3a8eb9SGleb Smirnoff 	case IP_DUMMYNET_CONFIGURE:
24763b3a8eb9SGleb Smirnoff 	case IP_DUMMYNET_DEL:	/* remove a pipe or queue */
24773b3a8eb9SGleb Smirnoff 	case IP_DUMMYNET_GET:
24783b3a8eb9SGleb Smirnoff 		D("dummynet: compat option %d", sopt->sopt_name);
24793b3a8eb9SGleb Smirnoff 		error = ip_dummynet_compat(sopt);
24803b3a8eb9SGleb Smirnoff 		break;
24813b3a8eb9SGleb Smirnoff 
24823b3a8eb9SGleb Smirnoff 	case IP_DUMMYNET3 :
24833b3a8eb9SGleb Smirnoff 		if (sopt->sopt_dir == SOPT_GET) {
24843b3a8eb9SGleb Smirnoff 			error = dummynet_get(sopt, NULL);
24853b3a8eb9SGleb Smirnoff 			break;
24863b3a8eb9SGleb Smirnoff 		}
24873b3a8eb9SGleb Smirnoff 		l = sopt->sopt_valsize;
24883b3a8eb9SGleb Smirnoff 		if (l < sizeof(struct dn_id) || l > 12000) {
24893b3a8eb9SGleb Smirnoff 			D("argument len %d invalid", l);
24903b3a8eb9SGleb Smirnoff 			break;
24913b3a8eb9SGleb Smirnoff 		}
24923b3a8eb9SGleb Smirnoff 		p = malloc(l, M_TEMP, M_WAITOK); // XXX can it fail ?
24933b3a8eb9SGleb Smirnoff 		error = sooptcopyin(sopt, p, l, l);
24943b3a8eb9SGleb Smirnoff 		if (error)
24953b3a8eb9SGleb Smirnoff 			break ;
24963b3a8eb9SGleb Smirnoff 		error = do_config(p, l);
24973b3a8eb9SGleb Smirnoff 		break;
24983b3a8eb9SGleb Smirnoff 	}
24993b3a8eb9SGleb Smirnoff 
25003b3a8eb9SGleb Smirnoff 	if (p != NULL)
25013b3a8eb9SGleb Smirnoff 		free(p, M_TEMP);
25023b3a8eb9SGleb Smirnoff 
25033b3a8eb9SGleb Smirnoff 	return error ;
25043b3a8eb9SGleb Smirnoff }
25053b3a8eb9SGleb Smirnoff 
25063b3a8eb9SGleb Smirnoff 
25073b3a8eb9SGleb Smirnoff static void
25083b3a8eb9SGleb Smirnoff ip_dn_init(void)
25093b3a8eb9SGleb Smirnoff {
25103b3a8eb9SGleb Smirnoff 	if (dn_cfg.init_done)
25113b3a8eb9SGleb Smirnoff 		return;
25123b3a8eb9SGleb Smirnoff 	printf("DUMMYNET %p with IPv6 initialized (100409)\n", curvnet);
25133b3a8eb9SGleb Smirnoff 	dn_cfg.init_done = 1;
25143b3a8eb9SGleb Smirnoff 	/* Set defaults here. MSVC does not accept initializers,
25153b3a8eb9SGleb Smirnoff 	 * and this is also useful for vimages
25163b3a8eb9SGleb Smirnoff 	 */
25173b3a8eb9SGleb Smirnoff 	/* queue limits */
25183b3a8eb9SGleb Smirnoff 	dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
25193b3a8eb9SGleb Smirnoff 	dn_cfg.byte_limit = 1024 * 1024;
25203b3a8eb9SGleb Smirnoff 	dn_cfg.expire = 1;
25213b3a8eb9SGleb Smirnoff 
25223b3a8eb9SGleb Smirnoff 	/* RED parameters */
25233b3a8eb9SGleb Smirnoff 	dn_cfg.red_lookup_depth = 256;	/* default lookup table depth */
25243b3a8eb9SGleb Smirnoff 	dn_cfg.red_avg_pkt_size = 512;	/* default medium packet size */
25253b3a8eb9SGleb Smirnoff 	dn_cfg.red_max_pkt_size = 1500;	/* default max packet size */
25263b3a8eb9SGleb Smirnoff 
25273b3a8eb9SGleb Smirnoff 	/* hash tables */
25283b3a8eb9SGleb Smirnoff 	dn_cfg.max_hash_size = 65536;	/* max in the hash tables */
25293b3a8eb9SGleb Smirnoff 	dn_cfg.hash_size = 64;		/* default hash size */
25303b3a8eb9SGleb Smirnoff 
25313b3a8eb9SGleb Smirnoff 	/* create hash tables for schedulers and flowsets.
25323b3a8eb9SGleb Smirnoff 	 * In both we search by key and by pointer.
25333b3a8eb9SGleb Smirnoff 	 */
25343b3a8eb9SGleb Smirnoff 	dn_cfg.schedhash = dn_ht_init(NULL, dn_cfg.hash_size,
25353b3a8eb9SGleb Smirnoff 		offsetof(struct dn_schk, schk_next),
25363b3a8eb9SGleb Smirnoff 		schk_hash, schk_match, schk_new);
25373b3a8eb9SGleb Smirnoff 	dn_cfg.fshash = dn_ht_init(NULL, dn_cfg.hash_size,
25383b3a8eb9SGleb Smirnoff 		offsetof(struct dn_fsk, fsk_next),
25393b3a8eb9SGleb Smirnoff 		fsk_hash, fsk_match, fsk_new);
25403b3a8eb9SGleb Smirnoff 
25413b3a8eb9SGleb Smirnoff 	/* bucket index to drain object */
25423b3a8eb9SGleb Smirnoff 	dn_cfg.drain_fs = 0;
25433b3a8eb9SGleb Smirnoff 	dn_cfg.drain_sch = 0;
25443b3a8eb9SGleb Smirnoff 
25453b3a8eb9SGleb Smirnoff 	heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id));
25463b3a8eb9SGleb Smirnoff 	SLIST_INIT(&dn_cfg.fsu);
25473b3a8eb9SGleb Smirnoff 	SLIST_INIT(&dn_cfg.schedlist);
25483b3a8eb9SGleb Smirnoff 
25493b3a8eb9SGleb Smirnoff 	DN_LOCK_INIT();
25503b3a8eb9SGleb Smirnoff 
25513b3a8eb9SGleb Smirnoff 	TASK_INIT(&dn_task, 0, dummynet_task, curvnet);
25525f4fc3dbSAlexander Motin 	dn_tq = taskqueue_create_fast("dummynet", M_WAITOK,
25533b3a8eb9SGleb Smirnoff 	    taskqueue_thread_enqueue, &dn_tq);
25543b3a8eb9SGleb Smirnoff 	taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
25553b3a8eb9SGleb Smirnoff 
2556fd90e2edSJung-uk Kim 	callout_init(&dn_timeout, 1);
25575f4fc3dbSAlexander Motin 	dn_reschedule();
25583b3a8eb9SGleb Smirnoff 
25593b3a8eb9SGleb Smirnoff 	/* Initialize curr_time adjustment mechanics. */
25603b3a8eb9SGleb Smirnoff 	getmicrouptime(&dn_cfg.prev_t);
25613b3a8eb9SGleb Smirnoff }
25623b3a8eb9SGleb Smirnoff 
25633b3a8eb9SGleb Smirnoff static void
25643b3a8eb9SGleb Smirnoff ip_dn_destroy(int last)
25653b3a8eb9SGleb Smirnoff {
25663b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
2567c8cfbc06SHans Petter Selasky 	/* ensure no more callouts are started */
2568c8cfbc06SHans Petter Selasky 	dn_gone = 1;
2569c8cfbc06SHans Petter Selasky 
2570c8cfbc06SHans Petter Selasky 	/* check for last */
25713b3a8eb9SGleb Smirnoff 	if (last) {
25723b3a8eb9SGleb Smirnoff 		ND("removing last instance\n");
25733b3a8eb9SGleb Smirnoff 		ip_dn_ctl_ptr = NULL;
25743b3a8eb9SGleb Smirnoff 		ip_dn_io_ptr = NULL;
25753b3a8eb9SGleb Smirnoff 	}
25763b3a8eb9SGleb Smirnoff 
25773b3a8eb9SGleb Smirnoff 	dummynet_flush();
25783b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
2579c8cfbc06SHans Petter Selasky 
2580c8cfbc06SHans Petter Selasky 	callout_drain(&dn_timeout);
25813b3a8eb9SGleb Smirnoff 	taskqueue_drain(dn_tq, &dn_task);
25823b3a8eb9SGleb Smirnoff 	taskqueue_free(dn_tq);
25833b3a8eb9SGleb Smirnoff 
25843b3a8eb9SGleb Smirnoff 	dn_ht_free(dn_cfg.schedhash, 0);
25853b3a8eb9SGleb Smirnoff 	dn_ht_free(dn_cfg.fshash, 0);
25863b3a8eb9SGleb Smirnoff 	heap_free(&dn_cfg.evheap);
25873b3a8eb9SGleb Smirnoff 
25883b3a8eb9SGleb Smirnoff 	DN_LOCK_DESTROY();
25893b3a8eb9SGleb Smirnoff }
25903b3a8eb9SGleb Smirnoff 
25913b3a8eb9SGleb Smirnoff static int
25923b3a8eb9SGleb Smirnoff dummynet_modevent(module_t mod, int type, void *data)
25933b3a8eb9SGleb Smirnoff {
25943b3a8eb9SGleb Smirnoff 
25953b3a8eb9SGleb Smirnoff 	if (type == MOD_LOAD) {
25963b3a8eb9SGleb Smirnoff 		if (ip_dn_io_ptr) {
25973b3a8eb9SGleb Smirnoff 			printf("DUMMYNET already loaded\n");
25983b3a8eb9SGleb Smirnoff 			return EEXIST ;
25993b3a8eb9SGleb Smirnoff 		}
26003b3a8eb9SGleb Smirnoff 		ip_dn_init();
26013b3a8eb9SGleb Smirnoff 		ip_dn_ctl_ptr = ip_dn_ctl;
26023b3a8eb9SGleb Smirnoff 		ip_dn_io_ptr = dummynet_io;
26033b3a8eb9SGleb Smirnoff 		return 0;
26043b3a8eb9SGleb Smirnoff 	} else if (type == MOD_UNLOAD) {
26053b3a8eb9SGleb Smirnoff 		ip_dn_destroy(1 /* last */);
26063b3a8eb9SGleb Smirnoff 		return 0;
26073b3a8eb9SGleb Smirnoff 	} else
26083b3a8eb9SGleb Smirnoff 		return EOPNOTSUPP;
26093b3a8eb9SGleb Smirnoff }
26103b3a8eb9SGleb Smirnoff 
26113b3a8eb9SGleb Smirnoff /* modevent helpers for the modules */
26123b3a8eb9SGleb Smirnoff static int
26133b3a8eb9SGleb Smirnoff load_dn_sched(struct dn_alg *d)
26143b3a8eb9SGleb Smirnoff {
26153b3a8eb9SGleb Smirnoff 	struct dn_alg *s;
26163b3a8eb9SGleb Smirnoff 
26173b3a8eb9SGleb Smirnoff 	if (d == NULL)
26183b3a8eb9SGleb Smirnoff 		return 1; /* error */
26193b3a8eb9SGleb Smirnoff 	ip_dn_init();	/* just in case, we need the lock */
26203b3a8eb9SGleb Smirnoff 
26213b3a8eb9SGleb Smirnoff 	/* Check that mandatory funcs exists */
26223b3a8eb9SGleb Smirnoff 	if (d->enqueue == NULL || d->dequeue == NULL) {
26233b3a8eb9SGleb Smirnoff 		D("missing enqueue or dequeue for %s", d->name);
26243b3a8eb9SGleb Smirnoff 		return 1;
26253b3a8eb9SGleb Smirnoff 	}
26263b3a8eb9SGleb Smirnoff 
26273b3a8eb9SGleb Smirnoff 	/* Search if scheduler already exists */
26283b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
26293b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(s, &dn_cfg.schedlist, next) {
26303b3a8eb9SGleb Smirnoff 		if (strcmp(s->name, d->name) == 0) {
26313b3a8eb9SGleb Smirnoff 			D("%s already loaded", d->name);
26323b3a8eb9SGleb Smirnoff 			break; /* scheduler already exists */
26333b3a8eb9SGleb Smirnoff 		}
26343b3a8eb9SGleb Smirnoff 	}
26353b3a8eb9SGleb Smirnoff 	if (s == NULL)
26363b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&dn_cfg.schedlist, d, next);
26373b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
26383b3a8eb9SGleb Smirnoff 	D("dn_sched %s %sloaded", d->name, s ? "not ":"");
26393b3a8eb9SGleb Smirnoff 	return s ? 1 : 0;
26403b3a8eb9SGleb Smirnoff }
26413b3a8eb9SGleb Smirnoff 
26423b3a8eb9SGleb Smirnoff static int
26433b3a8eb9SGleb Smirnoff unload_dn_sched(struct dn_alg *s)
26443b3a8eb9SGleb Smirnoff {
26453b3a8eb9SGleb Smirnoff 	struct dn_alg *tmp, *r;
26463b3a8eb9SGleb Smirnoff 	int err = EINVAL;
26473b3a8eb9SGleb Smirnoff 
26483b3a8eb9SGleb Smirnoff 	ND("called for %s", s->name);
26493b3a8eb9SGleb Smirnoff 
26503b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
26513b3a8eb9SGleb Smirnoff 	SLIST_FOREACH_SAFE(r, &dn_cfg.schedlist, next, tmp) {
26523b3a8eb9SGleb Smirnoff 		if (strcmp(s->name, r->name) != 0)
26533b3a8eb9SGleb Smirnoff 			continue;
26543b3a8eb9SGleb Smirnoff 		ND("ref_count = %d", r->ref_count);
26553b3a8eb9SGleb Smirnoff 		err = (r->ref_count != 0) ? EBUSY : 0;
26563b3a8eb9SGleb Smirnoff 		if (err == 0)
26573b3a8eb9SGleb Smirnoff 			SLIST_REMOVE(&dn_cfg.schedlist, r, dn_alg, next);
26583b3a8eb9SGleb Smirnoff 		break;
26593b3a8eb9SGleb Smirnoff 	}
26603b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
26613b3a8eb9SGleb Smirnoff 	D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
26623b3a8eb9SGleb Smirnoff 	return err;
26633b3a8eb9SGleb Smirnoff }
26643b3a8eb9SGleb Smirnoff 
26653b3a8eb9SGleb Smirnoff int
26663b3a8eb9SGleb Smirnoff dn_sched_modevent(module_t mod, int cmd, void *arg)
26673b3a8eb9SGleb Smirnoff {
26683b3a8eb9SGleb Smirnoff 	struct dn_alg *sch = arg;
26693b3a8eb9SGleb Smirnoff 
26703b3a8eb9SGleb Smirnoff 	if (cmd == MOD_LOAD)
26713b3a8eb9SGleb Smirnoff 		return load_dn_sched(sch);
26723b3a8eb9SGleb Smirnoff 	else if (cmd == MOD_UNLOAD)
26733b3a8eb9SGleb Smirnoff 		return unload_dn_sched(sch);
26743b3a8eb9SGleb Smirnoff 	else
26753b3a8eb9SGleb Smirnoff 		return EINVAL;
26763b3a8eb9SGleb Smirnoff }
26773b3a8eb9SGleb Smirnoff 
26783b3a8eb9SGleb Smirnoff static moduledata_t dummynet_mod = {
26793b3a8eb9SGleb Smirnoff 	"dummynet", dummynet_modevent, NULL
26803b3a8eb9SGleb Smirnoff };
26813b3a8eb9SGleb Smirnoff 
268289856f7eSBjoern A. Zeeb #define	DN_SI_SUB	SI_SUB_PROTO_FIREWALL
26833b3a8eb9SGleb Smirnoff #define	DN_MODEV_ORD	(SI_ORDER_ANY - 128) /* after ipfw */
26843b3a8eb9SGleb Smirnoff DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
2685f9ab623bSAlexander V. Chernikov MODULE_DEPEND(dummynet, ipfw, 3, 3, 3);
26863b3a8eb9SGleb Smirnoff MODULE_VERSION(dummynet, 3);
26873b3a8eb9SGleb Smirnoff 
26883b3a8eb9SGleb Smirnoff /*
26893b3a8eb9SGleb Smirnoff  * Starting up. Done in order after dummynet_modevent() has been called.
26903b3a8eb9SGleb Smirnoff  * VNET_SYSINIT is also called for each existing vnet and each new vnet.
26913b3a8eb9SGleb Smirnoff  */
26923b3a8eb9SGleb Smirnoff //VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_init, NULL);
26933b3a8eb9SGleb Smirnoff 
26943b3a8eb9SGleb Smirnoff /*
26953b3a8eb9SGleb Smirnoff  * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
26963b3a8eb9SGleb Smirnoff  * after dummynet_modevent() has been called. Not called on reboot.
26973b3a8eb9SGleb Smirnoff  * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
26983b3a8eb9SGleb Smirnoff  * or when the module is unloaded.
26993b3a8eb9SGleb Smirnoff  */
27003b3a8eb9SGleb Smirnoff //VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_destroy, NULL);
27013b3a8eb9SGleb Smirnoff 
270291336b40SDon Lewis #ifdef NEW_AQM
270391336b40SDon Lewis 
270491336b40SDon Lewis /* modevent helpers for the AQM modules */
270591336b40SDon Lewis static int
270691336b40SDon Lewis load_dn_aqm(struct dn_aqm *d)
270791336b40SDon Lewis {
270891336b40SDon Lewis 	struct dn_aqm *aqm=NULL;
270991336b40SDon Lewis 
271091336b40SDon Lewis 	if (d == NULL)
271191336b40SDon Lewis 		return 1; /* error */
271291336b40SDon Lewis 	ip_dn_init();	/* just in case, we need the lock */
271391336b40SDon Lewis 
271491336b40SDon Lewis 	/* Check that mandatory funcs exists */
271591336b40SDon Lewis 	if (d->enqueue == NULL || d->dequeue == NULL) {
271691336b40SDon Lewis 		D("missing enqueue or dequeue for %s", d->name);
271791336b40SDon Lewis 		return 1;
271891336b40SDon Lewis 	}
271991336b40SDon Lewis 
272091336b40SDon Lewis 	/* Search if AQM already exists */
272191336b40SDon Lewis 	DN_BH_WLOCK();
272291336b40SDon Lewis 	SLIST_FOREACH(aqm, &dn_cfg.aqmlist, next) {
272391336b40SDon Lewis 		if (strcmp(aqm->name, d->name) == 0) {
272491336b40SDon Lewis 			D("%s already loaded", d->name);
272591336b40SDon Lewis 			break; /* AQM already exists */
272691336b40SDon Lewis 		}
272791336b40SDon Lewis 	}
272891336b40SDon Lewis 	if (aqm == NULL)
272991336b40SDon Lewis 		SLIST_INSERT_HEAD(&dn_cfg.aqmlist, d, next);
273091336b40SDon Lewis 	DN_BH_WUNLOCK();
273191336b40SDon Lewis 	D("dn_aqm %s %sloaded", d->name, aqm ? "not ":"");
273291336b40SDon Lewis 	return aqm ? 1 : 0;
273391336b40SDon Lewis }
273491336b40SDon Lewis 
273591336b40SDon Lewis 
273691336b40SDon Lewis /* Callback to clean up AQM status for queues connected to a flowset
273791336b40SDon Lewis  * and then deconfigure the flowset.
273891336b40SDon Lewis  * This function is called before an AQM module is unloaded
273991336b40SDon Lewis  */
274091336b40SDon Lewis static int
274191336b40SDon Lewis fs_cleanup(void *_fs, void *arg)
274291336b40SDon Lewis {
274391336b40SDon Lewis 	struct dn_fsk *fs = _fs;
274491336b40SDon Lewis 	uint32_t type = *(uint32_t *)arg;
274591336b40SDon Lewis 
274691336b40SDon Lewis 	if (fs->aqmfp && fs->aqmfp->type == type)
274791336b40SDon Lewis 		aqm_cleanup_deconfig_fs(fs);
274891336b40SDon Lewis 
274991336b40SDon Lewis 	return 0;
275091336b40SDon Lewis }
275191336b40SDon Lewis 
275291336b40SDon Lewis static int
275391336b40SDon Lewis unload_dn_aqm(struct dn_aqm *aqm)
275491336b40SDon Lewis {
275591336b40SDon Lewis 	struct dn_aqm *tmp, *r;
275691336b40SDon Lewis 	int err = EINVAL;
275791336b40SDon Lewis 	err = 0;
275891336b40SDon Lewis 	ND("called for %s", aqm->name);
275991336b40SDon Lewis 
276091336b40SDon Lewis 	DN_BH_WLOCK();
276191336b40SDon Lewis 
276291336b40SDon Lewis 	/* clean up AQM status and deconfig flowset */
276391336b40SDon Lewis 	dn_ht_scan(dn_cfg.fshash, fs_cleanup, &aqm->type);
276491336b40SDon Lewis 
276591336b40SDon Lewis 	SLIST_FOREACH_SAFE(r, &dn_cfg.aqmlist, next, tmp) {
276691336b40SDon Lewis 		if (strcmp(aqm->name, r->name) != 0)
276791336b40SDon Lewis 			continue;
276891336b40SDon Lewis 		ND("ref_count = %d", r->ref_count);
276991336b40SDon Lewis 		err = (r->ref_count != 0 || r->cfg_ref_count != 0) ? EBUSY : 0;
277091336b40SDon Lewis 		if (err == 0)
277191336b40SDon Lewis 			SLIST_REMOVE(&dn_cfg.aqmlist, r, dn_aqm, next);
277291336b40SDon Lewis 		break;
277391336b40SDon Lewis 	}
277491336b40SDon Lewis 	DN_BH_WUNLOCK();
277591336b40SDon Lewis 	D("%s %sunloaded", aqm->name, err ? "not ":"");
277691336b40SDon Lewis 	if (err)
277791336b40SDon Lewis 		D("ref_count=%d, cfg_ref_count=%d", r->ref_count, r->cfg_ref_count);
277891336b40SDon Lewis 	return err;
277991336b40SDon Lewis }
278091336b40SDon Lewis 
278191336b40SDon Lewis int
278291336b40SDon Lewis dn_aqm_modevent(module_t mod, int cmd, void *arg)
278391336b40SDon Lewis {
278491336b40SDon Lewis 	struct dn_aqm *aqm = arg;
278591336b40SDon Lewis 
278691336b40SDon Lewis 	if (cmd == MOD_LOAD)
278791336b40SDon Lewis 		return load_dn_aqm(aqm);
278891336b40SDon Lewis 	else if (cmd == MOD_UNLOAD)
278991336b40SDon Lewis 		return unload_dn_aqm(aqm);
279091336b40SDon Lewis 	else
279191336b40SDon Lewis 		return EINVAL;
279291336b40SDon Lewis }
279391336b40SDon Lewis #endif
279491336b40SDon Lewis 
27953b3a8eb9SGleb Smirnoff /* end of file */
279691336b40SDon Lewis 
2797