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>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
28 static void explain(void)
30 fprintf(stderr
, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n");
31 fprintf(stderr
, " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
34 static int choke_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
37 struct tc_red_qopt opt
;
39 unsigned avpkt
= 1000;
40 double probability
= 0.02;
48 memset(&opt
, 0, sizeof(opt
));
51 if (strcmp(*argv
, "limit") == 0) {
53 if (get_unsigned(&opt
.limit
, *argv
, 0)) {
54 fprintf(stderr
, "Illegal \"limit\"\n");
57 } else if (strcmp(*argv
, "bandwidth") == 0) {
59 if (get_rate(&rate
, *argv
)) {
60 fprintf(stderr
, "Illegal \"bandwidth\"\n");
63 } else if (strcmp(*argv
, "ecn") == 0) {
65 } else if (strcmp(*argv
, "min") == 0) {
67 if (get_unsigned(&opt
.qth_min
, *argv
, 0)) {
68 fprintf(stderr
, "Illegal \"min\"\n");
71 } else if (strcmp(*argv
, "max") == 0) {
73 if (get_unsigned(&opt
.qth_max
, *argv
, 0)) {
74 fprintf(stderr
, "Illegal \"max\"\n");
77 } else if (strcmp(*argv
, "burst") == 0) {
79 if (get_unsigned(&burst
, *argv
, 0)) {
80 fprintf(stderr
, "Illegal \"burst\"\n");
83 } else if (strcmp(*argv
, "avpkt") == 0) {
85 if (get_size(&avpkt
, *argv
)) {
86 fprintf(stderr
, "Illegal \"avpkt\"\n");
89 } else if (strcmp(*argv
, "probability") == 0) {
91 if (sscanf(*argv
, "%lg", &probability
) != 1) {
92 fprintf(stderr
, "Illegal \"probability\"\n");
95 } else if (strcmp(*argv
, "help") == 0) {
99 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
106 if (!rate
|| !opt
.limit
) {
107 fprintf(stderr
, "Required parameter (bandwidth, limit) is missing\n");
111 /* Compute default min/max thresholds based on
112 Sally Floyd's recommendations:
113 http://www.icir.org/floyd/REDparameters.txt
116 opt
.qth_max
= opt
.limit
/ 4;
118 opt
.qth_min
= opt
.qth_max
/ 3;
120 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / 3;
122 if (opt
.qth_max
> opt
.limit
) {
123 fprintf(stderr
, "\"max\" is larger than \"limit\"\n");
127 if (opt
.qth_min
>= opt
.qth_max
) {
128 fprintf(stderr
, "\"min\" is not smaller than \"max\"\n");
132 wlog
= tc_red_eval_ewma(opt
.qth_min
*avpkt
, burst
, avpkt
);
134 fprintf(stderr
, "CHOKE: failed to calculate EWMA constant.\n");
138 fprintf(stderr
, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst
);
141 wlog
= tc_red_eval_P(opt
.qth_min
*avpkt
, opt
.qth_max
*avpkt
, probability
);
143 fprintf(stderr
, "CHOKE: failed to calculate probability.\n");
148 wlog
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
);
150 fprintf(stderr
, "CHOKE: failed to calculate idle damping table.\n");
153 opt
.Scell_log
= wlog
;
155 opt
.flags
|= TC_RED_ECN
;
157 tail
= NLMSG_TAIL(n
);
158 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
159 addattr_l(n
, 1024, TCA_CHOKE_PARMS
, &opt
, sizeof(opt
));
160 addattr_l(n
, 1024, TCA_CHOKE_STAB
, sbuf
, 256);
161 max_P
= probability
* pow(2, 32);
162 addattr_l(n
, 1024, TCA_CHOKE_MAX_P
, &max_P
, sizeof(max_P
));
163 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
167 static int choke_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
169 struct rtattr
*tb
[TCA_CHOKE_MAX
+1];
170 const struct tc_red_qopt
*qopt
;
176 parse_rtattr_nested(tb
, TCA_CHOKE_MAX
, opt
);
178 if (tb
[TCA_CHOKE_PARMS
] == NULL
)
180 qopt
= RTA_DATA(tb
[TCA_CHOKE_PARMS
]);
181 if (RTA_PAYLOAD(tb
[TCA_CHOKE_PARMS
]) < sizeof(*qopt
))
183 if (tb
[TCA_CHOKE_MAX_P
] &&
184 RTA_PAYLOAD(tb
[TCA_CHOKE_MAX_P
]) >= sizeof(__u32
))
185 max_P
= rta_getattr_u32(tb
[TCA_CHOKE_MAX_P
]);
187 fprintf(f
, "limit %up min %up max %up ",
188 qopt
->limit
, qopt
->qth_min
, qopt
->qth_max
);
190 if (qopt
->flags
& TC_RED_ECN
)
194 fprintf(f
, "ewma %u ", qopt
->Wlog
);
196 fprintf(f
, "probability %g ", max_P
/ pow(2, 32));
198 fprintf(f
, "Plog %u ", qopt
->Plog
);
199 fprintf(f
, "Scell_log %u", qopt
->Scell_log
);
204 static int choke_print_xstats(struct qdisc_util
*qu
, FILE *f
,
205 struct rtattr
*xstats
)
207 struct tc_choke_xstats
*st
;
212 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
215 st
= RTA_DATA(xstats
);
216 fprintf(f
, " marked %u early %u pdrop %u other %u matched %u",
217 st
->marked
, st
->early
, st
->pdrop
, st
->other
, st
->matched
);
222 struct qdisc_util choke_qdisc_util
= {
224 .parse_qopt
= choke_parse_opt
,
225 .print_qopt
= choke_print_opt
,
226 .print_xstats
= choke_print_xstats
,