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>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 static void explain_class(void)
28 fprintf(stderr
, "Usage: ... cbq bandwidth BPS rate BPS maxburst PKTS [ avpkt BYTES ]\n");
29 fprintf(stderr
, " [ minburst PKTS ] [ bounded ] [ isolated ]\n");
30 fprintf(stderr
, " [ allot BYTES ] [ mpu BYTES ] [ weight RATE ]\n");
31 fprintf(stderr
, " [ prio NUMBER ] [ cell BYTES ] [ ewma LOG ]\n");
32 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
33 fprintf(stderr
, " [ split CLASSID ] [ defmap MASK/CHANGE ]\n");
34 fprintf(stderr
, " [ overhead BYTES ] [ linklayer TYPE ]\n");
37 static void explain(void)
39 fprintf(stderr
, "Usage: ... cbq bandwidth BPS avpkt BYTES [ mpu BYTES ]\n");
40 fprintf(stderr
, " [ cell BYTES ] [ ewma LOG ]\n");
43 static void explain1(char *arg
)
45 fprintf(stderr
, "Illegal \"%s\"\n", arg
);
49 static int cbq_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
51 struct tc_ratespec r
= {};
52 struct tc_cbq_lssopt lss
= {};
54 unsigned mpu
= 0, avpkt
= 0, allot
= 0;
55 unsigned short overhead
= 0;
56 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
62 if (matches(*argv
, "bandwidth") == 0 ||
63 matches(*argv
, "rate") == 0) {
65 if (strchr(*argv
, '%')) {
66 if (get_percent_rate(&r
.rate
, *argv
, dev
)) {
67 explain1("bandwidth");
70 } else if (get_rate(&r
.rate
, *argv
)) {
71 explain1("bandwidth");
74 } else if (matches(*argv
, "ewma") == 0) {
76 if (get_integer(&ewma_log
, *argv
, 0)) {
81 fprintf(stderr
, "ewma_log must be < 32\n");
84 } else if (matches(*argv
, "cell") == 0) {
89 if (get_size(&cell
, *argv
)) {
93 for (i
= 0; i
< 32; i
++)
97 fprintf(stderr
, "cell must be 2^n\n");
101 } else if (matches(*argv
, "avpkt") == 0) {
103 if (get_size(&avpkt
, *argv
)) {
107 } else if (matches(*argv
, "mpu") == 0) {
109 if (get_size(&mpu
, *argv
)) {
113 } else if (matches(*argv
, "allot") == 0) {
115 /* Accept and ignore "allot" for backward compatibility */
116 if (get_size(&allot
, *argv
)) {
120 } else if (matches(*argv
, "overhead") == 0) {
122 if (get_u16(&overhead
, *argv
, 10)) {
123 explain1("overhead"); return -1;
125 } else if (matches(*argv
, "linklayer") == 0) {
127 if (get_linklayer(&linklayer
, *argv
)) {
128 explain1("linklayer"); return -1;
130 } else if (matches(*argv
, "help") == 0) {
134 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
141 /* OK. All options are parsed. */
144 fprintf(stderr
, "CBQ: bandwidth is required parameter.\n");
148 fprintf(stderr
, "CBQ: \"avpkt\" is required.\n");
151 if (allot
< (avpkt
*3)/2)
155 r
.overhead
= overhead
;
156 if (tc_calc_rtable(&r
, rtab
, cell_log
, allot
, linklayer
) < 0) {
157 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
162 ewma_log
= TC_CBQ_DEF_EWMA
;
163 lss
.ewma_log
= ewma_log
;
164 lss
.maxidle
= tc_calc_xmittime(r
.rate
, avpkt
);
165 lss
.change
= TCF_CBQ_LSS_MAXIDLE
|TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
168 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
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);
175 for (i
= 0; i
< 256; i
++)
176 printf("%u ", rtab
[i
]);
179 addattr_nest_end(n
, tail
);
183 static int cbq_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
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
= {};
194 unsigned int bndw
= 0;
195 unsigned minburst
= 0, maxburst
= 0;
196 unsigned short overhead
= 0;
197 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
201 if (matches(*argv
, "rate") == 0) {
203 if (strchr(*argv
, '%')) {
204 if (get_percent_rate(&r
.rate
, *argv
, dev
)) {
208 } else if (get_rate(&r
.rate
, *argv
)) {
212 } else if (matches(*argv
, "bandwidth") == 0) {
214 if (strchr(*argv
, '%')) {
215 if (get_percent_rate(&bndw
, *argv
, dev
)) {
216 explain1("bandwidth");
219 } else if (get_rate(&bndw
, *argv
)) {
220 explain1("bandwidth");
223 } else if (matches(*argv
, "minidle") == 0) {
225 if (get_u32(&lss
.minidle
, *argv
, 0)) {
229 lss
.change
|= TCF_CBQ_LSS_MINIDLE
;
230 } else if (matches(*argv
, "minburst") == 0) {
232 if (get_u32(&minburst
, *argv
, 0)) {
233 explain1("minburst");
236 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
237 } else if (matches(*argv
, "maxburst") == 0) {
239 if (get_u32(&maxburst
, *argv
, 0)) {
240 explain1("maxburst");
243 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
244 } else if (matches(*argv
, "bounded") == 0) {
245 lss
.flags
|= TCF_CBQ_LSS_BOUNDED
;
246 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
247 } else if (matches(*argv
, "borrow") == 0) {
248 lss
.flags
&= ~TCF_CBQ_LSS_BOUNDED
;
249 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
250 } else if (matches(*argv
, "isolated") == 0) {
251 lss
.flags
|= TCF_CBQ_LSS_ISOLATED
;
252 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
253 } else if (matches(*argv
, "sharing") == 0) {
254 lss
.flags
&= ~TCF_CBQ_LSS_ISOLATED
;
255 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
256 } else if (matches(*argv
, "ewma") == 0) {
258 if (get_integer(&ewma_log
, *argv
, 0)) {
263 fprintf(stderr
, "ewma_log must be < 32\n");
266 lss
.change
|= TCF_CBQ_LSS_EWMA
;
267 } else if (matches(*argv
, "cell") == 0) {
272 if (get_size(&cell
, *argv
)) {
276 for (i
= 0; i
< 32; i
++)
280 fprintf(stderr
, "cell must be 2^n\n");
284 } else if (matches(*argv
, "prio") == 0) {
288 if (get_u32(&prio
, *argv
, 0)) {
292 if (prio
> TC_CBQ_MAXPRIO
) {
293 fprintf(stderr
, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO
);
298 } else if (matches(*argv
, "allot") == 0) {
300 if (get_size(&wrr
.allot
, *argv
)) {
304 } else if (matches(*argv
, "avpkt") == 0) {
306 if (get_size(&lss
.avpkt
, *argv
)) {
310 lss
.change
|= TCF_CBQ_LSS_AVPKT
;
311 } else if (matches(*argv
, "mpu") == 0) {
313 if (get_size(&mpu
, *argv
)) {
317 } else if (matches(*argv
, "weight") == 0) {
319 if (get_size(&wrr
.weight
, *argv
)) {
324 } else if (matches(*argv
, "split") == 0) {
326 if (get_tc_classid(&fopt
.split
, *argv
)) {
327 fprintf(stderr
, "Invalid split node ID.\n");
331 } else if (matches(*argv
, "defmap") == 0) {
335 err
= sscanf(*argv
, "%08x/%08x", &fopt
.defmap
, &fopt
.defchange
);
337 fprintf(stderr
, "Invalid defmap, should be MASK32[/MASK]\n");
343 } else if (matches(*argv
, "overhead") == 0) {
345 if (get_u16(&overhead
, *argv
, 10)) {
346 explain1("overhead"); return -1;
348 } else if (matches(*argv
, "linklayer") == 0) {
350 if (get_linklayer(&linklayer
, *argv
)) {
351 explain1("linklayer"); return -1;
353 } else if (matches(*argv
, "help") == 0) {
357 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
364 /* OK. All options are parsed. */
366 /* 1. Prepare link sharing scheduler parameters */
368 unsigned int pktsize
= wrr
.allot
;
370 if (wrr
.allot
< (lss
.avpkt
*3)/2)
371 wrr
.allot
= (lss
.avpkt
*3)/2;
373 r
.overhead
= overhead
;
374 if (tc_calc_rtable(&r
, rtab
, cell_log
, pktsize
, linklayer
) < 0) {
375 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
380 ewma_log
= TC_CBQ_DEF_EWMA
;
381 lss
.ewma_log
= ewma_log
;
382 if (lss
.change
&(TCF_CBQ_LSS_OFFTIME
|TCF_CBQ_LSS_MAXIDLE
)) {
383 if (lss
.avpkt
== 0) {
384 fprintf(stderr
, "CBQ: avpkt is required for max/minburst.\n");
387 if (bndw
== 0 || r
.rate
== 0) {
388 fprintf(stderr
, "CBQ: bandwidth&rate are required for max/minburst.\n");
392 if (wrr
.priority
== 0 && (n
->nlmsg_flags
&NLM_F_EXCL
)) {
394 wrr
.priority
= TC_CBQ_MAXPRIO
;
396 wrr
.allot
= (lss
.avpkt
*3)/2;
400 wrr
.weight
= (wrr
.priority
== TC_CBQ_MAXPRIO
) ? 1 : r
.rate
;
401 if (wrr
.allot
== 0) {
402 fprintf(stderr
, "CBQ: \"allot\" is required to set WRR parameters.\n");
406 if (lss
.change
&TCF_CBQ_LSS_MAXIDLE
) {
407 lss
.maxidle
= tc_cbq_calc_maxidle(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, maxburst
);
408 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
409 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
411 if (lss
.change
&TCF_CBQ_LSS_OFFTIME
) {
412 lss
.offtime
= tc_cbq_calc_offtime(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, minburst
);
413 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
414 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
416 if (lss
.change
&TCF_CBQ_LSS_MINIDLE
) {
417 lss
.minidle
<<= lss
.ewma_log
;
418 lss
.change
|= TCF_CBQ_LSS_EWMA
;
421 tail
= addattr_nest(n
, 1024, TCA_OPTIONS
);
423 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
424 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
427 addattr_l(n
, 1024, TCA_CBQ_WRROPT
, &wrr
, sizeof(wrr
));
429 addattr_l(n
, 1024, TCA_CBQ_FOPT
, &fopt
, sizeof(fopt
));
431 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
432 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
436 for (i
= 0; i
< 256; i
++)
437 printf("%u ", rtab
[i
]);
441 addattr_nest_end(n
, tail
);
446 static int cbq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
448 struct rtattr
*tb
[TCA_CBQ_MAX
+1];
449 struct tc_ratespec
*r
= NULL
;
450 struct tc_cbq_lssopt
*lss
= NULL
;
451 struct tc_cbq_wrropt
*wrr
= NULL
;
452 struct tc_cbq_fopt
*fopt
= NULL
;
453 struct tc_cbq_ovl
*ovl
= NULL
;
454 unsigned int linklayer
;
462 parse_rtattr_nested(tb
, TCA_CBQ_MAX
, opt
);
464 if (tb
[TCA_CBQ_RATE
]) {
465 if (RTA_PAYLOAD(tb
[TCA_CBQ_RATE
]) < sizeof(*r
))
466 fprintf(stderr
, "CBQ: too short rate opt\n");
468 r
= RTA_DATA(tb
[TCA_CBQ_RATE
]);
470 if (tb
[TCA_CBQ_LSSOPT
]) {
471 if (RTA_PAYLOAD(tb
[TCA_CBQ_LSSOPT
]) < sizeof(*lss
))
472 fprintf(stderr
, "CBQ: too short lss opt\n");
474 lss
= RTA_DATA(tb
[TCA_CBQ_LSSOPT
]);
476 if (tb
[TCA_CBQ_WRROPT
]) {
477 if (RTA_PAYLOAD(tb
[TCA_CBQ_WRROPT
]) < sizeof(*wrr
))
478 fprintf(stderr
, "CBQ: too short wrr opt\n");
480 wrr
= RTA_DATA(tb
[TCA_CBQ_WRROPT
]);
482 if (tb
[TCA_CBQ_FOPT
]) {
483 if (RTA_PAYLOAD(tb
[TCA_CBQ_FOPT
]) < sizeof(*fopt
))
484 fprintf(stderr
, "CBQ: too short fopt\n");
486 fopt
= RTA_DATA(tb
[TCA_CBQ_FOPT
]);
488 if (tb
[TCA_CBQ_OVL_STRATEGY
]) {
489 if (RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]) < sizeof(*ovl
))
490 fprintf(stderr
, "CBQ: too short overlimit strategy %u/%u\n",
491 (unsigned int) RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]),
492 (unsigned int) sizeof(*ovl
));
494 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
500 print_rate(buf
, sizeof(buf
), r
->rate
);
501 fprintf(f
, "rate %s ", buf
);
502 linklayer
= (r
->linklayer
& TC_LINKLAYER_MASK
);
503 if (linklayer
> TC_LINKLAYER_ETHERNET
|| show_details
)
504 fprintf(f
, "linklayer %s ", sprint_linklayer(linklayer
, b2
));
506 fprintf(f
, "cell %ub ", 1<<r
->cell_log
);
508 fprintf(f
, "mpu %ub ", r
->mpu
);
510 fprintf(f
, "overhead %ub ", r
->overhead
);
513 if (lss
&& lss
->flags
) {
517 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
518 fprintf(f
, "bounded");
521 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
524 fprintf(f
, "isolated");
529 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
530 fprintf(f
, "prio %u", wrr
->priority
);
532 fprintf(f
, "prio no-transmit");
536 fprintf(f
, "/%u ", wrr
->cpriority
);
537 if (wrr
->weight
!= 1) {
538 print_rate(buf
, sizeof(buf
), wrr
->weight
);
539 fprintf(f
, "weight %s ", buf
);
542 fprintf(f
, "allot %ub ", wrr
->allot
);
545 if (lss
&& show_details
) {
546 fprintf(f
, "\nlevel %u ewma %u avpkt %ub ", lss
->level
, lss
->ewma_log
, lss
->avpkt
);
548 fprintf(f
, "maxidle %s ", sprint_ticks(lss
->maxidle
>>lss
->ewma_log
, b1
));
550 fprintf(f
, "[%08x] ", lss
->maxidle
);
552 if (lss
->minidle
!= 0x7fffffff) {
553 fprintf(f
, "minidle %s ", sprint_ticks(lss
->minidle
>>lss
->ewma_log
, b1
));
555 fprintf(f
, "[%08x] ", lss
->minidle
);
558 fprintf(f
, "offtime %s ", sprint_ticks(lss
->offtime
, b1
));
560 fprintf(f
, "[%08x] ", lss
->offtime
);
563 if (fopt
&& show_details
) {
566 print_tc_classid(buf
, sizeof(buf
), fopt
->split
);
567 fprintf(f
, "\nsplit %s ", buf
);
569 fprintf(f
, "defmap %08x", fopt
->defmap
);
575 static int cbq_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
577 struct tc_cbq_xstats
*st
;
582 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
585 st
= RTA_DATA(xstats
);
586 fprintf(f
, " borrowed %u overactions %u avgidle %g undertime %g", st
->borrows
,
587 st
->overactions
, (double)st
->avgidle
, (double)st
->undertime
);
591 struct qdisc_util cbq_qdisc_util
= {
593 .parse_qopt
= cbq_parse_opt
,
594 .print_qopt
= cbq_print_opt
,
595 .print_xstats
= cbq_print_xstats
,
596 .parse_copt
= cbq_parse_class_opt
,
597 .print_copt
= cbq_print_opt
,