| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc_qdisc.c "tc qdisc". |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
* J Hadi Salim: Extension to ingress |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <string.h> |
| 17 |
#include <math.h> |
| 18 |
#include <malloc.h> |
| 19 |
|
| 20 |
#include "utils.h" |
| 21 |
#include "tc_util.h" |
| 22 |
#include "tc_common.h" |
| 23 |
|
| 24 |
static int usage(void) |
| 25 |
{ |
| 26 |
fprintf(stderr, |
| 27 |
"Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n" |
| 28 |
" [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n" |
| 29 |
" [ estimator INTERVAL TIME_CONSTANT ]\n" |
| 30 |
" [ stab [ help | STAB_OPTIONS] ]\n" |
| 31 |
" [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n" |
| 32 |
" [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n" |
| 33 |
"\n" |
| 34 |
" tc qdisc { show | list } [ dev STRING ] [ QDISC_ID ] [ invisible ]\n" |
| 35 |
"Where:\n" |
| 36 |
"QDISC_KIND := { [p|b]fifo | tbf | prio | red | etc. }\n" |
| 37 |
"OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n" |
| 38 |
"STAB_OPTIONS := ... try tc qdisc add stab help\n" |
| 39 |
"QDISC_ID := { root | ingress | handle QHANDLE | parent CLASSID }\n"); |
| 40 |
return -1; |
| 41 |
} |
| 42 |
|
| 43 |
static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv) |
| 44 |
{ |
| 45 |
const struct qdisc_util *q = NULL; |
| 46 |
struct tc_estimator est = {}; |
| 47 |
struct { |
| 48 |
struct tc_sizespec szopts; |
| 49 |
__u16 *data; |
| 50 |
} stab = {}; |
| 51 |
char d[IFNAMSIZ] = {}; |
| 52 |
char k[FILTER_NAMESZ] = {}; |
| 53 |
struct { |
| 54 |
struct nlmsghdr n; |
| 55 |
struct tcmsg t; |
| 56 |
char buf[TCA_BUF_MAX]; |
| 57 |
} req = { |
| 58 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 59 |
.n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 60 |
.n.nlmsg_type = cmd, |
| 61 |
.t.tcm_family = AF_UNSPEC, |
| 62 |
}; |
| 63 |
__u32 ingress_block = 0; |
| 64 |
__u32 egress_block = 0; |
| 65 |
|
| 66 |
while (argc > 0) { |
| 67 |
if (strcmp(*argv, "dev") == 0) { |
| 68 |
NEXT_ARG(); |
| 69 |
if (d[0]) |
| 70 |
duparg("dev", *argv); |
| 71 |
strncpy(d, *argv, sizeof(d)-1); |
| 72 |
} else if (strcmp(*argv, "handle") == 0) { |
| 73 |
__u32 handle; |
| 74 |
|
| 75 |
if (req.t.tcm_handle) |
| 76 |
duparg("handle", *argv); |
| 77 |
NEXT_ARG(); |
| 78 |
if (get_qdisc_handle(&handle, *argv)) |
| 79 |
invarg("invalid qdisc ID", *argv); |
| 80 |
req.t.tcm_handle = handle; |
| 81 |
} else if (strcmp(*argv, "root") == 0) { |
| 82 |
if (req.t.tcm_parent) { |
| 83 |
fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
req.t.tcm_parent = TC_H_ROOT; |
| 87 |
} else if (strcmp(*argv, "clsact") == 0) { |
| 88 |
if (req.t.tcm_parent) { |
| 89 |
fprintf(stderr, "Error: \"clsact\" is a duplicate parent ID\n"); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
req.t.tcm_parent = TC_H_CLSACT; |
| 93 |
strncpy(k, "clsact", sizeof(k) - 1); |
| 94 |
q = get_qdisc_kind(k); |
| 95 |
req.t.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0); |
| 96 |
NEXT_ARG_FWD(); |
| 97 |
break; |
| 98 |
} else if (strcmp(*argv, "ingress") == 0) { |
| 99 |
if (req.t.tcm_parent) { |
| 100 |
fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
req.t.tcm_parent = TC_H_INGRESS; |
| 104 |
strncpy(k, "ingress", sizeof(k) - 1); |
| 105 |
q = get_qdisc_kind(k); |
| 106 |
req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0); |
| 107 |
NEXT_ARG_FWD(); |
| 108 |
break; |
| 109 |
} else if (strcmp(*argv, "parent") == 0) { |
| 110 |
__u32 handle; |
| 111 |
|
| 112 |
NEXT_ARG(); |
| 113 |
if (req.t.tcm_parent) |
| 114 |
duparg("parent", *argv); |
| 115 |
if (get_tc_classid(&handle, *argv)) |
| 116 |
invarg("invalid parent ID", *argv); |
| 117 |
req.t.tcm_parent = handle; |
| 118 |
} else if (matches(*argv, "estimator") == 0) { |
| 119 |
if (parse_estimator(&argc, &argv, &est)) |
| 120 |
return -1; |
| 121 |
} else if (matches(*argv, "stab") == 0) { |
| 122 |
if (parse_size_table(&argc, &argv, &stab.szopts) < 0) |
| 123 |
return -1; |
| 124 |
continue; |
| 125 |
} else if (matches(*argv, "ingress_block") == 0) { |
| 126 |
NEXT_ARG(); |
| 127 |
if (get_u32(&ingress_block, *argv, 0) || !ingress_block) |
| 128 |
invarg("invalid ingress block index value", *argv); |
| 129 |
} else if (matches(*argv, "egress_block") == 0) { |
| 130 |
NEXT_ARG(); |
| 131 |
if (get_u32(&egress_block, *argv, 0) || !egress_block) |
| 132 |
invarg("invalid egress block index value", *argv); |
| 133 |
} else if (matches(*argv, "help") == 0) { |
| 134 |
usage(); |
| 135 |
} else { |
| 136 |
strncpy(k, *argv, sizeof(k)-1); |
| 137 |
|
| 138 |
q = get_qdisc_kind(k); |
| 139 |
argc--; argv++; |
| 140 |
break; |
| 141 |
} |
| 142 |
argc--; argv++; |
| 143 |
} |
| 144 |
|
| 145 |
if (k[0]) |
| 146 |
addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1); |
| 147 |
if (est.ewma_log) |
| 148 |
addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); |
| 149 |
|
| 150 |
if (ingress_block) |
| 151 |
addattr32(&req.n, sizeof(req), |
| 152 |
TCA_INGRESS_BLOCK, ingress_block); |
| 153 |
if (egress_block) |
| 154 |
addattr32(&req.n, sizeof(req), |
| 155 |
TCA_EGRESS_BLOCK, egress_block); |
| 156 |
|
| 157 |
if (q) { |
| 158 |
if (q->parse_qopt) { |
| 159 |
if (q->parse_qopt(q, argc, argv, &req.n, d)) |
| 160 |
return 1; |
| 161 |
} else if (argc) { |
| 162 |
fprintf(stderr, "qdisc '%s' does not support option parsing\n", k); |
| 163 |
return -1; |
| 164 |
} |
| 165 |
} else { |
| 166 |
if (argc) { |
| 167 |
if (matches(*argv, "help") == 0) |
| 168 |
usage(); |
| 169 |
|
| 170 |
fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv); |
| 171 |
return -1; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (check_size_table_opts(&stab.szopts)) { |
| 176 |
struct rtattr *tail; |
| 177 |
|
| 178 |
if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) { |
| 179 |
fprintf(stderr, "failed to calculate size table.\n"); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
|
| 183 |
tail = addattr_nest(&req.n, sizeof(req), TCA_STAB); |
| 184 |
addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts, |
| 185 |
sizeof(stab.szopts)); |
| 186 |
if (stab.data) |
| 187 |
addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data, |
| 188 |
stab.szopts.tsize * sizeof(__u16)); |
| 189 |
addattr_nest_end(&req.n, tail); |
| 190 |
free(stab.data); |
| 191 |
} |
| 192 |
|
| 193 |
if (d[0]) { |
| 194 |
int idx; |
| 195 |
|
| 196 |
ll_init_map(&rth); |
| 197 |
|
| 198 |
idx = ll_name_to_index(d); |
| 199 |
if (!idx) |
| 200 |
return -nodev(d); |
| 201 |
req.t.tcm_ifindex = idx; |
| 202 |
} |
| 203 |
|
| 204 |
if (rtnl_talk(&rth, &req.n, NULL) < 0) |
| 205 |
return 2; |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
static int filter_ifindex; |
| 211 |
static __u32 filter_parent; |
| 212 |
static __u32 filter_handle; |
| 213 |
|
| 214 |
int print_qdisc(struct nlmsghdr *n, void *arg) |
| 215 |
{ |
| 216 |
FILE *fp = (FILE *)arg; |
| 217 |
struct tcmsg *t = NLMSG_DATA(n); |
| 218 |
int len = n->nlmsg_len; |
| 219 |
struct rtattr *tb[TCA_MAX+1]; |
| 220 |
const struct qdisc_util *q; |
| 221 |
char abuf[256]; |
| 222 |
|
| 223 |
if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) { |
| 224 |
fprintf(stderr, "Not a qdisc\n"); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
len -= NLMSG_LENGTH(sizeof(*t)); |
| 228 |
if (len < 0) { |
| 229 |
fprintf(stderr, "Wrong len %d\n", len); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
if (filter_ifindex && filter_ifindex != t->tcm_ifindex) |
| 234 |
return 0; |
| 235 |
|
| 236 |
if (filter_handle && filter_handle != t->tcm_handle) |
| 237 |
return 0; |
| 238 |
|
| 239 |
if (filter_parent && filter_parent != t->tcm_parent) |
| 240 |
return 0; |
| 241 |
|
| 242 |
parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED); |
| 243 |
|
| 244 |
if (tb[TCA_KIND] == NULL) { |
| 245 |
fprintf(stderr, "print_qdisc: NULL kind\n"); |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
open_json_object(NULL); |
| 250 |
|
| 251 |
if (n->nlmsg_type == RTM_DELQDISC) |
| 252 |
print_bool(PRINT_ANY, "deleted", "deleted ", true); |
| 253 |
|
| 254 |
if (n->nlmsg_type == RTM_NEWQDISC && |
| 255 |
(n->nlmsg_flags & NLM_F_CREATE) && |
| 256 |
(n->nlmsg_flags & NLM_F_REPLACE)) |
| 257 |
print_bool(PRINT_ANY, "replaced", "replaced ", true); |
| 258 |
|
| 259 |
if (n->nlmsg_type == RTM_NEWQDISC && |
| 260 |
(n->nlmsg_flags & NLM_F_CREATE) && |
| 261 |
(n->nlmsg_flags & NLM_F_EXCL)) |
| 262 |
print_bool(PRINT_ANY, "added", "added ", true); |
| 263 |
|
| 264 |
print_string(PRINT_ANY, "kind", "qdisc %s", |
| 265 |
rta_getattr_str(tb[TCA_KIND])); |
| 266 |
sprintf(abuf, "%x:", t->tcm_handle >> 16); |
| 267 |
print_string(PRINT_ANY, "handle", " %s", abuf); |
| 268 |
if (show_raw) { |
| 269 |
sprintf(abuf, "[%08x]", t->tcm_handle); |
| 270 |
print_string(PRINT_FP, NULL, "%s", abuf); |
| 271 |
} |
| 272 |
print_string(PRINT_FP, NULL, " ", NULL); |
| 273 |
|
| 274 |
if (filter_ifindex == 0) |
| 275 |
print_devname(PRINT_ANY, t->tcm_ifindex); |
| 276 |
|
| 277 |
if (t->tcm_parent == TC_H_ROOT) |
| 278 |
print_bool(PRINT_ANY, "root", "root ", true); |
| 279 |
else if (t->tcm_parent) { |
| 280 |
print_tc_classid(abuf, sizeof(abuf), t->tcm_parent); |
| 281 |
print_string(PRINT_ANY, "parent", "parent %s ", abuf); |
| 282 |
} |
| 283 |
|
| 284 |
if (t->tcm_info != 1) |
| 285 |
print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info); |
| 286 |
|
| 287 |
if (tb[TCA_HW_OFFLOAD] && |
| 288 |
(rta_getattr_u8(tb[TCA_HW_OFFLOAD]))) |
| 289 |
print_bool(PRINT_ANY, "offloaded", "offloaded ", true); |
| 290 |
|
| 291 |
if (tb[TCA_INGRESS_BLOCK] && |
| 292 |
RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) { |
| 293 |
__u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]); |
| 294 |
|
| 295 |
if (block) |
| 296 |
print_uint(PRINT_ANY, "ingress_block", |
| 297 |
"ingress_block %u ", block); |
| 298 |
} |
| 299 |
|
| 300 |
if (tb[TCA_EGRESS_BLOCK] && |
| 301 |
RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) { |
| 302 |
__u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]); |
| 303 |
|
| 304 |
if (block) |
| 305 |
print_uint(PRINT_ANY, "egress_block", |
| 306 |
"egress_block %u ", block); |
| 307 |
} |
| 308 |
|
| 309 |
/* pfifo_fast is generic enough to warrant the hardcoding --JHS */ |
| 310 |
if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0) |
| 311 |
q = get_qdisc_kind("prio"); |
| 312 |
else |
| 313 |
q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND])); |
| 314 |
|
| 315 |
open_json_object("options"); |
| 316 |
if (tb[TCA_OPTIONS]) { |
| 317 |
if (q) |
| 318 |
q->print_qopt(q, fp, tb[TCA_OPTIONS]); |
| 319 |
else |
| 320 |
fprintf(stderr, "Cannot parse qdisc parameters\n"); |
| 321 |
} |
| 322 |
close_json_object(); |
| 323 |
|
| 324 |
print_nl(); |
| 325 |
|
| 326 |
if (show_details && tb[TCA_STAB]) { |
| 327 |
print_size_table(tb[TCA_STAB]); |
| 328 |
print_nl(); |
| 329 |
} |
| 330 |
|
| 331 |
if (show_stats) { |
| 332 |
struct rtattr *xstats = NULL; |
| 333 |
|
| 334 |
if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) { |
| 335 |
print_tcstats_attr(fp, tb, " ", &xstats); |
| 336 |
print_nl(); |
| 337 |
} |
| 338 |
|
| 339 |
if (q && xstats && q->print_xstats) { |
| 340 |
q->print_xstats(q, fp, xstats); |
| 341 |
print_nl(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
print_ext_msg(tb); |
| 346 |
close_json_object(); |
| 347 |
fflush(fp); |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
static int tc_qdisc_list(int argc, char **argv) |
| 352 |
{ |
| 353 |
struct { |
| 354 |
struct nlmsghdr n; |
| 355 |
struct tcmsg t; |
| 356 |
char buf[256]; |
| 357 |
} req = { |
| 358 |
.n.nlmsg_type = RTM_GETQDISC, |
| 359 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 360 |
.t.tcm_family = AF_UNSPEC, |
| 361 |
}; |
| 362 |
|
| 363 |
char d[IFNAMSIZ] = {}; |
| 364 |
bool dump_invisible = false; |
| 365 |
__u32 handle; |
| 366 |
|
| 367 |
while (argc > 0) { |
| 368 |
if (strcmp(*argv, "dev") == 0) { |
| 369 |
NEXT_ARG(); |
| 370 |
strncpy(d, *argv, sizeof(d)-1); |
| 371 |
} else if (strcmp(*argv, "root") == 0) { |
| 372 |
if (filter_parent) |
| 373 |
invarg("parent is already specified", *argv); |
| 374 |
else if (filter_handle) |
| 375 |
invarg("handle is already specified", *argv); |
| 376 |
filter_parent = TC_H_ROOT; |
| 377 |
} else if (strcmp(*argv, "ingress") == 0 || |
| 378 |
strcmp(*argv, "clsact") == 0) { |
| 379 |
if (filter_parent) |
| 380 |
invarg("parent is already specified", *argv); |
| 381 |
else if (filter_handle) |
| 382 |
invarg("handle is already specified", *argv); |
| 383 |
filter_parent = TC_H_INGRESS; |
| 384 |
} else if (matches(*argv, "parent") == 0) { |
| 385 |
if (filter_parent) |
| 386 |
invarg("parent is already specified", *argv); |
| 387 |
else if (filter_handle) |
| 388 |
invarg("handle is already specified", *argv); |
| 389 |
NEXT_ARG(); |
| 390 |
if (get_tc_classid(&handle, *argv)) |
| 391 |
invarg("invalid parent ID", *argv); |
| 392 |
filter_parent = handle; |
| 393 |
} else if (matches(*argv, "handle") == 0) { |
| 394 |
if (filter_parent) |
| 395 |
invarg("parent is already specified", *argv); |
| 396 |
else if (filter_handle) |
| 397 |
invarg("handle is already specified", *argv); |
| 398 |
NEXT_ARG(); |
| 399 |
if (get_qdisc_handle(&handle, *argv)) |
| 400 |
invarg("invalid handle ID", *argv); |
| 401 |
filter_handle = handle; |
| 402 |
} else if (matches(*argv, "help") == 0) { |
| 403 |
usage(); |
| 404 |
} else if (strcmp(*argv, "invisible") == 0) { |
| 405 |
dump_invisible = true; |
| 406 |
} else { |
| 407 |
fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv); |
| 408 |
return -1; |
| 409 |
} |
| 410 |
|
| 411 |
argc--; argv++; |
| 412 |
} |
| 413 |
|
| 414 |
ll_init_map(&rth); |
| 415 |
|
| 416 |
if (d[0]) { |
| 417 |
req.t.tcm_ifindex = ll_name_to_index(d); |
| 418 |
if (!req.t.tcm_ifindex) |
| 419 |
return -nodev(d); |
| 420 |
filter_ifindex = req.t.tcm_ifindex; |
| 421 |
} |
| 422 |
|
| 423 |
if (dump_invisible) { |
| 424 |
addattr(&req.n, 256, TCA_DUMP_INVISIBLE); |
| 425 |
} |
| 426 |
|
| 427 |
if (rtnl_dump_request_n(&rth, &req.n) < 0) { |
| 428 |
perror("Cannot send request"); |
| 429 |
return 1; |
| 430 |
} |
| 431 |
|
| 432 |
new_json_obj(json); |
| 433 |
if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) { |
| 434 |
fprintf(stderr, "Dump terminated\n"); |
| 435 |
delete_json_obj(); |
| 436 |
return 1; |
| 437 |
} |
| 438 |
delete_json_obj(); |
| 439 |
|
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
int do_qdisc(int argc, char **argv) |
| 444 |
{ |
| 445 |
if (argc < 1) |
| 446 |
return tc_qdisc_list(0, NULL); |
| 447 |
if (matches(*argv, "add") == 0) |
| 448 |
return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); |
| 449 |
if (matches(*argv, "change") == 0) |
| 450 |
return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1); |
| 451 |
if (matches(*argv, "replace") == 0) |
| 452 |
return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); |
| 453 |
if (matches(*argv, "link") == 0) |
| 454 |
return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1); |
| 455 |
if (matches(*argv, "delete") == 0) |
| 456 |
return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1); |
| 457 |
if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 458 |
|| matches(*argv, "lst") == 0) |
| 459 |
return tc_qdisc_list(argc-1, argv+1); |
| 460 |
if (matches(*argv, "help") == 0) { |
| 461 |
usage(); |
| 462 |
return 0; |
| 463 |
} |
| 464 |
fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv); |
| 465 |
return -1; |
| 466 |
} |
| 467 |
|
| 468 |
struct tc_qdisc_block_exists_ctx { |
| 469 |
__u32 block_index; |
| 470 |
bool found; |
| 471 |
}; |
| 472 |
|
| 473 |
static int tc_qdisc_block_exists_cb(struct nlmsghdr *n, void *arg) |
| 474 |
{ |
| 475 |
struct tc_qdisc_block_exists_ctx *ctx = arg; |
| 476 |
struct tcmsg *t = NLMSG_DATA(n); |
| 477 |
struct rtattr *tb[TCA_MAX+1]; |
| 478 |
int len = n->nlmsg_len; |
| 479 |
const struct qdisc_util *q; |
| 480 |
const char *kind; |
| 481 |
int err; |
| 482 |
|
| 483 |
if (n->nlmsg_type != RTM_NEWQDISC) |
| 484 |
return 0; |
| 485 |
|
| 486 |
len -= NLMSG_LENGTH(sizeof(*t)); |
| 487 |
if (len < 0) |
| 488 |
return -1; |
| 489 |
|
| 490 |
parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED); |
| 491 |
|
| 492 |
if (tb[TCA_KIND] == NULL) |
| 493 |
return -1; |
| 494 |
|
| 495 |
if (tb[TCA_INGRESS_BLOCK] && |
| 496 |
RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) { |
| 497 |
__u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]); |
| 498 |
|
| 499 |
if (block == ctx->block_index) |
| 500 |
ctx->found = true; |
| 501 |
} |
| 502 |
|
| 503 |
if (tb[TCA_EGRESS_BLOCK] && |
| 504 |
RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) { |
| 505 |
__u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]); |
| 506 |
|
| 507 |
if (block == ctx->block_index) |
| 508 |
ctx->found = true; |
| 509 |
} |
| 510 |
|
| 511 |
kind = rta_getattr_str(tb[TCA_KIND]); |
| 512 |
q = get_qdisc_kind(kind); |
| 513 |
if (!q) |
| 514 |
return -1; |
| 515 |
if (q->has_block) { |
| 516 |
bool found = false; |
| 517 |
|
| 518 |
err = q->has_block(q, tb[TCA_OPTIONS], ctx->block_index, &found); |
| 519 |
if (err) |
| 520 |
return err; |
| 521 |
if (found) |
| 522 |
ctx->found = true; |
| 523 |
} |
| 524 |
|
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
bool tc_qdisc_block_exists(__u32 block_index) |
| 529 |
{ |
| 530 |
struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index }; |
| 531 |
struct tcmsg t = { .tcm_family = AF_UNSPEC }; |
| 532 |
|
| 533 |
if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { |
| 534 |
perror("Cannot send dump request"); |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) { |
| 539 |
perror("Dump terminated\n"); |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
return ctx.found; |
| 544 |
} |