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