]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
60987e3b7f1d4355034c5255fd33d49659e219f4
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 g_rth
;
39 int is_batch_mode
= 0;
41 static void *BODY
; /* cached handle dlopen(NULL) */
42 static struct qdisc_util
* qdisc_list
;
43 static struct filter_util
* filter_list
;
45 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
48 if (opt
&& RTA_PAYLOAD(opt
))
49 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
50 (unsigned) RTA_PAYLOAD(opt
));
54 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
57 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
63 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
65 if (opt
&& RTA_PAYLOAD(opt
))
66 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
67 fhandle
, (unsigned) RTA_PAYLOAD(opt
));
69 fprintf(f
, "fh %08x ", fhandle
);
73 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
78 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
82 struct tcmsg
*t
= NLMSG_DATA(n
);
83 if (get_u32(&handle
, fhandle
, 16)) {
84 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
87 t
->tcm_handle
= handle
;
92 struct qdisc_util
*get_qdisc_kind(const char *str
)
98 for (q
= qdisc_list
; q
; q
= q
->next
)
99 if (strcmp(q
->id
, str
) == 0)
102 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/q_%s.so", str
);
103 dlh
= dlopen(buf
, RTLD_LAZY
);
105 /* look in current binary, only open once */
108 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
114 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
120 q
->next
= qdisc_list
;
125 q
= malloc(sizeof(*q
));
128 memset(q
, 0, sizeof(*q
));
129 q
->id
= strcpy(malloc(strlen(str
)+1), str
);
130 q
->parse_qopt
= parse_noqopt
;
131 q
->print_qopt
= print_noqopt
;
138 struct filter_util
*get_filter_kind(const char *str
)
142 struct filter_util
*q
;
144 for (q
= filter_list
; q
; q
= q
->next
)
145 if (strcmp(q
->id
, str
) == 0)
148 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/f_%s.so", str
);
149 dlh
= dlopen(buf
, RTLD_LAZY
);
153 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
159 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
165 q
->next
= filter_list
;
169 q
= malloc(sizeof(*q
));
171 memset(q
, 0, sizeof(*q
));
172 strncpy(q
->id
, str
, 15);
173 q
->parse_fopt
= parse_nofopt
;
174 q
->print_fopt
= print_nofopt
;
180 static int usage(void)
182 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
183 "where OBJECT := { qdisc | class | filter | action }\n"
184 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
190 int main(int argc
, char **argv
)
194 basename
= strrchr(argv
[0], '/');
195 if (basename
== NULL
)
202 if (argc
> 1 && matches(argv
[1], "-batch") == 0) {
207 #define BMAXARG (sizeof(largv)/sizeof(char *)-2)
210 fprintf(stderr
, "Wrong number of arguments in batch mode\n");
213 if (matches(argv
[2], "-") != 0) {
214 if ((batch
= fopen(argv
[2], "r")) == NULL
) {
215 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s=n", argv
[2], strerror(errno
));
219 if ((batch
= fdopen(0, "r")) == NULL
) {
220 fprintf(stderr
, "Cannot open stdin for reading: %s=n", strerror(errno
));
228 if (rtnl_open(&g_rth
, 0) < 0) {
229 fprintf(stderr
, "Cannot open rtnetlink\n");
233 while (fgets(line
, sizeof(line
)-1, batch
)) {
234 if (line
[strlen(line
)-1]=='\n') {
235 line
[strlen(line
)-1] = '\0';
237 fprintf(stderr
, "No newline at the end of line, looks like to long (%d chars or more)\n",
242 largv
[largc
]=strtok(line
, " ");
243 if (largv
[largc
]==NULL
)
245 while ((largv
[++largc
]=strtok(NULL
, " ")) != NULL
) {
246 if (largc
> BMAXARG
) {
247 fprintf(stderr
, "Over %d arguments in batch mode, enough!\n",
253 if (largv
[0][0]=='#')
255 if (matches(largv
[0], "qdisc") == 0) {
256 ret
+= do_qdisc(largc
-1, largv
+1);
257 } else if (matches(largv
[0], "class") == 0) {
258 ret
+= do_class(largc
-1, largv
+1);
259 } else if (matches(largv
[0], "filter") == 0) {
260 ret
+= do_filter(largc
-1, largv
+1);
261 } else if (matches(largv
[0], "action") == 0) {
262 ret
+= do_action(largc
-1, largv
+1);
263 } else if (matches(largv
[0], "help") == 0) {
264 usage(); /* note that usage() doesn't return */
266 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", largv
[1]);
271 return 0; /* end of batch, that's all */
275 if (argv
[1][0] != '-')
277 if (matches(argv
[1], "-stats") == 0 ||
278 matches(argv
[1], "-statistics") == 0) {
280 } else if (matches(argv
[1], "-details") == 0) {
282 } else if (matches(argv
[1], "-raw") == 0) {
284 } else if (matches(argv
[1], "-Version") == 0) {
285 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
287 } else if (matches(argv
[1], "-iec") == 0) {
289 } else if (matches(argv
[1], "-help") == 0) {
292 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
301 if (matches(argv
[1], "qdisc") == 0)
302 return do_qdisc(argc
-2, argv
+2);
303 if (matches(argv
[1], "class") == 0)
304 return do_class(argc
-2, argv
+2);
305 if (matches(argv
[1], "filter") == 0)
306 return do_filter(argc
-2, argv
+2);
307 if (matches(argv
[1], "actions") == 0)
308 return do_action(argc
-2, argv
+2);
309 if (matches(argv
[1], "help") == 0)
311 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", argv
[1]);