]>
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"
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) 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 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
), "m_%s.so", str
);
101 dlh
= dlopen(buf
, RTLD_LAZY
);
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
, "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
, "help") == 0) {
189 } else if (new_cmd(argv
)) {
192 struct action_util
*a
= NULL
;
193 strncpy(k
, *argv
, sizeof (k
) - 1);
196 a
= get_action_kind(k
);
209 tail
= NLMSG_TAIL(n
);
210 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
211 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
213 ret
= a
->parse_aopt(a
,&argc
, &argv
, TCA_ACT_OPTIONS
, n
);
216 fprintf(stderr
,"bad action parsing\n");
219 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
226 fprintf(stderr
,"bad action empty %d\n",eap
);
230 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
237 /* no need to undo things, returning from here should
238 * cause enough pain */
239 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n",argc
,*argv
);
244 tc_print_one_action(FILE * f
, struct rtattr
*arg
)
247 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
249 struct action_util
*a
= NULL
;
254 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
255 if (tb
[TCA_ACT_KIND
] == NULL
) {
256 fprintf(stderr
, "NULL Action!\n");
261 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
266 fprintf(f
," %s \n", a
->id
);
271 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(FILE * f
, const struct rtattr
*arg
)
291 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
296 parse_rtattr_nested(tb
, TCA_ACT_MAX_PRIO
, arg
);
298 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1]) {
299 int ret
= tc_print_one_action(f
, tb
[0]);
303 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
305 fprintf(f
, "\n\taction order %d: ", i
+ batch_c
);
306 if (0 > tc_print_one_action(f
, tb
[i
])) {
307 fprintf(f
, "Error printing action\n");
313 batch_c
+=TCA_ACT_MAX_PRIO
;
317 int print_action(const struct sockaddr_nl
*who
,
321 FILE *fp
= (FILE*)arg
;
322 struct tcamsg
*t
= NLMSG_DATA(n
);
323 int len
= n
->nlmsg_len
;
324 struct rtattr
* tb
[TCAA_MAX
+1];
326 len
-= NLMSG_LENGTH(sizeof(*t
));
329 fprintf(stderr
, "Wrong len %d\n", len
);
333 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
335 if (NULL
== tb
[TCA_ACT_TAB
]) {
336 if (n
->nlmsg_type
!= RTM_GETACTION
)
337 fprintf(stderr
, "print_action: NULL kind\n");
341 if (n
->nlmsg_type
== RTM_DELACTION
) {
342 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
343 fprintf(fp
, "Flushed table ");
346 fprintf(fp
, "deleted action ");
350 if (n
->nlmsg_type
== RTM_NEWACTION
)
351 fprintf(fp
, "Added action ");
352 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
357 int tc_action_gd(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
360 struct action_util
*a
= NULL
;
362 char **argv
= *argv_p
;
366 struct sockaddr_nl nladdr
;
368 struct rtattr
*tail2
;
369 struct nlmsghdr
*ans
= NULL
;
377 req
.t
.tca_family
= AF_UNSPEC
;
379 memset(&req
, 0, sizeof(req
));
381 memset(&nladdr
, 0, sizeof(nladdr
));
382 nladdr
.nl_family
= AF_NETLINK
;
384 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
385 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
386 req
.n
.nlmsg_type
= cmd
;
391 tail
= NLMSG_TAIL(&req
.n
);
392 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
395 if (strcmp(*argv
, "action") == 0 ) {
399 } else if (strcmp(*argv
, "help") == 0) {
403 strncpy(k
, *argv
, sizeof (k
) - 1);
404 a
= get_action_kind(k
);
406 fprintf(stderr
, "Error: non existent action: %s\n",k
);
410 if (strcmp(a
->id
, k
) != 0) {
411 fprintf(stderr
, "Error: non existent action: %s\n",k
);
419 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
424 if (matches(*argv
, "index") == 0) {
426 if (get_u32(&i
, *argv
, 10)) {
427 fprintf(stderr
, "Illegal \"index\"\n");
434 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
439 tail2
= NLMSG_TAIL(&req
.n
);
440 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
441 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
442 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
443 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
447 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
449 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
450 if (cmd
== RTM_GETACTION
)
453 if (rtnl_talk(&rth
, &req
.n
, 0, 0, ans
, NULL
, NULL
) < 0) {
454 fprintf(stderr
, "We have an error talking to the kernel\n");
458 if (ans
&& print_action(NULL
, &req
.n
, (void*)stdout
) < 0) {
459 fprintf(stderr
, "Dump terminated\n");
469 int tc_action_modify(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
472 char **argv
= *argv_p
;
482 req
.t
.tca_family
= AF_UNSPEC
;
484 memset(&req
, 0, sizeof(req
));
486 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
487 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
488 req
.n
.nlmsg_type
= cmd
;
489 tail
= NLMSG_TAIL(&req
.n
);
492 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
493 fprintf(stderr
, "Illegal \"action\"\n");
496 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
498 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
499 fprintf(stderr
, "We have an error talking to the kernel\n");
509 int tc_act_list_or_flush(int argc
, char **argv
, int event
)
511 int ret
= 0, prio
= 0, msg_size
= 0;
513 struct rtattr
*tail
,*tail2
;
514 struct action_util
*a
= NULL
;
521 req
.t
.tca_family
= AF_UNSPEC
;
523 memset(&req
, 0, sizeof(req
));
525 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
527 tail
= NLMSG_TAIL(&req
.n
);
528 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
529 tail2
= NLMSG_TAIL(&req
.n
);
531 strncpy(k
, *argv
, sizeof (k
) - 1);
534 get_action_kind("gact");
537 a
= get_action_kind(k
);
539 fprintf(stderr
,"bad action %s\n",k
);
542 if (strcmp(a
->id
, k
) != 0) {
543 fprintf(stderr
,"bad action %s\n",k
);
546 strncpy(k
, *argv
, sizeof (k
) - 1);
548 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
549 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
550 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
551 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
553 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
555 if (event
== RTM_GETACTION
) {
556 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
557 perror("Cannot send dump request");
560 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
, NULL
, NULL
);
563 if (event
== RTM_DELACTION
) {
564 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
565 req
.n
.nlmsg_type
= RTM_DELACTION
;
566 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
567 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
568 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
569 fprintf(stderr
, "We have an error flushing\n");
580 int do_action(int argc
, char **argv
)
587 if (matches(*argv
, "add") == 0) {
588 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
589 } else if (matches(*argv
, "change") == 0 ||
590 matches(*argv
, "replace") == 0) {
591 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
592 } else if (matches(*argv
, "delete") == 0) {
595 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
596 } else if (matches(*argv
, "get") == 0) {
599 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
600 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
601 || matches(*argv
, "lst") == 0) {
606 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
607 } else if (matches(*argv
, "flush") == 0) {
612 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
613 } else if (matches(*argv
, "help") == 0) {
622 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);