1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/bits.h> 4 #include <linux/bitfield.h> 5 #include <linux/idr.h> 6 #include <linux/kernel.h> 7 #include <linux/netdevice.h> 8 #include <linux/netlink.h> 9 #include <linux/skbuff.h> 10 #include <linux/xarray.h> 11 #include <net/devlink.h> 12 #include <net/net_shaper.h> 13 14 #include "shaper_nl_gen.h" 15 16 #include "../core/dev.h" 17 18 #define NET_SHAPER_SCOPE_SHIFT 26 19 #define NET_SHAPER_ID_MASK GENMASK(NET_SHAPER_SCOPE_SHIFT - 1, 0) 20 #define NET_SHAPER_SCOPE_MASK GENMASK(31, NET_SHAPER_SCOPE_SHIFT) 21 22 #define NET_SHAPER_ID_UNSPEC NET_SHAPER_ID_MASK 23 24 static_assert(NET_SHAPER_ID_UNSPEC == NET_SHAPER_MAX_HANDLE_ID + 1); 25 26 struct net_shaper_hierarchy { 27 struct xarray shapers; 28 }; 29 30 struct net_shaper_nl_ctx { 31 struct net_shaper_binding binding; 32 netdevice_tracker dev_tracker; 33 unsigned long start_index; 34 }; 35 36 static struct net_shaper_binding *net_shaper_binding_from_ctx(void *ctx) 37 { 38 return &((struct net_shaper_nl_ctx *)ctx)->binding; 39 } 40 41 static struct net_shaper_hierarchy * 42 net_shaper_hierarchy(struct net_shaper_binding *binding) 43 { 44 /* Pairs with WRITE_ONCE() in net_shaper_hierarchy_setup. */ 45 if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV) 46 return READ_ONCE(binding->netdev->net_shaper_hierarchy); 47 48 /* No other type supported yet. */ 49 return NULL; 50 } 51 52 static struct net_shaper_hierarchy * 53 net_shaper_hierarchy_rcu(struct net_shaper_binding *binding) 54 { 55 /* Readers look up the device and take a ref, then take RCU lock 56 * later at which point netdev may have been unregistered and flushed. 57 * READ_ONCE() pairs with WRITE_ONCE() in net_shaper_hierarchy_setup. 58 */ 59 if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV && 60 READ_ONCE(binding->netdev->reg_state) <= NETREG_REGISTERED) 61 return READ_ONCE(binding->netdev->net_shaper_hierarchy); 62 63 /* No other type supported yet. */ 64 return NULL; 65 } 66 67 static const struct net_shaper_ops * 68 net_shaper_ops(struct net_shaper_binding *binding) 69 { 70 if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV) 71 return binding->netdev->netdev_ops->net_shaper_ops; 72 73 /* No other type supported yet. */ 74 return NULL; 75 } 76 77 /* Count the number of [multi] attributes of the given type. */ 78 static int net_shaper_list_len(struct genl_info *info, int type) 79 { 80 struct nlattr *attr; 81 int rem, cnt = 0; 82 83 nla_for_each_attr_type(attr, type, genlmsg_data(info->genlhdr), 84 genlmsg_len(info->genlhdr), rem) 85 cnt++; 86 return cnt; 87 } 88 89 static int net_shaper_handle_size(void) 90 { 91 return nla_total_size(nla_total_size(sizeof(u32)) + 92 nla_total_size(sizeof(u32))); 93 } 94 95 static int net_shaper_group_reply_size(void) 96 { 97 return nla_total_size(sizeof(u32)) + /* NET_SHAPER_A_IFINDEX */ 98 net_shaper_handle_size(); /* NET_SHAPER_A_HANDLE */ 99 } 100 101 static int net_shaper_fill_binding(struct sk_buff *msg, 102 const struct net_shaper_binding *binding, 103 u32 type) 104 { 105 /* Should never happen, as currently only NETDEV is supported. */ 106 if (WARN_ON_ONCE(binding->type != NET_SHAPER_BINDING_TYPE_NETDEV)) 107 return -EINVAL; 108 109 if (nla_put_u32(msg, type, binding->netdev->ifindex)) 110 return -EMSGSIZE; 111 112 return 0; 113 } 114 115 static int net_shaper_fill_handle(struct sk_buff *msg, 116 const struct net_shaper_handle *handle, 117 u32 type) 118 { 119 struct nlattr *handle_attr; 120 121 if (handle->scope == NET_SHAPER_SCOPE_UNSPEC) 122 return 0; 123 124 handle_attr = nla_nest_start(msg, type); 125 if (!handle_attr) 126 return -EMSGSIZE; 127 128 if (nla_put_u32(msg, NET_SHAPER_A_HANDLE_SCOPE, handle->scope) || 129 (handle->scope >= NET_SHAPER_SCOPE_QUEUE && 130 nla_put_u32(msg, NET_SHAPER_A_HANDLE_ID, handle->id))) 131 goto handle_nest_cancel; 132 133 nla_nest_end(msg, handle_attr); 134 return 0; 135 136 handle_nest_cancel: 137 nla_nest_cancel(msg, handle_attr); 138 return -EMSGSIZE; 139 } 140 141 static void net_shaper_copy(struct net_shaper *dst, 142 const struct net_shaper *src) 143 { 144 WRITE_ONCE(dst->parent.scope, READ_ONCE(src->parent.scope)); 145 WRITE_ONCE(dst->parent.id, READ_ONCE(src->parent.id)); 146 WRITE_ONCE(dst->handle.scope, READ_ONCE(src->handle.scope)); 147 WRITE_ONCE(dst->handle.id, READ_ONCE(src->handle.id)); 148 149 WRITE_ONCE(dst->metric, READ_ONCE(src->metric)); 150 WRITE_ONCE(dst->bw_min, READ_ONCE(src->bw_min)); 151 WRITE_ONCE(dst->bw_max, READ_ONCE(src->bw_max)); 152 WRITE_ONCE(dst->burst, READ_ONCE(src->burst)); 153 WRITE_ONCE(dst->priority, READ_ONCE(src->priority)); 154 WRITE_ONCE(dst->weight, READ_ONCE(src->weight)); 155 156 /* private fields are only used on the write path under the lock */ 157 data_race(dst->leaves = src->leaves); 158 } 159 160 static int 161 net_shaper_fill_one(struct sk_buff *msg, 162 const struct net_shaper_binding *binding, 163 const struct net_shaper *shaper, 164 const struct genl_info *info) 165 { 166 struct net_shaper cur; 167 void *hdr; 168 169 hdr = genlmsg_iput(msg, info); 170 if (!hdr) 171 return -EMSGSIZE; 172 173 /* Make a copy to avoid data races */ 174 net_shaper_copy(&cur, shaper); 175 176 if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_IFINDEX) || 177 net_shaper_fill_handle(msg, &cur.parent, 178 NET_SHAPER_A_PARENT) || 179 net_shaper_fill_handle(msg, &cur.handle, 180 NET_SHAPER_A_HANDLE) || 181 ((cur.bw_min || cur.bw_max || cur.burst) && 182 nla_put_u32(msg, NET_SHAPER_A_METRIC, cur.metric)) || 183 (cur.bw_min && 184 nla_put_uint(msg, NET_SHAPER_A_BW_MIN, cur.bw_min)) || 185 (cur.bw_max && 186 nla_put_uint(msg, NET_SHAPER_A_BW_MAX, cur.bw_max)) || 187 (cur.burst && 188 nla_put_uint(msg, NET_SHAPER_A_BURST, cur.burst)) || 189 (cur.priority && 190 nla_put_u32(msg, NET_SHAPER_A_PRIORITY, cur.priority)) || 191 (cur.weight && 192 nla_put_u32(msg, NET_SHAPER_A_WEIGHT, cur.weight))) 193 goto nla_put_failure; 194 195 genlmsg_end(msg, hdr); 196 197 return 0; 198 199 nla_put_failure: 200 genlmsg_cancel(msg, hdr); 201 return -EMSGSIZE; 202 } 203 204 /* Initialize the context fetching the relevant device and 205 * acquiring a reference to it. 206 */ 207 static int net_shaper_ctx_setup(const struct genl_info *info, int type, 208 struct net_shaper_nl_ctx *ctx) 209 { 210 struct net *ns = genl_info_net(info); 211 struct net_device *dev; 212 int ifindex; 213 214 if (GENL_REQ_ATTR_CHECK(info, type)) 215 return -EINVAL; 216 217 ifindex = nla_get_u32(info->attrs[type]); 218 dev = netdev_get_by_index(ns, ifindex, &ctx->dev_tracker, GFP_KERNEL); 219 if (!dev) { 220 NL_SET_BAD_ATTR(info->extack, info->attrs[type]); 221 return -ENOENT; 222 } 223 224 if (!dev->netdev_ops->net_shaper_ops) { 225 NL_SET_BAD_ATTR(info->extack, info->attrs[type]); 226 netdev_put(dev, &ctx->dev_tracker); 227 return -EOPNOTSUPP; 228 } 229 230 ctx->binding.type = NET_SHAPER_BINDING_TYPE_NETDEV; 231 ctx->binding.netdev = dev; 232 return 0; 233 } 234 235 /* Like net_shaper_ctx_setup(), but for "write" handlers (never for dumps!) 236 * Acquires the lock protecting the hierarchy (instance lock for netdev). 237 */ 238 static int net_shaper_ctx_setup_lock(const struct genl_info *info, int type, 239 struct net_shaper_nl_ctx *ctx) 240 { 241 struct net *ns = genl_info_net(info); 242 struct net_device *dev; 243 int ifindex; 244 245 if (GENL_REQ_ATTR_CHECK(info, type)) 246 return -EINVAL; 247 248 ifindex = nla_get_u32(info->attrs[type]); 249 dev = netdev_get_by_index_lock(ns, ifindex); 250 if (!dev) { 251 NL_SET_BAD_ATTR(info->extack, info->attrs[type]); 252 return -ENOENT; 253 } 254 255 if (!dev->netdev_ops->net_shaper_ops) { 256 NL_SET_BAD_ATTR(info->extack, info->attrs[type]); 257 netdev_unlock(dev); 258 return -EOPNOTSUPP; 259 } 260 261 ctx->binding.type = NET_SHAPER_BINDING_TYPE_NETDEV; 262 ctx->binding.netdev = dev; 263 return 0; 264 } 265 266 static void net_shaper_ctx_cleanup(struct net_shaper_nl_ctx *ctx) 267 { 268 if (ctx->binding.type == NET_SHAPER_BINDING_TYPE_NETDEV) 269 netdev_put(ctx->binding.netdev, &ctx->dev_tracker); 270 } 271 272 static void net_shaper_ctx_cleanup_unlock(struct net_shaper_nl_ctx *ctx) 273 { 274 if (ctx->binding.type == NET_SHAPER_BINDING_TYPE_NETDEV) 275 netdev_unlock(ctx->binding.netdev); 276 } 277 278 static u32 net_shaper_handle_to_index(const struct net_shaper_handle *handle) 279 { 280 return FIELD_PREP(NET_SHAPER_SCOPE_MASK, handle->scope) | 281 FIELD_PREP(NET_SHAPER_ID_MASK, handle->id); 282 } 283 284 static void net_shaper_index_to_handle(u32 index, 285 struct net_shaper_handle *handle) 286 { 287 handle->scope = FIELD_GET(NET_SHAPER_SCOPE_MASK, index); 288 handle->id = FIELD_GET(NET_SHAPER_ID_MASK, index); 289 } 290 291 static void net_shaper_default_parent(const struct net_shaper_handle *handle, 292 struct net_shaper_handle *parent) 293 { 294 switch (handle->scope) { 295 case NET_SHAPER_SCOPE_UNSPEC: 296 case NET_SHAPER_SCOPE_NETDEV: 297 case __NET_SHAPER_SCOPE_MAX: 298 parent->scope = NET_SHAPER_SCOPE_UNSPEC; 299 break; 300 301 case NET_SHAPER_SCOPE_QUEUE: 302 case NET_SHAPER_SCOPE_NODE: 303 parent->scope = NET_SHAPER_SCOPE_NETDEV; 304 break; 305 } 306 parent->id = 0; 307 } 308 309 static struct net_shaper * 310 net_shaper_lookup(struct net_shaper_binding *binding, 311 const struct net_shaper_handle *handle) 312 { 313 u32 index = net_shaper_handle_to_index(handle); 314 struct net_shaper_hierarchy *hierarchy; 315 struct net_shaper *cur; 316 317 hierarchy = net_shaper_hierarchy_rcu(binding); 318 if (!hierarchy) 319 return NULL; 320 321 cur = xa_load(&hierarchy->shapers, index); 322 /* Check valid before reading fields */ 323 if (!cur || !smp_load_acquire(&cur->valid)) 324 return NULL; 325 326 return cur; 327 } 328 329 /* Allocate on demand the per device shaper's hierarchy container. 330 * Called under the lock protecting the hierarchy (instance lock for netdev) 331 */ 332 static struct net_shaper_hierarchy * 333 net_shaper_hierarchy_setup(struct net_shaper_binding *binding) 334 { 335 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 336 337 if (hierarchy) 338 return hierarchy; 339 340 hierarchy = kmalloc_obj(*hierarchy); 341 if (!hierarchy) 342 return NULL; 343 344 /* The flag is required for ID allocation */ 345 xa_init_flags(&hierarchy->shapers, XA_FLAGS_ALLOC); 346 347 switch (binding->type) { 348 case NET_SHAPER_BINDING_TYPE_NETDEV: 349 /* Pairs with READ_ONCE in net_shaper_hierarchy. */ 350 WRITE_ONCE(binding->netdev->net_shaper_hierarchy, hierarchy); 351 break; 352 } 353 return hierarchy; 354 } 355 356 /* Prepare the hierarchy container to actually insert the given shaper, doing 357 * in advance the needed allocations. 358 */ 359 static int net_shaper_pre_insert(struct net_shaper_binding *binding, 360 struct net_shaper_handle *handle, 361 struct netlink_ext_ack *extack) 362 { 363 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 364 struct net_shaper *prev, *cur; 365 bool id_allocated = false; 366 int ret, index; 367 368 if (!hierarchy) 369 return -ENOMEM; 370 371 index = net_shaper_handle_to_index(handle); 372 cur = xa_load(&hierarchy->shapers, index); 373 if (cur) 374 return 0; 375 376 /* Allocated a new id, if needed. */ 377 if (handle->scope == NET_SHAPER_SCOPE_NODE && 378 handle->id == NET_SHAPER_ID_UNSPEC) { 379 u32 min, max; 380 381 handle->id = NET_SHAPER_MAX_HANDLE_ID; 382 max = net_shaper_handle_to_index(handle); 383 handle->id = 0; 384 min = net_shaper_handle_to_index(handle); 385 386 ret = xa_alloc(&hierarchy->shapers, &index, NULL, 387 XA_LIMIT(min, max), GFP_KERNEL); 388 if (ret < 0) { 389 NL_SET_ERR_MSG(extack, "Can't allocate new id for NODE shaper"); 390 return ret; 391 } 392 393 net_shaper_index_to_handle(index, handle); 394 id_allocated = true; 395 } 396 397 cur = kzalloc_obj(*cur); 398 if (!cur) { 399 ret = -ENOMEM; 400 goto free_id; 401 } 402 403 /* Insert as 'tentative' (no VALID mark). The mark will be set by 404 * net_shaper_commit() once the driver-side configuration succeeds. 405 */ 406 prev = xa_store(&hierarchy->shapers, index, cur, GFP_KERNEL); 407 if (xa_err(prev)) { 408 NL_SET_ERR_MSG(extack, "Can't insert shaper into device store"); 409 kfree(cur); 410 ret = xa_err(prev); 411 goto free_id; 412 } 413 return 0; 414 415 free_id: 416 if (id_allocated) 417 xa_erase(&hierarchy->shapers, index); 418 return ret; 419 } 420 421 /* Commit the tentative insert with the actual values. 422 * Must be called only after a successful net_shaper_pre_insert(). 423 */ 424 static void net_shaper_commit(struct net_shaper_binding *binding, 425 int nr_shapers, const struct net_shaper *shapers) 426 { 427 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 428 struct net_shaper *cur; 429 int index; 430 int i; 431 432 for (i = 0; i < nr_shapers; ++i) { 433 index = net_shaper_handle_to_index(&shapers[i].handle); 434 435 cur = xa_load(&hierarchy->shapers, index); 436 if (WARN_ON_ONCE(!cur)) 437 continue; 438 439 /* Successful update: update the hierarchy container... */ 440 net_shaper_copy(cur, &shapers[i]); 441 /* ... publish to lockless readers. */ 442 smp_store_release(&cur->valid, true); 443 } 444 } 445 446 /* Rollback all the tentative inserts from the hierarchy. */ 447 static void net_shaper_rollback(struct net_shaper_binding *binding) 448 { 449 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 450 struct net_shaper *cur; 451 unsigned long index; 452 453 if (!hierarchy) 454 return; 455 456 xa_for_each(&hierarchy->shapers, index, cur) { 457 if (cur->valid) 458 continue; 459 xa_erase(&hierarchy->shapers, index); 460 kfree_rcu(cur, rcu); 461 } 462 } 463 464 static int net_shaper_parse_handle(const struct nlattr *attr, 465 const struct genl_info *info, 466 struct net_shaper_handle *handle) 467 { 468 struct nlattr *tb[NET_SHAPER_A_HANDLE_MAX + 1]; 469 struct nlattr *id_attr; 470 u32 id = 0; 471 int ret; 472 473 ret = nla_parse_nested(tb, NET_SHAPER_A_HANDLE_MAX, attr, 474 net_shaper_handle_nl_policy, info->extack); 475 if (ret < 0) 476 return ret; 477 478 if (NL_REQ_ATTR_CHECK(info->extack, attr, tb, 479 NET_SHAPER_A_HANDLE_SCOPE)) 480 return -EINVAL; 481 482 handle->scope = nla_get_u32(tb[NET_SHAPER_A_HANDLE_SCOPE]); 483 484 /* The default id for NODE scope shapers is an invalid one 485 * to help the 'group' operation discriminate between new 486 * NODE shaper creation (ID_UNSPEC) and reuse of existing 487 * shaper (any other value). 488 */ 489 id_attr = tb[NET_SHAPER_A_HANDLE_ID]; 490 if (id_attr) { 491 id = nla_get_u32(id_attr); 492 } else if (handle->scope == NET_SHAPER_SCOPE_NODE) { 493 id = NET_SHAPER_ID_UNSPEC; 494 } else if (handle->scope == NET_SHAPER_SCOPE_QUEUE) { 495 NL_SET_ERR_ATTR_MISS(info->extack, attr, 496 NET_SHAPER_A_HANDLE_ID); 497 return -EINVAL; 498 } 499 500 if (id && handle->scope == NET_SHAPER_SCOPE_NETDEV) { 501 NL_SET_ERR_MSG_ATTR(info->extack, id_attr, 502 "Netdev scope is a singleton, must use ID 0"); 503 return -EINVAL; 504 } 505 506 handle->id = id; 507 return 0; 508 } 509 510 static int net_shaper_validate_caps(struct net_shaper_binding *binding, 511 struct nlattr **tb, 512 const struct genl_info *info, 513 struct net_shaper *shaper) 514 { 515 const struct net_shaper_ops *ops = net_shaper_ops(binding); 516 struct nlattr *bad = NULL; 517 unsigned long caps = 0; 518 519 ops->capabilities(binding, shaper->handle.scope, &caps); 520 521 if (tb[NET_SHAPER_A_PRIORITY] && 522 !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_PRIORITY))) 523 bad = tb[NET_SHAPER_A_PRIORITY]; 524 if (tb[NET_SHAPER_A_WEIGHT] && 525 !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_WEIGHT))) 526 bad = tb[NET_SHAPER_A_WEIGHT]; 527 if (tb[NET_SHAPER_A_BW_MIN] && 528 !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MIN))) 529 bad = tb[NET_SHAPER_A_BW_MIN]; 530 if (tb[NET_SHAPER_A_BW_MAX] && 531 !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX))) 532 bad = tb[NET_SHAPER_A_BW_MAX]; 533 if (tb[NET_SHAPER_A_BURST] && 534 !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BURST))) 535 bad = tb[NET_SHAPER_A_BURST]; 536 537 if (!caps) 538 bad = tb[NET_SHAPER_A_HANDLE]; 539 540 if (bad) { 541 NL_SET_BAD_ATTR(info->extack, bad); 542 return -EOPNOTSUPP; 543 } 544 545 if (shaper->handle.scope == NET_SHAPER_SCOPE_QUEUE && 546 binding->type == NET_SHAPER_BINDING_TYPE_NETDEV && 547 shaper->handle.id >= binding->netdev->real_num_tx_queues) { 548 NL_SET_ERR_MSG_FMT(info->extack, 549 "Not existing queue id %d max %d", 550 shaper->handle.id, 551 binding->netdev->real_num_tx_queues); 552 return -ENOENT; 553 } 554 555 /* The metric is really used only if there is *any* rate-related 556 * setting, either in current attributes set or in pre-existing 557 * values. 558 */ 559 if (shaper->burst || shaper->bw_min || shaper->bw_max) { 560 u32 metric_cap = NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS + 561 shaper->metric; 562 563 /* The metric test can fail even when the user did not 564 * specify the METRIC attribute. Pointing to rate related 565 * attribute will be confusing, as the attribute itself 566 * could be indeed supported, with a different metric. 567 * Be more specific. 568 */ 569 if (!(caps & BIT(metric_cap))) { 570 NL_SET_ERR_MSG_FMT(info->extack, "Bad metric %d", 571 shaper->metric); 572 return -EOPNOTSUPP; 573 } 574 } 575 return 0; 576 } 577 578 static int net_shaper_parse_info(struct net_shaper_binding *binding, 579 struct nlattr **tb, 580 const struct genl_info *info, 581 struct net_shaper *shaper, 582 bool *exists) 583 { 584 struct net_shaper *old; 585 int ret; 586 587 /* The shaper handle is the only mandatory attribute. */ 588 if (NL_REQ_ATTR_CHECK(info->extack, NULL, tb, NET_SHAPER_A_HANDLE)) 589 return -EINVAL; 590 591 ret = net_shaper_parse_handle(tb[NET_SHAPER_A_HANDLE], info, 592 &shaper->handle); 593 if (ret) 594 return ret; 595 596 if (shaper->handle.scope == NET_SHAPER_SCOPE_UNSPEC) { 597 NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]); 598 return -EINVAL; 599 } 600 601 /* Fetch existing hierarchy, if any, so that user provide info will 602 * incrementally update the existing shaper configuration. 603 */ 604 old = net_shaper_lookup(binding, &shaper->handle); 605 if (old) 606 *shaper = *old; 607 *exists = !!old; 608 609 if (tb[NET_SHAPER_A_METRIC]) 610 shaper->metric = nla_get_u32(tb[NET_SHAPER_A_METRIC]); 611 612 if (tb[NET_SHAPER_A_BW_MIN]) 613 shaper->bw_min = nla_get_uint(tb[NET_SHAPER_A_BW_MIN]); 614 615 if (tb[NET_SHAPER_A_BW_MAX]) 616 shaper->bw_max = nla_get_uint(tb[NET_SHAPER_A_BW_MAX]); 617 618 if (tb[NET_SHAPER_A_BURST]) 619 shaper->burst = nla_get_uint(tb[NET_SHAPER_A_BURST]); 620 621 if (tb[NET_SHAPER_A_PRIORITY]) 622 shaper->priority = nla_get_u32(tb[NET_SHAPER_A_PRIORITY]); 623 624 if (tb[NET_SHAPER_A_WEIGHT]) 625 shaper->weight = nla_get_u32(tb[NET_SHAPER_A_WEIGHT]); 626 627 ret = net_shaper_validate_caps(binding, tb, info, shaper); 628 if (ret < 0) 629 return ret; 630 631 return 0; 632 } 633 634 static int net_shaper_validate_nesting(struct net_shaper_binding *binding, 635 const struct net_shaper *shaper, 636 struct netlink_ext_ack *extack) 637 { 638 const struct net_shaper_ops *ops = net_shaper_ops(binding); 639 unsigned long caps = 0; 640 641 ops->capabilities(binding, shaper->handle.scope, &caps); 642 if (!(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_NESTING))) { 643 NL_SET_ERR_MSG_FMT(extack, 644 "Nesting not supported for scope %d", 645 shaper->handle.scope); 646 return -EOPNOTSUPP; 647 } 648 return 0; 649 } 650 651 /* Fetch the existing leaf and update it with the user-provided 652 * attributes. 653 */ 654 static int net_shaper_parse_leaf(struct net_shaper_binding *binding, 655 const struct nlattr *attr, 656 const struct genl_info *info, 657 const struct net_shaper *node, 658 struct net_shaper *shaper) 659 { 660 struct nlattr *tb[NET_SHAPER_A_WEIGHT + 1]; 661 bool exists; 662 int ret; 663 664 ret = nla_parse_nested(tb, NET_SHAPER_A_WEIGHT, attr, 665 net_shaper_leaf_info_nl_policy, info->extack); 666 if (ret < 0) 667 return ret; 668 669 ret = net_shaper_parse_info(binding, tb, info, shaper, &exists); 670 if (ret < 0) 671 return ret; 672 673 if (shaper->handle.scope != NET_SHAPER_SCOPE_QUEUE) { 674 NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]); 675 return -EINVAL; 676 } 677 678 if (node->handle.scope == NET_SHAPER_SCOPE_NODE) { 679 ret = net_shaper_validate_nesting(binding, shaper, 680 info->extack); 681 if (ret < 0) 682 return ret; 683 } 684 685 if (!exists) 686 net_shaper_default_parent(&shaper->handle, &shaper->parent); 687 return 0; 688 } 689 690 /* Alike net_parse_shaper_info(), but additionally allow the user specifying 691 * the shaper's parent handle. 692 */ 693 static int net_shaper_parse_node(struct net_shaper_binding *binding, 694 struct nlattr **tb, 695 const struct genl_info *info, 696 struct net_shaper *shaper) 697 { 698 bool exists; 699 int ret; 700 701 ret = net_shaper_parse_info(binding, tb, info, shaper, &exists); 702 if (ret) 703 return ret; 704 705 if (shaper->handle.scope != NET_SHAPER_SCOPE_NODE && 706 shaper->handle.scope != NET_SHAPER_SCOPE_NETDEV) { 707 NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]); 708 return -EINVAL; 709 } 710 711 if (tb[NET_SHAPER_A_PARENT]) { 712 ret = net_shaper_parse_handle(tb[NET_SHAPER_A_PARENT], info, 713 &shaper->parent); 714 if (ret) 715 return ret; 716 717 if (shaper->parent.scope != NET_SHAPER_SCOPE_NODE && 718 shaper->parent.scope != NET_SHAPER_SCOPE_NETDEV) { 719 NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_PARENT]); 720 return -EINVAL; 721 } 722 } 723 return 0; 724 } 725 726 static int net_shaper_generic_pre(struct genl_info *info, int type) 727 { 728 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)info->ctx; 729 730 BUILD_BUG_ON(sizeof(*ctx) > sizeof(info->ctx)); 731 732 return net_shaper_ctx_setup(info, type, ctx); 733 } 734 735 int net_shaper_nl_pre_doit(const struct genl_split_ops *ops, 736 struct sk_buff *skb, struct genl_info *info) 737 { 738 return net_shaper_generic_pre(info, NET_SHAPER_A_IFINDEX); 739 } 740 741 static void net_shaper_generic_post(struct genl_info *info) 742 { 743 net_shaper_ctx_cleanup((struct net_shaper_nl_ctx *)info->ctx); 744 } 745 746 void net_shaper_nl_post_doit(const struct genl_split_ops *ops, 747 struct sk_buff *skb, struct genl_info *info) 748 { 749 net_shaper_generic_post(info); 750 } 751 752 int net_shaper_nl_pre_doit_write(const struct genl_split_ops *ops, 753 struct sk_buff *skb, struct genl_info *info) 754 { 755 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)info->ctx; 756 757 BUILD_BUG_ON(sizeof(*ctx) > sizeof(info->ctx)); 758 759 return net_shaper_ctx_setup_lock(info, NET_SHAPER_A_IFINDEX, ctx); 760 } 761 762 void net_shaper_nl_post_doit_write(const struct genl_split_ops *ops, 763 struct sk_buff *skb, struct genl_info *info) 764 { 765 net_shaper_ctx_cleanup_unlock((struct net_shaper_nl_ctx *)info->ctx); 766 } 767 768 int net_shaper_nl_pre_dumpit(struct netlink_callback *cb) 769 { 770 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx; 771 const struct genl_info *info = genl_info_dump(cb); 772 773 return net_shaper_ctx_setup(info, NET_SHAPER_A_IFINDEX, ctx); 774 } 775 776 int net_shaper_nl_post_dumpit(struct netlink_callback *cb) 777 { 778 net_shaper_ctx_cleanup((struct net_shaper_nl_ctx *)cb->ctx); 779 return 0; 780 } 781 782 int net_shaper_nl_cap_pre_doit(const struct genl_split_ops *ops, 783 struct sk_buff *skb, struct genl_info *info) 784 { 785 return net_shaper_generic_pre(info, NET_SHAPER_A_CAPS_IFINDEX); 786 } 787 788 void net_shaper_nl_cap_post_doit(const struct genl_split_ops *ops, 789 struct sk_buff *skb, struct genl_info *info) 790 { 791 net_shaper_generic_post(info); 792 } 793 794 int net_shaper_nl_cap_pre_dumpit(struct netlink_callback *cb) 795 { 796 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx; 797 798 return net_shaper_ctx_setup(genl_info_dump(cb), 799 NET_SHAPER_A_CAPS_IFINDEX, ctx); 800 } 801 802 int net_shaper_nl_cap_post_dumpit(struct netlink_callback *cb) 803 { 804 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx; 805 806 net_shaper_ctx_cleanup(ctx); 807 return 0; 808 } 809 810 int net_shaper_nl_get_doit(struct sk_buff *skb, struct genl_info *info) 811 { 812 struct net_shaper_binding *binding; 813 struct net_shaper_handle handle; 814 struct net_shaper *shaper; 815 struct sk_buff *msg; 816 int ret; 817 818 if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_HANDLE)) 819 return -EINVAL; 820 821 binding = net_shaper_binding_from_ctx(info->ctx); 822 ret = net_shaper_parse_handle(info->attrs[NET_SHAPER_A_HANDLE], info, 823 &handle); 824 if (ret < 0) 825 return ret; 826 827 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 828 if (!msg) 829 return -ENOMEM; 830 831 rcu_read_lock(); 832 shaper = net_shaper_lookup(binding, &handle); 833 if (!shaper) { 834 NL_SET_BAD_ATTR(info->extack, 835 info->attrs[NET_SHAPER_A_HANDLE]); 836 rcu_read_unlock(); 837 ret = -ENOENT; 838 goto free_msg; 839 } 840 841 ret = net_shaper_fill_one(msg, binding, shaper, info); 842 rcu_read_unlock(); 843 if (ret) 844 goto free_msg; 845 846 return genlmsg_reply(msg, info); 847 848 free_msg: 849 nlmsg_free(msg); 850 return ret; 851 } 852 853 int net_shaper_nl_get_dumpit(struct sk_buff *skb, 854 struct netlink_callback *cb) 855 { 856 struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx; 857 const struct genl_info *info = genl_info_dump(cb); 858 struct net_shaper_hierarchy *hierarchy; 859 struct net_shaper_binding *binding; 860 struct net_shaper *shaper; 861 int ret = 0; 862 863 /* Don't error out dumps performed before any set operation. */ 864 binding = net_shaper_binding_from_ctx(ctx); 865 866 rcu_read_lock(); 867 hierarchy = net_shaper_hierarchy_rcu(binding); 868 if (!hierarchy) 869 goto out_unlock; 870 871 for (; (shaper = xa_find(&hierarchy->shapers, &ctx->start_index, 872 U32_MAX, XA_PRESENT)); 873 ctx->start_index++) { 874 /* Check valid before reading fields */ 875 if (!smp_load_acquire(&shaper->valid)) 876 continue; 877 878 ret = net_shaper_fill_one(skb, binding, shaper, info); 879 if (ret) 880 break; 881 } 882 out_unlock: 883 rcu_read_unlock(); 884 885 return ret; 886 } 887 888 int net_shaper_nl_set_doit(struct sk_buff *skb, struct genl_info *info) 889 { 890 struct net_shaper_hierarchy *hierarchy; 891 struct net_shaper_binding *binding; 892 const struct net_shaper_ops *ops; 893 struct net_shaper_handle handle; 894 struct net_shaper shaper = {}; 895 bool exists; 896 int ret; 897 898 binding = net_shaper_binding_from_ctx(info->ctx); 899 900 ret = net_shaper_parse_info(binding, info->attrs, info, &shaper, 901 &exists); 902 if (ret) 903 return ret; 904 905 if (!exists) 906 net_shaper_default_parent(&shaper.handle, &shaper.parent); 907 908 hierarchy = net_shaper_hierarchy_setup(binding); 909 if (!hierarchy) 910 return -ENOMEM; 911 912 /* The 'set' operation can't create node-scope shapers. */ 913 handle = shaper.handle; 914 if (handle.scope == NET_SHAPER_SCOPE_NODE && 915 !net_shaper_lookup(binding, &handle)) 916 return -ENOENT; 917 918 ret = net_shaper_pre_insert(binding, &handle, info->extack); 919 if (ret) 920 return ret; 921 922 ops = net_shaper_ops(binding); 923 ret = ops->set(binding, &shaper, info->extack); 924 if (ret) { 925 net_shaper_rollback(binding); 926 return ret; 927 } 928 929 net_shaper_commit(binding, 1, &shaper); 930 931 return 0; 932 } 933 934 static int __net_shaper_delete(struct net_shaper_binding *binding, 935 struct net_shaper *shaper, 936 struct netlink_ext_ack *extack) 937 { 938 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 939 struct net_shaper_handle parent_handle, handle = shaper->handle; 940 const struct net_shaper_ops *ops = net_shaper_ops(binding); 941 int ret; 942 943 again: 944 parent_handle = shaper->parent; 945 946 ret = ops->delete(binding, &handle, extack); 947 if (ret < 0) 948 return ret; 949 950 xa_erase(&hierarchy->shapers, net_shaper_handle_to_index(&handle)); 951 kfree_rcu(shaper, rcu); 952 953 /* Eventually delete the parent, if it is left over with no leaves. */ 954 if (parent_handle.scope == NET_SHAPER_SCOPE_NODE) { 955 shaper = net_shaper_lookup(binding, &parent_handle); 956 if (shaper && !--shaper->leaves) { 957 handle = parent_handle; 958 goto again; 959 } 960 } 961 return 0; 962 } 963 964 static int net_shaper_handle_cmp(const struct net_shaper_handle *a, 965 const struct net_shaper_handle *b) 966 { 967 /* Must avoid holes in struct net_shaper_handle. */ 968 BUILD_BUG_ON(sizeof(*a) != 8); 969 970 return memcmp(a, b, sizeof(*a)); 971 } 972 973 static int net_shaper_parse_leaves(struct net_shaper_binding *binding, 974 struct genl_info *info, 975 const struct net_shaper *node, 976 struct net_shaper *leaves, 977 int leaves_count) 978 { 979 struct nlattr *attr; 980 int i, j, ret, rem; 981 982 i = 0; 983 nla_for_each_attr_type(attr, NET_SHAPER_A_LEAVES, 984 genlmsg_data(info->genlhdr), 985 genlmsg_len(info->genlhdr), rem) { 986 if (WARN_ON_ONCE(i >= leaves_count)) 987 return -EINVAL; 988 989 ret = net_shaper_parse_leaf(binding, attr, info, 990 node, &leaves[i]); 991 if (ret) 992 return ret; 993 994 /* Reject duplicates */ 995 for (j = 0; j < i; j++) { 996 if (net_shaper_handle_cmp(&leaves[i].handle, 997 &leaves[j].handle)) 998 continue; 999 1000 NL_SET_ERR_MSG_ATTR_FMT(info->extack, attr, 1001 "Duplicate leaf shaper %d:%d", 1002 leaves[i].handle.scope, 1003 leaves[i].handle.id); 1004 return -EINVAL; 1005 } 1006 1007 i++; 1008 } 1009 1010 return 0; 1011 } 1012 1013 static int net_shaper_parent_from_leaves(int leaves_count, 1014 const struct net_shaper *leaves, 1015 struct net_shaper *node, 1016 struct netlink_ext_ack *extack) 1017 { 1018 struct net_shaper_handle parent = leaves[0].parent; 1019 int i; 1020 1021 for (i = 1; i < leaves_count; ++i) { 1022 if (net_shaper_handle_cmp(&leaves[i].parent, &parent)) { 1023 NL_SET_ERR_MSG_FMT(extack, "All the leaves shapers must have the same old parent"); 1024 return -EINVAL; 1025 } 1026 } 1027 1028 node->parent = parent; 1029 return 0; 1030 } 1031 1032 static int __net_shaper_group(struct net_shaper_binding *binding, 1033 bool update_node, int leaves_count, 1034 struct net_shaper *leaves, 1035 struct net_shaper *node, 1036 struct netlink_ext_ack *extack) 1037 { 1038 const struct net_shaper_ops *ops = net_shaper_ops(binding); 1039 struct net_shaper_handle leaf_handle; 1040 struct net_shaper *parent = NULL; 1041 bool new_node = false; 1042 int i, ret; 1043 1044 if (node->handle.scope == NET_SHAPER_SCOPE_NODE) { 1045 struct net_shaper *cur = NULL; 1046 1047 new_node = node->handle.id == NET_SHAPER_ID_UNSPEC; 1048 1049 if (!new_node) { 1050 cur = net_shaper_lookup(binding, &node->handle); 1051 if (!cur) { 1052 /* The related attribute is not available 1053 * when reaching here from the delete() op. 1054 */ 1055 NL_SET_ERR_MSG_FMT(extack, 1056 "Node shaper %d:%d does not exist", 1057 node->handle.scope, 1058 node->handle.id); 1059 return -ENOENT; 1060 } 1061 } 1062 1063 /* When unspecified, the node parent scope is inherited from 1064 * the leaves. 1065 */ 1066 if (node->parent.scope == NET_SHAPER_SCOPE_UNSPEC) { 1067 ret = net_shaper_parent_from_leaves(leaves_count, 1068 leaves, node, 1069 extack); 1070 if (ret) 1071 return ret; 1072 } 1073 1074 if (cur && net_shaper_handle_cmp(&cur->parent, 1075 &node->parent)) { 1076 NL_SET_ERR_MSG_FMT(extack, 1077 "Cannot reparent node shaper %d:%d", 1078 node->handle.scope, 1079 node->handle.id); 1080 return -EOPNOTSUPP; 1081 } 1082 1083 } else { 1084 net_shaper_default_parent(&node->handle, &node->parent); 1085 } 1086 1087 if (node->parent.scope == NET_SHAPER_SCOPE_NODE) { 1088 parent = net_shaper_lookup(binding, &node->parent); 1089 if (!parent) { 1090 NL_SET_ERR_MSG_FMT(extack, "Node parent shaper %d:%d does not exists", 1091 node->parent.scope, node->parent.id); 1092 return -ENOENT; 1093 } 1094 1095 ret = net_shaper_validate_nesting(binding, node, extack); 1096 if (ret < 0) 1097 return ret; 1098 } 1099 1100 if (update_node) { 1101 /* For newly created node scope shaper, the following will 1102 * update the handle, due to id allocation. 1103 */ 1104 ret = net_shaper_pre_insert(binding, &node->handle, extack); 1105 if (ret) 1106 return ret; 1107 } 1108 1109 for (i = 0; i < leaves_count; ++i) { 1110 leaf_handle = leaves[i].handle; 1111 1112 ret = net_shaper_pre_insert(binding, &leaf_handle, extack); 1113 if (ret) 1114 goto rollback; 1115 1116 if (!net_shaper_handle_cmp(&leaves[i].parent, &node->handle)) 1117 continue; 1118 1119 /* The leaves shapers will be nested to the node, update the 1120 * linking accordingly. 1121 */ 1122 leaves[i].parent = node->handle; 1123 node->leaves++; 1124 } 1125 1126 ret = ops->group(binding, leaves_count, leaves, node, extack); 1127 if (ret < 0) 1128 goto rollback; 1129 1130 /* The node's parent gains a new leaf only when the node itself 1131 * is created by this group operation 1132 */ 1133 if (new_node && parent) 1134 parent->leaves++; 1135 if (update_node) 1136 net_shaper_commit(binding, 1, node); 1137 net_shaper_commit(binding, leaves_count, leaves); 1138 return 0; 1139 1140 rollback: 1141 net_shaper_rollback(binding); 1142 return ret; 1143 } 1144 1145 static int net_shaper_pre_del_node(struct net_shaper_binding *binding, 1146 const struct net_shaper *shaper, 1147 struct netlink_ext_ack *extack) 1148 { 1149 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 1150 struct net_shaper *cur, *leaves, node = {}; 1151 int ret, leaves_count = 0; 1152 unsigned long index; 1153 bool update_node; 1154 1155 if (!shaper->leaves) 1156 return 0; 1157 1158 /* Fetch the new node information. */ 1159 node.handle = shaper->parent; 1160 cur = net_shaper_lookup(binding, &node.handle); 1161 if (cur) { 1162 node = *cur; 1163 } else { 1164 /* A scope NODE shaper can be nested only to the NETDEV scope 1165 * shaper without creating the latter, this check may fail only 1166 * if the data is in inconsistent status. 1167 */ 1168 if (WARN_ON_ONCE(node.handle.scope != NET_SHAPER_SCOPE_NETDEV)) 1169 return -EINVAL; 1170 } 1171 1172 leaves = kzalloc_objs(struct net_shaper, shaper->leaves); 1173 if (!leaves) 1174 return -ENOMEM; 1175 1176 /* Build the leaves arrays. */ 1177 xa_for_each(&hierarchy->shapers, index, cur) { 1178 if (net_shaper_handle_cmp(&cur->parent, &shaper->handle)) 1179 continue; 1180 1181 if (WARN_ON_ONCE(leaves_count == shaper->leaves)) { 1182 ret = -EINVAL; 1183 goto free; 1184 } 1185 1186 leaves[leaves_count++] = *cur; 1187 } 1188 1189 /* When re-linking to the netdev shaper, avoid the eventual, implicit, 1190 * creation of the new node, would be surprising since the user is 1191 * doing a delete operation. 1192 */ 1193 update_node = node.handle.scope != NET_SHAPER_SCOPE_NETDEV; 1194 ret = __net_shaper_group(binding, update_node, leaves_count, 1195 leaves, &node, extack); 1196 1197 free: 1198 kfree(leaves); 1199 return ret; 1200 } 1201 1202 int net_shaper_nl_delete_doit(struct sk_buff *skb, struct genl_info *info) 1203 { 1204 struct net_shaper_hierarchy *hierarchy; 1205 struct net_shaper_binding *binding; 1206 struct net_shaper_handle handle; 1207 struct net_shaper *shaper; 1208 int ret; 1209 1210 if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_HANDLE)) 1211 return -EINVAL; 1212 1213 binding = net_shaper_binding_from_ctx(info->ctx); 1214 1215 ret = net_shaper_parse_handle(info->attrs[NET_SHAPER_A_HANDLE], info, 1216 &handle); 1217 if (ret) 1218 return ret; 1219 1220 hierarchy = net_shaper_hierarchy(binding); 1221 if (!hierarchy) 1222 return -ENOENT; 1223 1224 shaper = net_shaper_lookup(binding, &handle); 1225 if (!shaper) 1226 return -ENOENT; 1227 1228 if (handle.scope == NET_SHAPER_SCOPE_NODE) { 1229 ret = net_shaper_pre_del_node(binding, shaper, info->extack); 1230 if (ret) 1231 return ret; 1232 } 1233 1234 return __net_shaper_delete(binding, shaper, info->extack); 1235 } 1236 1237 static int net_shaper_group_send_reply(struct net_shaper_binding *binding, 1238 const struct net_shaper_handle *handle, 1239 struct genl_info *info, 1240 struct sk_buff *msg) 1241 { 1242 void *hdr; 1243 1244 hdr = genlmsg_iput(msg, info); 1245 if (!hdr) 1246 goto free_msg; 1247 1248 if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_IFINDEX) || 1249 net_shaper_fill_handle(msg, handle, NET_SHAPER_A_HANDLE)) 1250 goto free_msg; 1251 1252 genlmsg_end(msg, hdr); 1253 1254 return genlmsg_reply(msg, info); 1255 1256 free_msg: 1257 /* Should never happen as msg is pre-allocated with enough space. */ 1258 WARN_ONCE(true, "calculated message payload length (%d)", 1259 net_shaper_group_reply_size()); 1260 nlmsg_free(msg); 1261 return -EMSGSIZE; 1262 } 1263 1264 int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info) 1265 { 1266 struct net_shaper **old_nodes, *leaves, node = {}; 1267 struct net_shaper_hierarchy *hierarchy; 1268 struct net_shaper_binding *binding; 1269 int i, ret, leaves_count; 1270 int old_nodes_count = 0; 1271 struct sk_buff *msg; 1272 1273 if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_LEAVES)) 1274 return -EINVAL; 1275 1276 binding = net_shaper_binding_from_ctx(info->ctx); 1277 1278 /* The group operation is optional. */ 1279 if (!net_shaper_ops(binding)->group) 1280 return -EOPNOTSUPP; 1281 1282 leaves_count = net_shaper_list_len(info, NET_SHAPER_A_LEAVES); 1283 if (!leaves_count) { 1284 NL_SET_BAD_ATTR(info->extack, 1285 info->attrs[NET_SHAPER_A_LEAVES]); 1286 return -EINVAL; 1287 } 1288 1289 leaves = kcalloc(leaves_count, sizeof(struct net_shaper) + 1290 sizeof(struct net_shaper *), GFP_KERNEL); 1291 if (!leaves) 1292 return -ENOMEM; 1293 old_nodes = (void *)&leaves[leaves_count]; 1294 1295 ret = net_shaper_parse_node(binding, info->attrs, info, &node); 1296 if (ret) 1297 goto free_leaves; 1298 1299 ret = net_shaper_parse_leaves(binding, info, &node, 1300 leaves, leaves_count); 1301 if (ret) 1302 goto free_leaves; 1303 1304 /* Prepare the msg reply in advance, to avoid device operation 1305 * rollback on allocation failure. 1306 */ 1307 msg = genlmsg_new(net_shaper_group_reply_size(), GFP_KERNEL); 1308 if (!msg) { 1309 ret = -ENOMEM; 1310 goto free_leaves; 1311 } 1312 1313 hierarchy = net_shaper_hierarchy_setup(binding); 1314 if (!hierarchy) { 1315 ret = -ENOMEM; 1316 goto free_msg; 1317 } 1318 1319 /* Record the node shapers that this group() operation can make 1320 * childless for later cleanup. 1321 */ 1322 for (i = 0; i < leaves_count; i++) { 1323 if (leaves[i].parent.scope == NET_SHAPER_SCOPE_NODE && 1324 net_shaper_handle_cmp(&leaves[i].parent, &node.handle)) { 1325 struct net_shaper *tmp; 1326 1327 tmp = net_shaper_lookup(binding, &leaves[i].parent); 1328 if (!tmp) 1329 continue; 1330 1331 old_nodes[old_nodes_count++] = tmp; 1332 } 1333 } 1334 1335 ret = __net_shaper_group(binding, true, leaves_count, leaves, &node, 1336 info->extack); 1337 if (ret) 1338 goto free_msg; 1339 1340 /* Check if we need to delete any node left alone by the new leaves 1341 * linkage. 1342 */ 1343 for (i = 0; i < old_nodes_count; ++i) { 1344 struct net_shaper *tmp = old_nodes[i]; 1345 1346 if (--tmp->leaves > 0) 1347 continue; 1348 1349 /* Errors here are not fatal: the grouping operation is 1350 * completed, and user-space can still explicitly clean-up 1351 * left-over nodes. 1352 */ 1353 __net_shaper_delete(binding, tmp, info->extack); 1354 } 1355 1356 ret = net_shaper_group_send_reply(binding, &node.handle, info, msg); 1357 if (ret) 1358 GENL_SET_ERR_MSG_FMT(info, "Can't send reply"); 1359 1360 free_leaves: 1361 kfree(leaves); 1362 return ret; 1363 1364 free_msg: 1365 kfree_skb(msg); 1366 goto free_leaves; 1367 } 1368 1369 static int 1370 net_shaper_cap_fill_one(struct sk_buff *msg, 1371 struct net_shaper_binding *binding, 1372 enum net_shaper_scope scope, unsigned long flags, 1373 const struct genl_info *info) 1374 { 1375 unsigned long cur; 1376 void *hdr; 1377 1378 hdr = genlmsg_iput(msg, info); 1379 if (!hdr) 1380 return -EMSGSIZE; 1381 1382 if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_CAPS_IFINDEX) || 1383 nla_put_u32(msg, NET_SHAPER_A_CAPS_SCOPE, scope)) 1384 goto nla_put_failure; 1385 1386 for (cur = NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS; 1387 cur <= NET_SHAPER_A_CAPS_MAX; ++cur) { 1388 if (flags & BIT(cur) && nla_put_flag(msg, cur)) 1389 goto nla_put_failure; 1390 } 1391 1392 genlmsg_end(msg, hdr); 1393 1394 return 0; 1395 1396 nla_put_failure: 1397 genlmsg_cancel(msg, hdr); 1398 return -EMSGSIZE; 1399 } 1400 1401 int net_shaper_nl_cap_get_doit(struct sk_buff *skb, struct genl_info *info) 1402 { 1403 struct net_shaper_binding *binding; 1404 const struct net_shaper_ops *ops; 1405 enum net_shaper_scope scope; 1406 unsigned long flags = 0; 1407 struct sk_buff *msg; 1408 int ret; 1409 1410 if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_CAPS_SCOPE)) 1411 return -EINVAL; 1412 1413 binding = net_shaper_binding_from_ctx(info->ctx); 1414 scope = nla_get_u32(info->attrs[NET_SHAPER_A_CAPS_SCOPE]); 1415 ops = net_shaper_ops(binding); 1416 ops->capabilities(binding, scope, &flags); 1417 if (!flags) 1418 return -EOPNOTSUPP; 1419 1420 msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1421 if (!msg) 1422 return -ENOMEM; 1423 1424 ret = net_shaper_cap_fill_one(msg, binding, scope, flags, info); 1425 if (ret) 1426 goto free_msg; 1427 1428 return genlmsg_reply(msg, info); 1429 1430 free_msg: 1431 nlmsg_free(msg); 1432 return ret; 1433 } 1434 1435 int net_shaper_nl_cap_get_dumpit(struct sk_buff *skb, 1436 struct netlink_callback *cb) 1437 { 1438 const struct genl_info *info = genl_info_dump(cb); 1439 struct net_shaper_binding *binding; 1440 const struct net_shaper_ops *ops; 1441 enum net_shaper_scope scope; 1442 int ret; 1443 1444 binding = net_shaper_binding_from_ctx(cb->ctx); 1445 ops = net_shaper_ops(binding); 1446 for (scope = 0; scope <= NET_SHAPER_SCOPE_MAX; ++scope) { 1447 unsigned long flags = 0; 1448 1449 ops->capabilities(binding, scope, &flags); 1450 if (!flags) 1451 continue; 1452 1453 ret = net_shaper_cap_fill_one(skb, binding, scope, flags, 1454 info); 1455 /* cap dumps are tiny, we expect them to fit in a single skb */ 1456 WARN_ON_ONCE(ret == -EMSGSIZE); 1457 if (ret) 1458 return ret; 1459 } 1460 1461 return 0; 1462 } 1463 1464 static void net_shaper_flush(struct net_shaper_binding *binding) 1465 { 1466 struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding); 1467 struct net_shaper *cur; 1468 unsigned long index; 1469 1470 if (!hierarchy) 1471 return; 1472 1473 xa_for_each(&hierarchy->shapers, index, cur) { 1474 xa_erase(&hierarchy->shapers, index); 1475 /* No need to use kfree_rcu(), netdev is already unpublished, 1476 * and synchronize_rcu() has been run as part of 1477 * unregister_netdevice(). 1478 */ 1479 kfree(cur); 1480 } 1481 1482 kfree(hierarchy); 1483 } 1484 1485 void net_shaper_flush_netdev(struct net_device *dev) 1486 { 1487 struct net_shaper_binding binding = { 1488 .type = NET_SHAPER_BINDING_TYPE_NETDEV, 1489 .netdev = dev, 1490 }; 1491 1492 net_shaper_flush(&binding); 1493 } 1494 1495 void net_shaper_set_real_num_tx_queues(struct net_device *dev, 1496 unsigned int txq) 1497 { 1498 struct net_shaper_hierarchy *hierarchy; 1499 struct net_shaper_binding binding; 1500 int i; 1501 1502 binding.type = NET_SHAPER_BINDING_TYPE_NETDEV; 1503 binding.netdev = dev; 1504 hierarchy = net_shaper_hierarchy(&binding); 1505 if (!hierarchy) 1506 return; 1507 1508 /* Only drivers implementing shapers support ensure 1509 * the lock is acquired in advance. 1510 */ 1511 netdev_assert_locked(dev); 1512 1513 /* Take action only when decreasing the tx queue number. */ 1514 for (i = txq; i < dev->real_num_tx_queues; ++i) { 1515 struct net_shaper_handle handle, parent_handle; 1516 struct net_shaper *shaper; 1517 u32 index; 1518 1519 handle.scope = NET_SHAPER_SCOPE_QUEUE; 1520 handle.id = i; 1521 shaper = net_shaper_lookup(&binding, &handle); 1522 if (!shaper) 1523 continue; 1524 1525 /* Don't touch the H/W for the queue shaper, the drivers already 1526 * deleted the queue and related resources. 1527 */ 1528 parent_handle = shaper->parent; 1529 index = net_shaper_handle_to_index(&handle); 1530 xa_erase(&hierarchy->shapers, index); 1531 kfree_rcu(shaper, rcu); 1532 1533 /* The recursion on parent does the full job. */ 1534 if (parent_handle.scope != NET_SHAPER_SCOPE_NODE) 1535 continue; 1536 1537 shaper = net_shaper_lookup(&binding, &parent_handle); 1538 if (shaper && !--shaper->leaves) 1539 __net_shaper_delete(&binding, shaper, NULL); 1540 } 1541 } 1542 1543 static int __init shaper_init(void) 1544 { 1545 return genl_register_family(&net_shaper_nl_family); 1546 } 1547 1548 subsys_initcall(shaper_init); 1549