1 /*
2 * Fair Queue Codel
3 *
4 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
17 *
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.
22 *
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
34 * DAMAGE.
35 *
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <syslog.h>
42 #include <fcntl.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <string.h>
47
48 #include "utils.h"
49 #include "tc_util.h"
50
explain(void)51 static void explain(void)
52 {
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");
57 }
58
fq_codel_parse_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)59 static int fq_codel_parse_opt(struct qdisc_util *qu, int argc, char **argv,
60 struct nlmsghdr *n)
61 {
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;
69 int ecn = -1;
70 struct rtattr *tail;
71
72 while (argc > 0) {
73 if (strcmp(*argv, "limit") == 0) {
74 NEXT_ARG();
75 if (get_unsigned(&limit, *argv, 0)) {
76 fprintf(stderr, "Illegal \"limit\"\n");
77 return -1;
78 }
79 } else if (strcmp(*argv, "flows") == 0) {
80 NEXT_ARG();
81 if (get_unsigned(&flows, *argv, 0)) {
82 fprintf(stderr, "Illegal \"flows\"\n");
83 return -1;
84 }
85 } else if (strcmp(*argv, "quantum") == 0) {
86 NEXT_ARG();
87 if (get_unsigned(&quantum, *argv, 0)) {
88 fprintf(stderr, "Illegal \"quantum\"\n");
89 return -1;
90 }
91 } else if (strcmp(*argv, "target") == 0) {
92 NEXT_ARG();
93 if (get_time(&target, *argv)) {
94 fprintf(stderr, "Illegal \"target\"\n");
95 return -1;
96 }
97 } else if (strcmp(*argv, "ce_threshold") == 0) {
98 NEXT_ARG();
99 if (get_time(&ce_threshold, *argv)) {
100 fprintf(stderr, "Illegal \"ce_threshold\"\n");
101 return -1;
102 }
103 } else if (strcmp(*argv, "memory_limit") == 0) {
104 NEXT_ARG();
105 if (get_size(&memory, *argv)) {
106 fprintf(stderr, "Illegal \"memory_limit\"\n");
107 return -1;
108 }
109 } else if (strcmp(*argv, "interval") == 0) {
110 NEXT_ARG();
111 if (get_time(&interval, *argv)) {
112 fprintf(stderr, "Illegal \"interval\"\n");
113 return -1;
114 }
115 } else if (strcmp(*argv, "ecn") == 0) {
116 ecn = 1;
117 } else if (strcmp(*argv, "noecn") == 0) {
118 ecn = 0;
119 } else if (strcmp(*argv, "help") == 0) {
120 explain();
121 return -1;
122 } else {
123 fprintf(stderr, "What is \"%s\"?\n", *argv);
124 explain();
125 return -1;
126 }
127 argc--; argv++;
128 }
129
130 tail = NLMSG_TAIL(n);
131 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
132 if (limit)
133 addattr_l(n, 1024, TCA_FQ_CODEL_LIMIT, &limit, sizeof(limit));
134 if (flows)
135 addattr_l(n, 1024, TCA_FQ_CODEL_FLOWS, &flows, sizeof(flows));
136 if (quantum)
137 addattr_l(n, 1024, TCA_FQ_CODEL_QUANTUM, &quantum, sizeof(quantum));
138 if (interval)
139 addattr_l(n, 1024, TCA_FQ_CODEL_INTERVAL, &interval, sizeof(interval));
140 if (target)
141 addattr_l(n, 1024, TCA_FQ_CODEL_TARGET, &target, sizeof(target));
142 if (ecn != -1)
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));
147 if (memory != ~0U)
148 addattr_l(n, 1024, TCA_FQ_CODEL_MEMORY_LIMIT,
149 &memory, sizeof(memory));
150
151 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
152 return 0;
153 }
154
fq_codel_print_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)155 static int fq_codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
156 {
157 struct rtattr *tb[TCA_FQ_CODEL_MAX + 1];
158 unsigned int limit;
159 unsigned int flows;
160 unsigned int interval;
161 unsigned int target;
162 unsigned int ecn;
163 unsigned int quantum;
164 unsigned int ce_threshold;
165 unsigned int memory_limit;
166
167 SPRINT_BUF(b1);
168
169 if (opt == NULL)
170 return 0;
171
172 parse_rtattr_nested(tb, TCA_FQ_CODEL_MAX, opt);
173
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);
178 }
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);
183 }
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);
188 }
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));
193 }
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));
198 }
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));
203 }
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]);
207
208 fprintf(f, "memory_limit %s ", sprint_size(memory_limit, b1));
209 }
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]);
213 if (ecn)
214 fprintf(f, "ecn ");
215 }
216
217 return 0;
218 }
219
fq_codel_print_xstats(struct qdisc_util * qu,FILE * f,struct rtattr * xstats)220 static int fq_codel_print_xstats(struct qdisc_util *qu, FILE *f,
221 struct rtattr *xstats)
222 {
223 struct tc_fq_codel_xstats _st = {}, *st;
224
225 SPRINT_BUF(b1);
226
227 if (xstats == NULL)
228 return 0;
229
230 st = RTA_DATA(xstats);
231 if (RTA_PAYLOAD(xstats) < sizeof(*st)) {
232 memcpy(&_st, st, RTA_PAYLOAD(xstats));
233 st = &_st;
234 }
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);
250 }
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));
262 else
263 fprintf(f, " drop_next %s",
264 sprint_time(st->class_stats.drop_next, b1));
265 }
266 }
267 return 0;
268
269 }
270
271 struct qdisc_util fq_codel_qdisc_util = {
272 .id = "fq_codel",
273 .parse_qopt = fq_codel_parse_opt,
274 .print_qopt = fq_codel_print_opt,
275 .print_xstats = fq_codel_print_xstats,
276 };
277