]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_tbf.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_tbf.c
1 /*
2 * q_tbf.c TBF.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21
22 #include "utils.h"
23 #include "tc_util.h"
24
25 static void explain(void)
26 {
27 fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
28 fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ] ");
29 fprintf(stderr, "[ overhead BYTES ] [ linklayer TYPE ]\n");
30 }
31
32 static void explain1(const char *arg, const char *val)
33 {
34 fprintf(stderr, "tbf: illegal value for \"%s\": \"%s\"\n", arg, val);
35 }
36
37
38 static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
39 {
40 int ok = 0;
41 struct tc_tbf_qopt opt = {};
42 __u32 rtab[256];
43 __u32 ptab[256];
44 unsigned buffer = 0, mtu = 0, mpu = 0, latency = 0;
45 int Rcell_log = -1, Pcell_log = -1;
46 unsigned short overhead = 0;
47 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
48 struct rtattr *tail;
49 __u64 rate64 = 0, prate64 = 0;
50
51 while (argc > 0) {
52 if (matches(*argv, "limit") == 0) {
53 NEXT_ARG();
54 if (opt.limit) {
55 fprintf(stderr, "tbf: duplicate \"limit\" specification\n");
56 return -1;
57 }
58 if (latency) {
59 fprintf(stderr, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n");
60 return -1;
61 }
62 if (get_size(&opt.limit, *argv)) {
63 explain1("limit", *argv);
64 return -1;
65 }
66 ok++;
67 } else if (matches(*argv, "latency") == 0) {
68 NEXT_ARG();
69 if (latency) {
70 fprintf(stderr, "tbf: duplicate \"latency\" specification\n");
71 return -1;
72 }
73 if (opt.limit) {
74 fprintf(stderr, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n");
75 return -1;
76 }
77 if (get_time(&latency, *argv)) {
78 explain1("latency", *argv);
79 return -1;
80 }
81 ok++;
82 } else if (matches(*argv, "burst") == 0 ||
83 strcmp(*argv, "buffer") == 0 ||
84 strcmp(*argv, "maxburst") == 0) {
85 const char *parm_name = *argv;
86
87 NEXT_ARG();
88 if (buffer) {
89 fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
90 return -1;
91 }
92 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
93 explain1(parm_name, *argv);
94 return -1;
95 }
96 ok++;
97 } else if (strcmp(*argv, "mtu") == 0 ||
98 strcmp(*argv, "minburst") == 0) {
99 const char *parm_name = *argv;
100
101 NEXT_ARG();
102 if (mtu) {
103 fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n");
104 return -1;
105 }
106 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
107 explain1(parm_name, *argv);
108 return -1;
109 }
110 ok++;
111 } else if (strcmp(*argv, "mpu") == 0) {
112 NEXT_ARG();
113 if (mpu) {
114 fprintf(stderr, "tbf: duplicate \"mpu\" specification\n");
115 return -1;
116 }
117 if (get_size(&mpu, *argv)) {
118 explain1("mpu", *argv);
119 return -1;
120 }
121 ok++;
122 } else if (strcmp(*argv, "rate") == 0) {
123 NEXT_ARG();
124 if (rate64) {
125 fprintf(stderr, "tbf: duplicate \"rate\" specification\n");
126 return -1;
127 }
128 if (strchr(*argv, '%')) {
129 if (get_percent_rate64(&rate64, *argv, dev)) {
130 explain1("rate", *argv);
131 return -1;
132 }
133 } else if (get_rate64(&rate64, *argv)) {
134 explain1("rate", *argv);
135 return -1;
136 }
137 ok++;
138 } else if (matches(*argv, "peakrate") == 0) {
139 NEXT_ARG();
140 if (prate64) {
141 fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n");
142 return -1;
143 }
144 if (strchr(*argv, '%')) {
145 if (get_percent_rate64(&prate64, *argv, dev)) {
146 explain1("peakrate", *argv);
147 return -1;
148 }
149 } else if (get_rate64(&prate64, *argv)) {
150 explain1("peakrate", *argv);
151 return -1;
152 }
153 ok++;
154 } else if (matches(*argv, "overhead") == 0) {
155 NEXT_ARG();
156 if (overhead) {
157 fprintf(stderr, "tbf: duplicate \"overhead\" specification\n");
158 return -1;
159 }
160 if (get_u16(&overhead, *argv, 10)) {
161 explain1("overhead", *argv); return -1;
162 }
163 } else if (matches(*argv, "linklayer") == 0) {
164 NEXT_ARG();
165 if (get_linklayer(&linklayer, *argv)) {
166 explain1("linklayer", *argv); return -1;
167 }
168 } else if (strcmp(*argv, "help") == 0) {
169 explain();
170 return -1;
171 } else {
172 fprintf(stderr, "tbf: unknown parameter \"%s\"\n", *argv);
173 explain();
174 return -1;
175 }
176 argc--; argv++;
177 }
178
179 int verdict = 0;
180
181 /* Be nice to the user: try to emit all error messages in
182 * one go rather than reveal one more problem when a
183 * previous one has been fixed.
184 */
185 if (rate64 == 0) {
186 fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n");
187 verdict = -1;
188 }
189 if (!buffer) {
190 fprintf(stderr, "tbf: the \"burst\" parameter is mandatory.\n");
191 verdict = -1;
192 }
193 if (prate64) {
194 if (!mtu) {
195 fprintf(stderr, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n");
196 verdict = -1;
197 }
198 }
199
200 if (opt.limit == 0 && latency == 0) {
201 fprintf(stderr, "tbf: either \"limit\" or \"latency\" is required.\n");
202 verdict = -1;
203 }
204
205 if (verdict != 0) {
206 explain();
207 return verdict;
208 }
209
210 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
211 opt.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
212
213 if (opt.limit == 0) {
214 double lim = rate64*(double)latency/TIME_UNITS_PER_SEC + buffer;
215
216 if (prate64) {
217 double lim2 = prate64*(double)latency/TIME_UNITS_PER_SEC + mtu;
218
219 if (lim2 < lim)
220 lim = lim2;
221 }
222 opt.limit = lim;
223 }
224
225 opt.rate.mpu = mpu;
226 opt.rate.overhead = overhead;
227 if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
228 fprintf(stderr, "tbf: failed to calculate rate table.\n");
229 return -1;
230 }
231 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
232
233 if (opt.peakrate.rate) {
234 opt.peakrate.mpu = mpu;
235 opt.peakrate.overhead = overhead;
236 if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
237 fprintf(stderr, "tbf: failed to calculate peak rate table.\n");
238 return -1;
239 }
240 opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
241 }
242
243 tail = NLMSG_TAIL(n);
244 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
245 addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
246 addattr_l(n, 2124, TCA_TBF_BURST, &buffer, sizeof(buffer));
247 if (rate64 >= (1ULL << 32))
248 addattr_l(n, 2124, TCA_TBF_RATE64, &rate64, sizeof(rate64));
249 addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
250 if (opt.peakrate.rate) {
251 if (prate64 >= (1ULL << 32))
252 addattr_l(n, 3124, TCA_TBF_PRATE64, &prate64, sizeof(prate64));
253 addattr_l(n, 3224, TCA_TBF_PBURST, &mtu, sizeof(mtu));
254 addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
255 }
256 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
257 return 0;
258 }
259
260 static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
261 {
262 struct rtattr *tb[TCA_TBF_MAX+1];
263 struct tc_tbf_qopt *qopt;
264 unsigned int linklayer;
265 double buffer, mtu;
266 double latency;
267 __u64 rate64 = 0, prate64 = 0;
268
269 SPRINT_BUF(b1);
270 SPRINT_BUF(b2);
271 SPRINT_BUF(b3);
272
273 if (opt == NULL)
274 return 0;
275
276 parse_rtattr_nested(tb, TCA_TBF_MAX, opt);
277
278 if (tb[TCA_TBF_PARMS] == NULL)
279 return -1;
280
281 qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
282 if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
283 return -1;
284 rate64 = qopt->rate.rate;
285 if (tb[TCA_TBF_RATE64] &&
286 RTA_PAYLOAD(tb[TCA_TBF_RATE64]) >= sizeof(rate64))
287 rate64 = rta_getattr_u64(tb[TCA_TBF_RATE64]);
288 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
289 buffer = tc_calc_xmitsize(rate64, qopt->buffer);
290 if (show_details) {
291 fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
292 1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
293 } else {
294 fprintf(f, "burst %s ", sprint_size(buffer, b1));
295 }
296 if (show_raw)
297 fprintf(f, "[%08x] ", qopt->buffer);
298 prate64 = qopt->peakrate.rate;
299 if (tb[TCA_TBF_PRATE64] &&
300 RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64))
301 prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]);
302 if (prate64) {
303 fprintf(f, "peakrate %s ", sprint_rate(prate64, b1));
304 if (qopt->mtu || qopt->peakrate.mpu) {
305 mtu = tc_calc_xmitsize(prate64, qopt->mtu);
306 if (show_details) {
307 fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
308 1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
309 } else {
310 fprintf(f, "minburst %s ", sprint_size(mtu, b1));
311 }
312 if (show_raw)
313 fprintf(f, "[%08x] ", qopt->mtu);
314 }
315 }
316
317 latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)rate64) - tc_core_tick2time(qopt->buffer);
318 if (prate64) {
319 double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)prate64) - tc_core_tick2time(qopt->mtu);
320
321 if (lat2 > latency)
322 latency = lat2;
323 }
324 if (latency >= 0.0)
325 fprintf(f, "lat %s ", sprint_time(latency, b1));
326 if (show_raw || latency < 0.0)
327 fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
328
329 if (qopt->rate.overhead) {
330 fprintf(f, "overhead %d", qopt->rate.overhead);
331 }
332 linklayer = (qopt->rate.linklayer & TC_LINKLAYER_MASK);
333 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
334 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
335
336 return 0;
337 }
338
339 struct qdisc_util tbf_qdisc_util = {
340 .id = "tbf",
341 .parse_qopt = tbf_parse_opt,
342 .print_qopt = tbf_print_opt,
343 };