]>
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;
39 static void *BODY
; /* cached handle dlopen(NULL) */
40 static struct qdisc_util
* qdisc_list
;
41 static struct filter_util
* filter_list
;
43 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
45 if (opt
&& RTA_PAYLOAD(opt
))
46 fprintf(f
, "[Unknown qdisc, optlen=%u] ", RTA_PAYLOAD(opt
));
50 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
53 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
59 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
61 if (opt
&& RTA_PAYLOAD(opt
))
62 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ", fhandle
, RTA_PAYLOAD(opt
));
64 fprintf(f
, "fh %08x ", fhandle
);
68 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
73 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
77 struct tcmsg
*t
= NLMSG_DATA(n
);
78 if (get_u32(&handle
, fhandle
, 16)) {
79 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
82 t
->tcm_handle
= handle
;
87 struct qdisc_util
*get_qdisc_kind(const char *str
)
93 for (q
= qdisc_list
; q
; q
= q
->next
)
94 if (strcmp(q
->id
, str
) == 0)
97 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/q_%s.so", str
);
98 dlh
= dlopen(buf
, RTLD_LAZY
);
100 /* look in current binary, only open once */
103 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
109 snprintf(buf
, sizeof(buf
), "%s_util", str
);
115 q
->next
= qdisc_list
;
120 q
= malloc(sizeof(*q
));
122 memset(q
, 0, sizeof(*q
));
123 strncpy(q
->id
, str
, 15);
124 q
->parse_qopt
= parse_noqopt
;
125 q
->print_qopt
= print_noqopt
;
132 struct filter_util
*get_filter_kind(const char *str
)
136 struct filter_util
*q
;
138 for (q
= filter_list
; q
; q
= q
->next
)
139 if (strcmp(q
->id
, str
) == 0)
142 snprintf(buf
, sizeof(buf
), "/usr/lib/tc/f_%s.so", str
);
143 dlh
= dlopen(buf
, RTLD_LAZY
);
147 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
153 snprintf(buf
, sizeof(buf
), "%s_util", str
);
159 q
->next
= filter_list
;
163 q
= malloc(sizeof(*q
));
165 memset(q
, 0, sizeof(*q
));
166 strncpy(q
->id
, str
, 15);
167 q
->parse_fopt
= parse_nofopt
;
168 q
->print_fopt
= print_nofopt
;
174 static void usage(void) __attribute__((noreturn
));
176 static void usage(void)
178 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
179 "where OBJECT := { qdisc | class | filter }\n"
180 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
186 int main(int argc
, char **argv
)
190 basename
= strrchr(argv
[0], '/');
191 if (basename
== NULL
)
198 if (argc
> 1 && matches(argv
[1], "-batch") == 0) {
203 #define BMAXARG (sizeof(largv)/sizeof(char *)-2)
206 fprintf(stderr
, "Wrong number of arguments in batch mode\n");
209 if (matches(argv
[2], "-") != 0) {
210 if ((batch
= fopen(argv
[2], "r")) == NULL
) {
211 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s=n", argv
[2], strerror(errno
));
215 if ((batch
= fdopen(0, "r")) == NULL
) {
216 fprintf(stderr
, "Cannot open stdin for reading: %s=n", strerror(errno
));
223 while (fgets(line
, sizeof(line
)-1, batch
)) {
224 if (line
[strlen(line
)-1]=='\n') {
225 line
[strlen(line
)-1] = '\0';
227 fprintf(stderr
, "No newline at the end of line, looks like to long (%d chars or more)\n", strlen(line
));
231 largv
[largc
]=strtok(line
, " ");
232 while ((largv
[++largc
]=strtok(NULL
, " ")) != NULL
) {
233 if (largc
> BMAXARG
) {
234 fprintf(stderr
, "Over %d arguments in batch mode, enough!\n", BMAXARG
);
239 if (matches(largv
[0], "qdisc") == 0) {
240 ret
+= do_qdisc(largc
-1, largv
+1);
241 } else if (matches(largv
[0], "class") == 0) {
242 ret
+= do_class(largc
-1, largv
+1);
243 } else if (matches(largv
[0], "filter") == 0) {
244 ret
+= do_filter(largc
-1, largv
+1);
245 } else if (matches(largv
[0], "help") == 0) {
246 usage(); /* note that usage() doesn't return */
248 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", largv
[1]);
253 exit(0); /* end of batch, that's all */
257 if (argv
[1][0] != '-')
259 if (matches(argv
[1], "-stats") == 0 ||
260 matches(argv
[1], "-statistics") == 0) {
262 } else if (matches(argv
[1], "-details") == 0) {
264 } else if (matches(argv
[1], "-raw") == 0) {
266 } else if (matches(argv
[1], "-Version") == 0) {
267 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
269 } else if (matches(argv
[1], "-iec") == 0) {
271 } else if (matches(argv
[1], "-help") == 0) {
274 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
283 if (matches(argv
[1], "qdisc") == 0)
284 return do_qdisc(argc
-2, argv
+2);
285 if (matches(argv
[1], "class") == 0)
286 return do_class(argc
-2, argv
+2);
287 if (matches(argv
[1], "filter") == 0)
288 return do_filter(argc
-2, argv
+2);
289 if (matches(argv
[1], "help") == 0)
291 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n", argv
[1]);