CPU localize dummynet(4) step 1/2
[dragonfly.git] / sys / net / dummynet / ip_dummynet.h
1 /*
2  * Copyright (c) 1998-2002 Luigi Rizzo, Universita` di Pisa
3  * Portions Copyright (c) 2000 Akamba Corp.
4  * All rights reserved
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/netinet/ip_dummynet.h,v 1.10.2.9 2003/05/13 09:31:06 maxim Exp $
28  * $DragonFly: src/sys/net/dummynet/ip_dummynet.h,v 1.17 2007/11/16 02:45:45 sephe Exp $
29  */
30
31 #ifndef _IP_DUMMYNET_H
32 #define _IP_DUMMYNET_H
33
34 /*
35  * We start with a heap, which is used in the scheduler to decide when to
36  * transmit packets etc.
37  *
38  * The key for the heap is used for two different values:
39  *
40  * 1. Timer ticks- max 10K/second, so 32 bits are enough;
41  *
42  * 2. Virtual times.  These increase in steps of len/x, where len is the
43  *    packet length, and x is either the weight of the flow, or the sum
44  *    of all weights.
45  *    If we limit to max 1000 flows and a max weight of 100, then x needs
46  *    17 bits.  The packet size is 16 bits, so we can easily overflow if
47  *    we do not allow errors.
48  *
49  * So we use a key "dn_key" which is 64 bits.
50  *
51  * MY_M is used as a shift count when doing fixed point arithmetic
52  * (a better name would be useful...).
53  */
54 typedef uint64_t        dn_key; /* sorting key */
55
56 /*
57  * Number of left shift to obtain a larger precision
58  *
59  * XXX With this scaling, max 1000 flows, max weight 100, 1Gbit/s, the
60  * virtual time wraps every 15 days.
61  */
62 #define MY_M            16
63
64 #ifdef _KERNEL
65
66 /*
67  * A heap entry is made of a key and a pointer to the actual object stored
68  * in the heap.
69  *
70  * The heap is an array of dn_heap_entry entries, dynamically allocated.
71  * Current size is "size", with "elements" actually in use.
72  *
73  * The heap normally supports only ordered insert and extract from the top.
74  * If we want to extract an object from the middle of the heap, we have to
75  * know where the object itself is located in the heap (or we need to scan
76  * the whole array).  To this purpose, an object has a field (int) which
77  * contains the index of the object itself into the heap.  When the object
78  * is moved, the field must also be updated.  The offset of the index in the
79  * object is stored in the 'offset' field in the heap descriptor.  The
80  * assumption is that this offset is non-zero if we want to support extract
81  * from the middle.
82  */
83 struct dn_heap_entry {
84     dn_key key;         /* sorting key.  Topmost element is smallest one */
85     void *object;       /* object pointer */
86 };
87
88 struct dn_heap {
89     int size;
90     int elements;
91     int offset; /* XXX if > 0 this is the offset of direct ptr to obj */
92     struct dn_heap_entry *p;    /* really an array of "size" entries */
93 };
94
95 struct dn_flow_id {
96     uint16_t fid_type;  /* ETHERTYPE_ */
97     uint16_t pad;
98     union {
99         struct {
100             uint32_t dst_ip;
101             uint32_t src_ip;
102             uint16_t dst_port;
103             uint16_t src_port;
104             uint8_t proto;
105             uint8_t flags;
106         } inet;
107     } fid_u;
108 #define fid_dst_ip      fid_u.inet.dst_ip
109 #define fid_src_ip      fid_u.inet.src_ip
110 #define fid_dst_port    fid_u.inet.dst_port
111 #define fid_src_port    fid_u.inet.src_port
112 #define fid_proto       fid_u.inet.proto
113 #define fid_flags       fid_u.inet.flags
114 };
115
116 typedef void    (*ip_dn_unref_priv_t)(void *);
117
118 /*
119  * struct dn_pkt identifies a packet in the dummynet queue, but is also used
120  * to tag packets passed back to the various destinations (ip_input(),
121  * ip_output() and so on).
122  *
123  * It is a tag (PACKET_TAG_DUMMYNET) associated with the actual mbuf.
124  */
125 struct dn_pkt {
126     struct mbuf *dn_m;
127     TAILQ_ENTRY(dn_pkt) dn_next;
128
129     void *dn_priv;
130     ip_dn_unref_priv_t dn_unref_priv;
131
132     uint32_t dn_flags;          /* action when packet comes out. */
133 #define DN_FLAGS_IS_PIPE        0x10
134 #define DN_FLAGS_DIR_MASK       0x0f
135 #define DN_TO_IP_OUT            1
136 #define DN_TO_IP_IN             2
137 #define DN_TO_ETH_DEMUX         4
138 #define DN_TO_ETH_OUT           5
139 #define DN_TO_MAX               6
140
141     dn_key output_time;         /* when the pkt is due for delivery */
142     struct ifnet *ifp;          /* interface, for ip_output */
143     struct sockaddr_in *dn_dst;
144     struct route ro;            /* route, for ip_output. MUST COPY */
145     int flags;                  /* flags, for ip_output (IPv6 ?) */
146
147     u_short pipe_nr;            /* pipe/flow_set number */
148     u_short pad;
149
150     struct dn_flow_id id;       /* flow id */
151     int cpuid;                  /* target cpu, for IP_OUT/ETH_DEMUX/ETH_OUT */
152 };
153 TAILQ_HEAD(dn_pkt_queue, dn_pkt);
154
155 /*
156  * Overall structure of dummynet (with WF2Q+):
157  *
158  * In dummynet, packets are selected with the firewall rules, and passed to
159  * two different objects: PIPE or QUEUE.
160  *
161  * A QUEUE is just a queue with configurable size and queue management policy.
162  * It is also associated with a mask (to discriminate among different flows),
163  * a weight (used to give different shares of the bandwidth to different flows)
164  * and a "pipe", which essentially supplies the transmit clock for all queues
165  * associated with that pipe.
166  *
167  * A PIPE emulates a fixed-bandwidth link, whose bandwidth is configurable.
168  * The "clock" for a pipe comes from an internal timer.  A pipe is also
169  * associated with one (or more, if masks are used) queue, where all packets
170  * for that pipe are stored.
171  *
172  * The bandwidth available on the pipe is shared by the queues associated with
173  * that pipe (only one in case the packet is sent to a PIPE) according to the
174  * WF2Q+ scheduling algorithm and the configured weights.
175  *
176  * In general, incoming packets are stored in the appropriate queue, which is
177  * then placed into one of a few heaps managed by a scheduler to decide when
178  * the packet should be extracted.  The scheduler (a function called dummynet())
179  * is run at every timer tick, and grabs queues from the head of the heaps when
180  * they are ready for processing.
181  *
182  * There are three data structures definining a pipe and associated queues:
183  *
184  *  + dn_pipe, which contains the main configuration parameters related to
185  *    delay and bandwidth;
186  *  + dn_flow_set, which contains WF2Q+ configuration, flow masks, plr and
187  *    RED configuration;
188  *  + dn_flow_queue, which is the per-flow queue (containing the packets)
189  *
190  * Multiple dn_flow_set can be linked to the same pipe, and multiple
191  * dn_flow_queue can be linked to the same dn_flow_set.
192  * All data structures are linked in a linear list which is used for
193  * housekeeping purposes.
194  *
195  * During configuration, we create and initialize the dn_flow_set and dn_pipe
196  * structures (a dn_pipe also contains a dn_flow_set).
197  *
198  * At runtime: packets are sent to the appropriate dn_flow_set (either WFQ
199  * ones, or the one embedded in the dn_pipe for fixed-rate flows), which in
200  * turn dispatches them to the appropriate dn_flow_queue (created dynamically
201  * according to the masks).
202  *
203  * The transmit clock for fixed rate flows (ready_event()) selects the
204  * dn_flow_queue to be used to transmit the next packet. For WF2Q,
205  * wfq_ready_event() extract a pipe which in turn selects the right flow using
206  * a number of heaps defined into the pipe itself.
207  */
208
209 /*
210  * Per flow queue.  This contains the flow identifier, the queue of packets,
211  * counters, and parameters used to support both RED and WF2Q+.
212  *
213  * A dn_flow_queue is created and initialized whenever a packet for a new
214  * flow arrives.
215  */
216 struct dn_flow_queue {
217     struct dn_flow_id id;
218     LIST_ENTRY(dn_flow_queue) q_link;
219
220     struct dn_pkt_queue queue;  /* queue of packets */
221     u_int len;
222     u_int len_bytes;
223     u_long numbytes;            /* credit for transmission (dynamic queues) */
224
225     uint64_t tot_pkts;          /* statistics counters */
226     uint64_t tot_bytes;
227     uint32_t drops;
228
229     int hash_slot;              /* debugging/diagnostic */
230
231     /* RED parameters */
232     int avg;                    /* average queue length est. (scaled) */
233     int count;                  /* arrivals since last RED drop */
234     int random;                 /* random value (scaled) */
235     uint32_t q_time;            /* start of queue idle time */
236
237     /* WF2Q+ support */
238     struct dn_flow_set *fs;     /* parent flow set */
239     int heap_pos;               /* position (index) of struct in heap */
240     dn_key sched_time;          /* current time when queue enters ready_heap */
241
242     dn_key S, F;                /* start time, finish time */
243     /*
244      * Setting F < S means the timestamp is invalid. We only need
245      * to test this when the queue is empty.
246      */
247 };
248 LIST_HEAD(dn_flowqueue_head, dn_flow_queue);
249
250 /*
251  * flow_set descriptor.  Contains the "template" parameters for the queue
252  * configuration, and pointers to the hash table of dn_flow_queue's.
253  *
254  * The hash table is an array of lists -- we identify the slot by hashing
255  * the flow-id, then scan the list looking for a match.
256  * The size of the hash table (buckets) is configurable on a per-queue basis.
257  *
258  * A dn_flow_set is created whenever a new queue or pipe is created (in the
259  * latter case, the structure is located inside the struct dn_pipe).
260  */
261 struct dn_flow_set {
262     u_short fs_nr;              /* flow_set number */
263     u_short flags_fs;           /* see 'Flow set flags' */
264
265     LIST_ENTRY(dn_flow_set) fs_link;
266
267     struct dn_pipe *pipe;       /* pointer to parent pipe */
268     u_short parent_nr;          /* parent pipe#, 0 if local to a pipe */
269
270     int weight;                 /* WFQ queue weight */
271     int qsize;                  /* queue size in slots or bytes */
272     int plr;                    /* pkt loss rate (2^31-1 means 100%) */
273
274     struct dn_flow_id flow_mask;
275
276     /* hash table of queues onto this flow_set */
277     int rq_size;                /* number of slots */
278     int rq_elements;            /* active elements */
279     struct dn_flowqueue_head *rq;/* array of rq_size entries */
280
281     uint32_t last_expired;      /* do not expire too frequently */
282     int backlogged;             /* #active queues for this flowset */
283
284     /* RED parameters */
285     int w_q;                    /* queue weight (scaled) */
286     int max_th;                 /* maximum threshold for queue (scaled) */
287     int min_th;                 /* minimum threshold for queue (scaled) */
288     int max_p;                  /* maximum value for p_b (scaled) */
289     u_int c_1;                  /* max_p/(max_th-min_th) (scaled) */
290     u_int c_2;                  /* max_p*min_th/(max_th-min_th) (scaled) */
291     u_int c_3;                  /* for GRED, (1-max_p)/max_th (scaled) */
292     u_int c_4;                  /* for GRED, 1 - 2*max_p (scaled) */
293     u_int *w_q_lookup;          /* lookup table for computing (1-w_q)^t */
294     u_int lookup_depth;         /* depth of lookup table */
295     int lookup_step;            /* granularity inside the lookup table */
296     int lookup_weight;          /* equal to (1-w_q)^t / (1-w_q)^(t+1) */
297     int avg_pkt_size;           /* medium packet size */
298     int max_pkt_size;           /* max packet size */
299 };
300 LIST_HEAD(dn_flowset_head, dn_flow_set);
301
302 /*
303  * Pipe descriptor. Contains global parameters, delay-line queue, and the
304  * flow_set used for fixed-rate queues.
305  *
306  * For WF2Q+ support it also has 3 heaps holding dn_flow_queue:
307  *  + not_eligible_heap, for queues whose start time is higher than the
308  *    virtual time. Sorted by start time.
309  *  + scheduler_heap, for queues eligible for scheduling.  Sorted by finish
310  *    time.
311  *  + idle_heap, all flows that are idle and can be removed.  We do that on
312  *    each tick so we do not slow down too much operations during forwarding.
313  */
314 struct dn_pipe {                /* a pipe */
315     int pipe_nr;                /* number */
316     int bandwidth;              /* really, bytes/tick. */
317     int delay;                  /* really, ticks */
318
319     struct dn_pkt_queue p_queue;/* packets in delay line */
320     LIST_ENTRY(dn_pipe) p_link;
321
322     /* WF2Q+ */
323     struct dn_heap scheduler_heap; /* top extract - key Finish time*/
324     struct dn_heap not_eligible_heap; /* top extract- key Start time */
325     struct dn_heap idle_heap;   /* random extract - key Start=Finish time */
326
327     dn_key V;                   /* virtual time */
328     int sum;                    /* sum of weights of all active sessions */
329     int numbytes;               /* bits I can transmit (more or less). */
330
331     dn_key sched_time;          /* time pipe was scheduled in ready_heap */
332
333     struct dn_flow_set fs;      /* used with fixed-rate flows */
334 };
335 LIST_HEAD(dn_pipe_head, dn_pipe);
336
337 typedef int     ip_dn_ctl_t(struct sockopt *);  /* raw_ip.c */
338 typedef int     ip_dn_io_t(struct mbuf *);
339
340 extern ip_dn_ctl_t      *ip_dn_ctl_ptr;
341 extern ip_dn_io_t       *ip_dn_io_ptr;
342
343 void    ip_dn_queue(struct mbuf *);
344 void    ip_dn_packet_free(struct dn_pkt *);
345 void    ip_dn_packet_redispatch(struct dn_pkt *);
346
347 #define DUMMYNET_LOADED (ip_dn_io_ptr != NULL)
348
349 #endif  /* _KERNEL */
350
351 struct dn_ioc_flowid {
352     uint16_t type;      /* ETHERTYPE_ */
353     uint16_t pad;
354     union {
355         struct {
356             uint32_t dst_ip;
357             uint32_t src_ip;
358             uint16_t dst_port;
359             uint16_t src_port;
360             uint8_t proto;
361             uint8_t flags;
362         } ip;
363         uint8_t pad[64];
364     } u;
365 };
366
367 struct dn_ioc_flowqueue {
368     u_int len;
369     u_int len_bytes;
370
371     uint64_t tot_pkts;
372     uint64_t tot_bytes;
373     uint32_t drops;
374
375     int hash_slot;              /* debugging/diagnostic */
376     dn_key S;                   /* virtual start time */
377     dn_key F;                   /* virtual finish time */
378
379     struct dn_ioc_flowid id;
380     uint8_t reserved[16];
381 };
382
383 struct dn_ioc_flowset {
384     u_short fs_type;            /* DN_IS_{QUEUE,PIPE}, MUST be first */
385
386     u_short fs_nr;              /* flow_set number */
387     u_short flags_fs;           /* see 'Flow set flags' */
388     u_short parent_nr;          /* parent pipe#, 0 if local to a pipe */
389
390     int weight;                 /* WFQ queue weight */
391     int qsize;                  /* queue size in slots or bytes */
392     int plr;                    /* pkt loss rate (2^31-1 means 100%) */
393
394     /* Hash table information */
395     int rq_size;                /* number of slots */
396     int rq_elements;            /* active elements */
397
398     /* RED parameters */
399     int w_q;                    /* queue weight (scaled) */
400     int max_th;                 /* maximum threshold for queue (scaled) */
401     int min_th;                 /* minimum threshold for queue (scaled) */
402     int max_p;                  /* maximum value for p_b (scaled) */
403     int lookup_step;            /* granularity inside the lookup table */
404     int lookup_weight;          /* equal to (1-w_q)^t / (1-w_q)^(t+1) */
405
406     struct dn_ioc_flowid flow_mask;
407     uint8_t reserved[16];
408 };
409
410 struct dn_ioc_pipe {
411     struct dn_ioc_flowset fs;   /* MUST be first */
412
413     int pipe_nr;                /* pipe number */
414     int bandwidth;              /* bit/second */
415     int delay;                  /* milliseconds */
416
417     dn_key V;                   /* virtual time */
418
419     uint8_t reserved[16];
420 };
421
422 /*
423  * Flow set flags
424  */
425 #define DN_HAVE_FLOW_MASK       0x0001
426 #define DN_IS_RED               0x0002
427 #define DN_IS_GENTLE_RED        0x0004
428 #define DN_QSIZE_IS_BYTES       0x0008  /* queue size is measured in bytes */
429 #define DN_NOERROR              0x0010  /* do not report ENOBUFS on drops */
430 #define DN_IS_PIPE              0x4000
431 #define DN_IS_QUEUE             0x8000
432
433 /*
434  * Macros for RED
435  */
436 #define SCALE_RED               16
437 #define SCALE(x)                ((x) << SCALE_RED)
438 #define SCALE_VAL(x)            ((x) >> SCALE_RED)
439 #define SCALE_MUL(x, y)         (((x) * (y)) >> SCALE_RED)
440
441 /*
442  * Maximum pipe number
443  */
444 #define DN_PIPE_NR_MAX          65536
445
446 #endif /* !_IP_DUMMYNET_H */