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>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #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: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
40 fprintf(stderr
, " default DEFAULT_VQ [ grio ] [ limit BYTES ] [ecn] [harddrop]\n");
41 fprintf(stderr
, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
42 fprintf(stderr
, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
43 fprintf(stderr
, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
46 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
,
51 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 currently supported\n",
69 } else if (strcmp(*argv
, "default") == 0) {
71 fprintf(stderr
, "\"default\" must be defined after \"vqs\"\n");
75 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
76 fprintf(stderr
, "Illegal \"default\"\n");
78 } else if (opt
.def_DP
>= opt
.DPs
) {
79 fprintf(stderr
, "\"default\" must be less than \"vqs\"\n");
82 } else if (strcmp(*argv
, "grio") == 0) {
84 } else if (strcmp(*argv
, "limit") == 0) {
86 if (get_size(&limit
, *argv
)) {
87 fprintf(stderr
, "Illegal \"limit\"\n");
90 } else if (strcmp(*argv
, "ecn") == 0) {
91 opt
.flags
|= TC_RED_ECN
;
92 } else if (strcmp(*argv
, "harddrop") == 0) {
93 opt
.flags
|= TC_RED_HARDDROP
;
94 } else if (strcmp(*argv
, "help") == 0) {
98 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
105 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
106 fprintf(stderr
, "Illegal gred setup parameters\n");
110 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt
.DPs
, opt
.def_DP
);
111 n
->nlmsg_flags
|= NLM_F_CREATE
;
112 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
113 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
115 addattr32(n
, 1024, TCA_GRED_LIMIT
, limit
);
116 addattr_nest_end(n
, tail
);
120 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
125 struct tc_gred_qopt opt
= { 0 };
126 unsigned int burst
= 0;
127 unsigned int avpkt
= 0;
128 double probability
= 0.02;
129 unsigned int rate
= 0;
138 if (strcmp(*argv
, "limit") == 0) {
140 if (get_size(&opt
.limit
, *argv
)) {
141 fprintf(stderr
, "Illegal \"limit\"\n");
145 } else if (strcmp(*argv
, "setup") == 0) {
147 fprintf(stderr
, "Illegal \"setup\"\n");
150 return init_gred(qu
, argc
-1, argv
+1, n
);
151 } else if (strcmp(*argv
, "min") == 0) {
153 if (get_size(&opt
.qth_min
, *argv
)) {
154 fprintf(stderr
, "Illegal \"min\"\n");
158 } else if (strcmp(*argv
, "max") == 0) {
160 if (get_size(&opt
.qth_max
, *argv
)) {
161 fprintf(stderr
, "Illegal \"max\"\n");
165 } else if (strcmp(*argv
, "vq") == 0 ||
166 strcmp(*argv
, "DP") == 0) {
168 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
169 fprintf(stderr
, "Illegal \"vq\"\n");
171 } else if (opt
.DP
>= MAX_DPs
) {
172 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
175 } /* need a better error check */
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 (strchr(*argv
, '%')) {
206 if (get_percent_rate(&rate
, *argv
, dev
)) {
207 fprintf(stderr
, "Illegal \"bandwidth\"\n");
210 } else if (get_rate(&rate
, *argv
)) {
211 fprintf(stderr
, "Illegal \"bandwidth\"\n");
215 } else if (strcmp(*argv
, "help") == 0) {
219 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
230 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
232 fprintf(stderr
, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
236 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
237 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
240 get_rate(&rate
, "10Mbit");
241 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
243 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
244 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
248 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too large.\n",
251 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
252 fprintf(stderr
, "GRED: failed to calculate probability.\n");
256 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
258 fprintf(stderr
, "GRED: failed to calculate idle damping table.\n");
261 opt
.Scell_log
= parm
;
263 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
264 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
265 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
266 max_P
= probability
* pow(2, 32);
267 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
268 addattr_nest_end(n
, tail
);
272 struct tc_gred_info
{
285 gred_parse_vqs(struct tc_gred_info
*info
, struct rtattr
*vqs
)
287 int rem
= RTA_PAYLOAD(vqs
);
288 unsigned int offset
= 0;
290 while (rem
> offset
) {
291 struct rtattr
*tb_entry
[TCA_GRED_VQ_ENTRY_MAX
+ 1] = {};
292 struct rtattr
*tb
[TCA_GRED_VQ_MAX
+ 1] = {};
293 struct rtattr
*entry
;
297 entry
= RTA_DATA(vqs
) + offset
;
299 parse_rtattr(tb_entry
, TCA_GRED_VQ_ENTRY_MAX
, entry
,
301 len
= RTA_LENGTH(RTA_PAYLOAD(entry
));
304 if (!tb_entry
[TCA_GRED_VQ_ENTRY
]) {
306 "ERROR: Failed to parse Virtual Queue entry\n");
310 parse_rtattr_nested(tb
, TCA_GRED_VQ_MAX
,
311 tb_entry
[TCA_GRED_VQ_ENTRY
]);
313 if (!tb
[TCA_GRED_VQ_DP
]) {
315 "ERROR: Virtual Queue without DP attribute\n");
319 dp
= rta_getattr_u32(tb
[TCA_GRED_VQ_DP
]);
321 if (tb
[TCA_GRED_VQ_STAT_BYTES
])
323 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BYTES
]);
324 if (tb
[TCA_GRED_VQ_STAT_PACKETS
])
326 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PACKETS
]);
327 if (tb
[TCA_GRED_VQ_STAT_BACKLOG
])
329 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BACKLOG
]);
330 if (tb
[TCA_GRED_VQ_STAT_PROB_DROP
])
332 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_DROP
]);
333 if (tb
[TCA_GRED_VQ_STAT_PROB_MARK
])
335 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_MARK
]);
336 if (tb
[TCA_GRED_VQ_STAT_FORCED_DROP
])
337 info
[dp
].forced_drop
=
338 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_DROP
]);
339 if (tb
[TCA_GRED_VQ_STAT_FORCED_MARK
])
340 info
[dp
].forced_mark
=
341 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_MARK
]);
342 if (tb
[TCA_GRED_VQ_STAT_PDROP
])
344 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PDROP
]);
345 if (tb
[TCA_GRED_VQ_STAT_OTHER
])
347 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_OTHER
]);
352 gred_print_stats(struct tc_gred_info
*info
, struct tc_gred_qopt
*qopt
)
354 __u64 bytes
= info
? info
->bytes
: qopt
->bytesin
;
358 if (!is_json_context())
359 printf("\n Queue size: ");
361 print_uint(PRINT_JSON
, "qave", NULL
, qopt
->qave
);
362 print_string(PRINT_FP
, NULL
, "average %s ",
363 sprint_size(qopt
->qave
, b1
));
365 print_uint(PRINT_JSON
, "backlog", NULL
, qopt
->backlog
);
366 print_string(PRINT_FP
, NULL
, "current %s ",
367 sprint_size(qopt
->backlog
, b1
));
369 if (!is_json_context())
370 printf("\n Dropped packets: ");
373 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
375 print_uint(PRINT_ANY
, "prob_drop", "early %u ",
377 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", info
->pdrop
);
378 print_uint(PRINT_ANY
, "other", "other %u ", info
->other
);
380 if (!is_json_context())
381 printf("\n Marked packets: ");
382 print_uint(PRINT_ANY
, "forced_mark", "forced %u ",
384 print_uint(PRINT_ANY
, "prob_mark", "early %u ",
387 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
389 print_uint(PRINT_ANY
, "prob_drop", "early %u ", qopt
->early
);
390 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", qopt
->pdrop
);
391 print_uint(PRINT_ANY
, "other", "other %u ", qopt
->other
);
394 if (!is_json_context())
395 printf("\n Total packets: ");
397 print_uint(PRINT_ANY
, "packets", "%u ", qopt
->packets
);
399 print_uint(PRINT_JSON
, "bytes", NULL
, bytes
);
400 print_string(PRINT_FP
, NULL
, "(%s) ", sprint_size(bytes
, b1
));
403 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
405 struct tc_gred_info infos
[MAX_DPs
] = {};
406 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
407 struct tc_gred_sopt
*sopt
;
408 struct tc_gred_qopt
*qopt
;
409 bool vq_info
= false;
419 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
421 if (tb
[TCA_GRED_PARMS
] == NULL
)
424 if (tb
[TCA_GRED_MAX_P
] &&
425 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
426 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
428 if (tb
[TCA_GRED_LIMIT
] &&
429 RTA_PAYLOAD(tb
[TCA_GRED_LIMIT
]) == sizeof(__u32
))
430 limit
= RTA_DATA(tb
[TCA_GRED_LIMIT
]);
432 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
433 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
434 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
435 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
436 fprintf(f
, "\n GRED received message smaller than expected\n");
440 if (tb
[TCA_GRED_VQ_LIST
] && show_stats
) {
441 gred_parse_vqs(infos
, tb
[TCA_GRED_VQ_LIST
]);
445 print_uint(PRINT_ANY
, "dp_cnt", "vqs %u ", sopt
->DPs
);
446 print_uint(PRINT_ANY
, "dp_default", "default %u ", sopt
->def_DP
);
449 print_bool(PRINT_ANY
, "grio", "grio ", true);
451 print_bool(PRINT_ANY
, "grio", NULL
, false);
454 print_uint(PRINT_JSON
, "limit", NULL
, *limit
);
455 print_string(PRINT_FP
, NULL
, "limit %s ",
456 sprint_size(*limit
, b1
));
459 tc_red_print_flags(sopt
->flags
);
461 open_json_array(PRINT_JSON
, "vqs");
462 for (i
= 0; i
< MAX_DPs
; i
++, qopt
++) {
463 if (qopt
->DP
>= MAX_DPs
)
466 open_json_object(NULL
);
468 print_uint(PRINT_ANY
, "vq", "\n vq %u ", qopt
->DP
);
469 print_hhu(PRINT_ANY
, "prio", "prio %hhu ", qopt
->prio
);
471 print_uint(PRINT_JSON
, "limit", NULL
, qopt
->limit
);
472 print_string(PRINT_FP
, NULL
, "limit %s ",
473 sprint_size(qopt
->limit
, b1
));
475 print_uint(PRINT_JSON
, "min", NULL
, qopt
->qth_min
);
476 print_string(PRINT_FP
, NULL
, "min %s ",
477 sprint_size(qopt
->qth_min
, b1
));
479 print_uint(PRINT_JSON
, "max", NULL
, qopt
->qth_max
);
480 print_string(PRINT_FP
, NULL
, "max %s ",
481 sprint_size(qopt
->qth_max
, b1
));
484 print_uint(PRINT_ANY
, "ewma", "ewma %u ", qopt
->Wlog
);
486 print_float(PRINT_ANY
, "probability",
488 max_p
[i
] / pow(2, 32));
490 print_uint(PRINT_ANY
, "Plog", "Plog %u ",
492 print_uint(PRINT_ANY
, "Scell_log", "Scell_log %u ",
496 gred_print_stats(vq_info
? &infos
[i
] : NULL
, qopt
);
499 close_json_array(PRINT_JSON
, "vqs");
503 struct qdisc_util gred_qdisc_util
= {
505 .parse_qopt
= gred_parse_opt
,
506 .print_qopt
= gred_print_opt
,