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: ... gred DP drop-probability limit BYTES "
41 "min BYTES max BYTES\n");
42 fprintf(stderr
, " avpkt BYTES burst PACKETS probability PROBABILITY "
44 fprintf(stderr
, " [prio value]\n");
45 fprintf(stderr
," OR ...\n");
46 fprintf(stderr
," gred setup DPs <num of DPs> default <default DP> "
50 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
,
55 struct tc_gred_sopt opt
= { 0 };
60 DPRINTF(stderr
,"init_gred: invoked with %s\n",*argv
);
61 if (strcmp(*argv
, "DPs") == 0) {
63 if (get_unsigned(&opt
.DPs
, *argv
, 10)) {
64 fprintf(stderr
, "Illegal \"DPs\"\n");
66 } else if (opt
.DPs
> MAX_DPs
) {
67 fprintf(stderr
, "GRED: only %u DPs are "
68 "currently supported\n", MAX_DPs
);
71 } else if (strcmp(*argv
, "default") == 0) {
73 fprintf(stderr
, "\"default\" must be defined "
78 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
79 fprintf(stderr
, "Illegal \"default\"\n");
81 } else if (opt
.def_DP
>= opt
.DPs
) {
82 fprintf(stderr
, "\"default\" must be less than "
86 } else if (strcmp(*argv
, "grio") == 0) {
88 } else if (strcmp(*argv
, "help") == 0) {
92 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
99 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
100 fprintf(stderr
, "Illegal gred setup parameters \n");
104 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n",opt
.DPs
,opt
.def_DP
);
105 n
->nlmsg_flags
|=NLM_F_CREATE
;
106 tail
= NLMSG_TAIL(n
);
107 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
108 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
109 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
113 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
118 struct tc_gred_qopt opt
= { 0 };
121 double probability
= 0.02;
131 if (strcmp(*argv
, "limit") == 0) {
133 if (get_size(&opt
.limit
, *argv
)) {
134 fprintf(stderr
, "Illegal \"limit\"\n");
138 } else if (strcmp(*argv
, "setup") == 0) {
140 fprintf(stderr
, "Illegal \"setup\"\n");
143 return init_gred(qu
, argc
-1, argv
+1, n
);
144 } else if (strcmp(*argv
, "min") == 0) {
146 if (get_size(&opt
.qth_min
, *argv
)) {
147 fprintf(stderr
, "Illegal \"min\"\n");
151 } else if (strcmp(*argv
, "max") == 0) {
153 if (get_size(&opt
.qth_max
, *argv
)) {
154 fprintf(stderr
, "Illegal \"max\"\n");
158 } else if (strcmp(*argv
, "DP") == 0) {
160 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
161 fprintf(stderr
, "Illegal \"DP\"\n");
163 } else if (opt
.DP
>= MAX_DPs
) {
164 fprintf(stderr
, "GRED: only %u DPs are "
165 "currently supported\n", MAX_DPs
);
167 } /* need a better error check */
169 } else if (strcmp(*argv
, "burst") == 0) {
171 if (get_unsigned(&burst
, *argv
, 0)) {
172 fprintf(stderr
, "Illegal \"burst\"\n");
176 } else if (strcmp(*argv
, "avpkt") == 0) {
178 if (get_size(&avpkt
, *argv
)) {
179 fprintf(stderr
, "Illegal \"avpkt\"\n");
183 } else if (strcmp(*argv
, "probability") == 0) {
185 if (sscanf(*argv
, "%lg", &probability
) != 1) {
186 fprintf(stderr
, "Illegal \"probability\"\n");
190 } else if (strcmp(*argv
, "prio") == 0) {
192 opt
.prio
=strtol(*argv
, (char **)NULL
, 10);
193 /* some error check here */
195 } else if (strcmp(*argv
, "bandwidth") == 0) {
197 if (get_rate(&rate
, *argv
)) {
198 fprintf(stderr
, "Illegal \"bandwidth\"\n");
202 } else if (strcmp(*argv
, "help") == 0) {
206 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
217 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
219 fprintf(stderr
, "Required parameter (DP, limit, min, max, "
220 "avpkt) is missing\n");
224 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
225 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
228 get_rate(&rate
, "10Mbit");
229 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
231 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
232 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
236 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too "
239 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
240 fprintf(stderr
, "GRED: failed to calculate probability.\n");
244 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
246 fprintf(stderr
, "GRED: failed to calculate idle damping "
250 opt
.Scell_log
= parm
;
252 tail
= NLMSG_TAIL(n
);
253 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
254 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
255 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
256 max_P
= probability
* pow(2, 32);
257 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
258 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
262 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
264 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
265 struct tc_gred_sopt
*sopt
;
266 struct tc_gred_qopt
*qopt
;
276 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
278 if (tb
[TCA_GRED_PARMS
] == NULL
)
281 if (tb
[TCA_GRED_MAX_P
] &&
282 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
283 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
285 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
286 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
287 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
288 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
289 fprintf(f
,"\n GRED received message smaller than expected\n");
293 /* Bad hack! should really return a proper message as shown above*/
295 fprintf(f
, "DPs %u default %u %s",
298 sopt
->grio
? "grio " : "");
300 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
301 if (qopt
->DP
>= MAX_DPs
) continue;
302 fprintf(f
, "\n DP %u prio %hhu limit %s min %s max %s ",
305 sprint_size(qopt
->limit
, b1
),
306 sprint_size(qopt
->qth_min
, b2
),
307 sprint_size(qopt
->qth_max
, b3
));
309 fprintf(f
, "ewma %u ", qopt
->Wlog
);
311 fprintf(f
, "probability %lg ", max_p
[i
] / pow(2, 32));
313 fprintf(f
, "Plog %u ", qopt
->Plog
);
314 fprintf(f
, "Scell_log %u ", qopt
->Scell_log
);
317 fprintf(f
, "\n Queue size: average %s current %s ",
318 sprint_size(qopt
->qave
, b1
),
319 sprint_size(qopt
->backlog
, b2
));
320 fprintf(f
, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
325 fprintf(f
, "\n Total packets: %u (%s) ",
327 sprint_size(qopt
->bytesin
, b1
));
333 struct qdisc_util gred_qdisc_util
= {
335 .parse_qopt
= gred_parse_opt
,
336 .print_qopt
= gred_print_opt
,