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>
32 #define DPRINTF(format,args...) fprintf(stderr,format,##args)
34 #define DPRINTF(format,args...)
37 static void explain(void)
39 fprintf(stderr
, "Usage: ... gred DP drop-probability limit BYTES "
40 "min BYTES max BYTES\n");
41 fprintf(stderr
, " avpkt BYTES burst PACKETS probability PROBABILITY "
43 fprintf(stderr
, " [prio value]\n");
44 fprintf(stderr
," OR ...\n");
45 fprintf(stderr
," gred setup DPs <num of DPs> default <default DP> "
49 #define usage() return(-1)
51 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
55 struct tc_gred_sopt opt
;
56 memset(&opt
, 0, sizeof(struct tc_gred_sopt
));
59 DPRINTF(stderr
,"init_gred: invoked with %s\n",*argv
);
60 if (strcmp(*argv
, "DPs") == 0) {
62 DPRINTF(stderr
,"init_gred: next_arg with %s\n",*argv
);
63 opt
.DPs
=strtol(*argv
, (char **)NULL
, 10);
64 if (opt
.DPs
>MAX_DPs
) { /* need a better error check */
65 fprintf(stderr
, "DPs =%u \n",opt
.DPs
);
66 fprintf(stderr
, "Illegal \"DPs\"\n");
67 fprintf(stderr
, "GRED: only %d DPs are "
68 "currently supported\n",MAX_DPs
);
71 } else if (strcmp(*argv
, "default") == 0) {
73 opt
.def_DP
=strtol(*argv
, (char **)NULL
, 10);
75 fprintf(stderr
, "\"default DP\" must be "
76 "defined after DPs\n");
80 if (opt
.def_DP
>opt
.DPs
-1) {
82 if (opt
.def_DP
>opt
.DPs
) {
84 fprintf(stderr, "\"default DP\" must be less than %d\nNote: DP runs from 0 to %d for %d DPs\n",opt.DPs,opt.DPs-1,opt.DPs);
86 fprintf(stderr
, "\"default DP\" must be less than %d\n",opt
.DPs
);
89 } else if (strcmp(*argv
, "grio") == 0) {
91 } else if (strcmp(*argv
, "help") == 0) {
95 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
102 if ((!opt
.DPs
) || (!opt
.def_DP
))
104 fprintf(stderr
, "Illegal gred setup parameters \n");
107 DPRINTF("TC_GRED: sending DPs=%d default=%d\n",opt
.DPs
,opt
.def_DP
);
108 n
->nlmsg_flags
|=NLM_F_CREATE
;
109 tail
= (struct rtattr
*)(((void*)n
)+NLMSG_ALIGN(n
->nlmsg_len
));
111 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
112 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
113 tail
->rta_len
= (((void*)n
)+NLMSG_ALIGN(n
->nlmsg_len
)) - (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
;
125 double probability
= 0.02;
131 memset(&opt
, 0, sizeof(opt
));
134 if (strcmp(*argv
, "limit") == 0) {
136 if (get_size(&opt
.limit
, *argv
)) {
137 fprintf(stderr
, "Illegal \"limit\"\n");
141 } else if (strcmp(*argv
, "setup") == 0) {
143 fprintf(stderr
, "Illegal \"setup\"\n");
146 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
, "DP") == 0) {
164 opt
.DP
=strtol(*argv
, (char **)NULL
, 10);
165 DPRINTF ("\n ******* DP =%u\n",opt
.DP
);
166 if (opt
.DP
>MAX_DPs
) { /* need a better error check */
167 fprintf(stderr
, "DP =%u \n",opt
.DP
);
168 fprintf(stderr
, "Illegal \"DP\"\n");
169 fprintf(stderr
, "GRED: only %d DPs are currently supported\n",MAX_DPs
);
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 get_rate(&rate
, "10Mbit");
227 if (!opt
.qth_min
|| !opt
.qth_max
|| !burst
|| !opt
.limit
|| !avpkt
||
229 fprintf(stderr
, "Required parameter (min, max, burst, limit, "
230 "avpket, DP) is missing\n");
234 if ((wlog
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
235 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
239 fprintf(stderr
, "GRED: WARNING. Burst %d seems to be to "
242 if ((wlog
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
243 fprintf(stderr
, "GRED: failed to calculate probability.\n");
247 if ((wlog
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
249 fprintf(stderr
, "GRED: failed to calculate idle damping "
253 opt
.Scell_log
= wlog
;
255 tail
= (struct rtattr
*)(((void*)n
)+NLMSG_ALIGN(n
->nlmsg_len
));
257 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
258 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
259 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
260 tail
->rta_len
= (((void*)n
)+NLMSG_ALIGN(n
->nlmsg_len
)) - (void*)tail
;
264 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
266 struct rtattr
*tb
[TCA_GRED_STAB
+1];
267 struct tc_gred_qopt
*qopt
;
278 memset(tb
, 0, sizeof(tb
));
279 parse_rtattr(tb
, TCA_GRED_STAB
, RTA_DATA(opt
), RTA_PAYLOAD(opt
));
281 if (tb
[TCA_GRED_PARMS
] == NULL
)
284 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
285 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
)) {
286 printf("\n GRED DPs message smaller than expected\n");
290 DPRINTF(f
, "\n\tDPs:%d Default DP %d\n ",
291 sopt
->DPs
, sopt
->def_DP
);
293 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
294 if (RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
295 fprintf(f
,"\n GRED received message smaller than expected\n");
302 for (i
=0;i
<sopt
->DPs
;i
++)
304 /* Bad hack! should really return a proper message as shown above*/
306 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
307 if (qopt
->DP
>= MAX_DPs
) continue;
308 fprintf(f
, "\n DP:%d (prio %d) Average Queue %s Measured "
312 sprint_size(qopt
->qave
, b4
),
313 sprint_size(qopt
->backlog
, b5
));
314 fprintf(f
, "\n\t Packet drops: %d (forced %d early %d) ",
315 qopt
->forced
+qopt
->early
,
318 fprintf(f
, "\n\t Packet totals: %u (bytes %u) ",
322 fprintf(f
, "\n limit %s min %s max %s ",
323 sprint_size(qopt
->limit
, b1
),
324 sprint_size(qopt
->qth_min
, b2
),
325 sprint_size(qopt
->qth_max
, b3
));
326 fprintf(f
, "ewma %u Plog %u Scell_log %u",
327 qopt
->Wlog
, qopt
->Plog
, qopt
->Scell_log
);
332 struct qdisc_util gred_qdisc_util
= {
334 .parse_qopt
= gred_parse_opt
,
335 .print_qopt
= gred_print_opt
,