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