]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_fq.c
5.10.0
[mirror_iproute2.git] / tc / q_fq.c
1 /*
2 * Fair Queue
3 *
4 * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
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>
41 #include <fcntl.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <string.h>
46 #include <stdbool.h>
47
48 #include "utils.h"
49 #include "tc_util.h"
50
51 static void explain(void)
52 {
53 fprintf(stderr,
54 "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n"
55 " [ quantum BYTES ] [ initial_quantum BYTES ]\n"
56 " [ maxrate RATE ] [ buckets NUMBER ]\n"
57 " [ [no]pacing ] [ refill_delay TIME ]\n"
58 " [ low_rate_threshold RATE ]\n"
59 " [ orphan_mask MASK]\n"
60 " [ timer_slack TIME]\n"
61 " [ ce_threshold TIME ]\n");
62 }
63
64 static unsigned int ilog2(unsigned int val)
65 {
66 unsigned int res = 0;
67
68 val--;
69 while (val) {
70 res++;
71 val >>= 1;
72 }
73 return res;
74 }
75
76 static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv,
77 struct nlmsghdr *n, const char *dev)
78 {
79 unsigned int plimit;
80 unsigned int flow_plimit;
81 unsigned int quantum;
82 unsigned int initial_quantum;
83 unsigned int buckets = 0;
84 unsigned int maxrate;
85 unsigned int low_rate_threshold;
86 unsigned int defrate;
87 unsigned int refill_delay;
88 unsigned int orphan_mask;
89 unsigned int ce_threshold;
90 unsigned int timer_slack;
91 bool set_plimit = false;
92 bool set_flow_plimit = false;
93 bool set_quantum = false;
94 bool set_initial_quantum = false;
95 bool set_maxrate = false;
96 bool set_defrate = false;
97 bool set_refill_delay = false;
98 bool set_orphan_mask = false;
99 bool set_low_rate_threshold = false;
100 bool set_ce_threshold = false;
101 bool set_timer_slack = false;
102 int pacing = -1;
103 struct rtattr *tail;
104
105 while (argc > 0) {
106 if (strcmp(*argv, "limit") == 0) {
107 NEXT_ARG();
108 if (get_unsigned(&plimit, *argv, 0)) {
109 fprintf(stderr, "Illegal \"limit\"\n");
110 return -1;
111 }
112 set_plimit = true;
113 } else if (strcmp(*argv, "flow_limit") == 0) {
114 NEXT_ARG();
115 if (get_unsigned(&flow_plimit, *argv, 0)) {
116 fprintf(stderr, "Illegal \"flow_limit\"\n");
117 return -1;
118 }
119 set_flow_plimit = true;
120 } else if (strcmp(*argv, "buckets") == 0) {
121 NEXT_ARG();
122 if (get_unsigned(&buckets, *argv, 0)) {
123 fprintf(stderr, "Illegal \"buckets\"\n");
124 return -1;
125 }
126 } else if (strcmp(*argv, "maxrate") == 0) {
127 NEXT_ARG();
128 if (strchr(*argv, '%')) {
129 if (get_percent_rate(&maxrate, *argv, dev)) {
130 fprintf(stderr, "Illegal \"maxrate\"\n");
131 return -1;
132 }
133 } else if (get_rate(&maxrate, *argv)) {
134 fprintf(stderr, "Illegal \"maxrate\"\n");
135 return -1;
136 }
137 set_maxrate = true;
138 } else if (strcmp(*argv, "low_rate_threshold") == 0) {
139 NEXT_ARG();
140 if (get_rate(&low_rate_threshold, *argv)) {
141 fprintf(stderr, "Illegal \"low_rate_threshold\"\n");
142 return -1;
143 }
144 set_low_rate_threshold = true;
145 } else if (strcmp(*argv, "ce_threshold") == 0) {
146 NEXT_ARG();
147 if (get_time(&ce_threshold, *argv)) {
148 fprintf(stderr, "Illegal \"ce_threshold\"\n");
149 return -1;
150 }
151 set_ce_threshold = true;
152 } else if (strcmp(*argv, "timer_slack") == 0) {
153 __s64 t64;
154
155 NEXT_ARG();
156 if (get_time64(&t64, *argv)) {
157 fprintf(stderr, "Illegal \"timer_slack\"\n");
158 return -1;
159 }
160 timer_slack = t64;
161 if (timer_slack != t64) {
162 fprintf(stderr, "Illegal (out of range) \"timer_slack\"\n");
163 return -1;
164 }
165 set_timer_slack = true;
166 } else if (strcmp(*argv, "defrate") == 0) {
167 NEXT_ARG();
168 if (strchr(*argv, '%')) {
169 if (get_percent_rate(&defrate, *argv, dev)) {
170 fprintf(stderr, "Illegal \"defrate\"\n");
171 return -1;
172 }
173 } else if (get_rate(&defrate, *argv)) {
174 fprintf(stderr, "Illegal \"defrate\"\n");
175 return -1;
176 }
177 set_defrate = true;
178 } else if (strcmp(*argv, "quantum") == 0) {
179 NEXT_ARG();
180 if (get_unsigned(&quantum, *argv, 0)) {
181 fprintf(stderr, "Illegal \"quantum\"\n");
182 return -1;
183 }
184 set_quantum = true;
185 } else if (strcmp(*argv, "initial_quantum") == 0) {
186 NEXT_ARG();
187 if (get_unsigned(&initial_quantum, *argv, 0)) {
188 fprintf(stderr, "Illegal \"initial_quantum\"\n");
189 return -1;
190 }
191 set_initial_quantum = true;
192 } else if (strcmp(*argv, "orphan_mask") == 0) {
193 NEXT_ARG();
194 if (get_unsigned(&orphan_mask, *argv, 0)) {
195 fprintf(stderr, "Illegal \"initial_quantum\"\n");
196 return -1;
197 }
198 set_orphan_mask = true;
199 } else if (strcmp(*argv, "refill_delay") == 0) {
200 NEXT_ARG();
201 if (get_time(&refill_delay, *argv)) {
202 fprintf(stderr, "Illegal \"refill_delay\"\n");
203 return -1;
204 }
205 set_refill_delay = true;
206 } else if (strcmp(*argv, "pacing") == 0) {
207 pacing = 1;
208 } else if (strcmp(*argv, "nopacing") == 0) {
209 pacing = 0;
210 } else if (strcmp(*argv, "help") == 0) {
211 explain();
212 return -1;
213 } else {
214 fprintf(stderr, "What is \"%s\"?\n", *argv);
215 explain();
216 return -1;
217 }
218 argc--; argv++;
219 }
220
221 tail = addattr_nest(n, 1024, TCA_OPTIONS);
222 if (buckets) {
223 unsigned int log = ilog2(buckets);
224
225 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG,
226 &log, sizeof(log));
227 }
228 if (set_plimit)
229 addattr_l(n, 1024, TCA_FQ_PLIMIT,
230 &plimit, sizeof(plimit));
231 if (set_flow_plimit)
232 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT,
233 &flow_plimit, sizeof(flow_plimit));
234 if (set_quantum)
235 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum));
236 if (set_initial_quantum)
237 addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM,
238 &initial_quantum, sizeof(initial_quantum));
239 if (pacing != -1)
240 addattr_l(n, 1024, TCA_FQ_RATE_ENABLE,
241 &pacing, sizeof(pacing));
242 if (set_maxrate)
243 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE,
244 &maxrate, sizeof(maxrate));
245 if (set_low_rate_threshold)
246 addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD,
247 &low_rate_threshold, sizeof(low_rate_threshold));
248 if (set_defrate)
249 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE,
250 &defrate, sizeof(defrate));
251 if (set_refill_delay)
252 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY,
253 &refill_delay, sizeof(refill_delay));
254 if (set_orphan_mask)
255 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK,
256 &orphan_mask, sizeof(orphan_mask));
257 if (set_ce_threshold)
258 addattr_l(n, 1024, TCA_FQ_CE_THRESHOLD,
259 &ce_threshold, sizeof(ce_threshold));
260 if (set_timer_slack)
261 addattr_l(n, 1024, TCA_FQ_TIMER_SLACK,
262 &timer_slack, sizeof(timer_slack));
263 addattr_nest_end(n, tail);
264 return 0;
265 }
266
267 static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
268 {
269 struct rtattr *tb[TCA_FQ_MAX + 1];
270 unsigned int plimit, flow_plimit;
271 unsigned int buckets_log;
272 int pacing;
273 unsigned int rate, quantum;
274 unsigned int refill_delay;
275 unsigned int orphan_mask;
276 unsigned int ce_threshold;
277 unsigned int timer_slack;
278
279 SPRINT_BUF(b1);
280
281 if (opt == NULL)
282 return 0;
283
284 parse_rtattr_nested(tb, TCA_FQ_MAX, opt);
285
286 if (tb[TCA_FQ_PLIMIT] &&
287 RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) {
288 plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]);
289 print_uint(PRINT_ANY, "limit", "limit %up ", plimit);
290 }
291 if (tb[TCA_FQ_FLOW_PLIMIT] &&
292 RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) {
293 flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]);
294 print_uint(PRINT_ANY, "flow_limit", "flow_limit %up ",
295 flow_plimit);
296 }
297 if (tb[TCA_FQ_BUCKETS_LOG] &&
298 RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) {
299 buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]);
300 print_uint(PRINT_ANY, "buckets", "buckets %u ",
301 1U << buckets_log);
302 }
303 if (tb[TCA_FQ_ORPHAN_MASK] &&
304 RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) {
305 orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]);
306 print_uint(PRINT_ANY, "orphan_mask", "orphan_mask %u ",
307 orphan_mask);
308 }
309 if (tb[TCA_FQ_RATE_ENABLE] &&
310 RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) {
311 pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]);
312 if (pacing == 0)
313 print_bool(PRINT_ANY, "pacing", "nopacing ", false);
314 }
315 if (tb[TCA_FQ_QUANTUM] &&
316 RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) {
317 quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]);
318 print_uint(PRINT_JSON, "quantum", NULL, quantum);
319 print_string(PRINT_FP, NULL, "quantum %s ",
320 sprint_size(quantum, b1));
321 }
322 if (tb[TCA_FQ_INITIAL_QUANTUM] &&
323 RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) {
324 quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
325 print_uint(PRINT_JSON, "initial_quantum", NULL, quantum);
326 print_string(PRINT_FP, NULL, "initial_quantum %s ",
327 sprint_size(quantum, b1));
328 }
329 if (tb[TCA_FQ_FLOW_MAX_RATE] &&
330 RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) {
331 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
332
333 if (rate != ~0U) {
334 print_uint(PRINT_JSON, "maxrate", NULL, rate);
335 print_string(PRINT_FP, NULL, "maxrate %s ",
336 sprint_rate(rate, b1));
337 }
338 }
339 if (tb[TCA_FQ_FLOW_DEFAULT_RATE] &&
340 RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) {
341 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]);
342
343 if (rate != 0) {
344 print_uint(PRINT_JSON, "defrate", NULL, rate);
345 print_string(PRINT_FP, NULL, "defrate %s ",
346 sprint_rate(rate, b1));
347 }
348 }
349 if (tb[TCA_FQ_LOW_RATE_THRESHOLD] &&
350 RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) {
351 rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
352
353 if (rate != 0) {
354 print_uint(PRINT_JSON, "low_rate_threshold", NULL,
355 rate);
356 print_string(PRINT_FP, NULL, "low_rate_threshold %s ",
357 sprint_rate(rate, b1));
358 }
359 }
360 if (tb[TCA_FQ_FLOW_REFILL_DELAY] &&
361 RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) {
362 refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]);
363 print_uint(PRINT_JSON, "refill_delay", NULL, refill_delay);
364 print_string(PRINT_FP, NULL, "refill_delay %s ",
365 sprint_time(refill_delay, b1));
366 }
367
368 if (tb[TCA_FQ_CE_THRESHOLD] &&
369 RTA_PAYLOAD(tb[TCA_FQ_CE_THRESHOLD]) >= sizeof(__u32)) {
370 ce_threshold = rta_getattr_u32(tb[TCA_FQ_CE_THRESHOLD]);
371 if (ce_threshold != ~0U) {
372 print_uint(PRINT_JSON, "ce_threshold", NULL,
373 ce_threshold);
374 print_string(PRINT_FP, NULL, "ce_threshold %s ",
375 sprint_time(ce_threshold, b1));
376 }
377 }
378
379 if (tb[TCA_FQ_TIMER_SLACK] &&
380 RTA_PAYLOAD(tb[TCA_FQ_TIMER_SLACK]) >= sizeof(__u32)) {
381 timer_slack = rta_getattr_u32(tb[TCA_FQ_TIMER_SLACK]);
382 print_uint(PRINT_JSON, "timer_slack", NULL, timer_slack);
383 print_string(PRINT_FP, NULL, "timer_slack %s ",
384 sprint_time64(timer_slack, b1));
385 }
386
387 return 0;
388 }
389
390 static int fq_print_xstats(struct qdisc_util *qu, FILE *f,
391 struct rtattr *xstats)
392 {
393 struct tc_fq_qd_stats *st, _st;
394
395 SPRINT_BUF(b1);
396
397 if (xstats == NULL)
398 return 0;
399
400 memset(&_st, 0, sizeof(_st));
401 memcpy(&_st, RTA_DATA(xstats), min(RTA_PAYLOAD(xstats), sizeof(*st)));
402
403 st = &_st;
404
405 print_uint(PRINT_ANY, "flows", " flows %u", st->flows);
406 print_uint(PRINT_ANY, "inactive", " (inactive %u", st->inactive_flows);
407 print_uint(PRINT_ANY, "throttled", " throttled %u)",
408 st->throttled_flows);
409
410 if (st->time_next_delayed_flow > 0) {
411 print_lluint(PRINT_JSON, "next_packet_delay", NULL,
412 st->time_next_delayed_flow);
413 print_string(PRINT_FP, NULL, " next_packet_delay %s",
414 sprint_time64(st->time_next_delayed_flow, b1));
415 }
416
417 print_nl();
418 print_lluint(PRINT_ANY, "gc", " gc %llu", st->gc_flows);
419 print_lluint(PRINT_ANY, "highprio", " highprio %llu",
420 st->highprio_packets);
421
422 if (st->tcp_retrans)
423 print_lluint(PRINT_ANY, "retrans", " retrans %llu",
424 st->tcp_retrans);
425
426 print_lluint(PRINT_ANY, "throttled", " throttled %llu", st->throttled);
427
428 if (st->unthrottle_latency_ns) {
429 print_uint(PRINT_JSON, "latency", NULL,
430 st->unthrottle_latency_ns);
431 print_string(PRINT_FP, NULL, " latency %s",
432 sprint_time64(st->unthrottle_latency_ns, b1));
433 }
434
435 if (st->ce_mark)
436 print_lluint(PRINT_ANY, "ce_mark", " ce_mark %llu",
437 st->ce_mark);
438
439 if (st->flows_plimit)
440 print_lluint(PRINT_ANY, "flows_plimit", " flows_plimit %llu",
441 st->flows_plimit);
442
443 if (st->pkts_too_long || st->allocation_errors) {
444 print_nl();
445 print_lluint(PRINT_ANY, "pkts_too_long",
446 " pkts_too_long %llu", st->pkts_too_long);
447 print_lluint(PRINT_ANY, "alloc_errors", " alloc_errors %llu",
448 st->allocation_errors);
449 }
450
451 return 0;
452 }
453
454 struct qdisc_util fq_qdisc_util = {
455 .id = "fq",
456 .parse_qopt = fq_parse_opt,
457 .print_qopt = fq_print_opt,
458 .print_xstats = fq_print_xstats,
459 };