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