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
,
56 struct tc_gred_sopt opt
;
61 DPRINTF(stderr
,"init_gred: invoked with %s\n",*argv
);
62 if (strcmp(*argv
, "DPs") == 0) {
64 DPRINTF(stderr
,"init_gred: next_arg with %s\n",*argv
);
65 dps
= strtol(*argv
, (char **)NULL
, 10);
66 if (dps
< 0 || dps
>MAX_DPs
) {
67 fprintf(stderr
, "DPs =%d\n", dps
);
68 fprintf(stderr
, "Illegal \"DPs\"\n");
69 fprintf(stderr
, "GRED: only %d DPs are "
70 "currently supported\n",MAX_DPs
);
73 } else if (strcmp(*argv
, "default") == 0) {
75 def_dp
= strtol(*argv
, (char **)NULL
, 10);
77 fprintf(stderr
, "\"default DP\" must be "
78 "defined after DPs\n");
81 if (def_dp
< 0 || def_dp
> dps
) {
83 "\"default DP\" must be less than %d\n",
87 } else if (strcmp(*argv
, "grio") == 0) {
89 } else if (strcmp(*argv
, "help") == 0) {
93 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
100 if (!dps
|| def_dp
== -1) {
101 fprintf(stderr
, "Illegal gred setup parameters \n");
105 memset(&opt
, 0, sizeof(struct tc_gred_sopt
));
109 DPRINTF("TC_GRED: sending DPs=%d default=%d\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
));
114 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
118 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
123 struct tc_gred_qopt opt
;
126 double probability
= 0.02;
132 memset(&opt
, 0, sizeof(opt
));
135 if (strcmp(*argv
, "limit") == 0) {
137 if (get_size(&opt
.limit
, *argv
)) {
138 fprintf(stderr
, "Illegal \"limit\"\n");
142 } else if (strcmp(*argv
, "setup") == 0) {
144 fprintf(stderr
, "Illegal \"setup\"\n");
147 return init_gred(qu
,argc
-1, argv
+1,n
);
149 } else if (strcmp(*argv
, "min") == 0) {
151 if (get_size(&opt
.qth_min
, *argv
)) {
152 fprintf(stderr
, "Illegal \"min\"\n");
156 } else if (strcmp(*argv
, "max") == 0) {
158 if (get_size(&opt
.qth_max
, *argv
)) {
159 fprintf(stderr
, "Illegal \"max\"\n");
163 } else if (strcmp(*argv
, "DP") == 0) {
165 opt
.DP
=strtol(*argv
, (char **)NULL
, 10);
166 DPRINTF ("\n ******* DP =%u\n",opt
.DP
);
167 if (opt
.DP
>MAX_DPs
) { /* need a better error check */
168 fprintf(stderr
, "DP =%u \n",opt
.DP
);
169 fprintf(stderr
, "Illegal \"DP\"\n");
170 fprintf(stderr
, "GRED: only %d DPs are currently supported\n",MAX_DPs
);
174 } else if (strcmp(*argv
, "burst") == 0) {
176 if (get_unsigned(&burst
, *argv
, 0)) {
177 fprintf(stderr
, "Illegal \"burst\"\n");
181 } else if (strcmp(*argv
, "avpkt") == 0) {
183 if (get_size(&avpkt
, *argv
)) {
184 fprintf(stderr
, "Illegal \"avpkt\"\n");
188 } else if (strcmp(*argv
, "probability") == 0) {
190 if (sscanf(*argv
, "%lg", &probability
) != 1) {
191 fprintf(stderr
, "Illegal \"probability\"\n");
195 } else if (strcmp(*argv
, "prio") == 0) {
197 opt
.prio
=strtol(*argv
, (char **)NULL
, 10);
198 /* some error check here */
200 } else if (strcmp(*argv
, "bandwidth") == 0) {
202 if (get_rate(&rate
, *argv
)) {
203 fprintf(stderr
, "Illegal \"bandwidth\"\n");
207 } else if (strcmp(*argv
, "help") == 0) {
211 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
222 get_rate(&rate
, "10Mbit");
224 if (!opt
.qth_min
|| !opt
.qth_max
|| !burst
|| !opt
.limit
|| !avpkt
||
226 fprintf(stderr
, "Required parameter (min, max, burst, limit, "
227 "avpket, DP) is missing\n");
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 to "
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 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
260 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
262 struct rtattr
*tb
[TCA_GRED_STAB
+1];
263 struct tc_gred_qopt
*qopt
;
274 parse_rtattr_nested(tb
, TCA_GRED_STAB
, opt
);
276 if (tb
[TCA_GRED_PARMS
] == NULL
)
279 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
280 if (RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
281 fprintf(f
,"\n GRED received message smaller than expected\n");
285 /* Bad hack! should really return a proper message as shown above*/
287 for (i
=0;i
<MAX_DPs
;i
++, qopt
++) {
288 if (qopt
->DP
>= MAX_DPs
) continue;
289 fprintf(f
, "\n DP:%d (prio %d) Average Queue %s Measured "
293 sprint_size(qopt
->qave
, b4
),
294 sprint_size(qopt
->backlog
, b5
));
295 fprintf(f
, "\n\t Packet drops: %d (forced %d early %d) ",
296 qopt
->forced
+qopt
->early
,
299 fprintf(f
, "\n\t Packet totals: %u (bytes %u) ",
303 fprintf(f
, "\n 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
));
307 fprintf(f
, "ewma %u Plog %u Scell_log %u",
308 qopt
->Wlog
, qopt
->Plog
, qopt
->Scell_log
);
313 struct qdisc_util gred_qdisc_util
= {
315 .parse_qopt
= gred_parse_opt
,
316 .print_qopt
= gred_print_opt
,