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
);
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
);
295 hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
296 invarg("cookie must be a hex string\n",
299 act_ck_len
= slen
/ 2;
305 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
306 &act_ck
, act_ck_len
);
308 if (*argv
&& matches(*argv
, "hw_stats") == 0) {
310 ret
= parse_hw_stats(*argv
, n
);
312 invarg("value is invalid\n", *argv
);
316 if (*argv
&& strcmp(*argv
, "no_percpu") == 0) {
317 struct nla_bitfield32 flags
=
318 { TCA_ACT_FLAGS_NO_PERCPU_STATS
,
319 TCA_ACT_FLAGS_NO_PERCPU_STATS
};
321 addattr_l(n
, MAX_MSG
, TCA_ACT_FLAGS
, &flags
,
322 sizeof(struct nla_bitfield32
));
326 addattr_nest_end(n
, tail
);
332 fprintf(stderr
, "bad action empty %d\n", eap
);
336 addattr_nest_end(n
, tail2
);
343 /* no need to undo things, returning from here should
346 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
350 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
353 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
355 struct action_util
*a
= NULL
;
360 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
362 if (tb
[TCA_ACT_KIND
] == NULL
) {
363 fprintf(stderr
, "NULL Action!\n");
368 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
372 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
377 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
378 print_string(PRINT_FP
, NULL
, "\tAction statistics:", NULL
);
380 open_json_object("stats");
381 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
385 if (tb
[TCA_ACT_COOKIE
]) {
386 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
387 char b1
[strsz
* 2 + 1];
389 print_string(PRINT_ANY
, "cookie", "\tcookie %s",
390 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
391 strsz
, b1
, sizeof(b1
)));
394 if (tb
[TCA_ACT_FLAGS
]) {
395 struct nla_bitfield32
*flags
= RTA_DATA(tb
[TCA_ACT_FLAGS
]);
397 if (flags
->selector
& TCA_ACT_FLAGS_NO_PERCPU_STATS
)
398 print_bool(PRINT_ANY
, "no_percpu", "\tno_percpu",
400 TCA_ACT_FLAGS_NO_PERCPU_STATS
);
403 if (tb
[TCA_ACT_HW_STATS
])
404 print_hw_stats(tb
[TCA_ACT_HW_STATS
], false);
406 if (tb
[TCA_ACT_USED_HW_STATS
])
407 print_hw_stats(tb
[TCA_ACT_USED_HW_STATS
], true);
413 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
416 struct rtattr
*tb
[TCA_MAX
+ 1];
418 struct action_util
*a
= NULL
;
419 __u32
*delete_count
= 0;
421 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
423 if (tb
[TCA_KIND
] == NULL
) {
424 fprintf(stderr
, "NULL Action!\n");
428 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
432 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
433 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
439 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
448 tot_acts
= TCA_ACT_MAX_PRIO
;
450 struct rtattr
*tb
[tot_acts
+ 1];
452 parse_rtattr_nested(tb
, tot_acts
, arg
);
454 if (tab_flush
&& tb
[0] && !tb
[1])
455 return tc_print_action_flush(f
, tb
[0]);
457 open_json_array(PRINT_JSON
, "actions");
458 for (i
= 0; i
<= tot_acts
; i
++) {
460 open_json_object(NULL
);
462 print_uint(PRINT_ANY
, "order",
463 "\taction order %u: ", i
);
464 if (tc_print_one_action(f
, tb
[i
]) < 0) {
465 print_string(PRINT_FP
, NULL
,
466 "Error printing action\n", NULL
);
472 close_json_array(PRINT_JSON
, NULL
);
477 int print_action(struct nlmsghdr
*n
, void *arg
)
479 FILE *fp
= (FILE *)arg
;
480 struct tcamsg
*t
= NLMSG_DATA(n
);
481 int len
= n
->nlmsg_len
;
482 __u32
*tot_acts
= NULL
;
483 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
485 len
-= NLMSG_LENGTH(sizeof(*t
));
488 fprintf(stderr
, "Wrong len %d\n", len
);
492 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
494 if (tb
[TCA_ROOT_COUNT
])
495 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
497 open_json_object(NULL
);
498 print_uint(PRINT_ANY
, "total acts", "total acts %u",
499 tot_acts
? *tot_acts
: 0);
502 if (tb
[TCA_ACT_TAB
] == NULL
) {
503 if (n
->nlmsg_type
!= RTM_GETACTION
)
504 fprintf(stderr
, "print_action: NULL kind\n");
508 if (n
->nlmsg_type
== RTM_DELACTION
) {
509 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
510 fprintf(fp
, "Flushed table ");
513 fprintf(fp
, "Deleted action ");
517 if (n
->nlmsg_type
== RTM_NEWACTION
) {
518 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
519 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
520 fprintf(fp
, "Added action ");
521 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
522 fprintf(fp
, "Replaced action ");
526 open_json_object(NULL
);
527 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
533 static int tc_action_gd(int cmd
, unsigned int flags
,
534 int *argc_p
, char ***argv_p
)
536 char k
[FILTER_NAMESZ
];
537 struct action_util
*a
= NULL
;
539 char **argv
= *argv_p
;
544 struct rtattr
*tail2
;
545 struct nlmsghdr
*ans
= NULL
;
552 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
553 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
555 .t
.tca_family
= AF_UNSPEC
,
562 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
565 if (strcmp(*argv
, "action") == 0) {
569 } else if (strcmp(*argv
, "help") == 0) {
573 strncpy(k
, *argv
, sizeof(k
) - 1);
574 a
= get_action_kind(k
);
576 fprintf(stderr
, "Error: non existent action: %s\n", k
);
580 if (strcmp(a
->id
, k
) != 0) {
581 fprintf(stderr
, "Error: non existent action: %s\n", k
);
590 "Error: no index specified action: %s\n", k
);
595 if (matches(*argv
, "index") == 0) {
597 if (get_u32(&i
, *argv
, 10)) {
598 fprintf(stderr
, "Illegal \"index\"\n");
606 "Error: no index specified action: %s\n", k
);
611 tail2
= addattr_nest(&req
.n
, MAX_MSG
, ++prio
);
612 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
614 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
615 addattr_nest_end(&req
.n
, tail2
);
619 addattr_nest_end(&req
.n
, tail
);
621 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
623 if (rtnl_talk(&rth
, &req
.n
, cmd
== RTM_DELACTION
? NULL
: &ans
) < 0) {
624 fprintf(stderr
, "We have an error talking to the kernel\n");
628 if (cmd
== RTM_GETACTION
) {
630 ret
= print_action(ans
, stdout
);
632 fprintf(stderr
, "Dump terminated\n");
647 static int tc_action_modify(int cmd
, unsigned int flags
,
648 int *argc_p
, char ***argv_p
)
651 char **argv
= *argv_p
;
658 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
659 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
661 .t
.tca_family
= AF_UNSPEC
,
663 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
667 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
668 fprintf(stderr
, "Illegal \"action\"\n");
671 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
673 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
674 fprintf(stderr
, "We have an error talking to the kernel\n");
684 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
686 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
687 int ret
= 0, prio
= 0, msg_size
= 0;
688 struct action_util
*a
= NULL
;
689 struct nla_bitfield32 flag_select
= { 0 };
690 char **argv
= *argv_p
;
691 __u32 msec_since
= 0;
693 char k
[FILTER_NAMESZ
];
699 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
700 .t
.tca_family
= AF_UNSPEC
,
703 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
704 tail2
= NLMSG_TAIL(&req
.n
);
706 strncpy(k
, *argv
, sizeof(k
) - 1);
709 get_action_kind("gact");
712 a
= get_action_kind(k
);
714 fprintf(stderr
, "bad action %s\n", k
);
717 if (strcmp(a
->id
, k
) != 0) {
718 fprintf(stderr
, "bad action %s\n", k
);
721 strncpy(k
, *argv
, sizeof(k
) - 1);
726 if (argc
&& (strcmp(*argv
, "since") == 0)) {
728 if (get_u32(&msec_since
, *argv
, 0))
729 invarg("dump time \"since\" is invalid", *argv
);
732 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
733 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
734 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
735 addattr_nest_end(&req
.n
, tail
);
737 tail3
= NLMSG_TAIL(&req
.n
);
738 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
739 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
740 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
741 sizeof(struct nla_bitfield32
));
742 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
744 tail4
= NLMSG_TAIL(&req
.n
);
745 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
746 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
748 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
)
749 - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
751 if (event
== RTM_GETACTION
) {
752 if (rtnl_dump_request(&rth
, event
,
753 (void *)&req
.t
, msg_size
) < 0) {
754 perror("Cannot send dump request");
758 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
762 if (event
== RTM_DELACTION
) {
763 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
764 req
.n
.nlmsg_type
= RTM_DELACTION
;
765 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
766 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
767 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
768 fprintf(stderr
, "We have an error flushing\n");
781 int do_action(int argc
, char **argv
)
788 if (matches(*argv
, "add") == 0) {
789 ret
= tc_action_modify(RTM_NEWACTION
,
790 NLM_F_EXCL
| NLM_F_CREATE
,
792 } else if (matches(*argv
, "change") == 0 ||
793 matches(*argv
, "replace") == 0) {
794 ret
= tc_action_modify(RTM_NEWACTION
,
795 NLM_F_CREATE
| NLM_F_REPLACE
,
797 } else if (matches(*argv
, "delete") == 0) {
800 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
801 } else if (matches(*argv
, "get") == 0) {
804 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
805 } else if (matches(*argv
, "list") == 0 ||
806 matches(*argv
, "show") == 0 ||
807 matches(*argv
, "lst") == 0) {
815 return tc_act_list_or_flush(&argc
, &argv
,
817 } else if (matches(*argv
, "flush") == 0) {
825 return tc_act_list_or_flush(&argc
, &argv
,
827 } else if (matches(*argv
, "help") == 0) {
832 "Command \"%s\" is unknown, try \"tc actions help\".\n",