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]\n");
36 static int red_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
37 struct nlmsghdr
*n
, const char *dev
)
39 struct tc_red_qopt opt
= {};
40 unsigned int burst
= 0;
41 unsigned int avpkt
= 0;
42 double probability
= 0.02;
43 unsigned int rate
= 0;
50 if (strcmp(*argv
, "limit") == 0) {
52 if (get_size(&opt
.limit
, *argv
)) {
53 fprintf(stderr
, "Illegal \"limit\"\n");
56 } else if (strcmp(*argv
, "min") == 0) {
58 if (get_size(&opt
.qth_min
, *argv
)) {
59 fprintf(stderr
, "Illegal \"min\"\n");
62 } else if (strcmp(*argv
, "max") == 0) {
64 if (get_size(&opt
.qth_max
, *argv
)) {
65 fprintf(stderr
, "Illegal \"max\"\n");
68 } else if (strcmp(*argv
, "burst") == 0) {
70 if (get_unsigned(&burst
, *argv
, 0)) {
71 fprintf(stderr
, "Illegal \"burst\"\n");
74 } else if (strcmp(*argv
, "avpkt") == 0) {
76 if (get_size(&avpkt
, *argv
)) {
77 fprintf(stderr
, "Illegal \"avpkt\"\n");
80 } else if (strcmp(*argv
, "probability") == 0) {
82 if (sscanf(*argv
, "%lg", &probability
) != 1) {
83 fprintf(stderr
, "Illegal \"probability\"\n");
86 } else if (strcmp(*argv
, "bandwidth") == 0) {
88 if (strchr(*argv
, '%')) {
89 if (get_percent_rate(&rate
, *argv
, dev
)) {
90 fprintf(stderr
, "Illegal \"bandwidth\"\n");
93 } else if (get_rate(&rate
, *argv
)) {
94 fprintf(stderr
, "Illegal \"bandwidth\"\n");
97 } else if (strcmp(*argv
, "ecn") == 0) {
98 opt
.flags
|= TC_RED_ECN
;
99 } else if (strcmp(*argv
, "harddrop") == 0) {
100 opt
.flags
|= TC_RED_HARDDROP
;
101 } else if (strcmp(*argv
, "adaptative") == 0) {
102 opt
.flags
|= TC_RED_ADAPTATIVE
;
103 } else if (strcmp(*argv
, "adaptive") == 0) {
104 opt
.flags
|= TC_RED_ADAPTATIVE
;
105 } else if (strcmp(*argv
, "help") == 0) {
109 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
116 if (!opt
.limit
|| !avpkt
) {
117 fprintf(stderr
, "RED: Required parameter (limit, avpkt) is missing\n");
120 /* Compute default min/max thresholds based on
121 * Sally Floyd's recommendations:
122 * http://www.icir.org/floyd/REDparameters.txt
125 opt
.qth_max
= opt
.qth_min
? opt
.qth_min
* 3 : opt
.limit
/ 4;
127 opt
.qth_min
= opt
.qth_max
/ 3;
129 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
131 get_rate(&rate
, "10Mbit");
132 fprintf(stderr
, "RED: set bandwidth to 10Mbit\n");
134 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
135 fprintf(stderr
, "RED: failed to calculate EWMA constant.\n");
139 fprintf(stderr
, "RED: WARNING. Burst %u seems to be too large.\n", burst
);
141 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
142 fprintf(stderr
, "RED: failed to calculate probability.\n");
146 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0) {
147 fprintf(stderr
, "RED: failed to calculate idle damping table.\n");
150 opt
.Scell_log
= parm
;
152 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
153 addattr_l(n
, 1024, TCA_RED_PARMS
, &opt
, sizeof(opt
));
154 addattr_l(n
, 1024, TCA_RED_STAB
, sbuf
, 256);
155 max_P
= probability
* pow(2, 32);
156 addattr_l(n
, 1024, TCA_RED_MAX_P
, &max_P
, sizeof(max_P
));
157 addattr_nest_end(n
, tail
);
161 static int red_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
163 struct rtattr
*tb
[TCA_RED_MAX
+ 1];
164 struct tc_red_qopt
*qopt
;
174 parse_rtattr_nested(tb
, TCA_RED_MAX
, opt
);
176 if (tb
[TCA_RED_PARMS
] == NULL
)
178 qopt
= RTA_DATA(tb
[TCA_RED_PARMS
]);
179 if (RTA_PAYLOAD(tb
[TCA_RED_PARMS
]) < sizeof(*qopt
))
182 if (tb
[TCA_RED_MAX_P
] &&
183 RTA_PAYLOAD(tb
[TCA_RED_MAX_P
]) >= sizeof(__u32
))
184 max_P
= rta_getattr_u32(tb
[TCA_RED_MAX_P
]);
186 print_uint(PRINT_JSON
, "limit", NULL
, qopt
->limit
);
187 print_string(PRINT_FP
, NULL
, "limit %s ", sprint_size(qopt
->limit
, b1
));
188 print_uint(PRINT_JSON
, "min", NULL
, qopt
->qth_min
);
189 print_string(PRINT_FP
, NULL
, "min %s ", sprint_size(qopt
->qth_min
, b2
));
190 print_uint(PRINT_JSON
, "max", NULL
, qopt
->qth_max
);
191 print_string(PRINT_FP
, NULL
, "max %s ", sprint_size(qopt
->qth_max
, b3
));
193 tc_red_print_flags(qopt
->flags
);
196 print_uint(PRINT_ANY
, "ewma", "ewma %u ", qopt
->Wlog
);
198 print_float(PRINT_ANY
, "probability",
199 "probability %lg ", max_P
/ pow(2, 32));
201 print_uint(PRINT_ANY
, "Plog", "Plog %u ", qopt
->Plog
);
202 print_uint(PRINT_ANY
, "Scell_log", "Scell_log %u",
208 static int red_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
211 struct tc_red_xstats
*st
;
216 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
219 st
= RTA_DATA(xstats
);
220 print_uint(PRINT_ANY
, "marked", " marked %u ", st
->marked
);
221 print_uint(PRINT_ANY
, "early", "early %u ", st
->early
);
222 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", st
->pdrop
);
223 print_uint(PRINT_ANY
, "other", "other %u ", st
->other
);
229 struct qdisc_util red_qdisc_util
= {
231 .parse_qopt
= red_parse_opt
,
232 .print_qopt
= red_print_opt
,
233 .print_xstats
= red_print_xstats
,