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: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
32 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
34 #define DPRINTF(format, args...)
37 static void explain(void)
39 fprintf(stderr
, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
40 fprintf(stderr
, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
41 fprintf(stderr
, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
42 fprintf(stderr
, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
43 fprintf(stderr
, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
46 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
,
51 struct tc_gred_sopt opt
= { 0 };
57 DPRINTF(stderr
, "init_gred: invoked with %s\n", *argv
);
58 if (strcmp(*argv
, "vqs") == 0 ||
59 strcmp(*argv
, "DPs") == 0) {
61 if (get_unsigned(&opt
.DPs
, *argv
, 10)) {
62 fprintf(stderr
, "Illegal \"vqs\"\n");
64 } else if (opt
.DPs
> MAX_DPs
) {
65 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
69 } else if (strcmp(*argv
, "default") == 0) {
71 fprintf(stderr
, "\"default\" must be defined after \"vqs\"\n");
75 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
76 fprintf(stderr
, "Illegal \"default\"\n");
78 } else if (opt
.def_DP
>= opt
.DPs
) {
79 fprintf(stderr
, "\"default\" must be less than \"vqs\"\n");
82 } else if (strcmp(*argv
, "grio") == 0) {
84 } else if (strcmp(*argv
, "limit") == 0) {
86 if (get_size(&limit
, *argv
)) {
87 fprintf(stderr
, "Illegal \"limit\"\n");
90 } else if (strcmp(*argv
, "help") == 0) {
94 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
101 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
102 fprintf(stderr
, "Illegal gred setup parameters\n");
106 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt
.DPs
, opt
.def_DP
);
107 n
->nlmsg_flags
|= NLM_F_CREATE
;
108 tail
= NLMSG_TAIL(n
);
109 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
110 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
112 addattr32(n
, 1024, TCA_GRED_LIMIT
, limit
);
113 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
117 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
122 struct tc_gred_qopt opt
= { 0 };
123 unsigned int burst
= 0;
124 unsigned int avpkt
= 0;
125 double probability
= 0.02;
126 unsigned int rate
= 0;
135 if (strcmp(*argv
, "limit") == 0) {
137 if (get_size(&opt
.limit
, *argv
)) {
138 fprintf(stderr
, "Illegal \"limit\"\n");
142 } else if (strcmp(*argv
, "setup") == 0) {
144 fprintf(stderr
, "Illegal \"setup\"\n");
147 return init_gred(qu
, argc
-1, argv
+1, n
);
148 } else if (strcmp(*argv
, "min") == 0) {
150 if (get_size(&opt
.qth_min
, *argv
)) {
151 fprintf(stderr
, "Illegal \"min\"\n");
155 } else if (strcmp(*argv
, "max") == 0) {
157 if (get_size(&opt
.qth_max
, *argv
)) {
158 fprintf(stderr
, "Illegal \"max\"\n");
162 } else if (strcmp(*argv
, "vq") == 0 ||
163 strcmp(*argv
, "DP") == 0) {
165 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
166 fprintf(stderr
, "Illegal \"vq\"\n");
168 } else if (opt
.DP
>= MAX_DPs
) {
169 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
172 } /* need a better error check */
174 } else if (strcmp(*argv
, "burst") == 0) {
176 if (get_unsigned(&burst
, *argv
, 0)) {
177 fprintf(stderr
, "Illegal \"burst\"\n");
181 } else if (strcmp(*argv
, "avpkt") == 0) {
183 if (get_size(&avpkt
, *argv
)) {
184 fprintf(stderr
, "Illegal \"avpkt\"\n");
188 } else if (strcmp(*argv
, "probability") == 0) {
190 if (sscanf(*argv
, "%lg", &probability
) != 1) {
191 fprintf(stderr
, "Illegal \"probability\"\n");
195 } else if (strcmp(*argv
, "prio") == 0) {
197 opt
.prio
= strtol(*argv
, (char **)NULL
, 10);
198 /* some error check here */
200 } else if (strcmp(*argv
, "bandwidth") == 0) {
202 if (get_rate(&rate
, *argv
)) {
203 fprintf(stderr
, "Illegal \"bandwidth\"\n");
207 } else if (strcmp(*argv
, "help") == 0) {
211 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
222 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
224 fprintf(stderr
, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
228 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
229 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
232 get_rate(&rate
, "10Mbit");
233 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
235 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
236 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
240 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too large.\n",
243 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
244 fprintf(stderr
, "GRED: failed to calculate probability.\n");
248 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
250 fprintf(stderr
, "GRED: failed to calculate idle damping table.\n");
253 opt
.Scell_log
= parm
;
255 tail
= NLMSG_TAIL(n
);
256 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
257 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
258 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
259 max_P
= probability
* pow(2, 32);
260 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
261 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
265 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
267 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
268 struct tc_gred_sopt
*sopt
;
269 struct tc_gred_qopt
*qopt
;
281 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
283 if (tb
[TCA_GRED_PARMS
] == NULL
)
286 if (tb
[TCA_GRED_MAX_P
] &&
287 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
288 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
290 if (tb
[TCA_GRED_LIMIT
] &&
291 RTA_PAYLOAD(tb
[TCA_GRED_LIMIT
]) == sizeof(__u32
))
292 limit
= RTA_DATA(tb
[TCA_GRED_LIMIT
]);
294 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
295 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
296 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
297 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
298 fprintf(f
, "\n GRED received message smaller than expected\n");
302 /* Bad hack! should really return a proper message as shown above*/
304 fprintf(f
, "vqs %u default %u %s",
307 sopt
->grio
? "grio " : "");
310 fprintf(f
, "limit %s ",
311 sprint_size(*limit
, b1
));
313 for (i
= 0; i
< MAX_DPs
; i
++, qopt
++) {
314 if (qopt
->DP
>= MAX_DPs
) continue;
315 fprintf(f
, "\n vq %u prio %hhu limit %s min %s max %s ",
318 sprint_size(qopt
->limit
, b1
),
319 sprint_size(qopt
->qth_min
, b2
),
320 sprint_size(qopt
->qth_max
, b3
));
322 fprintf(f
, "ewma %u ", qopt
->Wlog
);
324 fprintf(f
, "probability %lg ", max_p
[i
] / pow(2, 32));
326 fprintf(f
, "Plog %u ", qopt
->Plog
);
327 fprintf(f
, "Scell_log %u ", qopt
->Scell_log
);
330 fprintf(f
, "\n Queue size: average %s current %s ",
331 sprint_size(qopt
->qave
, b1
),
332 sprint_size(qopt
->backlog
, b2
));
333 fprintf(f
, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
338 fprintf(f
, "\n Total packets: %u (%s) ",
340 sprint_size(qopt
->bytesin
, b1
));
346 struct qdisc_util gred_qdisc_util
= {
348 .parse_qopt
= gred_parse_opt
,
349 .print_qopt
= gred_print_opt
,