Debian Codegraph @

tc/q_red.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * q_red.c RED.
4 *
5 * Authors: Alexey Kuznetsov, <[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 #include <math.h>
17
18 #include "utils.h"
19 #include "tc_util.h"
20 #include "tc_qevent.h"
21
22 #include "tc_red.h"
23
24 static void explain(void)
25 {
26 fprintf(stderr,
27 "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n"
28 " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n"
29 " [ecn] [harddrop] [nodrop]\n"
30 " [qevent early_drop block IDX] [qevent mark block IDX]\n");
31 }
32
33 #define RED_SUPPORTED_FLAGS (TC_RED_HISTORIC_FLAGS | TC_RED_NODROP)
34
35 static struct qevent_plain qe_early_drop = {};
36 static struct qevent_plain qe_mark = {};
37 static struct qevent_util qevents[] = {
38 QEVENT("early_drop", plain, &qe_early_drop, TCA_RED_EARLY_DROP_BLOCK),
39 QEVENT("mark", plain, &qe_mark, TCA_RED_MARK_BLOCK),
40 {},
41 };
42
43 static int red_parse_opt(const struct qdisc_util *qu, int argc, char **argv,
44 struct nlmsghdr *n, const char *dev)
45 {
46 struct nla_bitfield32 flags_bf = {
47 .selector = RED_SUPPORTED_FLAGS,
48 };
49 struct tc_red_qopt opt = {};
50 unsigned int burst = 0;
51 unsigned int avpkt = 0;
52 double probability = 0.02;
53 unsigned int rate = 0;
54 int parm;
55 __u8 sbuf[256];
56 __u32 max_P;
57 struct rtattr *tail;
58
59 qevents_init(qevents);
60
61 while (argc > 0) {
62 if (strcmp(*argv, "limit") == 0) {
63 NEXT_ARG();
64 if (get_size(&opt.limit, *argv)) {
65 fprintf(stderr, "Illegal \"limit\"\n");
66 return -1;
67 }
68 } else if (strcmp(*argv, "min") == 0) {
69 NEXT_ARG();
70 if (get_size(&opt.qth_min, *argv)) {
71 fprintf(stderr, "Illegal \"min\"\n");
72 return -1;
73 }
74 } else if (strcmp(*argv, "max") == 0) {
75 NEXT_ARG();
76 if (get_size(&opt.qth_max, *argv)) {
77 fprintf(stderr, "Illegal \"max\"\n");
78 return -1;
79 }
80 } else if (strcmp(*argv, "burst") == 0) {
81 NEXT_ARG();
82 if (get_unsigned(&burst, *argv, 0)) {
83 fprintf(stderr, "Illegal \"burst\"\n");
84 return -1;
85 }
86 } else if (strcmp(*argv, "avpkt") == 0) {
87 NEXT_ARG();
88 if (get_size(&avpkt, *argv)) {
89 fprintf(stderr, "Illegal \"avpkt\"\n");
90 return -1;
91 }
92 } else if (strcmp(*argv, "probability") == 0) {
93 NEXT_ARG();
94 if (sscanf(*argv, "%lg", &probability) != 1) {
95 fprintf(stderr, "Illegal \"probability\"\n");
96 return -1;
97 }
98 } else if (strcmp(*argv, "bandwidth") == 0) {
99 NEXT_ARG();
100 if (strchr(*argv, '%')) {
101 if (get_percent_rate(&rate, *argv, dev)) {
102 fprintf(stderr, "Illegal \"bandwidth\"\n");
103 return -1;
104 }
105 } else if (get_rate(&rate, *argv)) {
106 fprintf(stderr, "Illegal \"bandwidth\"\n");
107 return -1;
108 }
109 } else if (strcmp(*argv, "ecn") == 0) {
110 flags_bf.value |= TC_RED_ECN;
111 } else if (strcmp(*argv, "harddrop") == 0) {
112 flags_bf.value |= TC_RED_HARDDROP;
113 } else if (strcmp(*argv, "nodrop") == 0) {
114 flags_bf.value |= TC_RED_NODROP;
115 } else if (strcmp(*argv, "adaptative") == 0) {
116 flags_bf.value |= TC_RED_ADAPTATIVE;
117 } else if (strcmp(*argv, "adaptive") == 0) {
118 flags_bf.value |= TC_RED_ADAPTATIVE;
119 } else if (matches(*argv, "qevent") == 0) {
120 NEXT_ARG();
121 if (qevent_parse(qevents, &argc, &argv))
122 return -1;
123 continue;
124 } else if (strcmp(*argv, "help") == 0) {
125 explain();
126 return -1;
127 } else {
128 fprintf(stderr, "What is \"%s\"?\n", *argv);
129 explain();
130 return -1;
131 }
132 argc--; argv++;
133 }
134
135 if (!opt.limit || !avpkt) {
136 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
137 return -1;
138 }
139 /* Compute default min/max thresholds based on
140 * Sally Floyd's recommendations:
141 * http://www.icir.org/floyd/REDparameters.txt
142 */
143 if (!opt.qth_max)
144 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
145 if (!opt.qth_min)
146 opt.qth_min = opt.qth_max / 3;
147 if (!burst)
148 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
149 if (!rate) {
150 get_rate(&rate, "10Mbit");
151 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
152 }
153 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
154 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
155 return -1;
156 }
157 if (parm >= 10)
158 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
159 opt.Wlog = parm;
160 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
161 fprintf(stderr, "RED: failed to calculate probability.\n");
162 return -1;
163 }
164 opt.Plog = parm;
165 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
166 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
167 return -1;
168 }
169 opt.Scell_log = parm;
170
171 tail = addattr_nest(n, 1024, TCA_OPTIONS);
172 addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
173 addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
174 max_P = probability * pow(2, 32);
175 addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
176 addattr_l(n, 1024, TCA_RED_FLAGS, &flags_bf, sizeof(flags_bf));
177 if (qevents_dump(qevents, n))
178 return -1;
179 addattr_nest_end(n, tail);
180 return 0;
181 }
182
183 static int red_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
184 {
185 struct rtattr *tb[TCA_RED_MAX + 1];
186 struct nla_bitfield32 *flags_bf;
187 struct tc_red_qopt *qopt;
188 __u32 max_P = 0;
189
190 if (opt == NULL)
191 return 0;
192
193 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
194
195 if (tb[TCA_RED_PARMS] == NULL)
196 return -1;
197 qopt = RTA_DATA(tb[TCA_RED_PARMS]);
198 if (RTA_PAYLOAD(tb[TCA_RED_PARMS]) < sizeof(*qopt))
199 return -1;
200
201 if (tb[TCA_RED_MAX_P] &&
202 RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
203 max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
204
205 if (tb[TCA_RED_FLAGS] &&
206 RTA_PAYLOAD(tb[TCA_RED_FLAGS]) >= sizeof(*flags_bf)) {
207 flags_bf = RTA_DATA(tb[TCA_RED_FLAGS]);
208 qopt->flags = flags_bf->value;
209 }
210
211 print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit);
212 print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min);
213 print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max);
214
215 tc_red_print_flags(qopt->flags);
216
217 if (show_details) {
218 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
219 if (max_P)
220 print_float(PRINT_ANY, "probability",
221 "probability %lg ", max_P / pow(2, 32));
222 else
223 print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
224 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
225 qopt->Scell_log);
226 }
227
228 qevents_init(qevents);
229 if (qevents_read(qevents, tb))
230 return -1;
231 qevents_print(qevents, f);
232 return 0;
233 }
234
235 static int red_print_xstats(const struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
236 {
237 #ifdef TC_RED_ECN
238 struct tc_red_xstats *st;
239
240 if (xstats == NULL)
241 return 0;
242
243 if (RTA_PAYLOAD(xstats) < sizeof(*st))
244 return -1;
245
246 st = RTA_DATA(xstats);
247 print_uint(PRINT_ANY, "marked", " marked %u ", st->marked);
248 print_uint(PRINT_ANY, "early", "early %u ", st->early);
249 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", st->pdrop);
250 print_uint(PRINT_ANY, "other", "other %u ", st->other);
251 #endif
252 return 0;
253 }
254
255 static int red_has_block(const struct qdisc_util *qu, struct rtattr *opt, __u32 block_idx, bool *p_has)
256 {
257 struct rtattr *tb[TCA_RED_MAX + 1];
258
259 if (opt == NULL)
260 return 0;
261
262 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
263
264 qevents_init(qevents);
265 if (qevents_read(qevents, tb))
266 return -1;
267
268 *p_has = qevents_have_block(qevents, block_idx);
269 return 0;
270 }
271
272 struct qdisc_util red_qdisc_util = {
273 .id = "red",
274 .parse_qopt = red_parse_opt,
275 .print_qopt = red_print_opt,
276 .print_xstats = red_print_xstats,
277 .has_block = red_has_block,
278 };