4 * Copyright (C) 2012,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
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
51 static void explain(void)
53 fprintf(stderr
, "Usage: ... fq_codel [ limit PACKETS ] [ flows NUMBER ]\n");
54 fprintf(stderr
, " [ target TIME ] [ interval TIME ]\n");
55 fprintf(stderr
, " [ quantum BYTES ] [ [no]ecn ]\n");
56 fprintf(stderr
, " [ ce_threshold TIME ]\n");
59 static int fq_codel_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
62 unsigned int limit
= 0;
63 unsigned int flows
= 0;
64 unsigned int target
= 0;
65 unsigned int interval
= 0;
66 unsigned int quantum
= 0;
67 unsigned int ce_threshold
= ~0U;
68 unsigned int memory
= ~0U;
73 if (strcmp(*argv
, "limit") == 0) {
75 if (get_unsigned(&limit
, *argv
, 0)) {
76 fprintf(stderr
, "Illegal \"limit\"\n");
79 } else if (strcmp(*argv
, "flows") == 0) {
81 if (get_unsigned(&flows
, *argv
, 0)) {
82 fprintf(stderr
, "Illegal \"flows\"\n");
85 } else if (strcmp(*argv
, "quantum") == 0) {
87 if (get_unsigned(&quantum
, *argv
, 0)) {
88 fprintf(stderr
, "Illegal \"quantum\"\n");
91 } else if (strcmp(*argv
, "target") == 0) {
93 if (get_time(&target
, *argv
)) {
94 fprintf(stderr
, "Illegal \"target\"\n");
97 } else if (strcmp(*argv
, "ce_threshold") == 0) {
99 if (get_time(&ce_threshold
, *argv
)) {
100 fprintf(stderr
, "Illegal \"ce_threshold\"\n");
103 } else if (strcmp(*argv
, "memory_limit") == 0) {
105 if (get_size(&memory
, *argv
)) {
106 fprintf(stderr
, "Illegal \"memory_limit\"\n");
109 } else if (strcmp(*argv
, "interval") == 0) {
111 if (get_time(&interval
, *argv
)) {
112 fprintf(stderr
, "Illegal \"interval\"\n");
115 } else if (strcmp(*argv
, "ecn") == 0) {
117 } else if (strcmp(*argv
, "noecn") == 0) {
119 } else if (strcmp(*argv
, "help") == 0) {
123 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
130 tail
= NLMSG_TAIL(n
);
131 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
133 addattr_l(n
, 1024, TCA_FQ_CODEL_LIMIT
, &limit
, sizeof(limit
));
135 addattr_l(n
, 1024, TCA_FQ_CODEL_FLOWS
, &flows
, sizeof(flows
));
137 addattr_l(n
, 1024, TCA_FQ_CODEL_QUANTUM
, &quantum
, sizeof(quantum
));
139 addattr_l(n
, 1024, TCA_FQ_CODEL_INTERVAL
, &interval
, sizeof(interval
));
141 addattr_l(n
, 1024, TCA_FQ_CODEL_TARGET
, &target
, sizeof(target
));
143 addattr_l(n
, 1024, TCA_FQ_CODEL_ECN
, &ecn
, sizeof(ecn
));
144 if (ce_threshold
!= ~0U)
145 addattr_l(n
, 1024, TCA_FQ_CODEL_CE_THRESHOLD
,
146 &ce_threshold
, sizeof(ce_threshold
));
148 addattr_l(n
, 1024, TCA_FQ_CODEL_MEMORY_LIMIT
,
149 &memory
, sizeof(memory
));
151 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
155 static int fq_codel_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
157 struct rtattr
*tb
[TCA_FQ_CODEL_MAX
+ 1];
160 unsigned int interval
;
163 unsigned int quantum
;
164 unsigned int ce_threshold
;
165 unsigned int memory_limit
;
172 parse_rtattr_nested(tb
, TCA_FQ_CODEL_MAX
, opt
);
174 if (tb
[TCA_FQ_CODEL_LIMIT
] &&
175 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_LIMIT
]) >= sizeof(__u32
)) {
176 limit
= rta_getattr_u32(tb
[TCA_FQ_CODEL_LIMIT
]);
177 fprintf(f
, "limit %up ", limit
);
179 if (tb
[TCA_FQ_CODEL_FLOWS
] &&
180 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_FLOWS
]) >= sizeof(__u32
)) {
181 flows
= rta_getattr_u32(tb
[TCA_FQ_CODEL_FLOWS
]);
182 fprintf(f
, "flows %u ", flows
);
184 if (tb
[TCA_FQ_CODEL_QUANTUM
] &&
185 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_QUANTUM
]) >= sizeof(__u32
)) {
186 quantum
= rta_getattr_u32(tb
[TCA_FQ_CODEL_QUANTUM
]);
187 fprintf(f
, "quantum %u ", quantum
);
189 if (tb
[TCA_FQ_CODEL_TARGET
] &&
190 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_TARGET
]) >= sizeof(__u32
)) {
191 target
= rta_getattr_u32(tb
[TCA_FQ_CODEL_TARGET
]);
192 fprintf(f
, "target %s ", sprint_time(target
, b1
));
194 if (tb
[TCA_FQ_CODEL_CE_THRESHOLD
] &&
195 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_CE_THRESHOLD
]) >= sizeof(__u32
)) {
196 ce_threshold
= rta_getattr_u32(tb
[TCA_FQ_CODEL_CE_THRESHOLD
]);
197 fprintf(f
, "ce_threshold %s ", sprint_time(ce_threshold
, b1
));
199 if (tb
[TCA_FQ_CODEL_INTERVAL
] &&
200 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_INTERVAL
]) >= sizeof(__u32
)) {
201 interval
= rta_getattr_u32(tb
[TCA_FQ_CODEL_INTERVAL
]);
202 fprintf(f
, "interval %s ", sprint_time(interval
, b1
));
204 if (tb
[TCA_FQ_CODEL_MEMORY_LIMIT
] &&
205 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_MEMORY_LIMIT
]) >= sizeof(__u32
)) {
206 memory_limit
= rta_getattr_u32(tb
[TCA_FQ_CODEL_MEMORY_LIMIT
]);
208 fprintf(f
, "memory_limit %s ", sprint_size(memory_limit
, b1
));
210 if (tb
[TCA_FQ_CODEL_ECN
] &&
211 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_ECN
]) >= sizeof(__u32
)) {
212 ecn
= rta_getattr_u32(tb
[TCA_FQ_CODEL_ECN
]);
220 static int fq_codel_print_xstats(struct qdisc_util
*qu
, FILE *f
,
221 struct rtattr
*xstats
)
223 struct tc_fq_codel_xstats _st
= {}, *st
;
230 st
= RTA_DATA(xstats
);
231 if (RTA_PAYLOAD(xstats
) < sizeof(*st
)) {
232 memcpy(&_st
, st
, RTA_PAYLOAD(xstats
));
235 if (st
->type
== TCA_FQ_CODEL_XSTATS_QDISC
) {
236 fprintf(f
, " maxpacket %u drop_overlimit %u new_flow_count %u ecn_mark %u",
237 st
->qdisc_stats
.maxpacket
,
238 st
->qdisc_stats
.drop_overlimit
,
239 st
->qdisc_stats
.new_flow_count
,
240 st
->qdisc_stats
.ecn_mark
);
241 if (st
->qdisc_stats
.ce_mark
)
242 fprintf(f
, " ce_mark %u", st
->qdisc_stats
.ce_mark
);
243 if (st
->qdisc_stats
.memory_usage
)
244 fprintf(f
, " memory_used %u", st
->qdisc_stats
.memory_usage
);
245 if (st
->qdisc_stats
.drop_overmemory
)
246 fprintf(f
, " drop_overmemory %u", st
->qdisc_stats
.drop_overmemory
);
247 fprintf(f
, "\n new_flows_len %u old_flows_len %u",
248 st
->qdisc_stats
.new_flows_len
,
249 st
->qdisc_stats
.old_flows_len
);
251 if (st
->type
== TCA_FQ_CODEL_XSTATS_CLASS
) {
252 fprintf(f
, " deficit %d count %u lastcount %u ldelay %s",
253 st
->class_stats
.deficit
,
254 st
->class_stats
.count
,
255 st
->class_stats
.lastcount
,
256 sprint_time(st
->class_stats
.ldelay
, b1
));
257 if (st
->class_stats
.dropping
) {
258 fprintf(f
, " dropping");
259 if (st
->class_stats
.drop_next
< 0)
260 fprintf(f
, " drop_next -%s",
261 sprint_time(-st
->class_stats
.drop_next
, b1
));
263 fprintf(f
, " drop_next %s",
264 sprint_time(st
->class_stats
.drop_next
, b1
));
271 struct qdisc_util fq_codel_qdisc_util
= {
273 .parse_qopt
= fq_codel_parse_opt
,
274 .print_qopt
= fq_codel_print_opt
,
275 .print_xstats
= fq_codel_print_xstats
,