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)
53 fprintf(stderr
, "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n");
54 fprintf(stderr
, " [ quantum BYTES ] [ initial_quantum BYTES ]\n");
55 fprintf(stderr
, " [ maxrate RATE ] [ buckets NUMBER ]\n");
56 fprintf(stderr
, " [ [no]pacing ] [ refill_delay TIME ]\n");
57 fprintf(stderr
, " [ low_rate_threshold RATE ]\n");
58 fprintf(stderr
, " [ orphan_mask MASK]\n");
61 static unsigned int ilog2(unsigned int val
)
73 static int fq_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
74 struct nlmsghdr
*n
, const char *dev
)
77 unsigned int flow_plimit
;
79 unsigned int initial_quantum
;
80 unsigned int buckets
= 0;
82 unsigned int low_rate_threshold
;
84 unsigned int refill_delay
;
85 unsigned int orphan_mask
;
86 bool set_plimit
= false;
87 bool set_flow_plimit
= false;
88 bool set_quantum
= false;
89 bool set_initial_quantum
= false;
90 bool set_maxrate
= false;
91 bool set_defrate
= false;
92 bool set_refill_delay
= false;
93 bool set_orphan_mask
= false;
94 bool set_low_rate_threshold
= false;
99 if (strcmp(*argv
, "limit") == 0) {
101 if (get_unsigned(&plimit
, *argv
, 0)) {
102 fprintf(stderr
, "Illegal \"limit\"\n");
106 } else if (strcmp(*argv
, "flow_limit") == 0) {
108 if (get_unsigned(&flow_plimit
, *argv
, 0)) {
109 fprintf(stderr
, "Illegal \"flow_limit\"\n");
112 set_flow_plimit
= true;
113 } else if (strcmp(*argv
, "buckets") == 0) {
115 if (get_unsigned(&buckets
, *argv
, 0)) {
116 fprintf(stderr
, "Illegal \"buckets\"\n");
119 } else if (strcmp(*argv
, "maxrate") == 0) {
121 if (strchr(*argv
, '%')) {
122 if (get_percent_rate(&maxrate
, *argv
, dev
)) {
123 fprintf(stderr
, "Illegal \"maxrate\"\n");
126 } else if (get_rate(&maxrate
, *argv
)) {
127 fprintf(stderr
, "Illegal \"maxrate\"\n");
131 } else if (strcmp(*argv
, "low_rate_threshold") == 0) {
133 if (get_rate(&low_rate_threshold
, *argv
)) {
134 fprintf(stderr
, "Illegal \"low_rate_threshold\"\n");
137 set_low_rate_threshold
= true;
138 } else if (strcmp(*argv
, "defrate") == 0) {
140 if (strchr(*argv
, '%')) {
141 if (get_percent_rate(&defrate
, *argv
, dev
)) {
142 fprintf(stderr
, "Illegal \"defrate\"\n");
145 } else if (get_rate(&defrate
, *argv
)) {
146 fprintf(stderr
, "Illegal \"defrate\"\n");
150 } else if (strcmp(*argv
, "quantum") == 0) {
152 if (get_unsigned(&quantum
, *argv
, 0)) {
153 fprintf(stderr
, "Illegal \"quantum\"\n");
157 } else if (strcmp(*argv
, "initial_quantum") == 0) {
159 if (get_unsigned(&initial_quantum
, *argv
, 0)) {
160 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
163 set_initial_quantum
= true;
164 } else if (strcmp(*argv
, "orphan_mask") == 0) {
166 if (get_unsigned(&orphan_mask
, *argv
, 0)) {
167 fprintf(stderr
, "Illegal \"initial_quantum\"\n");
170 set_orphan_mask
= true;
171 } else if (strcmp(*argv
, "refill_delay") == 0) {
173 if (get_time(&refill_delay
, *argv
)) {
174 fprintf(stderr
, "Illegal \"refill_delay\"\n");
177 set_refill_delay
= true;
178 } else if (strcmp(*argv
, "pacing") == 0) {
180 } else if (strcmp(*argv
, "nopacing") == 0) {
182 } else if (strcmp(*argv
, "help") == 0) {
186 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
193 tail
= NLMSG_TAIL(n
);
194 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
196 unsigned int log
= ilog2(buckets
);
198 addattr_l(n
, 1024, TCA_FQ_BUCKETS_LOG
,
202 addattr_l(n
, 1024, TCA_FQ_PLIMIT
,
203 &plimit
, sizeof(plimit
));
205 addattr_l(n
, 1024, TCA_FQ_FLOW_PLIMIT
,
206 &flow_plimit
, sizeof(flow_plimit
));
208 addattr_l(n
, 1024, TCA_FQ_QUANTUM
, &quantum
, sizeof(quantum
));
209 if (set_initial_quantum
)
210 addattr_l(n
, 1024, TCA_FQ_INITIAL_QUANTUM
,
211 &initial_quantum
, sizeof(initial_quantum
));
213 addattr_l(n
, 1024, TCA_FQ_RATE_ENABLE
,
214 &pacing
, sizeof(pacing
));
216 addattr_l(n
, 1024, TCA_FQ_FLOW_MAX_RATE
,
217 &maxrate
, sizeof(maxrate
));
218 if (set_low_rate_threshold
)
219 addattr_l(n
, 1024, TCA_FQ_LOW_RATE_THRESHOLD
,
220 &low_rate_threshold
, sizeof(low_rate_threshold
));
222 addattr_l(n
, 1024, TCA_FQ_FLOW_DEFAULT_RATE
,
223 &defrate
, sizeof(defrate
));
224 if (set_refill_delay
)
225 addattr_l(n
, 1024, TCA_FQ_FLOW_REFILL_DELAY
,
226 &refill_delay
, sizeof(refill_delay
));
228 addattr_l(n
, 1024, TCA_FQ_ORPHAN_MASK
,
229 &orphan_mask
, sizeof(refill_delay
));
230 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
234 static int fq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
236 struct rtattr
*tb
[TCA_FQ_MAX
+ 1];
237 unsigned int plimit
, flow_plimit
;
238 unsigned int buckets_log
;
240 unsigned int rate
, quantum
;
241 unsigned int refill_delay
;
242 unsigned int orphan_mask
;
249 parse_rtattr_nested(tb
, TCA_FQ_MAX
, opt
);
251 if (tb
[TCA_FQ_PLIMIT
] &&
252 RTA_PAYLOAD(tb
[TCA_FQ_PLIMIT
]) >= sizeof(__u32
)) {
253 plimit
= rta_getattr_u32(tb
[TCA_FQ_PLIMIT
]);
254 fprintf(f
, "limit %up ", plimit
);
256 if (tb
[TCA_FQ_FLOW_PLIMIT
] &&
257 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_PLIMIT
]) >= sizeof(__u32
)) {
258 flow_plimit
= rta_getattr_u32(tb
[TCA_FQ_FLOW_PLIMIT
]);
259 fprintf(f
, "flow_limit %up ", flow_plimit
);
261 if (tb
[TCA_FQ_BUCKETS_LOG
] &&
262 RTA_PAYLOAD(tb
[TCA_FQ_BUCKETS_LOG
]) >= sizeof(__u32
)) {
263 buckets_log
= rta_getattr_u32(tb
[TCA_FQ_BUCKETS_LOG
]);
264 fprintf(f
, "buckets %u ", 1U << buckets_log
);
266 if (tb
[TCA_FQ_ORPHAN_MASK
] &&
267 RTA_PAYLOAD(tb
[TCA_FQ_ORPHAN_MASK
]) >= sizeof(__u32
)) {
268 orphan_mask
= rta_getattr_u32(tb
[TCA_FQ_ORPHAN_MASK
]);
269 fprintf(f
, "orphan_mask %u ", orphan_mask
);
271 if (tb
[TCA_FQ_RATE_ENABLE
] &&
272 RTA_PAYLOAD(tb
[TCA_FQ_RATE_ENABLE
]) >= sizeof(int)) {
273 pacing
= rta_getattr_u32(tb
[TCA_FQ_RATE_ENABLE
]);
275 fprintf(f
, "nopacing ");
277 if (tb
[TCA_FQ_QUANTUM
] &&
278 RTA_PAYLOAD(tb
[TCA_FQ_QUANTUM
]) >= sizeof(__u32
)) {
279 quantum
= rta_getattr_u32(tb
[TCA_FQ_QUANTUM
]);
280 fprintf(f
, "quantum %u ", quantum
);
282 if (tb
[TCA_FQ_INITIAL_QUANTUM
] &&
283 RTA_PAYLOAD(tb
[TCA_FQ_INITIAL_QUANTUM
]) >= sizeof(__u32
)) {
284 quantum
= rta_getattr_u32(tb
[TCA_FQ_INITIAL_QUANTUM
]);
285 fprintf(f
, "initial_quantum %u ", quantum
);
287 if (tb
[TCA_FQ_FLOW_MAX_RATE
] &&
288 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_MAX_RATE
]) >= sizeof(__u32
)) {
289 rate
= rta_getattr_u32(tb
[TCA_FQ_FLOW_MAX_RATE
]);
292 fprintf(f
, "maxrate %s ", sprint_rate(rate
, b1
));
294 if (tb
[TCA_FQ_FLOW_DEFAULT_RATE
] &&
295 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_DEFAULT_RATE
]) >= sizeof(__u32
)) {
296 rate
= rta_getattr_u32(tb
[TCA_FQ_FLOW_DEFAULT_RATE
]);
299 fprintf(f
, "defrate %s ", sprint_rate(rate
, b1
));
301 if (tb
[TCA_FQ_LOW_RATE_THRESHOLD
] &&
302 RTA_PAYLOAD(tb
[TCA_FQ_LOW_RATE_THRESHOLD
]) >= sizeof(__u32
)) {
303 rate
= rta_getattr_u32(tb
[TCA_FQ_LOW_RATE_THRESHOLD
]);
306 fprintf(f
, "low_rate_threshold %s ", sprint_rate(rate
, b1
));
308 if (tb
[TCA_FQ_FLOW_REFILL_DELAY
] &&
309 RTA_PAYLOAD(tb
[TCA_FQ_FLOW_REFILL_DELAY
]) >= sizeof(__u32
)) {
310 refill_delay
= rta_getattr_u32(tb
[TCA_FQ_FLOW_REFILL_DELAY
]);
311 fprintf(f
, "refill_delay %s ", sprint_time(refill_delay
, b1
));
317 static int fq_print_xstats(struct qdisc_util
*qu
, FILE *f
,
318 struct rtattr
*xstats
)
320 struct tc_fq_qd_stats
*st
;
325 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
328 st
= RTA_DATA(xstats
);
330 fprintf(f
, " %u flows (%u inactive, %u throttled)",
331 st
->flows
, st
->inactive_flows
, st
->throttled_flows
);
333 if (st
->time_next_delayed_flow
> 0)
334 fprintf(f
, ", next packet delay %llu ns", st
->time_next_delayed_flow
);
336 fprintf(f
, "\n %llu gc, %llu highprio",
337 st
->gc_flows
, st
->highprio_packets
);
340 fprintf(f
, ", %llu retrans", st
->tcp_retrans
);
342 fprintf(f
, ", %llu throttled", st
->throttled
);
344 if (st
->unthrottle_latency_ns
)
345 fprintf(f
, ", %u ns latency", st
->unthrottle_latency_ns
);
347 if (st
->flows_plimit
)
348 fprintf(f
, ", %llu flows_plimit", st
->flows_plimit
);
350 if (st
->pkts_too_long
|| st
->allocation_errors
)
351 fprintf(f
, "\n %llu too long pkts, %llu alloc errors\n",
352 st
->pkts_too_long
, st
->allocation_errors
);
357 struct qdisc_util fq_qdisc_util
= {
359 .parse_qopt
= fq_parse_opt
,
360 .print_qopt
= fq_print_opt
,
361 .print_xstats
= fq_print_xstats
,