]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
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 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 ..
46 fprintf(stderr
, "usage: tc actions <ACTSPECOP>*\n");
48 "Where: \tACTSPECOP := ACR | GD | FL\n"
49 "\tACR := add | change | replace <ACTSPEC>*\n"
50 "\tGD := get | delete | <ACTISPEC>*\n"
51 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
52 "\tACTNAMESPEC := action <ACTNAME>\n"
53 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
54 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
55 "\tINDEXSPEC := index <32 bit indexvalue>\n"
56 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
57 "\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
58 "\t\tEach 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
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
75 char **argv
= *argv_p
;
78 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
80 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
85 static struct action_util
*get_action_kind(char *str
)
90 struct action_util
*a
;
95 for (a
= action_list
; a
; a
= a
->next
) {
96 if (strcmp(a
->id
, str
) == 0)
100 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
101 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
105 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
111 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
117 a
->next
= action_list
;
129 a
= calloc(1, sizeof(*a
));
131 strncpy(a
->id
, "noact", 15);
132 a
->parse_aopt
= parse_noaopt
;
133 a
->print_aopt
= print_noaopt
;
142 if ((matches(*argv
, "change") == 0) ||
143 (matches(*argv
, "replace") == 0) ||
144 (matches(*argv
, "delete") == 0) ||
145 (matches(*argv
, "get") == 0) ||
146 (matches(*argv
, "add") == 0))
154 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
157 char **argv
= *argv_p
;
158 struct rtattr
*tail
, *tail2
;
161 int eap
= 0; /* expect action parameters */
169 tail
= tail2
= NLMSG_TAIL(n
);
171 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
175 memset(k
, 0, sizeof(k
));
177 if (strcmp(*argv
, "action") == 0) {
183 get_action_kind("gact");
187 } else if (strcmp(*argv
, "flowid") == 0) {
189 } else if (strcmp(*argv
, "classid") == 0) {
191 } else if (strcmp(*argv
, "help") == 0) {
193 } else if (new_cmd(argv
)) {
196 struct action_util
*a
= NULL
;
198 strncpy(k
, *argv
, sizeof(k
) - 1);
201 a
= get_action_kind(k
);
214 tail
= NLMSG_TAIL(n
);
215 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
216 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
218 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
, n
);
221 fprintf(stderr
, "bad action parsing\n");
224 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
231 fprintf(stderr
, "bad action empty %d\n", eap
);
235 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
242 /* no need to undo things, returning from here should
243 * cause enough pain */
244 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
249 tc_print_one_action(FILE *f
, struct rtattr
*arg
)
252 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
254 struct action_util
*a
= NULL
;
259 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
261 if (tb
[TCA_ACT_KIND
] == NULL
) {
262 fprintf(stderr
, "NULL Action!\n");
267 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
271 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
276 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
277 fprintf(f
, "\tAction statistics:\n");
278 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
286 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
289 struct rtattr
*tb
[TCA_MAX
+ 1];
291 struct action_util
*a
= NULL
;
292 __u32
*delete_count
= 0;
294 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
296 if (tb
[TCA_KIND
] == NULL
) {
297 fprintf(stderr
, "NULL Action!\n");
301 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
305 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
306 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
312 tc_print_action(FILE *f
, const struct rtattr
*arg
)
316 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
321 parse_rtattr_nested(tb
, TCA_ACT_MAX_PRIO
, arg
);
323 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
324 return tc_print_action_flush(f
, tb
[0]);
326 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
328 fprintf(f
, "\n\taction order %d: ", i
);
329 if (tc_print_one_action(f
, tb
[i
]) < 0) {
330 fprintf(f
, "Error printing action\n");
339 int print_action(const struct sockaddr_nl
*who
,
343 FILE *fp
= (FILE *)arg
;
344 struct tcamsg
*t
= NLMSG_DATA(n
);
345 int len
= n
->nlmsg_len
;
346 struct rtattr
*tb
[TCAA_MAX
+1];
348 len
-= NLMSG_LENGTH(sizeof(*t
));
351 fprintf(stderr
, "Wrong len %d\n", len
);
355 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
357 if (tb
[TCA_ACT_TAB
] == NULL
) {
358 if (n
->nlmsg_type
!= RTM_GETACTION
)
359 fprintf(stderr
, "print_action: NULL kind\n");
363 if (n
->nlmsg_type
== RTM_DELACTION
) {
364 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
365 fprintf(fp
, "Flushed table ");
368 fprintf(fp
, "deleted action ");
372 if (n
->nlmsg_type
== RTM_NEWACTION
)
373 fprintf(fp
, "Added action ");
374 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
379 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
382 struct action_util
*a
= NULL
;
384 char **argv
= *argv_p
;
389 struct rtattr
*tail2
;
390 struct nlmsghdr
*ans
= NULL
;
397 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
398 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
400 .t
.tca_family
= AF_UNSPEC
,
407 tail
= NLMSG_TAIL(&req
.n
);
408 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
411 if (strcmp(*argv
, "action") == 0) {
415 } else if (strcmp(*argv
, "help") == 0) {
419 strncpy(k
, *argv
, sizeof(k
) - 1);
420 a
= get_action_kind(k
);
422 fprintf(stderr
, "Error: non existent action: %s\n", k
);
426 if (strcmp(a
->id
, k
) != 0) {
427 fprintf(stderr
, "Error: non existent action: %s\n", k
);
435 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
440 if (matches(*argv
, "index") == 0) {
442 if (get_u32(&i
, *argv
, 10)) {
443 fprintf(stderr
, "Illegal \"index\"\n");
450 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
455 tail2
= NLMSG_TAIL(&req
.n
);
456 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
457 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
458 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
459 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
463 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
465 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
466 if (cmd
== RTM_GETACTION
)
469 if (rtnl_talk(&rth
, &req
.n
, ans
, MAX_MSG
) < 0) {
470 fprintf(stderr
, "We have an error talking to the kernel\n");
474 if (ans
&& print_action(NULL
, &req
.n
, (void *)stdout
) < 0) {
475 fprintf(stderr
, "Dump terminated\n");
485 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
488 char **argv
= *argv_p
;
495 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
496 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
498 .t
.tca_family
= AF_UNSPEC
,
500 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
504 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
505 fprintf(stderr
, "Illegal \"action\"\n");
508 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
510 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
511 fprintf(stderr
, "We have an error talking to the kernel\n");
521 static int tc_act_list_or_flush(int argc
, char **argv
, int event
)
523 int ret
= 0, prio
= 0, msg_size
= 0;
525 struct rtattr
*tail
, *tail2
;
526 struct action_util
*a
= NULL
;
532 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
533 .t
.tca_family
= AF_UNSPEC
,
536 tail
= NLMSG_TAIL(&req
.n
);
537 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
538 tail2
= NLMSG_TAIL(&req
.n
);
540 strncpy(k
, *argv
, sizeof(k
) - 1);
543 get_action_kind("gact");
546 a
= get_action_kind(k
);
548 fprintf(stderr
, "bad action %s\n", k
);
551 if (strcmp(a
->id
, k
) != 0) {
552 fprintf(stderr
, "bad action %s\n", k
);
555 strncpy(k
, *argv
, sizeof(k
) - 1);
557 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
558 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
559 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
560 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
562 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
564 if (event
== RTM_GETACTION
) {
565 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
566 perror("Cannot send dump request");
569 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
572 if (event
== RTM_DELACTION
) {
573 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
574 req
.n
.nlmsg_type
= RTM_DELACTION
;
575 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
576 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
577 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
578 fprintf(stderr
, "We have an error flushing\n");
589 int do_action(int argc
, char **argv
)
596 if (matches(*argv
, "add") == 0) {
597 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
598 } else if (matches(*argv
, "change") == 0 ||
599 matches(*argv
, "replace") == 0) {
600 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
601 } else if (matches(*argv
, "delete") == 0) {
604 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
605 } else if (matches(*argv
, "get") == 0) {
608 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
609 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
610 || matches(*argv
, "lst") == 0) {
615 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
616 } else if (matches(*argv
, "flush") == 0) {
621 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
622 } else if (matches(*argv
, "help") == 0) {
631 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);