]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_gred.c
tc: gred: use extended stats if available
[mirror_iproute2.git] / tc / q_gred.c
CommitLineData
aba5acdf
SH
1/*
2 * q_gred.c GRED.
3 *
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.
8 *
ae665a52
SH
9 * Authors: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
aba5acdf
SH
12 *
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
aba5acdf
SH
18#include <fcntl.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
1b6f0bb5 23#include <math.h>
aba5acdf
SH
24
25#include "utils.h"
26#include "tc_util.h"
27
28#include "tc_red.h"
29
30
31#if 0
32a121cb 32#define DPRINTF(format, args...) fprintf(stderr, format, ##args)
aba5acdf 33#else
32a121cb 34#define DPRINTF(format, args...)
aba5acdf
SH
35#endif
36
37static void explain(void)
38{
357c45ad 39 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
aacee269 40 fprintf(stderr, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
357c45ad
DW
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");
aba5acdf
SH
44}
45
3d0b7439 46static int init_gred(struct qdisc_util *qu, int argc, char **argv,
ebde8780 47 struct nlmsghdr *n)
aba5acdf
SH
48{
49
50 struct rtattr *tail;
cb4bd0ec 51 struct tc_gred_sopt opt = { 0 };
aacee269 52 __u32 limit = 0;
eb6d7d6a
DW
53
54 opt.def_DP = MAX_DPs;
aba5acdf
SH
55
56 while (argc > 0) {
32a121cb 57 DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
357c45ad
DW
58 if (strcmp(*argv, "vqs") == 0 ||
59 strcmp(*argv, "DPs") == 0) {
aba5acdf 60 NEXT_ARG();
eb6d7d6a 61 if (get_unsigned(&opt.DPs, *argv, 10)) {
357c45ad 62 fprintf(stderr, "Illegal \"vqs\"\n");
eb6d7d6a
DW
63 return -1;
64 } else if (opt.DPs > MAX_DPs) {
32a121cb
SH
65 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
66 MAX_DPs);
aba5acdf
SH
67 return -1;
68 }
69 } else if (strcmp(*argv, "default") == 0) {
eb6d7d6a 70 if (opt.DPs == 0) {
32a121cb 71 fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
aba5acdf
SH
72 return -1;
73 }
eb6d7d6a
DW
74 NEXT_ARG();
75 if (get_unsigned(&opt.def_DP, *argv, 10)) {
76 fprintf(stderr, "Illegal \"default\"\n");
77 return -1;
78 } else if (opt.def_DP >= opt.DPs) {
32a121cb 79 fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
aba5acdf
SH
80 return -1;
81 }
82 } else if (strcmp(*argv, "grio") == 0) {
cb4bd0ec 83 opt.grio = 1;
aacee269
DW
84 } else if (strcmp(*argv, "limit") == 0) {
85 NEXT_ARG();
86 if (get_size(&limit, *argv)) {
87 fprintf(stderr, "Illegal \"limit\"\n");
88 return -1;
89 }
aba5acdf
SH
90 } else if (strcmp(*argv, "help") == 0) {
91 explain();
92 return -1;
93 } else {
94 fprintf(stderr, "What is \"%s\"?\n", *argv);
95 explain();
96 return -1;
97 }
98 argc--; argv++;
ebde8780 99 }
aba5acdf 100
eb6d7d6a 101 if (!opt.DPs || opt.def_DP == MAX_DPs) {
32a121cb 102 fprintf(stderr, "Illegal gred setup parameters\n");
ebde8780
SH
103 return -1;
104 }
105
32a121cb
SH
106 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt.DPs, opt.def_DP);
107 n->nlmsg_flags |= NLM_F_CREATE;
c14f9d92 108 tail = addattr_nest(n, 1024, TCA_OPTIONS);
aba5acdf 109 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
aacee269
DW
110 if (limit)
111 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
c14f9d92 112 addattr_nest_end(n, tail);
ebde8780 113 return 0;
aba5acdf
SH
114}
115/*
116^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117*/
927e3cfb 118static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
aba5acdf 119{
32a121cb 120 int ok = 0;
eb6d7d6a 121 struct tc_gred_qopt opt = { 0 };
32a121cb
SH
122 unsigned int burst = 0;
123 unsigned int avpkt = 0;
aba5acdf 124 double probability = 0.02;
32a121cb 125 unsigned int rate = 0;
9d9a67c7 126 int parm;
aba5acdf
SH
127 __u8 sbuf[256];
128 struct rtattr *tail;
1b6f0bb5 129 __u32 max_P;
aba5acdf 130
eb6d7d6a 131 opt.DP = MAX_DPs;
aba5acdf
SH
132
133 while (argc > 0) {
134 if (strcmp(*argv, "limit") == 0) {
135 NEXT_ARG();
136 if (get_size(&opt.limit, *argv)) {
137 fprintf(stderr, "Illegal \"limit\"\n");
138 return -1;
139 }
140 ok++;
141 } else if (strcmp(*argv, "setup") == 0) {
142 if (ok) {
143 fprintf(stderr, "Illegal \"setup\"\n");
144 return -1;
145 }
d73e0408 146 return init_gred(qu, argc-1, argv+1, n);
aba5acdf
SH
147 } else if (strcmp(*argv, "min") == 0) {
148 NEXT_ARG();
149 if (get_size(&opt.qth_min, *argv)) {
150 fprintf(stderr, "Illegal \"min\"\n");
151 return -1;
152 }
153 ok++;
154 } else if (strcmp(*argv, "max") == 0) {
155 NEXT_ARG();
156 if (get_size(&opt.qth_max, *argv)) {
157 fprintf(stderr, "Illegal \"max\"\n");
158 return -1;
159 }
160 ok++;
357c45ad
DW
161 } else if (strcmp(*argv, "vq") == 0 ||
162 strcmp(*argv, "DP") == 0) {
aba5acdf 163 NEXT_ARG();
eb6d7d6a 164 if (get_unsigned(&opt.DP, *argv, 10)) {
357c45ad 165 fprintf(stderr, "Illegal \"vq\"\n");
aba5acdf 166 return -1;
eb6d7d6a 167 } else if (opt.DP >= MAX_DPs) {
32a121cb
SH
168 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
169 MAX_DPs);
eb6d7d6a
DW
170 return -1;
171 } /* need a better error check */
aba5acdf
SH
172 ok++;
173 } else if (strcmp(*argv, "burst") == 0) {
174 NEXT_ARG();
d73e0408 175 if (get_unsigned(&burst, *argv, 0)) {
aba5acdf
SH
176 fprintf(stderr, "Illegal \"burst\"\n");
177 return -1;
178 }
179 ok++;
180 } else if (strcmp(*argv, "avpkt") == 0) {
181 NEXT_ARG();
182 if (get_size(&avpkt, *argv)) {
183 fprintf(stderr, "Illegal \"avpkt\"\n");
184 return -1;
185 }
186 ok++;
187 } else if (strcmp(*argv, "probability") == 0) {
188 NEXT_ARG();
189 if (sscanf(*argv, "%lg", &probability) != 1) {
190 fprintf(stderr, "Illegal \"probability\"\n");
191 return -1;
192 }
193 ok++;
194 } else if (strcmp(*argv, "prio") == 0) {
195 NEXT_ARG();
32a121cb 196 opt.prio = strtol(*argv, (char **)NULL, 10);
aba5acdf
SH
197 /* some error check here */
198 ok++;
199 } else if (strcmp(*argv, "bandwidth") == 0) {
200 NEXT_ARG();
927e3cfb
ND
201 if (strchr(*argv, '%')) {
202 if (get_percent_rate(&rate, *argv, dev)) {
203 fprintf(stderr, "Illegal \"bandwidth\"\n");
204 return -1;
205 }
206 } else if (get_rate(&rate, *argv)) {
aba5acdf
SH
207 fprintf(stderr, "Illegal \"bandwidth\"\n");
208 return -1;
209 }
210 ok++;
211 } else if (strcmp(*argv, "help") == 0) {
212 explain();
213 return -1;
214 } else {
215 fprintf(stderr, "What is \"%s\"?\n", *argv);
216 explain();
217 return -1;
218 }
219 argc--; argv++;
220 }
221
a77905ef
DW
222 if (!ok) {
223 explain();
224 return -1;
225 }
eb6d7d6a
DW
226 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
227 !avpkt) {
32a121cb 228 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
aba5acdf
SH
229 return -1;
230 }
ab15aeac
ED
231 if (!burst) {
232 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
233 fprintf(stderr, "GRED: set burst to %u\n", burst);
234 }
d93c909a
DW
235 if (!rate) {
236 get_rate(&rate, "10Mbit");
237 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
238 }
9d9a67c7 239 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
240 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
241 return -1;
242 }
9d9a67c7 243 if (parm >= 10)
32a121cb
SH
244 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
245 burst);
9d9a67c7
DW
246 opt.Wlog = parm;
247 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
248 fprintf(stderr, "GRED: failed to calculate probability.\n");
249 return -1;
250 }
9d9a67c7
DW
251 opt.Plog = parm;
252 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
aba5acdf 253 {
32a121cb 254 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
aba5acdf
SH
255 return -1;
256 }
9d9a67c7 257 opt.Scell_log = parm;
aba5acdf 258
c14f9d92 259 tail = addattr_nest(n, 1024, TCA_OPTIONS);
aba5acdf
SH
260 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
261 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
1b6f0bb5
ED
262 max_P = probability * pow(2, 32);
263 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
c14f9d92 264 addattr_nest_end(n, tail);
aba5acdf
SH
265 return 0;
266}
267
fdaff63c
JK
268struct tc_gred_info {
269 __u64 bytes;
270 __u32 packets;
271 __u32 backlog;
272 __u32 prob_drop;
273 __u32 prob_mark;
274 __u32 forced_drop;
275 __u32 forced_mark;
276 __u32 pdrop;
277 __u32 other;
278};
279
280static void
281gred_parse_vqs(struct tc_gred_info *info, struct rtattr *vqs)
c3e1cd28 282{
fdaff63c
JK
283 int rem = RTA_PAYLOAD(vqs);
284 unsigned int offset = 0;
285
286 while (rem > offset) {
287 struct rtattr *tb_entry[TCA_GRED_VQ_ENTRY_MAX + 1] = {};
288 struct rtattr *tb[TCA_GRED_VQ_MAX + 1] = {};
289 struct rtattr *entry;
290 unsigned int len;
291 unsigned int dp;
292
293 entry = RTA_DATA(vqs) + offset;
294
295 parse_rtattr(tb_entry, TCA_GRED_VQ_ENTRY_MAX, entry,
296 rem - offset);
297 len = RTA_LENGTH(RTA_PAYLOAD(entry));
298 offset += len;
299
300 if (!tb_entry[TCA_GRED_VQ_ENTRY]) {
301 fprintf(stderr,
302 "ERROR: Failed to parse Virtual Queue entry\n");
303 continue;
304 }
305
306 parse_rtattr_nested(tb, TCA_GRED_VQ_MAX,
307 tb_entry[TCA_GRED_VQ_ENTRY]);
308
309 if (!tb[TCA_GRED_VQ_DP]) {
310 fprintf(stderr,
311 "ERROR: Virtual Queue without DP attribute\n");
312 continue;
313 }
314
315 dp = rta_getattr_u32(tb[TCA_GRED_VQ_DP]);
316
317 if (tb[TCA_GRED_VQ_STAT_BYTES])
318 info[dp].bytes =
319 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BYTES]);
320 if (tb[TCA_GRED_VQ_STAT_PACKETS])
321 info[dp].packets =
322 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PACKETS]);
323 if (tb[TCA_GRED_VQ_STAT_BACKLOG])
324 info[dp].backlog =
325 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BACKLOG]);
326 if (tb[TCA_GRED_VQ_STAT_PROB_DROP])
327 info[dp].prob_drop =
328 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_DROP]);
329 if (tb[TCA_GRED_VQ_STAT_PROB_MARK])
330 info[dp].prob_mark =
331 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_MARK]);
332 if (tb[TCA_GRED_VQ_STAT_FORCED_DROP])
333 info[dp].forced_drop =
334 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_DROP]);
335 if (tb[TCA_GRED_VQ_STAT_FORCED_MARK])
336 info[dp].forced_mark =
337 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_MARK]);
338 if (tb[TCA_GRED_VQ_STAT_PDROP])
339 info[dp].pdrop =
340 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PDROP]);
341 if (tb[TCA_GRED_VQ_STAT_OTHER])
342 info[dp].other =
343 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_OTHER]);
344 }
345}
346
347static void
348gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
349{
350 __u64 bytes = info ? info->bytes : qopt->bytesin;
351
c3e1cd28
JK
352 SPRINT_BUF(b1);
353
354 if (!is_json_context())
355 printf("\n Queue size: ");
356
357 print_uint(PRINT_JSON, "qave", NULL, qopt->qave);
358 print_string(PRINT_FP, NULL, "average %s ",
359 sprint_size(qopt->qave, b1));
360
361 print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
362 print_string(PRINT_FP, NULL, "current %s ",
363 sprint_size(qopt->backlog, b1));
364
365 if (!is_json_context())
366 printf("\n Dropped packets: ");
367
fdaff63c
JK
368 if (info) {
369 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
370 info->forced_drop);
371 print_uint(PRINT_ANY, "prob_drop", "early %u ",
372 info->prob_drop);
373 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", info->pdrop);
374 print_uint(PRINT_ANY, "other", "other %u ", info->other);
375
376 if (!is_json_context())
377 printf("\n Marked packets: ");
378 print_uint(PRINT_ANY, "forced_mark", "forced %u ",
379 info->forced_mark);
380 print_uint(PRINT_ANY, "prob_mark", "early %u ",
381 info->prob_mark);
382 } else {
383 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
384 qopt->forced);
385 print_uint(PRINT_ANY, "prob_drop", "early %u ", qopt->early);
386 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", qopt->pdrop);
387 print_uint(PRINT_ANY, "other", "other %u ", qopt->other);
388 }
c3e1cd28
JK
389
390 if (!is_json_context())
391 printf("\n Total packets: ");
392
393 print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
394
fdaff63c
JK
395 print_uint(PRINT_JSON, "bytes", NULL, bytes);
396 print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1));
c3e1cd28
JK
397}
398
aba5acdf
SH
399static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
400{
fdaff63c 401 struct tc_gred_info infos[MAX_DPs] = {};
1b6f0bb5 402 struct rtattr *tb[TCA_GRED_MAX + 1];
1693a4d3 403 struct tc_gred_sopt *sopt;
aba5acdf 404 struct tc_gred_qopt *qopt;
fdaff63c 405 bool vq_info = false;
1b6f0bb5 406 __u32 *max_p = NULL;
aacee269 407 __u32 *limit = NULL;
32a121cb
SH
408 unsigned int i;
409
aba5acdf 410 SPRINT_BUF(b1);
aba5acdf
SH
411
412 if (opt == NULL)
413 return 0;
414
1b6f0bb5 415 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
aba5acdf
SH
416
417 if (tb[TCA_GRED_PARMS] == NULL)
418 return -1;
a5a6f1e8 419
1b6f0bb5
ED
420 if (tb[TCA_GRED_MAX_P] &&
421 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
422 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
423
aacee269
DW
424 if (tb[TCA_GRED_LIMIT] &&
425 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
426 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
427
1693a4d3 428 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
aba5acdf 429 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
1693a4d3
DW
430 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
431 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
32a121cb 432 fprintf(f, "\n GRED received message smaller than expected\n");
aba5acdf 433 return -1;
1693a4d3 434 }
ae665a52 435
fdaff63c
JK
436 if (tb[TCA_GRED_VQ_LIST] && show_stats) {
437 gred_parse_vqs(infos, tb[TCA_GRED_VQ_LIST]);
438 vq_info = true;
439 }
440
6475e6a5
JK
441 print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
442 print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
1693a4d3 443
6475e6a5
JK
444 if (sopt->grio)
445 print_bool(PRINT_ANY, "grio", "grio ", true);
446 else
447 print_bool(PRINT_ANY, "grio", NULL, false);
448
449 if (limit) {
450 print_uint(PRINT_JSON, "limit", NULL, *limit);
451 print_string(PRINT_FP, NULL, "limit %s ",
452 sprint_size(*limit, b1));
453 }
aacee269 454
6475e6a5 455 open_json_array(PRINT_JSON, "vqs");
32a121cb 456 for (i = 0; i < MAX_DPs; i++, qopt++) {
6475e6a5
JK
457 if (qopt->DP >= MAX_DPs)
458 continue;
459
460 open_json_object(NULL);
461
462 print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
463 print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
464
465 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
466 print_string(PRINT_FP, NULL, "limit %s ",
467 sprint_size(qopt->limit, b1));
468
469 print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
470 print_string(PRINT_FP, NULL, "min %s ",
471 sprint_size(qopt->qth_min, b1));
472
473 print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
474 print_string(PRINT_FP, NULL, "max %s ",
475 sprint_size(qopt->qth_max, b1));
476
1693a4d3 477 if (show_details) {
6475e6a5 478 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
1693a4d3 479 if (max_p)
6475e6a5
JK
480 print_float(PRINT_ANY, "probability",
481 "probability %lg ",
482 max_p[i] / pow(2, 32));
1693a4d3 483 else
6475e6a5
JK
484 print_uint(PRINT_ANY, "Plog", "Plog %u ",
485 qopt->Plog);
486 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u ",
487 qopt->Scell_log);
1693a4d3 488 }
c3e1cd28 489 if (show_stats)
fdaff63c 490 gred_print_stats(vq_info ? &infos[i] : NULL, qopt);
6475e6a5 491 close_json_object();
aba5acdf 492 }
6475e6a5 493 close_json_array(PRINT_JSON, "vqs");
aba5acdf
SH
494 return 0;
495}
496
95812b56 497struct qdisc_util gred_qdisc_util = {
f2f99e2e
SH
498 .id = "gred",
499 .parse_qopt = gred_parse_opt,
500 .print_qopt = gred_print_opt,
aba5acdf 501};