]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc.c
Build atm and netem as shared libraries
[mirror_iproute2.git] / tc / tc.c
CommitLineData
aba5acdf
SH
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
33int show_stats = 0;
34int show_details = 0;
35int show_raw = 0;
36int resolve_hosts = 0;
3ea2bf45 37int use_iec = 0;
aba5acdf
SH
38
39void *BODY;
40static struct qdisc_util * qdisc_list;
41static struct filter_util * filter_list;
42
43static 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
50static 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
59static 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
68static 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#if 0
88/* Builtin filter types */
89
90static int f_parse_noopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
91{
92 if (argc || fhandle) {
93 fprintf(stderr, "Filter \"%s\" has no options.\n", qu->id);
94 return -1;
95 }
96 return 0;
97}
98#endif
99
4094db72 100struct qdisc_util *get_qdisc_kind(const char *str)
aba5acdf
SH
101{
102 void *dlh;
103 char buf[256];
104 struct qdisc_util *q;
105
106 for (q = qdisc_list; q; q = q->next)
107 if (strcmp(q->id, str) == 0)
108 return q;
109
110 snprintf(buf, sizeof(buf), "q_%s.so", str);
111 dlh = dlopen(buf, RTLD_LAZY);
112 if (dlh == NULL) {
113 dlh = BODY;
114 if (dlh == NULL) {
115 dlh = BODY = dlopen(NULL, RTLD_LAZY);
116 if (dlh == NULL)
117 goto noexist;
118 }
119 }
120
121 snprintf(buf, sizeof(buf), "%s_util", str);
122 q = dlsym(dlh, buf);
123 if (q == NULL)
124 goto noexist;
125
126reg:
127 q->next = qdisc_list;
128 qdisc_list = q;
129 return q;
130
131noexist:
132 q = malloc(sizeof(*q));
133 if (q) {
134 memset(q, 0, sizeof(*q));
135 strncpy(q->id, str, 15);
136 q->parse_qopt = parse_noqopt;
137 q->print_qopt = print_noqopt;
138 goto reg;
139 }
140 return q;
141}
142
143
4094db72 144struct filter_util *get_filter_kind(const char *str)
aba5acdf
SH
145{
146 void *dlh;
147 char buf[256];
148 struct filter_util *q;
149
150 for (q = filter_list; q; q = q->next)
151 if (strcmp(q->id, str) == 0)
152 return q;
153
154 snprintf(buf, sizeof(buf), "f_%s.so", str);
155 dlh = dlopen(buf, RTLD_LAZY);
156 if (dlh == NULL) {
157 dlh = BODY;
158 if (dlh == NULL) {
159 dlh = BODY = dlopen(NULL, RTLD_LAZY);
160 if (dlh == NULL)
161 goto noexist;
162 }
163 }
164
165 snprintf(buf, sizeof(buf), "%s_util", str);
166 q = dlsym(dlh, buf);
167 if (q == NULL)
168 goto noexist;
169
170reg:
171 q->next = filter_list;
172 filter_list = q;
173 return q;
174
175noexist:
176 q = malloc(sizeof(*q));
177 if (q) {
178 memset(q, 0, sizeof(*q));
179 strncpy(q->id, str, 15);
180 q->parse_fopt = parse_nofopt;
181 q->print_fopt = print_nofopt;
182 goto reg;
183 }
184 return q;
185}
186
187static void usage(void) __attribute__((noreturn));
188
189static void usage(void)
190{
191 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
192 "where OBJECT := { qdisc | class | filter }\n"
193 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
194 exit(-1);
195}
196
197
198
199int main(int argc, char **argv)
200{
201 char *basename;
202
203 basename = strrchr(argv[0], '/');
204 if (basename == NULL)
205 basename = argv[0];
206 else
207 basename++;
208
209
210 /* batch mode */
211 if (argc > 1 && matches(argv[1], "-batch") == 0) {
212 FILE *batch;
213 char line[400];
214 char *largv[100];
215 int largc, ret=0;
216#define BMAXARG (sizeof(largv)/sizeof(char *)-2)
217
218 if (argc != 3) {
219 fprintf(stderr, "Wrong number of arguments in batch mode\n");
220 exit(-1);
221 }
222 if (matches(argv[2], "-") != 0) {
223 if ((batch = fopen(argv[2], "r")) == NULL) {
224 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", argv[2], strerror(errno));
225 exit(-1);
226 }
227 } else {
228 if ((batch = fdopen(0, "r")) == NULL) {
229 fprintf(stderr, "Cannot open stdin for reading: %s=n", strerror(errno));
230 exit(-1);
231 }
232 }
233
234 tc_core_init();
235
236 while (fgets(line, sizeof(line)-1, batch)) {
237 if (line[strlen(line)-1]=='\n') {
238 line[strlen(line)-1] = '\0';
239 } else {
240 fprintf(stderr, "No newline at the end of line, looks like to long (%d chars or more)\n", strlen(line));
241 exit(-1);
242 }
243 largc = 0;
244 largv[largc]=strtok(line, " ");
245 while ((largv[++largc]=strtok(NULL, " ")) != NULL) {
246 if (largc > BMAXARG) {
247 fprintf(stderr, "Over %d arguments in batch mode, enough!\n", BMAXARG);
248 exit(-1);
249 }
250 }
251
252 if (matches(largv[0], "qdisc") == 0) {
253 ret += do_qdisc(largc-1, largv+1);
254 } else if (matches(largv[0], "class") == 0) {
255 ret += do_class(largc-1, largv+1);
256 } else if (matches(largv[0], "filter") == 0) {
257 ret += do_filter(largc-1, largv+1);
258 } else if (matches(largv[0], "help") == 0) {
259 usage(); /* note that usage() doesn't return */
260 } else {
261 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", largv[1]);
262 exit(-1);
263 }
264 }
265 fclose(batch);
266 exit(0); /* end of batch, that's all */
267 }
268
269 while (argc > 1) {
270 if (argv[1][0] != '-')
271 break;
272 if (matches(argv[1], "-stats") == 0 ||
3ea2bf45 273 matches(argv[1], "-statistics") == 0) {
aba5acdf
SH
274 ++show_stats;
275 } else if (matches(argv[1], "-details") == 0) {
276 ++show_details;
277 } else if (matches(argv[1], "-raw") == 0) {
278 ++show_raw;
279 } else if (matches(argv[1], "-Version") == 0) {
280 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
281 exit(0);
3ea2bf45
SH
282 } else if (matches(argv[1], "-iec") == 0) {
283 ++use_iec;
aba5acdf
SH
284 } else if (matches(argv[1], "-help") == 0) {
285 usage();
286 } else {
287 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
288 exit(-1);
289 }
290 argc--; argv++;
291 }
292
293 tc_core_init();
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], "help") == 0)
303 usage();
304 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", argv[1]);
305 exit(-1);
306 }
307
308 usage();
309}