]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * tc_util.c Misc TC utility functions. | |
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> | |
aba5acdf SH |
16 | #include <fcntl.h> |
17 | #include <sys/socket.h> | |
dd9cc0ee | 18 | #include <sys/param.h> |
aba5acdf SH |
19 | #include <netinet/in.h> |
20 | #include <arpa/inet.h> | |
21 | #include <string.h> | |
22 | #include <math.h> | |
8b90a990 | 23 | #include <errno.h> |
aba5acdf SH |
24 | |
25 | #include "utils.h" | |
4612d04d | 26 | #include "names.h" |
aba5acdf | 27 | #include "tc_util.h" |
4612d04d | 28 | #include "tc_common.h" |
aba5acdf | 29 | |
5e3bb534 | 30 | #ifndef LIBDIR |
5c434a9e | 31 | #define LIBDIR "/usr/lib" |
b514b358 RA |
32 | #endif |
33 | ||
32a121cb | 34 | static struct db_names *cls_names; |
4612d04d | 35 | |
8b90a990 | 36 | #define NAMES_DB "/etc/iproute2/tc_cls" |
4612d04d VK |
37 | |
38 | int cls_names_init(char *path) | |
39 | { | |
8b90a990 VK |
40 | int ret; |
41 | ||
42 | cls_names = db_names_alloc(); | |
43 | if (!cls_names) | |
44 | return -1; | |
45 | ||
46 | ret = db_names_load(cls_names, path ?: NAMES_DB); | |
47 | if (ret == -ENOENT && path) { | |
48 | fprintf(stderr, "Can't open class names file: %s\n", path); | |
4612d04d VK |
49 | return -1; |
50 | } | |
8b90a990 VK |
51 | if (ret) { |
52 | db_names_free(cls_names); | |
53 | cls_names = NULL; | |
54 | } | |
4612d04d VK |
55 | |
56 | return 0; | |
57 | } | |
58 | ||
59 | void cls_names_uninit(void) | |
60 | { | |
61 | db_names_free(cls_names); | |
62 | } | |
63 | ||
aa27f88c SH |
64 | const char *get_tc_lib(void) |
65 | { | |
66 | const char *lib_dir; | |
67 | ||
68 | lib_dir = getenv("TC_LIB_DIR"); | |
69 | if (!lib_dir) | |
5e3bb534 | 70 | lib_dir = LIBDIR "/tc/"; |
aa27f88c SH |
71 | |
72 | return lib_dir; | |
73 | } | |
74 | ||
dbd90dc2 | 75 | int get_qdisc_handle(__u32 *h, const char *str) |
aba5acdf SH |
76 | { |
77 | __u32 maj; | |
78 | char *p; | |
79 | ||
80 | maj = TC_H_UNSPEC; | |
81 | if (strcmp(str, "none") == 0) | |
82 | goto ok; | |
83 | maj = strtoul(str, &p, 16); | |
087dec7f | 84 | if (p == str || maj >= (1 << 16)) |
aba5acdf SH |
85 | return -1; |
86 | maj <<= 16; | |
32a121cb | 87 | if (*p != ':' && *p != 0) |
aba5acdf SH |
88 | return -1; |
89 | ok: | |
90 | *h = maj; | |
91 | return 0; | |
92 | } | |
93 | ||
dbd90dc2 | 94 | int get_tc_classid(__u32 *h, const char *str) |
aba5acdf SH |
95 | { |
96 | __u32 maj, min; | |
97 | char *p; | |
98 | ||
99 | maj = TC_H_ROOT; | |
100 | if (strcmp(str, "root") == 0) | |
101 | goto ok; | |
102 | maj = TC_H_UNSPEC; | |
103 | if (strcmp(str, "none") == 0) | |
104 | goto ok; | |
105 | maj = strtoul(str, &p, 16); | |
106 | if (p == str) { | |
107 | maj = 0; | |
108 | if (*p != ':') | |
109 | return -1; | |
110 | } | |
111 | if (*p == ':') { | |
a8b303cc SH |
112 | if (maj >= (1<<16)) |
113 | return -1; | |
aba5acdf SH |
114 | maj <<= 16; |
115 | str = p+1; | |
116 | min = strtoul(str, &p, 16); | |
117 | if (*p != 0) | |
118 | return -1; | |
a8b303cc SH |
119 | if (min >= (1<<16)) |
120 | return -1; | |
aba5acdf SH |
121 | maj |= min; |
122 | } else if (*p != 0) | |
123 | return -1; | |
124 | ||
125 | ok: | |
126 | *h = maj; | |
127 | return 0; | |
128 | } | |
129 | ||
46679bbb | 130 | int print_tc_classid(char *buf, int blen, __u32 h) |
aba5acdf | 131 | { |
46679bbb VK |
132 | SPRINT_BUF(handle) = {}; |
133 | int hlen = SPRINT_BSIZE - 1; | |
4612d04d | 134 | |
aba5acdf | 135 | if (h == TC_H_ROOT) |
4612d04d | 136 | sprintf(handle, "root"); |
aba5acdf | 137 | else if (h == TC_H_UNSPEC) |
46679bbb | 138 | snprintf(handle, hlen, "none"); |
aba5acdf | 139 | else if (TC_H_MAJ(h) == 0) |
46679bbb | 140 | snprintf(handle, hlen, ":%x", TC_H_MIN(h)); |
aba5acdf | 141 | else if (TC_H_MIN(h) == 0) |
46679bbb | 142 | snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16); |
aba5acdf | 143 | else |
46679bbb | 144 | snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h)); |
4612d04d VK |
145 | |
146 | if (use_names) { | |
147 | char clname[IDNAME_MAX] = {}; | |
148 | ||
149 | if (id_to_name(cls_names, h, clname)) | |
46679bbb | 150 | snprintf(buf, blen, "%s#%s", clname, handle); |
4612d04d | 151 | else |
46679bbb | 152 | snprintf(buf, blen, "%s", handle); |
4612d04d | 153 | } else { |
46679bbb | 154 | snprintf(buf, blen, "%s", handle); |
4612d04d VK |
155 | } |
156 | ||
aba5acdf SH |
157 | return 0; |
158 | } | |
159 | ||
4612d04d | 160 | char *sprint_tc_classid(__u32 h, char *buf) |
aba5acdf SH |
161 | { |
162 | if (print_tc_classid(buf, SPRINT_BSIZE-1, h)) | |
163 | strcpy(buf, "???"); | |
164 | return buf; | |
165 | } | |
166 | ||
26ab0b10 SH |
167 | /* See http://physics.nist.gov/cuu/Units/binary.html */ |
168 | static const struct rate_suffix { | |
169 | const char *name; | |
170 | double scale; | |
171 | } suffixes[] = { | |
172 | { "bit", 1. }, | |
173 | { "Kibit", 1024. }, | |
174 | { "kbit", 1000. }, | |
175 | { "mibit", 1024.*1024. }, | |
176 | { "mbit", 1000000. }, | |
177 | { "gibit", 1024.*1024.*1024. }, | |
178 | { "gbit", 1000000000. }, | |
179 | { "tibit", 1024.*1024.*1024.*1024. }, | |
180 | { "tbit", 1000000000000. }, | |
181 | { "Bps", 8. }, | |
182 | { "KiBps", 8.*1024. }, | |
183 | { "KBps", 8000. }, | |
184 | { "MiBps", 8.*1024*1024. }, | |
185 | { "MBps", 8000000. }, | |
186 | { "GiBps", 8.*1024.*1024.*1024. }, | |
187 | { "GBps", 8000000000. }, | |
188 | { "TiBps", 8.*1024.*1024.*1024.*1024. }, | |
189 | { "TBps", 8000000000000. }, | |
190 | { NULL } | |
191 | }; | |
192 | ||
1e5746d5 AC |
193 | /* Parse a percent e.g: '30%' |
194 | * return: 0 = ok, -1 = error, 1 = out of range | |
195 | */ | |
196 | int parse_percent(double *val, const char *str) | |
197 | { | |
198 | char *p; | |
199 | ||
200 | *val = strtod(str, &p) / 100.; | |
201 | if (*val == HUGE_VALF || *val == HUGE_VALL) | |
202 | return 1; | |
203 | if (*p && strcmp(p, "%")) | |
204 | return -1; | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
817204d0 SH |
209 | static int parse_percent_rate(char *rate, size_t len, |
210 | const char *str, const char *dev) | |
927e3cfb ND |
211 | { |
212 | long dev_mbit; | |
213 | int ret; | |
9e46c5c2 | 214 | double perc, rate_bit; |
2d603d55 | 215 | char *str_perc = NULL; |
927e3cfb ND |
216 | |
217 | if (!dev[0]) { | |
218 | fprintf(stderr, "No device specified; specify device to rate limit by percentage\n"); | |
219 | return -1; | |
220 | } | |
221 | ||
222 | if (read_prop(dev, "speed", &dev_mbit)) | |
223 | return -1; | |
224 | ||
225 | ret = sscanf(str, "%m[0-9.%]", &str_perc); | |
226 | if (ret != 1) | |
227 | goto malf; | |
228 | ||
229 | if (parse_percent(&perc, str_perc)) | |
230 | goto malf; | |
231 | ||
232 | free(str_perc); | |
233 | ||
234 | if (perc > 1.0 || perc < 0.0) { | |
235 | fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str); | |
236 | return -1; | |
237 | } | |
238 | ||
9e46c5c2 | 239 | rate_bit = perc * dev_mbit * 1000 * 1000; |
927e3cfb | 240 | |
9e46c5c2 | 241 | ret = snprintf(rate, len, "%lf", rate_bit); |
817204d0 | 242 | if (ret <= 0 || ret >= len) { |
927e3cfb ND |
243 | fprintf(stderr, "Unable to parse calculated rate\n"); |
244 | return -1; | |
245 | } | |
246 | ||
247 | return 0; | |
248 | ||
249 | malf: | |
2d603d55 | 250 | free(str_perc); |
927e3cfb ND |
251 | fprintf(stderr, "Specified rate value could not be read or is malformed\n"); |
252 | return -1; | |
253 | } | |
254 | ||
255 | int get_percent_rate(unsigned int *rate, const char *str, const char *dev) | |
256 | { | |
257 | char r_str[20]; | |
258 | ||
817204d0 | 259 | if (parse_percent_rate(r_str, sizeof(r_str), str, dev)) |
927e3cfb ND |
260 | return -1; |
261 | ||
262 | return get_rate(rate, r_str); | |
263 | } | |
264 | ||
265 | int get_percent_rate64(__u64 *rate, const char *str, const char *dev) | |
266 | { | |
267 | char r_str[20]; | |
268 | ||
817204d0 | 269 | if (parse_percent_rate(r_str, sizeof(r_str), str, dev)) |
927e3cfb ND |
270 | return -1; |
271 | ||
272 | return get_rate64(rate, r_str); | |
273 | } | |
26ab0b10 | 274 | |
32a121cb | 275 | int get_rate(unsigned int *rate, const char *str) |
aba5acdf SH |
276 | { |
277 | char *p; | |
278 | double bps = strtod(str, &p); | |
26ab0b10 | 279 | const struct rate_suffix *s; |
aba5acdf SH |
280 | |
281 | if (p == str) | |
282 | return -1; | |
283 | ||
26ab0b10 SH |
284 | for (s = suffixes; s->name; ++s) { |
285 | if (strcasecmp(s->name, p) == 0) { | |
a303853e ED |
286 | bps *= s->scale; |
287 | p += strlen(p); | |
288 | break; | |
26ab0b10 SH |
289 | } |
290 | } | |
291 | ||
a303853e ED |
292 | if (*p) |
293 | return -1; /* unknown suffix */ | |
294 | ||
295 | bps /= 8; /* -> bytes per second */ | |
296 | *rate = bps; | |
297 | /* detect if an overflow happened */ | |
298 | if (*rate != floor(bps)) | |
299 | return -1; | |
300 | return 0; | |
aba5acdf SH |
301 | } |
302 | ||
8334bb32 ED |
303 | int get_rate64(__u64 *rate, const char *str) |
304 | { | |
305 | char *p; | |
306 | double bps = strtod(str, &p); | |
307 | const struct rate_suffix *s; | |
308 | ||
309 | if (p == str) | |
310 | return -1; | |
311 | ||
312 | for (s = suffixes; s->name; ++s) { | |
313 | if (strcasecmp(s->name, p) == 0) { | |
314 | bps *= s->scale; | |
315 | p += strlen(p); | |
316 | break; | |
317 | } | |
318 | } | |
319 | ||
320 | if (*p) | |
321 | return -1; /* unknown suffix */ | |
322 | ||
323 | bps /= 8; /* -> bytes per second */ | |
324 | *rate = bps; | |
325 | return 0; | |
326 | } | |
327 | ||
8f7574ed | 328 | void print_rate(char *buf, int len, __u64 rate) |
aba5acdf | 329 | { |
d40b38b4 | 330 | extern int use_iec; |
8cecdc28 ED |
331 | unsigned long kilo = use_iec ? 1024 : 1000; |
332 | const char *str = use_iec ? "i" : ""; | |
8cecdc28 | 333 | static char *units[5] = {"", "K", "M", "G", "T"}; |
ad1fe0d8 | 334 | int i; |
d40b38b4 | 335 | |
8cecdc28 ED |
336 | rate <<= 3; /* bytes/sec -> bits/sec */ |
337 | ||
ad1fe0d8 | 338 | for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { |
8cecdc28 ED |
339 | if (rate < kilo) |
340 | break; | |
341 | if (((rate % kilo) != 0) && rate < 1000*kilo) | |
342 | break; | |
343 | rate /= kilo; | |
d40b38b4 | 344 | } |
ad1fe0d8 | 345 | |
8cecdc28 | 346 | snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str); |
aba5acdf SH |
347 | } |
348 | ||
32a121cb | 349 | char *sprint_rate(__u64 rate, char *buf) |
aba5acdf | 350 | { |
d40b38b4 | 351 | print_rate(buf, SPRINT_BSIZE-1, rate); |
aba5acdf SH |
352 | return buf; |
353 | } | |
354 | ||
32a121cb | 355 | char *sprint_ticks(__u32 ticks, char *buf) |
bd29e35d PM |
356 | { |
357 | return sprint_time(tc_core_tick2time(ticks), buf); | |
358 | } | |
359 | ||
32a121cb | 360 | int get_size(unsigned int *size, const char *str) |
aba5acdf SH |
361 | { |
362 | double sz; | |
363 | char *p; | |
364 | ||
365 | sz = strtod(str, &p); | |
366 | if (p == str) | |
367 | return -1; | |
368 | ||
369 | if (*p) { | |
32a121cb | 370 | if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0) |
aba5acdf | 371 | sz *= 1024; |
32a121cb | 372 | else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0) |
dbd90dc2 SH |
373 | sz *= 1024*1024*1024; |
374 | else if (strcasecmp(p, "gbit") == 0) | |
375 | sz *= 1024*1024*1024/8; | |
32a121cb | 376 | else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0) |
aba5acdf SH |
377 | sz *= 1024*1024; |
378 | else if (strcasecmp(p, "mbit") == 0) | |
379 | sz *= 1024*1024/8; | |
380 | else if (strcasecmp(p, "kbit") == 0) | |
381 | sz *= 1024/8; | |
382 | else if (strcasecmp(p, "b") != 0) | |
383 | return -1; | |
384 | } | |
385 | ||
386 | *size = sz; | |
387 | return 0; | |
388 | } | |
389 | ||
32a121cb | 390 | int get_size_and_cell(unsigned int *size, int *cell_log, char *str) |
aba5acdf | 391 | { |
32a121cb | 392 | char *slash = strchr(str, '/'); |
aba5acdf SH |
393 | |
394 | if (slash) | |
395 | *slash = 0; | |
396 | ||
397 | if (get_size(size, str)) | |
398 | return -1; | |
399 | ||
400 | if (slash) { | |
401 | int cell; | |
402 | int i; | |
403 | ||
404 | if (get_integer(&cell, slash+1, 0)) | |
405 | return -1; | |
406 | *slash = '/'; | |
407 | ||
32a121cb | 408 | for (i = 0; i < 32; i++) { |
aba5acdf SH |
409 | if ((1<<i) == cell) { |
410 | *cell_log = i; | |
411 | return 0; | |
412 | } | |
413 | } | |
414 | return -1; | |
415 | } | |
416 | return 0; | |
417 | } | |
418 | ||
2d165c08 SH |
419 | void print_devname(enum output_type type, int ifindex) |
420 | { | |
421 | const char *ifname = ll_index_to_name(ifindex); | |
422 | ||
423 | if (!is_json_context()) | |
424 | printf("dev "); | |
425 | ||
426 | print_color_string(type, COLOR_IFNAME, | |
427 | "dev", "%s ", ifname); | |
428 | } | |
429 | ||
9455bec5 | 430 | static void print_size(char *buf, int len, __u32 sz) |
aba5acdf SH |
431 | { |
432 | double tmp = sz; | |
433 | ||
434 | if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) | |
435 | snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); | |
436 | else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) | |
437 | snprintf(buf, len, "%gKb", rint(tmp/1024)); | |
438 | else | |
439 | snprintf(buf, len, "%ub", sz); | |
aba5acdf SH |
440 | } |
441 | ||
32a121cb | 442 | char *sprint_size(__u32 size, char *buf) |
aba5acdf | 443 | { |
d40b38b4 | 444 | print_size(buf, SPRINT_BSIZE-1, size); |
aba5acdf SH |
445 | return buf; |
446 | } | |
447 | ||
e67aba55 | 448 | static const char *action_n2a(int action) |
2373fde9 | 449 | { |
70932006 PS |
450 | static char buf[64]; |
451 | ||
d19f72f7 JP |
452 | if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) |
453 | return "goto"; | |
35f2a763 JHS |
454 | if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) |
455 | return "jump"; | |
2373fde9 | 456 | switch (action) { |
70932006 | 457 | case TC_ACT_UNSPEC: |
2373fde9 | 458 | return "continue"; |
2373fde9 SH |
459 | case TC_ACT_OK: |
460 | return "pass"; | |
2373fde9 SH |
461 | case TC_ACT_SHOT: |
462 | return "drop"; | |
2373fde9 SH |
463 | case TC_ACT_RECLASSIFY: |
464 | return "reclassify"; | |
465 | case TC_ACT_PIPE: | |
466 | return "pipe"; | |
467 | case TC_ACT_STOLEN: | |
468 | return "stolen"; | |
d5ebd6fd JP |
469 | case TC_ACT_TRAP: |
470 | return "trap"; | |
2373fde9 | 471 | default: |
70932006 | 472 | snprintf(buf, 64, "%d", action); |
2373fde9 SH |
473 | return buf; |
474 | } | |
475 | } | |
aba5acdf | 476 | |
53aadc52 PS |
477 | /* Convert action branch name into numeric format. |
478 | * | |
479 | * Parameters: | |
480 | * @arg - string to parse | |
481 | * @result - pointer to output variable | |
482 | * @allow_num - whether @arg may be in numeric format already | |
483 | * | |
484 | * In error case, returns -1 and does not touch @result. Otherwise returns 0. | |
485 | */ | |
6f7df6b2 | 486 | int action_a2n(char *arg, int *result, bool allow_num) |
2373fde9 | 487 | { |
53aadc52 PS |
488 | int n; |
489 | char dummy; | |
490 | struct { | |
491 | const char *a; | |
492 | int n; | |
493 | } a2n[] = { | |
494 | {"continue", TC_ACT_UNSPEC}, | |
495 | {"drop", TC_ACT_SHOT}, | |
496 | {"shot", TC_ACT_SHOT}, | |
497 | {"pass", TC_ACT_OK}, | |
498 | {"ok", TC_ACT_OK}, | |
499 | {"reclassify", TC_ACT_RECLASSIFY}, | |
500 | {"pipe", TC_ACT_PIPE}, | |
d19f72f7 | 501 | {"goto", TC_ACT_GOTO_CHAIN}, |
35f2a763 | 502 | {"jump", TC_ACT_JUMP}, |
d5ebd6fd | 503 | {"trap", TC_ACT_TRAP}, |
53aadc52 PS |
504 | { NULL }, |
505 | }, *iter; | |
506 | ||
507 | for (iter = a2n; iter->a; iter++) { | |
508 | if (matches(arg, iter->a) != 0) | |
509 | continue; | |
6f7df6b2 PS |
510 | n = iter->n; |
511 | goto out_ok; | |
2373fde9 | 512 | } |
53aadc52 PS |
513 | if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1) |
514 | return -1; | |
515 | ||
6f7df6b2 PS |
516 | out_ok: |
517 | if (result) | |
518 | *result = n; | |
2373fde9 SH |
519 | return 0; |
520 | } | |
521 | ||
c794b7b1 JP |
522 | static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p, |
523 | bool allow_num, bool ignore_a2n_miss) | |
e67aba55 JP |
524 | { |
525 | int argc = *argc_p; | |
526 | char **argv = *argv_p; | |
527 | int result; | |
528 | ||
529 | if (!argc) | |
530 | return -1; | |
531 | if (action_a2n(*argv, &result, allow_num) == -1) { | |
c794b7b1 JP |
532 | if (!ignore_a2n_miss) |
533 | fprintf(stderr, "Bad action type %s\n", *argv); | |
e67aba55 JP |
534 | return -1; |
535 | } | |
d19f72f7 JP |
536 | if (result == TC_ACT_GOTO_CHAIN) { |
537 | __u32 chain_index; | |
538 | ||
539 | NEXT_ARG(); | |
540 | if (matches(*argv, "chain") != 0) { | |
541 | fprintf(stderr, "\"chain index\" expected\n"); | |
542 | return -1; | |
543 | } | |
544 | NEXT_ARG(); | |
545 | if (get_u32(&chain_index, *argv, 10) || | |
546 | chain_index > TC_ACT_EXT_VAL_MASK) { | |
547 | fprintf(stderr, "Illegal \"chain index\"\n"); | |
548 | return -1; | |
549 | } | |
550 | result |= chain_index; | |
551 | } | |
35f2a763 JHS |
552 | if (result == TC_ACT_JUMP) { |
553 | __u32 jump_cnt = 0; | |
554 | ||
555 | NEXT_ARG(); | |
556 | if (get_u32(&jump_cnt, *argv, 10) || | |
557 | jump_cnt > TC_ACT_EXT_VAL_MASK) { | |
558 | fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv); | |
559 | return -1; | |
560 | } | |
561 | result |= jump_cnt; | |
562 | } | |
75ef7b18 | 563 | NEXT_ARG_FWD(); |
e67aba55 JP |
564 | *argc_p = argc; |
565 | *argv_p = argv; | |
566 | *result_p = result; | |
567 | return 0; | |
568 | } | |
569 | ||
c794b7b1 JP |
570 | /* Parse action control including possible options. |
571 | * | |
572 | * Parameters: | |
573 | * @argc_p - pointer to argc to parse | |
574 | * @argv_p - pointer to argv to parse | |
575 | * @result_p - pointer to output variable | |
576 | * @allow_num - whether action may be in numeric format already | |
577 | * | |
578 | * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0. | |
579 | */ | |
580 | int parse_action_control(int *argc_p, char ***argv_p, | |
581 | int *result_p, bool allow_num) | |
582 | { | |
583 | return __parse_action_control(argc_p, argv_p, result_p, | |
584 | allow_num, false); | |
585 | } | |
586 | ||
e67aba55 JP |
587 | /* Parse action control including possible options. |
588 | * | |
589 | * Parameters: | |
590 | * @argc_p - pointer to argc to parse | |
591 | * @argv_p - pointer to argv to parse | |
592 | * @result_p - pointer to output variable | |
593 | * @allow_num - whether action may be in numeric format already | |
594 | * @default_result - set as a result in case of parsing error | |
595 | * | |
596 | * In case there is an error during parsing, the default result is used. | |
597 | */ | |
598 | void parse_action_control_dflt(int *argc_p, char ***argv_p, | |
599 | int *result_p, bool allow_num, | |
600 | int default_result) | |
601 | { | |
c794b7b1 | 602 | if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true)) |
e67aba55 JP |
603 | *result_p = default_result; |
604 | } | |
605 | ||
606 | static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p, | |
607 | int *result1_p, int *result2_p, | |
608 | bool allow_num) | |
609 | { | |
610 | int argc = *argc_p; | |
611 | char **argv = *argv_p; | |
66942e52 | 612 | int result1 = -1, result2; |
e67aba55 JP |
613 | int *result_p = &result1; |
614 | int ok = 0; | |
615 | int ret; | |
616 | ||
617 | while (argc > 0) { | |
618 | switch (ok) { | |
619 | case 1: | |
620 | if (strcmp(*argv, "/") != 0) | |
621 | goto out; | |
622 | result_p = &result2; | |
623 | NEXT_ARG(); | |
624 | /* fall-through */ | |
625 | case 0: /* fall-through */ | |
626 | case 2: | |
627 | ret = parse_action_control(&argc, &argv, | |
628 | result_p, allow_num); | |
629 | if (ret) | |
630 | return ret; | |
631 | ok++; | |
632 | break; | |
633 | default: | |
634 | goto out; | |
635 | } | |
636 | } | |
637 | out: | |
638 | *result1_p = result1; | |
639 | if (ok == 2) | |
640 | *result2_p = result2; | |
641 | *argc_p = argc; | |
642 | *argv_p = argv; | |
643 | return 0; | |
644 | } | |
645 | ||
646 | /* Parse action control with slash including possible options. | |
647 | * | |
648 | * Parameters: | |
649 | * @argc_p - pointer to argc to parse | |
650 | * @argv_p - pointer to argv to parse | |
651 | * @result1_p - pointer to the first (before slash) output variable | |
652 | * @result2_p - pointer to the second (after slash) output variable | |
653 | * @allow_num - whether action may be in numeric format already | |
654 | * | |
655 | * In error case, returns -1 and does not touch @result*. Otherwise returns 0. | |
656 | */ | |
657 | int parse_action_control_slash(int *argc_p, char ***argv_p, | |
658 | int *result1_p, int *result2_p, bool allow_num) | |
659 | { | |
75ef7b18 | 660 | int result1, result2, argc = *argc_p; |
e67aba55 | 661 | char **argv = *argv_p; |
e67aba55 JP |
662 | char *p = strchr(*argv, '/'); |
663 | ||
664 | if (!p) | |
665 | return parse_action_control_slash_spaces(argc_p, argv_p, | |
666 | result1_p, result2_p, | |
667 | allow_num); | |
668 | *p = 0; | |
669 | if (action_a2n(*argv, &result1, allow_num)) { | |
b7c61286 | 670 | *p = '/'; |
e67aba55 JP |
671 | return -1; |
672 | } | |
673 | ||
674 | *p = '/'; | |
675 | if (action_a2n(p + 1, &result2, allow_num)) | |
676 | return -1; | |
677 | ||
678 | *result1_p = result1; | |
679 | *result2_p = result2; | |
75ef7b18 DC |
680 | NEXT_ARG_FWD(); |
681 | *argc_p = argc; | |
682 | *argv_p = argv; | |
e67aba55 JP |
683 | return 0; |
684 | } | |
685 | ||
686 | void print_action_control(FILE *f, const char *prefix, | |
687 | int action, const char *suffix) | |
688 | { | |
2704bd62 JP |
689 | print_string(PRINT_FP, NULL, "%s", prefix); |
690 | open_json_object("control_action"); | |
691 | print_string(PRINT_ANY, "type", "%s", action_n2a(action)); | |
d19f72f7 | 692 | if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) |
2704bd62 JP |
693 | print_uint(PRINT_ANY, "chain", " chain %u", |
694 | action & TC_ACT_EXT_VAL_MASK); | |
35f2a763 | 695 | if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) |
2704bd62 JP |
696 | print_uint(PRINT_ANY, "jump", " %u", |
697 | action & TC_ACT_EXT_VAL_MASK); | |
698 | close_json_object(); | |
699 | print_string(PRINT_FP, NULL, "%s", suffix); | |
e67aba55 JP |
700 | } |
701 | ||
32a121cb | 702 | int get_linklayer(unsigned int *val, const char *arg) |
292f29b4 JDB |
703 | { |
704 | int res; | |
705 | ||
706 | if (matches(arg, "ethernet") == 0) | |
707 | res = LINKLAYER_ETHERNET; | |
708 | else if (matches(arg, "atm") == 0) | |
709 | res = LINKLAYER_ATM; | |
710 | else if (matches(arg, "adsl") == 0) | |
711 | res = LINKLAYER_ATM; | |
712 | else | |
713 | return -1; /* Indicate error */ | |
714 | ||
715 | *val = res; | |
716 | return 0; | |
717 | } | |
718 | ||
9455bec5 | 719 | static void print_linklayer(char *buf, int len, unsigned int linklayer) |
839c8456 JK |
720 | { |
721 | switch (linklayer) { | |
722 | case LINKLAYER_UNSPEC: | |
723 | snprintf(buf, len, "%s", "unspec"); | |
724 | return; | |
725 | case LINKLAYER_ETHERNET: | |
726 | snprintf(buf, len, "%s", "ethernet"); | |
727 | return; | |
728 | case LINKLAYER_ATM: | |
729 | snprintf(buf, len, "%s", "atm"); | |
730 | return; | |
731 | default: | |
732 | snprintf(buf, len, "%s", "unknown"); | |
733 | return; | |
734 | } | |
735 | } | |
736 | ||
32a121cb | 737 | char *sprint_linklayer(unsigned int linklayer, char *buf) |
839c8456 JK |
738 | { |
739 | print_linklayer(buf, SPRINT_BSIZE-1, linklayer); | |
740 | return buf; | |
741 | } | |
742 | ||
32a121cb | 743 | void print_tm(FILE *f, const struct tcf_t *tm) |
2373fde9 | 744 | { |
5e8bc631 | 745 | int hz = get_user_hz(); |
32a121cb | 746 | |
2704bd62 JP |
747 | if (tm->install != 0) { |
748 | print_uint(PRINT_JSON, "installed", NULL, tm->install); | |
749 | print_uint(PRINT_FP, NULL, " installed %u sec", | |
750 | (unsigned int)(tm->install/hz)); | |
751 | } | |
752 | if (tm->lastuse != 0) { | |
753 | print_uint(PRINT_JSON, "last_used", NULL, tm->lastuse); | |
754 | print_uint(PRINT_FP, NULL, " used %u sec", | |
755 | (unsigned int)(tm->lastuse/hz)); | |
756 | } | |
757 | if (tm->expires != 0) { | |
758 | print_uint(PRINT_JSON, "expires", NULL, tm->expires); | |
759 | print_uint(PRINT_FP, NULL, " expires %u sec", | |
760 | (unsigned int)(tm->expires/hz)); | |
761 | } | |
2373fde9 | 762 | } |
e5879dc6 | 763 | |
5ac13832 EC |
764 | static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix) |
765 | { | |
766 | struct gnet_stats_basic bs_hw; | |
767 | ||
768 | if (!tbs[TCA_STATS_BASIC_HW]) | |
769 | return; | |
770 | ||
771 | memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]), | |
772 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw))); | |
773 | ||
774 | if (bs_hw.bytes == 0 && bs_hw.packets == 0) | |
775 | return; | |
776 | ||
777 | if (tbs[TCA_STATS_BASIC]) { | |
778 | struct gnet_stats_basic bs; | |
779 | ||
780 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), | |
781 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), | |
782 | sizeof(bs))); | |
783 | ||
784 | if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) { | |
785 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
786 | print_string(PRINT_FP, NULL, "%s", prefix); | |
787 | print_lluint(PRINT_ANY, "sw_bytes", | |
788 | "Sent software %llu bytes", | |
789 | bs.bytes - bs_hw.bytes); | |
790 | print_uint(PRINT_ANY, "sw_packets", " %u pkt", | |
791 | bs.packets - bs_hw.packets); | |
792 | } | |
793 | } | |
794 | ||
795 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
796 | print_string(PRINT_FP, NULL, "%s", prefix); | |
797 | print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes", | |
798 | bs_hw.bytes); | |
799 | print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets); | |
800 | } | |
801 | ||
e5879dc6 | 802 | void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) |
803 | { | |
804 | SPRINT_BUF(b1); | |
7d69fd97 | 805 | struct rtattr *tbs[TCA_STATS_MAX + 1]; |
e5879dc6 | 806 | |
7d69fd97 | 807 | parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); |
e5879dc6 | 808 | |
809 | if (tbs[TCA_STATS_BASIC]) { | |
810 | struct gnet_stats_basic bs = {0}; | |
32a121cb | 811 | |
e5879dc6 | 812 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); |
4fcec7f3 JP |
813 | print_string(PRINT_FP, NULL, "%s", prefix); |
814 | print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes); | |
815 | print_uint(PRINT_ANY, "packets", " %u pkt", bs.packets); | |
e5879dc6 | 816 | } |
817 | ||
818 | if (tbs[TCA_STATS_QUEUE]) { | |
819 | struct gnet_stats_queue q = {0}; | |
32a121cb | 820 | |
e5879dc6 | 821 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
4fcec7f3 JP |
822 | print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops); |
823 | print_uint(PRINT_ANY, "overlimits", ", overlimits %u", | |
824 | q.overlimits); | |
825 | print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues); | |
e5879dc6 | 826 | } |
ae665a52 | 827 | |
5ac13832 EC |
828 | if (tbs[TCA_STATS_BASIC_HW]) |
829 | print_tcstats_basic_hw(tbs, prefix); | |
830 | ||
8f7574ed ED |
831 | if (tbs[TCA_STATS_RATE_EST64]) { |
832 | struct gnet_stats_rate_est64 re = {0}; | |
833 | ||
834 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]), | |
835 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]), | |
836 | sizeof(re))); | |
4fcec7f3 JP |
837 | print_string(PRINT_FP, NULL, "\n%s", prefix); |
838 | print_lluint(PRINT_JSON, "rate", NULL, re.bps); | |
839 | print_string(PRINT_FP, NULL, "rate %s", | |
840 | sprint_rate(re.bps, b1)); | |
841 | print_lluint(PRINT_ANY, "pps", " %llupps", re.pps); | |
8f7574ed | 842 | } else if (tbs[TCA_STATS_RATE_EST]) { |
e5879dc6 | 843 | struct gnet_stats_rate_est re = {0}; |
8f7574ed ED |
844 | |
845 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), | |
846 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); | |
4fcec7f3 JP |
847 | print_string(PRINT_FP, NULL, "\n%s", prefix); |
848 | print_uint(PRINT_JSON, "rate", NULL, re.bps); | |
849 | print_string(PRINT_FP, NULL, "rate %s", | |
850 | sprint_rate(re.bps, b1)); | |
851 | print_uint(PRINT_ANY, "pps", " %upps", re.pps); | |
e5879dc6 | 852 | } |
853 | ||
854 | if (tbs[TCA_STATS_QUEUE]) { | |
855 | struct gnet_stats_queue q = {0}; | |
32a121cb | 856 | |
e5879dc6 | 857 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
858 | if (!tbs[TCA_STATS_RATE_EST]) | |
3adcbf37 | 859 | print_string(PRINT_FP, NULL, "\n", ""); |
4fcec7f3 | 860 | print_uint(PRINT_JSON, "backlog", NULL, q.backlog); |
3adcbf37 | 861 | print_string(PRINT_FP, NULL, "%s", prefix); |
4fcec7f3 JP |
862 | print_string(PRINT_FP, NULL, "backlog %s", |
863 | sprint_size(q.backlog, b1)); | |
864 | print_uint(PRINT_ANY, "qlen", " %up", q.qlen); | |
44c76551 | 865 | print_uint(PRINT_FP, NULL, " requeues %u", q.requeues); |
e5879dc6 | 866 | } |
867 | ||
868 | if (xstats) | |
869 | *xstats = tbs[TCA_STATS_APP] ? : NULL; | |
870 | } | |
871 | ||
872 | void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) | |
873 | { | |
874 | SPRINT_BUF(b1); | |
875 | ||
876 | if (tb[TCA_STATS2]) { | |
877 | print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); | |
6f1940da | 878 | if (xstats && !*xstats) |
e5879dc6 | 879 | goto compat_xstats; |
880 | return; | |
881 | } | |
882 | /* backward compatibility */ | |
883 | if (tb[TCA_STATS]) { | |
d17b136f | 884 | struct tc_stats st = {}; |
e5879dc6 | 885 | |
886 | /* handle case where kernel returns more/less than we know about */ | |
e5879dc6 | 887 | memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); |
888 | ||
889 | fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", | |
ae665a52 | 890 | prefix, (unsigned long long)st.bytes, st.packets, st.drops, |
e5879dc6 | 891 | st.overlimits); |
892 | ||
893 | if (st.bps || st.pps || st.qlen || st.backlog) { | |
894 | fprintf(fp, "\n%s", prefix); | |
895 | if (st.bps || st.pps) { | |
896 | fprintf(fp, "rate "); | |
897 | if (st.bps) | |
898 | fprintf(fp, "%s ", sprint_rate(st.bps, b1)); | |
899 | if (st.pps) | |
900 | fprintf(fp, "%upps ", st.pps); | |
901 | } | |
902 | if (st.qlen || st.backlog) { | |
903 | fprintf(fp, "backlog "); | |
904 | if (st.backlog) | |
905 | fprintf(fp, "%s ", sprint_size(st.backlog, b1)); | |
906 | if (st.qlen) | |
907 | fprintf(fp, "%up ", st.qlen); | |
908 | } | |
909 | } | |
910 | } | |
911 | ||
912 | compat_xstats: | |
913 | if (tb[TCA_XSTATS] && xstats) | |
914 | *xstats = tb[TCA_XSTATS]; | |
915 | } |