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) {
88 if (get_size(&cell
, *argv
)) {
92 for (i
= 0; i
< 32; i
++)
96 fprintf(stderr
, "cell must be 2^n\n");
100 } else if (matches(*argv
, "avpkt") == 0) {
102 if (get_size(&avpkt
, *argv
)) {
106 } else if (matches(*argv
, "mpu") == 0) {
108 if (get_size(&mpu
, *argv
)) {
112 } else if (matches(*argv
, "allot") == 0) {
114 /* Accept and ignore "allot" for backward compatibility */
115 if (get_size(&allot
, *argv
)) {
119 } else if (matches(*argv
, "overhead") == 0) {
121 if (get_u16(&overhead
, *argv
, 10)) {
122 explain1("overhead"); return -1;
124 } else if (matches(*argv
, "linklayer") == 0) {
126 if (get_linklayer(&linklayer
, *argv
)) {
127 explain1("linklayer"); return -1;
129 } else if (matches(*argv
, "help") == 0) {
133 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
140 /* OK. All options are parsed. */
143 fprintf(stderr
, "CBQ: bandwidth is required parameter.\n");
147 fprintf(stderr
, "CBQ: \"avpkt\" is required.\n");
150 if (allot
< (avpkt
*3)/2)
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");
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
;
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);
175 for (i
= 0; i
< 256; i
++)
176 printf("%u ", rtab
[i
]);
179 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
183 static int cbq_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
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
;
195 unsigned int bndw
= 0;
196 unsigned minburst
= 0, maxburst
= 0;
197 unsigned short overhead
= 0;
198 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
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
));
208 if (matches(*argv
, "rate") == 0) {
210 if (get_rate(&r
.rate
, *argv
)) {
214 } else if (matches(*argv
, "bandwidth") == 0) {
216 if (get_rate(&bndw
, *argv
)) {
217 explain1("bandwidth");
220 } else if (matches(*argv
, "minidle") == 0) {
222 if (get_u32(&lss
.minidle
, *argv
, 0)) {
226 lss
.change
|= TCF_CBQ_LSS_MINIDLE
;
227 } else if (matches(*argv
, "minburst") == 0) {
229 if (get_u32(&minburst
, *argv
, 0)) {
230 explain1("minburst");
233 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
234 } else if (matches(*argv
, "maxburst") == 0) {
236 if (get_u32(&maxburst
, *argv
, 0)) {
237 explain1("maxburst");
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) {
255 if (get_integer(&ewma_log
, *argv
, 0)) {
260 fprintf(stderr
, "ewma_log must be < 32\n");
263 lss
.change
|= TCF_CBQ_LSS_EWMA
;
264 } else if (matches(*argv
, "cell") == 0) {
269 if (get_size(&cell
, *argv
)) {
273 for (i
= 0; i
< 32; i
++)
277 fprintf(stderr
, "cell must be 2^n\n");
281 } else if (matches(*argv
, "prio") == 0) {
285 if (get_u32(&prio
, *argv
, 0)) {
289 if (prio
> TC_CBQ_MAXPRIO
) {
290 fprintf(stderr
, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO
);
295 } else if (matches(*argv
, "allot") == 0) {
297 if (get_size(&wrr
.allot
, *argv
)) {
301 } else if (matches(*argv
, "avpkt") == 0) {
303 if (get_size(&lss
.avpkt
, *argv
)) {
307 lss
.change
|= TCF_CBQ_LSS_AVPKT
;
308 } else if (matches(*argv
, "mpu") == 0) {
310 if (get_size(&mpu
, *argv
)) {
314 } else if (matches(*argv
, "weight") == 0) {
316 if (get_size(&wrr
.weight
, *argv
)) {
321 } else if (matches(*argv
, "split") == 0) {
323 if (get_tc_classid(&fopt
.split
, *argv
)) {
324 fprintf(stderr
, "Invalid split node ID.\n");
328 } else if (matches(*argv
, "defmap") == 0) {
332 err
= sscanf(*argv
, "%08x/%08x", &fopt
.defmap
, &fopt
.defchange
);
334 fprintf(stderr
, "Invalid defmap, should be MASK32[/MASK]\n");
340 } else if (matches(*argv
, "overhead") == 0) {
342 if (get_u16(&overhead
, *argv
, 10)) {
343 explain1("overhead"); return -1;
345 } else if (matches(*argv
, "linklayer") == 0) {
347 if (get_linklayer(&linklayer
, *argv
)) {
348 explain1("linklayer"); return -1;
350 } else if (matches(*argv
, "help") == 0) {
354 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
361 /* OK. All options are parsed. */
363 /* 1. Prepare link sharing scheduler parameters */
365 unsigned int pktsize
= wrr
.allot
;
367 if (wrr
.allot
< (lss
.avpkt
*3)/2)
368 wrr
.allot
= (lss
.avpkt
*3)/2;
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");
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");
384 if (bndw
== 0 || r
.rate
== 0) {
385 fprintf(stderr
, "CBQ: bandwidth&rate are required for max/minburst.\n");
389 if (wrr
.priority
== 0 && (n
->nlmsg_flags
&NLM_F_EXCL
)) {
391 wrr
.priority
= TC_CBQ_MAXPRIO
;
393 wrr
.allot
= (lss
.avpkt
*3)/2;
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");
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
;
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
;
413 if (lss
.change
&TCF_CBQ_LSS_MINIDLE
) {
414 lss
.minidle
<<= lss
.ewma_log
;
415 lss
.change
|= TCF_CBQ_LSS_EWMA
;
418 tail
= NLMSG_TAIL(n
);
419 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
421 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
422 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
425 addattr_l(n
, 1024, TCA_CBQ_WRROPT
, &wrr
, sizeof(wrr
));
427 addattr_l(n
, 1024, TCA_CBQ_FOPT
, &fopt
, sizeof(fopt
));
429 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
430 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
434 for (i
= 0; i
< 256; i
++)
435 printf("%u ", rtab
[i
]);
439 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
444 static int cbq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
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
;
460 parse_rtattr_nested(tb
, TCA_CBQ_MAX
, opt
);
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");
466 r
= RTA_DATA(tb
[TCA_CBQ_RATE
]);
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");
472 lss
= RTA_DATA(tb
[TCA_CBQ_LSSOPT
]);
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");
478 wrr
= RTA_DATA(tb
[TCA_CBQ_WRROPT
]);
480 if (tb
[TCA_CBQ_FOPT
]) {
481 if (RTA_PAYLOAD(tb
[TCA_CBQ_FOPT
]) < sizeof(*fopt
))
482 fprintf(stderr
, "CBQ: too short fopt\n");
484 fopt
= RTA_DATA(tb
[TCA_CBQ_FOPT
]);
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
));
492 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
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
));
504 fprintf(f
, "cell %ub ", 1<<r
->cell_log
);
506 fprintf(f
, "mpu %ub ", r
->mpu
);
508 fprintf(f
, "overhead %ub ", r
->overhead
);
511 if (lss
&& lss
->flags
) {
515 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
516 fprintf(f
, "bounded");
519 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
522 fprintf(f
, "isolated");
527 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
528 fprintf(f
, "prio %u", wrr
->priority
);
530 fprintf(f
, "prio no-transmit");
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
);
540 fprintf(f
, "allot %ub ", wrr
->allot
);
543 if (lss
&& show_details
) {
544 fprintf(f
, "\nlevel %u ewma %u avpkt %ub ", lss
->level
, lss
->ewma_log
, lss
->avpkt
);
546 fprintf(f
, "maxidle %s ", sprint_ticks(lss
->maxidle
>>lss
->ewma_log
, b1
));
548 fprintf(f
, "[%08x] ", lss
->maxidle
);
550 if (lss
->minidle
!= 0x7fffffff) {
551 fprintf(f
, "minidle %s ", sprint_ticks(lss
->minidle
>>lss
->ewma_log
, b1
));
553 fprintf(f
, "[%08x] ", lss
->minidle
);
556 fprintf(f
, "offtime %s ", sprint_ticks(lss
->offtime
, b1
));
558 fprintf(f
, "[%08x] ", lss
->offtime
);
561 if (fopt
&& show_details
) {
564 print_tc_classid(buf
, sizeof(buf
), fopt
->split
);
565 fprintf(f
, "\nsplit %s ", buf
);
567 fprintf(f
, "defmap %08x", fopt
->defmap
);
573 static int cbq_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
575 struct tc_cbq_xstats
*st
;
580 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
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
);
589 struct qdisc_util cbq_qdisc_util
= {
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
,