]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_dummynet.h
xnu-344.tar.gz
[apple/xnu.git] / bsd / netinet / ip_dummynet.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1998 Luigi Rizzo
24 *
25 * Redistribution and use in source forms, with and without modification,
26 * are permitted provided that this entire comment appears intact.
27 *
28 * Redistribution in binary form may occur without any restrictions.
29 * Obviously, it would be nice if you gave credit where credit is due
30 * but requiring it would be too onerous.
31 *
32 * This software is provided ``AS IS'' without any warranties of any kind.
33 *
34 * $FreeBSD: src/sys/netinet/ip_dummynet.h,v 1.10.2.3 2001/02/01 20:25:09 luigi Exp $
35 */
36
37 #ifndef _IP_DUMMYNET_H
38 #define _IP_DUMMYNET_H
39 #include <sys/appleapiopts.h>
40 #ifdef __APPLE_API_PRIVATE
41
42 /*
43 * Definition of dummynet data structures. In the structures, I decided
44 * not to use the macros in <sys/queue.h> in the hope of making the code
45 * easier to port to other architectures. The type of lists and queue we
46 * use here is pretty simple anyways.
47 */
48
49 /*
50 * We start with a heap, which is used in the scheduler to decide when
51 * to transmit packets etc.
52 *
53 * The key for the heap is used for two different values:
54 *
55 * 1. timer ticks- max 10K/second, so 32 bits are enough;
56 *
57 * 2. virtual times. These increase in steps of len/x, where len is the
58 * packet length, and x is either the weight of the flow, or the
59 * sum of all weights.
60 * If we limit to max 1000 flows and a max weight of 100, then
61 * x needs 17 bits. The packet size is 16 bits, so we can easily
62 * overflow if we do not allow errors.
63 * So we use a key "dn_key" which is 64 bits. Some macros are used to
64 * compare key values and handle wraparounds.
65 * MAX64 returns the largest of two key values.
66 * MY_M is used as a shift count when doing fixed point arithmetic
67 * (a better name would be useful...).
68 */
69 typedef u_int64_t dn_key ; /* sorting key */
70 #define DN_KEY_LT(a,b) ((int64_t)((a)-(b)) < 0)
71 #define DN_KEY_LEQ(a,b) ((int64_t)((a)-(b)) <= 0)
72 #define DN_KEY_GT(a,b) ((int64_t)((a)-(b)) > 0)
73 #define DN_KEY_GEQ(a,b) ((int64_t)((a)-(b)) >= 0)
74 #define MAX64(x,y) (( (int64_t) ( (y)-(x) )) > 0 ) ? (y) : (x)
75 #define MY_M 16 /* number of left shift to obtain a larger precision */
76
77 /*
78 * XXX With this scaling, max 1000 flows, max weight 100, 1Gbit/s, the
79 * virtual time wraps every 15 days.
80 */
81
82 /*
83 * The OFFSET_OF macro is used to return the offset of a field within
84 * a structure. It is used by the heap management routines.
85 */
86 #define OFFSET_OF(type, field) ((int)&( ((type *)0)->field) )
87
88 /*
89 * A heap entry is made of a key and a pointer to the actual
90 * object stored in the heap.
91 * The heap is an array of dn_heap_entry entries, dynamically allocated.
92 * Current size is "size", with "elements" actually in use.
93 * The heap normally supports only ordered insert and extract from the top.
94 * If we want to extract an object from the middle of the heap, we
95 * have to know where the object itself is located in the heap (or we
96 * need to scan the whole array). To this purpose, an object has a
97 * field (int) which contains the index of the object itself into the
98 * heap. When the object is moved, the field must also be updated.
99 * The offset of the index in the object is stored in the 'offset'
100 * field in the heap descriptor. The assumption is that this offset
101 * is non-zero if we want to support extract from the middle.
102 */
103 struct dn_heap_entry {
104 dn_key key ; /* sorting key. Topmost element is smallest one */
105 void *object ; /* object pointer */
106 } ;
107
108 struct dn_heap {
109 int size ;
110 int elements ;
111 int offset ; /* XXX if > 0 this is the offset of direct ptr to obj */
112 struct dn_heap_entry *p ; /* really an array of "size" entries */
113 } ;
114
115 /*
116 * MT_DUMMYNET is a new (fake) mbuf type that is prepended to the
117 * packet when it comes out of a pipe. The definition
118 * ought to go in /sys/sys/mbuf.h but here it is less intrusive.
119 */
120
121 #define MT_DUMMYNET MT_CONTROL
122
123 /*
124 * struct dn_pkt identifies a packet in the dummynet queue. The
125 * first part is really an m_hdr for implementation purposes, and some
126 * fields are saved there. When passing the packet back to the ip_input/
127 * ip_output()/bdg_forward, the struct is prepended to the mbuf chain with type
128 * MT_DUMMYNET, and contains the pointer to the matching rule.
129 *
130 * Note: there is no real need to make this structure contain an m_hdr,
131 * in the future this should be changed to a normal data structure.
132 */
133 struct dn_pkt {
134 struct m_hdr hdr ;
135 #define dn_next hdr.mh_nextpkt /* next element in queue */
136 #define DN_NEXT(x) (struct dn_pkt *)(x)->dn_next
137 #define dn_m hdr.mh_next /* packet to be forwarded */
138 #define dn_dir hdr.mh_flags /* action when pkt extracted from a queue */
139 #define DN_TO_IP_OUT 1
140 #define DN_TO_IP_IN 2
141 #define DN_TO_BDG_FWD 3
142
143 dn_key output_time; /* when the pkt is due for delivery */
144 struct ifnet *ifp; /* interface, for ip_output */
145 struct sockaddr_in *dn_dst ;
146 struct route ro; /* route, for ip_output. MUST COPY */
147 int flags ; /* flags, for ip_output (IPv6 ?) */
148 };
149
150 /*
151 * Overall structure of dummynet (with WF2Q+):
152
153 In dummynet, packets are selected with the firewall rules, and passed
154 to two different objects: PIPE or QUEUE.
155
156 A QUEUE is just a queue with configurable size and queue management
157 policy. It is also associated with a mask (to discriminate among
158 different flows), a weight (used to give different shares of the
159 bandwidth to different flows) and a "pipe", which essentially
160 supplies the transmit clock for all queues associated with that
161 pipe.
162
163 A PIPE emulates a fixed-bandwidth link, whose bandwidth is
164 configurable. The "clock" for a pipe can come from either an
165 internal timer, or from the transmit interrupt of an interface.
166 A pipe is also associated with one (or more, if masks are used)
167 queue, where all packets for that pipe are stored.
168
169 The bandwidth available on the pipe is shared by the queues
170 associated with that pipe (only one in case the packet is sent
171 to a PIPE) according to the WF2Q+ scheduling algorithm and the
172 configured weights.
173
174 In general, incoming packets are stored in the appropriate queue,
175 which is then placed into one of a few heaps managed by a scheduler
176 to decide when the packet should be extracted.
177 The scheduler (a function called dummynet()) is run at every timer
178 tick, and grabs queues from the head of the heaps when they are
179 ready for processing.
180
181 There are three data structures definining a pipe and associated queues:
182
183 + dn_pipe, which contains the main configuration parameters related
184 to delay and bandwidth;
185 + dn_flow_set, which contains WF2Q+ configuration, flow
186 masks, plr and RED configuration;
187 + dn_flow_queue, which is the per-flow queue (containing the packets)
188
189 Multiple dn_flow_set can be linked to the same pipe, and multiple
190 dn_flow_queue can be linked to the same dn_flow_set.
191 All data structures are linked in a linear list which is used for
192 housekeeping purposes.
193
194 During configuration, we create and initialize the dn_flow_set
195 and dn_pipe structures (a dn_pipe also contains a dn_flow_set).
196
197 At runtime: packets are sent to the appropriate dn_flow_set (either
198 WFQ ones, or the one embedded in the dn_pipe for fixed-rate flows),
199 which in turn dispatches them to the appropriate dn_flow_queue
200 (created dynamically according to the masks).
201
202 The transmit clock for fixed rate flows (ready_event()) selects the
203 dn_flow_queue to be used to transmit the next packet. For WF2Q,
204 wfq_ready_event() extract a pipe which in turn selects the right
205 flow using a number of heaps defined into the pipe itself.
206
207 *
208 */
209
210 /*
211 * per flow queue. This contains the flow identifier, the queue
212 * of packets, counters, and parameters used to support both RED and
213 * WF2Q+.
214 */
215 struct dn_flow_queue {
216 struct dn_flow_queue *next ;
217 struct ipfw_flow_id id ;
218 struct dn_pkt *head, *tail ; /* queue of packets */
219 u_int len ;
220 u_int len_bytes ;
221 long numbytes ; /* credit for transmission (dynamic queues) */
222
223 u_int64_t tot_pkts ; /* statistics counters */
224 u_int64_t tot_bytes ;
225 u_int32_t drops ;
226 int hash_slot ; /* debugging/diagnostic */
227
228 /* RED parameters */
229 int avg ; /* average queue length est. (scaled) */
230 int count ; /* arrivals since last RED drop */
231 int random ; /* random value (scaled) */
232 u_int32_t q_time ; /* start of queue idle time */
233
234 /* WF2Q+ support */
235 struct dn_flow_set *fs ; /* parent flow set */
236 int heap_pos ; /* position (index) of struct in heap */
237 dn_key sched_time ; /* current time when queue enters ready_heap */
238
239 dn_key S,F ; /* start-time, finishing time */
240 /* setting F < S means the timestamp is invalid. We only need
241 * to test this when the queue is empty.
242 */
243 } ;
244
245 /*
246 * flow_set descriptor. Contains the "template" parameters for the
247 * queue configuration, and pointers to the hash table of dn_flow_queue's.
248 *
249 * The hash table is an array of lists -- we identify the slot by
250 * hashing the flow-id, then scan the list looking for a match.
251 * The size of the hash table (buckets) is configurable on a per-queue
252 * basis.
253 */
254 struct dn_flow_set {
255 struct dn_flow_set *next; /* next flow set in all_flow_sets list */
256
257 u_short fs_nr ; /* flow_set number */
258 u_short flags_fs;
259 #define DN_HAVE_FLOW_MASK 0x0001
260 #define DN_IS_PIPE 0x4000
261 #define DN_IS_QUEUE 0x8000
262 #define DN_IS_RED 0x0002
263 #define DN_IS_GENTLE_RED 0x0004
264 #define DN_QSIZE_IS_BYTES 0x0008 /* queue measured in bytes */
265
266 struct dn_pipe *pipe ; /* pointer to parent pipe */
267 u_short parent_nr ; /* parent pipe#, 0 if local to a pipe */
268
269 int weight ; /* WFQ queue weight */
270 int qsize ; /* queue size in slots or bytes */
271 int plr ; /* pkt loss rate (2^31-1 means 100%) */
272
273 struct ipfw_flow_id flow_mask ;
274 /* hash table of queues onto this flow_set */
275 int rq_size ; /* number of slots */
276 int rq_elements ; /* active elements */
277 struct dn_flow_queue **rq; /* array of rq_size entries */
278 u_int32_t last_expired ; /* do not expire too frequently */
279 /* XXX some RED parameters as well ? */
280 int backlogged ; /* #active queues for this flowset */
281
282 /* RED parameters */
283 #define SCALE_RED 16
284 #define SCALE(x) ( (x) << SCALE_RED )
285 #define SCALE_VAL(x) ( (x) >> SCALE_RED )
286 #define SCALE_MUL(x,y) ( ( (x) * (y) ) >> SCALE_RED )
287 int w_q ; /* queue weight (scaled) */
288 int max_th ; /* maximum threshold for queue (scaled) */
289 int min_th ; /* minimum threshold for queue (scaled) */
290 int max_p ; /* maximum value for p_b (scaled) */
291 u_int c_1 ; /* max_p/(max_th-min_th) (scaled) */
292 u_int c_2 ; /* max_p*min_th/(max_th-min_th) (scaled) */
293 u_int c_3 ; /* for GRED, (1-max_p)/max_th (scaled) */
294 u_int c_4 ; /* for GRED, 1 - 2*max_p (scaled) */
295 u_int * w_q_lookup ; /* lookup table for computing (1-w_q)^t */
296 u_int lookup_depth ; /* depth of lookup table */
297 int lookup_step ; /* granularity inside the lookup table */
298 int lookup_weight ; /* equal to (1-w_q)^t / (1-w_q)^(t+1) */
299 int avg_pkt_size ; /* medium packet size */
300 int max_pkt_size ; /* max packet size */
301 } ;
302
303 /*
304 * Pipe descriptor. Contains global parameters, delay-line queue,
305 * and the flow_set used for fixed-rate queues.
306 *
307 * For WF2Q support it also has 4 heaps holding dn_flow_queue:
308 * not_eligible_heap, for queues whose start time is higher
309 * than the virtual time. Sorted by start time.
310 * scheduler_heap, for queues eligible for scheduling. Sorted by
311 * finish time.
312 * backlogged_heap, all flows in the two heaps above, sorted by
313 * start time. This is used to compute the virtual time.
314 * idle_heap, all flows that are idle and can be removed. We
315 * do that on each tick so we do not slow down too much
316 * operations during forwarding.
317 *
318 */
319 struct dn_pipe { /* a pipe */
320 struct dn_pipe *next ;
321
322 int pipe_nr ; /* number */
323 int bandwidth; /* really, bytes/tick. */
324 int delay ; /* really, ticks */
325
326 struct dn_pkt *head, *tail ; /* packets in delay line */
327
328 /* WF2Q+ */
329 struct dn_heap scheduler_heap ; /* top extract - key Finish time*/
330 struct dn_heap not_eligible_heap; /* top extract- key Start time */
331 struct dn_heap idle_heap ; /* random extract - key Start=Finish time */
332
333 dn_key V ; /* virtual time */
334 int sum; /* sum of weights of all active sessions */
335 int numbytes; /* bit i can transmit (more or less). */
336
337 dn_key sched_time ; /* first time pipe is scheduled in ready_heap */
338
339 /* the tx clock can come from an interface. In this case, the
340 * name is below, and the pointer is filled when the rule is
341 * configured. We identify this by setting the if_name to a
342 * non-empty string.
343 */
344 char if_name[16];
345 struct ifnet *ifp ;
346 int ready ; /* set if ifp != NULL and we got a signal from it */
347
348 struct dn_flow_set fs ; /* used with fixed-rate flows */
349 };
350
351 #ifdef KERNEL
352
353 MALLOC_DECLARE(M_IPFW);
354
355 typedef int ip_dn_ctl_t __P((struct sockopt *)) ;
356 extern ip_dn_ctl_t *ip_dn_ctl_ptr;
357
358 void dn_rule_delete(void *r); /* used in ip_fw.c */
359 int dummynet_io(int pipe, int dir,
360 struct mbuf *m, struct ifnet *ifp, struct route *ro,
361 struct sockaddr_in * dst,
362 struct ip_fw_chain *rule, int flags);
363 #endif
364
365 #endif /* __APPLE_API_PRIVATE */
366 #endif /* _IP_DUMMYNET_H */