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 ]\n");
32 static void explain1(char *arg
)
34 fprintf(stderr
, "Illegal \"%s\"\n", arg
);
38 #define usage() return(-1)
40 static int tbf_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
43 struct tc_tbf_qopt opt
;
46 unsigned buffer
=0, mtu
=0, mpu
=0, latency
=0;
47 int Rcell_log
=-1, Pcell_log
= -1;
50 memset(&opt
, 0, sizeof(opt
));
53 if (matches(*argv
, "limit") == 0) {
55 if (opt
.limit
|| latency
) {
56 fprintf(stderr
, "Double \"limit/latency\" spec\n");
59 if (get_size(&opt
.limit
, *argv
)) {
64 } else if (matches(*argv
, "latency") == 0) {
66 if (opt
.limit
|| latency
) {
67 fprintf(stderr
, "Double \"limit/latency\" spec\n");
70 if (get_usecs(&latency
, *argv
)) {
75 } else if (matches(*argv
, "burst") == 0 ||
76 strcmp(*argv
, "buffer") == 0 ||
77 strcmp(*argv
, "maxburst") == 0) {
80 fprintf(stderr
, "Double \"buffer/burst\" spec\n");
83 if (get_size_and_cell(&buffer
, &Rcell_log
, *argv
) < 0) {
88 } else if (strcmp(*argv
, "mtu") == 0 ||
89 strcmp(*argv
, "minburst") == 0) {
92 fprintf(stderr
, "Double \"mtu/minburst\" spec\n");
95 if (get_size_and_cell(&mtu
, &Pcell_log
, *argv
) < 0) {
100 } else if (strcmp(*argv
, "mpu") == 0) {
103 fprintf(stderr
, "Double \"mpu\" spec\n");
106 if (get_size(&mpu
, *argv
)) {
111 } else if (strcmp(*argv
, "rate") == 0) {
114 fprintf(stderr
, "Double \"rate\" spec\n");
117 if (get_rate(&opt
.rate
.rate
, *argv
)) {
122 } else if (matches(*argv
, "peakrate") == 0) {
124 if (opt
.peakrate
.rate
) {
125 fprintf(stderr
, "Double \"peakrate\" spec\n");
128 if (get_rate(&opt
.peakrate
.rate
, *argv
)) {
129 explain1("peakrate");
133 } else if (strcmp(*argv
, "help") == 0) {
137 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
147 if (opt
.rate
.rate
== 0 || !buffer
) {
148 fprintf(stderr
, "Both \"rate\" and \"burst\" are required.\n");
151 if (opt
.peakrate
.rate
) {
153 fprintf(stderr
, "\"mtu\" is required, if \"peakrate\" is requested.\n");
158 if (opt
.limit
== 0 && latency
== 0) {
159 fprintf(stderr
, "Either \"limit\" or \"latency\" are required.\n");
163 if (opt
.limit
== 0) {
164 double lim
= opt
.rate
.rate
*(double)latency
/1000000 + buffer
;
165 if (opt
.peakrate
.rate
) {
166 double lim2
= opt
.peakrate
.rate
*(double)latency
/1000000 + mtu
;
173 if ((Rcell_log
= tc_calc_rtable(opt
.rate
.rate
, rtab
, Rcell_log
, mtu
, mpu
)) < 0) {
174 fprintf(stderr
, "TBF: failed to calculate rate table.\n");
177 opt
.buffer
= tc_calc_xmittime(opt
.rate
.rate
, buffer
);
178 opt
.rate
.cell_log
= Rcell_log
;
180 if (opt
.peakrate
.rate
) {
181 if ((Pcell_log
= tc_calc_rtable(opt
.peakrate
.rate
, ptab
, Pcell_log
, mtu
, mpu
)) < 0) {
182 fprintf(stderr
, "TBF: failed to calculate peak rate table.\n");
185 opt
.mtu
= tc_calc_xmittime(opt
.peakrate
.rate
, mtu
);
186 opt
.peakrate
.cell_log
= Pcell_log
;
187 opt
.peakrate
.mpu
= mpu
;
190 tail
= NLMSG_TAIL(n
);
191 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
192 addattr_l(n
, 2024, TCA_TBF_PARMS
, &opt
, sizeof(opt
));
193 addattr_l(n
, 3024, TCA_TBF_RTAB
, rtab
, 1024);
194 if (opt
.peakrate
.rate
)
195 addattr_l(n
, 4096, TCA_TBF_PTAB
, ptab
, 1024);
196 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
200 static int tbf_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
202 struct rtattr
*tb
[TCA_TBF_PTAB
+1];
203 struct tc_tbf_qopt
*qopt
;
212 parse_rtattr_nested(tb
, TCA_TBF_PTAB
, opt
);
214 if (tb
[TCA_TBF_PARMS
] == NULL
)
217 qopt
= RTA_DATA(tb
[TCA_TBF_PARMS
]);
218 if (RTA_PAYLOAD(tb
[TCA_TBF_PARMS
]) < sizeof(*qopt
))
220 fprintf(f
, "rate %s ", sprint_rate(qopt
->rate
.rate
, b1
));
221 buffer
= tc_calc_xmitsize(qopt
->rate
.rate
, qopt
->buffer
);
223 fprintf(f
, "burst %s/%u mpu %s ", sprint_size(buffer
, b1
),
224 1<<qopt
->rate
.cell_log
, sprint_size(qopt
->rate
.mpu
, b2
));
226 fprintf(f
, "burst %s ", sprint_size(buffer
, b1
));
229 fprintf(f
, "[%08x] ", qopt
->buffer
);
230 if (qopt
->peakrate
.rate
) {
231 fprintf(f
, "peakrate %s ", sprint_rate(qopt
->peakrate
.rate
, b1
));
232 if (qopt
->mtu
|| qopt
->peakrate
.mpu
) {
233 mtu
= tc_calc_xmitsize(qopt
->peakrate
.rate
, qopt
->mtu
);
235 fprintf(f
, "mtu %s/%u mpu %s ", sprint_size(mtu
, b1
),
236 1<<qopt
->peakrate
.cell_log
, sprint_size(qopt
->peakrate
.mpu
, b2
));
238 fprintf(f
, "minburst %s ", sprint_size(mtu
, b1
));
241 fprintf(f
, "[%08x] ", qopt
->mtu
);
246 fprintf(f
, "limit %s ", sprint_size(qopt
->limit
, b1
));
248 latency
= 1000000*(qopt
->limit
/(double)qopt
->rate
.rate
) - tc_core_tick2usec(qopt
->buffer
);
249 if (qopt
->peakrate
.rate
) {
250 double lat2
= 1000000*(qopt
->limit
/(double)qopt
->peakrate
.rate
) - tc_core_tick2usec(qopt
->mtu
);
254 fprintf(f
, "lat %s ", sprint_usecs(latency
, b1
));
259 struct qdisc_util tbf_qdisc_util
= {
261 .parse_qopt
= tbf_parse_opt
,
262 .print_qopt
= tbf_print_opt
,