blob: 9b50e7474769affc0f297198b1fcbf6bd8a09d1a [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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
33int show_stats = 0;
34int show_details = 0;
35int show_raw = 0;
Stephen Hemminger44dcfe82008-05-09 15:42:34 -070036int show_pretty = 0;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -070037int batch_mode = 0;
Stephen Hemminger44dcfe82008-05-09 15:42:34 -070038
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000039int resolve_hosts = 0;
osdl.net!shemminger3ea2bf42004-06-28 20:42:59 +000040int use_iec = 0;
osdl.net!shemminger08856f02005-03-18 19:40:55 +000041int force = 0;
osdl.net!shemminger79016602005-03-14 19:34:12 +000042struct rtnl_handle rth;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043
shemmingerf453a0d2005-10-07 16:33:21 +000044static void *BODY = NULL; /* cached handle dlopen(NULL) */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045static struct qdisc_util * qdisc_list;
46static struct filter_util * filter_list;
47
Stephen Hemmingerae665a52006-12-05 10:10:22 -080048static int print_noqopt(struct qdisc_util *qu, FILE *f,
osdl.net!shemminger1798c9d2004-08-31 17:45:21 +000049 struct rtattr *opt)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000050{
51 if (opt && RTA_PAYLOAD(opt))
Stephen Hemmingerae665a52006-12-05 10:10:22 -080052 fprintf(f, "[Unknown qdisc, optlen=%u] ",
osdl.net!shemmingerbb6a21a2004-10-06 23:17:10 +000053 (unsigned) RTA_PAYLOAD(opt));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000054 return 0;
55}
56
57static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
58{
59 if (argc) {
60 fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
61 return -1;
62 }
63 return 0;
64}
65
66static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
67{
68 if (opt && RTA_PAYLOAD(opt))
Stephen Hemmingerae665a52006-12-05 10:10:22 -080069 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
osdl.net!shemmingerbb6a21a2004-10-06 23:17:10 +000070 fhandle, (unsigned) RTA_PAYLOAD(opt));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000071 else if (fhandle)
72 fprintf(f, "fh %08x ", fhandle);
73 return 0;
74}
75
76static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
77{
78 __u32 handle;
79
80 if (argc) {
81 fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
82 return -1;
83 }
84 if (fhandle) {
85 struct tcmsg *t = NLMSG_DATA(n);
86 if (get_u32(&handle, fhandle, 16)) {
87 fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
88 return -1;
89 }
90 t->tcm_handle = handle;
91 }
92 return 0;
93}
94
osdl.org!shemminger4094db72004-06-02 20:22:08 +000095struct qdisc_util *get_qdisc_kind(const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000096{
97 void *dlh;
98 char buf[256];
99 struct qdisc_util *q;
100
101 for (q = qdisc_list; q; q = q->next)
102 if (strcmp(q->id, str) == 0)
103 return q;
104
Stephen Hemmingeraa27f882007-06-20 15:27:22 -0700105 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 dlh = dlopen(buf, RTLD_LAZY);
osdl.net!shemmingerb7a45152004-07-02 17:47:53 +0000107 if (!dlh) {
108 /* look in current binary, only open once */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000109 dlh = BODY;
110 if (dlh == NULL) {
111 dlh = BODY = dlopen(NULL, RTLD_LAZY);
112 if (dlh == NULL)
113 goto noexist;
114 }
115 }
116
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000117 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118 q = dlsym(dlh, buf);
119 if (q == NULL)
120 goto noexist;
121
122reg:
123 q->next = qdisc_list;
124 qdisc_list = q;
125 return q;
126
127noexist:
128 q = malloc(sizeof(*q));
129 if (q) {
osdl.net!shemminger1798c9d2004-08-31 17:45:21 +0000130
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 memset(q, 0, sizeof(*q));
osdl.net!shemminger1798c9d2004-08-31 17:45:21 +0000132 q->id = strcpy(malloc(strlen(str)+1), str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000133 q->parse_qopt = parse_noqopt;
134 q->print_qopt = print_noqopt;
135 goto reg;
136 }
137 return q;
138}
139
140
osdl.org!shemminger4094db72004-06-02 20:22:08 +0000141struct filter_util *get_filter_kind(const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142{
143 void *dlh;
144 char buf[256];
145 struct filter_util *q;
146
147 for (q = filter_list; q; q = q->next)
148 if (strcmp(q->id, str) == 0)
149 return q;
150
Stephen Hemmingeraa27f882007-06-20 15:27:22 -0700151 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000152 dlh = dlopen(buf, RTLD_LAZY);
153 if (dlh == NULL) {
154 dlh = BODY;
155 if (dlh == NULL) {
156 dlh = BODY = dlopen(NULL, RTLD_LAZY);
157 if (dlh == NULL)
158 goto noexist;
159 }
160 }
161
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000162 snprintf(buf, sizeof(buf), "%s_filter_util", str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163 q = dlsym(dlh, buf);
164 if (q == NULL)
165 goto noexist;
166
167reg:
168 q->next = filter_list;
169 filter_list = q;
170 return q;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000171noexist:
172 q = malloc(sizeof(*q));
173 if (q) {
174 memset(q, 0, sizeof(*q));
175 strncpy(q->id, str, 15);
176 q->parse_fopt = parse_nofopt;
177 q->print_fopt = print_nofopt;
178 goto reg;
179 }
180 return q;
181}
182
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000183static void usage(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000184{
185 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
Petr Jediný10494d22008-08-07 16:45:33 +0200186 " tc [-force] -batch filename\n"
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -0700187 "where OBJECT := { qdisc | class | filter | action | monitor }\n"
Petr Jediný10494d22008-08-07 16:45:33 +0200188 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n");
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000189}
190
191static int do_cmd(int argc, char **argv)
192{
193 if (matches(*argv, "qdisc") == 0)
194 return do_qdisc(argc-1, argv+1);
195
196 if (matches(*argv, "class") == 0)
197 return do_class(argc-1, argv+1);
198
199 if (matches(*argv, "filter") == 0)
200 return do_filter(argc-1, argv+1);
201
202 if (matches(*argv, "actions") == 0)
203 return do_action(argc-1, argv+1);
204
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -0700205 if (matches(*argv, "monitor") == 0)
206 return do_tcmonitor(argc-1, argv+1);
207
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000208 if (matches(*argv, "help") == 0) {
209 usage();
210 return 0;
211 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800212
213 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000214 *argv);
osdl.net!shemminger044ebf32005-03-14 19:02:41 +0000215 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000216}
217
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000218static int batch(const char *name)
219{
220 char *line = NULL;
221 size_t len = 0;
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000222 int ret = 0;
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000223
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700224 batch_mode = 1;
shemmingerdd3e9082005-06-23 17:32:22 +0000225 if (name && strcmp(name, "-") != 0) {
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000226 if (freopen(name, "r", stdin) == NULL) {
Stephen Hemminger84d66882008-02-07 22:10:14 -0800227 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000228 name, strerror(errno));
229 return -1;
230 }
231 }
232
233 tc_core_init();
234
235 if (rtnl_open(&rth, 0) < 0) {
236 fprintf(stderr, "Cannot open rtnetlink\n");
237 return -1;
238 }
239
shemminger351efcd2005-09-01 19:21:50 +0000240 cmdlineno = 0;
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000241 while (getcmdline(&line, &len, stdin) != -1) {
242 char *largv[100];
243 int largc;
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000244
245 largc = makeargs(line, largv, 100);
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000246 if (largc == 0)
247 continue; /* blank line */
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000248
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000249 if (do_cmd(largc, largv)) {
shemminger351efcd2005-09-01 19:21:50 +0000250 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000251 ret = 1;
252 if (!force)
253 break;
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000254 }
255 }
shemminger8ed63ab2005-09-21 19:33:17 +0000256 if (line)
257 free(line);
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000258
259 rtnl_close(&rth);
260 return ret;
261}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000262
263
264int main(int argc, char **argv)
265{
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000266 int ret;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700267 char *batch_file = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268
269 while (argc > 1) {
270 if (argv[1][0] != '-')
271 break;
272 if (matches(argv[1], "-stats") == 0 ||
osdl.net!shemminger3ea2bf42004-06-28 20:42:59 +0000273 matches(argv[1], "-statistics") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000274 ++show_stats;
275 } else if (matches(argv[1], "-details") == 0) {
276 ++show_details;
277 } else if (matches(argv[1], "-raw") == 0) {
278 ++show_raw;
Stephen Hemminger44dcfe82008-05-09 15:42:34 -0700279 } else if (matches(argv[1], "-pretty") == 0) {
280 ++show_pretty;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281 } else if (matches(argv[1], "-Version") == 0) {
282 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
osdl.net!shemminger044ebf32005-03-14 19:02:41 +0000283 return 0;
osdl.net!shemminger3ea2bf42004-06-28 20:42:59 +0000284 } else if (matches(argv[1], "-iec") == 0) {
285 ++use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000286 } else if (matches(argv[1], "-help") == 0) {
287 usage();
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000288 return 0;
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000289 } else if (matches(argv[1], "-force") == 0) {
290 ++force;
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000291 } else if (matches(argv[1], "-batch") == 0) {
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000292 argc--; argv++;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700293 if (argc <= 1)
294 usage();
295 batch_file = argv[1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296 } else {
297 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
osdl.net!shemminger044ebf32005-03-14 19:02:41 +0000298 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000299 }
300 argc--; argv++;
301 }
302
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700303 if (batch_file)
304 return batch(batch_file);
osdl.net!shemminger08856f02005-03-18 19:40:55 +0000305
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000306 if (argc <= 1) {
307 usage();
308 return 0;
309 }
310
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000311 tc_core_init();
osdl.net!shemminger79016602005-03-14 19:34:12 +0000312 if (rtnl_open(&rth, 0) < 0) {
313 fprintf(stderr, "Cannot open rtnetlink\n");
314 exit(1);
315 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000316
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000317 ret = do_cmd(argc-1, argv+1);
osdl.net!shemminger79016602005-03-14 19:34:12 +0000318 rtnl_close(&rth);
site!shemmingerc2f3a0f2005-03-14 22:19:16 +0000319
320 return ret;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000321}