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 " [ ce_threshold TIME ]\n");
63 static unsigned int ilog2(unsigned int val
)
75 static int fq_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
76 struct nlmsghdr
*n
, const char *dev
)
79 unsigned int flow_plimit
;
81 unsigned int initial_quantum
;
82 unsigned int buckets
= 0;
84 unsigned int low_rate_threshold
;
86 unsigned int refill_delay
;
87 unsigned int orphan_mask
;
88 unsigned int ce_threshold
;
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;
95 bool set_refill_delay
= false;
96 bool set_orphan_mask
= false;
97 bool set_low_rate_threshold
= false;
98 bool set_ce_threshold
= false;
103 if (strcmp(*argv
, "limit") == 0) {
105 if (get_unsigned(&plimit
, *argv
, 0)) {
106 fprintf(stderr
, "Illegal \"limit\"\n");
110 } else if (strcmp(*argv
, "flow_limit") == 0) {
112 if (get_unsigned(&flow_plimit
, *argv
, 0)) {
113 fprintf(stderr
, "Illegal \"flow_limit\"\n");
116 set_flow_plimit
= true;
117 } else if (strcmp(*argv
, "buckets") == 0) {
119 if (get_unsigned(&buckets
, *argv
, 0)) {
120 fprintf(stderr
, "Illegal \"buckets\"\n");
123 } else if (strcmp(*argv
, "maxrate") == 0) {
125 if (strchr(*argv
, '%')) {
126 if (get_percent_rate(&maxrate
, *argv
, dev
)) {
127 fprintf(stderr
, "Illegal \"maxrate\"\n");
130 } else if (get_rate(&maxrate
, *argv
)) {
131 fprintf(stderr
, "Illegal \"maxrate\"\n");
135 } else if (strcmp(*argv
, "low_rate_threshold") == 0) {
137 if (get_rate(&low_rate_threshold
, *argv
)) {
138 fprintf(stderr
, "Illegal \"low_rate_threshold\"\n");
141 set_low_rate_threshold
= true;
142 } else if (strcmp(*argv
, "ce_threshold") == 0) {
144 if (get_time(&ce_threshold
, *argv
)) {
145 fprintf(stderr
, "Illegal \"ce_threshold\"\n");
148 set_ce_threshold
= true;
149 } else if (strcmp(*argv
, "defrate") == 0) {
151 if (strchr(*argv
, '%')) {
152 if (get_percent_rate(&defrate
, *argv
, dev
)) {
153 fprintf(stderr
, "Illegal \"defrate\"\n");
156 } else if (get_rate(&defrate
, *argv
)) {
157 fprintf(stderr
, "Illegal \"defrate\"\n");
161 } else if (strcmp(*argv
, "quantum") == 0) {
163 if (get_unsigned(&quantum
, *argv
, 0)) {
164 fprintf(stderr
, "Illegal \"quantum\"\n");
168 } else if (strcmp(*argv
, "initial_quantum") == 0) {
170 if (get_unsigned(&initial_quantum
, *argv
, 0)) {
171 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
174 set_initial_quantum
= true;
175 } else if (strcmp(*argv
, "orphan_mask") == 0) {
177 if (get_unsigned(&orphan_mask
, *argv
, 0)) {
178 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
181 set_orphan_mask
= true;
182 } else if (strcmp(*argv
, "refill_delay") == 0) {
184 if (get_time(&refill_delay
, *argv
)) {
185 fprintf(stderr
, "Illegal \"refill_delay\"\n");
188 set_refill_delay
= true;
189 } else if (strcmp(*argv
, "pacing") == 0) {
191 } else if (strcmp(*argv
, "nopacing") == 0) {
193 } else if (strcmp(*argv
, "help") == 0) {
197 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
204 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
206 unsigned int log
= ilog2(buckets
);
208 addattr_l(n
, 1024, TCA_FQ_BUCKETS_LOG
,
212 addattr_l(n
, 1024, TCA_FQ_PLIMIT
,
213 &plimit
, sizeof(plimit
));
215 addattr_l(n
, 1024, TCA_FQ_FLOW_PLIMIT
,
216 &flow_plimit
, sizeof(flow_plimit
));
218 addattr_l(n
, 1024, TCA_FQ_QUANTUM
, &quantum
, sizeof(quantum
));
219 if (set_initial_quantum
)
220 addattr_l(n
, 1024, TCA_FQ_INITIAL_QUANTUM
,
221 &initial_quantum
, sizeof(initial_quantum
));
223 addattr_l(n
, 1024, TCA_FQ_RATE_ENABLE
,
224 &pacing
, sizeof(pacing
));
226 addattr_l(n
, 1024, TCA_FQ_FLOW_MAX_RATE
,
227 &maxrate
, sizeof(maxrate
));
228 if (set_low_rate_threshold
)
229 addattr_l(n
, 1024, TCA_FQ_LOW_RATE_THRESHOLD
,
230 &low_rate_threshold
, sizeof(low_rate_threshold
));
232 addattr_l(n
, 1024, TCA_FQ_FLOW_DEFAULT_RATE
,
233 &defrate
, sizeof(defrate
));
234 if (set_refill_delay
)
235 addattr_l(n
, 1024, TCA_FQ_FLOW_REFILL_DELAY
,
236 &refill_delay
, sizeof(refill_delay
));
238 addattr_l(n
, 1024, TCA_FQ_ORPHAN_MASK
,
239 &orphan_mask
, sizeof(refill_delay
));
240 if (set_ce_threshold
)
241 addattr_l(n
, 1024, TCA_FQ_CE_THRESHOLD
,
242 &ce_threshold
, sizeof(ce_threshold
));
243 addattr_nest_end(n
, tail
);
247 static int fq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
249 struct rtattr
*tb
[TCA_FQ_MAX
+ 1];
250 unsigned int plimit
, flow_plimit
;
251 unsigned int buckets_log
;
253 unsigned int rate
, quantum
;
254 unsigned int refill_delay
;
255 unsigned int orphan_mask
;
256 unsigned int ce_threshold
;
263 parse_rtattr_nested(tb
, TCA_FQ_MAX
, opt
);
265 if (tb
[TCA_FQ_PLIMIT
] &&
266 RTA_PAYLOAD(tb
[TCA_FQ_PLIMIT
]) >= sizeof(__u32
)) {
267 plimit
= rta_getattr_u32(tb
[TCA_FQ_PLIMIT
]);
268 fprintf(f
, "limit %up ", plimit
);
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
]);
273 fprintf(f
, "flow_limit %up ", flow_plimit
);
275 if (tb
[TCA_FQ_BUCKETS_LOG
] &&
276 RTA_PAYLOAD(tb
[TCA_FQ_BUCKETS_LOG
]) >= sizeof(__u32
)) {
277 buckets_log
= rta_getattr_u32(tb
[TCA_FQ_BUCKETS_LOG
]);
278 fprintf(f
, "buckets %u ", 1U << buckets_log
);
280 if (tb
[TCA_FQ_ORPHAN_MASK
] &&
281 RTA_PAYLOAD(tb
[TCA_FQ_ORPHAN_MASK
]) >= sizeof(__u32
)) {
282 orphan_mask
= rta_getattr_u32(tb
[TCA_FQ_ORPHAN_MASK
]);
283 fprintf(f
, "orphan_mask %u ", orphan_mask
);
285 if (tb
[TCA_FQ_RATE_ENABLE
] &&
286 RTA_PAYLOAD(tb
[TCA_FQ_RATE_ENABLE
]) >= sizeof(int)) {
287 pacing
= rta_getattr_u32(tb
[TCA_FQ_RATE_ENABLE
]);
289 fprintf(f
, "nopacing ");
291 if (tb
[TCA_FQ_QUANTUM
] &&
292 RTA_PAYLOAD(tb
[TCA_FQ_QUANTUM
]) >= sizeof(__u32
)) {
293 quantum
= rta_getattr_u32(tb
[TCA_FQ_QUANTUM
]);
294 fprintf(f
, "quantum %u ", quantum
);
296 if (tb
[TCA_FQ_INITIAL_QUANTUM
] &&
297 RTA_PAYLOAD(tb
[TCA_FQ_INITIAL_QUANTUM
]) >= sizeof(__u32
)) {
298 quantum
= rta_getattr_u32(tb
[TCA_FQ_INITIAL_QUANTUM
]);
299 fprintf(f
, "initial_quantum %u ", quantum
);
301 if (tb
[TCA_FQ_FLOW_MAX_RATE
] &&
302 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_MAX_RATE
]) >= sizeof(__u32
)) {
303 rate
= rta_getattr_u32(tb
[TCA_FQ_FLOW_MAX_RATE
]);
306 fprintf(f
, "maxrate %s ", sprint_rate(rate
, b1
));
308 if (tb
[TCA_FQ_FLOW_DEFAULT_RATE
] &&
309 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_DEFAULT_RATE
]) >= sizeof(__u32
)) {
310 rate
= rta_getattr_u32(tb
[TCA_FQ_FLOW_DEFAULT_RATE
]);
313 fprintf(f
, "defrate %s ", sprint_rate(rate
, b1
));
315 if (tb
[TCA_FQ_LOW_RATE_THRESHOLD
] &&
316 RTA_PAYLOAD(tb
[TCA_FQ_LOW_RATE_THRESHOLD
]) >= sizeof(__u32
)) {
317 rate
= rta_getattr_u32(tb
[TCA_FQ_LOW_RATE_THRESHOLD
]);
320 fprintf(f
, "low_rate_threshold %s ", sprint_rate(rate
, b1
));
322 if (tb
[TCA_FQ_FLOW_REFILL_DELAY
] &&
323 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_REFILL_DELAY
]) >= sizeof(__u32
)) {
324 refill_delay
= rta_getattr_u32(tb
[TCA_FQ_FLOW_REFILL_DELAY
]);
325 fprintf(f
, "refill_delay %s ", sprint_time(refill_delay
, b1
));
328 if (tb
[TCA_FQ_CE_THRESHOLD
] &&
329 RTA_PAYLOAD(tb
[TCA_FQ_CE_THRESHOLD
]) >= sizeof(__u32
)) {
330 ce_threshold
= rta_getattr_u32(tb
[TCA_FQ_CE_THRESHOLD
]);
331 if (ce_threshold
!= ~0U)
332 fprintf(f
, "ce_threshold %s ", sprint_time(ce_threshold
, b1
));
338 static int fq_print_xstats(struct qdisc_util
*qu
, FILE *f
,
339 struct rtattr
*xstats
)
341 struct tc_fq_qd_stats
*st
, _st
;
346 memset(&_st
, 0, sizeof(_st
));
347 memcpy(&_st
, RTA_DATA(xstats
), min(RTA_PAYLOAD(xstats
), sizeof(*st
)));
351 fprintf(f
, " %u flows (%u inactive, %u throttled)",
352 st
->flows
, st
->inactive_flows
, st
->throttled_flows
);
354 if (st
->time_next_delayed_flow
> 0)
355 fprintf(f
, ", next packet delay %llu ns", st
->time_next_delayed_flow
);
357 fprintf(f
, "\n %llu gc, %llu highprio",
358 st
->gc_flows
, st
->highprio_packets
);
361 fprintf(f
, ", %llu retrans", st
->tcp_retrans
);
363 fprintf(f
, ", %llu throttled", st
->throttled
);
365 if (st
->unthrottle_latency_ns
)
366 fprintf(f
, ", %u ns latency", st
->unthrottle_latency_ns
);
369 fprintf(f
, ", %llu ce_mark", st
->ce_mark
);
371 if (st
->flows_plimit
)
372 fprintf(f
, ", %llu flows_plimit", st
->flows_plimit
);
374 if (st
->pkts_too_long
|| st
->allocation_errors
)
375 fprintf(f
, "\n %llu too long pkts, %llu alloc errors\n",
376 st
->pkts_too_long
, st
->allocation_errors
);
381 struct qdisc_util fq_qdisc_util
= {
383 .parse_qopt
= fq_parse_opt
,
384 .print_qopt
= fq_print_opt
,
385 .print_xstats
= fq_print_xstats
,