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] [HWSTATSSPEC]\n"
55 " INDEXSPEC := index <32 bit indexvalue>\n"
56 " HWSTATSSPEC := hw_stats [ immediate | delayed | disabled ]\n"
57 " ACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
58 " Example ACTNAME is gact, mirred, bpf, etc\n"
59 " Each action has its own parameters (ACTPARAMS)\n"
65 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
67 if (opt
&& RTA_PAYLOAD(opt
))
68 fprintf(f
, "[Unknown action, optlen=%u] ",
69 (unsigned int) RTA_PAYLOAD(opt
));
73 static int parse_noaopt(struct action_util
*au
, int *argc_p
,
74 char ***argv_p
, int code
, struct nlmsghdr
*n
)
77 char **argv
= *argv_p
;
81 "Unknown action \"%s\", hence option \"%s\" is unparsable\n",
84 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
89 static struct action_util
*get_action_kind(char *str
)
94 struct action_util
*a
;
99 for (a
= action_list
; a
; a
= a
->next
) {
100 if (strcmp(a
->id
, str
) == 0)
104 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
105 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
109 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
115 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
121 a
->next
= action_list
;
133 a
= calloc(1, sizeof(*a
));
135 strncpy(a
->id
, "noact", 15);
136 a
->parse_aopt
= parse_noaopt
;
137 a
->print_aopt
= print_noaopt
;
146 return (matches(*argv
, "change") == 0) ||
147 (matches(*argv
, "replace") == 0) ||
148 (matches(*argv
, "delete") == 0) ||
149 (matches(*argv
, "get") == 0) ||
150 (matches(*argv
, "add") == 0);
153 static const struct hw_stats_item
{
156 } hw_stats_items
[] = {
157 { "immediate", TCA_ACT_HW_STATS_IMMEDIATE
},
158 { "delayed", TCA_ACT_HW_STATS_DELAYED
},
159 { "disabled", 0 }, /* no bit set */
162 static void print_hw_stats(const struct rtattr
*arg
, bool print_used
)
164 struct nla_bitfield32
*hw_stats_bf
= RTA_DATA(arg
);
168 hw_stats
= hw_stats_bf
->value
& hw_stats_bf
->selector
;
169 print_string(PRINT_FP
, NULL
, "\t", NULL
);
170 open_json_array(PRINT_ANY
, print_used
? "used_hw_stats" : "hw_stats");
172 for (i
= 0; i
< ARRAY_SIZE(hw_stats_items
); i
++) {
173 const struct hw_stats_item
*item
;
175 item
= &hw_stats_items
[i
];
176 if ((!hw_stats
&& !item
->type
) || hw_stats
& item
->type
)
177 print_string(PRINT_ANY
, NULL
, " %s", item
->str
);
179 close_json_array(PRINT_JSON
, NULL
);
180 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
183 static int parse_hw_stats(const char *str
, struct nlmsghdr
*n
)
187 for (i
= 0; i
< ARRAY_SIZE(hw_stats_items
); i
++) {
188 const struct hw_stats_item
*item
;
190 item
= &hw_stats_items
[i
];
191 if (matches(str
, item
->str
) == 0) {
192 struct nla_bitfield32 hw_stats_bf
= {
194 .selector
= item
->type
197 addattr_l(n
, MAX_MSG
, TCA_ACT_HW_STATS
,
198 &hw_stats_bf
, sizeof(hw_stats_bf
));
206 int parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
209 char **argv
= *argv_p
;
210 struct rtattr
*tail
, *tail2
;
211 char k
[FILTER_NAMESZ
];
214 int eap
= 0; /* expect action parameters */
218 unsigned char act_ck
[TC_COOKIE_MAX_SIZE
];
223 tail2
= addattr_nest(n
, MAX_MSG
, tca_id
);
227 memset(k
, 0, sizeof(k
));
229 if (strcmp(*argv
, "action") == 0) {
235 get_action_kind("gact");
238 } else if (strcmp(*argv
, "flowid") == 0) {
240 } else if (strcmp(*argv
, "classid") == 0) {
242 } else if (strcmp(*argv
, "help") == 0) {
244 } else if (new_cmd(argv
)) {
247 struct action_util
*a
= NULL
;
249 if (!action_a2n(*argv
, NULL
, false))
250 strncpy(k
, "gact", sizeof(k
) - 1);
252 strncpy(k
, *argv
, sizeof(k
) - 1);
255 a
= get_action_kind(k
);
268 tail
= addattr_nest(n
, MAX_MSG
, ++prio
);
269 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
271 ret
= a
->parse_aopt(a
, &argc
, &argv
,
272 TCA_ACT_OPTIONS
| NLA_F_NESTED
,
276 fprintf(stderr
, "bad action parsing\n");
280 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
284 slen
= strlen(*argv
);
285 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
286 char cookie_err_m
[128];
288 snprintf(cookie_err_m
, 128,
289 "%zd Max allowed size %d",
290 slen
, TC_COOKIE_MAX_SIZE
*2);
291 invarg(cookie_err_m
, *argv
);
294 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
295 invarg("cookie must be a hex string\n",
298 act_ck_len
= slen
/ 2;
304 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
305 &act_ck
, act_ck_len
);
307 if (*argv
&& matches(*argv
, "hw_stats") == 0) {
309 ret
= parse_hw_stats(*argv
, n
);
311 invarg("value is invalid\n", *argv
);
315 if (*argv
&& strcmp(*argv
, "no_percpu") == 0) {
316 struct nla_bitfield32 flags
=
317 { TCA_ACT_FLAGS_NO_PERCPU_STATS
,
318 TCA_ACT_FLAGS_NO_PERCPU_STATS
};
320 addattr_l(n
, MAX_MSG
, TCA_ACT_FLAGS
, &flags
,
321 sizeof(struct nla_bitfield32
));
325 addattr_nest_end(n
, tail
);
331 fprintf(stderr
, "bad action empty %d\n", eap
);
335 addattr_nest_end(n
, tail2
);
342 /* no need to undo things, returning from here should
345 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
349 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
352 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
354 struct action_util
*a
= NULL
;
359 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
361 if (tb
[TCA_ACT_KIND
] == NULL
) {
362 fprintf(stderr
, "NULL Action!\n");
367 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
371 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
376 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
377 print_string(PRINT_FP
, NULL
, "\tAction statistics:", NULL
);
378 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
379 open_json_object("stats");
380 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
382 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
384 if (tb
[TCA_ACT_COOKIE
]) {
385 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
386 char b1
[strsz
* 2 + 1];
388 print_string(PRINT_ANY
, "cookie", "\tcookie %s",
389 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
390 strsz
, b1
, sizeof(b1
)));
391 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
393 if (tb
[TCA_ACT_FLAGS
]) {
394 struct nla_bitfield32
*flags
= RTA_DATA(tb
[TCA_ACT_FLAGS
]);
396 if (flags
->selector
& TCA_ACT_FLAGS_NO_PERCPU_STATS
)
397 print_bool(PRINT_ANY
, "no_percpu", "\tno_percpu",
399 TCA_ACT_FLAGS_NO_PERCPU_STATS
);
400 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
402 if (tb
[TCA_ACT_HW_STATS
])
403 print_hw_stats(tb
[TCA_ACT_HW_STATS
], false);
405 if (tb
[TCA_ACT_USED_HW_STATS
])
406 print_hw_stats(tb
[TCA_ACT_USED_HW_STATS
], true);
412 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
415 struct rtattr
*tb
[TCA_MAX
+ 1];
417 struct action_util
*a
= NULL
;
418 __u32
*delete_count
= 0;
420 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
422 if (tb
[TCA_KIND
] == NULL
) {
423 fprintf(stderr
, "NULL Action!\n");
427 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
431 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
432 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
438 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
447 tot_acts
= TCA_ACT_MAX_PRIO
;
449 struct rtattr
*tb
[tot_acts
+ 1];
451 parse_rtattr_nested(tb
, tot_acts
, arg
);
453 if (tab_flush
&& tb
[0] && !tb
[1])
454 return tc_print_action_flush(f
, tb
[0]);
456 open_json_array(PRINT_JSON
, "actions");
457 for (i
= 0; i
<= tot_acts
; i
++) {
459 open_json_object(NULL
);
460 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
461 print_uint(PRINT_ANY
, "order",
462 "\taction order %u: ", i
);
463 if (tc_print_one_action(f
, tb
[i
]) < 0) {
464 print_string(PRINT_FP
, NULL
,
465 "Error printing action\n", NULL
);
471 close_json_array(PRINT_JSON
, NULL
);
476 int print_action(struct nlmsghdr
*n
, void *arg
)
478 FILE *fp
= (FILE *)arg
;
479 struct tcamsg
*t
= NLMSG_DATA(n
);
480 int len
= n
->nlmsg_len
;
481 __u32
*tot_acts
= NULL
;
482 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
484 len
-= NLMSG_LENGTH(sizeof(*t
));
487 fprintf(stderr
, "Wrong len %d\n", len
);
491 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
493 if (tb
[TCA_ROOT_COUNT
])
494 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
496 open_json_object(NULL
);
497 print_uint(PRINT_ANY
, "total acts", "total acts %u",
498 tot_acts
? *tot_acts
: 0);
499 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
501 if (tb
[TCA_ACT_TAB
] == NULL
) {
502 if (n
->nlmsg_type
!= RTM_GETACTION
)
503 fprintf(stderr
, "print_action: NULL kind\n");
507 if (n
->nlmsg_type
== RTM_DELACTION
) {
508 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
509 fprintf(fp
, "Flushed table ");
512 fprintf(fp
, "Deleted action ");
516 if (n
->nlmsg_type
== RTM_NEWACTION
) {
517 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
518 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
519 fprintf(fp
, "Added action ");
520 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
521 fprintf(fp
, "Replaced action ");
525 open_json_object(NULL
);
526 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
532 static int tc_action_gd(int cmd
, unsigned int flags
,
533 int *argc_p
, char ***argv_p
)
535 char k
[FILTER_NAMESZ
];
536 struct action_util
*a
= NULL
;
538 char **argv
= *argv_p
;
543 struct rtattr
*tail2
;
544 struct nlmsghdr
*ans
= NULL
;
551 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
552 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
554 .t
.tca_family
= AF_UNSPEC
,
561 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
564 if (strcmp(*argv
, "action") == 0) {
568 } else if (strcmp(*argv
, "help") == 0) {
572 strncpy(k
, *argv
, sizeof(k
) - 1);
573 a
= get_action_kind(k
);
575 fprintf(stderr
, "Error: non existent action: %s\n", k
);
579 if (strcmp(a
->id
, k
) != 0) {
580 fprintf(stderr
, "Error: non existent action: %s\n", k
);
589 "Error: no index specified action: %s\n", k
);
594 if (matches(*argv
, "index") == 0) {
596 if (get_u32(&i
, *argv
, 10)) {
597 fprintf(stderr
, "Illegal \"index\"\n");
605 "Error: no index specified action: %s\n", k
);
610 tail2
= addattr_nest(&req
.n
, MAX_MSG
, ++prio
);
611 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
613 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
614 addattr_nest_end(&req
.n
, tail2
);
618 addattr_nest_end(&req
.n
, tail
);
620 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
622 if (rtnl_talk(&rth
, &req
.n
, cmd
== RTM_DELACTION
? NULL
: &ans
) < 0) {
623 fprintf(stderr
, "We have an error talking to the kernel\n");
627 if (cmd
== RTM_GETACTION
) {
629 ret
= print_action(ans
, stdout
);
631 fprintf(stderr
, "Dump terminated\n");
646 static int tc_action_modify(int cmd
, unsigned int flags
,
647 int *argc_p
, char ***argv_p
)
650 char **argv
= *argv_p
;
657 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
658 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
660 .t
.tca_family
= AF_UNSPEC
,
662 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
666 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
667 fprintf(stderr
, "Illegal \"action\"\n");
670 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
672 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
673 fprintf(stderr
, "We have an error talking to the kernel\n");
683 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
685 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
686 int ret
= 0, prio
= 0, msg_size
= 0;
687 struct action_util
*a
= NULL
;
688 struct nla_bitfield32 flag_select
= { 0 };
689 char **argv
= *argv_p
;
690 __u32 msec_since
= 0;
692 char k
[FILTER_NAMESZ
];
698 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
699 .t
.tca_family
= AF_UNSPEC
,
702 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
703 tail2
= NLMSG_TAIL(&req
.n
);
705 strncpy(k
, *argv
, sizeof(k
) - 1);
708 get_action_kind("gact");
711 a
= get_action_kind(k
);
713 fprintf(stderr
, "bad action %s\n", k
);
716 if (strcmp(a
->id
, k
) != 0) {
717 fprintf(stderr
, "bad action %s\n", k
);
720 strncpy(k
, *argv
, sizeof(k
) - 1);
725 if (argc
&& (strcmp(*argv
, "since") == 0)) {
727 if (get_u32(&msec_since
, *argv
, 0))
728 invarg("dump time \"since\" is invalid", *argv
);
731 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
732 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
733 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
734 addattr_nest_end(&req
.n
, tail
);
736 tail3
= NLMSG_TAIL(&req
.n
);
737 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
738 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
739 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
740 sizeof(struct nla_bitfield32
));
741 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
743 tail4
= NLMSG_TAIL(&req
.n
);
744 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
745 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
747 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
)
748 - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
750 if (event
== RTM_GETACTION
) {
751 if (rtnl_dump_request(&rth
, event
,
752 (void *)&req
.t
, msg_size
) < 0) {
753 perror("Cannot send dump request");
757 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
761 if (event
== RTM_DELACTION
) {
762 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
763 req
.n
.nlmsg_type
= RTM_DELACTION
;
764 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
765 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
766 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
767 fprintf(stderr
, "We have an error flushing\n");
780 int do_action(int argc
, char **argv
)
787 if (matches(*argv
, "add") == 0) {
788 ret
= tc_action_modify(RTM_NEWACTION
,
789 NLM_F_EXCL
| NLM_F_CREATE
,
791 } else if (matches(*argv
, "change") == 0 ||
792 matches(*argv
, "replace") == 0) {
793 ret
= tc_action_modify(RTM_NEWACTION
,
794 NLM_F_CREATE
| NLM_F_REPLACE
,
796 } else if (matches(*argv
, "delete") == 0) {
799 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
800 } else if (matches(*argv
, "get") == 0) {
803 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
804 } else if (matches(*argv
, "list") == 0 ||
805 matches(*argv
, "show") == 0 ||
806 matches(*argv
, "lst") == 0) {
814 return tc_act_list_or_flush(&argc
, &argv
,
816 } else if (matches(*argv
, "flush") == 0) {
824 return tc_act_list_or_flush(&argc
, &argv
,
826 } else if (matches(*argv
, "help") == 0) {
831 "Command \"%s\" is unknown, try \"tc actions help\".\n",