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