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)
30 "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n"
31 " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
34 static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv,
35 struct nlmsghdr *n, const char *dev)
37 struct tc_red_qopt opt = {};
38 unsigned int burst = 0;
39 unsigned int avpkt = 1000;
40 double probability = 0.02;
41 unsigned int rate = 0;
49 if (strcmp(*argv, "limit") == 0) {
51 if (get_unsigned(&opt.limit, *argv, 0)) {
52 fprintf(stderr, "Illegal \"limit\"\n");
55 } else if (strcmp(*argv, "bandwidth") == 0) {
57 if (strchr(*argv, '%')) {
58 if (get_percent_rate(&rate, *argv, dev)) {
59 fprintf(stderr, "Illegal \"bandwidth\"\n");
62 } else if (get_rate(&rate, *argv)) {
63 fprintf(stderr, "Illegal \"bandwidth\"\n");
66 } else if (strcmp(*argv, "ecn") == 0) {
68 } else if (strcmp(*argv, "min") == 0) {
70 if (get_unsigned(&opt.qth_min, *argv, 0)) {
71 fprintf(stderr, "Illegal \"min\"\n");
74 } else if (strcmp(*argv, "max") == 0) {
76 if (get_unsigned(&opt.qth_max, *argv, 0)) {
77 fprintf(stderr, "Illegal \"max\"\n");
80 } else if (strcmp(*argv, "burst") == 0) {
82 if (get_unsigned(&burst, *argv, 0)) {
83 fprintf(stderr, "Illegal \"burst\"\n");
86 } else if (strcmp(*argv, "avpkt") == 0) {
88 if (get_size(&avpkt, *argv)) {
89 fprintf(stderr, "Illegal \"avpkt\"\n");
92 } else if (strcmp(*argv, "probability") == 0) {
94 if (sscanf(*argv, "%lg", &probability) != 1) {
95 fprintf(stderr, "Illegal \"probability\"\n");
98 } else if (strcmp(*argv, "help") == 0) {
102 fprintf(stderr, "What is \"%s\"?\n", *argv);
109 if (!rate || !opt.limit) {
110 fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n");
114 /* Compute default min/max thresholds based on
115 Sally Floyd's recommendations:
116 http://www.icir.org/floyd/REDparameters.txt
119 opt.qth_max = opt.limit / 4;
121 opt.qth_min = opt.qth_max / 3;
123 burst = (2 * opt.qth_min + opt.qth_max) / 3;
125 if (opt.qth_max > opt.limit) {
126 fprintf(stderr, "\"max\" is larger than \"limit\"\n");
130 if (opt.qth_min >= opt.qth_max) {
131 fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
135 wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt);
137 fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n");
141 fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst);
144 wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability);
146 fprintf(stderr, "CHOKE: failed to calculate probability.\n");
151 wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf);
153 fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n");
156 opt.Scell_log = wlog;
158 opt.flags |= TC_RED_ECN;
160 tail = addattr_nest(n, 1024, TCA_OPTIONS);
161 addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt));
162 addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256);
163 max_P = probability * pow(2, 32);
164 addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P));
165 addattr_nest_end(n, tail);
169 static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
171 struct rtattr *tb[TCA_CHOKE_MAX+1];
172 const struct tc_red_qopt *qopt;
178 parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt);
180 if (tb[TCA_CHOKE_PARMS] == NULL)
182 qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]);
183 if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS]) < sizeof(*qopt))
185 if (tb[TCA_CHOKE_MAX_P] &&
186 RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32))
187 max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]);
189 print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit);
190 print_uint(PRINT_ANY, "min", "min %up ", qopt->qth_min);
191 print_uint(PRINT_ANY, "max", "max %up ", qopt->qth_max);
193 tc_red_print_flags(qopt->flags);
196 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
199 print_float(PRINT_ANY, "probability",
200 "probability %lg ", max_P / pow(2, 32));
202 print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
204 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
210 static int choke_print_xstats(struct qdisc_util *qu, FILE *f,
211 struct rtattr *xstats)
213 struct tc_choke_xstats *st;
218 if (RTA_PAYLOAD(xstats) < sizeof(*st))
221 st = RTA_DATA(xstats);
223 print_uint(PRINT_ANY, "marked", " marked %u", st->marked);
224 print_uint(PRINT_ANY, "early", " early %u", st->early);
225 print_uint(PRINT_ANY, "pdrop", " pdrop %u", st->pdrop);
226 print_uint(PRINT_ANY, "other", " other %u", st->other);
227 print_uint(PRINT_ANY, "matched", " matched %u", st->matched);
233 struct qdisc_util choke_qdisc_util = {
235 .parse_qopt = choke_parse_opt,
236 .print_qopt = choke_print_opt,
237 .print_xstats = choke_print_xstats,