]>
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
= 0 ; //fuckin backward compatibility
40 /*XXX: In the near future add a action->print_help to improve
42 * This would mean new tc will not be backward compatible
43 * with any action .so from the old days. But if someone really
44 * does that, they would know how to fix this ..
47 fprintf (stderr
, "usage: tc actions <ACTSPECOP>*\n");
49 "Where: \tACTSPECOP := ACR | GD | FL\n"
50 "\tACR := add | change | replace <ACTSPEC>* \n"
51 "\tGD := get | delete | <ACTISPEC>*\n"
52 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
53 "\tACTNAMESPEC := action <ACTNAME>\n"
54 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
55 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
56 "\tINDEXSPEC := index <32 bit indexvalue>\n"
57 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
58 "\t\tExample ACTNAME is gact, mirred etc\n"
59 "\t\tEach action has its own parameters (ACTPARAMS)\n"
63 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
65 if (opt
&& RTA_PAYLOAD(opt
))
66 fprintf(f
, "[Unknown action, optlen=%u] ",
67 (unsigned) RTA_PAYLOAD(opt
));
71 static int parse_noaopt(struct action_util
*au
, int *argc_p
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
74 char **argv
= *argv_p
;
77 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
79 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
84 struct action_util
*get_action_kind(char *str
)
89 struct action_util
*a
;
94 for (a
= action_list
; a
; a
= a
->next
) {
95 if (strcmp(a
->id
, str
) == 0)
99 snprintf(buf
, sizeof(buf
), "m_%s.so", str
);
100 dlh
= dlopen(buf
, RTLD_LAZY
);
104 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
110 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
116 a
->next
= action_list
;
128 a
= malloc(sizeof(*a
));
130 memset(a
, 0, 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
, "add") == 0))
153 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
156 char **argv
= *argv_p
;
157 struct rtattr
*tail
, *tail2
;
160 int eap
= 0; /* expect action parameters */
168 tail
= tail2
= NLMSG_TAIL(n
);
170 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
174 memset(k
, 0, sizeof (k
));
176 if (strcmp(*argv
, "action") == 0 ) {
182 get_action_kind("gact");
186 } else if (strcmp(*argv
, "help") == 0) {
188 } else if (new_cmd(argv
)) {
191 struct action_util
*a
= NULL
;
192 strncpy(k
, *argv
, sizeof (k
) - 1);
195 a
= get_action_kind(k
);
208 tail
= NLMSG_TAIL(n
);
209 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
210 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
212 ret
= a
->parse_aopt(a
,&argc
, &argv
, TCA_ACT_OPTIONS
, n
);
215 fprintf(stderr
,"bad action parsing\n");
218 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
225 fprintf(stderr
,"bad action empty %d\n",eap
);
229 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
236 /* no need to undo things, returning from here should
237 * cause enough pain */
238 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n",argc
,*argv
);
243 tc_print_one_action(FILE * f
, struct rtattr
*arg
)
246 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
248 struct action_util
*a
= NULL
;
253 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
254 if (tb
[TCA_ACT_KIND
] == NULL
) {
255 fprintf(stderr
, "NULL Action!\n");
260 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
265 fprintf(f
," %s \n", a
->id
);
270 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(FILE * f
, const struct rtattr
*arg
)
290 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
295 parse_rtattr_nested(tb
, TCA_ACT_MAX_PRIO
, arg
);
297 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1]) {
298 int ret
= tc_print_one_action(f
, tb
[0]);
302 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
304 fprintf(f
, "\n\taction order %d: ", i
+ batch_c
);
305 if (0 > tc_print_one_action(f
, tb
[i
])) {
306 fprintf(f
, "Error printing action\n");
312 batch_c
+=TCA_ACT_MAX_PRIO
;
316 int print_action(const struct sockaddr_nl
*who
,
320 FILE *fp
= (FILE*)arg
;
321 struct tcamsg
*t
= NLMSG_DATA(n
);
322 int len
= n
->nlmsg_len
;
323 struct rtattr
* tb
[TCAA_MAX
+1];
325 len
-= NLMSG_LENGTH(sizeof(*t
));
328 fprintf(stderr
, "Wrong len %d\n", len
);
332 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
334 if (NULL
== tb
[TCA_ACT_TAB
]) {
335 if (n
->nlmsg_type
!= RTM_GETACTION
)
336 fprintf(stderr
, "print_action: NULL kind\n");
340 if (n
->nlmsg_type
== RTM_DELACTION
) {
341 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
342 fprintf(fp
, "Flushed table ");
345 fprintf(fp
, "deleted action ");
349 if (n
->nlmsg_type
== RTM_NEWACTION
)
350 fprintf(fp
, "Added action ");
351 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
356 int tc_action_gd(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
359 struct action_util
*a
= NULL
;
361 char **argv
= *argv_p
;
365 struct sockaddr_nl nladdr
;
367 struct rtattr
*tail2
;
368 struct nlmsghdr
*ans
= NULL
;
376 req
.t
.tca_family
= AF_UNSPEC
;
378 memset(&req
, 0, sizeof(req
));
380 memset(&nladdr
, 0, sizeof(nladdr
));
381 nladdr
.nl_family
= AF_NETLINK
;
383 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
384 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
385 req
.n
.nlmsg_type
= cmd
;
390 tail
= NLMSG_TAIL(&req
.n
);
391 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
394 if (strcmp(*argv
, "action") == 0 ) {
398 } else if (strcmp(*argv
, "help") == 0) {
402 strncpy(k
, *argv
, sizeof (k
) - 1);
403 a
= get_action_kind(k
);
405 fprintf(stderr
, "Error: non existent action: %s\n",k
);
409 if (strcmp(a
->id
, k
) != 0) {
410 fprintf(stderr
, "Error: non existent action: %s\n",k
);
418 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
423 if (matches(*argv
, "index") == 0) {
425 if (get_u32(&i
, *argv
, 10)) {
426 fprintf(stderr
, "Illegal \"index\"\n");
433 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
438 tail2
= NLMSG_TAIL(&req
.n
);
439 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
440 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
441 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
442 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
446 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
448 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
449 if (cmd
== RTM_GETACTION
)
452 if (rtnl_talk(&rth
, &req
.n
, 0, 0, ans
, NULL
, NULL
) < 0) {
453 fprintf(stderr
, "We have an error talking to the kernel\n");
457 if (ans
&& print_action(NULL
, &req
.n
, (void*)stdout
) < 0) {
458 fprintf(stderr
, "Dump terminated\n");
468 int tc_action_modify(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
471 char **argv
= *argv_p
;
481 req
.t
.tca_family
= AF_UNSPEC
;
483 memset(&req
, 0, sizeof(req
));
485 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
486 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
487 req
.n
.nlmsg_type
= cmd
;
488 tail
= NLMSG_TAIL(&req
.n
);
491 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
492 fprintf(stderr
, "Illegal \"action\"\n");
495 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
497 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
498 fprintf(stderr
, "We have an error talking to the kernel\n");
508 int tc_act_list_or_flush(int argc
, char **argv
, int event
)
510 int ret
= 0, prio
= 0, msg_size
= 0;
512 struct rtattr
*tail
,*tail2
;
513 struct action_util
*a
= NULL
;
520 req
.t
.tca_family
= AF_UNSPEC
;
522 memset(&req
, 0, sizeof(req
));
524 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
526 tail
= NLMSG_TAIL(&req
.n
);
527 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
528 tail2
= NLMSG_TAIL(&req
.n
);
530 strncpy(k
, *argv
, sizeof (k
) - 1);
533 get_action_kind("gact");
536 a
= get_action_kind(k
);
538 fprintf(stderr
,"bad action %s\n",k
);
541 if (strcmp(a
->id
, k
) != 0) {
542 fprintf(stderr
,"bad action %s\n",k
);
545 strncpy(k
, *argv
, sizeof (k
) - 1);
547 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
548 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
549 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
550 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
552 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
554 if (event
== RTM_GETACTION
) {
555 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
556 perror("Cannot send dump request");
559 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
, NULL
, NULL
);
562 if (event
== RTM_DELACTION
) {
563 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
564 req
.n
.nlmsg_type
= RTM_DELACTION
;
565 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
566 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
567 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
568 fprintf(stderr
, "We have an error flushing\n");
579 int do_action(int argc
, char **argv
)
586 if (matches(*argv
, "add") == 0) {
587 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
588 } else if (matches(*argv
, "change") == 0 ||
589 matches(*argv
, "replace") == 0) {
590 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
591 } else if (matches(*argv
, "delete") == 0) {
594 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
595 } else if (matches(*argv
, "get") == 0) {
598 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
599 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
600 || matches(*argv
, "lst") == 0) {
605 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
606 } else if (matches(*argv
, "flush") == 0) {
611 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
612 } else if (matches(*argv
, "help") == 0) {
621 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);