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(char *arg
)
35 fprintf(stderr
, "Illegal \"%s\"\n", arg
);
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 */
51 memset(&opt
, 0, sizeof(opt
));
54 if (matches(*argv
, "limit") == 0) {
56 if (opt
.limit
|| latency
) {
57 fprintf(stderr
, "Double \"limit/latency\" spec\n");
60 if (get_size(&opt
.limit
, *argv
)) {
65 } else if (matches(*argv
, "latency") == 0) {
67 if (opt
.limit
|| latency
) {
68 fprintf(stderr
, "Double \"limit/latency\" spec\n");
71 if (get_time(&latency
, *argv
)) {
76 } else if (matches(*argv
, "burst") == 0 ||
77 strcmp(*argv
, "buffer") == 0 ||
78 strcmp(*argv
, "maxburst") == 0) {
81 fprintf(stderr
, "Double \"buffer/burst\" spec\n");
84 if (get_size_and_cell(&buffer
, &Rcell_log
, *argv
) < 0) {
89 } else if (strcmp(*argv
, "mtu") == 0 ||
90 strcmp(*argv
, "minburst") == 0) {
93 fprintf(stderr
, "Double \"mtu/minburst\" spec\n");
96 if (get_size_and_cell(&mtu
, &Pcell_log
, *argv
) < 0) {
101 } else if (strcmp(*argv
, "mpu") == 0) {
104 fprintf(stderr
, "Double \"mpu\" spec\n");
107 if (get_size(&mpu
, *argv
)) {
112 } else if (strcmp(*argv
, "rate") == 0) {
115 fprintf(stderr
, "Double \"rate\" spec\n");
118 if (get_rate(&opt
.rate
.rate
, *argv
)) {
123 } else if (matches(*argv
, "peakrate") == 0) {
125 if (opt
.peakrate
.rate
) {
126 fprintf(stderr
, "Double \"peakrate\" spec\n");
129 if (get_rate(&opt
.peakrate
.rate
, *argv
)) {
130 explain1("peakrate");
134 } else if (matches(*argv
, "overhead") == 0) {
137 fprintf(stderr
, "Double \"overhead\" spec\n");
140 if (get_u16(&overhead
, *argv
, 10)) {
141 explain1("overhead"); return -1;
143 } else if (matches(*argv
, "linklayer") == 0) {
145 if (get_linklayer(&linklayer
, *argv
)) {
146 explain1("linklayer"); return -1;
148 } else if (strcmp(*argv
, "help") == 0) {
152 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
164 if (opt
.rate
.rate
== 0 || !buffer
) {
165 fprintf(stderr
, "Both \"rate\" and \"burst\" are required.\n");
168 if (opt
.peakrate
.rate
) {
170 fprintf(stderr
, "\"mtu\" is required, if \"peakrate\" is requested.\n");
175 if (opt
.limit
== 0 && latency
== 0) {
176 fprintf(stderr
, "Either \"limit\" or \"latency\" are required.\n");
180 if (opt
.limit
== 0) {
181 double lim
= opt
.rate
.rate
*(double)latency
/TIME_UNITS_PER_SEC
+ buffer
;
182 if (opt
.peakrate
.rate
) {
183 double lim2
= opt
.peakrate
.rate
*(double)latency
/TIME_UNITS_PER_SEC
+ mtu
;
191 opt
.rate
.overhead
= overhead
;
192 if (tc_calc_rtable(&opt
.rate
, rtab
, Rcell_log
, mtu
, linklayer
) < 0) {
193 fprintf(stderr
, "TBF: failed to calculate rate table.\n");
196 opt
.buffer
= tc_calc_xmittime(opt
.rate
.rate
, buffer
);
198 if (opt
.peakrate
.rate
) {
199 opt
.peakrate
.mpu
= mpu
;
200 opt
.peakrate
.overhead
= overhead
;
201 if (tc_calc_rtable(&opt
.peakrate
, ptab
, Pcell_log
, mtu
, linklayer
) < 0) {
202 fprintf(stderr
, "TBF: failed to calculate peak rate table.\n");
205 opt
.mtu
= tc_calc_xmittime(opt
.peakrate
.rate
, mtu
);
208 tail
= NLMSG_TAIL(n
);
209 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
210 addattr_l(n
, 2024, TCA_TBF_PARMS
, &opt
, sizeof(opt
));
211 addattr_l(n
, 3024, TCA_TBF_RTAB
, rtab
, 1024);
212 if (opt
.peakrate
.rate
)
213 addattr_l(n
, 4096, TCA_TBF_PTAB
, ptab
, 1024);
214 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
218 static int tbf_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
220 struct rtattr
*tb
[TCA_TBF_PTAB
+1];
221 struct tc_tbf_qopt
*qopt
;
230 parse_rtattr_nested(tb
, TCA_TBF_PTAB
, opt
);
232 if (tb
[TCA_TBF_PARMS
] == NULL
)
235 qopt
= RTA_DATA(tb
[TCA_TBF_PARMS
]);
236 if (RTA_PAYLOAD(tb
[TCA_TBF_PARMS
]) < sizeof(*qopt
))
238 fprintf(f
, "rate %s ", sprint_rate(qopt
->rate
.rate
, b1
));
239 buffer
= tc_calc_xmitsize(qopt
->rate
.rate
, qopt
->buffer
);
241 fprintf(f
, "burst %s/%u mpu %s ", sprint_size(buffer
, b1
),
242 1<<qopt
->rate
.cell_log
, sprint_size(qopt
->rate
.mpu
, b2
));
244 fprintf(f
, "burst %s ", sprint_size(buffer
, b1
));
247 fprintf(f
, "[%08x] ", qopt
->buffer
);
248 if (qopt
->peakrate
.rate
) {
249 fprintf(f
, "peakrate %s ", sprint_rate(qopt
->peakrate
.rate
, b1
));
250 if (qopt
->mtu
|| qopt
->peakrate
.mpu
) {
251 mtu
= tc_calc_xmitsize(qopt
->peakrate
.rate
, qopt
->mtu
);
253 fprintf(f
, "mtu %s/%u mpu %s ", sprint_size(mtu
, b1
),
254 1<<qopt
->peakrate
.cell_log
, sprint_size(qopt
->peakrate
.mpu
, b2
));
256 fprintf(f
, "minburst %s ", sprint_size(mtu
, b1
));
259 fprintf(f
, "[%08x] ", qopt
->mtu
);
264 fprintf(f
, "limit %s ", sprint_size(qopt
->limit
, b1
));
266 latency
= TIME_UNITS_PER_SEC
*(qopt
->limit
/(double)qopt
->rate
.rate
) - tc_core_tick2time(qopt
->buffer
);
267 if (qopt
->peakrate
.rate
) {
268 double lat2
= TIME_UNITS_PER_SEC
*(qopt
->limit
/(double)qopt
->peakrate
.rate
) - tc_core_tick2time(qopt
->mtu
);
272 fprintf(f
, "lat %s ", sprint_time(latency
, b1
));
274 if (qopt
->rate
.overhead
) {
275 fprintf(f
, "overhead %d", qopt
->rate
.overhead
);
281 struct qdisc_util tbf_qdisc_util
= {
283 .parse_qopt
= tbf_parse_opt
,
284 .print_qopt
= tbf_print_opt
,