]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
tc class: Show classes as ASCII graph
[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 show_graph = 0;
38
39 int batch_mode = 0;
40 int resolve_hosts = 0;
41 int use_iec = 0;
42 int force = 0;
43 struct rtnl_handle rth;
44
45 static void *BODY = NULL; /* cached handle dlopen(NULL) */
46 static struct qdisc_util * qdisc_list;
47 static struct filter_util * filter_list;
48
49 static int print_noqopt(struct qdisc_util *qu, FILE *f,
50 struct rtattr *opt)
51 {
52 if (opt && RTA_PAYLOAD(opt))
53 fprintf(f, "[Unknown qdisc, optlen=%u] ",
54 (unsigned) RTA_PAYLOAD(opt));
55 return 0;
56 }
57
58 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
59 {
60 if (argc) {
61 fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
62 return -1;
63 }
64 return 0;
65 }
66
67 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
68 {
69 if (opt && RTA_PAYLOAD(opt))
70 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
71 fhandle, (unsigned) RTA_PAYLOAD(opt));
72 else if (fhandle)
73 fprintf(f, "fh %08x ", fhandle);
74 return 0;
75 }
76
77 static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
78 {
79 __u32 handle;
80
81 if (argc) {
82 fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
83 return -1;
84 }
85 if (fhandle) {
86 struct tcmsg *t = NLMSG_DATA(n);
87 if (get_u32(&handle, fhandle, 16)) {
88 fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
89 return -1;
90 }
91 t->tcm_handle = handle;
92 }
93 return 0;
94 }
95
96 struct qdisc_util *get_qdisc_kind(const char *str)
97 {
98 void *dlh;
99 char buf[256];
100 struct qdisc_util *q;
101
102 for (q = qdisc_list; q; q = q->next)
103 if (strcmp(q->id, str) == 0)
104 return q;
105
106 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
107 dlh = dlopen(buf, RTLD_LAZY);
108 if (!dlh) {
109 /* look in current binary, only open once */
110 dlh = BODY;
111 if (dlh == NULL) {
112 dlh = BODY = dlopen(NULL, RTLD_LAZY);
113 if (dlh == NULL)
114 goto noexist;
115 }
116 }
117
118 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
119 q = dlsym(dlh, buf);
120 if (q == NULL)
121 goto noexist;
122
123 reg:
124 q->next = qdisc_list;
125 qdisc_list = q;
126 return q;
127
128 noexist:
129 q = malloc(sizeof(*q));
130 if (q) {
131
132 memset(q, 0, sizeof(*q));
133 q->id = strcpy(malloc(strlen(str)+1), str);
134 q->parse_qopt = parse_noqopt;
135 q->print_qopt = print_noqopt;
136 goto reg;
137 }
138 return q;
139 }
140
141
142 struct filter_util *get_filter_kind(const char *str)
143 {
144 void *dlh;
145 char buf[256];
146 struct filter_util *q;
147
148 for (q = filter_list; q; q = q->next)
149 if (strcmp(q->id, str) == 0)
150 return q;
151
152 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
153 dlh = dlopen(buf, RTLD_LAZY);
154 if (dlh == NULL) {
155 dlh = BODY;
156 if (dlh == NULL) {
157 dlh = BODY = dlopen(NULL, RTLD_LAZY);
158 if (dlh == NULL)
159 goto noexist;
160 }
161 }
162
163 snprintf(buf, sizeof(buf), "%s_filter_util", str);
164 q = dlsym(dlh, buf);
165 if (q == NULL)
166 goto noexist;
167
168 reg:
169 q->next = filter_list;
170 filter_list = q;
171 return q;
172 noexist:
173 q = malloc(sizeof(*q));
174 if (q) {
175 memset(q, 0, sizeof(*q));
176 strncpy(q->id, str, 15);
177 q->parse_fopt = parse_nofopt;
178 q->print_fopt = print_nofopt;
179 goto reg;
180 }
181 return q;
182 }
183
184 static void usage(void)
185 {
186 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
187 " tc [-force] -batch filename\n"
188 "where OBJECT := { qdisc | class | filter | action | monitor }\n"
189 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n");
190 }
191
192 static int do_cmd(int argc, char **argv)
193 {
194 if (matches(*argv, "qdisc") == 0)
195 return do_qdisc(argc-1, argv+1);
196
197 if (matches(*argv, "class") == 0)
198 return do_class(argc-1, argv+1);
199
200 if (matches(*argv, "filter") == 0)
201 return do_filter(argc-1, argv+1);
202
203 if (matches(*argv, "actions") == 0)
204 return do_action(argc-1, argv+1);
205
206 if (matches(*argv, "monitor") == 0)
207 return do_tcmonitor(argc-1, argv+1);
208
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 {
300 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
301 return -1;
302 }
303 argc--; argv++;
304 }
305
306 if (batch_file)
307 return batch(batch_file);
308
309 if (argc <= 1) {
310 usage();
311 return 0;
312 }
313
314 tc_core_init();
315 if (rtnl_open(&rth, 0) < 0) {
316 fprintf(stderr, "Cannot open rtnetlink\n");
317 exit(1);
318 }
319
320 ret = do_cmd(argc-1, argv+1);
321 rtnl_close(&rth);
322
323 return ret;
324 }