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 ]\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 };
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 "
66 "currently supported\n", MAX_DPs
);
69 } else if (strcmp(*argv
, "default") == 0) {
71 fprintf(stderr
, "\"default\" must be defined "
76 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
77 fprintf(stderr
, "Illegal \"default\"\n");
79 } else if (opt
.def_DP
>= opt
.DPs
) {
80 fprintf(stderr
, "\"default\" must be less than "
84 } else if (strcmp(*argv
, "grio") == 0) {
86 } else if (strcmp(*argv
, "help") == 0) {
90 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
97 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
98 fprintf(stderr
, "Illegal gred setup parameters \n");
102 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n",opt
.DPs
,opt
.def_DP
);
103 n
->nlmsg_flags
|=NLM_F_CREATE
;
104 tail
= NLMSG_TAIL(n
);
105 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
106 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
107 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
111 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
116 struct tc_gred_qopt opt
= { 0 };
119 double probability
= 0.02;
129 if (strcmp(*argv
, "limit") == 0) {
131 if (get_size(&opt
.limit
, *argv
)) {
132 fprintf(stderr
, "Illegal \"limit\"\n");
136 } else if (strcmp(*argv
, "setup") == 0) {
138 fprintf(stderr
, "Illegal \"setup\"\n");
141 return init_gred(qu
, argc
-1, argv
+1, n
);
142 } else if (strcmp(*argv
, "min") == 0) {
144 if (get_size(&opt
.qth_min
, *argv
)) {
145 fprintf(stderr
, "Illegal \"min\"\n");
149 } else if (strcmp(*argv
, "max") == 0) {
151 if (get_size(&opt
.qth_max
, *argv
)) {
152 fprintf(stderr
, "Illegal \"max\"\n");
156 } else if (strcmp(*argv
, "vq") == 0 ||
157 strcmp(*argv
, "DP") == 0) {
159 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
160 fprintf(stderr
, "Illegal \"vq\"\n");
162 } else if (opt
.DP
>= MAX_DPs
) {
163 fprintf(stderr
, "GRED: only %u VQs are "
164 "currently supported\n", MAX_DPs
);
166 } /* need a better error check */
168 } else if (strcmp(*argv
, "burst") == 0) {
170 if (get_unsigned(&burst
, *argv
, 0)) {
171 fprintf(stderr
, "Illegal \"burst\"\n");
175 } else if (strcmp(*argv
, "avpkt") == 0) {
177 if (get_size(&avpkt
, *argv
)) {
178 fprintf(stderr
, "Illegal \"avpkt\"\n");
182 } else if (strcmp(*argv
, "probability") == 0) {
184 if (sscanf(*argv
, "%lg", &probability
) != 1) {
185 fprintf(stderr
, "Illegal \"probability\"\n");
189 } else if (strcmp(*argv
, "prio") == 0) {
191 opt
.prio
=strtol(*argv
, (char **)NULL
, 10);
192 /* some error check here */
194 } else if (strcmp(*argv
, "bandwidth") == 0) {
196 if (get_rate(&rate
, *argv
)) {
197 fprintf(stderr
, "Illegal \"bandwidth\"\n");
201 } else if (strcmp(*argv
, "help") == 0) {
205 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
216 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
218 fprintf(stderr
, "Required parameter (vq, limit, min, max, "
219 "avpkt) is missing\n");
223 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
224 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
227 get_rate(&rate
, "10Mbit");
228 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
230 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
231 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
235 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too "
238 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
239 fprintf(stderr
, "GRED: failed to calculate probability.\n");
243 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
245 fprintf(stderr
, "GRED: failed to calculate idle damping "
249 opt
.Scell_log
= parm
;
251 tail
= NLMSG_TAIL(n
);
252 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
253 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
254 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
255 max_P
= probability
* pow(2, 32);
256 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
257 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
261 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
263 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
264 struct tc_gred_sopt
*sopt
;
265 struct tc_gred_qopt
*qopt
;
275 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
277 if (tb
[TCA_GRED_PARMS
] == NULL
)
280 if (tb
[TCA_GRED_MAX_P
] &&
281 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
282 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
284 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
285 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
286 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
287 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
288 fprintf(f
,"\n GRED received message smaller than expected\n");
292 /* Bad hack! should really return a proper message as shown above*/
294 fprintf(f
, "vqs %u default %u %s",
297 sopt
->grio
? "grio " : "");
299 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
300 if (qopt
->DP
>= MAX_DPs
) continue;
301 fprintf(f
, "\n vq %u prio %hhu limit %s min %s max %s ",
304 sprint_size(qopt
->limit
, b1
),
305 sprint_size(qopt
->qth_min
, b2
),
306 sprint_size(qopt
->qth_max
, b3
));
308 fprintf(f
, "ewma %u ", qopt
->Wlog
);
310 fprintf(f
, "probability %lg ", max_p
[i
] / pow(2, 32));
312 fprintf(f
, "Plog %u ", qopt
->Plog
);
313 fprintf(f
, "Scell_log %u ", qopt
->Scell_log
);
316 fprintf(f
, "\n Queue size: average %s current %s ",
317 sprint_size(qopt
->qave
, b1
),
318 sprint_size(qopt
->backlog
, b2
));
319 fprintf(f
, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
324 fprintf(f
, "\n Total packets: %u (%s) ",
326 sprint_size(qopt
->bytesin
, b1
));
332 struct qdisc_util gred_qdisc_util
= {
334 .parse_qopt
= gred_parse_opt
,
335 .print_qopt
= gred_print_opt
,