]>
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
38 static void act_usage(void)
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"
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) RTA_PAYLOAD(opt
));
73 static int parse_noaopt(struct action_util
*au
, int *argc_p
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
76 char **argv
= *argv_p
;
79 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
81 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
86 static struct action_util
*get_action_kind(char *str
)
91 struct action_util
*a
;
96 for (a
= action_list
; a
; a
= a
->next
) {
97 if (strcmp(a
->id
, str
) == 0)
101 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
102 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
106 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
112 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
118 a
->next
= action_list
;
130 a
= malloc(sizeof(*a
));
132 memset(a
, 0, sizeof(*a
));
133 strncpy(a
->id
, "noact", 15);
134 a
->parse_aopt
= parse_noaopt
;
135 a
->print_aopt
= print_noaopt
;
144 if ((matches(*argv
, "change") == 0) ||
145 (matches(*argv
, "replace") == 0)||
146 (matches(*argv
, "delete") == 0)||
147 (matches(*argv
, "get") == 0)||
148 (matches(*argv
, "add") == 0))
156 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
159 char **argv
= *argv_p
;
160 struct rtattr
*tail
, *tail2
;
163 int eap
= 0; /* expect action parameters */
171 tail
= tail2
= NLMSG_TAIL(n
);
173 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
177 memset(k
, 0, sizeof (k
));
179 if (strcmp(*argv
, "action") == 0 ) {
185 get_action_kind("gact");
189 } else if (strcmp(*argv
, "help") == 0) {
191 } else if (new_cmd(argv
)) {
194 struct action_util
*a
= NULL
;
195 strncpy(k
, *argv
, sizeof (k
) - 1);
198 a
= get_action_kind(k
);
211 tail
= NLMSG_TAIL(n
);
212 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
213 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
215 ret
= a
->parse_aopt(a
,&argc
, &argv
, TCA_ACT_OPTIONS
, n
);
218 fprintf(stderr
,"bad action parsing\n");
221 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
228 fprintf(stderr
,"bad action empty %d\n",eap
);
232 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
239 /* no need to undo things, returning from here should
240 * cause enough pain */
241 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n",argc
,*argv
);
246 tc_print_one_action(FILE * f
, struct rtattr
*arg
)
249 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
251 struct action_util
*a
= NULL
;
256 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
257 if (tb
[TCA_ACT_KIND
] == NULL
) {
258 fprintf(stderr
, "NULL Action!\n");
263 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
268 fprintf(f
," %s \n", a
->id
);
273 err
= a
->print_aopt(a
,f
,tb
[TCA_ACT_OPTIONS
]);
279 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
280 fprintf(f
, "\tAction statistics:\n");
281 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
289 tc_print_action(FILE * f
, const struct rtattr
*arg
)
293 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
298 parse_rtattr_nested(tb
, TCA_ACT_MAX_PRIO
, arg
);
300 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1]) {
301 int ret
= tc_print_one_action(f
, tb
[0]);
305 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
307 fprintf(f
, "\n\taction order %d: ", i
+ batch_c
);
308 if (0 > tc_print_one_action(f
, tb
[i
])) {
309 fprintf(f
, "Error printing action\n");
315 batch_c
+=TCA_ACT_MAX_PRIO
;
319 int print_action(const struct sockaddr_nl
*who
,
323 FILE *fp
= (FILE*)arg
;
324 struct tcamsg
*t
= NLMSG_DATA(n
);
325 int len
= n
->nlmsg_len
;
326 struct rtattr
* tb
[TCAA_MAX
+1];
328 len
-= NLMSG_LENGTH(sizeof(*t
));
331 fprintf(stderr
, "Wrong len %d\n", len
);
335 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
337 if (NULL
== tb
[TCA_ACT_TAB
]) {
338 if (n
->nlmsg_type
!= RTM_GETACTION
)
339 fprintf(stderr
, "print_action: NULL kind\n");
343 if (n
->nlmsg_type
== RTM_DELACTION
) {
344 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
345 fprintf(fp
, "Flushed table ");
348 fprintf(fp
, "deleted action ");
352 if (n
->nlmsg_type
== RTM_NEWACTION
)
353 fprintf(fp
, "Added action ");
354 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
359 static int tc_action_gd(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
362 struct action_util
*a
= NULL
;
364 char **argv
= *argv_p
;
368 struct sockaddr_nl nladdr
;
370 struct rtattr
*tail2
;
371 struct nlmsghdr
*ans
= NULL
;
379 req
.t
.tca_family
= AF_UNSPEC
;
381 memset(&req
, 0, sizeof(req
));
383 memset(&nladdr
, 0, sizeof(nladdr
));
384 nladdr
.nl_family
= AF_NETLINK
;
386 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
387 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
388 req
.n
.nlmsg_type
= cmd
;
393 tail
= NLMSG_TAIL(&req
.n
);
394 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
397 if (strcmp(*argv
, "action") == 0 ) {
401 } else if (strcmp(*argv
, "help") == 0) {
405 strncpy(k
, *argv
, sizeof (k
) - 1);
406 a
= get_action_kind(k
);
408 fprintf(stderr
, "Error: non existent action: %s\n",k
);
412 if (strcmp(a
->id
, k
) != 0) {
413 fprintf(stderr
, "Error: non existent action: %s\n",k
);
421 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
426 if (matches(*argv
, "index") == 0) {
428 if (get_u32(&i
, *argv
, 10)) {
429 fprintf(stderr
, "Illegal \"index\"\n");
436 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
441 tail2
= NLMSG_TAIL(&req
.n
);
442 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
443 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
444 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
445 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
449 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
451 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
452 if (cmd
== RTM_GETACTION
)
455 if (rtnl_talk(&rth
, &req
.n
, 0, 0, ans
) < 0) {
456 fprintf(stderr
, "We have an error talking to the kernel\n");
460 if (ans
&& print_action(NULL
, &req
.n
, (void*)stdout
) < 0) {
461 fprintf(stderr
, "Dump terminated\n");
471 static int tc_action_modify(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
474 char **argv
= *argv_p
;
484 req
.t
.tca_family
= AF_UNSPEC
;
486 memset(&req
, 0, sizeof(req
));
488 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
489 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
490 req
.n
.nlmsg_type
= cmd
;
491 tail
= NLMSG_TAIL(&req
.n
);
494 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
495 fprintf(stderr
, "Illegal \"action\"\n");
498 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
500 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0) {
501 fprintf(stderr
, "We have an error talking to the kernel\n");
511 static int tc_act_list_or_flush(int argc
, char **argv
, int event
)
513 int ret
= 0, prio
= 0, msg_size
= 0;
515 struct rtattr
*tail
,*tail2
;
516 struct action_util
*a
= NULL
;
523 req
.t
.tca_family
= AF_UNSPEC
;
525 memset(&req
, 0, sizeof(req
));
527 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
529 tail
= NLMSG_TAIL(&req
.n
);
530 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
531 tail2
= NLMSG_TAIL(&req
.n
);
533 strncpy(k
, *argv
, sizeof (k
) - 1);
536 get_action_kind("gact");
539 a
= get_action_kind(k
);
541 fprintf(stderr
,"bad action %s\n",k
);
544 if (strcmp(a
->id
, k
) != 0) {
545 fprintf(stderr
,"bad action %s\n",k
);
548 strncpy(k
, *argv
, sizeof (k
) - 1);
550 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
551 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
552 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
553 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
555 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
557 if (event
== RTM_GETACTION
) {
558 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
559 perror("Cannot send dump request");
562 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
565 if (event
== RTM_DELACTION
) {
566 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
567 req
.n
.nlmsg_type
= RTM_DELACTION
;
568 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
569 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
570 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0) {
571 fprintf(stderr
, "We have an error flushing\n");
582 int do_action(int argc
, char **argv
)
589 if (matches(*argv
, "add") == 0) {
590 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
591 } else if (matches(*argv
, "change") == 0 ||
592 matches(*argv
, "replace") == 0) {
593 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
594 } else if (matches(*argv
, "delete") == 0) {
597 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
598 } else if (matches(*argv
, "get") == 0) {
601 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
602 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
603 || matches(*argv
, "lst") == 0) {
608 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
609 } else if (matches(*argv
, "flush") == 0) {
614 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
615 } else if (matches(*argv
, "help") == 0) {
624 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);