]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
5a98d4ffe39094482d82ae71205fd8ff6d5209eb
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 int 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");
189 int main(int argc
, char **argv
)
192 if (argc
> 1 && matches(argv
[1], "-batch") == 0) {
197 #define BMAXARG (sizeof(largv)/sizeof(char *)-2)
200 fprintf(stderr
, "Wrong number of arguments in batch mode\n");
203 if (matches(argv
[2], "-") != 0) {
204 if ((batch
= fopen(argv
[2], "r")) == NULL
) {
205 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s=n", argv
[2], strerror(errno
));
209 if ((batch
= fdopen(0, "r")) == NULL
) {
210 fprintf(stderr
, "Cannot open stdin for reading: %s=n", strerror(errno
));
216 if (rtnl_open(&rth
, 0) < 0) {
217 fprintf(stderr
, "Cannot open rtnetlink\n");
221 while (fgets(line
, sizeof(line
)-1, batch
)) {
222 if (line
[strlen(line
)-1]=='\n') {
223 line
[strlen(line
)-1] = '\0';
225 fprintf(stderr
, "No newline at the end of line, looks like to long (%d chars or more)\n",
230 largv
[largc
]=strtok(line
, " ");
231 if (largv
[largc
]==NULL
)
233 while ((largv
[++largc
]=strtok(NULL
, " ")) != NULL
) {
234 if (largc
> BMAXARG
) {
235 fprintf(stderr
, "Over %d arguments in batch mode, enough!\n",
241 if (largv
[0][0]=='#')
244 if (matches(largv
[0], "qdisc") == 0) {
245 ret
+= do_qdisc(largc
-1, largv
+1);
246 } else if (matches(largv
[0], "class") == 0) {
247 ret
+= do_class(largc
-1, largv
+1);
248 } else if (matches(largv
[0], "filter") == 0) {
249 ret
+= do_filter(largc
-1, largv
+1);
250 } else if (matches(largv
[0], "action") == 0) {
251 ret
+= do_action(largc
-1, largv
+1);
252 } else if (matches(largv
[0], "help") == 0) {
253 usage(); /* note that usage() doesn't return */
255 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", largv
[1]);
262 return 0; /* end of batch, that's all */
266 if (argv
[1][0] != '-')
268 if (matches(argv
[1], "-stats") == 0 ||
269 matches(argv
[1], "-statistics") == 0) {
271 } else if (matches(argv
[1], "-details") == 0) {
273 } else if (matches(argv
[1], "-raw") == 0) {
275 } else if (matches(argv
[1], "-Version") == 0) {
276 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
278 } else if (matches(argv
[1], "-iec") == 0) {
280 } else if (matches(argv
[1], "-help") == 0) {
283 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
290 if (rtnl_open(&rth
, 0) < 0) {
291 fprintf(stderr
, "Cannot open rtnetlink\n");
296 if (matches(argv
[1], "qdisc") == 0)
297 return do_qdisc(argc
-2, argv
+2);
298 if (matches(argv
[1], "class") == 0)
299 return do_class(argc
-2, argv
+2);
300 if (matches(argv
[1], "filter") == 0)
301 return do_filter(argc
-2, argv
+2);
302 if (matches(argv
[1], "actions") == 0)
303 return do_action(argc
-2, argv
+2);
304 if (matches(argv
[1], "help") == 0)
306 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", argv
[1]);