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 DPRINTF(stderr
,"init_gred: next_arg with %s\n",*argv
);
64 dps
= strtol(*argv
, (char **)NULL
, 10);
65 if (dps
< 0 || dps
>MAX_DPs
) {
66 fprintf(stderr
, "DPs =%d\n", dps
);
67 fprintf(stderr
, "Illegal \"DPs\"\n");
68 fprintf(stderr
, "GRED: only %d DPs are "
69 "currently supported\n",MAX_DPs
);
72 } else if (strcmp(*argv
, "default") == 0) {
74 def_dp
= strtol(*argv
, (char **)NULL
, 10);
76 fprintf(stderr
, "\"default DP\" must be "
77 "defined after DPs\n");
80 if (def_dp
< 0 || def_dp
> dps
) {
82 "\"default DP\" must be less than %d\n",
86 } else if (strcmp(*argv
, "grio") == 0) {
88 } else if (strcmp(*argv
, "help") == 0) {
92 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
99 if (!dps
|| def_dp
== -1) {
100 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
= NLMSG_TAIL(n
);
110 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
111 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
112 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
116 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
121 struct tc_gred_qopt opt
;
124 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
);
173 } else if (strcmp(*argv
, "burst") == 0) {
175 if (get_unsigned(&burst
, *argv
, 0)) {
176 fprintf(stderr
, "Illegal \"burst\"\n");
180 } else if (strcmp(*argv
, "avpkt") == 0) {
182 if (get_size(&avpkt
, *argv
)) {
183 fprintf(stderr
, "Illegal \"avpkt\"\n");
187 } else if (strcmp(*argv
, "probability") == 0) {
189 if (sscanf(*argv
, "%lg", &probability
) != 1) {
190 fprintf(stderr
, "Illegal \"probability\"\n");
194 } else if (strcmp(*argv
, "prio") == 0) {
196 opt
.prio
=strtol(*argv
, (char **)NULL
, 10);
197 /* some error check here */
199 } else if (strcmp(*argv
, "bandwidth") == 0) {
201 if (get_rate(&rate
, *argv
)) {
202 fprintf(stderr
, "Illegal \"bandwidth\"\n");
206 } else if (strcmp(*argv
, "help") == 0) {
210 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
218 get_rate(&rate
, "10Mbit");
220 if (!opt
.qth_min
|| !opt
.qth_max
|| !opt
.limit
|| !avpkt
||
222 fprintf(stderr
, "Required parameter (min, max, limit, "
223 "avpkt, DP) is missing\n");
227 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
228 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
231 if ((wlog
= 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 %d seems to be too "
239 if ((wlog
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
240 fprintf(stderr
, "GRED: failed to calculate probability.\n");
244 if ((wlog
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
246 fprintf(stderr
, "GRED: failed to calculate idle damping "
250 opt
.Scell_log
= wlog
;
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_qopt
*qopt
;
277 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
279 if (tb
[TCA_GRED_PARMS
] == NULL
)
282 if (tb
[TCA_GRED_MAX_P
] &&
283 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
284 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
286 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
287 if (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 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
295 if (qopt
->DP
>= MAX_DPs
) continue;
296 fprintf(f
, "\n DP:%d (prio %d) Average Queue %s Measured "
300 sprint_size(qopt
->qave
, b4
),
301 sprint_size(qopt
->backlog
, b5
));
302 fprintf(f
, "\n\t Packet drops: %d (forced %d early %d) ",
303 qopt
->forced
+qopt
->early
,
306 fprintf(f
, "\n\t Packet totals: %u (bytes %u) ",
310 fprintf(f
, "\n limit %s min %s max %s ",
311 sprint_size(qopt
->limit
, b1
),
312 sprint_size(qopt
->qth_min
, b2
),
313 sprint_size(qopt
->qth_max
, b3
));
314 fprintf(f
, "ewma %u ", qopt
->Wlog
);
316 fprintf(f
, "probability %lg ", max_p
[i
] / pow(2, 32));
318 fprintf(f
, "Plog %u ", qopt
->Plog
);
319 fprintf(f
, "Scell_log %u", qopt
->Scell_log
);
324 struct qdisc_util gred_qdisc_util
= {
326 .parse_qopt
= gred_parse_opt
,
327 .print_qopt
= gred_print_opt
,