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