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>
26 static void explain(void)
28 fprintf(stderr
, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
29 fprintf(stderr
, " [ peakrate KBPS ] [ latency TIME ] ");
30 fprintf(stderr
, "[ overhead BYTES ] [ linklayer TYPE ]\n");
33 static void explain1(const char *arg
, const char *val
)
35 fprintf(stderr
, "tbf: illegal value for \"%s\": \"%s\"\n", arg
, val
);
39 static int tbf_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
42 struct tc_tbf_qopt opt
;
45 unsigned buffer
=0, mtu
=0, mpu
=0, latency
=0;
46 int Rcell_log
=-1, Pcell_log
= -1;
47 unsigned short overhead
=0;
48 unsigned int linklayer
= LINKLAYER_ETHERNET
; /* Assume ethernet */
50 __u64 rate64
= 0, prate64
= 0;
52 memset(&opt
, 0, sizeof(opt
));
55 if (matches(*argv
, "limit") == 0) {
58 fprintf(stderr
, "tbf: duplicate \"limit\" specification\n");
62 fprintf(stderr
, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n");
65 if (get_size(&opt
.limit
, *argv
)) {
66 explain1("limit", *argv
);
70 } else if (matches(*argv
, "latency") == 0) {
73 fprintf(stderr
, "tbf: duplicate \"latency\" specification\n");
77 fprintf(stderr
, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n");
80 if (get_time(&latency
, *argv
)) {
81 explain1("latency", *argv
);
85 } else if (matches(*argv
, "burst") == 0 ||
86 strcmp(*argv
, "buffer") == 0 ||
87 strcmp(*argv
, "maxburst") == 0) {
88 const char *parm_name
= *argv
;
91 fprintf(stderr
, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
94 if (get_size_and_cell(&buffer
, &Rcell_log
, *argv
) < 0) {
95 explain1(parm_name
, *argv
);
99 } else if (strcmp(*argv
, "mtu") == 0 ||
100 strcmp(*argv
, "minburst") == 0) {
101 const char *parm_name
= *argv
;
104 fprintf(stderr
, "tbf: duplicate \"mtu/minburst\" specification\n");
107 if (get_size_and_cell(&mtu
, &Pcell_log
, *argv
) < 0) {
108 explain1(parm_name
, *argv
);
112 } else if (strcmp(*argv
, "mpu") == 0) {
115 fprintf(stderr
, "tbf: duplicate \"mpu\" specification\n");
118 if (get_size(&mpu
, *argv
)) {
119 explain1("mpu", *argv
);
123 } else if (strcmp(*argv
, "rate") == 0) {
126 fprintf(stderr
, "tbf: duplicate \"rate\" specification\n");
129 if (get_rate64(&rate64
, *argv
)) {
130 explain1("rate", *argv
);
134 } else if (matches(*argv
, "peakrate") == 0) {
137 fprintf(stderr
, "tbf: duplicate \"peakrate\" specification\n");
140 if (get_rate64(&prate64
, *argv
)) {
141 explain1("peakrate", *argv
);
145 } else if (matches(*argv
, "overhead") == 0) {
148 fprintf(stderr
, "tbf: duplicate \"overhead\" specification\n");
151 if (get_u16(&overhead
, *argv
, 10)) {
152 explain1("overhead", *argv
); return -1;
154 } else if (matches(*argv
, "linklayer") == 0) {
156 if (get_linklayer(&linklayer
, *argv
)) {
157 explain1("linklayer", *argv
); return -1;
159 } else if (strcmp(*argv
, "help") == 0) {
163 fprintf(stderr
, "tbf: unknown parameter \"%s\"\n", *argv
);
172 /* Be nice to the user: try to emit all error messages in
173 * one go rather than reveal one more problem when a
174 * previous one has been fixed.
177 fprintf(stderr
, "tbf: the \"rate\" parameter is mandatory.\n");
181 fprintf(stderr
, "tbf: the \"burst\" parameter is mandatory.\n");
186 fprintf(stderr
, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n");
191 if (opt
.limit
== 0 && latency
== 0) {
192 fprintf(stderr
, "tbf: either \"limit\" or \"latency\" is required.\n");
201 opt
.rate
.rate
= (rate64
>= (1ULL << 32)) ? ~0U : rate64
;
202 opt
.peakrate
.rate
= (prate64
>= (1ULL << 32)) ? ~0U : prate64
;
204 if (opt
.limit
== 0) {
205 double lim
= rate64
*(double)latency
/TIME_UNITS_PER_SEC
+ buffer
;
207 double lim2
= prate64
*(double)latency
/TIME_UNITS_PER_SEC
+ mtu
;
215 opt
.rate
.overhead
= overhead
;
216 if (tc_calc_rtable(&opt
.rate
, rtab
, Rcell_log
, mtu
, linklayer
) < 0) {
217 fprintf(stderr
, "tbf: failed to calculate rate table.\n");
220 opt
.buffer
= tc_calc_xmittime(opt
.rate
.rate
, buffer
);
222 if (opt
.peakrate
.rate
) {
223 opt
.peakrate
.mpu
= mpu
;
224 opt
.peakrate
.overhead
= overhead
;
225 if (tc_calc_rtable(&opt
.peakrate
, ptab
, Pcell_log
, mtu
, linklayer
) < 0) {
226 fprintf(stderr
, "tbf: failed to calculate peak rate table.\n");
229 opt
.mtu
= tc_calc_xmittime(opt
.peakrate
.rate
, mtu
);
232 tail
= NLMSG_TAIL(n
);
233 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
234 addattr_l(n
, 2024, TCA_TBF_PARMS
, &opt
, sizeof(opt
));
235 if (rate64
>= (1ULL << 32))
236 addattr_l(n
, 2124, TCA_TBF_RATE64
, &rate64
, sizeof(rate64
));
237 addattr_l(n
, 3024, TCA_TBF_RTAB
, rtab
, 1024);
238 if (opt
.peakrate
.rate
) {
239 if (prate64
>= (1ULL << 32))
240 addattr_l(n
, 3124, TCA_TBF_PRATE64
, &prate64
, sizeof(prate64
));
241 addattr_l(n
, 4096, TCA_TBF_PTAB
, ptab
, 1024);
243 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
247 static int tbf_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
249 struct rtattr
*tb
[TCA_TBF_MAX
+1];
250 struct tc_tbf_qopt
*qopt
;
251 unsigned int linklayer
;
254 __u64 rate64
= 0, prate64
= 0;
262 parse_rtattr_nested(tb
, TCA_TBF_MAX
, opt
);
264 if (tb
[TCA_TBF_PARMS
] == NULL
)
267 qopt
= RTA_DATA(tb
[TCA_TBF_PARMS
]);
268 if (RTA_PAYLOAD(tb
[TCA_TBF_PARMS
]) < sizeof(*qopt
))
270 rate64
= qopt
->rate
.rate
;
271 if (tb
[TCA_TBF_RATE64
] &&
272 RTA_PAYLOAD(tb
[TCA_TBF_RATE64
]) >= sizeof(rate64
))
273 rate64
= rta_getattr_u64(tb
[TCA_TBF_RATE64
]);
274 fprintf(f
, "rate %s ", sprint_rate(rate64
, b1
));
275 buffer
= tc_calc_xmitsize(rate64
, qopt
->buffer
);
277 fprintf(f
, "burst %s/%u mpu %s ", sprint_size(buffer
, b1
),
278 1<<qopt
->rate
.cell_log
, sprint_size(qopt
->rate
.mpu
, b2
));
280 fprintf(f
, "burst %s ", sprint_size(buffer
, b1
));
283 fprintf(f
, "[%08x] ", qopt
->buffer
);
284 prate64
= qopt
->peakrate
.rate
;
285 if (tb
[TCA_TBF_PRATE64
] &&
286 RTA_PAYLOAD(tb
[TCA_TBF_PRATE64
]) >= sizeof(prate64
))
287 prate64
= rta_getattr_u64(tb
[TCA_TBF_PRATE64
]);
289 fprintf(f
, "peakrate %s ", sprint_rate(prate64
, b1
));
290 if (qopt
->mtu
|| qopt
->peakrate
.mpu
) {
291 mtu
= tc_calc_xmitsize(prate64
, qopt
->mtu
);
293 fprintf(f
, "mtu %s/%u mpu %s ", sprint_size(mtu
, b1
),
294 1<<qopt
->peakrate
.cell_log
, sprint_size(qopt
->peakrate
.mpu
, b2
));
296 fprintf(f
, "minburst %s ", sprint_size(mtu
, b1
));
299 fprintf(f
, "[%08x] ", qopt
->mtu
);
304 fprintf(f
, "limit %s ", sprint_size(qopt
->limit
, b1
));
306 latency
= TIME_UNITS_PER_SEC
*(qopt
->limit
/(double)rate64
) - tc_core_tick2time(qopt
->buffer
);
308 double lat2
= TIME_UNITS_PER_SEC
*(qopt
->limit
/(double)prate64
) - tc_core_tick2time(qopt
->mtu
);
312 fprintf(f
, "lat %s ", sprint_time(latency
, b1
));
314 if (qopt
->rate
.overhead
) {
315 fprintf(f
, "overhead %d", qopt
->rate
.overhead
);
317 linklayer
= (qopt
->rate
.linklayer
& TC_LINKLAYER_MASK
);
318 if (linklayer
> TC_LINKLAYER_ETHERNET
|| show_details
)
319 fprintf(f
, "linklayer %s ", sprint_linklayer(linklayer
, b3
));
324 struct qdisc_util tbf_qdisc_util
= {
326 .parse_qopt
= tbf_parse_opt
,
327 .print_qopt
= tbf_print_opt
,