]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_filter.c
1a1082b446857a5f786929f51e1a4280ec4f2905
2 * tc_filter.c "tc filter".
4 * This program is free software; you can redistribute 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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include <linux/if_ether.h>
27 #include "tc_common.h"
29 static void usage(void)
31 fprintf(stderr
, "Usage: tc filter [ add | del | change | replace | show ] dev STRING\n");
32 fprintf(stderr
, " [ pref PRIO ] protocol PROTO\n");
33 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
34 fprintf(stderr
, " [ root | ingress | egress | parent CLASSID ]\n");
35 fprintf(stderr
, " [ handle FILTERID ] [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr
, "\n");
37 fprintf(stderr
, " tc filter show [ dev STRING ] [ root | ingress | egress | parent CLASSID ]\n");
38 fprintf(stderr
, "Where:\n");
39 fprintf(stderr
, "FILTER_TYPE := { rsvp | u32 | bpf | fw | route | etc. }\n");
40 fprintf(stderr
, "FILTERID := ... format depends on classifier, see there\n");
41 fprintf(stderr
, "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
44 static int tc_filter_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
51 struct filter_util
*q
= NULL
;
58 struct tc_estimator est
;
60 memset(&req
, 0, sizeof(req
));
61 memset(&est
, 0, sizeof(est
));
62 memset(d
, 0, sizeof(d
));
63 memset(k
, 0, sizeof(k
));
64 memset(&req
, 0, sizeof(req
));
66 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
));
67 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
68 req
.n
.nlmsg_type
= cmd
;
69 req
.t
.tcm_family
= AF_UNSPEC
;
71 if (cmd
== RTM_NEWTFILTER
&& flags
& NLM_F_CREATE
)
72 protocol
= htons(ETH_P_ALL
);
75 if (strcmp(*argv
, "dev") == 0) {
79 strncpy(d
, *argv
, sizeof(d
)-1);
80 } else if (strcmp(*argv
, "root") == 0) {
81 if (req
.t
.tcm_parent
) {
82 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
85 req
.t
.tcm_parent
= TC_H_ROOT
;
86 } else if (strcmp(*argv
, "ingress") == 0) {
87 if (req
.t
.tcm_parent
) {
88 fprintf(stderr
, "Error: \"ingress\" is duplicate parent ID\n");
91 req
.t
.tcm_parent
= TC_H_MAKE(TC_H_CLSACT
,
93 } else if (strcmp(*argv
, "egress") == 0) {
94 if (req
.t
.tcm_parent
) {
95 fprintf(stderr
, "Error: \"egress\" is duplicate parent ID\n");
98 req
.t
.tcm_parent
= TC_H_MAKE(TC_H_CLSACT
,
100 } else if (strcmp(*argv
, "parent") == 0) {
103 if (req
.t
.tcm_parent
)
104 duparg("parent", *argv
);
105 if (get_tc_classid(&handle
, *argv
))
106 invarg("Invalid parent ID", *argv
);
107 req
.t
.tcm_parent
= handle
;
108 } else if (strcmp(*argv
, "handle") == 0) {
111 duparg("handle", *argv
);
113 } else if (matches(*argv
, "preference") == 0 ||
114 matches(*argv
, "priority") == 0) {
117 duparg("priority", *argv
);
118 if (get_u32(&prio
, *argv
, 0) || prio
> 0xFFFF)
119 invarg("invalid priority value", *argv
);
120 } else if (matches(*argv
, "protocol") == 0) {
124 duparg("protocol", *argv
);
125 if (ll_proto_a2n(&id
, *argv
))
126 invarg("invalid protocol", *argv
);
129 } else if (matches(*argv
, "estimator") == 0) {
130 if (parse_estimator(&argc
, &argv
, &est
) < 0)
132 } else if (matches(*argv
, "help") == 0) {
136 strncpy(k
, *argv
, sizeof(k
)-1);
138 q
= get_filter_kind(k
);
146 req
.t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
149 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
152 if (q
->parse_fopt(q
, fhandle
, argc
, argv
, &req
.n
))
156 fprintf(stderr
, "Must specify filter type when using "
161 if (matches(*argv
, "help") == 0)
163 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv
);
168 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
174 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
175 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
180 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
181 fprintf(stderr
, "We have an error talking to the kernel\n");
188 static __u32 filter_parent
;
189 static int filter_ifindex
;
190 static __u32 filter_prio
;
191 static __u32 filter_protocol
;
194 int print_filter(const struct sockaddr_nl
*who
,
198 FILE *fp
= (FILE*)arg
;
199 struct tcmsg
*t
= NLMSG_DATA(n
);
200 int len
= n
->nlmsg_len
;
201 struct rtattr
* tb
[TCA_MAX
+1];
202 struct filter_util
*q
;
205 if (n
->nlmsg_type
!= RTM_NEWTFILTER
&& n
->nlmsg_type
!= RTM_DELTFILTER
) {
206 fprintf(stderr
, "Not a filter\n");
209 len
-= NLMSG_LENGTH(sizeof(*t
));
211 fprintf(stderr
, "Wrong len %d\n", len
);
215 memset(tb
, 0, sizeof(tb
));
216 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
218 if (tb
[TCA_KIND
] == NULL
) {
219 fprintf(stderr
, "print_filter: NULL kind\n");
223 if (n
->nlmsg_type
== RTM_DELTFILTER
)
224 fprintf(fp
, "deleted ");
226 fprintf(fp
, "filter ");
227 if (!filter_ifindex
|| filter_ifindex
!= t
->tcm_ifindex
)
228 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
230 if (!filter_parent
|| filter_parent
!= t
->tcm_parent
) {
231 if (t
->tcm_parent
== TC_H_ROOT
)
232 fprintf(fp
, "root ");
233 else if (t
->tcm_parent
== TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_INGRESS
))
234 fprintf(fp
, "ingress ");
235 else if (t
->tcm_parent
== TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_EGRESS
))
236 fprintf(fp
, "egress ");
238 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
239 fprintf(fp
, "parent %s ", abuf
);
244 f_proto
= TC_H_MIN(t
->tcm_info
);
245 __u32 prio
= TC_H_MAJ(t
->tcm_info
)>>16;
246 if (!filter_protocol
|| filter_protocol
!= f_proto
) {
249 fprintf(fp
, "protocol %s ",
250 ll_proto_n2a(f_proto
, b1
, sizeof(b1
)));
253 if (!filter_prio
|| filter_prio
!= prio
) {
255 fprintf(fp
, "pref %u ", prio
);
258 fprintf(fp
, "%s ", rta_getattr_str(tb
[TCA_KIND
]));
259 q
= get_filter_kind(RTA_DATA(tb
[TCA_KIND
]));
260 if (tb
[TCA_OPTIONS
]) {
262 q
->print_fopt(q
, fp
, tb
[TCA_OPTIONS
], t
->tcm_handle
);
264 fprintf(fp
, "[cannot parse parameters]");
268 if (show_stats
&& (tb
[TCA_STATS
] || tb
[TCA_STATS2
])) {
269 print_tcstats_attr(fp
, tb
, " ", NULL
);
277 static int tc_filter_list(int argc
, char **argv
)
283 char *fhandle
= NULL
;
285 memset(&t
, 0, sizeof(t
));
286 t
.tcm_family
= AF_UNSPEC
;
287 memset(d
, 0, sizeof(d
));
290 if (strcmp(*argv
, "dev") == 0) {
293 duparg("dev", *argv
);
294 strncpy(d
, *argv
, sizeof(d
)-1);
295 } else if (strcmp(*argv
, "root") == 0) {
297 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
300 filter_parent
= t
.tcm_parent
= TC_H_ROOT
;
301 } else if (strcmp(*argv
, "ingress") == 0) {
303 fprintf(stderr
, "Error: \"ingress\" is duplicate parent ID\n");
306 filter_parent
= TC_H_MAKE(TC_H_CLSACT
,
308 t
.tcm_parent
= filter_parent
;
309 } else if (strcmp(*argv
, "egress") == 0) {
311 fprintf(stderr
, "Error: \"egress\" is duplicate parent ID\n");
314 filter_parent
= TC_H_MAKE(TC_H_CLSACT
,
316 t
.tcm_parent
= filter_parent
;
317 } else if (strcmp(*argv
, "parent") == 0) {
321 duparg("parent", *argv
);
322 if (get_tc_classid(&handle
, *argv
))
323 invarg("invalid parent ID", *argv
);
324 filter_parent
= t
.tcm_parent
= handle
;
325 } else if (strcmp(*argv
, "handle") == 0) {
328 duparg("handle", *argv
);
330 } else if (matches(*argv
, "preference") == 0 ||
331 matches(*argv
, "priority") == 0) {
334 duparg("priority", *argv
);
335 if (get_u32(&prio
, *argv
, 0))
336 invarg("invalid preference", *argv
);
338 } else if (matches(*argv
, "protocol") == 0) {
342 duparg("protocol", *argv
);
343 if (ll_proto_a2n(&res
, *argv
))
344 invarg("invalid protocol", *argv
);
346 filter_protocol
= protocol
;
347 } else if (matches(*argv
, "help") == 0) {
350 fprintf(stderr
, " What is \"%s\"? Try \"tc filter help\"\n", *argv
);
357 t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
362 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
363 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
366 filter_ifindex
= t
.tcm_ifindex
;
369 if (rtnl_dump_request(&rth
, RTM_GETTFILTER
, &t
, sizeof(t
)) < 0) {
370 perror("Cannot send dump request");
374 if (rtnl_dump_filter(&rth
, print_filter
, stdout
) < 0) {
375 fprintf(stderr
, "Dump terminated\n");
382 int do_filter(int argc
, char **argv
)
385 return tc_filter_list(0, NULL
);
386 if (matches(*argv
, "add") == 0)
387 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
388 if (matches(*argv
, "change") == 0)
389 return tc_filter_modify(RTM_NEWTFILTER
, 0, argc
-1, argv
+1);
390 if (matches(*argv
, "replace") == 0)
391 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_CREATE
, argc
-1, argv
+1);
392 if (matches(*argv
, "delete") == 0)
393 return tc_filter_modify(RTM_DELTFILTER
, 0, argc
-1, argv
+1);
395 if (matches(*argv
, "get") == 0)
396 return tc_filter_get(RTM_GETTFILTER
, 0, argc
-1, argv
+1);
398 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
399 || matches(*argv
, "lst") == 0)
400 return tc_filter_list(argc
-1, argv
+1);
401 if (matches(*argv
, "help") == 0) {
405 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv
);