]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2013 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
91447636 A |
28 | /* |
29 | * Copyright (c) 1998-2002 Luigi Rizzo, Universita` di Pisa | |
9bccf70c A |
30 | * Portions Copyright (c) 2000 Akamba Corp. |
31 | * All rights reserved | |
1c79356b | 32 | * |
9bccf70c A |
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. | |
1c79356b | 41 | * |
9bccf70c A |
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. | |
1c79356b | 53 | * |
91447636 | 54 | * $FreeBSD: src/sys/netinet/ip_dummynet.c,v 1.84 2004/08/25 09:31:30 pjd Exp $ |
1c79356b A |
55 | */ |
56 | ||
91447636 | 57 | #define DUMMYNET_DEBUG |
9bccf70c | 58 | |
1c79356b A |
59 | /* |
60 | * This module implements IP dummynet, a bandwidth limiter/delay emulator | |
61 | * used in conjunction with the ipfw package. | |
9bccf70c A |
62 | * Description of the data structures used is in ip_dummynet.h |
63 | * Here you mainly find the following blocks of code: | |
64 | * + variable declarations; | |
65 | * + heap management functions; | |
66 | * + scheduler and dummynet functions; | |
67 | * + configuration and initialization. | |
68 | * | |
91447636 | 69 | * NOTA BENE: critical sections are protected by the "dummynet lock". |
1c79356b | 70 | * |
9bccf70c | 71 | * Most important Changes: |
1c79356b | 72 | * |
9bccf70c A |
73 | * 010124: Fixed WF2Q behaviour |
74 | * 010122: Fixed spl protection. | |
75 | * 000601: WF2Q support | |
76 | * 000106: large rewrite, use heaps to handle very many pipes. | |
1c79356b | 77 | * 980513: initial release |
9bccf70c A |
78 | * |
79 | * include files marked with XXX are probably not needed | |
1c79356b A |
80 | */ |
81 | ||
1c79356b A |
82 | #include <sys/param.h> |
83 | #include <sys/systm.h> | |
84 | #include <sys/malloc.h> | |
85 | #include <sys/mbuf.h> | |
86 | #include <sys/queue.h> /* XXX */ | |
87 | #include <sys/kernel.h> | |
88 | #include <sys/socket.h> | |
89 | #include <sys/socketvar.h> | |
90 | #include <sys/time.h> | |
91 | #include <sys/sysctl.h> | |
92 | #include <net/if.h> | |
93 | #include <net/route.h> | |
91447636 | 94 | #include <net/kpi_protocol.h> |
316670eb A |
95 | #if DUMMYNET |
96 | #include <net/kpi_protocol.h> | |
97 | #endif /* DUMMYNET */ | |
1c79356b A |
98 | #include <netinet/in.h> |
99 | #include <netinet/in_systm.h> | |
100 | #include <netinet/in_var.h> | |
101 | #include <netinet/ip.h> | |
102 | #include <netinet/ip_fw.h> | |
103 | #include <netinet/ip_dummynet.h> | |
104 | #include <netinet/ip_var.h> | |
105 | ||
316670eb A |
106 | #include <netinet/ip6.h> /* for ip6_input, ip6_output prototypes */ |
107 | #include <netinet6/ip6_var.h> | |
108 | ||
109 | static struct ip_fw default_rule; | |
110 | ||
9bccf70c A |
111 | /* |
112 | * We keep a private variable for the simulation time, but we could | |
113 | * probably use an existing one ("softticks" in sys/kern/kern_timer.c) | |
114 | */ | |
115 | static dn_key curr_time = 0 ; /* current simulation time */ | |
116 | ||
0c530ab8 A |
117 | /* this is for the timer that fires to call dummynet() - we only enable the timer when |
118 | there are packets to process, otherwise it's disabled */ | |
119 | static int timer_enabled = 0; | |
120 | ||
9bccf70c A |
121 | static int dn_hash_size = 64 ; /* default hash size */ |
122 | ||
123 | /* statistics on number of queue searches and search steps */ | |
124 | static int searches, search_steps ; | |
125 | static int pipe_expire = 1 ; /* expire queue if empty */ | |
126 | static int dn_max_ratio = 16 ; /* max queues/buckets ratio */ | |
127 | ||
128 | static int red_lookup_depth = 256; /* RED - default lookup table depth */ | |
129 | static int red_avg_pkt_size = 512; /* RED - default medium packet size */ | |
130 | static int red_max_pkt_size = 1500; /* RED - default max packet size */ | |
131 | ||
6d2010ae A |
132 | static int serialize = 0; |
133 | ||
9bccf70c A |
134 | /* |
135 | * Three heaps contain queues and pipes that the scheduler handles: | |
136 | * | |
137 | * ready_heap contains all dn_flow_queue related to fixed-rate pipes. | |
138 | * | |
139 | * wfq_ready_heap contains the pipes associated with WF2Q flows | |
140 | * | |
141 | * extract_heap contains pipes associated with delay lines. | |
142 | * | |
143 | */ | |
144 | static struct dn_heap ready_heap, extract_heap, wfq_ready_heap ; | |
145 | ||
146 | static int heap_init(struct dn_heap *h, int size) ; | |
147 | static int heap_insert (struct dn_heap *h, dn_key key1, void *p); | |
148 | static void heap_extract(struct dn_heap *h, void *obj); | |
149 | ||
9bccf70c | 150 | |
b0d623f7 A |
151 | static void transmit_event(struct dn_pipe *pipe, struct mbuf **head, |
152 | struct mbuf **tail); | |
153 | static void ready_event(struct dn_flow_queue *q, struct mbuf **head, | |
154 | struct mbuf **tail); | |
155 | static void ready_event_wfq(struct dn_pipe *p, struct mbuf **head, | |
156 | struct mbuf **tail); | |
157 | ||
158 | /* | |
159 | * Packets are retrieved from queues in Dummynet in chains instead of | |
160 | * packet-by-packet. The entire list of packets is first dequeued and | |
161 | * sent out by the following function. | |
162 | */ | |
163 | static void dummynet_send(struct mbuf *m); | |
164 | ||
b0d623f7 A |
165 | #define HASHSIZE 16 |
166 | #define HASH(num) ((((num) >> 8) ^ ((num) >> 4) ^ (num)) & 0x0f) | |
167 | static struct dn_pipe_head pipehash[HASHSIZE]; /* all pipes */ | |
168 | static struct dn_flow_set_head flowsethash[HASHSIZE]; /* all flowsets */ | |
169 | ||
1c79356b | 170 | |
91447636 | 171 | #ifdef SYSCTL_NODE |
9bccf70c | 172 | SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet, |
6d2010ae | 173 | CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Dummynet"); |
9bccf70c | 174 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size, |
6d2010ae | 175 | CTLFLAG_RW | CTLFLAG_LOCKED, &dn_hash_size, 0, "Default hash table size"); |
b0d623f7 | 176 | SYSCTL_QUAD(_net_inet_ip_dummynet, OID_AUTO, curr_time, |
6d2010ae | 177 | CTLFLAG_RD | CTLFLAG_LOCKED, &curr_time, "Current tick"); |
9bccf70c | 178 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, ready_heap, |
6d2010ae | 179 | CTLFLAG_RD | CTLFLAG_LOCKED, &ready_heap.size, 0, "Size of ready heap"); |
9bccf70c | 180 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, extract_heap, |
6d2010ae | 181 | CTLFLAG_RD | CTLFLAG_LOCKED, &extract_heap.size, 0, "Size of extract heap"); |
9bccf70c | 182 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, searches, |
6d2010ae | 183 | CTLFLAG_RD | CTLFLAG_LOCKED, &searches, 0, "Number of queue searches"); |
9bccf70c | 184 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, search_steps, |
6d2010ae | 185 | CTLFLAG_RD | CTLFLAG_LOCKED, &search_steps, 0, "Number of queue search steps"); |
9bccf70c | 186 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire, |
6d2010ae | 187 | CTLFLAG_RW | CTLFLAG_LOCKED, &pipe_expire, 0, "Expire queue if empty"); |
9bccf70c | 188 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, max_chain_len, |
6d2010ae | 189 | CTLFLAG_RW | CTLFLAG_LOCKED, &dn_max_ratio, 0, |
9bccf70c A |
190 | "Max ratio between dynamic queues and buckets"); |
191 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth, | |
6d2010ae | 192 | CTLFLAG_RD | CTLFLAG_LOCKED, &red_lookup_depth, 0, "Depth of RED lookup table"); |
9bccf70c | 193 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size, |
6d2010ae | 194 | CTLFLAG_RD | CTLFLAG_LOCKED, &red_avg_pkt_size, 0, "RED Medium packet size"); |
9bccf70c | 195 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size, |
6d2010ae | 196 | CTLFLAG_RD | CTLFLAG_LOCKED, &red_max_pkt_size, 0, "RED Max packet size"); |
1c79356b A |
197 | #endif |
198 | ||
91447636 A |
199 | #ifdef DUMMYNET_DEBUG |
200 | int dummynet_debug = 0; | |
201 | #ifdef SYSCTL_NODE | |
6d2010ae | 202 | SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED, &dummynet_debug, |
91447636 A |
203 | 0, "control debugging printfs"); |
204 | #endif | |
205 | #define DPRINTF(X) if (dummynet_debug) printf X | |
206 | #else | |
207 | #define DPRINTF(X) | |
208 | #endif | |
209 | ||
2d21ac55 A |
210 | /* contrary to the comment above random(), it does not actually |
211 | * return a value [0, 2^31 - 1], which breaks plr amongst other | |
212 | * things. Masking it should work even if the behavior of | |
213 | * the function is fixed. | |
214 | */ | |
215 | #define MY_RANDOM (random() & 0x7FFFFFFF) | |
216 | ||
91447636 | 217 | /* dummynet lock */ |
b0d623f7 A |
218 | static lck_grp_t *dn_mutex_grp; |
219 | static lck_grp_attr_t *dn_mutex_grp_attr; | |
220 | static lck_attr_t *dn_mutex_attr; | |
316670eb A |
221 | decl_lck_mtx_data(static, dn_mutex_data); |
222 | static lck_mtx_t *dn_mutex = &dn_mutex_data; | |
91447636 | 223 | |
9bccf70c | 224 | static int config_pipe(struct dn_pipe *p); |
1c79356b A |
225 | static int ip_dn_ctl(struct sockopt *sopt); |
226 | ||
1c79356b | 227 | static void dummynet(void *); |
1c79356b | 228 | static void dummynet_flush(void); |
9bccf70c | 229 | void dummynet_drain(void); |
91447636 | 230 | static ip_dn_io_t dummynet_io; |
1c79356b | 231 | |
91447636 | 232 | int if_tx_rdy(struct ifnet *ifp); |
1c79356b | 233 | |
b0d623f7 A |
234 | static void cp_flow_set_to_64_user(struct dn_flow_set *set, struct dn_flow_set_64 *fs_bp); |
235 | static void cp_queue_to_64_user( struct dn_flow_queue *q, struct dn_flow_queue_64 *qp); | |
236 | static char *cp_pipe_to_64_user(struct dn_pipe *p, struct dn_pipe_64 *pipe_bp); | |
237 | static char* dn_copy_set_64(struct dn_flow_set *set, char *bp); | |
238 | static int cp_pipe_from_user_64( struct sockopt *sopt, struct dn_pipe *p ); | |
239 | ||
240 | static void cp_flow_set_to_32_user(struct dn_flow_set *set, struct dn_flow_set_32 *fs_bp); | |
241 | static void cp_queue_to_32_user( struct dn_flow_queue *q, struct dn_flow_queue_32 *qp); | |
242 | static char *cp_pipe_to_32_user(struct dn_pipe *p, struct dn_pipe_32 *pipe_bp); | |
243 | static char* dn_copy_set_32(struct dn_flow_set *set, char *bp); | |
244 | static int cp_pipe_from_user_32( struct sockopt *sopt, struct dn_pipe *p ); | |
245 | ||
246 | ||
9bccf70c A |
247 | /* |
248 | * Heap management functions. | |
249 | * | |
250 | * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2. | |
251 | * Some macros help finding parent/children so we can optimize them. | |
252 | * | |
253 | * heap_init() is called to expand the heap when needed. | |
254 | * Increment size in blocks of 16 entries. | |
255 | * XXX failure to allocate a new element is a pretty bad failure | |
256 | * as we basically stall a whole queue forever!! | |
257 | * Returns 1 on error, 0 on success | |
258 | */ | |
259 | #define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 ) | |
260 | #define HEAP_LEFT(x) ( 2*(x) + 1 ) | |
261 | #define HEAP_IS_LEFT(x) ( (x) & 1 ) | |
262 | #define HEAP_RIGHT(x) ( 2*(x) + 2 ) | |
263 | #define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; } | |
264 | #define HEAP_INCREMENT 15 | |
265 | ||
b0d623f7 A |
266 | |
267 | int cp_pipe_from_user_32( struct sockopt *sopt, struct dn_pipe *p ) | |
268 | { | |
269 | struct dn_pipe_32 user_pipe_32; | |
270 | int error=0; | |
271 | ||
272 | error = sooptcopyin(sopt, &user_pipe_32, sizeof(struct dn_pipe_32), sizeof(struct dn_pipe_32)); | |
273 | if ( !error ){ | |
274 | p->pipe_nr = user_pipe_32.pipe_nr; | |
275 | p->bandwidth = user_pipe_32.bandwidth; | |
276 | p->delay = user_pipe_32.delay; | |
277 | p->V = user_pipe_32.V; | |
278 | p->sum = user_pipe_32.sum; | |
279 | p->numbytes = user_pipe_32.numbytes; | |
280 | p->sched_time = user_pipe_32.sched_time; | |
281 | bcopy( user_pipe_32.if_name, p->if_name, IFNAMSIZ); | |
282 | p->ready = user_pipe_32.ready; | |
283 | ||
284 | p->fs.fs_nr = user_pipe_32.fs.fs_nr; | |
285 | p->fs.flags_fs = user_pipe_32.fs.flags_fs; | |
286 | p->fs.parent_nr = user_pipe_32.fs.parent_nr; | |
287 | p->fs.weight = user_pipe_32.fs.weight; | |
288 | p->fs.qsize = user_pipe_32.fs.qsize; | |
289 | p->fs.plr = user_pipe_32.fs.plr; | |
290 | p->fs.flow_mask = user_pipe_32.fs.flow_mask; | |
291 | p->fs.rq_size = user_pipe_32.fs.rq_size; | |
292 | p->fs.rq_elements = user_pipe_32.fs.rq_elements; | |
293 | p->fs.last_expired = user_pipe_32.fs.last_expired; | |
294 | p->fs.backlogged = user_pipe_32.fs.backlogged; | |
295 | p->fs.w_q = user_pipe_32.fs.w_q; | |
296 | p->fs.max_th = user_pipe_32.fs.max_th; | |
297 | p->fs.min_th = user_pipe_32.fs.min_th; | |
298 | p->fs.max_p = user_pipe_32.fs.max_p; | |
299 | p->fs.c_1 = user_pipe_32.fs.c_1; | |
300 | p->fs.c_2 = user_pipe_32.fs.c_2; | |
301 | p->fs.c_3 = user_pipe_32.fs.c_3; | |
302 | p->fs.c_4 = user_pipe_32.fs.c_4; | |
303 | p->fs.lookup_depth = user_pipe_32.fs.lookup_depth; | |
304 | p->fs.lookup_step = user_pipe_32.fs.lookup_step; | |
305 | p->fs.lookup_weight = user_pipe_32.fs.lookup_weight; | |
306 | p->fs.avg_pkt_size = user_pipe_32.fs.avg_pkt_size; | |
307 | p->fs.max_pkt_size = user_pipe_32.fs.max_pkt_size; | |
308 | } | |
309 | return error; | |
310 | } | |
311 | ||
312 | ||
313 | int cp_pipe_from_user_64( struct sockopt *sopt, struct dn_pipe *p ) | |
314 | { | |
315 | struct dn_pipe_64 user_pipe_64; | |
316 | int error=0; | |
317 | ||
318 | error = sooptcopyin(sopt, &user_pipe_64, sizeof(struct dn_pipe_64), sizeof(struct dn_pipe_64)); | |
319 | if ( !error ){ | |
320 | p->pipe_nr = user_pipe_64.pipe_nr; | |
321 | p->bandwidth = user_pipe_64.bandwidth; | |
322 | p->delay = user_pipe_64.delay; | |
323 | p->V = user_pipe_64.V; | |
324 | p->sum = user_pipe_64.sum; | |
325 | p->numbytes = user_pipe_64.numbytes; | |
326 | p->sched_time = user_pipe_64.sched_time; | |
327 | bcopy( user_pipe_64.if_name, p->if_name, IFNAMSIZ); | |
328 | p->ready = user_pipe_64.ready; | |
329 | ||
330 | p->fs.fs_nr = user_pipe_64.fs.fs_nr; | |
331 | p->fs.flags_fs = user_pipe_64.fs.flags_fs; | |
332 | p->fs.parent_nr = user_pipe_64.fs.parent_nr; | |
333 | p->fs.weight = user_pipe_64.fs.weight; | |
334 | p->fs.qsize = user_pipe_64.fs.qsize; | |
335 | p->fs.plr = user_pipe_64.fs.plr; | |
336 | p->fs.flow_mask = user_pipe_64.fs.flow_mask; | |
337 | p->fs.rq_size = user_pipe_64.fs.rq_size; | |
338 | p->fs.rq_elements = user_pipe_64.fs.rq_elements; | |
339 | p->fs.last_expired = user_pipe_64.fs.last_expired; | |
340 | p->fs.backlogged = user_pipe_64.fs.backlogged; | |
341 | p->fs.w_q = user_pipe_64.fs.w_q; | |
342 | p->fs.max_th = user_pipe_64.fs.max_th; | |
343 | p->fs.min_th = user_pipe_64.fs.min_th; | |
344 | p->fs.max_p = user_pipe_64.fs.max_p; | |
345 | p->fs.c_1 = user_pipe_64.fs.c_1; | |
346 | p->fs.c_2 = user_pipe_64.fs.c_2; | |
347 | p->fs.c_3 = user_pipe_64.fs.c_3; | |
348 | p->fs.c_4 = user_pipe_64.fs.c_4; | |
349 | p->fs.lookup_depth = user_pipe_64.fs.lookup_depth; | |
350 | p->fs.lookup_step = user_pipe_64.fs.lookup_step; | |
351 | p->fs.lookup_weight = user_pipe_64.fs.lookup_weight; | |
352 | p->fs.avg_pkt_size = user_pipe_64.fs.avg_pkt_size; | |
353 | p->fs.max_pkt_size = user_pipe_64.fs.max_pkt_size; | |
354 | } | |
355 | return error; | |
356 | } | |
357 | ||
358 | static void | |
359 | cp_flow_set_to_32_user(struct dn_flow_set *set, struct dn_flow_set_32 *fs_bp) | |
360 | { | |
361 | fs_bp->fs_nr = set->fs_nr; | |
362 | fs_bp->flags_fs = set->flags_fs ; | |
363 | fs_bp->parent_nr = set->parent_nr ; | |
364 | fs_bp->weight = set->weight ; | |
365 | fs_bp->qsize = set->qsize ; | |
366 | fs_bp->plr = set->plr ; | |
367 | fs_bp->flow_mask = set->flow_mask ; | |
368 | fs_bp->rq_size = set->rq_size ; | |
369 | fs_bp->rq_elements = set->rq_elements ; | |
370 | fs_bp->last_expired = set->last_expired ; | |
371 | fs_bp->backlogged = set->backlogged ; | |
372 | fs_bp->w_q = set->w_q ; | |
373 | fs_bp->max_th = set->max_th ; | |
374 | fs_bp->min_th = set->min_th ; | |
375 | fs_bp->max_p = set->max_p ; | |
376 | fs_bp->c_1 = set->c_1 ; | |
377 | fs_bp->c_2 = set->c_2 ; | |
378 | fs_bp->c_3 = set->c_3 ; | |
379 | fs_bp->c_4 = set->c_4 ; | |
380 | fs_bp->w_q_lookup = CAST_DOWN_EXPLICIT(user32_addr_t, set->w_q_lookup) ; | |
381 | fs_bp->lookup_depth = set->lookup_depth ; | |
382 | fs_bp->lookup_step = set->lookup_step ; | |
383 | fs_bp->lookup_weight = set->lookup_weight ; | |
384 | fs_bp->avg_pkt_size = set->avg_pkt_size ; | |
385 | fs_bp->max_pkt_size = set->max_pkt_size ; | |
386 | } | |
387 | ||
388 | static void | |
389 | cp_flow_set_to_64_user(struct dn_flow_set *set, struct dn_flow_set_64 *fs_bp) | |
390 | { | |
391 | fs_bp->fs_nr = set->fs_nr; | |
392 | fs_bp->flags_fs = set->flags_fs ; | |
393 | fs_bp->parent_nr = set->parent_nr ; | |
394 | fs_bp->weight = set->weight ; | |
395 | fs_bp->qsize = set->qsize ; | |
396 | fs_bp->plr = set->plr ; | |
397 | fs_bp->flow_mask = set->flow_mask ; | |
398 | fs_bp->rq_size = set->rq_size ; | |
399 | fs_bp->rq_elements = set->rq_elements ; | |
400 | fs_bp->last_expired = set->last_expired ; | |
401 | fs_bp->backlogged = set->backlogged ; | |
402 | fs_bp->w_q = set->w_q ; | |
403 | fs_bp->max_th = set->max_th ; | |
404 | fs_bp->min_th = set->min_th ; | |
405 | fs_bp->max_p = set->max_p ; | |
406 | fs_bp->c_1 = set->c_1 ; | |
407 | fs_bp->c_2 = set->c_2 ; | |
408 | fs_bp->c_3 = set->c_3 ; | |
409 | fs_bp->c_4 = set->c_4 ; | |
410 | fs_bp->w_q_lookup = CAST_DOWN(user64_addr_t, set->w_q_lookup) ; | |
411 | fs_bp->lookup_depth = set->lookup_depth ; | |
412 | fs_bp->lookup_step = set->lookup_step ; | |
413 | fs_bp->lookup_weight = set->lookup_weight ; | |
414 | fs_bp->avg_pkt_size = set->avg_pkt_size ; | |
415 | fs_bp->max_pkt_size = set->max_pkt_size ; | |
416 | } | |
417 | ||
418 | static | |
419 | void cp_queue_to_32_user( struct dn_flow_queue *q, struct dn_flow_queue_32 *qp) | |
420 | { | |
421 | qp->id = q->id; | |
422 | qp->len = q->len; | |
423 | qp->len_bytes = q->len_bytes; | |
424 | qp->numbytes = q->numbytes; | |
425 | qp->tot_pkts = q->tot_pkts; | |
426 | qp->tot_bytes = q->tot_bytes; | |
427 | qp->drops = q->drops; | |
428 | qp->hash_slot = q->hash_slot; | |
429 | qp->avg = q->avg; | |
430 | qp->count = q->count; | |
431 | qp->random = q->random; | |
432 | qp->q_time = q->q_time; | |
433 | qp->heap_pos = q->heap_pos; | |
434 | qp->sched_time = q->sched_time; | |
435 | qp->S = q->S; | |
436 | qp->F = q->F; | |
437 | } | |
438 | ||
439 | static | |
440 | void cp_queue_to_64_user( struct dn_flow_queue *q, struct dn_flow_queue_64 *qp) | |
441 | { | |
442 | qp->id = q->id; | |
443 | qp->len = q->len; | |
444 | qp->len_bytes = q->len_bytes; | |
445 | qp->numbytes = q->numbytes; | |
446 | qp->tot_pkts = q->tot_pkts; | |
447 | qp->tot_bytes = q->tot_bytes; | |
448 | qp->drops = q->drops; | |
449 | qp->hash_slot = q->hash_slot; | |
450 | qp->avg = q->avg; | |
451 | qp->count = q->count; | |
452 | qp->random = q->random; | |
453 | qp->q_time = q->q_time; | |
454 | qp->heap_pos = q->heap_pos; | |
455 | qp->sched_time = q->sched_time; | |
456 | qp->S = q->S; | |
457 | qp->F = q->F; | |
458 | } | |
459 | ||
460 | static | |
461 | char *cp_pipe_to_32_user(struct dn_pipe *p, struct dn_pipe_32 *pipe_bp) | |
462 | { | |
463 | char *bp; | |
464 | ||
465 | pipe_bp->pipe_nr = p->pipe_nr; | |
466 | pipe_bp->bandwidth = p->bandwidth; | |
6d2010ae | 467 | pipe_bp->delay = p->delay; |
b0d623f7 A |
468 | bcopy( &(p->scheduler_heap), &(pipe_bp->scheduler_heap), sizeof(struct dn_heap_32)); |
469 | pipe_bp->scheduler_heap.p = CAST_DOWN_EXPLICIT(user32_addr_t, pipe_bp->scheduler_heap.p); | |
470 | bcopy( &(p->not_eligible_heap), &(pipe_bp->not_eligible_heap), sizeof(struct dn_heap_32)); | |
471 | pipe_bp->not_eligible_heap.p = CAST_DOWN_EXPLICIT(user32_addr_t, pipe_bp->not_eligible_heap.p); | |
472 | bcopy( &(p->idle_heap), &(pipe_bp->idle_heap), sizeof(struct dn_heap_32)); | |
473 | pipe_bp->idle_heap.p = CAST_DOWN_EXPLICIT(user32_addr_t, pipe_bp->idle_heap.p); | |
474 | pipe_bp->V = p->V; | |
475 | pipe_bp->sum = p->sum; | |
476 | pipe_bp->numbytes = p->numbytes; | |
477 | pipe_bp->sched_time = p->sched_time; | |
478 | bcopy( p->if_name, pipe_bp->if_name, IFNAMSIZ); | |
479 | pipe_bp->ifp = CAST_DOWN_EXPLICIT(user32_addr_t, p->ifp); | |
480 | pipe_bp->ready = p->ready; | |
481 | ||
482 | cp_flow_set_to_32_user( &(p->fs), &(pipe_bp->fs)); | |
483 | ||
484 | pipe_bp->delay = (pipe_bp->delay * 1000) / (hz*10) ; | |
485 | /* | |
486 | * XXX the following is a hack based on ->next being the | |
487 | * first field in dn_pipe and dn_flow_set. The correct | |
488 | * solution would be to move the dn_flow_set to the beginning | |
489 | * of struct dn_pipe. | |
490 | */ | |
491 | pipe_bp->next = CAST_DOWN_EXPLICIT( user32_addr_t, DN_IS_PIPE ); | |
492 | /* clean pointers */ | |
493 | pipe_bp->head = pipe_bp->tail = (user32_addr_t) 0 ; | |
494 | pipe_bp->fs.next = (user32_addr_t)0 ; | |
495 | pipe_bp->fs.pipe = (user32_addr_t)0 ; | |
496 | pipe_bp->fs.rq = (user32_addr_t)0 ; | |
497 | bp = ((char *)pipe_bp) + sizeof(struct dn_pipe_32); | |
498 | return( dn_copy_set_32( &(p->fs), bp) ); | |
499 | } | |
500 | ||
501 | static | |
502 | char *cp_pipe_to_64_user(struct dn_pipe *p, struct dn_pipe_64 *pipe_bp) | |
503 | { | |
504 | char *bp; | |
505 | ||
506 | pipe_bp->pipe_nr = p->pipe_nr; | |
507 | pipe_bp->bandwidth = p->bandwidth; | |
6d2010ae | 508 | pipe_bp->delay = p->delay; |
b0d623f7 A |
509 | bcopy( &(p->scheduler_heap), &(pipe_bp->scheduler_heap), sizeof(struct dn_heap_64)); |
510 | pipe_bp->scheduler_heap.p = CAST_DOWN(user64_addr_t, pipe_bp->scheduler_heap.p); | |
511 | bcopy( &(p->not_eligible_heap), &(pipe_bp->not_eligible_heap), sizeof(struct dn_heap_64)); | |
512 | pipe_bp->not_eligible_heap.p = CAST_DOWN(user64_addr_t, pipe_bp->not_eligible_heap.p); | |
513 | bcopy( &(p->idle_heap), &(pipe_bp->idle_heap), sizeof(struct dn_heap_64)); | |
514 | pipe_bp->idle_heap.p = CAST_DOWN(user64_addr_t, pipe_bp->idle_heap.p); | |
515 | pipe_bp->V = p->V; | |
516 | pipe_bp->sum = p->sum; | |
517 | pipe_bp->numbytes = p->numbytes; | |
518 | pipe_bp->sched_time = p->sched_time; | |
519 | bcopy( p->if_name, pipe_bp->if_name, IFNAMSIZ); | |
520 | pipe_bp->ifp = CAST_DOWN(user64_addr_t, p->ifp); | |
521 | pipe_bp->ready = p->ready; | |
522 | ||
523 | cp_flow_set_to_64_user( &(p->fs), &(pipe_bp->fs)); | |
524 | ||
525 | pipe_bp->delay = (pipe_bp->delay * 1000) / (hz*10) ; | |
526 | /* | |
527 | * XXX the following is a hack based on ->next being the | |
528 | * first field in dn_pipe and dn_flow_set. The correct | |
529 | * solution would be to move the dn_flow_set to the beginning | |
530 | * of struct dn_pipe. | |
531 | */ | |
532 | pipe_bp->next = CAST_DOWN( user64_addr_t, DN_IS_PIPE ); | |
533 | /* clean pointers */ | |
534 | pipe_bp->head = pipe_bp->tail = USER_ADDR_NULL ; | |
535 | pipe_bp->fs.next = USER_ADDR_NULL ; | |
536 | pipe_bp->fs.pipe = USER_ADDR_NULL ; | |
537 | pipe_bp->fs.rq = USER_ADDR_NULL ; | |
538 | bp = ((char *)pipe_bp) + sizeof(struct dn_pipe_64); | |
539 | return( dn_copy_set_64( &(p->fs), bp) ); | |
540 | } | |
541 | ||
9bccf70c A |
542 | static int |
543 | heap_init(struct dn_heap *h, int new_size) | |
91447636 | 544 | { |
9bccf70c A |
545 | struct dn_heap_entry *p; |
546 | ||
547 | if (h->size >= new_size ) { | |
91447636 | 548 | printf("dummynet: heap_init, Bogus call, have %d want %d\n", |
9bccf70c A |
549 | h->size, new_size); |
550 | return 0 ; | |
91447636 | 551 | } |
9bccf70c | 552 | new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT ; |
91447636 | 553 | p = _MALLOC(new_size * sizeof(*p), M_DUMMYNET, M_DONTWAIT ); |
9bccf70c | 554 | if (p == NULL) { |
91447636 | 555 | printf("dummynet: heap_init, resize %d failed\n", new_size ); |
9bccf70c A |
556 | return 1 ; /* error */ |
557 | } | |
558 | if (h->size > 0) { | |
559 | bcopy(h->p, p, h->size * sizeof(*p) ); | |
91447636 | 560 | FREE(h->p, M_DUMMYNET); |
9bccf70c A |
561 | } |
562 | h->p = p ; | |
563 | h->size = new_size ; | |
564 | return 0 ; | |
565 | } | |
566 | ||
567 | /* | |
568 | * Insert element in heap. Normally, p != NULL, we insert p in | |
569 | * a new position and bubble up. If p == NULL, then the element is | |
570 | * already in place, and key is the position where to start the | |
571 | * bubble-up. | |
572 | * Returns 1 on failure (cannot allocate new heap entry) | |
573 | * | |
574 | * If offset > 0 the position (index, int) of the element in the heap is | |
575 | * also stored in the element itself at the given offset in bytes. | |
576 | */ | |
577 | #define SET_OFFSET(heap, node) \ | |
578 | if (heap->offset > 0) \ | |
579 | *((int *)((char *)(heap->p[node].object) + heap->offset)) = node ; | |
580 | /* | |
581 | * RESET_OFFSET is used for sanity checks. It sets offset to an invalid value. | |
582 | */ | |
583 | #define RESET_OFFSET(heap, node) \ | |
584 | if (heap->offset > 0) \ | |
585 | *((int *)((char *)(heap->p[node].object) + heap->offset)) = -1 ; | |
586 | static int | |
587 | heap_insert(struct dn_heap *h, dn_key key1, void *p) | |
91447636 | 588 | { |
9bccf70c A |
589 | int son = h->elements ; |
590 | ||
591 | if (p == NULL) /* data already there, set starting point */ | |
592 | son = key1 ; | |
593 | else { /* insert new element at the end, possibly resize */ | |
594 | son = h->elements ; | |
595 | if (son == h->size) /* need resize... */ | |
596 | if (heap_init(h, h->elements+1) ) | |
597 | return 1 ; /* failure... */ | |
598 | h->p[son].object = p ; | |
599 | h->p[son].key = key1 ; | |
600 | h->elements++ ; | |
601 | } | |
602 | while (son > 0) { /* bubble up */ | |
603 | int father = HEAP_FATHER(son) ; | |
604 | struct dn_heap_entry tmp ; | |
605 | ||
606 | if (DN_KEY_LT( h->p[father].key, h->p[son].key ) ) | |
91447636 | 607 | break ; /* found right position */ |
9bccf70c A |
608 | /* son smaller than father, swap and repeat */ |
609 | HEAP_SWAP(h->p[son], h->p[father], tmp) ; | |
610 | SET_OFFSET(h, son); | |
611 | son = father ; | |
612 | } | |
613 | SET_OFFSET(h, son); | |
614 | return 0 ; | |
1c79356b A |
615 | } |
616 | ||
617 | /* | |
9bccf70c | 618 | * remove top element from heap, or obj if obj != NULL |
1c79356b A |
619 | */ |
620 | static void | |
9bccf70c | 621 | heap_extract(struct dn_heap *h, void *obj) |
91447636 | 622 | { |
2d21ac55 | 623 | int child, father, maxelt = h->elements - 1 ; |
9bccf70c | 624 | |
2d21ac55 | 625 | if (maxelt < 0) { |
39236c6e A |
626 | printf("dummynet: warning, extract from empty heap 0x%llx\n", |
627 | (uint64_t)VM_KERNEL_ADDRPERM(h)); | |
9bccf70c A |
628 | return ; |
629 | } | |
630 | father = 0 ; /* default: move up smallest child */ | |
631 | if (obj != NULL) { /* extract specific element, index is at offset */ | |
632 | if (h->offset <= 0) | |
91447636 | 633 | panic("dummynet: heap_extract from middle not supported on this heap!!!\n"); |
9bccf70c A |
634 | father = *((int *)((char *)obj + h->offset)) ; |
635 | if (father < 0 || father >= h->elements) { | |
636 | printf("dummynet: heap_extract, father %d out of bound 0..%d\n", | |
637 | father, h->elements); | |
91447636 | 638 | panic("dummynet: heap_extract"); |
9bccf70c A |
639 | } |
640 | } | |
641 | RESET_OFFSET(h, father); | |
642 | child = HEAP_LEFT(father) ; /* left child */ | |
2d21ac55 A |
643 | while (child <= maxelt) { /* valid entry */ |
644 | if (child != maxelt && DN_KEY_LT(h->p[child+1].key, h->p[child].key) ) | |
9bccf70c A |
645 | child = child+1 ; /* take right child, otherwise left */ |
646 | h->p[father] = h->p[child] ; | |
647 | SET_OFFSET(h, father); | |
648 | father = child ; | |
649 | child = HEAP_LEFT(child) ; /* left child for next loop */ | |
91447636 | 650 | } |
9bccf70c | 651 | h->elements-- ; |
2d21ac55 | 652 | if (father != maxelt) { |
1c79356b | 653 | /* |
9bccf70c | 654 | * Fill hole with last entry and bubble up, reusing the insert code |
1c79356b | 655 | */ |
2d21ac55 | 656 | h->p[father] = h->p[maxelt] ; |
9bccf70c A |
657 | heap_insert(h, father, NULL); /* this one cannot fail */ |
658 | } | |
91447636 | 659 | } |
1c79356b | 660 | |
9bccf70c A |
661 | /* |
662 | * heapify() will reorganize data inside an array to maintain the | |
663 | * heap property. It is needed when we delete a bunch of entries. | |
664 | */ | |
665 | static void | |
666 | heapify(struct dn_heap *h) | |
667 | { | |
668 | int i ; | |
669 | ||
670 | for (i = 0 ; i < h->elements ; i++ ) | |
671 | heap_insert(h, i , NULL) ; | |
672 | } | |
673 | ||
674 | /* | |
675 | * cleanup the heap and free data structure | |
676 | */ | |
677 | static void | |
678 | heap_free(struct dn_heap *h) | |
679 | { | |
680 | if (h->size >0 ) | |
91447636 | 681 | FREE(h->p, M_DUMMYNET); |
2d21ac55 | 682 | bzero(h, sizeof(*h)); |
9bccf70c A |
683 | } |
684 | ||
685 | /* | |
686 | * --- end of heap management functions --- | |
687 | */ | |
688 | ||
91447636 A |
689 | /* |
690 | * Return the mbuf tag holding the dummynet state. As an optimization | |
691 | * this is assumed to be the first tag on the list. If this turns out | |
692 | * wrong we'll need to search the list. | |
693 | */ | |
694 | static struct dn_pkt_tag * | |
695 | dn_tag_get(struct mbuf *m) | |
696 | { | |
697 | struct m_tag *mtag = m_tag_first(m); | |
316670eb A |
698 | |
699 | if (!(mtag != NULL && | |
700 | mtag->m_tag_id == KERNEL_MODULE_TAG_ID && | |
701 | mtag->m_tag_type == KERNEL_TAG_TYPE_DUMMYNET)) | |
39236c6e A |
702 | panic("packet on dummynet queue w/o dummynet tag: 0x%llx", |
703 | (uint64_t)VM_KERNEL_ADDRPERM(m)); | |
316670eb | 704 | |
91447636 A |
705 | return (struct dn_pkt_tag *)(mtag+1); |
706 | } | |
707 | ||
9bccf70c A |
708 | /* |
709 | * Scheduler functions: | |
710 | * | |
711 | * transmit_event() is called when the delay-line needs to enter | |
712 | * the scheduler, either because of existing pkts getting ready, | |
713 | * or new packets entering the queue. The event handled is the delivery | |
714 | * time of the packet. | |
715 | * | |
716 | * ready_event() does something similar with fixed-rate queues, and the | |
717 | * event handled is the finish time of the head pkt. | |
718 | * | |
719 | * wfq_ready_event() does something similar with WF2Q queues, and the | |
720 | * event handled is the start time of the head pkt. | |
721 | * | |
722 | * In all cases, we make sure that the data structures are consistent | |
723 | * before passing pkts out, because this might trigger recursive | |
724 | * invocations of the procedures. | |
725 | */ | |
726 | static void | |
b0d623f7 | 727 | transmit_event(struct dn_pipe *pipe, struct mbuf **head, struct mbuf **tail) |
9bccf70c | 728 | { |
316670eb A |
729 | struct mbuf *m ; |
730 | struct dn_pkt_tag *pkt = NULL; | |
731 | u_int64_t schedule_time; | |
b0d623f7 | 732 | |
91447636 | 733 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); |
316670eb | 734 | ASSERT(serialize >= 0); |
b0d623f7 A |
735 | if (serialize == 0) { |
736 | while ((m = pipe->head) != NULL) { | |
737 | pkt = dn_tag_get(m); | |
316670eb | 738 | if (!DN_KEY_LEQ(pkt->dn_output_time, curr_time)) |
91447636 | 739 | break; |
b0d623f7 A |
740 | |
741 | pipe->head = m->m_nextpkt; | |
742 | if (*tail != NULL) | |
743 | (*tail)->m_nextpkt = m; | |
744 | else | |
745 | *head = m; | |
746 | *tail = m; | |
91447636 | 747 | } |
6d2010ae | 748 | |
b0d623f7 A |
749 | if (*tail != NULL) |
750 | (*tail)->m_nextpkt = NULL; | |
316670eb | 751 | } |
6d2010ae | 752 | |
316670eb A |
753 | schedule_time = pkt == NULL || DN_KEY_LEQ(pkt->dn_output_time, curr_time) ? |
754 | curr_time + 1 : pkt->dn_output_time; | |
b0d623f7 | 755 | |
316670eb A |
756 | /* if there are leftover packets, put the pipe into the heap for next ready event */ |
757 | if ((m = pipe->head) != NULL) { | |
91447636 A |
758 | pkt = dn_tag_get(m); |
759 | /* XXX should check errors on heap_insert, by draining the | |
760 | * whole pipe p and hoping in the future we are more successful | |
761 | */ | |
6d2010ae | 762 | heap_insert(&extract_heap, schedule_time, pipe); |
316670eb | 763 | } |
1c79356b | 764 | } |
9bccf70c | 765 | |
1c79356b | 766 | /* |
9bccf70c A |
767 | * the following macro computes how many ticks we have to wait |
768 | * before being able to transmit a packet. The credit is taken from | |
769 | * either a pipe (WF2Q) or a flow_queue (per-flow queueing) | |
1c79356b | 770 | */ |
0c530ab8 A |
771 | |
772 | /* hz is 100, which gives a granularity of 10ms in the old timer. | |
773 | * The timer has been changed to fire every 1ms, so the use of | |
774 | * hz has been modified here. All instances of hz have been left | |
775 | * in place but adjusted by a factor of 10 so that hz is functionally | |
776 | * equal to 1000. | |
777 | */ | |
91447636 | 778 | #define SET_TICKS(_m, q, p) \ |
0c530ab8 | 779 | ((_m)->m_pkthdr.len*8*(hz*10) - (q)->numbytes + p->bandwidth - 1 ) / \ |
9bccf70c A |
780 | p->bandwidth ; |
781 | ||
782 | /* | |
783 | * extract pkt from queue, compute output time (could be now) | |
784 | * and put into delay line (p_queue) | |
785 | */ | |
786 | static void | |
91447636 | 787 | move_pkt(struct mbuf *pkt, struct dn_flow_queue *q, |
9bccf70c | 788 | struct dn_pipe *p, int len) |
1c79356b | 789 | { |
91447636 A |
790 | struct dn_pkt_tag *dt = dn_tag_get(pkt); |
791 | ||
792 | q->head = pkt->m_nextpkt ; | |
9bccf70c A |
793 | q->len-- ; |
794 | q->len_bytes -= len ; | |
1c79356b | 795 | |
316670eb | 796 | dt->dn_output_time = curr_time + p->delay ; |
1c79356b | 797 | |
9bccf70c A |
798 | if (p->head == NULL) |
799 | p->head = pkt; | |
800 | else | |
91447636 | 801 | p->tail->m_nextpkt = pkt; |
9bccf70c | 802 | p->tail = pkt; |
91447636 | 803 | p->tail->m_nextpkt = NULL; |
1c79356b A |
804 | } |
805 | ||
806 | /* | |
9bccf70c A |
807 | * ready_event() is invoked every time the queue must enter the |
808 | * scheduler, either because the first packet arrives, or because | |
809 | * a previously scheduled event fired. | |
810 | * On invokation, drain as many pkts as possible (could be 0) and then | |
811 | * if there are leftover packets reinsert the pkt in the scheduler. | |
1c79356b | 812 | */ |
9bccf70c | 813 | static void |
b0d623f7 | 814 | ready_event(struct dn_flow_queue *q, struct mbuf **head, struct mbuf **tail) |
1c79356b | 815 | { |
91447636 | 816 | struct mbuf *pkt; |
9bccf70c A |
817 | struct dn_pipe *p = q->fs->pipe ; |
818 | int p_was_empty ; | |
1c79356b | 819 | |
91447636 A |
820 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); |
821 | ||
9bccf70c | 822 | if (p == NULL) { |
b0d623f7 A |
823 | printf("dummynet: ready_event pipe is gone\n"); |
824 | return ; | |
9bccf70c A |
825 | } |
826 | p_was_empty = (p->head == NULL) ; | |
1c79356b | 827 | |
1c79356b | 828 | /* |
9bccf70c A |
829 | * schedule fixed-rate queues linked to this pipe: |
830 | * Account for the bw accumulated since last scheduling, then | |
831 | * drain as many pkts as allowed by q->numbytes and move to | |
832 | * the delay line (in p) computing output time. | |
833 | * bandwidth==0 (no limit) means we can drain the whole queue, | |
834 | * setting len_scaled = 0 does the job. | |
835 | */ | |
836 | q->numbytes += ( curr_time - q->sched_time ) * p->bandwidth; | |
837 | while ( (pkt = q->head) != NULL ) { | |
91447636 | 838 | int len = pkt->m_pkthdr.len; |
0c530ab8 | 839 | int len_scaled = p->bandwidth ? len*8*(hz*10) : 0 ; |
9bccf70c A |
840 | if (len_scaled > q->numbytes ) |
841 | break ; | |
842 | q->numbytes -= len_scaled ; | |
843 | move_pkt(pkt, q, p, len); | |
844 | } | |
845 | /* | |
846 | * If we have more packets queued, schedule next ready event | |
847 | * (can only occur when bandwidth != 0, otherwise we would have | |
848 | * flushed the whole queue in the previous loop). | |
849 | * To this purpose we record the current time and compute how many | |
850 | * ticks to go for the finish time of the packet. | |
851 | */ | |
852 | if ( (pkt = q->head) != NULL ) { /* this implies bandwidth != 0 */ | |
853 | dn_key t = SET_TICKS(pkt, q, p); /* ticks i have to wait */ | |
854 | q->sched_time = curr_time ; | |
855 | heap_insert(&ready_heap, curr_time + t, (void *)q ); | |
856 | /* XXX should check errors on heap_insert, and drain the whole | |
857 | * queue on error hoping next time we are luckier. | |
858 | */ | |
91447636 | 859 | } else { /* RED needs to know when the queue becomes empty */ |
9bccf70c | 860 | q->q_time = curr_time; |
91447636 A |
861 | q->numbytes = 0; |
862 | } | |
9bccf70c A |
863 | /* |
864 | * If the delay line was empty call transmit_event(p) now. | |
865 | * Otherwise, the scheduler will take care of it. | |
1c79356b | 866 | */ |
9bccf70c | 867 | if (p_was_empty) |
b0d623f7 | 868 | transmit_event(p, head, tail); |
9bccf70c | 869 | } |
1c79356b | 870 | |
9bccf70c A |
871 | /* |
872 | * Called when we can transmit packets on WF2Q queues. Take pkts out of | |
873 | * the queues at their start time, and enqueue into the delay line. | |
874 | * Packets are drained until p->numbytes < 0. As long as | |
875 | * len_scaled >= p->numbytes, the packet goes into the delay line | |
876 | * with a deadline p->delay. For the last packet, if p->numbytes<0, | |
877 | * there is an additional delay. | |
878 | */ | |
879 | static void | |
b0d623f7 | 880 | ready_event_wfq(struct dn_pipe *p, struct mbuf **head, struct mbuf **tail) |
9bccf70c A |
881 | { |
882 | int p_was_empty = (p->head == NULL) ; | |
883 | struct dn_heap *sch = &(p->scheduler_heap); | |
884 | struct dn_heap *neh = &(p->not_eligible_heap) ; | |
b0d623f7 | 885 | int64_t p_numbytes = p->numbytes; |
9bccf70c | 886 | |
91447636 | 887 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); |
316670eb | 888 | |
9bccf70c | 889 | if (p->if_name[0] == 0) /* tx clock is simulated */ |
b0d623f7 | 890 | p_numbytes += ( curr_time - p->sched_time ) * p->bandwidth; |
9bccf70c | 891 | else { /* tx clock is for real, the ifq must be empty or this is a NOP */ |
316670eb | 892 | if (p->ifp && !IFCQ_IS_EMPTY(&p->ifp->if_snd)) |
9bccf70c A |
893 | return ; |
894 | else { | |
91447636 A |
895 | DPRINTF(("dummynet: pipe %d ready from %s --\n", |
896 | p->pipe_nr, p->if_name)); | |
9bccf70c | 897 | } |
1c79356b | 898 | } |
9bccf70c | 899 | |
1c79356b | 900 | /* |
9bccf70c A |
901 | * While we have backlogged traffic AND credit, we need to do |
902 | * something on the queue. | |
1c79356b | 903 | */ |
b0d623f7 | 904 | while ( p_numbytes >=0 && (sch->elements>0 || neh->elements >0) ) { |
9bccf70c A |
905 | if (sch->elements > 0) { /* have some eligible pkts to send out */ |
906 | struct dn_flow_queue *q = sch->p[0].object ; | |
91447636 A |
907 | struct mbuf *pkt = q->head; |
908 | struct dn_flow_set *fs = q->fs; | |
909 | u_int64_t len = pkt->m_pkthdr.len; | |
0c530ab8 | 910 | int len_scaled = p->bandwidth ? len*8*(hz*10) : 0 ; |
1c79356b | 911 | |
9bccf70c | 912 | heap_extract(sch, NULL); /* remove queue from heap */ |
b0d623f7 | 913 | p_numbytes -= len_scaled ; |
9bccf70c A |
914 | move_pkt(pkt, q, p, len); |
915 | ||
916 | p->V += (len<<MY_M) / p->sum ; /* update V */ | |
917 | q->S = q->F ; /* update start time */ | |
918 | if (q->len == 0) { /* Flow not backlogged any more */ | |
919 | fs->backlogged-- ; | |
920 | heap_insert(&(p->idle_heap), q->F, q); | |
921 | } else { /* still backlogged */ | |
922 | /* | |
923 | * update F and position in backlogged queue, then | |
924 | * put flow in not_eligible_heap (we will fix this later). | |
925 | */ | |
91447636 | 926 | len = (q->head)->m_pkthdr.len; |
9bccf70c A |
927 | q->F += (len<<MY_M)/(u_int64_t) fs->weight ; |
928 | if (DN_KEY_LEQ(q->S, p->V)) | |
929 | heap_insert(neh, q->S, q); | |
930 | else | |
931 | heap_insert(sch, q->F, q); | |
932 | } | |
933 | } | |
934 | /* | |
935 | * now compute V = max(V, min(S_i)). Remember that all elements in sch | |
936 | * have by definition S_i <= V so if sch is not empty, V is surely | |
937 | * the max and we must not update it. Conversely, if sch is empty | |
938 | * we only need to look at neh. | |
939 | */ | |
940 | if (sch->elements == 0 && neh->elements > 0) | |
941 | p->V = MAX64 ( p->V, neh->p[0].key ); | |
942 | /* move from neh to sch any packets that have become eligible */ | |
943 | while (neh->elements > 0 && DN_KEY_LEQ(neh->p[0].key, p->V) ) { | |
944 | struct dn_flow_queue *q = neh->p[0].object ; | |
945 | heap_extract(neh, NULL); | |
946 | heap_insert(sch, q->F, q); | |
947 | } | |
948 | ||
949 | if (p->if_name[0] != '\0') {/* tx clock is from a real thing */ | |
b0d623f7 | 950 | p_numbytes = -1 ; /* mark not ready for I/O */ |
9bccf70c A |
951 | break ; |
952 | } | |
1c79356b | 953 | } |
b0d623f7 | 954 | if (sch->elements == 0 && neh->elements == 0 && p_numbytes >= 0 |
9bccf70c A |
955 | && p->idle_heap.elements > 0) { |
956 | /* | |
957 | * no traffic and no events scheduled. We can get rid of idle-heap. | |
958 | */ | |
959 | int i ; | |
960 | ||
961 | for (i = 0 ; i < p->idle_heap.elements ; i++) { | |
962 | struct dn_flow_queue *q = p->idle_heap.p[i].object ; | |
1c79356b | 963 | |
9bccf70c A |
964 | q->F = 0 ; |
965 | q->S = q->F + 1 ; | |
966 | } | |
967 | p->sum = 0 ; | |
968 | p->V = 0 ; | |
969 | p->idle_heap.elements = 0 ; | |
970 | } | |
971 | /* | |
972 | * If we are getting clocks from dummynet (not a real interface) and | |
973 | * If we are under credit, schedule the next ready event. | |
974 | * Also fix the delivery time of the last packet. | |
1c79356b | 975 | */ |
b0d623f7 | 976 | if (p->if_name[0]==0 && p_numbytes < 0) { /* this implies bandwidth >0 */ |
9bccf70c | 977 | dn_key t=0 ; /* number of ticks i have to wait */ |
1c79356b | 978 | |
9bccf70c | 979 | if (p->bandwidth > 0) |
b0d623f7 | 980 | t = ( p->bandwidth -1 - p_numbytes) / p->bandwidth ; |
316670eb | 981 | dn_tag_get(p->tail)->dn_output_time += t ; |
9bccf70c A |
982 | p->sched_time = curr_time ; |
983 | heap_insert(&wfq_ready_heap, curr_time + t, (void *)p); | |
984 | /* XXX should check errors on heap_insert, and drain the whole | |
985 | * queue on error hoping next time we are luckier. | |
986 | */ | |
1c79356b | 987 | } |
b0d623f7 A |
988 | |
989 | /* Fit (adjust if necessary) 64bit result into 32bit variable. */ | |
990 | if (p_numbytes > INT_MAX) | |
991 | p->numbytes = INT_MAX; | |
992 | else if (p_numbytes < INT_MIN) | |
993 | p->numbytes = INT_MIN; | |
994 | else | |
995 | p->numbytes = p_numbytes; | |
996 | ||
9bccf70c A |
997 | /* |
998 | * If the delay line was empty call transmit_event(p) now. | |
999 | * Otherwise, the scheduler will take care of it. | |
1000 | */ | |
1001 | if (p_was_empty) | |
b0d623f7 A |
1002 | transmit_event(p, head, tail); |
1003 | ||
1c79356b A |
1004 | } |
1005 | ||
1006 | /* | |
0c530ab8 | 1007 | * This is called every 1ms. It is used to |
9bccf70c | 1008 | * increment the current tick counter and schedule expired events. |
1c79356b A |
1009 | */ |
1010 | static void | |
2d21ac55 | 1011 | dummynet(__unused void * unused) |
1c79356b | 1012 | { |
9bccf70c A |
1013 | void *p ; /* generic parameter to handler */ |
1014 | struct dn_heap *h ; | |
9bccf70c | 1015 | struct dn_heap *heaps[3]; |
b0d623f7 | 1016 | struct mbuf *head = NULL, *tail = NULL; |
9bccf70c A |
1017 | int i; |
1018 | struct dn_pipe *pe ; | |
0c530ab8 A |
1019 | struct timespec ts; |
1020 | struct timeval tv; | |
1c79356b | 1021 | |
9bccf70c A |
1022 | heaps[0] = &ready_heap ; /* fixed-rate queues */ |
1023 | heaps[1] = &wfq_ready_heap ; /* wfq queues */ | |
1024 | heaps[2] = &extract_heap ; /* delay line */ | |
b0d623f7 | 1025 | |
91447636 A |
1026 | lck_mtx_lock(dn_mutex); |
1027 | ||
0c530ab8 A |
1028 | /* make all time measurements in milliseconds (ms) - |
1029 | * here we convert secs and usecs to msecs (just divide the | |
1030 | * usecs and take the closest whole number). | |
1031 | */ | |
1032 | microuptime(&tv); | |
1033 | curr_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); | |
b0d623f7 | 1034 | |
9bccf70c A |
1035 | for (i=0; i < 3 ; i++) { |
1036 | h = heaps[i]; | |
1037 | while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time) ) { | |
0c530ab8 | 1038 | if (h->p[0].key > curr_time) |
b0d623f7 A |
1039 | printf("dummynet: warning, heap %d is %d ticks late\n", |
1040 | i, (int)(curr_time - h->p[0].key)); | |
0c530ab8 A |
1041 | p = h->p[0].object ; /* store a copy before heap_extract */ |
1042 | heap_extract(h, NULL); /* need to extract before processing */ | |
1043 | if (i == 0) | |
b0d623f7 | 1044 | ready_event(p, &head, &tail) ; |
0c530ab8 | 1045 | else if (i == 1) { |
b0d623f7 A |
1046 | struct dn_pipe *pipe = p; |
1047 | if (pipe->if_name[0] != '\0') | |
1048 | printf("dummynet: bad ready_event_wfq for pipe %s\n", | |
1049 | pipe->if_name); | |
1050 | else | |
1051 | ready_event_wfq(p, &head, &tail) ; | |
1052 | } else { | |
1053 | transmit_event(p, &head, &tail); | |
1054 | } | |
9bccf70c | 1055 | } |
1c79356b | 1056 | } |
9bccf70c | 1057 | /* sweep pipes trying to expire idle flow_queues */ |
b0d623f7 A |
1058 | for (i = 0; i < HASHSIZE; i++) |
1059 | SLIST_FOREACH(pe, &pipehash[i], next) | |
9bccf70c A |
1060 | if (pe->idle_heap.elements > 0 && |
1061 | DN_KEY_LT(pe->idle_heap.p[0].key, pe->V) ) { | |
1062 | struct dn_flow_queue *q = pe->idle_heap.p[0].object ; | |
1c79356b | 1063 | |
9bccf70c A |
1064 | heap_extract(&(pe->idle_heap), NULL); |
1065 | q->S = q->F + 1 ; /* mark timestamp as invalid */ | |
1066 | pe->sum -= q->fs->weight ; | |
1067 | } | |
316670eb | 1068 | |
0c530ab8 A |
1069 | /* check the heaps to see if there's still stuff in there, and |
1070 | * only set the timer if there are packets to process | |
1071 | */ | |
1072 | timer_enabled = 0; | |
1073 | for (i=0; i < 3 ; i++) { | |
1074 | h = heaps[i]; | |
1075 | if (h->elements > 0) { // set the timer | |
1076 | ts.tv_sec = 0; | |
1077 | ts.tv_nsec = 1 * 1000000; // 1ms | |
1078 | timer_enabled = 1; | |
1079 | bsd_timeout(dummynet, NULL, &ts); | |
1080 | break; | |
1081 | } | |
1082 | } | |
6d2010ae | 1083 | |
b0d623f7 | 1084 | if (head != NULL) |
6d2010ae A |
1085 | serialize++; |
1086 | ||
1087 | lck_mtx_unlock(dn_mutex); | |
b0d623f7 A |
1088 | |
1089 | /* Send out the de-queued list of ready-to-send packets */ | |
1090 | if (head != NULL) { | |
1091 | dummynet_send(head); | |
1092 | lck_mtx_lock(dn_mutex); | |
6d2010ae | 1093 | serialize--; |
b0d623f7 A |
1094 | lck_mtx_unlock(dn_mutex); |
1095 | } | |
1096 | } | |
1097 | ||
1098 | ||
1099 | static void | |
1100 | dummynet_send(struct mbuf *m) | |
1101 | { | |
1102 | struct dn_pkt_tag *pkt; | |
1103 | struct mbuf *n; | |
1104 | ||
1105 | for (; m != NULL; m = n) { | |
1106 | n = m->m_nextpkt; | |
1107 | m->m_nextpkt = NULL; | |
1108 | pkt = dn_tag_get(m); | |
39236c6e A |
1109 | |
1110 | DPRINTF(("dummynet_send m: 0x%llx dn_dir: %d dn_flags: 0x%x\n", | |
1111 | (uint64_t)VM_KERNEL_ADDRPERM(m), pkt->dn_dir, | |
1112 | pkt->dn_flags)); | |
1113 | ||
b0d623f7 A |
1114 | switch (pkt->dn_dir) { |
1115 | case DN_TO_IP_OUT: { | |
39236c6e A |
1116 | struct route tmp_rt; |
1117 | ||
1118 | /* route is already in the packet's dn_ro */ | |
1119 | bzero(&tmp_rt, sizeof (tmp_rt)); | |
1120 | ||
316670eb A |
1121 | /* Force IP_RAWOUTPUT as the IP header is fully formed */ |
1122 | pkt->dn_flags |= IP_RAWOUTPUT | IP_FORWARDING; | |
1123 | (void)ip_output(m, NULL, &tmp_rt, pkt->dn_flags, NULL, NULL); | |
39236c6e | 1124 | ROUTE_RELEASE(&tmp_rt); |
b0d623f7 A |
1125 | break ; |
1126 | } | |
1127 | case DN_TO_IP_IN : | |
1128 | proto_inject(PF_INET, m); | |
1129 | break ; | |
316670eb A |
1130 | #ifdef INET6 |
1131 | case DN_TO_IP6_OUT: { | |
39236c6e A |
1132 | /* routes already in the packet's dn_{ro6,pmtu} */ |
1133 | ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL); | |
316670eb A |
1134 | break; |
1135 | } | |
1136 | case DN_TO_IP6_IN: | |
1137 | proto_inject(PF_INET6, m); | |
1138 | break; | |
1139 | #endif /* INET6 */ | |
b0d623f7 A |
1140 | default: |
1141 | printf("dummynet: bad switch %d!\n", pkt->dn_dir); | |
1142 | m_freem(m); | |
1143 | break ; | |
1144 | } | |
1145 | } | |
9bccf70c | 1146 | } |
b0d623f7 A |
1147 | |
1148 | ||
9bccf70c | 1149 | |
1c79356b | 1150 | /* |
9bccf70c | 1151 | * called by an interface when tx_rdy occurs. |
1c79356b | 1152 | */ |
9bccf70c A |
1153 | int |
1154 | if_tx_rdy(struct ifnet *ifp) | |
1c79356b | 1155 | { |
9bccf70c | 1156 | struct dn_pipe *p; |
b0d623f7 A |
1157 | struct mbuf *head = NULL, *tail = NULL; |
1158 | int i; | |
1159 | ||
91447636 | 1160 | lck_mtx_lock(dn_mutex); |
b0d623f7 A |
1161 | |
1162 | for (i = 0; i < HASHSIZE; i++) | |
1163 | SLIST_FOREACH(p, &pipehash[i], next) | |
1164 | if (p->ifp == ifp) | |
1165 | break ; | |
9bccf70c A |
1166 | if (p == NULL) { |
1167 | char buf[32]; | |
39236c6e | 1168 | snprintf(buf, sizeof(buf), "%s", if_name(ifp)); |
b0d623f7 A |
1169 | for (i = 0; i < HASHSIZE; i++) |
1170 | SLIST_FOREACH(p, &pipehash[i], next) | |
9bccf70c A |
1171 | if (!strcmp(p->if_name, buf) ) { |
1172 | p->ifp = ifp ; | |
91447636 | 1173 | DPRINTF(("dummynet: ++ tx rdy from %s (now found)\n", buf)); |
9bccf70c A |
1174 | break ; |
1175 | } | |
1176 | } | |
1177 | if (p != NULL) { | |
39236c6e A |
1178 | DPRINTF(("dummynet: ++ tx rdy from %s - qlen %d\n", if_name(ifp), |
1179 | IFCQ_LEN(&ifp->if_snd))); | |
9bccf70c | 1180 | p->numbytes = 0 ; /* mark ready for I/O */ |
b0d623f7 | 1181 | ready_event_wfq(p, &head, &tail); |
1c79356b | 1182 | } |
6d2010ae A |
1183 | |
1184 | if (head != NULL) { | |
1185 | serialize++; | |
1186 | } | |
1187 | ||
b0d623f7 A |
1188 | lck_mtx_unlock(dn_mutex); |
1189 | ||
b0d623f7 | 1190 | /* Send out the de-queued list of ready-to-send packets */ |
6d2010ae | 1191 | if (head != NULL) { |
b0d623f7 | 1192 | dummynet_send(head); |
316670eb | 1193 | lck_mtx_lock(dn_mutex); |
6d2010ae | 1194 | serialize--; |
316670eb | 1195 | lck_mtx_unlock(dn_mutex); |
6d2010ae | 1196 | } |
9bccf70c | 1197 | return 0; |
1c79356b A |
1198 | } |
1199 | ||
1c79356b | 1200 | /* |
9bccf70c A |
1201 | * Unconditionally expire empty queues in case of shortage. |
1202 | * Returns the number of queues freed. | |
1c79356b | 1203 | */ |
9bccf70c A |
1204 | static int |
1205 | expire_queues(struct dn_flow_set *fs) | |
1c79356b | 1206 | { |
9bccf70c A |
1207 | struct dn_flow_queue *q, *prev ; |
1208 | int i, initial_elements = fs->rq_elements ; | |
91447636 | 1209 | struct timeval timenow; |
1c79356b | 1210 | |
6d2010ae | 1211 | /* reviewed for getmicrotime usage */ |
91447636 A |
1212 | getmicrotime(&timenow); |
1213 | ||
1214 | if (fs->last_expired == timenow.tv_sec) | |
9bccf70c | 1215 | return 0 ; |
91447636 | 1216 | fs->last_expired = timenow.tv_sec ; |
9bccf70c A |
1217 | for (i = 0 ; i <= fs->rq_size ; i++) /* last one is overflow */ |
1218 | for (prev=NULL, q = fs->rq[i] ; q != NULL ; ) | |
1219 | if (q->head != NULL || q->S != q->F+1) { | |
1220 | prev = q ; | |
1221 | q = q->next ; | |
1222 | } else { /* entry is idle, expire it */ | |
1223 | struct dn_flow_queue *old_q = q ; | |
1224 | ||
1225 | if (prev != NULL) | |
1226 | prev->next = q = q->next ; | |
1227 | else | |
1228 | fs->rq[i] = q = q->next ; | |
1229 | fs->rq_elements-- ; | |
91447636 | 1230 | FREE(old_q, M_DUMMYNET); |
1c79356b | 1231 | } |
9bccf70c | 1232 | return initial_elements - fs->rq_elements ; |
1c79356b A |
1233 | } |
1234 | ||
1235 | /* | |
9bccf70c A |
1236 | * If room, create a new queue and put at head of slot i; |
1237 | * otherwise, create or use the default queue. | |
1c79356b | 1238 | */ |
9bccf70c A |
1239 | static struct dn_flow_queue * |
1240 | create_queue(struct dn_flow_set *fs, int i) | |
1c79356b | 1241 | { |
9bccf70c | 1242 | struct dn_flow_queue *q ; |
1c79356b | 1243 | |
9bccf70c A |
1244 | if (fs->rq_elements > fs->rq_size * dn_max_ratio && |
1245 | expire_queues(fs) == 0) { | |
1246 | /* | |
1247 | * No way to get room, use or create overflow queue. | |
1248 | */ | |
1249 | i = fs->rq_size ; | |
1250 | if ( fs->rq[i] != NULL ) | |
1251 | return fs->rq[i] ; | |
1252 | } | |
91447636 | 1253 | q = _MALLOC(sizeof(*q), M_DUMMYNET, M_DONTWAIT | M_ZERO); |
9bccf70c | 1254 | if (q == NULL) { |
91447636 | 1255 | printf("dummynet: sorry, cannot allocate queue for new flow\n"); |
9bccf70c A |
1256 | return NULL ; |
1257 | } | |
9bccf70c A |
1258 | q->fs = fs ; |
1259 | q->hash_slot = i ; | |
1260 | q->next = fs->rq[i] ; | |
1261 | q->S = q->F + 1; /* hack - mark timestamp as invalid */ | |
1262 | fs->rq[i] = q ; | |
1263 | fs->rq_elements++ ; | |
1264 | return q ; | |
1265 | } | |
1c79356b | 1266 | |
9bccf70c A |
1267 | /* |
1268 | * Given a flow_set and a pkt in last_pkt, find a matching queue | |
1269 | * after appropriate masking. The queue is moved to front | |
1270 | * so that further searches take less time. | |
1271 | */ | |
1272 | static struct dn_flow_queue * | |
316670eb | 1273 | find_queue(struct dn_flow_set *fs, struct ip_flow_id *id) |
9bccf70c A |
1274 | { |
1275 | int i = 0 ; /* we need i and q for new allocations */ | |
1276 | struct dn_flow_queue *q, *prev; | |
316670eb | 1277 | int is_v6 = IS_IP6_FLOW_ID(id); |
1c79356b | 1278 | |
9bccf70c A |
1279 | if ( !(fs->flags_fs & DN_HAVE_FLOW_MASK) ) |
1280 | q = fs->rq[0] ; | |
1281 | else { | |
316670eb | 1282 | /* first, do the masking, then hash */ |
91447636 A |
1283 | id->dst_port &= fs->flow_mask.dst_port ; |
1284 | id->src_port &= fs->flow_mask.src_port ; | |
1285 | id->proto &= fs->flow_mask.proto ; | |
1286 | id->flags = 0 ; /* we don't care about this one */ | |
316670eb A |
1287 | if (is_v6) { |
1288 | APPLY_MASK(&id->dst_ip6, &fs->flow_mask.dst_ip6); | |
1289 | APPLY_MASK(&id->src_ip6, &fs->flow_mask.src_ip6); | |
1290 | id->flow_id6 &= fs->flow_mask.flow_id6; | |
1291 | ||
1292 | i = ((id->dst_ip6.__u6_addr.__u6_addr32[0]) & 0xffff)^ | |
1293 | ((id->dst_ip6.__u6_addr.__u6_addr32[1]) & 0xffff)^ | |
1294 | ((id->dst_ip6.__u6_addr.__u6_addr32[2]) & 0xffff)^ | |
1295 | ((id->dst_ip6.__u6_addr.__u6_addr32[3]) & 0xffff)^ | |
1296 | ||
1297 | ((id->dst_ip6.__u6_addr.__u6_addr32[0] >> 15) & 0xffff)^ | |
1298 | ((id->dst_ip6.__u6_addr.__u6_addr32[1] >> 15) & 0xffff)^ | |
1299 | ((id->dst_ip6.__u6_addr.__u6_addr32[2] >> 15) & 0xffff)^ | |
1300 | ((id->dst_ip6.__u6_addr.__u6_addr32[3] >> 15) & 0xffff)^ | |
1301 | ||
1302 | ((id->src_ip6.__u6_addr.__u6_addr32[0] << 1) & 0xfffff)^ | |
1303 | ((id->src_ip6.__u6_addr.__u6_addr32[1] << 1) & 0xfffff)^ | |
1304 | ((id->src_ip6.__u6_addr.__u6_addr32[2] << 1) & 0xfffff)^ | |
1305 | ((id->src_ip6.__u6_addr.__u6_addr32[3] << 1) & 0xfffff)^ | |
1306 | ||
3e170ce0 A |
1307 | ((id->src_ip6.__u6_addr.__u6_addr32[0] >> 16) & 0xffff)^ |
1308 | ((id->src_ip6.__u6_addr.__u6_addr32[1] >> 16) & 0xffff)^ | |
1309 | ((id->src_ip6.__u6_addr.__u6_addr32[2] >> 16) & 0xffff)^ | |
1310 | ((id->src_ip6.__u6_addr.__u6_addr32[3] >> 16) & 0xffff)^ | |
316670eb A |
1311 | |
1312 | (id->dst_port << 1) ^ (id->src_port) ^ | |
1313 | (id->proto ) ^ | |
1314 | (id->flow_id6); | |
1315 | } else { | |
1316 | id->dst_ip &= fs->flow_mask.dst_ip ; | |
1317 | id->src_ip &= fs->flow_mask.src_ip ; | |
1318 | ||
1319 | i = ( (id->dst_ip) & 0xffff ) ^ | |
1320 | ( (id->dst_ip >> 15) & 0xffff ) ^ | |
1321 | ( (id->src_ip << 1) & 0xffff ) ^ | |
1322 | ( (id->src_ip >> 16 ) & 0xffff ) ^ | |
1323 | (id->dst_port << 1) ^ (id->src_port) ^ | |
1324 | (id->proto ); | |
1325 | } | |
9bccf70c A |
1326 | i = i % fs->rq_size ; |
1327 | /* finally, scan the current list for a match */ | |
1328 | searches++ ; | |
1329 | for (prev=NULL, q = fs->rq[i] ; q ; ) { | |
1330 | search_steps++; | |
316670eb A |
1331 | if (is_v6 && |
1332 | IN6_ARE_ADDR_EQUAL(&id->dst_ip6,&q->id.dst_ip6) && | |
1333 | IN6_ARE_ADDR_EQUAL(&id->src_ip6,&q->id.src_ip6) && | |
1334 | id->dst_port == q->id.dst_port && | |
1335 | id->src_port == q->id.src_port && | |
1336 | id->proto == q->id.proto && | |
1337 | id->flags == q->id.flags && | |
1338 | id->flow_id6 == q->id.flow_id6) | |
1339 | break ; /* found */ | |
1340 | ||
1341 | if (!is_v6 && id->dst_ip == q->id.dst_ip && | |
1342 | id->src_ip == q->id.src_ip && | |
1343 | id->dst_port == q->id.dst_port && | |
1344 | id->src_port == q->id.src_port && | |
1345 | id->proto == q->id.proto && | |
1346 | id->flags == q->id.flags) | |
1347 | break ; /* found */ | |
1348 | ||
1349 | /* No match. Check if we can expire the entry */ | |
1350 | if (pipe_expire && q->head == NULL && q->S == q->F+1 ) { | |
9bccf70c A |
1351 | /* entry is idle and not in any heap, expire it */ |
1352 | struct dn_flow_queue *old_q = q ; | |
1c79356b | 1353 | |
9bccf70c A |
1354 | if (prev != NULL) |
1355 | prev->next = q = q->next ; | |
1356 | else | |
1357 | fs->rq[i] = q = q->next ; | |
1358 | fs->rq_elements-- ; | |
91447636 | 1359 | FREE(old_q, M_DUMMYNET); |
9bccf70c A |
1360 | continue ; |
1361 | } | |
1362 | prev = q ; | |
1363 | q = q->next ; | |
1c79356b | 1364 | } |
9bccf70c A |
1365 | if (q && prev != NULL) { /* found and not in front */ |
1366 | prev->next = q->next ; | |
1367 | q->next = fs->rq[i] ; | |
1368 | fs->rq[i] = q ; | |
1c79356b | 1369 | } |
9bccf70c A |
1370 | } |
1371 | if (q == NULL) { /* no match, need to allocate a new entry */ | |
1372 | q = create_queue(fs, i); | |
1373 | if (q != NULL) | |
91447636 | 1374 | q->id = *id ; |
9bccf70c A |
1375 | } |
1376 | return q ; | |
1377 | } | |
1378 | ||
1379 | static int | |
1380 | red_drops(struct dn_flow_set *fs, struct dn_flow_queue *q, int len) | |
1381 | { | |
1382 | /* | |
1383 | * RED algorithm | |
91447636 | 1384 | * |
9bccf70c A |
1385 | * RED calculates the average queue size (avg) using a low-pass filter |
1386 | * with an exponential weighted (w_q) moving average: | |
1387 | * avg <- (1-w_q) * avg + w_q * q_size | |
1388 | * where q_size is the queue length (measured in bytes or * packets). | |
91447636 | 1389 | * |
9bccf70c A |
1390 | * If q_size == 0, we compute the idle time for the link, and set |
1391 | * avg = (1 - w_q)^(idle/s) | |
1392 | * where s is the time needed for transmitting a medium-sized packet. | |
91447636 | 1393 | * |
9bccf70c A |
1394 | * Now, if avg < min_th the packet is enqueued. |
1395 | * If avg > max_th the packet is dropped. Otherwise, the packet is | |
1396 | * dropped with probability P function of avg. | |
91447636 | 1397 | * |
9bccf70c A |
1398 | */ |
1399 | ||
1400 | int64_t p_b = 0; | |
1401 | /* queue in bytes or packets ? */ | |
1402 | u_int q_size = (fs->flags_fs & DN_QSIZE_IS_BYTES) ? q->len_bytes : q->len; | |
1403 | ||
91447636 | 1404 | DPRINTF(("\ndummynet: %d q: %2u ", (int) curr_time, q_size)); |
9bccf70c A |
1405 | |
1406 | /* average queue size estimation */ | |
1407 | if (q_size != 0) { | |
1408 | /* | |
1409 | * queue is not empty, avg <- avg + (q_size - avg) * w_q | |
1410 | */ | |
1411 | int diff = SCALE(q_size) - q->avg; | |
1412 | int64_t v = SCALE_MUL((int64_t) diff, (int64_t) fs->w_q); | |
1413 | ||
1414 | q->avg += (int) v; | |
1415 | } else { | |
1416 | /* | |
1417 | * queue is empty, find for how long the queue has been | |
1418 | * empty and use a lookup table for computing | |
1419 | * (1 - * w_q)^(idle_time/s) where s is the time to send a | |
1420 | * (small) packet. | |
1421 | * XXX check wraps... | |
1422 | */ | |
1423 | if (q->avg) { | |
1424 | u_int t = (curr_time - q->q_time) / fs->lookup_step; | |
1425 | ||
1426 | q->avg = (t < fs->lookup_depth) ? | |
1427 | SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0; | |
1428 | } | |
1429 | } | |
91447636 | 1430 | DPRINTF(("dummynet: avg: %u ", SCALE_VAL(q->avg))); |
9bccf70c A |
1431 | |
1432 | /* should i drop ? */ | |
1433 | ||
1434 | if (q->avg < fs->min_th) { | |
1435 | q->count = -1; | |
1436 | return 0; /* accept packet ; */ | |
1437 | } | |
1438 | if (q->avg >= fs->max_th) { /* average queue >= max threshold */ | |
1439 | if (fs->flags_fs & DN_IS_GENTLE_RED) { | |
1c79356b | 1440 | /* |
9bccf70c A |
1441 | * According to Gentle-RED, if avg is greater than max_th the |
1442 | * packet is dropped with a probability | |
1443 | * p_b = c_3 * avg - c_4 | |
1444 | * where c_3 = (1 - max_p) / max_th, and c_4 = 1 - 2 * max_p | |
1c79356b | 1445 | */ |
9bccf70c A |
1446 | p_b = SCALE_MUL((int64_t) fs->c_3, (int64_t) q->avg) - fs->c_4; |
1447 | } else { | |
1448 | q->count = -1; | |
91447636 | 1449 | DPRINTF(("dummynet: - drop")); |
9bccf70c A |
1450 | return 1 ; |
1451 | } | |
1452 | } else if (q->avg > fs->min_th) { | |
1453 | /* | |
1454 | * we compute p_b using the linear dropping function p_b = c_1 * | |
1455 | * avg - c_2, where c_1 = max_p / (max_th - min_th), and c_2 = | |
1456 | * max_p * min_th / (max_th - min_th) | |
1457 | */ | |
1458 | p_b = SCALE_MUL((int64_t) fs->c_1, (int64_t) q->avg) - fs->c_2; | |
1459 | } | |
1460 | if (fs->flags_fs & DN_QSIZE_IS_BYTES) | |
1461 | p_b = (p_b * len) / fs->max_pkt_size; | |
1462 | if (++q->count == 0) | |
2d21ac55 | 1463 | q->random = MY_RANDOM & 0xffff; |
9bccf70c A |
1464 | else { |
1465 | /* | |
1466 | * q->count counts packets arrived since last drop, so a greater | |
1467 | * value of q->count means a greater packet drop probability. | |
1468 | */ | |
1469 | if (SCALE_MUL(p_b, SCALE((int64_t) q->count)) > q->random) { | |
1470 | q->count = 0; | |
91447636 | 1471 | DPRINTF(("dummynet: - red drop")); |
9bccf70c | 1472 | /* after a drop we calculate a new random value */ |
2d21ac55 | 1473 | q->random = MY_RANDOM & 0xffff; |
9bccf70c A |
1474 | return 1; /* drop */ |
1475 | } | |
1476 | } | |
1477 | /* end of RED algorithm */ | |
1478 | return 0 ; /* accept */ | |
1479 | } | |
1480 | ||
1481 | static __inline | |
1482 | struct dn_flow_set * | |
b0d623f7 | 1483 | locate_flowset(int fs_nr) |
9bccf70c | 1484 | { |
91447636 | 1485 | struct dn_flow_set *fs; |
b0d623f7 A |
1486 | SLIST_FOREACH(fs, &flowsethash[HASH(fs_nr)], next) |
1487 | if (fs->fs_nr == fs_nr) | |
1488 | return fs ; | |
1489 | ||
1490 | return (NULL); | |
1491 | } | |
9bccf70c | 1492 | |
b0d623f7 A |
1493 | static __inline struct dn_pipe * |
1494 | locate_pipe(int pipe_nr) | |
1495 | { | |
1496 | struct dn_pipe *pipe; | |
91447636 | 1497 | |
b0d623f7 A |
1498 | SLIST_FOREACH(pipe, &pipehash[HASH(pipe_nr)], next) |
1499 | if (pipe->pipe_nr == pipe_nr) | |
1500 | return (pipe); | |
91447636 | 1501 | |
b0d623f7 A |
1502 | return (NULL); |
1503 | } | |
91447636 | 1504 | |
91447636 | 1505 | |
9bccf70c A |
1506 | |
1507 | /* | |
1508 | * dummynet hook for packets. Below 'pipe' is a pipe or a queue | |
1509 | * depending on whether WF2Q or fixed bw is used. | |
91447636 A |
1510 | * |
1511 | * pipe_nr pipe or queue the packet is destined for. | |
1512 | * dir where shall we send the packet after dummynet. | |
1513 | * m the mbuf with the packet | |
1514 | * ifp the 'ifp' parameter from the caller. | |
1515 | * NULL in ip_input, destination interface in ip_output, | |
1516 | * real_dst in bdg_forward | |
1517 | * ro route parameter (only used in ip_output, NULL otherwise) | |
1518 | * dst destination address, only used by ip_output | |
1519 | * rule matching rule, in case of multiple passes | |
1520 | * flags flags from the caller, only used in ip_output | |
1521 | * | |
9bccf70c | 1522 | */ |
91447636 | 1523 | static int |
316670eb | 1524 | dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa, int client) |
9bccf70c | 1525 | { |
316670eb | 1526 | struct mbuf *head = NULL, *tail = NULL; |
91447636 A |
1527 | struct dn_pkt_tag *pkt; |
1528 | struct m_tag *mtag; | |
b0d623f7 | 1529 | struct dn_flow_set *fs = NULL; |
9bccf70c A |
1530 | struct dn_pipe *pipe ; |
1531 | u_int64_t len = m->m_pkthdr.len ; | |
1532 | struct dn_flow_queue *q = NULL ; | |
39236c6e | 1533 | int is_pipe = 0; |
0c530ab8 A |
1534 | struct timespec ts; |
1535 | struct timeval tv; | |
39236c6e A |
1536 | |
1537 | DPRINTF(("dummynet_io m: 0x%llx pipe: %d dir: %d client: %d\n", | |
1538 | (uint64_t)VM_KERNEL_ADDRPERM(m), pipe_nr, dir, client)); | |
316670eb A |
1539 | |
1540 | #if IPFIREWALL | |
91447636 | 1541 | #if IPFW2 |
316670eb A |
1542 | if (client == DN_CLIENT_IPFW) { |
1543 | ipfw_insn *cmd = fwa->fwa_ipfw_rule->cmd + fwa->fwa_ipfw_rule->act_ofs; | |
91447636 | 1544 | |
316670eb A |
1545 | if (cmd->opcode == O_LOG) |
1546 | cmd += F_LEN(cmd); | |
1547 | is_pipe = (cmd->opcode == O_PIPE); | |
1548 | } | |
91447636 | 1549 | #else |
316670eb A |
1550 | if (client == DN_CLIENT_IPFW) |
1551 | is_pipe = (fwa->fwa_ipfw_rule->fw_flg & IP_FW_F_COMMAND) == IP_FW_F_PIPE; | |
91447636 | 1552 | #endif |
316670eb A |
1553 | #endif /* IPFIREWALL */ |
1554 | ||
1555 | #if DUMMYNET | |
1556 | if (client == DN_CLIENT_PF) | |
1557 | is_pipe = fwa->fwa_flags == DN_IS_PIPE ? 1 : 0; | |
1558 | #endif /* DUMMYNET */ | |
9bccf70c A |
1559 | |
1560 | pipe_nr &= 0xffff ; | |
1561 | ||
91447636 | 1562 | lck_mtx_lock(dn_mutex); |
0c530ab8 A |
1563 | |
1564 | /* make all time measurements in milliseconds (ms) - | |
1565 | * here we convert secs and usecs to msecs (just divide the | |
1566 | * usecs and take the closest whole number). | |
1567 | */ | |
316670eb | 1568 | microuptime(&tv); |
0c530ab8 | 1569 | curr_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
91447636 A |
1570 | |
1571 | /* | |
1572 | * This is a dummynet rule, so we expect an O_PIPE or O_QUEUE rule. | |
1573 | */ | |
b0d623f7 A |
1574 | if (is_pipe) { |
1575 | pipe = locate_pipe(pipe_nr); | |
1576 | if (pipe != NULL) | |
1577 | fs = &(pipe->fs); | |
1578 | } else | |
1579 | fs = locate_flowset(pipe_nr); | |
1580 | ||
1581 | ||
1582 | if (fs == NULL){ | |
91447636 | 1583 | goto dropit ; /* this queue/pipe does not exist! */ |
b0d623f7 | 1584 | } |
9bccf70c A |
1585 | pipe = fs->pipe ; |
1586 | if (pipe == NULL) { /* must be a queue, try find a matching pipe */ | |
b0d623f7 A |
1587 | pipe = locate_pipe(fs->parent_nr); |
1588 | ||
9bccf70c A |
1589 | if (pipe != NULL) |
1590 | fs->pipe = pipe ; | |
1591 | else { | |
91447636 | 1592 | printf("dummynet: no pipe %d for queue %d, drop pkt\n", |
9bccf70c A |
1593 | fs->parent_nr, fs->fs_nr); |
1594 | goto dropit ; | |
1595 | } | |
1596 | } | |
316670eb | 1597 | q = find_queue(fs, &(fwa->fwa_id)); |
9bccf70c A |
1598 | if ( q == NULL ) |
1599 | goto dropit ; /* cannot allocate queue */ | |
1600 | /* | |
1601 | * update statistics, then check reasons to drop pkt | |
1602 | */ | |
1603 | q->tot_bytes += len ; | |
1604 | q->tot_pkts++ ; | |
2d21ac55 | 1605 | if ( fs->plr && (MY_RANDOM < fs->plr) ) |
9bccf70c A |
1606 | goto dropit ; /* random pkt drop */ |
1607 | if ( fs->flags_fs & DN_QSIZE_IS_BYTES) { | |
1608 | if (q->len_bytes > fs->qsize) | |
1609 | goto dropit ; /* queue size overflow */ | |
1610 | } else { | |
1611 | if (q->len >= fs->qsize) | |
1612 | goto dropit ; /* queue count overflow */ | |
1613 | } | |
1614 | if ( fs->flags_fs & DN_IS_RED && red_drops(fs, q, len) ) | |
1615 | goto dropit ; | |
1616 | ||
91447636 | 1617 | /* XXX expensive to zero, see if we can remove it*/ |
6d2010ae A |
1618 | mtag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET, |
1619 | sizeof(struct dn_pkt_tag), M_NOWAIT, m); | |
91447636 A |
1620 | if ( mtag == NULL ) |
1621 | goto dropit ; /* cannot allocate packet header */ | |
1622 | m_tag_prepend(m, mtag); /* attach to mbuf chain */ | |
1623 | ||
1624 | pkt = (struct dn_pkt_tag *)(mtag+1); | |
13fec989 | 1625 | bzero(pkt, sizeof(struct dn_pkt_tag)); |
9bccf70c | 1626 | /* ok, i can handle the pkt now... */ |
9bccf70c | 1627 | /* build and enqueue packet + parameters */ |
316670eb A |
1628 | /* |
1629 | * PF is checked before ipfw so remember ipfw rule only when | |
1630 | * the caller is ipfw. When the caller is PF, fwa_ipfw_rule | |
1631 | * is a fake rule just used for convenience | |
1632 | */ | |
1633 | if (client == DN_CLIENT_IPFW) | |
1634 | pkt->dn_ipfw_rule = fwa->fwa_ipfw_rule; | |
1635 | pkt->dn_pf_rule = fwa->fwa_pf_rule; | |
9bccf70c | 1636 | pkt->dn_dir = dir ; |
316670eb | 1637 | pkt->dn_client = client; |
9bccf70c | 1638 | |
316670eb | 1639 | pkt->dn_ifp = fwa->fwa_oif; |
9bccf70c | 1640 | if (dir == DN_TO_IP_OUT) { |
316670eb A |
1641 | /* |
1642 | * We need to copy *ro because for ICMP pkts (and maybe others) | |
1643 | * the caller passed a pointer into the stack; dst might also be | |
1644 | * a pointer into *ro so it needs to be updated. | |
1645 | */ | |
1646 | if (fwa->fwa_ro) { | |
39236c6e | 1647 | route_copyout(&pkt->dn_ro, fwa->fwa_ro, sizeof (pkt->dn_ro)); |
316670eb A |
1648 | } |
1649 | if (fwa->fwa_dst) { | |
1650 | if (fwa->fwa_dst == (struct sockaddr_in *)&fwa->fwa_ro->ro_dst) /* dst points into ro */ | |
1651 | fwa->fwa_dst = (struct sockaddr_in *)&(pkt->dn_ro.ro_dst) ; | |
1652 | ||
1653 | bcopy (fwa->fwa_dst, &pkt->dn_dst, sizeof(pkt->dn_dst)); | |
1654 | } | |
1655 | } else if (dir == DN_TO_IP6_OUT) { | |
1656 | if (fwa->fwa_ro6) { | |
39236c6e A |
1657 | route_copyout((struct route *)&pkt->dn_ro6, |
1658 | (struct route *)fwa->fwa_ro6, sizeof (pkt->dn_ro6)); | |
316670eb A |
1659 | } |
1660 | if (fwa->fwa_ro6_pmtu) { | |
39236c6e A |
1661 | route_copyout((struct route *)&pkt->dn_ro6_pmtu, |
1662 | (struct route *)fwa->fwa_ro6_pmtu, sizeof (pkt->dn_ro6_pmtu)); | |
316670eb A |
1663 | } |
1664 | if (fwa->fwa_dst6) { | |
1665 | if (fwa->fwa_dst6 == (struct sockaddr_in6 *)&fwa->fwa_ro6->ro_dst) /* dst points into ro */ | |
1666 | fwa->fwa_dst6 = (struct sockaddr_in6 *)&(pkt->dn_ro6.ro_dst) ; | |
1667 | ||
1668 | bcopy (fwa->fwa_dst6, &pkt->dn_dst6, sizeof(pkt->dn_dst6)); | |
1669 | } | |
1670 | pkt->dn_origifp = fwa->fwa_origifp; | |
1671 | pkt->dn_mtu = fwa->fwa_mtu; | |
1672 | pkt->dn_alwaysfrag = fwa->fwa_alwaysfrag; | |
1673 | pkt->dn_unfragpartlen = fwa->fwa_unfragpartlen; | |
1674 | if (fwa->fwa_exthdrs) { | |
1675 | bcopy (fwa->fwa_exthdrs, &pkt->dn_exthdrs, sizeof(pkt->dn_exthdrs)); | |
1676 | /* | |
1677 | * Need to zero out the source structure so the mbufs | |
1678 | * won't be freed by ip6_output() | |
1679 | */ | |
1680 | bzero(fwa->fwa_exthdrs, sizeof(struct ip6_exthdrs)); | |
1681 | } | |
1682 | } | |
1683 | if (dir == DN_TO_IP_OUT || dir == DN_TO_IP6_OUT) { | |
1684 | pkt->dn_flags = fwa->fwa_oflags; | |
1685 | if (fwa->fwa_ipoa != NULL) | |
1686 | pkt->dn_ipoa = *(fwa->fwa_ipoa); | |
1687 | } | |
9bccf70c | 1688 | if (q->head == NULL) |
91447636 | 1689 | q->head = m; |
9bccf70c | 1690 | else |
91447636 A |
1691 | q->tail->m_nextpkt = m; |
1692 | q->tail = m; | |
9bccf70c A |
1693 | q->len++; |
1694 | q->len_bytes += len ; | |
1695 | ||
91447636 | 1696 | if ( q->head != m ) /* flow was not idle, we are done */ |
9bccf70c A |
1697 | goto done; |
1698 | /* | |
1699 | * If we reach this point the flow was previously idle, so we need | |
1700 | * to schedule it. This involves different actions for fixed-rate or | |
1701 | * WF2Q queues. | |
1702 | */ | |
91447636 | 1703 | if (is_pipe) { |
9bccf70c A |
1704 | /* |
1705 | * Fixed-rate queue: just insert into the ready_heap. | |
1706 | */ | |
1707 | dn_key t = 0 ; | |
91447636 A |
1708 | if (pipe->bandwidth) |
1709 | t = SET_TICKS(m, q, pipe); | |
9bccf70c | 1710 | q->sched_time = curr_time ; |
316670eb | 1711 | if (t == 0) /* must process it now */ |
b0d623f7 | 1712 | ready_event( q , &head, &tail ); |
9bccf70c A |
1713 | else |
1714 | heap_insert(&ready_heap, curr_time + t , q ); | |
1715 | } else { | |
1716 | /* | |
1717 | * WF2Q. First, compute start time S: if the flow was idle (S=F+1) | |
1718 | * set S to the virtual time V for the controlling pipe, and update | |
1719 | * the sum of weights for the pipe; otherwise, remove flow from | |
1720 | * idle_heap and set S to max(F,V). | |
1721 | * Second, compute finish time F = S + len/weight. | |
1722 | * Third, if pipe was idle, update V=max(S, V). | |
1723 | * Fourth, count one more backlogged flow. | |
1724 | */ | |
1725 | if (DN_KEY_GT(q->S, q->F)) { /* means timestamps are invalid */ | |
1726 | q->S = pipe->V ; | |
1727 | pipe->sum += fs->weight ; /* add weight of new queue */ | |
1728 | } else { | |
1729 | heap_extract(&(pipe->idle_heap), q); | |
1730 | q->S = MAX64(q->F, pipe->V ) ; | |
1731 | } | |
1732 | q->F = q->S + ( len<<MY_M )/(u_int64_t) fs->weight; | |
1733 | ||
1734 | if (pipe->not_eligible_heap.elements == 0 && | |
1735 | pipe->scheduler_heap.elements == 0) | |
1736 | pipe->V = MAX64 ( q->S, pipe->V ); | |
1737 | fs->backlogged++ ; | |
1738 | /* | |
1739 | * Look at eligibility. A flow is not eligibile if S>V (when | |
1740 | * this happens, it means that there is some other flow already | |
1741 | * scheduled for the same pipe, so the scheduler_heap cannot be | |
1742 | * empty). If the flow is not eligible we just store it in the | |
1743 | * not_eligible_heap. Otherwise, we store in the scheduler_heap | |
1744 | * and possibly invoke ready_event_wfq() right now if there is | |
1745 | * leftover credit. | |
1746 | * Note that for all flows in scheduler_heap (SCH), S_i <= V, | |
1747 | * and for all flows in not_eligible_heap (NEH), S_i > V . | |
1748 | * So when we need to compute max( V, min(S_i) ) forall i in SCH+NEH, | |
1749 | * we only need to look into NEH. | |
1750 | */ | |
1751 | if (DN_KEY_GT(q->S, pipe->V) ) { /* not eligible */ | |
1752 | if (pipe->scheduler_heap.elements == 0) | |
91447636 | 1753 | printf("dummynet: ++ ouch! not eligible but empty scheduler!\n"); |
9bccf70c A |
1754 | heap_insert(&(pipe->not_eligible_heap), q->S, q); |
1755 | } else { | |
1756 | heap_insert(&(pipe->scheduler_heap), q->F, q); | |
1757 | if (pipe->numbytes >= 0) { /* pipe is idle */ | |
1758 | if (pipe->scheduler_heap.elements != 1) | |
91447636 A |
1759 | printf("dummynet: OUCH! pipe should have been idle!\n"); |
1760 | DPRINTF(("dummynet: waking up pipe %d at %d\n", | |
1761 | pipe->pipe_nr, (int)(q->F >> MY_M))); | |
9bccf70c | 1762 | pipe->sched_time = curr_time ; |
b0d623f7 | 1763 | ready_event_wfq(pipe, &head, &tail); |
1c79356b | 1764 | } |
9bccf70c A |
1765 | } |
1766 | } | |
1767 | done: | |
0c530ab8 A |
1768 | /* start the timer and set global if not already set */ |
1769 | if (!timer_enabled) { | |
1770 | ts.tv_sec = 0; | |
1771 | ts.tv_nsec = 1 * 1000000; // 1ms | |
1772 | timer_enabled = 1; | |
1773 | bsd_timeout(dummynet, NULL, &ts); | |
6d2010ae | 1774 | } |
0c530ab8 | 1775 | |
91447636 | 1776 | lck_mtx_unlock(dn_mutex); |
316670eb A |
1777 | |
1778 | if (head != NULL) { | |
b0d623f7 | 1779 | dummynet_send(head); |
316670eb | 1780 | } |
b0d623f7 | 1781 | |
9bccf70c A |
1782 | return 0; |
1783 | ||
1784 | dropit: | |
9bccf70c A |
1785 | if (q) |
1786 | q->drops++ ; | |
91447636 | 1787 | lck_mtx_unlock(dn_mutex); |
9bccf70c | 1788 | m_freem(m); |
91447636 | 1789 | return ( (fs && (fs->flags_fs & DN_NOERROR)) ? 0 : ENOBUFS); |
9bccf70c A |
1790 | } |
1791 | ||
1792 | /* | |
39236c6e | 1793 | * Below, the ROUTE_RELEASE is only needed when (pkt->dn_dir == DN_TO_IP_OUT) |
9bccf70c A |
1794 | * Doing this would probably save us the initial bzero of dn_pkt |
1795 | */ | |
b0d623f7 | 1796 | #define DN_FREE_PKT(_m) do { \ |
91447636 | 1797 | struct m_tag *tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET, NULL); \ |
b0d623f7 | 1798 | if (tag) { \ |
91447636 | 1799 | struct dn_pkt_tag *n = (struct dn_pkt_tag *)(tag+1); \ |
39236c6e | 1800 | ROUTE_RELEASE(&n->dn_ro); \ |
b0d623f7 A |
1801 | } \ |
1802 | m_tag_delete(_m, tag); \ | |
1803 | m_freem(_m); \ | |
91447636 | 1804 | } while (0) |
9bccf70c A |
1805 | |
1806 | /* | |
1807 | * Dispose all packets and flow_queues on a flow_set. | |
1808 | * If all=1, also remove red lookup table and other storage, | |
1809 | * including the descriptor itself. | |
1810 | * For the one in dn_pipe MUST also cleanup ready_heap... | |
1811 | */ | |
1812 | static void | |
1813 | purge_flow_set(struct dn_flow_set *fs, int all) | |
1814 | { | |
9bccf70c A |
1815 | struct dn_flow_queue *q, *qn ; |
1816 | int i ; | |
1817 | ||
91447636 A |
1818 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); |
1819 | ||
9bccf70c A |
1820 | for (i = 0 ; i <= fs->rq_size ; i++ ) { |
1821 | for (q = fs->rq[i] ; q ; q = qn ) { | |
91447636 A |
1822 | struct mbuf *m, *mnext; |
1823 | ||
1824 | mnext = q->head; | |
1825 | while ((m = mnext) != NULL) { | |
1826 | mnext = m->m_nextpkt; | |
1827 | DN_FREE_PKT(m); | |
1828 | } | |
9bccf70c | 1829 | qn = q->next ; |
91447636 | 1830 | FREE(q, M_DUMMYNET); |
9bccf70c A |
1831 | } |
1832 | fs->rq[i] = NULL ; | |
1833 | } | |
1834 | fs->rq_elements = 0 ; | |
1835 | if (all) { | |
1836 | /* RED - free lookup table */ | |
1837 | if (fs->w_q_lookup) | |
91447636 | 1838 | FREE(fs->w_q_lookup, M_DUMMYNET); |
9bccf70c | 1839 | if (fs->rq) |
91447636 | 1840 | FREE(fs->rq, M_DUMMYNET); |
9bccf70c A |
1841 | /* if this fs is not part of a pipe, free it */ |
1842 | if (fs->pipe && fs != &(fs->pipe->fs) ) | |
91447636 | 1843 | FREE(fs, M_DUMMYNET); |
9bccf70c A |
1844 | } |
1845 | } | |
1846 | ||
1847 | /* | |
1848 | * Dispose all packets queued on a pipe (not a flow_set). | |
1849 | * Also free all resources associated to a pipe, which is about | |
1850 | * to be deleted. | |
1851 | */ | |
1852 | static void | |
1853 | purge_pipe(struct dn_pipe *pipe) | |
1854 | { | |
91447636 | 1855 | struct mbuf *m, *mnext; |
9bccf70c A |
1856 | |
1857 | purge_flow_set( &(pipe->fs), 1 ); | |
1858 | ||
91447636 A |
1859 | mnext = pipe->head; |
1860 | while ((m = mnext) != NULL) { | |
1861 | mnext = m->m_nextpkt; | |
1862 | DN_FREE_PKT(m); | |
1863 | } | |
9bccf70c A |
1864 | |
1865 | heap_free( &(pipe->scheduler_heap) ); | |
1866 | heap_free( &(pipe->not_eligible_heap) ); | |
1867 | heap_free( &(pipe->idle_heap) ); | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * Delete all pipes and heaps returning memory. Must also | |
1872 | * remove references from all ipfw rules to all pipes. | |
1873 | */ | |
1874 | static void | |
2d21ac55 | 1875 | dummynet_flush(void) |
9bccf70c | 1876 | { |
b0d623f7 A |
1877 | struct dn_pipe *pipe, *pipe1; |
1878 | struct dn_flow_set *fs, *fs1; | |
1879 | int i; | |
9bccf70c | 1880 | |
91447636 | 1881 | lck_mtx_lock(dn_mutex); |
9bccf70c | 1882 | |
316670eb A |
1883 | #if IPFW2 |
1884 | /* remove all references to pipes ...*/ | |
1885 | flush_pipe_ptrs(NULL); | |
1886 | #endif /* IPFW2 */ | |
1887 | ||
b0d623f7 A |
1888 | /* Free heaps so we don't have unwanted events. */ |
1889 | heap_free(&ready_heap); | |
1890 | heap_free(&wfq_ready_heap); | |
1891 | heap_free(&extract_heap); | |
91447636 | 1892 | |
b0d623f7 A |
1893 | /* |
1894 | * Now purge all queued pkts and delete all pipes. | |
1895 | * | |
1896 | * XXXGL: can we merge the for(;;) cycles into one or not? | |
1897 | */ | |
1898 | for (i = 0; i < HASHSIZE; i++) | |
1899 | SLIST_FOREACH_SAFE(fs, &flowsethash[i], next, fs1) { | |
1900 | SLIST_REMOVE(&flowsethash[i], fs, dn_flow_set, next); | |
1901 | purge_flow_set(fs, 1); | |
1902 | } | |
1903 | for (i = 0; i < HASHSIZE; i++) | |
1904 | SLIST_FOREACH_SAFE(pipe, &pipehash[i], next, pipe1) { | |
1905 | SLIST_REMOVE(&pipehash[i], pipe, dn_pipe, next); | |
1906 | purge_pipe(pipe); | |
1907 | FREE(pipe, M_DUMMYNET); | |
1908 | } | |
91447636 | 1909 | lck_mtx_unlock(dn_mutex); |
9bccf70c A |
1910 | } |
1911 | ||
1912 | ||
9bccf70c | 1913 | static void |
316670eb | 1914 | dn_ipfw_rule_delete_fs(struct dn_flow_set *fs, void *r) |
9bccf70c A |
1915 | { |
1916 | int i ; | |
1917 | struct dn_flow_queue *q ; | |
91447636 | 1918 | struct mbuf *m ; |
9bccf70c A |
1919 | |
1920 | for (i = 0 ; i <= fs->rq_size ; i++) /* last one is ovflow */ | |
1921 | for (q = fs->rq[i] ; q ; q = q->next ) | |
91447636 A |
1922 | for (m = q->head ; m ; m = m->m_nextpkt ) { |
1923 | struct dn_pkt_tag *pkt = dn_tag_get(m) ; | |
316670eb A |
1924 | if (pkt->dn_ipfw_rule == r) |
1925 | pkt->dn_ipfw_rule = &default_rule ; | |
91447636 | 1926 | } |
9bccf70c A |
1927 | } |
1928 | /* | |
1929 | * when a firewall rule is deleted, scan all queues and remove the flow-id | |
1930 | * from packets matching this rule. | |
1931 | */ | |
1932 | void | |
316670eb | 1933 | dn_ipfw_rule_delete(void *r) |
9bccf70c A |
1934 | { |
1935 | struct dn_pipe *p ; | |
9bccf70c | 1936 | struct dn_flow_set *fs ; |
91447636 A |
1937 | struct dn_pkt_tag *pkt ; |
1938 | struct mbuf *m ; | |
b0d623f7 | 1939 | int i; |
91447636 A |
1940 | |
1941 | lck_mtx_lock(dn_mutex); | |
9bccf70c A |
1942 | |
1943 | /* | |
1944 | * If the rule references a queue (dn_flow_set), then scan | |
1945 | * the flow set, otherwise scan pipes. Should do either, but doing | |
1946 | * both does not harm. | |
1947 | */ | |
b0d623f7 A |
1948 | for (i = 0; i < HASHSIZE; i++) |
1949 | SLIST_FOREACH(fs, &flowsethash[i], next) | |
316670eb | 1950 | dn_ipfw_rule_delete_fs(fs, r); |
b0d623f7 A |
1951 | |
1952 | for (i = 0; i < HASHSIZE; i++) | |
1953 | SLIST_FOREACH(p, &pipehash[i], next) { | |
1954 | fs = &(p->fs); | |
316670eb | 1955 | dn_ipfw_rule_delete_fs(fs, r); |
b0d623f7 A |
1956 | for (m = p->head ; m ; m = m->m_nextpkt ) { |
1957 | pkt = dn_tag_get(m); | |
316670eb A |
1958 | if (pkt->dn_ipfw_rule == r) |
1959 | pkt->dn_ipfw_rule = &default_rule; | |
b0d623f7 | 1960 | } |
91447636 | 1961 | } |
b0d623f7 | 1962 | lck_mtx_unlock(dn_mutex); |
9bccf70c A |
1963 | } |
1964 | ||
1965 | /* | |
1966 | * setup RED parameters | |
1967 | */ | |
1968 | static int | |
91447636 | 1969 | config_red(struct dn_flow_set *p, struct dn_flow_set * x) |
9bccf70c A |
1970 | { |
1971 | int i; | |
1972 | ||
1973 | x->w_q = p->w_q; | |
1974 | x->min_th = SCALE(p->min_th); | |
1975 | x->max_th = SCALE(p->max_th); | |
1976 | x->max_p = p->max_p; | |
1977 | ||
1978 | x->c_1 = p->max_p / (p->max_th - p->min_th); | |
1979 | x->c_2 = SCALE_MUL(x->c_1, SCALE(p->min_th)); | |
1980 | if (x->flags_fs & DN_IS_GENTLE_RED) { | |
1981 | x->c_3 = (SCALE(1) - p->max_p) / p->max_th; | |
1982 | x->c_4 = (SCALE(1) - 2 * p->max_p); | |
1983 | } | |
1984 | ||
1985 | /* if the lookup table already exist, free and create it again */ | |
91447636 A |
1986 | if (x->w_q_lookup) { |
1987 | FREE(x->w_q_lookup, M_DUMMYNET); | |
1988 | x->w_q_lookup = NULL ; | |
1989 | } | |
9bccf70c | 1990 | if (red_lookup_depth == 0) { |
91447636 A |
1991 | printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth must be > 0\n"); |
1992 | FREE(x, M_DUMMYNET); | |
9bccf70c A |
1993 | return EINVAL; |
1994 | } | |
1995 | x->lookup_depth = red_lookup_depth; | |
1996 | x->w_q_lookup = (u_int *) _MALLOC(x->lookup_depth * sizeof(int), | |
91447636 | 1997 | M_DUMMYNET, M_DONTWAIT); |
9bccf70c | 1998 | if (x->w_q_lookup == NULL) { |
91447636 A |
1999 | printf("dummynet: sorry, cannot allocate red lookup table\n"); |
2000 | FREE(x, M_DUMMYNET); | |
9bccf70c A |
2001 | return ENOSPC; |
2002 | } | |
2003 | ||
2004 | /* fill the lookup table with (1 - w_q)^x */ | |
2005 | x->lookup_step = p->lookup_step ; | |
2006 | x->lookup_weight = p->lookup_weight ; | |
2007 | x->w_q_lookup[0] = SCALE(1) - x->w_q; | |
2008 | for (i = 1; i < x->lookup_depth; i++) | |
2009 | x->w_q_lookup[i] = SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight); | |
2010 | if (red_avg_pkt_size < 1) | |
2011 | red_avg_pkt_size = 512 ; | |
2012 | x->avg_pkt_size = red_avg_pkt_size ; | |
2013 | if (red_max_pkt_size < 1) | |
2014 | red_max_pkt_size = 1500 ; | |
2015 | x->max_pkt_size = red_max_pkt_size ; | |
2016 | return 0 ; | |
2017 | } | |
2018 | ||
2019 | static int | |
2020 | alloc_hash(struct dn_flow_set *x, struct dn_flow_set *pfs) | |
2021 | { | |
2022 | if (x->flags_fs & DN_HAVE_FLOW_MASK) { /* allocate some slots */ | |
2023 | int l = pfs->rq_size; | |
2024 | ||
2025 | if (l == 0) | |
2026 | l = dn_hash_size; | |
2027 | if (l < 4) | |
2028 | l = 4; | |
91447636 A |
2029 | else if (l > DN_MAX_HASH_SIZE) |
2030 | l = DN_MAX_HASH_SIZE; | |
9bccf70c A |
2031 | x->rq_size = l; |
2032 | } else /* one is enough for null mask */ | |
2033 | x->rq_size = 1; | |
2034 | x->rq = _MALLOC((1 + x->rq_size) * sizeof(struct dn_flow_queue *), | |
91447636 | 2035 | M_DUMMYNET, M_DONTWAIT | M_ZERO); |
9bccf70c | 2036 | if (x->rq == NULL) { |
91447636 | 2037 | printf("dummynet: sorry, cannot allocate queue\n"); |
9bccf70c A |
2038 | return ENOSPC; |
2039 | } | |
9bccf70c A |
2040 | x->rq_elements = 0; |
2041 | return 0 ; | |
2042 | } | |
2043 | ||
2044 | static void | |
2045 | set_fs_parms(struct dn_flow_set *x, struct dn_flow_set *src) | |
2046 | { | |
2047 | x->flags_fs = src->flags_fs; | |
2048 | x->qsize = src->qsize; | |
2049 | x->plr = src->plr; | |
2050 | x->flow_mask = src->flow_mask; | |
2051 | if (x->flags_fs & DN_QSIZE_IS_BYTES) { | |
2052 | if (x->qsize > 1024*1024) | |
2053 | x->qsize = 1024*1024 ; | |
2054 | } else { | |
2055 | if (x->qsize == 0) | |
316670eb | 2056 | x->qsize = 50 ; |
9bccf70c | 2057 | if (x->qsize > 100) |
316670eb | 2058 | x->qsize = 50 ; |
9bccf70c A |
2059 | } |
2060 | /* configuring RED */ | |
2061 | if ( x->flags_fs & DN_IS_RED ) | |
2062 | config_red(src, x) ; /* XXX should check errors */ | |
2063 | } | |
2064 | ||
2065 | /* | |
2066 | * setup pipe or queue parameters. | |
2067 | */ | |
2068 | ||
91447636 | 2069 | static int |
9bccf70c A |
2070 | config_pipe(struct dn_pipe *p) |
2071 | { | |
91447636 | 2072 | int i, r; |
9bccf70c | 2073 | struct dn_flow_set *pfs = &(p->fs); |
91447636 | 2074 | struct dn_flow_queue *q; |
9bccf70c | 2075 | |
91447636 A |
2076 | /* |
2077 | * The config program passes parameters as follows: | |
9bccf70c A |
2078 | * bw = bits/second (0 means no limits), |
2079 | * delay = ms, must be translated into ticks. | |
2080 | * qsize = slots/bytes | |
91447636 | 2081 | */ |
0c530ab8 | 2082 | p->delay = ( p->delay * (hz*10) ) / 1000 ; |
9bccf70c A |
2083 | /* We need either a pipe number or a flow_set number */ |
2084 | if (p->pipe_nr == 0 && pfs->fs_nr == 0) | |
2085 | return EINVAL ; | |
2086 | if (p->pipe_nr != 0 && pfs->fs_nr != 0) | |
2087 | return EINVAL ; | |
2088 | if (p->pipe_nr != 0) { /* this is a pipe */ | |
b0d623f7 | 2089 | struct dn_pipe *x, *b; |
91447636 A |
2090 | |
2091 | lck_mtx_lock(dn_mutex); | |
9bccf70c | 2092 | |
b0d623f7 A |
2093 | /* locate pipe */ |
2094 | b = locate_pipe(p->pipe_nr); | |
2095 | ||
9bccf70c | 2096 | if (b == NULL || b->pipe_nr != p->pipe_nr) { /* new pipe */ |
91447636 | 2097 | x = _MALLOC(sizeof(struct dn_pipe), M_DUMMYNET, M_DONTWAIT | M_ZERO) ; |
9bccf70c | 2098 | if (x == NULL) { |
91447636 A |
2099 | lck_mtx_unlock(dn_mutex); |
2100 | printf("dummynet: no memory for new pipe\n"); | |
9bccf70c | 2101 | return ENOSPC; |
1c79356b | 2102 | } |
9bccf70c A |
2103 | x->pipe_nr = p->pipe_nr; |
2104 | x->fs.pipe = x ; | |
2105 | /* idle_heap is the only one from which we extract from the middle. | |
2106 | */ | |
2107 | x->idle_heap.size = x->idle_heap.elements = 0 ; | |
b0d623f7 | 2108 | x->idle_heap.offset=offsetof(struct dn_flow_queue, heap_pos); |
91447636 | 2109 | } else { |
9bccf70c | 2110 | x = b; |
91447636 A |
2111 | /* Flush accumulated credit for all queues */ |
2112 | for (i = 0; i <= x->fs.rq_size; i++) | |
2113 | for (q = x->fs.rq[i]; q; q = q->next) | |
2114 | q->numbytes = 0; | |
2115 | } | |
9bccf70c | 2116 | |
91447636 | 2117 | x->bandwidth = p->bandwidth ; |
9bccf70c A |
2118 | x->numbytes = 0; /* just in case... */ |
2119 | bcopy(p->if_name, x->if_name, sizeof(p->if_name) ); | |
2120 | x->ifp = NULL ; /* reset interface ptr */ | |
91447636 | 2121 | x->delay = p->delay ; |
9bccf70c | 2122 | set_fs_parms(&(x->fs), pfs); |
1c79356b | 2123 | |
1c79356b | 2124 | |
9bccf70c | 2125 | if ( x->fs.rq == NULL ) { /* a new pipe */ |
91447636 A |
2126 | r = alloc_hash(&(x->fs), pfs) ; |
2127 | if (r) { | |
2128 | lck_mtx_unlock(dn_mutex); | |
2129 | FREE(x, M_DUMMYNET); | |
2130 | return r ; | |
9bccf70c | 2131 | } |
b0d623f7 A |
2132 | SLIST_INSERT_HEAD(&pipehash[HASH(x->pipe_nr)], |
2133 | x, next); | |
9bccf70c | 2134 | } |
91447636 | 2135 | lck_mtx_unlock(dn_mutex); |
9bccf70c | 2136 | } else { /* config queue */ |
b0d623f7 | 2137 | struct dn_flow_set *x, *b ; |
9bccf70c | 2138 | |
91447636 | 2139 | lck_mtx_lock(dn_mutex); |
9bccf70c | 2140 | /* locate flow_set */ |
b0d623f7 | 2141 | b = locate_flowset(pfs->fs_nr); |
1c79356b | 2142 | |
9bccf70c | 2143 | if (b == NULL || b->fs_nr != pfs->fs_nr) { /* new */ |
91447636 A |
2144 | if (pfs->parent_nr == 0) { /* need link to a pipe */ |
2145 | lck_mtx_unlock(dn_mutex); | |
2146 | return EINVAL ; | |
2147 | } | |
2148 | x = _MALLOC(sizeof(struct dn_flow_set), M_DUMMYNET, M_DONTWAIT | M_ZERO); | |
9bccf70c | 2149 | if (x == NULL) { |
91447636 A |
2150 | lck_mtx_unlock(dn_mutex); |
2151 | printf("dummynet: no memory for new flow_set\n"); | |
2152 | return ENOSPC; | |
1c79356b | 2153 | } |
9bccf70c A |
2154 | x->fs_nr = pfs->fs_nr; |
2155 | x->parent_nr = pfs->parent_nr; | |
2156 | x->weight = pfs->weight ; | |
2157 | if (x->weight == 0) | |
2158 | x->weight = 1 ; | |
2159 | else if (x->weight > 100) | |
2160 | x->weight = 100 ; | |
2161 | } else { | |
2162 | /* Change parent pipe not allowed; must delete and recreate */ | |
91447636 A |
2163 | if (pfs->parent_nr != 0 && b->parent_nr != pfs->parent_nr) { |
2164 | lck_mtx_unlock(dn_mutex); | |
2165 | return EINVAL ; | |
2166 | } | |
9bccf70c | 2167 | x = b; |
1c79356b | 2168 | } |
9bccf70c A |
2169 | set_fs_parms(x, pfs); |
2170 | ||
2171 | if ( x->rq == NULL ) { /* a new flow_set */ | |
91447636 A |
2172 | r = alloc_hash(x, pfs) ; |
2173 | if (r) { | |
2174 | lck_mtx_unlock(dn_mutex); | |
2175 | FREE(x, M_DUMMYNET); | |
2176 | return r ; | |
9bccf70c | 2177 | } |
b0d623f7 A |
2178 | SLIST_INSERT_HEAD(&flowsethash[HASH(x->fs_nr)], |
2179 | x, next); | |
9bccf70c | 2180 | } |
91447636 | 2181 | lck_mtx_unlock(dn_mutex); |
9bccf70c A |
2182 | } |
2183 | return 0 ; | |
2184 | } | |
2185 | ||
2186 | /* | |
2187 | * Helper function to remove from a heap queues which are linked to | |
2188 | * a flow_set about to be deleted. | |
2189 | */ | |
2190 | static void | |
2191 | fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs) | |
2192 | { | |
2193 | int i = 0, found = 0 ; | |
2194 | for (; i < h->elements ;) | |
2195 | if ( ((struct dn_flow_queue *)h->p[i].object)->fs == fs) { | |
2196 | h->elements-- ; | |
2197 | h->p[i] = h->p[h->elements] ; | |
2198 | found++ ; | |
2199 | } else | |
2200 | i++ ; | |
2201 | if (found) | |
2202 | heapify(h); | |
1c79356b A |
2203 | } |
2204 | ||
9bccf70c A |
2205 | /* |
2206 | * helper function to remove a pipe from a heap (can be there at most once) | |
2207 | */ | |
2208 | static void | |
2209 | pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p) | |
2210 | { | |
2211 | if (h->elements > 0) { | |
2212 | int i = 0 ; | |
2213 | for (i=0; i < h->elements ; i++ ) { | |
2214 | if (h->p[i].object == p) { /* found it */ | |
2215 | h->elements-- ; | |
2216 | h->p[i] = h->p[h->elements] ; | |
2217 | heapify(h); | |
2218 | break ; | |
2219 | } | |
2220 | } | |
2221 | } | |
2222 | } | |
2223 | ||
2224 | /* | |
2225 | * drain all queues. Called in case of severe mbuf shortage. | |
2226 | */ | |
1c79356b | 2227 | void |
2d21ac55 | 2228 | dummynet_drain(void) |
1c79356b | 2229 | { |
9bccf70c A |
2230 | struct dn_flow_set *fs; |
2231 | struct dn_pipe *p; | |
91447636 | 2232 | struct mbuf *m, *mnext; |
b0d623f7 | 2233 | int i; |
91447636 A |
2234 | |
2235 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); | |
9bccf70c A |
2236 | |
2237 | heap_free(&ready_heap); | |
2238 | heap_free(&wfq_ready_heap); | |
2239 | heap_free(&extract_heap); | |
2240 | /* remove all references to this pipe from flow_sets */ | |
b0d623f7 A |
2241 | for (i = 0; i < HASHSIZE; i++) |
2242 | SLIST_FOREACH(fs, &flowsethash[i], next) | |
2243 | purge_flow_set(fs, 0); | |
9bccf70c | 2244 | |
b0d623f7 A |
2245 | for (i = 0; i < HASHSIZE; i++) |
2246 | SLIST_FOREACH(p, &pipehash[i], next) { | |
2247 | purge_flow_set(&(p->fs), 0); | |
91447636 A |
2248 | |
2249 | mnext = p->head; | |
2250 | while ((m = mnext) != NULL) { | |
2251 | mnext = m->m_nextpkt; | |
2252 | DN_FREE_PKT(m); | |
2253 | } | |
9bccf70c A |
2254 | p->head = p->tail = NULL ; |
2255 | } | |
1c79356b A |
2256 | } |
2257 | ||
9bccf70c A |
2258 | /* |
2259 | * Fully delete a pipe or a queue, cleaning up associated info. | |
2260 | */ | |
91447636 | 2261 | static int |
9bccf70c A |
2262 | delete_pipe(struct dn_pipe *p) |
2263 | { | |
9bccf70c A |
2264 | if (p->pipe_nr == 0 && p->fs.fs_nr == 0) |
2265 | return EINVAL ; | |
2266 | if (p->pipe_nr != 0 && p->fs.fs_nr != 0) | |
2267 | return EINVAL ; | |
2268 | if (p->pipe_nr != 0) { /* this is an old-style pipe */ | |
b0d623f7 | 2269 | struct dn_pipe *b; |
9bccf70c | 2270 | struct dn_flow_set *fs; |
b0d623f7 | 2271 | int i; |
1c79356b | 2272 | |
91447636 | 2273 | lck_mtx_lock(dn_mutex); |
9bccf70c | 2274 | /* locate pipe */ |
b0d623f7 A |
2275 | b = locate_pipe(p->pipe_nr); |
2276 | if(b == NULL){ | |
91447636 | 2277 | lck_mtx_unlock(dn_mutex); |
9bccf70c | 2278 | return EINVAL ; /* not found */ |
91447636 | 2279 | } |
1c79356b | 2280 | |
b0d623f7 A |
2281 | /* Unlink from list of pipes. */ |
2282 | SLIST_REMOVE(&pipehash[HASH(b->pipe_nr)], b, dn_pipe, next); | |
2283 | ||
316670eb | 2284 | #if IPFW2 |
9bccf70c | 2285 | /* remove references to this pipe from the ip_fw rules. */ |
91447636 | 2286 | flush_pipe_ptrs(&(b->fs)); |
316670eb | 2287 | #endif /* IPFW2 */ |
9bccf70c | 2288 | |
b0d623f7 A |
2289 | /* Remove all references to this pipe from flow_sets. */ |
2290 | for (i = 0; i < HASHSIZE; i++) | |
2291 | SLIST_FOREACH(fs, &flowsethash[i], next) | |
2292 | if (fs->pipe == b) { | |
2293 | printf("dummynet: ++ ref to pipe %d from fs %d\n", | |
2294 | p->pipe_nr, fs->fs_nr); | |
2295 | fs->pipe = NULL ; | |
2296 | purge_flow_set(fs, 0); | |
2297 | } | |
9bccf70c | 2298 | fs_remove_from_heap(&ready_heap, &(b->fs)); |
b0d623f7 | 2299 | |
9bccf70c A |
2300 | purge_pipe(b); /* remove all data associated to this pipe */ |
2301 | /* remove reference to here from extract_heap and wfq_ready_heap */ | |
2302 | pipe_remove_from_heap(&extract_heap, b); | |
2303 | pipe_remove_from_heap(&wfq_ready_heap, b); | |
91447636 A |
2304 | lck_mtx_unlock(dn_mutex); |
2305 | ||
2306 | FREE(b, M_DUMMYNET); | |
9bccf70c | 2307 | } else { /* this is a WF2Q queue (dn_flow_set) */ |
b0d623f7 | 2308 | struct dn_flow_set *b; |
9bccf70c | 2309 | |
91447636 | 2310 | lck_mtx_lock(dn_mutex); |
9bccf70c | 2311 | /* locate set */ |
b0d623f7 A |
2312 | b = locate_flowset(p->fs.fs_nr); |
2313 | if (b == NULL) { | |
91447636 | 2314 | lck_mtx_unlock(dn_mutex); |
9bccf70c | 2315 | return EINVAL ; /* not found */ |
91447636 | 2316 | } |
9bccf70c | 2317 | |
316670eb | 2318 | #if IPFW2 |
9bccf70c | 2319 | /* remove references to this flow_set from the ip_fw rules. */ |
91447636 | 2320 | flush_pipe_ptrs(b); |
316670eb | 2321 | #endif /* IPFW2 */ |
9bccf70c | 2322 | |
b0d623f7 A |
2323 | /* Unlink from list of flowsets. */ |
2324 | SLIST_REMOVE( &flowsethash[HASH(b->fs_nr)], b, dn_flow_set, next); | |
2325 | ||
9bccf70c A |
2326 | if (b->pipe != NULL) { |
2327 | /* Update total weight on parent pipe and cleanup parent heaps */ | |
2328 | b->pipe->sum -= b->weight * b->backlogged ; | |
2329 | fs_remove_from_heap(&(b->pipe->not_eligible_heap), b); | |
2330 | fs_remove_from_heap(&(b->pipe->scheduler_heap), b); | |
2331 | #if 1 /* XXX should i remove from idle_heap as well ? */ | |
2332 | fs_remove_from_heap(&(b->pipe->idle_heap), b); | |
2333 | #endif | |
2334 | } | |
2335 | purge_flow_set(b, 1); | |
91447636 | 2336 | lck_mtx_unlock(dn_mutex); |
9bccf70c A |
2337 | } |
2338 | return 0 ; | |
2339 | } | |
2340 | ||
2341 | /* | |
2342 | * helper function used to copy data from kernel in DUMMYNET_GET | |
2343 | */ | |
b0d623f7 A |
2344 | static |
2345 | char* dn_copy_set_32(struct dn_flow_set *set, char *bp) | |
9bccf70c A |
2346 | { |
2347 | int i, copied = 0 ; | |
b0d623f7 A |
2348 | struct dn_flow_queue *q; |
2349 | struct dn_flow_queue_32 *qp = (struct dn_flow_queue_32 *)bp; | |
2350 | ||
91447636 | 2351 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); |
b0d623f7 A |
2352 | |
2353 | for (i = 0 ; i <= set->rq_size ; i++) | |
2354 | for (q = set->rq[i] ; q ; q = q->next, qp++ ) { | |
2355 | if (q->hash_slot != i) | |
2356 | printf("dummynet: ++ at %d: wrong slot (have %d, " | |
2357 | "should be %d)\n", copied, q->hash_slot, i); | |
2358 | if (q->fs != set) | |
39236c6e A |
2359 | printf("dummynet: ++ at %d: wrong fs ptr " |
2360 | "(have 0x%llx, should be 0x%llx)\n", i, | |
2361 | (uint64_t)VM_KERNEL_ADDRPERM(q->fs), | |
2362 | (uint64_t)VM_KERNEL_ADDRPERM(set)); | |
b0d623f7 A |
2363 | copied++ ; |
2364 | cp_queue_to_32_user( q, qp ); | |
2365 | /* cleanup pointers */ | |
2366 | qp->next = (user32_addr_t)0 ; | |
2367 | qp->head = qp->tail = (user32_addr_t)0 ; | |
2368 | qp->fs = (user32_addr_t)0 ; | |
2369 | } | |
2370 | if (copied != set->rq_elements) | |
2371 | printf("dummynet: ++ wrong count, have %d should be %d\n", | |
2372 | copied, set->rq_elements); | |
2373 | return (char *)qp ; | |
2374 | } | |
91447636 | 2375 | |
b0d623f7 A |
2376 | static |
2377 | char* dn_copy_set_64(struct dn_flow_set *set, char *bp) | |
2378 | { | |
2379 | int i, copied = 0 ; | |
2380 | struct dn_flow_queue *q; | |
2381 | struct dn_flow_queue_64 *qp = (struct dn_flow_queue_64 *)bp; | |
2382 | ||
2383 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); | |
2384 | ||
9bccf70c | 2385 | for (i = 0 ; i <= set->rq_size ; i++) |
b0d623f7 A |
2386 | for (q = set->rq[i] ; q ; q = q->next, qp++ ) { |
2387 | if (q->hash_slot != i) | |
2388 | printf("dummynet: ++ at %d: wrong slot (have %d, " | |
2389 | "should be %d)\n", copied, q->hash_slot, i); | |
2390 | if (q->fs != set) | |
39236c6e A |
2391 | printf("dummynet: ++ at %d: wrong fs ptr " |
2392 | "(have 0x%llx, should be 0x%llx)\n", i, | |
2393 | (uint64_t)VM_KERNEL_ADDRPERM(q->fs), | |
2394 | (uint64_t)VM_KERNEL_ADDRPERM(set)); | |
b0d623f7 A |
2395 | copied++ ; |
2396 | //bcopy(q, qp, sizeof(*q)); | |
2397 | cp_queue_to_64_user( q, qp ); | |
2398 | /* cleanup pointers */ | |
2399 | qp->next = USER_ADDR_NULL ; | |
2400 | qp->head = qp->tail = USER_ADDR_NULL ; | |
2401 | qp->fs = USER_ADDR_NULL ; | |
2402 | } | |
9bccf70c | 2403 | if (copied != set->rq_elements) |
b0d623f7 A |
2404 | printf("dummynet: ++ wrong count, have %d should be %d\n", |
2405 | copied, set->rq_elements); | |
9bccf70c A |
2406 | return (char *)qp ; |
2407 | } | |
1c79356b | 2408 | |
91447636 | 2409 | static size_t |
b0d623f7 | 2410 | dn_calc_size(int is64user) |
1c79356b | 2411 | { |
9bccf70c A |
2412 | struct dn_flow_set *set ; |
2413 | struct dn_pipe *p ; | |
b0d623f7 A |
2414 | size_t size = 0 ; |
2415 | size_t pipesize; | |
2416 | size_t queuesize; | |
2417 | size_t setsize; | |
2418 | int i; | |
91447636 A |
2419 | |
2420 | lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED); | |
b0d623f7 A |
2421 | if ( is64user ){ |
2422 | pipesize = sizeof(struct dn_pipe_64); | |
2423 | queuesize = sizeof(struct dn_flow_queue_64); | |
2424 | setsize = sizeof(struct dn_flow_set_64); | |
2425 | } | |
2426 | else { | |
2427 | pipesize = sizeof(struct dn_pipe_32); | |
2428 | queuesize = sizeof( struct dn_flow_queue_32 ); | |
2429 | setsize = sizeof(struct dn_flow_set_32); | |
2430 | } | |
9bccf70c A |
2431 | /* |
2432 | * compute size of data structures: list of pipes and flow_sets. | |
2433 | */ | |
b0d623f7 A |
2434 | for (i = 0; i < HASHSIZE; i++) { |
2435 | SLIST_FOREACH(p, &pipehash[i], next) | |
2436 | size += sizeof(*p) + | |
2437 | p->fs.rq_elements * sizeof(struct dn_flow_queue); | |
2438 | SLIST_FOREACH(set, &flowsethash[i], next) | |
2439 | size += sizeof (*set) + | |
2440 | set->rq_elements * sizeof(struct dn_flow_queue); | |
2441 | } | |
2442 | return size; | |
91447636 A |
2443 | } |
2444 | ||
2445 | static int | |
2446 | dummynet_get(struct sockopt *sopt) | |
2447 | { | |
b0d623f7 | 2448 | char *buf, *bp=NULL; /* bp is the "copy-pointer" */ |
91447636 A |
2449 | size_t size ; |
2450 | struct dn_flow_set *set ; | |
2451 | struct dn_pipe *p ; | |
2452 | int error=0, i ; | |
b0d623f7 | 2453 | int is64user = 0; |
91447636 A |
2454 | |
2455 | /* XXX lock held too long */ | |
2456 | lck_mtx_lock(dn_mutex); | |
2457 | /* | |
2458 | * XXX: Ugly, but we need to allocate memory with M_WAITOK flag and we | |
2459 | * cannot use this flag while holding a mutex. | |
2460 | */ | |
b0d623f7 A |
2461 | if (proc_is64bit(sopt->sopt_p)) |
2462 | is64user = 1; | |
91447636 | 2463 | for (i = 0; i < 10; i++) { |
b0d623f7 | 2464 | size = dn_calc_size(is64user); |
91447636 A |
2465 | lck_mtx_unlock(dn_mutex); |
2466 | buf = _MALLOC(size, M_TEMP, M_WAITOK); | |
b0d623f7 A |
2467 | if (buf == NULL) |
2468 | return ENOBUFS; | |
91447636 | 2469 | lck_mtx_lock(dn_mutex); |
b0d623f7 | 2470 | if (size == dn_calc_size(is64user)) |
91447636 A |
2471 | break; |
2472 | FREE(buf, M_TEMP); | |
2473 | buf = NULL; | |
2474 | } | |
2475 | if (buf == NULL) { | |
2476 | lck_mtx_unlock(dn_mutex); | |
2477 | return ENOBUFS ; | |
9bccf70c | 2478 | } |
9bccf70c | 2479 | |
9bccf70c | 2480 | |
b0d623f7 A |
2481 | bp = buf; |
2482 | for (i = 0; i < HASHSIZE; i++) | |
2483 | SLIST_FOREACH(p, &pipehash[i], next) { | |
2484 | /* | |
2485 | * copy pipe descriptor into *bp, convert delay back to ms, | |
2486 | * then copy the flow_set descriptor(s) one at a time. | |
2487 | * After each flow_set, copy the queue descriptor it owns. | |
2488 | */ | |
2489 | if ( is64user ){ | |
2490 | bp = cp_pipe_to_64_user(p, (struct dn_pipe_64 *)bp); | |
2491 | } | |
2492 | else{ | |
2493 | bp = cp_pipe_to_32_user(p, (struct dn_pipe_32 *)bp); | |
2494 | } | |
9bccf70c | 2495 | } |
b0d623f7 A |
2496 | for (i = 0; i < HASHSIZE; i++) |
2497 | SLIST_FOREACH(set, &flowsethash[i], next) { | |
2498 | struct dn_flow_set_64 *fs_bp = (struct dn_flow_set_64 *)bp ; | |
2499 | cp_flow_set_to_64_user(set, fs_bp); | |
2500 | /* XXX same hack as above */ | |
2501 | fs_bp->next = CAST_DOWN(user64_addr_t, DN_IS_QUEUE); | |
2502 | fs_bp->pipe = USER_ADDR_NULL; | |
2503 | fs_bp->rq = USER_ADDR_NULL ; | |
2504 | bp += sizeof(struct dn_flow_set_64); | |
2505 | bp = dn_copy_set_64( set, bp ); | |
9bccf70c | 2506 | } |
91447636 A |
2507 | lck_mtx_unlock(dn_mutex); |
2508 | ||
9bccf70c A |
2509 | error = sooptcopyout(sopt, buf, size); |
2510 | FREE(buf, M_TEMP); | |
2511 | return error ; | |
1c79356b A |
2512 | } |
2513 | ||
9bccf70c A |
2514 | /* |
2515 | * Handler for the various dummynet socket options (get, flush, config, del) | |
2516 | */ | |
1c79356b | 2517 | static int |
9bccf70c | 2518 | ip_dn_ctl(struct sockopt *sopt) |
1c79356b | 2519 | { |
9bccf70c A |
2520 | int error = 0 ; |
2521 | struct dn_pipe *p, tmp_pipe; | |
2522 | ||
2523 | /* Disallow sets in really-really secure mode. */ | |
2524 | if (sopt->sopt_dir == SOPT_SET && securelevel >= 3) | |
2525 | return (EPERM); | |
2526 | ||
2527 | switch (sopt->sopt_name) { | |
2528 | default : | |
91447636 | 2529 | printf("dummynet: -- unknown option %d", sopt->sopt_name); |
9bccf70c A |
2530 | return EINVAL ; |
2531 | ||
2532 | case IP_DUMMYNET_GET : | |
2533 | error = dummynet_get(sopt); | |
2534 | break ; | |
2535 | ||
2536 | case IP_DUMMYNET_FLUSH : | |
2537 | dummynet_flush() ; | |
2538 | break ; | |
91447636 | 2539 | |
9bccf70c A |
2540 | case IP_DUMMYNET_CONFIGURE : |
2541 | p = &tmp_pipe ; | |
b0d623f7 A |
2542 | if (proc_is64bit(sopt->sopt_p)) |
2543 | error = cp_pipe_from_user_64( sopt, p ); | |
2544 | else | |
2545 | error = cp_pipe_from_user_32( sopt, p ); | |
2546 | ||
9bccf70c A |
2547 | if (error) |
2548 | break ; | |
2549 | error = config_pipe(p); | |
2550 | break ; | |
2551 | ||
2552 | case IP_DUMMYNET_DEL : /* remove a pipe or queue */ | |
2553 | p = &tmp_pipe ; | |
b0d623f7 A |
2554 | if (proc_is64bit(sopt->sopt_p)) |
2555 | error = cp_pipe_from_user_64( sopt, p ); | |
2556 | else | |
2557 | error = cp_pipe_from_user_32( sopt, p ); | |
9bccf70c A |
2558 | if (error) |
2559 | break ; | |
2560 | ||
2561 | error = delete_pipe(p); | |
2562 | break ; | |
2563 | } | |
2564 | return error ; | |
1c79356b A |
2565 | } |
2566 | ||
91447636 | 2567 | void |
9bccf70c | 2568 | ip_dn_init(void) |
1c79356b | 2569 | { |
91447636 A |
2570 | /* setup locks */ |
2571 | dn_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
2572 | dn_mutex_grp = lck_grp_alloc_init("dn", dn_mutex_grp_attr); | |
2573 | dn_mutex_attr = lck_attr_alloc_init(); | |
316670eb | 2574 | lck_mtx_init(dn_mutex, dn_mutex_grp, dn_mutex_attr); |
91447636 | 2575 | |
b0d623f7 | 2576 | ready_heap.size = ready_heap.elements = 0 ; |
316670eb A |
2577 | ready_heap.offset = 0 ; |
2578 | ||
2579 | wfq_ready_heap.size = wfq_ready_heap.elements = 0 ; | |
2580 | wfq_ready_heap.offset = 0 ; | |
9bccf70c | 2581 | |
316670eb A |
2582 | extract_heap.size = extract_heap.elements = 0 ; |
2583 | extract_heap.offset = 0 ; | |
2584 | ip_dn_ctl_ptr = ip_dn_ctl; | |
2585 | ip_dn_io_ptr = dummynet_io; | |
9bccf70c | 2586 | |
316670eb A |
2587 | bzero(&default_rule, sizeof default_rule); |
2588 | ||
2589 | default_rule.act_ofs = 0; | |
2590 | default_rule.rulenum = IPFW_DEFAULT_RULE; | |
2591 | default_rule.cmd_len = 1; | |
2592 | default_rule.set = RESVD_SET; | |
2593 | ||
2594 | default_rule.cmd[0].len = 1; | |
2595 | default_rule.cmd[0].opcode = | |
2596 | #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT | |
fe8ab488 | 2597 | (1) ? O_ACCEPT : |
316670eb A |
2598 | #endif |
2599 | O_DENY; | |
1c79356b | 2600 | } |