Debian Codegraph @

tc/q_codel.c

raw
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Codel - The Controlled-Delay Active Queue Management algorithm
4 *
5 * Copyright (C) 2011-2012 Kathleen Nichols <[email protected]>
6 * Copyright (C) 2011-2012 Van Jacobson <[email protected]>
7 * Copyright (C) 2012 Michael D. Taht <[email protected]>
8 * Copyright (C) 2012,2015 Eric Dumazet <[email protected]>
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <string.h>
19
20 #include "utils.h"
21 #include "tc_util.h"
22
23 static void explain(void)
24 {
25 fprintf(stderr,
26 "Usage: ... codel [ limit PACKETS ] [ target TIME ]\n"
27 " [ interval TIME ] [ ecn | noecn ]\n"
28 " [ ce_threshold TIME ]\n");
29 }
30
31 static int codel_parse_opt(const struct qdisc_util *qu, int argc, char **argv,
32 struct nlmsghdr *n, const char *dev)
33 {
34 unsigned int limit = 0;
35 unsigned int target = 0;
36 unsigned int interval = 0;
37 unsigned int ce_threshold = ~0U;
38 int ecn = -1;
39 struct rtattr *tail;
40
41 while (argc > 0) {
42 if (strcmp(*argv, "limit") == 0) {
43 NEXT_ARG();
44 if (get_unsigned(&limit, *argv, 0)) {
45 fprintf(stderr, "Illegal \"limit\"\n");
46 return -1;
47 }
48 } else if (strcmp(*argv, "target") == 0) {
49 NEXT_ARG();
50 if (get_time(&target, *argv)) {
51 fprintf(stderr, "Illegal \"target\"\n");
52 return -1;
53 }
54 } else if (strcmp(*argv, "ce_threshold") == 0) {
55 NEXT_ARG();
56 if (get_time(&ce_threshold, *argv)) {
57 fprintf(stderr, "Illegal \"ce_threshold\"\n");
58 return -1;
59 }
60 } else if (strcmp(*argv, "interval") == 0) {
61 NEXT_ARG();
62 if (get_time(&interval, *argv)) {
63 fprintf(stderr, "Illegal \"interval\"\n");
64 return -1;
65 }
66 } else if (strcmp(*argv, "ecn") == 0) {
67 ecn = 1;
68 } else if (strcmp(*argv, "noecn") == 0) {
69 ecn = 0;
70 } else if (strcmp(*argv, "help") == 0) {
71 explain();
72 return -1;
73 } else {
74 fprintf(stderr, "What is \"%s\"?\n", *argv);
75 explain();
76 return -1;
77 }
78 argc--; argv++;
79 }
80
81 tail = addattr_nest(n, 1024, TCA_OPTIONS);
82 if (limit)
83 addattr_l(n, 1024, TCA_CODEL_LIMIT, &limit, sizeof(limit));
84 if (interval)
85 addattr_l(n, 1024, TCA_CODEL_INTERVAL, &interval, sizeof(interval));
86 if (target)
87 addattr_l(n, 1024, TCA_CODEL_TARGET, &target, sizeof(target));
88 if (ecn != -1)
89 addattr_l(n, 1024, TCA_CODEL_ECN, &ecn, sizeof(ecn));
90 if (ce_threshold != ~0U)
91 addattr_l(n, 1024, TCA_CODEL_CE_THRESHOLD,
92 &ce_threshold, sizeof(ce_threshold));
93
94 addattr_nest_end(n, tail);
95 return 0;
96 }
97
98 static int codel_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
99 {
100 struct rtattr *tb[TCA_CODEL_MAX + 1];
101 unsigned int limit;
102 unsigned int interval;
103 unsigned int target;
104 unsigned int ecn;
105 unsigned int ce_threshold;
106
107 SPRINT_BUF(b1);
108
109 if (opt == NULL)
110 return 0;
111
112 parse_rtattr_nested(tb, TCA_CODEL_MAX, opt);
113
114 if (tb[TCA_CODEL_LIMIT] &&
115 RTA_PAYLOAD(tb[TCA_CODEL_LIMIT]) >= sizeof(__u32)) {
116 limit = rta_getattr_u32(tb[TCA_CODEL_LIMIT]);
117 print_uint(PRINT_ANY, "limit", "limit %up ", limit);
118 }
119 if (tb[TCA_CODEL_TARGET] &&
120 RTA_PAYLOAD(tb[TCA_CODEL_TARGET]) >= sizeof(__u32)) {
121 target = rta_getattr_u32(tb[TCA_CODEL_TARGET]);
122 print_uint(PRINT_JSON, "target", NULL, target);
123 print_string(PRINT_FP, NULL, "target %s ",
124 sprint_time(target, b1));
125 }
126 if (tb[TCA_CODEL_CE_THRESHOLD] &&
127 RTA_PAYLOAD(tb[TCA_CODEL_CE_THRESHOLD]) >= sizeof(__u32)) {
128 ce_threshold = rta_getattr_u32(tb[TCA_CODEL_CE_THRESHOLD]);
129 print_uint(PRINT_JSON, "ce_threshold", NULL, ce_threshold);
130 print_string(PRINT_FP, NULL, "ce_threshold %s ",
131 sprint_time(ce_threshold, b1));
132 }
133 if (tb[TCA_CODEL_INTERVAL] &&
134 RTA_PAYLOAD(tb[TCA_CODEL_INTERVAL]) >= sizeof(__u32)) {
135 interval = rta_getattr_u32(tb[TCA_CODEL_INTERVAL]);
136 print_uint(PRINT_JSON, "interval", NULL, interval);
137 print_string(PRINT_FP, NULL, "interval %s ",
138 sprint_time(interval, b1));
139 }
140 if (tb[TCA_CODEL_ECN] &&
141 RTA_PAYLOAD(tb[TCA_CODEL_ECN]) >= sizeof(__u32)) {
142 ecn = rta_getattr_u32(tb[TCA_CODEL_ECN]);
143 if (ecn)
144 print_bool(PRINT_ANY, "ecn", "ecn ", true);
145 }
146
147 return 0;
148 }
149
150 static int codel_print_xstats(const struct qdisc_util *qu, FILE *f,
151 struct rtattr *xstats)
152 {
153 struct tc_codel_xstats _st = {}, *st;
154
155 SPRINT_BUF(b1);
156
157 if (xstats == NULL)
158 return 0;
159
160 st = RTA_DATA(xstats);
161 if (RTA_PAYLOAD(xstats) < sizeof(*st)) {
162 memcpy(&_st, st, RTA_PAYLOAD(xstats));
163 st = &_st;
164 }
165
166 print_uint(PRINT_ANY, "count", " count %u", st->count);
167 print_uint(PRINT_ANY, "lastcount", " lastcount %u", st->lastcount);
168 print_uint(PRINT_JSON, "ldelay", NULL, st->ldelay);
169 print_string(PRINT_FP, NULL, " ldelay %s", sprint_time(st->ldelay, b1));
170
171 if (st->dropping)
172 print_bool(PRINT_ANY, "dropping", " dropping", true);
173
174 print_int(PRINT_JSON, "drop_next", NULL, st->drop_next);
175 if (st->drop_next < 0)
176 print_string(PRINT_FP, NULL, " drop_next -%s",
177 sprint_time(-st->drop_next, b1));
178 else
179 print_string(PRINT_FP, NULL, " drop_next %s",
180 sprint_time(st->drop_next, b1));
181
182 print_nl();
183 print_uint(PRINT_ANY, "maxpacket", " maxpacket %u", st->maxpacket);
184 print_uint(PRINT_ANY, "ecn_mark", " ecn_mark %u", st->ecn_mark);
185 print_uint(PRINT_ANY, "drop_overlimit", " drop_overlimit %u",
186 st->drop_overlimit);
187
188 if (st->ce_mark)
189 print_uint(PRINT_ANY, "ce_mark", " ce_mark %u", st->ce_mark);
190
191 return 0;
192
193 }
194
195 struct qdisc_util codel_qdisc_util = {
196 .id = "codel",
197 .parse_qopt = codel_parse_opt,
198 .print_qopt = codel_print_opt,
199 .print_xstats = codel_print_xstats,
200 };