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: Stephen Hemminger <shemminger@vyatta.com>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
27 static void explain(void)
29 fprintf(stderr
, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n");
30 fprintf(stderr
, " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
33 static int choke_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
34 struct nlmsghdr
*n
, const char *dev
)
36 struct tc_red_qopt opt
= {};
37 unsigned int burst
= 0;
38 unsigned int avpkt
= 1000;
39 double probability
= 0.02;
40 unsigned int rate
= 0;
48 if (strcmp(*argv
, "limit") == 0) {
50 if (get_unsigned(&opt
.limit
, *argv
, 0)) {
51 fprintf(stderr
, "Illegal \"limit\"\n");
54 } else if (strcmp(*argv
, "bandwidth") == 0) {
56 if (strchr(*argv
, '%')) {
57 if (get_percent_rate(&rate
, *argv
, dev
)) {
58 fprintf(stderr
, "Illegal \"bandwidth\"\n");
61 } else if (get_rate(&rate
, *argv
)) {
62 fprintf(stderr
, "Illegal \"bandwidth\"\n");
65 } else if (strcmp(*argv
, "ecn") == 0) {
67 } else if (strcmp(*argv
, "min") == 0) {
69 if (get_unsigned(&opt
.qth_min
, *argv
, 0)) {
70 fprintf(stderr
, "Illegal \"min\"\n");
73 } else if (strcmp(*argv
, "max") == 0) {
75 if (get_unsigned(&opt
.qth_max
, *argv
, 0)) {
76 fprintf(stderr
, "Illegal \"max\"\n");
79 } else if (strcmp(*argv
, "burst") == 0) {
81 if (get_unsigned(&burst
, *argv
, 0)) {
82 fprintf(stderr
, "Illegal \"burst\"\n");
85 } else if (strcmp(*argv
, "avpkt") == 0) {
87 if (get_size(&avpkt
, *argv
)) {
88 fprintf(stderr
, "Illegal \"avpkt\"\n");
91 } else if (strcmp(*argv
, "probability") == 0) {
93 if (sscanf(*argv
, "%lg", &probability
) != 1) {
94 fprintf(stderr
, "Illegal \"probability\"\n");
97 } else if (strcmp(*argv
, "help") == 0) {
101 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
108 if (!rate
|| !opt
.limit
) {
109 fprintf(stderr
, "Required parameter (bandwidth, limit) is missing\n");
113 /* Compute default min/max thresholds based on
114 Sally Floyd's recommendations:
115 http://www.icir.org/floyd/REDparameters.txt
118 opt
.qth_max
= opt
.limit
/ 4;
120 opt
.qth_min
= opt
.qth_max
/ 3;
122 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / 3;
124 if (opt
.qth_max
> opt
.limit
) {
125 fprintf(stderr
, "\"max\" is larger than \"limit\"\n");
129 if (opt
.qth_min
>= opt
.qth_max
) {
130 fprintf(stderr
, "\"min\" is not smaller than \"max\"\n");
134 wlog
= tc_red_eval_ewma(opt
.qth_min
*avpkt
, burst
, avpkt
);
136 fprintf(stderr
, "CHOKE: failed to calculate EWMA constant.\n");
140 fprintf(stderr
, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst
);
143 wlog
= tc_red_eval_P(opt
.qth_min
*avpkt
, opt
.qth_max
*avpkt
, probability
);
145 fprintf(stderr
, "CHOKE: failed to calculate probability.\n");
150 wlog
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
);
152 fprintf(stderr
, "CHOKE: failed to calculate idle damping table.\n");
155 opt
.Scell_log
= wlog
;
157 opt
.flags
|= TC_RED_ECN
;
159 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
160 addattr_l(n
, 1024, TCA_CHOKE_PARMS
, &opt
, sizeof(opt
));
161 addattr_l(n
, 1024, TCA_CHOKE_STAB
, sbuf
, 256);
162 max_P
= probability
* pow(2, 32);
163 addattr_l(n
, 1024, TCA_CHOKE_MAX_P
, &max_P
, sizeof(max_P
));
164 addattr_nest_end(n
, tail
);
168 static int choke_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
170 struct rtattr
*tb
[TCA_CHOKE_MAX
+1];
171 const struct tc_red_qopt
*qopt
;
177 parse_rtattr_nested(tb
, TCA_CHOKE_MAX
, opt
);
179 if (tb
[TCA_CHOKE_PARMS
] == NULL
)
181 qopt
= RTA_DATA(tb
[TCA_CHOKE_PARMS
]);
182 if (RTA_PAYLOAD(tb
[TCA_CHOKE_PARMS
]) < sizeof(*qopt
))
184 if (tb
[TCA_CHOKE_MAX_P
] &&
185 RTA_PAYLOAD(tb
[TCA_CHOKE_MAX_P
]) >= sizeof(__u32
))
186 max_P
= rta_getattr_u32(tb
[TCA_CHOKE_MAX_P
]);
188 fprintf(f
, "limit %up min %up max %up ",
189 qopt
->limit
, qopt
->qth_min
, qopt
->qth_max
);
191 if (qopt
->flags
& TC_RED_ECN
)
195 fprintf(f
, "ewma %u ", qopt
->Wlog
);
197 fprintf(f
, "probability %g ", max_P
/ pow(2, 32));
199 fprintf(f
, "Plog %u ", qopt
->Plog
);
200 fprintf(f
, "Scell_log %u", qopt
->Scell_log
);
205 static int choke_print_xstats(struct qdisc_util
*qu
, FILE *f
,
206 struct rtattr
*xstats
)
208 struct tc_choke_xstats
*st
;
213 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
216 st
= RTA_DATA(xstats
);
217 fprintf(f
, " marked %u early %u pdrop %u other %u matched %u",
218 st
->marked
, st
->early
, st
->pdrop
, st
->other
, st
->matched
);
223 struct qdisc_util choke_qdisc_util
= {
225 .parse_qopt
= choke_parse_opt
,
226 .print_qopt
= choke_print_opt
,
227 .print_xstats
= choke_print_xstats
,