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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
27 static void explain_class(void)
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");
38 static void explain(void)
40 fprintf(stderr, "Usage: ... cbq bandwidth BPS avpkt BYTES [ mpu BYTES ]\n");
41 fprintf(stderr, " [ cell BYTES ] [ ewma LOG ]\n");
44 static void explain1(char *arg)
46 fprintf(stderr, "Illegal \"%s\"\n", arg);
50 static int cbq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
53 struct tc_cbq_lssopt lss;
55 unsigned mpu=0, avpkt=0, allot=0;
56 unsigned short overhead=0;
57 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
62 memset(&lss, 0, sizeof(lss));
63 memset(&r, 0, sizeof(r));
66 if (matches(*argv, "bandwidth") == 0 ||
67 matches(*argv, "rate") == 0) {
69 if (get_rate(&r.rate, *argv)) {
70 explain1("bandwidth");
73 } else if (matches(*argv, "ewma") == 0) {
75 if (get_integer(&ewma_log, *argv, 0)) {
80 fprintf(stderr, "ewma_log must be < 32\n");
83 } else if (matches(*argv, "cell") == 0) {
87 if (get_size(&cell, *argv)) {
95 fprintf(stderr, "cell must be 2^n\n");
99 } else if (matches(*argv, "avpkt") == 0) {
101 if (get_size(&avpkt, *argv)) {
105 } else if (matches(*argv, "mpu") == 0) {
107 if (get_size(&mpu, *argv)) {
111 } else if (matches(*argv, "allot") == 0) {
113 /* Accept and ignore "allot" for backward compatibility */
114 if (get_size(&allot, *argv)) {
118 } else if (matches(*argv, "overhead") == 0) {
120 if (get_u16(&overhead, *argv, 10)) {
121 explain1("overhead"); return -1;
123 } else if (matches(*argv, "linklayer") == 0) {
125 if (get_linklayer(&linklayer, *argv)) {
126 explain1("linklayer"); return -1;
128 } else if (matches(*argv, "help") == 0) {
132 fprintf(stderr, "What is \"%s\"?\n", *argv);
139 /* OK. All options are parsed. */
142 fprintf(stderr, "CBQ: bandwidth is required parameter.\n");
146 fprintf(stderr, "CBQ: \"avpkt\" is required.\n");
149 if (allot < (avpkt*3)/2)
153 r.overhead = overhead;
154 if (tc_calc_rtable(&r, rtab, cell_log, allot, linklayer) < 0) {
155 fprintf(stderr, "CBQ: failed to calculate rate table.\n");
160 ewma_log = TC_CBQ_DEF_EWMA;
161 lss.ewma_log = ewma_log;
162 lss.maxidle = tc_calc_xmittime(r.rate, avpkt);
163 lss.change = TCF_CBQ_LSS_MAXIDLE|TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
166 tail = NLMSG_TAIL(n);
167 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
168 addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r));
169 addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss));
170 addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024);
173 for (i=0; i<256; i++)
174 printf("%u ", rtab[i]);
177 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
181 static int cbq_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
183 int wrr_ok=0, fopt_ok=0;
184 struct tc_ratespec r;
185 struct tc_cbq_lssopt lss;
186 struct tc_cbq_wrropt wrr;
187 struct tc_cbq_fopt fopt;
188 struct tc_cbq_ovl ovl;
194 unsigned minburst=0, maxburst=0;
195 unsigned short overhead=0;
196 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
199 memset(&r, 0, sizeof(r));
200 memset(&lss, 0, sizeof(lss));
201 memset(&wrr, 0, sizeof(wrr));
202 memset(&fopt, 0, sizeof(fopt));
203 memset(&ovl, 0, sizeof(ovl));
206 if (matches(*argv, "rate") == 0) {
208 if (get_rate(&r.rate, *argv)) {
212 } else if (matches(*argv, "bandwidth") == 0) {
214 if (get_rate(&bndw, *argv)) {
215 explain1("bandwidth");
218 } else if (matches(*argv, "minidle") == 0) {
220 if (get_u32(&lss.minidle, *argv, 0)) {
224 lss.change |= TCF_CBQ_LSS_MINIDLE;
225 } else if (matches(*argv, "minburst") == 0) {
227 if (get_u32(&minburst, *argv, 0)) {
228 explain1("minburst");
231 lss.change |= TCF_CBQ_LSS_OFFTIME;
232 } else if (matches(*argv, "maxburst") == 0) {
234 if (get_u32(&maxburst, *argv, 0)) {
235 explain1("maxburst");
238 lss.change |= TCF_CBQ_LSS_MAXIDLE;
239 } else if (matches(*argv, "bounded") == 0) {
240 lss.flags |= TCF_CBQ_LSS_BOUNDED;
241 lss.change |= TCF_CBQ_LSS_FLAGS;
242 } else if (matches(*argv, "borrow") == 0) {
243 lss.flags &= ~TCF_CBQ_LSS_BOUNDED;
244 lss.change |= TCF_CBQ_LSS_FLAGS;
245 } else if (matches(*argv, "isolated") == 0) {
246 lss.flags |= TCF_CBQ_LSS_ISOLATED;
247 lss.change |= TCF_CBQ_LSS_FLAGS;
248 } else if (matches(*argv, "sharing") == 0) {
249 lss.flags &= ~TCF_CBQ_LSS_ISOLATED;
250 lss.change |= TCF_CBQ_LSS_FLAGS;
251 } else if (matches(*argv, "ewma") == 0) {
253 if (get_integer(&ewma_log, *argv, 0)) {
258 fprintf(stderr, "ewma_log must be < 32\n");
261 lss.change |= TCF_CBQ_LSS_EWMA;
262 } else if (matches(*argv, "cell") == 0) {
266 if (get_size(&cell, *argv)) {
274 fprintf(stderr, "cell must be 2^n\n");
278 } else if (matches(*argv, "prio") == 0) {
281 if (get_u32(&prio, *argv, 0)) {
285 if (prio > TC_CBQ_MAXPRIO) {
286 fprintf(stderr, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO);
291 } else if (matches(*argv, "allot") == 0) {
293 if (get_size(&wrr.allot, *argv)) {
297 } else if (matches(*argv, "avpkt") == 0) {
299 if (get_size(&lss.avpkt, *argv)) {
303 lss.change |= TCF_CBQ_LSS_AVPKT;
304 } else if (matches(*argv, "mpu") == 0) {
306 if (get_size(&mpu, *argv)) {
310 } else if (matches(*argv, "weight") == 0) {
312 if (get_size(&wrr.weight, *argv)) {
317 } else if (matches(*argv, "split") == 0) {
319 if (get_tc_classid(&fopt.split, *argv)) {
320 fprintf(stderr, "Invalid split node ID.\n");
324 } else if (matches(*argv, "defmap") == 0) {
327 err = sscanf(*argv, "%08x/%08x", &fopt.defmap, &fopt.defchange);
329 fprintf(stderr, "Invalid defmap, should be MASK32[/MASK]\n");
335 } else if (matches(*argv, "overhead") == 0) {
337 if (get_u16(&overhead, *argv, 10)) {
338 explain1("overhead"); return -1;
340 } else if (matches(*argv, "linklayer") == 0) {
342 if (get_linklayer(&linklayer, *argv)) {
343 explain1("linklayer"); return -1;
345 } else if (matches(*argv, "help") == 0) {
349 fprintf(stderr, "What is \"%s\"?\n", *argv);
356 /* OK. All options are parsed. */
358 /* 1. Prepare link sharing scheduler parameters */
360 unsigned pktsize = wrr.allot;
361 if (wrr.allot < (lss.avpkt*3)/2)
362 wrr.allot = (lss.avpkt*3)/2;
364 r.overhead = overhead;
365 if (tc_calc_rtable(&r, rtab, cell_log, pktsize, linklayer) < 0) {
366 fprintf(stderr, "CBQ: failed to calculate rate table.\n");
371 ewma_log = TC_CBQ_DEF_EWMA;
372 lss.ewma_log = ewma_log;
373 if (lss.change&(TCF_CBQ_LSS_OFFTIME|TCF_CBQ_LSS_MAXIDLE)) {
374 if (lss.avpkt == 0) {
375 fprintf(stderr, "CBQ: avpkt is required for max/minburst.\n");
378 if (bndw==0 || r.rate == 0) {
379 fprintf(stderr, "CBQ: bandwidth&rate are required for max/minburst.\n");
383 if (wrr.priority == 0 && (n->nlmsg_flags&NLM_F_EXCL)) {
385 wrr.priority = TC_CBQ_MAXPRIO;
387 wrr.allot = (lss.avpkt*3)/2;
391 wrr.weight = (wrr.priority == TC_CBQ_MAXPRIO) ? 1 : r.rate;
392 if (wrr.allot == 0) {
393 fprintf(stderr, "CBQ: \"allot\" is required to set WRR parameters.\n");
397 if (lss.change&TCF_CBQ_LSS_MAXIDLE) {
398 lss.maxidle = tc_cbq_calc_maxidle(bndw, r.rate, lss.avpkt, ewma_log, maxburst);
399 lss.change |= TCF_CBQ_LSS_MAXIDLE;
400 lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
402 if (lss.change&TCF_CBQ_LSS_OFFTIME) {
403 lss.offtime = tc_cbq_calc_offtime(bndw, r.rate, lss.avpkt, ewma_log, minburst);
404 lss.change |= TCF_CBQ_LSS_OFFTIME;
405 lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT;
407 if (lss.change&TCF_CBQ_LSS_MINIDLE) {
408 lss.minidle <<= lss.ewma_log;
409 lss.change |= TCF_CBQ_LSS_EWMA;
412 tail = NLMSG_TAIL(n);
413 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
415 lss.change |= TCF_CBQ_LSS_FLAGS;
416 addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss));
419 addattr_l(n, 1024, TCA_CBQ_WRROPT, &wrr, sizeof(wrr));
421 addattr_l(n, 1024, TCA_CBQ_FOPT, &fopt, sizeof(fopt));
423 addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r));
424 addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024);
427 for (i=0; i<256; i++)
428 printf("%u ", rtab[i]);
432 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
437 static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
439 struct rtattr *tb[TCA_CBQ_MAX+1];
440 struct tc_ratespec *r = NULL;
441 struct tc_cbq_lssopt *lss = NULL;
442 struct tc_cbq_wrropt *wrr = NULL;
443 struct tc_cbq_fopt *fopt = NULL;
444 struct tc_cbq_ovl *ovl = NULL;
450 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
452 if (tb[TCA_CBQ_RATE]) {
453 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
454 fprintf(stderr, "CBQ: too short rate opt\n");
456 r = RTA_DATA(tb[TCA_CBQ_RATE]);
458 if (tb[TCA_CBQ_LSSOPT]) {
459 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
460 fprintf(stderr, "CBQ: too short lss opt\n");
462 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
464 if (tb[TCA_CBQ_WRROPT]) {
465 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
466 fprintf(stderr, "CBQ: too short wrr opt\n");
468 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
470 if (tb[TCA_CBQ_FOPT]) {
471 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
472 fprintf(stderr, "CBQ: too short fopt\n");
474 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
476 if (tb[TCA_CBQ_OVL_STRATEGY]) {
477 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
478 fprintf(stderr, "CBQ: too short overlimit strategy %u/%u\n",
479 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
480 (unsigned) sizeof(*ovl));
482 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
487 print_rate(buf, sizeof(buf), r->rate);
488 fprintf(f, "rate %s ", buf);
490 fprintf(f, "cell %ub ", 1<<r->cell_log);
492 fprintf(f, "mpu %ub ", r->mpu);
494 fprintf(f, "overhead %ub ", r->overhead);
497 if (lss && lss->flags) {
500 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
501 fprintf(f, "bounded");
504 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
507 fprintf(f, "isolated");
512 if (wrr->priority != TC_CBQ_MAXPRIO)
513 fprintf(f, "prio %u", wrr->priority);
515 fprintf(f, "prio no-transmit");
518 fprintf(f, "/%u ", wrr->cpriority);
519 if (wrr->weight != 1) {
520 print_rate(buf, sizeof(buf), wrr->weight);
521 fprintf(f, "weight %s ", buf);
524 fprintf(f, "allot %ub ", wrr->allot);
527 if (lss && show_details) {
528 fprintf(f, "\nlevel %u ewma %u avpkt %ub ", lss->level, lss->ewma_log, lss->avpkt);
530 fprintf(f, "maxidle %s ", sprint_ticks(lss->maxidle>>lss->ewma_log, b1));
532 fprintf(f, "[%08x] ", lss->maxidle);
534 if (lss->minidle!=0x7fffffff) {
535 fprintf(f, "minidle %s ", sprint_ticks(lss->minidle>>lss->ewma_log, b1));
537 fprintf(f, "[%08x] ", lss->minidle);
540 fprintf(f, "offtime %s ", sprint_ticks(lss->offtime, b1));
542 fprintf(f, "[%08x] ", lss->offtime);
545 if (fopt && show_details) {
547 print_tc_classid(buf, sizeof(buf), fopt->split);
548 fprintf(f, "\nsplit %s ", buf);
550 fprintf(f, "defmap %08x", fopt->defmap);
556 static int cbq_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
558 struct tc_cbq_xstats *st;
563 if (RTA_PAYLOAD(xstats) < sizeof(*st))
566 st = RTA_DATA(xstats);
567 fprintf(f, " borrowed %u overactions %u avgidle %g undertime %g", st->borrows,
568 st->overactions, (double)st->avgidle, (double)st->undertime);
572 struct qdisc_util cbq_qdisc_util = {
574 .parse_qopt = cbq_parse_opt,
575 .print_qopt = cbq_print_opt,
576 .print_xstats = cbq_print_xstats,
577 .parse_copt = cbq_parse_class_opt,
578 .print_copt = cbq_print_opt,