]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
5a98d4ffe39094482d82ae71205fd8ff6d5209eb
[mirror_iproute2.git] / tc / tc.c
1 /*
2 * tc.c "tc" utility frontend.
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 * Fixes:
12 *
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <fcntl.h>
21 #include <dlfcn.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "SNAPSHOT.h"
29 #include "utils.h"
30 #include "tc_util.h"
31 #include "tc_common.h"
32
33 int show_stats = 0;
34 int show_details = 0;
35 int show_raw = 0;
36 int resolve_hosts = 0;
37 int use_iec = 0;
38 struct rtnl_handle rth;
39
40 static void *BODY; /* cached handle dlopen(NULL) */
41 static struct qdisc_util * qdisc_list;
42 static struct filter_util * filter_list;
43
44 static int print_noqopt(struct qdisc_util *qu, FILE *f,
45 struct rtattr *opt)
46 {
47 if (opt && RTA_PAYLOAD(opt))
48 fprintf(f, "[Unknown qdisc, optlen=%u] ",
49 (unsigned) RTA_PAYLOAD(opt));
50 return 0;
51 }
52
53 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
54 {
55 if (argc) {
56 fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
57 return -1;
58 }
59 return 0;
60 }
61
62 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
63 {
64 if (opt && RTA_PAYLOAD(opt))
65 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
66 fhandle, (unsigned) RTA_PAYLOAD(opt));
67 else if (fhandle)
68 fprintf(f, "fh %08x ", fhandle);
69 return 0;
70 }
71
72 static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
73 {
74 __u32 handle;
75
76 if (argc) {
77 fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
78 return -1;
79 }
80 if (fhandle) {
81 struct tcmsg *t = NLMSG_DATA(n);
82 if (get_u32(&handle, fhandle, 16)) {
83 fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
84 return -1;
85 }
86 t->tcm_handle = handle;
87 }
88 return 0;
89 }
90
91 struct qdisc_util *get_qdisc_kind(const char *str)
92 {
93 void *dlh;
94 char buf[256];
95 struct qdisc_util *q;
96
97 for (q = qdisc_list; q; q = q->next)
98 if (strcmp(q->id, str) == 0)
99 return q;
100
101 snprintf(buf, sizeof(buf), "/usr/lib/tc/q_%s.so", str);
102 dlh = dlopen(buf, RTLD_LAZY);
103 if (!dlh) {
104 /* look in current binary, only open once */
105 dlh = BODY;
106 if (dlh == NULL) {
107 dlh = BODY = dlopen(NULL, RTLD_LAZY);
108 if (dlh == NULL)
109 goto noexist;
110 }
111 }
112
113 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
114 q = dlsym(dlh, buf);
115 if (q == NULL)
116 goto noexist;
117
118 reg:
119 q->next = qdisc_list;
120 qdisc_list = q;
121 return q;
122
123 noexist:
124 q = malloc(sizeof(*q));
125 if (q) {
126
127 memset(q, 0, sizeof(*q));
128 q->id = strcpy(malloc(strlen(str)+1), str);
129 q->parse_qopt = parse_noqopt;
130 q->print_qopt = print_noqopt;
131 goto reg;
132 }
133 return q;
134 }
135
136
137 struct filter_util *get_filter_kind(const char *str)
138 {
139 void *dlh;
140 char buf[256];
141 struct filter_util *q;
142
143 for (q = filter_list; q; q = q->next)
144 if (strcmp(q->id, str) == 0)
145 return q;
146
147 snprintf(buf, sizeof(buf), "/usr/lib/tc/f_%s.so", str);
148 dlh = dlopen(buf, RTLD_LAZY);
149 if (dlh == NULL) {
150 dlh = BODY;
151 if (dlh == NULL) {
152 dlh = BODY = dlopen(NULL, RTLD_LAZY);
153 if (dlh == NULL)
154 goto noexist;
155 }
156 }
157
158 snprintf(buf, sizeof(buf), "%s_filter_util", str);
159 q = dlsym(dlh, buf);
160 if (q == NULL)
161 goto noexist;
162
163 reg:
164 q->next = filter_list;
165 filter_list = q;
166 return q;
167 noexist:
168 q = malloc(sizeof(*q));
169 if (q) {
170 memset(q, 0, sizeof(*q));
171 strncpy(q->id, str, 15);
172 q->parse_fopt = parse_nofopt;
173 q->print_fopt = print_nofopt;
174 goto reg;
175 }
176 return q;
177 }
178
179 static int usage(void)
180 {
181 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
182 "where OBJECT := { qdisc | class | filter | action }\n"
183 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
184 return -1;
185 }
186
187
188
189 int main(int argc, char **argv)
190 {
191 /* batch mode */
192 if (argc > 1 && matches(argv[1], "-batch") == 0) {
193 FILE *batch;
194 char line[400];
195 char *largv[100];
196 int largc, ret=0;
197 #define BMAXARG (sizeof(largv)/sizeof(char *)-2)
198
199 if (argc != 3) {
200 fprintf(stderr, "Wrong number of arguments in batch mode\n");
201 return -1;
202 }
203 if (matches(argv[2], "-") != 0) {
204 if ((batch = fopen(argv[2], "r")) == NULL) {
205 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", argv[2], strerror(errno));
206 return -1;
207 }
208 } else {
209 if ((batch = fdopen(0, "r")) == NULL) {
210 fprintf(stderr, "Cannot open stdin for reading: %s=n", strerror(errno));
211 return -1;
212 }
213 }
214
215 tc_core_init();
216 if (rtnl_open(&rth, 0) < 0) {
217 fprintf(stderr, "Cannot open rtnetlink\n");
218 exit(1);
219 }
220
221 while (fgets(line, sizeof(line)-1, batch)) {
222 if (line[strlen(line)-1]=='\n') {
223 line[strlen(line)-1] = '\0';
224 } else {
225 fprintf(stderr, "No newline at the end of line, looks like to long (%d chars or more)\n",
226 (int) strlen(line));
227 return -1;
228 }
229 largc = 0;
230 largv[largc]=strtok(line, " ");
231 if (largv[largc]==NULL)
232 continue;
233 while ((largv[++largc]=strtok(NULL, " ")) != NULL) {
234 if (largc > BMAXARG) {
235 fprintf(stderr, "Over %d arguments in batch mode, enough!\n",
236 (int) BMAXARG);
237 return -1;
238 }
239 }
240
241 if (largv[0][0]=='#')
242 continue;
243
244 if (matches(largv[0], "qdisc") == 0) {
245 ret += do_qdisc(largc-1, largv+1);
246 } else if (matches(largv[0], "class") == 0) {
247 ret += do_class(largc-1, largv+1);
248 } else if (matches(largv[0], "filter") == 0) {
249 ret += do_filter(largc-1, largv+1);
250 } else if (matches(largv[0], "action") == 0) {
251 ret += do_action(largc-1, largv+1);
252 } else if (matches(largv[0], "help") == 0) {
253 usage(); /* note that usage() doesn't return */
254 } else {
255 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", largv[1]);
256 }
257 }
258 fclose(batch);
259
260 rtnl_close(&rth);
261
262 return 0; /* end of batch, that's all */
263 }
264
265 while (argc > 1) {
266 if (argv[1][0] != '-')
267 break;
268 if (matches(argv[1], "-stats") == 0 ||
269 matches(argv[1], "-statistics") == 0) {
270 ++show_stats;
271 } else if (matches(argv[1], "-details") == 0) {
272 ++show_details;
273 } else if (matches(argv[1], "-raw") == 0) {
274 ++show_raw;
275 } else if (matches(argv[1], "-Version") == 0) {
276 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
277 return 0;
278 } else if (matches(argv[1], "-iec") == 0) {
279 ++use_iec;
280 } else if (matches(argv[1], "-help") == 0) {
281 usage();
282 } else {
283 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
284 return -1;
285 }
286 argc--; argv++;
287 }
288
289 tc_core_init();
290 if (rtnl_open(&rth, 0) < 0) {
291 fprintf(stderr, "Cannot open rtnetlink\n");
292 exit(1);
293 }
294
295 if (argc > 1) {
296 if (matches(argv[1], "qdisc") == 0)
297 return do_qdisc(argc-2, argv+2);
298 if (matches(argv[1], "class") == 0)
299 return do_class(argc-2, argv+2);
300 if (matches(argv[1], "filter") == 0)
301 return do_filter(argc-2, argv+2);
302 if (matches(argv[1], "actions") == 0)
303 return do_action(argc-2, argv+2);
304 if (matches(argv[1], "help") == 0)
305 usage();
306 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", argv[1]);
307 return -1;
308 }
309
310 rtnl_close(&rth);
311 usage();
312 return 0;
313 }