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