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