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