]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
tc: gred: support controlling RED flags
[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 ]\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 int ok = 0;
125 struct tc_gred_qopt opt = { 0 };
126 unsigned int burst = 0;
127 unsigned int avpkt = 0;
128 double probability = 0.02;
129 unsigned int rate = 0;
130 int parm;
131 __u8 sbuf[256];
132 struct rtattr *tail;
133 __u32 max_P;
134
135 opt.DP = MAX_DPs;
136
137 while (argc > 0) {
138 if (strcmp(*argv, "limit") == 0) {
139 NEXT_ARG();
140 if (get_size(&opt.limit, *argv)) {
141 fprintf(stderr, "Illegal \"limit\"\n");
142 return -1;
143 }
144 ok++;
145 } else if (strcmp(*argv, "setup") == 0) {
146 if (ok) {
147 fprintf(stderr, "Illegal \"setup\"\n");
148 return -1;
149 }
150 return init_gred(qu, argc-1, argv+1, n);
151 } else if (strcmp(*argv, "min") == 0) {
152 NEXT_ARG();
153 if (get_size(&opt.qth_min, *argv)) {
154 fprintf(stderr, "Illegal \"min\"\n");
155 return -1;
156 }
157 ok++;
158 } else if (strcmp(*argv, "max") == 0) {
159 NEXT_ARG();
160 if (get_size(&opt.qth_max, *argv)) {
161 fprintf(stderr, "Illegal \"max\"\n");
162 return -1;
163 }
164 ok++;
165 } else if (strcmp(*argv, "vq") == 0 ||
166 strcmp(*argv, "DP") == 0) {
167 NEXT_ARG();
168 if (get_unsigned(&opt.DP, *argv, 10)) {
169 fprintf(stderr, "Illegal \"vq\"\n");
170 return -1;
171 } else if (opt.DP >= MAX_DPs) {
172 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
173 MAX_DPs);
174 return -1;
175 } /* need a better error check */
176 ok++;
177 } else if (strcmp(*argv, "burst") == 0) {
178 NEXT_ARG();
179 if (get_unsigned(&burst, *argv, 0)) {
180 fprintf(stderr, "Illegal \"burst\"\n");
181 return -1;
182 }
183 ok++;
184 } else if (strcmp(*argv, "avpkt") == 0) {
185 NEXT_ARG();
186 if (get_size(&avpkt, *argv)) {
187 fprintf(stderr, "Illegal \"avpkt\"\n");
188 return -1;
189 }
190 ok++;
191 } else if (strcmp(*argv, "probability") == 0) {
192 NEXT_ARG();
193 if (sscanf(*argv, "%lg", &probability) != 1) {
194 fprintf(stderr, "Illegal \"probability\"\n");
195 return -1;
196 }
197 ok++;
198 } else if (strcmp(*argv, "prio") == 0) {
199 NEXT_ARG();
200 opt.prio = strtol(*argv, (char **)NULL, 10);
201 /* some error check here */
202 ok++;
203 } else if (strcmp(*argv, "bandwidth") == 0) {
204 NEXT_ARG();
205 if (strchr(*argv, '%')) {
206 if (get_percent_rate(&rate, *argv, dev)) {
207 fprintf(stderr, "Illegal \"bandwidth\"\n");
208 return -1;
209 }
210 } else if (get_rate(&rate, *argv)) {
211 fprintf(stderr, "Illegal \"bandwidth\"\n");
212 return -1;
213 }
214 ok++;
215 } else if (strcmp(*argv, "help") == 0) {
216 explain();
217 return -1;
218 } else {
219 fprintf(stderr, "What is \"%s\"?\n", *argv);
220 explain();
221 return -1;
222 }
223 argc--; argv++;
224 }
225
226 if (!ok) {
227 explain();
228 return -1;
229 }
230 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
231 !avpkt) {
232 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
233 return -1;
234 }
235 if (!burst) {
236 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
237 fprintf(stderr, "GRED: set burst to %u\n", burst);
238 }
239 if (!rate) {
240 get_rate(&rate, "10Mbit");
241 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
242 }
243 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
244 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
245 return -1;
246 }
247 if (parm >= 10)
248 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
249 burst);
250 opt.Wlog = parm;
251 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
252 fprintf(stderr, "GRED: failed to calculate probability.\n");
253 return -1;
254 }
255 opt.Plog = parm;
256 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
257 {
258 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
259 return -1;
260 }
261 opt.Scell_log = parm;
262
263 tail = addattr_nest(n, 1024, TCA_OPTIONS);
264 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
265 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
266 max_P = probability * pow(2, 32);
267 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
268 addattr_nest_end(n, tail);
269 return 0;
270 }
271
272 struct tc_gred_info {
273 __u64 bytes;
274 __u32 packets;
275 __u32 backlog;
276 __u32 prob_drop;
277 __u32 prob_mark;
278 __u32 forced_drop;
279 __u32 forced_mark;
280 __u32 pdrop;
281 __u32 other;
282 };
283
284 static void
285 gred_parse_vqs(struct tc_gred_info *info, struct rtattr *vqs)
286 {
287 int rem = RTA_PAYLOAD(vqs);
288 unsigned int offset = 0;
289
290 while (rem > offset) {
291 struct rtattr *tb_entry[TCA_GRED_VQ_ENTRY_MAX + 1] = {};
292 struct rtattr *tb[TCA_GRED_VQ_MAX + 1] = {};
293 struct rtattr *entry;
294 unsigned int len;
295 unsigned int dp;
296
297 entry = RTA_DATA(vqs) + offset;
298
299 parse_rtattr(tb_entry, TCA_GRED_VQ_ENTRY_MAX, entry,
300 rem - offset);
301 len = RTA_LENGTH(RTA_PAYLOAD(entry));
302 offset += len;
303
304 if (!tb_entry[TCA_GRED_VQ_ENTRY]) {
305 fprintf(stderr,
306 "ERROR: Failed to parse Virtual Queue entry\n");
307 continue;
308 }
309
310 parse_rtattr_nested(tb, TCA_GRED_VQ_MAX,
311 tb_entry[TCA_GRED_VQ_ENTRY]);
312
313 if (!tb[TCA_GRED_VQ_DP]) {
314 fprintf(stderr,
315 "ERROR: Virtual Queue without DP attribute\n");
316 continue;
317 }
318
319 dp = rta_getattr_u32(tb[TCA_GRED_VQ_DP]);
320
321 if (tb[TCA_GRED_VQ_STAT_BYTES])
322 info[dp].bytes =
323 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BYTES]);
324 if (tb[TCA_GRED_VQ_STAT_PACKETS])
325 info[dp].packets =
326 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PACKETS]);
327 if (tb[TCA_GRED_VQ_STAT_BACKLOG])
328 info[dp].backlog =
329 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BACKLOG]);
330 if (tb[TCA_GRED_VQ_STAT_PROB_DROP])
331 info[dp].prob_drop =
332 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_DROP]);
333 if (tb[TCA_GRED_VQ_STAT_PROB_MARK])
334 info[dp].prob_mark =
335 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_MARK]);
336 if (tb[TCA_GRED_VQ_STAT_FORCED_DROP])
337 info[dp].forced_drop =
338 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_DROP]);
339 if (tb[TCA_GRED_VQ_STAT_FORCED_MARK])
340 info[dp].forced_mark =
341 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_MARK]);
342 if (tb[TCA_GRED_VQ_STAT_PDROP])
343 info[dp].pdrop =
344 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PDROP]);
345 if (tb[TCA_GRED_VQ_STAT_OTHER])
346 info[dp].other =
347 rta_getattr_u32(tb[TCA_GRED_VQ_STAT_OTHER]);
348 }
349 }
350
351 static void
352 gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
353 {
354 __u64 bytes = info ? info->bytes : qopt->bytesin;
355
356 SPRINT_BUF(b1);
357
358 if (!is_json_context())
359 printf("\n Queue size: ");
360
361 print_uint(PRINT_JSON, "qave", NULL, qopt->qave);
362 print_string(PRINT_FP, NULL, "average %s ",
363 sprint_size(qopt->qave, b1));
364
365 print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
366 print_string(PRINT_FP, NULL, "current %s ",
367 sprint_size(qopt->backlog, b1));
368
369 if (!is_json_context())
370 printf("\n Dropped packets: ");
371
372 if (info) {
373 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
374 info->forced_drop);
375 print_uint(PRINT_ANY, "prob_drop", "early %u ",
376 info->prob_drop);
377 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", info->pdrop);
378 print_uint(PRINT_ANY, "other", "other %u ", info->other);
379
380 if (!is_json_context())
381 printf("\n Marked packets: ");
382 print_uint(PRINT_ANY, "forced_mark", "forced %u ",
383 info->forced_mark);
384 print_uint(PRINT_ANY, "prob_mark", "early %u ",
385 info->prob_mark);
386 } else {
387 print_uint(PRINT_ANY, "forced_drop", "forced %u ",
388 qopt->forced);
389 print_uint(PRINT_ANY, "prob_drop", "early %u ", qopt->early);
390 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", qopt->pdrop);
391 print_uint(PRINT_ANY, "other", "other %u ", qopt->other);
392 }
393
394 if (!is_json_context())
395 printf("\n Total packets: ");
396
397 print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
398
399 print_uint(PRINT_JSON, "bytes", NULL, bytes);
400 print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1));
401 }
402
403 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
404 {
405 struct tc_gred_info infos[MAX_DPs] = {};
406 struct rtattr *tb[TCA_GRED_MAX + 1];
407 struct tc_gred_sopt *sopt;
408 struct tc_gred_qopt *qopt;
409 bool vq_info = false;
410 __u32 *max_p = NULL;
411 __u32 *limit = NULL;
412 unsigned int i;
413
414 SPRINT_BUF(b1);
415
416 if (opt == NULL)
417 return 0;
418
419 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
420
421 if (tb[TCA_GRED_PARMS] == NULL)
422 return -1;
423
424 if (tb[TCA_GRED_MAX_P] &&
425 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
426 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
427
428 if (tb[TCA_GRED_LIMIT] &&
429 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
430 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
431
432 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
433 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
434 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
435 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
436 fprintf(f, "\n GRED received message smaller than expected\n");
437 return -1;
438 }
439
440 if (tb[TCA_GRED_VQ_LIST] && show_stats) {
441 gred_parse_vqs(infos, tb[TCA_GRED_VQ_LIST]);
442 vq_info = true;
443 }
444
445 print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
446 print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
447
448 if (sopt->grio)
449 print_bool(PRINT_ANY, "grio", "grio ", true);
450 else
451 print_bool(PRINT_ANY, "grio", NULL, false);
452
453 if (limit) {
454 print_uint(PRINT_JSON, "limit", NULL, *limit);
455 print_string(PRINT_FP, NULL, "limit %s ",
456 sprint_size(*limit, b1));
457 }
458
459 tc_red_print_flags(sopt->flags);
460
461 open_json_array(PRINT_JSON, "vqs");
462 for (i = 0; i < MAX_DPs; i++, qopt++) {
463 if (qopt->DP >= MAX_DPs)
464 continue;
465
466 open_json_object(NULL);
467
468 print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
469 print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
470
471 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
472 print_string(PRINT_FP, NULL, "limit %s ",
473 sprint_size(qopt->limit, b1));
474
475 print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
476 print_string(PRINT_FP, NULL, "min %s ",
477 sprint_size(qopt->qth_min, b1));
478
479 print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
480 print_string(PRINT_FP, NULL, "max %s ",
481 sprint_size(qopt->qth_max, b1));
482
483 if (show_details) {
484 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
485 if (max_p)
486 print_float(PRINT_ANY, "probability",
487 "probability %lg ",
488 max_p[i] / pow(2, 32));
489 else
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);
494 }
495 if (show_stats)
496 gred_print_stats(vq_info ? &infos[i] : NULL, qopt);
497 close_json_object();
498 }
499 close_json_array(PRINT_JSON, "vqs");
500 return 0;
501 }
502
503 struct qdisc_util gred_qdisc_util = {
504 .id = "gred",
505 .parse_qopt = gred_parse_opt,
506 .print_qopt = gred_print_opt,
507 };