]>
Commit | Line | Data |
---|---|---|
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 | ||
33 | int show_stats = 0; | |
34 | int show_details = 0; | |
35 | int show_raw = 0; | |
44dcfe82 | 36 | int show_pretty = 0; |
d954b34a | 37 | int show_graph = 0; |
44dcfe82 | 38 | |
d954b34a | 39 | int batch_mode = 0; |
aba5acdf | 40 | int resolve_hosts = 0; |
3ea2bf45 | 41 | int use_iec = 0; |
08856f02 | 42 | int force = 0; |
7901660a | 43 | struct rtnl_handle rth; |
aba5acdf | 44 | |
f453a0d4 | 45 | static void *BODY = NULL; /* cached handle dlopen(NULL) */ |
aba5acdf SH |
46 | static struct qdisc_util * qdisc_list; |
47 | static struct filter_util * filter_list; | |
48 | ||
ae665a52 | 49 | static int print_noqopt(struct qdisc_util *qu, FILE *f, |
1798c9d5 | 50 | struct rtattr *opt) |
aba5acdf SH |
51 | { |
52 | if (opt && RTA_PAYLOAD(opt)) | |
ae665a52 | 53 | fprintf(f, "[Unknown qdisc, optlen=%u] ", |
bb6a21a4 | 54 | (unsigned) RTA_PAYLOAD(opt)); |
aba5acdf SH |
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)) | |
ae665a52 | 70 | fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", |
bb6a21a4 | 71 | fhandle, (unsigned) RTA_PAYLOAD(opt)); |
aba5acdf SH |
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 | ||
4094db72 | 96 | struct qdisc_util *get_qdisc_kind(const char *str) |
aba5acdf SH |
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 | ||
aa27f88c | 106 | snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str); |
aba5acdf | 107 | dlh = dlopen(buf, RTLD_LAZY); |
b7a45150 SH |
108 | if (!dlh) { |
109 | /* look in current binary, only open once */ | |
aba5acdf SH |
110 | dlh = BODY; |
111 | if (dlh == NULL) { | |
112 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
113 | if (dlh == NULL) | |
114 | goto noexist; | |
115 | } | |
116 | } | |
117 | ||
95812b56 | 118 | snprintf(buf, sizeof(buf), "%s_qdisc_util", str); |
aba5acdf SH |
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) { | |
1798c9d5 | 131 | |
aba5acdf | 132 | memset(q, 0, sizeof(*q)); |
1798c9d5 | 133 | q->id = strcpy(malloc(strlen(str)+1), str); |
aba5acdf SH |
134 | q->parse_qopt = parse_noqopt; |
135 | q->print_qopt = print_noqopt; | |
136 | goto reg; | |
137 | } | |
138 | return q; | |
139 | } | |
140 | ||
141 | ||
4094db72 | 142 | struct filter_util *get_filter_kind(const char *str) |
aba5acdf SH |
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 | ||
aa27f88c | 152 | snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str); |
aba5acdf SH |
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 | ||
95812b56 | 163 | snprintf(buf, sizeof(buf), "%s_filter_util", str); |
aba5acdf SH |
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; | |
aba5acdf SH |
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 | ||
c2f3a0f9 | 184 | static void usage(void) |
aba5acdf SH |
185 | { |
186 | fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
10494d27 | 187 | " tc [-force] -batch filename\n" |
5bec3484 | 188 | "where OBJECT := { qdisc | class | filter | action | monitor }\n" |
10494d27 | 189 | " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n"); |
c2f3a0f9 | 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 | ||
5bec3484 JHS |
206 | if (matches(*argv, "monitor") == 0) |
207 | return do_tcmonitor(argc-1, argv+1); | |
208 | ||
c2f3a0f9 | 209 | if (matches(*argv, "help") == 0) { |
210 | usage(); | |
211 | return 0; | |
212 | } | |
ae665a52 SH |
213 | |
214 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", | |
c2f3a0f9 | 215 | *argv); |
044ebf35 | 216 | return -1; |
aba5acdf SH |
217 | } |
218 | ||
c2f3a0f9 | 219 | static int batch(const char *name) |
aba5acdf | 220 | { |
c2f3a0f9 | 221 | char *line = NULL; |
222 | size_t len = 0; | |
08856f02 | 223 | int ret = 0; |
c2f3a0f9 | 224 | |
a3aa47a5 | 225 | batch_mode = 1; |
dd3e9085 | 226 | if (name && strcmp(name, "-") != 0) { |
c2f3a0f9 | 227 | if (freopen(name, "r", stdin) == NULL) { |
84d66882 | 228 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n", |
c2f3a0f9 | 229 | name, strerror(errno)); |
044ebf35 | 230 | return -1; |
aba5acdf | 231 | } |
c2f3a0f9 | 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 | ||
351efcde | 241 | cmdlineno = 0; |
08856f02 SH |
242 | while (getcmdline(&line, &len, stdin) != -1) { |
243 | char *largv[100]; | |
244 | int largc; | |
aba5acdf | 245 | |
c2f3a0f9 | 246 | largc = makeargs(line, largv, 100); |
08856f02 SH |
247 | if (largc == 0) |
248 | continue; /* blank line */ | |
aba5acdf | 249 | |
08856f02 | 250 | if (do_cmd(largc, largv)) { |
351efcde | 251 | fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno); |
08856f02 SH |
252 | ret = 1; |
253 | if (!force) | |
254 | break; | |
aba5acdf | 255 | } |
c2f3a0f9 | 256 | } |
8ed63ab1 SH |
257 | if (line) |
258 | free(line); | |
7901660a | 259 | |
c2f3a0f9 | 260 | rtnl_close(&rth); |
261 | return ret; | |
262 | } | |
7901660a | 263 | |
c2f3a0f9 | 264 | |
265 | int main(int argc, char **argv) | |
266 | { | |
267 | int ret; | |
a3aa47a5 | 268 | char *batch_file = NULL; |
aba5acdf SH |
269 | |
270 | while (argc > 1) { | |
271 | if (argv[1][0] != '-') | |
272 | break; | |
273 | if (matches(argv[1], "-stats") == 0 || | |
3ea2bf45 | 274 | matches(argv[1], "-statistics") == 0) { |
aba5acdf SH |
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; | |
44dcfe82 SH |
280 | } else if (matches(argv[1], "-pretty") == 0) { |
281 | ++show_pretty; | |
d954b34a VK |
282 | } else if (matches(argv[1], "-graph") == 0) { |
283 | show_graph = 1; | |
aba5acdf SH |
284 | } else if (matches(argv[1], "-Version") == 0) { |
285 | printf("tc utility, iproute2-ss%s\n", SNAPSHOT); | |
044ebf35 | 286 | return 0; |
3ea2bf45 SH |
287 | } else if (matches(argv[1], "-iec") == 0) { |
288 | ++use_iec; | |
aba5acdf SH |
289 | } else if (matches(argv[1], "-help") == 0) { |
290 | usage(); | |
c2f3a0f9 | 291 | return 0; |
08856f02 SH |
292 | } else if (matches(argv[1], "-force") == 0) { |
293 | ++force; | |
c2f3a0f9 | 294 | } else if (matches(argv[1], "-batch") == 0) { |
08856f02 | 295 | argc--; argv++; |
a3aa47a5 SH |
296 | if (argc <= 1) |
297 | usage(); | |
298 | batch_file = argv[1]; | |
aba5acdf SH |
299 | } else { |
300 | fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]); | |
044ebf35 | 301 | return -1; |
aba5acdf SH |
302 | } |
303 | argc--; argv++; | |
304 | } | |
305 | ||
a3aa47a5 SH |
306 | if (batch_file) |
307 | return batch(batch_file); | |
08856f02 | 308 | |
c2f3a0f9 | 309 | if (argc <= 1) { |
310 | usage(); | |
311 | return 0; | |
312 | } | |
313 | ||
aba5acdf | 314 | tc_core_init(); |
7901660a SH |
315 | if (rtnl_open(&rth, 0) < 0) { |
316 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
317 | exit(1); | |
318 | } | |
aba5acdf | 319 | |
c2f3a0f9 | 320 | ret = do_cmd(argc-1, argv+1); |
7901660a | 321 | rtnl_close(&rth); |
c2f3a0f9 | 322 | |
323 | return ret; | |
aba5acdf | 324 | } |