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