]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
2 * tc.c "tc" utility frontend.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
31 #include "tc_common.h"
36 int resolve_hosts
= 0;
38 struct rtnl_handle rth
;
40 static void *BODY
; /* cached handle dlopen(NULL) */
41 static struct qdisc_util
* qdisc_list
;
42 static struct filter_util
* filter_list
;
44 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
47 if (opt
&& RTA_PAYLOAD(opt
))
48 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
49 (unsigned) RTA_PAYLOAD(opt
));
53 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
56 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
62 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
64 if (opt
&& RTA_PAYLOAD(opt
))
65 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
66 fhandle
, (unsigned) RTA_PAYLOAD(opt
));
68 fprintf(f
, "fh %08x ", fhandle
);
72 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
77 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
81 struct tcmsg
*t
= NLMSG_DATA(n
);
82 if (get_u32(&handle
, fhandle
, 16)) {
83 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
86 t
->tcm_handle
= handle
;
91 struct qdisc_util
*get_qdisc_kind(const char *str
)
97 for (q
= qdisc_list
; q
; q
= q
->next
)
98 if (strcmp(q
->id
, str
) == 0)
101 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/q_%s.so", str
);
102 dlh
= dlopen(buf
, RTLD_LAZY
);
104 /* look in current binary, only open once */
107 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
113 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
119 q
->next
= qdisc_list
;
124 q
= malloc(sizeof(*q
));
127 memset(q
, 0, sizeof(*q
));
128 q
->id
= strcpy(malloc(strlen(str
)+1), str
);
129 q
->parse_qopt
= parse_noqopt
;
130 q
->print_qopt
= print_noqopt
;
137 struct filter_util
*get_filter_kind(const char *str
)
141 struct filter_util
*q
;
143 for (q
= filter_list
; q
; q
= q
->next
)
144 if (strcmp(q
->id
, str
) == 0)
147 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/f_%s.so", str
);
148 dlh
= dlopen(buf
, RTLD_LAZY
);
152 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
158 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
164 q
->next
= filter_list
;
168 q
= malloc(sizeof(*q
));
170 memset(q
, 0, sizeof(*q
));
171 strncpy(q
->id
, str
, 15);
172 q
->parse_fopt
= parse_nofopt
;
173 q
->print_fopt
= print_nofopt
;
179 static void usage(void)
181 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
182 "where OBJECT := { qdisc | class | filter | action }\n"
183 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
186 static int do_cmd(int argc
, char **argv
)
188 if (matches(*argv
, "qdisc") == 0)
189 return do_qdisc(argc
-1, argv
+1);
191 if (matches(*argv
, "class") == 0)
192 return do_class(argc
-1, argv
+1);
194 if (matches(*argv
, "filter") == 0)
195 return do_filter(argc
-1, argv
+1);
197 if (matches(*argv
, "actions") == 0)
198 return do_action(argc
-1, argv
+1);
200 if (matches(*argv
, "help") == 0) {
205 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
210 static int makeargs(char *line
, char *argv
[], int maxargs
)
212 static const char ws
[] = " \t\r\n";
216 for (cp
= strtok(line
, ws
); cp
; cp
= strtok(NULL
, ws
)) {
217 if (argc
>= maxargs
) {
218 fprintf(stderr
, "Too many arguments to command\n");
228 static int batch(const char *name
)
237 if (strcmp(name
, "-") != 0) {
238 if (freopen(name
, "r", stdin
) == NULL
) {
239 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s=n",
240 name
, strerror(errno
));
247 if (rtnl_open(&rth
, 0) < 0) {
248 fprintf(stderr
, "Cannot open rtnetlink\n");
252 while ((cc
= getline(&line
, &len
, stdin
)) != -1) {
255 /* ignore blank lines and comments */
256 if (*line
== '\n' || *line
== '#')
259 /* handle continuation lines */
260 while (cc
>= 2 && strcmp(line
+cc
-2, "\\\n") == 0) {
264 cc1
= getline(&line1
, &len1
, stdin
);
267 fprintf(stderr
, "Missing continuation line\n");
271 line
= realloc(line
, cc
+ cc1
);
273 fprintf(stderr
, "Out of memory\n");
277 strcpy(line
+cc
-2, line1
);
282 largc
= makeargs(line
, largv
, 100);
284 ret
= do_cmd(largc
, largv
);
286 fprintf(stderr
, "Command failed %s:%d\n", name
, lineno
);
296 int main(int argc
, char **argv
)
301 if (argv
[1][0] != '-')
303 if (matches(argv
[1], "-stats") == 0 ||
304 matches(argv
[1], "-statistics") == 0) {
306 } else if (matches(argv
[1], "-details") == 0) {
308 } else if (matches(argv
[1], "-raw") == 0) {
310 } else if (matches(argv
[1], "-Version") == 0) {
311 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
313 } else if (matches(argv
[1], "-iec") == 0) {
315 } else if (matches(argv
[1], "-help") == 0) {
318 } else if (matches(argv
[1], "-batch") == 0) {
320 fprintf(stderr
, "Wrong number of arguments in batch mode\n");
324 return batch(argv
[2]);
326 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
338 if (rtnl_open(&rth
, 0) < 0) {
339 fprintf(stderr
, "Cannot open rtnetlink\n");
343 ret
= do_cmd(argc
-1, argv
+1);