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
,
65 unsigned interval
= 0;
67 unsigned ce_threshold
= ~0U;
72 if (strcmp(*argv
, "limit") == 0) {
74 if (get_unsigned(&limit
, *argv
, 0)) {
75 fprintf(stderr
, "Illegal \"limit\"\n");
78 } else if (strcmp(*argv
, "flows") == 0) {
80 if (get_unsigned(&flows
, *argv
, 0)) {
81 fprintf(stderr
, "Illegal \"flows\"\n");
84 } else if (strcmp(*argv
, "quantum") == 0) {
86 if (get_unsigned(&quantum
, *argv
, 0)) {
87 fprintf(stderr
, "Illegal \"quantum\"\n");
90 } else if (strcmp(*argv
, "target") == 0) {
92 if (get_time(&target
, *argv
)) {
93 fprintf(stderr
, "Illegal \"target\"\n");
96 } else if (strcmp(*argv
, "ce_threshold") == 0) {
98 if (get_time(&ce_threshold
, *argv
)) {
99 fprintf(stderr
, "Illegal \"ce_threshold\"\n");
102 } else if (strcmp(*argv
, "interval") == 0) {
104 if (get_time(&interval
, *argv
)) {
105 fprintf(stderr
, "Illegal \"interval\"\n");
108 } else if (strcmp(*argv
, "ecn") == 0) {
110 } else if (strcmp(*argv
, "noecn") == 0) {
112 } else if (strcmp(*argv
, "help") == 0) {
116 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
123 tail
= NLMSG_TAIL(n
);
124 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
126 addattr_l(n
, 1024, TCA_FQ_CODEL_LIMIT
, &limit
, sizeof(limit
));
128 addattr_l(n
, 1024, TCA_FQ_CODEL_FLOWS
, &flows
, sizeof(flows
));
130 addattr_l(n
, 1024, TCA_FQ_CODEL_QUANTUM
, &quantum
, sizeof(quantum
));
132 addattr_l(n
, 1024, TCA_FQ_CODEL_INTERVAL
, &interval
, sizeof(interval
));
134 addattr_l(n
, 1024, TCA_FQ_CODEL_TARGET
, &target
, sizeof(target
));
136 addattr_l(n
, 1024, TCA_FQ_CODEL_ECN
, &ecn
, sizeof(ecn
));
137 if (ce_threshold
!= ~0U)
138 addattr_l(n
, 1024, TCA_FQ_CODEL_CE_THRESHOLD
,
139 &ce_threshold
, sizeof(ce_threshold
));
140 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
144 static int fq_codel_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
146 struct rtattr
*tb
[TCA_FQ_CODEL_MAX
+ 1];
153 unsigned ce_threshold
;
159 parse_rtattr_nested(tb
, TCA_FQ_CODEL_MAX
, opt
);
161 if (tb
[TCA_FQ_CODEL_LIMIT
] &&
162 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_LIMIT
]) >= sizeof(__u32
)) {
163 limit
= rta_getattr_u32(tb
[TCA_FQ_CODEL_LIMIT
]);
164 fprintf(f
, "limit %up ", limit
);
166 if (tb
[TCA_FQ_CODEL_FLOWS
] &&
167 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_FLOWS
]) >= sizeof(__u32
)) {
168 flows
= rta_getattr_u32(tb
[TCA_FQ_CODEL_FLOWS
]);
169 fprintf(f
, "flows %u ", flows
);
171 if (tb
[TCA_FQ_CODEL_QUANTUM
] &&
172 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_QUANTUM
]) >= sizeof(__u32
)) {
173 quantum
= rta_getattr_u32(tb
[TCA_FQ_CODEL_QUANTUM
]);
174 fprintf(f
, "quantum %u ", quantum
);
176 if (tb
[TCA_FQ_CODEL_TARGET
] &&
177 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_TARGET
]) >= sizeof(__u32
)) {
178 target
= rta_getattr_u32(tb
[TCA_FQ_CODEL_TARGET
]);
179 fprintf(f
, "target %s ", sprint_time(target
, b1
));
181 if (tb
[TCA_FQ_CODEL_CE_THRESHOLD
] &&
182 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_CE_THRESHOLD
]) >= sizeof(__u32
)) {
183 ce_threshold
= rta_getattr_u32(tb
[TCA_FQ_CODEL_CE_THRESHOLD
]);
184 fprintf(f
, "ce_threshold %s ", sprint_time(ce_threshold
, b1
));
186 if (tb
[TCA_FQ_CODEL_INTERVAL
] &&
187 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_INTERVAL
]) >= sizeof(__u32
)) {
188 interval
= rta_getattr_u32(tb
[TCA_FQ_CODEL_INTERVAL
]);
189 fprintf(f
, "interval %s ", sprint_time(interval
, b1
));
191 if (tb
[TCA_FQ_CODEL_ECN
] &&
192 RTA_PAYLOAD(tb
[TCA_FQ_CODEL_ECN
]) >= sizeof(__u32
)) {
193 ecn
= rta_getattr_u32(tb
[TCA_FQ_CODEL_ECN
]);
201 static int fq_codel_print_xstats(struct qdisc_util
*qu
, FILE *f
,
202 struct rtattr
*xstats
)
204 struct tc_fq_codel_xstats _st
, *st
;
210 st
= RTA_DATA(xstats
);
211 if (RTA_PAYLOAD(xstats
) < sizeof(*st
)) {
212 memset(&_st
, 0, sizeof(_st
));
213 memcpy(&_st
, st
, RTA_PAYLOAD(xstats
));
216 if (st
->type
== TCA_FQ_CODEL_XSTATS_QDISC
) {
217 fprintf(f
, " maxpacket %u drop_overlimit %u new_flow_count %u ecn_mark %u",
218 st
->qdisc_stats
.maxpacket
,
219 st
->qdisc_stats
.drop_overlimit
,
220 st
->qdisc_stats
.new_flow_count
,
221 st
->qdisc_stats
.ecn_mark
);
222 if (st
->qdisc_stats
.ce_mark
)
223 fprintf(f
, " ce_mark %u", st
->qdisc_stats
.ce_mark
);
224 fprintf(f
, "\n new_flows_len %u old_flows_len %u",
225 st
->qdisc_stats
.new_flows_len
,
226 st
->qdisc_stats
.old_flows_len
);
228 if (st
->type
== TCA_FQ_CODEL_XSTATS_CLASS
) {
229 fprintf(f
, " deficit %d count %u lastcount %u ldelay %s",
230 st
->class_stats
.deficit
,
231 st
->class_stats
.count
,
232 st
->class_stats
.lastcount
,
233 sprint_time(st
->class_stats
.ldelay
, b1
));
234 if (st
->class_stats
.dropping
) {
235 fprintf(f
, " dropping");
236 if (st
->class_stats
.drop_next
< 0)
237 fprintf(f
, " drop_next -%s",
238 sprint_time(-st
->class_stats
.drop_next
, b1
));
240 fprintf(f
, " drop_next %s",
241 sprint_time(st
->class_stats
.drop_next
, b1
));
248 struct qdisc_util fq_codel_qdisc_util
= {
250 .parse_qopt
= fq_codel_parse_opt
,
251 .print_qopt
= fq_codel_print_opt
,
252 .print_xstats
= fq_codel_print_xstats
,