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
)
52 struct tc_ratespec r
= {};
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 */
63 if (matches(*argv
, "bandwidth") == 0 ||
64 matches(*argv
, "rate") == 0) {
66 if (get_rate(&r
.rate
, *argv
)) {
67 explain1("bandwidth");
70 } else if (matches(*argv
, "ewma") == 0) {
72 if (get_integer(&ewma_log
, *argv
, 0)) {
77 fprintf(stderr
, "ewma_log must be < 32\n");
80 } else if (matches(*argv
, "cell") == 0) {
85 if (get_size(&cell
, *argv
)) {
89 for (i
= 0; i
< 32; i
++)
93 fprintf(stderr
, "cell must be 2^n\n");
97 } else if (matches(*argv
, "avpkt") == 0) {
99 if (get_size(&avpkt
, *argv
)) {
103 } else if (matches(*argv
, "mpu") == 0) {
105 if (get_size(&mpu
, *argv
)) {
109 } else if (matches(*argv
, "allot") == 0) {
111 /* Accept and ignore "allot" for backward compatibility */
112 if (get_size(&allot
, *argv
)) {
116 } else if (matches(*argv
, "overhead") == 0) {
118 if (get_u16(&overhead
, *argv
, 10)) {
119 explain1("overhead"); return -1;
121 } else if (matches(*argv
, "linklayer") == 0) {
123 if (get_linklayer(&linklayer
, *argv
)) {
124 explain1("linklayer"); return -1;
126 } else if (matches(*argv
, "help") == 0) {
130 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
137 /* OK. All options are parsed. */
140 fprintf(stderr
, "CBQ: bandwidth is required parameter.\n");
144 fprintf(stderr
, "CBQ: \"avpkt\" is required.\n");
147 if (allot
< (avpkt
*3)/2)
151 r
.overhead
= overhead
;
152 if (tc_calc_rtable(&r
, rtab
, cell_log
, allot
, linklayer
) < 0) {
153 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
158 ewma_log
= TC_CBQ_DEF_EWMA
;
159 lss
.ewma_log
= ewma_log
;
160 lss
.maxidle
= tc_calc_xmittime(r
.rate
, avpkt
);
161 lss
.change
= TCF_CBQ_LSS_MAXIDLE
|TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
164 tail
= NLMSG_TAIL(n
);
165 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
166 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
167 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
168 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
172 for (i
= 0; i
< 256; i
++)
173 printf("%u ", rtab
[i
]);
176 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
180 static int cbq_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
182 int wrr_ok
= 0, fopt_ok
= 0;
183 struct tc_ratespec r
= {};
184 struct tc_cbq_lssopt lss
= {};
185 struct tc_cbq_wrropt wrr
= {};
186 struct tc_cbq_fopt fopt
= {};
191 unsigned int bndw
= 0;
192 unsigned minburst
= 0, maxburst
= 0;
193 unsigned short overhead
= 0;
194 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
198 if (matches(*argv
, "rate") == 0) {
200 if (get_rate(&r
.rate
, *argv
)) {
204 } else if (matches(*argv
, "bandwidth") == 0) {
206 if (get_rate(&bndw
, *argv
)) {
207 explain1("bandwidth");
210 } else if (matches(*argv
, "minidle") == 0) {
212 if (get_u32(&lss
.minidle
, *argv
, 0)) {
216 lss
.change
|= TCF_CBQ_LSS_MINIDLE
;
217 } else if (matches(*argv
, "minburst") == 0) {
219 if (get_u32(&minburst
, *argv
, 0)) {
220 explain1("minburst");
223 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
224 } else if (matches(*argv
, "maxburst") == 0) {
226 if (get_u32(&maxburst
, *argv
, 0)) {
227 explain1("maxburst");
230 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
231 } else if (matches(*argv
, "bounded") == 0) {
232 lss
.flags
|= TCF_CBQ_LSS_BOUNDED
;
233 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
234 } else if (matches(*argv
, "borrow") == 0) {
235 lss
.flags
&= ~TCF_CBQ_LSS_BOUNDED
;
236 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
237 } else if (matches(*argv
, "isolated") == 0) {
238 lss
.flags
|= TCF_CBQ_LSS_ISOLATED
;
239 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
240 } else if (matches(*argv
, "sharing") == 0) {
241 lss
.flags
&= ~TCF_CBQ_LSS_ISOLATED
;
242 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
243 } else if (matches(*argv
, "ewma") == 0) {
245 if (get_integer(&ewma_log
, *argv
, 0)) {
250 fprintf(stderr
, "ewma_log must be < 32\n");
253 lss
.change
|= TCF_CBQ_LSS_EWMA
;
254 } else if (matches(*argv
, "cell") == 0) {
259 if (get_size(&cell
, *argv
)) {
263 for (i
= 0; i
< 32; i
++)
267 fprintf(stderr
, "cell must be 2^n\n");
271 } else if (matches(*argv
, "prio") == 0) {
275 if (get_u32(&prio
, *argv
, 0)) {
279 if (prio
> TC_CBQ_MAXPRIO
) {
280 fprintf(stderr
, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO
);
285 } else if (matches(*argv
, "allot") == 0) {
287 if (get_size(&wrr
.allot
, *argv
)) {
291 } else if (matches(*argv
, "avpkt") == 0) {
293 if (get_size(&lss
.avpkt
, *argv
)) {
297 lss
.change
|= TCF_CBQ_LSS_AVPKT
;
298 } else if (matches(*argv
, "mpu") == 0) {
300 if (get_size(&mpu
, *argv
)) {
304 } else if (matches(*argv
, "weight") == 0) {
306 if (get_size(&wrr
.weight
, *argv
)) {
311 } else if (matches(*argv
, "split") == 0) {
313 if (get_tc_classid(&fopt
.split
, *argv
)) {
314 fprintf(stderr
, "Invalid split node ID.\n");
318 } else if (matches(*argv
, "defmap") == 0) {
322 err
= sscanf(*argv
, "%08x/%08x", &fopt
.defmap
, &fopt
.defchange
);
324 fprintf(stderr
, "Invalid defmap, should be MASK32[/MASK]\n");
330 } else if (matches(*argv
, "overhead") == 0) {
332 if (get_u16(&overhead
, *argv
, 10)) {
333 explain1("overhead"); return -1;
335 } else if (matches(*argv
, "linklayer") == 0) {
337 if (get_linklayer(&linklayer
, *argv
)) {
338 explain1("linklayer"); return -1;
340 } else if (matches(*argv
, "help") == 0) {
344 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
351 /* OK. All options are parsed. */
353 /* 1. Prepare link sharing scheduler parameters */
355 unsigned int pktsize
= wrr
.allot
;
357 if (wrr
.allot
< (lss
.avpkt
*3)/2)
358 wrr
.allot
= (lss
.avpkt
*3)/2;
360 r
.overhead
= overhead
;
361 if (tc_calc_rtable(&r
, rtab
, cell_log
, pktsize
, linklayer
) < 0) {
362 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
367 ewma_log
= TC_CBQ_DEF_EWMA
;
368 lss
.ewma_log
= ewma_log
;
369 if (lss
.change
&(TCF_CBQ_LSS_OFFTIME
|TCF_CBQ_LSS_MAXIDLE
)) {
370 if (lss
.avpkt
== 0) {
371 fprintf(stderr
, "CBQ: avpkt is required for max/minburst.\n");
374 if (bndw
== 0 || r
.rate
== 0) {
375 fprintf(stderr
, "CBQ: bandwidth&rate are required for max/minburst.\n");
379 if (wrr
.priority
== 0 && (n
->nlmsg_flags
&NLM_F_EXCL
)) {
381 wrr
.priority
= TC_CBQ_MAXPRIO
;
383 wrr
.allot
= (lss
.avpkt
*3)/2;
387 wrr
.weight
= (wrr
.priority
== TC_CBQ_MAXPRIO
) ? 1 : r
.rate
;
388 if (wrr
.allot
== 0) {
389 fprintf(stderr
, "CBQ: \"allot\" is required to set WRR parameters.\n");
393 if (lss
.change
&TCF_CBQ_LSS_MAXIDLE
) {
394 lss
.maxidle
= tc_cbq_calc_maxidle(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, maxburst
);
395 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
396 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
398 if (lss
.change
&TCF_CBQ_LSS_OFFTIME
) {
399 lss
.offtime
= tc_cbq_calc_offtime(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, minburst
);
400 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
401 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
403 if (lss
.change
&TCF_CBQ_LSS_MINIDLE
) {
404 lss
.minidle
<<= lss
.ewma_log
;
405 lss
.change
|= TCF_CBQ_LSS_EWMA
;
408 tail
= NLMSG_TAIL(n
);
409 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
411 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
412 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
415 addattr_l(n
, 1024, TCA_CBQ_WRROPT
, &wrr
, sizeof(wrr
));
417 addattr_l(n
, 1024, TCA_CBQ_FOPT
, &fopt
, sizeof(fopt
));
419 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
420 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
424 for (i
= 0; i
< 256; i
++)
425 printf("%u ", rtab
[i
]);
429 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
434 static int cbq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
436 struct rtattr
*tb
[TCA_CBQ_MAX
+1];
437 struct tc_ratespec
*r
= NULL
;
438 struct tc_cbq_lssopt
*lss
= NULL
;
439 struct tc_cbq_wrropt
*wrr
= NULL
;
440 struct tc_cbq_fopt
*fopt
= NULL
;
441 struct tc_cbq_ovl
*ovl
= NULL
;
442 unsigned int linklayer
;
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 int) RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]),
480 (unsigned int) sizeof(*ovl
));
482 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
488 print_rate(buf
, sizeof(buf
), r
->rate
);
489 fprintf(f
, "rate %s ", buf
);
490 linklayer
= (r
->linklayer
& TC_LINKLAYER_MASK
);
491 if (linklayer
> TC_LINKLAYER_ETHERNET
|| show_details
)
492 fprintf(f
, "linklayer %s ", sprint_linklayer(linklayer
, b2
));
494 fprintf(f
, "cell %ub ", 1<<r
->cell_log
);
496 fprintf(f
, "mpu %ub ", r
->mpu
);
498 fprintf(f
, "overhead %ub ", r
->overhead
);
501 if (lss
&& lss
->flags
) {
505 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
506 fprintf(f
, "bounded");
509 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
512 fprintf(f
, "isolated");
517 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
518 fprintf(f
, "prio %u", wrr
->priority
);
520 fprintf(f
, "prio no-transmit");
524 fprintf(f
, "/%u ", wrr
->cpriority
);
525 if (wrr
->weight
!= 1) {
526 print_rate(buf
, sizeof(buf
), wrr
->weight
);
527 fprintf(f
, "weight %s ", buf
);
530 fprintf(f
, "allot %ub ", wrr
->allot
);
533 if (lss
&& show_details
) {
534 fprintf(f
, "\nlevel %u ewma %u avpkt %ub ", lss
->level
, lss
->ewma_log
, lss
->avpkt
);
536 fprintf(f
, "maxidle %s ", sprint_ticks(lss
->maxidle
>>lss
->ewma_log
, b1
));
538 fprintf(f
, "[%08x] ", lss
->maxidle
);
540 if (lss
->minidle
!= 0x7fffffff) {
541 fprintf(f
, "minidle %s ", sprint_ticks(lss
->minidle
>>lss
->ewma_log
, b1
));
543 fprintf(f
, "[%08x] ", lss
->minidle
);
546 fprintf(f
, "offtime %s ", sprint_ticks(lss
->offtime
, b1
));
548 fprintf(f
, "[%08x] ", lss
->offtime
);
551 if (fopt
&& show_details
) {
554 print_tc_classid(buf
, sizeof(buf
), fopt
->split
);
555 fprintf(f
, "\nsplit %s ", buf
);
557 fprintf(f
, "defmap %08x", fopt
->defmap
);
563 static int cbq_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
565 struct tc_cbq_xstats
*st
;
570 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
573 st
= RTA_DATA(xstats
);
574 fprintf(f
, " borrowed %u overactions %u avgidle %g undertime %g", st
->borrows
,
575 st
->overactions
, (double)st
->avgidle
, (double)st
->undertime
);
579 struct qdisc_util cbq_qdisc_util
= {
581 .parse_qopt
= cbq_parse_opt
,
582 .print_qopt
= cbq_print_opt
,
583 .print_xstats
= cbq_print_xstats
,
584 .parse_copt
= cbq_parse_class_opt
,
585 .print_copt
= cbq_print_opt
,