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 ] [ecn] [harddrop]\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
)
124 struct rtattr
*tail
, *entry
, *vqs
;
126 struct tc_gred_qopt opt
= { 0 };
127 unsigned int burst
= 0;
128 unsigned int avpkt
= 0;
129 unsigned int flags
= 0;
130 double probability
= 0.02;
131 unsigned int rate
= 0;
139 if (strcmp(*argv
, "limit") == 0) {
141 if (get_size(&opt
.limit
, *argv
)) {
142 fprintf(stderr
, "Illegal \"limit\"\n");
146 } else if (strcmp(*argv
, "setup") == 0) {
148 fprintf(stderr
, "Illegal \"setup\"\n");
151 return init_gred(qu
, argc
-1, argv
+1, n
);
152 } else if (strcmp(*argv
, "min") == 0) {
154 if (get_size(&opt
.qth_min
, *argv
)) {
155 fprintf(stderr
, "Illegal \"min\"\n");
159 } else if (strcmp(*argv
, "max") == 0) {
161 if (get_size(&opt
.qth_max
, *argv
)) {
162 fprintf(stderr
, "Illegal \"max\"\n");
166 } else if (strcmp(*argv
, "vq") == 0 ||
167 strcmp(*argv
, "DP") == 0) {
169 if (get_unsigned(&opt
.DP
, *argv
, 10)) {
170 fprintf(stderr
, "Illegal \"vq\"\n");
172 } else if (opt
.DP
>= MAX_DPs
) {
173 fprintf(stderr
, "GRED: only %u VQs are currently supported\n",
176 } /* need a better error check */
178 } else if (strcmp(*argv
, "burst") == 0) {
180 if (get_unsigned(&burst
, *argv
, 0)) {
181 fprintf(stderr
, "Illegal \"burst\"\n");
185 } else if (strcmp(*argv
, "avpkt") == 0) {
187 if (get_size(&avpkt
, *argv
)) {
188 fprintf(stderr
, "Illegal \"avpkt\"\n");
192 } else if (strcmp(*argv
, "probability") == 0) {
194 if (sscanf(*argv
, "%lg", &probability
) != 1) {
195 fprintf(stderr
, "Illegal \"probability\"\n");
199 } else if (strcmp(*argv
, "prio") == 0) {
201 opt
.prio
= strtol(*argv
, (char **)NULL
, 10);
202 /* some error check here */
204 } else if (strcmp(*argv
, "bandwidth") == 0) {
206 if (strchr(*argv
, '%')) {
207 if (get_percent_rate(&rate
, *argv
, dev
)) {
208 fprintf(stderr
, "Illegal \"bandwidth\"\n");
211 } else if (get_rate(&rate
, *argv
)) {
212 fprintf(stderr
, "Illegal \"bandwidth\"\n");
216 } else if (strcmp(*argv
, "ecn") == 0) {
218 } else if (strcmp(*argv
, "harddrop") == 0) {
219 flags
|= TC_RED_HARDDROP
;
220 } else if (strcmp(*argv
, "help") == 0) {
224 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
235 if (opt
.DP
== MAX_DPs
|| !opt
.limit
|| !opt
.qth_min
|| !opt
.qth_max
||
237 fprintf(stderr
, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
241 burst
= (2 * opt
.qth_min
+ opt
.qth_max
) / (3 * avpkt
);
242 fprintf(stderr
, "GRED: set burst to %u\n", burst
);
245 get_rate(&rate
, "10Mbit");
246 fprintf(stderr
, "GRED: set bandwidth to 10Mbit\n");
248 if ((parm
= tc_red_eval_ewma(opt
.qth_min
, burst
, avpkt
)) < 0) {
249 fprintf(stderr
, "GRED: failed to calculate EWMA constant.\n");
253 fprintf(stderr
, "GRED: WARNING. Burst %u seems to be too large.\n",
256 if ((parm
= tc_red_eval_P(opt
.qth_min
, opt
.qth_max
, probability
)) < 0) {
257 fprintf(stderr
, "GRED: failed to calculate probability.\n");
261 if ((parm
= tc_red_eval_idle_damping(opt
.Wlog
, avpkt
, rate
, sbuf
)) < 0)
263 fprintf(stderr
, "GRED: failed to calculate idle damping table.\n");
266 opt
.Scell_log
= parm
;
268 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
269 addattr_l(n
, 1024, TCA_GRED_PARMS
, &opt
, sizeof(opt
));
270 addattr_l(n
, 1024, TCA_GRED_STAB
, sbuf
, 256);
271 max_P
= probability
* pow(2, 32);
272 addattr32(n
, 1024, TCA_GRED_MAX_P
, max_P
);
274 vqs
= addattr_nest(n
, 1024, TCA_GRED_VQ_LIST
);
275 entry
= addattr_nest(n
, 1024, TCA_GRED_VQ_ENTRY
);
276 addattr32(n
, 1024, TCA_GRED_VQ_DP
, opt
.DP
);
277 addattr32(n
, 1024, TCA_GRED_VQ_FLAGS
, flags
);
278 addattr_nest_end(n
, entry
);
279 addattr_nest_end(n
, vqs
);
281 addattr_nest_end(n
, tail
);
285 struct tc_gred_info
{
300 gred_parse_vqs(struct tc_gred_info
*info
, struct rtattr
*vqs
)
302 int rem
= RTA_PAYLOAD(vqs
);
303 unsigned int offset
= 0;
305 while (rem
> offset
) {
306 struct rtattr
*tb_entry
[TCA_GRED_VQ_ENTRY_MAX
+ 1] = {};
307 struct rtattr
*tb
[TCA_GRED_VQ_MAX
+ 1] = {};
308 struct rtattr
*entry
;
312 entry
= RTA_DATA(vqs
) + offset
;
314 parse_rtattr(tb_entry
, TCA_GRED_VQ_ENTRY_MAX
, entry
,
316 len
= RTA_LENGTH(RTA_PAYLOAD(entry
));
319 if (!tb_entry
[TCA_GRED_VQ_ENTRY
]) {
321 "ERROR: Failed to parse Virtual Queue entry\n");
325 parse_rtattr_nested(tb
, TCA_GRED_VQ_MAX
,
326 tb_entry
[TCA_GRED_VQ_ENTRY
]);
328 if (!tb
[TCA_GRED_VQ_DP
]) {
330 "ERROR: Virtual Queue without DP attribute\n");
334 dp
= rta_getattr_u32(tb
[TCA_GRED_VQ_DP
]);
336 if (tb
[TCA_GRED_VQ_STAT_BYTES
])
338 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BYTES
]);
339 if (tb
[TCA_GRED_VQ_STAT_PACKETS
])
341 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PACKETS
]);
342 if (tb
[TCA_GRED_VQ_STAT_BACKLOG
])
344 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_BACKLOG
]);
345 if (tb
[TCA_GRED_VQ_STAT_PROB_DROP
])
347 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_DROP
]);
348 if (tb
[TCA_GRED_VQ_STAT_PROB_MARK
])
350 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PROB_MARK
]);
351 if (tb
[TCA_GRED_VQ_STAT_FORCED_DROP
])
352 info
[dp
].forced_drop
=
353 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_DROP
]);
354 if (tb
[TCA_GRED_VQ_STAT_FORCED_MARK
])
355 info
[dp
].forced_mark
=
356 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_FORCED_MARK
]);
357 if (tb
[TCA_GRED_VQ_STAT_PDROP
])
359 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_PDROP
]);
360 if (tb
[TCA_GRED_VQ_STAT_OTHER
])
362 rta_getattr_u32(tb
[TCA_GRED_VQ_STAT_OTHER
]);
363 info
[dp
].flags_present
= !!tb
[TCA_GRED_VQ_FLAGS
];
364 if (tb
[TCA_GRED_VQ_FLAGS
])
366 rta_getattr_u32(tb
[TCA_GRED_VQ_FLAGS
]);
371 gred_print_stats(struct tc_gred_info
*info
, struct tc_gred_qopt
*qopt
)
373 __u64 bytes
= info
? info
->bytes
: qopt
->bytesin
;
377 if (!is_json_context())
378 printf("\n Queue size: ");
380 print_uint(PRINT_JSON
, "qave", NULL
, qopt
->qave
);
381 print_string(PRINT_FP
, NULL
, "average %s ",
382 sprint_size(qopt
->qave
, b1
));
384 print_uint(PRINT_JSON
, "backlog", NULL
, qopt
->backlog
);
385 print_string(PRINT_FP
, NULL
, "current %s ",
386 sprint_size(qopt
->backlog
, b1
));
388 if (!is_json_context())
389 printf("\n Dropped packets: ");
392 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
394 print_uint(PRINT_ANY
, "prob_drop", "early %u ",
396 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", info
->pdrop
);
397 print_uint(PRINT_ANY
, "other", "other %u ", info
->other
);
399 if (!is_json_context())
400 printf("\n Marked packets: ");
401 print_uint(PRINT_ANY
, "forced_mark", "forced %u ",
403 print_uint(PRINT_ANY
, "prob_mark", "early %u ",
406 print_uint(PRINT_ANY
, "forced_drop", "forced %u ",
408 print_uint(PRINT_ANY
, "prob_drop", "early %u ", qopt
->early
);
409 print_uint(PRINT_ANY
, "pdrop", "pdrop %u ", qopt
->pdrop
);
410 print_uint(PRINT_ANY
, "other", "other %u ", qopt
->other
);
413 if (!is_json_context())
414 printf("\n Total packets: ");
416 print_uint(PRINT_ANY
, "packets", "%u ", qopt
->packets
);
418 print_uint(PRINT_JSON
, "bytes", NULL
, bytes
);
419 print_string(PRINT_FP
, NULL
, "(%s) ", sprint_size(bytes
, b1
));
422 static int gred_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
424 struct tc_gred_info infos
[MAX_DPs
] = {};
425 struct rtattr
*tb
[TCA_GRED_MAX
+ 1];
426 struct tc_gred_sopt
*sopt
;
427 struct tc_gred_qopt
*qopt
;
428 bool vq_info
= false;
438 parse_rtattr_nested(tb
, TCA_GRED_MAX
, opt
);
440 if (tb
[TCA_GRED_PARMS
] == NULL
)
443 if (tb
[TCA_GRED_MAX_P
] &&
444 RTA_PAYLOAD(tb
[TCA_GRED_MAX_P
]) >= sizeof(__u32
) * MAX_DPs
)
445 max_p
= RTA_DATA(tb
[TCA_GRED_MAX_P
]);
447 if (tb
[TCA_GRED_LIMIT
] &&
448 RTA_PAYLOAD(tb
[TCA_GRED_LIMIT
]) == sizeof(__u32
))
449 limit
= RTA_DATA(tb
[TCA_GRED_LIMIT
]);
451 sopt
= RTA_DATA(tb
[TCA_GRED_DPS
]);
452 qopt
= RTA_DATA(tb
[TCA_GRED_PARMS
]);
453 if (RTA_PAYLOAD(tb
[TCA_GRED_DPS
]) < sizeof(*sopt
) ||
454 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
]) < sizeof(*qopt
)*MAX_DPs
) {
455 fprintf(f
, "\n GRED received message smaller than expected\n");
459 if (tb
[TCA_GRED_VQ_LIST
]) {
460 gred_parse_vqs(infos
, tb
[TCA_GRED_VQ_LIST
]);
464 print_uint(PRINT_ANY
, "dp_cnt", "vqs %u ", sopt
->DPs
);
465 print_uint(PRINT_ANY
, "dp_default", "default %u ", sopt
->def_DP
);
468 print_bool(PRINT_ANY
, "grio", "grio ", true);
470 print_bool(PRINT_ANY
, "grio", NULL
, false);
473 print_uint(PRINT_JSON
, "limit", NULL
, *limit
);
474 print_string(PRINT_FP
, NULL
, "limit %s ",
475 sprint_size(*limit
, b1
));
478 tc_red_print_flags(sopt
->flags
);
480 open_json_array(PRINT_JSON
, "vqs");
481 for (i
= 0; i
< MAX_DPs
; i
++, qopt
++) {
482 if (qopt
->DP
>= MAX_DPs
)
485 open_json_object(NULL
);
487 print_uint(PRINT_ANY
, "vq", "\n vq %u ", qopt
->DP
);
488 print_hhu(PRINT_ANY
, "prio", "prio %hhu ", qopt
->prio
);
490 print_uint(PRINT_JSON
, "limit", NULL
, qopt
->limit
);
491 print_string(PRINT_FP
, NULL
, "limit %s ",
492 sprint_size(qopt
->limit
, b1
));
494 print_uint(PRINT_JSON
, "min", NULL
, qopt
->qth_min
);
495 print_string(PRINT_FP
, NULL
, "min %s ",
496 sprint_size(qopt
->qth_min
, b1
));
498 print_uint(PRINT_JSON
, "max", NULL
, qopt
->qth_max
);
499 print_string(PRINT_FP
, NULL
, "max %s ",
500 sprint_size(qopt
->qth_max
, b1
));
502 if (infos
[i
].flags_present
)
503 tc_red_print_flags(infos
[i
].flags
);
506 print_uint(PRINT_ANY
, "ewma", "ewma %u ", qopt
->Wlog
);
508 print_float(PRINT_ANY
, "probability",
510 max_p
[i
] / pow(2, 32));
512 print_uint(PRINT_ANY
, "Plog", "Plog %u ",
514 print_uint(PRINT_ANY
, "Scell_log", "Scell_log %u ",
518 gred_print_stats(vq_info
? &infos
[i
] : NULL
, qopt
);
521 close_json_array(PRINT_JSON
, "vqs");
525 struct qdisc_util gred_qdisc_util
= {
527 .parse_qopt
= gred_parse_opt
,
528 .print_qopt
= gred_print_opt
,