]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_red.c
Merge branch 'tc-mpls-l2-vpn' into next
[mirror_iproute2.git] / tc / q_red.c
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>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <math.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25 #include "tc_qevent.h"
26
27 #include "tc_red.h"
28
29 static void explain(void)
30 {
31 fprintf(stderr,
32 "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n"
33 " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n"
34 " [ecn] [harddrop] [nodrop]\n"
35 " [qevent early_drop block IDX] [qevent mark block IDX]\n");
36 }
37
38 #define RED_SUPPORTED_FLAGS (TC_RED_HISTORIC_FLAGS | TC_RED_NODROP)
39
40 static struct qevent_plain qe_early_drop = {};
41 static struct qevent_plain qe_mark = {};
42 static 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
48 static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv,
49 struct nlmsghdr *n, const char *dev)
50 {
51 struct nla_bitfield32 flags_bf = {
52 .selector = RED_SUPPORTED_FLAGS,
53 };
54 struct tc_red_qopt opt = {};
55 unsigned int burst = 0;
56 unsigned int avpkt = 0;
57 double probability = 0.02;
58 unsigned int rate = 0;
59 int parm;
60 __u8 sbuf[256];
61 __u32 max_P;
62 struct rtattr *tail;
63
64 qevents_init(qevents);
65
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 }
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 }
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 }
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 }
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 }
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 }
103 } else if (strcmp(*argv, "bandwidth") == 0) {
104 NEXT_ARG();
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)) {
111 fprintf(stderr, "Illegal \"bandwidth\"\n");
112 return -1;
113 }
114 } else if (strcmp(*argv, "ecn") == 0) {
115 flags_bf.value |= TC_RED_ECN;
116 } else if (strcmp(*argv, "harddrop") == 0) {
117 flags_bf.value |= TC_RED_HARDDROP;
118 } else if (strcmp(*argv, "nodrop") == 0) {
119 flags_bf.value |= TC_RED_NODROP;
120 } else if (strcmp(*argv, "adaptative") == 0) {
121 flags_bf.value |= TC_RED_ADAPTATIVE;
122 } else if (strcmp(*argv, "adaptive") == 0) {
123 flags_bf.value |= TC_RED_ADAPTATIVE;
124 } else if (matches(*argv, "qevent") == 0) {
125 NEXT_ARG();
126 if (qevent_parse(qevents, &argc, &argv))
127 return -1;
128 continue;
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
140 if (!opt.limit || !avpkt) {
141 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
142 return -1;
143 }
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)
153 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
154 if (!rate) {
155 get_rate(&rate, "10Mbit");
156 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
157 }
158 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
159 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
160 return -1;
161 }
162 if (parm >= 10)
163 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
164 opt.Wlog = parm;
165 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
166 fprintf(stderr, "RED: failed to calculate probability.\n");
167 return -1;
168 }
169 opt.Plog = parm;
170 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
171 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
172 return -1;
173 }
174 opt.Scell_log = parm;
175
176 tail = addattr_nest(n, 1024, TCA_OPTIONS);
177 addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
178 addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
179 max_P = probability * pow(2, 32);
180 addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
181 addattr_l(n, 1024, TCA_RED_FLAGS, &flags_bf, sizeof(flags_bf));
182 if (qevents_dump(qevents, n))
183 return -1;
184 addattr_nest_end(n, tail);
185 return 0;
186 }
187
188 static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
189 {
190 struct rtattr *tb[TCA_RED_MAX + 1];
191 struct nla_bitfield32 *flags_bf;
192 struct tc_red_qopt *qopt;
193 __u32 max_P = 0;
194
195 SPRINT_BUF(b1);
196 SPRINT_BUF(b2);
197 SPRINT_BUF(b3);
198
199 if (opt == NULL)
200 return 0;
201
202 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
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;
209
210 if (tb[TCA_RED_MAX_P] &&
211 RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
212 max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
213
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
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);
225 print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3));
226
227 tc_red_print_flags(qopt->flags);
228
229 if (show_details) {
230 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
231 if (max_P)
232 print_float(PRINT_ANY, "probability",
233 "probability %lg ", max_P / pow(2, 32));
234 else
235 print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
236 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
237 qopt->Scell_log);
238 }
239
240 qevents_init(qevents);
241 if (qevents_read(qevents, tb))
242 return -1;
243 qevents_print(qevents, f);
244 return 0;
245 }
246
247 static 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);
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);
263 #endif
264 return 0;
265 }
266
267 static 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 }
283
284 struct qdisc_util red_qdisc_util = {
285 .id = "red",
286 .parse_qopt = red_parse_opt,
287 .print_qopt = red_print_opt,
288 .print_xstats = red_print_xstats,
289 .has_block = red_has_block,
290 };