]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_red.c
Merge branch 'main' into next
[mirror_iproute2.git] / tc / q_red.c
CommitLineData
aba5acdf
SH
1/*
2 * q_red.c RED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
d798a048 21#include <math.h>
aba5acdf
SH
22
23#include "utils.h"
24#include "tc_util.h"
d0e45043 25#include "tc_qevent.h"
aba5acdf
SH
26
27#include "tc_red.h"
28
29static void explain(void)
30{
8589eb4e
MC
31 fprintf(stderr,
32 "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n"
33 " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n"
d0e45043
PM
34 " [ecn] [harddrop] [nodrop]\n"
35 " [qevent early_drop block IDX] [qevent mark block IDX]\n");
aba5acdf
SH
36}
37
6c10fdca
PM
38#define RED_SUPPORTED_FLAGS (TC_RED_HISTORIC_FLAGS | TC_RED_NODROP)
39
d0e45043
PM
40static struct qevent_plain qe_early_drop = {};
41static struct qevent_plain qe_mark = {};
42static struct qevent_util qevents[] = {
43 QEVENT("early_drop", plain, &qe_early_drop, TCA_RED_EARLY_DROP_BLOCK),
44 QEVENT("mark", plain, &qe_mark, TCA_RED_MARK_BLOCK),
45 {},
46};
47
859af0a5
SH
48static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv,
49 struct nlmsghdr *n, const char *dev)
aba5acdf 50{
6c10fdca
PM
51 struct nla_bitfield32 flags_bf = {
52 .selector = RED_SUPPORTED_FLAGS,
53 };
d17b136f 54 struct tc_red_qopt opt = {};
32a121cb
SH
55 unsigned int burst = 0;
56 unsigned int avpkt = 0;
aba5acdf 57 double probability = 0.02;
32a121cb 58 unsigned int rate = 0;
9d9a67c7 59 int parm;
aba5acdf 60 __u8 sbuf[256];
e7e4abea 61 __u32 max_P;
aba5acdf
SH
62 struct rtattr *tail;
63
d0e45043
PM
64 qevents_init(qevents);
65
aba5acdf
SH
66 while (argc > 0) {
67 if (strcmp(*argv, "limit") == 0) {
68 NEXT_ARG();
69 if (get_size(&opt.limit, *argv)) {
70 fprintf(stderr, "Illegal \"limit\"\n");
71 return -1;
72 }
aba5acdf
SH
73 } else if (strcmp(*argv, "min") == 0) {
74 NEXT_ARG();
75 if (get_size(&opt.qth_min, *argv)) {
76 fprintf(stderr, "Illegal \"min\"\n");
77 return -1;
78 }
aba5acdf
SH
79 } else if (strcmp(*argv, "max") == 0) {
80 NEXT_ARG();
81 if (get_size(&opt.qth_max, *argv)) {
82 fprintf(stderr, "Illegal \"max\"\n");
83 return -1;
84 }
aba5acdf
SH
85 } else if (strcmp(*argv, "burst") == 0) {
86 NEXT_ARG();
87 if (get_unsigned(&burst, *argv, 0)) {
88 fprintf(stderr, "Illegal \"burst\"\n");
89 return -1;
90 }
aba5acdf
SH
91 } else if (strcmp(*argv, "avpkt") == 0) {
92 NEXT_ARG();
93 if (get_size(&avpkt, *argv)) {
94 fprintf(stderr, "Illegal \"avpkt\"\n");
95 return -1;
96 }
aba5acdf
SH
97 } else if (strcmp(*argv, "probability") == 0) {
98 NEXT_ARG();
99 if (sscanf(*argv, "%lg", &probability) != 1) {
100 fprintf(stderr, "Illegal \"probability\"\n");
101 return -1;
102 }
aba5acdf
SH
103 } else if (strcmp(*argv, "bandwidth") == 0) {
104 NEXT_ARG();
927e3cfb
ND
105 if (strchr(*argv, '%')) {
106 if (get_percent_rate(&rate, *argv, dev)) {
107 fprintf(stderr, "Illegal \"bandwidth\"\n");
108 return -1;
109 }
110 } else if (get_rate(&rate, *argv)) {
aba5acdf
SH
111 fprintf(stderr, "Illegal \"bandwidth\"\n");
112 return -1;
113 }
aba5acdf 114 } else if (strcmp(*argv, "ecn") == 0) {
6c10fdca 115 flags_bf.value |= TC_RED_ECN;
841fc7bc 116 } else if (strcmp(*argv, "harddrop") == 0) {
6c10fdca
PM
117 flags_bf.value |= TC_RED_HARDDROP;
118 } else if (strcmp(*argv, "nodrop") == 0) {
119 flags_bf.value |= TC_RED_NODROP;
e7e4abea 120 } else if (strcmp(*argv, "adaptative") == 0) {
6c10fdca 121 flags_bf.value |= TC_RED_ADAPTATIVE;
54a2fce8 122 } else if (strcmp(*argv, "adaptive") == 0) {
6c10fdca 123 flags_bf.value |= TC_RED_ADAPTATIVE;
d0e45043
PM
124 } else if (matches(*argv, "qevent") == 0) {
125 NEXT_ARG();
126 if (qevent_parse(qevents, &argc, &argv))
127 return -1;
128 continue;
aba5acdf
SH
129 } else if (strcmp(*argv, "help") == 0) {
130 explain();
131 return -1;
132 } else {
133 fprintf(stderr, "What is \"%s\"?\n", *argv);
134 explain();
135 return -1;
136 }
137 argc--; argv++;
138 }
139
841fc7bc
ED
140 if (!opt.limit || !avpkt) {
141 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
aba5acdf
SH
142 return -1;
143 }
841fc7bc
ED
144 /* Compute default min/max thresholds based on
145 * Sally Floyd's recommendations:
146 * http://www.icir.org/floyd/REDparameters.txt
147 */
148 if (!opt.qth_max)
149 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
150 if (!opt.qth_min)
151 opt.qth_min = opt.qth_max / 3;
152 if (!burst)
ab15aeac 153 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
d93c909a
DW
154 if (!rate) {
155 get_rate(&rate, "10Mbit");
156 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
157 }
9d9a67c7 158 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
159 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
160 return -1;
161 }
9d9a67c7 162 if (parm >= 10)
6c99695d 163 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
9d9a67c7
DW
164 opt.Wlog = parm;
165 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
166 fprintf(stderr, "RED: failed to calculate probability.\n");
167 return -1;
168 }
9d9a67c7
DW
169 opt.Plog = parm;
170 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
aba5acdf
SH
171 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
172 return -1;
173 }
9d9a67c7 174 opt.Scell_log = parm;
aba5acdf 175
c14f9d92 176 tail = addattr_nest(n, 1024, TCA_OPTIONS);
aba5acdf
SH
177 addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
178 addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
e7e4abea
ED
179 max_P = probability * pow(2, 32);
180 addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
6c10fdca 181 addattr_l(n, 1024, TCA_RED_FLAGS, &flags_bf, sizeof(flags_bf));
d0e45043
PM
182 if (qevents_dump(qevents, n))
183 return -1;
c14f9d92 184 addattr_nest_end(n, tail);
aba5acdf
SH
185 return 0;
186}
187
188static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
189{
841fc7bc 190 struct rtattr *tb[TCA_RED_MAX + 1];
6c10fdca 191 struct nla_bitfield32 *flags_bf;
aba5acdf 192 struct tc_red_qopt *qopt;
e7e4abea 193 __u32 max_P = 0;
32a121cb 194
aba5acdf
SH
195 SPRINT_BUF(b1);
196 SPRINT_BUF(b2);
197 SPRINT_BUF(b3);
198
199 if (opt == NULL)
200 return 0;
201
841fc7bc 202 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
aba5acdf
SH
203
204 if (tb[TCA_RED_PARMS] == NULL)
205 return -1;
206 qopt = RTA_DATA(tb[TCA_RED_PARMS]);
207 if (RTA_PAYLOAD(tb[TCA_RED_PARMS]) < sizeof(*qopt))
208 return -1;
e7e4abea
ED
209
210 if (tb[TCA_RED_MAX_P] &&
211 RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
ff24746c 212 max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
e7e4abea 213
6c10fdca
PM
214 if (tb[TCA_RED_FLAGS] &&
215 RTA_PAYLOAD(tb[TCA_RED_FLAGS]) >= sizeof(*flags_bf)) {
216 flags_bf = RTA_DATA(tb[TCA_RED_FLAGS]);
217 qopt->flags = flags_bf->value;
218 }
219
097415d5
JK
220 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
221 print_string(PRINT_FP, NULL, "limit %s ", sprint_size(qopt->limit, b1));
222 print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
223 print_string(PRINT_FP, NULL, "min %s ", sprint_size(qopt->qth_min, b2));
224 print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
41a9e384 225 print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3));
097415d5 226
33021752
JK
227 tc_red_print_flags(qopt->flags);
228
aba5acdf 229 if (show_details) {
41a9e384 230 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
e7e4abea 231 if (max_P)
097415d5
JK
232 print_float(PRINT_ANY, "probability",
233 "probability %lg ", max_P / pow(2, 32));
e7e4abea 234 else
097415d5
JK
235 print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
236 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
237 qopt->Scell_log);
aba5acdf 238 }
d0e45043
PM
239
240 qevents_init(qevents);
241 if (qevents_read(qevents, tb))
242 return -1;
243 qevents_print(qevents, f);
aba5acdf
SH
244 return 0;
245}
246
247static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
248{
249#ifdef TC_RED_ECN
250 struct tc_red_xstats *st;
251
252 if (xstats == NULL)
253 return 0;
254
255 if (RTA_PAYLOAD(xstats) < sizeof(*st))
256 return -1;
257
258 st = RTA_DATA(xstats);
097415d5
JK
259 print_uint(PRINT_ANY, "marked", " marked %u ", st->marked);
260 print_uint(PRINT_ANY, "early", "early %u ", st->early);
261 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", st->pdrop);
262 print_uint(PRINT_ANY, "other", "other %u ", st->other);
aba5acdf
SH
263#endif
264 return 0;
265}
266
02dce2fd
PM
267static int red_has_block(struct qdisc_util *qu, struct rtattr *opt, __u32 block_idx, bool *p_has)
268{
269 struct rtattr *tb[TCA_RED_MAX + 1];
270
271 if (opt == NULL)
272 return 0;
273
274 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
275
276 qevents_init(qevents);
277 if (qevents_read(qevents, tb))
278 return -1;
279
280 *p_has = qevents_have_block(qevents, block_idx);
281 return 0;
282}
aba5acdf 283
95812b56 284struct qdisc_util red_qdisc_util = {
f2f99e2e
SH
285 .id = "red",
286 .parse_qopt = red_parse_opt,
287 .print_qopt = red_print_opt,
288 .print_xstats = red_print_xstats,
02dce2fd 289 .has_block = red_has_block,
aba5acdf 290};