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>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
33 #define DPRINTF(format,args...) fprintf(stderr,format,##args)
35 #define DPRINTF(format,args...)
38 static void explain(void)
40 fprintf(stderr
, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
41 fprintf(stderr
, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
42 fprintf(stderr
, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
43 fprintf(stderr
, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
44 fprintf(stderr
, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
47 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
,
52 struct tc_gred_sopt opt
= { 0 };
58 DPRINTF(stderr
,"init_gred: invoked with %s\n",*argv
);
59 if (strcmp(*argv
, "vqs") == 0 ||
60 strcmp(*argv
, "DPs") == 0) {
62 if (get_unsigned(&opt
.DPs
, *argv
, 10)) {
63 fprintf(stderr
, "Illegal \"vqs\"\n");
65 } else if (opt
.DPs
> MAX_DPs
) {
66 fprintf(stderr
, "GRED: only %u VQs are "
67 "currently supported\n", MAX_DPs
);
70 } else if (strcmp(*argv
, "default") == 0) {
72 fprintf(stderr
, "\"default\" must be defined "
77 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
78 fprintf(stderr
, "Illegal \"default\"\n");
80 } else if (opt
.def_DP
>= opt
.DPs
) {
81 fprintf(stderr
, "\"default\" must be less than "
85 } else if (strcmp(*argv
, "grio") == 0) {
87 } else if (strcmp(*argv
, "limit") == 0) {
89 if (get_size(&limit
, *argv
)) {
90 fprintf(stderr
, "Illegal \"limit\"\n");
93 } else if (strcmp(*argv
, "help") == 0) {
97 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
104 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
105 fprintf(stderr
, "Illegal gred setup parameters \n");
109 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n",opt
.DPs
,opt
.def_DP
);
110 n
->nlmsg_flags
|=NLM_F_CREATE
;
111 tail
= NLMSG_TAIL(n
);
112 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
113 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
115 addattr32(n
, 1024, TCA_GRED_LIMIT
, limit
);
116 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
120 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
125 struct tc_gred_qopt opt
= { 0 };
128 double probability
= 0.02;
138 if (strcmp(*argv
, "limit") == 0) {
140 if (get_size(&opt
.limit
, *argv
)) {
141 fprintf(stderr
, "Illegal \"limit\"\n");
145 } else if (strcmp(*argv
, "setup") == 0) {
147 fprintf(stderr
, "Illegal \"setup\"\n");
150 return init_gred(qu
, argc
-1, argv
+1, n
);
151 } else if (strcmp(*argv
, "min") == 0) {
153 if (get_size(&opt
.qth_min
, *argv
)) {
154 fprintf(stderr
, "Illegal \"min\"\n");
158 } else if (strcmp(*argv
, "max") == 0) {
160 if (get_size(&opt
.qth_max
, *argv
)) {
161 fprintf(stderr
, "Illegal \"max\"\n");
165 } else if (strcmp(*argv
, "vq") == 0 ||
166 strcmp(*argv
, "DP") == 0) {
168 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
169 fprintf(stderr
, "Illegal \"vq\"\n");
171 } else if (opt
.DP
>= MAX_DPs
) {
172 fprintf(stderr
, "GRED: only %u VQs are "
173 "currently supported\n", MAX_DPs
);
175 } /* need a better error check */
177 } else if (strcmp(*argv
, "burst") == 0) {
179 if (get_unsigned(&burst
, *argv
, 0)) {
180 fprintf(stderr
, "Illegal \"burst\"\n");
184 } else if (strcmp(*argv
, "avpkt") == 0) {
186 if (get_size(&avpkt
, *argv
)) {
187 fprintf(stderr
, "Illegal \"avpkt\"\n");
191 } else if (strcmp(*argv
, "probability") == 0) {
193 if (sscanf(*argv
, "%lg", &probability
) != 1) {
194 fprintf(stderr
, "Illegal \"probability\"\n");
198 } else if (strcmp(*argv
, "prio") == 0) {
200 opt
.prio
=strtol(*argv
, (char **)NULL
, 10);
201 /* some error check here */
203 } else if (strcmp(*argv
, "bandwidth") == 0) {
205 if (get_rate(&rate
, *argv
)) {
206 fprintf(stderr
, "Illegal \"bandwidth\"\n");
210 } else if (strcmp(*argv
, "help") == 0) {
214 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
225 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
227 fprintf(stderr
, "Required parameter (vq, limit, min, max, "
228 "avpkt) is missing\n");
232 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
233 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
236 get_rate(&rate
, "10Mbit");
237 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
239 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
240 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
244 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too "
247 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
248 fprintf(stderr
, "GRED: failed to calculate probability.\n");
252 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
254 fprintf(stderr
, "GRED: failed to calculate idle damping "
258 opt
.Scell_log
= parm
;
260 tail
= NLMSG_TAIL(n
);
261 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
262 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
263 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
264 max_P
= probability
* pow(2, 32);
265 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
266 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
270 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
272 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
273 struct tc_gred_sopt
*sopt
;
274 struct tc_gred_qopt
*qopt
;
285 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
287 if (tb
[TCA_GRED_PARMS
] == NULL
)
290 if (tb
[TCA_GRED_MAX_P
] &&
291 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
292 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
294 if (tb
[TCA_GRED_LIMIT
] &&
295 RTA_PAYLOAD(tb
[TCA_GRED_LIMIT
]) == sizeof(__u32
))
296 limit
= RTA_DATA(tb
[TCA_GRED_LIMIT
]);
298 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
299 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
300 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
301 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
302 fprintf(f
,"\n GRED received message smaller than expected\n");
306 /* Bad hack! should really return a proper message as shown above*/
308 fprintf(f
, "vqs %u default %u %s",
311 sopt
->grio
? "grio " : "");
314 fprintf(f
, "limit %s ",
315 sprint_size(*limit
, b1
));
317 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
318 if (qopt
->DP
>= MAX_DPs
) continue;
319 fprintf(f
, "\n vq %u prio %hhu limit %s min %s max %s ",
322 sprint_size(qopt
->limit
, b1
),
323 sprint_size(qopt
->qth_min
, b2
),
324 sprint_size(qopt
->qth_max
, b3
));
326 fprintf(f
, "ewma %u ", qopt
->Wlog
);
328 fprintf(f
, "probability %lg ", max_p
[i
] / pow(2, 32));
330 fprintf(f
, "Plog %u ", qopt
->Plog
);
331 fprintf(f
, "Scell_log %u ", qopt
->Scell_log
);
334 fprintf(f
, "\n Queue size: average %s current %s ",
335 sprint_size(qopt
->qave
, b1
),
336 sprint_size(qopt
->backlog
, b2
));
337 fprintf(f
, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
342 fprintf(f
, "\n Total packets: %u (%s) ",
344 sprint_size(qopt
->bytesin
, b1
));
350 struct qdisc_util gred_qdisc_util
= {
352 .parse_qopt
= gred_parse_opt
,
353 .print_qopt
= gred_print_opt
,