]>
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>
33 static struct action_util
* action_list
;
35 int gact_ld
= 0 ; //fuckin backward compatibility
42 fprintf (stderr
, "action usage improper\n");
45 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
47 if (opt
&& RTA_PAYLOAD(opt
))
48 fprintf(f
, "[Unknown action, optlen=%u] ",
49 (unsigned) RTA_PAYLOAD(opt
));
53 static int parse_noaopt(struct action_util
*au
, int *argc_p
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
56 char **argv
= *argv_p
;
59 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
61 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
66 struct action_util
*get_action_kind(char *str
)
70 struct action_util
*a
;
75 for (a
= action_list
; a
; a
= a
->next
) {
76 if (strcmp(a
->id
, str
) == 0)
80 snprintf(buf
, sizeof(buf
), "m_%s.so", str
);
81 dlh
= dlopen(buf
, RTLD_LAZY
);
85 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
91 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
97 a
->next
= action_list
;
109 a
= malloc(sizeof(*a
));
111 memset(a
, 0, sizeof(*a
));
112 strncpy(a
->id
, "noact", 15);
113 a
->parse_aopt
= parse_noaopt
;
114 a
->print_aopt
= print_noaopt
;
123 if ((matches(*argv
, "change") == 0) ||
124 (matches(*argv
, "replace") == 0)||
125 (matches(*argv
, "delete") == 0)||
126 (matches(*argv
, "add") == 0))
134 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
137 char **argv
= *argv_p
;
138 struct rtattr
*tail
, *tail2
;
141 int eap
= 0; /* expect action parameters */
150 (struct rtattr
*) (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
));
152 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
156 memset(k
, 0, sizeof (k
));
158 if (strcmp(*argv
, "action") == 0 ) {
164 get_action_kind("gact");
168 } else if (strcmp(*argv
, "help") == 0) {
170 } else if (new_cmd(argv
)) {
173 struct action_util
*a
= NULL
;
174 strncpy(k
, *argv
, sizeof (k
) - 1);
177 a
= get_action_kind(k
);
191 (struct rtattr
*) (((void *) n
) +
192 NLMSG_ALIGN(n
->nlmsg_len
));
193 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
194 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
196 ret
= a
->parse_aopt(a
,&argc
, &argv
, TCA_ACT_OPTIONS
, n
);
199 fprintf(stderr
,"bad action parsing\n");
203 (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
)) -
211 fprintf(stderr
,"bad action empty %d\n",eap
);
216 (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
)) - (void *) tail2
;
223 /* no need to undo things, returning from here should
224 * cause enough pain */
225 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n",argc
,*argv
);
230 tc_print_one_action(FILE * f
, struct rtattr
*arg
)
233 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
235 struct action_util
*a
= NULL
;
240 memset(tb
, 0, sizeof (tb
));
241 parse_rtattr(tb
, TCA_ACT_MAX
, RTA_DATA(arg
), RTA_PAYLOAD(arg
));
242 if (tb
[TCA_ACT_KIND
] == NULL
) {
243 fprintf(stderr
, "NULL Action!\n");
248 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
253 fprintf(f
," %s \n", a
->id
);
258 err
= a
->print_aopt(a
,f
,tb
[TCA_ACT_OPTIONS
]);
264 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
265 fprintf(f
, "\tAction statistics:\n");
266 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
274 tc_print_action(FILE * f
, const struct rtattr
*arg
)
278 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
283 memset(tb
, 0, sizeof (tb
));
284 parse_rtattr(tb
, TCA_ACT_MAX_PRIO
, RTA_DATA(arg
), RTA_PAYLOAD(arg
));
286 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1]) {
287 int ret
= tc_print_one_action(f
, tb
[0]);
291 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
293 fprintf(f
, "\n\taction order %d: ", i
+ batch_c
);
294 if (0 > tc_print_one_action(f
, tb
[i
])) {
295 fprintf(f
, "Error printing action\n");
301 batch_c
+=TCA_ACT_MAX_PRIO
;
305 static int do_print_action(const struct sockaddr_nl
*who
,
309 FILE *fp
= (FILE*)arg
;
310 struct tcamsg
*t
= NLMSG_DATA(n
);
311 int len
= n
->nlmsg_len
;
312 struct rtattr
* tb
[TCAA_MAX
+1];
314 len
-= NLMSG_LENGTH(sizeof(*t
));
317 fprintf(stderr
, "Wrong len %d\n", len
);
321 memset(tb
, 0, sizeof(tb
));
322 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
324 if (NULL
== tb
[TCA_ACT_TAB
]) {
325 if (n
->nlmsg_type
!= RTM_GETACTION
)
326 fprintf(stderr
, "do_print_action: NULL kind\n");
330 if (n
->nlmsg_type
== RTM_DELACTION
) {
331 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
332 fprintf(fp
, "Flushed table ");
335 fprintf(fp
, "deleted action ");
339 if (n
->nlmsg_type
== RTM_NEWACTION
)
340 fprintf(fp
, "Added action ");
341 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
346 int tc_action_gd(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
349 struct action_util
*a
= NULL
;
351 char **argv
= *argv_p
;
355 struct rtnl_handle rth
;
356 struct sockaddr_nl nladdr
;
358 struct rtattr
*tail2
;
359 struct nlmsghdr
*ans
= NULL
;
367 req
.t
.tca_family
= AF_UNSPEC
;
369 memset(&req
, 0, sizeof(req
));
371 memset(&nladdr
, 0, sizeof(nladdr
));
372 nladdr
.nl_family
= AF_NETLINK
;
374 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
375 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
376 req
.n
.nlmsg_type
= cmd
;
381 tail
= (struct rtattr
*)(((void*)&req
.n
)+NLMSG_ALIGN(req
.n
.nlmsg_len
));
383 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
386 if (strcmp(*argv
, "action") == 0 ) {
390 } else if (strcmp(*argv
, "help") == 0) {
394 strncpy(k
, *argv
, sizeof (k
) - 1);
395 a
= get_action_kind(k
);
397 fprintf(stderr
, "Error: non existent action: %s\n",k
);
401 if (strcmp(a
->id
, k
) != 0) {
402 fprintf(stderr
, "Error: non existent action: %s\n",k
);
410 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
415 if (matches(*argv
, "index") == 0) {
417 if (get_u32(&i
, *argv
, 10)) {
418 fprintf(stderr
, "Illegal \"index\"\n");
425 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
431 (struct rtattr
*) (((void *) &req
.n
) + NLMSG_ALIGN(req
.n
.nlmsg_len
));
432 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
433 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
434 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
436 (((void *) &req
.n
) + NLMSG_ALIGN(req
.n
.nlmsg_len
)) - (void *) tail2
;
440 tail
->rta_len
= (((void*)&req
.n
)+ NLMSG_ALIGN(req
.n
.nlmsg_len
)) - (void*)tail
;
442 if (rtnl_open(&rth
, 0) < 0) {
443 fprintf(stderr
, "Cannot open rtnetlink\n");
447 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
448 if (cmd
== RTM_GETACTION
)
450 if (rtnl_talk(&rth
, &req
.n
, 0, 0, ans
, NULL
, NULL
) < 0) {
451 fprintf(stderr
, "We have an error talking to the kernel\n");
456 if (ans
&& do_print_action(NULL
, &req
.n
, (void*)stdout
) < 0) {
457 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
;
475 struct rtnl_handle rth
;
483 req
.t
.tca_family
= AF_UNSPEC
;
485 memset(&req
, 0, sizeof(req
));
487 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
488 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
489 req
.n
.nlmsg_type
= cmd
;
490 tail
= (struct rtattr
*)(((void*)&req
.n
)+NLMSG_ALIGN(req
.n
.nlmsg_len
));
493 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
494 fprintf(stderr
, "Illegal \"action\"\n");
497 tail
->rta_len
= (((void*)&req
.n
)+req
.n
.nlmsg_len
) - (void*)tail
;
499 if (rtnl_open(&rth
, 0) < 0) {
500 fprintf(stderr
, "Cannot open rtnetlink\n");
505 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
506 fprintf(stderr
, "We have an error talking to the kernel\n");
516 int tc_act_list_or_flush(int argc
, char **argv
, int event
)
518 int ret
= 0, prio
= 0, msg_size
= 0;
520 struct rtnl_handle rth
;
521 struct rtattr
*tail
,*tail2
;
522 struct action_util
*a
= NULL
;
529 req
.t
.tca_family
= AF_UNSPEC
;
531 memset(&req
, 0, sizeof(req
));
533 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
535 tail
= (struct rtattr
*)(((void*)&req
.n
)+NLMSG_ALIGN(req
.n
.nlmsg_len
));
537 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
539 (struct rtattr
*) (((void *) &req
.n
) + NLMSG_ALIGN(req
.n
.nlmsg_len
));
541 strncpy(k
, *argv
, sizeof (k
) - 1);
544 get_action_kind("gact");
547 a
= get_action_kind(k
);
549 fprintf(stderr
,"bad action %s\n",k
);
552 if (strcmp(a
->id
, k
) != 0) {
553 fprintf(stderr
,"bad action %s\n",k
);
556 strncpy(k
, *argv
, sizeof (k
) - 1);
558 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
559 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
561 (((void *) &req
.n
) + NLMSG_ALIGN(req
.n
.nlmsg_len
)) - (void *) tail2
;
562 tail
->rta_len
= (((void*)&req
.n
)+NLMSG_ALIGN(req
.n
.nlmsg_len
)) - (void*)tail
;
565 if (rtnl_open(&rth
, 0) < 0) {
566 fprintf(stderr
, "Cannot open rtnetlink\n");
570 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
572 if (event
== RTM_GETACTION
) {
573 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
574 perror("Cannot send dump request");
577 ret
= rtnl_dump_filter(&rth
, do_print_action
, stdout
, NULL
, NULL
);
580 if (event
== RTM_DELACTION
) {
581 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
582 req
.n
.nlmsg_type
= RTM_DELACTION
;
583 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
584 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
585 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
586 fprintf(stderr
, "We have an error flushing\n");
599 int do_action(int argc
, char **argv
)
606 if (matches(*argv
, "add") == 0) {
607 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
608 } else if (matches(*argv
, "change") == 0 ||
609 matches(*argv
, "replace") == 0) {
610 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
611 } else if (matches(*argv
, "delete") == 0) {
614 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
615 } else if (matches(*argv
, "get") == 0) {
618 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
619 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
620 || matches(*argv
, "lst") == 0) {
625 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
626 } else if (matches(*argv
, "flush") == 0) {
631 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
632 } else if (matches(*argv
, "help") == 0) {
641 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc action help\".\n", *argv
);