13b3a8eb9SGleb Smirnoff /*- 23b3a8eb9SGleb Smirnoff * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa 33b3a8eb9SGleb Smirnoff * Portions Copyright (c) 2000 Akamba Corp. 43b3a8eb9SGleb Smirnoff * All rights reserved 53b3a8eb9SGleb Smirnoff * 63b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 73b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions 83b3a8eb9SGleb Smirnoff * are met: 93b3a8eb9SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 103b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 113b3a8eb9SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 123b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 133b3a8eb9SGleb Smirnoff * documentation and/or other materials provided with the distribution. 143b3a8eb9SGleb Smirnoff * 153b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 163b3a8eb9SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 173b3a8eb9SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 183b3a8eb9SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 193b3a8eb9SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 203b3a8eb9SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 213b3a8eb9SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 223b3a8eb9SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 233b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 243b3a8eb9SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 253b3a8eb9SGleb Smirnoff * SUCH DAMAGE. 263b3a8eb9SGleb Smirnoff */ 273b3a8eb9SGleb Smirnoff 283b3a8eb9SGleb Smirnoff #include <sys/cdefs.h> 293b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$"); 303b3a8eb9SGleb Smirnoff 313b3a8eb9SGleb Smirnoff /* 323b3a8eb9SGleb Smirnoff * Configuration and internal object management for dummynet. 333b3a8eb9SGleb Smirnoff */ 343b3a8eb9SGleb Smirnoff 353b3a8eb9SGleb Smirnoff #include "opt_inet6.h" 363b3a8eb9SGleb Smirnoff 373b3a8eb9SGleb Smirnoff #include <sys/param.h> 383b3a8eb9SGleb Smirnoff #include <sys/systm.h> 393b3a8eb9SGleb Smirnoff #include <sys/malloc.h> 403b3a8eb9SGleb Smirnoff #include <sys/mbuf.h> 413b3a8eb9SGleb Smirnoff #include <sys/kernel.h> 423b3a8eb9SGleb Smirnoff #include <sys/lock.h> 433b3a8eb9SGleb Smirnoff #include <sys/module.h> 443b3a8eb9SGleb Smirnoff #include <sys/priv.h> 453b3a8eb9SGleb Smirnoff #include <sys/proc.h> 463b3a8eb9SGleb Smirnoff #include <sys/rwlock.h> 473b3a8eb9SGleb Smirnoff #include <sys/socket.h> 483b3a8eb9SGleb Smirnoff #include <sys/socketvar.h> 493b3a8eb9SGleb Smirnoff #include <sys/time.h> 503b3a8eb9SGleb Smirnoff #include <sys/taskqueue.h> 513b3a8eb9SGleb Smirnoff #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */ 523b3a8eb9SGleb Smirnoff #include <netinet/in.h> 533b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */ 543b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h> 553b3a8eb9SGleb Smirnoff #include <netinet/ip_dummynet.h> 563b3a8eb9SGleb Smirnoff 573b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_fw_private.h> 583b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_heap.h> 593b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_dn_private.h> 603b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_sched.h> 613b3a8eb9SGleb Smirnoff 623b3a8eb9SGleb Smirnoff /* which objects to copy */ 633b3a8eb9SGleb Smirnoff #define DN_C_LINK 0x01 643b3a8eb9SGleb Smirnoff #define DN_C_SCH 0x02 653b3a8eb9SGleb Smirnoff #define DN_C_FLOW 0x04 663b3a8eb9SGleb Smirnoff #define DN_C_FS 0x08 673b3a8eb9SGleb Smirnoff #define DN_C_QUEUE 0x10 683b3a8eb9SGleb Smirnoff 693b3a8eb9SGleb Smirnoff /* we use this argument in case of a schk_new */ 703b3a8eb9SGleb Smirnoff struct schk_new_arg { 713b3a8eb9SGleb Smirnoff struct dn_alg *fp; 723b3a8eb9SGleb Smirnoff struct dn_sch *sch; 733b3a8eb9SGleb Smirnoff }; 743b3a8eb9SGleb Smirnoff 753b3a8eb9SGleb Smirnoff /*---- callout hooks. ----*/ 763b3a8eb9SGleb Smirnoff static struct callout dn_timeout; 773b3a8eb9SGleb Smirnoff static struct task dn_task; 783b3a8eb9SGleb Smirnoff static struct taskqueue *dn_tq = NULL; 793b3a8eb9SGleb Smirnoff 803b3a8eb9SGleb Smirnoff static void 813b3a8eb9SGleb Smirnoff dummynet(void *arg) 823b3a8eb9SGleb Smirnoff { 833b3a8eb9SGleb Smirnoff 843b3a8eb9SGleb Smirnoff (void)arg; /* UNUSED */ 85*5f4fc3dbSAlexander Motin taskqueue_enqueue_fast(dn_tq, &dn_task); 863b3a8eb9SGleb Smirnoff } 873b3a8eb9SGleb Smirnoff 883b3a8eb9SGleb Smirnoff void 893b3a8eb9SGleb Smirnoff dn_reschedule(void) 903b3a8eb9SGleb Smirnoff { 91*5f4fc3dbSAlexander Motin 92*5f4fc3dbSAlexander Motin callout_reset_sbt(&dn_timeout, tick_sbt, 0, dummynet, NULL, 93*5f4fc3dbSAlexander Motin C_HARDCLOCK | C_DIRECT_EXEC); 943b3a8eb9SGleb Smirnoff } 953b3a8eb9SGleb Smirnoff /*----- end of callout hooks -----*/ 963b3a8eb9SGleb Smirnoff 973b3a8eb9SGleb Smirnoff /* Return a scheduler descriptor given the type or name. */ 983b3a8eb9SGleb Smirnoff static struct dn_alg * 993b3a8eb9SGleb Smirnoff find_sched_type(int type, char *name) 1003b3a8eb9SGleb Smirnoff { 1013b3a8eb9SGleb Smirnoff struct dn_alg *d; 1023b3a8eb9SGleb Smirnoff 1033b3a8eb9SGleb Smirnoff SLIST_FOREACH(d, &dn_cfg.schedlist, next) { 1043b3a8eb9SGleb Smirnoff if (d->type == type || (name && !strcasecmp(d->name, name))) 1053b3a8eb9SGleb Smirnoff return d; 1063b3a8eb9SGleb Smirnoff } 1073b3a8eb9SGleb Smirnoff return NULL; /* not found */ 1083b3a8eb9SGleb Smirnoff } 1093b3a8eb9SGleb Smirnoff 1103b3a8eb9SGleb Smirnoff int 1113b3a8eb9SGleb Smirnoff ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg) 1123b3a8eb9SGleb Smirnoff { 1133b3a8eb9SGleb Smirnoff int oldv = *v; 1143b3a8eb9SGleb Smirnoff const char *op = NULL; 1153b3a8eb9SGleb Smirnoff if (dflt < lo) 1163b3a8eb9SGleb Smirnoff dflt = lo; 1173b3a8eb9SGleb Smirnoff if (dflt > hi) 1183b3a8eb9SGleb Smirnoff dflt = hi; 1193b3a8eb9SGleb Smirnoff if (oldv < lo) { 1203b3a8eb9SGleb Smirnoff *v = dflt; 1213b3a8eb9SGleb Smirnoff op = "Bump"; 1223b3a8eb9SGleb Smirnoff } else if (oldv > hi) { 1233b3a8eb9SGleb Smirnoff *v = hi; 1243b3a8eb9SGleb Smirnoff op = "Clamp"; 1253b3a8eb9SGleb Smirnoff } else 1263b3a8eb9SGleb Smirnoff return *v; 1273b3a8eb9SGleb Smirnoff if (op && msg) 1283b3a8eb9SGleb Smirnoff printf("%s %s to %d (was %d)\n", op, msg, *v, oldv); 1293b3a8eb9SGleb Smirnoff return *v; 1303b3a8eb9SGleb Smirnoff } 1313b3a8eb9SGleb Smirnoff 1323b3a8eb9SGleb Smirnoff /*---- flow_id mask, hash and compare functions ---*/ 1333b3a8eb9SGleb Smirnoff /* 1343b3a8eb9SGleb Smirnoff * The flow_id includes the 5-tuple, the queue/pipe number 1353b3a8eb9SGleb Smirnoff * which we store in the extra area in host order, 1363b3a8eb9SGleb Smirnoff * and for ipv6 also the flow_id6. 1373b3a8eb9SGleb Smirnoff * XXX see if we want the tos byte (can store in 'flags') 1383b3a8eb9SGleb Smirnoff */ 1393b3a8eb9SGleb Smirnoff static struct ipfw_flow_id * 1403b3a8eb9SGleb Smirnoff flow_id_mask(struct ipfw_flow_id *mask, struct ipfw_flow_id *id) 1413b3a8eb9SGleb Smirnoff { 1423b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(id); 1433b3a8eb9SGleb Smirnoff 1443b3a8eb9SGleb Smirnoff id->dst_port &= mask->dst_port; 1453b3a8eb9SGleb Smirnoff id->src_port &= mask->src_port; 1463b3a8eb9SGleb Smirnoff id->proto &= mask->proto; 1473b3a8eb9SGleb Smirnoff id->extra &= mask->extra; 1483b3a8eb9SGleb Smirnoff if (is_v6) { 1493b3a8eb9SGleb Smirnoff APPLY_MASK(&id->dst_ip6, &mask->dst_ip6); 1503b3a8eb9SGleb Smirnoff APPLY_MASK(&id->src_ip6, &mask->src_ip6); 1513b3a8eb9SGleb Smirnoff id->flow_id6 &= mask->flow_id6; 1523b3a8eb9SGleb Smirnoff } else { 1533b3a8eb9SGleb Smirnoff id->dst_ip &= mask->dst_ip; 1543b3a8eb9SGleb Smirnoff id->src_ip &= mask->src_ip; 1553b3a8eb9SGleb Smirnoff } 1563b3a8eb9SGleb Smirnoff return id; 1573b3a8eb9SGleb Smirnoff } 1583b3a8eb9SGleb Smirnoff 1593b3a8eb9SGleb Smirnoff /* computes an OR of two masks, result in dst and also returned */ 1603b3a8eb9SGleb Smirnoff static struct ipfw_flow_id * 1613b3a8eb9SGleb Smirnoff flow_id_or(struct ipfw_flow_id *src, struct ipfw_flow_id *dst) 1623b3a8eb9SGleb Smirnoff { 1633b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(dst); 1643b3a8eb9SGleb Smirnoff 1653b3a8eb9SGleb Smirnoff dst->dst_port |= src->dst_port; 1663b3a8eb9SGleb Smirnoff dst->src_port |= src->src_port; 1673b3a8eb9SGleb Smirnoff dst->proto |= src->proto; 1683b3a8eb9SGleb Smirnoff dst->extra |= src->extra; 1693b3a8eb9SGleb Smirnoff if (is_v6) { 1703b3a8eb9SGleb Smirnoff #define OR_MASK(_d, _s) \ 1713b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[0] |= (_s)->__u6_addr.__u6_addr32[0]; \ 1723b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[1] |= (_s)->__u6_addr.__u6_addr32[1]; \ 1733b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[2] |= (_s)->__u6_addr.__u6_addr32[2]; \ 1743b3a8eb9SGleb Smirnoff (_d)->__u6_addr.__u6_addr32[3] |= (_s)->__u6_addr.__u6_addr32[3]; 1753b3a8eb9SGleb Smirnoff OR_MASK(&dst->dst_ip6, &src->dst_ip6); 1763b3a8eb9SGleb Smirnoff OR_MASK(&dst->src_ip6, &src->src_ip6); 1773b3a8eb9SGleb Smirnoff #undef OR_MASK 1783b3a8eb9SGleb Smirnoff dst->flow_id6 |= src->flow_id6; 1793b3a8eb9SGleb Smirnoff } else { 1803b3a8eb9SGleb Smirnoff dst->dst_ip |= src->dst_ip; 1813b3a8eb9SGleb Smirnoff dst->src_ip |= src->src_ip; 1823b3a8eb9SGleb Smirnoff } 1833b3a8eb9SGleb Smirnoff return dst; 1843b3a8eb9SGleb Smirnoff } 1853b3a8eb9SGleb Smirnoff 1863b3a8eb9SGleb Smirnoff static int 1873b3a8eb9SGleb Smirnoff nonzero_mask(struct ipfw_flow_id *m) 1883b3a8eb9SGleb Smirnoff { 1893b3a8eb9SGleb Smirnoff if (m->dst_port || m->src_port || m->proto || m->extra) 1903b3a8eb9SGleb Smirnoff return 1; 1913b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(m)) { 1923b3a8eb9SGleb Smirnoff return 1933b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[0] || 1943b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[1] || 1953b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[2] || 1963b3a8eb9SGleb Smirnoff m->dst_ip6.__u6_addr.__u6_addr32[3] || 1973b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[0] || 1983b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[1] || 1993b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[2] || 2003b3a8eb9SGleb Smirnoff m->src_ip6.__u6_addr.__u6_addr32[3] || 2013b3a8eb9SGleb Smirnoff m->flow_id6; 2023b3a8eb9SGleb Smirnoff } else { 2033b3a8eb9SGleb Smirnoff return m->dst_ip || m->src_ip; 2043b3a8eb9SGleb Smirnoff } 2053b3a8eb9SGleb Smirnoff } 2063b3a8eb9SGleb Smirnoff 2073b3a8eb9SGleb Smirnoff /* XXX we may want a better hash function */ 2083b3a8eb9SGleb Smirnoff static uint32_t 2093b3a8eb9SGleb Smirnoff flow_id_hash(struct ipfw_flow_id *id) 2103b3a8eb9SGleb Smirnoff { 2113b3a8eb9SGleb Smirnoff uint32_t i; 2123b3a8eb9SGleb Smirnoff 2133b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(id)) { 2143b3a8eb9SGleb Smirnoff uint32_t *d = (uint32_t *)&id->dst_ip6; 2153b3a8eb9SGleb Smirnoff uint32_t *s = (uint32_t *)&id->src_ip6; 2163b3a8eb9SGleb Smirnoff i = (d[0] ) ^ (d[1]) ^ 2173b3a8eb9SGleb Smirnoff (d[2] ) ^ (d[3]) ^ 2183b3a8eb9SGleb Smirnoff (d[0] >> 15) ^ (d[1] >> 15) ^ 2193b3a8eb9SGleb Smirnoff (d[2] >> 15) ^ (d[3] >> 15) ^ 2203b3a8eb9SGleb Smirnoff (s[0] << 1) ^ (s[1] << 1) ^ 2213b3a8eb9SGleb Smirnoff (s[2] << 1) ^ (s[3] << 1) ^ 2223b3a8eb9SGleb Smirnoff (s[0] << 16) ^ (s[1] << 16) ^ 2233b3a8eb9SGleb Smirnoff (s[2] << 16) ^ (s[3] << 16) ^ 2243b3a8eb9SGleb Smirnoff (id->dst_port << 1) ^ (id->src_port) ^ 2253b3a8eb9SGleb Smirnoff (id->extra) ^ 2263b3a8eb9SGleb Smirnoff (id->proto ) ^ (id->flow_id6); 2273b3a8eb9SGleb Smirnoff } else { 2283b3a8eb9SGleb Smirnoff i = (id->dst_ip) ^ (id->dst_ip >> 15) ^ 2293b3a8eb9SGleb Smirnoff (id->src_ip << 1) ^ (id->src_ip >> 16) ^ 2303b3a8eb9SGleb Smirnoff (id->extra) ^ 2313b3a8eb9SGleb Smirnoff (id->dst_port << 1) ^ (id->src_port) ^ (id->proto); 2323b3a8eb9SGleb Smirnoff } 2333b3a8eb9SGleb Smirnoff return i; 2343b3a8eb9SGleb Smirnoff } 2353b3a8eb9SGleb Smirnoff 2363b3a8eb9SGleb Smirnoff /* Like bcmp, returns 0 if ids match, 1 otherwise. */ 2373b3a8eb9SGleb Smirnoff static int 2383b3a8eb9SGleb Smirnoff flow_id_cmp(struct ipfw_flow_id *id1, struct ipfw_flow_id *id2) 2393b3a8eb9SGleb Smirnoff { 2403b3a8eb9SGleb Smirnoff int is_v6 = IS_IP6_FLOW_ID(id1); 2413b3a8eb9SGleb Smirnoff 2423b3a8eb9SGleb Smirnoff if (!is_v6) { 2433b3a8eb9SGleb Smirnoff if (IS_IP6_FLOW_ID(id2)) 2443b3a8eb9SGleb Smirnoff return 1; /* different address families */ 2453b3a8eb9SGleb Smirnoff 2463b3a8eb9SGleb Smirnoff return (id1->dst_ip == id2->dst_ip && 2473b3a8eb9SGleb Smirnoff id1->src_ip == id2->src_ip && 2483b3a8eb9SGleb Smirnoff id1->dst_port == id2->dst_port && 2493b3a8eb9SGleb Smirnoff id1->src_port == id2->src_port && 2503b3a8eb9SGleb Smirnoff id1->proto == id2->proto && 2513b3a8eb9SGleb Smirnoff id1->extra == id2->extra) ? 0 : 1; 2523b3a8eb9SGleb Smirnoff } 2533b3a8eb9SGleb Smirnoff /* the ipv6 case */ 2543b3a8eb9SGleb Smirnoff return ( 2553b3a8eb9SGleb Smirnoff !bcmp(&id1->dst_ip6,&id2->dst_ip6, sizeof(id1->dst_ip6)) && 2563b3a8eb9SGleb Smirnoff !bcmp(&id1->src_ip6,&id2->src_ip6, sizeof(id1->src_ip6)) && 2573b3a8eb9SGleb Smirnoff id1->dst_port == id2->dst_port && 2583b3a8eb9SGleb Smirnoff id1->src_port == id2->src_port && 2593b3a8eb9SGleb Smirnoff id1->proto == id2->proto && 2603b3a8eb9SGleb Smirnoff id1->extra == id2->extra && 2613b3a8eb9SGleb Smirnoff id1->flow_id6 == id2->flow_id6) ? 0 : 1; 2623b3a8eb9SGleb Smirnoff } 2633b3a8eb9SGleb Smirnoff /*--------- end of flow-id mask, hash and compare ---------*/ 2643b3a8eb9SGleb Smirnoff 2653b3a8eb9SGleb Smirnoff /*--- support functions for the qht hashtable ---- 2663b3a8eb9SGleb Smirnoff * Entries are hashed by flow-id 2673b3a8eb9SGleb Smirnoff */ 2683b3a8eb9SGleb Smirnoff static uint32_t 2693b3a8eb9SGleb Smirnoff q_hash(uintptr_t key, int flags, void *arg) 2703b3a8eb9SGleb Smirnoff { 2713b3a8eb9SGleb Smirnoff /* compute the hash slot from the flow id */ 2723b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ? 2733b3a8eb9SGleb Smirnoff &((struct dn_queue *)key)->ni.fid : 2743b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key; 2753b3a8eb9SGleb Smirnoff 2763b3a8eb9SGleb Smirnoff return flow_id_hash(id); 2773b3a8eb9SGleb Smirnoff } 2783b3a8eb9SGleb Smirnoff 2793b3a8eb9SGleb Smirnoff static int 2803b3a8eb9SGleb Smirnoff q_match(void *obj, uintptr_t key, int flags, void *arg) 2813b3a8eb9SGleb Smirnoff { 2823b3a8eb9SGleb Smirnoff struct dn_queue *o = (struct dn_queue *)obj; 2833b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id2; 2843b3a8eb9SGleb Smirnoff 2853b3a8eb9SGleb Smirnoff if (flags & DNHT_KEY_IS_OBJ) { 2863b3a8eb9SGleb Smirnoff /* compare pointers */ 2873b3a8eb9SGleb Smirnoff id2 = &((struct dn_queue *)key)->ni.fid; 2883b3a8eb9SGleb Smirnoff } else { 2893b3a8eb9SGleb Smirnoff id2 = (struct ipfw_flow_id *)key; 2903b3a8eb9SGleb Smirnoff } 2913b3a8eb9SGleb Smirnoff return (0 == flow_id_cmp(&o->ni.fid, id2)); 2923b3a8eb9SGleb Smirnoff } 2933b3a8eb9SGleb Smirnoff 2943b3a8eb9SGleb Smirnoff /* 2953b3a8eb9SGleb Smirnoff * create a new queue instance for the given 'key'. 2963b3a8eb9SGleb Smirnoff */ 2973b3a8eb9SGleb Smirnoff static void * 2983b3a8eb9SGleb Smirnoff q_new(uintptr_t key, int flags, void *arg) 2993b3a8eb9SGleb Smirnoff { 3003b3a8eb9SGleb Smirnoff struct dn_queue *q, *template = arg; 3013b3a8eb9SGleb Smirnoff struct dn_fsk *fs = template->fs; 3023b3a8eb9SGleb Smirnoff int size = sizeof(*q) + fs->sched->fp->q_datalen; 3033b3a8eb9SGleb Smirnoff 3043b3a8eb9SGleb Smirnoff q = malloc(size, M_DUMMYNET, M_NOWAIT | M_ZERO); 3053b3a8eb9SGleb Smirnoff if (q == NULL) { 3063b3a8eb9SGleb Smirnoff D("no memory for new queue"); 3073b3a8eb9SGleb Smirnoff return NULL; 3083b3a8eb9SGleb Smirnoff } 3093b3a8eb9SGleb Smirnoff 3103b3a8eb9SGleb Smirnoff set_oid(&q->ni.oid, DN_QUEUE, size); 3113b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) 3123b3a8eb9SGleb Smirnoff q->ni.fid = *(struct ipfw_flow_id *)key; 3133b3a8eb9SGleb Smirnoff q->fs = fs; 3143b3a8eb9SGleb Smirnoff q->_si = template->_si; 3153b3a8eb9SGleb Smirnoff q->_si->q_count++; 3163b3a8eb9SGleb Smirnoff 3173b3a8eb9SGleb Smirnoff if (fs->sched->fp->new_queue) 3183b3a8eb9SGleb Smirnoff fs->sched->fp->new_queue(q); 3193b3a8eb9SGleb Smirnoff dn_cfg.queue_count++; 3203b3a8eb9SGleb Smirnoff return q; 3213b3a8eb9SGleb Smirnoff } 3223b3a8eb9SGleb Smirnoff 3233b3a8eb9SGleb Smirnoff /* 3243b3a8eb9SGleb Smirnoff * Notify schedulers that a queue is going away. 3253b3a8eb9SGleb Smirnoff * If (flags & DN_DESTROY), also free the packets. 3263b3a8eb9SGleb Smirnoff * The version for callbacks is called q_delete_cb(). 3273b3a8eb9SGleb Smirnoff */ 3283b3a8eb9SGleb Smirnoff static void 3293b3a8eb9SGleb Smirnoff dn_delete_queue(struct dn_queue *q, int flags) 3303b3a8eb9SGleb Smirnoff { 3313b3a8eb9SGleb Smirnoff struct dn_fsk *fs = q->fs; 3323b3a8eb9SGleb Smirnoff 3333b3a8eb9SGleb Smirnoff // D("fs %p si %p\n", fs, q->_si); 3343b3a8eb9SGleb Smirnoff /* notify the parent scheduler that the queue is going away */ 3353b3a8eb9SGleb Smirnoff if (fs && fs->sched->fp->free_queue) 3363b3a8eb9SGleb Smirnoff fs->sched->fp->free_queue(q); 3373b3a8eb9SGleb Smirnoff q->_si->q_count--; 3383b3a8eb9SGleb Smirnoff q->_si = NULL; 3393b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY) { 3403b3a8eb9SGleb Smirnoff if (q->mq.head) 3413b3a8eb9SGleb Smirnoff dn_free_pkts(q->mq.head); 3423b3a8eb9SGleb Smirnoff bzero(q, sizeof(*q)); // safety 3433b3a8eb9SGleb Smirnoff free(q, M_DUMMYNET); 3443b3a8eb9SGleb Smirnoff dn_cfg.queue_count--; 3453b3a8eb9SGleb Smirnoff } 3463b3a8eb9SGleb Smirnoff } 3473b3a8eb9SGleb Smirnoff 3483b3a8eb9SGleb Smirnoff static int 3493b3a8eb9SGleb Smirnoff q_delete_cb(void *q, void *arg) 3503b3a8eb9SGleb Smirnoff { 3513b3a8eb9SGleb Smirnoff int flags = (int)(uintptr_t)arg; 3523b3a8eb9SGleb Smirnoff dn_delete_queue(q, flags); 3533b3a8eb9SGleb Smirnoff return (flags & DN_DESTROY) ? DNHT_SCAN_DEL : 0; 3543b3a8eb9SGleb Smirnoff } 3553b3a8eb9SGleb Smirnoff 3563b3a8eb9SGleb Smirnoff /* 3573b3a8eb9SGleb Smirnoff * calls dn_delete_queue/q_delete_cb on all queues, 3583b3a8eb9SGleb Smirnoff * which notifies the parent scheduler and possibly drains packets. 3593b3a8eb9SGleb Smirnoff * flags & DN_DESTROY: drains queues and destroy qht; 3603b3a8eb9SGleb Smirnoff */ 3613b3a8eb9SGleb Smirnoff static void 3623b3a8eb9SGleb Smirnoff qht_delete(struct dn_fsk *fs, int flags) 3633b3a8eb9SGleb Smirnoff { 3643b3a8eb9SGleb Smirnoff ND("fs %d start flags %d qht %p", 3653b3a8eb9SGleb Smirnoff fs->fs.fs_nr, flags, fs->qht); 3663b3a8eb9SGleb Smirnoff if (!fs->qht) 3673b3a8eb9SGleb Smirnoff return; 3683b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) { 3693b3a8eb9SGleb Smirnoff dn_ht_scan(fs->qht, q_delete_cb, (void *)(uintptr_t)flags); 3703b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY) { 3713b3a8eb9SGleb Smirnoff dn_ht_free(fs->qht, 0); 3723b3a8eb9SGleb Smirnoff fs->qht = NULL; 3733b3a8eb9SGleb Smirnoff } 3743b3a8eb9SGleb Smirnoff } else { 3753b3a8eb9SGleb Smirnoff dn_delete_queue((struct dn_queue *)(fs->qht), flags); 3763b3a8eb9SGleb Smirnoff if (flags & DN_DESTROY) 3773b3a8eb9SGleb Smirnoff fs->qht = NULL; 3783b3a8eb9SGleb Smirnoff } 3793b3a8eb9SGleb Smirnoff } 3803b3a8eb9SGleb Smirnoff 3813b3a8eb9SGleb Smirnoff /* 3823b3a8eb9SGleb Smirnoff * Find and possibly create the queue for a MULTIQUEUE scheduler. 3833b3a8eb9SGleb Smirnoff * We never call it for !MULTIQUEUE (the queue is in the sch_inst). 3843b3a8eb9SGleb Smirnoff */ 3853b3a8eb9SGleb Smirnoff struct dn_queue * 3863b3a8eb9SGleb Smirnoff ipdn_q_find(struct dn_fsk *fs, struct dn_sch_inst *si, 3873b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id) 3883b3a8eb9SGleb Smirnoff { 3893b3a8eb9SGleb Smirnoff struct dn_queue template; 3903b3a8eb9SGleb Smirnoff 3913b3a8eb9SGleb Smirnoff template._si = si; 3923b3a8eb9SGleb Smirnoff template.fs = fs; 3933b3a8eb9SGleb Smirnoff 3943b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) { 3953b3a8eb9SGleb Smirnoff struct ipfw_flow_id masked_id; 3963b3a8eb9SGleb Smirnoff if (fs->qht == NULL) { 3973b3a8eb9SGleb Smirnoff fs->qht = dn_ht_init(NULL, fs->fs.buckets, 3983b3a8eb9SGleb Smirnoff offsetof(struct dn_queue, q_next), 3993b3a8eb9SGleb Smirnoff q_hash, q_match, q_new); 4003b3a8eb9SGleb Smirnoff if (fs->qht == NULL) 4013b3a8eb9SGleb Smirnoff return NULL; 4023b3a8eb9SGleb Smirnoff } 4033b3a8eb9SGleb Smirnoff masked_id = *id; 4043b3a8eb9SGleb Smirnoff flow_id_mask(&fs->fsk_mask, &masked_id); 4053b3a8eb9SGleb Smirnoff return dn_ht_find(fs->qht, (uintptr_t)&masked_id, 4063b3a8eb9SGleb Smirnoff DNHT_INSERT, &template); 4073b3a8eb9SGleb Smirnoff } else { 4083b3a8eb9SGleb Smirnoff if (fs->qht == NULL) 4093b3a8eb9SGleb Smirnoff fs->qht = q_new(0, 0, &template); 4103b3a8eb9SGleb Smirnoff return (struct dn_queue *)fs->qht; 4113b3a8eb9SGleb Smirnoff } 4123b3a8eb9SGleb Smirnoff } 4133b3a8eb9SGleb Smirnoff /*--- end of queue hash table ---*/ 4143b3a8eb9SGleb Smirnoff 4153b3a8eb9SGleb Smirnoff /*--- support functions for the sch_inst hashtable ---- 4163b3a8eb9SGleb Smirnoff * 4173b3a8eb9SGleb Smirnoff * These are hashed by flow-id 4183b3a8eb9SGleb Smirnoff */ 4193b3a8eb9SGleb Smirnoff static uint32_t 4203b3a8eb9SGleb Smirnoff si_hash(uintptr_t key, int flags, void *arg) 4213b3a8eb9SGleb Smirnoff { 4223b3a8eb9SGleb Smirnoff /* compute the hash slot from the flow id */ 4233b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id = (flags & DNHT_KEY_IS_OBJ) ? 4243b3a8eb9SGleb Smirnoff &((struct dn_sch_inst *)key)->ni.fid : 4253b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key; 4263b3a8eb9SGleb Smirnoff 4273b3a8eb9SGleb Smirnoff return flow_id_hash(id); 4283b3a8eb9SGleb Smirnoff } 4293b3a8eb9SGleb Smirnoff 4303b3a8eb9SGleb Smirnoff static int 4313b3a8eb9SGleb Smirnoff si_match(void *obj, uintptr_t key, int flags, void *arg) 4323b3a8eb9SGleb Smirnoff { 4333b3a8eb9SGleb Smirnoff struct dn_sch_inst *o = obj; 4343b3a8eb9SGleb Smirnoff struct ipfw_flow_id *id2; 4353b3a8eb9SGleb Smirnoff 4363b3a8eb9SGleb Smirnoff id2 = (flags & DNHT_KEY_IS_OBJ) ? 4373b3a8eb9SGleb Smirnoff &((struct dn_sch_inst *)key)->ni.fid : 4383b3a8eb9SGleb Smirnoff (struct ipfw_flow_id *)key; 4393b3a8eb9SGleb Smirnoff return flow_id_cmp(&o->ni.fid, id2) == 0; 4403b3a8eb9SGleb Smirnoff } 4413b3a8eb9SGleb Smirnoff 4423b3a8eb9SGleb Smirnoff /* 4433b3a8eb9SGleb Smirnoff * create a new instance for the given 'key' 4443b3a8eb9SGleb Smirnoff * Allocate memory for instance, delay line and scheduler private data. 4453b3a8eb9SGleb Smirnoff */ 4463b3a8eb9SGleb Smirnoff static void * 4473b3a8eb9SGleb Smirnoff si_new(uintptr_t key, int flags, void *arg) 4483b3a8eb9SGleb Smirnoff { 4493b3a8eb9SGleb Smirnoff struct dn_schk *s = arg; 4503b3a8eb9SGleb Smirnoff struct dn_sch_inst *si; 4513b3a8eb9SGleb Smirnoff int l = sizeof(*si) + s->fp->si_datalen; 4523b3a8eb9SGleb Smirnoff 4533b3a8eb9SGleb Smirnoff si = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO); 4543b3a8eb9SGleb Smirnoff if (si == NULL) 4553b3a8eb9SGleb Smirnoff goto error; 4563b3a8eb9SGleb Smirnoff 4573b3a8eb9SGleb Smirnoff /* Set length only for the part passed up to userland. */ 4583b3a8eb9SGleb Smirnoff set_oid(&si->ni.oid, DN_SCH_I, sizeof(struct dn_flow)); 4593b3a8eb9SGleb Smirnoff set_oid(&(si->dline.oid), DN_DELAY_LINE, 4603b3a8eb9SGleb Smirnoff sizeof(struct delay_line)); 4613b3a8eb9SGleb Smirnoff /* mark si and dline as outside the event queue */ 4623b3a8eb9SGleb Smirnoff si->ni.oid.id = si->dline.oid.id = -1; 4633b3a8eb9SGleb Smirnoff 4643b3a8eb9SGleb Smirnoff si->sched = s; 4653b3a8eb9SGleb Smirnoff si->dline.si = si; 4663b3a8eb9SGleb Smirnoff 4673b3a8eb9SGleb Smirnoff if (s->fp->new_sched && s->fp->new_sched(si)) { 4683b3a8eb9SGleb Smirnoff D("new_sched error"); 4693b3a8eb9SGleb Smirnoff goto error; 4703b3a8eb9SGleb Smirnoff } 4713b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) 4723b3a8eb9SGleb Smirnoff si->ni.fid = *(struct ipfw_flow_id *)key; 4733b3a8eb9SGleb Smirnoff 4743b3a8eb9SGleb Smirnoff dn_cfg.si_count++; 4753b3a8eb9SGleb Smirnoff return si; 4763b3a8eb9SGleb Smirnoff 4773b3a8eb9SGleb Smirnoff error: 4783b3a8eb9SGleb Smirnoff if (si) { 4793b3a8eb9SGleb Smirnoff bzero(si, sizeof(*si)); // safety 4803b3a8eb9SGleb Smirnoff free(si, M_DUMMYNET); 4813b3a8eb9SGleb Smirnoff } 4823b3a8eb9SGleb Smirnoff return NULL; 4833b3a8eb9SGleb Smirnoff } 4843b3a8eb9SGleb Smirnoff 4853b3a8eb9SGleb Smirnoff /* 4863b3a8eb9SGleb Smirnoff * Callback from siht to delete all scheduler instances. Remove 4873b3a8eb9SGleb Smirnoff * si and delay line from the system heap, destroy all queues. 4883b3a8eb9SGleb Smirnoff * We assume that all flowset have been notified and do not 4893b3a8eb9SGleb Smirnoff * point to us anymore. 4903b3a8eb9SGleb Smirnoff */ 4913b3a8eb9SGleb Smirnoff static int 4923b3a8eb9SGleb Smirnoff si_destroy(void *_si, void *arg) 4933b3a8eb9SGleb Smirnoff { 4943b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si; 4953b3a8eb9SGleb Smirnoff struct dn_schk *s = si->sched; 4963b3a8eb9SGleb Smirnoff struct delay_line *dl = &si->dline; 4973b3a8eb9SGleb Smirnoff 4983b3a8eb9SGleb Smirnoff if (dl->oid.subtype) /* remove delay line from event heap */ 4993b3a8eb9SGleb Smirnoff heap_extract(&dn_cfg.evheap, dl); 5003b3a8eb9SGleb Smirnoff dn_free_pkts(dl->mq.head); /* drain delay line */ 5013b3a8eb9SGleb Smirnoff if (si->kflags & DN_ACTIVE) /* remove si from event heap */ 5023b3a8eb9SGleb Smirnoff heap_extract(&dn_cfg.evheap, si); 5033b3a8eb9SGleb Smirnoff if (s->fp->free_sched) 5043b3a8eb9SGleb Smirnoff s->fp->free_sched(si); 5053b3a8eb9SGleb Smirnoff bzero(si, sizeof(*si)); /* safety */ 5063b3a8eb9SGleb Smirnoff free(si, M_DUMMYNET); 5073b3a8eb9SGleb Smirnoff dn_cfg.si_count--; 5083b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL; 5093b3a8eb9SGleb Smirnoff } 5103b3a8eb9SGleb Smirnoff 5113b3a8eb9SGleb Smirnoff /* 5123b3a8eb9SGleb Smirnoff * Find the scheduler instance for this packet. If we need to apply 5133b3a8eb9SGleb Smirnoff * a mask, do on a local copy of the flow_id to preserve the original. 5143b3a8eb9SGleb Smirnoff * Assume siht is always initialized if we have a mask. 5153b3a8eb9SGleb Smirnoff */ 5163b3a8eb9SGleb Smirnoff struct dn_sch_inst * 5173b3a8eb9SGleb Smirnoff ipdn_si_find(struct dn_schk *s, struct ipfw_flow_id *id) 5183b3a8eb9SGleb Smirnoff { 5193b3a8eb9SGleb Smirnoff 5203b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) { 5213b3a8eb9SGleb Smirnoff struct ipfw_flow_id id_t = *id; 5223b3a8eb9SGleb Smirnoff flow_id_mask(&s->sch.sched_mask, &id_t); 5233b3a8eb9SGleb Smirnoff return dn_ht_find(s->siht, (uintptr_t)&id_t, 5243b3a8eb9SGleb Smirnoff DNHT_INSERT, s); 5253b3a8eb9SGleb Smirnoff } 5263b3a8eb9SGleb Smirnoff if (!s->siht) 5273b3a8eb9SGleb Smirnoff s->siht = si_new(0, 0, s); 5283b3a8eb9SGleb Smirnoff return (struct dn_sch_inst *)s->siht; 5293b3a8eb9SGleb Smirnoff } 5303b3a8eb9SGleb Smirnoff 5313b3a8eb9SGleb Smirnoff /* callback to flush credit for the scheduler instance */ 5323b3a8eb9SGleb Smirnoff static int 5333b3a8eb9SGleb Smirnoff si_reset_credit(void *_si, void *arg) 5343b3a8eb9SGleb Smirnoff { 5353b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si; 5363b3a8eb9SGleb Smirnoff struct dn_link *p = &si->sched->link; 5373b3a8eb9SGleb Smirnoff 5383b3a8eb9SGleb Smirnoff si->credit = p->burst + (dn_cfg.io_fast ? p->bandwidth : 0); 5393b3a8eb9SGleb Smirnoff return 0; 5403b3a8eb9SGleb Smirnoff } 5413b3a8eb9SGleb Smirnoff 5423b3a8eb9SGleb Smirnoff static void 5433b3a8eb9SGleb Smirnoff schk_reset_credit(struct dn_schk *s) 5443b3a8eb9SGleb Smirnoff { 5453b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) 5463b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, si_reset_credit, NULL); 5473b3a8eb9SGleb Smirnoff else if (s->siht) 5483b3a8eb9SGleb Smirnoff si_reset_credit(s->siht, NULL); 5493b3a8eb9SGleb Smirnoff } 5503b3a8eb9SGleb Smirnoff /*---- end of sch_inst hashtable ---------------------*/ 5513b3a8eb9SGleb Smirnoff 5523b3a8eb9SGleb Smirnoff /*------------------------------------------------------- 5533b3a8eb9SGleb Smirnoff * flowset hash (fshash) support. Entries are hashed by fs_nr. 5543b3a8eb9SGleb Smirnoff * New allocations are put in the fsunlinked list, from which 5553b3a8eb9SGleb Smirnoff * they are removed when they point to a specific scheduler. 5563b3a8eb9SGleb Smirnoff */ 5573b3a8eb9SGleb Smirnoff static uint32_t 5583b3a8eb9SGleb Smirnoff fsk_hash(uintptr_t key, int flags, void *arg) 5593b3a8eb9SGleb Smirnoff { 5603b3a8eb9SGleb Smirnoff uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key : 5613b3a8eb9SGleb Smirnoff ((struct dn_fsk *)key)->fs.fs_nr; 5623b3a8eb9SGleb Smirnoff 5633b3a8eb9SGleb Smirnoff return ( (i>>8)^(i>>4)^i ); 5643b3a8eb9SGleb Smirnoff } 5653b3a8eb9SGleb Smirnoff 5663b3a8eb9SGleb Smirnoff static int 5673b3a8eb9SGleb Smirnoff fsk_match(void *obj, uintptr_t key, int flags, void *arg) 5683b3a8eb9SGleb Smirnoff { 5693b3a8eb9SGleb Smirnoff struct dn_fsk *fs = obj; 5703b3a8eb9SGleb Smirnoff int i = !(flags & DNHT_KEY_IS_OBJ) ? key : 5713b3a8eb9SGleb Smirnoff ((struct dn_fsk *)key)->fs.fs_nr; 5723b3a8eb9SGleb Smirnoff 5733b3a8eb9SGleb Smirnoff return (fs->fs.fs_nr == i); 5743b3a8eb9SGleb Smirnoff } 5753b3a8eb9SGleb Smirnoff 5763b3a8eb9SGleb Smirnoff static void * 5773b3a8eb9SGleb Smirnoff fsk_new(uintptr_t key, int flags, void *arg) 5783b3a8eb9SGleb Smirnoff { 5793b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 5803b3a8eb9SGleb Smirnoff 5813b3a8eb9SGleb Smirnoff fs = malloc(sizeof(*fs), M_DUMMYNET, M_NOWAIT | M_ZERO); 5823b3a8eb9SGleb Smirnoff if (fs) { 5833b3a8eb9SGleb Smirnoff set_oid(&fs->fs.oid, DN_FS, sizeof(fs->fs)); 5843b3a8eb9SGleb Smirnoff dn_cfg.fsk_count++; 5853b3a8eb9SGleb Smirnoff fs->drain_bucket = 0; 5863b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain); 5873b3a8eb9SGleb Smirnoff } 5883b3a8eb9SGleb Smirnoff return fs; 5893b3a8eb9SGleb Smirnoff } 5903b3a8eb9SGleb Smirnoff 5913b3a8eb9SGleb Smirnoff /* 5923b3a8eb9SGleb Smirnoff * detach flowset from its current scheduler. Flags as follows: 5933b3a8eb9SGleb Smirnoff * DN_DETACH removes from the fsk_list 5943b3a8eb9SGleb Smirnoff * DN_DESTROY deletes individual queues 5953b3a8eb9SGleb Smirnoff * DN_DELETE_FS destroys the flowset (otherwise goes in unlinked). 5963b3a8eb9SGleb Smirnoff */ 5973b3a8eb9SGleb Smirnoff static void 5983b3a8eb9SGleb Smirnoff fsk_detach(struct dn_fsk *fs, int flags) 5993b3a8eb9SGleb Smirnoff { 6003b3a8eb9SGleb Smirnoff if (flags & DN_DELETE_FS) 6013b3a8eb9SGleb Smirnoff flags |= DN_DESTROY; 6023b3a8eb9SGleb Smirnoff ND("fs %d from sched %d flags %s %s %s", 6033b3a8eb9SGleb Smirnoff fs->fs.fs_nr, fs->fs.sched_nr, 6043b3a8eb9SGleb Smirnoff (flags & DN_DELETE_FS) ? "DEL_FS":"", 6053b3a8eb9SGleb Smirnoff (flags & DN_DESTROY) ? "DEL":"", 6063b3a8eb9SGleb Smirnoff (flags & DN_DETACH) ? "DET":""); 6073b3a8eb9SGleb Smirnoff if (flags & DN_DETACH) { /* detach from the list */ 6083b3a8eb9SGleb Smirnoff struct dn_fsk_head *h; 6093b3a8eb9SGleb Smirnoff h = fs->sched ? &fs->sched->fsk_list : &dn_cfg.fsu; 6103b3a8eb9SGleb Smirnoff SLIST_REMOVE(h, fs, dn_fsk, sch_chain); 6113b3a8eb9SGleb Smirnoff } 6123b3a8eb9SGleb Smirnoff /* Free the RED parameters, they will be recomputed on 6133b3a8eb9SGleb Smirnoff * subsequent attach if needed. 6143b3a8eb9SGleb Smirnoff */ 6153b3a8eb9SGleb Smirnoff if (fs->w_q_lookup) 6163b3a8eb9SGleb Smirnoff free(fs->w_q_lookup, M_DUMMYNET); 6173b3a8eb9SGleb Smirnoff fs->w_q_lookup = NULL; 6183b3a8eb9SGleb Smirnoff qht_delete(fs, flags); 6193b3a8eb9SGleb Smirnoff if (fs->sched && fs->sched->fp->free_fsk) 6203b3a8eb9SGleb Smirnoff fs->sched->fp->free_fsk(fs); 6213b3a8eb9SGleb Smirnoff fs->sched = NULL; 6223b3a8eb9SGleb Smirnoff if (flags & DN_DELETE_FS) { 623578acad3SEitan Adler bzero(fs, sizeof(*fs)); /* safety */ 6243b3a8eb9SGleb Smirnoff free(fs, M_DUMMYNET); 6253b3a8eb9SGleb Smirnoff dn_cfg.fsk_count--; 6263b3a8eb9SGleb Smirnoff } else { 6273b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&dn_cfg.fsu, fs, sch_chain); 6283b3a8eb9SGleb Smirnoff } 6293b3a8eb9SGleb Smirnoff } 6303b3a8eb9SGleb Smirnoff 6313b3a8eb9SGleb Smirnoff /* 6323b3a8eb9SGleb Smirnoff * Detach or destroy all flowsets in a list. 6333b3a8eb9SGleb Smirnoff * flags specifies what to do: 6343b3a8eb9SGleb Smirnoff * DN_DESTROY: flush all queues 6353b3a8eb9SGleb Smirnoff * DN_DELETE_FS: DN_DESTROY + destroy flowset 6363b3a8eb9SGleb Smirnoff * DN_DELETE_FS implies DN_DESTROY 6373b3a8eb9SGleb Smirnoff */ 6383b3a8eb9SGleb Smirnoff static void 6393b3a8eb9SGleb Smirnoff fsk_detach_list(struct dn_fsk_head *h, int flags) 6403b3a8eb9SGleb Smirnoff { 6413b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 6423b3a8eb9SGleb Smirnoff int n = 0; /* only for stats */ 6433b3a8eb9SGleb Smirnoff 6443b3a8eb9SGleb Smirnoff ND("head %p flags %x", h, flags); 6453b3a8eb9SGleb Smirnoff while ((fs = SLIST_FIRST(h))) { 6463b3a8eb9SGleb Smirnoff SLIST_REMOVE_HEAD(h, sch_chain); 6473b3a8eb9SGleb Smirnoff n++; 6483b3a8eb9SGleb Smirnoff fsk_detach(fs, flags); 6493b3a8eb9SGleb Smirnoff } 6503b3a8eb9SGleb Smirnoff ND("done %d flowsets", n); 6513b3a8eb9SGleb Smirnoff } 6523b3a8eb9SGleb Smirnoff 6533b3a8eb9SGleb Smirnoff /* 6543b3a8eb9SGleb Smirnoff * called on 'queue X delete' -- removes the flowset from fshash, 6553b3a8eb9SGleb Smirnoff * deletes all queues for the flowset, and removes the flowset. 6563b3a8eb9SGleb Smirnoff */ 6573b3a8eb9SGleb Smirnoff static int 6583b3a8eb9SGleb Smirnoff delete_fs(int i, int locked) 6593b3a8eb9SGleb Smirnoff { 6603b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 6613b3a8eb9SGleb Smirnoff int err = 0; 6623b3a8eb9SGleb Smirnoff 6633b3a8eb9SGleb Smirnoff if (!locked) 6643b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 6653b3a8eb9SGleb Smirnoff fs = dn_ht_find(dn_cfg.fshash, i, DNHT_REMOVE, NULL); 6663b3a8eb9SGleb Smirnoff ND("fs %d found %p", i, fs); 6673b3a8eb9SGleb Smirnoff if (fs) { 6683b3a8eb9SGleb Smirnoff fsk_detach(fs, DN_DETACH | DN_DELETE_FS); 6693b3a8eb9SGleb Smirnoff err = 0; 6703b3a8eb9SGleb Smirnoff } else 6713b3a8eb9SGleb Smirnoff err = EINVAL; 6723b3a8eb9SGleb Smirnoff if (!locked) 6733b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 6743b3a8eb9SGleb Smirnoff return err; 6753b3a8eb9SGleb Smirnoff } 6763b3a8eb9SGleb Smirnoff 6773b3a8eb9SGleb Smirnoff /*----- end of flowset hashtable support -------------*/ 6783b3a8eb9SGleb Smirnoff 6793b3a8eb9SGleb Smirnoff /*------------------------------------------------------------ 6803b3a8eb9SGleb Smirnoff * Scheduler hash. When searching by index we pass sched_nr, 6813b3a8eb9SGleb Smirnoff * otherwise we pass struct dn_sch * which is the first field in 6823b3a8eb9SGleb Smirnoff * struct dn_schk so we can cast between the two. We use this trick 6833b3a8eb9SGleb Smirnoff * because in the create phase (but it should be fixed). 6843b3a8eb9SGleb Smirnoff */ 6853b3a8eb9SGleb Smirnoff static uint32_t 6863b3a8eb9SGleb Smirnoff schk_hash(uintptr_t key, int flags, void *_arg) 6873b3a8eb9SGleb Smirnoff { 6883b3a8eb9SGleb Smirnoff uint32_t i = !(flags & DNHT_KEY_IS_OBJ) ? key : 6893b3a8eb9SGleb Smirnoff ((struct dn_schk *)key)->sch.sched_nr; 6903b3a8eb9SGleb Smirnoff return ( (i>>8)^(i>>4)^i ); 6913b3a8eb9SGleb Smirnoff } 6923b3a8eb9SGleb Smirnoff 6933b3a8eb9SGleb Smirnoff static int 6943b3a8eb9SGleb Smirnoff schk_match(void *obj, uintptr_t key, int flags, void *_arg) 6953b3a8eb9SGleb Smirnoff { 6963b3a8eb9SGleb Smirnoff struct dn_schk *s = (struct dn_schk *)obj; 6973b3a8eb9SGleb Smirnoff int i = !(flags & DNHT_KEY_IS_OBJ) ? key : 6983b3a8eb9SGleb Smirnoff ((struct dn_schk *)key)->sch.sched_nr; 6993b3a8eb9SGleb Smirnoff return (s->sch.sched_nr == i); 7003b3a8eb9SGleb Smirnoff } 7013b3a8eb9SGleb Smirnoff 7023b3a8eb9SGleb Smirnoff /* 7033b3a8eb9SGleb Smirnoff * Create the entry and intialize with the sched hash if needed. 7043b3a8eb9SGleb Smirnoff * Leave s->fp unset so we can tell whether a dn_ht_find() returns 7053b3a8eb9SGleb Smirnoff * a new object or a previously existing one. 7063b3a8eb9SGleb Smirnoff */ 7073b3a8eb9SGleb Smirnoff static void * 7083b3a8eb9SGleb Smirnoff schk_new(uintptr_t key, int flags, void *arg) 7093b3a8eb9SGleb Smirnoff { 7103b3a8eb9SGleb Smirnoff struct schk_new_arg *a = arg; 7113b3a8eb9SGleb Smirnoff struct dn_schk *s; 7123b3a8eb9SGleb Smirnoff int l = sizeof(*s) +a->fp->schk_datalen; 7133b3a8eb9SGleb Smirnoff 7143b3a8eb9SGleb Smirnoff s = malloc(l, M_DUMMYNET, M_NOWAIT | M_ZERO); 7153b3a8eb9SGleb Smirnoff if (s == NULL) 7163b3a8eb9SGleb Smirnoff return NULL; 7173b3a8eb9SGleb Smirnoff set_oid(&s->link.oid, DN_LINK, sizeof(s->link)); 7183b3a8eb9SGleb Smirnoff s->sch = *a->sch; // copy initial values 7193b3a8eb9SGleb Smirnoff s->link.link_nr = s->sch.sched_nr; 7203b3a8eb9SGleb Smirnoff SLIST_INIT(&s->fsk_list); 7213b3a8eb9SGleb Smirnoff /* initialize the hash table or create the single instance */ 7223b3a8eb9SGleb Smirnoff s->fp = a->fp; /* si_new needs this */ 7233b3a8eb9SGleb Smirnoff s->drain_bucket = 0; 7243b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) { 7253b3a8eb9SGleb Smirnoff s->siht = dn_ht_init(NULL, s->sch.buckets, 7263b3a8eb9SGleb Smirnoff offsetof(struct dn_sch_inst, si_next), 7273b3a8eb9SGleb Smirnoff si_hash, si_match, si_new); 7283b3a8eb9SGleb Smirnoff if (s->siht == NULL) { 7293b3a8eb9SGleb Smirnoff free(s, M_DUMMYNET); 7303b3a8eb9SGleb Smirnoff return NULL; 7313b3a8eb9SGleb Smirnoff } 7323b3a8eb9SGleb Smirnoff } 7333b3a8eb9SGleb Smirnoff s->fp = NULL; /* mark as a new scheduler */ 7343b3a8eb9SGleb Smirnoff dn_cfg.schk_count++; 7353b3a8eb9SGleb Smirnoff return s; 7363b3a8eb9SGleb Smirnoff } 7373b3a8eb9SGleb Smirnoff 7383b3a8eb9SGleb Smirnoff /* 7393b3a8eb9SGleb Smirnoff * Callback for sched delete. Notify all attached flowsets to 7403b3a8eb9SGleb Smirnoff * detach from the scheduler, destroy the internal flowset, and 7413b3a8eb9SGleb Smirnoff * all instances. The scheduler goes away too. 7423b3a8eb9SGleb Smirnoff * arg is 0 (only detach flowsets and destroy instances) 7433b3a8eb9SGleb Smirnoff * DN_DESTROY (detach & delete queues, delete schk) 7443b3a8eb9SGleb Smirnoff * or DN_DELETE_FS (delete queues and flowsets, delete schk) 7453b3a8eb9SGleb Smirnoff */ 7463b3a8eb9SGleb Smirnoff static int 7473b3a8eb9SGleb Smirnoff schk_delete_cb(void *obj, void *arg) 7483b3a8eb9SGleb Smirnoff { 7493b3a8eb9SGleb Smirnoff struct dn_schk *s = obj; 7503b3a8eb9SGleb Smirnoff #if 0 7513b3a8eb9SGleb Smirnoff int a = (int)arg; 7523b3a8eb9SGleb Smirnoff ND("sched %d arg %s%s", 7533b3a8eb9SGleb Smirnoff s->sch.sched_nr, 7543b3a8eb9SGleb Smirnoff a&DN_DESTROY ? "DEL ":"", 7553b3a8eb9SGleb Smirnoff a&DN_DELETE_FS ? "DEL_FS":""); 7563b3a8eb9SGleb Smirnoff #endif 7573b3a8eb9SGleb Smirnoff fsk_detach_list(&s->fsk_list, arg ? DN_DESTROY : 0); 7583b3a8eb9SGleb Smirnoff /* no more flowset pointing to us now */ 7593b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) { 7603b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, si_destroy, NULL); 7613b3a8eb9SGleb Smirnoff dn_ht_free(s->siht, 0); 7623b3a8eb9SGleb Smirnoff } else if (s->siht) 7633b3a8eb9SGleb Smirnoff si_destroy(s->siht, NULL); 7643b3a8eb9SGleb Smirnoff if (s->profile) { 7653b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET); 7663b3a8eb9SGleb Smirnoff s->profile = NULL; 7673b3a8eb9SGleb Smirnoff } 7683b3a8eb9SGleb Smirnoff s->siht = NULL; 7693b3a8eb9SGleb Smirnoff if (s->fp->destroy) 7703b3a8eb9SGleb Smirnoff s->fp->destroy(s); 7713b3a8eb9SGleb Smirnoff bzero(s, sizeof(*s)); // safety 7723b3a8eb9SGleb Smirnoff free(obj, M_DUMMYNET); 7733b3a8eb9SGleb Smirnoff dn_cfg.schk_count--; 7743b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL; 7753b3a8eb9SGleb Smirnoff } 7763b3a8eb9SGleb Smirnoff 7773b3a8eb9SGleb Smirnoff /* 7783b3a8eb9SGleb Smirnoff * called on a 'sched X delete' command. Deletes a single scheduler. 7793b3a8eb9SGleb Smirnoff * This is done by removing from the schedhash, unlinking all 7803b3a8eb9SGleb Smirnoff * flowsets and deleting their traffic. 7813b3a8eb9SGleb Smirnoff */ 7823b3a8eb9SGleb Smirnoff static int 7833b3a8eb9SGleb Smirnoff delete_schk(int i) 7843b3a8eb9SGleb Smirnoff { 7853b3a8eb9SGleb Smirnoff struct dn_schk *s; 7863b3a8eb9SGleb Smirnoff 7873b3a8eb9SGleb Smirnoff s = dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL); 7883b3a8eb9SGleb Smirnoff ND("%d %p", i, s); 7893b3a8eb9SGleb Smirnoff if (!s) 7903b3a8eb9SGleb Smirnoff return EINVAL; 7913b3a8eb9SGleb Smirnoff delete_fs(i + DN_MAX_ID, 1); /* first delete internal fs */ 7923b3a8eb9SGleb Smirnoff /* then detach flowsets, delete traffic */ 7933b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void*)(uintptr_t)DN_DESTROY); 7943b3a8eb9SGleb Smirnoff return 0; 7953b3a8eb9SGleb Smirnoff } 7963b3a8eb9SGleb Smirnoff /*--- end of schk hashtable support ---*/ 7973b3a8eb9SGleb Smirnoff 7983b3a8eb9SGleb Smirnoff static int 7993b3a8eb9SGleb Smirnoff copy_obj(char **start, char *end, void *_o, const char *msg, int i) 8003b3a8eb9SGleb Smirnoff { 8013b3a8eb9SGleb Smirnoff struct dn_id *o = _o; 8023b3a8eb9SGleb Smirnoff int have = end - *start; 8033b3a8eb9SGleb Smirnoff 8043b3a8eb9SGleb Smirnoff if (have < o->len || o->len == 0 || o->type == 0) { 8053b3a8eb9SGleb Smirnoff D("(WARN) type %d %s %d have %d need %d", 8063b3a8eb9SGleb Smirnoff o->type, msg, i, have, o->len); 8073b3a8eb9SGleb Smirnoff return 1; 8083b3a8eb9SGleb Smirnoff } 8093b3a8eb9SGleb Smirnoff ND("type %d %s %d len %d", o->type, msg, i, o->len); 8103b3a8eb9SGleb Smirnoff bcopy(_o, *start, o->len); 8113b3a8eb9SGleb Smirnoff if (o->type == DN_LINK) { 8123b3a8eb9SGleb Smirnoff /* Adjust burst parameter for link */ 8133b3a8eb9SGleb Smirnoff struct dn_link *l = (struct dn_link *)*start; 8143b3a8eb9SGleb Smirnoff l->burst = div64(l->burst, 8 * hz); 8153b3a8eb9SGleb Smirnoff l->delay = l->delay * 1000 / hz; 8163b3a8eb9SGleb Smirnoff } else if (o->type == DN_SCH) { 8173b3a8eb9SGleb Smirnoff /* Set id->id to the number of instances */ 8183b3a8eb9SGleb Smirnoff struct dn_schk *s = _o; 8193b3a8eb9SGleb Smirnoff struct dn_id *id = (struct dn_id *)(*start); 8203b3a8eb9SGleb Smirnoff id->id = (s->sch.flags & DN_HAVE_MASK) ? 8213b3a8eb9SGleb Smirnoff dn_ht_entries(s->siht) : (s->siht ? 1 : 0); 8223b3a8eb9SGleb Smirnoff } 8233b3a8eb9SGleb Smirnoff *start += o->len; 8243b3a8eb9SGleb Smirnoff return 0; 8253b3a8eb9SGleb Smirnoff } 8263b3a8eb9SGleb Smirnoff 8273b3a8eb9SGleb Smirnoff /* Specific function to copy a queue. 8283b3a8eb9SGleb Smirnoff * Copies only the user-visible part of a queue (which is in 8293b3a8eb9SGleb Smirnoff * a struct dn_flow), and sets len accordingly. 8303b3a8eb9SGleb Smirnoff */ 8313b3a8eb9SGleb Smirnoff static int 8323b3a8eb9SGleb Smirnoff copy_obj_q(char **start, char *end, void *_o, const char *msg, int i) 8333b3a8eb9SGleb Smirnoff { 8343b3a8eb9SGleb Smirnoff struct dn_id *o = _o; 8353b3a8eb9SGleb Smirnoff int have = end - *start; 8363b3a8eb9SGleb Smirnoff int len = sizeof(struct dn_flow); /* see above comment */ 8373b3a8eb9SGleb Smirnoff 8383b3a8eb9SGleb Smirnoff if (have < len || o->len == 0 || o->type != DN_QUEUE) { 8393b3a8eb9SGleb Smirnoff D("ERROR type %d %s %d have %d need %d", 8403b3a8eb9SGleb Smirnoff o->type, msg, i, have, len); 8413b3a8eb9SGleb Smirnoff return 1; 8423b3a8eb9SGleb Smirnoff } 8433b3a8eb9SGleb Smirnoff ND("type %d %s %d len %d", o->type, msg, i, len); 8443b3a8eb9SGleb Smirnoff bcopy(_o, *start, len); 8453b3a8eb9SGleb Smirnoff ((struct dn_id*)(*start))->len = len; 8463b3a8eb9SGleb Smirnoff *start += len; 8473b3a8eb9SGleb Smirnoff return 0; 8483b3a8eb9SGleb Smirnoff } 8493b3a8eb9SGleb Smirnoff 8503b3a8eb9SGleb Smirnoff static int 8513b3a8eb9SGleb Smirnoff copy_q_cb(void *obj, void *arg) 8523b3a8eb9SGleb Smirnoff { 8533b3a8eb9SGleb Smirnoff struct dn_queue *q = obj; 8543b3a8eb9SGleb Smirnoff struct copy_args *a = arg; 8553b3a8eb9SGleb Smirnoff struct dn_flow *ni = (struct dn_flow *)(*a->start); 8563b3a8eb9SGleb Smirnoff if (copy_obj_q(a->start, a->end, &q->ni, "queue", -1)) 8573b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 8583b3a8eb9SGleb Smirnoff ni->oid.type = DN_FLOW; /* override the DN_QUEUE */ 8593b3a8eb9SGleb Smirnoff ni->oid.id = si_hash((uintptr_t)&ni->fid, 0, NULL); 8603b3a8eb9SGleb Smirnoff return 0; 8613b3a8eb9SGleb Smirnoff } 8623b3a8eb9SGleb Smirnoff 8633b3a8eb9SGleb Smirnoff static int 8643b3a8eb9SGleb Smirnoff copy_q(struct copy_args *a, struct dn_fsk *fs, int flags) 8653b3a8eb9SGleb Smirnoff { 8663b3a8eb9SGleb Smirnoff if (!fs->qht) 8673b3a8eb9SGleb Smirnoff return 0; 8683b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) 8693b3a8eb9SGleb Smirnoff dn_ht_scan(fs->qht, copy_q_cb, a); 8703b3a8eb9SGleb Smirnoff else 8713b3a8eb9SGleb Smirnoff copy_q_cb(fs->qht, a); 8723b3a8eb9SGleb Smirnoff return 0; 8733b3a8eb9SGleb Smirnoff } 8743b3a8eb9SGleb Smirnoff 8753b3a8eb9SGleb Smirnoff /* 8763b3a8eb9SGleb Smirnoff * This routine only copies the initial part of a profile ? XXX 8773b3a8eb9SGleb Smirnoff */ 8783b3a8eb9SGleb Smirnoff static int 8793b3a8eb9SGleb Smirnoff copy_profile(struct copy_args *a, struct dn_profile *p) 8803b3a8eb9SGleb Smirnoff { 8813b3a8eb9SGleb Smirnoff int have = a->end - *a->start; 8823b3a8eb9SGleb Smirnoff /* XXX here we check for max length */ 8833b3a8eb9SGleb Smirnoff int profile_len = sizeof(struct dn_profile) - 8843b3a8eb9SGleb Smirnoff ED_MAX_SAMPLES_NO*sizeof(int); 8853b3a8eb9SGleb Smirnoff 8863b3a8eb9SGleb Smirnoff if (p == NULL) 8873b3a8eb9SGleb Smirnoff return 0; 8883b3a8eb9SGleb Smirnoff if (have < profile_len) { 8893b3a8eb9SGleb Smirnoff D("error have %d need %d", have, profile_len); 8903b3a8eb9SGleb Smirnoff return 1; 8913b3a8eb9SGleb Smirnoff } 8923b3a8eb9SGleb Smirnoff bcopy(p, *a->start, profile_len); 8933b3a8eb9SGleb Smirnoff ((struct dn_id *)(*a->start))->len = profile_len; 8943b3a8eb9SGleb Smirnoff *a->start += profile_len; 8953b3a8eb9SGleb Smirnoff return 0; 8963b3a8eb9SGleb Smirnoff } 8973b3a8eb9SGleb Smirnoff 8983b3a8eb9SGleb Smirnoff static int 8993b3a8eb9SGleb Smirnoff copy_flowset(struct copy_args *a, struct dn_fsk *fs, int flags) 9003b3a8eb9SGleb Smirnoff { 9013b3a8eb9SGleb Smirnoff struct dn_fs *ufs = (struct dn_fs *)(*a->start); 9023b3a8eb9SGleb Smirnoff if (!fs) 9033b3a8eb9SGleb Smirnoff return 0; 9043b3a8eb9SGleb Smirnoff ND("flowset %d", fs->fs.fs_nr); 9053b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, &fs->fs, "flowset", fs->fs.fs_nr)) 9063b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 9073b3a8eb9SGleb Smirnoff ufs->oid.id = (fs->fs.flags & DN_QHT_HASH) ? 9083b3a8eb9SGleb Smirnoff dn_ht_entries(fs->qht) : (fs->qht ? 1 : 0); 9093b3a8eb9SGleb Smirnoff if (flags) { /* copy queues */ 9103b3a8eb9SGleb Smirnoff copy_q(a, fs, 0); 9113b3a8eb9SGleb Smirnoff } 9123b3a8eb9SGleb Smirnoff return 0; 9133b3a8eb9SGleb Smirnoff } 9143b3a8eb9SGleb Smirnoff 9153b3a8eb9SGleb Smirnoff static int 9163b3a8eb9SGleb Smirnoff copy_si_cb(void *obj, void *arg) 9173b3a8eb9SGleb Smirnoff { 9183b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = obj; 9193b3a8eb9SGleb Smirnoff struct copy_args *a = arg; 9203b3a8eb9SGleb Smirnoff struct dn_flow *ni = (struct dn_flow *)(*a->start); 9213b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, &si->ni, "inst", 9223b3a8eb9SGleb Smirnoff si->sched->sch.sched_nr)) 9233b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 9243b3a8eb9SGleb Smirnoff ni->oid.type = DN_FLOW; /* override the DN_SCH_I */ 9253b3a8eb9SGleb Smirnoff ni->oid.id = si_hash((uintptr_t)si, DNHT_KEY_IS_OBJ, NULL); 9263b3a8eb9SGleb Smirnoff return 0; 9273b3a8eb9SGleb Smirnoff } 9283b3a8eb9SGleb Smirnoff 9293b3a8eb9SGleb Smirnoff static int 9303b3a8eb9SGleb Smirnoff copy_si(struct copy_args *a, struct dn_schk *s, int flags) 9313b3a8eb9SGleb Smirnoff { 9323b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) 9333b3a8eb9SGleb Smirnoff dn_ht_scan(s->siht, copy_si_cb, a); 9343b3a8eb9SGleb Smirnoff else if (s->siht) 9353b3a8eb9SGleb Smirnoff copy_si_cb(s->siht, a); 9363b3a8eb9SGleb Smirnoff return 0; 9373b3a8eb9SGleb Smirnoff } 9383b3a8eb9SGleb Smirnoff 9393b3a8eb9SGleb Smirnoff /* 9403b3a8eb9SGleb Smirnoff * compute a list of children of a scheduler and copy up 9413b3a8eb9SGleb Smirnoff */ 9423b3a8eb9SGleb Smirnoff static int 9433b3a8eb9SGleb Smirnoff copy_fsk_list(struct copy_args *a, struct dn_schk *s, int flags) 9443b3a8eb9SGleb Smirnoff { 9453b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 9463b3a8eb9SGleb Smirnoff struct dn_id *o; 9473b3a8eb9SGleb Smirnoff uint32_t *p; 9483b3a8eb9SGleb Smirnoff 9493b3a8eb9SGleb Smirnoff int n = 0, space = sizeof(*o); 9503b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain) { 9513b3a8eb9SGleb Smirnoff if (fs->fs.fs_nr < DN_MAX_ID) 9523b3a8eb9SGleb Smirnoff n++; 9533b3a8eb9SGleb Smirnoff } 9543b3a8eb9SGleb Smirnoff space += n * sizeof(uint32_t); 9553b3a8eb9SGleb Smirnoff DX(3, "sched %d has %d flowsets", s->sch.sched_nr, n); 9563b3a8eb9SGleb Smirnoff if (a->end - *(a->start) < space) 9573b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 9583b3a8eb9SGleb Smirnoff o = (struct dn_id *)(*(a->start)); 9593b3a8eb9SGleb Smirnoff o->len = space; 9603b3a8eb9SGleb Smirnoff *a->start += o->len; 9613b3a8eb9SGleb Smirnoff o->type = DN_TEXT; 9623b3a8eb9SGleb Smirnoff p = (uint32_t *)(o+1); 9633b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain) 9643b3a8eb9SGleb Smirnoff if (fs->fs.fs_nr < DN_MAX_ID) 9653b3a8eb9SGleb Smirnoff *p++ = fs->fs.fs_nr; 9663b3a8eb9SGleb Smirnoff return 0; 9673b3a8eb9SGleb Smirnoff } 9683b3a8eb9SGleb Smirnoff 9693b3a8eb9SGleb Smirnoff static int 9703b3a8eb9SGleb Smirnoff copy_data_helper(void *_o, void *_arg) 9713b3a8eb9SGleb Smirnoff { 9723b3a8eb9SGleb Smirnoff struct copy_args *a = _arg; 9733b3a8eb9SGleb Smirnoff uint32_t *r = a->extra->r; /* start of first range */ 9743b3a8eb9SGleb Smirnoff uint32_t *lim; /* first invalid pointer */ 9753b3a8eb9SGleb Smirnoff int n; 9763b3a8eb9SGleb Smirnoff 9773b3a8eb9SGleb Smirnoff lim = (uint32_t *)((char *)(a->extra) + a->extra->o.len); 9783b3a8eb9SGleb Smirnoff 9793b3a8eb9SGleb Smirnoff if (a->type == DN_LINK || a->type == DN_SCH) { 9803b3a8eb9SGleb Smirnoff /* pipe|sched show, we receive a dn_schk */ 9813b3a8eb9SGleb Smirnoff struct dn_schk *s = _o; 9823b3a8eb9SGleb Smirnoff 9833b3a8eb9SGleb Smirnoff n = s->sch.sched_nr; 9843b3a8eb9SGleb Smirnoff if (a->type == DN_SCH && n >= DN_MAX_ID) 9853b3a8eb9SGleb Smirnoff return 0; /* not a scheduler */ 9863b3a8eb9SGleb Smirnoff if (a->type == DN_LINK && n <= DN_MAX_ID) 9873b3a8eb9SGleb Smirnoff return 0; /* not a pipe */ 9883b3a8eb9SGleb Smirnoff 9893b3a8eb9SGleb Smirnoff /* see if the object is within one of our ranges */ 9903b3a8eb9SGleb Smirnoff for (;r < lim; r += 2) { 9913b3a8eb9SGleb Smirnoff if (n < r[0] || n > r[1]) 9923b3a8eb9SGleb Smirnoff continue; 9933b3a8eb9SGleb Smirnoff /* Found a valid entry, copy and we are done */ 9943b3a8eb9SGleb Smirnoff if (a->flags & DN_C_LINK) { 9953b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, 9963b3a8eb9SGleb Smirnoff &s->link, "link", n)) 9973b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 9983b3a8eb9SGleb Smirnoff if (copy_profile(a, s->profile)) 9993b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 10003b3a8eb9SGleb Smirnoff if (copy_flowset(a, s->fs, 0)) 10013b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 10023b3a8eb9SGleb Smirnoff } 10033b3a8eb9SGleb Smirnoff if (a->flags & DN_C_SCH) { 10043b3a8eb9SGleb Smirnoff if (copy_obj(a->start, a->end, 10053b3a8eb9SGleb Smirnoff &s->sch, "sched", n)) 10063b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 10073b3a8eb9SGleb Smirnoff /* list all attached flowsets */ 10083b3a8eb9SGleb Smirnoff if (copy_fsk_list(a, s, 0)) 10093b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 10103b3a8eb9SGleb Smirnoff } 10113b3a8eb9SGleb Smirnoff if (a->flags & DN_C_FLOW) 10123b3a8eb9SGleb Smirnoff copy_si(a, s, 0); 10133b3a8eb9SGleb Smirnoff break; 10143b3a8eb9SGleb Smirnoff } 10153b3a8eb9SGleb Smirnoff } else if (a->type == DN_FS) { 10163b3a8eb9SGleb Smirnoff /* queue show, skip internal flowsets */ 10173b3a8eb9SGleb Smirnoff struct dn_fsk *fs = _o; 10183b3a8eb9SGleb Smirnoff 10193b3a8eb9SGleb Smirnoff n = fs->fs.fs_nr; 10203b3a8eb9SGleb Smirnoff if (n >= DN_MAX_ID) 10213b3a8eb9SGleb Smirnoff return 0; 10223b3a8eb9SGleb Smirnoff /* see if the object is within one of our ranges */ 10233b3a8eb9SGleb Smirnoff for (;r < lim; r += 2) { 10243b3a8eb9SGleb Smirnoff if (n < r[0] || n > r[1]) 10253b3a8eb9SGleb Smirnoff continue; 10263b3a8eb9SGleb Smirnoff if (copy_flowset(a, fs, 0)) 10273b3a8eb9SGleb Smirnoff return DNHT_SCAN_END; 10283b3a8eb9SGleb Smirnoff copy_q(a, fs, 0); 10293b3a8eb9SGleb Smirnoff break; /* we are done */ 10303b3a8eb9SGleb Smirnoff } 10313b3a8eb9SGleb Smirnoff } 10323b3a8eb9SGleb Smirnoff return 0; 10333b3a8eb9SGleb Smirnoff } 10343b3a8eb9SGleb Smirnoff 10353b3a8eb9SGleb Smirnoff static inline struct dn_schk * 10363b3a8eb9SGleb Smirnoff locate_scheduler(int i) 10373b3a8eb9SGleb Smirnoff { 10383b3a8eb9SGleb Smirnoff return dn_ht_find(dn_cfg.schedhash, i, 0, NULL); 10393b3a8eb9SGleb Smirnoff } 10403b3a8eb9SGleb Smirnoff 10413b3a8eb9SGleb Smirnoff /* 10423b3a8eb9SGleb Smirnoff * red parameters are in fixed point arithmetic. 10433b3a8eb9SGleb Smirnoff */ 10443b3a8eb9SGleb Smirnoff static int 10453b3a8eb9SGleb Smirnoff config_red(struct dn_fsk *fs) 10463b3a8eb9SGleb Smirnoff { 10473b3a8eb9SGleb Smirnoff int64_t s, idle, weight, w0; 10483b3a8eb9SGleb Smirnoff int t, i; 10493b3a8eb9SGleb Smirnoff 10503b3a8eb9SGleb Smirnoff fs->w_q = fs->fs.w_q; 10513b3a8eb9SGleb Smirnoff fs->max_p = fs->fs.max_p; 10523b3a8eb9SGleb Smirnoff ND("called"); 10533b3a8eb9SGleb Smirnoff /* Doing stuff that was in userland */ 10543b3a8eb9SGleb Smirnoff i = fs->sched->link.bandwidth; 10553b3a8eb9SGleb Smirnoff s = (i <= 0) ? 0 : 10563b3a8eb9SGleb Smirnoff hz * dn_cfg.red_avg_pkt_size * 8 * SCALE(1) / i; 10573b3a8eb9SGleb Smirnoff 10583b3a8eb9SGleb Smirnoff idle = div64((s * 3) , fs->w_q); /* s, fs->w_q scaled; idle not scaled */ 10593b3a8eb9SGleb Smirnoff fs->lookup_step = div64(idle , dn_cfg.red_lookup_depth); 10603b3a8eb9SGleb Smirnoff /* fs->lookup_step not scaled, */ 10613b3a8eb9SGleb Smirnoff if (!fs->lookup_step) 10623b3a8eb9SGleb Smirnoff fs->lookup_step = 1; 10633b3a8eb9SGleb Smirnoff w0 = weight = SCALE(1) - fs->w_q; //fs->w_q scaled 10643b3a8eb9SGleb Smirnoff 10653b3a8eb9SGleb Smirnoff for (t = fs->lookup_step; t > 1; --t) 10663b3a8eb9SGleb Smirnoff weight = SCALE_MUL(weight, w0); 10673b3a8eb9SGleb Smirnoff fs->lookup_weight = (int)(weight); // scaled 10683b3a8eb9SGleb Smirnoff 10693b3a8eb9SGleb Smirnoff /* Now doing stuff that was in kerneland */ 10703b3a8eb9SGleb Smirnoff fs->min_th = SCALE(fs->fs.min_th); 10713b3a8eb9SGleb Smirnoff fs->max_th = SCALE(fs->fs.max_th); 10723b3a8eb9SGleb Smirnoff 10733b3a8eb9SGleb Smirnoff fs->c_1 = fs->max_p / (fs->fs.max_th - fs->fs.min_th); 10743b3a8eb9SGleb Smirnoff fs->c_2 = SCALE_MUL(fs->c_1, SCALE(fs->fs.min_th)); 10753b3a8eb9SGleb Smirnoff 10763b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_IS_GENTLE_RED) { 10773b3a8eb9SGleb Smirnoff fs->c_3 = (SCALE(1) - fs->max_p) / fs->fs.max_th; 10783b3a8eb9SGleb Smirnoff fs->c_4 = SCALE(1) - 2 * fs->max_p; 10793b3a8eb9SGleb Smirnoff } 10803b3a8eb9SGleb Smirnoff 10813b3a8eb9SGleb Smirnoff /* If the lookup table already exist, free and create it again. */ 10823b3a8eb9SGleb Smirnoff if (fs->w_q_lookup) { 10833b3a8eb9SGleb Smirnoff free(fs->w_q_lookup, M_DUMMYNET); 10843b3a8eb9SGleb Smirnoff fs->w_q_lookup = NULL; 10853b3a8eb9SGleb Smirnoff } 10863b3a8eb9SGleb Smirnoff if (dn_cfg.red_lookup_depth == 0) { 10873b3a8eb9SGleb Smirnoff printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth" 10883b3a8eb9SGleb Smirnoff "must be > 0\n"); 10893b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_RED; 10903b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_GENTLE_RED; 10913b3a8eb9SGleb Smirnoff return (EINVAL); 10923b3a8eb9SGleb Smirnoff } 10933b3a8eb9SGleb Smirnoff fs->lookup_depth = dn_cfg.red_lookup_depth; 10943b3a8eb9SGleb Smirnoff fs->w_q_lookup = (u_int *)malloc(fs->lookup_depth * sizeof(int), 10953b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT); 10963b3a8eb9SGleb Smirnoff if (fs->w_q_lookup == NULL) { 10973b3a8eb9SGleb Smirnoff printf("dummynet: sorry, cannot allocate red lookup table\n"); 10983b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_RED; 10993b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_IS_GENTLE_RED; 11003b3a8eb9SGleb Smirnoff return(ENOSPC); 11013b3a8eb9SGleb Smirnoff } 11023b3a8eb9SGleb Smirnoff 11033b3a8eb9SGleb Smirnoff /* Fill the lookup table with (1 - w_q)^x */ 11043b3a8eb9SGleb Smirnoff fs->w_q_lookup[0] = SCALE(1) - fs->w_q; 11053b3a8eb9SGleb Smirnoff 11063b3a8eb9SGleb Smirnoff for (i = 1; i < fs->lookup_depth; i++) 11073b3a8eb9SGleb Smirnoff fs->w_q_lookup[i] = 11083b3a8eb9SGleb Smirnoff SCALE_MUL(fs->w_q_lookup[i - 1], fs->lookup_weight); 11093b3a8eb9SGleb Smirnoff 11103b3a8eb9SGleb Smirnoff if (dn_cfg.red_avg_pkt_size < 1) 11113b3a8eb9SGleb Smirnoff dn_cfg.red_avg_pkt_size = 512; 11123b3a8eb9SGleb Smirnoff fs->avg_pkt_size = dn_cfg.red_avg_pkt_size; 11133b3a8eb9SGleb Smirnoff if (dn_cfg.red_max_pkt_size < 1) 11143b3a8eb9SGleb Smirnoff dn_cfg.red_max_pkt_size = 1500; 11153b3a8eb9SGleb Smirnoff fs->max_pkt_size = dn_cfg.red_max_pkt_size; 11163b3a8eb9SGleb Smirnoff ND("exit"); 11173b3a8eb9SGleb Smirnoff return 0; 11183b3a8eb9SGleb Smirnoff } 11193b3a8eb9SGleb Smirnoff 11203b3a8eb9SGleb Smirnoff /* Scan all flowset attached to this scheduler and update red */ 11213b3a8eb9SGleb Smirnoff static void 11223b3a8eb9SGleb Smirnoff update_red(struct dn_schk *s) 11233b3a8eb9SGleb Smirnoff { 11243b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 11253b3a8eb9SGleb Smirnoff SLIST_FOREACH(fs, &s->fsk_list, sch_chain) { 11263b3a8eb9SGleb Smirnoff if (fs && (fs->fs.flags & DN_IS_RED)) 11273b3a8eb9SGleb Smirnoff config_red(fs); 11283b3a8eb9SGleb Smirnoff } 11293b3a8eb9SGleb Smirnoff } 11303b3a8eb9SGleb Smirnoff 11313b3a8eb9SGleb Smirnoff /* attach flowset to scheduler s, possibly requeue */ 11323b3a8eb9SGleb Smirnoff static void 11333b3a8eb9SGleb Smirnoff fsk_attach(struct dn_fsk *fs, struct dn_schk *s) 11343b3a8eb9SGleb Smirnoff { 11353b3a8eb9SGleb Smirnoff ND("remove fs %d from fsunlinked, link to sched %d", 11363b3a8eb9SGleb Smirnoff fs->fs.fs_nr, s->sch.sched_nr); 11373b3a8eb9SGleb Smirnoff SLIST_REMOVE(&dn_cfg.fsu, fs, dn_fsk, sch_chain); 11383b3a8eb9SGleb Smirnoff fs->sched = s; 11393b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&s->fsk_list, fs, sch_chain); 11403b3a8eb9SGleb Smirnoff if (s->fp->new_fsk) 11413b3a8eb9SGleb Smirnoff s->fp->new_fsk(fs); 11423b3a8eb9SGleb Smirnoff /* XXX compute fsk_mask */ 11433b3a8eb9SGleb Smirnoff fs->fsk_mask = fs->fs.flow_mask; 11443b3a8eb9SGleb Smirnoff if (fs->sched->sch.flags & DN_HAVE_MASK) 11453b3a8eb9SGleb Smirnoff flow_id_or(&fs->sched->sch.sched_mask, &fs->fsk_mask); 11463b3a8eb9SGleb Smirnoff if (fs->qht) { 11473b3a8eb9SGleb Smirnoff /* 11483b3a8eb9SGleb Smirnoff * we must drain qht according to the old 11493b3a8eb9SGleb Smirnoff * type, and reinsert according to the new one. 11503b3a8eb9SGleb Smirnoff * The requeue is complex -- in general we need to 11513b3a8eb9SGleb Smirnoff * reclassify every single packet. 11523b3a8eb9SGleb Smirnoff * For the time being, let's hope qht is never set 11533b3a8eb9SGleb Smirnoff * when we reach this point. 11543b3a8eb9SGleb Smirnoff */ 11553b3a8eb9SGleb Smirnoff D("XXX TODO requeue from fs %d to sch %d", 11563b3a8eb9SGleb Smirnoff fs->fs.fs_nr, s->sch.sched_nr); 11573b3a8eb9SGleb Smirnoff fs->qht = NULL; 11583b3a8eb9SGleb Smirnoff } 11593b3a8eb9SGleb Smirnoff /* set the new type for qht */ 11603b3a8eb9SGleb Smirnoff if (nonzero_mask(&fs->fsk_mask)) 11613b3a8eb9SGleb Smirnoff fs->fs.flags |= DN_QHT_HASH; 11623b3a8eb9SGleb Smirnoff else 11633b3a8eb9SGleb Smirnoff fs->fs.flags &= ~DN_QHT_HASH; 11643b3a8eb9SGleb Smirnoff 11653b3a8eb9SGleb Smirnoff /* XXX config_red() can fail... */ 11663b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_IS_RED) 11673b3a8eb9SGleb Smirnoff config_red(fs); 11683b3a8eb9SGleb Smirnoff } 11693b3a8eb9SGleb Smirnoff 11703b3a8eb9SGleb Smirnoff /* update all flowsets which may refer to this scheduler */ 11713b3a8eb9SGleb Smirnoff static void 11723b3a8eb9SGleb Smirnoff update_fs(struct dn_schk *s) 11733b3a8eb9SGleb Smirnoff { 11743b3a8eb9SGleb Smirnoff struct dn_fsk *fs, *tmp; 11753b3a8eb9SGleb Smirnoff 11763b3a8eb9SGleb Smirnoff SLIST_FOREACH_SAFE(fs, &dn_cfg.fsu, sch_chain, tmp) { 11773b3a8eb9SGleb Smirnoff if (s->sch.sched_nr != fs->fs.sched_nr) { 11783b3a8eb9SGleb Smirnoff D("fs %d for sch %d not %d still unlinked", 11793b3a8eb9SGleb Smirnoff fs->fs.fs_nr, fs->fs.sched_nr, 11803b3a8eb9SGleb Smirnoff s->sch.sched_nr); 11813b3a8eb9SGleb Smirnoff continue; 11823b3a8eb9SGleb Smirnoff } 11833b3a8eb9SGleb Smirnoff fsk_attach(fs, s); 11843b3a8eb9SGleb Smirnoff } 11853b3a8eb9SGleb Smirnoff } 11863b3a8eb9SGleb Smirnoff 11873b3a8eb9SGleb Smirnoff /* 11883b3a8eb9SGleb Smirnoff * Configuration -- to preserve backward compatibility we use 11893b3a8eb9SGleb Smirnoff * the following scheme (N is 65536) 11903b3a8eb9SGleb Smirnoff * NUMBER SCHED LINK FLOWSET 11913b3a8eb9SGleb Smirnoff * 1 .. N-1 (1)WFQ (2)WFQ (3)queue 11923b3a8eb9SGleb Smirnoff * N+1 .. 2N-1 (4)FIFO (5)FIFO (6)FIFO for sched 1..N-1 11933b3a8eb9SGleb Smirnoff * 2N+1 .. 3N-1 -- -- (7)FIFO for sched N+1..2N-1 11943b3a8eb9SGleb Smirnoff * 11953b3a8eb9SGleb Smirnoff * "pipe i config" configures #1, #2 and #3 11963b3a8eb9SGleb Smirnoff * "sched i config" configures #1 and possibly #6 11973b3a8eb9SGleb Smirnoff * "queue i config" configures #3 11983b3a8eb9SGleb Smirnoff * #1 is configured with 'pipe i config' or 'sched i config' 11993b3a8eb9SGleb Smirnoff * #2 is configured with 'pipe i config', and created if not 12003b3a8eb9SGleb Smirnoff * existing with 'sched i config' 12013b3a8eb9SGleb Smirnoff * #3 is configured with 'queue i config' 12023b3a8eb9SGleb Smirnoff * #4 is automatically configured after #1, can only be FIFO 12033b3a8eb9SGleb Smirnoff * #5 is automatically configured after #2 12043b3a8eb9SGleb Smirnoff * #6 is automatically created when #1 is !MULTIQUEUE, 12053b3a8eb9SGleb Smirnoff * and can be updated. 12063b3a8eb9SGleb Smirnoff * #7 is automatically configured after #2 12073b3a8eb9SGleb Smirnoff */ 12083b3a8eb9SGleb Smirnoff 12093b3a8eb9SGleb Smirnoff /* 12103b3a8eb9SGleb Smirnoff * configure a link (and its FIFO instance) 12113b3a8eb9SGleb Smirnoff */ 12123b3a8eb9SGleb Smirnoff static int 12133b3a8eb9SGleb Smirnoff config_link(struct dn_link *p, struct dn_id *arg) 12143b3a8eb9SGleb Smirnoff { 12153b3a8eb9SGleb Smirnoff int i; 12163b3a8eb9SGleb Smirnoff 12173b3a8eb9SGleb Smirnoff if (p->oid.len != sizeof(*p)) { 12183b3a8eb9SGleb Smirnoff D("invalid pipe len %d", p->oid.len); 12193b3a8eb9SGleb Smirnoff return EINVAL; 12203b3a8eb9SGleb Smirnoff } 12213b3a8eb9SGleb Smirnoff i = p->link_nr; 12223b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID) 12233b3a8eb9SGleb Smirnoff return EINVAL; 12243b3a8eb9SGleb Smirnoff /* 12253b3a8eb9SGleb Smirnoff * The config program passes parameters as follows: 12263b3a8eb9SGleb Smirnoff * bw = bits/second (0 means no limits), 12273b3a8eb9SGleb Smirnoff * delay = ms, must be translated into ticks. 12283b3a8eb9SGleb Smirnoff * qsize = slots/bytes 12293b3a8eb9SGleb Smirnoff * burst ??? 12303b3a8eb9SGleb Smirnoff */ 12313b3a8eb9SGleb Smirnoff p->delay = (p->delay * hz) / 1000; 12323b3a8eb9SGleb Smirnoff /* Scale burst size: bytes -> bits * hz */ 12333b3a8eb9SGleb Smirnoff p->burst *= 8 * hz; 12343b3a8eb9SGleb Smirnoff 12353b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 12363b3a8eb9SGleb Smirnoff /* do it twice, base link and FIFO link */ 12373b3a8eb9SGleb Smirnoff for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) { 12383b3a8eb9SGleb Smirnoff struct dn_schk *s = locate_scheduler(i); 12393b3a8eb9SGleb Smirnoff if (s == NULL) { 12403b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 12413b3a8eb9SGleb Smirnoff D("sched %d not found", i); 12423b3a8eb9SGleb Smirnoff return EINVAL; 12433b3a8eb9SGleb Smirnoff } 12443b3a8eb9SGleb Smirnoff /* remove profile if exists */ 12453b3a8eb9SGleb Smirnoff if (s->profile) { 12463b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET); 12473b3a8eb9SGleb Smirnoff s->profile = NULL; 12483b3a8eb9SGleb Smirnoff } 12493b3a8eb9SGleb Smirnoff /* copy all parameters */ 12503b3a8eb9SGleb Smirnoff s->link.oid = p->oid; 12513b3a8eb9SGleb Smirnoff s->link.link_nr = i; 12523b3a8eb9SGleb Smirnoff s->link.delay = p->delay; 12533b3a8eb9SGleb Smirnoff if (s->link.bandwidth != p->bandwidth) { 12543b3a8eb9SGleb Smirnoff /* XXX bandwidth changes, need to update red params */ 12553b3a8eb9SGleb Smirnoff s->link.bandwidth = p->bandwidth; 12563b3a8eb9SGleb Smirnoff update_red(s); 12573b3a8eb9SGleb Smirnoff } 12583b3a8eb9SGleb Smirnoff s->link.burst = p->burst; 12593b3a8eb9SGleb Smirnoff schk_reset_credit(s); 12603b3a8eb9SGleb Smirnoff } 12613b3a8eb9SGleb Smirnoff dn_cfg.id++; 12623b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 12633b3a8eb9SGleb Smirnoff return 0; 12643b3a8eb9SGleb Smirnoff } 12653b3a8eb9SGleb Smirnoff 12663b3a8eb9SGleb Smirnoff /* 12673b3a8eb9SGleb Smirnoff * configure a flowset. Can be called from inside with locked=1, 12683b3a8eb9SGleb Smirnoff */ 12693b3a8eb9SGleb Smirnoff static struct dn_fsk * 12703b3a8eb9SGleb Smirnoff config_fs(struct dn_fs *nfs, struct dn_id *arg, int locked) 12713b3a8eb9SGleb Smirnoff { 12723b3a8eb9SGleb Smirnoff int i; 12733b3a8eb9SGleb Smirnoff struct dn_fsk *fs; 12743b3a8eb9SGleb Smirnoff 12753b3a8eb9SGleb Smirnoff if (nfs->oid.len != sizeof(*nfs)) { 12763b3a8eb9SGleb Smirnoff D("invalid flowset len %d", nfs->oid.len); 12773b3a8eb9SGleb Smirnoff return NULL; 12783b3a8eb9SGleb Smirnoff } 12793b3a8eb9SGleb Smirnoff i = nfs->fs_nr; 12803b3a8eb9SGleb Smirnoff if (i <= 0 || i >= 3*DN_MAX_ID) 12813b3a8eb9SGleb Smirnoff return NULL; 12823b3a8eb9SGleb Smirnoff ND("flowset %d", i); 12833b3a8eb9SGleb Smirnoff /* XXX other sanity checks */ 12843b3a8eb9SGleb Smirnoff if (nfs->flags & DN_QSIZE_BYTES) { 12853b3a8eb9SGleb Smirnoff ipdn_bound_var(&nfs->qsize, 16384, 12863b3a8eb9SGleb Smirnoff 1500, dn_cfg.byte_limit, NULL); // "queue byte size"); 12873b3a8eb9SGleb Smirnoff } else { 12883b3a8eb9SGleb Smirnoff ipdn_bound_var(&nfs->qsize, 50, 12893b3a8eb9SGleb Smirnoff 1, dn_cfg.slot_limit, NULL); // "queue slot size"); 12903b3a8eb9SGleb Smirnoff } 12913b3a8eb9SGleb Smirnoff if (nfs->flags & DN_HAVE_MASK) { 12923b3a8eb9SGleb Smirnoff /* make sure we have some buckets */ 12933b3a8eb9SGleb Smirnoff ipdn_bound_var((int *)&nfs->buckets, dn_cfg.hash_size, 12943b3a8eb9SGleb Smirnoff 1, dn_cfg.max_hash_size, "flowset buckets"); 12953b3a8eb9SGleb Smirnoff } else { 12963b3a8eb9SGleb Smirnoff nfs->buckets = 1; /* we only need 1 */ 12973b3a8eb9SGleb Smirnoff } 12983b3a8eb9SGleb Smirnoff if (!locked) 12993b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 13003b3a8eb9SGleb Smirnoff do { /* exit with break when done */ 13013b3a8eb9SGleb Smirnoff struct dn_schk *s; 13023b3a8eb9SGleb Smirnoff int flags = nfs->sched_nr ? DNHT_INSERT : 0; 13033b3a8eb9SGleb Smirnoff int j; 13043b3a8eb9SGleb Smirnoff int oldc = dn_cfg.fsk_count; 13053b3a8eb9SGleb Smirnoff fs = dn_ht_find(dn_cfg.fshash, i, flags, NULL); 13063b3a8eb9SGleb Smirnoff if (fs == NULL) { 13073b3a8eb9SGleb Smirnoff D("missing sched for flowset %d", i); 13083b3a8eb9SGleb Smirnoff break; 13093b3a8eb9SGleb Smirnoff } 13103b3a8eb9SGleb Smirnoff /* grab some defaults from the existing one */ 13113b3a8eb9SGleb Smirnoff if (nfs->sched_nr == 0) /* reuse */ 13123b3a8eb9SGleb Smirnoff nfs->sched_nr = fs->fs.sched_nr; 13133b3a8eb9SGleb Smirnoff for (j = 0; j < sizeof(nfs->par)/sizeof(nfs->par[0]); j++) { 13143b3a8eb9SGleb Smirnoff if (nfs->par[j] == -1) /* reuse */ 13153b3a8eb9SGleb Smirnoff nfs->par[j] = fs->fs.par[j]; 13163b3a8eb9SGleb Smirnoff } 13173b3a8eb9SGleb Smirnoff if (bcmp(&fs->fs, nfs, sizeof(*nfs)) == 0) { 13183b3a8eb9SGleb Smirnoff ND("flowset %d unchanged", i); 13193b3a8eb9SGleb Smirnoff break; /* no change, nothing to do */ 13203b3a8eb9SGleb Smirnoff } 13213b3a8eb9SGleb Smirnoff if (oldc != dn_cfg.fsk_count) /* new item */ 13223b3a8eb9SGleb Smirnoff dn_cfg.id++; 13233b3a8eb9SGleb Smirnoff s = locate_scheduler(nfs->sched_nr); 13243b3a8eb9SGleb Smirnoff /* detach from old scheduler if needed, preserving 13253b3a8eb9SGleb Smirnoff * queues if we need to reattach. Then update the 13263b3a8eb9SGleb Smirnoff * configuration, and possibly attach to the new sched. 13273b3a8eb9SGleb Smirnoff */ 13283b3a8eb9SGleb Smirnoff DX(2, "fs %d changed sched %d@%p to %d@%p", 13293b3a8eb9SGleb Smirnoff fs->fs.fs_nr, 13303b3a8eb9SGleb Smirnoff fs->fs.sched_nr, fs->sched, nfs->sched_nr, s); 13313b3a8eb9SGleb Smirnoff if (fs->sched) { 13323b3a8eb9SGleb Smirnoff int flags = s ? DN_DETACH : (DN_DETACH | DN_DESTROY); 13333b3a8eb9SGleb Smirnoff flags |= DN_DESTROY; /* XXX temporary */ 13343b3a8eb9SGleb Smirnoff fsk_detach(fs, flags); 13353b3a8eb9SGleb Smirnoff } 13363b3a8eb9SGleb Smirnoff fs->fs = *nfs; /* copy configuration */ 13373b3a8eb9SGleb Smirnoff if (s != NULL) 13383b3a8eb9SGleb Smirnoff fsk_attach(fs, s); 13393b3a8eb9SGleb Smirnoff } while (0); 13403b3a8eb9SGleb Smirnoff if (!locked) 13413b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 13423b3a8eb9SGleb Smirnoff return fs; 13433b3a8eb9SGleb Smirnoff } 13443b3a8eb9SGleb Smirnoff 13453b3a8eb9SGleb Smirnoff /* 13463b3a8eb9SGleb Smirnoff * config/reconfig a scheduler and its FIFO variant. 13473b3a8eb9SGleb Smirnoff * For !MULTIQUEUE schedulers, also set up the flowset. 13483b3a8eb9SGleb Smirnoff * 13493b3a8eb9SGleb Smirnoff * On reconfigurations (detected because s->fp is set), 13503b3a8eb9SGleb Smirnoff * detach existing flowsets preserving traffic, preserve link, 13513b3a8eb9SGleb Smirnoff * and delete the old scheduler creating a new one. 13523b3a8eb9SGleb Smirnoff */ 13533b3a8eb9SGleb Smirnoff static int 13543b3a8eb9SGleb Smirnoff config_sched(struct dn_sch *_nsch, struct dn_id *arg) 13553b3a8eb9SGleb Smirnoff { 13563b3a8eb9SGleb Smirnoff struct dn_schk *s; 13573b3a8eb9SGleb Smirnoff struct schk_new_arg a; /* argument for schk_new */ 13583b3a8eb9SGleb Smirnoff int i; 13593b3a8eb9SGleb Smirnoff struct dn_link p; /* copy of oldlink */ 13603b3a8eb9SGleb Smirnoff struct dn_profile *pf = NULL; /* copy of old link profile */ 13613b3a8eb9SGleb Smirnoff /* Used to preserv mask parameter */ 13623b3a8eb9SGleb Smirnoff struct ipfw_flow_id new_mask; 13633b3a8eb9SGleb Smirnoff int new_buckets = 0; 13643b3a8eb9SGleb Smirnoff int new_flags = 0; 13653b3a8eb9SGleb Smirnoff int pipe_cmd; 13663b3a8eb9SGleb Smirnoff int err = ENOMEM; 13673b3a8eb9SGleb Smirnoff 13683b3a8eb9SGleb Smirnoff a.sch = _nsch; 13693b3a8eb9SGleb Smirnoff if (a.sch->oid.len != sizeof(*a.sch)) { 13703b3a8eb9SGleb Smirnoff D("bad sched len %d", a.sch->oid.len); 13713b3a8eb9SGleb Smirnoff return EINVAL; 13723b3a8eb9SGleb Smirnoff } 13733b3a8eb9SGleb Smirnoff i = a.sch->sched_nr; 13743b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID) 13753b3a8eb9SGleb Smirnoff return EINVAL; 13763b3a8eb9SGleb Smirnoff /* make sure we have some buckets */ 13773b3a8eb9SGleb Smirnoff if (a.sch->flags & DN_HAVE_MASK) 13783b3a8eb9SGleb Smirnoff ipdn_bound_var((int *)&a.sch->buckets, dn_cfg.hash_size, 13793b3a8eb9SGleb Smirnoff 1, dn_cfg.max_hash_size, "sched buckets"); 13803b3a8eb9SGleb Smirnoff /* XXX other sanity checks */ 13813b3a8eb9SGleb Smirnoff bzero(&p, sizeof(p)); 13823b3a8eb9SGleb Smirnoff 13833b3a8eb9SGleb Smirnoff pipe_cmd = a.sch->flags & DN_PIPE_CMD; 13843b3a8eb9SGleb Smirnoff a.sch->flags &= ~DN_PIPE_CMD; //XXX do it even if is not set? 13853b3a8eb9SGleb Smirnoff if (pipe_cmd) { 13863b3a8eb9SGleb Smirnoff /* Copy mask parameter */ 13873b3a8eb9SGleb Smirnoff new_mask = a.sch->sched_mask; 13883b3a8eb9SGleb Smirnoff new_buckets = a.sch->buckets; 13893b3a8eb9SGleb Smirnoff new_flags = a.sch->flags; 13903b3a8eb9SGleb Smirnoff } 13913b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 13923b3a8eb9SGleb Smirnoff again: /* run twice, for wfq and fifo */ 13933b3a8eb9SGleb Smirnoff /* 13943b3a8eb9SGleb Smirnoff * lookup the type. If not supplied, use the previous one 13953b3a8eb9SGleb Smirnoff * or default to WF2Q+. Otherwise, return an error. 13963b3a8eb9SGleb Smirnoff */ 13973b3a8eb9SGleb Smirnoff dn_cfg.id++; 13983b3a8eb9SGleb Smirnoff a.fp = find_sched_type(a.sch->oid.subtype, a.sch->name); 13993b3a8eb9SGleb Smirnoff if (a.fp != NULL) { 14003b3a8eb9SGleb Smirnoff /* found. Lookup or create entry */ 14013b3a8eb9SGleb Smirnoff s = dn_ht_find(dn_cfg.schedhash, i, DNHT_INSERT, &a); 14023b3a8eb9SGleb Smirnoff } else if (a.sch->oid.subtype == 0 && !a.sch->name[0]) { 14033b3a8eb9SGleb Smirnoff /* No type. search existing s* or retry with WF2Q+ */ 14043b3a8eb9SGleb Smirnoff s = dn_ht_find(dn_cfg.schedhash, i, 0, &a); 14053b3a8eb9SGleb Smirnoff if (s != NULL) { 14063b3a8eb9SGleb Smirnoff a.fp = s->fp; 14073b3a8eb9SGleb Smirnoff /* Scheduler exists, skip to FIFO scheduler 14083b3a8eb9SGleb Smirnoff * if command was pipe config... 14093b3a8eb9SGleb Smirnoff */ 14103b3a8eb9SGleb Smirnoff if (pipe_cmd) 14113b3a8eb9SGleb Smirnoff goto next; 14123b3a8eb9SGleb Smirnoff } else { 14133b3a8eb9SGleb Smirnoff /* New scheduler, create a wf2q+ with no mask 14143b3a8eb9SGleb Smirnoff * if command was pipe config... 14153b3a8eb9SGleb Smirnoff */ 14163b3a8eb9SGleb Smirnoff if (pipe_cmd) { 14173b3a8eb9SGleb Smirnoff /* clear mask parameter */ 14183b3a8eb9SGleb Smirnoff bzero(&a.sch->sched_mask, sizeof(new_mask)); 14193b3a8eb9SGleb Smirnoff a.sch->buckets = 0; 14203b3a8eb9SGleb Smirnoff a.sch->flags &= ~DN_HAVE_MASK; 14213b3a8eb9SGleb Smirnoff } 14223b3a8eb9SGleb Smirnoff a.sch->oid.subtype = DN_SCHED_WF2QP; 14233b3a8eb9SGleb Smirnoff goto again; 14243b3a8eb9SGleb Smirnoff } 14253b3a8eb9SGleb Smirnoff } else { 14263b3a8eb9SGleb Smirnoff D("invalid scheduler type %d %s", 14273b3a8eb9SGleb Smirnoff a.sch->oid.subtype, a.sch->name); 14283b3a8eb9SGleb Smirnoff err = EINVAL; 14293b3a8eb9SGleb Smirnoff goto error; 14303b3a8eb9SGleb Smirnoff } 14313b3a8eb9SGleb Smirnoff /* normalize name and subtype */ 14323b3a8eb9SGleb Smirnoff a.sch->oid.subtype = a.fp->type; 14333b3a8eb9SGleb Smirnoff bzero(a.sch->name, sizeof(a.sch->name)); 14343b3a8eb9SGleb Smirnoff strlcpy(a.sch->name, a.fp->name, sizeof(a.sch->name)); 14353b3a8eb9SGleb Smirnoff if (s == NULL) { 14363b3a8eb9SGleb Smirnoff D("cannot allocate scheduler %d", i); 14373b3a8eb9SGleb Smirnoff goto error; 14383b3a8eb9SGleb Smirnoff } 14393b3a8eb9SGleb Smirnoff /* restore existing link if any */ 14403b3a8eb9SGleb Smirnoff if (p.link_nr) { 14413b3a8eb9SGleb Smirnoff s->link = p; 14423b3a8eb9SGleb Smirnoff if (!pf || pf->link_nr != p.link_nr) { /* no saved value */ 14433b3a8eb9SGleb Smirnoff s->profile = NULL; /* XXX maybe not needed */ 14443b3a8eb9SGleb Smirnoff } else { 14453b3a8eb9SGleb Smirnoff s->profile = malloc(sizeof(struct dn_profile), 14463b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO); 14473b3a8eb9SGleb Smirnoff if (s->profile == NULL) { 14483b3a8eb9SGleb Smirnoff D("cannot allocate profile"); 14493b3a8eb9SGleb Smirnoff goto error; //XXX 14503b3a8eb9SGleb Smirnoff } 14513b3a8eb9SGleb Smirnoff bcopy(pf, s->profile, sizeof(*pf)); 14523b3a8eb9SGleb Smirnoff } 14533b3a8eb9SGleb Smirnoff } 14543b3a8eb9SGleb Smirnoff p.link_nr = 0; 14553b3a8eb9SGleb Smirnoff if (s->fp == NULL) { 14563b3a8eb9SGleb Smirnoff DX(2, "sched %d new type %s", i, a.fp->name); 14573b3a8eb9SGleb Smirnoff } else if (s->fp != a.fp || 14583b3a8eb9SGleb Smirnoff bcmp(a.sch, &s->sch, sizeof(*a.sch)) ) { 14593b3a8eb9SGleb Smirnoff /* already existing. */ 14603b3a8eb9SGleb Smirnoff DX(2, "sched %d type changed from %s to %s", 14613b3a8eb9SGleb Smirnoff i, s->fp->name, a.fp->name); 14623b3a8eb9SGleb Smirnoff DX(4, " type/sub %d/%d -> %d/%d", 14633b3a8eb9SGleb Smirnoff s->sch.oid.type, s->sch.oid.subtype, 14643b3a8eb9SGleb Smirnoff a.sch->oid.type, a.sch->oid.subtype); 14653b3a8eb9SGleb Smirnoff if (s->link.link_nr == 0) 14663b3a8eb9SGleb Smirnoff D("XXX WARNING link 0 for sched %d", i); 14673b3a8eb9SGleb Smirnoff p = s->link; /* preserve link */ 14683b3a8eb9SGleb Smirnoff if (s->profile) {/* preserve profile */ 14693b3a8eb9SGleb Smirnoff if (!pf) 14703b3a8eb9SGleb Smirnoff pf = malloc(sizeof(*pf), 14713b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO); 14723b3a8eb9SGleb Smirnoff if (pf) /* XXX should issue a warning otherwise */ 14733b3a8eb9SGleb Smirnoff bcopy(s->profile, pf, sizeof(*pf)); 14743b3a8eb9SGleb Smirnoff } 14753b3a8eb9SGleb Smirnoff /* remove from the hash */ 14763b3a8eb9SGleb Smirnoff dn_ht_find(dn_cfg.schedhash, i, DNHT_REMOVE, NULL); 14773b3a8eb9SGleb Smirnoff /* Detach flowsets, preserve queues. */ 14783b3a8eb9SGleb Smirnoff // schk_delete_cb(s, NULL); 14793b3a8eb9SGleb Smirnoff // XXX temporarily, kill queues 14803b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void *)DN_DESTROY); 14813b3a8eb9SGleb Smirnoff goto again; 14823b3a8eb9SGleb Smirnoff } else { 14833b3a8eb9SGleb Smirnoff DX(4, "sched %d unchanged type %s", i, a.fp->name); 14843b3a8eb9SGleb Smirnoff } 14853b3a8eb9SGleb Smirnoff /* complete initialization */ 14863b3a8eb9SGleb Smirnoff s->sch = *a.sch; 14873b3a8eb9SGleb Smirnoff s->fp = a.fp; 14883b3a8eb9SGleb Smirnoff s->cfg = arg; 14893b3a8eb9SGleb Smirnoff // XXX schk_reset_credit(s); 14903b3a8eb9SGleb Smirnoff /* create the internal flowset if needed, 14913b3a8eb9SGleb Smirnoff * trying to reuse existing ones if available 14923b3a8eb9SGleb Smirnoff */ 14933b3a8eb9SGleb Smirnoff if (!(s->fp->flags & DN_MULTIQUEUE) && !s->fs) { 14943b3a8eb9SGleb Smirnoff s->fs = dn_ht_find(dn_cfg.fshash, i, 0, NULL); 14953b3a8eb9SGleb Smirnoff if (!s->fs) { 14963b3a8eb9SGleb Smirnoff struct dn_fs fs; 14973b3a8eb9SGleb Smirnoff bzero(&fs, sizeof(fs)); 14983b3a8eb9SGleb Smirnoff set_oid(&fs.oid, DN_FS, sizeof(fs)); 14993b3a8eb9SGleb Smirnoff fs.fs_nr = i + DN_MAX_ID; 15003b3a8eb9SGleb Smirnoff fs.sched_nr = i; 15013b3a8eb9SGleb Smirnoff s->fs = config_fs(&fs, NULL, 1 /* locked */); 15023b3a8eb9SGleb Smirnoff } 15033b3a8eb9SGleb Smirnoff if (!s->fs) { 15043b3a8eb9SGleb Smirnoff schk_delete_cb(s, (void *)DN_DESTROY); 15053b3a8eb9SGleb Smirnoff D("error creating internal fs for %d", i); 15063b3a8eb9SGleb Smirnoff goto error; 15073b3a8eb9SGleb Smirnoff } 15083b3a8eb9SGleb Smirnoff } 15093b3a8eb9SGleb Smirnoff /* call init function after the flowset is created */ 15103b3a8eb9SGleb Smirnoff if (s->fp->config) 15113b3a8eb9SGleb Smirnoff s->fp->config(s); 15123b3a8eb9SGleb Smirnoff update_fs(s); 15133b3a8eb9SGleb Smirnoff next: 15143b3a8eb9SGleb Smirnoff if (i < DN_MAX_ID) { /* now configure the FIFO instance */ 15153b3a8eb9SGleb Smirnoff i += DN_MAX_ID; 15163b3a8eb9SGleb Smirnoff if (pipe_cmd) { 15173b3a8eb9SGleb Smirnoff /* Restore mask parameter for FIFO */ 15183b3a8eb9SGleb Smirnoff a.sch->sched_mask = new_mask; 15193b3a8eb9SGleb Smirnoff a.sch->buckets = new_buckets; 15203b3a8eb9SGleb Smirnoff a.sch->flags = new_flags; 15213b3a8eb9SGleb Smirnoff } else { 15223b3a8eb9SGleb Smirnoff /* sched config shouldn't modify the FIFO scheduler */ 15233b3a8eb9SGleb Smirnoff if (dn_ht_find(dn_cfg.schedhash, i, 0, &a) != NULL) { 15243b3a8eb9SGleb Smirnoff /* FIFO already exist, don't touch it */ 15253b3a8eb9SGleb Smirnoff err = 0; /* and this is not an error */ 15263b3a8eb9SGleb Smirnoff goto error; 15273b3a8eb9SGleb Smirnoff } 15283b3a8eb9SGleb Smirnoff } 15293b3a8eb9SGleb Smirnoff a.sch->sched_nr = i; 15303b3a8eb9SGleb Smirnoff a.sch->oid.subtype = DN_SCHED_FIFO; 15313b3a8eb9SGleb Smirnoff bzero(a.sch->name, sizeof(a.sch->name)); 15323b3a8eb9SGleb Smirnoff goto again; 15333b3a8eb9SGleb Smirnoff } 15343b3a8eb9SGleb Smirnoff err = 0; 15353b3a8eb9SGleb Smirnoff error: 15363b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 15373b3a8eb9SGleb Smirnoff if (pf) 15383b3a8eb9SGleb Smirnoff free(pf, M_DUMMYNET); 15393b3a8eb9SGleb Smirnoff return err; 15403b3a8eb9SGleb Smirnoff } 15413b3a8eb9SGleb Smirnoff 15423b3a8eb9SGleb Smirnoff /* 15433b3a8eb9SGleb Smirnoff * attach a profile to a link 15443b3a8eb9SGleb Smirnoff */ 15453b3a8eb9SGleb Smirnoff static int 15463b3a8eb9SGleb Smirnoff config_profile(struct dn_profile *pf, struct dn_id *arg) 15473b3a8eb9SGleb Smirnoff { 15483b3a8eb9SGleb Smirnoff struct dn_schk *s; 15493b3a8eb9SGleb Smirnoff int i, olen, err = 0; 15503b3a8eb9SGleb Smirnoff 15513b3a8eb9SGleb Smirnoff if (pf->oid.len < sizeof(*pf)) { 15523b3a8eb9SGleb Smirnoff D("short profile len %d", pf->oid.len); 15533b3a8eb9SGleb Smirnoff return EINVAL; 15543b3a8eb9SGleb Smirnoff } 15553b3a8eb9SGleb Smirnoff i = pf->link_nr; 15563b3a8eb9SGleb Smirnoff if (i <= 0 || i >= DN_MAX_ID) 15573b3a8eb9SGleb Smirnoff return EINVAL; 15583b3a8eb9SGleb Smirnoff /* XXX other sanity checks */ 15593b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 15603b3a8eb9SGleb Smirnoff for (; i < 2*DN_MAX_ID; i += DN_MAX_ID) { 15613b3a8eb9SGleb Smirnoff s = locate_scheduler(i); 15623b3a8eb9SGleb Smirnoff 15633b3a8eb9SGleb Smirnoff if (s == NULL) { 15643b3a8eb9SGleb Smirnoff err = EINVAL; 15653b3a8eb9SGleb Smirnoff break; 15663b3a8eb9SGleb Smirnoff } 15673b3a8eb9SGleb Smirnoff dn_cfg.id++; 15683b3a8eb9SGleb Smirnoff /* 15693b3a8eb9SGleb Smirnoff * If we had a profile and the new one does not fit, 15703b3a8eb9SGleb Smirnoff * or it is deleted, then we need to free memory. 15713b3a8eb9SGleb Smirnoff */ 15723b3a8eb9SGleb Smirnoff if (s->profile && (pf->samples_no == 0 || 15733b3a8eb9SGleb Smirnoff s->profile->oid.len < pf->oid.len)) { 15743b3a8eb9SGleb Smirnoff free(s->profile, M_DUMMYNET); 15753b3a8eb9SGleb Smirnoff s->profile = NULL; 15763b3a8eb9SGleb Smirnoff } 15773b3a8eb9SGleb Smirnoff if (pf->samples_no == 0) 15783b3a8eb9SGleb Smirnoff continue; 15793b3a8eb9SGleb Smirnoff /* 15803b3a8eb9SGleb Smirnoff * new profile, possibly allocate memory 15813b3a8eb9SGleb Smirnoff * and copy data. 15823b3a8eb9SGleb Smirnoff */ 15833b3a8eb9SGleb Smirnoff if (s->profile == NULL) 15843b3a8eb9SGleb Smirnoff s->profile = malloc(pf->oid.len, 15853b3a8eb9SGleb Smirnoff M_DUMMYNET, M_NOWAIT | M_ZERO); 15863b3a8eb9SGleb Smirnoff if (s->profile == NULL) { 15873b3a8eb9SGleb Smirnoff D("no memory for profile %d", i); 15883b3a8eb9SGleb Smirnoff err = ENOMEM; 15893b3a8eb9SGleb Smirnoff break; 15903b3a8eb9SGleb Smirnoff } 15913b3a8eb9SGleb Smirnoff /* preserve larger length XXX double check */ 15923b3a8eb9SGleb Smirnoff olen = s->profile->oid.len; 15933b3a8eb9SGleb Smirnoff if (olen < pf->oid.len) 15943b3a8eb9SGleb Smirnoff olen = pf->oid.len; 15953b3a8eb9SGleb Smirnoff bcopy(pf, s->profile, pf->oid.len); 15963b3a8eb9SGleb Smirnoff s->profile->oid.len = olen; 15973b3a8eb9SGleb Smirnoff } 15983b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 15993b3a8eb9SGleb Smirnoff return err; 16003b3a8eb9SGleb Smirnoff } 16013b3a8eb9SGleb Smirnoff 16023b3a8eb9SGleb Smirnoff /* 16033b3a8eb9SGleb Smirnoff * Delete all objects: 16043b3a8eb9SGleb Smirnoff */ 16053b3a8eb9SGleb Smirnoff static void 16063b3a8eb9SGleb Smirnoff dummynet_flush(void) 16073b3a8eb9SGleb Smirnoff { 16083b3a8eb9SGleb Smirnoff 16093b3a8eb9SGleb Smirnoff /* delete all schedulers and related links/queues/flowsets */ 16103b3a8eb9SGleb Smirnoff dn_ht_scan(dn_cfg.schedhash, schk_delete_cb, 16113b3a8eb9SGleb Smirnoff (void *)(uintptr_t)DN_DELETE_FS); 16123b3a8eb9SGleb Smirnoff /* delete all remaining (unlinked) flowsets */ 16133b3a8eb9SGleb Smirnoff DX(4, "still %d unlinked fs", dn_cfg.fsk_count); 16143b3a8eb9SGleb Smirnoff dn_ht_free(dn_cfg.fshash, DNHT_REMOVE); 16153b3a8eb9SGleb Smirnoff fsk_detach_list(&dn_cfg.fsu, DN_DELETE_FS); 16163b3a8eb9SGleb Smirnoff /* Reinitialize system heap... */ 16173b3a8eb9SGleb Smirnoff heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id)); 16183b3a8eb9SGleb Smirnoff } 16193b3a8eb9SGleb Smirnoff 16203b3a8eb9SGleb Smirnoff /* 16213b3a8eb9SGleb Smirnoff * Main handler for configuration. We are guaranteed to be called 16223b3a8eb9SGleb Smirnoff * with an oid which is at least a dn_id. 16233b3a8eb9SGleb Smirnoff * - the first object is the command (config, delete, flush, ...) 16243b3a8eb9SGleb Smirnoff * - config_link must be issued after the corresponding config_sched 16253b3a8eb9SGleb Smirnoff * - parameters (DN_TXT) for an object must preceed the object 16263b3a8eb9SGleb Smirnoff * processed on a config_sched. 16273b3a8eb9SGleb Smirnoff */ 16283b3a8eb9SGleb Smirnoff int 16293b3a8eb9SGleb Smirnoff do_config(void *p, int l) 16303b3a8eb9SGleb Smirnoff { 16313b3a8eb9SGleb Smirnoff struct dn_id *next, *o; 16323b3a8eb9SGleb Smirnoff int err = 0, err2 = 0; 16333b3a8eb9SGleb Smirnoff struct dn_id *arg = NULL; 16343b3a8eb9SGleb Smirnoff uintptr_t *a; 16353b3a8eb9SGleb Smirnoff 16363b3a8eb9SGleb Smirnoff o = p; 16373b3a8eb9SGleb Smirnoff if (o->id != DN_API_VERSION) { 16383b3a8eb9SGleb Smirnoff D("invalid api version got %d need %d", 16393b3a8eb9SGleb Smirnoff o->id, DN_API_VERSION); 16403b3a8eb9SGleb Smirnoff return EINVAL; 16413b3a8eb9SGleb Smirnoff } 16423b3a8eb9SGleb Smirnoff for (; l >= sizeof(*o); o = next) { 16433b3a8eb9SGleb Smirnoff struct dn_id *prev = arg; 16443b3a8eb9SGleb Smirnoff if (o->len < sizeof(*o) || l < o->len) { 16453b3a8eb9SGleb Smirnoff D("bad len o->len %d len %d", o->len, l); 16463b3a8eb9SGleb Smirnoff err = EINVAL; 16473b3a8eb9SGleb Smirnoff break; 16483b3a8eb9SGleb Smirnoff } 16493b3a8eb9SGleb Smirnoff l -= o->len; 16503b3a8eb9SGleb Smirnoff next = (struct dn_id *)((char *)o + o->len); 16513b3a8eb9SGleb Smirnoff err = 0; 16523b3a8eb9SGleb Smirnoff switch (o->type) { 16533b3a8eb9SGleb Smirnoff default: 16543b3a8eb9SGleb Smirnoff D("cmd %d not implemented", o->type); 16553b3a8eb9SGleb Smirnoff break; 16563b3a8eb9SGleb Smirnoff 16573b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL 16583b3a8eb9SGleb Smirnoff /* sysctl emulation. 16593b3a8eb9SGleb Smirnoff * if we recognize the command, jump to the correct 16603b3a8eb9SGleb Smirnoff * handler and return 16613b3a8eb9SGleb Smirnoff */ 16623b3a8eb9SGleb Smirnoff case DN_SYSCTL_SET: 16633b3a8eb9SGleb Smirnoff err = kesysctl_emu_set(p, l); 16643b3a8eb9SGleb Smirnoff return err; 16653b3a8eb9SGleb Smirnoff #endif 16663b3a8eb9SGleb Smirnoff 16673b3a8eb9SGleb Smirnoff case DN_CMD_CONFIG: /* simply a header */ 16683b3a8eb9SGleb Smirnoff break; 16693b3a8eb9SGleb Smirnoff 16703b3a8eb9SGleb Smirnoff case DN_CMD_DELETE: 16713b3a8eb9SGleb Smirnoff /* the argument is in the first uintptr_t after o */ 16723b3a8eb9SGleb Smirnoff a = (uintptr_t *)(o+1); 16733b3a8eb9SGleb Smirnoff if (o->len < sizeof(*o) + sizeof(*a)) { 16743b3a8eb9SGleb Smirnoff err = EINVAL; 16753b3a8eb9SGleb Smirnoff break; 16763b3a8eb9SGleb Smirnoff } 16773b3a8eb9SGleb Smirnoff switch (o->subtype) { 16783b3a8eb9SGleb Smirnoff case DN_LINK: 16793b3a8eb9SGleb Smirnoff /* delete base and derived schedulers */ 16803b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 16813b3a8eb9SGleb Smirnoff err = delete_schk(*a); 16823b3a8eb9SGleb Smirnoff err2 = delete_schk(*a + DN_MAX_ID); 16833b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 16843b3a8eb9SGleb Smirnoff if (!err) 16853b3a8eb9SGleb Smirnoff err = err2; 16863b3a8eb9SGleb Smirnoff break; 16873b3a8eb9SGleb Smirnoff 16883b3a8eb9SGleb Smirnoff default: 16893b3a8eb9SGleb Smirnoff D("invalid delete type %d", 16903b3a8eb9SGleb Smirnoff o->subtype); 16913b3a8eb9SGleb Smirnoff err = EINVAL; 16923b3a8eb9SGleb Smirnoff break; 16933b3a8eb9SGleb Smirnoff 16943b3a8eb9SGleb Smirnoff case DN_FS: 16953b3a8eb9SGleb Smirnoff err = (*a <1 || *a >= DN_MAX_ID) ? 16963b3a8eb9SGleb Smirnoff EINVAL : delete_fs(*a, 0) ; 16973b3a8eb9SGleb Smirnoff break; 16983b3a8eb9SGleb Smirnoff } 16993b3a8eb9SGleb Smirnoff break; 17003b3a8eb9SGleb Smirnoff 17013b3a8eb9SGleb Smirnoff case DN_CMD_FLUSH: 17023b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 17033b3a8eb9SGleb Smirnoff dummynet_flush(); 17043b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 17053b3a8eb9SGleb Smirnoff break; 17063b3a8eb9SGleb Smirnoff case DN_TEXT: /* store argument the next block */ 17073b3a8eb9SGleb Smirnoff prev = NULL; 17083b3a8eb9SGleb Smirnoff arg = o; 17093b3a8eb9SGleb Smirnoff break; 17103b3a8eb9SGleb Smirnoff case DN_LINK: 17113b3a8eb9SGleb Smirnoff err = config_link((struct dn_link *)o, arg); 17123b3a8eb9SGleb Smirnoff break; 17133b3a8eb9SGleb Smirnoff case DN_PROFILE: 17143b3a8eb9SGleb Smirnoff err = config_profile((struct dn_profile *)o, arg); 17153b3a8eb9SGleb Smirnoff break; 17163b3a8eb9SGleb Smirnoff case DN_SCH: 17173b3a8eb9SGleb Smirnoff err = config_sched((struct dn_sch *)o, arg); 17183b3a8eb9SGleb Smirnoff break; 17193b3a8eb9SGleb Smirnoff case DN_FS: 17203b3a8eb9SGleb Smirnoff err = (NULL==config_fs((struct dn_fs *)o, arg, 0)); 17213b3a8eb9SGleb Smirnoff break; 17223b3a8eb9SGleb Smirnoff } 17233b3a8eb9SGleb Smirnoff if (prev) 17243b3a8eb9SGleb Smirnoff arg = NULL; 17253b3a8eb9SGleb Smirnoff if (err != 0) 17263b3a8eb9SGleb Smirnoff break; 17273b3a8eb9SGleb Smirnoff } 17283b3a8eb9SGleb Smirnoff return err; 17293b3a8eb9SGleb Smirnoff } 17303b3a8eb9SGleb Smirnoff 17313b3a8eb9SGleb Smirnoff static int 17323b3a8eb9SGleb Smirnoff compute_space(struct dn_id *cmd, struct copy_args *a) 17333b3a8eb9SGleb Smirnoff { 17343b3a8eb9SGleb Smirnoff int x = 0, need = 0; 17353b3a8eb9SGleb Smirnoff int profile_size = sizeof(struct dn_profile) - 17363b3a8eb9SGleb Smirnoff ED_MAX_SAMPLES_NO*sizeof(int); 17373b3a8eb9SGleb Smirnoff 17383b3a8eb9SGleb Smirnoff /* NOTE about compute space: 17393b3a8eb9SGleb Smirnoff * NP = dn_cfg.schk_count 17403b3a8eb9SGleb Smirnoff * NSI = dn_cfg.si_count 17413b3a8eb9SGleb Smirnoff * NF = dn_cfg.fsk_count 17423b3a8eb9SGleb Smirnoff * NQ = dn_cfg.queue_count 17433b3a8eb9SGleb Smirnoff * - ipfw pipe show 17443b3a8eb9SGleb Smirnoff * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler 17453b3a8eb9SGleb Smirnoff * link, scheduler template, flowset 17463b3a8eb9SGleb Smirnoff * integrated in scheduler and header 17473b3a8eb9SGleb Smirnoff * for flowset list 17483b3a8eb9SGleb Smirnoff * (NSI)*(dn_flow) all scheduler instance (includes 17493b3a8eb9SGleb Smirnoff * the queue instance) 17503b3a8eb9SGleb Smirnoff * - ipfw sched show 17513b3a8eb9SGleb Smirnoff * (NP/2)*(dn_link + dn_sch + dn_id + dn_fs) only half scheduler 17523b3a8eb9SGleb Smirnoff * link, scheduler template, flowset 17533b3a8eb9SGleb Smirnoff * integrated in scheduler and header 17543b3a8eb9SGleb Smirnoff * for flowset list 17553b3a8eb9SGleb Smirnoff * (NSI * dn_flow) all scheduler instances 17563b3a8eb9SGleb Smirnoff * (NF * sizeof(uint_32)) space for flowset list linked to scheduler 17573b3a8eb9SGleb Smirnoff * (NQ * dn_queue) all queue [XXXfor now not listed] 17583b3a8eb9SGleb Smirnoff * - ipfw queue show 17593b3a8eb9SGleb Smirnoff * (NF * dn_fs) all flowset 17603b3a8eb9SGleb Smirnoff * (NQ * dn_queue) all queues 17613b3a8eb9SGleb Smirnoff */ 17623b3a8eb9SGleb Smirnoff switch (cmd->subtype) { 17633b3a8eb9SGleb Smirnoff default: 17643b3a8eb9SGleb Smirnoff return -1; 17653b3a8eb9SGleb Smirnoff /* XXX where do LINK and SCH differ ? */ 17663b3a8eb9SGleb Smirnoff /* 'ipfw sched show' could list all queues associated to 17673b3a8eb9SGleb Smirnoff * a scheduler. This feature for now is disabled 17683b3a8eb9SGleb Smirnoff */ 17693b3a8eb9SGleb Smirnoff case DN_LINK: /* pipe show */ 17703b3a8eb9SGleb Smirnoff x = DN_C_LINK | DN_C_SCH | DN_C_FLOW; 17713b3a8eb9SGleb Smirnoff need += dn_cfg.schk_count * 17723b3a8eb9SGleb Smirnoff (sizeof(struct dn_fs) + profile_size) / 2; 17733b3a8eb9SGleb Smirnoff need += dn_cfg.fsk_count * sizeof(uint32_t); 17743b3a8eb9SGleb Smirnoff break; 17753b3a8eb9SGleb Smirnoff case DN_SCH: /* sched show */ 17763b3a8eb9SGleb Smirnoff need += dn_cfg.schk_count * 17773b3a8eb9SGleb Smirnoff (sizeof(struct dn_fs) + profile_size) / 2; 17783b3a8eb9SGleb Smirnoff need += dn_cfg.fsk_count * sizeof(uint32_t); 17793b3a8eb9SGleb Smirnoff x = DN_C_SCH | DN_C_LINK | DN_C_FLOW; 17803b3a8eb9SGleb Smirnoff break; 17813b3a8eb9SGleb Smirnoff case DN_FS: /* queue show */ 17823b3a8eb9SGleb Smirnoff x = DN_C_FS | DN_C_QUEUE; 17833b3a8eb9SGleb Smirnoff break; 17843b3a8eb9SGleb Smirnoff case DN_GET_COMPAT: /* compatibility mode */ 17853b3a8eb9SGleb Smirnoff need = dn_compat_calc_size(); 17863b3a8eb9SGleb Smirnoff break; 17873b3a8eb9SGleb Smirnoff } 17883b3a8eb9SGleb Smirnoff a->flags = x; 17893b3a8eb9SGleb Smirnoff if (x & DN_C_SCH) { 17903b3a8eb9SGleb Smirnoff need += dn_cfg.schk_count * sizeof(struct dn_sch) / 2; 17913b3a8eb9SGleb Smirnoff /* NOT also, each fs might be attached to a sched */ 17923b3a8eb9SGleb Smirnoff need += dn_cfg.schk_count * sizeof(struct dn_id) / 2; 17933b3a8eb9SGleb Smirnoff } 17943b3a8eb9SGleb Smirnoff if (x & DN_C_FS) 17953b3a8eb9SGleb Smirnoff need += dn_cfg.fsk_count * sizeof(struct dn_fs); 17963b3a8eb9SGleb Smirnoff if (x & DN_C_LINK) { 17973b3a8eb9SGleb Smirnoff need += dn_cfg.schk_count * sizeof(struct dn_link) / 2; 17983b3a8eb9SGleb Smirnoff } 17993b3a8eb9SGleb Smirnoff /* 18003b3a8eb9SGleb Smirnoff * When exporting a queue to userland, only pass up the 18013b3a8eb9SGleb Smirnoff * struct dn_flow, which is the only visible part. 18023b3a8eb9SGleb Smirnoff */ 18033b3a8eb9SGleb Smirnoff 18043b3a8eb9SGleb Smirnoff if (x & DN_C_QUEUE) 18053b3a8eb9SGleb Smirnoff need += dn_cfg.queue_count * sizeof(struct dn_flow); 18063b3a8eb9SGleb Smirnoff if (x & DN_C_FLOW) 18073b3a8eb9SGleb Smirnoff need += dn_cfg.si_count * (sizeof(struct dn_flow)); 18083b3a8eb9SGleb Smirnoff return need; 18093b3a8eb9SGleb Smirnoff } 18103b3a8eb9SGleb Smirnoff 18113b3a8eb9SGleb Smirnoff /* 18123b3a8eb9SGleb Smirnoff * If compat != NULL dummynet_get is called in compatibility mode. 18133b3a8eb9SGleb Smirnoff * *compat will be the pointer to the buffer to pass to ipfw 18143b3a8eb9SGleb Smirnoff */ 18153b3a8eb9SGleb Smirnoff int 18163b3a8eb9SGleb Smirnoff dummynet_get(struct sockopt *sopt, void **compat) 18173b3a8eb9SGleb Smirnoff { 18183b3a8eb9SGleb Smirnoff int have, i, need, error; 18193b3a8eb9SGleb Smirnoff char *start = NULL, *buf; 18203b3a8eb9SGleb Smirnoff size_t sopt_valsize; 18213b3a8eb9SGleb Smirnoff struct dn_id *cmd; 18223b3a8eb9SGleb Smirnoff struct copy_args a; 18233b3a8eb9SGleb Smirnoff struct copy_range r; 18243b3a8eb9SGleb Smirnoff int l = sizeof(struct dn_id); 18253b3a8eb9SGleb Smirnoff 18263b3a8eb9SGleb Smirnoff bzero(&a, sizeof(a)); 18273b3a8eb9SGleb Smirnoff bzero(&r, sizeof(r)); 18283b3a8eb9SGleb Smirnoff 18293b3a8eb9SGleb Smirnoff /* save and restore original sopt_valsize around copyin */ 18303b3a8eb9SGleb Smirnoff sopt_valsize = sopt->sopt_valsize; 18313b3a8eb9SGleb Smirnoff 18323b3a8eb9SGleb Smirnoff cmd = &r.o; 18333b3a8eb9SGleb Smirnoff 18343b3a8eb9SGleb Smirnoff if (!compat) { 18353b3a8eb9SGleb Smirnoff /* copy at least an oid, and possibly a full object */ 18363b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, cmd, sizeof(r), sizeof(*cmd)); 18373b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize; 18383b3a8eb9SGleb Smirnoff if (error) 18393b3a8eb9SGleb Smirnoff goto done; 18403b3a8eb9SGleb Smirnoff l = cmd->len; 18413b3a8eb9SGleb Smirnoff #ifdef EMULATE_SYSCTL 18423b3a8eb9SGleb Smirnoff /* sysctl emulation. */ 18433b3a8eb9SGleb Smirnoff if (cmd->type == DN_SYSCTL_GET) 18443b3a8eb9SGleb Smirnoff return kesysctl_emu_get(sopt); 18453b3a8eb9SGleb Smirnoff #endif 18463b3a8eb9SGleb Smirnoff if (l > sizeof(r)) { 18473b3a8eb9SGleb Smirnoff /* request larger than default, allocate buffer */ 18483b3a8eb9SGleb Smirnoff cmd = malloc(l, M_DUMMYNET, M_WAITOK); 18493b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, cmd, l, l); 18503b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize; 18513b3a8eb9SGleb Smirnoff if (error) 18523b3a8eb9SGleb Smirnoff goto done; 18533b3a8eb9SGleb Smirnoff } 18543b3a8eb9SGleb Smirnoff } else { /* compatibility */ 18553b3a8eb9SGleb Smirnoff error = 0; 18563b3a8eb9SGleb Smirnoff cmd->type = DN_CMD_GET; 18573b3a8eb9SGleb Smirnoff cmd->len = sizeof(struct dn_id); 18583b3a8eb9SGleb Smirnoff cmd->subtype = DN_GET_COMPAT; 18593b3a8eb9SGleb Smirnoff // cmd->id = sopt_valsize; 18603b3a8eb9SGleb Smirnoff D("compatibility mode"); 18613b3a8eb9SGleb Smirnoff } 18623b3a8eb9SGleb Smirnoff a.extra = (struct copy_range *)cmd; 18633b3a8eb9SGleb Smirnoff if (cmd->len == sizeof(*cmd)) { /* no range, create a default */ 18643b3a8eb9SGleb Smirnoff uint32_t *rp = (uint32_t *)(cmd + 1); 18653b3a8eb9SGleb Smirnoff cmd->len += 2* sizeof(uint32_t); 18663b3a8eb9SGleb Smirnoff rp[0] = 1; 18673b3a8eb9SGleb Smirnoff rp[1] = DN_MAX_ID - 1; 18683b3a8eb9SGleb Smirnoff if (cmd->subtype == DN_LINK) { 18693b3a8eb9SGleb Smirnoff rp[0] += DN_MAX_ID; 18703b3a8eb9SGleb Smirnoff rp[1] += DN_MAX_ID; 18713b3a8eb9SGleb Smirnoff } 18723b3a8eb9SGleb Smirnoff } 18733b3a8eb9SGleb Smirnoff /* Count space (under lock) and allocate (outside lock). 18743b3a8eb9SGleb Smirnoff * Exit with lock held if we manage to get enough buffer. 18753b3a8eb9SGleb Smirnoff * Try a few times then give up. 18763b3a8eb9SGleb Smirnoff */ 18773b3a8eb9SGleb Smirnoff for (have = 0, i = 0; i < 10; i++) { 18783b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 18793b3a8eb9SGleb Smirnoff need = compute_space(cmd, &a); 18803b3a8eb9SGleb Smirnoff 18813b3a8eb9SGleb Smirnoff /* if there is a range, ignore value from compute_space() */ 18823b3a8eb9SGleb Smirnoff if (l > sizeof(*cmd)) 18833b3a8eb9SGleb Smirnoff need = sopt_valsize - sizeof(*cmd); 18843b3a8eb9SGleb Smirnoff 18853b3a8eb9SGleb Smirnoff if (need < 0) { 18863b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 18873b3a8eb9SGleb Smirnoff error = EINVAL; 18883b3a8eb9SGleb Smirnoff goto done; 18893b3a8eb9SGleb Smirnoff } 18903b3a8eb9SGleb Smirnoff need += sizeof(*cmd); 18913b3a8eb9SGleb Smirnoff cmd->id = need; 18923b3a8eb9SGleb Smirnoff if (have >= need) 18933b3a8eb9SGleb Smirnoff break; 18943b3a8eb9SGleb Smirnoff 18953b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 18963b3a8eb9SGleb Smirnoff if (start) 18973b3a8eb9SGleb Smirnoff free(start, M_DUMMYNET); 18983b3a8eb9SGleb Smirnoff start = NULL; 18993b3a8eb9SGleb Smirnoff if (need > sopt_valsize) 19003b3a8eb9SGleb Smirnoff break; 19013b3a8eb9SGleb Smirnoff 19023b3a8eb9SGleb Smirnoff have = need; 19033b3a8eb9SGleb Smirnoff start = malloc(have, M_DUMMYNET, M_WAITOK | M_ZERO); 19043b3a8eb9SGleb Smirnoff } 19053b3a8eb9SGleb Smirnoff 19063b3a8eb9SGleb Smirnoff if (start == NULL) { 19073b3a8eb9SGleb Smirnoff if (compat) { 19083b3a8eb9SGleb Smirnoff *compat = NULL; 19093b3a8eb9SGleb Smirnoff error = 1; // XXX 19103b3a8eb9SGleb Smirnoff } else { 19113b3a8eb9SGleb Smirnoff error = sooptcopyout(sopt, cmd, sizeof(*cmd)); 19123b3a8eb9SGleb Smirnoff } 19133b3a8eb9SGleb Smirnoff goto done; 19143b3a8eb9SGleb Smirnoff } 19153b3a8eb9SGleb Smirnoff ND("have %d:%d sched %d, %d:%d links %d, %d:%d flowsets %d, " 19163b3a8eb9SGleb Smirnoff "%d:%d si %d, %d:%d queues %d", 19173b3a8eb9SGleb Smirnoff dn_cfg.schk_count, sizeof(struct dn_sch), DN_SCH, 19183b3a8eb9SGleb Smirnoff dn_cfg.schk_count, sizeof(struct dn_link), DN_LINK, 19193b3a8eb9SGleb Smirnoff dn_cfg.fsk_count, sizeof(struct dn_fs), DN_FS, 19203b3a8eb9SGleb Smirnoff dn_cfg.si_count, sizeof(struct dn_flow), DN_SCH_I, 19213b3a8eb9SGleb Smirnoff dn_cfg.queue_count, sizeof(struct dn_queue), DN_QUEUE); 19223b3a8eb9SGleb Smirnoff sopt->sopt_valsize = sopt_valsize; 19233b3a8eb9SGleb Smirnoff a.type = cmd->subtype; 19243b3a8eb9SGleb Smirnoff 19253b3a8eb9SGleb Smirnoff if (compat == NULL) { 19263b3a8eb9SGleb Smirnoff bcopy(cmd, start, sizeof(*cmd)); 19273b3a8eb9SGleb Smirnoff ((struct dn_id*)(start))->len = sizeof(struct dn_id); 19283b3a8eb9SGleb Smirnoff buf = start + sizeof(*cmd); 19293b3a8eb9SGleb Smirnoff } else 19303b3a8eb9SGleb Smirnoff buf = start; 19313b3a8eb9SGleb Smirnoff a.start = &buf; 19323b3a8eb9SGleb Smirnoff a.end = start + have; 19333b3a8eb9SGleb Smirnoff /* start copying other objects */ 19343b3a8eb9SGleb Smirnoff if (compat) { 19353b3a8eb9SGleb Smirnoff a.type = DN_COMPAT_PIPE; 19363b3a8eb9SGleb Smirnoff dn_ht_scan(dn_cfg.schedhash, copy_data_helper_compat, &a); 19373b3a8eb9SGleb Smirnoff a.type = DN_COMPAT_QUEUE; 19383b3a8eb9SGleb Smirnoff dn_ht_scan(dn_cfg.fshash, copy_data_helper_compat, &a); 19393b3a8eb9SGleb Smirnoff } else if (a.type == DN_FS) { 19403b3a8eb9SGleb Smirnoff dn_ht_scan(dn_cfg.fshash, copy_data_helper, &a); 19413b3a8eb9SGleb Smirnoff } else { 19423b3a8eb9SGleb Smirnoff dn_ht_scan(dn_cfg.schedhash, copy_data_helper, &a); 19433b3a8eb9SGleb Smirnoff } 19443b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 19453b3a8eb9SGleb Smirnoff 19463b3a8eb9SGleb Smirnoff if (compat) { 19473b3a8eb9SGleb Smirnoff *compat = start; 19483b3a8eb9SGleb Smirnoff sopt->sopt_valsize = buf - start; 19493b3a8eb9SGleb Smirnoff /* free() is done by ip_dummynet_compat() */ 19503b3a8eb9SGleb Smirnoff start = NULL; //XXX hack 19513b3a8eb9SGleb Smirnoff } else { 19523b3a8eb9SGleb Smirnoff error = sooptcopyout(sopt, start, buf - start); 19533b3a8eb9SGleb Smirnoff } 19543b3a8eb9SGleb Smirnoff done: 19553b3a8eb9SGleb Smirnoff if (cmd && cmd != &r.o) 19563b3a8eb9SGleb Smirnoff free(cmd, M_DUMMYNET); 19573b3a8eb9SGleb Smirnoff if (start) 19583b3a8eb9SGleb Smirnoff free(start, M_DUMMYNET); 19593b3a8eb9SGleb Smirnoff return error; 19603b3a8eb9SGleb Smirnoff } 19613b3a8eb9SGleb Smirnoff 19623b3a8eb9SGleb Smirnoff /* Callback called on scheduler instance to delete it if idle */ 19633b3a8eb9SGleb Smirnoff static int 19643b3a8eb9SGleb Smirnoff drain_scheduler_cb(void *_si, void *arg) 19653b3a8eb9SGleb Smirnoff { 19663b3a8eb9SGleb Smirnoff struct dn_sch_inst *si = _si; 19673b3a8eb9SGleb Smirnoff 19683b3a8eb9SGleb Smirnoff if ((si->kflags & DN_ACTIVE) || si->dline.mq.head != NULL) 19693b3a8eb9SGleb Smirnoff return 0; 19703b3a8eb9SGleb Smirnoff 19713b3a8eb9SGleb Smirnoff if (si->sched->fp->flags & DN_MULTIQUEUE) { 19723b3a8eb9SGleb Smirnoff if (si->q_count == 0) 19733b3a8eb9SGleb Smirnoff return si_destroy(si, NULL); 19743b3a8eb9SGleb Smirnoff else 19753b3a8eb9SGleb Smirnoff return 0; 19763b3a8eb9SGleb Smirnoff } else { /* !DN_MULTIQUEUE */ 19773b3a8eb9SGleb Smirnoff if ((si+1)->ni.length == 0) 19783b3a8eb9SGleb Smirnoff return si_destroy(si, NULL); 19793b3a8eb9SGleb Smirnoff else 19803b3a8eb9SGleb Smirnoff return 0; 19813b3a8eb9SGleb Smirnoff } 19823b3a8eb9SGleb Smirnoff return 0; /* unreachable */ 19833b3a8eb9SGleb Smirnoff } 19843b3a8eb9SGleb Smirnoff 19853b3a8eb9SGleb Smirnoff /* Callback called on scheduler to check if it has instances */ 19863b3a8eb9SGleb Smirnoff static int 19873b3a8eb9SGleb Smirnoff drain_scheduler_sch_cb(void *_s, void *arg) 19883b3a8eb9SGleb Smirnoff { 19893b3a8eb9SGleb Smirnoff struct dn_schk *s = _s; 19903b3a8eb9SGleb Smirnoff 19913b3a8eb9SGleb Smirnoff if (s->sch.flags & DN_HAVE_MASK) { 19923b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(s->siht, &s->drain_bucket, 19933b3a8eb9SGleb Smirnoff drain_scheduler_cb, NULL); 19943b3a8eb9SGleb Smirnoff s->drain_bucket++; 19953b3a8eb9SGleb Smirnoff } else { 19963b3a8eb9SGleb Smirnoff if (s->siht) { 19973b3a8eb9SGleb Smirnoff if (drain_scheduler_cb(s->siht, NULL) == DNHT_SCAN_DEL) 19983b3a8eb9SGleb Smirnoff s->siht = NULL; 19993b3a8eb9SGleb Smirnoff } 20003b3a8eb9SGleb Smirnoff } 20013b3a8eb9SGleb Smirnoff return 0; 20023b3a8eb9SGleb Smirnoff } 20033b3a8eb9SGleb Smirnoff 20043b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of scheduler */ 20053b3a8eb9SGleb Smirnoff void 20063b3a8eb9SGleb Smirnoff dn_drain_scheduler(void) 20073b3a8eb9SGleb Smirnoff { 20083b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(dn_cfg.schedhash, &dn_cfg.drain_sch, 20093b3a8eb9SGleb Smirnoff drain_scheduler_sch_cb, NULL); 20103b3a8eb9SGleb Smirnoff dn_cfg.drain_sch++; 20113b3a8eb9SGleb Smirnoff } 20123b3a8eb9SGleb Smirnoff 20133b3a8eb9SGleb Smirnoff /* Callback called on queue to delete if it is idle */ 20143b3a8eb9SGleb Smirnoff static int 20153b3a8eb9SGleb Smirnoff drain_queue_cb(void *_q, void *arg) 20163b3a8eb9SGleb Smirnoff { 20173b3a8eb9SGleb Smirnoff struct dn_queue *q = _q; 20183b3a8eb9SGleb Smirnoff 20193b3a8eb9SGleb Smirnoff if (q->ni.length == 0) { 20203b3a8eb9SGleb Smirnoff dn_delete_queue(q, DN_DESTROY); 20213b3a8eb9SGleb Smirnoff return DNHT_SCAN_DEL; /* queue is deleted */ 20223b3a8eb9SGleb Smirnoff } 20233b3a8eb9SGleb Smirnoff 20243b3a8eb9SGleb Smirnoff return 0; /* queue isn't deleted */ 20253b3a8eb9SGleb Smirnoff } 20263b3a8eb9SGleb Smirnoff 20273b3a8eb9SGleb Smirnoff /* Callback called on flowset used to check if it has queues */ 20283b3a8eb9SGleb Smirnoff static int 20293b3a8eb9SGleb Smirnoff drain_queue_fs_cb(void *_fs, void *arg) 20303b3a8eb9SGleb Smirnoff { 20313b3a8eb9SGleb Smirnoff struct dn_fsk *fs = _fs; 20323b3a8eb9SGleb Smirnoff 20333b3a8eb9SGleb Smirnoff if (fs->fs.flags & DN_QHT_HASH) { 20343b3a8eb9SGleb Smirnoff /* Flowset has a hash table for queues */ 20353b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(fs->qht, &fs->drain_bucket, 20363b3a8eb9SGleb Smirnoff drain_queue_cb, NULL); 20373b3a8eb9SGleb Smirnoff fs->drain_bucket++; 20383b3a8eb9SGleb Smirnoff } else { 20393b3a8eb9SGleb Smirnoff /* No hash table for this flowset, null the pointer 20403b3a8eb9SGleb Smirnoff * if the queue is deleted 20413b3a8eb9SGleb Smirnoff */ 20423b3a8eb9SGleb Smirnoff if (fs->qht) { 20433b3a8eb9SGleb Smirnoff if (drain_queue_cb(fs->qht, NULL) == DNHT_SCAN_DEL) 20443b3a8eb9SGleb Smirnoff fs->qht = NULL; 20453b3a8eb9SGleb Smirnoff } 20463b3a8eb9SGleb Smirnoff } 20473b3a8eb9SGleb Smirnoff return 0; 20483b3a8eb9SGleb Smirnoff } 20493b3a8eb9SGleb Smirnoff 20503b3a8eb9SGleb Smirnoff /* Called every tick, try to delete a 'bucket' of queue */ 20513b3a8eb9SGleb Smirnoff void 20523b3a8eb9SGleb Smirnoff dn_drain_queue(void) 20533b3a8eb9SGleb Smirnoff { 20543b3a8eb9SGleb Smirnoff /* scan a bucket of flowset */ 20553b3a8eb9SGleb Smirnoff dn_ht_scan_bucket(dn_cfg.fshash, &dn_cfg.drain_fs, 20563b3a8eb9SGleb Smirnoff drain_queue_fs_cb, NULL); 20573b3a8eb9SGleb Smirnoff dn_cfg.drain_fs++; 20583b3a8eb9SGleb Smirnoff } 20593b3a8eb9SGleb Smirnoff 20603b3a8eb9SGleb Smirnoff /* 20613b3a8eb9SGleb Smirnoff * Handler for the various dummynet socket options 20623b3a8eb9SGleb Smirnoff */ 20633b3a8eb9SGleb Smirnoff static int 20643b3a8eb9SGleb Smirnoff ip_dn_ctl(struct sockopt *sopt) 20653b3a8eb9SGleb Smirnoff { 20663b3a8eb9SGleb Smirnoff void *p = NULL; 20673b3a8eb9SGleb Smirnoff int error, l; 20683b3a8eb9SGleb Smirnoff 20693b3a8eb9SGleb Smirnoff error = priv_check(sopt->sopt_td, PRIV_NETINET_DUMMYNET); 20703b3a8eb9SGleb Smirnoff if (error) 20713b3a8eb9SGleb Smirnoff return (error); 20723b3a8eb9SGleb Smirnoff 20733b3a8eb9SGleb Smirnoff /* Disallow sets in really-really secure mode. */ 20743b3a8eb9SGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) { 20753b3a8eb9SGleb Smirnoff error = securelevel_ge(sopt->sopt_td->td_ucred, 3); 20763b3a8eb9SGleb Smirnoff if (error) 20773b3a8eb9SGleb Smirnoff return (error); 20783b3a8eb9SGleb Smirnoff } 20793b3a8eb9SGleb Smirnoff 20803b3a8eb9SGleb Smirnoff switch (sopt->sopt_name) { 20813b3a8eb9SGleb Smirnoff default : 20823b3a8eb9SGleb Smirnoff D("dummynet: unknown option %d", sopt->sopt_name); 20833b3a8eb9SGleb Smirnoff error = EINVAL; 20843b3a8eb9SGleb Smirnoff break; 20853b3a8eb9SGleb Smirnoff 20863b3a8eb9SGleb Smirnoff case IP_DUMMYNET_FLUSH: 20873b3a8eb9SGleb Smirnoff case IP_DUMMYNET_CONFIGURE: 20883b3a8eb9SGleb Smirnoff case IP_DUMMYNET_DEL: /* remove a pipe or queue */ 20893b3a8eb9SGleb Smirnoff case IP_DUMMYNET_GET: 20903b3a8eb9SGleb Smirnoff D("dummynet: compat option %d", sopt->sopt_name); 20913b3a8eb9SGleb Smirnoff error = ip_dummynet_compat(sopt); 20923b3a8eb9SGleb Smirnoff break; 20933b3a8eb9SGleb Smirnoff 20943b3a8eb9SGleb Smirnoff case IP_DUMMYNET3 : 20953b3a8eb9SGleb Smirnoff if (sopt->sopt_dir == SOPT_GET) { 20963b3a8eb9SGleb Smirnoff error = dummynet_get(sopt, NULL); 20973b3a8eb9SGleb Smirnoff break; 20983b3a8eb9SGleb Smirnoff } 20993b3a8eb9SGleb Smirnoff l = sopt->sopt_valsize; 21003b3a8eb9SGleb Smirnoff if (l < sizeof(struct dn_id) || l > 12000) { 21013b3a8eb9SGleb Smirnoff D("argument len %d invalid", l); 21023b3a8eb9SGleb Smirnoff break; 21033b3a8eb9SGleb Smirnoff } 21043b3a8eb9SGleb Smirnoff p = malloc(l, M_TEMP, M_WAITOK); // XXX can it fail ? 21053b3a8eb9SGleb Smirnoff error = sooptcopyin(sopt, p, l, l); 21063b3a8eb9SGleb Smirnoff if (error) 21073b3a8eb9SGleb Smirnoff break ; 21083b3a8eb9SGleb Smirnoff error = do_config(p, l); 21093b3a8eb9SGleb Smirnoff break; 21103b3a8eb9SGleb Smirnoff } 21113b3a8eb9SGleb Smirnoff 21123b3a8eb9SGleb Smirnoff if (p != NULL) 21133b3a8eb9SGleb Smirnoff free(p, M_TEMP); 21143b3a8eb9SGleb Smirnoff 21153b3a8eb9SGleb Smirnoff return error ; 21163b3a8eb9SGleb Smirnoff } 21173b3a8eb9SGleb Smirnoff 21183b3a8eb9SGleb Smirnoff 21193b3a8eb9SGleb Smirnoff static void 21203b3a8eb9SGleb Smirnoff ip_dn_init(void) 21213b3a8eb9SGleb Smirnoff { 21223b3a8eb9SGleb Smirnoff if (dn_cfg.init_done) 21233b3a8eb9SGleb Smirnoff return; 21243b3a8eb9SGleb Smirnoff printf("DUMMYNET %p with IPv6 initialized (100409)\n", curvnet); 21253b3a8eb9SGleb Smirnoff dn_cfg.init_done = 1; 21263b3a8eb9SGleb Smirnoff /* Set defaults here. MSVC does not accept initializers, 21273b3a8eb9SGleb Smirnoff * and this is also useful for vimages 21283b3a8eb9SGleb Smirnoff */ 21293b3a8eb9SGleb Smirnoff /* queue limits */ 21303b3a8eb9SGleb Smirnoff dn_cfg.slot_limit = 100; /* Foot shooting limit for queues. */ 21313b3a8eb9SGleb Smirnoff dn_cfg.byte_limit = 1024 * 1024; 21323b3a8eb9SGleb Smirnoff dn_cfg.expire = 1; 21333b3a8eb9SGleb Smirnoff 21343b3a8eb9SGleb Smirnoff /* RED parameters */ 21353b3a8eb9SGleb Smirnoff dn_cfg.red_lookup_depth = 256; /* default lookup table depth */ 21363b3a8eb9SGleb Smirnoff dn_cfg.red_avg_pkt_size = 512; /* default medium packet size */ 21373b3a8eb9SGleb Smirnoff dn_cfg.red_max_pkt_size = 1500; /* default max packet size */ 21383b3a8eb9SGleb Smirnoff 21393b3a8eb9SGleb Smirnoff /* hash tables */ 21403b3a8eb9SGleb Smirnoff dn_cfg.max_hash_size = 65536; /* max in the hash tables */ 21413b3a8eb9SGleb Smirnoff dn_cfg.hash_size = 64; /* default hash size */ 21423b3a8eb9SGleb Smirnoff 21433b3a8eb9SGleb Smirnoff /* create hash tables for schedulers and flowsets. 21443b3a8eb9SGleb Smirnoff * In both we search by key and by pointer. 21453b3a8eb9SGleb Smirnoff */ 21463b3a8eb9SGleb Smirnoff dn_cfg.schedhash = dn_ht_init(NULL, dn_cfg.hash_size, 21473b3a8eb9SGleb Smirnoff offsetof(struct dn_schk, schk_next), 21483b3a8eb9SGleb Smirnoff schk_hash, schk_match, schk_new); 21493b3a8eb9SGleb Smirnoff dn_cfg.fshash = dn_ht_init(NULL, dn_cfg.hash_size, 21503b3a8eb9SGleb Smirnoff offsetof(struct dn_fsk, fsk_next), 21513b3a8eb9SGleb Smirnoff fsk_hash, fsk_match, fsk_new); 21523b3a8eb9SGleb Smirnoff 21533b3a8eb9SGleb Smirnoff /* bucket index to drain object */ 21543b3a8eb9SGleb Smirnoff dn_cfg.drain_fs = 0; 21553b3a8eb9SGleb Smirnoff dn_cfg.drain_sch = 0; 21563b3a8eb9SGleb Smirnoff 21573b3a8eb9SGleb Smirnoff heap_init(&dn_cfg.evheap, 16, offsetof(struct dn_id, id)); 21583b3a8eb9SGleb Smirnoff SLIST_INIT(&dn_cfg.fsu); 21593b3a8eb9SGleb Smirnoff SLIST_INIT(&dn_cfg.schedlist); 21603b3a8eb9SGleb Smirnoff 21613b3a8eb9SGleb Smirnoff DN_LOCK_INIT(); 21623b3a8eb9SGleb Smirnoff 21633b3a8eb9SGleb Smirnoff TASK_INIT(&dn_task, 0, dummynet_task, curvnet); 2164*5f4fc3dbSAlexander Motin dn_tq = taskqueue_create_fast("dummynet", M_WAITOK, 21653b3a8eb9SGleb Smirnoff taskqueue_thread_enqueue, &dn_tq); 21663b3a8eb9SGleb Smirnoff taskqueue_start_threads(&dn_tq, 1, PI_NET, "dummynet"); 21673b3a8eb9SGleb Smirnoff 21683b3a8eb9SGleb Smirnoff callout_init(&dn_timeout, CALLOUT_MPSAFE); 2169*5f4fc3dbSAlexander Motin dn_reschedule(); 21703b3a8eb9SGleb Smirnoff 21713b3a8eb9SGleb Smirnoff /* Initialize curr_time adjustment mechanics. */ 21723b3a8eb9SGleb Smirnoff getmicrouptime(&dn_cfg.prev_t); 21733b3a8eb9SGleb Smirnoff } 21743b3a8eb9SGleb Smirnoff 21753b3a8eb9SGleb Smirnoff static void 21763b3a8eb9SGleb Smirnoff ip_dn_destroy(int last) 21773b3a8eb9SGleb Smirnoff { 21783b3a8eb9SGleb Smirnoff callout_drain(&dn_timeout); 21793b3a8eb9SGleb Smirnoff 21803b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 21813b3a8eb9SGleb Smirnoff if (last) { 21823b3a8eb9SGleb Smirnoff ND("removing last instance\n"); 21833b3a8eb9SGleb Smirnoff ip_dn_ctl_ptr = NULL; 21843b3a8eb9SGleb Smirnoff ip_dn_io_ptr = NULL; 21853b3a8eb9SGleb Smirnoff } 21863b3a8eb9SGleb Smirnoff 21873b3a8eb9SGleb Smirnoff dummynet_flush(); 21883b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 21893b3a8eb9SGleb Smirnoff taskqueue_drain(dn_tq, &dn_task); 21903b3a8eb9SGleb Smirnoff taskqueue_free(dn_tq); 21913b3a8eb9SGleb Smirnoff 21923b3a8eb9SGleb Smirnoff dn_ht_free(dn_cfg.schedhash, 0); 21933b3a8eb9SGleb Smirnoff dn_ht_free(dn_cfg.fshash, 0); 21943b3a8eb9SGleb Smirnoff heap_free(&dn_cfg.evheap); 21953b3a8eb9SGleb Smirnoff 21963b3a8eb9SGleb Smirnoff DN_LOCK_DESTROY(); 21973b3a8eb9SGleb Smirnoff } 21983b3a8eb9SGleb Smirnoff 21993b3a8eb9SGleb Smirnoff static int 22003b3a8eb9SGleb Smirnoff dummynet_modevent(module_t mod, int type, void *data) 22013b3a8eb9SGleb Smirnoff { 22023b3a8eb9SGleb Smirnoff 22033b3a8eb9SGleb Smirnoff if (type == MOD_LOAD) { 22043b3a8eb9SGleb Smirnoff if (ip_dn_io_ptr) { 22053b3a8eb9SGleb Smirnoff printf("DUMMYNET already loaded\n"); 22063b3a8eb9SGleb Smirnoff return EEXIST ; 22073b3a8eb9SGleb Smirnoff } 22083b3a8eb9SGleb Smirnoff ip_dn_init(); 22093b3a8eb9SGleb Smirnoff ip_dn_ctl_ptr = ip_dn_ctl; 22103b3a8eb9SGleb Smirnoff ip_dn_io_ptr = dummynet_io; 22113b3a8eb9SGleb Smirnoff return 0; 22123b3a8eb9SGleb Smirnoff } else if (type == MOD_UNLOAD) { 22133b3a8eb9SGleb Smirnoff ip_dn_destroy(1 /* last */); 22143b3a8eb9SGleb Smirnoff return 0; 22153b3a8eb9SGleb Smirnoff } else 22163b3a8eb9SGleb Smirnoff return EOPNOTSUPP; 22173b3a8eb9SGleb Smirnoff } 22183b3a8eb9SGleb Smirnoff 22193b3a8eb9SGleb Smirnoff /* modevent helpers for the modules */ 22203b3a8eb9SGleb Smirnoff static int 22213b3a8eb9SGleb Smirnoff load_dn_sched(struct dn_alg *d) 22223b3a8eb9SGleb Smirnoff { 22233b3a8eb9SGleb Smirnoff struct dn_alg *s; 22243b3a8eb9SGleb Smirnoff 22253b3a8eb9SGleb Smirnoff if (d == NULL) 22263b3a8eb9SGleb Smirnoff return 1; /* error */ 22273b3a8eb9SGleb Smirnoff ip_dn_init(); /* just in case, we need the lock */ 22283b3a8eb9SGleb Smirnoff 22293b3a8eb9SGleb Smirnoff /* Check that mandatory funcs exists */ 22303b3a8eb9SGleb Smirnoff if (d->enqueue == NULL || d->dequeue == NULL) { 22313b3a8eb9SGleb Smirnoff D("missing enqueue or dequeue for %s", d->name); 22323b3a8eb9SGleb Smirnoff return 1; 22333b3a8eb9SGleb Smirnoff } 22343b3a8eb9SGleb Smirnoff 22353b3a8eb9SGleb Smirnoff /* Search if scheduler already exists */ 22363b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 22373b3a8eb9SGleb Smirnoff SLIST_FOREACH(s, &dn_cfg.schedlist, next) { 22383b3a8eb9SGleb Smirnoff if (strcmp(s->name, d->name) == 0) { 22393b3a8eb9SGleb Smirnoff D("%s already loaded", d->name); 22403b3a8eb9SGleb Smirnoff break; /* scheduler already exists */ 22413b3a8eb9SGleb Smirnoff } 22423b3a8eb9SGleb Smirnoff } 22433b3a8eb9SGleb Smirnoff if (s == NULL) 22443b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&dn_cfg.schedlist, d, next); 22453b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 22463b3a8eb9SGleb Smirnoff D("dn_sched %s %sloaded", d->name, s ? "not ":""); 22473b3a8eb9SGleb Smirnoff return s ? 1 : 0; 22483b3a8eb9SGleb Smirnoff } 22493b3a8eb9SGleb Smirnoff 22503b3a8eb9SGleb Smirnoff static int 22513b3a8eb9SGleb Smirnoff unload_dn_sched(struct dn_alg *s) 22523b3a8eb9SGleb Smirnoff { 22533b3a8eb9SGleb Smirnoff struct dn_alg *tmp, *r; 22543b3a8eb9SGleb Smirnoff int err = EINVAL; 22553b3a8eb9SGleb Smirnoff 22563b3a8eb9SGleb Smirnoff ND("called for %s", s->name); 22573b3a8eb9SGleb Smirnoff 22583b3a8eb9SGleb Smirnoff DN_BH_WLOCK(); 22593b3a8eb9SGleb Smirnoff SLIST_FOREACH_SAFE(r, &dn_cfg.schedlist, next, tmp) { 22603b3a8eb9SGleb Smirnoff if (strcmp(s->name, r->name) != 0) 22613b3a8eb9SGleb Smirnoff continue; 22623b3a8eb9SGleb Smirnoff ND("ref_count = %d", r->ref_count); 22633b3a8eb9SGleb Smirnoff err = (r->ref_count != 0) ? EBUSY : 0; 22643b3a8eb9SGleb Smirnoff if (err == 0) 22653b3a8eb9SGleb Smirnoff SLIST_REMOVE(&dn_cfg.schedlist, r, dn_alg, next); 22663b3a8eb9SGleb Smirnoff break; 22673b3a8eb9SGleb Smirnoff } 22683b3a8eb9SGleb Smirnoff DN_BH_WUNLOCK(); 22693b3a8eb9SGleb Smirnoff D("dn_sched %s %sunloaded", s->name, err ? "not ":""); 22703b3a8eb9SGleb Smirnoff return err; 22713b3a8eb9SGleb Smirnoff } 22723b3a8eb9SGleb Smirnoff 22733b3a8eb9SGleb Smirnoff int 22743b3a8eb9SGleb Smirnoff dn_sched_modevent(module_t mod, int cmd, void *arg) 22753b3a8eb9SGleb Smirnoff { 22763b3a8eb9SGleb Smirnoff struct dn_alg *sch = arg; 22773b3a8eb9SGleb Smirnoff 22783b3a8eb9SGleb Smirnoff if (cmd == MOD_LOAD) 22793b3a8eb9SGleb Smirnoff return load_dn_sched(sch); 22803b3a8eb9SGleb Smirnoff else if (cmd == MOD_UNLOAD) 22813b3a8eb9SGleb Smirnoff return unload_dn_sched(sch); 22823b3a8eb9SGleb Smirnoff else 22833b3a8eb9SGleb Smirnoff return EINVAL; 22843b3a8eb9SGleb Smirnoff } 22853b3a8eb9SGleb Smirnoff 22863b3a8eb9SGleb Smirnoff static moduledata_t dummynet_mod = { 22873b3a8eb9SGleb Smirnoff "dummynet", dummynet_modevent, NULL 22883b3a8eb9SGleb Smirnoff }; 22893b3a8eb9SGleb Smirnoff 22903b3a8eb9SGleb Smirnoff #define DN_SI_SUB SI_SUB_PROTO_IFATTACHDOMAIN 22913b3a8eb9SGleb Smirnoff #define DN_MODEV_ORD (SI_ORDER_ANY - 128) /* after ipfw */ 22923b3a8eb9SGleb Smirnoff DECLARE_MODULE(dummynet, dummynet_mod, DN_SI_SUB, DN_MODEV_ORD); 22933b3a8eb9SGleb Smirnoff MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 22943b3a8eb9SGleb Smirnoff MODULE_VERSION(dummynet, 3); 22953b3a8eb9SGleb Smirnoff 22963b3a8eb9SGleb Smirnoff /* 22973b3a8eb9SGleb Smirnoff * Starting up. Done in order after dummynet_modevent() has been called. 22983b3a8eb9SGleb Smirnoff * VNET_SYSINIT is also called for each existing vnet and each new vnet. 22993b3a8eb9SGleb Smirnoff */ 23003b3a8eb9SGleb Smirnoff //VNET_SYSINIT(vnet_dn_init, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_init, NULL); 23013b3a8eb9SGleb Smirnoff 23023b3a8eb9SGleb Smirnoff /* 23033b3a8eb9SGleb Smirnoff * Shutdown handlers up shop. These are done in REVERSE ORDER, but still 23043b3a8eb9SGleb Smirnoff * after dummynet_modevent() has been called. Not called on reboot. 23053b3a8eb9SGleb Smirnoff * VNET_SYSUNINIT is also called for each exiting vnet as it exits. 23063b3a8eb9SGleb Smirnoff * or when the module is unloaded. 23073b3a8eb9SGleb Smirnoff */ 23083b3a8eb9SGleb Smirnoff //VNET_SYSUNINIT(vnet_dn_uninit, DN_SI_SUB, DN_MODEV_ORD+2, ip_dn_destroy, NULL); 23093b3a8eb9SGleb Smirnoff 23103b3a8eb9SGleb Smirnoff /* end of file */ 2311