]>
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.; | |
6bc13e4a | 201 | if (*val > 1.0 || *val < 0.0) |
1e5746d5 AC |
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 | ||
6bc13e4a AC |
229 | ret = parse_percent(&perc, str_perc); |
230 | if (ret == 1) { | |
231 | fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str); | |
232 | goto err; | |
233 | } else if (ret == -1) { | |
927e3cfb | 234 | goto malf; |
6bc13e4a | 235 | } |
927e3cfb ND |
236 | |
237 | free(str_perc); | |
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: | |
250 | fprintf(stderr, "Specified rate value could not be read or is malformed\n"); | |
6bc13e4a AC |
251 | err: |
252 | free(str_perc); | |
927e3cfb ND |
253 | return -1; |
254 | } | |
255 | ||
256 | int get_percent_rate(unsigned int *rate, const char *str, const char *dev) | |
257 | { | |
258 | char r_str[20]; | |
259 | ||
817204d0 | 260 | if (parse_percent_rate(r_str, sizeof(r_str), str, dev)) |
927e3cfb ND |
261 | return -1; |
262 | ||
263 | return get_rate(rate, r_str); | |
264 | } | |
265 | ||
266 | int get_percent_rate64(__u64 *rate, const char *str, const char *dev) | |
267 | { | |
268 | char r_str[20]; | |
269 | ||
817204d0 | 270 | if (parse_percent_rate(r_str, sizeof(r_str), str, dev)) |
927e3cfb ND |
271 | return -1; |
272 | ||
273 | return get_rate64(rate, r_str); | |
274 | } | |
26ab0b10 | 275 | |
32a121cb | 276 | int get_rate(unsigned int *rate, const char *str) |
aba5acdf SH |
277 | { |
278 | char *p; | |
279 | double bps = strtod(str, &p); | |
26ab0b10 | 280 | const struct rate_suffix *s; |
aba5acdf SH |
281 | |
282 | if (p == str) | |
283 | return -1; | |
284 | ||
26ab0b10 SH |
285 | for (s = suffixes; s->name; ++s) { |
286 | if (strcasecmp(s->name, p) == 0) { | |
a303853e ED |
287 | bps *= s->scale; |
288 | p += strlen(p); | |
289 | break; | |
26ab0b10 SH |
290 | } |
291 | } | |
292 | ||
a303853e ED |
293 | if (*p) |
294 | return -1; /* unknown suffix */ | |
295 | ||
296 | bps /= 8; /* -> bytes per second */ | |
297 | *rate = bps; | |
298 | /* detect if an overflow happened */ | |
299 | if (*rate != floor(bps)) | |
300 | return -1; | |
301 | return 0; | |
aba5acdf SH |
302 | } |
303 | ||
8334bb32 ED |
304 | int get_rate64(__u64 *rate, const char *str) |
305 | { | |
306 | char *p; | |
307 | double bps = strtod(str, &p); | |
308 | const struct rate_suffix *s; | |
309 | ||
310 | if (p == str) | |
311 | return -1; | |
312 | ||
313 | for (s = suffixes; s->name; ++s) { | |
314 | if (strcasecmp(s->name, p) == 0) { | |
315 | bps *= s->scale; | |
316 | p += strlen(p); | |
317 | break; | |
318 | } | |
319 | } | |
320 | ||
321 | if (*p) | |
322 | return -1; /* unknown suffix */ | |
323 | ||
324 | bps /= 8; /* -> bytes per second */ | |
325 | *rate = bps; | |
326 | return 0; | |
327 | } | |
328 | ||
60265cc2 PM |
329 | void tc_print_rate(enum output_type t, const char *key, const char *fmt, |
330 | unsigned long long rate) | |
aba5acdf | 331 | { |
60265cc2 | 332 | print_rate(use_iec, t, key, fmt, rate); |
aba5acdf SH |
333 | } |
334 | ||
32a121cb | 335 | char *sprint_ticks(__u32 ticks, char *buf) |
bd29e35d PM |
336 | { |
337 | return sprint_time(tc_core_tick2time(ticks), buf); | |
338 | } | |
339 | ||
32a121cb | 340 | int get_size(unsigned int *size, const char *str) |
aba5acdf SH |
341 | { |
342 | double sz; | |
343 | char *p; | |
344 | ||
345 | sz = strtod(str, &p); | |
346 | if (p == str) | |
347 | return -1; | |
348 | ||
349 | if (*p) { | |
32a121cb | 350 | if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0) |
aba5acdf | 351 | sz *= 1024; |
32a121cb | 352 | else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0) |
dbd90dc2 SH |
353 | sz *= 1024*1024*1024; |
354 | else if (strcasecmp(p, "gbit") == 0) | |
355 | sz *= 1024*1024*1024/8; | |
32a121cb | 356 | else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0) |
aba5acdf SH |
357 | sz *= 1024*1024; |
358 | else if (strcasecmp(p, "mbit") == 0) | |
359 | sz *= 1024*1024/8; | |
360 | else if (strcasecmp(p, "kbit") == 0) | |
361 | sz *= 1024/8; | |
362 | else if (strcasecmp(p, "b") != 0) | |
363 | return -1; | |
364 | } | |
365 | ||
366 | *size = sz; | |
e07c57e9 OU |
367 | |
368 | /* detect if an overflow happened */ | |
369 | if (*size != floor(sz)) | |
370 | return -1; | |
371 | ||
aba5acdf SH |
372 | return 0; |
373 | } | |
374 | ||
32a121cb | 375 | int get_size_and_cell(unsigned int *size, int *cell_log, char *str) |
aba5acdf | 376 | { |
32a121cb | 377 | char *slash = strchr(str, '/'); |
aba5acdf SH |
378 | |
379 | if (slash) | |
380 | *slash = 0; | |
381 | ||
382 | if (get_size(size, str)) | |
383 | return -1; | |
384 | ||
385 | if (slash) { | |
386 | int cell; | |
387 | int i; | |
388 | ||
389 | if (get_integer(&cell, slash+1, 0)) | |
390 | return -1; | |
391 | *slash = '/'; | |
392 | ||
32a121cb | 393 | for (i = 0; i < 32; i++) { |
aba5acdf SH |
394 | if ((1<<i) == cell) { |
395 | *cell_log = i; | |
396 | return 0; | |
397 | } | |
398 | } | |
399 | return -1; | |
400 | } | |
401 | return 0; | |
402 | } | |
403 | ||
2d165c08 SH |
404 | void print_devname(enum output_type type, int ifindex) |
405 | { | |
406 | const char *ifname = ll_index_to_name(ifindex); | |
407 | ||
408 | if (!is_json_context()) | |
409 | printf("dev "); | |
410 | ||
411 | print_color_string(type, COLOR_IFNAME, | |
412 | "dev", "%s ", ifname); | |
413 | } | |
414 | ||
9455bec5 | 415 | static void print_size(char *buf, int len, __u32 sz) |
aba5acdf SH |
416 | { |
417 | double tmp = sz; | |
418 | ||
419 | if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) | |
420 | snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); | |
421 | else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) | |
422 | snprintf(buf, len, "%gKb", rint(tmp/1024)); | |
423 | else | |
424 | snprintf(buf, len, "%ub", sz); | |
aba5acdf SH |
425 | } |
426 | ||
32a121cb | 427 | char *sprint_size(__u32 size, char *buf) |
aba5acdf | 428 | { |
d40b38b4 | 429 | print_size(buf, SPRINT_BSIZE-1, size); |
aba5acdf SH |
430 | return buf; |
431 | } | |
432 | ||
e67aba55 | 433 | static const char *action_n2a(int action) |
2373fde9 | 434 | { |
70932006 PS |
435 | static char buf[64]; |
436 | ||
d19f72f7 JP |
437 | if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) |
438 | return "goto"; | |
35f2a763 JHS |
439 | if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) |
440 | return "jump"; | |
2373fde9 | 441 | switch (action) { |
70932006 | 442 | case TC_ACT_UNSPEC: |
2373fde9 | 443 | return "continue"; |
2373fde9 SH |
444 | case TC_ACT_OK: |
445 | return "pass"; | |
2373fde9 SH |
446 | case TC_ACT_SHOT: |
447 | return "drop"; | |
2373fde9 SH |
448 | case TC_ACT_RECLASSIFY: |
449 | return "reclassify"; | |
450 | case TC_ACT_PIPE: | |
451 | return "pipe"; | |
452 | case TC_ACT_STOLEN: | |
453 | return "stolen"; | |
d5ebd6fd JP |
454 | case TC_ACT_TRAP: |
455 | return "trap"; | |
2373fde9 | 456 | default: |
70932006 | 457 | snprintf(buf, 64, "%d", action); |
2373fde9 SH |
458 | return buf; |
459 | } | |
460 | } | |
aba5acdf | 461 | |
53aadc52 PS |
462 | /* Convert action branch name into numeric format. |
463 | * | |
464 | * Parameters: | |
465 | * @arg - string to parse | |
466 | * @result - pointer to output variable | |
467 | * @allow_num - whether @arg may be in numeric format already | |
468 | * | |
469 | * In error case, returns -1 and does not touch @result. Otherwise returns 0. | |
470 | */ | |
6f7df6b2 | 471 | int action_a2n(char *arg, int *result, bool allow_num) |
2373fde9 | 472 | { |
53aadc52 PS |
473 | int n; |
474 | char dummy; | |
475 | struct { | |
476 | const char *a; | |
477 | int n; | |
478 | } a2n[] = { | |
479 | {"continue", TC_ACT_UNSPEC}, | |
480 | {"drop", TC_ACT_SHOT}, | |
481 | {"shot", TC_ACT_SHOT}, | |
482 | {"pass", TC_ACT_OK}, | |
483 | {"ok", TC_ACT_OK}, | |
484 | {"reclassify", TC_ACT_RECLASSIFY}, | |
485 | {"pipe", TC_ACT_PIPE}, | |
d19f72f7 | 486 | {"goto", TC_ACT_GOTO_CHAIN}, |
35f2a763 | 487 | {"jump", TC_ACT_JUMP}, |
d5ebd6fd | 488 | {"trap", TC_ACT_TRAP}, |
53aadc52 PS |
489 | { NULL }, |
490 | }, *iter; | |
491 | ||
492 | for (iter = a2n; iter->a; iter++) { | |
493 | if (matches(arg, iter->a) != 0) | |
494 | continue; | |
6f7df6b2 PS |
495 | n = iter->n; |
496 | goto out_ok; | |
2373fde9 | 497 | } |
53aadc52 PS |
498 | if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1) |
499 | return -1; | |
500 | ||
6f7df6b2 PS |
501 | out_ok: |
502 | if (result) | |
503 | *result = n; | |
2373fde9 SH |
504 | return 0; |
505 | } | |
506 | ||
c794b7b1 JP |
507 | static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p, |
508 | bool allow_num, bool ignore_a2n_miss) | |
e67aba55 JP |
509 | { |
510 | int argc = *argc_p; | |
511 | char **argv = *argv_p; | |
512 | int result; | |
513 | ||
514 | if (!argc) | |
515 | return -1; | |
516 | if (action_a2n(*argv, &result, allow_num) == -1) { | |
c794b7b1 JP |
517 | if (!ignore_a2n_miss) |
518 | fprintf(stderr, "Bad action type %s\n", *argv); | |
e67aba55 JP |
519 | return -1; |
520 | } | |
d19f72f7 JP |
521 | if (result == TC_ACT_GOTO_CHAIN) { |
522 | __u32 chain_index; | |
523 | ||
524 | NEXT_ARG(); | |
525 | if (matches(*argv, "chain") != 0) { | |
526 | fprintf(stderr, "\"chain index\" expected\n"); | |
527 | return -1; | |
528 | } | |
529 | NEXT_ARG(); | |
530 | if (get_u32(&chain_index, *argv, 10) || | |
531 | chain_index > TC_ACT_EXT_VAL_MASK) { | |
532 | fprintf(stderr, "Illegal \"chain index\"\n"); | |
533 | return -1; | |
534 | } | |
535 | result |= chain_index; | |
536 | } | |
35f2a763 JHS |
537 | if (result == TC_ACT_JUMP) { |
538 | __u32 jump_cnt = 0; | |
539 | ||
540 | NEXT_ARG(); | |
541 | if (get_u32(&jump_cnt, *argv, 10) || | |
542 | jump_cnt > TC_ACT_EXT_VAL_MASK) { | |
543 | fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv); | |
544 | return -1; | |
545 | } | |
546 | result |= jump_cnt; | |
547 | } | |
75ef7b18 | 548 | NEXT_ARG_FWD(); |
e67aba55 JP |
549 | *argc_p = argc; |
550 | *argv_p = argv; | |
551 | *result_p = result; | |
552 | return 0; | |
553 | } | |
554 | ||
c794b7b1 JP |
555 | /* Parse action control including possible options. |
556 | * | |
557 | * Parameters: | |
558 | * @argc_p - pointer to argc to parse | |
559 | * @argv_p - pointer to argv to parse | |
560 | * @result_p - pointer to output variable | |
561 | * @allow_num - whether action may be in numeric format already | |
562 | * | |
563 | * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0. | |
564 | */ | |
565 | int parse_action_control(int *argc_p, char ***argv_p, | |
566 | int *result_p, bool allow_num) | |
567 | { | |
568 | return __parse_action_control(argc_p, argv_p, result_p, | |
569 | allow_num, false); | |
570 | } | |
571 | ||
e67aba55 JP |
572 | /* Parse action control including possible options. |
573 | * | |
574 | * Parameters: | |
575 | * @argc_p - pointer to argc to parse | |
576 | * @argv_p - pointer to argv to parse | |
577 | * @result_p - pointer to output variable | |
578 | * @allow_num - whether action may be in numeric format already | |
579 | * @default_result - set as a result in case of parsing error | |
580 | * | |
581 | * In case there is an error during parsing, the default result is used. | |
582 | */ | |
583 | void parse_action_control_dflt(int *argc_p, char ***argv_p, | |
584 | int *result_p, bool allow_num, | |
585 | int default_result) | |
586 | { | |
c794b7b1 | 587 | if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true)) |
e67aba55 JP |
588 | *result_p = default_result; |
589 | } | |
590 | ||
591 | static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p, | |
592 | int *result1_p, int *result2_p, | |
593 | bool allow_num) | |
594 | { | |
595 | int argc = *argc_p; | |
596 | char **argv = *argv_p; | |
66942e52 | 597 | int result1 = -1, result2; |
e67aba55 JP |
598 | int *result_p = &result1; |
599 | int ok = 0; | |
600 | int ret; | |
601 | ||
602 | while (argc > 0) { | |
603 | switch (ok) { | |
604 | case 1: | |
605 | if (strcmp(*argv, "/") != 0) | |
606 | goto out; | |
607 | result_p = &result2; | |
608 | NEXT_ARG(); | |
609 | /* fall-through */ | |
610 | case 0: /* fall-through */ | |
611 | case 2: | |
612 | ret = parse_action_control(&argc, &argv, | |
613 | result_p, allow_num); | |
614 | if (ret) | |
615 | return ret; | |
616 | ok++; | |
617 | break; | |
618 | default: | |
619 | goto out; | |
620 | } | |
621 | } | |
622 | out: | |
623 | *result1_p = result1; | |
624 | if (ok == 2) | |
625 | *result2_p = result2; | |
626 | *argc_p = argc; | |
627 | *argv_p = argv; | |
628 | return 0; | |
629 | } | |
630 | ||
631 | /* Parse action control with slash including possible options. | |
632 | * | |
633 | * Parameters: | |
634 | * @argc_p - pointer to argc to parse | |
635 | * @argv_p - pointer to argv to parse | |
636 | * @result1_p - pointer to the first (before slash) output variable | |
637 | * @result2_p - pointer to the second (after slash) output variable | |
638 | * @allow_num - whether action may be in numeric format already | |
639 | * | |
640 | * In error case, returns -1 and does not touch @result*. Otherwise returns 0. | |
641 | */ | |
642 | int parse_action_control_slash(int *argc_p, char ***argv_p, | |
643 | int *result1_p, int *result2_p, bool allow_num) | |
644 | { | |
75ef7b18 | 645 | int result1, result2, argc = *argc_p; |
e67aba55 | 646 | char **argv = *argv_p; |
e67aba55 JP |
647 | char *p = strchr(*argv, '/'); |
648 | ||
649 | if (!p) | |
650 | return parse_action_control_slash_spaces(argc_p, argv_p, | |
651 | result1_p, result2_p, | |
652 | allow_num); | |
653 | *p = 0; | |
654 | if (action_a2n(*argv, &result1, allow_num)) { | |
b7c61286 | 655 | *p = '/'; |
e67aba55 JP |
656 | return -1; |
657 | } | |
658 | ||
659 | *p = '/'; | |
660 | if (action_a2n(p + 1, &result2, allow_num)) | |
661 | return -1; | |
662 | ||
663 | *result1_p = result1; | |
664 | *result2_p = result2; | |
75ef7b18 DC |
665 | NEXT_ARG_FWD(); |
666 | *argc_p = argc; | |
667 | *argv_p = argv; | |
e67aba55 JP |
668 | return 0; |
669 | } | |
670 | ||
671 | void print_action_control(FILE *f, const char *prefix, | |
672 | int action, const char *suffix) | |
673 | { | |
2704bd62 JP |
674 | print_string(PRINT_FP, NULL, "%s", prefix); |
675 | open_json_object("control_action"); | |
676 | print_string(PRINT_ANY, "type", "%s", action_n2a(action)); | |
d19f72f7 | 677 | if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) |
2704bd62 JP |
678 | print_uint(PRINT_ANY, "chain", " chain %u", |
679 | action & TC_ACT_EXT_VAL_MASK); | |
35f2a763 | 680 | if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) |
2704bd62 JP |
681 | print_uint(PRINT_ANY, "jump", " %u", |
682 | action & TC_ACT_EXT_VAL_MASK); | |
683 | close_json_object(); | |
684 | print_string(PRINT_FP, NULL, "%s", suffix); | |
e67aba55 JP |
685 | } |
686 | ||
32a121cb | 687 | int get_linklayer(unsigned int *val, const char *arg) |
292f29b4 JDB |
688 | { |
689 | int res; | |
690 | ||
691 | if (matches(arg, "ethernet") == 0) | |
692 | res = LINKLAYER_ETHERNET; | |
693 | else if (matches(arg, "atm") == 0) | |
694 | res = LINKLAYER_ATM; | |
695 | else if (matches(arg, "adsl") == 0) | |
696 | res = LINKLAYER_ATM; | |
697 | else | |
698 | return -1; /* Indicate error */ | |
699 | ||
700 | *val = res; | |
701 | return 0; | |
702 | } | |
703 | ||
9455bec5 | 704 | static void print_linklayer(char *buf, int len, unsigned int linklayer) |
839c8456 JK |
705 | { |
706 | switch (linklayer) { | |
707 | case LINKLAYER_UNSPEC: | |
708 | snprintf(buf, len, "%s", "unspec"); | |
709 | return; | |
710 | case LINKLAYER_ETHERNET: | |
711 | snprintf(buf, len, "%s", "ethernet"); | |
712 | return; | |
713 | case LINKLAYER_ATM: | |
714 | snprintf(buf, len, "%s", "atm"); | |
715 | return; | |
716 | default: | |
717 | snprintf(buf, len, "%s", "unknown"); | |
718 | return; | |
719 | } | |
720 | } | |
721 | ||
32a121cb | 722 | char *sprint_linklayer(unsigned int linklayer, char *buf) |
839c8456 JK |
723 | { |
724 | print_linklayer(buf, SPRINT_BSIZE-1, linklayer); | |
725 | return buf; | |
726 | } | |
727 | ||
32a121cb | 728 | void print_tm(FILE *f, const struct tcf_t *tm) |
2373fde9 | 729 | { |
5e8bc631 | 730 | int hz = get_user_hz(); |
32a121cb | 731 | |
db35e411 RM |
732 | if (tm->install != 0) |
733 | print_uint(PRINT_ANY, "installed", " installed %u sec", | |
734 | tm->install / hz); | |
735 | ||
736 | if (tm->lastuse != 0) | |
737 | print_uint(PRINT_ANY, "last_used", " used %u sec", | |
738 | tm->lastuse / hz); | |
739 | ||
bd4b8c63 RM |
740 | if (tm->firstuse != 0) |
741 | print_uint(PRINT_ANY, "first_used", " firstused %u sec", | |
742 | tm->firstuse / hz); | |
743 | ||
db35e411 RM |
744 | if (tm->expires != 0) |
745 | print_uint(PRINT_ANY, "expires", " expires %u sec", | |
746 | tm->expires / hz); | |
2373fde9 | 747 | } |
e5879dc6 | 748 | |
5ac13832 EC |
749 | static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix) |
750 | { | |
751 | struct gnet_stats_basic bs_hw; | |
752 | ||
753 | if (!tbs[TCA_STATS_BASIC_HW]) | |
754 | return; | |
755 | ||
756 | memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]), | |
757 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw))); | |
758 | ||
759 | if (bs_hw.bytes == 0 && bs_hw.packets == 0) | |
760 | return; | |
761 | ||
762 | if (tbs[TCA_STATS_BASIC]) { | |
763 | struct gnet_stats_basic bs; | |
764 | ||
765 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), | |
766 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), | |
767 | sizeof(bs))); | |
768 | ||
769 | if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) { | |
0501fe73 | 770 | print_nl(); |
5ac13832 EC |
771 | print_string(PRINT_FP, NULL, "%s", prefix); |
772 | print_lluint(PRINT_ANY, "sw_bytes", | |
773 | "Sent software %llu bytes", | |
774 | bs.bytes - bs_hw.bytes); | |
775 | print_uint(PRINT_ANY, "sw_packets", " %u pkt", | |
776 | bs.packets - bs_hw.packets); | |
777 | } | |
778 | } | |
779 | ||
0501fe73 | 780 | print_nl(); |
5ac13832 EC |
781 | print_string(PRINT_FP, NULL, "%s", prefix); |
782 | print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes", | |
783 | bs_hw.bytes); | |
784 | print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets); | |
785 | } | |
786 | ||
e5879dc6 | 787 | void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) |
788 | { | |
789 | SPRINT_BUF(b1); | |
7d69fd97 | 790 | struct rtattr *tbs[TCA_STATS_MAX + 1]; |
e5879dc6 | 791 | |
7d69fd97 | 792 | parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); |
e5879dc6 | 793 | |
794 | if (tbs[TCA_STATS_BASIC]) { | |
795 | struct gnet_stats_basic bs = {0}; | |
81b365eb ED |
796 | __u64 packets64 = 0; |
797 | ||
798 | if (tbs[TCA_STATS_PKT64]) | |
799 | packets64 = rta_getattr_u64(tbs[TCA_STATS_PKT64]); | |
32a121cb | 800 | |
42060e8d SH |
801 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), |
802 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); | |
4fcec7f3 JP |
803 | print_string(PRINT_FP, NULL, "%s", prefix); |
804 | print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes); | |
81b365eb | 805 | if (packets64) |
42060e8d SH |
806 | print_lluint(PRINT_ANY, "packets", |
807 | " %llu pkt", packets64); | |
81b365eb | 808 | else |
42060e8d SH |
809 | print_uint(PRINT_ANY, "packets", |
810 | " %u pkt", bs.packets); | |
e5879dc6 | 811 | } |
812 | ||
813 | if (tbs[TCA_STATS_QUEUE]) { | |
814 | struct gnet_stats_queue q = {0}; | |
32a121cb | 815 | |
42060e8d SH |
816 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), |
817 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); | |
4fcec7f3 JP |
818 | print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops); |
819 | print_uint(PRINT_ANY, "overlimits", ", overlimits %u", | |
820 | q.overlimits); | |
821 | print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues); | |
e5879dc6 | 822 | } |
ae665a52 | 823 | |
5ac13832 EC |
824 | if (tbs[TCA_STATS_BASIC_HW]) |
825 | print_tcstats_basic_hw(tbs, prefix); | |
826 | ||
8f7574ed ED |
827 | if (tbs[TCA_STATS_RATE_EST64]) { |
828 | struct gnet_stats_rate_est64 re = {0}; | |
829 | ||
830 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]), | |
831 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]), | |
832 | sizeof(re))); | |
4fcec7f3 JP |
833 | print_string(PRINT_FP, NULL, "\n%s", prefix); |
834 | print_lluint(PRINT_JSON, "rate", NULL, re.bps); | |
60265cc2 | 835 | tc_print_rate(PRINT_FP, NULL, "rate %s", re.bps); |
4fcec7f3 | 836 | print_lluint(PRINT_ANY, "pps", " %llupps", re.pps); |
8f7574ed | 837 | } else if (tbs[TCA_STATS_RATE_EST]) { |
e5879dc6 | 838 | struct gnet_stats_rate_est re = {0}; |
8f7574ed ED |
839 | |
840 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), | |
841 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); | |
4fcec7f3 JP |
842 | print_string(PRINT_FP, NULL, "\n%s", prefix); |
843 | print_uint(PRINT_JSON, "rate", NULL, re.bps); | |
60265cc2 | 844 | tc_print_rate(PRINT_FP, NULL, "rate %s", re.bps); |
4fcec7f3 | 845 | print_uint(PRINT_ANY, "pps", " %upps", re.pps); |
e5879dc6 | 846 | } |
847 | ||
848 | if (tbs[TCA_STATS_QUEUE]) { | |
849 | struct gnet_stats_queue q = {0}; | |
32a121cb | 850 | |
42060e8d SH |
851 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), |
852 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); | |
e5879dc6 | 853 | if (!tbs[TCA_STATS_RATE_EST]) |
7b0d424a | 854 | print_nl(); |
4fcec7f3 | 855 | print_uint(PRINT_JSON, "backlog", NULL, q.backlog); |
3adcbf37 | 856 | print_string(PRINT_FP, NULL, "%s", prefix); |
4fcec7f3 JP |
857 | print_string(PRINT_FP, NULL, "backlog %s", |
858 | sprint_size(q.backlog, b1)); | |
859 | print_uint(PRINT_ANY, "qlen", " %up", q.qlen); | |
44c76551 | 860 | print_uint(PRINT_FP, NULL, " requeues %u", q.requeues); |
e5879dc6 | 861 | } |
862 | ||
863 | if (xstats) | |
864 | *xstats = tbs[TCA_STATS_APP] ? : NULL; | |
865 | } | |
866 | ||
42060e8d SH |
867 | void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, |
868 | struct rtattr **xstats) | |
e5879dc6 | 869 | { |
870 | SPRINT_BUF(b1); | |
871 | ||
872 | if (tb[TCA_STATS2]) { | |
873 | print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); | |
6f1940da | 874 | if (xstats && !*xstats) |
e5879dc6 | 875 | goto compat_xstats; |
876 | return; | |
877 | } | |
878 | /* backward compatibility */ | |
879 | if (tb[TCA_STATS]) { | |
d17b136f | 880 | struct tc_stats st = {}; |
e5879dc6 | 881 | |
882 | /* handle case where kernel returns more/less than we know about */ | |
42060e8d SH |
883 | memcpy(&st, RTA_DATA(tb[TCA_STATS]), |
884 | MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); | |
e5879dc6 | 885 | |
42060e8d SH |
886 | fprintf(fp, |
887 | "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", | |
888 | prefix, (unsigned long long)st.bytes, | |
889 | st.packets, st.drops, st.overlimits); | |
e5879dc6 | 890 | |
891 | if (st.bps || st.pps || st.qlen || st.backlog) { | |
892 | fprintf(fp, "\n%s", prefix); | |
893 | if (st.bps || st.pps) { | |
894 | fprintf(fp, "rate "); | |
895 | if (st.bps) | |
60265cc2 PM |
896 | tc_print_rate(PRINT_FP, NULL, "%s ", |
897 | st.bps); | |
e5879dc6 | 898 | if (st.pps) |
899 | fprintf(fp, "%upps ", st.pps); | |
900 | } | |
901 | if (st.qlen || st.backlog) { | |
902 | fprintf(fp, "backlog "); | |
903 | if (st.backlog) | |
42060e8d SH |
904 | fprintf(fp, "%s ", |
905 | sprint_size(st.backlog, b1)); | |
e5879dc6 | 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 | } | |
c8a49431 | 916 | |
04b21501 EB |
917 | static void print_masked_type(__u32 type_max, |
918 | __u32 (*rta_getattr_type)(const struct rtattr *), | |
919 | const char *name, struct rtattr *attr, | |
746e6c0f | 920 | struct rtattr *mask_attr, bool newline) |
04b21501 EB |
921 | { |
922 | SPRINT_BUF(namefrm); | |
923 | __u32 value, mask; | |
924 | SPRINT_BUF(out); | |
925 | size_t done; | |
926 | ||
927 | if (!attr) | |
928 | return; | |
929 | ||
930 | value = rta_getattr_type(attr); | |
931 | mask = mask_attr ? rta_getattr_type(mask_attr) : type_max; | |
932 | ||
933 | if (is_json_context()) { | |
934 | sprintf(namefrm, "\n %s %%u", name); | |
935 | print_hu(PRINT_ANY, name, namefrm, | |
936 | rta_getattr_type(attr)); | |
937 | if (mask != type_max) { | |
938 | char mask_name[SPRINT_BSIZE-6]; | |
939 | ||
940 | sprintf(mask_name, "%s_mask", name); | |
746e6c0f EB |
941 | if (newline) |
942 | print_string(PRINT_FP, NULL, "%s ", _SL_); | |
943 | sprintf(namefrm, " %s %%u", mask_name); | |
04b21501 EB |
944 | print_hu(PRINT_ANY, mask_name, namefrm, mask); |
945 | } | |
946 | } else { | |
947 | done = sprintf(out, "%u", value); | |
948 | if (mask != type_max) | |
949 | sprintf(out + done, "/0x%x", mask); | |
746e6c0f EB |
950 | if (newline) |
951 | print_string(PRINT_FP, NULL, "%s ", _SL_); | |
952 | sprintf(namefrm, " %s %%s", name); | |
04b21501 EB |
953 | print_string(PRINT_ANY, name, namefrm, out); |
954 | } | |
955 | } | |
956 | ||
c8a49431 | 957 | void print_masked_u32(const char *name, struct rtattr *attr, |
746e6c0f | 958 | struct rtattr *mask_attr, bool newline) |
c8a49431 | 959 | { |
bb3ee8b3 EB |
960 | print_masked_type(UINT32_MAX, rta_getattr_u32, name, attr, mask_attr, |
961 | newline); | |
962 | } | |
c8a49431 | 963 | |
bb3ee8b3 EB |
964 | static __u32 __rta_getattr_u16_u32(const struct rtattr *attr) |
965 | { | |
966 | return rta_getattr_u16(attr); | |
c8a49431 PB |
967 | } |
968 | ||
969 | void print_masked_u16(const char *name, struct rtattr *attr, | |
746e6c0f | 970 | struct rtattr *mask_attr, bool newline) |
c8a49431 | 971 | { |
bb3ee8b3 EB |
972 | print_masked_type(UINT16_MAX, __rta_getattr_u16_u32, name, attr, |
973 | mask_attr, newline); | |
c8a49431 | 974 | } |
04b21501 EB |
975 | |
976 | static __u32 __rta_getattr_u8_u32(const struct rtattr *attr) | |
977 | { | |
978 | return rta_getattr_u8(attr); | |
979 | } | |
980 | ||
981 | void print_masked_u8(const char *name, struct rtattr *attr, | |
746e6c0f | 982 | struct rtattr *mask_attr, bool newline) |
04b21501 EB |
983 | { |
984 | print_masked_type(UINT8_MAX, __rta_getattr_u8_u32, name, attr, | |
746e6c0f | 985 | mask_attr, newline); |
04b21501 | 986 | } |
75fb816d EB |
987 | |
988 | static __u32 __rta_getattr_be16_u32(const struct rtattr *attr) | |
989 | { | |
990 | return rta_getattr_be16(attr); | |
991 | } | |
992 | ||
993 | void print_masked_be16(const char *name, struct rtattr *attr, | |
994 | struct rtattr *mask_attr, bool newline) | |
995 | { | |
996 | print_masked_type(UINT16_MAX, __rta_getattr_be16_u32, name, attr, | |
997 | mask_attr, newline); | |
998 | } |