]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_fq.c
Merge branch 'gcc-10' into main
[mirror_iproute2.git] / tc / q_fq.c
CommitLineData
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
51static void explain(void)
52{
8589eb4e
MC
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"
be9ca9d5 60 " [ timer_slack TIME]\n"
8589eb4e 61 " [ ce_threshold TIME ]\n");
bc113e46
ED
62}
63
64static 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
76static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 77 struct nlmsghdr *n, const char *dev)
bc113e46 78{
aeb199d5
YY
79 unsigned int plimit;
80 unsigned int flow_plimit;
81 unsigned int quantum;
82 unsigned int initial_quantum;
bc113e46 83 unsigned int buckets = 0;
aeb199d5 84 unsigned int maxrate;
ff28b751 85 unsigned int low_rate_threshold;
aeb199d5 86 unsigned int defrate;
565af7b8 87 unsigned int refill_delay;
8d5bd8c3 88 unsigned int orphan_mask;
55e106c4 89 unsigned int ce_threshold;
be9ca9d5 90 unsigned int timer_slack;
aeb199d5
YY
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;
565af7b8 97 bool set_refill_delay = false;
8d5bd8c3 98 bool set_orphan_mask = false;
ff28b751 99 bool set_low_rate_threshold = false;
55e106c4 100 bool set_ce_threshold = false;
be9ca9d5 101 bool set_timer_slack = false;
bc113e46
ED
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 }
aeb199d5 112 set_plimit = true;
bc113e46
ED
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 }
aeb199d5 119 set_flow_plimit = true;
bc113e46
ED
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();
927e3cfb
ND
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)) {
bc113e46
ED
134 fprintf(stderr, "Illegal \"maxrate\"\n");
135 return -1;
136 }
aeb199d5 137 set_maxrate = true;
ff28b751
ED
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;
55e106c4
ED
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;
be9ca9d5
ED
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;
bc113e46
ED
166 } else if (strcmp(*argv, "defrate") == 0) {
167 NEXT_ARG();
927e3cfb
ND
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)) {
bc113e46
ED
174 fprintf(stderr, "Illegal \"defrate\"\n");
175 return -1;
176 }
aeb199d5 177 set_defrate = true;
bc113e46
ED
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 }
aeb199d5 184 set_quantum = true;
bc113e46
ED
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 }
aeb199d5 191 set_initial_quantum = true;
8d5bd8c3
ED
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;
565af7b8
PS
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;
bc113e46
ED
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
c14f9d92 221 tail = addattr_nest(n, 1024, TCA_OPTIONS);
bc113e46
ED
222 if (buckets) {
223 unsigned int log = ilog2(buckets);
224
225 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG,
226 &log, sizeof(log));
227 }
aeb199d5 228 if (set_plimit)
bc113e46
ED
229 addattr_l(n, 1024, TCA_FQ_PLIMIT,
230 &plimit, sizeof(plimit));
aeb199d5 231 if (set_flow_plimit)
bc113e46
ED
232 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT,
233 &flow_plimit, sizeof(flow_plimit));
aeb199d5 234 if (set_quantum)
bc113e46 235 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum));
aeb199d5 236 if (set_initial_quantum)
bc113e46
ED
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));
aeb199d5 242 if (set_maxrate)
bc113e46
ED
243 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE,
244 &maxrate, sizeof(maxrate));
ff28b751
ED
245 if (set_low_rate_threshold)
246 addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD,
247 &low_rate_threshold, sizeof(low_rate_threshold));
aeb199d5 248 if (set_defrate)
bc113e46
ED
249 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE,
250 &defrate, sizeof(defrate));
565af7b8
PS
251 if (set_refill_delay)
252 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY,
8fe98398 253 &refill_delay, sizeof(refill_delay));
8d5bd8c3
ED
254 if (set_orphan_mask)
255 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK,
78ace1c2 256 &orphan_mask, sizeof(orphan_mask));
55e106c4
ED
257 if (set_ce_threshold)
258 addattr_l(n, 1024, TCA_FQ_CE_THRESHOLD,
259 &ce_threshold, sizeof(ce_threshold));
be9ca9d5
ED
260 if (set_timer_slack)
261 addattr_l(n, 1024, TCA_FQ_TIMER_SLACK,
262 &timer_slack, sizeof(timer_slack));
c14f9d92 263 addattr_nest_end(n, tail);
bc113e46
ED
264 return 0;
265}
266
267static 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;
565af7b8 274 unsigned int refill_delay;
8d5bd8c3 275 unsigned int orphan_mask;
55e106c4 276 unsigned int ce_threshold;
be9ca9d5 277 unsigned int timer_slack;
32a121cb 278
bc113e46
ED
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]);
d15e2bfc 289 print_uint(PRINT_ANY, "limit", "limit %up ", plimit);
bc113e46
ED
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]);
d15e2bfc
LM
294 print_uint(PRINT_ANY, "flow_limit", "flow_limit %up ",
295 flow_plimit);
bc113e46
ED
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]);
d15e2bfc
LM
300 print_uint(PRINT_ANY, "buckets", "buckets %u ",
301 1U << buckets_log);
bc113e46 302 }
8d5bd8c3
ED
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]);
d15e2bfc
LM
306 print_uint(PRINT_ANY, "orphan_mask", "orphan_mask %u ",
307 orphan_mask);
8d5bd8c3 308 }
bc113e46
ED
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)
d15e2bfc 313 print_bool(PRINT_ANY, "pacing", "nopacing ", false);
bc113e46
ED
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]);
d15e2bfc
LM
318 print_uint(PRINT_JSON, "quantum", NULL, quantum);
319 print_string(PRINT_FP, NULL, "quantum %s ",
320 sprint_size(quantum, b1));
bc113e46
ED
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]);
d15e2bfc
LM
325 print_uint(PRINT_JSON, "initial_quantum", NULL, quantum);
326 print_string(PRINT_FP, NULL, "initial_quantum %s ",
327 sprint_size(quantum, b1));
bc113e46
ED
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
d15e2bfc
LM
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 }
bc113e46
ED
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
d15e2bfc
LM
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 }
bc113e46 348 }
ff28b751
ED
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
d15e2bfc
LM
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 }
ff28b751 359 }
565af7b8
PS
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]);
d15e2bfc
LM
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));
565af7b8 366 }
bc113e46 367
55e106c4
ED
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]);
d15e2bfc
LM
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 }
55e106c4
ED
377 }
378
be9ca9d5
ED
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]);
0ecb90b3
ED
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));
be9ca9d5
ED
385 }
386
bc113e46
ED
387 return 0;
388}
389
390static int fq_print_xstats(struct qdisc_util *qu, FILE *f,
391 struct rtattr *xstats)
392{
55e106c4 393 struct tc_fq_qd_stats *st, _st;
bc113e46 394
d15e2bfc
LM
395 SPRINT_BUF(b1);
396
bc113e46
ED
397 if (xstats == NULL)
398 return 0;
399
55e106c4
ED
400 memset(&_st, 0, sizeof(_st));
401 memcpy(&_st, RTA_DATA(xstats), min(RTA_PAYLOAD(xstats), sizeof(*st)));
bc113e46 402
55e106c4 403 st = &_st;
bc113e46 404
d15e2bfc
LM
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);
bc113e46 409
d15e2bfc
LM
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 }
bc113e46 416
d15e2bfc
LM
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);
bc113e46
ED
421
422 if (st->tcp_retrans)
d15e2bfc
LM
423 print_lluint(PRINT_ANY, "retrans", " retrans %llu",
424 st->tcp_retrans);
bc113e46 425
d15e2bfc 426 print_lluint(PRINT_ANY, "throttled", " throttled %llu", st->throttled);
bc113e46 427
d15e2bfc
LM
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 }
39f8caeb 434
55e106c4 435 if (st->ce_mark)
d15e2bfc
LM
436 print_lluint(PRINT_ANY, "ce_mark", " ce_mark %llu",
437 st->ce_mark);
55e106c4 438
bc113e46 439 if (st->flows_plimit)
d15e2bfc
LM
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);
0ecb90b3 447 print_lluint(PRINT_ANY, "alloc_errors", " alloc_errors %llu",
d15e2bfc
LM
448 st->allocation_errors);
449 }
bc113e46
ED
450
451 return 0;
452}
453
454struct 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};