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)
30 fprintf(stderr
, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
31 fprintf(stderr
, " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n");
32 fprintf(stderr
, " [ecn] [harddrop]\n");
35 static int red_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
36 struct nlmsghdr
*n
, const char *dev
)
38 struct tc_red_qopt opt
= {};
39 unsigned int burst
= 0;
40 unsigned int avpkt
= 0;
41 double probability
= 0.02;
42 unsigned int rate
= 0;
49 if (strcmp(*argv
, "limit") == 0) {
51 if (get_size(&opt
.limit
, *argv
)) {
52 fprintf(stderr
, "Illegal \"limit\"\n");
55 } else if (strcmp(*argv
, "min") == 0) {
57 if (get_size(&opt
.qth_min
, *argv
)) {
58 fprintf(stderr
, "Illegal \"min\"\n");
61 } else if (strcmp(*argv
, "max") == 0) {
63 if (get_size(&opt
.qth_max
, *argv
)) {
64 fprintf(stderr
, "Illegal \"max\"\n");
67 } else if (strcmp(*argv
, "burst") == 0) {
69 if (get_unsigned(&burst
, *argv
, 0)) {
70 fprintf(stderr
, "Illegal \"burst\"\n");
73 } else if (strcmp(*argv
, "avpkt") == 0) {
75 if (get_size(&avpkt
, *argv
)) {
76 fprintf(stderr
, "Illegal \"avpkt\"\n");
79 } else if (strcmp(*argv
, "probability") == 0) {
81 if (sscanf(*argv
, "%lg", &probability
) != 1) {
82 fprintf(stderr
, "Illegal \"probability\"\n");
85 } else if (strcmp(*argv
, "bandwidth") == 0) {
87 if (strchr(*argv
, '%')) {
88 if (get_percent_rate(&rate
, *argv
, dev
)) {
89 fprintf(stderr
, "Illegal \"bandwidth\"\n");
92 } else if (get_rate(&rate
, *argv
)) {
93 fprintf(stderr
, "Illegal \"bandwidth\"\n");
96 } else if (strcmp(*argv
, "ecn") == 0) {
97 opt
.flags
|= TC_RED_ECN
;
98 } else if (strcmp(*argv
, "harddrop") == 0) {
99 opt
.flags
|= TC_RED_HARDDROP
;
100 } else if (strcmp(*argv
, "adaptative") == 0) {
101 opt
.flags
|= TC_RED_ADAPTATIVE
;
102 } else if (strcmp(*argv
, "adaptive") == 0) {
103 opt
.flags
|= TC_RED_ADAPTATIVE
;
104 } else if (strcmp(*argv
, "help") == 0) {
108 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
115 if (!opt
.limit
|| !avpkt
) {
116 fprintf(stderr
, "RED: Required parameter (limit, avpkt) is missing\n");
119 /* Compute default min/max thresholds based on
120 * Sally Floyd's recommendations:
121 * http://www.icir.org/floyd/REDparameters.txt
124 opt
.qth_max
= opt
.qth_min
? opt
.qth_min
* 3 : opt
.limit
/ 4;
126 opt
.qth_min
= opt
.qth_max
/ 3;
128 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
130 get_rate(&rate
, "10Mbit");
131 fprintf(stderr
, "RED: set bandwidth to 10Mbit\n");
133 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
134 fprintf(stderr
, "RED: failed to calculate EWMA constant.\n");
138 fprintf(stderr
, "RED: WARNING. Burst %u seems to be too large.\n", burst
);
140 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
141 fprintf(stderr
, "RED: failed to calculate probability.\n");
145 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0) {
146 fprintf(stderr
, "RED: failed to calculate idle damping table.\n");
149 opt
.Scell_log
= parm
;
151 tail
= NLMSG_TAIL(n
);
152 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
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 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) 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 fprintf(f
, "limit %s min %s max %s ",
187 sprint_size(qopt
->limit
, b1
),
188 sprint_size(qopt
->qth_min
, b2
),
189 sprint_size(qopt
->qth_max
, b3
));
190 if (qopt
->flags
& TC_RED_ECN
)
192 if (qopt
->flags
& TC_RED_HARDDROP
)
193 fprintf(f
, "harddrop ");
194 if (qopt
->flags
& TC_RED_ADAPTATIVE
)
195 fprintf(f
, "adaptive ");
197 fprintf(f
, "ewma %u ", qopt
->Wlog
);
199 fprintf(f
, "probability %lg ", max_P
/ pow(2, 32));
201 fprintf(f
, "Plog %u ", qopt
->Plog
);
202 fprintf(f
, "Scell_log %u", qopt
->Scell_log
);
207 static int red_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
210 struct tc_red_xstats
*st
;
215 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
218 st
= RTA_DATA(xstats
);
219 fprintf(f
, " marked %u early %u pdrop %u other %u",
220 st
->marked
, st
->early
, st
->pdrop
, st
->other
);
228 struct qdisc_util red_qdisc_util
= {
230 .parse_qopt
= red_parse_opt
,
231 .print_qopt
= red_print_opt
,
232 .print_xstats
= red_print_xstats
,