]>
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>
30 static struct action_util
* action_list
;
32 int gact_ld
= 0 ; //fuckin backward compatibility
39 fprintf (stderr
, "action usage improper\n");
42 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
44 if (opt
&& RTA_PAYLOAD(opt
))
45 fprintf(f
, "[Unknown action, optlen=%u] ",
46 (unsigned) RTA_PAYLOAD(opt
));
50 static int parse_noaopt(struct action_util
*au
, int *argc_p
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
53 char **argv
= *argv_p
;
56 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
58 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
63 struct action_util
*get_action_kind(char *str
)
68 struct action_util
*a
;
73 for (a
= action_list
; a
; a
= a
->next
) {
74 if (strcmp(a
->id
, str
) == 0)
78 snprintf(buf
, sizeof(buf
), "m_%s.so", str
);
79 dlh
= dlopen(buf
, RTLD_LAZY
);
83 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
89 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
95 a
->next
= action_list
;
107 a
= malloc(sizeof(*a
));
109 memset(a
, 0, sizeof(*a
));
110 strncpy(a
->id
, "noact", 15);
111 a
->parse_aopt
= parse_noaopt
;
112 a
->print_aopt
= print_noaopt
;
121 if ((matches(*argv
, "change") == 0) ||
122 (matches(*argv
, "replace") == 0)||
123 (matches(*argv
, "delete") == 0)||
124 (matches(*argv
, "add") == 0))
132 parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
135 char **argv
= *argv_p
;
136 struct rtattr
*tail
, *tail2
;
139 int eap
= 0; /* expect action parameters */
147 tail
= tail2
= NLMSG_TAIL(n
);
149 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
153 memset(k
, 0, sizeof (k
));
155 if (strcmp(*argv
, "action") == 0 ) {
161 get_action_kind("gact");
165 } else if (strcmp(*argv
, "help") == 0) {
167 } else if (new_cmd(argv
)) {
170 struct action_util
*a
= NULL
;
171 strncpy(k
, *argv
, sizeof (k
) - 1);
174 a
= get_action_kind(k
);
187 tail
= NLMSG_TAIL(n
);
188 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
189 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
191 ret
= a
->parse_aopt(a
,&argc
, &argv
, TCA_ACT_OPTIONS
, n
);
194 fprintf(stderr
,"bad action parsing\n");
197 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
204 fprintf(stderr
,"bad action empty %d\n",eap
);
208 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
215 /* no need to undo things, returning from here should
216 * cause enough pain */
217 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n",argc
,*argv
);
222 tc_print_one_action(FILE * f
, struct rtattr
*arg
)
225 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
227 struct action_util
*a
= NULL
;
232 memset(tb
, 0, sizeof (tb
));
233 parse_rtattr(tb
, TCA_ACT_MAX
, RTA_DATA(arg
), RTA_PAYLOAD(arg
));
234 if (tb
[TCA_ACT_KIND
] == NULL
) {
235 fprintf(stderr
, "NULL Action!\n");
240 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
245 fprintf(f
," %s \n", a
->id
);
250 err
= a
->print_aopt(a
,f
,tb
[TCA_ACT_OPTIONS
]);
256 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
257 fprintf(f
, "\tAction statistics:\n");
258 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
266 tc_print_action(FILE * f
, const struct rtattr
*arg
)
270 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
275 memset(tb
, 0, sizeof (tb
));
276 parse_rtattr(tb
, TCA_ACT_MAX_PRIO
, RTA_DATA(arg
), RTA_PAYLOAD(arg
));
278 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1]) {
279 int ret
= tc_print_one_action(f
, tb
[0]);
283 for (i
= 0; i
< TCA_ACT_MAX_PRIO
; i
++) {
285 fprintf(f
, "\n\taction order %d: ", i
+ batch_c
);
286 if (0 > tc_print_one_action(f
, tb
[i
])) {
287 fprintf(f
, "Error printing action\n");
293 batch_c
+=TCA_ACT_MAX_PRIO
;
297 static int do_print_action(const struct sockaddr_nl
*who
,
301 FILE *fp
= (FILE*)arg
;
302 struct tcamsg
*t
= NLMSG_DATA(n
);
303 int len
= n
->nlmsg_len
;
304 struct rtattr
* tb
[TCAA_MAX
+1];
306 len
-= NLMSG_LENGTH(sizeof(*t
));
309 fprintf(stderr
, "Wrong len %d\n", len
);
313 memset(tb
, 0, sizeof(tb
));
314 parse_rtattr(tb
, TCAA_MAX
, TA_RTA(t
), len
);
316 if (NULL
== tb
[TCA_ACT_TAB
]) {
317 if (n
->nlmsg_type
!= RTM_GETACTION
)
318 fprintf(stderr
, "do_print_action: NULL kind\n");
322 if (n
->nlmsg_type
== RTM_DELACTION
) {
323 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
324 fprintf(fp
, "Flushed table ");
327 fprintf(fp
, "deleted action ");
331 if (n
->nlmsg_type
== RTM_NEWACTION
)
332 fprintf(fp
, "Added action ");
333 tc_print_action(fp
, tb
[TCA_ACT_TAB
]);
338 int tc_action_gd(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
341 struct action_util
*a
= NULL
;
343 char **argv
= *argv_p
;
347 struct rtnl_handle rth
;
348 struct sockaddr_nl nladdr
;
350 struct rtattr
*tail2
;
351 struct nlmsghdr
*ans
= NULL
;
359 req
.t
.tca_family
= AF_UNSPEC
;
361 memset(&req
, 0, sizeof(req
));
363 memset(&nladdr
, 0, sizeof(nladdr
));
364 nladdr
.nl_family
= AF_NETLINK
;
366 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
367 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
368 req
.n
.nlmsg_type
= cmd
;
373 tail
= NLMSG_TAIL(&req
.n
);
374 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
377 if (strcmp(*argv
, "action") == 0 ) {
381 } else if (strcmp(*argv
, "help") == 0) {
385 strncpy(k
, *argv
, sizeof (k
) - 1);
386 a
= get_action_kind(k
);
388 fprintf(stderr
, "Error: non existent action: %s\n",k
);
392 if (strcmp(a
->id
, k
) != 0) {
393 fprintf(stderr
, "Error: non existent action: %s\n",k
);
401 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
406 if (matches(*argv
, "index") == 0) {
408 if (get_u32(&i
, *argv
, 10)) {
409 fprintf(stderr
, "Illegal \"index\"\n");
416 fprintf(stderr
, "Error: no index specified action: %s\n",k
);
421 tail2
= NLMSG_TAIL(&req
.n
);
422 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
423 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
424 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
425 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
429 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
431 if (rtnl_open(&rth
, 0) < 0) {
432 fprintf(stderr
, "Cannot open rtnetlink\n");
436 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
437 if (cmd
== RTM_GETACTION
)
439 if (rtnl_talk(&rth
, &req
.n
, 0, 0, ans
, NULL
, NULL
) < 0) {
440 fprintf(stderr
, "We have an error talking to the kernel\n");
445 if (ans
&& do_print_action(NULL
, &req
.n
, (void*)stdout
) < 0) {
446 fprintf(stderr
, "Dump terminated\n");
458 int tc_action_modify(int cmd
, unsigned flags
, int *argc_p
, char ***argv_p
)
461 char **argv
= *argv_p
;
464 struct rtnl_handle rth
;
472 req
.t
.tca_family
= AF_UNSPEC
;
474 memset(&req
, 0, sizeof(req
));
476 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
477 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
478 req
.n
.nlmsg_type
= cmd
;
479 tail
= NLMSG_TAIL(&req
.n
);
482 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
483 fprintf(stderr
, "Illegal \"action\"\n");
486 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
488 if (rtnl_open(&rth
, 0) < 0) {
489 fprintf(stderr
, "Cannot open rtnetlink\n");
494 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
495 fprintf(stderr
, "We have an error talking to the kernel\n");
505 int tc_act_list_or_flush(int argc
, char **argv
, int event
)
507 int ret
= 0, prio
= 0, msg_size
= 0;
509 struct rtnl_handle rth
;
510 struct rtattr
*tail
,*tail2
;
511 struct action_util
*a
= NULL
;
518 req
.t
.tca_family
= AF_UNSPEC
;
520 memset(&req
, 0, sizeof(req
));
522 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
524 tail
= NLMSG_TAIL(&req
.n
);
525 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
526 tail2
= NLMSG_TAIL(&req
.n
);
528 strncpy(k
, *argv
, sizeof (k
) - 1);
531 get_action_kind("gact");
534 a
= get_action_kind(k
);
536 fprintf(stderr
,"bad action %s\n",k
);
539 if (strcmp(a
->id
, k
) != 0) {
540 fprintf(stderr
,"bad action %s\n",k
);
543 strncpy(k
, *argv
, sizeof (k
) - 1);
545 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
546 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
547 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
548 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
550 if (rtnl_open(&rth
, 0) < 0) {
551 fprintf(stderr
, "Cannot open rtnetlink\n");
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
, do_print_action
, stdout
, NULL
, NULL
);
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
, NULL
, NULL
) < 0) {
571 fprintf(stderr
, "We have an error flushing\n");
584 int do_action(int argc
, char **argv
)
591 if (matches(*argv
, "add") == 0) {
592 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
593 } else if (matches(*argv
, "change") == 0 ||
594 matches(*argv
, "replace") == 0) {
595 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
596 } else if (matches(*argv
, "delete") == 0) {
599 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
600 } else if (matches(*argv
, "get") == 0) {
603 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
604 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
605 || matches(*argv
, "lst") == 0) {
610 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_GETACTION
);
611 } else if (matches(*argv
, "flush") == 0) {
616 return tc_act_list_or_flush(argc
-2, argv
+2, RTM_DELACTION
);
617 } else if (matches(*argv
, "help") == 0) {
626 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc action help\".\n", *argv
);