2 * m_action.c Action Management
4 * This program is free software; you can distribute 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.
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
12 * - parse to be passed a filedescriptor for logging purposes
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
28 #include "tc_common.h"
31 static struct action_util
*action_list
;
33 static int gact_ld
; /* f*ckin backward compatibility */
37 static void act_usage(void)
39 /*XXX: In the near future add a action->print_help to improve
41 * This would mean new tc will not be backward compatible
42 * with any action .so from the old days. But if someone really
43 * does that, they would know how to fix this ..
47 "usage: tc actions <ACTSPECOP>*\n"
48 "Where: ACTSPECOP := ACR | GD | FL\n"
49 " ACR := add | change | replace <ACTSPEC>*\n"
50 " GD := get | delete | <ACTISPEC>*\n"
51 " FL := ls | list | flush | <ACTNAMESPEC>\n"
52 " ACTNAMESPEC := action <ACTNAME>\n"
53 " ACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
54 " ACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
55 " INDEXSPEC := index <32 bit indexvalue>\n"
56 " ACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
57 " Example ACTNAME is gact, mirred, bpf, etc\n"
58 " Each action has its own parameters (ACTPARAMS)\n"
64 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
66 if (opt
&& RTA_PAYLOAD(opt
))
67 fprintf(f
, "[Unknown action, optlen=%u] ",
68 (unsigned int) RTA_PAYLOAD(opt
));
72 static int parse_noaopt(struct action_util
*au
, int *argc_p
,
73 char ***argv_p
, int code
, struct nlmsghdr
*n
)
76 char **argv
= *argv_p
;
80 "Unknown action \"%s\", hence option \"%s\" is unparsable\n",
83 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
88 static struct action_util
*get_action_kind(char *str
)
93 struct action_util
*a
;
98 for (a
= action_list
; a
; a
= a
->next
) {
99 if (strcmp(a
->id
, str
) == 0)
103 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
104 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
108 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
114 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
120 a
->next
= action_list
;
132 a
= calloc(1, sizeof(*a
));
134 strncpy(a
->id
, "noact", 15);
135 a
->parse_aopt
= parse_noaopt
;
136 a
->print_aopt
= print_noaopt
;
145 return (matches(*argv
, "change") == 0) ||
146 (matches(*argv
, "replace") == 0) ||
147 (matches(*argv
, "delete") == 0) ||
148 (matches(*argv
, "get") == 0) ||
149 (matches(*argv
, "add") == 0);
152 int parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
155 char **argv
= *argv_p
;
156 struct rtattr
*tail
, *tail2
;
157 char k
[FILTER_NAMESZ
];
160 int eap
= 0; /* expect action parameters */
164 unsigned char act_ck
[TC_COOKIE_MAX_SIZE
];
169 tail2
= addattr_nest(n
, MAX_MSG
, tca_id
);
173 memset(k
, 0, sizeof(k
));
175 if (strcmp(*argv
, "action") == 0) {
181 get_action_kind("gact");
184 } else if (strcmp(*argv
, "flowid") == 0) {
186 } else if (strcmp(*argv
, "classid") == 0) {
188 } else if (strcmp(*argv
, "help") == 0) {
190 } else if (new_cmd(argv
)) {
193 struct action_util
*a
= NULL
;
195 if (!action_a2n(*argv
, NULL
, false))
196 strncpy(k
, "gact", sizeof(k
) - 1);
198 strncpy(k
, *argv
, sizeof(k
) - 1);
201 a
= get_action_kind(k
);
214 tail
= addattr_nest(n
, MAX_MSG
, ++prio
);
215 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
217 ret
= a
->parse_aopt(a
, &argc
, &argv
,
218 TCA_ACT_OPTIONS
| NLA_F_NESTED
,
222 fprintf(stderr
, "bad action parsing\n");
226 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
230 slen
= strlen(*argv
);
231 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
232 char cookie_err_m
[128];
234 snprintf(cookie_err_m
, 128,
235 "%zd Max allowed size %d",
236 slen
, TC_COOKIE_MAX_SIZE
*2);
237 invarg(cookie_err_m
, *argv
);
240 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
241 invarg("cookie must be a hex string\n",
244 act_ck_len
= slen
/ 2;
250 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
251 &act_ck
, act_ck_len
);
253 if (*argv
&& strcmp(*argv
, "no_percpu") == 0) {
254 struct nla_bitfield32 flags
=
255 { TCA_ACT_FLAGS_NO_PERCPU_STATS
,
256 TCA_ACT_FLAGS_NO_PERCPU_STATS
};
258 addattr_l(n
, MAX_MSG
, TCA_ACT_FLAGS
, &flags
,
259 sizeof(struct nla_bitfield32
));
263 addattr_nest_end(n
, tail
);
269 fprintf(stderr
, "bad action empty %d\n", eap
);
273 addattr_nest_end(n
, tail2
);
280 /* no need to undo things, returning from here should
283 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
287 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
290 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
292 struct action_util
*a
= NULL
;
297 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
299 if (tb
[TCA_ACT_KIND
] == NULL
) {
300 fprintf(stderr
, "NULL Action!\n");
305 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
309 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
314 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
315 print_string(PRINT_FP
, NULL
, "\tAction statistics:", NULL
);
316 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
317 open_json_object("stats");
318 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
320 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
322 if (tb
[TCA_ACT_COOKIE
]) {
323 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
324 char b1
[strsz
* 2 + 1];
326 print_string(PRINT_ANY
, "cookie", "\tcookie %s",
327 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
328 strsz
, b1
, sizeof(b1
)));
329 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
331 if (tb
[TCA_ACT_FLAGS
]) {
332 struct nla_bitfield32
*flags
= RTA_DATA(tb
[TCA_ACT_FLAGS
]);
334 if (flags
->selector
& TCA_ACT_FLAGS_NO_PERCPU_STATS
)
335 print_bool(PRINT_ANY
, "no_percpu", "\tno_percpu",
337 TCA_ACT_FLAGS_NO_PERCPU_STATS
);
338 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
345 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
348 struct rtattr
*tb
[TCA_MAX
+ 1];
350 struct action_util
*a
= NULL
;
351 __u32
*delete_count
= 0;
353 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
355 if (tb
[TCA_KIND
] == NULL
) {
356 fprintf(stderr
, "NULL Action!\n");
360 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
364 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
365 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
371 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
380 tot_acts
= TCA_ACT_MAX_PRIO
;
382 struct rtattr
*tb
[tot_acts
+ 1];
384 parse_rtattr_nested(tb
, tot_acts
, arg
);
386 if (tab_flush
&& tb
[0] && !tb
[1])
387 return tc_print_action_flush(f
, tb
[0]);
389 open_json_array(PRINT_JSON
, "actions");
390 for (i
= 0; i
<= tot_acts
; i
++) {
392 open_json_object(NULL
);
393 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
394 print_uint(PRINT_ANY
, "order",
395 "\taction order %u: ", i
);
396 if (tc_print_one_action(f
, tb
[i
]) < 0) {
397 print_string(PRINT_FP
, NULL
,
398 "Error printing action\n", NULL
);
404 close_json_array(PRINT_JSON
, NULL
);
409 int print_action(struct nlmsghdr
*n
, void *arg
)
411 FILE *fp
= (FILE *)arg
;
412 struct tcamsg
*t
= NLMSG_DATA(n
);
413 int len
= n
->nlmsg_len
;
414 __u32
*tot_acts
= NULL
;
415 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
417 len
-= NLMSG_LENGTH(sizeof(*t
));
420 fprintf(stderr
, "Wrong len %d\n", len
);
424 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
426 if (tb
[TCA_ROOT_COUNT
])
427 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
429 open_json_object(NULL
);
430 print_uint(PRINT_ANY
, "total acts", "total acts %u",
431 tot_acts
? *tot_acts
: 0);
432 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
434 if (tb
[TCA_ACT_TAB
] == NULL
) {
435 if (n
->nlmsg_type
!= RTM_GETACTION
)
436 fprintf(stderr
, "print_action: NULL kind\n");
440 if (n
->nlmsg_type
== RTM_DELACTION
) {
441 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
442 fprintf(fp
, "Flushed table ");
445 fprintf(fp
, "Deleted action ");
449 if (n
->nlmsg_type
== RTM_NEWACTION
) {
450 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
451 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
452 fprintf(fp
, "Added action ");
453 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
454 fprintf(fp
, "Replaced action ");
458 open_json_object(NULL
);
459 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
465 static int tc_action_gd(int cmd
, unsigned int flags
,
466 int *argc_p
, char ***argv_p
)
468 char k
[FILTER_NAMESZ
];
469 struct action_util
*a
= NULL
;
471 char **argv
= *argv_p
;
476 struct rtattr
*tail2
;
477 struct nlmsghdr
*ans
= NULL
;
484 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
485 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
487 .t
.tca_family
= AF_UNSPEC
,
494 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
497 if (strcmp(*argv
, "action") == 0) {
501 } else if (strcmp(*argv
, "help") == 0) {
505 strncpy(k
, *argv
, sizeof(k
) - 1);
506 a
= get_action_kind(k
);
508 fprintf(stderr
, "Error: non existent action: %s\n", k
);
512 if (strcmp(a
->id
, k
) != 0) {
513 fprintf(stderr
, "Error: non existent action: %s\n", k
);
522 "Error: no index specified action: %s\n", k
);
527 if (matches(*argv
, "index") == 0) {
529 if (get_u32(&i
, *argv
, 10)) {
530 fprintf(stderr
, "Illegal \"index\"\n");
538 "Error: no index specified action: %s\n", k
);
543 tail2
= addattr_nest(&req
.n
, MAX_MSG
, ++prio
);
544 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
546 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
547 addattr_nest_end(&req
.n
, tail2
);
551 addattr_nest_end(&req
.n
, tail
);
553 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
555 if (rtnl_talk(&rth
, &req
.n
, cmd
== RTM_DELACTION
? NULL
: &ans
) < 0) {
556 fprintf(stderr
, "We have an error talking to the kernel\n");
560 if (cmd
== RTM_GETACTION
) {
562 ret
= print_action(ans
, stdout
);
564 fprintf(stderr
, "Dump terminated\n");
579 static int tc_action_modify(int cmd
, unsigned int flags
,
580 int *argc_p
, char ***argv_p
)
583 char **argv
= *argv_p
;
590 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
591 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
593 .t
.tca_family
= AF_UNSPEC
,
595 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
599 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
600 fprintf(stderr
, "Illegal \"action\"\n");
603 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
605 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
606 fprintf(stderr
, "We have an error talking to the kernel\n");
616 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
618 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
619 int ret
= 0, prio
= 0, msg_size
= 0;
620 struct action_util
*a
= NULL
;
621 struct nla_bitfield32 flag_select
= { 0 };
622 char **argv
= *argv_p
;
623 __u32 msec_since
= 0;
625 char k
[FILTER_NAMESZ
];
631 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
632 .t
.tca_family
= AF_UNSPEC
,
635 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
636 tail2
= NLMSG_TAIL(&req
.n
);
638 strncpy(k
, *argv
, sizeof(k
) - 1);
641 get_action_kind("gact");
644 a
= get_action_kind(k
);
646 fprintf(stderr
, "bad action %s\n", k
);
649 if (strcmp(a
->id
, k
) != 0) {
650 fprintf(stderr
, "bad action %s\n", k
);
653 strncpy(k
, *argv
, sizeof(k
) - 1);
658 if (argc
&& (strcmp(*argv
, "since") == 0)) {
660 if (get_u32(&msec_since
, *argv
, 0))
661 invarg("dump time \"since\" is invalid", *argv
);
664 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
665 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
666 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
667 addattr_nest_end(&req
.n
, tail
);
669 tail3
= NLMSG_TAIL(&req
.n
);
670 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
671 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
672 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
673 sizeof(struct nla_bitfield32
));
674 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
676 tail4
= NLMSG_TAIL(&req
.n
);
677 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
678 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
680 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
)
681 - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
683 if (event
== RTM_GETACTION
) {
684 if (rtnl_dump_request(&rth
, event
,
685 (void *)&req
.t
, msg_size
) < 0) {
686 perror("Cannot send dump request");
690 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
694 if (event
== RTM_DELACTION
) {
695 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
696 req
.n
.nlmsg_type
= RTM_DELACTION
;
697 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
698 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
699 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
700 fprintf(stderr
, "We have an error flushing\n");
713 int do_action(int argc
, char **argv
)
720 if (matches(*argv
, "add") == 0) {
721 ret
= tc_action_modify(RTM_NEWACTION
,
722 NLM_F_EXCL
| NLM_F_CREATE
,
724 } else if (matches(*argv
, "change") == 0 ||
725 matches(*argv
, "replace") == 0) {
726 ret
= tc_action_modify(RTM_NEWACTION
,
727 NLM_F_CREATE
| NLM_F_REPLACE
,
729 } else if (matches(*argv
, "delete") == 0) {
732 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
733 } else if (matches(*argv
, "get") == 0) {
736 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
737 } else if (matches(*argv
, "list") == 0 ||
738 matches(*argv
, "show") == 0 ||
739 matches(*argv
, "lst") == 0) {
747 return tc_act_list_or_flush(&argc
, &argv
,
749 } else if (matches(*argv
, "flush") == 0) {
757 return tc_act_list_or_flush(&argc
, &argv
,
759 } else if (matches(*argv
, "help") == 0) {
764 "Command \"%s\" is unknown, try \"tc actions help\".\n",