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
= NLMSG_TAIL(n
);
169 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
170 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
171 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
172 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
176 for (i
= 0; i
< 256; i
++)
177 printf("%u ", rtab
[i
]);
180 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
184 static int cbq_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
186 int wrr_ok
= 0, fopt_ok
= 0;
187 struct tc_ratespec r
= {};
188 struct tc_cbq_lssopt lss
= {};
189 struct tc_cbq_wrropt wrr
= {};
190 struct tc_cbq_fopt fopt
= {};
195 unsigned int bndw
= 0;
196 unsigned minburst
= 0, maxburst
= 0;
197 unsigned short overhead
= 0;
198 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
202 if (matches(*argv
, "rate") == 0) {
204 if (strchr(*argv
, '%')) {
205 if (get_percent_rate(&r
.rate
, *argv
, dev
)) {
209 } else if (get_rate(&r
.rate
, *argv
)) {
213 } else if (matches(*argv
, "bandwidth") == 0) {
215 if (strchr(*argv
, '%')) {
216 if (get_percent_rate(&bndw
, *argv
, dev
)) {
217 explain1("bandwidth");
220 } else if (get_rate(&bndw
, *argv
)) {
221 explain1("bandwidth");
224 } else if (matches(*argv
, "minidle") == 0) {
226 if (get_u32(&lss
.minidle
, *argv
, 0)) {
230 lss
.change
|= TCF_CBQ_LSS_MINIDLE
;
231 } else if (matches(*argv
, "minburst") == 0) {
233 if (get_u32(&minburst
, *argv
, 0)) {
234 explain1("minburst");
237 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
238 } else if (matches(*argv
, "maxburst") == 0) {
240 if (get_u32(&maxburst
, *argv
, 0)) {
241 explain1("maxburst");
244 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
245 } else if (matches(*argv
, "bounded") == 0) {
246 lss
.flags
|= TCF_CBQ_LSS_BOUNDED
;
247 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
248 } else if (matches(*argv
, "borrow") == 0) {
249 lss
.flags
&= ~TCF_CBQ_LSS_BOUNDED
;
250 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
251 } else if (matches(*argv
, "isolated") == 0) {
252 lss
.flags
|= TCF_CBQ_LSS_ISOLATED
;
253 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
254 } else if (matches(*argv
, "sharing") == 0) {
255 lss
.flags
&= ~TCF_CBQ_LSS_ISOLATED
;
256 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
257 } else if (matches(*argv
, "ewma") == 0) {
259 if (get_integer(&ewma_log
, *argv
, 0)) {
264 fprintf(stderr
, "ewma_log must be < 32\n");
267 lss
.change
|= TCF_CBQ_LSS_EWMA
;
268 } else if (matches(*argv
, "cell") == 0) {
273 if (get_size(&cell
, *argv
)) {
277 for (i
= 0; i
< 32; i
++)
281 fprintf(stderr
, "cell must be 2^n\n");
285 } else if (matches(*argv
, "prio") == 0) {
289 if (get_u32(&prio
, *argv
, 0)) {
293 if (prio
> TC_CBQ_MAXPRIO
) {
294 fprintf(stderr
, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO
);
299 } else if (matches(*argv
, "allot") == 0) {
301 if (get_size(&wrr
.allot
, *argv
)) {
305 } else if (matches(*argv
, "avpkt") == 0) {
307 if (get_size(&lss
.avpkt
, *argv
)) {
311 lss
.change
|= TCF_CBQ_LSS_AVPKT
;
312 } else if (matches(*argv
, "mpu") == 0) {
314 if (get_size(&mpu
, *argv
)) {
318 } else if (matches(*argv
, "weight") == 0) {
320 if (get_size(&wrr
.weight
, *argv
)) {
325 } else if (matches(*argv
, "split") == 0) {
327 if (get_tc_classid(&fopt
.split
, *argv
)) {
328 fprintf(stderr
, "Invalid split node ID.\n");
332 } else if (matches(*argv
, "defmap") == 0) {
336 err
= sscanf(*argv
, "%08x/%08x", &fopt
.defmap
, &fopt
.defchange
);
338 fprintf(stderr
, "Invalid defmap, should be MASK32[/MASK]\n");
344 } else if (matches(*argv
, "overhead") == 0) {
346 if (get_u16(&overhead
, *argv
, 10)) {
347 explain1("overhead"); return -1;
349 } else if (matches(*argv
, "linklayer") == 0) {
351 if (get_linklayer(&linklayer
, *argv
)) {
352 explain1("linklayer"); return -1;
354 } else if (matches(*argv
, "help") == 0) {
358 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
365 /* OK. All options are parsed. */
367 /* 1. Prepare link sharing scheduler parameters */
369 unsigned int pktsize
= wrr
.allot
;
371 if (wrr
.allot
< (lss
.avpkt
*3)/2)
372 wrr
.allot
= (lss
.avpkt
*3)/2;
374 r
.overhead
= overhead
;
375 if (tc_calc_rtable(&r
, rtab
, cell_log
, pktsize
, linklayer
) < 0) {
376 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
381 ewma_log
= TC_CBQ_DEF_EWMA
;
382 lss
.ewma_log
= ewma_log
;
383 if (lss
.change
&(TCF_CBQ_LSS_OFFTIME
|TCF_CBQ_LSS_MAXIDLE
)) {
384 if (lss
.avpkt
== 0) {
385 fprintf(stderr
, "CBQ: avpkt is required for max/minburst.\n");
388 if (bndw
== 0 || r
.rate
== 0) {
389 fprintf(stderr
, "CBQ: bandwidth&rate are required for max/minburst.\n");
393 if (wrr
.priority
== 0 && (n
->nlmsg_flags
&NLM_F_EXCL
)) {
395 wrr
.priority
= TC_CBQ_MAXPRIO
;
397 wrr
.allot
= (lss
.avpkt
*3)/2;
401 wrr
.weight
= (wrr
.priority
== TC_CBQ_MAXPRIO
) ? 1 : r
.rate
;
402 if (wrr
.allot
== 0) {
403 fprintf(stderr
, "CBQ: \"allot\" is required to set WRR parameters.\n");
407 if (lss
.change
&TCF_CBQ_LSS_MAXIDLE
) {
408 lss
.maxidle
= tc_cbq_calc_maxidle(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, maxburst
);
409 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
410 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
412 if (lss
.change
&TCF_CBQ_LSS_OFFTIME
) {
413 lss
.offtime
= tc_cbq_calc_offtime(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, minburst
);
414 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
415 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
417 if (lss
.change
&TCF_CBQ_LSS_MINIDLE
) {
418 lss
.minidle
<<= lss
.ewma_log
;
419 lss
.change
|= TCF_CBQ_LSS_EWMA
;
422 tail
= NLMSG_TAIL(n
);
423 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
425 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
426 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
429 addattr_l(n
, 1024, TCA_CBQ_WRROPT
, &wrr
, sizeof(wrr
));
431 addattr_l(n
, 1024, TCA_CBQ_FOPT
, &fopt
, sizeof(fopt
));
433 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
434 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
438 for (i
= 0; i
< 256; i
++)
439 printf("%u ", rtab
[i
]);
443 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
448 static int cbq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
450 struct rtattr
*tb
[TCA_CBQ_MAX
+1];
451 struct tc_ratespec
*r
= NULL
;
452 struct tc_cbq_lssopt
*lss
= NULL
;
453 struct tc_cbq_wrropt
*wrr
= NULL
;
454 struct tc_cbq_fopt
*fopt
= NULL
;
455 struct tc_cbq_ovl
*ovl
= NULL
;
456 unsigned int linklayer
;
464 parse_rtattr_nested(tb
, TCA_CBQ_MAX
, opt
);
466 if (tb
[TCA_CBQ_RATE
]) {
467 if (RTA_PAYLOAD(tb
[TCA_CBQ_RATE
]) < sizeof(*r
))
468 fprintf(stderr
, "CBQ: too short rate opt\n");
470 r
= RTA_DATA(tb
[TCA_CBQ_RATE
]);
472 if (tb
[TCA_CBQ_LSSOPT
]) {
473 if (RTA_PAYLOAD(tb
[TCA_CBQ_LSSOPT
]) < sizeof(*lss
))
474 fprintf(stderr
, "CBQ: too short lss opt\n");
476 lss
= RTA_DATA(tb
[TCA_CBQ_LSSOPT
]);
478 if (tb
[TCA_CBQ_WRROPT
]) {
479 if (RTA_PAYLOAD(tb
[TCA_CBQ_WRROPT
]) < sizeof(*wrr
))
480 fprintf(stderr
, "CBQ: too short wrr opt\n");
482 wrr
= RTA_DATA(tb
[TCA_CBQ_WRROPT
]);
484 if (tb
[TCA_CBQ_FOPT
]) {
485 if (RTA_PAYLOAD(tb
[TCA_CBQ_FOPT
]) < sizeof(*fopt
))
486 fprintf(stderr
, "CBQ: too short fopt\n");
488 fopt
= RTA_DATA(tb
[TCA_CBQ_FOPT
]);
490 if (tb
[TCA_CBQ_OVL_STRATEGY
]) {
491 if (RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]) < sizeof(*ovl
))
492 fprintf(stderr
, "CBQ: too short overlimit strategy %u/%u\n",
493 (unsigned int) RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]),
494 (unsigned int) sizeof(*ovl
));
496 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
502 print_rate(buf
, sizeof(buf
), r
->rate
);
503 fprintf(f
, "rate %s ", buf
);
504 linklayer
= (r
->linklayer
& TC_LINKLAYER_MASK
);
505 if (linklayer
> TC_LINKLAYER_ETHERNET
|| show_details
)
506 fprintf(f
, "linklayer %s ", sprint_linklayer(linklayer
, b2
));
508 fprintf(f
, "cell %ub ", 1<<r
->cell_log
);
510 fprintf(f
, "mpu %ub ", r
->mpu
);
512 fprintf(f
, "overhead %ub ", r
->overhead
);
515 if (lss
&& lss
->flags
) {
519 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
520 fprintf(f
, "bounded");
523 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
526 fprintf(f
, "isolated");
531 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
532 fprintf(f
, "prio %u", wrr
->priority
);
534 fprintf(f
, "prio no-transmit");
538 fprintf(f
, "/%u ", wrr
->cpriority
);
539 if (wrr
->weight
!= 1) {
540 print_rate(buf
, sizeof(buf
), wrr
->weight
);
541 fprintf(f
, "weight %s ", buf
);
544 fprintf(f
, "allot %ub ", wrr
->allot
);
547 if (lss
&& show_details
) {
548 fprintf(f
, "\nlevel %u ewma %u avpkt %ub ", lss
->level
, lss
->ewma_log
, lss
->avpkt
);
550 fprintf(f
, "maxidle %s ", sprint_ticks(lss
->maxidle
>>lss
->ewma_log
, b1
));
552 fprintf(f
, "[%08x] ", lss
->maxidle
);
554 if (lss
->minidle
!= 0x7fffffff) {
555 fprintf(f
, "minidle %s ", sprint_ticks(lss
->minidle
>>lss
->ewma_log
, b1
));
557 fprintf(f
, "[%08x] ", lss
->minidle
);
560 fprintf(f
, "offtime %s ", sprint_ticks(lss
->offtime
, b1
));
562 fprintf(f
, "[%08x] ", lss
->offtime
);
565 if (fopt
&& show_details
) {
568 print_tc_classid(buf
, sizeof(buf
), fopt
->split
);
569 fprintf(f
, "\nsplit %s ", buf
);
571 fprintf(f
, "defmap %08x", fopt
->defmap
);
577 static int cbq_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
579 struct tc_cbq_xstats
*st
;
584 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
587 st
= RTA_DATA(xstats
);
588 fprintf(f
, " borrowed %u overactions %u avgidle %g undertime %g", st
->borrows
,
589 st
->overactions
, (double)st
->avgidle
, (double)st
->undertime
);
593 struct qdisc_util cbq_qdisc_util
= {
595 .parse_qopt
= cbq_parse_opt
,
596 .print_qopt
= cbq_print_opt
,
597 .print_xstats
= cbq_print_xstats
,
598 .parse_copt
= cbq_parse_class_opt
,
599 .print_copt
= cbq_print_opt
,