]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc.c
Cleanup GCC4 warnings about signedness.
[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;
08856f02 38int force = 0;
7901660a 39struct rtnl_handle rth;
aba5acdf 40
b7a45150 41static void *BODY; /* cached handle dlopen(NULL) */
aba5acdf
SH
42static struct qdisc_util * qdisc_list;
43static struct filter_util * filter_list;
44
1798c9d5
SH
45static int print_noqopt(struct qdisc_util *qu, FILE *f,
46 struct rtattr *opt)
aba5acdf
SH
47{
48 if (opt && RTA_PAYLOAD(opt))
bb6a21a4
SH
49 fprintf(f, "[Unknown qdisc, optlen=%u] ",
50 (unsigned) RTA_PAYLOAD(opt));
aba5acdf
SH
51 return 0;
52}
53
54static 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
63static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
64{
65 if (opt && RTA_PAYLOAD(opt))
bb6a21a4
SH
66 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
67 fhandle, (unsigned) RTA_PAYLOAD(opt));
aba5acdf
SH
68 else if (fhandle)
69 fprintf(f, "fh %08x ", fhandle);
70 return 0;
71}
72
73static 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
4094db72 92struct qdisc_util *get_qdisc_kind(const char *str)
aba5acdf
SH
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
b7a45150 102 snprintf(buf, sizeof(buf), "/usr/lib/tc/q_%s.so", str);
aba5acdf 103 dlh = dlopen(buf, RTLD_LAZY);
b7a45150
SH
104 if (!dlh) {
105 /* look in current binary, only open once */
aba5acdf
SH
106 dlh = BODY;
107 if (dlh == NULL) {
108 dlh = BODY = dlopen(NULL, RTLD_LAZY);
109 if (dlh == NULL)
110 goto noexist;
111 }
112 }
113
95812b56 114 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
aba5acdf
SH
115 q = dlsym(dlh, buf);
116 if (q == NULL)
117 goto noexist;
118
119reg:
120 q->next = qdisc_list;
121 qdisc_list = q;
122 return q;
123
124noexist:
125 q = malloc(sizeof(*q));
126 if (q) {
1798c9d5 127
aba5acdf 128 memset(q, 0, sizeof(*q));
1798c9d5 129 q->id = strcpy(malloc(strlen(str)+1), str);
aba5acdf
SH
130 q->parse_qopt = parse_noqopt;
131 q->print_qopt = print_noqopt;
132 goto reg;
133 }
134 return q;
135}
136
137
4094db72 138struct filter_util *get_filter_kind(const char *str)
aba5acdf
SH
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
b7a45150 148 snprintf(buf, sizeof(buf), "/usr/lib/tc/f_%s.so", str);
aba5acdf
SH
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
95812b56 159 snprintf(buf, sizeof(buf), "%s_filter_util", str);
aba5acdf
SH
160 q = dlsym(dlh, buf);
161 if (q == NULL)
162 goto noexist;
163
164reg:
165 q->next = filter_list;
166 filter_list = q;
167 return q;
aba5acdf
SH
168noexist:
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
c2f3a0f9 180static void usage(void)
aba5acdf
SH
181{
182 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
2373fde9 183 "where OBJECT := { qdisc | class | filter | action }\n"
dd3e9085 184 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] [file] }\n");
c2f3a0f9 185}
186
187static int do_cmd(int argc, char **argv)
188{
189 if (matches(*argv, "qdisc") == 0)
190 return do_qdisc(argc-1, argv+1);
191
192 if (matches(*argv, "class") == 0)
193 return do_class(argc-1, argv+1);
194
195 if (matches(*argv, "filter") == 0)
196 return do_filter(argc-1, argv+1);
197
198 if (matches(*argv, "actions") == 0)
199 return do_action(argc-1, argv+1);
200
201 if (matches(*argv, "help") == 0) {
202 usage();
203 return 0;
204 }
205
206 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
207 *argv);
044ebf35 208 return -1;
aba5acdf
SH
209}
210
08856f02 211/* split command line into argument vector */
c2f3a0f9 212static int makeargs(char *line, char *argv[], int maxargs)
213{
214 static const char ws[] = " \t\r\n";
215 char *cp;
216 int argc = 0;
aba5acdf 217
c2f3a0f9 218 for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) {
dd3e9085 219 if (argc >= (maxargs - 1)) {
c2f3a0f9 220 fprintf(stderr, "Too many arguments to command\n");
221 exit(1);
222 }
223 argv[argc++] = cp;
224 }
225 argv[argc] = NULL;
aba5acdf 226
c2f3a0f9 227 return argc;
228}
229
08856f02
SH
230static int lineno;
231/* Like glibc getline but handle continuation lines and comments */
232static size_t getcmdline(char **linep, size_t *lenp, FILE *in)
233{
234 size_t cc;
235 char *cp;
236
dd3e9085 237 if ((cc = getline(linep, lenp, in)) < 0)
08856f02
SH
238 return cc; /* eof or error */
239 ++lineno;
240
241 cp = strchr(*linep, '#');
242 if (cp)
243 *cp = '\0';
244
245 while ((cp = strstr(*linep, "\\\n")) != NULL) {
246 char *line1 = NULL;
f332d169 247 size_t len1 = 0;
08856f02
SH
248 size_t cc1;
249
250 if ((cc1 = getline(&line1, &len1, in)) < 0) {
251 fprintf(stderr, "Missing continuation line\n");
252 return cc1;
253 }
254
255 ++lineno;
256 *cp = 0;
257
258 cp = strchr(line1, '#');
259 if (cp)
260 *cp = '\0';
261
262 *linep = realloc(*linep, strlen(*linep) + strlen(line1) + 1);
263 if (!*linep) {
264 fprintf(stderr, "Out of memory\n");
265 return -1;
266 }
267 cc += cc1 - 2;
268 strcat(*linep, line1);
269 free(line1);
270 }
271 return cc;
272}
273
c2f3a0f9 274static int batch(const char *name)
aba5acdf 275{
c2f3a0f9 276 char *line = NULL;
277 size_t len = 0;
08856f02 278 int ret = 0;
c2f3a0f9 279
dd3e9085 280 if (name && strcmp(name, "-") != 0) {
c2f3a0f9 281 if (freopen(name, "r", stdin) == NULL) {
282 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n",
283 name, strerror(errno));
044ebf35 284 return -1;
aba5acdf 285 }
c2f3a0f9 286 }
287
288 tc_core_init();
289
290 if (rtnl_open(&rth, 0) < 0) {
291 fprintf(stderr, "Cannot open rtnetlink\n");
292 return -1;
293 }
294
08856f02
SH
295 lineno = 0;
296 while (getcmdline(&line, &len, stdin) != -1) {
297 char *largv[100];
298 int largc;
aba5acdf 299
c2f3a0f9 300 largc = makeargs(line, largv, 100);
08856f02
SH
301 if (largc == 0)
302 continue; /* blank line */
aba5acdf 303
08856f02 304 if (do_cmd(largc, largv)) {
c2f3a0f9 305 fprintf(stderr, "Command failed %s:%d\n", name, lineno);
08856f02
SH
306 ret = 1;
307 if (!force)
308 break;
aba5acdf 309 }
c2f3a0f9 310 }
7901660a 311
c2f3a0f9 312 rtnl_close(&rth);
313 return ret;
314}
7901660a 315
c2f3a0f9 316
317int main(int argc, char **argv)
318{
319 int ret;
dd3e9085 320 int do_batching = 0;
08856f02 321 char *batchfile = NULL;
aba5acdf
SH
322
323 while (argc > 1) {
324 if (argv[1][0] != '-')
325 break;
326 if (matches(argv[1], "-stats") == 0 ||
3ea2bf45 327 matches(argv[1], "-statistics") == 0) {
aba5acdf
SH
328 ++show_stats;
329 } else if (matches(argv[1], "-details") == 0) {
330 ++show_details;
331 } else if (matches(argv[1], "-raw") == 0) {
332 ++show_raw;
333 } else if (matches(argv[1], "-Version") == 0) {
334 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
044ebf35 335 return 0;
3ea2bf45
SH
336 } else if (matches(argv[1], "-iec") == 0) {
337 ++use_iec;
aba5acdf
SH
338 } else if (matches(argv[1], "-help") == 0) {
339 usage();
c2f3a0f9 340 return 0;
08856f02
SH
341 } else if (matches(argv[1], "-force") == 0) {
342 ++force;
c2f3a0f9 343 } else if (matches(argv[1], "-batch") == 0) {
dd3e9085
SH
344 do_batching = 1;
345 if (argc > 2)
346 batchfile = argv[2];
08856f02 347 argc--; argv++;
aba5acdf
SH
348 } else {
349 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
044ebf35 350 return -1;
aba5acdf
SH
351 }
352 argc--; argv++;
353 }
354
dd3e9085 355 if (do_batching)
08856f02
SH
356 return batch(batchfile);
357
c2f3a0f9 358 if (argc <= 1) {
359 usage();
360 return 0;
361 }
362
aba5acdf 363 tc_core_init();
7901660a
SH
364 if (rtnl_open(&rth, 0) < 0) {
365 fprintf(stderr, "Cannot open rtnetlink\n");
366 exit(1);
367 }
aba5acdf 368
c2f3a0f9 369 ret = do_cmd(argc-1, argv+1);
7901660a 370 rtnl_close(&rth);
c2f3a0f9 371
372 return ret;
aba5acdf 373}