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