| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
* Copyright © 2019 VMware, Inc. */ |
| 3 |
|
| 4 |
#include <linux/pkt_sched.h> |
| 5 |
|
| 6 |
#include "sd-netlink.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "conf-parser.h" |
| 10 |
#include "drr.h" |
| 11 |
#include "htb.h" |
| 12 |
#include "networkd-link.h" |
| 13 |
#include "networkd-manager.h" |
| 14 |
#include "networkd-network.h" |
| 15 |
#include "networkd-queue.h" |
| 16 |
#include "ordered-set.h" |
| 17 |
#include "qfq.h" |
| 18 |
#include "set.h" |
| 19 |
#include "siphash24.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "tc-util.h" |
| 22 |
#include "tclass.h" |
| 23 |
|
| 24 |
const TClassVTable * const tclass_vtable[_TCLASS_KIND_MAX] = { |
| 25 |
[TCLASS_KIND_DRR] = &drr_tclass_vtable, |
| 26 |
[TCLASS_KIND_HTB] = &htb_tclass_vtable, |
| 27 |
[TCLASS_KIND_QFQ] = &qfq_tclass_vtable, |
| 28 |
}; |
| 29 |
|
| 30 |
static TClass* tclass_detach_impl(TClass *tclass) { |
| 31 |
assert(tclass); |
| 32 |
assert(!tclass->link || !tclass->network); |
| 33 |
|
| 34 |
if (tclass->network) { |
| 35 |
assert(tclass->section); |
| 36 |
hashmap_remove(tclass->network->tclasses_by_section, tclass->section); |
| 37 |
|
| 38 |
tclass->network = NULL; |
| 39 |
return tclass; |
| 40 |
} |
| 41 |
|
| 42 |
if (tclass->link) { |
| 43 |
set_remove(tclass->link->tclasses, tclass); |
| 44 |
|
| 45 |
tclass->link = NULL; |
| 46 |
return tclass; |
| 47 |
} |
| 48 |
|
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
static void tclass_detach(TClass *tclass) { |
| 53 |
assert(tclass); |
| 54 |
|
| 55 |
tclass_unref(tclass_detach_impl(tclass)); |
| 56 |
} |
| 57 |
|
| 58 |
static void tclass_hash_func(const TClass *tclass, struct siphash *state); |
| 59 |
static int tclass_compare_func(const TClass *a, const TClass *b); |
| 60 |
|
| 61 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 62 |
tclass_hash_ops, |
| 63 |
TClass, |
| 64 |
tclass_hash_func, |
| 65 |
tclass_compare_func, |
| 66 |
tclass_detach); |
| 67 |
|
| 68 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 69 |
tclass_section_hash_ops, |
| 70 |
ConfigSection, |
| 71 |
config_section_hash_func, |
| 72 |
config_section_compare_func, |
| 73 |
TClass, |
| 74 |
tclass_detach); |
| 75 |
|
| 76 |
static int tclass_new(TClassKind kind, TClass **ret) { |
| 77 |
_cleanup_(tclass_unrefp) TClass *tclass = NULL; |
| 78 |
int r; |
| 79 |
|
| 80 |
assert(ret); |
| 81 |
|
| 82 |
if (kind == _TCLASS_KIND_INVALID) { |
| 83 |
tclass = new(TClass, 1); |
| 84 |
if (!tclass) |
| 85 |
return -ENOMEM; |
| 86 |
|
| 87 |
*tclass = (TClass) { |
| 88 |
.n_ref = 1, |
| 89 |
.parent = TC_H_ROOT, |
| 90 |
.kind = kind, |
| 91 |
}; |
| 92 |
} else { |
| 93 |
assert(kind >= 0 && kind < _TCLASS_KIND_MAX); |
| 94 |
tclass = malloc0(tclass_vtable[kind]->object_size); |
| 95 |
if (!tclass) |
| 96 |
return -ENOMEM; |
| 97 |
|
| 98 |
tclass->n_ref = 1; |
| 99 |
tclass->parent = TC_H_ROOT; |
| 100 |
tclass->kind = kind; |
| 101 |
|
| 102 |
if (TCLASS_VTABLE(tclass)->init) { |
| 103 |
r = TCLASS_VTABLE(tclass)->init(tclass); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
*ret = TAKE_PTR(tclass); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int tclass_new_static(TClassKind kind, Network *network, const char *filename, unsigned section_line, TClass **ret) { |
| 115 |
_cleanup_(config_section_freep) ConfigSection *n = NULL; |
| 116 |
_cleanup_(tclass_unrefp) TClass *tclass = NULL; |
| 117 |
TClass *existing; |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(network); |
| 121 |
assert(ret); |
| 122 |
assert(filename); |
| 123 |
assert(section_line > 0); |
| 124 |
|
| 125 |
r = config_section_new(filename, section_line, &n); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
|
| 129 |
existing = hashmap_get(network->tclasses_by_section, n); |
| 130 |
if (existing) { |
| 131 |
if (existing->kind != kind) |
| 132 |
return -EINVAL; |
| 133 |
|
| 134 |
*ret = existing; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
r = tclass_new(kind, &tclass); |
| 139 |
if (r < 0) |
| 140 |
return r; |
| 141 |
|
| 142 |
tclass->network = network; |
| 143 |
tclass->section = TAKE_PTR(n); |
| 144 |
tclass->source = NETWORK_CONFIG_SOURCE_STATIC; |
| 145 |
|
| 146 |
r = hashmap_ensure_put(&network->tclasses_by_section, &tclass_section_hash_ops, tclass->section, tclass); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
*ret = TAKE_PTR(tclass); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static TClass* tclass_free(TClass *tclass) { |
| 155 |
if (!tclass) |
| 156 |
return NULL; |
| 157 |
|
| 158 |
tclass_detach_impl(tclass); |
| 159 |
|
| 160 |
config_section_free(tclass->section); |
| 161 |
|
| 162 |
free(tclass->tca_kind); |
| 163 |
return mfree(tclass); |
| 164 |
} |
| 165 |
|
| 166 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(TClass, tclass, tclass_free); |
| 167 |
|
| 168 |
static const char *tclass_get_tca_kind(const TClass *tclass) { |
| 169 |
assert(tclass); |
| 170 |
|
| 171 |
return (TCLASS_VTABLE(tclass) && TCLASS_VTABLE(tclass)->tca_kind) ? |
| 172 |
TCLASS_VTABLE(tclass)->tca_kind : tclass->tca_kind; |
| 173 |
} |
| 174 |
|
| 175 |
static void tclass_hash_func(const TClass *tclass, struct siphash *state) { |
| 176 |
assert(tclass); |
| 177 |
assert(state); |
| 178 |
|
| 179 |
siphash24_compress_typesafe(tclass->classid, state); |
| 180 |
siphash24_compress_typesafe(tclass->parent, state); |
| 181 |
siphash24_compress_string(tclass_get_tca_kind(tclass), state); |
| 182 |
} |
| 183 |
|
| 184 |
static int tclass_compare_func(const TClass *a, const TClass *b) { |
| 185 |
int r; |
| 186 |
|
| 187 |
assert(a); |
| 188 |
assert(b); |
| 189 |
|
| 190 |
r = CMP(a->classid, b->classid); |
| 191 |
if (r != 0) |
| 192 |
return r; |
| 193 |
|
| 194 |
r = CMP(a->parent, b->parent); |
| 195 |
if (r != 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
return strcmp_ptr(tclass_get_tca_kind(a), tclass_get_tca_kind(b)); |
| 199 |
} |
| 200 |
|
| 201 |
static int tclass_get(Link *link, const TClass *in, TClass **ret) { |
| 202 |
TClass *existing; |
| 203 |
|
| 204 |
assert(link); |
| 205 |
assert(in); |
| 206 |
|
| 207 |
existing = set_get(link->tclasses, in); |
| 208 |
if (!existing) |
| 209 |
return -ENOENT; |
| 210 |
|
| 211 |
if (ret) |
| 212 |
*ret = existing; |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
static int tclass_get_request(Link *link, const TClass *tclass, Request **ret) { |
| 217 |
Request *req; |
| 218 |
|
| 219 |
assert(link); |
| 220 |
assert(link->manager); |
| 221 |
assert(tclass); |
| 222 |
|
| 223 |
req = ordered_set_get( |
| 224 |
link->manager->request_queue, |
| 225 |
&(Request) { |
| 226 |
.link = link, |
| 227 |
.type = REQUEST_TYPE_TC_CLASS, |
| 228 |
.userdata = (void*) tclass, |
| 229 |
.hash_func = (hash_func_t) tclass_hash_func, |
| 230 |
.compare_func = (compare_func_t) tclass_compare_func, |
| 231 |
}); |
| 232 |
if (!req) |
| 233 |
return -ENOENT; |
| 234 |
|
| 235 |
if (ret) |
| 236 |
*ret = req; |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int tclass_attach(Link *link, TClass *tclass) { |
| 241 |
int r; |
| 242 |
|
| 243 |
assert(link); |
| 244 |
assert(tclass); |
| 245 |
assert(!tclass->link); |
| 246 |
assert(!tclass->network); |
| 247 |
|
| 248 |
r = set_ensure_put(&link->tclasses, &tclass_hash_ops, tclass); |
| 249 |
if (r < 0) |
| 250 |
return r; |
| 251 |
if (r == 0) |
| 252 |
return -EEXIST; |
| 253 |
|
| 254 |
tclass->link = link; |
| 255 |
tclass_ref(tclass); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static int tclass_dup(const TClass *src, TClass **ret) { |
| 260 |
_cleanup_(tclass_unrefp) TClass *dst = NULL; |
| 261 |
|
| 262 |
assert(src); |
| 263 |
assert(ret); |
| 264 |
|
| 265 |
if (TCLASS_VTABLE(src)) |
| 266 |
dst = memdup(src, TCLASS_VTABLE(src)->object_size); |
| 267 |
else |
| 268 |
dst = newdup(TClass, src, 1); |
| 269 |
if (!dst) |
| 270 |
return -ENOMEM; |
| 271 |
|
| 272 |
/* clear the reference counter and all pointers */ |
| 273 |
dst->n_ref = 1; |
| 274 |
dst->network = NULL; |
| 275 |
dst->section = NULL; |
| 276 |
dst->link = NULL; |
| 277 |
dst->tca_kind = NULL; |
| 278 |
|
| 279 |
if (src->tca_kind) { |
| 280 |
dst->tca_kind = strdup(src->tca_kind); |
| 281 |
if (!dst->tca_kind) |
| 282 |
return -ENOMEM; |
| 283 |
} |
| 284 |
|
| 285 |
*ret = TAKE_PTR(dst); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
int link_find_tclass(Link *link, uint32_t classid, TClass **ret) { |
| 290 |
TClass *tclass; |
| 291 |
|
| 292 |
assert(link); |
| 293 |
|
| 294 |
SET_FOREACH(tclass, link->tclasses) { |
| 295 |
if (tclass->classid != classid) |
| 296 |
continue; |
| 297 |
|
| 298 |
if (!tclass_exists(tclass)) |
| 299 |
continue; |
| 300 |
|
| 301 |
if (ret) |
| 302 |
*ret = tclass; |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
return -ENOENT; |
| 307 |
} |
| 308 |
|
| 309 |
static void log_tclass_debug(TClass *tclass, Link *link, const char *str) { |
| 310 |
_cleanup_free_ char *state = NULL; |
| 311 |
|
| 312 |
assert(tclass); |
| 313 |
assert(str); |
| 314 |
|
| 315 |
if (!DEBUG_LOGGING) |
| 316 |
return; |
| 317 |
|
| 318 |
(void) network_config_state_to_string_alloc(tclass->state, &state); |
| 319 |
|
| 320 |
log_link_debug(link, "%s %s TClass (%s): classid=%"PRIx32":%"PRIx32", parent=%"PRIx32":%"PRIx32", kind=%s", |
| 321 |
str, strna(network_config_source_to_string(tclass->source)), strna(state), |
| 322 |
TC_H_MAJ(tclass->classid) >> 16, TC_H_MIN(tclass->classid), |
| 323 |
TC_H_MAJ(tclass->parent) >> 16, TC_H_MIN(tclass->parent), |
| 324 |
strna(tclass_get_tca_kind(tclass))); |
| 325 |
} |
| 326 |
|
| 327 |
void tclass_mark_recursive(TClass *tclass) { |
| 328 |
QDisc *qdisc; |
| 329 |
|
| 330 |
assert(tclass); |
| 331 |
assert(tclass->link); |
| 332 |
|
| 333 |
if (tclass_is_marked(tclass)) |
| 334 |
return; |
| 335 |
|
| 336 |
tclass_mark(tclass); |
| 337 |
|
| 338 |
/* Also mark all child qdiscs assigned to the class. */ |
| 339 |
SET_FOREACH(qdisc, tclass->link->qdiscs) { |
| 340 |
if (qdisc->parent != tclass->classid) |
| 341 |
continue; |
| 342 |
|
| 343 |
qdisc_mark_recursive(qdisc); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void link_tclass_drop_marked(Link *link) { |
| 348 |
TClass *tclass; |
| 349 |
|
| 350 |
assert(link); |
| 351 |
|
| 352 |
SET_FOREACH(tclass, link->tclasses) { |
| 353 |
Request *req; |
| 354 |
|
| 355 |
if (!tclass_is_marked(tclass)) |
| 356 |
continue; |
| 357 |
|
| 358 |
tclass_unmark(tclass); |
| 359 |
tclass_enter_removed(tclass); |
| 360 |
if (tclass_get_request(link, tclass, &req) >= 0) |
| 361 |
tclass_enter_removed(req->userdata); |
| 362 |
|
| 363 |
if (tclass->state == 0) { |
| 364 |
log_tclass_debug(tclass, link, "Forgetting"); |
| 365 |
tclass_detach(tclass); |
| 366 |
} else |
| 367 |
log_tclass_debug(tclass, link, "Removed"); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
static void tclass_drop(TClass *tclass) { |
| 372 |
assert(tclass); |
| 373 |
|
| 374 |
tclass_mark_recursive(tclass); |
| 375 |
|
| 376 |
/* link_tclass_drop_marked() may invalidate tclass, so run link_qdisc_drop_marked() first. */ |
| 377 |
link_qdisc_drop_marked(tclass->link); |
| 378 |
link_tclass_drop_marked(tclass->link); |
| 379 |
} |
| 380 |
|
| 381 |
static int tclass_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, TClass *tclass) { |
| 382 |
int r; |
| 383 |
|
| 384 |
assert(m); |
| 385 |
assert(link); |
| 386 |
|
| 387 |
r = sd_netlink_message_get_errno(m); |
| 388 |
if (r < 0 && r != -EEXIST) { |
| 389 |
log_link_message_error_errno(link, m, r, "Could not set TClass"); |
| 390 |
link_enter_failed(link); |
| 391 |
return 1; |
| 392 |
} |
| 393 |
|
| 394 |
if (link->tc_messages == 0) { |
| 395 |
log_link_debug(link, "Traffic control configured"); |
| 396 |
link->tc_configured = true; |
| 397 |
link_check_ready(link); |
| 398 |
} |
| 399 |
|
| 400 |
return 1; |
| 401 |
} |
| 402 |
|
| 403 |
static int tclass_configure(TClass *tclass, Link *link, Request *req) { |
| 404 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 405 |
int r; |
| 406 |
|
| 407 |
assert(tclass); |
| 408 |
assert(link); |
| 409 |
assert(link->manager); |
| 410 |
assert(link->manager->rtnl); |
| 411 |
assert(link->ifindex > 0); |
| 412 |
assert(req); |
| 413 |
|
| 414 |
log_tclass_debug(tclass, link, "Configuring"); |
| 415 |
|
| 416 |
r = sd_rtnl_message_new_traffic_control(link->manager->rtnl, &m, RTM_NEWTCLASS, |
| 417 |
link->ifindex, tclass->classid, tclass->parent); |
| 418 |
if (r < 0) |
| 419 |
return r; |
| 420 |
|
| 421 |
r = sd_netlink_message_append_string(m, TCA_KIND, TCLASS_VTABLE(tclass)->tca_kind); |
| 422 |
if (r < 0) |
| 423 |
return r; |
| 424 |
|
| 425 |
if (TCLASS_VTABLE(tclass)->fill_message) { |
| 426 |
r = TCLASS_VTABLE(tclass)->fill_message(link, tclass, m); |
| 427 |
if (r < 0) |
| 428 |
return r; |
| 429 |
} |
| 430 |
|
| 431 |
return request_call_netlink_async(link->manager->rtnl, m, req); |
| 432 |
} |
| 433 |
|
| 434 |
static bool tclass_is_ready_to_configure(TClass *tclass, Link *link) { |
| 435 |
assert(tclass); |
| 436 |
assert(link); |
| 437 |
|
| 438 |
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 439 |
return false; |
| 440 |
|
| 441 |
return link_find_qdisc(link, TC_H_MAJ(tclass->classid), tclass_get_tca_kind(tclass), NULL) >= 0; |
| 442 |
} |
| 443 |
|
| 444 |
static int tclass_process_request(Request *req, Link *link, TClass *tclass) { |
| 445 |
TClass *existing; |
| 446 |
int r; |
| 447 |
|
| 448 |
assert(req); |
| 449 |
assert(link); |
| 450 |
assert(tclass); |
| 451 |
|
| 452 |
if (!tclass_is_ready_to_configure(tclass, link)) |
| 453 |
return 0; |
| 454 |
|
| 455 |
r = tclass_configure(tclass, link, req); |
| 456 |
if (r < 0) |
| 457 |
return log_link_warning_errno(link, r, "Failed to configure TClass: %m"); |
| 458 |
|
| 459 |
tclass_enter_configuring(tclass); |
| 460 |
if (tclass_get(link, tclass, &existing) >= 0) |
| 461 |
tclass_enter_configuring(existing); |
| 462 |
|
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
int link_request_tclass(Link *link, const TClass *tclass) { |
| 467 |
_cleanup_(tclass_unrefp) TClass *tmp = NULL; |
| 468 |
TClass *existing = NULL; |
| 469 |
int r; |
| 470 |
|
| 471 |
assert(link); |
| 472 |
assert(tclass); |
| 473 |
assert(tclass->source != NETWORK_CONFIG_SOURCE_FOREIGN); |
| 474 |
|
| 475 |
if (tclass_get_request(link, tclass, NULL) >= 0) |
| 476 |
return 0; /* already requested, skipping. */ |
| 477 |
|
| 478 |
r = tclass_dup(tclass, &tmp); |
| 479 |
if (r < 0) |
| 480 |
return r; |
| 481 |
|
| 482 |
if (tclass_get(link, tclass, &existing) >= 0) |
| 483 |
/* Copy state for logging below. */ |
| 484 |
tmp->state = existing->state; |
| 485 |
|
| 486 |
log_tclass_debug(tmp, link, "Requesting"); |
| 487 |
r = link_queue_request_safe(link, REQUEST_TYPE_TC_CLASS, |
| 488 |
tmp, |
| 489 |
tclass_unref, |
| 490 |
tclass_hash_func, |
| 491 |
tclass_compare_func, |
| 492 |
tclass_process_request, |
| 493 |
&link->tc_messages, |
| 494 |
tclass_handler, |
| 495 |
NULL); |
| 496 |
if (r <= 0) |
| 497 |
return r; |
| 498 |
|
| 499 |
tclass_enter_requesting(tmp); |
| 500 |
if (existing) |
| 501 |
tclass_enter_requesting(existing); |
| 502 |
|
| 503 |
TAKE_PTR(tmp); |
| 504 |
return 1; |
| 505 |
} |
| 506 |
|
| 507 |
int manager_rtnl_process_tclass(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
| 508 |
_cleanup_(tclass_unrefp) TClass *tmp = NULL; |
| 509 |
Request *req = NULL; |
| 510 |
TClass *tclass = NULL; |
| 511 |
Link *link; |
| 512 |
uint16_t type; |
| 513 |
int ifindex, r; |
| 514 |
|
| 515 |
assert(rtnl); |
| 516 |
assert(message); |
| 517 |
assert(m); |
| 518 |
|
| 519 |
if (sd_netlink_message_is_error(message)) { |
| 520 |
r = sd_netlink_message_get_errno(message); |
| 521 |
if (r < 0) |
| 522 |
log_message_warning_errno(message, r, "rtnl: failed to receive TClass message, ignoring"); |
| 523 |
|
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
r = sd_netlink_message_get_type(message, &type); |
| 528 |
if (r < 0) { |
| 529 |
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
| 530 |
return 0; |
| 531 |
} else if (!IN_SET(type, RTM_NEWTCLASS, RTM_DELTCLASS)) { |
| 532 |
log_warning("rtnl: received unexpected message type %u when processing TClass, ignoring.", type); |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
r = sd_rtnl_message_traffic_control_get_ifindex(message, &ifindex); |
| 537 |
if (r < 0) { |
| 538 |
log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); |
| 539 |
return 0; |
| 540 |
} else if (ifindex <= 0) { |
| 541 |
log_warning("rtnl: received TClass message with invalid ifindex %d, ignoring.", ifindex); |
| 542 |
return 0; |
| 543 |
} |
| 544 |
|
| 545 |
if (link_get_by_index(m, ifindex, &link) < 0) { |
| 546 |
if (!m->enumerating) |
| 547 |
log_warning("rtnl: received TClass for link '%d' we don't know about, ignoring.", ifindex); |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
r = tclass_new(_TCLASS_KIND_INVALID, &tmp); |
| 552 |
if (r < 0) |
| 553 |
return log_oom(); |
| 554 |
|
| 555 |
r = sd_rtnl_message_traffic_control_get_handle(message, &tmp->classid); |
| 556 |
if (r < 0) { |
| 557 |
log_link_warning_errno(link, r, "rtnl: received TClass message without handle, ignoring: %m"); |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
r = sd_rtnl_message_traffic_control_get_parent(message, &tmp->parent); |
| 562 |
if (r < 0) { |
| 563 |
log_link_warning_errno(link, r, "rtnl: received TClass message without parent, ignoring: %m"); |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
r = sd_netlink_message_read_string_strdup(message, TCA_KIND, &tmp->tca_kind); |
| 568 |
if (r < 0) { |
| 569 |
log_link_warning_errno(link, r, "rtnl: received TClass message without kind, ignoring: %m"); |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
(void) tclass_get(link, tmp, &tclass); |
| 574 |
(void) tclass_get_request(link, tmp, &req); |
| 575 |
|
| 576 |
if (type == RTM_DELTCLASS) { |
| 577 |
if (tclass) |
| 578 |
tclass_drop(tclass); |
| 579 |
else |
| 580 |
log_tclass_debug(tmp, link, "Kernel removed unknown"); |
| 581 |
|
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
bool is_new = false; |
| 586 |
if (!tclass) { |
| 587 |
/* If we did not know the tclass, then save it. */ |
| 588 |
r = tclass_attach(link, tmp); |
| 589 |
if (r < 0) { |
| 590 |
log_link_warning_errno(link, r, "Failed to remember TClass, ignoring: %m"); |
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
tclass = tmp; |
| 595 |
is_new = true; |
| 596 |
} |
| 597 |
|
| 598 |
/* Also update information that cannot be obtained through netlink notification. */ |
| 599 |
if (req && req->waiting_reply) { |
| 600 |
TClass *t = ASSERT_PTR(req->userdata); |
| 601 |
|
| 602 |
tclass->source = t->source; |
| 603 |
} |
| 604 |
|
| 605 |
tclass_enter_configured(tclass); |
| 606 |
if (req) |
| 607 |
tclass_enter_configured(req->userdata); |
| 608 |
|
| 609 |
log_tclass_debug(tclass, link, is_new ? "Remembering" : "Received remembered"); |
| 610 |
return 1; |
| 611 |
} |
| 612 |
|
| 613 |
int link_enumerate_tclass(Link *link, uint32_t parent) { |
| 614 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
| 615 |
int r; |
| 616 |
|
| 617 |
assert(link); |
| 618 |
assert(link->manager); |
| 619 |
assert(link->manager->rtnl); |
| 620 |
|
| 621 |
r = sd_rtnl_message_new_traffic_control(link->manager->rtnl, &req, RTM_GETTCLASS, link->ifindex, 0, parent); |
| 622 |
if (r < 0) |
| 623 |
return r; |
| 624 |
|
| 625 |
return manager_enumerate_internal(link->manager, link->manager->rtnl, req, manager_rtnl_process_tclass); |
| 626 |
} |
| 627 |
|
| 628 |
static int tclass_section_verify(TClass *tclass) { |
| 629 |
int r; |
| 630 |
|
| 631 |
assert(tclass); |
| 632 |
|
| 633 |
if (section_is_invalid(tclass->section)) |
| 634 |
return -EINVAL; |
| 635 |
|
| 636 |
if (TCLASS_VTABLE(tclass)->verify) { |
| 637 |
r = TCLASS_VTABLE(tclass)->verify(tclass); |
| 638 |
if (r < 0) |
| 639 |
return r; |
| 640 |
} |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
void network_drop_invalid_tclass(Network *network) { |
| 646 |
TClass *tclass; |
| 647 |
|
| 648 |
assert(network); |
| 649 |
|
| 650 |
HASHMAP_FOREACH(tclass, network->tclasses_by_section) |
| 651 |
if (tclass_section_verify(tclass) < 0) |
| 652 |
tclass_detach(tclass); |
| 653 |
} |
| 654 |
|
| 655 |
int config_parse_tclass_parent( |
| 656 |
const char *unit, |
| 657 |
const char *filename, |
| 658 |
unsigned line, |
| 659 |
const char *section, |
| 660 |
unsigned section_line, |
| 661 |
const char *lvalue, |
| 662 |
int ltype, |
| 663 |
const char *rvalue, |
| 664 |
void *data, |
| 665 |
void *userdata) { |
| 666 |
|
| 667 |
_cleanup_(tclass_unref_or_set_invalidp) TClass *tclass = NULL; |
| 668 |
Network *network = ASSERT_PTR(data); |
| 669 |
int r; |
| 670 |
|
| 671 |
assert(filename); |
| 672 |
assert(lvalue); |
| 673 |
assert(rvalue); |
| 674 |
|
| 675 |
r = tclass_new_static(ltype, network, filename, section_line, &tclass); |
| 676 |
if (r == -ENOMEM) |
| 677 |
return log_oom(); |
| 678 |
if (r < 0) { |
| 679 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 680 |
"Failed to create traffic control class, ignoring assignment: %m"); |
| 681 |
return 0; |
| 682 |
} |
| 683 |
|
| 684 |
if (streq(rvalue, "root")) |
| 685 |
tclass->parent = TC_H_ROOT; |
| 686 |
else { |
| 687 |
r = parse_handle(rvalue, &tclass->parent); |
| 688 |
if (r < 0) { |
| 689 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 690 |
"Failed to parse 'Parent=', ignoring assignment: %s", |
| 691 |
rvalue); |
| 692 |
return 0; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
TAKE_PTR(tclass); |
| 697 |
|
| 698 |
return 0; |
| 699 |
} |
| 700 |
|
| 701 |
int config_parse_tclass_classid( |
| 702 |
const char *unit, |
| 703 |
const char *filename, |
| 704 |
unsigned line, |
| 705 |
const char *section, |
| 706 |
unsigned section_line, |
| 707 |
const char *lvalue, |
| 708 |
int ltype, |
| 709 |
const char *rvalue, |
| 710 |
void *data, |
| 711 |
void *userdata) { |
| 712 |
|
| 713 |
_cleanup_(tclass_unref_or_set_invalidp) TClass *tclass = NULL; |
| 714 |
Network *network = ASSERT_PTR(data); |
| 715 |
int r; |
| 716 |
|
| 717 |
assert(filename); |
| 718 |
assert(lvalue); |
| 719 |
assert(rvalue); |
| 720 |
|
| 721 |
r = tclass_new_static(ltype, network, filename, section_line, &tclass); |
| 722 |
if (r == -ENOMEM) |
| 723 |
return log_oom(); |
| 724 |
if (r < 0) { |
| 725 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 726 |
"Failed to create traffic control class, ignoring assignment: %m"); |
| 727 |
return 0; |
| 728 |
} |
| 729 |
|
| 730 |
if (isempty(rvalue)) { |
| 731 |
tclass->classid = TC_H_UNSPEC; |
| 732 |
TAKE_PTR(tclass); |
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
r = parse_handle(rvalue, &tclass->classid); |
| 737 |
if (r < 0) { |
| 738 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 739 |
"Failed to parse 'ClassId=', ignoring assignment: %s", |
| 740 |
rvalue); |
| 741 |
return 0; |
| 742 |
} |
| 743 |
|
| 744 |
TAKE_PTR(tclass); |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |