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)
40 "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n"
41 " default DEFAULT_VQ [ grio ] [ limit BYTES ] [ecn] [harddrop]\n"
42 " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n"
43 " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n"
44 " [ probability PROBABILITY ] [ bandwidth KBPS ] [ecn] [harddrop]\n");
47 static int init_gred(struct qdisc_util
*qu
, int argc
, char **argv
,
52 struct tc_gred_sopt opt
= { 0 };
58 DPRINTF(stderr
, "init_gred: invoked with %s\n", *argv
);
59 if (strcmp(*argv
, "vqs") == 0 ||
60 strcmp(*argv
, "DPs") == 0) {
62 if (get_unsigned(&opt
.DPs
, *argv
, 10)) {
63 fprintf(stderr
, "Illegal \"vqs\"\n");
65 } else if (opt
.DPs
> MAX_DPs
) {
66 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
70 } else if (strcmp(*argv
, "default") == 0) {
72 fprintf(stderr
, "\"default\" must be defined after \"vqs\"\n");
76 if (get_unsigned(&opt
.def_DP
, *argv
, 10)) {
77 fprintf(stderr
, "Illegal \"default\"\n");
79 } else if (opt
.def_DP
>= opt
.DPs
) {
80 fprintf(stderr
, "\"default\" must be less than \"vqs\"\n");
83 } else if (strcmp(*argv
, "grio") == 0) {
85 } else if (strcmp(*argv
, "limit") == 0) {
87 if (get_size(&limit
, *argv
)) {
88 fprintf(stderr
, "Illegal \"limit\"\n");
91 } else if (strcmp(*argv
, "ecn") == 0) {
92 opt
.flags
|= TC_RED_ECN
;
93 } else if (strcmp(*argv
, "harddrop") == 0) {
94 opt
.flags
|= TC_RED_HARDDROP
;
95 } else if (strcmp(*argv
, "help") == 0) {
99 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
106 if (!opt
.DPs
|| opt
.def_DP
== MAX_DPs
) {
107 fprintf(stderr
, "Illegal gred setup parameters\n");
111 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt
.DPs
, opt
.def_DP
);
112 n
->nlmsg_flags
|= NLM_F_CREATE
;
113 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
114 addattr_l(n
, 1024, TCA_GRED_DPS
, &opt
, sizeof(struct tc_gred_sopt
));
116 addattr32(n
, 1024, TCA_GRED_LIMIT
, limit
);
117 addattr_nest_end(n
, tail
);
121 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123 static int gred_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
125 struct rtattr
*tail
, *entry
, *vqs
;
127 struct tc_gred_qopt opt
= { 0 };
128 unsigned int burst
= 0;
129 unsigned int avpkt
= 0;
130 unsigned int flags
= 0;
131 double probability
= 0.02;
132 unsigned int rate
= 0;
140 if (strcmp(*argv
, "limit") == 0) {
142 if (get_size(&opt
.limit
, *argv
)) {
143 fprintf(stderr
, "Illegal \"limit\"\n");
147 } else if (strcmp(*argv
, "setup") == 0) {
149 fprintf(stderr
, "Illegal \"setup\"\n");
152 return init_gred(qu
, argc
-1, argv
+1, n
);
153 } else if (strcmp(*argv
, "min") == 0) {
155 if (get_size(&opt
.qth_min
, *argv
)) {
156 fprintf(stderr
, "Illegal \"min\"\n");
160 } else if (strcmp(*argv
, "max") == 0) {
162 if (get_size(&opt
.qth_max
, *argv
)) {
163 fprintf(stderr
, "Illegal \"max\"\n");
167 } else if (strcmp(*argv
, "vq") == 0 ||
168 strcmp(*argv
, "DP") == 0) {
170 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
171 fprintf(stderr
, "Illegal \"vq\"\n");
173 } else if (opt
.DP
>= MAX_DPs
) {
174 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
177 } /* need a better error check */
179 } else if (strcmp(*argv
, "burst") == 0) {
181 if (get_unsigned(&burst
, *argv
, 0)) {
182 fprintf(stderr
, "Illegal \"burst\"\n");
186 } else if (strcmp(*argv
, "avpkt") == 0) {
188 if (get_size(&avpkt
, *argv
)) {
189 fprintf(stderr
, "Illegal \"avpkt\"\n");
193 } else if (strcmp(*argv
, "probability") == 0) {
195 if (sscanf(*argv
, "%lg", &probability
) != 1) {
196 fprintf(stderr
, "Illegal \"probability\"\n");
200 } else if (strcmp(*argv
, "prio") == 0) {
202 opt
.prio
= strtol(*argv
, (char **)NULL
, 10);
203 /* some error check here */
205 } else if (strcmp(*argv
, "bandwidth") == 0) {
207 if (strchr(*argv
, '%')) {
208 if (get_percent_rate(&rate
, *argv
, dev
)) {
209 fprintf(stderr
, "Illegal \"bandwidth\"\n");
212 } else if (get_rate(&rate
, *argv
)) {
213 fprintf(stderr
, "Illegal \"bandwidth\"\n");
217 } else if (strcmp(*argv
, "ecn") == 0) {
219 } else if (strcmp(*argv
, "harddrop") == 0) {
220 flags
|= TC_RED_HARDDROP
;
221 } else if (strcmp(*argv
, "help") == 0) {
225 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
236 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
238 fprintf(stderr
, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
242 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
243 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
246 get_rate(&rate
, "10Mbit");
247 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
249 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
250 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
254 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too large.\n",
257 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
258 fprintf(stderr
, "GRED: failed to calculate probability.\n");
262 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
264 fprintf(stderr
, "GRED: failed to calculate idle damping table.\n");
267 opt
.Scell_log
= parm
;
269 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
270 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
271 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
272 max_P
= probability
* pow(2, 32);
273 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
275 vqs
= addattr_nest(n
, 1024, TCA_GRED_VQ_LIST
);
276 entry
= addattr_nest(n
, 1024, TCA_GRED_VQ_ENTRY
);
277 addattr32(n
, 1024, TCA_GRED_VQ_DP
, opt
.DP
);
278 addattr32(n
, 1024, TCA_GRED_VQ_FLAGS
, flags
);
279 addattr_nest_end(n
, entry
);
280 addattr_nest_end(n
, vqs
);
282 addattr_nest_end(n
, tail
);
286 struct tc_gred_info
{
301 gred_parse_vqs(struct tc_gred_info
*info
, struct rtattr
*vqs
)
303 int rem
= RTA_PAYLOAD(vqs
);
304 unsigned int offset
= 0;
306 while (rem
> offset
) {
307 struct rtattr
*tb_entry
[TCA_GRED_VQ_ENTRY_MAX
+ 1] = {};
308 struct rtattr
*tb
[TCA_GRED_VQ_MAX
+ 1] = {};
309 struct rtattr
*entry
;
313 entry
= RTA_DATA(vqs
) + offset
;
315 parse_rtattr(tb_entry
, TCA_GRED_VQ_ENTRY_MAX
, entry
,
317 len
= RTA_LENGTH(RTA_PAYLOAD(entry
));
320 if (!tb_entry
[TCA_GRED_VQ_ENTRY
]) {
322 "ERROR: Failed to parse Virtual Queue entry\n");
326 parse_rtattr_nested(tb
, TCA_GRED_VQ_MAX
,
327 tb_entry
[TCA_GRED_VQ_ENTRY
]);
329 if (!tb
[TCA_GRED_VQ_DP
]) {
331 "ERROR: Virtual Queue without DP attribute\n");
335 dp
= rta_getattr_u32(tb
[TCA_GRED_VQ_DP
]);
337 if (tb
[TCA_GRED_VQ_STAT_BYTES
])
339 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BYTES
]);
340 if (tb
[TCA_GRED_VQ_STAT_PACKETS
])
342 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PACKETS
]);
343 if (tb
[TCA_GRED_VQ_STAT_BACKLOG
])
345 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BACKLOG
]);
346 if (tb
[TCA_GRED_VQ_STAT_PROB_DROP
])
348 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_DROP
]);
349 if (tb
[TCA_GRED_VQ_STAT_PROB_MARK
])
351 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_MARK
]);
352 if (tb
[TCA_GRED_VQ_STAT_FORCED_DROP
])
353 info
[dp
].forced_drop
=
354 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_DROP
]);
355 if (tb
[TCA_GRED_VQ_STAT_FORCED_MARK
])
356 info
[dp
].forced_mark
=
357 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_MARK
]);
358 if (tb
[TCA_GRED_VQ_STAT_PDROP
])
360 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PDROP
]);
361 if (tb
[TCA_GRED_VQ_STAT_OTHER
])
363 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_OTHER
]);
364 info
[dp
].flags_present
= !!tb
[TCA_GRED_VQ_FLAGS
];
365 if (tb
[TCA_GRED_VQ_FLAGS
])
367 rta_getattr_u32(tb
[TCA_GRED_VQ_FLAGS
]);
372 gred_print_stats(struct tc_gred_info
*info
, struct tc_gred_qopt
*qopt
)
374 __u64 bytes
= info
? info
->bytes
: qopt
->bytesin
;
378 if (!is_json_context())
379 printf("\n Queue size: ");
381 print_uint(PRINT_JSON
, "qave", NULL
, qopt
->qave
);
382 print_string(PRINT_FP
, NULL
, "average %s ",
383 sprint_size(qopt
->qave
, b1
));
385 print_uint(PRINT_JSON
, "backlog", NULL
, qopt
->backlog
);
386 print_string(PRINT_FP
, NULL
, "current %s ",
387 sprint_size(qopt
->backlog
, b1
));
389 if (!is_json_context())
390 printf("\n Dropped packets: ");
393 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
395 print_uint(PRINT_ANY
, "prob_drop", "early %u ",
397 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", info
->pdrop
);
398 print_uint(PRINT_ANY
, "other", "other %u ", info
->other
);
400 if (!is_json_context())
401 printf("\n Marked packets: ");
402 print_uint(PRINT_ANY
, "forced_mark", "forced %u ",
404 print_uint(PRINT_ANY
, "prob_mark", "early %u ",
407 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
409 print_uint(PRINT_ANY
, "prob_drop", "early %u ", qopt
->early
);
410 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", qopt
->pdrop
);
411 print_uint(PRINT_ANY
, "other", "other %u ", qopt
->other
);
414 if (!is_json_context())
415 printf("\n Total packets: ");
417 print_uint(PRINT_ANY
, "packets", "%u ", qopt
->packets
);
419 print_uint(PRINT_JSON
, "bytes", NULL
, bytes
);
420 print_string(PRINT_FP
, NULL
, "(%s) ", sprint_size(bytes
, b1
));
423 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
425 struct tc_gred_info infos
[MAX_DPs
] = {};
426 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
427 struct tc_gred_sopt
*sopt
;
428 struct tc_gred_qopt
*qopt
;
429 bool vq_info
= false;
439 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
441 if (tb
[TCA_GRED_PARMS
] == NULL
)
444 if (tb
[TCA_GRED_MAX_P
] &&
445 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
446 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
448 if (tb
[TCA_GRED_LIMIT
] &&
449 RTA_PAYLOAD(tb
[TCA_GRED_LIMIT
]) == sizeof(__u32
))
450 limit
= RTA_DATA(tb
[TCA_GRED_LIMIT
]);
452 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
453 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
454 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
455 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
456 fprintf(f
, "\n GRED received message smaller than expected\n");
460 if (tb
[TCA_GRED_VQ_LIST
]) {
461 gred_parse_vqs(infos
, tb
[TCA_GRED_VQ_LIST
]);
465 print_uint(PRINT_ANY
, "dp_cnt", "vqs %u ", sopt
->DPs
);
466 print_uint(PRINT_ANY
, "dp_default", "default %u ", sopt
->def_DP
);
469 print_bool(PRINT_ANY
, "grio", "grio ", true);
471 print_bool(PRINT_ANY
, "grio", NULL
, false);
474 print_uint(PRINT_JSON
, "limit", NULL
, *limit
);
475 print_string(PRINT_FP
, NULL
, "limit %s ",
476 sprint_size(*limit
, b1
));
479 tc_red_print_flags(sopt
->flags
);
481 open_json_array(PRINT_JSON
, "vqs");
482 for (i
= 0; i
< MAX_DPs
; i
++, qopt
++) {
483 if (qopt
->DP
>= MAX_DPs
)
486 open_json_object(NULL
);
488 print_uint(PRINT_ANY
, "vq", "\n vq %u ", qopt
->DP
);
489 print_hhu(PRINT_ANY
, "prio", "prio %hhu ", qopt
->prio
);
491 print_uint(PRINT_JSON
, "limit", NULL
, qopt
->limit
);
492 print_string(PRINT_FP
, NULL
, "limit %s ",
493 sprint_size(qopt
->limit
, b1
));
495 print_uint(PRINT_JSON
, "min", NULL
, qopt
->qth_min
);
496 print_string(PRINT_FP
, NULL
, "min %s ",
497 sprint_size(qopt
->qth_min
, b1
));
499 print_uint(PRINT_JSON
, "max", NULL
, qopt
->qth_max
);
500 print_string(PRINT_FP
, NULL
, "max %s ",
501 sprint_size(qopt
->qth_max
, b1
));
503 if (infos
[i
].flags_present
)
504 tc_red_print_flags(infos
[i
].flags
);
507 print_uint(PRINT_ANY
, "ewma", "ewma %u ", qopt
->Wlog
);
509 print_float(PRINT_ANY
, "probability",
511 max_p
[i
] / pow(2, 32));
513 print_uint(PRINT_ANY
, "Plog", "Plog %u ",
515 print_uint(PRINT_ANY
, "Scell_log", "Scell_log %u ",
519 gred_print_stats(vq_info
? &infos
[i
] : NULL
, qopt
);
522 close_json_array(PRINT_JSON
, "vqs");
526 struct qdisc_util gred_qdisc_util
= {
528 .parse_qopt
= gred_parse_opt
,
529 .print_qopt
= gred_print_opt
,