]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_filter.c
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 int flags
, int argc
, char **argv
)
51 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
52 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
54 .t
.tcm_family
= AF_UNSPEC
,
56 struct filter_util
*q
= NULL
;
63 struct tc_estimator est
= {};
65 if (cmd
== RTM_NEWTFILTER
&& flags
& NLM_F_CREATE
)
66 protocol
= htons(ETH_P_ALL
);
69 if (strcmp(*argv
, "dev") == 0) {
73 strncpy(d
, *argv
, sizeof(d
)-1);
74 } else if (strcmp(*argv
, "root") == 0) {
75 if (req
.t
.tcm_parent
) {
76 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
79 req
.t
.tcm_parent
= TC_H_ROOT
;
80 } else if (strcmp(*argv
, "ingress") == 0) {
81 if (req
.t
.tcm_parent
) {
82 fprintf(stderr
, "Error: \"ingress\" is duplicate parent ID\n");
85 req
.t
.tcm_parent
= TC_H_MAKE(TC_H_CLSACT
,
87 } else if (strcmp(*argv
, "egress") == 0) {
88 if (req
.t
.tcm_parent
) {
89 fprintf(stderr
, "Error: \"egress\" is duplicate parent ID\n");
92 req
.t
.tcm_parent
= TC_H_MAKE(TC_H_CLSACT
,
94 } else if (strcmp(*argv
, "parent") == 0) {
99 duparg("parent", *argv
);
100 if (get_tc_classid(&handle
, *argv
))
101 invarg("Invalid parent ID", *argv
);
102 req
.t
.tcm_parent
= handle
;
103 } else if (strcmp(*argv
, "handle") == 0) {
106 duparg("handle", *argv
);
108 } else if (matches(*argv
, "preference") == 0 ||
109 matches(*argv
, "priority") == 0) {
112 duparg("priority", *argv
);
113 if (get_u32(&prio
, *argv
, 0) || prio
> 0xFFFF)
114 invarg("invalid priority value", *argv
);
115 } else if (matches(*argv
, "protocol") == 0) {
120 duparg("protocol", *argv
);
121 if (ll_proto_a2n(&id
, *argv
))
122 invarg("invalid protocol", *argv
);
125 } else if (matches(*argv
, "estimator") == 0) {
126 if (parse_estimator(&argc
, &argv
, &est
) < 0)
128 } else if (matches(*argv
, "help") == 0) {
132 strncpy(k
, *argv
, sizeof(k
)-1);
134 q
= get_filter_kind(k
);
142 req
.t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
145 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
148 if (q
->parse_fopt(q
, fhandle
, argc
, argv
, &req
.n
))
152 fprintf(stderr
, "Must specify filter type when using \"handle\"\n");
156 if (matches(*argv
, "help") == 0)
158 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv
);
163 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
169 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
170 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
175 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0) {
176 fprintf(stderr
, "We have an error talking to the kernel\n");
183 static __u32 filter_parent
;
184 static int filter_ifindex
;
185 static __u32 filter_prio
;
186 static __u32 filter_protocol
;
189 int print_filter(const struct sockaddr_nl
*who
,
193 FILE *fp
= (FILE *)arg
;
194 struct tcmsg
*t
= NLMSG_DATA(n
);
195 int len
= n
->nlmsg_len
;
196 struct rtattr
*tb
[TCA_MAX
+1];
197 struct filter_util
*q
;
200 if (n
->nlmsg_type
!= RTM_NEWTFILTER
&& n
->nlmsg_type
!= RTM_DELTFILTER
) {
201 fprintf(stderr
, "Not a filter\n");
204 len
-= NLMSG_LENGTH(sizeof(*t
));
206 fprintf(stderr
, "Wrong len %d\n", len
);
210 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
212 if (tb
[TCA_KIND
] == NULL
) {
213 fprintf(stderr
, "print_filter: NULL kind\n");
217 if (n
->nlmsg_type
== RTM_DELTFILTER
)
218 fprintf(fp
, "deleted ");
220 fprintf(fp
, "filter ");
221 if (!filter_ifindex
|| filter_ifindex
!= t
->tcm_ifindex
)
222 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
224 if (!filter_parent
|| filter_parent
!= t
->tcm_parent
) {
225 if (t
->tcm_parent
== TC_H_ROOT
)
226 fprintf(fp
, "root ");
227 else if (t
->tcm_parent
== TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_INGRESS
))
228 fprintf(fp
, "ingress ");
229 else if (t
->tcm_parent
== TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_EGRESS
))
230 fprintf(fp
, "egress ");
232 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
233 fprintf(fp
, "parent %s ", abuf
);
238 f_proto
= TC_H_MIN(t
->tcm_info
);
239 __u32 prio
= TC_H_MAJ(t
->tcm_info
)>>16;
241 if (!filter_protocol
|| filter_protocol
!= f_proto
) {
244 fprintf(fp
, "protocol %s ",
245 ll_proto_n2a(f_proto
, b1
, sizeof(b1
)));
248 if (!filter_prio
|| filter_prio
!= prio
) {
250 fprintf(fp
, "pref %u ", prio
);
253 fprintf(fp
, "%s ", rta_getattr_str(tb
[TCA_KIND
]));
254 q
= get_filter_kind(RTA_DATA(tb
[TCA_KIND
]));
255 if (tb
[TCA_OPTIONS
]) {
257 q
->print_fopt(q
, fp
, tb
[TCA_OPTIONS
], t
->tcm_handle
);
259 fprintf(fp
, "[cannot parse parameters]");
263 if (show_stats
&& (tb
[TCA_STATS
] || tb
[TCA_STATS2
])) {
264 print_tcstats_attr(fp
, tb
, " ", NULL
);
272 static int tc_filter_list(int argc
, char **argv
)
274 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
278 char *fhandle
= NULL
;
281 if (strcmp(*argv
, "dev") == 0) {
284 duparg("dev", *argv
);
285 strncpy(d
, *argv
, sizeof(d
)-1);
286 } else if (strcmp(*argv
, "root") == 0) {
288 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
291 filter_parent
= t
.tcm_parent
= TC_H_ROOT
;
292 } else if (strcmp(*argv
, "ingress") == 0) {
294 fprintf(stderr
, "Error: \"ingress\" is duplicate parent ID\n");
297 filter_parent
= TC_H_MAKE(TC_H_CLSACT
,
299 t
.tcm_parent
= filter_parent
;
300 } else if (strcmp(*argv
, "egress") == 0) {
302 fprintf(stderr
, "Error: \"egress\" is duplicate parent ID\n");
305 filter_parent
= TC_H_MAKE(TC_H_CLSACT
,
307 t
.tcm_parent
= filter_parent
;
308 } else if (strcmp(*argv
, "parent") == 0) {
313 duparg("parent", *argv
);
314 if (get_tc_classid(&handle
, *argv
))
315 invarg("invalid parent ID", *argv
);
316 filter_parent
= t
.tcm_parent
= handle
;
317 } else if (strcmp(*argv
, "handle") == 0) {
320 duparg("handle", *argv
);
322 } else if (matches(*argv
, "preference") == 0 ||
323 matches(*argv
, "priority") == 0) {
326 duparg("priority", *argv
);
327 if (get_u32(&prio
, *argv
, 0))
328 invarg("invalid preference", *argv
);
330 } else if (matches(*argv
, "protocol") == 0) {
335 duparg("protocol", *argv
);
336 if (ll_proto_a2n(&res
, *argv
))
337 invarg("invalid protocol", *argv
);
339 filter_protocol
= protocol
;
340 } else if (matches(*argv
, "help") == 0) {
343 fprintf(stderr
, " What is \"%s\"? Try \"tc filter help\"\n", *argv
);
350 t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
355 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
356 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
359 filter_ifindex
= t
.tcm_ifindex
;
362 if (rtnl_dump_request(&rth
, RTM_GETTFILTER
, &t
, sizeof(t
)) < 0) {
363 perror("Cannot send dump request");
367 if (rtnl_dump_filter(&rth
, print_filter
, stdout
) < 0) {
368 fprintf(stderr
, "Dump terminated\n");
375 int do_filter(int argc
, char **argv
)
378 return tc_filter_list(0, NULL
);
379 if (matches(*argv
, "add") == 0)
380 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
381 if (matches(*argv
, "change") == 0)
382 return tc_filter_modify(RTM_NEWTFILTER
, 0, argc
-1, argv
+1);
383 if (matches(*argv
, "replace") == 0)
384 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_CREATE
, argc
-1, argv
+1);
385 if (matches(*argv
, "delete") == 0)
386 return tc_filter_modify(RTM_DELTFILTER
, 0, argc
-1, argv
+1);
388 if (matches(*argv
, "get") == 0)
389 return tc_filter_get(RTM_GETTFILTER
, 0, argc
-1, argv
+1);
391 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
392 || matches(*argv
, "lst") == 0)
393 return tc_filter_list(argc
-1, argv
+1);
394 if (matches(*argv
, "help") == 0) {
398 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv
);