| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc.c "tc" utility frontend. |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <dlfcn.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <string.h> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#include "version.h" |
| 20 |
#include "utils.h" |
| 21 |
#include "tc_util.h" |
| 22 |
#include "tc_common.h" |
| 23 |
#include "namespace.h" |
| 24 |
#include "rt_names.h" |
| 25 |
#include "bpf_util.h" |
| 26 |
|
| 27 |
int show_stats; |
| 28 |
int show_details; |
| 29 |
int show_raw; |
| 30 |
int show_graph; |
| 31 |
int timestamp; |
| 32 |
|
| 33 |
int batch_mode; |
| 34 |
int force; |
| 35 |
bool use_names; |
| 36 |
int json; |
| 37 |
int oneline; |
| 38 |
int brief; |
| 39 |
|
| 40 |
int echo_request; |
| 41 |
|
| 42 |
static char *conf_file; |
| 43 |
|
| 44 |
struct rtnl_handle rth; |
| 45 |
|
| 46 |
static void *BODY; /* cached handle dlopen(NULL) */ |
| 47 |
static struct qdisc_util *qdisc_list; |
| 48 |
static struct filter_util *filter_list; |
| 49 |
|
| 50 |
static int print_noqopt(const struct qdisc_util *qu, FILE *f, |
| 51 |
struct rtattr *opt) |
| 52 |
{ |
| 53 |
if (opt && RTA_PAYLOAD(opt)) |
| 54 |
fprintf(f, "[Unknown qdisc, optlen=%u] ", |
| 55 |
(unsigned int) RTA_PAYLOAD(opt)); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static int parse_noqopt(const struct qdisc_util *qu, int argc, char **argv, |
| 60 |
struct nlmsghdr *n, const char *dev) |
| 61 |
{ |
| 62 |
if (argc) { |
| 63 |
fprintf(stderr, |
| 64 |
"Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", |
| 65 |
qu->id, *argv); |
| 66 |
return -1; |
| 67 |
} |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static int print_nofopt(const struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle) |
| 72 |
{ |
| 73 |
if (opt && RTA_PAYLOAD(opt)) |
| 74 |
fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", |
| 75 |
fhandle, (unsigned int) RTA_PAYLOAD(opt)); |
| 76 |
else if (fhandle) |
| 77 |
fprintf(f, "fh %08x ", fhandle); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int parse_nofopt(const struct filter_util *qu, char *fhandle, |
| 82 |
int argc, char **argv, struct nlmsghdr *n) |
| 83 |
{ |
| 84 |
__u32 handle; |
| 85 |
|
| 86 |
if (argc) { |
| 87 |
fprintf(stderr, |
| 88 |
"Unknown filter \"%s\", hence option \"%s\" is unparsable\n", |
| 89 |
qu->id, *argv); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
if (fhandle) { |
| 93 |
struct tcmsg *t = NLMSG_DATA(n); |
| 94 |
|
| 95 |
if (get_u32(&handle, fhandle, 16)) { |
| 96 |
fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
t->tcm_handle = handle; |
| 100 |
} |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
const struct qdisc_util *get_qdisc_kind(const char *str) |
| 105 |
{ |
| 106 |
void *dlh; |
| 107 |
char buf[256]; |
| 108 |
struct qdisc_util *q; |
| 109 |
|
| 110 |
for (q = qdisc_list; q; q = q->next) |
| 111 |
if (strcmp(q->id, str) == 0) |
| 112 |
return q; |
| 113 |
|
| 114 |
snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str); |
| 115 |
dlh = dlopen(buf, RTLD_LAZY); |
| 116 |
if (!dlh) { |
| 117 |
/* look in current binary, only open once */ |
| 118 |
dlh = BODY; |
| 119 |
if (dlh == NULL) { |
| 120 |
dlh = BODY = dlopen(NULL, RTLD_LAZY); |
| 121 |
if (dlh == NULL) |
| 122 |
goto noexist; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
snprintf(buf, sizeof(buf), "%s_qdisc_util", str); |
| 127 |
q = dlsym(dlh, buf); |
| 128 |
if (q == NULL) |
| 129 |
goto noexist; |
| 130 |
|
| 131 |
reg: |
| 132 |
q->next = qdisc_list; |
| 133 |
qdisc_list = q; |
| 134 |
return q; |
| 135 |
|
| 136 |
noexist: |
| 137 |
q = calloc(1, sizeof(*q)); |
| 138 |
if (q) { |
| 139 |
q->id = strdup(str); |
| 140 |
q->parse_qopt = parse_noqopt; |
| 141 |
q->print_qopt = print_noqopt; |
| 142 |
goto reg; |
| 143 |
} |
| 144 |
return q; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
const struct filter_util *get_filter_kind(const char *str) |
| 149 |
{ |
| 150 |
void *dlh; |
| 151 |
char buf[256]; |
| 152 |
struct filter_util *q; |
| 153 |
|
| 154 |
for (q = filter_list; q; q = q->next) |
| 155 |
if (strcmp(q->id, str) == 0) |
| 156 |
return q; |
| 157 |
|
| 158 |
snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str); |
| 159 |
dlh = dlopen(buf, RTLD_LAZY); |
| 160 |
if (dlh == NULL) { |
| 161 |
dlh = BODY; |
| 162 |
if (dlh == NULL) { |
| 163 |
dlh = BODY = dlopen(NULL, RTLD_LAZY); |
| 164 |
if (dlh == NULL) |
| 165 |
goto noexist; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
snprintf(buf, sizeof(buf), "%s_filter_util", str); |
| 170 |
q = dlsym(dlh, buf); |
| 171 |
if (q == NULL) |
| 172 |
goto noexist; |
| 173 |
|
| 174 |
reg: |
| 175 |
q->next = filter_list; |
| 176 |
filter_list = q; |
| 177 |
return q; |
| 178 |
noexist: |
| 179 |
q = calloc(1, sizeof(*q)); |
| 180 |
if (q) { |
| 181 |
strncpy(q->id, str, 15); |
| 182 |
q->parse_fopt = parse_nofopt; |
| 183 |
q->print_fopt = print_nofopt; |
| 184 |
goto reg; |
| 185 |
} |
| 186 |
return q; |
| 187 |
} |
| 188 |
|
| 189 |
static void usage(void) |
| 190 |
{ |
| 191 |
fprintf(stderr, |
| 192 |
"Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" |
| 193 |
" tc [-force] -batch filename\n" |
| 194 |
"where OBJECT := { qdisc | class | filter | chain |\n" |
| 195 |
" action | monitor | exec }\n" |
| 196 |
" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n" |
| 197 |
" -o[neline] | -j[son] | -p[retty] | -c[olor]\n" |
| 198 |
" -b[atch] [filename] | -n[etns] name | -N[umeric] |\n" |
| 199 |
" -nm | -nam[es] | { -cf | -conf } path\n" |
| 200 |
" -br[ief] | -echo }\n"); |
| 201 |
} |
| 202 |
|
| 203 |
static int do_cmd(int argc, char **argv) |
| 204 |
{ |
| 205 |
if (matches(*argv, "qdisc") == 0) |
| 206 |
return do_qdisc(argc-1, argv+1); |
| 207 |
if (matches(*argv, "class") == 0) |
| 208 |
return do_class(argc-1, argv+1); |
| 209 |
if (matches(*argv, "filter") == 0) |
| 210 |
return do_filter(argc-1, argv+1); |
| 211 |
if (matches(*argv, "chain") == 0) |
| 212 |
return do_chain(argc-1, argv+1); |
| 213 |
if (matches(*argv, "actions") == 0) |
| 214 |
return do_action(argc-1, argv+1); |
| 215 |
if (matches(*argv, "monitor") == 0) |
| 216 |
return do_tcmonitor(argc-1, argv+1); |
| 217 |
if (matches(*argv, "exec") == 0) |
| 218 |
return do_exec(argc-1, argv+1); |
| 219 |
if (matches(*argv, "help") == 0) { |
| 220 |
usage(); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", |
| 225 |
*argv); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
static int tc_batch_cmd(int argc, char *argv[], void *data) |
| 230 |
{ |
| 231 |
return do_cmd(argc, argv); |
| 232 |
} |
| 233 |
|
| 234 |
static int batch(const char *name) |
| 235 |
{ |
| 236 |
int ret; |
| 237 |
|
| 238 |
batch_mode = 1; |
| 239 |
tc_core_init(); |
| 240 |
|
| 241 |
if (rtnl_open(&rth, 0) < 0) { |
| 242 |
fprintf(stderr, "Cannot open rtnetlink\n"); |
| 243 |
return -1; |
| 244 |
} |
| 245 |
|
| 246 |
ret = do_batch(name, force, tc_batch_cmd, NULL); |
| 247 |
|
| 248 |
rtnl_close(&rth); |
| 249 |
return ret; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
int main(int argc, char **argv) |
| 254 |
{ |
| 255 |
const char *libbpf_version; |
| 256 |
char *batch_file = NULL; |
| 257 |
int color = default_color_opt(); |
| 258 |
int ret; |
| 259 |
|
| 260 |
while (argc > 1) { |
| 261 |
if (argv[1][0] != '-') |
| 262 |
break; |
| 263 |
if (matches(argv[1], "-stats") == 0 || |
| 264 |
matches(argv[1], "-statistics") == 0) { |
| 265 |
++show_stats; |
| 266 |
} else if (matches(argv[1], "-details") == 0) { |
| 267 |
++show_details; |
| 268 |
} else if (matches(argv[1], "-raw") == 0) { |
| 269 |
++show_raw; |
| 270 |
} else if (matches(argv[1], "-pretty") == 0) { |
| 271 |
++pretty; |
| 272 |
} else if (matches(argv[1], "-graph") == 0) { |
| 273 |
show_graph = 1; |
| 274 |
} else if (matches(argv[1], "-Version") == 0) { |
| 275 |
printf("tc utility, iproute2-%s", version); |
| 276 |
libbpf_version = get_libbpf_version(); |
| 277 |
if (libbpf_version) |
| 278 |
printf(", libbpf %s", libbpf_version); |
| 279 |
printf("\n"); |
| 280 |
return 0; |
| 281 |
} else if (matches(argv[1], "-iec") == 0) { |
| 282 |
++use_iec; |
| 283 |
} else if (matches(argv[1], "-help") == 0) { |
| 284 |
usage(); |
| 285 |
return 0; |
| 286 |
} else if (matches(argv[1], "-force") == 0) { |
| 287 |
++force; |
| 288 |
} else if (matches(argv[1], "-batch") == 0) { |
| 289 |
argc--; argv++; |
| 290 |
if (argc <= 1) |
| 291 |
missarg("batch file"); |
| 292 |
batch_file = argv[1]; |
| 293 |
} else if (matches(argv[1], "-netns") == 0) { |
| 294 |
NEXT_ARG(); |
| 295 |
if (netns_switch(argv[1])) |
| 296 |
return -1; |
| 297 |
} else if (matches(argv[1], "-Numeric") == 0) { |
| 298 |
++numeric; |
| 299 |
} else if (matches(argv[1], "-names") == 0 || |
| 300 |
matches(argv[1], "-nm") == 0) { |
| 301 |
use_names = true; |
| 302 |
} else if (matches(argv[1], "-cf") == 0 || |
| 303 |
matches(argv[1], "-conf") == 0) { |
| 304 |
NEXT_ARG(); |
| 305 |
conf_file = argv[1]; |
| 306 |
} else if (matches_color(argv[1], &color)) { |
| 307 |
} else if (matches(argv[1], "-timestamp") == 0) { |
| 308 |
timestamp++; |
| 309 |
} else if (matches(argv[1], "-tshort") == 0) { |
| 310 |
++timestamp; |
| 311 |
++timestamp_short; |
| 312 |
} else if (matches(argv[1], "-json") == 0) { |
| 313 |
++json; |
| 314 |
} else if (matches(argv[1], "-oneline") == 0) { |
| 315 |
++oneline; |
| 316 |
} else if (matches(argv[1], "-brief") == 0) { |
| 317 |
++brief; |
| 318 |
} else if (strcmp(argv[1], "-echo") == 0) { |
| 319 |
++echo_request; |
| 320 |
} else { |
| 321 |
fprintf(stderr, |
| 322 |
"Option \"%s\" is unknown, try \"tc -help\".\n", |
| 323 |
argv[1]); |
| 324 |
return -1; |
| 325 |
} |
| 326 |
argc--; argv++; |
| 327 |
} |
| 328 |
|
| 329 |
_SL_ = oneline ? "\\" : "\n"; |
| 330 |
|
| 331 |
check_enable_color(color, json); |
| 332 |
|
| 333 |
if (batch_file) |
| 334 |
return batch(batch_file); |
| 335 |
|
| 336 |
if (argc <= 1) { |
| 337 |
usage(); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
tc_core_init(); |
| 342 |
if (rtnl_open(&rth, 0) < 0) { |
| 343 |
fprintf(stderr, "Cannot open rtnetlink\n"); |
| 344 |
exit(1); |
| 345 |
} |
| 346 |
|
| 347 |
if (use_names && cls_names_init(conf_file)) { |
| 348 |
ret = -1; |
| 349 |
goto Exit; |
| 350 |
} |
| 351 |
|
| 352 |
ret = do_cmd(argc-1, argv+1); |
| 353 |
Exit: |
| 354 |
rtnl_close(&rth); |
| 355 |
|
| 356 |
if (use_names) |
| 357 |
cls_names_uninit(); |
| 358 |
|
| 359 |
return ret; |
| 360 |
} |