Debian Codegraph @

tc/q_fq_codel.c

raw
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Fair Queue Codel
4 *
5 * Copyright (C) 2012,2015 Eric Dumazet <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16
17 #include "utils.h"
18 #include "tc_util.h"
19
20 static void explain(void)
21 {
22 fprintf(stderr,
23 "Usage: ... fq_codel [ limit PACKETS ] [ flows NUMBER ]\n"
24 "[ memory_limit BYTES ]\n"
25 "[ target TIME ] [ interval TIME ]\n"
26 "[ quantum BYTES ] [ [no]ecn ]\n"
27 "[ ce_threshold TIME ]\n"
28 "[ ce_threshold_selector VALUE/MASK ]\n"
29 "[ drop_batch SIZE ]\n");
30 }
31
32 static int fq_codel_parse_opt(const struct qdisc_util *qu, int argc, char **argv,
33 struct nlmsghdr *n, const char *dev)
34 {
35 unsigned int drop_batch = 0;
36 unsigned int limit = 0;
37 unsigned int flows = 0;
38 unsigned int target = 0;
39 unsigned int interval = 0;
40 unsigned int quantum = 0;
41 unsigned int ce_threshold = ~0U;
42 unsigned int memory = ~0U;
43 __u8 ce_threshold_mask = 0;
44 __u8 ce_threshold_selector = 0xFF;
45 int ecn = -1;
46 struct rtattr *tail;
47
48 while (argc > 0) {
49 if (strcmp(*argv, "limit") == 0) {
50 NEXT_ARG();
51 if (get_unsigned(&limit, *argv, 0)) {
52 fprintf(stderr, "Illegal \"limit\"\n");
53 return -1;
54 }
55 } else if (strcmp(*argv, "flows") == 0) {
56 NEXT_ARG();
57 if (get_unsigned(&flows, *argv, 0)) {
58 fprintf(stderr, "Illegal \"flows\"\n");
59 return -1;
60 }
61 } else if (strcmp(*argv, "quantum") == 0) {
62 NEXT_ARG();
63 if (get_unsigned(&quantum, *argv, 0)) {
64 fprintf(stderr, "Illegal \"quantum\"\n");
65 return -1;
66 }
67 } else if (strcmp(*argv, "drop_batch") == 0) {
68 NEXT_ARG();
69 if (get_unsigned(&drop_batch, *argv, 0)) {
70 fprintf(stderr, "Illegal \"drop_batch\"\n");
71 return -1;
72 }
73 } else if (strcmp(*argv, "target") == 0) {
74 NEXT_ARG();
75 if (get_time(&target, *argv)) {
76 fprintf(stderr, "Illegal \"target\"\n");
77 return -1;
78 }
79 } else if (strcmp(*argv, "ce_threshold") == 0) {
80 NEXT_ARG();
81 if (get_time(&ce_threshold, *argv)) {
82 fprintf(stderr, "Illegal \"ce_threshold\"\n");
83 return -1;
84 }
85 } else if (strcmp(*argv, "ce_threshold_selector") == 0) {
86 char *sep;
87
88 NEXT_ARG();
89 sep = strchr(*argv, '/');
90 if (!sep) {
91 fprintf(stderr, "Missing mask for \"ce_threshold_selector\"\n");
92 return -1;
93 }
94 *sep++ = '\0';
95 if (get_u8(&ce_threshold_mask, sep, 0)) {
96 fprintf(stderr, "Illegal mask for \"ce_threshold_selector\"\n");
97 return -1;
98 }
99 if (get_u8(&ce_threshold_selector, *argv, 0)) {
100 fprintf(stderr, "Illegal \"ce_threshold_selector\"\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 = addattr_nest(n, 1024, TCA_OPTIONS);
131 if (limit)
132 addattr_l(n, 1024, TCA_FQ_CODEL_LIMIT, &limit, sizeof(limit));
133 if (flows)
134 addattr_l(n, 1024, TCA_FQ_CODEL_FLOWS, &flows, sizeof(flows));
135 if (quantum)
136 addattr_l(n, 1024, TCA_FQ_CODEL_QUANTUM, &quantum, sizeof(quantum));
137 if (interval)
138 addattr_l(n, 1024, TCA_FQ_CODEL_INTERVAL, &interval, sizeof(interval));
139 if (target)
140 addattr_l(n, 1024, TCA_FQ_CODEL_TARGET, &target, sizeof(target));
141 if (ecn != -1)
142 addattr_l(n, 1024, TCA_FQ_CODEL_ECN, &ecn, sizeof(ecn));
143 if (ce_threshold != ~0U)
144 addattr_l(n, 1024, TCA_FQ_CODEL_CE_THRESHOLD,
145 &ce_threshold, sizeof(ce_threshold));
146 if (ce_threshold_selector != 0xFF) {
147 addattr8(n, 1024, TCA_FQ_CODEL_CE_THRESHOLD_MASK, ce_threshold_mask);
148 addattr8(n, 1024, TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR, ce_threshold_selector);
149 }
150 if (memory != ~0U)
151 addattr_l(n, 1024, TCA_FQ_CODEL_MEMORY_LIMIT,
152 &memory, sizeof(memory));
153 if (drop_batch)
154 addattr_l(n, 1024, TCA_FQ_CODEL_DROP_BATCH_SIZE, &drop_batch, sizeof(drop_batch));
155
156 addattr_nest_end(n, tail);
157 return 0;
158 }
159
160 static int fq_codel_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
161 {
162 struct rtattr *tb[TCA_FQ_CODEL_MAX + 1];
163 unsigned int limit;
164 unsigned int flows;
165 unsigned int interval;
166 unsigned int target;
167 unsigned int ecn;
168 unsigned int quantum;
169 unsigned int ce_threshold;
170 __u8 ce_threshold_selector = 0;
171 __u8 ce_threshold_mask = 0;
172 unsigned int memory_limit;
173 unsigned int drop_batch;
174
175 SPRINT_BUF(b1);
176
177 if (opt == NULL)
178 return 0;
179
180 parse_rtattr_nested(tb, TCA_FQ_CODEL_MAX, opt);
181
182 if (tb[TCA_FQ_CODEL_LIMIT] &&
183 RTA_PAYLOAD(tb[TCA_FQ_CODEL_LIMIT]) >= sizeof(__u32)) {
184 limit = rta_getattr_u32(tb[TCA_FQ_CODEL_LIMIT]);
185 print_uint(PRINT_ANY, "limit", "limit %up ", limit);
186 }
187 if (tb[TCA_FQ_CODEL_FLOWS] &&
188 RTA_PAYLOAD(tb[TCA_FQ_CODEL_FLOWS]) >= sizeof(__u32)) {
189 flows = rta_getattr_u32(tb[TCA_FQ_CODEL_FLOWS]);
190 print_uint(PRINT_ANY, "flows", "flows %u ", flows);
191 }
192 if (tb[TCA_FQ_CODEL_QUANTUM] &&
193 RTA_PAYLOAD(tb[TCA_FQ_CODEL_QUANTUM]) >= sizeof(__u32)) {
194 quantum = rta_getattr_u32(tb[TCA_FQ_CODEL_QUANTUM]);
195 print_uint(PRINT_ANY, "quantum", "quantum %u ", quantum);
196 }
197 if (tb[TCA_FQ_CODEL_TARGET] &&
198 RTA_PAYLOAD(tb[TCA_FQ_CODEL_TARGET]) >= sizeof(__u32)) {
199 target = rta_getattr_u32(tb[TCA_FQ_CODEL_TARGET]);
200 print_uint(PRINT_JSON, "target", NULL, target);
201 print_string(PRINT_FP, NULL, "target %s ",
202 sprint_time(target, b1));
203 }
204 if (tb[TCA_FQ_CODEL_CE_THRESHOLD] &&
205 RTA_PAYLOAD(tb[TCA_FQ_CODEL_CE_THRESHOLD]) >= sizeof(__u32)) {
206 ce_threshold = rta_getattr_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]);
207 print_uint(PRINT_JSON, "ce_threshold", NULL, ce_threshold);
208 print_string(PRINT_FP, NULL, "ce_threshold %s ",
209 sprint_time(ce_threshold, b1));
210 }
211 if (tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR] &&
212 RTA_PAYLOAD(tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR]) >= sizeof(__u8))
213 ce_threshold_selector = rta_getattr_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR]);
214 if (tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK] &&
215 RTA_PAYLOAD(tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK]) >= sizeof(__u8))
216 ce_threshold_mask = rta_getattr_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK]);
217 if (ce_threshold_mask || ce_threshold_selector) {
218 print_hhu(PRINT_ANY, "ce_threshold_selector", "ce_threshold_selector %#x",
219 ce_threshold_selector);
220 print_hhu(PRINT_ANY, "ce_threshold_mask", "/%#x ",
221 ce_threshold_mask);
222 }
223
224 if (tb[TCA_FQ_CODEL_INTERVAL] &&
225 RTA_PAYLOAD(tb[TCA_FQ_CODEL_INTERVAL]) >= sizeof(__u32)) {
226 interval = rta_getattr_u32(tb[TCA_FQ_CODEL_INTERVAL]);
227 print_uint(PRINT_JSON, "interval", NULL, interval);
228 print_string(PRINT_FP, NULL, "interval %s ",
229 sprint_time(interval, b1));
230 }
231 if (tb[TCA_FQ_CODEL_MEMORY_LIMIT] &&
232 RTA_PAYLOAD(tb[TCA_FQ_CODEL_MEMORY_LIMIT]) >= sizeof(__u32)) {
233 memory_limit = rta_getattr_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]);
234 print_size(PRINT_ANY, "memory_limit", "memory_limit %s ",
235 memory_limit);
236 }
237 if (tb[TCA_FQ_CODEL_ECN] &&
238 RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) {
239 ecn = rta_getattr_u32(tb[TCA_FQ_CODEL_ECN]);
240 if (ecn)
241 print_bool(PRINT_ANY, "ecn", "ecn ", true);
242 }
243 if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE] &&
244 RTA_PAYLOAD(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]) >= sizeof(__u32)) {
245 drop_batch = rta_getattr_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]);
246 if (drop_batch)
247 print_uint(PRINT_ANY, "drop_batch", "drop_batch %u ", drop_batch);
248 }
249
250 return 0;
251 }
252
253 static int fq_codel_print_xstats(const struct qdisc_util *qu, FILE *f,
254 struct rtattr *xstats)
255 {
256 struct tc_fq_codel_xstats _st = {}, *st;
257
258 SPRINT_BUF(b1);
259
260 if (xstats == NULL)
261 return 0;
262
263 st = RTA_DATA(xstats);
264 if (RTA_PAYLOAD(xstats) < sizeof(*st)) {
265 memcpy(&_st, st, RTA_PAYLOAD(xstats));
266 st = &_st;
267 }
268 if (st->type == TCA_FQ_CODEL_XSTATS_QDISC) {
269 print_uint(PRINT_ANY, "maxpacket", " maxpacket %u",
270 st->qdisc_stats.maxpacket);
271 print_uint(PRINT_ANY, "drop_overlimit", " drop_overlimit %u",
272 st->qdisc_stats.drop_overlimit);
273 print_uint(PRINT_ANY, "new_flow_count", " new_flow_count %u",
274 st->qdisc_stats.new_flow_count);
275 print_uint(PRINT_ANY, "ecn_mark", " ecn_mark %u",
276 st->qdisc_stats.ecn_mark);
277 if (st->qdisc_stats.ce_mark)
278 print_uint(PRINT_ANY, "ce_mark", " ce_mark %u",
279 st->qdisc_stats.ce_mark);
280 if (st->qdisc_stats.memory_usage)
281 print_uint(PRINT_ANY, "memory_used", " memory_used %u",
282 st->qdisc_stats.memory_usage);
283 if (st->qdisc_stats.drop_overmemory)
284 print_uint(PRINT_ANY, "drop_overmemory", " drop_overmemory %u",
285 st->qdisc_stats.drop_overmemory);
286 print_nl();
287 print_uint(PRINT_ANY, "new_flows_len", " new_flows_len %u",
288 st->qdisc_stats.new_flows_len);
289 print_uint(PRINT_ANY, "old_flows_len", " old_flows_len %u",
290 st->qdisc_stats.old_flows_len);
291 }
292 if (st->type == TCA_FQ_CODEL_XSTATS_CLASS) {
293 print_int(PRINT_ANY, "deficit", " deficit %d",
294 st->class_stats.deficit);
295 print_uint(PRINT_ANY, "count", " count %u",
296 st->class_stats.count);
297 print_uint(PRINT_ANY, "lastcount", " lastcount %u",
298 st->class_stats.lastcount);
299 print_uint(PRINT_JSON, "ldelay", NULL,
300 st->class_stats.ldelay);
301 print_string(PRINT_FP, NULL, " ldelay %s",
302 sprint_time(st->class_stats.ldelay, b1));
303 if (st->class_stats.dropping) {
304 print_bool(PRINT_ANY, "dropping", " dropping", true);
305 print_int(PRINT_JSON, "drop_next", NULL,
306 st->class_stats.drop_next);
307 if (st->class_stats.drop_next < 0)
308 print_string(PRINT_FP, NULL, " drop_next -%s",
309 sprint_time(-st->class_stats.drop_next, b1));
310 else {
311 print_string(PRINT_FP, NULL, " drop_next %s",
312 sprint_time(st->class_stats.drop_next, b1));
313 }
314 }
315 }
316 return 0;
317
318 }
319
320 struct qdisc_util fq_codel_qdisc_util = {
321 .id = "fq_codel",
322 .parse_qopt = fq_codel_parse_opt,
323 .print_qopt = fq_codel_print_opt,
324 .print_xstats = fq_codel_print_xstats,
325 };