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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
28 static void explain(void)
31 "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n"
32 " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n"
33 " [ecn] [harddrop] [nodrop]\n");
36 #define RED_SUPPORTED_FLAGS (TC_RED_HISTORIC_FLAGS | TC_RED_NODROP)
38 static int red_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
39 struct nlmsghdr
*n
, const char *dev
)
41 struct nla_bitfield32 flags_bf
= {
42 .selector
= RED_SUPPORTED_FLAGS
,
44 struct tc_red_qopt opt
= {};
45 unsigned int burst
= 0;
46 unsigned int avpkt
= 0;
47 double probability
= 0.02;
48 unsigned int rate
= 0;
55 if (strcmp(*argv
, "limit") == 0) {
57 if (get_size(&opt
.limit
, *argv
)) {
58 fprintf(stderr
, "Illegal \"limit\"\n");
61 } else if (strcmp(*argv
, "min") == 0) {
63 if (get_size(&opt
.qth_min
, *argv
)) {
64 fprintf(stderr
, "Illegal \"min\"\n");
67 } else if (strcmp(*argv
, "max") == 0) {
69 if (get_size(&opt
.qth_max
, *argv
)) {
70 fprintf(stderr
, "Illegal \"max\"\n");
73 } else if (strcmp(*argv
, "burst") == 0) {
75 if (get_unsigned(&burst
, *argv
, 0)) {
76 fprintf(stderr
, "Illegal \"burst\"\n");
79 } else if (strcmp(*argv
, "avpkt") == 0) {
81 if (get_size(&avpkt
, *argv
)) {
82 fprintf(stderr
, "Illegal \"avpkt\"\n");
85 } else if (strcmp(*argv
, "probability") == 0) {
87 if (sscanf(*argv
, "%lg", &probability
) != 1) {
88 fprintf(stderr
, "Illegal \"probability\"\n");
91 } else if (strcmp(*argv
, "bandwidth") == 0) {
93 if (strchr(*argv
, '%')) {
94 if (get_percent_rate(&rate
, *argv
, dev
)) {
95 fprintf(stderr
, "Illegal \"bandwidth\"\n");
98 } else if (get_rate(&rate
, *argv
)) {
99 fprintf(stderr
, "Illegal \"bandwidth\"\n");
102 } else if (strcmp(*argv
, "ecn") == 0) {
103 flags_bf
.value
|= TC_RED_ECN
;
104 } else if (strcmp(*argv
, "harddrop") == 0) {
105 flags_bf
.value
|= TC_RED_HARDDROP
;
106 } else if (strcmp(*argv
, "nodrop") == 0) {
107 flags_bf
.value
|= TC_RED_NODROP
;
108 } else if (strcmp(*argv
, "adaptative") == 0) {
109 flags_bf
.value
|= TC_RED_ADAPTATIVE
;
110 } else if (strcmp(*argv
, "adaptive") == 0) {
111 flags_bf
.value
|= TC_RED_ADAPTATIVE
;
112 } else if (strcmp(*argv
, "help") == 0) {
116 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
123 if (!opt
.limit
|| !avpkt
) {
124 fprintf(stderr
, "RED: Required parameter (limit, avpkt) is missing\n");
127 /* Compute default min/max thresholds based on
128 * Sally Floyd's recommendations:
129 * http://www.icir.org/floyd/REDparameters.txt
132 opt
.qth_max
= opt
.qth_min
? opt
.qth_min
* 3 : opt
.limit
/ 4;
134 opt
.qth_min
= opt
.qth_max
/ 3;
136 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
138 get_rate(&rate
, "10Mbit");
139 fprintf(stderr
, "RED: set bandwidth to 10Mbit\n");
141 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
142 fprintf(stderr
, "RED: failed to calculate EWMA constant.\n");
146 fprintf(stderr
, "RED: WARNING. Burst %u seems to be too large.\n", burst
);
148 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
149 fprintf(stderr
, "RED: failed to calculate probability.\n");
153 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0) {
154 fprintf(stderr
, "RED: failed to calculate idle damping table.\n");
157 opt
.Scell_log
= parm
;
159 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
160 addattr_l(n
, 1024, TCA_RED_PARMS
, &opt
, sizeof(opt
));
161 addattr_l(n
, 1024, TCA_RED_STAB
, sbuf
, 256);
162 max_P
= probability
* pow(2, 32);
163 addattr_l(n
, 1024, TCA_RED_MAX_P
, &max_P
, sizeof(max_P
));
164 addattr_l(n
, 1024, TCA_RED_FLAGS
, &flags_bf
, sizeof(flags_bf
));
165 addattr_nest_end(n
, tail
);
169 static int red_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
171 struct rtattr
*tb
[TCA_RED_MAX
+ 1];
172 struct nla_bitfield32
*flags_bf
;
173 struct tc_red_qopt
*qopt
;
183 parse_rtattr_nested(tb
, TCA_RED_MAX
, opt
);
185 if (tb
[TCA_RED_PARMS
] == NULL
)
187 qopt
= RTA_DATA(tb
[TCA_RED_PARMS
]);
188 if (RTA_PAYLOAD(tb
[TCA_RED_PARMS
]) < sizeof(*qopt
))
191 if (tb
[TCA_RED_MAX_P
] &&
192 RTA_PAYLOAD(tb
[TCA_RED_MAX_P
]) >= sizeof(__u32
))
193 max_P
= rta_getattr_u32(tb
[TCA_RED_MAX_P
]);
195 if (tb
[TCA_RED_FLAGS
] &&
196 RTA_PAYLOAD(tb
[TCA_RED_FLAGS
]) >= sizeof(*flags_bf
)) {
197 flags_bf
= RTA_DATA(tb
[TCA_RED_FLAGS
]);
198 qopt
->flags
= flags_bf
->value
;
201 print_uint(PRINT_JSON
, "limit", NULL
, qopt
->limit
);
202 print_string(PRINT_FP
, NULL
, "limit %s ", sprint_size(qopt
->limit
, b1
));
203 print_uint(PRINT_JSON
, "min", NULL
, qopt
->qth_min
);
204 print_string(PRINT_FP
, NULL
, "min %s ", sprint_size(qopt
->qth_min
, b2
));
205 print_uint(PRINT_JSON
, "max", NULL
, qopt
->qth_max
);
206 print_string(PRINT_FP
, NULL
, "max %s ", sprint_size(qopt
->qth_max
, b3
));
208 tc_red_print_flags(qopt
->flags
);
211 print_uint(PRINT_ANY
, "ewma", "ewma %u ", qopt
->Wlog
);
213 print_float(PRINT_ANY
, "probability",
214 "probability %lg ", max_P
/ pow(2, 32));
216 print_uint(PRINT_ANY
, "Plog", "Plog %u ", qopt
->Plog
);
217 print_uint(PRINT_ANY
, "Scell_log", "Scell_log %u",
223 static int red_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
226 struct tc_red_xstats
*st
;
231 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
234 st
= RTA_DATA(xstats
);
235 print_uint(PRINT_ANY
, "marked", " marked %u ", st
->marked
);
236 print_uint(PRINT_ANY
, "early", "early %u ", st
->early
);
237 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", st
->pdrop
);
238 print_uint(PRINT_ANY
, "other", "other %u ", st
->other
);
244 struct qdisc_util red_qdisc_util
= {
246 .parse_qopt
= red_parse_opt
,
247 .print_qopt
= red_print_opt
,
248 .print_xstats
= red_print_xstats
,