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