4 * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
51 static void explain(void)
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");
64 static unsigned int ilog2(unsigned int val
)
76 static int fq_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
77 struct nlmsghdr
*n
, const char *dev
)
80 unsigned int flow_plimit
;
82 unsigned int initial_quantum
;
83 unsigned int buckets
= 0;
85 unsigned int low_rate_threshold
;
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;
106 if (strcmp(*argv
, "limit") == 0) {
108 if (get_unsigned(&plimit
, *argv
, 0)) {
109 fprintf(stderr
, "Illegal \"limit\"\n");
113 } else if (strcmp(*argv
, "flow_limit") == 0) {
115 if (get_unsigned(&flow_plimit
, *argv
, 0)) {
116 fprintf(stderr
, "Illegal \"flow_limit\"\n");
119 set_flow_plimit
= true;
120 } else if (strcmp(*argv
, "buckets") == 0) {
122 if (get_unsigned(&buckets
, *argv
, 0)) {
123 fprintf(stderr
, "Illegal \"buckets\"\n");
126 } else if (strcmp(*argv
, "maxrate") == 0) {
128 if (strchr(*argv
, '%')) {
129 if (get_percent_rate(&maxrate
, *argv
, dev
)) {
130 fprintf(stderr
, "Illegal \"maxrate\"\n");
133 } else if (get_rate(&maxrate
, *argv
)) {
134 fprintf(stderr
, "Illegal \"maxrate\"\n");
138 } else if (strcmp(*argv
, "low_rate_threshold") == 0) {
140 if (get_rate(&low_rate_threshold
, *argv
)) {
141 fprintf(stderr
, "Illegal \"low_rate_threshold\"\n");
144 set_low_rate_threshold
= true;
145 } else if (strcmp(*argv
, "ce_threshold") == 0) {
147 if (get_time(&ce_threshold
, *argv
)) {
148 fprintf(stderr
, "Illegal \"ce_threshold\"\n");
151 set_ce_threshold
= true;
152 } else if (strcmp(*argv
, "timer_slack") == 0) {
156 if (get_time64(&t64
, *argv
)) {
157 fprintf(stderr
, "Illegal \"timer_slack\"\n");
161 if (timer_slack
!= t64
) {
162 fprintf(stderr
, "Illegal (out of range) \"timer_slack\"\n");
165 set_timer_slack
= true;
166 } else if (strcmp(*argv
, "defrate") == 0) {
168 if (strchr(*argv
, '%')) {
169 if (get_percent_rate(&defrate
, *argv
, dev
)) {
170 fprintf(stderr
, "Illegal \"defrate\"\n");
173 } else if (get_rate(&defrate
, *argv
)) {
174 fprintf(stderr
, "Illegal \"defrate\"\n");
178 } else if (strcmp(*argv
, "quantum") == 0) {
180 if (get_unsigned(&quantum
, *argv
, 0)) {
181 fprintf(stderr
, "Illegal \"quantum\"\n");
185 } else if (strcmp(*argv
, "initial_quantum") == 0) {
187 if (get_unsigned(&initial_quantum
, *argv
, 0)) {
188 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
191 set_initial_quantum
= true;
192 } else if (strcmp(*argv
, "orphan_mask") == 0) {
194 if (get_unsigned(&orphan_mask
, *argv
, 0)) {
195 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
198 set_orphan_mask
= true;
199 } else if (strcmp(*argv
, "refill_delay") == 0) {
201 if (get_time(&refill_delay
, *argv
)) {
202 fprintf(stderr
, "Illegal \"refill_delay\"\n");
205 set_refill_delay
= true;
206 } else if (strcmp(*argv
, "pacing") == 0) {
208 } else if (strcmp(*argv
, "nopacing") == 0) {
210 } else if (strcmp(*argv
, "help") == 0) {
214 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
221 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
223 unsigned int log
= ilog2(buckets
);
225 addattr_l(n
, 1024, TCA_FQ_BUCKETS_LOG
,
229 addattr_l(n
, 1024, TCA_FQ_PLIMIT
,
230 &plimit
, sizeof(plimit
));
232 addattr_l(n
, 1024, TCA_FQ_FLOW_PLIMIT
,
233 &flow_plimit
, sizeof(flow_plimit
));
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
));
240 addattr_l(n
, 1024, TCA_FQ_RATE_ENABLE
,
241 &pacing
, sizeof(pacing
));
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
));
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
));
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
));
261 addattr_l(n
, 1024, TCA_FQ_TIMER_SLACK
,
262 &timer_slack
, sizeof(timer_slack
));
263 addattr_nest_end(n
, tail
);
267 static int fq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
269 struct rtattr
*tb
[TCA_FQ_MAX
+ 1];
270 unsigned int plimit
, flow_plimit
;
271 unsigned int buckets_log
;
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
;
284 parse_rtattr_nested(tb
, TCA_FQ_MAX
, opt
);
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
);
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 ",
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 ",
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 ",
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
]);
313 print_bool(PRINT_ANY
, "pacing", "nopacing ", false);
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
));
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
));
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
]);
334 print_uint(PRINT_JSON
, "maxrate", NULL
, rate
);
335 print_string(PRINT_FP
, NULL
, "maxrate %s ",
336 sprint_rate(rate
, b1
));
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
]);
344 print_uint(PRINT_JSON
, "defrate", NULL
, rate
);
345 print_string(PRINT_FP
, NULL
, "defrate %s ",
346 sprint_rate(rate
, b1
));
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
]);
354 print_uint(PRINT_JSON
, "low_rate_threshold", NULL
,
356 print_string(PRINT_FP
, NULL
, "low_rate_threshold %s ",
357 sprint_rate(rate
, b1
));
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
));
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
,
374 print_string(PRINT_FP
, NULL
, "ce_threshold %s ",
375 sprint_time(ce_threshold
, b1
));
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
));
390 static int fq_print_xstats(struct qdisc_util
*qu
, FILE *f
,
391 struct rtattr
*xstats
)
393 struct tc_fq_qd_stats
*st
, _st
;
400 memset(&_st
, 0, sizeof(_st
));
401 memcpy(&_st
, RTA_DATA(xstats
), min(RTA_PAYLOAD(xstats
), sizeof(*st
)));
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
);
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
));
418 print_lluint(PRINT_ANY
, "gc", " gc %llu", st
->gc_flows
);
419 print_lluint(PRINT_ANY
, "highprio", " highprio %llu",
420 st
->highprio_packets
);
423 print_lluint(PRINT_ANY
, "retrans", " retrans %llu",
426 print_lluint(PRINT_ANY
, "throttled", " throttled %llu", st
->throttled
);
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
));
436 print_lluint(PRINT_ANY
, "ce_mark", " ce_mark %llu",
439 if (st
->flows_plimit
)
440 print_lluint(PRINT_ANY
, "flows_plimit", " flows_plimit %llu",
443 if (st
->pkts_too_long
|| st
->allocation_errors
) {
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
);
454 struct qdisc_util fq_qdisc_util
= {
456 .parse_qopt
= fq_parse_opt
,
457 .print_qopt
= fq_print_opt
,
458 .print_xstats
= fq_print_xstats
,