| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc_class.c "tc class". |
| 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 <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <math.h> |
| 17 |
|
| 18 |
#include "utils.h" |
| 19 |
#include "tc_util.h" |
| 20 |
#include "tc_common.h" |
| 21 |
#include "list.h" |
| 22 |
|
| 23 |
struct graph_node { |
| 24 |
struct hlist_node hlist; |
| 25 |
__u32 id; |
| 26 |
__u32 parent_id; |
| 27 |
struct graph_node *parent_node; |
| 28 |
struct graph_node *right_node; |
| 29 |
void *data; |
| 30 |
int data_len; |
| 31 |
int nodes_count; |
| 32 |
}; |
| 33 |
|
| 34 |
static struct hlist_head cls_list = {}; |
| 35 |
static struct hlist_head root_cls_list = {}; |
| 36 |
|
| 37 |
static void usage(void); |
| 38 |
|
| 39 |
static void usage(void) |
| 40 |
{ |
| 41 |
fprintf(stderr, |
| 42 |
"Usage: tc class [ add | del | change | replace | show ] dev STRING\n" |
| 43 |
" [ classid CLASSID ] [ root | parent CLASSID ]\n" |
| 44 |
" [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n" |
| 45 |
"\n" |
| 46 |
" tc class show [ dev STRING ] [ root | parent CLASSID ]\n" |
| 47 |
"Where:\n" |
| 48 |
"QDISC_KIND := { prio | etc. }\n" |
| 49 |
"OPTIONS := ... try tc class add <desired QDISC_KIND> help\n"); |
| 50 |
} |
| 51 |
|
| 52 |
static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv) |
| 53 |
{ |
| 54 |
struct { |
| 55 |
struct nlmsghdr n; |
| 56 |
struct tcmsg t; |
| 57 |
char buf[4096]; |
| 58 |
} req = { |
| 59 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 60 |
.n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 61 |
.n.nlmsg_type = cmd, |
| 62 |
.t.tcm_family = AF_UNSPEC, |
| 63 |
}; |
| 64 |
const struct qdisc_util *q = NULL; |
| 65 |
struct tc_estimator est = {}; |
| 66 |
char d[IFNAMSIZ] = {}; |
| 67 |
char k[FILTER_NAMESZ] = {}; |
| 68 |
|
| 69 |
while (argc > 0) { |
| 70 |
if (strcmp(*argv, "dev") == 0) { |
| 71 |
NEXT_ARG(); |
| 72 |
if (d[0]) |
| 73 |
duparg("dev", *argv); |
| 74 |
strncpy(d, *argv, sizeof(d)-1); |
| 75 |
} else if (strcmp(*argv, "classid") == 0) { |
| 76 |
__u32 handle; |
| 77 |
|
| 78 |
NEXT_ARG(); |
| 79 |
if (req.t.tcm_handle) |
| 80 |
duparg("classid", *argv); |
| 81 |
if (get_tc_classid(&handle, *argv)) |
| 82 |
invarg("invalid class ID", *argv); |
| 83 |
req.t.tcm_handle = handle; |
| 84 |
} else if (strcmp(*argv, "handle") == 0) { |
| 85 |
fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n"); |
| 86 |
return -1; |
| 87 |
} else if (strcmp(*argv, "root") == 0) { |
| 88 |
if (req.t.tcm_parent) { |
| 89 |
fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n"); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
req.t.tcm_parent = TC_H_ROOT; |
| 93 |
} else if (strcmp(*argv, "parent") == 0) { |
| 94 |
__u32 handle; |
| 95 |
|
| 96 |
NEXT_ARG(); |
| 97 |
if (req.t.tcm_parent) |
| 98 |
duparg("parent", *argv); |
| 99 |
if (get_tc_classid(&handle, *argv)) |
| 100 |
invarg("invalid parent ID", *argv); |
| 101 |
req.t.tcm_parent = handle; |
| 102 |
} else if (matches(*argv, "estimator") == 0) { |
| 103 |
if (parse_estimator(&argc, &argv, &est)) |
| 104 |
return -1; |
| 105 |
} else if (matches(*argv, "help") == 0) { |
| 106 |
usage(); |
| 107 |
} else { |
| 108 |
strncpy(k, *argv, sizeof(k)-1); |
| 109 |
|
| 110 |
q = get_qdisc_kind(k); |
| 111 |
argc--; argv++; |
| 112 |
break; |
| 113 |
} |
| 114 |
argc--; argv++; |
| 115 |
} |
| 116 |
|
| 117 |
if (k[0]) |
| 118 |
addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1); |
| 119 |
if (est.ewma_log) |
| 120 |
addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); |
| 121 |
|
| 122 |
if (q) { |
| 123 |
if (q->parse_copt == NULL) { |
| 124 |
fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k); |
| 125 |
return 1; |
| 126 |
} |
| 127 |
if (q->parse_copt(q, argc, argv, &req.n, d)) |
| 128 |
return 1; |
| 129 |
} else { |
| 130 |
if (argc) { |
| 131 |
if (matches(*argv, "help") == 0) |
| 132 |
usage(); |
| 133 |
fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if (d[0]) { |
| 139 |
ll_init_map(&rth); |
| 140 |
|
| 141 |
req.t.tcm_ifindex = ll_name_to_index(d); |
| 142 |
if (!req.t.tcm_ifindex) |
| 143 |
return -nodev(d); |
| 144 |
} |
| 145 |
|
| 146 |
if (rtnl_talk(&rth, &req.n, NULL) < 0) |
| 147 |
return 2; |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
static int filter_ifindex; |
| 153 |
static __u32 filter_qdisc; |
| 154 |
static __u32 filter_classid; |
| 155 |
|
| 156 |
static void graph_node_add(__u32 parent_id, __u32 id, void *data, |
| 157 |
int len) |
| 158 |
{ |
| 159 |
struct graph_node *node = calloc(1, sizeof(struct graph_node)); |
| 160 |
|
| 161 |
node->id = id; |
| 162 |
node->parent_id = parent_id; |
| 163 |
|
| 164 |
if (data && len) { |
| 165 |
node->data = malloc(len); |
| 166 |
node->data_len = len; |
| 167 |
memcpy(node->data, data, len); |
| 168 |
} |
| 169 |
|
| 170 |
if (parent_id == TC_H_ROOT) |
| 171 |
hlist_add_head(&node->hlist, &root_cls_list); |
| 172 |
else |
| 173 |
hlist_add_head(&node->hlist, &cls_list); |
| 174 |
} |
| 175 |
|
| 176 |
static void graph_indent(char *buf, struct graph_node *node, int is_newline, |
| 177 |
int add_spaces) |
| 178 |
{ |
| 179 |
char spaces[100] = {0}; |
| 180 |
|
| 181 |
while (node && node->parent_node) { |
| 182 |
node->parent_node->right_node = node; |
| 183 |
node = node->parent_node; |
| 184 |
} |
| 185 |
while (node && node->right_node) { |
| 186 |
if (node->hlist.next) |
| 187 |
strcat(buf, "| "); |
| 188 |
else |
| 189 |
strcat(buf, " "); |
| 190 |
|
| 191 |
node = node->right_node; |
| 192 |
} |
| 193 |
|
| 194 |
if (is_newline) { |
| 195 |
if (node->hlist.next && node->nodes_count) |
| 196 |
strcat(buf, "| |"); |
| 197 |
else if (node->hlist.next) |
| 198 |
strcat(buf, "| "); |
| 199 |
else if (node->nodes_count) |
| 200 |
strcat(buf, " |"); |
| 201 |
else if (!node->hlist.next) |
| 202 |
strcat(buf, " "); |
| 203 |
} |
| 204 |
if (add_spaces > 0) { |
| 205 |
sprintf(spaces, "%-*s", add_spaces, ""); |
| 206 |
strcat(buf, spaces); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list, |
| 211 |
int level) |
| 212 |
{ |
| 213 |
struct hlist_node *n, *tmp_cls; |
| 214 |
char cls_id_str[256] = {}; |
| 215 |
struct rtattr *tb[TCA_MAX + 1]; |
| 216 |
const struct qdisc_util *q; |
| 217 |
char str[300] = {}; |
| 218 |
|
| 219 |
hlist_for_each_safe(n, tmp_cls, root_list) { |
| 220 |
struct hlist_node *c, *tmp_chld; |
| 221 |
struct hlist_head children = {}; |
| 222 |
struct graph_node *cls = container_of(n, struct graph_node, |
| 223 |
hlist); |
| 224 |
|
| 225 |
hlist_for_each_safe(c, tmp_chld, &cls_list) { |
| 226 |
struct graph_node *child = container_of(c, |
| 227 |
struct graph_node, hlist); |
| 228 |
|
| 229 |
if (cls->id == child->parent_id) { |
| 230 |
hlist_del(c); |
| 231 |
hlist_add_head(c, &children); |
| 232 |
cls->nodes_count++; |
| 233 |
child->parent_node = cls; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
graph_indent(buf, cls, 0, 0); |
| 238 |
|
| 239 |
print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id); |
| 240 |
snprintf(str, sizeof(str), |
| 241 |
"+---(%s)", cls_id_str); |
| 242 |
strcat(buf, str); |
| 243 |
|
| 244 |
parse_rtattr_flags(tb, TCA_MAX, (struct rtattr *)cls->data, |
| 245 |
cls->data_len, NLA_F_NESTED); |
| 246 |
|
| 247 |
if (tb[TCA_KIND] == NULL) { |
| 248 |
strcat(buf, " [unknown qdisc kind] "); |
| 249 |
} else { |
| 250 |
const char *kind = rta_getattr_str(tb[TCA_KIND]); |
| 251 |
|
| 252 |
sprintf(str, " %s ", kind); |
| 253 |
strcat(buf, str); |
| 254 |
fprintf(fp, "%s", buf); |
| 255 |
buf[0] = '\0'; |
| 256 |
|
| 257 |
q = get_qdisc_kind(kind); |
| 258 |
if (q && q->print_copt) { |
| 259 |
q->print_copt(q, fp, tb[TCA_OPTIONS]); |
| 260 |
} |
| 261 |
if (q && show_stats) { |
| 262 |
int cls_indent = strlen(q->id) - 2 + |
| 263 |
strlen(cls_id_str); |
| 264 |
struct rtattr *stats = NULL; |
| 265 |
|
| 266 |
graph_indent(buf, cls, 1, cls_indent); |
| 267 |
|
| 268 |
if (tb[TCA_STATS] || tb[TCA_STATS2]) { |
| 269 |
fprintf(fp, "\n"); |
| 270 |
print_tcstats_attr(fp, tb, buf, &stats); |
| 271 |
buf[0] = '\0'; |
| 272 |
} |
| 273 |
if (cls->hlist.next || cls->nodes_count) { |
| 274 |
strcat(buf, "\n"); |
| 275 |
graph_indent(buf, cls, 1, 0); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
free(cls->data); |
| 280 |
fprintf(fp, "%s\n", buf); |
| 281 |
buf[0] = '\0'; |
| 282 |
|
| 283 |
graph_cls_show(fp, buf, &children, level + 1); |
| 284 |
if (!cls->hlist.next) { |
| 285 |
graph_indent(buf, cls, 0, 0); |
| 286 |
strcat(buf, "\n"); |
| 287 |
} |
| 288 |
|
| 289 |
fprintf(fp, "%s", buf); |
| 290 |
buf[0] = '\0'; |
| 291 |
free(cls); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
int print_class(struct nlmsghdr *n, void *arg) |
| 296 |
{ |
| 297 |
FILE *fp = (FILE *)arg; |
| 298 |
struct tcmsg *t = NLMSG_DATA(n); |
| 299 |
int len = n->nlmsg_len; |
| 300 |
struct rtattr *tb[TCA_MAX + 1]; |
| 301 |
const struct qdisc_util *q; |
| 302 |
char abuf[256]; |
| 303 |
|
| 304 |
if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) { |
| 305 |
fprintf(stderr, "Not a class\n"); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
len -= NLMSG_LENGTH(sizeof(*t)); |
| 309 |
if (len < 0) { |
| 310 |
fprintf(stderr, "Wrong len %d\n", len); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
if (show_graph) { |
| 315 |
graph_node_add(t->tcm_parent, t->tcm_handle, TCA_RTA(t), len); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc)) |
| 320 |
return 0; |
| 321 |
|
| 322 |
if (filter_classid && t->tcm_handle != filter_classid) |
| 323 |
return 0; |
| 324 |
|
| 325 |
parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED); |
| 326 |
|
| 327 |
if (tb[TCA_KIND] == NULL) { |
| 328 |
fprintf(stderr, "print_class: NULL kind\n"); |
| 329 |
return -1; |
| 330 |
} |
| 331 |
|
| 332 |
open_json_object(NULL); |
| 333 |
if (n->nlmsg_type == RTM_DELTCLASS) |
| 334 |
print_null(PRINT_ANY, "deleted", "deleted ", NULL); |
| 335 |
|
| 336 |
abuf[0] = 0; |
| 337 |
if (t->tcm_handle) { |
| 338 |
if (filter_qdisc) |
| 339 |
print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle)); |
| 340 |
else |
| 341 |
print_tc_classid(abuf, sizeof(abuf), t->tcm_handle); |
| 342 |
} |
| 343 |
print_string(PRINT_ANY, "class", "class %s ", rta_getattr_str(tb[TCA_KIND])); |
| 344 |
print_string(PRINT_ANY, "handle", "%s ", abuf); |
| 345 |
|
| 346 |
if (filter_ifindex == 0) |
| 347 |
print_devname(PRINT_ANY, t->tcm_ifindex); |
| 348 |
|
| 349 |
if (t->tcm_parent == TC_H_ROOT) |
| 350 |
print_bool(PRINT_ANY, "root", "root ", true); |
| 351 |
else { |
| 352 |
if (filter_qdisc) |
| 353 |
print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent)); |
| 354 |
else |
| 355 |
print_tc_classid(abuf, sizeof(abuf), t->tcm_parent); |
| 356 |
print_string(PRINT_ANY, "parent", "parent %s ", abuf); |
| 357 |
} |
| 358 |
if (t->tcm_info) |
| 359 |
print_0xhex(PRINT_ANY, "leaf", "leaf %x: ", t->tcm_info>>16); |
| 360 |
|
| 361 |
q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND])); |
| 362 |
if (tb[TCA_OPTIONS]) { |
| 363 |
if (q && q->print_copt) |
| 364 |
q->print_copt(q, fp, tb[TCA_OPTIONS]); |
| 365 |
else |
| 366 |
fprintf(stderr, "[cannot parse class parameters]"); |
| 367 |
} |
| 368 |
print_nl(); |
| 369 |
if (show_stats) { |
| 370 |
struct rtattr *xstats = NULL; |
| 371 |
open_json_object("stats"); |
| 372 |
if (tb[TCA_STATS] || tb[TCA_STATS2]) { |
| 373 |
print_tcstats_attr(fp, tb, " ", &xstats); |
| 374 |
print_string(PRINT_FP, NULL, "\n", NULL); |
| 375 |
} |
| 376 |
if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) { |
| 377 |
q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]); |
| 378 |
print_string(PRINT_FP, NULL, "\n", NULL); |
| 379 |
} |
| 380 |
close_json_object(); |
| 381 |
} |
| 382 |
close_json_object(); |
| 383 |
fflush(fp); |
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
static int tc_class_list(int argc, char **argv) |
| 389 |
{ |
| 390 |
struct tcmsg t = { .tcm_family = AF_UNSPEC }; |
| 391 |
char d[IFNAMSIZ] = {}; |
| 392 |
char buf[1024] = {0}; |
| 393 |
|
| 394 |
filter_qdisc = 0; |
| 395 |
filter_classid = 0; |
| 396 |
|
| 397 |
while (argc > 0) { |
| 398 |
if (strcmp(*argv, "dev") == 0) { |
| 399 |
NEXT_ARG(); |
| 400 |
if (d[0]) |
| 401 |
duparg("dev", *argv); |
| 402 |
strncpy(d, *argv, sizeof(d)-1); |
| 403 |
} else if (strcmp(*argv, "qdisc") == 0) { |
| 404 |
NEXT_ARG(); |
| 405 |
if (filter_qdisc) |
| 406 |
duparg("qdisc", *argv); |
| 407 |
if (get_qdisc_handle(&filter_qdisc, *argv)) |
| 408 |
invarg("invalid qdisc ID", *argv); |
| 409 |
} else if (strcmp(*argv, "classid") == 0) { |
| 410 |
NEXT_ARG(); |
| 411 |
if (filter_classid) |
| 412 |
duparg("classid", *argv); |
| 413 |
if (get_tc_classid(&filter_classid, *argv)) |
| 414 |
invarg("invalid class ID", *argv); |
| 415 |
} else if (strcmp(*argv, "root") == 0) { |
| 416 |
if (t.tcm_parent) { |
| 417 |
fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); |
| 418 |
return -1; |
| 419 |
} |
| 420 |
t.tcm_parent = TC_H_ROOT; |
| 421 |
} else if (strcmp(*argv, "parent") == 0) { |
| 422 |
__u32 handle; |
| 423 |
|
| 424 |
if (t.tcm_parent) |
| 425 |
duparg("parent", *argv); |
| 426 |
NEXT_ARG(); |
| 427 |
if (get_tc_classid(&handle, *argv)) |
| 428 |
invarg("invalid parent ID", *argv); |
| 429 |
t.tcm_parent = handle; |
| 430 |
} else if (matches(*argv, "help") == 0) { |
| 431 |
usage(); |
| 432 |
} else { |
| 433 |
fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv); |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
argc--; argv++; |
| 438 |
} |
| 439 |
|
| 440 |
ll_init_map(&rth); |
| 441 |
|
| 442 |
if (d[0]) { |
| 443 |
t.tcm_ifindex = ll_name_to_index(d); |
| 444 |
if (!t.tcm_ifindex) |
| 445 |
return -nodev(d); |
| 446 |
filter_ifindex = t.tcm_ifindex; |
| 447 |
} |
| 448 |
|
| 449 |
if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { |
| 450 |
perror("Cannot send dump request"); |
| 451 |
return 1; |
| 452 |
} |
| 453 |
new_json_obj(json); |
| 454 |
if (rtnl_dump_filter(&rth, print_class, stdout) < 0) { |
| 455 |
fprintf(stderr, "Dump terminated\n"); |
| 456 |
delete_json_obj(); |
| 457 |
return 1; |
| 458 |
} |
| 459 |
delete_json_obj(); |
| 460 |
|
| 461 |
if (show_graph) |
| 462 |
graph_cls_show(stdout, &buf[0], &root_cls_list, 0); |
| 463 |
|
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
int do_class(int argc, char **argv) |
| 468 |
{ |
| 469 |
if (argc < 1) |
| 470 |
return tc_class_list(0, NULL); |
| 471 |
if (matches(*argv, "add") == 0) |
| 472 |
return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); |
| 473 |
if (matches(*argv, "change") == 0) |
| 474 |
return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1); |
| 475 |
if (matches(*argv, "replace") == 0) |
| 476 |
return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1); |
| 477 |
if (matches(*argv, "delete") == 0) |
| 478 |
return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1); |
| 479 |
if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 480 |
|| matches(*argv, "lst") == 0) |
| 481 |
return tc_class_list(argc-1, argv+1); |
| 482 |
if (matches(*argv, "help") == 0) { |
| 483 |
usage(); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv); |
| 487 |
return -1; |
| 488 |
} |