]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc.c
tc: B.W limits can now be specified in %.
[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;
4612d04d
VK
44
45static char *conf_file;
46
7901660a 47struct rtnl_handle rth;
aba5acdf 48
32a121cb
SH
49static void *BODY; /* cached handle dlopen(NULL) */
50static struct qdisc_util *qdisc_list;
51static struct filter_util *filter_list;
aba5acdf 52
ae665a52 53static int print_noqopt(struct qdisc_util *qu, FILE *f,
1798c9d5 54 struct rtattr *opt)
aba5acdf
SH
55{
56 if (opt && RTA_PAYLOAD(opt))
ae665a52 57 fprintf(f, "[Unknown qdisc, optlen=%u] ",
32a121cb 58 (unsigned int) RTA_PAYLOAD(opt));
aba5acdf
SH
59 return 0;
60}
61
927e3cfb 62static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
aba5acdf
SH
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
71static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
72{
73 if (opt && RTA_PAYLOAD(opt))
ae665a52 74 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
32a121cb 75 fhandle, (unsigned int) RTA_PAYLOAD(opt));
aba5acdf
SH
76 else if (fhandle)
77 fprintf(f, "fh %08x ", fhandle);
78 return 0;
79}
80
81static 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);
32a121cb 91
aba5acdf
SH
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
4094db72 101struct qdisc_util *get_qdisc_kind(const char *str)
aba5acdf
SH
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
aa27f88c 111 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
aba5acdf 112 dlh = dlopen(buf, RTLD_LAZY);
b7a45150
SH
113 if (!dlh) {
114 /* look in current binary, only open once */
aba5acdf
SH
115 dlh = BODY;
116 if (dlh == NULL) {
117 dlh = BODY = dlopen(NULL, RTLD_LAZY);
118 if (dlh == NULL)
119 goto noexist;
120 }
121 }
122
95812b56 123 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
aba5acdf
SH
124 q = dlsym(dlh, buf);
125 if (q == NULL)
126 goto noexist;
127
128reg:
129 q->next = qdisc_list;
130 qdisc_list = q;
131 return q;
132
133noexist:
f89bb021 134 q = calloc(1, sizeof(*q));
aba5acdf 135 if (q) {
f89bb021 136 q->id = strdup(str);
aba5acdf
SH
137 q->parse_qopt = parse_noqopt;
138 q->print_qopt = print_noqopt;
139 goto reg;
140 }
141 return q;
142}
143
144
4094db72 145struct filter_util *get_filter_kind(const char *str)
aba5acdf
SH
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
aa27f88c 155 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
aba5acdf
SH
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
95812b56 166 snprintf(buf, sizeof(buf), "%s_filter_util", str);
aba5acdf
SH
167 q = dlsym(dlh, buf);
168 if (q == NULL)
169 goto noexist;
170
171reg:
172 q->next = filter_list;
173 filter_list = q;
174 return q;
aba5acdf 175noexist:
f89bb021 176 q = calloc(1, sizeof(*q));
aba5acdf 177 if (q) {
aba5acdf
SH
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
c2f3a0f9 186static void usage(void)
aba5acdf
SH
187{
188 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
10494d27 189 " tc [-force] -batch filename\n"
32a121cb
SH
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"
4612d04d 192 " -nm | -nam[es] | { -cf | -conf } path }\n");
c2f3a0f9 193}
194
195static int do_cmd(int argc, char **argv)
196{
197 if (matches(*argv, "qdisc") == 0)
198 return do_qdisc(argc-1, argv+1);
c2f3a0f9 199 if (matches(*argv, "class") == 0)
200 return do_class(argc-1, argv+1);
c2f3a0f9 201 if (matches(*argv, "filter") == 0)
202 return do_filter(argc-1, argv+1);
c2f3a0f9 203 if (matches(*argv, "actions") == 0)
204 return do_action(argc-1, argv+1);
5bec3484
JHS
205 if (matches(*argv, "monitor") == 0)
206 return do_tcmonitor(argc-1, argv+1);
4bd62446
DB
207 if (matches(*argv, "exec") == 0)
208 return do_exec(argc-1, argv+1);
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 219static 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
265int 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;
4612d04d 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];
67e1d73b
VK
299 } else if (matches(argv[1], "-netns") == 0) {
300 NEXT_ARG();
301 if (netns_switch(argv[1]))
302 return -1;
4612d04d
VK
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];
32a6fbe5
ED
310 } else if (matches(argv[1], "-timestamp") == 0) {
311 timestamp++;
312 } else if (matches(argv[1], "-tshort") == 0) {
313 ++timestamp;
314 ++timestamp_short;
aba5acdf
SH
315 } else {
316 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
044ebf35 317 return -1;
aba5acdf
SH
318 }
319 argc--; argv++;
320 }
321
a3aa47a5
SH
322 if (batch_file)
323 return batch(batch_file);
08856f02 324
c2f3a0f9 325 if (argc <= 1) {
326 usage();
327 return 0;
328 }
329
aba5acdf 330 tc_core_init();
7901660a
SH
331 if (rtnl_open(&rth, 0) < 0) {
332 fprintf(stderr, "Cannot open rtnetlink\n");
333 exit(1);
334 }
aba5acdf 335
4612d04d
VK
336 if (use_names && cls_names_init(conf_file)) {
337 ret = -1;
338 goto Exit;
339 }
340
c2f3a0f9 341 ret = do_cmd(argc-1, argv+1);
4612d04d 342Exit:
7901660a 343 rtnl_close(&rth);
c2f3a0f9 344
4612d04d
VK
345 if (use_names)
346 cls_names_uninit();
347
c2f3a0f9 348 return ret;
aba5acdf 349}