]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
bridge: fdb: add support for src_vni option
[mirror_iproute2.git] / tc / q_gred.c
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 *
9 * Authors: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 *
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <string.h>
23 #include <math.h>
24
25 #include "utils.h"
26 #include "tc_util.h"
27
28 #include "tc_red.h"
29
30
31 #if 0
32 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
33 #else
34 #define DPRINTF(format, args...)
35 #endif
36
37 static void explain(void)
38 {
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");
44 }
45
46 static int init_gred(struct qdisc_util *qu, int argc, char **argv,
47 struct nlmsghdr *n)
48 {
49
50 struct rtattr *tail;
51 struct tc_gred_sopt opt = { 0 };
52 __u32 limit = 0;
53
54 opt.def_DP = MAX_DPs;
55
56 while (argc > 0) {
57 DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
58 if (strcmp(*argv, "vqs") == 0 ||
59 strcmp(*argv, "DPs") == 0) {
60 NEXT_ARG();
61 if (get_unsigned(&opt.DPs, *argv, 10)) {
62 fprintf(stderr, "Illegal \"vqs\"\n");
63 return -1;
64 } else if (opt.DPs > MAX_DPs) {
65 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
66 MAX_DPs);
67 return -1;
68 }
69 } else if (strcmp(*argv, "default") == 0) {
70 if (opt.DPs == 0) {
71 fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
72 return -1;
73 }
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) {
79 fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
80 return -1;
81 }
82 } else if (strcmp(*argv, "grio") == 0) {
83 opt.grio = 1;
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 }
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) {
95 explain();
96 return -1;
97 } else {
98 fprintf(stderr, "What is \"%s\"?\n", *argv);
99 explain();
100 return -1;
101 }
102 argc--; argv++;
103 }
104
105 if (!opt.DPs || opt.def_DP == MAX_DPs) {
106 fprintf(stderr, "Illegal gred setup parameters\n");
107 return -1;
108 }
109
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));
114 if (limit)
115 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
116 addattr_nest_end(n, tail);
117 return 0;
118 }
119 /*
120 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121 */
122 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
123 {
124 struct rtattr *tail, *entry, *vqs;
125 int ok = 0;
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;
132 int parm;
133 __u8 sbuf[256];
134 __u32 max_P;
135
136 opt.DP = MAX_DPs;
137
138 while (argc > 0) {
139 if (strcmp(*argv, "limit") == 0) {
140 NEXT_ARG();
141 if (get_size(&opt.limit, *argv)) {
142 fprintf(stderr, "Illegal \"limit\"\n");
143 return -1;
144 }
145 ok++;
146 } else if (strcmp(*argv, "setup") == 0) {
147 if (ok) {
148 fprintf(stderr, "Illegal \"setup\"\n");
149 return -1;
150 }
151 return init_gred(qu, argc-1, argv+1, n);
152 } else if (strcmp(*argv, "min") == 0) {
153 NEXT_ARG();
154 if (get_size(&opt.qth_min, *argv)) {
155 fprintf(stderr, "Illegal \"min\"\n");
156 return -1;
157 }
158 ok++;
159 } else if (strcmp(*argv, "max") == 0) {
160 NEXT_ARG();
161 if (get_size(&opt.qth_max, *argv)) {
162 fprintf(stderr, "Illegal \"max\"\n");
163 return -1;
164 }
165 ok++;
166 } else if (strcmp(*argv, "vq") == 0 ||
167 strcmp(*argv, "DP") == 0) {
168 NEXT_ARG();
169 if (get_unsigned(&opt.DP, *argv, 10)) {
170 fprintf(stderr, "Illegal \"vq\"\n");
171 return -1;
172 } else if (opt.DP >= MAX_DPs) {
173 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
174 MAX_DPs);
175 return -1;
176 } /* need a better error check */
177 ok++;
178 } else if (strcmp(*argv, "burst") == 0) {
179 NEXT_ARG();
180 if (get_unsigned(&burst, *argv, 0)) {
181 fprintf(stderr, "Illegal \"burst\"\n");
182 return -1;
183 }
184 ok++;
185 } else if (strcmp(*argv, "avpkt") == 0) {
186 NEXT_ARG();
187 if (get_size(&avpkt, *argv)) {
188 fprintf(stderr, "Illegal \"avpkt\"\n");
189 return -1;
190 }
191 ok++;
192 } else if (strcmp(*argv, "probability") == 0) {
193 NEXT_ARG();
194 if (sscanf(*argv, "%lg", &probability) != 1) {
195 fprintf(stderr, "Illegal \"probability\"\n");
196 return -1;
197 }
198 ok++;
199 } else if (strcmp(*argv, "prio") == 0) {
200 NEXT_ARG();
201 opt.prio = strtol(*argv, (char **)NULL, 10);
202 /* some error check here */
203 ok++;
204 } else if (strcmp(*argv, "bandwidth") == 0) {
205 NEXT_ARG();
206 if (strchr(*argv, '%')) {
207 if (get_percent_rate(&rate, *argv, dev)) {
208 fprintf(stderr, "Illegal \"bandwidth\"\n");
209 return -1;
210 }
211 } else if (get_rate(&rate, *argv)) {
212 fprintf(stderr, "Illegal \"bandwidth\"\n");
213 return -1;
214 }
215 ok++;
216 } else if (strcmp(*argv, "ecn") == 0) {
217 flags |= TC_RED_ECN;
218 } else if (strcmp(*argv, "harddrop") == 0) {
219 flags |= TC_RED_HARDDROP;
220 } else if (strcmp(*argv, "help") == 0) {
221 explain();
222 return -1;
223 } else {
224 fprintf(stderr, "What is \"%s\"?\n", *argv);
225 explain();
226 return -1;
227 }
228 argc--; argv++;
229 }
230
231 if (!ok) {
232 explain();
233 return -1;
234 }
235 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
236 !avpkt) {
237 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
238 return -1;
239 }
240 if (!burst) {
241 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
242 fprintf(stderr, "GRED: set burst to %u\n", burst);
243 }
244 if (!rate) {
245 get_rate(&rate, "10Mbit");
246 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
247 }
248 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
249 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
250 return -1;
251 }
252 if (parm >= 10)
253 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
254 burst);
255 opt.Wlog = parm;
256 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
257 fprintf(stderr, "GRED: failed to calculate probability.\n");
258 return -1;
259 }
260 opt.Plog = parm;
261 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
262 {
263 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
264 return -1;
265 }
266 opt.Scell_log = parm;
267
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);
273
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);
280
281 addattr_nest_end(n, tail);
282 return 0;
283 }
284
285 struct tc_gred_info {
286 bool flags_present;
287 __u64 bytes;
288 __u32 packets;
289 __u32 backlog;
290 __u32 prob_drop;
291 __u32 prob_mark;
292 __u32 forced_drop;
293 __u32 forced_mark;
294 __u32 pdrop;
295 __u32 other;
296 __u32 flags;
297 };
298
299 static void
300 gred_parse_vqs(struct tc_gred_info *info, struct rtattr *vqs)
301 {
302 int rem = RTA_PAYLOAD(vqs);
303 unsigned int offset = 0;
304
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;
309 unsigned int len;
310 unsigned int dp;
311
312 entry = RTA_DATA(vqs) + offset;
313
314 parse_rtattr(tb_entry, TCA_GRED_VQ_ENTRY_MAX, entry,
315 rem - offset);
316 len = RTA_LENGTH(RTA_PAYLOAD(entry));
317 offset += len;
318
319 if (!tb_entry[TCA_GRED_VQ_ENTRY]) {
320 fprintf(stderr,
321 "ERROR: Failed to parse Virtual Queue entry\n");
322 continue;
323 }
324
325 parse_rtattr_nested(tb, TCA_GRED_VQ_MAX,
326 tb_entry[TCA_GRED_VQ_ENTRY]);
327
328 if (!tb[TCA_GRED_VQ_DP]) {
329 fprintf(stderr,
330 "ERROR: Virtual Queue without DP attribute\n");
331 continue;
332 }
333
334 dp = rta_getattr_u32(tb[TCA_GRED_VQ_DP]);
335
336 if (tb[TCA_GRED_VQ_STAT_BYTES])
337 info[dp].bytes =
338 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BYTES]);
339 if (tb[TCA_GRED_VQ_STAT_PACKETS])
340 info[dp].packets =
341 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PACKETS]);
342 if (tb[TCA_GRED_VQ_STAT_BACKLOG])
343 info[dp].backlog =
344 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BACKLOG]);
345 if (tb[TCA_GRED_VQ_STAT_PROB_DROP])
346 info[dp].prob_drop =
347 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_DROP]);
348 if (tb[TCA_GRED_VQ_STAT_PROB_MARK])
349 info[dp].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])
358 info[dp].pdrop =
359 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PDROP]);
360 if (tb[TCA_GRED_VQ_STAT_OTHER])
361 info[dp].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])
365 info[dp].flags =
366 rta_getattr_u32(tb[TCA_GRED_VQ_FLAGS]);
367 }
368 }
369
370 static void
371 gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
372 {
373 __u64 bytes = info ? info->bytes : qopt->bytesin;
374
375 SPRINT_BUF(b1);
376
377 if (!is_json_context())
378 printf("\n Queue size: ");
379
380 print_uint(PRINT_JSON, "qave", NULL, qopt->qave);
381 print_string(PRINT_FP, NULL, "average %s ",
382 sprint_size(qopt->qave, b1));
383
384 print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
385 print_string(PRINT_FP, NULL, "current %s ",
386 sprint_size(qopt->backlog, b1));
387
388 if (!is_json_context())
389 printf("\n Dropped packets: ");
390
391 if (info) {
392 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
393 info->forced_drop);
394 print_uint(PRINT_ANY, "prob_drop", "early %u ",
395 info->prob_drop);
396 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", info->pdrop);
397 print_uint(PRINT_ANY, "other", "other %u ", info->other);
398
399 if (!is_json_context())
400 printf("\n Marked packets: ");
401 print_uint(PRINT_ANY, "forced_mark", "forced %u ",
402 info->forced_mark);
403 print_uint(PRINT_ANY, "prob_mark", "early %u ",
404 info->prob_mark);
405 } else {
406 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
407 qopt->forced);
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);
411 }
412
413 if (!is_json_context())
414 printf("\n Total packets: ");
415
416 print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
417
418 print_uint(PRINT_JSON, "bytes", NULL, bytes);
419 print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1));
420 }
421
422 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
423 {
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;
429 __u32 *max_p = NULL;
430 __u32 *limit = NULL;
431 unsigned int i;
432
433 SPRINT_BUF(b1);
434
435 if (opt == NULL)
436 return 0;
437
438 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
439
440 if (tb[TCA_GRED_PARMS] == NULL)
441 return -1;
442
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]);
446
447 if (tb[TCA_GRED_LIMIT] &&
448 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
449 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
450
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");
456 return -1;
457 }
458
459 if (tb[TCA_GRED_VQ_LIST]) {
460 gred_parse_vqs(infos, tb[TCA_GRED_VQ_LIST]);
461 vq_info = true;
462 }
463
464 print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
465 print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
466
467 if (sopt->grio)
468 print_bool(PRINT_ANY, "grio", "grio ", true);
469 else
470 print_bool(PRINT_ANY, "grio", NULL, false);
471
472 if (limit) {
473 print_uint(PRINT_JSON, "limit", NULL, *limit);
474 print_string(PRINT_FP, NULL, "limit %s ",
475 sprint_size(*limit, b1));
476 }
477
478 tc_red_print_flags(sopt->flags);
479
480 open_json_array(PRINT_JSON, "vqs");
481 for (i = 0; i < MAX_DPs; i++, qopt++) {
482 if (qopt->DP >= MAX_DPs)
483 continue;
484
485 open_json_object(NULL);
486
487 print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
488 print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
489
490 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
491 print_string(PRINT_FP, NULL, "limit %s ",
492 sprint_size(qopt->limit, b1));
493
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));
497
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));
501
502 if (infos[i].flags_present)
503 tc_red_print_flags(infos[i].flags);
504
505 if (show_details) {
506 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
507 if (max_p)
508 print_float(PRINT_ANY, "probability",
509 "probability %lg ",
510 max_p[i] / pow(2, 32));
511 else
512 print_uint(PRINT_ANY, "Plog", "Plog %u ",
513 qopt->Plog);
514 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u ",
515 qopt->Scell_log);
516 }
517 if (show_stats)
518 gred_print_stats(vq_info ? &infos[i] : NULL, qopt);
519 close_json_object();
520 }
521 close_json_array(PRINT_JSON, "vqs");
522 return 0;
523 }
524
525 struct qdisc_util gred_qdisc_util = {
526 .id = "gred",
527 .parse_qopt = gred_parse_opt,
528 .print_qopt = gred_print_opt,
529 };