2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Luigi Rizzo, Riccardo Panicucci, Universita` di Pisa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * internal dummynet APIs.
35 #ifndef _IP_DN_PRIVATE_H
36 #define _IP_DN_PRIVATE_H
39 * use ND() to remove debugging, D() to print a line,
40 * DX(level, ...) to print above a certain level
41 * If you redefine D() you are expected to redefine all.
44 #define ND(fmt, ...) do {} while (0)
45 #define D1(fmt, ...) do {} while (0)
46 #define D(fmt, ...) printf("%-10s " fmt "\n", \
47 __FUNCTION__, ## __VA_ARGS__)
48 #define DX(lev, fmt, ...) do { \
49 if (V_dn_cfg.debug > lev) D(fmt, ## __VA_ARGS__); } while (0)
52 MALLOC_DECLARE(M_DUMMYNET);
55 #define div64(a, b) ((int64_t)(a) / (int64_t)(b))
58 #define DN_LOCK_INIT() do { \
59 mtx_init(&V_dn_cfg.uh_mtx, "dn_uh", NULL, MTX_DEF); \
60 mtx_init(&V_dn_cfg.bh_mtx, "dn_bh", NULL, MTX_DEF); \
62 #define DN_LOCK_DESTROY() do { \
63 mtx_destroy(&V_dn_cfg.uh_mtx); \
64 mtx_destroy(&V_dn_cfg.bh_mtx); \
66 #if 0 /* not used yet */
67 #define DN_UH_RLOCK() mtx_lock(&V_dn_cfg.uh_mtx)
68 #define DN_UH_RUNLOCK() mtx_unlock(&V_dn_cfg.uh_mtx)
69 #define DN_UH_WLOCK() mtx_lock(&V_dn_cfg.uh_mtx)
70 #define DN_UH_WUNLOCK() mtx_unlock(&V_dn_cfg.uh_mtx)
71 #define DN_UH_LOCK_ASSERT() mtx_assert(&V_dn_cfg.uh_mtx, MA_OWNED)
74 #define DN_BH_RLOCK() mtx_lock(&V_dn_cfg.uh_mtx)
75 #define DN_BH_RUNLOCK() mtx_unlock(&V_dn_cfg.uh_mtx)
76 #define DN_BH_WLOCK() mtx_lock(&V_dn_cfg.uh_mtx)
77 #define DN_BH_WUNLOCK() mtx_unlock(&V_dn_cfg.uh_mtx)
78 #define DN_BH_LOCK_ASSERT() mtx_assert(&V_dn_cfg.uh_mtx, MA_OWNED)
80 SLIST_HEAD(dn_schk_head, dn_schk);
81 SLIST_HEAD(dn_sch_inst_head, dn_sch_inst);
82 SLIST_HEAD(dn_fsk_head, dn_fsk);
83 SLIST_HEAD(dn_queue_head, dn_queue);
84 SLIST_HEAD(dn_alg_head, dn_alg);
87 SLIST_HEAD(dn_aqm_head, dn_aqm); /* for new AQMs */
90 struct mq { /* a basic queue of packets*/
91 struct mbuf *head, *tail;
96 set_oid(struct dn_id *o, int type, int len)
104 * configuration and data for a dummynet instance
106 * When a configuration is modified from userland, 'id' is incremented
107 * so we can use the value to check for stale pointers.
110 uint32_t id; /* configuration version */
112 /* defaults (sysctl-accessible) */
113 int red_lookup_depth;
114 int red_avg_pkt_size;
115 int red_max_pkt_size;
118 long byte_limit; /* max queue sizes */
125 struct timeval prev_t; /* last time dummynet_tick ran */
126 struct dn_heap evheap; /* scheduled events */
128 long tick_last; /* Last tick duration (usec). */
129 long tick_delta; /* Last vs standard tick diff (usec). */
130 long tick_delta_sum; /* Accumulated tick difference (usec).*/
131 long tick_adjustment; /* Tick adjustments done. */
132 long tick_lost; /* Lost(coalesced) ticks number. */
133 /* Adjusted vs non-adjusted curr_time difference (ticks). */
136 /* counters of objects -- used for reporting space */
142 /* packet counters */
143 unsigned long io_pkt;
144 unsigned long io_pkt_fast;
145 unsigned long io_pkt_drop;
147 /* ticks and other stuff */
149 /* flowsets and schedulers are in hash tables, with 'hash_size'
150 * buckets. fshash is looked up at every packet arrival
151 * so better be generous if we expect many entries.
153 struct dn_ht *fshash;
154 struct dn_ht *schedhash;
155 /* list of flowsets without a scheduler -- use sch_chain */
156 struct dn_fsk_head fsu; /* list of unlinked flowsets */
158 /* Store the fs/sch to scan when draining. The value is the
159 * bucket number of the hash table. Expire can be disabled
160 * with net.inet.ip.dummynet.expire=0, or it happens every
166 uint32_t expire_cycle; /* tick count */
172 * This file is normally used in the kernel, unless we do
173 * some userland tests, in which case we do not need a mtx.
174 * uh_mtx arbitrates between system calls and also
175 * protects fshash, schedhash and fsunlinked.
176 * These structures are readonly for the lower half.
177 * bh_mtx protects all other structures which may be
178 * modified upon packet arrivals
180 #if defined( __linux__ ) || defined( _WIN32 )
192 * Delay line, contains all packets on output from a link.
193 * Every scheduler instance has one.
197 struct dn_sch_inst *si;
202 * The kernel side of a flowset. It is linked in a hash table
203 * of flowsets, and in a list of children of their parent scheduler.
204 * qht is either the queue or (if HAVE_MASK) a hash table queues.
205 * Note that the mask to use is the (flow_mask|sched_mask), which
206 * changes as we attach/detach schedulers. So we store it here.
208 * XXX If we want to add scheduler-specific parameters, we need to
209 * put them in external storage because the scheduler may not be
210 * available when the fsk is created.
212 struct dn_fsk { /* kernel side of a flowset */
214 SLIST_ENTRY(dn_fsk) fsk_next; /* hash chain for fshash */
216 struct ipfw_flow_id fsk_mask;
218 /* qht is a hash table of queues, or just a single queue
219 * a bit in fs.flags tells us which one
222 struct dn_schk *sched; /* Sched we are linked to */
223 SLIST_ENTRY(dn_fsk) sch_chain; /* list of fsk attached to sched */
225 /* bucket index used by drain routine to drain queues for this
229 /* Parameter realted to RED / GRED */
230 /* original values are in dn_fs*/
231 int w_q ; /* queue weight (scaled) */
232 int max_th ; /* maximum threshold for queue (scaled) */
233 int min_th ; /* minimum threshold for queue (scaled) */
234 int max_p ; /* maximum value for p_b (scaled) */
236 u_int c_1 ; /* max_p/(max_th-min_th) (scaled) */
237 u_int c_2 ; /* max_p*min_th/(max_th-min_th) (scaled) */
238 u_int c_3 ; /* for GRED, (1-max_p)/max_th (scaled) */
239 u_int c_4 ; /* for GRED, 1 - 2*max_p (scaled) */
240 u_int * w_q_lookup ; /* lookup table for computing (1-w_q)^t */
241 u_int lookup_depth ; /* depth of lookup table */
242 int lookup_step ; /* granularity inside the lookup table */
243 int lookup_weight ; /* equal to (1-w_q)^t / (1-w_q)^(t+1) */
244 int avg_pkt_size ; /* medium packet size */
245 int max_pkt_size ; /* max packet size */
247 struct dn_aqm *aqmfp; /* Pointer to AQM functions */
248 void *aqmcfg; /* configuration parameters for AQM */
253 * A queue is created as a child of a flowset unless it belongs to
254 * a !MULTIQUEUE scheduler. It is normally in a hash table in the
255 * flowset. fs always points to the parent flowset.
256 * si normally points to the sch_inst, unless the flowset has been
257 * detached from the scheduler -- in this case si == NULL and we
258 * should not enqueue.
261 struct dn_flow ni; /* oid, flow_id, stats */
262 struct mq mq; /* packets queue */
263 struct dn_sch_inst *_si; /* owner scheduler instance */
264 SLIST_ENTRY(dn_queue) q_next; /* hash chain list for qht */
265 struct dn_fsk *fs; /* parent flowset. */
268 int avg; /* average queue length est. (scaled) */
269 int count; /* arrivals since last RED drop */
270 int random; /* random value (scaled) */
271 uint64_t q_time; /* start of queue idle time */
273 void *aqm_status; /* per-queue status variables*/
279 * The kernel side of a scheduler. Contains the userland config,
280 * a link, pointer to extra config arguments from command line,
281 * kernel flags, and a pointer to the scheduler methods.
282 * It is stored in a hash table, and holds a list of all
283 * flowsets and scheduler instances.
284 * XXX sch must be at the beginning, see schk_hash().
288 struct dn_alg *fp; /* Pointer to scheduler functions */
289 struct dn_link link; /* The link, embedded */
290 struct dn_profile *profile; /* delay profile, if any */
291 struct dn_id *cfg; /* extra config arguments */
293 SLIST_ENTRY(dn_schk) schk_next; /* hash chain for schedhash */
295 struct dn_fsk_head fsk_list; /* all fsk linked to me */
296 struct dn_fsk *fs; /* Flowset for !MULTIQUEUE */
298 /* bucket index used by the drain routine to drain the scheduler
299 * instance for this flowset.
303 /* Hash table of all instances (through sch.sched_mask)
304 * or single instance if no mask. Always valid.
310 * Scheduler instance.
311 * Contains variables and all queues relative to a this instance.
312 * This struct is created a runtime.
315 struct dn_flow ni; /* oid, flowid and stats */
316 SLIST_ENTRY(dn_sch_inst) si_next; /* hash chain for siht */
317 struct delay_line dline;
318 struct dn_schk *sched; /* the template */
319 int kflags; /* DN_ACTIVE */
321 int64_t credit; /* bits I can transmit (more or less). */
322 uint64_t sched_time; /* time link was scheduled in ready_heap */
323 uint64_t idle_time; /* start of scheduler instance idle time */
325 /* q_count is the number of queues that this instance is using.
326 * The counter is incremented or decremented when
327 * a reference from the queue is created or deleted.
328 * It is used to make sure that a scheduler instance can be safely
329 * deleted by the drain routine. See notes below.
336 * NOTE about object drain.
337 * The system will automatically (XXX check when) drain queues and
338 * scheduler instances when they are idle.
339 * A queue is idle when it has no packets; an instance is idle when
340 * it is not in the evheap heap, and the corresponding delay line is empty.
341 * A queue can be safely deleted when it is idle because of the scheduler
342 * function xxx_free_queue() will remove any references to it.
343 * An instance can be only deleted when no queues reference it. To be sure
344 * of that, a counter (q_count) stores the number of queues that are pointing
349 * - take all flowset in a bucket for the flowset hash table
350 * - take all queues in a bucket for the flowset
351 * - increment the queue bucket
352 * - scan next flowset bucket
353 * Nothing is done if a bucket contains no entries.
355 * The same schema is used for sceduler instances
358 /* kernel-side flags. Linux has DN_DELETE in fcntl.h
361 /* 1 and 2 are reserved for the SCAN flags */
362 DN_DESTROY = 0x0004, /* destroy */
363 DN_DELETE_FS = 0x0008, /* destroy flowset */
365 DN_ACTIVE = 0x0020, /* object is in evheap */
366 DN_F_DLINE = 0x0040, /* object is a delay line */
367 DN_DEL_SAFE = 0x0080, /* delete a queue only if no longer needed
369 DN_QHT_IS_Q = 0x0100, /* in flowset, qht is a single queue */
373 * Packets processed by dummynet have an mbuf tag associated with
374 * them that carries their dummynet state.
375 * Outside dummynet, only the 'rule' field is relevant, and it must
376 * be at the beginning of the structure.
379 struct ipfw_rule_ref rule; /* matching rule */
381 /* second part, dummynet specific */
382 int dn_dir; /* action when packet comes out.*/
383 /* see ip_fw_private.h */
384 uint64_t output_time; /* when the pkt is due for delivery*/
385 struct ifnet *ifp; /* interface, for ip_output */
386 struct _ip6dn_args ip6opt; /* XXX ipv6 options */
387 uint16_t iphdr_off; /* IP header offset for mtodo() */
391 * Possible values for dn_dir. XXXGL: this needs to be reviewed
392 * and converted to same values ip_fw_args.flags use.
399 PROTO_LAYER2 = 0x4, /* set for layer 2 */
402 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
405 //extern struct dn_parms V_dn_cfg;
406 VNET_DECLARE(struct dn_parms, dn_cfg);
407 #define V_dn_cfg VNET(dn_cfg)
409 int dummynet_io(struct mbuf **, struct ip_fw_args *);
410 void dummynet_task(void *context, int pending);
411 void dn_reschedule(void);
412 struct dn_pkt_tag * dn_tag_get(struct mbuf *m);
414 struct dn_queue *ipdn_q_find(struct dn_fsk *, struct dn_sch_inst *,
415 struct ipfw_flow_id *);
416 struct dn_sch_inst *ipdn_si_find(struct dn_schk *, struct ipfw_flow_id *);
419 * copy_range is a template for requests for ranges of pipes/queues/scheds.
420 * The number of ranges is variable and can be derived by o.len.
421 * As a default, we use a small number of entries so that the struct
422 * fits easily on the stack and is sufficient for most common requests.
424 #define DEFAULT_RANGES 5
427 uint32_t r[ 2 * DEFAULT_RANGES ];
435 struct copy_range *extra; /* extra filtering */
439 int ip_dummynet_compat(struct sockopt *sopt);
440 int dummynet_get(struct sockopt *sopt, void **compat);
441 int dn_c_copy_q (void *_ni, void *arg);
442 int dn_c_copy_pipe(struct dn_schk *s, struct copy_args *a, int nq);
443 int dn_c_copy_fs(struct dn_fsk *f, struct copy_args *a, int nq);
444 int dn_compat_copy_queue(struct copy_args *a, void *_o);
445 int dn_compat_copy_pipe(struct copy_args *a, void *_o);
446 int copy_data_helper_compat(void *_o, void *_arg);
447 int dn_compat_calc_size(void);
448 int do_config(void *p, int l);
450 /* function to drain idle object */
451 void dn_drain_scheduler(void);
452 void dn_drain_queue(void);
455 int ecn_mark(struct mbuf* m);
457 /* moved from ip_dn_io.c to here to be available for AQMs modules*/
459 mq_append(struct mq *q, struct mbuf *m)
462 // buffers from netmap need to be copied
463 // XXX note that the routine is not expected to fail
464 ND("append %p to %p", m, q);
465 if (m->m_flags & M_STACK) {
470 ofs = m->m_data - m->__m_extbuf;
472 MGETHDR(m_new, M_NOWAIT, MT_DATA);
473 ND("*** WARNING, volatile buf %p ext %p %d dofs %d m_new %p",
474 m, m->__m_extbuf, m->__m_extlen, ofs, m_new);
475 p = m_new->__m_extbuf; /* new pointer */
476 l = m_new->__m_extlen; /* new len */
477 if (l <= m->__m_extlen) {
478 panic("extlen too large");
482 m_new->m_flags &= ~M_STACK;
483 m_new->__m_extbuf = p; // point to new buffer
484 _pkt_copy(m->__m_extbuf, p, m->__m_extlen);
485 m_new->m_data = p + ofs;
488 #endif /* USERSPACE */
492 q->tail->m_nextpkt = m;
499 #endif /* _IP_DN_PRIVATE_H */