]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_dummynet.h
718a32f66190d04cd1e32edbdf748185a3783003
[apple/xnu.git] / bsd / netinet / ip_dummynet.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1998-2002 Luigi Rizzo, Universita` di Pisa
32 * Portions Copyright (c) 2000 Akamba Corp.
33 * All rights reserved
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * $FreeBSD: src/sys/netinet/ip_dummynet.h,v 1.32 2004/08/17 22:05:54 andre Exp $
57 */
58
59 #ifndef _IP_DUMMYNET_H
60 #define _IP_DUMMYNET_H
61 #include <sys/appleapiopts.h>
62
63 #ifdef PRIVATE
64 /*
65 * Definition of dummynet data structures. In the structures, I decided
66 * not to use the macros in <sys/queue.h> in the hope of making the code
67 * easier to port to other architectures. The type of lists and queue we
68 * use here is pretty simple anyways.
69 */
70
71 /*
72 * We start with a heap, which is used in the scheduler to decide when
73 * to transmit packets etc.
74 *
75 * The key for the heap is used for two different values:
76 *
77 * 1. timer ticks- max 10K/second, so 32 bits are enough;
78 *
79 * 2. virtual times. These increase in steps of len/x, where len is the
80 * packet length, and x is either the weight of the flow, or the
81 * sum of all weights.
82 * If we limit to max 1000 flows and a max weight of 100, then
83 * x needs 17 bits. The packet size is 16 bits, so we can easily
84 * overflow if we do not allow errors.
85 * So we use a key "dn_key" which is 64 bits. Some macros are used to
86 * compare key values and handle wraparounds.
87 * MAX64 returns the largest of two key values.
88 * MY_M is used as a shift count when doing fixed point arithmetic
89 * (a better name would be useful...).
90 */
91 typedef u_int64_t dn_key ; /* sorting key */
92 #define DN_KEY_LT(a,b) ((int64_t)((a)-(b)) < 0)
93 #define DN_KEY_LEQ(a,b) ((int64_t)((a)-(b)) <= 0)
94 #define DN_KEY_GT(a,b) ((int64_t)((a)-(b)) > 0)
95 #define DN_KEY_GEQ(a,b) ((int64_t)((a)-(b)) >= 0)
96 #define MAX64(x,y) (( (int64_t) ( (y)-(x) )) > 0 ) ? (y) : (x)
97 #define MY_M 16 /* number of left shift to obtain a larger precision */
98
99 /*
100 * XXX With this scaling, max 1000 flows, max weight 100, 1Gbit/s, the
101 * virtual time wraps every 15 days.
102 */
103
104 /*
105 * The OFFSET_OF macro is used to return the offset of a field within
106 * a structure. It is used by the heap management routines.
107 */
108 #define OFFSET_OF(type, field) ((int)&( ((type *)0)->field) )
109
110 /*
111 * The maximum hash table size for queues. This value must be a power
112 * of 2.
113 */
114 #define DN_MAX_HASH_SIZE 65536
115
116 /*
117 * A heap entry is made of a key and a pointer to the actual
118 * object stored in the heap.
119 * The heap is an array of dn_heap_entry entries, dynamically allocated.
120 * Current size is "size", with "elements" actually in use.
121 * The heap normally supports only ordered insert and extract from the top.
122 * If we want to extract an object from the middle of the heap, we
123 * have to know where the object itself is located in the heap (or we
124 * need to scan the whole array). To this purpose, an object has a
125 * field (int) which contains the index of the object itself into the
126 * heap. When the object is moved, the field must also be updated.
127 * The offset of the index in the object is stored in the 'offset'
128 * field in the heap descriptor. The assumption is that this offset
129 * is non-zero if we want to support extract from the middle.
130 */
131 struct dn_heap_entry {
132 dn_key key ; /* sorting key. Topmost element is smallest one */
133 void *object ; /* object pointer */
134 } ;
135
136 struct dn_heap {
137 int size ;
138 int elements ;
139 int offset ; /* XXX if > 0 this is the offset of direct ptr to obj */
140 struct dn_heap_entry *p ; /* really an array of "size" entries */
141 } ;
142
143 /*
144 * Packets processed by dummynet have an mbuf tag associated with
145 * them that carries their dummynet state. This is used within
146 * the dummynet code as well as outside when checking for special
147 * processing requirements.
148 */
149 #ifdef KERNEL
150 struct dn_pkt_tag {
151 struct ip_fw *rule; /* matching rule */
152 int dn_dir; /* action when packet comes out. */
153 #define DN_TO_IP_OUT 1
154 #define DN_TO_IP_IN 2
155 #define DN_TO_BDG_FWD 3
156
157 dn_key output_time; /* when the pkt is due for delivery */
158 struct ifnet *ifp; /* interface, for ip_output */
159 struct sockaddr_in *dn_dst ;
160 struct route ro; /* route, for ip_output. MUST COPY */
161 int flags ; /* flags, for ip_output (IPv6 ?) */
162 };
163 #else
164 struct dn_pkt;
165 #endif /* KERNEL */
166
167 /*
168 * Overall structure of dummynet (with WF2Q+):
169
170 In dummynet, packets are selected with the firewall rules, and passed
171 to two different objects: PIPE or QUEUE.
172
173 A QUEUE is just a queue with configurable size and queue management
174 policy. It is also associated with a mask (to discriminate among
175 different flows), a weight (used to give different shares of the
176 bandwidth to different flows) and a "pipe", which essentially
177 supplies the transmit clock for all queues associated with that
178 pipe.
179
180 A PIPE emulates a fixed-bandwidth link, whose bandwidth is
181 configurable. The "clock" for a pipe can come from either an
182 internal timer, or from the transmit interrupt of an interface.
183 A pipe is also associated with one (or more, if masks are used)
184 queue, where all packets for that pipe are stored.
185
186 The bandwidth available on the pipe is shared by the queues
187 associated with that pipe (only one in case the packet is sent
188 to a PIPE) according to the WF2Q+ scheduling algorithm and the
189 configured weights.
190
191 In general, incoming packets are stored in the appropriate queue,
192 which is then placed into one of a few heaps managed by a scheduler
193 to decide when the packet should be extracted.
194 The scheduler (a function called dummynet()) is run at every timer
195 tick, and grabs queues from the head of the heaps when they are
196 ready for processing.
197
198 There are three data structures definining a pipe and associated queues:
199
200 + dn_pipe, which contains the main configuration parameters related
201 to delay and bandwidth;
202 + dn_flow_set, which contains WF2Q+ configuration, flow
203 masks, plr and RED configuration;
204 + dn_flow_queue, which is the per-flow queue (containing the packets)
205
206 Multiple dn_flow_set can be linked to the same pipe, and multiple
207 dn_flow_queue can be linked to the same dn_flow_set.
208 All data structures are linked in a linear list which is used for
209 housekeeping purposes.
210
211 During configuration, we create and initialize the dn_flow_set
212 and dn_pipe structures (a dn_pipe also contains a dn_flow_set).
213
214 At runtime: packets are sent to the appropriate dn_flow_set (either
215 WFQ ones, or the one embedded in the dn_pipe for fixed-rate flows),
216 which in turn dispatches them to the appropriate dn_flow_queue
217 (created dynamically according to the masks).
218
219 The transmit clock for fixed rate flows (ready_event()) selects the
220 dn_flow_queue to be used to transmit the next packet. For WF2Q,
221 wfq_ready_event() extract a pipe which in turn selects the right
222 flow using a number of heaps defined into the pipe itself.
223
224 *
225 */
226
227 /*
228 * per flow queue. This contains the flow identifier, the queue
229 * of packets, counters, and parameters used to support both RED and
230 * WF2Q+.
231 *
232 * A dn_flow_queue is created and initialized whenever a packet for
233 * a new flow arrives.
234 */
235 struct dn_flow_queue {
236 struct dn_flow_queue *next ;
237 struct ipfw_flow_id id ;
238
239 struct mbuf *head, *tail ; /* queue of packets */
240 u_int len ;
241 u_int len_bytes ;
242 u_long numbytes ; /* credit for transmission (dynamic queues) */
243
244 u_int64_t tot_pkts ; /* statistics counters */
245 u_int64_t tot_bytes ;
246 u_int32_t drops ;
247
248 int hash_slot ; /* debugging/diagnostic */
249
250 /* RED parameters */
251 int avg ; /* average queue length est. (scaled) */
252 int count ; /* arrivals since last RED drop */
253 int random ; /* random value (scaled) */
254 u_int32_t q_time ; /* start of queue idle time */
255
256 /* WF2Q+ support */
257 struct dn_flow_set *fs ; /* parent flow set */
258 int heap_pos ; /* position (index) of struct in heap */
259 dn_key sched_time ; /* current time when queue enters ready_heap */
260
261 dn_key S,F ; /* start time, finish time */
262 /*
263 * Setting F < S means the timestamp is invalid. We only need
264 * to test this when the queue is empty.
265 */
266 } ;
267
268 /*
269 * flow_set descriptor. Contains the "template" parameters for the
270 * queue configuration, and pointers to the hash table of dn_flow_queue's.
271 *
272 * The hash table is an array of lists -- we identify the slot by
273 * hashing the flow-id, then scan the list looking for a match.
274 * The size of the hash table (buckets) is configurable on a per-queue
275 * basis.
276 *
277 * A dn_flow_set is created whenever a new queue or pipe is created (in the
278 * latter case, the structure is located inside the struct dn_pipe).
279 */
280 struct dn_flow_set {
281 struct dn_flow_set *next; /* next flow set in all_flow_sets list */
282
283 u_short fs_nr ; /* flow_set number */
284 u_short flags_fs;
285 #define DN_HAVE_FLOW_MASK 0x0001
286 #define DN_IS_RED 0x0002
287 #define DN_IS_GENTLE_RED 0x0004
288 #define DN_QSIZE_IS_BYTES 0x0008 /* queue size is measured in bytes */
289 #define DN_NOERROR 0x0010 /* do not report ENOBUFS on drops */
290 #define DN_IS_PIPE 0x4000
291 #define DN_IS_QUEUE 0x8000
292
293 struct dn_pipe *pipe ; /* pointer to parent pipe */
294 u_short parent_nr ; /* parent pipe#, 0 if local to a pipe */
295
296 int weight ; /* WFQ queue weight */
297 int qsize ; /* queue size in slots or bytes */
298 int plr ; /* pkt loss rate (2^31-1 means 100%) */
299
300 struct ipfw_flow_id flow_mask ;
301
302 /* hash table of queues onto this flow_set */
303 int rq_size ; /* number of slots */
304 int rq_elements ; /* active elements */
305 struct dn_flow_queue **rq; /* array of rq_size entries */
306
307 u_int32_t last_expired ; /* do not expire too frequently */
308 int backlogged ; /* #active queues for this flowset */
309
310 /* RED parameters */
311 #define SCALE_RED 16
312 #define SCALE(x) ( (x) << SCALE_RED )
313 #define SCALE_VAL(x) ( (x) >> SCALE_RED )
314 #define SCALE_MUL(x,y) ( ( (x) * (y) ) >> SCALE_RED )
315 int w_q ; /* queue weight (scaled) */
316 int max_th ; /* maximum threshold for queue (scaled) */
317 int min_th ; /* minimum threshold for queue (scaled) */
318 int max_p ; /* maximum value for p_b (scaled) */
319 u_int c_1 ; /* max_p/(max_th-min_th) (scaled) */
320 u_int c_2 ; /* max_p*min_th/(max_th-min_th) (scaled) */
321 u_int c_3 ; /* for GRED, (1-max_p)/max_th (scaled) */
322 u_int c_4 ; /* for GRED, 1 - 2*max_p (scaled) */
323 u_int * w_q_lookup ; /* lookup table for computing (1-w_q)^t */
324 u_int lookup_depth ; /* depth of lookup table */
325 int lookup_step ; /* granularity inside the lookup table */
326 int lookup_weight ; /* equal to (1-w_q)^t / (1-w_q)^(t+1) */
327 int avg_pkt_size ; /* medium packet size */
328 int max_pkt_size ; /* max packet size */
329 } ;
330
331 /*
332 * Pipe descriptor. Contains global parameters, delay-line queue,
333 * and the flow_set used for fixed-rate queues.
334 *
335 * For WF2Q+ support it also has 3 heaps holding dn_flow_queue:
336 * not_eligible_heap, for queues whose start time is higher
337 * than the virtual time. Sorted by start time.
338 * scheduler_heap, for queues eligible for scheduling. Sorted by
339 * finish time.
340 * idle_heap, all flows that are idle and can be removed. We
341 * do that on each tick so we do not slow down too much
342 * operations during forwarding.
343 *
344 */
345 struct dn_pipe { /* a pipe */
346 struct dn_pipe *next ;
347
348 int pipe_nr ; /* number */
349 int bandwidth; /* really, bytes/tick. */
350 int delay ; /* really, ticks */
351
352 struct mbuf *head, *tail ; /* packets in delay line */
353
354 /* WF2Q+ */
355 struct dn_heap scheduler_heap ; /* top extract - key Finish time*/
356 struct dn_heap not_eligible_heap; /* top extract- key Start time */
357 struct dn_heap idle_heap ; /* random extract - key Start=Finish time */
358
359 dn_key V ; /* virtual time */
360 int sum; /* sum of weights of all active sessions */
361 int numbytes; /* bits I can transmit (more or less). */
362
363 dn_key sched_time ; /* time pipe was scheduled in ready_heap */
364
365 /*
366 * When the tx clock come from an interface (if_name[0] != '\0'), its name
367 * is stored below, whereas the ifp is filled when the rule is configured.
368 */
369 char if_name[IFNAMSIZ];
370 struct ifnet *ifp ;
371 int ready ; /* set if ifp != NULL and we got a signal from it */
372
373 struct dn_flow_set fs ; /* used with fixed-rate flows */
374 };
375
376 #ifdef KERNEL
377
378 void ip_dn_init(void); /* called from raw_ip.c:load_ipfw() */
379
380 typedef int ip_dn_ctl_t(struct sockopt *); /* raw_ip.c */
381 typedef void ip_dn_ruledel_t(void *); /* ip_fw.c */
382 typedef int ip_dn_io_t(struct mbuf *m, int pipe_nr, int dir,
383 struct ip_fw_args *fwa);
384 extern ip_dn_ctl_t *ip_dn_ctl_ptr;
385 extern ip_dn_ruledel_t *ip_dn_ruledel_ptr;
386 extern ip_dn_io_t *ip_dn_io_ptr;
387 #define DUMMYNET_LOADED (ip_dn_io_ptr != NULL)
388
389 /*
390 * Return the IPFW rule associated with the dummynet tag; if any.
391 * Make sure that the dummynet tag is not reused by lower layers.
392 */
393 static __inline struct ip_fw *
394 ip_dn_claim_rule(struct mbuf *m)
395 {
396 struct m_tag *mtag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
397 KERNEL_TAG_TYPE_DUMMYNET, NULL);
398 if (mtag != NULL) {
399 mtag->m_tag_type = KERNEL_TAG_TYPE_NONE;
400 return (((struct dn_pkt_tag *)(mtag+1))->rule);
401 } else
402 return (NULL);
403 }
404 #endif /* KERNEL */
405
406 #endif /* PRIVATE */
407 #endif /* _IP_DUMMYNET_H */