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>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #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
, struct nlmsghdr
*n
)
37 struct tc_red_qopt opt
;
40 double probability
= 0.02;
47 memset(&opt
, 0, sizeof(opt
));
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 (get_rate(&rate
, *argv
)) {
89 fprintf(stderr
, "Illegal \"bandwidth\"\n");
92 } else if (strcmp(*argv
, "ecn") == 0) {
93 opt
.flags
|= TC_RED_ECN
;
94 } else if (strcmp(*argv
, "harddrop") == 0) {
95 opt
.flags
|= TC_RED_HARDDROP
;
96 } else if (strcmp(*argv
, "adaptative") == 0) {
97 opt
.flags
|= TC_RED_ADAPTATIVE
;
98 } else if (strcmp(*argv
, "adaptive") == 0) {
99 opt
.flags
|= TC_RED_ADAPTATIVE
;
100 } else if (strcmp(*argv
, "help") == 0) {
104 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
112 get_rate(&rate
, "10Mbit");
114 if (!opt
.limit
|| !avpkt
) {
115 fprintf(stderr
, "RED: Required parameter (limit, avpkt) is missing\n");
118 /* Compute default min/max thresholds based on
119 * Sally Floyd's recommendations:
120 * http://www.icir.org/floyd/REDparameters.txt
123 opt
.qth_max
= opt
.qth_min
? opt
.qth_min
* 3 : opt
.limit
/ 4;
125 opt
.qth_min
= opt
.qth_max
/ 3;
127 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
128 if ((wlog
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
129 fprintf(stderr
, "RED: failed to calculate EWMA constant.\n");
133 fprintf(stderr
, "RED: WARNING. Burst %d seems to be too large.\n", burst
);
135 if ((wlog
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
136 fprintf(stderr
, "RED: failed to calculate probability.\n");
140 if ((wlog
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0) {
141 fprintf(stderr
, "RED: failed to calculate idle damping table.\n");
144 opt
.Scell_log
= wlog
;
146 tail
= NLMSG_TAIL(n
);
147 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
148 addattr_l(n
, 1024, TCA_RED_PARMS
, &opt
, sizeof(opt
));
149 addattr_l(n
, 1024, TCA_RED_STAB
, sbuf
, 256);
150 max_P
= probability
* pow(2, 32);
151 addattr_l(n
, 1024, TCA_RED_MAX_P
, &max_P
, sizeof(max_P
));
152 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
156 static int red_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
158 struct rtattr
*tb
[TCA_RED_MAX
+ 1];
159 struct tc_red_qopt
*qopt
;
168 parse_rtattr_nested(tb
, TCA_RED_MAX
, opt
);
170 if (tb
[TCA_RED_PARMS
] == NULL
)
172 qopt
= RTA_DATA(tb
[TCA_RED_PARMS
]);
173 if (RTA_PAYLOAD(tb
[TCA_RED_PARMS
]) < sizeof(*qopt
))
176 if (tb
[TCA_RED_MAX_P
] &&
177 RTA_PAYLOAD(tb
[TCA_RED_MAX_P
]) >= sizeof(__u32
))
178 max_P
= *(__u32
*)RTA_DATA(tb
[TCA_RED_MAX_P
]);
180 fprintf(f
, "limit %s min %s max %s ",
181 sprint_size(qopt
->limit
, b1
),
182 sprint_size(qopt
->qth_min
, b2
),
183 sprint_size(qopt
->qth_max
, b3
));
184 if (qopt
->flags
& TC_RED_ECN
)
186 if (qopt
->flags
& TC_RED_HARDDROP
)
187 fprintf(f
, "harddrop ");
188 if (qopt
->flags
& TC_RED_ADAPTATIVE
)
189 fprintf(f
, "adaptive ");
191 fprintf(f
, "ewma %u ", qopt
->Wlog
);
193 fprintf(f
, "probability %lg ", max_P
/ pow(2, 32));
195 fprintf(f
, "Plog %u ", qopt
->Plog
);
196 fprintf(f
, "Scell_log %u", qopt
->Scell_log
);
201 static int red_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
204 struct tc_red_xstats
*st
;
209 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
212 st
= RTA_DATA(xstats
);
213 fprintf(f
, " marked %u early %u pdrop %u other %u",
214 st
->marked
, st
->early
, st
->pdrop
, st
->other
);
222 struct qdisc_util red_qdisc_util
= {
224 .parse_qopt
= red_parse_opt
,
225 .print_qopt
= red_print_opt
,
226 .print_xstats
= red_print_xstats
,