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