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