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