Debian Codegraph @

tc/q_tbf.c

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