13b3a8eb9SGleb Smirnoff /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
491336b40SDon Lewis * Codel/FQ_Codel and PIE/FQ-PIE Code:
591336b40SDon Lewis * Copyright (C) 2016 Centre for Advanced Internet Architectures,
691336b40SDon Lewis * Swinburne University of Technology, Melbourne, Australia.
791336b40SDon Lewis * Portions of this code were made possible in part by a gift from
891336b40SDon Lewis * The Comcast Innovation Fund.
991336b40SDon Lewis * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
1091336b40SDon Lewis *
113b3a8eb9SGleb Smirnoff * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
123b3a8eb9SGleb Smirnoff * Portions Copyright (c) 2000 Akamba Corp.
133b3a8eb9SGleb Smirnoff * All rights reserved
143b3a8eb9SGleb Smirnoff *
153b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
163b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions
173b3a8eb9SGleb Smirnoff * are met:
183b3a8eb9SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
193b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
203b3a8eb9SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
213b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
223b3a8eb9SGleb Smirnoff * documentation and/or other materials provided with the distribution.
233b3a8eb9SGleb Smirnoff *
243b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
253b3a8eb9SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
263b3a8eb9SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
273b3a8eb9SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
283b3a8eb9SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
293b3a8eb9SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
303b3a8eb9SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
313b3a8eb9SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
323b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
333b3a8eb9SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
343b3a8eb9SGleb Smirnoff * SUCH DAMAGE.
353b3a8eb9SGleb Smirnoff */
363b3a8eb9SGleb Smirnoff
373b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
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>
4551d73df1SKristof Provost #include <sys/ck.h>
463b3a8eb9SGleb Smirnoff #include <sys/systm.h>
473b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
483b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
493b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
503b3a8eb9SGleb Smirnoff #include <sys/lock.h>
513b3a8eb9SGleb Smirnoff #include <sys/module.h>
52eedc7fd9SGleb Smirnoff #include <sys/mutex.h>
533b3a8eb9SGleb Smirnoff #include <sys/priv.h>
543b3a8eb9SGleb Smirnoff #include <sys/proc.h>
553b3a8eb9SGleb Smirnoff #include <sys/rwlock.h>
563b3a8eb9SGleb Smirnoff #include <sys/socket.h>
573b3a8eb9SGleb Smirnoff #include <sys/socketvar.h>
583b3a8eb9SGleb Smirnoff #include <sys/time.h>
593b3a8eb9SGleb Smirnoff #include <sys/taskqueue.h>
603b3a8eb9SGleb Smirnoff #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
613b3a8eb9SGleb Smirnoff #include <netinet/in.h>
623b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */
633b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h>
643b3a8eb9SGleb Smirnoff #include <netinet/ip_dummynet.h>
65fe3bcfbdSTom Jones #include <net/vnet.h>
663b3a8eb9SGleb Smirnoff
673b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_fw_private.h>
683b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_heap.h>
693b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_dn_private.h>
7091336b40SDon Lewis #ifdef NEW_AQM
7191336b40SDon Lewis #include <netpfil/ipfw/dn_aqm.h>
7291336b40SDon Lewis #endif
733b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_sched.h>
743b3a8eb9SGleb Smirnoff
753b3a8eb9SGleb Smirnoff /* which objects to copy */
763b3a8eb9SGleb Smirnoff #define DN_C_LINK 0x01
773b3a8eb9SGleb Smirnoff #define DN_C_SCH 0x02
783b3a8eb9SGleb Smirnoff #define DN_C_FLOW 0x04
793b3a8eb9SGleb Smirnoff #define DN_C_FS 0x08
803b3a8eb9SGleb Smirnoff #define DN_C_QUEUE 0x10
813b3a8eb9SGleb Smirnoff
823b3a8eb9SGleb Smirnoff /* we use this argument in case of a schk_new */
833b3a8eb9SGleb Smirnoff struct schk_new_arg {
843b3a8eb9SGleb Smirnoff struct dn_alg *fp;
853b3a8eb9SGleb Smirnoff struct dn_sch *sch;
863b3a8eb9SGleb Smirnoff };
873b3a8eb9SGleb Smirnoff
883b3a8eb9SGleb Smirnoff /*---- callout hooks. ----*/
893b3a8eb9SGleb Smirnoff static struct callout dn_timeout;
90fe3bcfbdSTom Jones static int dn_tasks_started = 0;
91c8cfbc06SHans Petter Selasky static int dn_gone;
923b3a8eb9SGleb Smirnoff static struct task dn_task;
933b3a8eb9SGleb Smirnoff static struct taskqueue *dn_tq = NULL;
943b3a8eb9SGleb Smirnoff
95fe3bcfbdSTom Jones /* global scheduler list */
9651d73df1SKristof Provost struct mtx sched_mtx;
9751d73df1SKristof Provost CK_LIST_HEAD(, dn_alg) schedlist;
98fe3bcfbdSTom Jones #ifdef NEW_AQM
9951d73df1SKristof Provost CK_LIST_HEAD(, dn_aqm) aqmlist; /* list of AQMs */
100fe3bcfbdSTom Jones #endif
101fe3bcfbdSTom Jones
1023b3a8eb9SGleb Smirnoff static void
dummynet(void * arg)1033b3a8eb9SGleb Smirnoff dummynet(void *arg)
1043b3a8eb9SGleb Smirnoff {
1053b3a8eb9SGleb Smirnoff
1063b3a8eb9SGleb Smirnoff (void)arg; /* UNUSED */
107cbc4d2dbSJohn Baldwin taskqueue_enqueue(dn_tq, &dn_task);
1083b3a8eb9SGleb Smirnoff }
1093b3a8eb9SGleb Smirnoff
1103b3a8eb9SGleb Smirnoff void
dummynet_sched_lock(void)1113f3e4f3cSKristof Provost dummynet_sched_lock(void)
1123f3e4f3cSKristof Provost {
1133f3e4f3cSKristof Provost mtx_lock(&sched_mtx);
1143f3e4f3cSKristof Provost }
1153f3e4f3cSKristof Provost
1163f3e4f3cSKristof Provost void
dummynet_sched_unlock(void)1173f3e4f3cSKristof Provost dummynet_sched_unlock(void)
1183f3e4f3cSKristof Provost {
1193f3e4f3cSKristof Provost mtx_unlock(&sched_mtx);
1203f3e4f3cSKristof Provost }
1213f3e4f3cSKristof Provost
1223f3e4f3cSKristof Provost void
dn_reschedule(void)1233b3a8eb9SGleb Smirnoff dn_reschedule(void)
1243b3a8eb9SGleb Smirnoff {
1255f4fc3dbSAlexander Motin
126c8cfbc06SHans Petter Selasky if (dn_gone != 0)
127c8cfbc06SHans Petter Selasky return;
1285f4fc3dbSAlexander Motin callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL,
1295f4fc3dbSAlexander Motin C_HARDCLOCK | C_DIRECT_EXEC);
1303b3a8eb9SGleb Smirnoff }
1313b3a8eb9SGleb Smirnoff /*----- end of callout hooks -----*/
1323b3a8eb9SGleb Smirnoff
13391336b40SDon Lewis #ifdef NEW_AQM
13491336b40SDon Lewis /* Return AQM descriptor for given type or name. */
13591336b40SDon Lewis static struct dn_aqm *
find_aqm_type(int type,char * name)13691336b40SDon Lewis find_aqm_type(int type, char *name)
13791336b40SDon Lewis {
13891336b40SDon Lewis struct dn_aqm *d;
13991336b40SDon Lewis
14051d73df1SKristof Provost NET_EPOCH_ASSERT();
14151d73df1SKristof Provost
14251d73df1SKristof Provost CK_LIST_FOREACH(d, &aqmlist, next) {
14391336b40SDon Lewis if (d->type == type || (name && !strcasecmp(d->name, name)))
14491336b40SDon Lewis return d;
14591336b40SDon Lewis }
14691336b40SDon Lewis return NULL; /* not found */
14791336b40SDon Lewis }
14891336b40SDon Lewis #endif
14991336b40SDon Lewis
1503b3a8eb9SGleb Smirnoff /* Return a scheduler descriptor given the type or name. */
1513b3a8eb9SGleb Smirnoff static struct dn_alg *
find_sched_type(int type,char * name)1523b3a8eb9SGleb Smirnoff find_sched_type(int type, char *name)
1533b3a8eb9SGleb Smirnoff {
1543b3a8eb9SGleb Smirnoff struct dn_alg *d;
1553b3a8eb9SGleb Smirnoff
15651d73df1SKristof Provost NET_EPOCH_ASSERT();
15751d73df1SKristof Provost
15851d73df1SKristof Provost CK_LIST_FOREACH(d, &schedlist, next) {
1593b3a8eb9SGleb Smirnoff if (d->type == type || (name && !strcasecmp(d->name, name)))
1603b3a8eb9SGleb Smirnoff return d;
1613b3a8eb9SGleb Smirnoff }
1623b3a8eb9SGleb Smirnoff return NULL; /* not found */
1633b3a8eb9SGleb Smirnoff }
1643b3a8eb9SGleb Smirnoff
1653b3a8eb9SGleb Smirnoff int
ipdn_bound_var(int * v,int dflt,int lo,int hi,const char * msg)1663b3a8eb9SGleb Smirnoff ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
1673b3a8eb9SGleb Smirnoff {
1683b3a8eb9SGleb Smirnoff int oldv = *v;
1693b3a8eb9SGleb Smirnoff const char *op = NULL;
1703b3a8eb9SGleb Smirnoff if (dflt < lo)
1713b3a8eb9SGleb Smirnoff dflt = lo;
1723b3a8eb9SGleb Smirnoff if (dflt > hi)
1733b3a8eb9SGleb Smirnoff dflt = hi;
1743b3a8eb9SGleb Smirnoff if (oldv < lo) {
1753b3a8eb9SGleb Smirnoff *v = dflt;
1763b3a8eb9SGleb Smirnoff op = "Bump";
1773b3a8eb9SGleb Smirnoff } else if (oldv > hi) {
1783b3a8eb9SGleb Smirnoff *v = hi;
1793b3a8eb9SGleb Smirnoff op = "Clamp";
1803b3a8eb9SGleb Smirnoff } else
1813b3a8eb9SGleb Smirnoff return *v;
182c5dd8bacSLuiz Otavio O Souza if (op && msg && bootverbose)
1833b3a8eb9SGleb Smirnoff printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
1843b3a8eb9SGleb Smirnoff return *v;
1853b3a8eb9SGleb Smirnoff }
1863b3a8eb9SGleb Smirnoff
1873b3a8eb9SGleb Smirnoff /*---- flow_id mask, hash and compare functions ---*/
1883b3a8eb9SGleb Smirnoff /*
1893b3a8eb9SGleb Smirnoff * The flow_id includes the 5-tuple, the queue/pipe number
1903b3a8eb9SGleb Smirnoff * which we store in the extra area in host order,
1913b3a8eb9SGleb Smirnoff * and for ipv6 also the flow_id6.
1923b3a8eb9SGleb Smirnoff * XXX see if we want the tos byte (can store in 'flags')
1933b3a8eb9SGleb Smirnoff */
1943b3a8eb9SGleb Smirnoff static struct ipfw_flow_id *
flow_id_mask(struct ipfw_flow_id * mask,struct ipfw_flow_id * id)1953b3a8eb9SGleb Smirnoff flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id)
1963b3a8eb9SGleb Smirnoff {
1973b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(id);
1983b3a8eb9SGleb Smirnoff
1993b3a8eb9SGleb Smirnoff id->dst_port &= mask->dst_port;
2003b3a8eb9SGleb Smirnoff id->src_port &= mask->src_port;
2013b3a8eb9SGleb Smirnoff id->proto &= mask->proto;
2023b3a8eb9SGleb Smirnoff id->extra &= mask->extra;
2033b3a8eb9SGleb Smirnoff if (is_v6) {
2043b3a8eb9SGleb Smirnoff APPLY_MASK(&id->dst_ip6, &mask->dst_ip6);
2053b3a8eb9SGleb Smirnoff APPLY_MASK(&id->src_ip6, &mask->src_ip6);
2063b3a8eb9SGleb Smirnoff id->flow_id6 &= mask->flow_id6;
2073b3a8eb9SGleb Smirnoff } else {
2083b3a8eb9SGleb Smirnoff id->dst_ip &= mask->dst_ip;
2093b3a8eb9SGleb Smirnoff id->src_ip &= mask->src_ip;
2103b3a8eb9SGleb Smirnoff }
2113b3a8eb9SGleb Smirnoff return id;
2123b3a8eb9SGleb Smirnoff }
2133b3a8eb9SGleb Smirnoff
2143b3a8eb9SGleb Smirnoff /* computes an OR of two masks, result in dst and also returned */
2153b3a8eb9SGleb Smirnoff static struct ipfw_flow_id *
flow_id_or(struct ipfw_flow_id * src,struct ipfw_flow_id * dst)2163b3a8eb9SGleb Smirnoff flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst)
2173b3a8eb9SGleb Smirnoff {
2183b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(dst);
2193b3a8eb9SGleb Smirnoff
2203b3a8eb9SGleb Smirnoff dst->dst_port |= src->dst_port;
2213b3a8eb9SGleb Smirnoff dst->src_port |= src->src_port;
2223b3a8eb9SGleb Smirnoff dst->proto |= src->proto;
2233b3a8eb9SGleb Smirnoff dst->extra |= src->extra;
2243b3a8eb9SGleb Smirnoff if (is_v6) {
2253b3a8eb9SGleb Smirnoff #define OR_MASK(_d, _s) \
2263b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \
2273b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \
2283b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \
2293b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3];
2303b3a8eb9SGleb Smirnoff OR_MASK(&dst->dst_ip6, &src->dst_ip6);
2313b3a8eb9SGleb Smirnoff OR_MASK(&dst->src_ip6, &src->src_ip6);
2323b3a8eb9SGleb Smirnoff #undef OR_MASK
2333b3a8eb9SGleb Smirnoff dst->flow_id6 |= src->flow_id6;
2343b3a8eb9SGleb Smirnoff } else {
2353b3a8eb9SGleb Smirnoff dst->dst_ip |= src->dst_ip;
2363b3a8eb9SGleb Smirnoff dst->src_ip |= src->src_ip;
2373b3a8eb9SGleb Smirnoff }
2383b3a8eb9SGleb Smirnoff return dst;
2393b3a8eb9SGleb Smirnoff }
2403b3a8eb9SGleb Smirnoff
2413b3a8eb9SGleb Smirnoff static int
nonzero_mask(struct ipfw_flow_id * m)2423b3a8eb9SGleb Smirnoff nonzero_mask(struct ipfw_flow_id *m)
2433b3a8eb9SGleb Smirnoff {
2443b3a8eb9SGleb Smirnoff if (m->dst_port || m->src_port || m->proto || m->extra)
2453b3a8eb9SGleb Smirnoff return 1;
2463b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(m)) {
2473b3a8eb9SGleb Smirnoff return
2483b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[0] ||
2493b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[1] ||
2503b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[2] ||
2513b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[3] ||
2523b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[0] ||
2533b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[1] ||
2543b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[2] ||
2553b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[3] ||
2563b3a8eb9SGleb Smirnoff m->flow_id6;
2573b3a8eb9SGleb Smirnoff } else {
2583b3a8eb9SGleb Smirnoff return m->dst_ip || m->src_ip;
2593b3a8eb9SGleb Smirnoff }
2603b3a8eb9SGleb Smirnoff }
2613b3a8eb9SGleb Smirnoff
2623b3a8eb9SGleb Smirnoff /* XXX we may want a better hash function */
2633b3a8eb9SGleb Smirnoff static uint32_t
flow_id_hash(struct ipfw_flow_id * id)2643b3a8eb9SGleb Smirnoff flow_id_hash(struct ipfw_flow_id *id)
2653b3a8eb9SGleb Smirnoff {
2663b3a8eb9SGleb Smirnoff uint32_t i;
2673b3a8eb9SGleb Smirnoff
2683b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(id)) {
2693b3a8eb9SGleb Smirnoff uint32_t *d = (uint32_t *)&id->dst_ip6;
2703b3a8eb9SGleb Smirnoff uint32_t *s = (uint32_t *)&id->src_ip6;
2713b3a8eb9SGleb Smirnoff i = (d[0] ) ^ (d[1]) ^
2723b3a8eb9SGleb Smirnoff (d[2] ) ^ (d[3]) ^
2733b3a8eb9SGleb Smirnoff (d[0] >> 15) ^ (d[1] >> 15) ^
2743b3a8eb9SGleb Smirnoff (d[2] >> 15) ^ (d[3] >> 15) ^
2753b3a8eb9SGleb Smirnoff (s[0] << 1) ^ (s[1] << 1) ^
2763b3a8eb9SGleb Smirnoff (s[2] << 1) ^ (s[3] << 1) ^
2773b3a8eb9SGleb Smirnoff (s[0] << 16) ^ (s[1] << 16) ^
2783b3a8eb9SGleb Smirnoff (s[2] << 16) ^ (s[3] << 16) ^
2793b3a8eb9SGleb Smirnoff (id->dst_port << 1) ^ (id->src_port) ^
2803b3a8eb9SGleb Smirnoff (id->extra) ^
2813b3a8eb9SGleb Smirnoff (id->proto ) ^ (id->flow_id6);
2823b3a8eb9SGleb Smirnoff } else {
2833b3a8eb9SGleb Smirnoff i = (id->dst_ip) ^ (id->dst_ip >> 15) ^
2843b3a8eb9SGleb Smirnoff (id->src_ip << 1) ^ (id->src_ip >> 16) ^
2853b3a8eb9SGleb Smirnoff (id->extra) ^
2863b3a8eb9SGleb Smirnoff (id->dst_port << 1) ^ (id->src_port) ^ (id->proto);
2873b3a8eb9SGleb Smirnoff }
2883b3a8eb9SGleb Smirnoff return i;
2893b3a8eb9SGleb Smirnoff }
2903b3a8eb9SGleb Smirnoff
2913b3a8eb9SGleb Smirnoff /* Like bcmp, returns 0 if ids match, 1 otherwise. */
2923b3a8eb9SGleb Smirnoff static int
flow_id_cmp(struct ipfw_flow_id * id1,struct ipfw_flow_id * id2)2933b3a8eb9SGleb Smirnoff flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2)
2943b3a8eb9SGleb Smirnoff {
2953b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(id1);
2963b3a8eb9SGleb Smirnoff
2973b3a8eb9SGleb Smirnoff if (!is_v6) {
2983b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(id2))
2993b3a8eb9SGleb Smirnoff return 1; /* different address families */
3003b3a8eb9SGleb Smirnoff
3013b3a8eb9SGleb Smirnoff return (id1->dst_ip == id2->dst_ip &&
3023b3a8eb9SGleb Smirnoff id1->src_ip == id2->src_ip &&
3033b3a8eb9SGleb Smirnoff id1->dst_port == id2->dst_port &&
3043b3a8eb9SGleb Smirnoff id1->src_port == id2->src_port &&
3053b3a8eb9SGleb Smirnoff id1->proto == id2->proto &&
3063b3a8eb9SGleb Smirnoff id1->extra == id2->extra) ? 0 : 1;
3073b3a8eb9SGleb Smirnoff }
3083b3a8eb9SGleb Smirnoff /* the ipv6 case */
3093b3a8eb9SGleb Smirnoff return (
3103b3a8eb9SGleb Smirnoff !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) &&
3113b3a8eb9SGleb Smirnoff !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) &&
3123b3a8eb9SGleb Smirnoff id1->dst_port == id2->dst_port &&
3133b3a8eb9SGleb Smirnoff id1->src_port == id2->src_port &&
3143b3a8eb9SGleb Smirnoff id1->proto == id2->proto &&
3153b3a8eb9SGleb Smirnoff id1->extra == id2->extra &&
3163b3a8eb9SGleb Smirnoff id1->flow_id6 == id2->flow_id6) ? 0 : 1;
3173b3a8eb9SGleb Smirnoff }
3183b3a8eb9SGleb Smirnoff /*--------- end of flow-id mask, hash and compare ---------*/
3193b3a8eb9SGleb Smirnoff
3203b3a8eb9SGleb Smirnoff /*--- support functions for the qht hashtable ----
3213b3a8eb9SGleb Smirnoff * Entries are hashed by flow-id
3223b3a8eb9SGleb Smirnoff */
3233b3a8eb9SGleb Smirnoff static uint32_t
q_hash(uintptr_t key,int flags,void * arg)3243b3a8eb9SGleb Smirnoff q_hash(uintptr_t key, int flags, void *arg)
3253b3a8eb9SGleb Smirnoff {
3263b3a8eb9SGleb Smirnoff /* compute the hash slot from the flow id */
3273b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
3283b3a8eb9SGleb Smirnoff &((struct dn_queue *)key)->ni.fid :
3293b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key;
3303b3a8eb9SGleb Smirnoff
3313b3a8eb9SGleb Smirnoff return flow_id_hash(id);
3323b3a8eb9SGleb Smirnoff }
3333b3a8eb9SGleb Smirnoff
3343b3a8eb9SGleb Smirnoff static int
q_match(void * obj,uintptr_t key,int flags,void * arg)3353b3a8eb9SGleb Smirnoff q_match(void *obj, uintptr_t key, int flags, void *arg)
3363b3a8eb9SGleb Smirnoff {
3373b3a8eb9SGleb Smirnoff struct dn_queue *o = (struct dn_queue *)obj;
3383b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id2;
3393b3a8eb9SGleb Smirnoff
3403b3a8eb9SGleb Smirnoff if (flags & DNHT_KEY_IS_OBJ) {
3413b3a8eb9SGleb Smirnoff /* compare pointers */
3423b3a8eb9SGleb Smirnoff id2 = &((struct dn_queue *)key)->ni.fid;
3433b3a8eb9SGleb Smirnoff } else {
3443b3a8eb9SGleb Smirnoff id2 = (struct ipfw_flow_id *)key;
3453b3a8eb9SGleb Smirnoff }
3463b3a8eb9SGleb Smirnoff return (0 == flow_id_cmp(&o->ni.fid, id2));
3473b3a8eb9SGleb Smirnoff }
3483b3a8eb9SGleb Smirnoff
3493b3a8eb9SGleb Smirnoff /*
3503b3a8eb9SGleb Smirnoff * create a new queue instance for the given 'key'.
3513b3a8eb9SGleb Smirnoff */
3523b3a8eb9SGleb Smirnoff static void *
q_new(uintptr_t key,int flags,void * arg)3533b3a8eb9SGleb Smirnoff q_new(uintptr_t key, int flags, void *arg)
3543b3a8eb9SGleb Smirnoff {
3553b3a8eb9SGleb Smirnoff struct dn_queue *q, *template = arg;
3563b3a8eb9SGleb Smirnoff struct dn_fsk *fs = template->fs;
3573b3a8eb9SGleb Smirnoff int size = sizeof(*q) + fs->sched->fp->q_datalen;
3583b3a8eb9SGleb Smirnoff
3593b3a8eb9SGleb Smirnoff q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO);
3603b3a8eb9SGleb Smirnoff if (q == NULL) {
3613b3a8eb9SGleb Smirnoff D("no memory for new queue");
3623b3a8eb9SGleb Smirnoff return NULL;
3633b3a8eb9SGleb Smirnoff }
3643b3a8eb9SGleb Smirnoff
3653b3a8eb9SGleb Smirnoff set_oid(&q->ni.oid, DN_QUEUE, size);
3663b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH)
3673b3a8eb9SGleb Smirnoff q->ni.fid = *(struct ipfw_flow_id *)key;
3683b3a8eb9SGleb Smirnoff q->fs = fs;
3693b3a8eb9SGleb Smirnoff q->_si = template->_si;
3703b3a8eb9SGleb Smirnoff q->_si->q_count++;
3713b3a8eb9SGleb Smirnoff
3723b3a8eb9SGleb Smirnoff if (fs->sched->fp->new_queue)
3733b3a8eb9SGleb Smirnoff fs->sched->fp->new_queue(q);
37491336b40SDon Lewis
37591336b40SDon Lewis #ifdef NEW_AQM
37691336b40SDon Lewis /* call AQM init function after creating a queue*/
37791336b40SDon Lewis if (fs->aqmfp && fs->aqmfp->init)
37891336b40SDon Lewis if(fs->aqmfp->init(q))
37991336b40SDon Lewis D("unable to init AQM for fs %d", fs->fs.fs_nr);
38091336b40SDon Lewis #endif
381fe3bcfbdSTom Jones V_dn_cfg.queue_count++;
38291336b40SDon Lewis
3833b3a8eb9SGleb Smirnoff return q;
3843b3a8eb9SGleb Smirnoff }
3853b3a8eb9SGleb Smirnoff
3863b3a8eb9SGleb Smirnoff /*
3873b3a8eb9SGleb Smirnoff * Notify schedulers that a queue is going away.
3883b3a8eb9SGleb Smirnoff * If (flags & DN_DESTROY), also free the packets.
3893b3a8eb9SGleb Smirnoff * The version for callbacks is called q_delete_cb().
3903b3a8eb9SGleb Smirnoff */
3913b3a8eb9SGleb Smirnoff static void
dn_delete_queue(struct dn_queue * q,int flags)3923b3a8eb9SGleb Smirnoff dn_delete_queue(struct dn_queue *q, int flags)
3933b3a8eb9SGleb Smirnoff {
3943b3a8eb9SGleb Smirnoff struct dn_fsk *fs = q->fs;
3953b3a8eb9SGleb Smirnoff
39691336b40SDon Lewis #ifdef NEW_AQM
39791336b40SDon Lewis /* clean up AQM status for queue 'q'
39891336b40SDon Lewis * cleanup here is called just with MULTIQUEUE
39991336b40SDon Lewis */
40091336b40SDon Lewis if (fs && fs->aqmfp && fs->aqmfp->cleanup)
40191336b40SDon Lewis fs->aqmfp->cleanup(q);
40291336b40SDon Lewis #endif
4033b3a8eb9SGleb Smirnoff // D("fs %p si %p\n", fs, q->_si);
4043b3a8eb9SGleb Smirnoff /* notify the parent scheduler that the queue is going away */
4053b3a8eb9SGleb Smirnoff if (fs && fs->sched->fp->free_queue)
4063b3a8eb9SGleb Smirnoff fs->sched->fp->free_queue(q);
4073b3a8eb9SGleb Smirnoff q->_si->q_count--;
4083b3a8eb9SGleb Smirnoff q->_si = NULL;
4093b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY) {
4103b3a8eb9SGleb Smirnoff if (q->mq.head)
4113b3a8eb9SGleb Smirnoff dn_free_pkts(q->mq.head);
4123b3a8eb9SGleb Smirnoff bzero(q, sizeof(*q)); // safety
4133b3a8eb9SGleb Smirnoff free(q, M_DUMMYNET);
414fe3bcfbdSTom Jones V_dn_cfg.queue_count--;
4153b3a8eb9SGleb Smirnoff }
4163b3a8eb9SGleb Smirnoff }
4173b3a8eb9SGleb Smirnoff
4183b3a8eb9SGleb Smirnoff static int
q_delete_cb(void * q,void * arg)4193b3a8eb9SGleb Smirnoff q_delete_cb(void *q, void *arg)
4203b3a8eb9SGleb Smirnoff {
4213b3a8eb9SGleb Smirnoff int flags = (int)(uintptr_t)arg;
4223b3a8eb9SGleb Smirnoff dn_delete_queue(q, flags);
4233b3a8eb9SGleb Smirnoff return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0;
4243b3a8eb9SGleb Smirnoff }
4253b3a8eb9SGleb Smirnoff
4263b3a8eb9SGleb Smirnoff /*
4273b3a8eb9SGleb Smirnoff * calls dn_delete_queue/q_delete_cb on all queues,
4283b3a8eb9SGleb Smirnoff * which notifies the parent scheduler and possibly drains packets.
4293b3a8eb9SGleb Smirnoff * flags & DN_DESTROY: drains queues and destroy qht;
4303b3a8eb9SGleb Smirnoff */
4313b3a8eb9SGleb Smirnoff static void
qht_delete(struct dn_fsk * fs,int flags)4323b3a8eb9SGleb Smirnoff qht_delete(struct dn_fsk *fs, int flags)
4333b3a8eb9SGleb Smirnoff {
4343b3a8eb9SGleb Smirnoff ND("fs %d start flags %d qht %p",
4353b3a8eb9SGleb Smirnoff fs->fs.fs_nr, flags, fs->qht);
4363b3a8eb9SGleb Smirnoff if (!fs->qht)
4373b3a8eb9SGleb Smirnoff return;
4383b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) {
4393b3a8eb9SGleb Smirnoff dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags);
4403b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY) {
4413b3a8eb9SGleb Smirnoff dn_ht_free(fs->qht, 0);
4423b3a8eb9SGleb Smirnoff fs->qht = NULL;
4433b3a8eb9SGleb Smirnoff }
4443b3a8eb9SGleb Smirnoff } else {
4453b3a8eb9SGleb Smirnoff dn_delete_queue((struct dn_queue *)(fs->qht), flags);
4463b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY)
4473b3a8eb9SGleb Smirnoff fs->qht = NULL;
4483b3a8eb9SGleb Smirnoff }
4493b3a8eb9SGleb Smirnoff }
4503b3a8eb9SGleb Smirnoff
4513b3a8eb9SGleb Smirnoff /*
4523b3a8eb9SGleb Smirnoff * Find and possibly create the queue for a MULTIQUEUE scheduler.
4533b3a8eb9SGleb Smirnoff * We never call it for !MULTIQUEUE (the queue is in the sch_inst).
4543b3a8eb9SGleb Smirnoff */
4553b3a8eb9SGleb Smirnoff struct dn_queue *
ipdn_q_find(struct dn_fsk * fs,struct dn_sch_inst * si,struct ipfw_flow_id * id)4563b3a8eb9SGleb Smirnoff ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si,
4573b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id)
4583b3a8eb9SGleb Smirnoff {
4593b3a8eb9SGleb Smirnoff struct dn_queue template;
4603b3a8eb9SGleb Smirnoff
4613b3a8eb9SGleb Smirnoff template._si = si;
4623b3a8eb9SGleb Smirnoff template.fs = fs;
4633b3a8eb9SGleb Smirnoff
4643b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) {
4653b3a8eb9SGleb Smirnoff struct ipfw_flow_id masked_id;
4663b3a8eb9SGleb Smirnoff if (fs->qht == NULL) {
4673b3a8eb9SGleb Smirnoff fs->qht = dn_ht_init(NULL, fs->fs.buckets,
4683b3a8eb9SGleb Smirnoff offsetof(struct dn_queue, q_next),
4693b3a8eb9SGleb Smirnoff q_hash, q_match, q_new);
4703b3a8eb9SGleb Smirnoff if (fs->qht == NULL)
4713b3a8eb9SGleb Smirnoff return NULL;
4723b3a8eb9SGleb Smirnoff }
4733b3a8eb9SGleb Smirnoff masked_id = *id;
4743b3a8eb9SGleb Smirnoff flow_id_mask(&fs->fsk_mask, &masked_id);
4753b3a8eb9SGleb Smirnoff return dn_ht_find(fs->qht, (uintptr_t)&masked_id,
4763b3a8eb9SGleb Smirnoff DNHT_INSERT, &template);
4773b3a8eb9SGleb Smirnoff } else {
4783b3a8eb9SGleb Smirnoff if (fs->qht == NULL)
4793b3a8eb9SGleb Smirnoff fs->qht = q_new(0, 0, &template);
4803b3a8eb9SGleb Smirnoff return (struct dn_queue *)fs->qht;
4813b3a8eb9SGleb Smirnoff }
4823b3a8eb9SGleb Smirnoff }
4833b3a8eb9SGleb Smirnoff /*--- end of queue hash table ---*/
4843b3a8eb9SGleb Smirnoff
4853b3a8eb9SGleb Smirnoff /*--- support functions for the sch_inst hashtable ----
4863b3a8eb9SGleb Smirnoff *
4873b3a8eb9SGleb Smirnoff * These are hashed by flow-id
4883b3a8eb9SGleb Smirnoff */
4893b3a8eb9SGleb Smirnoff static uint32_t
si_hash(uintptr_t key,int flags,void * arg)4903b3a8eb9SGleb Smirnoff si_hash(uintptr_t key, int flags, void *arg)
4913b3a8eb9SGleb Smirnoff {
4923b3a8eb9SGleb Smirnoff /* compute the hash slot from the flow id */
4933b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ?
4943b3a8eb9SGleb Smirnoff &((struct dn_sch_inst *)key)->ni.fid :
4953b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key;
4963b3a8eb9SGleb Smirnoff
4973b3a8eb9SGleb Smirnoff return flow_id_hash(id);
4983b3a8eb9SGleb Smirnoff }
4993b3a8eb9SGleb Smirnoff
5003b3a8eb9SGleb Smirnoff static int
si_match(void * obj,uintptr_t key,int flags,void * arg)5013b3a8eb9SGleb Smirnoff si_match(void *obj, uintptr_t key, int flags, void *arg)
5023b3a8eb9SGleb Smirnoff {
5033b3a8eb9SGleb Smirnoff struct dn_sch_inst *o = obj;
5043b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id2;
5053b3a8eb9SGleb Smirnoff
5063b3a8eb9SGleb Smirnoff id2 = (flags & DNHT_KEY_IS_OBJ) ?
5073b3a8eb9SGleb Smirnoff &((struct dn_sch_inst *)key)->ni.fid :
5083b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key;
5093b3a8eb9SGleb Smirnoff return flow_id_cmp(&o->ni.fid, id2) == 0;
5103b3a8eb9SGleb Smirnoff }
5113b3a8eb9SGleb Smirnoff
5123b3a8eb9SGleb Smirnoff /*
5133b3a8eb9SGleb Smirnoff * create a new instance for the given 'key'
5143b3a8eb9SGleb Smirnoff * Allocate memory for instance, delay line and scheduler private data.
5153b3a8eb9SGleb Smirnoff */
5163b3a8eb9SGleb Smirnoff static void *
si_new(uintptr_t key,int flags,void * arg)5173b3a8eb9SGleb Smirnoff si_new(uintptr_t key, int flags, void *arg)
5183b3a8eb9SGleb Smirnoff {
5193b3a8eb9SGleb Smirnoff struct dn_schk *s = arg;
5203b3a8eb9SGleb Smirnoff struct dn_sch_inst *si;
5213b3a8eb9SGleb Smirnoff int l = sizeof(*si) + s->fp->si_datalen;
5223b3a8eb9SGleb Smirnoff
5233b3a8eb9SGleb Smirnoff si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
5243b3a8eb9SGleb Smirnoff if (si == NULL)
5253b3a8eb9SGleb Smirnoff goto error;
5263b3a8eb9SGleb Smirnoff
5273b3a8eb9SGleb Smirnoff /* Set length only for the part passed up to userland. */
5283b3a8eb9SGleb Smirnoff set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow));
5293b3a8eb9SGleb Smirnoff set_oid(&(si->dline.oid), DN_DELAY_LINE,
5303b3a8eb9SGleb Smirnoff sizeof(struct delay_line));
5313b3a8eb9SGleb Smirnoff /* mark si and dline as outside the event queue */
5323b3a8eb9SGleb Smirnoff si->ni.oid.id = si->dline.oid.id = -1;
5333b3a8eb9SGleb Smirnoff
5343b3a8eb9SGleb Smirnoff si->sched = s;
5353b3a8eb9SGleb Smirnoff si->dline.si = si;
5363b3a8eb9SGleb Smirnoff
5373b3a8eb9SGleb Smirnoff if (s->fp->new_sched && s->fp->new_sched(si)) {
5383b3a8eb9SGleb Smirnoff D("new_sched error");
5393b3a8eb9SGleb Smirnoff goto error;
5403b3a8eb9SGleb Smirnoff }
5413b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK)
5423b3a8eb9SGleb Smirnoff si->ni.fid = *(struct ipfw_flow_id *)key;
5433b3a8eb9SGleb Smirnoff
54491336b40SDon Lewis #ifdef NEW_AQM
54591336b40SDon Lewis /* init AQM status for !DN_MULTIQUEUE sched*/
54691336b40SDon Lewis if (!(s->fp->flags & DN_MULTIQUEUE))
54791336b40SDon Lewis if (s->fs->aqmfp && s->fs->aqmfp->init)
54891336b40SDon Lewis if(s->fs->aqmfp->init((struct dn_queue *)(si + 1))) {
54991336b40SDon Lewis D("unable to init AQM for fs %d", s->fs->fs.fs_nr);
55091336b40SDon Lewis goto error;
55191336b40SDon Lewis }
55291336b40SDon Lewis #endif
55391336b40SDon Lewis
554fe3bcfbdSTom Jones V_dn_cfg.si_count++;
5553b3a8eb9SGleb Smirnoff return si;
5563b3a8eb9SGleb Smirnoff
5573b3a8eb9SGleb Smirnoff error:
5583b3a8eb9SGleb Smirnoff if (si) {
5593b3a8eb9SGleb Smirnoff bzero(si, sizeof(*si)); // safety
5603b3a8eb9SGleb Smirnoff free(si, M_DUMMYNET);
5613b3a8eb9SGleb Smirnoff }
5623b3a8eb9SGleb Smirnoff return NULL;
5633b3a8eb9SGleb Smirnoff }
5643b3a8eb9SGleb Smirnoff
5653b3a8eb9SGleb Smirnoff /*
5663b3a8eb9SGleb Smirnoff * Callback from siht to delete all scheduler instances. Remove
5673b3a8eb9SGleb Smirnoff * si and delay line from the system heap, destroy all queues.
5683b3a8eb9SGleb Smirnoff * We assume that all flowset have been notified and do not
5693b3a8eb9SGleb Smirnoff * point to us anymore.
5703b3a8eb9SGleb Smirnoff */
5713b3a8eb9SGleb Smirnoff static int
si_destroy(void * _si,void * arg)5723b3a8eb9SGleb Smirnoff si_destroy(void *_si, void *arg)
5733b3a8eb9SGleb Smirnoff {
5743b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si;
5753b3a8eb9SGleb Smirnoff struct dn_schk *s = si->sched;
5763b3a8eb9SGleb Smirnoff struct delay_line *dl = &si->dline;
5773b3a8eb9SGleb Smirnoff
5783b3a8eb9SGleb Smirnoff if (dl->oid.subtype) /* remove delay line from event heap */
579fe3bcfbdSTom Jones heap_extract(&V_dn_cfg.evheap, dl);
5803b3a8eb9SGleb Smirnoff dn_free_pkts(dl->mq.head); /* drain delay line */
5813b3a8eb9SGleb Smirnoff if (si->kflags & DN_ACTIVE) /* remove si from event heap */
582fe3bcfbdSTom Jones heap_extract(&V_dn_cfg.evheap, si);
58391336b40SDon Lewis
58491336b40SDon Lewis #ifdef NEW_AQM
58591336b40SDon Lewis /* clean up AQM status for !DN_MULTIQUEUE sched
58691336b40SDon Lewis * Note that all queues belong to fs were cleaned up in fsk_detach.
58791336b40SDon Lewis * When drain_scheduler is called s->fs and q->fs are pointing
58891336b40SDon Lewis * to a correct fs, so we can use fs in this case.
58991336b40SDon Lewis */
59091336b40SDon Lewis if (!(s->fp->flags & DN_MULTIQUEUE)) {
59191336b40SDon Lewis struct dn_queue *q = (struct dn_queue *)(si + 1);
59291336b40SDon Lewis if (q->aqm_status && q->fs->aqmfp)
59391336b40SDon Lewis if (q->fs->aqmfp->cleanup)
59491336b40SDon Lewis q->fs->aqmfp->cleanup(q);
59591336b40SDon Lewis }
59691336b40SDon Lewis #endif
5973b3a8eb9SGleb Smirnoff if (s->fp->free_sched)
5983b3a8eb9SGleb Smirnoff s->fp->free_sched(si);
5993b3a8eb9SGleb Smirnoff bzero(si, sizeof(*si)); /* safety */
6003b3a8eb9SGleb Smirnoff free(si, M_DUMMYNET);
601fe3bcfbdSTom Jones V_dn_cfg.si_count--;
6023b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL;
6033b3a8eb9SGleb Smirnoff }
6043b3a8eb9SGleb Smirnoff
6053b3a8eb9SGleb Smirnoff /*
6063b3a8eb9SGleb Smirnoff * Find the scheduler instance for this packet. If we need to apply
6073b3a8eb9SGleb Smirnoff * a mask, do on a local copy of the flow_id to preserve the original.
6083b3a8eb9SGleb Smirnoff * Assume siht is always initialized if we have a mask.
6093b3a8eb9SGleb Smirnoff */
6103b3a8eb9SGleb Smirnoff struct dn_sch_inst *
ipdn_si_find(struct dn_schk * s,struct ipfw_flow_id * id)6113b3a8eb9SGleb Smirnoff ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id)
6123b3a8eb9SGleb Smirnoff {
6133b3a8eb9SGleb Smirnoff
6143b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) {
6153b3a8eb9SGleb Smirnoff struct ipfw_flow_id id_t = *id;
6163b3a8eb9SGleb Smirnoff flow_id_mask(&s->sch.sched_mask, &id_t);
6173b3a8eb9SGleb Smirnoff return dn_ht_find(s->siht, (uintptr_t)&id_t,
6183b3a8eb9SGleb Smirnoff DNHT_INSERT, s);
6193b3a8eb9SGleb Smirnoff }
6203b3a8eb9SGleb Smirnoff if (!s->siht)
6213b3a8eb9SGleb Smirnoff s->siht = si_new(0, 0, s);
6223b3a8eb9SGleb Smirnoff return (struct dn_sch_inst *)s->siht;
6233b3a8eb9SGleb Smirnoff }
6243b3a8eb9SGleb Smirnoff
6253b3a8eb9SGleb Smirnoff /* callback to flush credit for the scheduler instance */
6263b3a8eb9SGleb Smirnoff static int
si_reset_credit(void * _si,void * arg)6273b3a8eb9SGleb Smirnoff si_reset_credit(void *_si, void *arg)
6283b3a8eb9SGleb Smirnoff {
6293b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si;
6303b3a8eb9SGleb Smirnoff struct dn_link *p = &si->sched->link;
6313b3a8eb9SGleb Smirnoff
632fe3bcfbdSTom Jones si->credit = p->burst + (V_dn_cfg.io_fast ? p->bandwidth : 0);
6333b3a8eb9SGleb Smirnoff return 0;
6343b3a8eb9SGleb Smirnoff }
6353b3a8eb9SGleb Smirnoff
6363b3a8eb9SGleb Smirnoff static void
schk_reset_credit(struct dn_schk * s)6373b3a8eb9SGleb Smirnoff schk_reset_credit(struct dn_schk *s)
6383b3a8eb9SGleb Smirnoff {
6393b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK)
6403b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, si_reset_credit, NULL);
6413b3a8eb9SGleb Smirnoff else if (s->siht)
6423b3a8eb9SGleb Smirnoff si_reset_credit(s->siht, NULL);
6433b3a8eb9SGleb Smirnoff }
6443b3a8eb9SGleb Smirnoff /*---- end of sch_inst hashtable ---------------------*/
6453b3a8eb9SGleb Smirnoff
6463b3a8eb9SGleb Smirnoff /*-------------------------------------------------------
6473b3a8eb9SGleb Smirnoff * flowset hash (fshash) support. Entries are hashed by fs_nr.
6483b3a8eb9SGleb Smirnoff * New allocations are put in the fsunlinked list, from which
6493b3a8eb9SGleb Smirnoff * they are removed when they point to a specific scheduler.
6503b3a8eb9SGleb Smirnoff */
6513b3a8eb9SGleb Smirnoff static uint32_t
fsk_hash(uintptr_t key,int flags,void * arg)6523b3a8eb9SGleb Smirnoff fsk_hash(uintptr_t key, int flags, void *arg)
6533b3a8eb9SGleb Smirnoff {
6543b3a8eb9SGleb Smirnoff uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
6553b3a8eb9SGleb Smirnoff ((struct dn_fsk *)key)->fs.fs_nr;
6563b3a8eb9SGleb Smirnoff
6573b3a8eb9SGleb Smirnoff return ( (i>>8)^(i>>4)^i );
6583b3a8eb9SGleb Smirnoff }
6593b3a8eb9SGleb Smirnoff
6603b3a8eb9SGleb Smirnoff static int
fsk_match(void * obj,uintptr_t key,int flags,void * arg)6613b3a8eb9SGleb Smirnoff fsk_match(void *obj, uintptr_t key, int flags, void *arg)
6623b3a8eb9SGleb Smirnoff {
6633b3a8eb9SGleb Smirnoff struct dn_fsk *fs = obj;
6643b3a8eb9SGleb Smirnoff int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
6653b3a8eb9SGleb Smirnoff ((struct dn_fsk *)key)->fs.fs_nr;
6663b3a8eb9SGleb Smirnoff
6673b3a8eb9SGleb Smirnoff return (fs->fs.fs_nr == i);
6683b3a8eb9SGleb Smirnoff }
6693b3a8eb9SGleb Smirnoff
6703b3a8eb9SGleb Smirnoff static void *
fsk_new(uintptr_t key,int flags,void * arg)6713b3a8eb9SGleb Smirnoff fsk_new(uintptr_t key, int flags, void *arg)
6723b3a8eb9SGleb Smirnoff {
6733b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
6743b3a8eb9SGleb Smirnoff
6753b3a8eb9SGleb Smirnoff fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO);
6763b3a8eb9SGleb Smirnoff if (fs) {
6773b3a8eb9SGleb Smirnoff set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs));
678fe3bcfbdSTom Jones V_dn_cfg.fsk_count++;
6793b3a8eb9SGleb Smirnoff fs->drain_bucket = 0;
680fe3bcfbdSTom Jones SLIST_INSERT_HEAD(&V_dn_cfg.fsu, fs, sch_chain);
6813b3a8eb9SGleb Smirnoff }
6823b3a8eb9SGleb Smirnoff return fs;
6833b3a8eb9SGleb Smirnoff }
6843b3a8eb9SGleb Smirnoff
68591336b40SDon Lewis #ifdef NEW_AQM
68691336b40SDon Lewis /* callback function for cleaning up AQM queue status belongs to a flowset
68791336b40SDon Lewis * connected to scheduler instance '_si' (for !DN_MULTIQUEUE only).
68891336b40SDon Lewis */
68991336b40SDon Lewis static int
si_cleanup_q(void * _si,void * arg)69091336b40SDon Lewis si_cleanup_q(void *_si, void *arg)
69191336b40SDon Lewis {
69291336b40SDon Lewis struct dn_sch_inst *si = _si;
69391336b40SDon Lewis
69491336b40SDon Lewis if (!(si->sched->fp->flags & DN_MULTIQUEUE)) {
69591336b40SDon Lewis if (si->sched->fs->aqmfp && si->sched->fs->aqmfp->cleanup)
69691336b40SDon Lewis si->sched->fs->aqmfp->cleanup((struct dn_queue *) (si+1));
69791336b40SDon Lewis }
69891336b40SDon Lewis return 0;
69991336b40SDon Lewis }
70091336b40SDon Lewis
70191336b40SDon Lewis /* callback to clean up queue AQM status.*/
70291336b40SDon Lewis static int
q_cleanup_q(void * _q,void * arg)70391336b40SDon Lewis q_cleanup_q(void *_q, void *arg)
70491336b40SDon Lewis {
70591336b40SDon Lewis struct dn_queue *q = _q;
70691336b40SDon Lewis q->fs->aqmfp->cleanup(q);
70791336b40SDon Lewis return 0;
70891336b40SDon Lewis }
70991336b40SDon Lewis
71091336b40SDon Lewis /* Clean up all AQM queues status belongs to flowset 'fs' and then
71191336b40SDon Lewis * deconfig AQM for flowset 'fs'
71291336b40SDon Lewis */
71391336b40SDon Lewis static void
aqm_cleanup_deconfig_fs(struct dn_fsk * fs)71491336b40SDon Lewis aqm_cleanup_deconfig_fs(struct dn_fsk *fs)
71591336b40SDon Lewis {
71691336b40SDon Lewis struct dn_sch_inst *si;
71791336b40SDon Lewis
71891336b40SDon Lewis /* clean up AQM status for all queues for !DN_MULTIQUEUE sched*/
71991336b40SDon Lewis if (fs->fs.fs_nr > DN_MAX_ID) {
72091336b40SDon Lewis if (fs->sched && !(fs->sched->fp->flags & DN_MULTIQUEUE)) {
72191336b40SDon Lewis if (fs->sched->sch.flags & DN_HAVE_MASK)
72291336b40SDon Lewis dn_ht_scan(fs->sched->siht, si_cleanup_q, NULL);
72391336b40SDon Lewis else {
72491336b40SDon Lewis /* single si i.e. no sched mask */
72591336b40SDon Lewis si = (struct dn_sch_inst *) fs->sched->siht;
72691336b40SDon Lewis if (si && fs->aqmfp && fs->aqmfp->cleanup)
72791336b40SDon Lewis fs->aqmfp->cleanup((struct dn_queue *) (si+1));
72891336b40SDon Lewis }
72991336b40SDon Lewis }
73091336b40SDon Lewis }
73191336b40SDon Lewis
73291336b40SDon Lewis /* clean up AQM status for all queues for DN_MULTIQUEUE sched*/
73391336b40SDon Lewis if (fs->sched && fs->sched->fp->flags & DN_MULTIQUEUE && fs->qht) {
73491336b40SDon Lewis if (fs->fs.flags & DN_QHT_HASH)
73591336b40SDon Lewis dn_ht_scan(fs->qht, q_cleanup_q, NULL);
73691336b40SDon Lewis else
73791336b40SDon Lewis fs->aqmfp->cleanup((struct dn_queue *)(fs->qht));
73891336b40SDon Lewis }
73991336b40SDon Lewis
74091336b40SDon Lewis /* deconfig AQM */
74191336b40SDon Lewis if(fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig)
74291336b40SDon Lewis fs->aqmfp->deconfig(fs);
74391336b40SDon Lewis }
74491336b40SDon Lewis #endif
74591336b40SDon Lewis
7463b3a8eb9SGleb Smirnoff /*
7473b3a8eb9SGleb Smirnoff * detach flowset from its current scheduler. Flags as follows:
7483b3a8eb9SGleb Smirnoff * DN_DETACH removes from the fsk_list
7493b3a8eb9SGleb Smirnoff * DN_DESTROY deletes individual queues
7503b3a8eb9SGleb Smirnoff * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked).
7513b3a8eb9SGleb Smirnoff */
7523b3a8eb9SGleb Smirnoff static void
fsk_detach(struct dn_fsk * fs,int flags)7533b3a8eb9SGleb Smirnoff fsk_detach(struct dn_fsk *fs, int flags)
7543b3a8eb9SGleb Smirnoff {
7553b3a8eb9SGleb Smirnoff if (flags & DN_DELETE_FS)
7563b3a8eb9SGleb Smirnoff flags |= DN_DESTROY;
7573b3a8eb9SGleb Smirnoff ND("fs %d from sched %d flags %s %s %s",
7583b3a8eb9SGleb Smirnoff fs->fs.fs_nr, fs->fs.sched_nr,
7593b3a8eb9SGleb Smirnoff (flags & DN_DELETE_FS) ? "DEL_FS":"",
7603b3a8eb9SGleb Smirnoff (flags & DN_DESTROY) ? "DEL":"",
7613b3a8eb9SGleb Smirnoff (flags & DN_DETACH) ? "DET":"");
7623b3a8eb9SGleb Smirnoff if (flags & DN_DETACH) { /* detach from the list */
7633b3a8eb9SGleb Smirnoff struct dn_fsk_head *h;
764fe3bcfbdSTom Jones h = fs->sched ? &fs->sched->fsk_list : &V_dn_cfg.fsu;
7653b3a8eb9SGleb Smirnoff SLIST_REMOVE(h, fs, dn_fsk, sch_chain);
7663b3a8eb9SGleb Smirnoff }
7673b3a8eb9SGleb Smirnoff /* Free the RED parameters, they will be recomputed on
7683b3a8eb9SGleb Smirnoff * subsequent attach if needed.
7693b3a8eb9SGleb Smirnoff */
7703b3a8eb9SGleb Smirnoff free(fs->w_q_lookup, M_DUMMYNET);
7713b3a8eb9SGleb Smirnoff fs->w_q_lookup = NULL;
7723b3a8eb9SGleb Smirnoff qht_delete(fs, flags);
77391336b40SDon Lewis #ifdef NEW_AQM
77491336b40SDon Lewis aqm_cleanup_deconfig_fs(fs);
77591336b40SDon Lewis #endif
77691336b40SDon Lewis
7773b3a8eb9SGleb Smirnoff if (fs->sched && fs->sched->fp->free_fsk)
7783b3a8eb9SGleb Smirnoff fs->sched->fp->free_fsk(fs);
7793b3a8eb9SGleb Smirnoff fs->sched = NULL;
7803b3a8eb9SGleb Smirnoff if (flags & DN_DELETE_FS) {
781578acad3SEitan Adler bzero(fs, sizeof(*fs)); /* safety */
7823b3a8eb9SGleb Smirnoff free(fs, M_DUMMYNET);
783fe3bcfbdSTom Jones V_dn_cfg.fsk_count--;
7843b3a8eb9SGleb Smirnoff } else {
785fe3bcfbdSTom Jones SLIST_INSERT_HEAD(&V_dn_cfg.fsu, fs, sch_chain);
7863b3a8eb9SGleb Smirnoff }
7873b3a8eb9SGleb Smirnoff }
7883b3a8eb9SGleb Smirnoff
7893b3a8eb9SGleb Smirnoff /*
7903b3a8eb9SGleb Smirnoff * Detach or destroy all flowsets in a list.
7913b3a8eb9SGleb Smirnoff * flags specifies what to do:
7923b3a8eb9SGleb Smirnoff * DN_DESTROY: flush all queues
7933b3a8eb9SGleb Smirnoff * DN_DELETE_FS: DN_DESTROY + destroy flowset
7943b3a8eb9SGleb Smirnoff * DN_DELETE_FS implies DN_DESTROY
7953b3a8eb9SGleb Smirnoff */
7963b3a8eb9SGleb Smirnoff static void
fsk_detach_list(struct dn_fsk_head * h,int flags)7973b3a8eb9SGleb Smirnoff fsk_detach_list(struct dn_fsk_head *h, int flags)
7983b3a8eb9SGleb Smirnoff {
7993b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
8005c329f0aSDimitry Andric int n __unused = 0; /* only for stats */
8013b3a8eb9SGleb Smirnoff
8023b3a8eb9SGleb Smirnoff ND("head %p flags %x", h, flags);
8033b3a8eb9SGleb Smirnoff while ((fs = SLIST_FIRST(h))) {
8043b3a8eb9SGleb Smirnoff SLIST_REMOVE_HEAD(h, sch_chain);
8053b3a8eb9SGleb Smirnoff n++;
8063b3a8eb9SGleb Smirnoff fsk_detach(fs, flags);
8073b3a8eb9SGleb Smirnoff }
8083b3a8eb9SGleb Smirnoff ND("done %d flowsets", n);
8093b3a8eb9SGleb Smirnoff }
8103b3a8eb9SGleb Smirnoff
8113b3a8eb9SGleb Smirnoff /*
8123b3a8eb9SGleb Smirnoff * called on 'queue X delete' -- removes the flowset from fshash,
8133b3a8eb9SGleb Smirnoff * deletes all queues for the flowset, and removes the flowset.
8143b3a8eb9SGleb Smirnoff */
8153b3a8eb9SGleb Smirnoff static int
delete_fs(int i,int locked)8163b3a8eb9SGleb Smirnoff delete_fs(int i, int locked)
8173b3a8eb9SGleb Smirnoff {
8183b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
8193b3a8eb9SGleb Smirnoff int err = 0;
8203b3a8eb9SGleb Smirnoff
8213b3a8eb9SGleb Smirnoff if (!locked)
8223b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
823fe3bcfbdSTom Jones fs = dn_ht_find(V_dn_cfg.fshash, i, DNHT_REMOVE, NULL);
8243b3a8eb9SGleb Smirnoff ND("fs %d found %p", i, fs);
8253b3a8eb9SGleb Smirnoff if (fs) {
8263b3a8eb9SGleb Smirnoff fsk_detach(fs, DN_DETACH | DN_DELETE_FS);
8273b3a8eb9SGleb Smirnoff err = 0;
8283b3a8eb9SGleb Smirnoff } else
8293b3a8eb9SGleb Smirnoff err = EINVAL;
8303b3a8eb9SGleb Smirnoff if (!locked)
8313b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
8323b3a8eb9SGleb Smirnoff return err;
8333b3a8eb9SGleb Smirnoff }
8343b3a8eb9SGleb Smirnoff
8353b3a8eb9SGleb Smirnoff /*----- end of flowset hashtable support -------------*/
8363b3a8eb9SGleb Smirnoff
8373b3a8eb9SGleb Smirnoff /*------------------------------------------------------------
8383b3a8eb9SGleb Smirnoff * Scheduler hash. When searching by index we pass sched_nr,
8393b3a8eb9SGleb Smirnoff * otherwise we pass struct dn_sch * which is the first field in
8403b3a8eb9SGleb Smirnoff * struct dn_schk so we can cast between the two. We use this trick
8413b3a8eb9SGleb Smirnoff * because in the create phase (but it should be fixed).
8423b3a8eb9SGleb Smirnoff */
8433b3a8eb9SGleb Smirnoff static uint32_t
schk_hash(uintptr_t key,int flags,void * _arg)8443b3a8eb9SGleb Smirnoff schk_hash(uintptr_t key, int flags, void *_arg)
8453b3a8eb9SGleb Smirnoff {
8463b3a8eb9SGleb Smirnoff uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key :
8473b3a8eb9SGleb Smirnoff ((struct dn_schk *)key)->sch.sched_nr;
8483b3a8eb9SGleb Smirnoff return ( (i>>8)^(i>>4)^i );
8493b3a8eb9SGleb Smirnoff }
8503b3a8eb9SGleb Smirnoff
8513b3a8eb9SGleb Smirnoff static int
schk_match(void * obj,uintptr_t key,int flags,void * _arg)8523b3a8eb9SGleb Smirnoff schk_match(void *obj, uintptr_t key, int flags, void *_arg)
8533b3a8eb9SGleb Smirnoff {
8543b3a8eb9SGleb Smirnoff struct dn_schk *s = (struct dn_schk *)obj;
8553b3a8eb9SGleb Smirnoff int i = !(flags & DNHT_KEY_IS_OBJ) ? key :
8563b3a8eb9SGleb Smirnoff ((struct dn_schk *)key)->sch.sched_nr;
8573b3a8eb9SGleb Smirnoff return (s->sch.sched_nr == i);
8583b3a8eb9SGleb Smirnoff }
8593b3a8eb9SGleb Smirnoff
8603b3a8eb9SGleb Smirnoff /*
8613b3a8eb9SGleb Smirnoff * Create the entry and intialize with the sched hash if needed.
8623b3a8eb9SGleb Smirnoff * Leave s->fp unset so we can tell whether a dn_ht_find() returns
8633b3a8eb9SGleb Smirnoff * a new object or a previously existing one.
8643b3a8eb9SGleb Smirnoff */
8653b3a8eb9SGleb Smirnoff static void *
schk_new(uintptr_t key,int flags,void * arg)8663b3a8eb9SGleb Smirnoff schk_new(uintptr_t key, int flags, void *arg)
8673b3a8eb9SGleb Smirnoff {
8683b3a8eb9SGleb Smirnoff struct schk_new_arg *a = arg;
8693b3a8eb9SGleb Smirnoff struct dn_schk *s;
8703b3a8eb9SGleb Smirnoff int l = sizeof(*s) +a->fp->schk_datalen;
8713b3a8eb9SGleb Smirnoff
8723b3a8eb9SGleb Smirnoff s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO);
8733b3a8eb9SGleb Smirnoff if (s == NULL)
8743b3a8eb9SGleb Smirnoff return NULL;
8753b3a8eb9SGleb Smirnoff set_oid(&s->link.oid, DN_LINK, sizeof(s->link));
8763b3a8eb9SGleb Smirnoff s->sch = *a->sch; // copy initial values
8773b3a8eb9SGleb Smirnoff s->link.link_nr = s->sch.sched_nr;
8783b3a8eb9SGleb Smirnoff SLIST_INIT(&s->fsk_list);
8793b3a8eb9SGleb Smirnoff /* initialize the hash table or create the single instance */
8803b3a8eb9SGleb Smirnoff s->fp = a->fp; /* si_new needs this */
8813b3a8eb9SGleb Smirnoff s->drain_bucket = 0;
8823b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) {
8833b3a8eb9SGleb Smirnoff s->siht = dn_ht_init(NULL, s->sch.buckets,
8843b3a8eb9SGleb Smirnoff offsetof(struct dn_sch_inst, si_next),
8853b3a8eb9SGleb Smirnoff si_hash, si_match, si_new);
8863b3a8eb9SGleb Smirnoff if (s->siht == NULL) {
8873b3a8eb9SGleb Smirnoff free(s, M_DUMMYNET);
8883b3a8eb9SGleb Smirnoff return NULL;
8893b3a8eb9SGleb Smirnoff }
8903b3a8eb9SGleb Smirnoff }
8913b3a8eb9SGleb Smirnoff s->fp = NULL; /* mark as a new scheduler */
892fe3bcfbdSTom Jones V_dn_cfg.schk_count++;
8933b3a8eb9SGleb Smirnoff return s;
8943b3a8eb9SGleb Smirnoff }
8953b3a8eb9SGleb Smirnoff
8963b3a8eb9SGleb Smirnoff /*
8973b3a8eb9SGleb Smirnoff * Callback for sched delete. Notify all attached flowsets to
8983b3a8eb9SGleb Smirnoff * detach from the scheduler, destroy the internal flowset, and
8993b3a8eb9SGleb Smirnoff * all instances. The scheduler goes away too.
9003b3a8eb9SGleb Smirnoff * arg is 0 (only detach flowsets and destroy instances)
9013b3a8eb9SGleb Smirnoff * DN_DESTROY (detach & delete queues, delete schk)
9023b3a8eb9SGleb Smirnoff * or DN_DELETE_FS (delete queues and flowsets, delete schk)
9033b3a8eb9SGleb Smirnoff */
9043b3a8eb9SGleb Smirnoff static int
schk_delete_cb(void * obj,void * arg)9053b3a8eb9SGleb Smirnoff schk_delete_cb(void *obj, void *arg)
9063b3a8eb9SGleb Smirnoff {
9073b3a8eb9SGleb Smirnoff struct dn_schk *s = obj;
9083b3a8eb9SGleb Smirnoff #if 0
9093b3a8eb9SGleb Smirnoff int a = (int)arg;
9103b3a8eb9SGleb Smirnoff ND("sched %d arg %s%s",
9113b3a8eb9SGleb Smirnoff s->sch.sched_nr,
9123b3a8eb9SGleb Smirnoff a&DN_DESTROY ? "DEL ":"",
9133b3a8eb9SGleb Smirnoff a&DN_DELETE_FS ? "DEL_FS":"");
9143b3a8eb9SGleb Smirnoff #endif
9153b3a8eb9SGleb Smirnoff fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0);
9163b3a8eb9SGleb Smirnoff /* no more flowset pointing to us now */
9173b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) {
9183b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, si_destroy, NULL);
9193b3a8eb9SGleb Smirnoff dn_ht_free(s->siht, 0);
9203b3a8eb9SGleb Smirnoff } else if (s->siht)
9213b3a8eb9SGleb Smirnoff si_destroy(s->siht, NULL);
9221b2dbe37SKristof Provost
9233b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET);
9243b3a8eb9SGleb Smirnoff s->profile = NULL;
9253b3a8eb9SGleb Smirnoff s->siht = NULL;
9263b3a8eb9SGleb Smirnoff if (s->fp->destroy)
9273b3a8eb9SGleb Smirnoff s->fp->destroy(s);
9283b3a8eb9SGleb Smirnoff bzero(s, sizeof(*s)); // safety
9293b3a8eb9SGleb Smirnoff free(obj, M_DUMMYNET);
930fe3bcfbdSTom Jones V_dn_cfg.schk_count--;
9313b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL;
9323b3a8eb9SGleb Smirnoff }
9333b3a8eb9SGleb Smirnoff
9343b3a8eb9SGleb Smirnoff /*
9353b3a8eb9SGleb Smirnoff * called on a 'sched X delete' command. Deletes a single scheduler.
9363b3a8eb9SGleb Smirnoff * This is done by removing from the schedhash, unlinking all
9373b3a8eb9SGleb Smirnoff * flowsets and deleting their traffic.
9383b3a8eb9SGleb Smirnoff */
9393b3a8eb9SGleb Smirnoff static int
delete_schk(int i)9403b3a8eb9SGleb Smirnoff delete_schk(int i)
9413b3a8eb9SGleb Smirnoff {
9423b3a8eb9SGleb Smirnoff struct dn_schk *s;
9433b3a8eb9SGleb Smirnoff
944fe3bcfbdSTom Jones s = dn_ht_find(V_dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
9453b3a8eb9SGleb Smirnoff ND("%d %p", i, s);
9463b3a8eb9SGleb Smirnoff if (!s)
9473b3a8eb9SGleb Smirnoff return EINVAL;
9483b3a8eb9SGleb Smirnoff delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */
9493b3a8eb9SGleb Smirnoff /* then detach flowsets, delete traffic */
9503b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY);
9513b3a8eb9SGleb Smirnoff return 0;
9523b3a8eb9SGleb Smirnoff }
9533b3a8eb9SGleb Smirnoff /*--- end of schk hashtable support ---*/
9543b3a8eb9SGleb Smirnoff
9553b3a8eb9SGleb Smirnoff static int
copy_obj(char ** start,char * end,void * _o,const char * msg,int i)9563b3a8eb9SGleb Smirnoff copy_obj(char **start, char *end, void *_o, const char *msg, int i)
9573b3a8eb9SGleb Smirnoff {
9580ac43d97SMarius Strobl struct dn_id o;
9590ac43d97SMarius Strobl union {
9600ac43d97SMarius Strobl struct dn_link l;
9610ac43d97SMarius Strobl struct dn_schk s;
9620ac43d97SMarius Strobl } dn;
9633b3a8eb9SGleb Smirnoff int have = end - *start;
9643b3a8eb9SGleb Smirnoff
9650ac43d97SMarius Strobl memcpy(&o, _o, sizeof(o));
9660ac43d97SMarius Strobl if (have < o.len || o.len == 0 || o.type == 0) {
9673b3a8eb9SGleb Smirnoff D("(WARN) type %d %s %d have %d need %d",
9680ac43d97SMarius Strobl o.type, msg, i, have, o.len);
9693b3a8eb9SGleb Smirnoff return 1;
9703b3a8eb9SGleb Smirnoff }
9710ac43d97SMarius Strobl ND("type %d %s %d len %d", o.type, msg, i, o.len);
9720ac43d97SMarius Strobl if (o.type == DN_LINK) {
9730ac43d97SMarius Strobl memcpy(&dn.l, _o, sizeof(dn.l));
9743b3a8eb9SGleb Smirnoff /* Adjust burst parameter for link */
9750ac43d97SMarius Strobl dn.l.burst = div64(dn.l.burst, 8 * hz);
9760ac43d97SMarius Strobl dn.l.delay = dn.l.delay * 1000 / hz;
9770ac43d97SMarius Strobl memcpy(*start, &dn.l, sizeof(dn.l));
9780ac43d97SMarius Strobl } else if (o.type == DN_SCH) {
9790ac43d97SMarius Strobl /* Set dn.s.sch.oid.id to the number of instances */
9800ac43d97SMarius Strobl memcpy(&dn.s, _o, sizeof(dn.s));
9810ac43d97SMarius Strobl dn.s.sch.oid.id = (dn.s.sch.flags & DN_HAVE_MASK) ?
9820ac43d97SMarius Strobl dn_ht_entries(dn.s.siht) : (dn.s.siht ? 1 : 0);
9830ac43d97SMarius Strobl memcpy(*start, &dn.s, sizeof(dn.s));
9840ac43d97SMarius Strobl } else
9850ac43d97SMarius Strobl memcpy(*start, _o, o.len);
9860ac43d97SMarius Strobl *start += o.len;
9873b3a8eb9SGleb Smirnoff return 0;
9883b3a8eb9SGleb Smirnoff }
9893b3a8eb9SGleb Smirnoff
9903b3a8eb9SGleb Smirnoff /* Specific function to copy a queue.
9913b3a8eb9SGleb Smirnoff * Copies only the user-visible part of a queue (which is in
9923b3a8eb9SGleb Smirnoff * a struct dn_flow), and sets len accordingly.
9933b3a8eb9SGleb Smirnoff */
9943b3a8eb9SGleb Smirnoff static int
copy_obj_q(char ** start,char * end,void * _o,const char * msg,int i)9953b3a8eb9SGleb Smirnoff copy_obj_q(char **start, char *end, void *_o, const char *msg, int i)
9963b3a8eb9SGleb Smirnoff {
9973b3a8eb9SGleb Smirnoff struct dn_id *o = _o;
9983b3a8eb9SGleb Smirnoff int have = end - *start;
9993b3a8eb9SGleb Smirnoff int len = sizeof(struct dn_flow); /* see above comment */
10003b3a8eb9SGleb Smirnoff
10013b3a8eb9SGleb Smirnoff if (have < len || o->len == 0 || o->type != DN_QUEUE) {
10023b3a8eb9SGleb Smirnoff D("ERROR type %d %s %d have %d need %d",
10033b3a8eb9SGleb Smirnoff o->type, msg, i, have, len);
10043b3a8eb9SGleb Smirnoff return 1;
10053b3a8eb9SGleb Smirnoff }
10063b3a8eb9SGleb Smirnoff ND("type %d %s %d len %d", o->type, msg, i, len);
10070ac43d97SMarius Strobl memcpy(*start, _o, len);
10083b3a8eb9SGleb Smirnoff ((struct dn_id*)(*start))->len = len;
10093b3a8eb9SGleb Smirnoff *start += len;
10103b3a8eb9SGleb Smirnoff return 0;
10113b3a8eb9SGleb Smirnoff }
10123b3a8eb9SGleb Smirnoff
10133b3a8eb9SGleb Smirnoff static int
copy_q_cb(void * obj,void * arg)10143b3a8eb9SGleb Smirnoff copy_q_cb(void *obj, void *arg)
10153b3a8eb9SGleb Smirnoff {
10163b3a8eb9SGleb Smirnoff struct dn_queue *q = obj;
10173b3a8eb9SGleb Smirnoff struct copy_args *a = arg;
10183b3a8eb9SGleb Smirnoff struct dn_flow *ni = (struct dn_flow *)(*a->start);
10193b3a8eb9SGleb Smirnoff if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1))
10203b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
10213b3a8eb9SGleb Smirnoff ni->oid.type = DN_FLOW; /* override the DN_QUEUE */
10223b3a8eb9SGleb Smirnoff ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL);
10233b3a8eb9SGleb Smirnoff return 0;
10243b3a8eb9SGleb Smirnoff }
10253b3a8eb9SGleb Smirnoff
10263b3a8eb9SGleb Smirnoff static int
copy_q(struct copy_args * a,struct dn_fsk * fs,int flags)10273b3a8eb9SGleb Smirnoff copy_q(struct copy_args *a, struct dn_fsk *fs, int flags)
10283b3a8eb9SGleb Smirnoff {
10293b3a8eb9SGleb Smirnoff if (!fs->qht)
10303b3a8eb9SGleb Smirnoff return 0;
10313b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH)
10323b3a8eb9SGleb Smirnoff dn_ht_scan(fs->qht, copy_q_cb, a);
10333b3a8eb9SGleb Smirnoff else
10343b3a8eb9SGleb Smirnoff copy_q_cb(fs->qht, a);
10353b3a8eb9SGleb Smirnoff return 0;
10363b3a8eb9SGleb Smirnoff }
10373b3a8eb9SGleb Smirnoff
10383b3a8eb9SGleb Smirnoff /*
10393b3a8eb9SGleb Smirnoff * This routine only copies the initial part of a profile ? XXX
10403b3a8eb9SGleb Smirnoff */
10413b3a8eb9SGleb Smirnoff static int
copy_profile(struct copy_args * a,struct dn_profile * p)10423b3a8eb9SGleb Smirnoff copy_profile(struct copy_args *a, struct dn_profile *p)
10433b3a8eb9SGleb Smirnoff {
10443b3a8eb9SGleb Smirnoff int have = a->end - *a->start;
10453b3a8eb9SGleb Smirnoff /* XXX here we check for max length */
10463b3a8eb9SGleb Smirnoff int profile_len = sizeof(struct dn_profile) -
10473b3a8eb9SGleb Smirnoff ED_MAX_SAMPLES_NO*sizeof(int);
10483b3a8eb9SGleb Smirnoff
10493b3a8eb9SGleb Smirnoff if (p == NULL)
10503b3a8eb9SGleb Smirnoff return 0;
10513b3a8eb9SGleb Smirnoff if (have < profile_len) {
10523b3a8eb9SGleb Smirnoff D("error have %d need %d", have, profile_len);
10533b3a8eb9SGleb Smirnoff return 1;
10543b3a8eb9SGleb Smirnoff }
10550ac43d97SMarius Strobl memcpy(*a->start, p, profile_len);
10563b3a8eb9SGleb Smirnoff ((struct dn_id *)(*a->start))->len = profile_len;
10573b3a8eb9SGleb Smirnoff *a->start += profile_len;
10583b3a8eb9SGleb Smirnoff return 0;
10593b3a8eb9SGleb Smirnoff }
10603b3a8eb9SGleb Smirnoff
10613b3a8eb9SGleb Smirnoff static int
copy_flowset(struct copy_args * a,struct dn_fsk * fs,int flags)10623b3a8eb9SGleb Smirnoff copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags)
10633b3a8eb9SGleb Smirnoff {
10643b3a8eb9SGleb Smirnoff struct dn_fs *ufs = (struct dn_fs *)(*a->start);
10653b3a8eb9SGleb Smirnoff if (!fs)
10663b3a8eb9SGleb Smirnoff return 0;
10673b3a8eb9SGleb Smirnoff ND("flowset %d", fs->fs.fs_nr);
10683b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr))
10693b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
10703b3a8eb9SGleb Smirnoff ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ?
10713b3a8eb9SGleb Smirnoff dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0);
10723b3a8eb9SGleb Smirnoff if (flags) { /* copy queues */
10733b3a8eb9SGleb Smirnoff copy_q(a, fs, 0);
10743b3a8eb9SGleb Smirnoff }
10753b3a8eb9SGleb Smirnoff return 0;
10763b3a8eb9SGleb Smirnoff }
10773b3a8eb9SGleb Smirnoff
10783b3a8eb9SGleb Smirnoff static int
copy_si_cb(void * obj,void * arg)10793b3a8eb9SGleb Smirnoff copy_si_cb(void *obj, void *arg)
10803b3a8eb9SGleb Smirnoff {
10813b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = obj;
10823b3a8eb9SGleb Smirnoff struct copy_args *a = arg;
10833b3a8eb9SGleb Smirnoff struct dn_flow *ni = (struct dn_flow *)(*a->start);
10843b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, &si->ni, "inst",
10853b3a8eb9SGleb Smirnoff si->sched->sch.sched_nr))
10863b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
10873b3a8eb9SGleb Smirnoff ni->oid.type = DN_FLOW; /* override the DN_SCH_I */
10883b3a8eb9SGleb Smirnoff ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL);
10893b3a8eb9SGleb Smirnoff return 0;
10903b3a8eb9SGleb Smirnoff }
10913b3a8eb9SGleb Smirnoff
10923b3a8eb9SGleb Smirnoff static int
copy_si(struct copy_args * a,struct dn_schk * s,int flags)10933b3a8eb9SGleb Smirnoff copy_si(struct copy_args *a, struct dn_schk *s, int flags)
10943b3a8eb9SGleb Smirnoff {
10953b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK)
10963b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, copy_si_cb, a);
10973b3a8eb9SGleb Smirnoff else if (s->siht)
10983b3a8eb9SGleb Smirnoff copy_si_cb(s->siht, a);
10993b3a8eb9SGleb Smirnoff return 0;
11003b3a8eb9SGleb Smirnoff }
11013b3a8eb9SGleb Smirnoff
11023b3a8eb9SGleb Smirnoff /*
11033b3a8eb9SGleb Smirnoff * compute a list of children of a scheduler and copy up
11043b3a8eb9SGleb Smirnoff */
11053b3a8eb9SGleb Smirnoff static int
copy_fsk_list(struct copy_args * a,struct dn_schk * s,int flags)11063b3a8eb9SGleb Smirnoff copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags)
11073b3a8eb9SGleb Smirnoff {
11083b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
11093b3a8eb9SGleb Smirnoff struct dn_id *o;
11103b3a8eb9SGleb Smirnoff uint32_t *p;
11113b3a8eb9SGleb Smirnoff
11123b3a8eb9SGleb Smirnoff int n = 0, space = sizeof(*o);
11133b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
11143b3a8eb9SGleb Smirnoff if (fs->fs.fs_nr < DN_MAX_ID)
11153b3a8eb9SGleb Smirnoff n++;
11163b3a8eb9SGleb Smirnoff }
11173b3a8eb9SGleb Smirnoff space += n * sizeof(uint32_t);
11183b3a8eb9SGleb Smirnoff DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n);
11193b3a8eb9SGleb Smirnoff if (a->end - *(a->start) < space)
11203b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11213b3a8eb9SGleb Smirnoff o = (struct dn_id *)(*(a->start));
11223b3a8eb9SGleb Smirnoff o->len = space;
11233b3a8eb9SGleb Smirnoff *a->start += o->len;
11243b3a8eb9SGleb Smirnoff o->type = DN_TEXT;
11253b3a8eb9SGleb Smirnoff p = (uint32_t *)(o+1);
11263b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain)
11273b3a8eb9SGleb Smirnoff if (fs->fs.fs_nr < DN_MAX_ID)
11283b3a8eb9SGleb Smirnoff *p++ = fs->fs.fs_nr;
11293b3a8eb9SGleb Smirnoff return 0;
11303b3a8eb9SGleb Smirnoff }
11313b3a8eb9SGleb Smirnoff
11323b3a8eb9SGleb Smirnoff static int
copy_data_helper(void * _o,void * _arg)11333b3a8eb9SGleb Smirnoff copy_data_helper(void *_o, void *_arg)
11343b3a8eb9SGleb Smirnoff {
11353b3a8eb9SGleb Smirnoff struct copy_args *a = _arg;
11363b3a8eb9SGleb Smirnoff uint32_t *r = a->extra->r; /* start of first range */
11373b3a8eb9SGleb Smirnoff uint32_t *lim; /* first invalid pointer */
11383b3a8eb9SGleb Smirnoff int n;
11393b3a8eb9SGleb Smirnoff
11403b3a8eb9SGleb Smirnoff lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len);
11413b3a8eb9SGleb Smirnoff
11423b3a8eb9SGleb Smirnoff if (a->type == DN_LINK || a->type == DN_SCH) {
11433b3a8eb9SGleb Smirnoff /* pipe|sched show, we receive a dn_schk */
11443b3a8eb9SGleb Smirnoff struct dn_schk *s = _o;
11453b3a8eb9SGleb Smirnoff
11463b3a8eb9SGleb Smirnoff n = s->sch.sched_nr;
11473b3a8eb9SGleb Smirnoff if (a->type == DN_SCH && n >= DN_MAX_ID)
11483b3a8eb9SGleb Smirnoff return 0; /* not a scheduler */
11493b3a8eb9SGleb Smirnoff if (a->type == DN_LINK && n <= DN_MAX_ID)
11503b3a8eb9SGleb Smirnoff return 0; /* not a pipe */
11513b3a8eb9SGleb Smirnoff
11523b3a8eb9SGleb Smirnoff /* see if the object is within one of our ranges */
11533b3a8eb9SGleb Smirnoff for (;r < lim; r += 2) {
11543b3a8eb9SGleb Smirnoff if (n < r[0] || n > r[1])
11553b3a8eb9SGleb Smirnoff continue;
11563b3a8eb9SGleb Smirnoff /* Found a valid entry, copy and we are done */
11573b3a8eb9SGleb Smirnoff if (a->flags & DN_C_LINK) {
11583b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end,
11593b3a8eb9SGleb Smirnoff &s->link, "link", n))
11603b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11613b3a8eb9SGleb Smirnoff if (copy_profile(a, s->profile))
11623b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11633b3a8eb9SGleb Smirnoff if (copy_flowset(a, s->fs, 0))
11643b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11653b3a8eb9SGleb Smirnoff }
11663b3a8eb9SGleb Smirnoff if (a->flags & DN_C_SCH) {
11673b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end,
11683b3a8eb9SGleb Smirnoff &s->sch, "sched", n))
11693b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11703b3a8eb9SGleb Smirnoff /* list all attached flowsets */
11713b3a8eb9SGleb Smirnoff if (copy_fsk_list(a, s, 0))
11723b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11733b3a8eb9SGleb Smirnoff }
11743b3a8eb9SGleb Smirnoff if (a->flags & DN_C_FLOW)
11753b3a8eb9SGleb Smirnoff copy_si(a, s, 0);
11763b3a8eb9SGleb Smirnoff break;
11773b3a8eb9SGleb Smirnoff }
11783b3a8eb9SGleb Smirnoff } else if (a->type == DN_FS) {
11793b3a8eb9SGleb Smirnoff /* queue show, skip internal flowsets */
11803b3a8eb9SGleb Smirnoff struct dn_fsk *fs = _o;
11813b3a8eb9SGleb Smirnoff
11823b3a8eb9SGleb Smirnoff n = fs->fs.fs_nr;
11833b3a8eb9SGleb Smirnoff if (n >= DN_MAX_ID)
11843b3a8eb9SGleb Smirnoff return 0;
11853b3a8eb9SGleb Smirnoff /* see if the object is within one of our ranges */
11863b3a8eb9SGleb Smirnoff for (;r < lim; r += 2) {
11873b3a8eb9SGleb Smirnoff if (n < r[0] || n > r[1])
11883b3a8eb9SGleb Smirnoff continue;
11893b3a8eb9SGleb Smirnoff if (copy_flowset(a, fs, 0))
11903b3a8eb9SGleb Smirnoff return DNHT_SCAN_END;
11913b3a8eb9SGleb Smirnoff copy_q(a, fs, 0);
11923b3a8eb9SGleb Smirnoff break; /* we are done */
11933b3a8eb9SGleb Smirnoff }
11943b3a8eb9SGleb Smirnoff }
11953b3a8eb9SGleb Smirnoff return 0;
11963b3a8eb9SGleb Smirnoff }
11973b3a8eb9SGleb Smirnoff
11983b3a8eb9SGleb Smirnoff static inline struct dn_schk *
locate_scheduler(int i)11993b3a8eb9SGleb Smirnoff locate_scheduler(int i)
12003b3a8eb9SGleb Smirnoff {
1201fe3bcfbdSTom Jones return dn_ht_find(V_dn_cfg.schedhash, i, 0, NULL);
12023b3a8eb9SGleb Smirnoff }
12033b3a8eb9SGleb Smirnoff
12043b3a8eb9SGleb Smirnoff /*
12053b3a8eb9SGleb Smirnoff * red parameters are in fixed point arithmetic.
12063b3a8eb9SGleb Smirnoff */
12073b3a8eb9SGleb Smirnoff static int
config_red(struct dn_fsk * fs)12083b3a8eb9SGleb Smirnoff config_red(struct dn_fsk *fs)
12093b3a8eb9SGleb Smirnoff {
12103b3a8eb9SGleb Smirnoff int64_t s, idle, weight, w0;
12113b3a8eb9SGleb Smirnoff int t, i;
12123b3a8eb9SGleb Smirnoff
12133b3a8eb9SGleb Smirnoff fs->w_q = fs->fs.w_q;
12143b3a8eb9SGleb Smirnoff fs->max_p = fs->fs.max_p;
12153b3a8eb9SGleb Smirnoff ND("called");
12163b3a8eb9SGleb Smirnoff /* Doing stuff that was in userland */
12173b3a8eb9SGleb Smirnoff i = fs->sched->link.bandwidth;
12183b3a8eb9SGleb Smirnoff s = (i <= 0) ? 0 :
1219fe3bcfbdSTom Jones hz * V_dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i;
12203b3a8eb9SGleb Smirnoff
12213b3a8eb9SGleb Smirnoff idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */
1222fe3bcfbdSTom Jones fs->lookup_step = div64(idle , V_dn_cfg.red_lookup_depth);
12233b3a8eb9SGleb Smirnoff /* fs->lookup_step not scaled, */
12243b3a8eb9SGleb Smirnoff if (!fs->lookup_step)
12253b3a8eb9SGleb Smirnoff fs->lookup_step = 1;
12263b3a8eb9SGleb Smirnoff w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled
12273b3a8eb9SGleb Smirnoff
12283b3a8eb9SGleb Smirnoff for (t = fs->lookup_step; t > 1; --t)
12293b3a8eb9SGleb Smirnoff weight = SCALE_MUL(weight, w0);
12303b3a8eb9SGleb Smirnoff fs->lookup_weight = (int)(weight); // scaled
12313b3a8eb9SGleb Smirnoff
12323b3a8eb9SGleb Smirnoff /* Now doing stuff that was in kerneland */
12333b3a8eb9SGleb Smirnoff fs->min_th = SCALE(fs->fs.min_th);
12343b3a8eb9SGleb Smirnoff fs->max_th = SCALE(fs->fs.max_th);
12353b3a8eb9SGleb Smirnoff
1236fc5e1956SHiren Panchasara if (fs->fs.max_th == fs->fs.min_th)
1237fc5e1956SHiren Panchasara fs->c_1 = fs->max_p;
1238fc5e1956SHiren Panchasara else
1239fc5e1956SHiren Panchasara fs->c_1 = SCALE((int64_t)(fs->max_p)) / (fs->fs.max_th - fs->fs.min_th);
12403b3a8eb9SGleb Smirnoff fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th));
12413b3a8eb9SGleb Smirnoff
12423b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_IS_GENTLE_RED) {
12433b3a8eb9SGleb Smirnoff fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th;
12443b3a8eb9SGleb Smirnoff fs->c_4 = SCALE(1) - 2 * fs->max_p;
12453b3a8eb9SGleb Smirnoff }
12463b3a8eb9SGleb Smirnoff
12473b3a8eb9SGleb Smirnoff /* If the lookup table already exist, free and create it again. */
12483b3a8eb9SGleb Smirnoff free(fs->w_q_lookup, M_DUMMYNET);
12493b3a8eb9SGleb Smirnoff fs->w_q_lookup = NULL;
1250fe3bcfbdSTom Jones if (V_dn_cfg.red_lookup_depth == 0) {
12513b3a8eb9SGleb Smirnoff printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth"
12523b3a8eb9SGleb Smirnoff "must be > 0\n");
12533b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_RED;
12543b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_GENTLE_RED;
12553b3a8eb9SGleb Smirnoff return (EINVAL);
12563b3a8eb9SGleb Smirnoff }
1257fe3bcfbdSTom Jones fs->lookup_depth = V_dn_cfg.red_lookup_depth;
12583b3a8eb9SGleb Smirnoff fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int),
12593b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT);
12603b3a8eb9SGleb Smirnoff if (fs->w_q_lookup == NULL) {
12613b3a8eb9SGleb Smirnoff printf("dummynet: sorry, cannot allocate red lookup table\n");
12623b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_RED;
12633b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_GENTLE_RED;
12643b3a8eb9SGleb Smirnoff return(ENOSPC);
12653b3a8eb9SGleb Smirnoff }
12663b3a8eb9SGleb Smirnoff
12673b3a8eb9SGleb Smirnoff /* Fill the lookup table with (1 - w_q)^x */
12683b3a8eb9SGleb Smirnoff fs->w_q_lookup[0] = SCALE(1) - fs->w_q;
12693b3a8eb9SGleb Smirnoff
12703b3a8eb9SGleb Smirnoff for (i = 1; i < fs->lookup_depth; i++)
12713b3a8eb9SGleb Smirnoff fs->w_q_lookup[i] =
12723b3a8eb9SGleb Smirnoff SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight);
12733b3a8eb9SGleb Smirnoff
1274fe3bcfbdSTom Jones if (V_dn_cfg.red_avg_pkt_size < 1)
1275fe3bcfbdSTom Jones V_dn_cfg.red_avg_pkt_size = 512;
1276fe3bcfbdSTom Jones fs->avg_pkt_size = V_dn_cfg.red_avg_pkt_size;
1277fe3bcfbdSTom Jones if (V_dn_cfg.red_max_pkt_size < 1)
1278fe3bcfbdSTom Jones V_dn_cfg.red_max_pkt_size = 1500;
1279fe3bcfbdSTom Jones fs->max_pkt_size = V_dn_cfg.red_max_pkt_size;
12803b3a8eb9SGleb Smirnoff ND("exit");
12813b3a8eb9SGleb Smirnoff return 0;
12823b3a8eb9SGleb Smirnoff }
12833b3a8eb9SGleb Smirnoff
12843b3a8eb9SGleb Smirnoff /* Scan all flowset attached to this scheduler and update red */
12853b3a8eb9SGleb Smirnoff static void
update_red(struct dn_schk * s)12863b3a8eb9SGleb Smirnoff update_red(struct dn_schk *s)
12873b3a8eb9SGleb Smirnoff {
12883b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
12893b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain) {
12903b3a8eb9SGleb Smirnoff if (fs && (fs->fs.flags & DN_IS_RED))
12913b3a8eb9SGleb Smirnoff config_red(fs);
12923b3a8eb9SGleb Smirnoff }
12933b3a8eb9SGleb Smirnoff }
12943b3a8eb9SGleb Smirnoff
12953b3a8eb9SGleb Smirnoff /* attach flowset to scheduler s, possibly requeue */
12963b3a8eb9SGleb Smirnoff static void
fsk_attach(struct dn_fsk * fs,struct dn_schk * s)12973b3a8eb9SGleb Smirnoff fsk_attach(struct dn_fsk *fs, struct dn_schk *s)
12983b3a8eb9SGleb Smirnoff {
12993b3a8eb9SGleb Smirnoff ND("remove fs %d from fsunlinked, link to sched %d",
13003b3a8eb9SGleb Smirnoff fs->fs.fs_nr, s->sch.sched_nr);
1301fe3bcfbdSTom Jones SLIST_REMOVE(&V_dn_cfg.fsu, fs, dn_fsk, sch_chain);
13023b3a8eb9SGleb Smirnoff fs->sched = s;
13033b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain);
13043b3a8eb9SGleb Smirnoff if (s->fp->new_fsk)
13053b3a8eb9SGleb Smirnoff s->fp->new_fsk(fs);
13063b3a8eb9SGleb Smirnoff /* XXX compute fsk_mask */
13073b3a8eb9SGleb Smirnoff fs->fsk_mask = fs->fs.flow_mask;
13083b3a8eb9SGleb Smirnoff if (fs->sched->sch.flags & DN_HAVE_MASK)
13093b3a8eb9SGleb Smirnoff flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask);
13103b3a8eb9SGleb Smirnoff if (fs->qht) {
13113b3a8eb9SGleb Smirnoff /*
13123b3a8eb9SGleb Smirnoff * we must drain qht according to the old
13133b3a8eb9SGleb Smirnoff * type, and reinsert according to the new one.
13143b3a8eb9SGleb Smirnoff * The requeue is complex -- in general we need to
13153b3a8eb9SGleb Smirnoff * reclassify every single packet.
13163b3a8eb9SGleb Smirnoff * For the time being, let's hope qht is never set
13173b3a8eb9SGleb Smirnoff * when we reach this point.
13183b3a8eb9SGleb Smirnoff */
13193b3a8eb9SGleb Smirnoff D("XXX TODO requeue from fs %d to sch %d",
13203b3a8eb9SGleb Smirnoff fs->fs.fs_nr, s->sch.sched_nr);
13213b3a8eb9SGleb Smirnoff fs->qht = NULL;
13223b3a8eb9SGleb Smirnoff }
13233b3a8eb9SGleb Smirnoff /* set the new type for qht */
13243b3a8eb9SGleb Smirnoff if (nonzero_mask(&fs->fsk_mask))
13253b3a8eb9SGleb Smirnoff fs->fs.flags |= DN_QHT_HASH;
13263b3a8eb9SGleb Smirnoff else
13273b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_QHT_HASH;
13283b3a8eb9SGleb Smirnoff
13293b3a8eb9SGleb Smirnoff /* XXX config_red() can fail... */
13303b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_IS_RED)
13313b3a8eb9SGleb Smirnoff config_red(fs);
13323b3a8eb9SGleb Smirnoff }
13333b3a8eb9SGleb Smirnoff
13343b3a8eb9SGleb Smirnoff /* update all flowsets which may refer to this scheduler */
13353b3a8eb9SGleb Smirnoff static void
update_fs(struct dn_schk * s)13363b3a8eb9SGleb Smirnoff update_fs(struct dn_schk *s)
13373b3a8eb9SGleb Smirnoff {
13383b3a8eb9SGleb Smirnoff struct dn_fsk *fs, *tmp;
13393b3a8eb9SGleb Smirnoff
1340fe3bcfbdSTom Jones SLIST_FOREACH_SAFE(fs, &V_dn_cfg.fsu, sch_chain, tmp) {
13413b3a8eb9SGleb Smirnoff if (s->sch.sched_nr != fs->fs.sched_nr) {
13423b3a8eb9SGleb Smirnoff D("fs %d for sch %d not %d still unlinked",
13433b3a8eb9SGleb Smirnoff fs->fs.fs_nr, fs->fs.sched_nr,
13443b3a8eb9SGleb Smirnoff s->sch.sched_nr);
13453b3a8eb9SGleb Smirnoff continue;
13463b3a8eb9SGleb Smirnoff }
13473b3a8eb9SGleb Smirnoff fsk_attach(fs, s);
13483b3a8eb9SGleb Smirnoff }
13493b3a8eb9SGleb Smirnoff }
13503b3a8eb9SGleb Smirnoff
135191336b40SDon Lewis #ifdef NEW_AQM
135291336b40SDon Lewis /* Retrieve AQM configurations to ipfw userland
135391336b40SDon Lewis */
135491336b40SDon Lewis static int
get_aqm_parms(struct sockopt * sopt)135591336b40SDon Lewis get_aqm_parms(struct sockopt *sopt)
135691336b40SDon Lewis {
135791336b40SDon Lewis struct dn_extra_parms *ep;
135891336b40SDon Lewis struct dn_fsk *fs;
135991336b40SDon Lewis size_t sopt_valsize;
136091336b40SDon Lewis int l, err = 0;
136191336b40SDon Lewis
136291336b40SDon Lewis sopt_valsize = sopt->sopt_valsize;
136391336b40SDon Lewis l = sizeof(*ep);
136491336b40SDon Lewis if (sopt->sopt_valsize < l) {
136591336b40SDon Lewis D("bad len sopt->sopt_valsize %d len %d",
136691336b40SDon Lewis (int) sopt->sopt_valsize , l);
136791336b40SDon Lewis err = EINVAL;
136891336b40SDon Lewis return err;
136991336b40SDon Lewis }
137051d73df1SKristof Provost ep = malloc(l, M_DUMMYNET, M_NOWAIT);
137191336b40SDon Lewis if(!ep) {
137291336b40SDon Lewis err = ENOMEM ;
137391336b40SDon Lewis return err;
137491336b40SDon Lewis }
137591336b40SDon Lewis do {
137691336b40SDon Lewis err = sooptcopyin(sopt, ep, l, l);
137791336b40SDon Lewis if(err)
137891336b40SDon Lewis break;
137991336b40SDon Lewis sopt->sopt_valsize = sopt_valsize;
138091336b40SDon Lewis if (ep->oid.len < l) {
138191336b40SDon Lewis err = EINVAL;
138291336b40SDon Lewis break;
138391336b40SDon Lewis }
138491336b40SDon Lewis
1385fe3bcfbdSTom Jones fs = dn_ht_find(V_dn_cfg.fshash, ep->nr, 0, NULL);
138691336b40SDon Lewis if (!fs) {
138791336b40SDon Lewis D("fs %d not found", ep->nr);
138891336b40SDon Lewis err = EINVAL;
138991336b40SDon Lewis break;
139091336b40SDon Lewis }
139191336b40SDon Lewis
139291336b40SDon Lewis if (fs->aqmfp && fs->aqmfp->getconfig) {
139391336b40SDon Lewis if(fs->aqmfp->getconfig(fs, ep)) {
139491336b40SDon Lewis D("Error while trying to get AQM params");
139591336b40SDon Lewis err = EINVAL;
139691336b40SDon Lewis break;
139791336b40SDon Lewis }
139891336b40SDon Lewis ep->oid.len = l;
139991336b40SDon Lewis err = sooptcopyout(sopt, ep, l);
140091336b40SDon Lewis }
140191336b40SDon Lewis }while(0);
140291336b40SDon Lewis
140391336b40SDon Lewis free(ep, M_DUMMYNET);
140491336b40SDon Lewis return err;
140591336b40SDon Lewis }
140691336b40SDon Lewis
140791336b40SDon Lewis /* Retrieve AQM configurations to ipfw userland
140891336b40SDon Lewis */
140991336b40SDon Lewis static int
get_sched_parms(struct sockopt * sopt)141091336b40SDon Lewis get_sched_parms(struct sockopt *sopt)
141191336b40SDon Lewis {
141291336b40SDon Lewis struct dn_extra_parms *ep;
141391336b40SDon Lewis struct dn_schk *schk;
141491336b40SDon Lewis size_t sopt_valsize;
141591336b40SDon Lewis int l, err = 0;
141691336b40SDon Lewis
141791336b40SDon Lewis sopt_valsize = sopt->sopt_valsize;
141891336b40SDon Lewis l = sizeof(*ep);
141991336b40SDon Lewis if (sopt->sopt_valsize < l) {
142091336b40SDon Lewis D("bad len sopt->sopt_valsize %d len %d",
142191336b40SDon Lewis (int) sopt->sopt_valsize , l);
142291336b40SDon Lewis err = EINVAL;
142391336b40SDon Lewis return err;
142491336b40SDon Lewis }
142551d73df1SKristof Provost ep = malloc(l, M_DUMMYNET, M_NOWAIT);
142691336b40SDon Lewis if(!ep) {
142791336b40SDon Lewis err = ENOMEM ;
142891336b40SDon Lewis return err;
142991336b40SDon Lewis }
143091336b40SDon Lewis do {
143191336b40SDon Lewis err = sooptcopyin(sopt, ep, l, l);
143291336b40SDon Lewis if(err)
143391336b40SDon Lewis break;
143491336b40SDon Lewis sopt->sopt_valsize = sopt_valsize;
143591336b40SDon Lewis if (ep->oid.len < l) {
143691336b40SDon Lewis err = EINVAL;
143791336b40SDon Lewis break;
143891336b40SDon Lewis }
143991336b40SDon Lewis
144091336b40SDon Lewis schk = locate_scheduler(ep->nr);
144191336b40SDon Lewis if (!schk) {
144291336b40SDon Lewis D("sched %d not found", ep->nr);
144391336b40SDon Lewis err = EINVAL;
144491336b40SDon Lewis break;
144591336b40SDon Lewis }
144691336b40SDon Lewis
144791336b40SDon Lewis if (schk->fp && schk->fp->getconfig) {
144891336b40SDon Lewis if(schk->fp->getconfig(schk, ep)) {
144991336b40SDon Lewis D("Error while trying to get sched params");
145091336b40SDon Lewis err = EINVAL;
145191336b40SDon Lewis break;
145291336b40SDon Lewis }
145391336b40SDon Lewis ep->oid.len = l;
145491336b40SDon Lewis err = sooptcopyout(sopt, ep, l);
145591336b40SDon Lewis }
145691336b40SDon Lewis }while(0);
145791336b40SDon Lewis free(ep, M_DUMMYNET);
145891336b40SDon Lewis
145991336b40SDon Lewis return err;
146091336b40SDon Lewis }
146191336b40SDon Lewis
146291336b40SDon Lewis /* Configure AQM for flowset 'fs'.
146391336b40SDon Lewis * extra parameters are passed from userland.
146491336b40SDon Lewis */
146591336b40SDon Lewis static int
config_aqm(struct dn_fsk * fs,struct dn_extra_parms * ep,int busy)146691336b40SDon Lewis config_aqm(struct dn_fsk *fs, struct dn_extra_parms *ep, int busy)
146791336b40SDon Lewis {
146891336b40SDon Lewis int err = 0;
146991336b40SDon Lewis
147051d73df1SKristof Provost NET_EPOCH_ASSERT();
147151d73df1SKristof Provost
147291336b40SDon Lewis do {
147391336b40SDon Lewis /* no configurations */
147491336b40SDon Lewis if (!ep) {
147591336b40SDon Lewis err = 0;
147691336b40SDon Lewis break;
147791336b40SDon Lewis }
147891336b40SDon Lewis
147991336b40SDon Lewis /* no AQM for this flowset*/
148091336b40SDon Lewis if (!strcmp(ep->name,"")) {
148191336b40SDon Lewis err = 0;
148291336b40SDon Lewis break;
148391336b40SDon Lewis }
148491336b40SDon Lewis if (ep->oid.len < sizeof(*ep)) {
148591336b40SDon Lewis D("short aqm len %d", ep->oid.len);
148691336b40SDon Lewis err = EINVAL;
148791336b40SDon Lewis break;
148891336b40SDon Lewis }
148991336b40SDon Lewis
149091336b40SDon Lewis if (busy) {
149191336b40SDon Lewis D("Unable to configure flowset, flowset busy!");
149291336b40SDon Lewis err = EINVAL;
149391336b40SDon Lewis break;
149491336b40SDon Lewis }
149591336b40SDon Lewis
149691336b40SDon Lewis /* deconfigure old aqm if exist */
149791336b40SDon Lewis if (fs->aqmcfg && fs->aqmfp && fs->aqmfp->deconfig) {
149891336b40SDon Lewis aqm_cleanup_deconfig_fs(fs);
149991336b40SDon Lewis }
150091336b40SDon Lewis
150191336b40SDon Lewis if (!(fs->aqmfp = find_aqm_type(0, ep->name))) {
150291336b40SDon Lewis D("AQM functions not found for type %s!", ep->name);
150391336b40SDon Lewis fs->fs.flags &= ~DN_IS_AQM;
150491336b40SDon Lewis err = EINVAL;
150591336b40SDon Lewis break;
150691336b40SDon Lewis } else
150791336b40SDon Lewis fs->fs.flags |= DN_IS_AQM;
150891336b40SDon Lewis
150991336b40SDon Lewis if (ep->oid.subtype != DN_AQM_PARAMS) {
151091336b40SDon Lewis D("Wrong subtype");
151191336b40SDon Lewis err = EINVAL;
151291336b40SDon Lewis break;
151391336b40SDon Lewis }
151491336b40SDon Lewis
151591336b40SDon Lewis if (fs->aqmfp->config) {
151691336b40SDon Lewis err = fs->aqmfp->config(fs, ep, ep->oid.len);
151791336b40SDon Lewis if (err) {
151891336b40SDon Lewis D("Unable to configure AQM for FS %d", fs->fs.fs_nr );
151991336b40SDon Lewis fs->fs.flags &= ~DN_IS_AQM;
152091336b40SDon Lewis fs->aqmfp = NULL;
152191336b40SDon Lewis break;
152291336b40SDon Lewis }
152391336b40SDon Lewis }
152491336b40SDon Lewis } while(0);
152591336b40SDon Lewis
152691336b40SDon Lewis return err;
152791336b40SDon Lewis }
152891336b40SDon Lewis #endif
152991336b40SDon Lewis
15303b3a8eb9SGleb Smirnoff /*
15313b3a8eb9SGleb Smirnoff * Configuration -- to preserve backward compatibility we use
15323b3a8eb9SGleb Smirnoff * the following scheme (N is 65536)
15333b3a8eb9SGleb Smirnoff * NUMBER SCHED LINK FLOWSET
15343b3a8eb9SGleb Smirnoff * 1 .. N-1 (1)WFQ (2)WFQ (3)queue
15353b3a8eb9SGleb Smirnoff * N+1 .. 2N-1 (4)FIFO (5)FIFO (6)FIFO for sched 1..N-1
15363b3a8eb9SGleb Smirnoff * 2N+1 .. 3N-1 -- -- (7)FIFO for sched N+1..2N-1
15373b3a8eb9SGleb Smirnoff *
15383b3a8eb9SGleb Smirnoff * "pipe i config" configures #1, #2 and #3
15393b3a8eb9SGleb Smirnoff * "sched i config" configures #1 and possibly #6
15403b3a8eb9SGleb Smirnoff * "queue i config" configures #3
15413b3a8eb9SGleb Smirnoff * #1 is configured with 'pipe i config' or 'sched i config'
15423b3a8eb9SGleb Smirnoff * #2 is configured with 'pipe i config', and created if not
15433b3a8eb9SGleb Smirnoff * existing with 'sched i config'
15443b3a8eb9SGleb Smirnoff * #3 is configured with 'queue i config'
15453b3a8eb9SGleb Smirnoff * #4 is automatically configured after #1, can only be FIFO
15463b3a8eb9SGleb Smirnoff * #5 is automatically configured after #2
15473b3a8eb9SGleb Smirnoff * #6 is automatically created when #1 is !MULTIQUEUE,
15483b3a8eb9SGleb Smirnoff * and can be updated.
15493b3a8eb9SGleb Smirnoff * #7 is automatically configured after #2
15503b3a8eb9SGleb Smirnoff */
15513b3a8eb9SGleb Smirnoff
15523b3a8eb9SGleb Smirnoff /*
15533b3a8eb9SGleb Smirnoff * configure a link (and its FIFO instance)
15543b3a8eb9SGleb Smirnoff */
15553b3a8eb9SGleb Smirnoff static int
config_link(struct dn_link * p,struct dn_id * arg)15563b3a8eb9SGleb Smirnoff config_link(struct dn_link *p, struct dn_id *arg)
15573b3a8eb9SGleb Smirnoff {
15583b3a8eb9SGleb Smirnoff int i;
15593b3a8eb9SGleb Smirnoff
15603b3a8eb9SGleb Smirnoff if (p->oid.len != sizeof(*p)) {
15613b3a8eb9SGleb Smirnoff D("invalid pipe len %d", p->oid.len);
15623b3a8eb9SGleb Smirnoff return EINVAL;
15633b3a8eb9SGleb Smirnoff }
15643b3a8eb9SGleb Smirnoff i = p->link_nr;
15653b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID)
15663b3a8eb9SGleb Smirnoff return EINVAL;
15673b3a8eb9SGleb Smirnoff /*
15683b3a8eb9SGleb Smirnoff * The config program passes parameters as follows:
15693b3a8eb9SGleb Smirnoff * bw = bits/second (0 means no limits),
15703b3a8eb9SGleb Smirnoff * delay = ms, must be translated into ticks.
15713b3a8eb9SGleb Smirnoff * qsize = slots/bytes
15723b3a8eb9SGleb Smirnoff * burst ???
15733b3a8eb9SGleb Smirnoff */
15743b3a8eb9SGleb Smirnoff p->delay = (p->delay * hz) / 1000;
15753b3a8eb9SGleb Smirnoff /* Scale burst size: bytes -> bits * hz */
15763b3a8eb9SGleb Smirnoff p->burst *= 8 * hz;
15773b3a8eb9SGleb Smirnoff
15783b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
15793b3a8eb9SGleb Smirnoff /* do it twice, base link and FIFO link */
15803b3a8eb9SGleb Smirnoff for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
15813b3a8eb9SGleb Smirnoff struct dn_schk *s = locate_scheduler(i);
15823b3a8eb9SGleb Smirnoff if (s == NULL) {
15833b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
15843b3a8eb9SGleb Smirnoff D("sched %d not found", i);
15853b3a8eb9SGleb Smirnoff return EINVAL;
15863b3a8eb9SGleb Smirnoff }
15873b3a8eb9SGleb Smirnoff /* remove profile if exists */
15883b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET);
15893b3a8eb9SGleb Smirnoff s->profile = NULL;
15901b2dbe37SKristof Provost
15913b3a8eb9SGleb Smirnoff /* copy all parameters */
15923b3a8eb9SGleb Smirnoff s->link.oid = p->oid;
15933b3a8eb9SGleb Smirnoff s->link.link_nr = i;
15943b3a8eb9SGleb Smirnoff s->link.delay = p->delay;
15953b3a8eb9SGleb Smirnoff if (s->link.bandwidth != p->bandwidth) {
15963b3a8eb9SGleb Smirnoff /* XXX bandwidth changes, need to update red params */
15973b3a8eb9SGleb Smirnoff s->link.bandwidth = p->bandwidth;
15983b3a8eb9SGleb Smirnoff update_red(s);
15993b3a8eb9SGleb Smirnoff }
16003b3a8eb9SGleb Smirnoff s->link.burst = p->burst;
16013b3a8eb9SGleb Smirnoff schk_reset_credit(s);
16023b3a8eb9SGleb Smirnoff }
1603fe3bcfbdSTom Jones V_dn_cfg.id++;
16043b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
16053b3a8eb9SGleb Smirnoff return 0;
16063b3a8eb9SGleb Smirnoff }
16073b3a8eb9SGleb Smirnoff
16083b3a8eb9SGleb Smirnoff /*
16093b3a8eb9SGleb Smirnoff * configure a flowset. Can be called from inside with locked=1,
16103b3a8eb9SGleb Smirnoff */
16113b3a8eb9SGleb Smirnoff static struct dn_fsk *
config_fs(struct dn_fs * nfs,struct dn_id * arg,int locked)16123b3a8eb9SGleb Smirnoff config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked)
16133b3a8eb9SGleb Smirnoff {
16143b3a8eb9SGleb Smirnoff int i;
16153b3a8eb9SGleb Smirnoff struct dn_fsk *fs;
16160ac43d97SMarius Strobl #ifdef NEW_AQM
16170ac43d97SMarius Strobl struct dn_extra_parms *ep;
16180ac43d97SMarius Strobl #endif
16193b3a8eb9SGleb Smirnoff
16203b3a8eb9SGleb Smirnoff if (nfs->oid.len != sizeof(*nfs)) {
16213b3a8eb9SGleb Smirnoff D("invalid flowset len %d", nfs->oid.len);
16223b3a8eb9SGleb Smirnoff return NULL;
16233b3a8eb9SGleb Smirnoff }
16243b3a8eb9SGleb Smirnoff i = nfs->fs_nr;
16253b3a8eb9SGleb Smirnoff if (i <= 0 || i >= 3*DN_MAX_ID)
16263b3a8eb9SGleb Smirnoff return NULL;
16270ac43d97SMarius Strobl #ifdef NEW_AQM
16280ac43d97SMarius Strobl ep = NULL;
16290ac43d97SMarius Strobl if (arg != NULL) {
163051d73df1SKristof Provost ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
16310ac43d97SMarius Strobl if (ep == NULL)
16320ac43d97SMarius Strobl return (NULL);
16330ac43d97SMarius Strobl memcpy(ep, arg, sizeof(*ep));
16340ac43d97SMarius Strobl }
16350ac43d97SMarius Strobl #endif
16363b3a8eb9SGleb Smirnoff ND("flowset %d", i);
16373b3a8eb9SGleb Smirnoff /* XXX other sanity checks */
16383b3a8eb9SGleb Smirnoff if (nfs->flags & DN_QSIZE_BYTES) {
16393b3a8eb9SGleb Smirnoff ipdn_bound_var(&nfs->qsize, 16384,
1640fe3bcfbdSTom Jones 1500, V_dn_cfg.byte_limit, NULL); // "queue byte size");
16413b3a8eb9SGleb Smirnoff } else {
16423b3a8eb9SGleb Smirnoff ipdn_bound_var(&nfs->qsize, 50,
1643fe3bcfbdSTom Jones 1, V_dn_cfg.slot_limit, NULL); // "queue slot size");
16443b3a8eb9SGleb Smirnoff }
16453b3a8eb9SGleb Smirnoff if (nfs->flags & DN_HAVE_MASK) {
16463b3a8eb9SGleb Smirnoff /* make sure we have some buckets */
1647fe3bcfbdSTom Jones ipdn_bound_var((int *)&nfs->buckets, V_dn_cfg.hash_size,
1648fe3bcfbdSTom Jones 1, V_dn_cfg.max_hash_size, "flowset buckets");
16493b3a8eb9SGleb Smirnoff } else {
16503b3a8eb9SGleb Smirnoff nfs->buckets = 1; /* we only need 1 */
16513b3a8eb9SGleb Smirnoff }
16523b3a8eb9SGleb Smirnoff if (!locked)
16533b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
16543b3a8eb9SGleb Smirnoff do { /* exit with break when done */
16553b3a8eb9SGleb Smirnoff struct dn_schk *s;
16563b3a8eb9SGleb Smirnoff int flags = nfs->sched_nr ? DNHT_INSERT : 0;
16573b3a8eb9SGleb Smirnoff int j;
1658fe3bcfbdSTom Jones int oldc = V_dn_cfg.fsk_count;
1659fe3bcfbdSTom Jones fs = dn_ht_find(V_dn_cfg.fshash, i, flags, NULL);
16603b3a8eb9SGleb Smirnoff if (fs == NULL) {
16613b3a8eb9SGleb Smirnoff D("missing sched for flowset %d", i);
16623b3a8eb9SGleb Smirnoff break;
16633b3a8eb9SGleb Smirnoff }
16643b3a8eb9SGleb Smirnoff /* grab some defaults from the existing one */
16653b3a8eb9SGleb Smirnoff if (nfs->sched_nr == 0) /* reuse */
16663b3a8eb9SGleb Smirnoff nfs->sched_nr = fs->fs.sched_nr;
16673b3a8eb9SGleb Smirnoff for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) {
16683b3a8eb9SGleb Smirnoff if (nfs->par[j] == -1) /* reuse */
16693b3a8eb9SGleb Smirnoff nfs->par[j] = fs->fs.par[j];
16703b3a8eb9SGleb Smirnoff }
16713b3a8eb9SGleb Smirnoff if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) {
16723b3a8eb9SGleb Smirnoff ND("flowset %d unchanged", i);
167391336b40SDon Lewis #ifdef NEW_AQM
16740ac43d97SMarius Strobl if (ep != NULL) {
16750ac43d97SMarius Strobl /*
16760ac43d97SMarius Strobl * Reconfigure AQM as the parameters can be changed.
16770ac43d97SMarius Strobl * We consider the flowset as busy if it has scheduler
16780ac43d97SMarius Strobl * instance(s).
167991336b40SDon Lewis */
168091336b40SDon Lewis s = locate_scheduler(nfs->sched_nr);
16810ac43d97SMarius Strobl config_aqm(fs, ep, s != NULL && s->siht != NULL);
16820ac43d97SMarius Strobl }
168391336b40SDon Lewis #endif
16843b3a8eb9SGleb Smirnoff break; /* no change, nothing to do */
16853b3a8eb9SGleb Smirnoff }
1686fe3bcfbdSTom Jones if (oldc != V_dn_cfg.fsk_count) /* new item */
1687fe3bcfbdSTom Jones V_dn_cfg.id++;
16883b3a8eb9SGleb Smirnoff s = locate_scheduler(nfs->sched_nr);
16893b3a8eb9SGleb Smirnoff /* detach from old scheduler if needed, preserving
16903b3a8eb9SGleb Smirnoff * queues if we need to reattach. Then update the
16913b3a8eb9SGleb Smirnoff * configuration, and possibly attach to the new sched.
16923b3a8eb9SGleb Smirnoff */
16933b3a8eb9SGleb Smirnoff DX(2, "fs %d changed sched %d@%p to %d@%p",
16943b3a8eb9SGleb Smirnoff fs->fs.fs_nr,
16953b3a8eb9SGleb Smirnoff fs->fs.sched_nr, fs->sched, nfs->sched_nr, s);
16963b3a8eb9SGleb Smirnoff if (fs->sched) {
16973b3a8eb9SGleb Smirnoff int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY);
16983b3a8eb9SGleb Smirnoff flags |= DN_DESTROY; /* XXX temporary */
16993b3a8eb9SGleb Smirnoff fsk_detach(fs, flags);
17003b3a8eb9SGleb Smirnoff }
17013b3a8eb9SGleb Smirnoff fs->fs = *nfs; /* copy configuration */
170291336b40SDon Lewis #ifdef NEW_AQM
170391336b40SDon Lewis fs->aqmfp = NULL;
17040ac43d97SMarius Strobl if (ep != NULL)
17050ac43d97SMarius Strobl config_aqm(fs, ep, s != NULL &&
17060ac43d97SMarius Strobl s->siht != NULL);
170791336b40SDon Lewis #endif
17083b3a8eb9SGleb Smirnoff if (s != NULL)
17093b3a8eb9SGleb Smirnoff fsk_attach(fs, s);
17103b3a8eb9SGleb Smirnoff } while (0);
17113b3a8eb9SGleb Smirnoff if (!locked)
17123b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
17130ac43d97SMarius Strobl #ifdef NEW_AQM
17140ac43d97SMarius Strobl free(ep, M_TEMP);
17150ac43d97SMarius Strobl #endif
17163b3a8eb9SGleb Smirnoff return fs;
17173b3a8eb9SGleb Smirnoff }
17183b3a8eb9SGleb Smirnoff
17193b3a8eb9SGleb Smirnoff /*
17203b3a8eb9SGleb Smirnoff * config/reconfig a scheduler and its FIFO variant.
17213b3a8eb9SGleb Smirnoff * For !MULTIQUEUE schedulers, also set up the flowset.
17223b3a8eb9SGleb Smirnoff *
17233b3a8eb9SGleb Smirnoff * On reconfigurations (detected because s->fp is set),
17243b3a8eb9SGleb Smirnoff * detach existing flowsets preserving traffic, preserve link,
17253b3a8eb9SGleb Smirnoff * and delete the old scheduler creating a new one.
17263b3a8eb9SGleb Smirnoff */
17273b3a8eb9SGleb Smirnoff static int
config_sched(struct dn_sch * _nsch,struct dn_id * arg)17283b3a8eb9SGleb Smirnoff config_sched(struct dn_sch *_nsch, struct dn_id *arg)
17293b3a8eb9SGleb Smirnoff {
17303b3a8eb9SGleb Smirnoff struct dn_schk *s;
17313b3a8eb9SGleb Smirnoff struct schk_new_arg a; /* argument for schk_new */
17323b3a8eb9SGleb Smirnoff int i;
17333b3a8eb9SGleb Smirnoff struct dn_link p; /* copy of oldlink */
17343b3a8eb9SGleb Smirnoff struct dn_profile *pf = NULL; /* copy of old link profile */
1735*f795d545SGordon Bergling /* Used to preserve mask parameter */
17363b3a8eb9SGleb Smirnoff struct ipfw_flow_id new_mask;
17373b3a8eb9SGleb Smirnoff int new_buckets = 0;
17383b3a8eb9SGleb Smirnoff int new_flags = 0;
17393b3a8eb9SGleb Smirnoff int pipe_cmd;
17403b3a8eb9SGleb Smirnoff int err = ENOMEM;
17413b3a8eb9SGleb Smirnoff
174251d73df1SKristof Provost NET_EPOCH_ASSERT();
174351d73df1SKristof Provost
17443b3a8eb9SGleb Smirnoff a.sch = _nsch;
17453b3a8eb9SGleb Smirnoff if (a.sch->oid.len != sizeof(*a.sch)) {
17463b3a8eb9SGleb Smirnoff D("bad sched len %d", a.sch->oid.len);
17473b3a8eb9SGleb Smirnoff return EINVAL;
17483b3a8eb9SGleb Smirnoff }
17493b3a8eb9SGleb Smirnoff i = a.sch->sched_nr;
17503b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID)
17513b3a8eb9SGleb Smirnoff return EINVAL;
17523b3a8eb9SGleb Smirnoff /* make sure we have some buckets */
17533b3a8eb9SGleb Smirnoff if (a.sch->flags & DN_HAVE_MASK)
1754fe3bcfbdSTom Jones ipdn_bound_var((int *)&a.sch->buckets, V_dn_cfg.hash_size,
1755fe3bcfbdSTom Jones 1, V_dn_cfg.max_hash_size, "sched buckets");
17563b3a8eb9SGleb Smirnoff /* XXX other sanity checks */
17573b3a8eb9SGleb Smirnoff bzero(&p, sizeof(p));
17583b3a8eb9SGleb Smirnoff
17593b3a8eb9SGleb Smirnoff pipe_cmd = a.sch->flags & DN_PIPE_CMD;
17603b3a8eb9SGleb Smirnoff a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set?
17613b3a8eb9SGleb Smirnoff if (pipe_cmd) {
17623b3a8eb9SGleb Smirnoff /* Copy mask parameter */
17633b3a8eb9SGleb Smirnoff new_mask = a.sch->sched_mask;
17643b3a8eb9SGleb Smirnoff new_buckets = a.sch->buckets;
17653b3a8eb9SGleb Smirnoff new_flags = a.sch->flags;
17663b3a8eb9SGleb Smirnoff }
17673b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
17683b3a8eb9SGleb Smirnoff again: /* run twice, for wfq and fifo */
17693b3a8eb9SGleb Smirnoff /*
17703b3a8eb9SGleb Smirnoff * lookup the type. If not supplied, use the previous one
17713b3a8eb9SGleb Smirnoff * or default to WF2Q+. Otherwise, return an error.
17723b3a8eb9SGleb Smirnoff */
1773fe3bcfbdSTom Jones V_dn_cfg.id++;
17743b3a8eb9SGleb Smirnoff a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name);
17753b3a8eb9SGleb Smirnoff if (a.fp != NULL) {
17763b3a8eb9SGleb Smirnoff /* found. Lookup or create entry */
1777fe3bcfbdSTom Jones s = dn_ht_find(V_dn_cfg.schedhash, i, DNHT_INSERT, &a);
17783b3a8eb9SGleb Smirnoff } else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) {
17793b3a8eb9SGleb Smirnoff /* No type. search existing s* or retry with WF2Q+ */
1780fe3bcfbdSTom Jones s = dn_ht_find(V_dn_cfg.schedhash, i, 0, &a);
17813b3a8eb9SGleb Smirnoff if (s != NULL) {
17823b3a8eb9SGleb Smirnoff a.fp = s->fp;
17833b3a8eb9SGleb Smirnoff /* Scheduler exists, skip to FIFO scheduler
17843b3a8eb9SGleb Smirnoff * if command was pipe config...
17853b3a8eb9SGleb Smirnoff */
17863b3a8eb9SGleb Smirnoff if (pipe_cmd)
17873b3a8eb9SGleb Smirnoff goto next;
17883b3a8eb9SGleb Smirnoff } else {
17893b3a8eb9SGleb Smirnoff /* New scheduler, create a wf2q+ with no mask
17903b3a8eb9SGleb Smirnoff * if command was pipe config...
17913b3a8eb9SGleb Smirnoff */
17923b3a8eb9SGleb Smirnoff if (pipe_cmd) {
17933b3a8eb9SGleb Smirnoff /* clear mask parameter */
17943b3a8eb9SGleb Smirnoff bzero(&a.sch->sched_mask, sizeof(new_mask));
17953b3a8eb9SGleb Smirnoff a.sch->buckets = 0;
17963b3a8eb9SGleb Smirnoff a.sch->flags &= ~DN_HAVE_MASK;
17973b3a8eb9SGleb Smirnoff }
17983b3a8eb9SGleb Smirnoff a.sch->oid.subtype = DN_SCHED_WF2QP;
17993b3a8eb9SGleb Smirnoff goto again;
18003b3a8eb9SGleb Smirnoff }
18013b3a8eb9SGleb Smirnoff } else {
18023b3a8eb9SGleb Smirnoff D("invalid scheduler type %d %s",
18033b3a8eb9SGleb Smirnoff a.sch->oid.subtype, a.sch->name);
18043b3a8eb9SGleb Smirnoff err = EINVAL;
18053b3a8eb9SGleb Smirnoff goto error;
18063b3a8eb9SGleb Smirnoff }
18073b3a8eb9SGleb Smirnoff /* normalize name and subtype */
18083b3a8eb9SGleb Smirnoff a.sch->oid.subtype = a.fp->type;
18093b3a8eb9SGleb Smirnoff bzero(a.sch->name, sizeof(a.sch->name));
18103b3a8eb9SGleb Smirnoff strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name));
18113b3a8eb9SGleb Smirnoff if (s == NULL) {
18123b3a8eb9SGleb Smirnoff D("cannot allocate scheduler %d", i);
18133b3a8eb9SGleb Smirnoff goto error;
18143b3a8eb9SGleb Smirnoff }
18153b3a8eb9SGleb Smirnoff /* restore existing link if any */
18163b3a8eb9SGleb Smirnoff if (p.link_nr) {
18173b3a8eb9SGleb Smirnoff s->link = p;
18183b3a8eb9SGleb Smirnoff if (!pf || pf->link_nr != p.link_nr) { /* no saved value */
18193b3a8eb9SGleb Smirnoff s->profile = NULL; /* XXX maybe not needed */
18203b3a8eb9SGleb Smirnoff } else {
18213b3a8eb9SGleb Smirnoff s->profile = malloc(sizeof(struct dn_profile),
18223b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO);
18233b3a8eb9SGleb Smirnoff if (s->profile == NULL) {
18243b3a8eb9SGleb Smirnoff D("cannot allocate profile");
18253b3a8eb9SGleb Smirnoff goto error; //XXX
18263b3a8eb9SGleb Smirnoff }
18270ac43d97SMarius Strobl memcpy(s->profile, pf, sizeof(*pf));
18283b3a8eb9SGleb Smirnoff }
18293b3a8eb9SGleb Smirnoff }
18303b3a8eb9SGleb Smirnoff p.link_nr = 0;
18313b3a8eb9SGleb Smirnoff if (s->fp == NULL) {
18323b3a8eb9SGleb Smirnoff DX(2, "sched %d new type %s", i, a.fp->name);
18333b3a8eb9SGleb Smirnoff } else if (s->fp != a.fp ||
18343b3a8eb9SGleb Smirnoff bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) {
18353b3a8eb9SGleb Smirnoff /* already existing. */
18363b3a8eb9SGleb Smirnoff DX(2, "sched %d type changed from %s to %s",
18373b3a8eb9SGleb Smirnoff i, s->fp->name, a.fp->name);
18383b3a8eb9SGleb Smirnoff DX(4, " type/sub %d/%d -> %d/%d",
18393b3a8eb9SGleb Smirnoff s->sch.oid.type, s->sch.oid.subtype,
18403b3a8eb9SGleb Smirnoff a.sch->oid.type, a.sch->oid.subtype);
18413b3a8eb9SGleb Smirnoff if (s->link.link_nr == 0)
18423b3a8eb9SGleb Smirnoff D("XXX WARNING link 0 for sched %d", i);
18433b3a8eb9SGleb Smirnoff p = s->link; /* preserve link */
18443b3a8eb9SGleb Smirnoff if (s->profile) {/* preserve profile */
18453b3a8eb9SGleb Smirnoff if (!pf)
18463b3a8eb9SGleb Smirnoff pf = malloc(sizeof(*pf),
18473b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO);
18483b3a8eb9SGleb Smirnoff if (pf) /* XXX should issue a warning otherwise */
18490ac43d97SMarius Strobl memcpy(pf, s->profile, sizeof(*pf));
18503b3a8eb9SGleb Smirnoff }
18513b3a8eb9SGleb Smirnoff /* remove from the hash */
1852fe3bcfbdSTom Jones dn_ht_find(V_dn_cfg.schedhash, i, DNHT_REMOVE, NULL);
18533b3a8eb9SGleb Smirnoff /* Detach flowsets, preserve queues. */
18543b3a8eb9SGleb Smirnoff // schk_delete_cb(s, NULL);
18553b3a8eb9SGleb Smirnoff // XXX temporarily, kill queues
18563b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void *)DN_DESTROY);
18573b3a8eb9SGleb Smirnoff goto again;
18583b3a8eb9SGleb Smirnoff } else {
18593b3a8eb9SGleb Smirnoff DX(4, "sched %d unchanged type %s", i, a.fp->name);
18603b3a8eb9SGleb Smirnoff }
18613b3a8eb9SGleb Smirnoff /* complete initialization */
18623b3a8eb9SGleb Smirnoff s->sch = *a.sch;
18633b3a8eb9SGleb Smirnoff s->fp = a.fp;
18643b3a8eb9SGleb Smirnoff s->cfg = arg;
18653b3a8eb9SGleb Smirnoff // XXX schk_reset_credit(s);
18663b3a8eb9SGleb Smirnoff /* create the internal flowset if needed,
18673b3a8eb9SGleb Smirnoff * trying to reuse existing ones if available
18683b3a8eb9SGleb Smirnoff */
18693b3a8eb9SGleb Smirnoff if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) {
1870fe3bcfbdSTom Jones s->fs = dn_ht_find(V_dn_cfg.fshash, i, 0, NULL);
18713b3a8eb9SGleb Smirnoff if (!s->fs) {
18723b3a8eb9SGleb Smirnoff struct dn_fs fs;
18733b3a8eb9SGleb Smirnoff bzero(&fs, sizeof(fs));
18743b3a8eb9SGleb Smirnoff set_oid(&fs.oid, DN_FS, sizeof(fs));
18753b3a8eb9SGleb Smirnoff fs.fs_nr = i + DN_MAX_ID;
18763b3a8eb9SGleb Smirnoff fs.sched_nr = i;
18773b3a8eb9SGleb Smirnoff s->fs = config_fs(&fs, NULL, 1 /* locked */);
18783b3a8eb9SGleb Smirnoff }
18793b3a8eb9SGleb Smirnoff if (!s->fs) {
18803b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void *)DN_DESTROY);
18813b3a8eb9SGleb Smirnoff D("error creating internal fs for %d", i);
18823b3a8eb9SGleb Smirnoff goto error;
18833b3a8eb9SGleb Smirnoff }
18843b3a8eb9SGleb Smirnoff }
18853b3a8eb9SGleb Smirnoff /* call init function after the flowset is created */
18863b3a8eb9SGleb Smirnoff if (s->fp->config)
18873b3a8eb9SGleb Smirnoff s->fp->config(s);
18883b3a8eb9SGleb Smirnoff update_fs(s);
18893b3a8eb9SGleb Smirnoff next:
18903b3a8eb9SGleb Smirnoff if (i < DN_MAX_ID) { /* now configure the FIFO instance */
18913b3a8eb9SGleb Smirnoff i += DN_MAX_ID;
18923b3a8eb9SGleb Smirnoff if (pipe_cmd) {
18933b3a8eb9SGleb Smirnoff /* Restore mask parameter for FIFO */
18943b3a8eb9SGleb Smirnoff a.sch->sched_mask = new_mask;
18953b3a8eb9SGleb Smirnoff a.sch->buckets = new_buckets;
18963b3a8eb9SGleb Smirnoff a.sch->flags = new_flags;
18973b3a8eb9SGleb Smirnoff } else {
18983b3a8eb9SGleb Smirnoff /* sched config shouldn't modify the FIFO scheduler */
1899fe3bcfbdSTom Jones if (dn_ht_find(V_dn_cfg.schedhash, i, 0, &a) != NULL) {
19003b3a8eb9SGleb Smirnoff /* FIFO already exist, don't touch it */
19013b3a8eb9SGleb Smirnoff err = 0; /* and this is not an error */
19023b3a8eb9SGleb Smirnoff goto error;
19033b3a8eb9SGleb Smirnoff }
19043b3a8eb9SGleb Smirnoff }
19053b3a8eb9SGleb Smirnoff a.sch->sched_nr = i;
19063b3a8eb9SGleb Smirnoff a.sch->oid.subtype = DN_SCHED_FIFO;
19073b3a8eb9SGleb Smirnoff bzero(a.sch->name, sizeof(a.sch->name));
19083b3a8eb9SGleb Smirnoff goto again;
19093b3a8eb9SGleb Smirnoff }
19103b3a8eb9SGleb Smirnoff err = 0;
19113b3a8eb9SGleb Smirnoff error:
19123b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
19133b3a8eb9SGleb Smirnoff free(pf, M_DUMMYNET);
19143b3a8eb9SGleb Smirnoff return err;
19153b3a8eb9SGleb Smirnoff }
19163b3a8eb9SGleb Smirnoff
19173b3a8eb9SGleb Smirnoff /*
19183b3a8eb9SGleb Smirnoff * attach a profile to a link
19193b3a8eb9SGleb Smirnoff */
19203b3a8eb9SGleb Smirnoff static int
config_profile(struct dn_profile * pf,struct dn_id * arg)19213b3a8eb9SGleb Smirnoff config_profile(struct dn_profile *pf, struct dn_id *arg)
19223b3a8eb9SGleb Smirnoff {
19233b3a8eb9SGleb Smirnoff struct dn_schk *s;
19243b3a8eb9SGleb Smirnoff int i, olen, err = 0;
19253b3a8eb9SGleb Smirnoff
19263b3a8eb9SGleb Smirnoff if (pf->oid.len < sizeof(*pf)) {
19273b3a8eb9SGleb Smirnoff D("short profile len %d", pf->oid.len);
19283b3a8eb9SGleb Smirnoff return EINVAL;
19293b3a8eb9SGleb Smirnoff }
19303b3a8eb9SGleb Smirnoff i = pf->link_nr;
19313b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID)
19323b3a8eb9SGleb Smirnoff return EINVAL;
19333b3a8eb9SGleb Smirnoff /* XXX other sanity checks */
19343b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
19353b3a8eb9SGleb Smirnoff for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) {
19363b3a8eb9SGleb Smirnoff s = locate_scheduler(i);
19373b3a8eb9SGleb Smirnoff
19383b3a8eb9SGleb Smirnoff if (s == NULL) {
19393b3a8eb9SGleb Smirnoff err = EINVAL;
19403b3a8eb9SGleb Smirnoff break;
19413b3a8eb9SGleb Smirnoff }
1942fe3bcfbdSTom Jones V_dn_cfg.id++;
19433b3a8eb9SGleb Smirnoff /*
19443b3a8eb9SGleb Smirnoff * If we had a profile and the new one does not fit,
19453b3a8eb9SGleb Smirnoff * or it is deleted, then we need to free memory.
19463b3a8eb9SGleb Smirnoff */
19473b3a8eb9SGleb Smirnoff if (s->profile && (pf->samples_no == 0 ||
19483b3a8eb9SGleb Smirnoff s->profile->oid.len < pf->oid.len)) {
19493b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET);
19503b3a8eb9SGleb Smirnoff s->profile = NULL;
19513b3a8eb9SGleb Smirnoff }
19523b3a8eb9SGleb Smirnoff if (pf->samples_no == 0)
19533b3a8eb9SGleb Smirnoff continue;
19543b3a8eb9SGleb Smirnoff /*
19553b3a8eb9SGleb Smirnoff * new profile, possibly allocate memory
19563b3a8eb9SGleb Smirnoff * and copy data.
19573b3a8eb9SGleb Smirnoff */
19583b3a8eb9SGleb Smirnoff if (s->profile == NULL)
19593b3a8eb9SGleb Smirnoff s->profile = malloc(pf->oid.len,
19603b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO);
19613b3a8eb9SGleb Smirnoff if (s->profile == NULL) {
19623b3a8eb9SGleb Smirnoff D("no memory for profile %d", i);
19633b3a8eb9SGleb Smirnoff err = ENOMEM;
19643b3a8eb9SGleb Smirnoff break;
19653b3a8eb9SGleb Smirnoff }
19663b3a8eb9SGleb Smirnoff /* preserve larger length XXX double check */
19673b3a8eb9SGleb Smirnoff olen = s->profile->oid.len;
19683b3a8eb9SGleb Smirnoff if (olen < pf->oid.len)
19693b3a8eb9SGleb Smirnoff olen = pf->oid.len;
19700ac43d97SMarius Strobl memcpy(s->profile, pf, pf->oid.len);
19713b3a8eb9SGleb Smirnoff s->profile->oid.len = olen;
19723b3a8eb9SGleb Smirnoff }
19733b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
19743b3a8eb9SGleb Smirnoff return err;
19753b3a8eb9SGleb Smirnoff }
19763b3a8eb9SGleb Smirnoff
19773b3a8eb9SGleb Smirnoff /*
19783b3a8eb9SGleb Smirnoff * Delete all objects:
19793b3a8eb9SGleb Smirnoff */
19803b3a8eb9SGleb Smirnoff static void
dummynet_flush(void)19813b3a8eb9SGleb Smirnoff dummynet_flush(void)
19823b3a8eb9SGleb Smirnoff {
19833b3a8eb9SGleb Smirnoff
19843b3a8eb9SGleb Smirnoff /* delete all schedulers and related links/queues/flowsets */
1985fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.schedhash, schk_delete_cb,
19863b3a8eb9SGleb Smirnoff (void *)(uintptr_t)DN_DELETE_FS);
19873b3a8eb9SGleb Smirnoff /* delete all remaining (unlinked) flowsets */
1988fe3bcfbdSTom Jones DX(4, "still %d unlinked fs", V_dn_cfg.fsk_count);
1989fe3bcfbdSTom Jones dn_ht_free(V_dn_cfg.fshash, DNHT_REMOVE);
1990fe3bcfbdSTom Jones fsk_detach_list(&V_dn_cfg.fsu, DN_DELETE_FS);
19913b3a8eb9SGleb Smirnoff /* Reinitialize system heap... */
1992fe3bcfbdSTom Jones heap_init(&V_dn_cfg.evheap, 16, offsetof(struct dn_id, id));
19933b3a8eb9SGleb Smirnoff }
19943b3a8eb9SGleb Smirnoff
19953b3a8eb9SGleb Smirnoff /*
19963b3a8eb9SGleb Smirnoff * Main handler for configuration. We are guaranteed to be called
19973b3a8eb9SGleb Smirnoff * with an oid which is at least a dn_id.
19983b3a8eb9SGleb Smirnoff * - the first object is the command (config, delete, flush, ...)
19993b3a8eb9SGleb Smirnoff * - config_link must be issued after the corresponding config_sched
2000a4641f4eSPedro F. Giffuni * - parameters (DN_TXT) for an object must precede the object
20013b3a8eb9SGleb Smirnoff * processed on a config_sched.
20023b3a8eb9SGleb Smirnoff */
20033b3a8eb9SGleb Smirnoff int
do_config(void * p,size_t l)20041c732c85SMark Johnston do_config(void *p, size_t l)
20053b3a8eb9SGleb Smirnoff {
20060ac43d97SMarius Strobl struct dn_id o;
20070ac43d97SMarius Strobl union {
20080ac43d97SMarius Strobl struct dn_profile profile;
20090ac43d97SMarius Strobl struct dn_fs fs;
20100ac43d97SMarius Strobl struct dn_link link;
20110ac43d97SMarius Strobl struct dn_sch sched;
20120ac43d97SMarius Strobl } *dn;
20130ac43d97SMarius Strobl struct dn_id *arg;
20140ac43d97SMarius Strobl uintptr_t a;
20150ac43d97SMarius Strobl int err, err2, off;
20163b3a8eb9SGleb Smirnoff
20170ac43d97SMarius Strobl memcpy(&o, p, sizeof(o));
20180ac43d97SMarius Strobl if (o.id != DN_API_VERSION) {
20190ac43d97SMarius Strobl D("invalid api version got %d need %d", o.id, DN_API_VERSION);
20203b3a8eb9SGleb Smirnoff return EINVAL;
20213b3a8eb9SGleb Smirnoff }
20220ac43d97SMarius Strobl arg = NULL;
20230ac43d97SMarius Strobl dn = NULL;
2024d5ea04eeSMark Johnston off = 0;
2025d5ea04eeSMark Johnston while (l >= sizeof(o)) {
2026d5ea04eeSMark Johnston memcpy(&o, (char *)p + off, sizeof(o));
20270ac43d97SMarius Strobl if (o.len < sizeof(o) || l < o.len) {
20281c732c85SMark Johnston D("bad len o.len %d len %zu", o.len, l);
20293b3a8eb9SGleb Smirnoff err = EINVAL;
20303b3a8eb9SGleb Smirnoff break;
20313b3a8eb9SGleb Smirnoff }
20320ac43d97SMarius Strobl l -= o.len;
20333b3a8eb9SGleb Smirnoff err = 0;
20340ac43d97SMarius Strobl switch (o.type) {
20353b3a8eb9SGleb Smirnoff default:
20360ac43d97SMarius Strobl D("cmd %d not implemented", o.type);
20373b3a8eb9SGleb Smirnoff break;
20383b3a8eb9SGleb Smirnoff
20393b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL
20403b3a8eb9SGleb Smirnoff /* sysctl emulation.
20413b3a8eb9SGleb Smirnoff * if we recognize the command, jump to the correct
20423b3a8eb9SGleb Smirnoff * handler and return
20433b3a8eb9SGleb Smirnoff */
20443b3a8eb9SGleb Smirnoff case DN_SYSCTL_SET:
20453b3a8eb9SGleb Smirnoff err = kesysctl_emu_set(p, l);
20463b3a8eb9SGleb Smirnoff return err;
20473b3a8eb9SGleb Smirnoff #endif
20483b3a8eb9SGleb Smirnoff
20493b3a8eb9SGleb Smirnoff case DN_CMD_CONFIG: /* simply a header */
20503b3a8eb9SGleb Smirnoff break;
20513b3a8eb9SGleb Smirnoff
20523b3a8eb9SGleb Smirnoff case DN_CMD_DELETE:
20533b3a8eb9SGleb Smirnoff /* the argument is in the first uintptr_t after o */
20540ac43d97SMarius Strobl if (o.len < sizeof(o) + sizeof(a)) {
20553b3a8eb9SGleb Smirnoff err = EINVAL;
20563b3a8eb9SGleb Smirnoff break;
20573b3a8eb9SGleb Smirnoff }
20580ac43d97SMarius Strobl memcpy(&a, (char *)p + off + sizeof(o), sizeof(a));
20590ac43d97SMarius Strobl switch (o.subtype) {
20603b3a8eb9SGleb Smirnoff case DN_LINK:
20613b3a8eb9SGleb Smirnoff /* delete base and derived schedulers */
20623b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
20630ac43d97SMarius Strobl err = delete_schk(a);
20640ac43d97SMarius Strobl err2 = delete_schk(a + DN_MAX_ID);
20653b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
20663b3a8eb9SGleb Smirnoff if (!err)
20673b3a8eb9SGleb Smirnoff err = err2;
20683b3a8eb9SGleb Smirnoff break;
20693b3a8eb9SGleb Smirnoff
20703b3a8eb9SGleb Smirnoff default:
20710ac43d97SMarius Strobl D("invalid delete type %d", o.subtype);
20723b3a8eb9SGleb Smirnoff err = EINVAL;
20733b3a8eb9SGleb Smirnoff break;
20743b3a8eb9SGleb Smirnoff
20753b3a8eb9SGleb Smirnoff case DN_FS:
20760ac43d97SMarius Strobl err = (a < 1 || a >= DN_MAX_ID) ?
20770ac43d97SMarius Strobl EINVAL : delete_fs(a, 0) ;
20783b3a8eb9SGleb Smirnoff break;
20793b3a8eb9SGleb Smirnoff }
20803b3a8eb9SGleb Smirnoff break;
20813b3a8eb9SGleb Smirnoff
20823b3a8eb9SGleb Smirnoff case DN_CMD_FLUSH:
20833b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
20843b3a8eb9SGleb Smirnoff dummynet_flush();
20853b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
20863b3a8eb9SGleb Smirnoff break;
20870ac43d97SMarius Strobl case DN_TEXT: /* store argument of next block */
20880ac43d97SMarius Strobl free(arg, M_TEMP);
208951d73df1SKristof Provost arg = malloc(o.len, M_TEMP, M_NOWAIT);
209051d73df1SKristof Provost if (arg == NULL) {
209151d73df1SKristof Provost err = ENOMEM;
209251d73df1SKristof Provost break;
209351d73df1SKristof Provost }
20940ac43d97SMarius Strobl memcpy(arg, (char *)p + off, o.len);
20953b3a8eb9SGleb Smirnoff break;
20963b3a8eb9SGleb Smirnoff case DN_LINK:
20970ac43d97SMarius Strobl if (dn == NULL)
209851d73df1SKristof Provost dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
209951d73df1SKristof Provost if (dn == NULL) {
210051d73df1SKristof Provost err = ENOMEM;
210151d73df1SKristof Provost break;
210251d73df1SKristof Provost }
21030ac43d97SMarius Strobl memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
21040ac43d97SMarius Strobl err = config_link(&dn->link, arg);
21053b3a8eb9SGleb Smirnoff break;
21063b3a8eb9SGleb Smirnoff case DN_PROFILE:
21070ac43d97SMarius Strobl if (dn == NULL)
210851d73df1SKristof Provost dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
210951d73df1SKristof Provost if (dn == NULL) {
211051d73df1SKristof Provost err = ENOMEM;
211151d73df1SKristof Provost break;
211251d73df1SKristof Provost }
21130ac43d97SMarius Strobl memcpy(&dn->profile, (char *)p + off,
21140ac43d97SMarius Strobl sizeof(dn->profile));
21150ac43d97SMarius Strobl err = config_profile(&dn->profile, arg);
21163b3a8eb9SGleb Smirnoff break;
21173b3a8eb9SGleb Smirnoff case DN_SCH:
21180ac43d97SMarius Strobl if (dn == NULL)
211951d73df1SKristof Provost dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
212051d73df1SKristof Provost if (dn == NULL) {
212151d73df1SKristof Provost err = ENOMEM;
212251d73df1SKristof Provost break;
212351d73df1SKristof Provost }
21240ac43d97SMarius Strobl memcpy(&dn->sched, (char *)p + off,
21250ac43d97SMarius Strobl sizeof(dn->sched));
21260ac43d97SMarius Strobl err = config_sched(&dn->sched, arg);
21273b3a8eb9SGleb Smirnoff break;
21283b3a8eb9SGleb Smirnoff case DN_FS:
21290ac43d97SMarius Strobl if (dn == NULL)
213051d73df1SKristof Provost dn = malloc(sizeof(*dn), M_TEMP, M_NOWAIT);
213151d73df1SKristof Provost if (dn == NULL) {
213251d73df1SKristof Provost err = ENOMEM;
213351d73df1SKristof Provost break;
213451d73df1SKristof Provost }
21350ac43d97SMarius Strobl memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
21360ac43d97SMarius Strobl err = (NULL == config_fs(&dn->fs, arg, 0));
21373b3a8eb9SGleb Smirnoff break;
21383b3a8eb9SGleb Smirnoff }
21393b3a8eb9SGleb Smirnoff if (err != 0)
21403b3a8eb9SGleb Smirnoff break;
21410ac43d97SMarius Strobl off += o.len;
21423b3a8eb9SGleb Smirnoff }
21430ac43d97SMarius Strobl free(arg, M_TEMP);
21440ac43d97SMarius Strobl free(dn, M_TEMP);
21453b3a8eb9SGleb Smirnoff return err;
21463b3a8eb9SGleb Smirnoff }
21473b3a8eb9SGleb Smirnoff
21483b3a8eb9SGleb Smirnoff static int
compute_space(struct dn_id * cmd,struct copy_args * a)21493b3a8eb9SGleb Smirnoff compute_space(struct dn_id *cmd, struct copy_args *a)
21503b3a8eb9SGleb Smirnoff {
21513b3a8eb9SGleb Smirnoff int x = 0, need = 0;
21523b3a8eb9SGleb Smirnoff int profile_size = sizeof(struct dn_profile) -
21533b3a8eb9SGleb Smirnoff ED_MAX_SAMPLES_NO*sizeof(int);
21543b3a8eb9SGleb Smirnoff
21553b3a8eb9SGleb Smirnoff /* NOTE about compute space:
2156fe3bcfbdSTom Jones * NP = V_dn_cfg.schk_count
2157fe3bcfbdSTom Jones * NSI = V_dn_cfg.si_count
2158fe3bcfbdSTom Jones * NF = V_dn_cfg.fsk_count
2159fe3bcfbdSTom Jones * NQ = V_dn_cfg.queue_count
21603b3a8eb9SGleb Smirnoff * - ipfw pipe show
21613b3a8eb9SGleb Smirnoff * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
21623b3a8eb9SGleb Smirnoff * link, scheduler template, flowset
21633b3a8eb9SGleb Smirnoff * integrated in scheduler and header
21643b3a8eb9SGleb Smirnoff * for flowset list
21653b3a8eb9SGleb Smirnoff * (NSI)*(dn_flow) all scheduler instance (includes
21663b3a8eb9SGleb Smirnoff * the queue instance)
21673b3a8eb9SGleb Smirnoff * - ipfw sched show
21683b3a8eb9SGleb Smirnoff * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler
21693b3a8eb9SGleb Smirnoff * link, scheduler template, flowset
21703b3a8eb9SGleb Smirnoff * integrated in scheduler and header
21713b3a8eb9SGleb Smirnoff * for flowset list
21723b3a8eb9SGleb Smirnoff * (NSI * dn_flow) all scheduler instances
21733b3a8eb9SGleb Smirnoff * (NF * sizeof(uint_32)) space for flowset list linked to scheduler
21743b3a8eb9SGleb Smirnoff * (NQ * dn_queue) all queue [XXXfor now not listed]
21753b3a8eb9SGleb Smirnoff * - ipfw queue show
21763b3a8eb9SGleb Smirnoff * (NF * dn_fs) all flowset
21773b3a8eb9SGleb Smirnoff * (NQ * dn_queue) all queues
21783b3a8eb9SGleb Smirnoff */
21793b3a8eb9SGleb Smirnoff switch (cmd->subtype) {
21803b3a8eb9SGleb Smirnoff default:
21813b3a8eb9SGleb Smirnoff return -1;
21823b3a8eb9SGleb Smirnoff /* XXX where do LINK and SCH differ ? */
21833b3a8eb9SGleb Smirnoff /* 'ipfw sched show' could list all queues associated to
21843b3a8eb9SGleb Smirnoff * a scheduler. This feature for now is disabled
21853b3a8eb9SGleb Smirnoff */
21863b3a8eb9SGleb Smirnoff case DN_LINK: /* pipe show */
21873b3a8eb9SGleb Smirnoff x = DN_C_LINK | DN_C_SCH | DN_C_FLOW;
2188fe3bcfbdSTom Jones need += V_dn_cfg.schk_count *
21893b3a8eb9SGleb Smirnoff (sizeof(struct dn_fs) + profile_size) / 2;
2190fe3bcfbdSTom Jones need += V_dn_cfg.fsk_count * sizeof(uint32_t);
21913b3a8eb9SGleb Smirnoff break;
21923b3a8eb9SGleb Smirnoff case DN_SCH: /* sched show */
2193fe3bcfbdSTom Jones need += V_dn_cfg.schk_count *
21943b3a8eb9SGleb Smirnoff (sizeof(struct dn_fs) + profile_size) / 2;
2195fe3bcfbdSTom Jones need += V_dn_cfg.fsk_count * sizeof(uint32_t);
21963b3a8eb9SGleb Smirnoff x = DN_C_SCH | DN_C_LINK | DN_C_FLOW;
21973b3a8eb9SGleb Smirnoff break;
21983b3a8eb9SGleb Smirnoff case DN_FS: /* queue show */
21993b3a8eb9SGleb Smirnoff x = DN_C_FS | DN_C_QUEUE;
22003b3a8eb9SGleb Smirnoff break;
22013b3a8eb9SGleb Smirnoff case DN_GET_COMPAT: /* compatibility mode */
22023b3a8eb9SGleb Smirnoff need = dn_compat_calc_size();
22033b3a8eb9SGleb Smirnoff break;
22043b3a8eb9SGleb Smirnoff }
22053b3a8eb9SGleb Smirnoff a->flags = x;
22063b3a8eb9SGleb Smirnoff if (x & DN_C_SCH) {
2207fe3bcfbdSTom Jones need += V_dn_cfg.schk_count * sizeof(struct dn_sch) / 2;
22083b3a8eb9SGleb Smirnoff /* NOT also, each fs might be attached to a sched */
2209fe3bcfbdSTom Jones need += V_dn_cfg.schk_count * sizeof(struct dn_id) / 2;
22103b3a8eb9SGleb Smirnoff }
22113b3a8eb9SGleb Smirnoff if (x & DN_C_FS)
2212fe3bcfbdSTom Jones need += V_dn_cfg.fsk_count * sizeof(struct dn_fs);
22133b3a8eb9SGleb Smirnoff if (x & DN_C_LINK) {
2214fe3bcfbdSTom Jones need += V_dn_cfg.schk_count * sizeof(struct dn_link) / 2;
22153b3a8eb9SGleb Smirnoff }
22163b3a8eb9SGleb Smirnoff /*
22173b3a8eb9SGleb Smirnoff * When exporting a queue to userland, only pass up the
22183b3a8eb9SGleb Smirnoff * struct dn_flow, which is the only visible part.
22193b3a8eb9SGleb Smirnoff */
22203b3a8eb9SGleb Smirnoff
22213b3a8eb9SGleb Smirnoff if (x & DN_C_QUEUE)
2222fe3bcfbdSTom Jones need += V_dn_cfg.queue_count * sizeof(struct dn_flow);
22233b3a8eb9SGleb Smirnoff if (x & DN_C_FLOW)
2224fe3bcfbdSTom Jones need += V_dn_cfg.si_count * (sizeof(struct dn_flow));
22253b3a8eb9SGleb Smirnoff return need;
22263b3a8eb9SGleb Smirnoff }
22273b3a8eb9SGleb Smirnoff
22283b3a8eb9SGleb Smirnoff /*
22293b3a8eb9SGleb Smirnoff * If compat != NULL dummynet_get is called in compatibility mode.
22303b3a8eb9SGleb Smirnoff * *compat will be the pointer to the buffer to pass to ipfw
22313b3a8eb9SGleb Smirnoff */
22323b3a8eb9SGleb Smirnoff int
dummynet_get(struct sockopt * sopt,void ** compat)22333b3a8eb9SGleb Smirnoff dummynet_get(struct sockopt *sopt, void **compat)
22343b3a8eb9SGleb Smirnoff {
22353b3a8eb9SGleb Smirnoff int have, i, need, error;
22363b3a8eb9SGleb Smirnoff char *start = NULL, *buf;
22373b3a8eb9SGleb Smirnoff size_t sopt_valsize;
22383b3a8eb9SGleb Smirnoff struct dn_id *cmd;
22393b3a8eb9SGleb Smirnoff struct copy_args a;
22403b3a8eb9SGleb Smirnoff struct copy_range r;
22413b3a8eb9SGleb Smirnoff int l = sizeof(struct dn_id);
22423b3a8eb9SGleb Smirnoff
22433b3a8eb9SGleb Smirnoff bzero(&a, sizeof(a));
22443b3a8eb9SGleb Smirnoff bzero(&r, sizeof(r));
22453b3a8eb9SGleb Smirnoff
22463b3a8eb9SGleb Smirnoff /* save and restore original sopt_valsize around copyin */
22473b3a8eb9SGleb Smirnoff sopt_valsize = sopt->sopt_valsize;
22483b3a8eb9SGleb Smirnoff
22493b3a8eb9SGleb Smirnoff cmd = &r.o;
22503b3a8eb9SGleb Smirnoff
22513b3a8eb9SGleb Smirnoff if (!compat) {
22523b3a8eb9SGleb Smirnoff /* copy at least an oid, and possibly a full object */
22533b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd));
22543b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize;
22553b3a8eb9SGleb Smirnoff if (error)
22563b3a8eb9SGleb Smirnoff goto done;
22573b3a8eb9SGleb Smirnoff l = cmd->len;
22583b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL
22593b3a8eb9SGleb Smirnoff /* sysctl emulation. */
22603b3a8eb9SGleb Smirnoff if (cmd->type == DN_SYSCTL_GET)
22613b3a8eb9SGleb Smirnoff return kesysctl_emu_get(sopt);
22623b3a8eb9SGleb Smirnoff #endif
22633b3a8eb9SGleb Smirnoff if (l > sizeof(r)) {
22643b3a8eb9SGleb Smirnoff /* request larger than default, allocate buffer */
226551d73df1SKristof Provost cmd = malloc(l, M_DUMMYNET, M_NOWAIT);
226651d73df1SKristof Provost if (cmd == NULL) {
226751d73df1SKristof Provost error = ENOMEM;
226851d73df1SKristof Provost goto done;
226951d73df1SKristof Provost }
22703b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, cmd, l, l);
22713b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize;
22723b3a8eb9SGleb Smirnoff if (error)
22733b3a8eb9SGleb Smirnoff goto done;
22743b3a8eb9SGleb Smirnoff }
22753b3a8eb9SGleb Smirnoff } else { /* compatibility */
22763b3a8eb9SGleb Smirnoff error = 0;
22773b3a8eb9SGleb Smirnoff cmd->type = DN_CMD_GET;
22783b3a8eb9SGleb Smirnoff cmd->len = sizeof(struct dn_id);
22793b3a8eb9SGleb Smirnoff cmd->subtype = DN_GET_COMPAT;
22803b3a8eb9SGleb Smirnoff // cmd->id = sopt_valsize;
22813b3a8eb9SGleb Smirnoff D("compatibility mode");
22823b3a8eb9SGleb Smirnoff }
228391336b40SDon Lewis
228491336b40SDon Lewis #ifdef NEW_AQM
228591336b40SDon Lewis /* get AQM params */
228691336b40SDon Lewis if(cmd->subtype == DN_AQM_PARAMS) {
228791336b40SDon Lewis error = get_aqm_parms(sopt);
228891336b40SDon Lewis goto done;
228991336b40SDon Lewis /* get Scheduler params */
229091336b40SDon Lewis } else if (cmd->subtype == DN_SCH_PARAMS) {
229191336b40SDon Lewis error = get_sched_parms(sopt);
229291336b40SDon Lewis goto done;
229391336b40SDon Lewis }
229491336b40SDon Lewis #endif
229591336b40SDon Lewis
22963b3a8eb9SGleb Smirnoff a.extra = (struct copy_range *)cmd;
22973b3a8eb9SGleb Smirnoff if (cmd->len == sizeof(*cmd)) { /* no range, create a default */
22983b3a8eb9SGleb Smirnoff uint32_t *rp = (uint32_t *)(cmd + 1);
22993b3a8eb9SGleb Smirnoff cmd->len += 2* sizeof(uint32_t);
23003b3a8eb9SGleb Smirnoff rp[0] = 1;
23013b3a8eb9SGleb Smirnoff rp[1] = DN_MAX_ID - 1;
23023b3a8eb9SGleb Smirnoff if (cmd->subtype == DN_LINK) {
23033b3a8eb9SGleb Smirnoff rp[0] += DN_MAX_ID;
23043b3a8eb9SGleb Smirnoff rp[1] += DN_MAX_ID;
23053b3a8eb9SGleb Smirnoff }
23063b3a8eb9SGleb Smirnoff }
23073b3a8eb9SGleb Smirnoff /* Count space (under lock) and allocate (outside lock).
23083b3a8eb9SGleb Smirnoff * Exit with lock held if we manage to get enough buffer.
23093b3a8eb9SGleb Smirnoff * Try a few times then give up.
23103b3a8eb9SGleb Smirnoff */
23113b3a8eb9SGleb Smirnoff for (have = 0, i = 0; i < 10; i++) {
23123b3a8eb9SGleb Smirnoff DN_BH_WLOCK();
23133b3a8eb9SGleb Smirnoff need = compute_space(cmd, &a);
23143b3a8eb9SGleb Smirnoff
23153b3a8eb9SGleb Smirnoff /* if there is a range, ignore value from compute_space() */
23163b3a8eb9SGleb Smirnoff if (l > sizeof(*cmd))
23173b3a8eb9SGleb Smirnoff need = sopt_valsize - sizeof(*cmd);
23183b3a8eb9SGleb Smirnoff
23193b3a8eb9SGleb Smirnoff if (need < 0) {
23203b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
23213b3a8eb9SGleb Smirnoff error = EINVAL;
23223b3a8eb9SGleb Smirnoff goto done;
23233b3a8eb9SGleb Smirnoff }
23243b3a8eb9SGleb Smirnoff need += sizeof(*cmd);
23253b3a8eb9SGleb Smirnoff cmd->id = need;
23263b3a8eb9SGleb Smirnoff if (have >= need)
23273b3a8eb9SGleb Smirnoff break;
23283b3a8eb9SGleb Smirnoff
23293b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
23303b3a8eb9SGleb Smirnoff free(start, M_DUMMYNET);
23313b3a8eb9SGleb Smirnoff start = NULL;
23323b3a8eb9SGleb Smirnoff if (need > sopt_valsize)
23333b3a8eb9SGleb Smirnoff break;
23343b3a8eb9SGleb Smirnoff
23353b3a8eb9SGleb Smirnoff have = need;
233651d73df1SKristof Provost start = malloc(have, M_DUMMYNET, M_NOWAIT | M_ZERO);
23373b3a8eb9SGleb Smirnoff }
23383b3a8eb9SGleb Smirnoff
23393b3a8eb9SGleb Smirnoff if (start == NULL) {
23403b3a8eb9SGleb Smirnoff if (compat) {
23413b3a8eb9SGleb Smirnoff *compat = NULL;
23423b3a8eb9SGleb Smirnoff error = 1; // XXX
23433b3a8eb9SGleb Smirnoff } else {
23443b3a8eb9SGleb Smirnoff error = sooptcopyout(sopt, cmd, sizeof(*cmd));
23453b3a8eb9SGleb Smirnoff }
23463b3a8eb9SGleb Smirnoff goto done;
23473b3a8eb9SGleb Smirnoff }
23483b3a8eb9SGleb Smirnoff ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, "
23493b3a8eb9SGleb Smirnoff "%d:%d si %d, %d:%d queues %d",
2350fe3bcfbdSTom Jones V_dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH,
2351fe3bcfbdSTom Jones V_dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK,
2352fe3bcfbdSTom Jones V_dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS,
2353fe3bcfbdSTom Jones V_dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I,
2354fe3bcfbdSTom Jones V_dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE);
23553b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize;
23563b3a8eb9SGleb Smirnoff a.type = cmd->subtype;
23573b3a8eb9SGleb Smirnoff
23583b3a8eb9SGleb Smirnoff if (compat == NULL) {
23590ac43d97SMarius Strobl memcpy(start, cmd, sizeof(*cmd));
23603b3a8eb9SGleb Smirnoff ((struct dn_id*)(start))->len = sizeof(struct dn_id);
23613b3a8eb9SGleb Smirnoff buf = start + sizeof(*cmd);
23623b3a8eb9SGleb Smirnoff } else
23633b3a8eb9SGleb Smirnoff buf = start;
23643b3a8eb9SGleb Smirnoff a.start = &buf;
23653b3a8eb9SGleb Smirnoff a.end = start + have;
23663b3a8eb9SGleb Smirnoff /* start copying other objects */
23673b3a8eb9SGleb Smirnoff if (compat) {
23683b3a8eb9SGleb Smirnoff a.type = DN_COMPAT_PIPE;
2369fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.schedhash, copy_data_helper_compat, &a);
23703b3a8eb9SGleb Smirnoff a.type = DN_COMPAT_QUEUE;
2371fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.fshash, copy_data_helper_compat, &a);
23723b3a8eb9SGleb Smirnoff } else if (a.type == DN_FS) {
2373fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.fshash, copy_data_helper, &a);
23743b3a8eb9SGleb Smirnoff } else {
2375fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.schedhash, copy_data_helper, &a);
23763b3a8eb9SGleb Smirnoff }
23773b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK();
23783b3a8eb9SGleb Smirnoff
23793b3a8eb9SGleb Smirnoff if (compat) {
23803b3a8eb9SGleb Smirnoff *compat = start;
23813b3a8eb9SGleb Smirnoff sopt->sopt_valsize = buf - start;
23823b3a8eb9SGleb Smirnoff /* free() is done by ip_dummynet_compat() */
23833b3a8eb9SGleb Smirnoff start = NULL; //XXX hack
23843b3a8eb9SGleb Smirnoff } else {
23853b3a8eb9SGleb Smirnoff error = sooptcopyout(sopt, start, buf - start);
23863b3a8eb9SGleb Smirnoff }
23873b3a8eb9SGleb Smirnoff done:
23881b2dbe37SKristof Provost if (cmd != &r.o)
23893b3a8eb9SGleb Smirnoff free(cmd, M_DUMMYNET);
23903b3a8eb9SGleb Smirnoff free(start, M_DUMMYNET);
23913b3a8eb9SGleb Smirnoff return error;
23923b3a8eb9SGleb Smirnoff }
23933b3a8eb9SGleb Smirnoff
23943b3a8eb9SGleb Smirnoff /* Callback called on scheduler instance to delete it if idle */
23953b3a8eb9SGleb Smirnoff static int
drain_scheduler_cb(void * _si,void * arg)23963b3a8eb9SGleb Smirnoff drain_scheduler_cb(void *_si, void *arg)
23973b3a8eb9SGleb Smirnoff {
23983b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si;
23993b3a8eb9SGleb Smirnoff
24003b3a8eb9SGleb Smirnoff if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL)
24013b3a8eb9SGleb Smirnoff return 0;
24023b3a8eb9SGleb Smirnoff
24033b3a8eb9SGleb Smirnoff if (si->sched->fp->flags & DN_MULTIQUEUE) {
24043b3a8eb9SGleb Smirnoff if (si->q_count == 0)
24053b3a8eb9SGleb Smirnoff return si_destroy(si, NULL);
24063b3a8eb9SGleb Smirnoff else
24073b3a8eb9SGleb Smirnoff return 0;
24083b3a8eb9SGleb Smirnoff } else { /* !DN_MULTIQUEUE */
24093b3a8eb9SGleb Smirnoff if ((si+1)->ni.length == 0)
24103b3a8eb9SGleb Smirnoff return si_destroy(si, NULL);
24113b3a8eb9SGleb Smirnoff else
24123b3a8eb9SGleb Smirnoff return 0;
24133b3a8eb9SGleb Smirnoff }
24143b3a8eb9SGleb Smirnoff return 0; /* unreachable */
24153b3a8eb9SGleb Smirnoff }
24163b3a8eb9SGleb Smirnoff
24173b3a8eb9SGleb Smirnoff /* Callback called on scheduler to check if it has instances */
24183b3a8eb9SGleb Smirnoff static int
drain_scheduler_sch_cb(void * _s,void * arg)24193b3a8eb9SGleb Smirnoff drain_scheduler_sch_cb(void *_s, void *arg)
24203b3a8eb9SGleb Smirnoff {
24213b3a8eb9SGleb Smirnoff struct dn_schk *s = _s;
24223b3a8eb9SGleb Smirnoff
24233b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) {
24243b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(s->siht, &s->drain_bucket,
24253b3a8eb9SGleb Smirnoff drain_scheduler_cb, NULL);
24263b3a8eb9SGleb Smirnoff s->drain_bucket++;
24273b3a8eb9SGleb Smirnoff } else {
24283b3a8eb9SGleb Smirnoff if (s->siht) {
24293b3a8eb9SGleb Smirnoff if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL)
24303b3a8eb9SGleb Smirnoff s->siht = NULL;
24313b3a8eb9SGleb Smirnoff }
24323b3a8eb9SGleb Smirnoff }
24333b3a8eb9SGleb Smirnoff return 0;
24343b3a8eb9SGleb Smirnoff }
24353b3a8eb9SGleb Smirnoff
24363b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of scheduler */
24373b3a8eb9SGleb Smirnoff void
dn_drain_scheduler(void)24383b3a8eb9SGleb Smirnoff dn_drain_scheduler(void)
24393b3a8eb9SGleb Smirnoff {
2440fe3bcfbdSTom Jones dn_ht_scan_bucket(V_dn_cfg.schedhash, &V_dn_cfg.drain_sch,
24413b3a8eb9SGleb Smirnoff drain_scheduler_sch_cb, NULL);
2442fe3bcfbdSTom Jones V_dn_cfg.drain_sch++;
24433b3a8eb9SGleb Smirnoff }
24443b3a8eb9SGleb Smirnoff
24453b3a8eb9SGleb Smirnoff /* Callback called on queue to delete if it is idle */
24463b3a8eb9SGleb Smirnoff static int
drain_queue_cb(void * _q,void * arg)24473b3a8eb9SGleb Smirnoff drain_queue_cb(void *_q, void *arg)
24483b3a8eb9SGleb Smirnoff {
24493b3a8eb9SGleb Smirnoff struct dn_queue *q = _q;
24503b3a8eb9SGleb Smirnoff
24513b3a8eb9SGleb Smirnoff if (q->ni.length == 0) {
24523b3a8eb9SGleb Smirnoff dn_delete_queue(q, DN_DESTROY);
24533b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL; /* queue is deleted */
24543b3a8eb9SGleb Smirnoff }
24553b3a8eb9SGleb Smirnoff
24563b3a8eb9SGleb Smirnoff return 0; /* queue isn't deleted */
24573b3a8eb9SGleb Smirnoff }
24583b3a8eb9SGleb Smirnoff
24593b3a8eb9SGleb Smirnoff /* Callback called on flowset used to check if it has queues */
24603b3a8eb9SGleb Smirnoff static int
drain_queue_fs_cb(void * _fs,void * arg)24613b3a8eb9SGleb Smirnoff drain_queue_fs_cb(void *_fs, void *arg)
24623b3a8eb9SGleb Smirnoff {
24633b3a8eb9SGleb Smirnoff struct dn_fsk *fs = _fs;
24643b3a8eb9SGleb Smirnoff
24653b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) {
24663b3a8eb9SGleb Smirnoff /* Flowset has a hash table for queues */
24673b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(fs->qht, &fs->drain_bucket,
24683b3a8eb9SGleb Smirnoff drain_queue_cb, NULL);
24693b3a8eb9SGleb Smirnoff fs->drain_bucket++;
24703b3a8eb9SGleb Smirnoff } else {
24713b3a8eb9SGleb Smirnoff /* No hash table for this flowset, null the pointer
24723b3a8eb9SGleb Smirnoff * if the queue is deleted
24733b3a8eb9SGleb Smirnoff */
24743b3a8eb9SGleb Smirnoff if (fs->qht) {
24753b3a8eb9SGleb Smirnoff if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL)
24763b3a8eb9SGleb Smirnoff fs->qht = NULL;
24773b3a8eb9SGleb Smirnoff }
24783b3a8eb9SGleb Smirnoff }
24793b3a8eb9SGleb Smirnoff return 0;
24803b3a8eb9SGleb Smirnoff }
24813b3a8eb9SGleb Smirnoff
24823b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of queue */
24833b3a8eb9SGleb Smirnoff void
dn_drain_queue(void)24843b3a8eb9SGleb Smirnoff dn_drain_queue(void)
24853b3a8eb9SGleb Smirnoff {
24863b3a8eb9SGleb Smirnoff /* scan a bucket of flowset */
2487fe3bcfbdSTom Jones dn_ht_scan_bucket(V_dn_cfg.fshash, &V_dn_cfg.drain_fs,
24883b3a8eb9SGleb Smirnoff drain_queue_fs_cb, NULL);
2489fe3bcfbdSTom Jones V_dn_cfg.drain_fs++;
24903b3a8eb9SGleb Smirnoff }
24913b3a8eb9SGleb Smirnoff
24923b3a8eb9SGleb Smirnoff /*
24933b3a8eb9SGleb Smirnoff * Handler for the various dummynet socket options
24943b3a8eb9SGleb Smirnoff */
24953b3a8eb9SGleb Smirnoff static int
ip_dn_ctl(struct sockopt * sopt)24963b3a8eb9SGleb Smirnoff ip_dn_ctl(struct sockopt *sopt)
24973b3a8eb9SGleb Smirnoff {
249851d73df1SKristof Provost struct epoch_tracker et;
24993b3a8eb9SGleb Smirnoff void *p = NULL;
25001c732c85SMark Johnston size_t l;
25011c732c85SMark Johnston int error;
25023b3a8eb9SGleb Smirnoff
25033b3a8eb9SGleb Smirnoff error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET);
25043b3a8eb9SGleb Smirnoff if (error)
25053b3a8eb9SGleb Smirnoff return (error);
25063b3a8eb9SGleb Smirnoff
25073b3a8eb9SGleb Smirnoff /* Disallow sets in really-really secure mode. */
25083b3a8eb9SGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) {
25093b3a8eb9SGleb Smirnoff error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
25103b3a8eb9SGleb Smirnoff if (error)
25113b3a8eb9SGleb Smirnoff return (error);
25123b3a8eb9SGleb Smirnoff }
25133b3a8eb9SGleb Smirnoff
251451d73df1SKristof Provost NET_EPOCH_ENTER(et);
251551d73df1SKristof Provost
25163b3a8eb9SGleb Smirnoff switch (sopt->sopt_name) {
25173b3a8eb9SGleb Smirnoff default :
25183b3a8eb9SGleb Smirnoff D("dummynet: unknown option %d", sopt->sopt_name);
25193b3a8eb9SGleb Smirnoff error = EINVAL;
25203b3a8eb9SGleb Smirnoff break;
25213b3a8eb9SGleb Smirnoff
25223b3a8eb9SGleb Smirnoff case IP_DUMMYNET_FLUSH:
25233b3a8eb9SGleb Smirnoff case IP_DUMMYNET_CONFIGURE:
25243b3a8eb9SGleb Smirnoff case IP_DUMMYNET_DEL: /* remove a pipe or queue */
25253b3a8eb9SGleb Smirnoff case IP_DUMMYNET_GET:
25263b3a8eb9SGleb Smirnoff D("dummynet: compat option %d", sopt->sopt_name);
25273b3a8eb9SGleb Smirnoff error = ip_dummynet_compat(sopt);
25283b3a8eb9SGleb Smirnoff break;
25293b3a8eb9SGleb Smirnoff
25303b3a8eb9SGleb Smirnoff case IP_DUMMYNET3:
25313b3a8eb9SGleb Smirnoff if (sopt->sopt_dir == SOPT_GET) {
25323b3a8eb9SGleb Smirnoff error = dummynet_get(sopt, NULL);
25333b3a8eb9SGleb Smirnoff break;
25343b3a8eb9SGleb Smirnoff }
25353b3a8eb9SGleb Smirnoff l = sopt->sopt_valsize;
25363b3a8eb9SGleb Smirnoff if (l < sizeof(struct dn_id) || l > 12000) {
25371c732c85SMark Johnston D("argument len %zu invalid", l);
25383b3a8eb9SGleb Smirnoff break;
25393b3a8eb9SGleb Smirnoff }
254051d73df1SKristof Provost p = malloc(l, M_TEMP, M_NOWAIT);
254151d73df1SKristof Provost if (p == NULL) {
254251d73df1SKristof Provost error = ENOMEM;
254351d73df1SKristof Provost break;
254451d73df1SKristof Provost }
25453b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, p, l, l);
25461c732c85SMark Johnston if (error == 0)
25473b3a8eb9SGleb Smirnoff error = do_config(p, l);
25483b3a8eb9SGleb Smirnoff break;
25493b3a8eb9SGleb Smirnoff }
25503b3a8eb9SGleb Smirnoff
25513b3a8eb9SGleb Smirnoff free(p, M_TEMP);
25523b3a8eb9SGleb Smirnoff
255351d73df1SKristof Provost NET_EPOCH_EXIT(et);
255451d73df1SKristof Provost
25553b3a8eb9SGleb Smirnoff return error ;
25563b3a8eb9SGleb Smirnoff }
25573b3a8eb9SGleb Smirnoff
25583b3a8eb9SGleb Smirnoff static void
ip_dn_vnet_init(void)2559fe3bcfbdSTom Jones ip_dn_vnet_init(void)
25603b3a8eb9SGleb Smirnoff {
2561fe3bcfbdSTom Jones if (V_dn_cfg.init_done)
25623b3a8eb9SGleb Smirnoff return;
2563cbb019b8SKristof Provost
25643b3a8eb9SGleb Smirnoff /* Set defaults here. MSVC does not accept initializers,
25653b3a8eb9SGleb Smirnoff * and this is also useful for vimages
25663b3a8eb9SGleb Smirnoff */
25673b3a8eb9SGleb Smirnoff /* queue limits */
2568fe3bcfbdSTom Jones V_dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */
2569fe3bcfbdSTom Jones V_dn_cfg.byte_limit = 1024 * 1024;
2570fe3bcfbdSTom Jones V_dn_cfg.expire = 1;
25713b3a8eb9SGleb Smirnoff
25723b3a8eb9SGleb Smirnoff /* RED parameters */
2573fe3bcfbdSTom Jones V_dn_cfg.red_lookup_depth = 256; /* default lookup table depth */
2574fe3bcfbdSTom Jones V_dn_cfg.red_avg_pkt_size = 512; /* default medium packet size */
2575fe3bcfbdSTom Jones V_dn_cfg.red_max_pkt_size = 1500; /* default max packet size */
25763b3a8eb9SGleb Smirnoff
25773b3a8eb9SGleb Smirnoff /* hash tables */
2578fe3bcfbdSTom Jones V_dn_cfg.max_hash_size = 65536; /* max in the hash tables */
2579fe3bcfbdSTom Jones V_dn_cfg.hash_size = 64; /* default hash size */
25803b3a8eb9SGleb Smirnoff
25813b3a8eb9SGleb Smirnoff /* create hash tables for schedulers and flowsets.
25823b3a8eb9SGleb Smirnoff * In both we search by key and by pointer.
25833b3a8eb9SGleb Smirnoff */
2584fe3bcfbdSTom Jones V_dn_cfg.schedhash = dn_ht_init(NULL, V_dn_cfg.hash_size,
25853b3a8eb9SGleb Smirnoff offsetof(struct dn_schk, schk_next),
25863b3a8eb9SGleb Smirnoff schk_hash, schk_match, schk_new);
2587fe3bcfbdSTom Jones V_dn_cfg.fshash = dn_ht_init(NULL, V_dn_cfg.hash_size,
25883b3a8eb9SGleb Smirnoff offsetof(struct dn_fsk, fsk_next),
25893b3a8eb9SGleb Smirnoff fsk_hash, fsk_match, fsk_new);
25903b3a8eb9SGleb Smirnoff
25913b3a8eb9SGleb Smirnoff /* bucket index to drain object */
2592fe3bcfbdSTom Jones V_dn_cfg.drain_fs = 0;
2593fe3bcfbdSTom Jones V_dn_cfg.drain_sch = 0;
25943b3a8eb9SGleb Smirnoff
2595fe3bcfbdSTom Jones heap_init(&V_dn_cfg.evheap, 16, offsetof(struct dn_id, id));
2596fe3bcfbdSTom Jones SLIST_INIT(&V_dn_cfg.fsu);
25973b3a8eb9SGleb Smirnoff
25983b3a8eb9SGleb Smirnoff DN_LOCK_INIT();
25993b3a8eb9SGleb Smirnoff
2600fe3bcfbdSTom Jones /* Initialize curr_time adjustment mechanics. */
2601fe3bcfbdSTom Jones getmicrouptime(&V_dn_cfg.prev_t);
2602cbb019b8SKristof Provost
2603cbb019b8SKristof Provost V_dn_cfg.init_done = 1;
2604fe3bcfbdSTom Jones }
2605fe3bcfbdSTom Jones
2606fe3bcfbdSTom Jones static void
ip_dn_vnet_destroy(void)2607fe3bcfbdSTom Jones ip_dn_vnet_destroy(void)
2608fe3bcfbdSTom Jones {
2609fe3bcfbdSTom Jones DN_BH_WLOCK();
2610fe3bcfbdSTom Jones dummynet_flush();
2611fe3bcfbdSTom Jones DN_BH_WUNLOCK();
2612fe3bcfbdSTom Jones
2613fe3bcfbdSTom Jones dn_ht_free(V_dn_cfg.schedhash, 0);
2614fe3bcfbdSTom Jones dn_ht_free(V_dn_cfg.fshash, 0);
2615fe3bcfbdSTom Jones heap_free(&V_dn_cfg.evheap);
2616fe3bcfbdSTom Jones
2617fe3bcfbdSTom Jones DN_LOCK_DESTROY();
2618fe3bcfbdSTom Jones }
2619fe3bcfbdSTom Jones
2620fe3bcfbdSTom Jones static void
ip_dn_init(void)2621fe3bcfbdSTom Jones ip_dn_init(void)
2622fe3bcfbdSTom Jones {
2623fe3bcfbdSTom Jones if (dn_tasks_started)
2624fe3bcfbdSTom Jones return;
262551d73df1SKristof Provost
262651d73df1SKristof Provost mtx_init(&sched_mtx, "dn_sched", NULL, MTX_DEF);
262751d73df1SKristof Provost
2628fe3bcfbdSTom Jones dn_tasks_started = 1;
2629fe3bcfbdSTom Jones TASK_INIT(&dn_task, 0, dummynet_task, NULL);
26305f4fc3dbSAlexander Motin dn_tq = taskqueue_create_fast("dummynet", M_WAITOK,
26313b3a8eb9SGleb Smirnoff taskqueue_thread_enqueue, &dn_tq);
26323b3a8eb9SGleb Smirnoff taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet");
26333b3a8eb9SGleb Smirnoff
263451d73df1SKristof Provost CK_LIST_INIT(&schedlist);
2635fd90e2edSJung-uk Kim callout_init(&dn_timeout, 1);
26365f4fc3dbSAlexander Motin dn_reschedule();
26373b3a8eb9SGleb Smirnoff }
26383b3a8eb9SGleb Smirnoff
26393b3a8eb9SGleb Smirnoff static void
ip_dn_destroy(int last)26403b3a8eb9SGleb Smirnoff ip_dn_destroy(int last)
26413b3a8eb9SGleb Smirnoff {
2642c8cfbc06SHans Petter Selasky /* ensure no more callouts are started */
2643c8cfbc06SHans Petter Selasky dn_gone = 1;
2644c8cfbc06SHans Petter Selasky
2645c8cfbc06SHans Petter Selasky /* check for last */
26463b3a8eb9SGleb Smirnoff if (last) {
26473b3a8eb9SGleb Smirnoff ND("removing last instance\n");
26483b3a8eb9SGleb Smirnoff ip_dn_ctl_ptr = NULL;
26493b3a8eb9SGleb Smirnoff ip_dn_io_ptr = NULL;
26503b3a8eb9SGleb Smirnoff }
26513b3a8eb9SGleb Smirnoff
2652c8cfbc06SHans Petter Selasky callout_drain(&dn_timeout);
26533b3a8eb9SGleb Smirnoff taskqueue_drain(dn_tq, &dn_task);
26543b3a8eb9SGleb Smirnoff taskqueue_free(dn_tq);
26553b3a8eb9SGleb Smirnoff }
26563b3a8eb9SGleb Smirnoff
26573b3a8eb9SGleb Smirnoff static int
dummynet_modevent(module_t mod,int type,void * data)26583b3a8eb9SGleb Smirnoff dummynet_modevent(module_t mod, int type, void *data)
26593b3a8eb9SGleb Smirnoff {
26603b3a8eb9SGleb Smirnoff
26613b3a8eb9SGleb Smirnoff if (type == MOD_LOAD) {
26623b3a8eb9SGleb Smirnoff if (ip_dn_io_ptr) {
26633b3a8eb9SGleb Smirnoff printf("DUMMYNET already loaded\n");
26643b3a8eb9SGleb Smirnoff return EEXIST ;
26653b3a8eb9SGleb Smirnoff }
26663b3a8eb9SGleb Smirnoff ip_dn_init();
26673b3a8eb9SGleb Smirnoff ip_dn_ctl_ptr = ip_dn_ctl;
26683b3a8eb9SGleb Smirnoff ip_dn_io_ptr = dummynet_io;
26693b3a8eb9SGleb Smirnoff return 0;
26703b3a8eb9SGleb Smirnoff } else if (type == MOD_UNLOAD) {
26713b3a8eb9SGleb Smirnoff ip_dn_destroy(1 /* last */);
26723b3a8eb9SGleb Smirnoff return 0;
26733b3a8eb9SGleb Smirnoff } else
26743b3a8eb9SGleb Smirnoff return EOPNOTSUPP;
26753b3a8eb9SGleb Smirnoff }
26763b3a8eb9SGleb Smirnoff
26773b3a8eb9SGleb Smirnoff /* modevent helpers for the modules */
26783b3a8eb9SGleb Smirnoff static int
load_dn_sched(struct dn_alg * d)26793b3a8eb9SGleb Smirnoff load_dn_sched(struct dn_alg *d)
26803b3a8eb9SGleb Smirnoff {
26813b3a8eb9SGleb Smirnoff struct dn_alg *s;
26823b3a8eb9SGleb Smirnoff
26833b3a8eb9SGleb Smirnoff if (d == NULL)
26843b3a8eb9SGleb Smirnoff return 1; /* error */
26853b3a8eb9SGleb Smirnoff ip_dn_init(); /* just in case, we need the lock */
26863b3a8eb9SGleb Smirnoff
26873b3a8eb9SGleb Smirnoff /* Check that mandatory funcs exists */
26883b3a8eb9SGleb Smirnoff if (d->enqueue == NULL || d->dequeue == NULL) {
26893b3a8eb9SGleb Smirnoff D("missing enqueue or dequeue for %s", d->name);
26903b3a8eb9SGleb Smirnoff return 1;
26913b3a8eb9SGleb Smirnoff }
26923b3a8eb9SGleb Smirnoff
26933b3a8eb9SGleb Smirnoff /* Search if scheduler already exists */
269451d73df1SKristof Provost mtx_lock(&sched_mtx);
269551d73df1SKristof Provost CK_LIST_FOREACH(s, &schedlist, next) {
26963b3a8eb9SGleb Smirnoff if (strcmp(s->name, d->name) == 0) {
26973b3a8eb9SGleb Smirnoff D("%s already loaded", d->name);
26983b3a8eb9SGleb Smirnoff break; /* scheduler already exists */
26993b3a8eb9SGleb Smirnoff }
27003b3a8eb9SGleb Smirnoff }
27013b3a8eb9SGleb Smirnoff if (s == NULL)
270251d73df1SKristof Provost CK_LIST_INSERT_HEAD(&schedlist, d, next);
270351d73df1SKristof Provost mtx_unlock(&sched_mtx);
27043b3a8eb9SGleb Smirnoff D("dn_sched %s %sloaded", d->name, s ? "not ":"");
27053b3a8eb9SGleb Smirnoff return s ? 1 : 0;
27063b3a8eb9SGleb Smirnoff }
27073b3a8eb9SGleb Smirnoff
27083b3a8eb9SGleb Smirnoff static int
unload_dn_sched(struct dn_alg * s)27093b3a8eb9SGleb Smirnoff unload_dn_sched(struct dn_alg *s)
27103b3a8eb9SGleb Smirnoff {
27113b3a8eb9SGleb Smirnoff struct dn_alg *tmp, *r;
27123b3a8eb9SGleb Smirnoff int err = EINVAL;
27133b3a8eb9SGleb Smirnoff
27143b3a8eb9SGleb Smirnoff ND("called for %s", s->name);
27153b3a8eb9SGleb Smirnoff
271651d73df1SKristof Provost mtx_lock(&sched_mtx);
271751d73df1SKristof Provost CK_LIST_FOREACH_SAFE(r, &schedlist, next, tmp) {
27183b3a8eb9SGleb Smirnoff if (strcmp(s->name, r->name) != 0)
27193b3a8eb9SGleb Smirnoff continue;
27203b3a8eb9SGleb Smirnoff ND("ref_count = %d", r->ref_count);
27213b3a8eb9SGleb Smirnoff err = (r->ref_count != 0) ? EBUSY : 0;
27223b3a8eb9SGleb Smirnoff if (err == 0)
272351d73df1SKristof Provost CK_LIST_REMOVE(r, next);
27243b3a8eb9SGleb Smirnoff break;
27253b3a8eb9SGleb Smirnoff }
272651d73df1SKristof Provost mtx_unlock(&sched_mtx);
272751d73df1SKristof Provost NET_EPOCH_WAIT();
27283b3a8eb9SGleb Smirnoff D("dn_sched %s %sunloaded", s->name, err ? "not ":"");
27293b3a8eb9SGleb Smirnoff return err;
27303b3a8eb9SGleb Smirnoff }
27313b3a8eb9SGleb Smirnoff
27323b3a8eb9SGleb Smirnoff int
dn_sched_modevent(module_t mod,int cmd,void * arg)27333b3a8eb9SGleb Smirnoff dn_sched_modevent(module_t mod, int cmd, void *arg)
27343b3a8eb9SGleb Smirnoff {
27353b3a8eb9SGleb Smirnoff struct dn_alg *sch = arg;
27363b3a8eb9SGleb Smirnoff
27373b3a8eb9SGleb Smirnoff if (cmd == MOD_LOAD)
27383b3a8eb9SGleb Smirnoff return load_dn_sched(sch);
27393b3a8eb9SGleb Smirnoff else if (cmd == MOD_UNLOAD)
27403b3a8eb9SGleb Smirnoff return unload_dn_sched(sch);
27413b3a8eb9SGleb Smirnoff else
27423b3a8eb9SGleb Smirnoff return EINVAL;
27433b3a8eb9SGleb Smirnoff }
27443b3a8eb9SGleb Smirnoff
27453b3a8eb9SGleb Smirnoff static moduledata_t dummynet_mod = {
27463b3a8eb9SGleb Smirnoff "dummynet", dummynet_modevent, NULL
27473b3a8eb9SGleb Smirnoff };
27483b3a8eb9SGleb Smirnoff
274989856f7eSBjoern A. Zeeb #define DN_SI_SUB SI_SUB_PROTO_FIREWALL
27503b3a8eb9SGleb Smirnoff #define DN_MODEV_ORD (SI_ORDER_ANY - 128) /* after ipfw */
27513b3a8eb9SGleb Smirnoff DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD);
27523b3a8eb9SGleb Smirnoff MODULE_VERSION(dummynet, 3);
27533b3a8eb9SGleb Smirnoff
27543b3a8eb9SGleb Smirnoff /*
27553b3a8eb9SGleb Smirnoff * Starting up. Done in order after dummynet_modevent() has been called.
27563b3a8eb9SGleb Smirnoff * VNET_SYSINIT is also called for each existing vnet and each new vnet.
27573b3a8eb9SGleb Smirnoff */
2758fe3bcfbdSTom Jones VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_vnet_init, NULL);
27593b3a8eb9SGleb Smirnoff
27603b3a8eb9SGleb Smirnoff /*
27613b3a8eb9SGleb Smirnoff * Shutdown handlers up shop. These are done in REVERSE ORDER, but still
27623b3a8eb9SGleb Smirnoff * after dummynet_modevent() has been called. Not called on reboot.
27633b3a8eb9SGleb Smirnoff * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
27643b3a8eb9SGleb Smirnoff * or when the module is unloaded.
27653b3a8eb9SGleb Smirnoff */
2766fe3bcfbdSTom Jones VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_vnet_destroy, NULL);
27673b3a8eb9SGleb Smirnoff
276891336b40SDon Lewis #ifdef NEW_AQM
276991336b40SDon Lewis
277091336b40SDon Lewis /* modevent helpers for the AQM modules */
277191336b40SDon Lewis static int
load_dn_aqm(struct dn_aqm * d)277291336b40SDon Lewis load_dn_aqm(struct dn_aqm *d)
277391336b40SDon Lewis {
277491336b40SDon Lewis struct dn_aqm *aqm=NULL;
277591336b40SDon Lewis
277691336b40SDon Lewis if (d == NULL)
277791336b40SDon Lewis return 1; /* error */
277891336b40SDon Lewis ip_dn_init(); /* just in case, we need the lock */
277991336b40SDon Lewis
278091336b40SDon Lewis /* Check that mandatory funcs exists */
278191336b40SDon Lewis if (d->enqueue == NULL || d->dequeue == NULL) {
278291336b40SDon Lewis D("missing enqueue or dequeue for %s", d->name);
278391336b40SDon Lewis return 1;
278491336b40SDon Lewis }
278591336b40SDon Lewis
278651d73df1SKristof Provost mtx_lock(&sched_mtx);
278751d73df1SKristof Provost
278891336b40SDon Lewis /* Search if AQM already exists */
278951d73df1SKristof Provost CK_LIST_FOREACH(aqm, &aqmlist, next) {
279091336b40SDon Lewis if (strcmp(aqm->name, d->name) == 0) {
279191336b40SDon Lewis D("%s already loaded", d->name);
279291336b40SDon Lewis break; /* AQM already exists */
279391336b40SDon Lewis }
279491336b40SDon Lewis }
279591336b40SDon Lewis if (aqm == NULL)
279651d73df1SKristof Provost CK_LIST_INSERT_HEAD(&aqmlist, d, next);
279751d73df1SKristof Provost
279851d73df1SKristof Provost mtx_unlock(&sched_mtx);
279951d73df1SKristof Provost
280091336b40SDon Lewis D("dn_aqm %s %sloaded", d->name, aqm ? "not ":"");
280191336b40SDon Lewis return aqm ? 1 : 0;
280291336b40SDon Lewis }
280391336b40SDon Lewis
280491336b40SDon Lewis /* Callback to clean up AQM status for queues connected to a flowset
280591336b40SDon Lewis * and then deconfigure the flowset.
280691336b40SDon Lewis * This function is called before an AQM module is unloaded
280791336b40SDon Lewis */
280891336b40SDon Lewis static int
fs_cleanup(void * _fs,void * arg)280991336b40SDon Lewis fs_cleanup(void *_fs, void *arg)
281091336b40SDon Lewis {
281191336b40SDon Lewis struct dn_fsk *fs = _fs;
281291336b40SDon Lewis uint32_t type = *(uint32_t *)arg;
281391336b40SDon Lewis
281491336b40SDon Lewis if (fs->aqmfp && fs->aqmfp->type == type)
281591336b40SDon Lewis aqm_cleanup_deconfig_fs(fs);
281691336b40SDon Lewis
281791336b40SDon Lewis return 0;
281891336b40SDon Lewis }
281991336b40SDon Lewis
282091336b40SDon Lewis static int
unload_dn_aqm(struct dn_aqm * aqm)282191336b40SDon Lewis unload_dn_aqm(struct dn_aqm *aqm)
282291336b40SDon Lewis {
282391336b40SDon Lewis struct dn_aqm *tmp, *r;
282491336b40SDon Lewis int err = EINVAL;
282591336b40SDon Lewis err = 0;
282691336b40SDon Lewis ND("called for %s", aqm->name);
282791336b40SDon Lewis
282891336b40SDon Lewis /* clean up AQM status and deconfig flowset */
2829fe3bcfbdSTom Jones dn_ht_scan(V_dn_cfg.fshash, fs_cleanup, &aqm->type);
283091336b40SDon Lewis
283151d73df1SKristof Provost mtx_lock(&sched_mtx);
283251d73df1SKristof Provost
283351d73df1SKristof Provost CK_LIST_FOREACH_SAFE(r, &aqmlist, next, tmp) {
283491336b40SDon Lewis if (strcmp(aqm->name, r->name) != 0)
283591336b40SDon Lewis continue;
283691336b40SDon Lewis ND("ref_count = %d", r->ref_count);
283791336b40SDon Lewis err = (r->ref_count != 0 || r->cfg_ref_count != 0) ? EBUSY : 0;
283891336b40SDon Lewis if (err == 0)
283951d73df1SKristof Provost CK_LIST_REMOVE(r, next);
284091336b40SDon Lewis break;
284191336b40SDon Lewis }
284251d73df1SKristof Provost
284351d73df1SKristof Provost mtx_unlock(&sched_mtx);
284451d73df1SKristof Provost NET_EPOCH_WAIT();
284551d73df1SKristof Provost
284691336b40SDon Lewis D("%s %sunloaded", aqm->name, err ? "not ":"");
284791336b40SDon Lewis if (err)
284891336b40SDon Lewis D("ref_count=%d, cfg_ref_count=%d", r->ref_count, r->cfg_ref_count);
284991336b40SDon Lewis return err;
285091336b40SDon Lewis }
285191336b40SDon Lewis
285291336b40SDon Lewis int
dn_aqm_modevent(module_t mod,int cmd,void * arg)285391336b40SDon Lewis dn_aqm_modevent(module_t mod, int cmd, void *arg)
285491336b40SDon Lewis {
285591336b40SDon Lewis struct dn_aqm *aqm = arg;
285691336b40SDon Lewis
285791336b40SDon Lewis if (cmd == MOD_LOAD)
285891336b40SDon Lewis return load_dn_aqm(aqm);
285991336b40SDon Lewis else if (cmd == MOD_UNLOAD)
286091336b40SDon Lewis return unload_dn_aqm(aqm);
286191336b40SDon Lewis else
286291336b40SDon Lewis return EINVAL;
286391336b40SDon Lewis }
286491336b40SDon Lewis #endif
286591336b40SDon Lewis
28663b3a8eb9SGleb Smirnoff /* end of file */
2867