]>
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
= malloc(sizeof(*a
));
131 memset(a
, 0, sizeof(*a
));
132 strncpy(a
->id
, "noact", 15);
133 a
->parse_aopt
= parse_noaopt
;
134 a
->print_aopt
= print_noaopt
;
143 if ((matches(*argv
, "change") == 0) ||
144 (matches(*argv
, "replace") == 0) ||
145 (matches(*argv
, "delete") == 0) ||
146 (matches(*argv
, "get") == 0) ||
147 (matches(*argv
, "add") == 0))
155 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
158 char **argv
= *argv_p
;
159 struct rtattr
*tail
, *tail2
;
162 int eap
= 0; /* expect action parameters */
170 tail
= tail2
= NLMSG_TAIL(n
);
172 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
176 memset(k
, 0, sizeof(k
));
178 if (strcmp(*argv
, "action") == 0) {
184 get_action_kind("gact");
188 } else if (strcmp(*argv
, "flowid") == 0) {
190 } else if (strcmp(*argv
, "classid") == 0) {
192 } else if (strcmp(*argv
, "help") == 0) {
194 } else if (new_cmd(argv
)) {
197 struct action_util
*a
= NULL
;
199 strncpy(k
, *argv
, sizeof(k
) - 1);
202 a
= get_action_kind(k
);
215 tail
= NLMSG_TAIL(n
);
216 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
217 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
219 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
, n
);
222 fprintf(stderr
, "bad action parsing\n");
225 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
232 fprintf(stderr
, "bad action empty %d\n", eap
);
236 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
243 /* no need to undo things, returning from here should
244 * cause enough pain */
245 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
250 tc_print_one_action(FILE *f
, struct rtattr
*arg
)
253 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
255 struct action_util
*a
= NULL
;
260 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
262 if (tb
[TCA_ACT_KIND
] == NULL
) {
263 fprintf(stderr
, "NULL Action!\n");
268 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
272 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
277 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
278 fprintf(f
, "\tAction statistics:\n");
279 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
287 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
290 struct rtattr
*tb
[TCA_MAX
+ 1];
292 struct action_util
*a
= NULL
;
293 __u32
*delete_count
= 0;
295 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
297 if (tb
[TCA_KIND
] == NULL
) {
298 fprintf(stderr
, "NULL Action!\n");
302 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
306 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
307 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
313 tc_print_action(FILE *f
, const struct rtattr
*arg
)
317 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
322 parse_rtattr_nested(tb
, TCA_ACT_MAX_PRIO
, arg
);
324 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
325 return tc_print_action_flush(f
, tb
[0]);
327 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
329 fprintf(f
, "\n\taction order %d: ", i
);
330 if (tc_print_one_action(f
, tb
[i
]) < 0) {
331 fprintf(f
, "Error printing action\n");
340 int print_action(const struct sockaddr_nl
*who
,
344 FILE *fp
= (FILE *)arg
;
345 struct tcamsg
*t
= NLMSG_DATA(n
);
346 int len
= n
->nlmsg_len
;
347 struct rtattr
*tb
[TCAA_MAX
+1];
349 len
-= NLMSG_LENGTH(sizeof(*t
));
352 fprintf(stderr
, "Wrong len %d\n", len
);
356 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
358 if (tb
[TCA_ACT_TAB
] == NULL
) {
359 if (n
->nlmsg_type
!= RTM_GETACTION
)
360 fprintf(stderr
, "print_action: NULL kind\n");
364 if (n
->nlmsg_type
== RTM_DELACTION
) {
365 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
366 fprintf(fp
, "Flushed table ");
369 fprintf(fp
, "deleted action ");
373 if (n
->nlmsg_type
== RTM_NEWACTION
)
374 fprintf(fp
, "Added action ");
375 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
380 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
383 struct action_util
*a
= NULL
;
385 char **argv
= *argv_p
;
389 struct sockaddr_nl nladdr
;
391 struct rtattr
*tail2
;
392 struct nlmsghdr
*ans
= NULL
;
400 .nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
401 .nlmsg_flags
= NLM_F_REQUEST
| flags
,
404 .t
.tca_family
= AF_UNSPEC
,
408 memset(&nladdr
, 0, sizeof(nladdr
));
409 nladdr
.nl_family
= AF_NETLINK
;
415 tail
= NLMSG_TAIL(&req
.n
);
416 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
419 if (strcmp(*argv
, "action") == 0) {
423 } else if (strcmp(*argv
, "help") == 0) {
427 strncpy(k
, *argv
, sizeof(k
) - 1);
428 a
= get_action_kind(k
);
430 fprintf(stderr
, "Error: non existent action: %s\n", k
);
434 if (strcmp(a
->id
, k
) != 0) {
435 fprintf(stderr
, "Error: non existent action: %s\n", k
);
443 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
448 if (matches(*argv
, "index") == 0) {
450 if (get_u32(&i
, *argv
, 10)) {
451 fprintf(stderr
, "Illegal \"index\"\n");
458 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
463 tail2
= NLMSG_TAIL(&req
.n
);
464 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
465 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
466 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
467 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
471 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
473 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
474 if (cmd
== RTM_GETACTION
)
477 if (rtnl_talk(&rth
, &req
.n
, ans
, MAX_MSG
) < 0) {
478 fprintf(stderr
, "We have an error talking to the kernel\n");
482 if (ans
&& print_action(NULL
, &req
.n
, (void *)stdout
) < 0) {
483 fprintf(stderr
, "Dump terminated\n");
493 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
496 char **argv
= *argv_p
;
506 .nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
507 .nlmsg_flags
= NLM_F_REQUEST
| flags
,
510 .t
.tca_family
= AF_UNSPEC
,
514 tail
= NLMSG_TAIL(&req
.n
);
517 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
518 fprintf(stderr
, "Illegal \"action\"\n");
521 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
523 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
524 fprintf(stderr
, "We have an error talking to the kernel\n");
534 static int tc_act_list_or_flush(int argc
, char **argv
, int event
)
536 int ret
= 0, prio
= 0, msg_size
= 0;
538 struct rtattr
*tail
, *tail2
;
539 struct action_util
*a
= NULL
;
545 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
546 .t
.tca_family
= AF_UNSPEC
,
550 tail
= NLMSG_TAIL(&req
.n
);
551 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
552 tail2
= NLMSG_TAIL(&req
.n
);
554 strncpy(k
, *argv
, sizeof(k
) - 1);
557 get_action_kind("gact");
560 a
= get_action_kind(k
);
562 fprintf(stderr
, "bad action %s\n", k
);
565 if (strcmp(a
->id
, k
) != 0) {
566 fprintf(stderr
, "bad action %s\n", k
);
569 strncpy(k
, *argv
, sizeof(k
) - 1);
571 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
572 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
573 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
574 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
576 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
578 if (event
== RTM_GETACTION
) {
579 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
580 perror("Cannot send dump request");
583 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
586 if (event
== RTM_DELACTION
) {
587 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
588 req
.n
.nlmsg_type
= RTM_DELACTION
;
589 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
590 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
591 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
592 fprintf(stderr
, "We have an error flushing\n");
603 int do_action(int argc
, char **argv
)
610 if (matches(*argv
, "add") == 0) {
611 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
612 } else if (matches(*argv
, "change") == 0 ||
613 matches(*argv
, "replace") == 0) {
614 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
615 } else if (matches(*argv
, "delete") == 0) {
618 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
619 } else if (matches(*argv
, "get") == 0) {
622 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
623 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
624 || matches(*argv
, "lst") == 0) {
629 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
630 } else if (matches(*argv
, "flush") == 0) {
635 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
636 } else if (matches(*argv
, "help") == 0) {
645 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);