ip_dn_private.h revision 206425
1130561Sobrien/*- 2130561Sobrien * Copyright (c) 2010 Luigi Rizzo, Riccardo Panicucci, Universita` di Pisa 3130561Sobrien * All rights reserved 4130561Sobrien * 5130561Sobrien * Redistribution and use in source and binary forms, with or without 6130561Sobrien * modification, are permitted provided that the following conditions 7130561Sobrien * are met: 8130561Sobrien * 1. Redistributions of source code must retain the above copyright 9130561Sobrien * notice, this list of conditions and the following disclaimer. 10130561Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11130561Sobrien * notice, this list of conditions and the following disclaimer in the 12130561Sobrien * documentation and/or other materials provided with the distribution. 13130561Sobrien * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * internal dummynet APIs. 29 * 30 * $FreeBSD: head/sys/netinet/ipfw/ip_dn_private.h 206425 2010-04-09 16:06:53Z luigi $ 31 */ 32 33#ifndef _IP_DN_PRIVATE_H 34#define _IP_DN_PRIVATE_H 35 36/* debugging support 37 * use ND() to remove debugging, D() to print a line, 38 * DX(level, ...) to print above a certain level 39 * If you redefine D() you are expected to redefine all. 40 */ 41#ifndef D 42#define ND(fmt, ...) do {} while (0) 43#define D1(fmt, ...) do {} while (0) 44#define D(fmt, ...) printf("%-10s " fmt "\n", \ 45 __FUNCTION__, ## __VA_ARGS__) 46#define DX(lev, fmt, ...) do { \ 47 if (dn_cfg.debug > lev) D(fmt, ## __VA_ARGS__); } while (0) 48#endif 49 50MALLOC_DECLARE(M_DUMMYNET); 51 52#ifndef FREE_PKT 53#define FREE_PKT(m) m_freem(m) 54#endif 55 56#ifndef __linux__ 57#define div64(a, b) ((int64_t)(a) / (int64_t)(b)) 58#endif 59 60#define DN_LOCK_INIT() do { \ 61 mtx_init(&dn_cfg.uh_mtx, "dn_uh", NULL, MTX_DEF); \ 62 mtx_init(&dn_cfg.bh_mtx, "dn_bh", NULL, MTX_DEF); \ 63 } while (0) 64#define DN_LOCK_DESTROY() do { \ 65 mtx_destroy(&dn_cfg.uh_mtx); \ 66 mtx_destroy(&dn_cfg.bh_mtx); \ 67 } while (0) 68#if 0 /* not used yet */ 69#define DN_UH_RLOCK() mtx_lock(&dn_cfg.uh_mtx) 70#define DN_UH_RUNLOCK() mtx_unlock(&dn_cfg.uh_mtx) 71#define DN_UH_WLOCK() mtx_lock(&dn_cfg.uh_mtx) 72#define DN_UH_WUNLOCK() mtx_unlock(&dn_cfg.uh_mtx) 73#define DN_UH_LOCK_ASSERT() mtx_assert(&dn_cfg.uh_mtx, MA_OWNED) 74#endif 75 76#define DN_BH_RLOCK() mtx_lock(&dn_cfg.uh_mtx) 77#define DN_BH_RUNLOCK() mtx_unlock(&dn_cfg.uh_mtx) 78#define DN_BH_WLOCK() mtx_lock(&dn_cfg.uh_mtx) 79#define DN_BH_WUNLOCK() mtx_unlock(&dn_cfg.uh_mtx) 80#define DN_BH_LOCK_ASSERT() mtx_assert(&dn_cfg.uh_mtx, MA_OWNED) 81 82SLIST_HEAD(dn_schk_head, dn_schk); 83SLIST_HEAD(dn_sch_inst_head, dn_sch_inst); 84SLIST_HEAD(dn_fsk_head, dn_fsk); 85SLIST_HEAD(dn_queue_head, dn_queue); 86SLIST_HEAD(dn_alg_head, dn_alg); 87 88struct mq { /* a basic queue of packets*/ 89 struct mbuf *head, *tail; 90}; 91 92static inline void 93set_oid(struct dn_id *o, int type, int len) 94{ 95 o->type = type; 96 o->len = len; 97 o->subtype = 0; 98}; 99 100/* 101 * configuration and global data for a dummynet instance 102 * 103 * When a configuration is modified from userland, 'id' is incremented 104 * so we can use the value to check for stale pointers. 105 */ 106struct dn_parms { 107 uint32_t id; /* configuration version */ 108 109 /* defaults (sysctl-accessible) */ 110 int red_lookup_depth; 111 int red_avg_pkt_size; 112 int red_max_pkt_size; 113 int hash_size; 114 int max_hash_size; 115 long byte_limit; /* max queue sizes */ 116 long slot_limit; 117 118 int io_fast; 119 int debug; 120 121 /* timekeeping */ 122 struct timeval prev_t; /* last time dummynet_tick ran */ 123 struct dn_heap evheap; /* scheduled events */ 124 125 /* counters of objects -- used for reporting space */ 126 int schk_count; 127 int si_count; 128 int fsk_count; 129 int queue_count; 130 131 /* ticks and other stuff */ 132 uint64_t curr_time; 133 /* flowsets and schedulers are in hash tables, with 'hash_size' 134 * buckets. fshash is looked up at every packet arrival 135 * so better be generous if we expect many entries. 136 */ 137 struct dn_ht *fshash; 138 struct dn_ht *schedhash; 139 /* list of flowsets without a scheduler -- use sch_chain */ 140 struct dn_fsk_head fsu; /* list of unlinked flowsets */ 141 struct dn_alg_head schedlist; /* list of algorithms */ 142 143 /* Store the fs/sch to scan when draining. The value is the 144 * bucket number of the hash table. Expire can be disabled 145 * with net.inet.ip.dummynet.expire=0, or it happens every 146 * expire ticks. 147 **/ 148 int drain_fs; 149 int drain_sch; 150 uint32_t expire; 151 uint32_t expire_cycle; /* tick count */ 152 153 /* if the upper half is busy doing something long, 154 * can set the busy flag and we will enqueue packets in 155 * a queue for later processing. 156 */ 157 int busy; 158 struct mq pending; 159 160#ifdef _KERNEL 161 /* 162 * This file is normally used in the kernel, unless we do 163 * some userland tests, in which case we do not need a mtx. 164 * uh_mtx arbitrates between system calls and also 165 * protects fshash, schedhash and fsunlinked. 166 * These structures are readonly for the lower half. 167 * bh_mtx protects all other structures which may be 168 * modified upon packet arrivals 169 */ 170#if defined( __linux__ ) || defined( _WIN32 ) 171 spinlock_t uh_mtx; 172 spinlock_t bh_mtx; 173#else 174 struct mtx uh_mtx; 175 struct mtx bh_mtx; 176#endif 177 178#endif /* _KERNEL */ 179}; 180 181/* 182 * Delay line, contains all packets on output from a link. 183 * Every scheduler instance has one. 184 */ 185struct delay_line { 186 struct dn_id oid; 187 struct dn_sch_inst *si; 188 struct mq mq; 189}; 190 191/* 192 * The kernel side of a flowset. It is linked in a hash table 193 * of flowsets, and in a list of children of their parent scheduler. 194 * qht is either the queue or (if HAVE_MASK) a hash table queues. 195 * Note that the mask to use is the (flow_mask|sched_mask), which 196 * changes as we attach/detach schedulers. So we store it here. 197 * 198 * XXX If we want to add scheduler-specific parameters, we need to 199 * put them in external storage because the scheduler may not be 200 * available when the fsk is created. 201 */ 202struct dn_fsk { /* kernel side of a flowset */ 203 struct dn_fs fs; 204 SLIST_ENTRY(dn_fsk) fsk_next; /* hash chain for fshash */ 205 206 struct ipfw_flow_id fsk_mask; 207 208 /* qht is a hash table of queues, or just a single queue 209 * a bit in fs.flags tells us which one 210 */ 211 struct dn_ht *qht; 212 struct dn_schk *sched; /* Sched we are linked to */ 213 SLIST_ENTRY(dn_fsk) sch_chain; /* list of fsk attached to sched */ 214 215 /* bucket index used by drain routine to drain queues for this 216 * flowset 217 */ 218 int drain_bucket; 219 /* Parameter realted to RED / GRED */ 220 /* original values are in dn_fs*/ 221 int w_q ; /* queue weight (scaled) */ 222 int max_th ; /* maximum threshold for queue (scaled) */ 223 int min_th ; /* minimum threshold for queue (scaled) */ 224 int max_p ; /* maximum value for p_b (scaled) */ 225 226 u_int c_1 ; /* max_p/(max_th-min_th) (scaled) */ 227 u_int c_2 ; /* max_p*min_th/(max_th-min_th) (scaled) */ 228 u_int c_3 ; /* for GRED, (1-max_p)/max_th (scaled) */ 229 u_int c_4 ; /* for GRED, 1 - 2*max_p (scaled) */ 230 u_int * w_q_lookup ; /* lookup table for computing (1-w_q)^t */ 231 u_int lookup_depth ; /* depth of lookup table */ 232 int lookup_step ; /* granularity inside the lookup table */ 233 int lookup_weight ; /* equal to (1-w_q)^t / (1-w_q)^(t+1) */ 234 int avg_pkt_size ; /* medium packet size */ 235 int max_pkt_size ; /* max packet size */ 236}; 237 238/* 239 * A queue is created as a child of a flowset unless it belongs to 240 * a !MULTIQUEUE scheduler. It is normally in a hash table in the 241 * flowset. fs always points to the parent flowset. 242 * si normally points to the sch_inst, unless the flowset has been 243 * detached from the scheduler -- in this case si == NULL and we 244 * should not enqueue. 245 */ 246struct dn_queue { 247 struct dn_flow ni; /* oid, flow_id, stats */ 248 struct mq mq; /* packets queue */ 249 struct dn_sch_inst *_si; /* owner scheduler instance */ 250 SLIST_ENTRY(dn_queue) q_next; /* hash chain list for qht */ 251 struct dn_fsk *fs; /* parent flowset. */ 252 253 /* RED parameters */ 254 int avg; /* average queue length est. (scaled) */ 255 int count; /* arrivals since last RED drop */ 256 int random; /* random value (scaled) */ 257 uint64_t q_time; /* start of queue idle time */ 258 259}; 260 261/* 262 * The kernel side of a scheduler. Contains the userland config, 263 * a link, pointer to extra config arguments from command line, 264 * kernel flags, and a pointer to the scheduler methods. 265 * It is stored in a hash table, and holds a list of all 266 * flowsets and scheduler instances. 267 * XXX sch must be at the beginning, see schk_hash(). 268 */ 269struct dn_schk { 270 struct dn_sch sch; 271 struct dn_alg *fp; /* Pointer to scheduler functions */ 272 struct dn_link link; /* The link, embedded */ 273 struct dn_profile *profile; /* delay profile, if any */ 274 struct dn_id *cfg; /* extra config arguments */ 275 276 SLIST_ENTRY(dn_schk) schk_next; /* hash chain for schedhash */ 277 278 struct dn_fsk_head fsk_list; /* all fsk linked to me */ 279 struct dn_fsk *fs; /* Flowset for !MULTIQUEUE */ 280 281 /* bucket index used by the drain routine to drain the scheduler 282 * instance for this flowset. 283 */ 284 int drain_bucket; 285 286 /* Hash table of all instances (through sch.sched_mask) 287 * or single instance if no mask. Always valid. 288 */ 289 struct dn_ht *siht; 290}; 291 292 293/* 294 * Scheduler instance. 295 * Contains variables and all queues relative to a this instance. 296 * This struct is created a runtime. 297 */ 298struct dn_sch_inst { 299 struct dn_flow ni; /* oid, flowid and stats */ 300 SLIST_ENTRY(dn_sch_inst) si_next; /* hash chain for siht */ 301 struct delay_line dline; 302 struct dn_schk *sched; /* the template */ 303 int kflags; /* DN_ACTIVE */ 304 305 int64_t credit; /* bits I can transmit (more or less). */ 306 uint64_t sched_time; /* time link was scheduled in ready_heap */ 307 uint64_t idle_time; /* start of scheduler instance idle time */ 308 309 /* q_count is the number of queues that this instance is using. 310 * The counter is incremented or decremented when 311 * a reference from the queue is created or deleted. 312 * It is used to make sure that a scheduler instance can be safely 313 * deleted by the drain routine. See notes below. 314 */ 315 int q_count; 316 317}; 318 319/* 320 * NOTE about object drain. 321 * The system will automatically (XXX check when) drain queues and 322 * scheduler instances when they are idle. 323 * A queue is idle when it has no packets; an instance is idle when 324 * it is not in the evheap heap, and the corresponding delay line is empty. 325 * A queue can be safely deleted when it is idle because of the scheduler 326 * function xxx_free_queue() will remove any references to it. 327 * An instance can be only deleted when no queues reference it. To be sure 328 * of that, a counter (q_count) stores the number of queues that are pointing 329 * to the instance. 330 * 331 * XXX 332 * Order of scan: 333 * - take all flowset in a bucket for the flowset hash table 334 * - take all queues in a bucket for the flowset 335 * - increment the queue bucket 336 * - scan next flowset bucket 337 * Nothing is done if a bucket contains no entries. 338 * 339 * The same schema is used for sceduler instances 340 */ 341 342 343/* kernel-side flags. Linux has DN_DELETE in fcntl.h 344 */ 345enum { 346 /* 1 and 2 are reserved for the SCAN flags */ 347 DN_DESTROY = 0x0004, /* destroy */ 348 DN_DELETE_FS = 0x0008, /* destroy flowset */ 349 DN_DETACH = 0x0010, 350 DN_ACTIVE = 0x0020, /* object is in evheap */ 351 DN_F_DLINE = 0x0040, /* object is a delay line */ 352 DN_F_SCHI = 0x00C0, /* object is a sched.instance */ 353 DN_QHT_IS_Q = 0x0100, /* in flowset, qht is a single queue */ 354}; 355 356extern struct dn_parms dn_cfg; 357 358int dummynet_io(struct mbuf **, int , struct ip_fw_args *); 359void dummynet_task(void *context, int pending); 360void dn_reschedule(void); 361 362struct dn_queue *ipdn_q_find(struct dn_fsk *, struct dn_sch_inst *, 363 struct ipfw_flow_id *); 364struct dn_sch_inst *ipdn_si_find(struct dn_schk *, struct ipfw_flow_id *); 365 366/* 367 * copy_range is a template for requests for ranges of pipes/queues/scheds. 368 * The number of ranges is variable and can be derived by o.len. 369 * As a default, we use a small number of entries so that the struct 370 * fits easily on the stack and is sufficient for most common requests. 371 */ 372#define DEFAULT_RANGES 5 373struct copy_range { 374 struct dn_id o; 375 uint32_t r[ 2 * DEFAULT_RANGES ]; 376}; 377 378struct copy_args { 379 char **start; 380 char *end; 381 int flags; 382 int type; 383 struct copy_range *extra; /* extra filtering */ 384}; 385 386struct sockopt; 387int ip_dummynet_compat(struct sockopt *sopt); 388int dummynet_get(struct sockopt *sopt, void **compat); 389int dn_c_copy_q (void *_ni, void *arg); 390int dn_c_copy_pipe(struct dn_schk *s, struct copy_args *a, int nq); 391int dn_c_copy_fs(struct dn_fsk *f, struct copy_args *a, int nq); 392int dn_compat_copy_queue(struct copy_args *a, void *_o); 393int dn_compat_copy_pipe(struct copy_args *a, void *_o); 394int copy_data_helper_compat(void *_o, void *_arg); 395int dn_compat_calc_size(void); 396int do_config(void *p, int l); 397 398/* function to drain idle object */ 399void dn_drain_scheduler(void); 400void dn_drain_queue(void); 401 402#endif /* _IP_DN_PRIVATE_H */ 403