]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_cbq.c
tc: code cleanup
[mirror_iproute2.git] / tc / q_cbq.c
1 /*
2 * q_cbq.c CBQ.
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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25 #include "tc_cbq.h"
26
27 static void explain_class(void)
28 {
29 fprintf(stderr, "Usage: ... cbq bandwidth BPS rate BPS maxburst PKTS [ avpkt BYTES ]\n");
30 fprintf(stderr, " [ minburst PKTS ] [ bounded ] [ isolated ]\n");
31 fprintf(stderr, " [ allot BYTES ] [ mpu BYTES ] [ weight RATE ]\n");
32 fprintf(stderr, " [ prio NUMBER ] [ cell BYTES ] [ ewma LOG ]\n");
33 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
34 fprintf(stderr, " [ split CLASSID ] [ defmap MASK/CHANGE ]\n");
35 fprintf(stderr, " [ overhead BYTES ] [ linklayer TYPE ]\n");
36 }
37
38 static void explain(void)
39 {
40 fprintf(stderr, "Usage: ... cbq bandwidth BPS avpkt BYTES [ mpu BYTES ]\n");
41 fprintf(stderr, " [ cell BYTES ] [ ewma LOG ]\n");
42 }
43
44 static void explain1(char *arg)
45 {
46 fprintf(stderr, "Illegal \"%s\"\n", arg);
47 }
48
49
50 static int cbq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
51 {
52 struct tc_ratespec r;
53 struct tc_cbq_lssopt lss;
54 __u32 rtab[256];
55 unsigned mpu = 0, avpkt = 0, allot = 0;
56 unsigned short overhead = 0;
57 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
58 int cell_log = -1;
59 int ewma_log = -1;
60 struct rtattr *tail;
61
62 memset(&lss, 0, sizeof(lss));
63 memset(&r, 0, sizeof(r));
64
65 while (argc > 0) {
66 if (matches(*argv, "bandwidth") == 0 ||
67 matches(*argv, "rate") == 0) {
68 NEXT_ARG();
69 if (get_rate(&r.rate, *argv)) {
70 explain1("bandwidth");
71 return -1;
72 }
73 } else if (matches(*argv, "ewma") == 0) {
74 NEXT_ARG();
75 if (get_integer(&ewma_log, *argv, 0)) {
76 explain1("ewma");
77 return -1;
78 }
79 if (ewma_log > 31) {
80 fprintf(stderr, "ewma_log must be < 32\n");
81 return -1;
82 }
83 } else if (matches(*argv, "cell") == 0) {
84 unsigned int cell;
85 int i;
86
87 NEXT_ARG();
88 if (get_size(&cell, *argv)) {
89 explain1("cell");
90 return -1;
91 }
92 for (i = 0; i < 32; i++)
93 if ((1<<i) == cell)
94 break;
95 if (i >= 32) {
96 fprintf(stderr, "cell must be 2^n\n");
97 return -1;
98 }
99 cell_log = i;
100 } else if (matches(*argv, "avpkt") == 0) {
101 NEXT_ARG();
102 if (get_size(&avpkt, *argv)) {
103 explain1("avpkt");
104 return -1;
105 }
106 } else if (matches(*argv, "mpu") == 0) {
107 NEXT_ARG();
108 if (get_size(&mpu, *argv)) {
109 explain1("mpu");
110 return -1;
111 }
112 } else if (matches(*argv, "allot") == 0) {
113 NEXT_ARG();
114 /* Accept and ignore "allot" for backward compatibility */
115 if (get_size(&allot, *argv)) {
116 explain1("allot");
117 return -1;
118 }
119 } else if (matches(*argv, "overhead") == 0) {
120 NEXT_ARG();
121 if (get_u16(&overhead, *argv, 10)) {
122 explain1("overhead"); return -1;
123 }
124 } else if (matches(*argv, "linklayer") == 0) {
125 NEXT_ARG();
126 if (get_linklayer(&linklayer, *argv)) {
127 explain1("linklayer"); return -1;
128 }
129 } else if (matches(*argv, "help") == 0) {
130 explain();
131 return -1;
132 } else {
133 fprintf(stderr, "What is \"%s\"?\n", *argv);
134 explain();
135 return -1;
136 }
137 argc--; argv++;
138 }
139
140 /* OK. All options are parsed. */
141
142 if (r.rate == 0) {
143 fprintf(stderr, "CBQ: bandwidth is required parameter.\n");
144 return -1;
145 }
146 if (avpkt == 0) {
147 fprintf(stderr, "CBQ: \"avpkt\" is required.\n");
148 return -1;
149 }
150 if (allot < (avpkt*3)/2)
151 allot = (avpkt*3)/2;
152
153 r.mpu = mpu;
154 r.overhead = overhead;
155 if (tc_calc_rtable(&r, rtab, cell_log, allot, linklayer) < 0) {
156 fprintf(stderr, "CBQ: failed to calculate rate table.\n");
157 return -1;
158 }
159
160 if (ewma_log < 0)
161 ewma_log = TC_CBQ_DEF_EWMA;
162 lss.ewma_log = ewma_log;
163 lss.maxidle = tc_calc_xmittime(r.rate, avpkt);
164 lss.change = TCF_CBQ_LSS_MAXIDLE|TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
165 lss.avpkt = avpkt;
166
167 tail = NLMSG_TAIL(n);
168 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
169 addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r));
170 addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss));
171 addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024);
172 if (show_raw) {
173 int i;
174
175 for (i = 0; i < 256; i++)
176 printf("%u ", rtab[i]);
177 printf("\n");
178 }
179 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
180 return 0;
181 }
182
183 static int cbq_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
184 {
185 int wrr_ok = 0, fopt_ok = 0;
186 struct tc_ratespec r;
187 struct tc_cbq_lssopt lss;
188 struct tc_cbq_wrropt wrr;
189 struct tc_cbq_fopt fopt;
190 struct tc_cbq_ovl ovl;
191 __u32 rtab[256];
192 unsigned mpu = 0;
193 int cell_log = -1;
194 int ewma_log = -1;
195 unsigned int bndw = 0;
196 unsigned minburst = 0, maxburst = 0;
197 unsigned short overhead = 0;
198 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
199 struct rtattr *tail;
200
201 memset(&r, 0, sizeof(r));
202 memset(&lss, 0, sizeof(lss));
203 memset(&wrr, 0, sizeof(wrr));
204 memset(&fopt, 0, sizeof(fopt));
205 memset(&ovl, 0, sizeof(ovl));
206
207 while (argc > 0) {
208 if (matches(*argv, "rate") == 0) {
209 NEXT_ARG();
210 if (get_rate(&r.rate, *argv)) {
211 explain1("rate");
212 return -1;
213 }
214 } else if (matches(*argv, "bandwidth") == 0) {
215 NEXT_ARG();
216 if (get_rate(&bndw, *argv)) {
217 explain1("bandwidth");
218 return -1;
219 }
220 } else if (matches(*argv, "minidle") == 0) {
221 NEXT_ARG();
222 if (get_u32(&lss.minidle, *argv, 0)) {
223 explain1("minidle");
224 return -1;
225 }
226 lss.change |= TCF_CBQ_LSS_MINIDLE;
227 } else if (matches(*argv, "minburst") == 0) {
228 NEXT_ARG();
229 if (get_u32(&minburst, *argv, 0)) {
230 explain1("minburst");
231 return -1;
232 }
233 lss.change |= TCF_CBQ_LSS_OFFTIME;
234 } else if (matches(*argv, "maxburst") == 0) {
235 NEXT_ARG();
236 if (get_u32(&maxburst, *argv, 0)) {
237 explain1("maxburst");
238 return -1;
239 }
240 lss.change |= TCF_CBQ_LSS_MAXIDLE;
241 } else if (matches(*argv, "bounded") == 0) {
242 lss.flags |= TCF_CBQ_LSS_BOUNDED;
243 lss.change |= TCF_CBQ_LSS_FLAGS;
244 } else if (matches(*argv, "borrow") == 0) {
245 lss.flags &= ~TCF_CBQ_LSS_BOUNDED;
246 lss.change |= TCF_CBQ_LSS_FLAGS;
247 } else if (matches(*argv, "isolated") == 0) {
248 lss.flags |= TCF_CBQ_LSS_ISOLATED;
249 lss.change |= TCF_CBQ_LSS_FLAGS;
250 } else if (matches(*argv, "sharing") == 0) {
251 lss.flags &= ~TCF_CBQ_LSS_ISOLATED;
252 lss.change |= TCF_CBQ_LSS_FLAGS;
253 } else if (matches(*argv, "ewma") == 0) {
254 NEXT_ARG();
255 if (get_integer(&ewma_log, *argv, 0)) {
256 explain1("ewma");
257 return -1;
258 }
259 if (ewma_log > 31) {
260 fprintf(stderr, "ewma_log must be < 32\n");
261 return -1;
262 }
263 lss.change |= TCF_CBQ_LSS_EWMA;
264 } else if (matches(*argv, "cell") == 0) {
265 unsigned int cell;
266 int i;
267
268 NEXT_ARG();
269 if (get_size(&cell, *argv)) {
270 explain1("cell");
271 return -1;
272 }
273 for (i = 0; i < 32; i++)
274 if ((1<<i) == cell)
275 break;
276 if (i >= 32) {
277 fprintf(stderr, "cell must be 2^n\n");
278 return -1;
279 }
280 cell_log = i;
281 } else if (matches(*argv, "prio") == 0) {
282 unsigned int prio;
283
284 NEXT_ARG();
285 if (get_u32(&prio, *argv, 0)) {
286 explain1("prio");
287 return -1;
288 }
289 if (prio > TC_CBQ_MAXPRIO) {
290 fprintf(stderr, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO);
291 return -1;
292 }
293 wrr.priority = prio;
294 wrr_ok++;
295 } else if (matches(*argv, "allot") == 0) {
296 NEXT_ARG();
297 if (get_size(&wrr.allot, *argv)) {
298 explain1("allot");
299 return -1;
300 }
301 } else if (matches(*argv, "avpkt") == 0) {
302 NEXT_ARG();
303 if (get_size(&lss.avpkt, *argv)) {
304 explain1("avpkt");
305 return -1;
306 }
307 lss.change |= TCF_CBQ_LSS_AVPKT;
308 } else if (matches(*argv, "mpu") == 0) {
309 NEXT_ARG();
310 if (get_size(&mpu, *argv)) {
311 explain1("mpu");
312 return -1;
313 }
314 } else if (matches(*argv, "weight") == 0) {
315 NEXT_ARG();
316 if (get_size(&wrr.weight, *argv)) {
317 explain1("weight");
318 return -1;
319 }
320 wrr_ok++;
321 } else if (matches(*argv, "split") == 0) {
322 NEXT_ARG();
323 if (get_tc_classid(&fopt.split, *argv)) {
324 fprintf(stderr, "Invalid split node ID.\n");
325 return -1;
326 }
327 fopt_ok++;
328 } else if (matches(*argv, "defmap") == 0) {
329 int err;
330
331 NEXT_ARG();
332 err = sscanf(*argv, "%08x/%08x", &fopt.defmap, &fopt.defchange);
333 if (err < 1) {
334 fprintf(stderr, "Invalid defmap, should be MASK32[/MASK]\n");
335 return -1;
336 }
337 if (err == 1)
338 fopt.defchange = ~0;
339 fopt_ok++;
340 } else if (matches(*argv, "overhead") == 0) {
341 NEXT_ARG();
342 if (get_u16(&overhead, *argv, 10)) {
343 explain1("overhead"); return -1;
344 }
345 } else if (matches(*argv, "linklayer") == 0) {
346 NEXT_ARG();
347 if (get_linklayer(&linklayer, *argv)) {
348 explain1("linklayer"); return -1;
349 }
350 } else if (matches(*argv, "help") == 0) {
351 explain_class();
352 return -1;
353 } else {
354 fprintf(stderr, "What is \"%s\"?\n", *argv);
355 explain_class();
356 return -1;
357 }
358 argc--; argv++;
359 }
360
361 /* OK. All options are parsed. */
362
363 /* 1. Prepare link sharing scheduler parameters */
364 if (r.rate) {
365 unsigned int pktsize = wrr.allot;
366
367 if (wrr.allot < (lss.avpkt*3)/2)
368 wrr.allot = (lss.avpkt*3)/2;
369 r.mpu = mpu;
370 r.overhead = overhead;
371 if (tc_calc_rtable(&r, rtab, cell_log, pktsize, linklayer) < 0) {
372 fprintf(stderr, "CBQ: failed to calculate rate table.\n");
373 return -1;
374 }
375 }
376 if (ewma_log < 0)
377 ewma_log = TC_CBQ_DEF_EWMA;
378 lss.ewma_log = ewma_log;
379 if (lss.change&(TCF_CBQ_LSS_OFFTIME|TCF_CBQ_LSS_MAXIDLE)) {
380 if (lss.avpkt == 0) {
381 fprintf(stderr, "CBQ: avpkt is required for max/minburst.\n");
382 return -1;
383 }
384 if (bndw == 0 || r.rate == 0) {
385 fprintf(stderr, "CBQ: bandwidth&rate are required for max/minburst.\n");
386 return -1;
387 }
388 }
389 if (wrr.priority == 0 && (n->nlmsg_flags&NLM_F_EXCL)) {
390 wrr_ok = 1;
391 wrr.priority = TC_CBQ_MAXPRIO;
392 if (wrr.allot == 0)
393 wrr.allot = (lss.avpkt*3)/2;
394 }
395 if (wrr_ok) {
396 if (wrr.weight == 0)
397 wrr.weight = (wrr.priority == TC_CBQ_MAXPRIO) ? 1 : r.rate;
398 if (wrr.allot == 0) {
399 fprintf(stderr, "CBQ: \"allot\" is required to set WRR parameters.\n");
400 return -1;
401 }
402 }
403 if (lss.change&TCF_CBQ_LSS_MAXIDLE) {
404 lss.maxidle = tc_cbq_calc_maxidle(bndw, r.rate, lss.avpkt, ewma_log, maxburst);
405 lss.change |= TCF_CBQ_LSS_MAXIDLE;
406 lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
407 }
408 if (lss.change&TCF_CBQ_LSS_OFFTIME) {
409 lss.offtime = tc_cbq_calc_offtime(bndw, r.rate, lss.avpkt, ewma_log, minburst);
410 lss.change |= TCF_CBQ_LSS_OFFTIME;
411 lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
412 }
413 if (lss.change&TCF_CBQ_LSS_MINIDLE) {
414 lss.minidle <<= lss.ewma_log;
415 lss.change |= TCF_CBQ_LSS_EWMA;
416 }
417
418 tail = NLMSG_TAIL(n);
419 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
420 if (lss.change) {
421 lss.change |= TCF_CBQ_LSS_FLAGS;
422 addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss));
423 }
424 if (wrr_ok)
425 addattr_l(n, 1024, TCA_CBQ_WRROPT, &wrr, sizeof(wrr));
426 if (fopt_ok)
427 addattr_l(n, 1024, TCA_CBQ_FOPT, &fopt, sizeof(fopt));
428 if (r.rate) {
429 addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r));
430 addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024);
431 if (show_raw) {
432 int i;
433
434 for (i = 0; i < 256; i++)
435 printf("%u ", rtab[i]);
436 printf("\n");
437 }
438 }
439 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
440 return 0;
441 }
442
443
444 static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
445 {
446 struct rtattr *tb[TCA_CBQ_MAX+1];
447 struct tc_ratespec *r = NULL;
448 struct tc_cbq_lssopt *lss = NULL;
449 struct tc_cbq_wrropt *wrr = NULL;
450 struct tc_cbq_fopt *fopt = NULL;
451 struct tc_cbq_ovl *ovl = NULL;
452 unsigned int linklayer;
453
454 SPRINT_BUF(b1);
455 SPRINT_BUF(b2);
456
457 if (opt == NULL)
458 return 0;
459
460 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
461
462 if (tb[TCA_CBQ_RATE]) {
463 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
464 fprintf(stderr, "CBQ: too short rate opt\n");
465 else
466 r = RTA_DATA(tb[TCA_CBQ_RATE]);
467 }
468 if (tb[TCA_CBQ_LSSOPT]) {
469 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
470 fprintf(stderr, "CBQ: too short lss opt\n");
471 else
472 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
473 }
474 if (tb[TCA_CBQ_WRROPT]) {
475 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
476 fprintf(stderr, "CBQ: too short wrr opt\n");
477 else
478 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
479 }
480 if (tb[TCA_CBQ_FOPT]) {
481 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
482 fprintf(stderr, "CBQ: too short fopt\n");
483 else
484 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
485 }
486 if (tb[TCA_CBQ_OVL_STRATEGY]) {
487 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
488 fprintf(stderr, "CBQ: too short overlimit strategy %u/%u\n",
489 (unsigned int) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
490 (unsigned int) sizeof(*ovl));
491 else
492 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
493 }
494
495 if (r) {
496 char buf[64];
497
498 print_rate(buf, sizeof(buf), r->rate);
499 fprintf(f, "rate %s ", buf);
500 linklayer = (r->linklayer & TC_LINKLAYER_MASK);
501 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
502 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
503 if (show_details) {
504 fprintf(f, "cell %ub ", 1<<r->cell_log);
505 if (r->mpu)
506 fprintf(f, "mpu %ub ", r->mpu);
507 if (r->overhead)
508 fprintf(f, "overhead %ub ", r->overhead);
509 }
510 }
511 if (lss && lss->flags) {
512 int comma = 0;
513
514 fprintf(f, "(");
515 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
516 fprintf(f, "bounded");
517 comma = 1;
518 }
519 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
520 if (comma)
521 fprintf(f, ",");
522 fprintf(f, "isolated");
523 }
524 fprintf(f, ") ");
525 }
526 if (wrr) {
527 if (wrr->priority != TC_CBQ_MAXPRIO)
528 fprintf(f, "prio %u", wrr->priority);
529 else
530 fprintf(f, "prio no-transmit");
531 if (show_details) {
532 char buf[64];
533
534 fprintf(f, "/%u ", wrr->cpriority);
535 if (wrr->weight != 1) {
536 print_rate(buf, sizeof(buf), wrr->weight);
537 fprintf(f, "weight %s ", buf);
538 }
539 if (wrr->allot)
540 fprintf(f, "allot %ub ", wrr->allot);
541 }
542 }
543 if (lss && show_details) {
544 fprintf(f, "\nlevel %u ewma %u avpkt %ub ", lss->level, lss->ewma_log, lss->avpkt);
545 if (lss->maxidle) {
546 fprintf(f, "maxidle %s ", sprint_ticks(lss->maxidle>>lss->ewma_log, b1));
547 if (show_raw)
548 fprintf(f, "[%08x] ", lss->maxidle);
549 }
550 if (lss->minidle != 0x7fffffff) {
551 fprintf(f, "minidle %s ", sprint_ticks(lss->minidle>>lss->ewma_log, b1));
552 if (show_raw)
553 fprintf(f, "[%08x] ", lss->minidle);
554 }
555 if (lss->offtime) {
556 fprintf(f, "offtime %s ", sprint_ticks(lss->offtime, b1));
557 if (show_raw)
558 fprintf(f, "[%08x] ", lss->offtime);
559 }
560 }
561 if (fopt && show_details) {
562 char buf[64];
563
564 print_tc_classid(buf, sizeof(buf), fopt->split);
565 fprintf(f, "\nsplit %s ", buf);
566 if (fopt->defmap) {
567 fprintf(f, "defmap %08x", fopt->defmap);
568 }
569 }
570 return 0;
571 }
572
573 static int cbq_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
574 {
575 struct tc_cbq_xstats *st;
576
577 if (xstats == NULL)
578 return 0;
579
580 if (RTA_PAYLOAD(xstats) < sizeof(*st))
581 return -1;
582
583 st = RTA_DATA(xstats);
584 fprintf(f, " borrowed %u overactions %u avgidle %g undertime %g", st->borrows,
585 st->overactions, (double)st->avgidle, (double)st->undertime);
586 return 0;
587 }
588
589 struct qdisc_util cbq_qdisc_util = {
590 .id = "cbq",
591 .parse_qopt = cbq_parse_opt,
592 .print_qopt = cbq_print_opt,
593 .print_xstats = cbq_print_xstats,
594 .parse_copt = cbq_parse_class_opt,
595 .print_copt = cbq_print_opt,
596 };