]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_filter.c
iproute2: gnet_stats
[mirror_iproute2.git] / tc / tc_filter.c
1 /*
2 * tc_filter.c "tc filter".
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <net/if.h>
19 #include <net/if_arp.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24 #include <linux/if_ether.h>
25
26 #include "rt_names.h"
27 #include "utils.h"
28 #include "tc_util.h"
29 #include "tc_common.h"
30
31 static void usage(void) __attribute__((noreturn));
32
33 static void usage(void)
34 {
35 fprintf(stderr, "Usage: tc filter [ add | del | change | get ] dev STRING\n");
36 fprintf(stderr, " [ pref PRIO ] [ protocol PROTO ]\n");
37 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
38 fprintf(stderr, " [ root | classid CLASSID ] [ handle FILTERID ]\n");
39 fprintf(stderr, " [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n");
40 fprintf(stderr, "\n");
41 fprintf(stderr, " tc filter show [ dev STRING ] [ root | parent CLASSID ]\n");
42 fprintf(stderr, "Where:\n");
43 fprintf(stderr, "FILTER_TYPE := { rsvp | u32 | fw | route | etc. }\n");
44 fprintf(stderr, "FILTERID := ... format depends on classifier, see there\n");
45 fprintf(stderr, "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
46 exit(-1);
47 }
48
49
50 int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
51 {
52 struct rtnl_handle rth;
53 struct {
54 struct nlmsghdr n;
55 struct tcmsg t;
56 char buf[MAX_MSG];
57 } req;
58 struct filter_util *q = NULL;
59 __u32 prio = 0;
60 __u32 protocol = 0;
61 char *fhandle = NULL;
62 char d[16];
63 char k[16];
64 struct tc_estimator est;
65
66 memset(&req, 0, sizeof(req));
67 memset(&est, 0, sizeof(est));
68 memset(d, 0, sizeof(d));
69 memset(k, 0, sizeof(k));
70 memset(&req, 0, sizeof(req));
71
72 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
73 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
74 req.n.nlmsg_type = cmd;
75 req.t.tcm_family = AF_UNSPEC;
76
77 while (argc > 0) {
78 if (strcmp(*argv, "dev") == 0) {
79 NEXT_ARG();
80 if (d[0])
81 duparg("dev", *argv);
82 strncpy(d, *argv, sizeof(d)-1);
83 } else if (strcmp(*argv, "root") == 0) {
84 if (req.t.tcm_parent) {
85 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
86 exit(-1);
87 }
88 req.t.tcm_parent = TC_H_ROOT;
89 } else if (strcmp(*argv, "parent") == 0) {
90 __u32 handle;
91 NEXT_ARG();
92 if (req.t.tcm_parent)
93 duparg("parent", *argv);
94 if (get_tc_classid(&handle, *argv))
95 invarg(*argv, "Invalid parent ID");
96 req.t.tcm_parent = handle;
97 } else if (strcmp(*argv, "handle") == 0) {
98 NEXT_ARG();
99 if (fhandle)
100 duparg("handle", *argv);
101 fhandle = *argv;
102 } else if (matches(*argv, "preference") == 0 ||
103 matches(*argv, "priority") == 0) {
104 NEXT_ARG();
105 if (prio)
106 duparg("priority", *argv);
107 if (get_u32(&prio, *argv, 0))
108 invarg(*argv, "invalid prpriority value");
109 } else if (matches(*argv, "protocol") == 0) {
110 __u16 id;
111 NEXT_ARG();
112 if (protocol)
113 duparg("protocol", *argv);
114 if (ll_proto_a2n(&id, *argv))
115 invarg(*argv, "invalid protocol");
116 protocol = id;
117 } else if (matches(*argv, "estimator") == 0) {
118 if (parse_estimator(&argc, &argv, &est) < 0)
119 return -1;
120 } else if (matches(*argv, "help") == 0) {
121 usage();
122 } else {
123 strncpy(k, *argv, sizeof(k)-1);
124
125 q = get_filter_kind(k);
126 argc--; argv++;
127 break;
128 }
129
130 argc--; argv++;
131 }
132
133 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
134
135 if (k[0])
136 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
137
138 if (q) {
139 if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
140 exit(1);
141 } else {
142 if (fhandle) {
143 fprintf(stderr, "Must specify filter type when using "
144 "\"handle\"\n");
145 exit(-1);
146 }
147 if (argc) {
148 if (matches(*argv, "help") == 0)
149 usage();
150 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv);
151 exit(-1);
152 }
153 }
154 if (est.ewma_log)
155 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
156
157
158 if (rtnl_open(&rth, 0) < 0) {
159 fprintf(stderr, "Cannot open rtnetlink\n");
160 exit(1);
161 }
162
163 if (d[0]) {
164 ll_init_map(&rth);
165
166 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
167 fprintf(stderr, "Cannot find device \"%s\"\n", d);
168 rtnl_close(&rth);
169 exit(1);
170 }
171 }
172
173 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
174 fprintf(stderr, "We have an error talking to the kernel\n");
175 rtnl_close(&rth);
176 exit(2);
177 }
178
179 rtnl_close(&rth);
180 return 0;
181 }
182
183 static __u32 filter_parent;
184 static int filter_ifindex;
185 static __u32 filter_prio;
186 static __u32 filter_protocol;
187
188 static int print_filter(const struct sockaddr_nl *who,
189 struct nlmsghdr *n,
190 void *arg)
191 {
192 FILE *fp = (FILE*)arg;
193 struct tcmsg *t = NLMSG_DATA(n);
194 int len = n->nlmsg_len;
195 struct rtattr * tb[TCA_MAX+1];
196 struct filter_util *q;
197 char abuf[256];
198
199 if (n->nlmsg_type != RTM_NEWTFILTER && n->nlmsg_type != RTM_DELTFILTER) {
200 fprintf(stderr, "Not a filter\n");
201 return 0;
202 }
203 len -= NLMSG_LENGTH(sizeof(*t));
204 if (len < 0) {
205 fprintf(stderr, "Wrong len %d\n", len);
206 return -1;
207 }
208
209 memset(tb, 0, sizeof(tb));
210 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
211
212 if (tb[TCA_KIND] == NULL) {
213 fprintf(stderr, "print_filter: NULL kind\n");
214 return -1;
215 }
216
217 if (n->nlmsg_type == RTM_DELTFILTER)
218 fprintf(fp, "deleted ");
219
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));
223
224 if (!filter_parent || filter_parent != t->tcm_parent) {
225 if (t->tcm_parent == TC_H_ROOT)
226 fprintf(fp, "root ");
227 else {
228 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
229 fprintf(fp, "parent %s ", abuf);
230 }
231 }
232 if (t->tcm_info) {
233 __u32 protocol = TC_H_MIN(t->tcm_info);
234 __u32 prio = TC_H_MAJ(t->tcm_info)>>16;
235 if (!filter_protocol || filter_protocol != protocol) {
236 if (protocol) {
237 SPRINT_BUF(b1);
238 fprintf(fp, "protocol %s ",
239 ll_proto_n2a(protocol, b1, sizeof(b1)));
240 }
241 }
242 if (!filter_prio || filter_prio != prio) {
243 if (prio)
244 fprintf(fp, "pref %u ", prio);
245 }
246 }
247 fprintf(fp, "%s ", (char*)RTA_DATA(tb[TCA_KIND]));
248 q = get_filter_kind(RTA_DATA(tb[TCA_KIND]));
249 if (tb[TCA_OPTIONS]) {
250 if (q)
251 q->print_fopt(q, fp, tb[TCA_OPTIONS], t->tcm_handle);
252 else
253 fprintf(fp, "[cannot parse parameters]");
254 }
255 fprintf(fp, "\n");
256
257 if (show_stats && (tb[TCA_STATS] || tb[TCA_STATS2])) {
258 print_tcstats_attr(fp, tb, " ", NULL);
259 fprintf(fp, "\n");
260 }
261
262 fflush(fp);
263 return 0;
264 }
265
266
267 int tc_filter_list(int argc, char **argv)
268 {
269 struct tcmsg t;
270 struct rtnl_handle rth;
271 char d[16];
272 __u32 prio = 0;
273 __u32 protocol = 0;
274 char *fhandle = NULL;
275
276 memset(&t, 0, sizeof(t));
277 t.tcm_family = AF_UNSPEC;
278 memset(d, 0, sizeof(d));
279
280 while (argc > 0) {
281 if (strcmp(*argv, "dev") == 0) {
282 NEXT_ARG();
283 if (d[0])
284 duparg("dev", *argv);
285 strncpy(d, *argv, sizeof(d)-1);
286 } else if (strcmp(*argv, "root") == 0) {
287 if (t.tcm_parent) {
288 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
289 exit(-1);
290 }
291 filter_parent = t.tcm_parent = TC_H_ROOT;
292 } else if (strcmp(*argv, "parent") == 0) {
293 __u32 handle;
294 NEXT_ARG();
295 if (t.tcm_parent)
296 duparg("parent", *argv);
297 if (get_tc_classid(&handle, *argv))
298 invarg(*argv, "invalid parent ID");
299 filter_parent = t.tcm_parent = handle;
300 } else if (strcmp(*argv, "handle") == 0) {
301 NEXT_ARG();
302 if (fhandle)
303 duparg("handle", *argv);
304 fhandle = *argv;
305 } else if (matches(*argv, "preference") == 0 ||
306 matches(*argv, "priority") == 0) {
307 NEXT_ARG();
308 if (prio)
309 duparg("priority", *argv);
310 if (get_u32(&prio, *argv, 0))
311 invarg(*argv, "invalid preference");
312 filter_prio = prio;
313 } else if (matches(*argv, "protocol") == 0) {
314 __u16 res;
315 NEXT_ARG();
316 if (protocol)
317 duparg("protocol", *argv);
318 if (ll_proto_a2n(&res, *argv))
319 invarg(*argv, "invalid protocol");
320 protocol = res;
321 filter_protocol = protocol;
322 } else if (matches(*argv, "help") == 0) {
323 usage();
324 } else {
325 fprintf(stderr, " What is \"%s\"? Try \"tc filter help\"\n", *argv);
326 exit(-1);
327 }
328
329 argc--; argv++;
330 }
331
332 t.tcm_info = TC_H_MAKE(prio<<16, protocol);
333
334 if (rtnl_open(&rth, 0) < 0) {
335 fprintf(stderr, "Cannot open rtnetlink\n");
336 exit(1);
337 }
338
339 ll_init_map(&rth);
340
341 if (d[0]) {
342 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
343 fprintf(stderr, "Cannot find device \"%s\"\n", d);
344 rtnl_close(&rth);
345 exit(1);
346 }
347 filter_ifindex = t.tcm_ifindex;
348 }
349
350 if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
351 perror("Cannot send dump request");
352 rtnl_close(&rth);
353 exit(1);
354 }
355
356 if (rtnl_dump_filter(&rth, print_filter, stdout, NULL, NULL) < 0) {
357 fprintf(stderr, "Dump terminated\n");
358 exit(1);
359 }
360
361 rtnl_close(&rth);
362 return 0;
363 }
364
365 int do_filter(int argc, char **argv)
366 {
367 if (argc < 1)
368 return tc_filter_list(0, NULL);
369 if (matches(*argv, "add") == 0)
370 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
371 if (matches(*argv, "change") == 0)
372 return tc_filter_modify(RTM_NEWTFILTER, 0, argc-1, argv+1);
373 if (matches(*argv, "replace") == 0)
374 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_CREATE, argc-1, argv+1);
375 if (matches(*argv, "delete") == 0)
376 return tc_filter_modify(RTM_DELTFILTER, 0, argc-1, argv+1);
377 #if 0
378 if (matches(*argv, "get") == 0)
379 return tc_filter_get(RTM_GETTFILTER, 0, argc-1, argv+1);
380 #endif
381 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
382 || matches(*argv, "lst") == 0)
383 return tc_filter_list(argc-1, argv+1);
384 if (matches(*argv, "help") == 0)
385 usage();
386 fprintf(stderr, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv);
387 exit(-1);
388 }
389