]>
Commit | Line | Data |
---|---|---|
bc113e46 ED |
1 | /* |
2 | * Fair Queue | |
3 | * | |
8d5bd8c3 | 4 | * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com> |
bc113e46 ED |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions, and the following disclaimer, | |
11 | * without modification. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. The names of the authors may not be used to endorse or promote products | |
16 | * derived from this software without specific prior written permission. | |
17 | * | |
18 | * Alternatively, provided that this notice is retained in full, this | |
19 | * software may be distributed under the terms of the GNU General | |
20 | * Public License ("GPL") version 2, in which case the provisions of the | |
21 | * GPL apply INSTEAD OF those given above. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
34 | * DAMAGE. | |
35 | * | |
36 | */ | |
37 | ||
38 | #include <stdio.h> | |
39 | #include <stdlib.h> | |
40 | #include <unistd.h> | |
bc113e46 ED |
41 | #include <fcntl.h> |
42 | #include <sys/socket.h> | |
43 | #include <netinet/in.h> | |
44 | #include <arpa/inet.h> | |
45 | #include <string.h> | |
aeb199d5 | 46 | #include <stdbool.h> |
bc113e46 ED |
47 | |
48 | #include "utils.h" | |
49 | #include "tc_util.h" | |
50 | ||
51 | static void explain(void) | |
52 | { | |
53 | fprintf(stderr, "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n"); | |
54 | fprintf(stderr, " [ quantum BYTES ] [ initial_quantum BYTES ]\n"); | |
55 | fprintf(stderr, " [ maxrate RATE ] [ buckets NUMBER ]\n"); | |
565af7b8 | 56 | fprintf(stderr, " [ [no]pacing ] [ refill_delay TIME ]\n"); |
ff28b751 | 57 | fprintf(stderr, " [ low_rate_threshold RATE ]\n"); |
8d5bd8c3 | 58 | fprintf(stderr, " [ orphan_mask MASK]\n"); |
55e106c4 | 59 | fprintf(stderr, " [ ce_threshold TIME ]\n"); |
bc113e46 ED |
60 | } |
61 | ||
62 | static unsigned int ilog2(unsigned int val) | |
63 | { | |
64 | unsigned int res = 0; | |
65 | ||
66 | val--; | |
67 | while (val) { | |
68 | res++; | |
69 | val >>= 1; | |
70 | } | |
71 | return res; | |
72 | } | |
73 | ||
74 | static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv, | |
927e3cfb | 75 | struct nlmsghdr *n, const char *dev) |
bc113e46 | 76 | { |
aeb199d5 YY |
77 | unsigned int plimit; |
78 | unsigned int flow_plimit; | |
79 | unsigned int quantum; | |
80 | unsigned int initial_quantum; | |
bc113e46 | 81 | unsigned int buckets = 0; |
aeb199d5 | 82 | unsigned int maxrate; |
ff28b751 | 83 | unsigned int low_rate_threshold; |
aeb199d5 | 84 | unsigned int defrate; |
565af7b8 | 85 | unsigned int refill_delay; |
8d5bd8c3 | 86 | unsigned int orphan_mask; |
55e106c4 | 87 | unsigned int ce_threshold; |
aeb199d5 YY |
88 | bool set_plimit = false; |
89 | bool set_flow_plimit = false; | |
90 | bool set_quantum = false; | |
91 | bool set_initial_quantum = false; | |
92 | bool set_maxrate = false; | |
93 | bool set_defrate = false; | |
565af7b8 | 94 | bool set_refill_delay = false; |
8d5bd8c3 | 95 | bool set_orphan_mask = false; |
ff28b751 | 96 | bool set_low_rate_threshold = false; |
55e106c4 | 97 | bool set_ce_threshold = false; |
bc113e46 ED |
98 | int pacing = -1; |
99 | struct rtattr *tail; | |
100 | ||
101 | while (argc > 0) { | |
102 | if (strcmp(*argv, "limit") == 0) { | |
103 | NEXT_ARG(); | |
104 | if (get_unsigned(&plimit, *argv, 0)) { | |
105 | fprintf(stderr, "Illegal \"limit\"\n"); | |
106 | return -1; | |
107 | } | |
aeb199d5 | 108 | set_plimit = true; |
bc113e46 ED |
109 | } else if (strcmp(*argv, "flow_limit") == 0) { |
110 | NEXT_ARG(); | |
111 | if (get_unsigned(&flow_plimit, *argv, 0)) { | |
112 | fprintf(stderr, "Illegal \"flow_limit\"\n"); | |
113 | return -1; | |
114 | } | |
aeb199d5 | 115 | set_flow_plimit = true; |
bc113e46 ED |
116 | } else if (strcmp(*argv, "buckets") == 0) { |
117 | NEXT_ARG(); | |
118 | if (get_unsigned(&buckets, *argv, 0)) { | |
119 | fprintf(stderr, "Illegal \"buckets\"\n"); | |
120 | return -1; | |
121 | } | |
122 | } else if (strcmp(*argv, "maxrate") == 0) { | |
123 | NEXT_ARG(); | |
927e3cfb ND |
124 | if (strchr(*argv, '%')) { |
125 | if (get_percent_rate(&maxrate, *argv, dev)) { | |
126 | fprintf(stderr, "Illegal \"maxrate\"\n"); | |
127 | return -1; | |
128 | } | |
129 | } else if (get_rate(&maxrate, *argv)) { | |
bc113e46 ED |
130 | fprintf(stderr, "Illegal \"maxrate\"\n"); |
131 | return -1; | |
132 | } | |
aeb199d5 | 133 | set_maxrate = true; |
ff28b751 ED |
134 | } else if (strcmp(*argv, "low_rate_threshold") == 0) { |
135 | NEXT_ARG(); | |
136 | if (get_rate(&low_rate_threshold, *argv)) { | |
137 | fprintf(stderr, "Illegal \"low_rate_threshold\"\n"); | |
138 | return -1; | |
139 | } | |
140 | set_low_rate_threshold = true; | |
55e106c4 ED |
141 | } else if (strcmp(*argv, "ce_threshold") == 0) { |
142 | NEXT_ARG(); | |
143 | if (get_time(&ce_threshold, *argv)) { | |
144 | fprintf(stderr, "Illegal \"ce_threshold\"\n"); | |
145 | return -1; | |
146 | } | |
147 | set_ce_threshold = true; | |
bc113e46 ED |
148 | } else if (strcmp(*argv, "defrate") == 0) { |
149 | NEXT_ARG(); | |
927e3cfb ND |
150 | if (strchr(*argv, '%')) { |
151 | if (get_percent_rate(&defrate, *argv, dev)) { | |
152 | fprintf(stderr, "Illegal \"defrate\"\n"); | |
153 | return -1; | |
154 | } | |
155 | } else if (get_rate(&defrate, *argv)) { | |
bc113e46 ED |
156 | fprintf(stderr, "Illegal \"defrate\"\n"); |
157 | return -1; | |
158 | } | |
aeb199d5 | 159 | set_defrate = true; |
bc113e46 ED |
160 | } else if (strcmp(*argv, "quantum") == 0) { |
161 | NEXT_ARG(); | |
162 | if (get_unsigned(&quantum, *argv, 0)) { | |
163 | fprintf(stderr, "Illegal \"quantum\"\n"); | |
164 | return -1; | |
165 | } | |
aeb199d5 | 166 | set_quantum = true; |
bc113e46 ED |
167 | } else if (strcmp(*argv, "initial_quantum") == 0) { |
168 | NEXT_ARG(); | |
169 | if (get_unsigned(&initial_quantum, *argv, 0)) { | |
170 | fprintf(stderr, "Illegal \"initial_quantum\"\n"); | |
171 | return -1; | |
172 | } | |
aeb199d5 | 173 | set_initial_quantum = true; |
8d5bd8c3 ED |
174 | } else if (strcmp(*argv, "orphan_mask") == 0) { |
175 | NEXT_ARG(); | |
176 | if (get_unsigned(&orphan_mask, *argv, 0)) { | |
177 | fprintf(stderr, "Illegal \"initial_quantum\"\n"); | |
178 | return -1; | |
179 | } | |
180 | set_orphan_mask = true; | |
565af7b8 PS |
181 | } else if (strcmp(*argv, "refill_delay") == 0) { |
182 | NEXT_ARG(); | |
183 | if (get_time(&refill_delay, *argv)) { | |
184 | fprintf(stderr, "Illegal \"refill_delay\"\n"); | |
185 | return -1; | |
186 | } | |
187 | set_refill_delay = true; | |
bc113e46 ED |
188 | } else if (strcmp(*argv, "pacing") == 0) { |
189 | pacing = 1; | |
190 | } else if (strcmp(*argv, "nopacing") == 0) { | |
191 | pacing = 0; | |
192 | } else if (strcmp(*argv, "help") == 0) { | |
193 | explain(); | |
194 | return -1; | |
195 | } else { | |
196 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
197 | explain(); | |
198 | return -1; | |
199 | } | |
200 | argc--; argv++; | |
201 | } | |
202 | ||
c14f9d92 | 203 | tail = addattr_nest(n, 1024, TCA_OPTIONS); |
bc113e46 ED |
204 | if (buckets) { |
205 | unsigned int log = ilog2(buckets); | |
206 | ||
207 | addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG, | |
208 | &log, sizeof(log)); | |
209 | } | |
aeb199d5 | 210 | if (set_plimit) |
bc113e46 ED |
211 | addattr_l(n, 1024, TCA_FQ_PLIMIT, |
212 | &plimit, sizeof(plimit)); | |
aeb199d5 | 213 | if (set_flow_plimit) |
bc113e46 ED |
214 | addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT, |
215 | &flow_plimit, sizeof(flow_plimit)); | |
aeb199d5 | 216 | if (set_quantum) |
bc113e46 | 217 | addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum)); |
aeb199d5 | 218 | if (set_initial_quantum) |
bc113e46 ED |
219 | addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM, |
220 | &initial_quantum, sizeof(initial_quantum)); | |
221 | if (pacing != -1) | |
222 | addattr_l(n, 1024, TCA_FQ_RATE_ENABLE, | |
223 | &pacing, sizeof(pacing)); | |
aeb199d5 | 224 | if (set_maxrate) |
bc113e46 ED |
225 | addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE, |
226 | &maxrate, sizeof(maxrate)); | |
ff28b751 ED |
227 | if (set_low_rate_threshold) |
228 | addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD, | |
229 | &low_rate_threshold, sizeof(low_rate_threshold)); | |
aeb199d5 | 230 | if (set_defrate) |
bc113e46 ED |
231 | addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE, |
232 | &defrate, sizeof(defrate)); | |
565af7b8 PS |
233 | if (set_refill_delay) |
234 | addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY, | |
8fe98398 | 235 | &refill_delay, sizeof(refill_delay)); |
8d5bd8c3 ED |
236 | if (set_orphan_mask) |
237 | addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK, | |
8fe98398 | 238 | &orphan_mask, sizeof(refill_delay)); |
55e106c4 ED |
239 | if (set_ce_threshold) |
240 | addattr_l(n, 1024, TCA_FQ_CE_THRESHOLD, | |
241 | &ce_threshold, sizeof(ce_threshold)); | |
c14f9d92 | 242 | addattr_nest_end(n, tail); |
bc113e46 ED |
243 | return 0; |
244 | } | |
245 | ||
246 | static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
247 | { | |
248 | struct rtattr *tb[TCA_FQ_MAX + 1]; | |
249 | unsigned int plimit, flow_plimit; | |
250 | unsigned int buckets_log; | |
251 | int pacing; | |
252 | unsigned int rate, quantum; | |
565af7b8 | 253 | unsigned int refill_delay; |
8d5bd8c3 | 254 | unsigned int orphan_mask; |
55e106c4 | 255 | unsigned int ce_threshold; |
32a121cb | 256 | |
bc113e46 ED |
257 | SPRINT_BUF(b1); |
258 | ||
259 | if (opt == NULL) | |
260 | return 0; | |
261 | ||
262 | parse_rtattr_nested(tb, TCA_FQ_MAX, opt); | |
263 | ||
264 | if (tb[TCA_FQ_PLIMIT] && | |
265 | RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) { | |
266 | plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]); | |
267 | fprintf(f, "limit %up ", plimit); | |
268 | } | |
269 | if (tb[TCA_FQ_FLOW_PLIMIT] && | |
270 | RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) { | |
271 | flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]); | |
272 | fprintf(f, "flow_limit %up ", flow_plimit); | |
273 | } | |
274 | if (tb[TCA_FQ_BUCKETS_LOG] && | |
275 | RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) { | |
276 | buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]); | |
277 | fprintf(f, "buckets %u ", 1U << buckets_log); | |
278 | } | |
8d5bd8c3 ED |
279 | if (tb[TCA_FQ_ORPHAN_MASK] && |
280 | RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) { | |
281 | orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]); | |
282 | fprintf(f, "orphan_mask %u ", orphan_mask); | |
283 | } | |
bc113e46 ED |
284 | if (tb[TCA_FQ_RATE_ENABLE] && |
285 | RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) { | |
286 | pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]); | |
287 | if (pacing == 0) | |
288 | fprintf(f, "nopacing "); | |
289 | } | |
290 | if (tb[TCA_FQ_QUANTUM] && | |
291 | RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) { | |
292 | quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]); | |
293 | fprintf(f, "quantum %u ", quantum); | |
294 | } | |
295 | if (tb[TCA_FQ_INITIAL_QUANTUM] && | |
296 | RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) { | |
297 | quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]); | |
298 | fprintf(f, "initial_quantum %u ", quantum); | |
299 | } | |
300 | if (tb[TCA_FQ_FLOW_MAX_RATE] && | |
301 | RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) { | |
302 | rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]); | |
303 | ||
304 | if (rate != ~0U) | |
305 | fprintf(f, "maxrate %s ", sprint_rate(rate, b1)); | |
306 | } | |
307 | if (tb[TCA_FQ_FLOW_DEFAULT_RATE] && | |
308 | RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) { | |
309 | rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]); | |
310 | ||
311 | if (rate != 0) | |
312 | fprintf(f, "defrate %s ", sprint_rate(rate, b1)); | |
313 | } | |
ff28b751 ED |
314 | if (tb[TCA_FQ_LOW_RATE_THRESHOLD] && |
315 | RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) { | |
316 | rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]); | |
317 | ||
318 | if (rate != 0) | |
319 | fprintf(f, "low_rate_threshold %s ", sprint_rate(rate, b1)); | |
320 | } | |
565af7b8 PS |
321 | if (tb[TCA_FQ_FLOW_REFILL_DELAY] && |
322 | RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) { | |
323 | refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]); | |
324 | fprintf(f, "refill_delay %s ", sprint_time(refill_delay, b1)); | |
325 | } | |
bc113e46 | 326 | |
55e106c4 ED |
327 | if (tb[TCA_FQ_CE_THRESHOLD] && |
328 | RTA_PAYLOAD(tb[TCA_FQ_CE_THRESHOLD]) >= sizeof(__u32)) { | |
329 | ce_threshold = rta_getattr_u32(tb[TCA_FQ_CE_THRESHOLD]); | |
330 | if (ce_threshold != ~0U) | |
331 | fprintf(f, "ce_threshold %s ", sprint_time(ce_threshold, b1)); | |
332 | } | |
333 | ||
bc113e46 ED |
334 | return 0; |
335 | } | |
336 | ||
337 | static int fq_print_xstats(struct qdisc_util *qu, FILE *f, | |
338 | struct rtattr *xstats) | |
339 | { | |
55e106c4 | 340 | struct tc_fq_qd_stats *st, _st; |
bc113e46 ED |
341 | |
342 | if (xstats == NULL) | |
343 | return 0; | |
344 | ||
55e106c4 ED |
345 | memset(&_st, 0, sizeof(_st)); |
346 | memcpy(&_st, RTA_DATA(xstats), min(RTA_PAYLOAD(xstats), sizeof(*st))); | |
bc113e46 | 347 | |
55e106c4 | 348 | st = &_st; |
bc113e46 ED |
349 | |
350 | fprintf(f, " %u flows (%u inactive, %u throttled)", | |
351 | st->flows, st->inactive_flows, st->throttled_flows); | |
352 | ||
353 | if (st->time_next_delayed_flow > 0) | |
354 | fprintf(f, ", next packet delay %llu ns", st->time_next_delayed_flow); | |
355 | ||
356 | fprintf(f, "\n %llu gc, %llu highprio", | |
357 | st->gc_flows, st->highprio_packets); | |
358 | ||
359 | if (st->tcp_retrans) | |
360 | fprintf(f, ", %llu retrans", st->tcp_retrans); | |
361 | ||
362 | fprintf(f, ", %llu throttled", st->throttled); | |
363 | ||
39f8caeb ED |
364 | if (st->unthrottle_latency_ns) |
365 | fprintf(f, ", %u ns latency", st->unthrottle_latency_ns); | |
366 | ||
55e106c4 ED |
367 | if (st->ce_mark) |
368 | fprintf(f, ", %llu ce_mark", st->ce_mark); | |
369 | ||
bc113e46 ED |
370 | if (st->flows_plimit) |
371 | fprintf(f, ", %llu flows_plimit", st->flows_plimit); | |
372 | ||
373 | if (st->pkts_too_long || st->allocation_errors) | |
374 | fprintf(f, "\n %llu too long pkts, %llu alloc errors\n", | |
375 | st->pkts_too_long, st->allocation_errors); | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | struct qdisc_util fq_qdisc_util = { | |
381 | .id = "fq", | |
382 | .parse_qopt = fq_parse_opt, | |
383 | .print_qopt = fq_print_opt, | |
384 | .print_xstats = fq_print_xstats, | |
385 | }; |