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");
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
);
48 #define usage() return(-1)
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;
61 memset(&lss
, 0, sizeof(lss
));
62 memset(&r
, 0, sizeof(r
));
65 if (matches(*argv
, "bandwidth") == 0 ||
66 matches(*argv
, "rate") == 0) {
68 if (get_rate(&r
.rate
, *argv
)) {
69 explain1("bandwidth");
72 } else if (matches(*argv
, "ewma") == 0) {
74 if (get_integer(&ewma_log
, *argv
, 0)) {
79 fprintf(stderr
, "ewma_log must be < 32\n");
82 } else if (matches(*argv
, "cell") == 0) {
86 if (get_size(&cell
, *argv
)) {
94 fprintf(stderr
, "cell must be 2^n\n");
98 } else if (matches(*argv
, "avpkt") == 0) {
100 if (get_size(&avpkt
, *argv
)) {
104 } else if (matches(*argv
, "mpu") == 0) {
106 if (get_size(&mpu
, *argv
)) {
110 } else if (matches(*argv
, "allot") == 0) {
112 /* Accept and ignore "allot" for backward compatibility */
113 if (get_size(&allot
, *argv
)) {
117 } else if (matches(*argv
, "overhead") == 0) {
119 if (get_u16(&overhead
, *argv
, 10)) {
120 explain1("overhead"); return -1;
122 } else if (matches(*argv
, "help") == 0) {
126 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
133 /* OK. All options are parsed. */
136 fprintf(stderr
, "CBQ: bandwidth is required parameter.\n");
140 fprintf(stderr
, "CBQ: \"avpkt\" is required.\n");
143 if (allot
< (avpkt
*3)/2)
147 r
.overhead
= overhead
;
148 if (tc_calc_rtable(&r
, rtab
, cell_log
, allot
) < 0) {
149 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
154 ewma_log
= TC_CBQ_DEF_EWMA
;
155 lss
.ewma_log
= ewma_log
;
156 lss
.maxidle
= tc_calc_xmittime(r
.rate
, avpkt
);
157 lss
.change
= TCF_CBQ_LSS_MAXIDLE
|TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
160 tail
= NLMSG_TAIL(n
);
161 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
162 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
163 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
164 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
167 for (i
=0; i
<256; i
++)
168 printf("%u ", rtab
[i
]);
171 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
175 static int cbq_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
177 int wrr_ok
=0, fopt_ok
=0;
178 struct tc_ratespec r
;
179 struct tc_cbq_lssopt lss
;
180 struct tc_cbq_wrropt wrr
;
181 struct tc_cbq_fopt fopt
;
182 struct tc_cbq_ovl ovl
;
188 unsigned minburst
=0, maxburst
=0;
189 unsigned short overhead
=0;
192 memset(&r
, 0, sizeof(r
));
193 memset(&lss
, 0, sizeof(lss
));
194 memset(&wrr
, 0, sizeof(wrr
));
195 memset(&fopt
, 0, sizeof(fopt
));
196 memset(&ovl
, 0, sizeof(ovl
));
199 if (matches(*argv
, "rate") == 0) {
201 if (get_rate(&r
.rate
, *argv
)) {
205 } else if (matches(*argv
, "bandwidth") == 0) {
207 if (get_rate(&bndw
, *argv
)) {
208 explain1("bandwidth");
211 } else if (matches(*argv
, "minidle") == 0) {
213 if (get_u32(&lss
.minidle
, *argv
, 0)) {
217 lss
.change
|= TCF_CBQ_LSS_MINIDLE
;
218 } else if (matches(*argv
, "minburst") == 0) {
220 if (get_u32(&minburst
, *argv
, 0)) {
221 explain1("minburst");
224 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
225 } else if (matches(*argv
, "maxburst") == 0) {
227 if (get_u32(&maxburst
, *argv
, 0)) {
228 explain1("maxburst");
231 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
232 } else if (matches(*argv
, "bounded") == 0) {
233 lss
.flags
|= TCF_CBQ_LSS_BOUNDED
;
234 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
235 } else if (matches(*argv
, "borrow") == 0) {
236 lss
.flags
&= ~TCF_CBQ_LSS_BOUNDED
;
237 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
238 } else if (matches(*argv
, "isolated") == 0) {
239 lss
.flags
|= TCF_CBQ_LSS_ISOLATED
;
240 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
241 } else if (matches(*argv
, "sharing") == 0) {
242 lss
.flags
&= ~TCF_CBQ_LSS_ISOLATED
;
243 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
244 } else if (matches(*argv
, "ewma") == 0) {
246 if (get_integer(&ewma_log
, *argv
, 0)) {
251 fprintf(stderr
, "ewma_log must be < 32\n");
254 lss
.change
|= TCF_CBQ_LSS_EWMA
;
255 } else if (matches(*argv
, "cell") == 0) {
259 if (get_size(&cell
, *argv
)) {
267 fprintf(stderr
, "cell must be 2^n\n");
271 } else if (matches(*argv
, "prio") == 0) {
274 if (get_u32(&prio
, *argv
, 0)) {
278 if (prio
> TC_CBQ_MAXPRIO
) {
279 fprintf(stderr
, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO
);
284 } else if (matches(*argv
, "allot") == 0) {
286 if (get_size(&wrr
.allot
, *argv
)) {
290 } else if (matches(*argv
, "avpkt") == 0) {
292 if (get_size(&lss
.avpkt
, *argv
)) {
296 lss
.change
|= TCF_CBQ_LSS_AVPKT
;
297 } else if (matches(*argv
, "mpu") == 0) {
299 if (get_size(&mpu
, *argv
)) {
303 } else if (matches(*argv
, "weight") == 0) {
305 if (get_size(&wrr
.weight
, *argv
)) {
310 } else if (matches(*argv
, "split") == 0) {
312 if (get_tc_classid(&fopt
.split
, *argv
)) {
313 fprintf(stderr
, "Invalid split node ID.\n");
317 } else if (matches(*argv
, "defmap") == 0) {
320 err
= sscanf(*argv
, "%08x/%08x", &fopt
.defmap
, &fopt
.defchange
);
322 fprintf(stderr
, "Invalid defmap, should be MASK32[/MASK]\n");
328 } else if (matches(*argv
, "overhead") == 0) {
330 if (get_u16(&overhead
, *argv
, 10)) {
331 explain1("overhead"); return -1;
333 } else if (matches(*argv
, "help") == 0) {
337 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
344 /* OK. All options are parsed. */
346 /* 1. Prepare link sharing scheduler parameters */
348 unsigned pktsize
= wrr
.allot
;
349 if (wrr
.allot
< (lss
.avpkt
*3)/2)
350 wrr
.allot
= (lss
.avpkt
*3)/2;
352 r
.overhead
= overhead
;
353 if (tc_calc_rtable(&r
, rtab
, cell_log
, pktsize
) < 0) {
354 fprintf(stderr
, "CBQ: failed to calculate rate table.\n");
359 ewma_log
= TC_CBQ_DEF_EWMA
;
360 lss
.ewma_log
= ewma_log
;
361 if (lss
.change
&(TCF_CBQ_LSS_OFFTIME
|TCF_CBQ_LSS_MAXIDLE
)) {
362 if (lss
.avpkt
== 0) {
363 fprintf(stderr
, "CBQ: avpkt is required for max/minburst.\n");
366 if (bndw
==0 || r
.rate
== 0) {
367 fprintf(stderr
, "CBQ: bandwidth&rate are required for max/minburst.\n");
371 if (wrr
.priority
== 0 && (n
->nlmsg_flags
&NLM_F_EXCL
)) {
373 wrr
.priority
= TC_CBQ_MAXPRIO
;
375 wrr
.allot
= (lss
.avpkt
*3)/2;
379 wrr
.weight
= (wrr
.priority
== TC_CBQ_MAXPRIO
) ? 1 : r
.rate
;
380 if (wrr
.allot
== 0) {
381 fprintf(stderr
, "CBQ: \"allot\" is required to set WRR parameters.\n");
385 if (lss
.change
&TCF_CBQ_LSS_MAXIDLE
) {
386 lss
.maxidle
= tc_cbq_calc_maxidle(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, maxburst
);
387 lss
.change
|= TCF_CBQ_LSS_MAXIDLE
;
388 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
390 if (lss
.change
&TCF_CBQ_LSS_OFFTIME
) {
391 lss
.offtime
= tc_cbq_calc_offtime(bndw
, r
.rate
, lss
.avpkt
, ewma_log
, minburst
);
392 lss
.change
|= TCF_CBQ_LSS_OFFTIME
;
393 lss
.change
|= TCF_CBQ_LSS_EWMA
|TCF_CBQ_LSS_AVPKT
;
395 if (lss
.change
&TCF_CBQ_LSS_MINIDLE
) {
396 lss
.minidle
<<= lss
.ewma_log
;
397 lss
.change
|= TCF_CBQ_LSS_EWMA
;
400 tail
= NLMSG_TAIL(n
);
401 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
403 lss
.change
|= TCF_CBQ_LSS_FLAGS
;
404 addattr_l(n
, 1024, TCA_CBQ_LSSOPT
, &lss
, sizeof(lss
));
407 addattr_l(n
, 1024, TCA_CBQ_WRROPT
, &wrr
, sizeof(wrr
));
409 addattr_l(n
, 1024, TCA_CBQ_FOPT
, &fopt
, sizeof(fopt
));
411 addattr_l(n
, 1024, TCA_CBQ_RATE
, &r
, sizeof(r
));
412 addattr_l(n
, 3024, TCA_CBQ_RTAB
, rtab
, 1024);
415 for (i
=0; i
<256; i
++)
416 printf("%u ", rtab
[i
]);
420 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
425 static int cbq_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
427 struct rtattr
*tb
[TCA_CBQ_MAX
+1];
428 struct tc_ratespec
*r
= NULL
;
429 struct tc_cbq_lssopt
*lss
= NULL
;
430 struct tc_cbq_wrropt
*wrr
= NULL
;
431 struct tc_cbq_fopt
*fopt
= NULL
;
432 struct tc_cbq_ovl
*ovl
= NULL
;
438 parse_rtattr_nested(tb
, TCA_CBQ_MAX
, opt
);
440 if (tb
[TCA_CBQ_RATE
]) {
441 if (RTA_PAYLOAD(tb
[TCA_CBQ_RATE
]) < sizeof(*r
))
442 fprintf(stderr
, "CBQ: too short rate opt\n");
444 r
= RTA_DATA(tb
[TCA_CBQ_RATE
]);
446 if (tb
[TCA_CBQ_LSSOPT
]) {
447 if (RTA_PAYLOAD(tb
[TCA_CBQ_LSSOPT
]) < sizeof(*lss
))
448 fprintf(stderr
, "CBQ: too short lss opt\n");
450 lss
= RTA_DATA(tb
[TCA_CBQ_LSSOPT
]);
452 if (tb
[TCA_CBQ_WRROPT
]) {
453 if (RTA_PAYLOAD(tb
[TCA_CBQ_WRROPT
]) < sizeof(*wrr
))
454 fprintf(stderr
, "CBQ: too short wrr opt\n");
456 wrr
= RTA_DATA(tb
[TCA_CBQ_WRROPT
]);
458 if (tb
[TCA_CBQ_FOPT
]) {
459 if (RTA_PAYLOAD(tb
[TCA_CBQ_FOPT
]) < sizeof(*fopt
))
460 fprintf(stderr
, "CBQ: too short fopt\n");
462 fopt
= RTA_DATA(tb
[TCA_CBQ_FOPT
]);
464 if (tb
[TCA_CBQ_OVL_STRATEGY
]) {
465 if (RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]) < sizeof(*ovl
))
466 fprintf(stderr
, "CBQ: too short overlimit strategy %u/%u\n",
467 (unsigned) RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]),
468 (unsigned) sizeof(*ovl
));
470 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
475 print_rate(buf
, sizeof(buf
), r
->rate
);
476 fprintf(f
, "rate %s ", buf
);
478 fprintf(f
, "cell %ub ", 1<<r
->cell_log
);
480 fprintf(f
, "mpu %ub ", r
->mpu
);
482 fprintf(f
, "overhead %ub ", r
->overhead
);
485 if (lss
&& lss
->flags
) {
488 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
489 fprintf(f
, "bounded");
492 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
495 fprintf(f
, "isolated");
500 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
501 fprintf(f
, "prio %u", wrr
->priority
);
503 fprintf(f
, "prio no-transmit");
506 fprintf(f
, "/%u ", wrr
->cpriority
);
507 if (wrr
->weight
!= 1) {
508 print_rate(buf
, sizeof(buf
), wrr
->weight
);
509 fprintf(f
, "weight %s ", buf
);
512 fprintf(f
, "allot %ub ", wrr
->allot
);
515 if (lss
&& show_details
) {
516 fprintf(f
, "\nlevel %u ewma %u avpkt %ub ", lss
->level
, lss
->ewma_log
, lss
->avpkt
);
518 fprintf(f
, "maxidle %s ", sprint_ticks(lss
->maxidle
>>lss
->ewma_log
, b1
));
520 fprintf(f
, "[%08x] ", lss
->maxidle
);
522 if (lss
->minidle
!=0x7fffffff) {
523 fprintf(f
, "minidle %s ", sprint_ticks(lss
->minidle
>>lss
->ewma_log
, b1
));
525 fprintf(f
, "[%08x] ", lss
->minidle
);
528 fprintf(f
, "offtime %s ", sprint_ticks(lss
->offtime
, b1
));
530 fprintf(f
, "[%08x] ", lss
->offtime
);
533 if (fopt
&& show_details
) {
535 print_tc_classid(buf
, sizeof(buf
), fopt
->split
);
536 fprintf(f
, "\nsplit %s ", buf
);
538 fprintf(f
, "defmap %08x", fopt
->defmap
);
544 static int cbq_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
546 struct tc_cbq_xstats
*st
;
551 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
554 st
= RTA_DATA(xstats
);
555 fprintf(f
, " borrowed %u overactions %u avgidle %g undertime %g", st
->borrows
,
556 st
->overactions
, (double)st
->avgidle
, (double)st
->undertime
);
560 struct qdisc_util cbq_qdisc_util
= {
562 .parse_qopt
= cbq_parse_opt
,
563 .print_qopt
= cbq_print_opt
,
564 .print_xstats
= cbq_print_xstats
,
565 .parse_copt
= cbq_parse_class_opt
,
566 .print_copt
= cbq_print_opt
,