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