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