| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_action.c Action Management |
| 4 |
* |
| 5 |
* Authors: J Hadi Salim ([email protected]) |
| 6 |
* |
| 7 |
* TODO: |
| 8 |
* - parse to be passed a filedescriptor for logging purposes |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdbool.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <sys/socket.h> |
| 18 |
#include <netinet/in.h> |
| 19 |
#include <arpa/inet.h> |
| 20 |
#include <string.h> |
| 21 |
#include <dlfcn.h> |
| 22 |
|
| 23 |
#include "utils.h" |
| 24 |
#include "tc_common.h" |
| 25 |
#include "tc_util.h" |
| 26 |
|
| 27 |
static struct action_util *action_list; |
| 28 |
#ifdef CONFIG_GACT |
| 29 |
static int gact_ld; /* f*ckin backward compatibility */ |
| 30 |
#endif |
| 31 |
static int tab_flush; |
| 32 |
|
| 33 |
static void act_usage(void) |
| 34 |
{ |
| 35 |
/*XXX: In the near future add a action->print_help to improve |
| 36 |
* usability |
| 37 |
* This would mean new tc will not be backward compatible |
| 38 |
* with any action .so from the old days. But if someone really |
| 39 |
* does that, they would know how to fix this .. |
| 40 |
* |
| 41 |
*/ |
| 42 |
fprintf(stderr, |
| 43 |
"usage: tc actions <ACTSPECOP>*\n" |
| 44 |
"Where: ACTSPECOP := ACR | GD | FL\n" |
| 45 |
" ACR := add | change | replace <ACTSPEC>*\n" |
| 46 |
" GD := get | delete | <ACTISPEC>*\n" |
| 47 |
" FL := ls | list | flush | <ACTNAMESPEC>\n" |
| 48 |
" ACTNAMESPEC := action <ACTNAME>\n" |
| 49 |
" ACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n" |
| 50 |
" ACTSPEC := action <ACTDETAIL> [INDEXSPEC] [HWSTATSSPEC] [SKIPSPEC]\n" |
| 51 |
" INDEXSPEC := index <32 bit indexvalue>\n" |
| 52 |
" HWSTATSSPEC := hw_stats [ immediate | delayed | disabled ]\n" |
| 53 |
" SKIPSPEC := [ skip_sw | skip_hw ]\n" |
| 54 |
" ACTDETAIL := <ACTNAME> <ACTPARAMS>\n" |
| 55 |
" Example ACTNAME is gact, mirred, bpf, etc\n" |
| 56 |
" Each action has its own parameters (ACTPARAMS)\n" |
| 57 |
"\n"); |
| 58 |
|
| 59 |
exit(-1); |
| 60 |
} |
| 61 |
|
| 62 |
static int print_noaopt(const struct action_util *au, FILE *f, struct rtattr *opt) |
| 63 |
{ |
| 64 |
if (opt && RTA_PAYLOAD(opt)) |
| 65 |
fprintf(stderr, "[Unknown action, optlen=%u] ", |
| 66 |
(unsigned int) RTA_PAYLOAD(opt)); |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static int parse_noaopt(const struct action_util *au, int *argc_p, |
| 71 |
char ***argv_p, int code, struct nlmsghdr *n) |
| 72 |
{ |
| 73 |
int argc = *argc_p; |
| 74 |
char **argv = *argv_p; |
| 75 |
|
| 76 |
if (argc) |
| 77 |
fprintf(stderr, |
| 78 |
"Unknown action \"%s\", hence option \"%s\" is unparsable\n", |
| 79 |
au->id, *argv); |
| 80 |
else |
| 81 |
fprintf(stderr, "Unknown action \"%s\"\n", au->id); |
| 82 |
|
| 83 |
return -1; |
| 84 |
} |
| 85 |
|
| 86 |
static struct action_util *get_action_kind(const char *str) |
| 87 |
{ |
| 88 |
static void *aBODY; |
| 89 |
void *dlh; |
| 90 |
char buf[256]; |
| 91 |
struct action_util *a; |
| 92 |
#ifdef CONFIG_GACT |
| 93 |
int looked4gact = 0; |
| 94 |
restart_s: |
| 95 |
#endif |
| 96 |
for (a = action_list; a; a = a->next) { |
| 97 |
if (strcmp(a->id, str) == 0) |
| 98 |
return a; |
| 99 |
} |
| 100 |
|
| 101 |
snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str); |
| 102 |
dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); |
| 103 |
if (dlh == NULL) { |
| 104 |
dlh = aBODY; |
| 105 |
if (dlh == NULL) { |
| 106 |
dlh = aBODY = dlopen(NULL, RTLD_LAZY); |
| 107 |
if (dlh == NULL) |
| 108 |
goto noexist; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
snprintf(buf, sizeof(buf), "%s_action_util", str); |
| 113 |
a = dlsym(dlh, buf); |
| 114 |
if (a == NULL) |
| 115 |
goto noexist; |
| 116 |
|
| 117 |
reg: |
| 118 |
a->next = action_list; |
| 119 |
action_list = a; |
| 120 |
return a; |
| 121 |
|
| 122 |
noexist: |
| 123 |
#ifdef CONFIG_GACT |
| 124 |
if (!looked4gact) { |
| 125 |
looked4gact = 1; |
| 126 |
str = "gact"; |
| 127 |
goto restart_s; |
| 128 |
} |
| 129 |
#endif |
| 130 |
a = calloc(1, sizeof(*a)); |
| 131 |
if (a) { |
| 132 |
strncpy(a->id, "noact", 15); |
| 133 |
a->parse_aopt = parse_noaopt; |
| 134 |
a->print_aopt = print_noaopt; |
| 135 |
goto reg; |
| 136 |
} |
| 137 |
return a; |
| 138 |
} |
| 139 |
|
| 140 |
static bool |
| 141 |
new_cmd(char **argv) |
| 142 |
{ |
| 143 |
return (matches(*argv, "change") == 0) || |
| 144 |
(matches(*argv, "replace") == 0) || |
| 145 |
(matches(*argv, "delete") == 0) || |
| 146 |
(matches(*argv, "get") == 0) || |
| 147 |
(matches(*argv, "add") == 0); |
| 148 |
} |
| 149 |
|
| 150 |
static const struct hw_stats_item { |
| 151 |
const char *str; |
| 152 |
__u8 type; |
| 153 |
} hw_stats_items[] = { |
| 154 |
{ "immediate", TCA_ACT_HW_STATS_IMMEDIATE }, |
| 155 |
{ "delayed", TCA_ACT_HW_STATS_DELAYED }, |
| 156 |
{ "disabled", 0 }, /* no bit set */ |
| 157 |
}; |
| 158 |
|
| 159 |
static void print_hw_stats(const struct rtattr *arg, bool print_used) |
| 160 |
{ |
| 161 |
struct nla_bitfield32 *hw_stats_bf = RTA_DATA(arg); |
| 162 |
__u8 hw_stats; |
| 163 |
int i; |
| 164 |
|
| 165 |
hw_stats = hw_stats_bf->value & hw_stats_bf->selector; |
| 166 |
print_string(PRINT_FP, NULL, "\t", NULL); |
| 167 |
open_json_array(PRINT_ANY, print_used ? "used_hw_stats" : "hw_stats"); |
| 168 |
|
| 169 |
for (i = 0; i < ARRAY_SIZE(hw_stats_items); i++) { |
| 170 |
const struct hw_stats_item *item; |
| 171 |
|
| 172 |
item = &hw_stats_items[i]; |
| 173 |
if ((!hw_stats && !item->type) || hw_stats & item->type) |
| 174 |
print_string(PRINT_ANY, NULL, " %s", item->str); |
| 175 |
} |
| 176 |
close_json_array(PRINT_JSON, NULL); |
| 177 |
print_nl(); |
| 178 |
} |
| 179 |
|
| 180 |
static int parse_hw_stats(const char *str, struct nlmsghdr *n) |
| 181 |
{ |
| 182 |
int i; |
| 183 |
|
| 184 |
for (i = 0; i < ARRAY_SIZE(hw_stats_items); i++) { |
| 185 |
const struct hw_stats_item *item; |
| 186 |
|
| 187 |
item = &hw_stats_items[i]; |
| 188 |
if (matches(str, item->str) == 0) { |
| 189 |
struct nla_bitfield32 hw_stats_bf = { |
| 190 |
.value = item->type, |
| 191 |
.selector = item->type |
| 192 |
}; |
| 193 |
|
| 194 |
addattr_l(n, MAX_MSG, TCA_ACT_HW_STATS, |
| 195 |
&hw_stats_bf, sizeof(hw_stats_bf)); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
return -1; |
| 201 |
} |
| 202 |
|
| 203 |
int parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 204 |
{ |
| 205 |
int argc = *argc_p; |
| 206 |
char **argv = *argv_p; |
| 207 |
struct rtattr *tail, *tail2; |
| 208 |
char k[FILTER_NAMESZ]; |
| 209 |
int act_ck_len = 0; |
| 210 |
int ok = 0; |
| 211 |
int eap = 0; /* expect action parameters */ |
| 212 |
|
| 213 |
int ret = 0; |
| 214 |
int prio = 0; |
| 215 |
unsigned char act_ck[TC_COOKIE_MAX_SIZE]; |
| 216 |
|
| 217 |
if (argc <= 0) |
| 218 |
return -1; |
| 219 |
|
| 220 |
tail2 = addattr_nest(n, MAX_MSG, tca_id); |
| 221 |
|
| 222 |
while (argc > 0) { |
| 223 |
|
| 224 |
memset(k, 0, sizeof(k)); |
| 225 |
|
| 226 |
if (strcmp(*argv, "action") == 0) { |
| 227 |
argc--; |
| 228 |
argv++; |
| 229 |
eap = 1; |
| 230 |
#ifdef CONFIG_GACT |
| 231 |
if (!gact_ld) |
| 232 |
get_action_kind("gact"); |
| 233 |
#endif |
| 234 |
continue; |
| 235 |
} else if (strcmp(*argv, "flowid") == 0) { |
| 236 |
break; |
| 237 |
} else if (strcmp(*argv, "classid") == 0) { |
| 238 |
break; |
| 239 |
} else if (strcmp(*argv, "help") == 0) { |
| 240 |
return -1; |
| 241 |
} else if (new_cmd(argv)) { |
| 242 |
goto done0; |
| 243 |
} else { |
| 244 |
struct action_util *a = NULL; |
| 245 |
int skip_loop = 2; |
| 246 |
__u32 flag = 0; |
| 247 |
|
| 248 |
if (!action_a2n(*argv, NULL, false)) |
| 249 |
strncpy(k, "gact", sizeof(k) - 1); |
| 250 |
else |
| 251 |
strncpy(k, *argv, sizeof(k) - 1); |
| 252 |
eap = 0; |
| 253 |
if (argc > 0) { |
| 254 |
a = get_action_kind(k); |
| 255 |
} else { |
| 256 |
done0: |
| 257 |
if (ok) |
| 258 |
break; |
| 259 |
else |
| 260 |
goto done; |
| 261 |
} |
| 262 |
|
| 263 |
if (a == NULL) |
| 264 |
goto bad_val; |
| 265 |
|
| 266 |
|
| 267 |
tail = addattr_nest(n, MAX_MSG, ++prio); |
| 268 |
addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1); |
| 269 |
|
| 270 |
ret = a->parse_aopt(a, &argc, &argv, |
| 271 |
TCA_ACT_OPTIONS | NLA_F_NESTED, |
| 272 |
n); |
| 273 |
|
| 274 |
if (ret < 0) { |
| 275 |
fprintf(stderr, "bad action parsing\n"); |
| 276 |
goto bad_val; |
| 277 |
} |
| 278 |
|
| 279 |
if (*argv && strcmp(*argv, "cookie") == 0) { |
| 280 |
size_t slen; |
| 281 |
|
| 282 |
NEXT_ARG(); |
| 283 |
slen = strlen(*argv); |
| 284 |
if (slen > TC_COOKIE_MAX_SIZE * 2) { |
| 285 |
char cookie_err_m[128]; |
| 286 |
|
| 287 |
snprintf(cookie_err_m, 128, |
| 288 |
"%zd Max allowed size %d", |
| 289 |
slen, TC_COOKIE_MAX_SIZE*2); |
| 290 |
invarg(cookie_err_m, *argv); |
| 291 |
} |
| 292 |
|
| 293 |
if (slen % 2 || |
| 294 |
hex2mem(*argv, act_ck, slen / 2) < 0) |
| 295 |
invarg("cookie must be a hex string\n", |
| 296 |
*argv); |
| 297 |
|
| 298 |
act_ck_len = slen / 2; |
| 299 |
argc--; |
| 300 |
argv++; |
| 301 |
} |
| 302 |
|
| 303 |
if (act_ck_len) |
| 304 |
addattr_l(n, MAX_MSG, TCA_ACT_COOKIE, |
| 305 |
&act_ck, act_ck_len); |
| 306 |
|
| 307 |
if (*argv && matches(*argv, "hw_stats") == 0) { |
| 308 |
NEXT_ARG(); |
| 309 |
ret = parse_hw_stats(*argv, n); |
| 310 |
if (ret < 0) |
| 311 |
invarg("value is invalid\n", *argv); |
| 312 |
NEXT_ARG_FWD(); |
| 313 |
} |
| 314 |
|
| 315 |
if (*argv && strcmp(*argv, "no_percpu") == 0) { |
| 316 |
flag |= TCA_ACT_FLAGS_NO_PERCPU_STATS; |
| 317 |
NEXT_ARG_FWD(); |
| 318 |
} |
| 319 |
|
| 320 |
/* we need to parse twice to fix skip flag out of order */ |
| 321 |
while (skip_loop--) { |
| 322 |
if (*argv && strcmp(*argv, "skip_sw") == 0) { |
| 323 |
flag |= TCA_ACT_FLAGS_SKIP_SW; |
| 324 |
NEXT_ARG_FWD(); |
| 325 |
} else if (*argv && strcmp(*argv, "skip_hw") == 0) { |
| 326 |
flag |= TCA_ACT_FLAGS_SKIP_HW; |
| 327 |
NEXT_ARG_FWD(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (flag) { |
| 332 |
struct nla_bitfield32 flags = |
| 333 |
{ flag, flag }; |
| 334 |
|
| 335 |
addattr_l(n, MAX_MSG, TCA_ACT_FLAGS, &flags, |
| 336 |
sizeof(struct nla_bitfield32)); |
| 337 |
} |
| 338 |
|
| 339 |
addattr_nest_end(n, tail); |
| 340 |
ok++; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
if (eap > 0) { |
| 345 |
fprintf(stderr, "bad action empty %d\n", eap); |
| 346 |
goto bad_val; |
| 347 |
} |
| 348 |
|
| 349 |
addattr_nest_end(n, tail2); |
| 350 |
|
| 351 |
done: |
| 352 |
*argc_p = argc; |
| 353 |
*argv_p = argv; |
| 354 |
return 0; |
| 355 |
bad_val: |
| 356 |
/* no need to undo things, returning from here should |
| 357 |
* cause enough pain |
| 358 |
*/ |
| 359 |
fprintf(stderr, "parse_action: bad value (%d:%s)!\n", argc, *argv); |
| 360 |
return -1; |
| 361 |
} |
| 362 |
|
| 363 |
static int tc_print_one_action(FILE *f, struct rtattr *arg, bool bind) |
| 364 |
{ |
| 365 |
|
| 366 |
struct rtattr *tb[TCA_ACT_MAX + 1]; |
| 367 |
int err = 0; |
| 368 |
struct action_util *a = NULL; |
| 369 |
|
| 370 |
if (arg == NULL) |
| 371 |
return -1; |
| 372 |
|
| 373 |
parse_rtattr_nested(tb, TCA_ACT_MAX, arg); |
| 374 |
|
| 375 |
if (tb[TCA_ACT_KIND] == NULL) { |
| 376 |
fprintf(stderr, "NULL Action!\n"); |
| 377 |
return -1; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND])); |
| 382 |
if (a == NULL) |
| 383 |
return err; |
| 384 |
|
| 385 |
err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]); |
| 386 |
|
| 387 |
if (err < 0) |
| 388 |
return err; |
| 389 |
|
| 390 |
if (brief && tb[TCA_ACT_INDEX]) { |
| 391 |
print_uint(PRINT_ANY, "index", "\t index %u", |
| 392 |
rta_getattr_u32(tb[TCA_ACT_INDEX])); |
| 393 |
print_nl(); |
| 394 |
} |
| 395 |
if (show_stats && tb[TCA_ACT_STATS]) { |
| 396 |
print_string(PRINT_FP, NULL, "\tAction statistics:", NULL); |
| 397 |
print_nl(); |
| 398 |
open_json_object("stats"); |
| 399 |
print_tcstats2_attr(tb[TCA_ACT_STATS], "\t", NULL); |
| 400 |
close_json_object(); |
| 401 |
print_nl(); |
| 402 |
} |
| 403 |
if (tb[TCA_ACT_COOKIE]) { |
| 404 |
int strsz = RTA_PAYLOAD(tb[TCA_ACT_COOKIE]); |
| 405 |
char b1[strsz * 2 + 1]; |
| 406 |
|
| 407 |
print_string(PRINT_ANY, "cookie", "\tcookie %s", |
| 408 |
hexstring_n2a(RTA_DATA(tb[TCA_ACT_COOKIE]), |
| 409 |
strsz, b1, sizeof(b1))); |
| 410 |
print_nl(); |
| 411 |
} |
| 412 |
if (tb[TCA_ACT_FLAGS] || tb[TCA_ACT_IN_HW_COUNT]) { |
| 413 |
bool skip_hw = false; |
| 414 |
bool newline = false; |
| 415 |
|
| 416 |
if (tb[TCA_ACT_FLAGS]) { |
| 417 |
struct nla_bitfield32 *flags = RTA_DATA(tb[TCA_ACT_FLAGS]); |
| 418 |
|
| 419 |
if (flags->selector & TCA_ACT_FLAGS_NO_PERCPU_STATS) { |
| 420 |
newline = true; |
| 421 |
print_bool(PRINT_ANY, "no_percpu", "\tno_percpu", |
| 422 |
flags->value & |
| 423 |
TCA_ACT_FLAGS_NO_PERCPU_STATS); |
| 424 |
} |
| 425 |
if (!bind) { |
| 426 |
if (flags->selector & TCA_ACT_FLAGS_SKIP_HW) { |
| 427 |
newline = true; |
| 428 |
print_bool(PRINT_ANY, "skip_hw", "\tskip_hw", |
| 429 |
flags->value & |
| 430 |
TCA_ACT_FLAGS_SKIP_HW); |
| 431 |
skip_hw = !!(flags->value & TCA_ACT_FLAGS_SKIP_HW); |
| 432 |
} |
| 433 |
if (flags->selector & TCA_ACT_FLAGS_SKIP_SW) { |
| 434 |
newline = true; |
| 435 |
print_bool(PRINT_ANY, "skip_sw", "\tskip_sw", |
| 436 |
flags->value & |
| 437 |
TCA_ACT_FLAGS_SKIP_SW); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
if (tb[TCA_ACT_IN_HW_COUNT] && !bind && !skip_hw) { |
| 442 |
__u32 count = rta_getattr_u32(tb[TCA_ACT_IN_HW_COUNT]); |
| 443 |
|
| 444 |
newline = true; |
| 445 |
if (count) { |
| 446 |
print_bool(PRINT_ANY, "in_hw", "\tin_hw", |
| 447 |
true); |
| 448 |
print_uint(PRINT_ANY, "in_hw_count", |
| 449 |
" in_hw_count %u", count); |
| 450 |
} else { |
| 451 |
print_bool(PRINT_ANY, "not_in_hw", |
| 452 |
"\tnot_in_hw", true); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
if (newline) |
| 457 |
print_nl(); |
| 458 |
} |
| 459 |
if (tb[TCA_ACT_HW_STATS]) |
| 460 |
print_hw_stats(tb[TCA_ACT_HW_STATS], false); |
| 461 |
|
| 462 |
if (tb[TCA_ACT_USED_HW_STATS]) |
| 463 |
print_hw_stats(tb[TCA_ACT_USED_HW_STATS], true); |
| 464 |
|
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
static int |
| 469 |
tc_print_action_flush(FILE *f, const struct rtattr *arg) |
| 470 |
{ |
| 471 |
|
| 472 |
struct rtattr *tb[TCA_MAX + 1]; |
| 473 |
int err = 0; |
| 474 |
struct action_util *a = NULL; |
| 475 |
__u32 *delete_count = 0; |
| 476 |
|
| 477 |
parse_rtattr_nested(tb, TCA_MAX, arg); |
| 478 |
|
| 479 |
if (tb[TCA_KIND] == NULL) { |
| 480 |
fprintf(stderr, "NULL Action!\n"); |
| 481 |
return -1; |
| 482 |
} |
| 483 |
|
| 484 |
a = get_action_kind(RTA_DATA(tb[TCA_KIND])); |
| 485 |
if (a == NULL) |
| 486 |
return err; |
| 487 |
|
| 488 |
delete_count = RTA_DATA(tb[TCA_FCNT]); |
| 489 |
fprintf(f, " %s (%d entries)\n", a->id, *delete_count); |
| 490 |
tab_flush = 0; |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
static int |
| 495 |
tc_dump_action(FILE *f, const struct rtattr *arg, unsigned short tot_acts, |
| 496 |
bool bind) |
| 497 |
{ |
| 498 |
|
| 499 |
int i; |
| 500 |
|
| 501 |
if (arg == NULL) |
| 502 |
return 0; |
| 503 |
|
| 504 |
if (!tot_acts) |
| 505 |
tot_acts = TCA_ACT_MAX_PRIO; |
| 506 |
|
| 507 |
struct rtattr *tb[tot_acts + 1]; |
| 508 |
|
| 509 |
parse_rtattr_nested(tb, tot_acts, arg); |
| 510 |
|
| 511 |
if (tab_flush && tb[0] && !tb[1]) |
| 512 |
return tc_print_action_flush(f, tb[0]); |
| 513 |
|
| 514 |
open_json_array(PRINT_JSON, "actions"); |
| 515 |
for (i = 0; i <= tot_acts; i++) { |
| 516 |
if (tb[i]) { |
| 517 |
open_json_object(NULL); |
| 518 |
print_nl(); |
| 519 |
print_uint(PRINT_ANY, "order", |
| 520 |
"\taction order %u: ", i); |
| 521 |
if (tc_print_one_action(f, tb[i], bind) < 0) |
| 522 |
fprintf(stderr, "Error printing action\n"); |
| 523 |
close_json_object(); |
| 524 |
} |
| 525 |
|
| 526 |
} |
| 527 |
close_json_array(PRINT_JSON, NULL); |
| 528 |
|
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
int |
| 533 |
tc_print_action(FILE *f, const struct rtattr *arg, unsigned short tot_acts) |
| 534 |
{ |
| 535 |
return tc_dump_action(f, arg, tot_acts, true); |
| 536 |
} |
| 537 |
|
| 538 |
int print_action(struct nlmsghdr *n, void *arg) |
| 539 |
{ |
| 540 |
FILE *fp = (FILE *)arg; |
| 541 |
struct tcamsg *t = NLMSG_DATA(n); |
| 542 |
int len = n->nlmsg_len; |
| 543 |
__u32 *tot_acts = NULL; |
| 544 |
struct rtattr *tb[TCA_ROOT_MAX+1]; |
| 545 |
|
| 546 |
len -= NLMSG_LENGTH(sizeof(*t)); |
| 547 |
|
| 548 |
if (len < 0) { |
| 549 |
fprintf(stderr, "Wrong len %d\n", len); |
| 550 |
return -1; |
| 551 |
} |
| 552 |
|
| 553 |
parse_rtattr(tb, TCA_ROOT_MAX, TA_RTA(t), len); |
| 554 |
|
| 555 |
if (tb[TCA_ROOT_COUNT]) |
| 556 |
tot_acts = RTA_DATA(tb[TCA_ROOT_COUNT]); |
| 557 |
|
| 558 |
open_json_object(NULL); |
| 559 |
print_uint(PRINT_ANY, "total acts", "total acts %u", |
| 560 |
tot_acts ? *tot_acts : 0); |
| 561 |
print_nl(); |
| 562 |
close_json_object(); |
| 563 |
if (tb[TCA_ACT_TAB] == NULL) { |
| 564 |
if (n->nlmsg_type != RTM_GETACTION) |
| 565 |
fprintf(stderr, "print_action: NULL kind\n"); |
| 566 |
return -1; |
| 567 |
} |
| 568 |
|
| 569 |
if (n->nlmsg_type == RTM_DELACTION) { |
| 570 |
if (n->nlmsg_flags & NLM_F_ROOT) { |
| 571 |
fprintf(fp, "Flushed table "); |
| 572 |
tab_flush = 1; |
| 573 |
} else { |
| 574 |
fprintf(fp, "Deleted action "); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (n->nlmsg_type == RTM_NEWACTION) { |
| 579 |
if ((n->nlmsg_flags & NLM_F_CREATE) && |
| 580 |
!(n->nlmsg_flags & NLM_F_REPLACE)) { |
| 581 |
fprintf(fp, "Added action "); |
| 582 |
} else if (n->nlmsg_flags & NLM_F_REPLACE) { |
| 583 |
fprintf(fp, "Replaced action "); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
open_json_object(NULL); |
| 588 |
tc_dump_action(fp, tb[TCA_ACT_TAB], tot_acts ? *tot_acts:0, false); |
| 589 |
|
| 590 |
if (tb[TCA_ROOT_EXT_WARN_MSG]) { |
| 591 |
print_string(PRINT_ANY, "warn", "%s", |
| 592 |
rta_getattr_str(tb[TCA_ROOT_EXT_WARN_MSG])); |
| 593 |
print_nl(); |
| 594 |
} |
| 595 |
|
| 596 |
close_json_object(); |
| 597 |
|
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
static int tc_action_gd(int cmd, unsigned int flags, |
| 602 |
int *argc_p, char ***argv_p) |
| 603 |
{ |
| 604 |
char k[FILTER_NAMESZ]; |
| 605 |
struct action_util *a = NULL; |
| 606 |
int argc = *argc_p; |
| 607 |
char **argv = *argv_p; |
| 608 |
int prio = 0; |
| 609 |
int ret = 0; |
| 610 |
__u32 i = 0; |
| 611 |
struct rtattr *tail; |
| 612 |
struct rtattr *tail2; |
| 613 |
struct nlmsghdr *ans = NULL; |
| 614 |
|
| 615 |
struct { |
| 616 |
struct nlmsghdr n; |
| 617 |
struct tcamsg t; |
| 618 |
char buf[MAX_MSG]; |
| 619 |
} req = { |
| 620 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)), |
| 621 |
.n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 622 |
.n.nlmsg_type = cmd, |
| 623 |
.t.tca_family = AF_UNSPEC, |
| 624 |
}; |
| 625 |
|
| 626 |
argc -= 1; |
| 627 |
argv += 1; |
| 628 |
|
| 629 |
|
| 630 |
tail = addattr_nest(&req.n, MAX_MSG, TCA_ACT_TAB); |
| 631 |
|
| 632 |
while (argc > 0) { |
| 633 |
if (strcmp(*argv, "action") == 0) { |
| 634 |
argc--; |
| 635 |
argv++; |
| 636 |
continue; |
| 637 |
} else if (strcmp(*argv, "help") == 0) { |
| 638 |
return -1; |
| 639 |
} |
| 640 |
|
| 641 |
strncpy(k, *argv, sizeof(k) - 1); |
| 642 |
a = get_action_kind(k); |
| 643 |
if (a == NULL) { |
| 644 |
fprintf(stderr, "Error: non existent action: %s\n", k); |
| 645 |
ret = -1; |
| 646 |
goto bad_val; |
| 647 |
} |
| 648 |
if (strcmp(a->id, k) != 0) { |
| 649 |
fprintf(stderr, "Error: non existent action: %s\n", k); |
| 650 |
ret = -1; |
| 651 |
goto bad_val; |
| 652 |
} |
| 653 |
|
| 654 |
argc -= 1; |
| 655 |
argv += 1; |
| 656 |
if (argc <= 0) { |
| 657 |
fprintf(stderr, |
| 658 |
"Error: no index specified action: %s\n", k); |
| 659 |
ret = -1; |
| 660 |
goto bad_val; |
| 661 |
} |
| 662 |
|
| 663 |
if (matches(*argv, "index") == 0) { |
| 664 |
NEXT_ARG(); |
| 665 |
if (get_u32(&i, *argv, 10)) { |
| 666 |
fprintf(stderr, "Illegal \"index\"\n"); |
| 667 |
ret = -1; |
| 668 |
goto bad_val; |
| 669 |
} |
| 670 |
argc -= 1; |
| 671 |
argv += 1; |
| 672 |
} else { |
| 673 |
fprintf(stderr, |
| 674 |
"Error: no index specified action: %s\n", k); |
| 675 |
ret = -1; |
| 676 |
goto bad_val; |
| 677 |
} |
| 678 |
|
| 679 |
tail2 = addattr_nest(&req.n, MAX_MSG, ++prio); |
| 680 |
addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1); |
| 681 |
if (i > 0) |
| 682 |
addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i); |
| 683 |
addattr_nest_end(&req.n, tail2); |
| 684 |
|
| 685 |
} |
| 686 |
|
| 687 |
addattr_nest_end(&req.n, tail); |
| 688 |
|
| 689 |
req.n.nlmsg_seq = rth.dump = ++rth.seq; |
| 690 |
|
| 691 |
if (cmd == RTM_DELACTION) { |
| 692 |
if (echo_request) |
| 693 |
ret = rtnl_echo_talk(&rth, &req.n, json, print_action); |
| 694 |
else |
| 695 |
ret = rtnl_talk(&rth, &req.n, NULL); |
| 696 |
} else { |
| 697 |
ret = rtnl_talk(&rth, &req.n, &ans); |
| 698 |
} |
| 699 |
|
| 700 |
if (ret < 0) { |
| 701 |
fprintf(stderr, "We have an error talking to the kernel\n"); |
| 702 |
return 1; |
| 703 |
} |
| 704 |
|
| 705 |
if (cmd == RTM_GETACTION) { |
| 706 |
new_json_obj(json); |
| 707 |
ret = print_action(ans, stdout); |
| 708 |
if (ret < 0) { |
| 709 |
fprintf(stderr, "Dump terminated\n"); |
| 710 |
free(ans); |
| 711 |
delete_json_obj(); |
| 712 |
return 1; |
| 713 |
} |
| 714 |
delete_json_obj(); |
| 715 |
} |
| 716 |
free(ans); |
| 717 |
|
| 718 |
*argc_p = argc; |
| 719 |
*argv_p = argv; |
| 720 |
bad_val: |
| 721 |
return ret; |
| 722 |
} |
| 723 |
|
| 724 |
static int tc_action_modify(int cmd, unsigned int flags, |
| 725 |
int *argc_p, char ***argv_p) |
| 726 |
{ |
| 727 |
int argc = *argc_p; |
| 728 |
char **argv = *argv_p; |
| 729 |
int ret = 0; |
| 730 |
struct { |
| 731 |
struct nlmsghdr n; |
| 732 |
struct tcamsg t; |
| 733 |
char buf[MAX_MSG]; |
| 734 |
} req = { |
| 735 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)), |
| 736 |
.n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 737 |
.n.nlmsg_type = cmd, |
| 738 |
.t.tca_family = AF_UNSPEC, |
| 739 |
}; |
| 740 |
struct rtattr *tail = NLMSG_TAIL(&req.n); |
| 741 |
|
| 742 |
argc -= 1; |
| 743 |
argv += 1; |
| 744 |
if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) { |
| 745 |
fprintf(stderr, "Illegal \"action\"\n"); |
| 746 |
return -1; |
| 747 |
} |
| 748 |
tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail; |
| 749 |
|
| 750 |
if (echo_request) |
| 751 |
ret = rtnl_echo_talk(&rth, &req.n, json, print_action); |
| 752 |
else |
| 753 |
ret = rtnl_talk(&rth, &req.n, NULL); |
| 754 |
|
| 755 |
if (ret < 0) { |
| 756 |
fprintf(stderr, "We have an error talking to the kernel\n"); |
| 757 |
ret = -1; |
| 758 |
} |
| 759 |
|
| 760 |
*argc_p = argc; |
| 761 |
*argv_p = argv; |
| 762 |
|
| 763 |
return ret; |
| 764 |
} |
| 765 |
|
| 766 |
static int tc_act_list_or_flush(int *argc_p, char ***argv_p, int event) |
| 767 |
{ |
| 768 |
struct rtattr *tail, *tail2, *tail3, *tail4; |
| 769 |
int ret = 0, prio = 0, msg_size = 0; |
| 770 |
struct action_util *a = NULL; |
| 771 |
struct nla_bitfield32 flag_select = { 0 }; |
| 772 |
char **argv = *argv_p; |
| 773 |
__u32 msec_since = 0; |
| 774 |
int argc = *argc_p; |
| 775 |
char k[FILTER_NAMESZ]; |
| 776 |
struct { |
| 777 |
struct nlmsghdr n; |
| 778 |
struct tcamsg t; |
| 779 |
char buf[MAX_MSG]; |
| 780 |
} req = { |
| 781 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)), |
| 782 |
.t.tca_family = AF_UNSPEC, |
| 783 |
}; |
| 784 |
|
| 785 |
tail = addattr_nest(&req.n, MAX_MSG, TCA_ACT_TAB); |
| 786 |
tail2 = NLMSG_TAIL(&req.n); |
| 787 |
|
| 788 |
strncpy(k, *argv, sizeof(k) - 1); |
| 789 |
#ifdef CONFIG_GACT |
| 790 |
if (!gact_ld) |
| 791 |
get_action_kind("gact"); |
| 792 |
|
| 793 |
#endif |
| 794 |
a = get_action_kind(k); |
| 795 |
if (a == NULL) { |
| 796 |
fprintf(stderr, "bad action %s\n", k); |
| 797 |
goto bad_val; |
| 798 |
} |
| 799 |
if (strcmp(a->id, k) != 0) { |
| 800 |
fprintf(stderr, "bad action %s\n", k); |
| 801 |
goto bad_val; |
| 802 |
} |
| 803 |
strncpy(k, *argv, sizeof(k) - 1); |
| 804 |
|
| 805 |
argc -= 1; |
| 806 |
argv += 1; |
| 807 |
|
| 808 |
if (argc && (strcmp(*argv, "since") == 0)) { |
| 809 |
NEXT_ARG(); |
| 810 |
if (get_u32(&msec_since, *argv, 0)) |
| 811 |
invarg("dump time \"since\" is invalid", *argv); |
| 812 |
} |
| 813 |
|
| 814 |
addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0); |
| 815 |
addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1); |
| 816 |
tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2; |
| 817 |
addattr_nest_end(&req.n, tail); |
| 818 |
|
| 819 |
tail3 = NLMSG_TAIL(&req.n); |
| 820 |
flag_select.value |= TCA_ACT_FLAG_LARGE_DUMP_ON; |
| 821 |
flag_select.selector |= TCA_ACT_FLAG_LARGE_DUMP_ON; |
| 822 |
if (brief) { |
| 823 |
flag_select.value |= TCA_ACT_FLAG_TERSE_DUMP; |
| 824 |
flag_select.selector |= TCA_ACT_FLAG_TERSE_DUMP; |
| 825 |
} |
| 826 |
addattr_l(&req.n, MAX_MSG, TCA_ROOT_FLAGS, &flag_select, |
| 827 |
sizeof(struct nla_bitfield32)); |
| 828 |
tail3->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail3; |
| 829 |
if (msec_since) { |
| 830 |
tail4 = NLMSG_TAIL(&req.n); |
| 831 |
addattr32(&req.n, MAX_MSG, TCA_ROOT_TIME_DELTA, msec_since); |
| 832 |
tail4->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail4; |
| 833 |
} |
| 834 |
msg_size = NLMSG_ALIGN(req.n.nlmsg_len) |
| 835 |
- NLMSG_ALIGN(sizeof(struct nlmsghdr)); |
| 836 |
|
| 837 |
if (event == RTM_GETACTION) { |
| 838 |
if (rtnl_dump_request(&rth, event, |
| 839 |
(void *)&req.t, msg_size) < 0) { |
| 840 |
perror("Cannot send dump request"); |
| 841 |
return 1; |
| 842 |
} |
| 843 |
new_json_obj(json); |
| 844 |
ret = rtnl_dump_filter(&rth, print_action, stdout); |
| 845 |
delete_json_obj(); |
| 846 |
} |
| 847 |
|
| 848 |
if (event == RTM_DELACTION) { |
| 849 |
req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len); |
| 850 |
req.n.nlmsg_type = RTM_DELACTION; |
| 851 |
req.n.nlmsg_flags |= NLM_F_ROOT; |
| 852 |
req.n.nlmsg_flags |= NLM_F_REQUEST; |
| 853 |
|
| 854 |
if (echo_request) |
| 855 |
ret = rtnl_echo_talk(&rth, &req.n, json, print_action); |
| 856 |
else |
| 857 |
ret = rtnl_talk(&rth, &req.n, NULL); |
| 858 |
if (ret < 0) { |
| 859 |
fprintf(stderr, "We have an error flushing\n"); |
| 860 |
return 1; |
| 861 |
} |
| 862 |
|
| 863 |
} |
| 864 |
|
| 865 |
bad_val: |
| 866 |
|
| 867 |
*argc_p = argc; |
| 868 |
*argv_p = argv; |
| 869 |
return ret; |
| 870 |
} |
| 871 |
|
| 872 |
int do_action(int argc, char **argv) |
| 873 |
{ |
| 874 |
|
| 875 |
int ret = 0; |
| 876 |
|
| 877 |
while (argc > 0) { |
| 878 |
|
| 879 |
if (matches(*argv, "add") == 0) { |
| 880 |
ret = tc_action_modify(RTM_NEWACTION, |
| 881 |
NLM_F_EXCL | NLM_F_CREATE, |
| 882 |
&argc, &argv); |
| 883 |
} else if (matches(*argv, "change") == 0 || |
| 884 |
matches(*argv, "replace") == 0) { |
| 885 |
ret = tc_action_modify(RTM_NEWACTION, |
| 886 |
NLM_F_CREATE | NLM_F_REPLACE, |
| 887 |
&argc, &argv); |
| 888 |
} else if (matches(*argv, "delete") == 0) { |
| 889 |
argc -= 1; |
| 890 |
argv += 1; |
| 891 |
ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv); |
| 892 |
} else if (matches(*argv, "get") == 0) { |
| 893 |
argc -= 1; |
| 894 |
argv += 1; |
| 895 |
ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv); |
| 896 |
} else if (matches(*argv, "list") == 0 || |
| 897 |
matches(*argv, "show") == 0 || |
| 898 |
matches(*argv, "lst") == 0) { |
| 899 |
if (argc <= 2) { |
| 900 |
act_usage(); |
| 901 |
return -1; |
| 902 |
} |
| 903 |
|
| 904 |
argc -= 2; |
| 905 |
argv += 2; |
| 906 |
return tc_act_list_or_flush(&argc, &argv, |
| 907 |
RTM_GETACTION); |
| 908 |
} else if (matches(*argv, "flush") == 0) { |
| 909 |
if (argc <= 2) { |
| 910 |
act_usage(); |
| 911 |
return -1; |
| 912 |
} |
| 913 |
|
| 914 |
argc -= 2; |
| 915 |
argv += 2; |
| 916 |
return tc_act_list_or_flush(&argc, &argv, |
| 917 |
RTM_DELACTION); |
| 918 |
} else if (matches(*argv, "help") == 0) { |
| 919 |
act_usage(); |
| 920 |
return -1; |
| 921 |
} else { |
| 922 |
fprintf(stderr, |
| 923 |
"Command \"%s\" is unknown, try \"tc actions help\".\n", |
| 924 |
*argv); |
| 925 |
return -1; |
| 926 |
} |
| 927 |
|
| 928 |
if (ret < 0) |
| 929 |
return -1; |
| 930 |
} |
| 931 |
|
| 932 |
return 0; |
| 933 |
} |