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: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
31 fprintf(stderr
, " [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n");
32 fprintf(stderr
, " [ headdrop ]\n");
33 fprintf(stderr
, " [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n");
34 fprintf(stderr
, " [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n");
35 fprintf(stderr
, " [ ecn ] [ harddrop ]\n");
38 static int sfq_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
41 struct tc_sfq_qopt_v1 opt
;
42 unsigned int burst
= 0;
44 unsigned int avpkt
= 1000;
45 double probability
= 0.02;
47 memset(&opt
, 0, sizeof(opt
));
50 if (strcmp(*argv
, "quantum") == 0) {
52 if (get_size(&opt
.v0
.quantum
, *argv
)) {
53 fprintf(stderr
, "Illegal \"limit\"\n");
57 } else if (strcmp(*argv
, "perturb") == 0) {
59 if (get_integer(&opt
.v0
.perturb_period
, *argv
, 0)) {
60 fprintf(stderr
, "Illegal \"perturb\"\n");
64 } else if (strcmp(*argv
, "limit") == 0) {
66 if (get_u32(&opt
.v0
.limit
, *argv
, 0)) {
67 fprintf(stderr
, "Illegal \"limit\"\n");
70 if (opt
.v0
.limit
< 2) {
71 fprintf(stderr
, "Illegal \"limit\", must be > 1\n");
75 } else if (strcmp(*argv
, "divisor") == 0) {
77 if (get_u32(&opt
.v0
.divisor
, *argv
, 0)) {
78 fprintf(stderr
, "Illegal \"divisor\"\n");
82 } else if (strcmp(*argv
, "flows") == 0) {
84 if (get_u32(&opt
.v0
.flows
, *argv
, 0)) {
85 fprintf(stderr
, "Illegal \"flows\"\n");
89 } else if (strcmp(*argv
, "depth") == 0) {
91 if (get_u32(&opt
.depth
, *argv
, 0)) {
92 fprintf(stderr
, "Illegal \"flows\"\n");
96 } else if (strcmp(*argv
, "headdrop") == 0) {
99 } else if (strcmp(*argv
, "redflowlimit") == 0) {
101 if (get_u32(&opt
.limit
, *argv
, 0)) {
102 fprintf(stderr
, "Illegal \"redflowlimit\"\n");
106 } else if (strcmp(*argv
, "min") == 0) {
108 if (get_u32(&opt
.qth_min
, *argv
, 0)) {
109 fprintf(stderr
, "Illegal \"min\"\n");
113 } else if (strcmp(*argv
, "max") == 0) {
115 if (get_u32(&opt
.qth_max
, *argv
, 0)) {
116 fprintf(stderr
, "Illegal \"max\"\n");
120 } else if (strcmp(*argv
, "burst") == 0) {
122 if (get_unsigned(&burst
, *argv
, 0)) {
123 fprintf(stderr
, "Illegal \"burst\"\n");
127 } else if (strcmp(*argv
, "avpkt") == 0) {
129 if (get_size(&avpkt
, *argv
)) {
130 fprintf(stderr
, "Illegal \"avpkt\"\n");
134 } else if (strcmp(*argv
, "probability") == 0) {
136 if (sscanf(*argv
, "%lg", &probability
) != 1) {
137 fprintf(stderr
, "Illegal \"probability\"\n");
141 } else if (strcmp(*argv
, "ecn") == 0) {
142 opt
.flags
|= TC_RED_ECN
;
144 } else if (strcmp(*argv
, "harddrop") == 0) {
145 opt
.flags
|= TC_RED_HARDDROP
;
147 } else if (strcmp(*argv
, "help") == 0) {
151 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
159 fprintf(stderr
, "Required parameter (redflowlimit) is missing\n");
162 /* Compute default min/max thresholds based on
163 Sally Floyd's recommendations:
164 http://www.icir.org/floyd/REDparameters.txt
167 opt
.qth_max
= opt
.limit
/ 4;
169 opt
.qth_min
= opt
.qth_max
/ 3;
171 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
173 if (opt
.qth_max
> opt
.limit
) {
174 fprintf(stderr
, "\"max\" is larger than \"limit\"\n");
178 if (opt
.qth_min
>= opt
.qth_max
) {
179 fprintf(stderr
, "\"min\" is not smaller than \"max\"\n");
183 wlog
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
);
185 fprintf(stderr
, "SFQ: failed to calculate EWMA constant.\n");
189 fprintf(stderr
, "SFQ: WARNING. Burst %u seems to be too large.\n", burst
);
192 wlog
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
);
194 fprintf(stderr
, "SFQ: failed to calculate probability.\n");
198 opt
.max_P
= probability
* pow(2, 32);
202 addattr_l(n
, 1024, TCA_OPTIONS
, &opt
, sizeof(opt
));
206 static int sfq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
208 struct tc_sfq_qopt
*qopt
;
209 struct tc_sfq_qopt_v1
*qopt_ext
= NULL
;
216 if (RTA_PAYLOAD(opt
) < sizeof(*qopt
))
218 if (RTA_PAYLOAD(opt
) >= sizeof(*qopt_ext
))
219 qopt_ext
= RTA_DATA(opt
);
220 qopt
= RTA_DATA(opt
);
221 fprintf(f
, "limit %up ", qopt
->limit
);
222 fprintf(f
, "quantum %s ", sprint_size(qopt
->quantum
, b1
));
223 if (qopt_ext
&& qopt_ext
->depth
)
224 fprintf(f
, "depth %u ", qopt_ext
->depth
);
225 if (qopt_ext
&& qopt_ext
->headdrop
)
226 fprintf(f
, "headdrop ");
229 fprintf(f
, "flows %u/%u ", qopt
->flows
, qopt
->divisor
);
231 fprintf(f
, "divisor %u ", qopt
->divisor
);
232 if (qopt
->perturb_period
)
233 fprintf(f
, "perturb %dsec ", qopt
->perturb_period
);
234 if (qopt_ext
&& qopt_ext
->qth_min
) {
235 fprintf(f
, "\n ewma %u ", qopt_ext
->Wlog
);
236 fprintf(f
, "min %s max %s probability %g ",
237 sprint_size(qopt_ext
->qth_min
, b2
),
238 sprint_size(qopt_ext
->qth_max
, b3
),
239 qopt_ext
->max_P
/ pow(2, 32));
240 if (qopt_ext
->flags
& TC_RED_ECN
)
243 fprintf(f
, "\n prob_mark %u prob_mark_head %u prob_drop %u",
244 qopt_ext
->stats
.prob_mark
,
245 qopt_ext
->stats
.prob_mark_head
,
246 qopt_ext
->stats
.prob_drop
);
247 fprintf(f
, "\n forced_mark %u forced_mark_head %u forced_drop %u",
248 qopt_ext
->stats
.forced_mark
,
249 qopt_ext
->stats
.forced_mark_head
,
250 qopt_ext
->stats
.forced_drop
);
256 static int sfq_print_xstats(struct qdisc_util
*qu
, FILE *f
,
257 struct rtattr
*xstats
)
259 struct tc_sfq_xstats
*st
;
263 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
265 st
= RTA_DATA(xstats
);
267 fprintf(f
, " allot %d ", st
->allot
);
272 struct qdisc_util sfq_qdisc_util
= {
274 .parse_qopt
= sfq_parse_opt
,
275 .print_qopt
= sfq_print_opt
,
276 .print_xstats
= sfq_print_xstats
,