1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net-sysfs.c - network device class and attributes 4 * 5 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org> 6 */ 7 8 #include <linux/capability.h> 9 #include <linux/kernel.h> 10 #include <linux/netdevice.h> 11 #include <linux/if_arp.h> 12 #include <linux/slab.h> 13 #include <linux/sched/signal.h> 14 #include <linux/sched/isolation.h> 15 #include <linux/nsproxy.h> 16 #include <net/sock.h> 17 #include <net/net_namespace.h> 18 #include <linux/rtnetlink.h> 19 #include <linux/vmalloc.h> 20 #include <linux/export.h> 21 #include <linux/jiffies.h> 22 #include <linux/pm_runtime.h> 23 #include <linux/of.h> 24 #include <linux/of_net.h> 25 #include <linux/cpu.h> 26 #include <net/netdev_lock.h> 27 #include <net/netdev_rx_queue.h> 28 #include <net/rps.h> 29 30 #include "dev.h" 31 #include "net-sysfs.h" 32 33 #ifdef CONFIG_SYSFS 34 static const char fmt_hex[] = "%#x\n"; 35 static const char fmt_dec[] = "%d\n"; 36 static const char fmt_uint[] = "%u\n"; 37 static const char fmt_ulong[] = "%lu\n"; 38 static const char fmt_u64[] = "%llu\n"; 39 40 /* Caller holds RTNL, netdev->lock or RCU */ 41 static inline int dev_isalive(const struct net_device *dev) 42 { 43 return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED; 44 } 45 46 /* There is a possible ABBA deadlock between rtnl_lock and kernfs_node->active, 47 * when unregistering a net device and accessing associated sysfs files. The 48 * potential deadlock is as follow: 49 * 50 * CPU 0 CPU 1 51 * 52 * rtnl_lock vfs_read 53 * unregister_netdevice_many kernfs_seq_start 54 * device_del / kobject_put kernfs_get_active (kn->active++) 55 * kernfs_drain sysfs_kf_seq_show 56 * wait_event( rtnl_lock 57 * kn->active == KN_DEACTIVATED_BIAS) -> waits on CPU 0 to release 58 * -> waits on CPU 1 to decrease kn->active the rtnl lock. 59 * 60 * The historical fix was to use rtnl_trylock with restart_syscall to bail out 61 * of sysfs operations when the lock couldn't be taken. This fixed the above 62 * issue as it allowed CPU 1 to bail out of the ABBA situation. 63 * 64 * But it came with performances issues, as syscalls are being restarted in 65 * loops when there was contention on the rtnl lock, with huge slow downs in 66 * specific scenarios (e.g. lots of virtual interfaces created and userspace 67 * daemons querying their attributes). 68 * 69 * The idea below is to bail out of the active kernfs_node protection 70 * (kn->active) while trying to take the rtnl lock. 71 * 72 * This replaces rtnl_lock() and still has to be used with rtnl_unlock(). The 73 * net device is guaranteed to be alive if this returns successfully. 74 */ 75 static int sysfs_rtnl_lock(struct kobject *kobj, struct attribute *attr, 76 struct net_device *ndev) 77 { 78 struct kernfs_node *kn; 79 int ret = 0; 80 81 /* First, we hold a reference to the net device as the unregistration 82 * path might run in parallel. This will ensure the net device and the 83 * associated sysfs objects won't be freed while we try to take the rtnl 84 * lock. 85 */ 86 dev_hold(ndev); 87 /* sysfs_break_active_protection was introduced to allow self-removal of 88 * devices and their associated sysfs files by bailing out of the 89 * sysfs/kernfs protection. We do this here to allow the unregistration 90 * path to complete in parallel. The following takes a reference on the 91 * kobject and the kernfs_node being accessed. 92 * 93 * This works because we hold a reference onto the net device and the 94 * unregistration path will wait for us eventually in netdev_run_todo 95 * (outside an rtnl lock section). 96 */ 97 kn = sysfs_break_active_protection(kobj, attr); 98 /* We can now try to take the rtnl lock. This can't deadlock us as the 99 * unregistration path is able to drain sysfs files (kernfs_node) thanks 100 * to the above dance. 101 */ 102 if (rtnl_lock_interruptible()) { 103 ret = -ERESTARTSYS; 104 goto unbreak; 105 } 106 /* Check dismantle on the device hasn't started, otherwise deny the 107 * operation. 108 */ 109 if (!dev_isalive(ndev)) { 110 rtnl_unlock(); 111 ret = -ENODEV; 112 goto unbreak; 113 } 114 /* We are now sure the device dismantle hasn't started nor that it can 115 * start before we exit the locking section as we hold the rtnl lock. 116 * There's no need to keep unbreaking the sysfs protection nor to hold 117 * a net device reference from that point; that was only needed to take 118 * the rtnl lock. 119 */ 120 unbreak: 121 sysfs_unbreak_active_protection(kn); 122 dev_put(ndev); 123 124 return ret; 125 } 126 127 /* use same locking rules as GIF* ioctl's */ 128 static ssize_t netdev_show(const struct device *dev, 129 struct device_attribute *attr, char *buf, 130 ssize_t (*format)(const struct net_device *, char *)) 131 { 132 struct net_device *ndev = to_net_dev(dev); 133 ssize_t ret = -EINVAL; 134 135 rcu_read_lock(); 136 if (dev_isalive(ndev)) 137 ret = (*format)(ndev, buf); 138 rcu_read_unlock(); 139 140 return ret; 141 } 142 143 /* generate a show function for simple field */ 144 #define NETDEVICE_SHOW(field, format_string) \ 145 static ssize_t format_##field(const struct net_device *dev, char *buf) \ 146 { \ 147 return sysfs_emit(buf, format_string, READ_ONCE(dev->field)); \ 148 } \ 149 static ssize_t field##_show(struct device *dev, \ 150 struct device_attribute *attr, char *buf) \ 151 { \ 152 return netdev_show(dev, attr, buf, format_##field); \ 153 } \ 154 155 #define NETDEVICE_SHOW_RO(field, format_string) \ 156 NETDEVICE_SHOW(field, format_string); \ 157 static DEVICE_ATTR_RO(field) 158 159 #define NETDEVICE_SHOW_RW(field, format_string) \ 160 NETDEVICE_SHOW(field, format_string); \ 161 static DEVICE_ATTR_RW(field) 162 163 /* use same locking and permission rules as SIF* ioctl's */ 164 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, 165 const char *buf, size_t len, 166 int (*set)(struct net_device *, unsigned long)) 167 { 168 struct net_device *netdev = to_net_dev(dev); 169 struct net *net = dev_net(netdev); 170 unsigned long new; 171 int ret; 172 173 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 174 return -EPERM; 175 176 ret = kstrtoul(buf, 0, &new); 177 if (ret) 178 goto err; 179 180 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 181 if (ret) 182 goto err; 183 184 ret = (*set)(netdev, new); 185 if (ret == 0) 186 ret = len; 187 188 rtnl_unlock(); 189 err: 190 return ret; 191 } 192 193 /* Same as netdev_store() but takes netdev_lock() instead of rtnl_lock() */ 194 static ssize_t 195 netdev_lock_store(struct device *dev, struct device_attribute *attr, 196 const char *buf, size_t len, 197 int (*set)(struct net_device *, unsigned long)) 198 { 199 struct net_device *netdev = to_net_dev(dev); 200 struct net *net = dev_net(netdev); 201 unsigned long new; 202 int ret; 203 204 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 205 return -EPERM; 206 207 ret = kstrtoul(buf, 0, &new); 208 if (ret) 209 return ret; 210 211 netdev_lock(netdev); 212 213 if (dev_isalive(netdev)) { 214 ret = (*set)(netdev, new); 215 if (ret == 0) 216 ret = len; 217 } 218 netdev_unlock(netdev); 219 220 return ret; 221 } 222 223 NETDEVICE_SHOW_RO(dev_id, fmt_hex); 224 NETDEVICE_SHOW_RO(dev_port, fmt_dec); 225 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec); 226 NETDEVICE_SHOW_RO(addr_len, fmt_dec); 227 NETDEVICE_SHOW_RO(ifindex, fmt_dec); 228 NETDEVICE_SHOW_RO(type, fmt_dec); 229 NETDEVICE_SHOW_RO(link_mode, fmt_dec); 230 231 static ssize_t iflink_show(struct device *dev, struct device_attribute *attr, 232 char *buf) 233 { 234 struct net_device *ndev = to_net_dev(dev); 235 236 return sysfs_emit(buf, fmt_dec, dev_get_iflink(ndev)); 237 } 238 static DEVICE_ATTR_RO(iflink); 239 240 static ssize_t format_name_assign_type(const struct net_device *dev, char *buf) 241 { 242 return sysfs_emit(buf, fmt_dec, READ_ONCE(dev->name_assign_type)); 243 } 244 245 static ssize_t name_assign_type_show(struct device *dev, 246 struct device_attribute *attr, 247 char *buf) 248 { 249 struct net_device *ndev = to_net_dev(dev); 250 ssize_t ret = -EINVAL; 251 252 if (READ_ONCE(ndev->name_assign_type) != NET_NAME_UNKNOWN) 253 ret = netdev_show(dev, attr, buf, format_name_assign_type); 254 255 return ret; 256 } 257 static DEVICE_ATTR_RO(name_assign_type); 258 259 /* use same locking rules as GIFHWADDR ioctl's (dev_get_mac_address()) */ 260 static ssize_t address_show(struct device *dev, struct device_attribute *attr, 261 char *buf) 262 { 263 struct net_device *ndev = to_net_dev(dev); 264 ssize_t ret = -EINVAL; 265 266 down_read(&dev_addr_sem); 267 268 rcu_read_lock(); 269 if (dev_isalive(ndev)) 270 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len); 271 rcu_read_unlock(); 272 273 up_read(&dev_addr_sem); 274 return ret; 275 } 276 static DEVICE_ATTR_RO(address); 277 278 static ssize_t broadcast_show(struct device *dev, 279 struct device_attribute *attr, char *buf) 280 { 281 struct net_device *ndev = to_net_dev(dev); 282 int ret = -EINVAL; 283 284 rcu_read_lock(); 285 if (dev_isalive(ndev)) 286 ret = sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len); 287 rcu_read_unlock(); 288 return ret; 289 } 290 static DEVICE_ATTR_RO(broadcast); 291 292 static int change_carrier(struct net_device *dev, unsigned long new_carrier) 293 { 294 if (!netif_running(dev)) 295 return -EINVAL; 296 return dev_change_carrier(dev, (bool)new_carrier); 297 } 298 299 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr, 300 const char *buf, size_t len) 301 { 302 struct net_device *netdev = to_net_dev(dev); 303 304 /* The check is also done in change_carrier; this helps returning early 305 * without hitting the locking section in netdev_store. 306 */ 307 if (!netdev->netdev_ops->ndo_change_carrier) 308 return -EOPNOTSUPP; 309 310 return netdev_store(dev, attr, buf, len, change_carrier); 311 } 312 313 static ssize_t carrier_show(struct device *dev, 314 struct device_attribute *attr, char *buf) 315 { 316 struct net_device *netdev = to_net_dev(dev); 317 int ret; 318 319 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 320 if (ret) 321 return ret; 322 323 ret = -EINVAL; 324 if (netif_running(netdev)) { 325 /* Synchronize carrier state with link watch, 326 * see also rtnl_getlink(). 327 */ 328 linkwatch_sync_dev(netdev); 329 330 ret = sysfs_emit(buf, fmt_dec, !!netif_carrier_ok(netdev)); 331 } 332 333 rtnl_unlock(); 334 return ret; 335 } 336 static DEVICE_ATTR_RW(carrier); 337 338 static ssize_t speed_show(struct device *dev, 339 struct device_attribute *attr, char *buf) 340 { 341 struct net_device *netdev = to_net_dev(dev); 342 int ret = -EINVAL; 343 344 /* The check is also done in __ethtool_get_link_ksettings; this helps 345 * returning early without hitting the locking section below. 346 */ 347 if (!netdev->ethtool_ops->get_link_ksettings) 348 return ret; 349 350 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 351 if (ret) 352 return ret; 353 354 ret = -EINVAL; 355 if (netif_running(netdev)) { 356 struct ethtool_link_ksettings cmd; 357 358 if (!__ethtool_get_link_ksettings(netdev, &cmd)) 359 ret = sysfs_emit(buf, fmt_dec, cmd.base.speed); 360 } 361 rtnl_unlock(); 362 return ret; 363 } 364 static DEVICE_ATTR_RO(speed); 365 366 static ssize_t duplex_show(struct device *dev, 367 struct device_attribute *attr, char *buf) 368 { 369 struct net_device *netdev = to_net_dev(dev); 370 int ret = -EINVAL; 371 372 /* The check is also done in __ethtool_get_link_ksettings; this helps 373 * returning early without hitting the locking section below. 374 */ 375 if (!netdev->ethtool_ops->get_link_ksettings) 376 return ret; 377 378 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 379 if (ret) 380 return ret; 381 382 ret = -EINVAL; 383 if (netif_running(netdev)) { 384 struct ethtool_link_ksettings cmd; 385 386 if (!__ethtool_get_link_ksettings(netdev, &cmd)) { 387 const char *duplex; 388 389 switch (cmd.base.duplex) { 390 case DUPLEX_HALF: 391 duplex = "half"; 392 break; 393 case DUPLEX_FULL: 394 duplex = "full"; 395 break; 396 default: 397 duplex = "unknown"; 398 break; 399 } 400 ret = sysfs_emit(buf, "%s\n", duplex); 401 } 402 } 403 rtnl_unlock(); 404 return ret; 405 } 406 static DEVICE_ATTR_RO(duplex); 407 408 static ssize_t testing_show(struct device *dev, 409 struct device_attribute *attr, char *buf) 410 { 411 struct net_device *netdev = to_net_dev(dev); 412 413 if (netif_running(netdev)) 414 return sysfs_emit(buf, fmt_dec, !!netif_testing(netdev)); 415 416 return -EINVAL; 417 } 418 static DEVICE_ATTR_RO(testing); 419 420 static ssize_t dormant_show(struct device *dev, 421 struct device_attribute *attr, char *buf) 422 { 423 struct net_device *netdev = to_net_dev(dev); 424 425 if (netif_running(netdev)) 426 return sysfs_emit(buf, fmt_dec, !!netif_dormant(netdev)); 427 428 return -EINVAL; 429 } 430 static DEVICE_ATTR_RO(dormant); 431 432 static const char *const operstates[] = { 433 "unknown", 434 "notpresent", /* currently unused */ 435 "down", 436 "lowerlayerdown", 437 "testing", 438 "dormant", 439 "up" 440 }; 441 442 static ssize_t operstate_show(struct device *dev, 443 struct device_attribute *attr, char *buf) 444 { 445 const struct net_device *netdev = to_net_dev(dev); 446 unsigned char operstate; 447 448 operstate = READ_ONCE(netdev->operstate); 449 if (!netif_running(netdev)) 450 operstate = IF_OPER_DOWN; 451 452 if (operstate >= ARRAY_SIZE(operstates)) 453 return -EINVAL; /* should not happen */ 454 455 return sysfs_emit(buf, "%s\n", operstates[operstate]); 456 } 457 static DEVICE_ATTR_RO(operstate); 458 459 static ssize_t carrier_changes_show(struct device *dev, 460 struct device_attribute *attr, 461 char *buf) 462 { 463 struct net_device *netdev = to_net_dev(dev); 464 465 return sysfs_emit(buf, fmt_dec, 466 atomic_read(&netdev->carrier_up_count) + 467 atomic_read(&netdev->carrier_down_count)); 468 } 469 static DEVICE_ATTR_RO(carrier_changes); 470 471 static ssize_t carrier_up_count_show(struct device *dev, 472 struct device_attribute *attr, 473 char *buf) 474 { 475 struct net_device *netdev = to_net_dev(dev); 476 477 return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_up_count)); 478 } 479 static DEVICE_ATTR_RO(carrier_up_count); 480 481 static ssize_t carrier_down_count_show(struct device *dev, 482 struct device_attribute *attr, 483 char *buf) 484 { 485 struct net_device *netdev = to_net_dev(dev); 486 487 return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_down_count)); 488 } 489 static DEVICE_ATTR_RO(carrier_down_count); 490 491 /* read-write attributes */ 492 493 static int change_mtu(struct net_device *dev, unsigned long new_mtu) 494 { 495 return dev_set_mtu(dev, (int)new_mtu); 496 } 497 498 static ssize_t mtu_store(struct device *dev, struct device_attribute *attr, 499 const char *buf, size_t len) 500 { 501 return netdev_store(dev, attr, buf, len, change_mtu); 502 } 503 NETDEVICE_SHOW_RW(mtu, fmt_dec); 504 505 static int change_flags(struct net_device *dev, unsigned long new_flags) 506 { 507 return dev_change_flags(dev, (unsigned int)new_flags, NULL); 508 } 509 510 static ssize_t flags_store(struct device *dev, struct device_attribute *attr, 511 const char *buf, size_t len) 512 { 513 return netdev_store(dev, attr, buf, len, change_flags); 514 } 515 NETDEVICE_SHOW_RW(flags, fmt_hex); 516 517 static ssize_t tx_queue_len_store(struct device *dev, 518 struct device_attribute *attr, 519 const char *buf, size_t len) 520 { 521 if (!capable(CAP_NET_ADMIN)) 522 return -EPERM; 523 524 return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len); 525 } 526 NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec); 527 528 static int change_gro_flush_timeout(struct net_device *dev, unsigned long val) 529 { 530 netdev_set_gro_flush_timeout(dev, val); 531 return 0; 532 } 533 534 static ssize_t gro_flush_timeout_store(struct device *dev, 535 struct device_attribute *attr, 536 const char *buf, size_t len) 537 { 538 if (!capable(CAP_NET_ADMIN)) 539 return -EPERM; 540 541 return netdev_lock_store(dev, attr, buf, len, change_gro_flush_timeout); 542 } 543 NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong); 544 545 static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val) 546 { 547 if (val > S32_MAX) 548 return -ERANGE; 549 550 netdev_set_defer_hard_irqs(dev, (u32)val); 551 return 0; 552 } 553 554 static ssize_t napi_defer_hard_irqs_store(struct device *dev, 555 struct device_attribute *attr, 556 const char *buf, size_t len) 557 { 558 if (!capable(CAP_NET_ADMIN)) 559 return -EPERM; 560 561 return netdev_lock_store(dev, attr, buf, len, 562 change_napi_defer_hard_irqs); 563 } 564 NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_uint); 565 566 static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr, 567 const char *buf, size_t len) 568 { 569 struct net_device *netdev = to_net_dev(dev); 570 struct net *net = dev_net(netdev); 571 size_t count = len; 572 ssize_t ret; 573 574 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 575 return -EPERM; 576 577 /* ignore trailing newline */ 578 if (len > 0 && buf[len - 1] == '\n') 579 --count; 580 581 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 582 if (ret) 583 return ret; 584 585 ret = dev_set_alias(netdev, buf, count); 586 if (ret < 0) 587 goto err; 588 ret = len; 589 netdev_state_change(netdev); 590 err: 591 rtnl_unlock(); 592 593 return ret; 594 } 595 596 static ssize_t ifalias_show(struct device *dev, 597 struct device_attribute *attr, char *buf) 598 { 599 const struct net_device *netdev = to_net_dev(dev); 600 char tmp[IFALIASZ]; 601 ssize_t ret; 602 603 ret = dev_get_alias(netdev, tmp, sizeof(tmp)); 604 if (ret > 0) 605 ret = sysfs_emit(buf, "%s\n", tmp); 606 return ret; 607 } 608 static DEVICE_ATTR_RW(ifalias); 609 610 static int change_group(struct net_device *dev, unsigned long new_group) 611 { 612 dev_set_group(dev, (int)new_group); 613 return 0; 614 } 615 616 static ssize_t group_store(struct device *dev, struct device_attribute *attr, 617 const char *buf, size_t len) 618 { 619 return netdev_store(dev, attr, buf, len, change_group); 620 } 621 NETDEVICE_SHOW(group, fmt_dec); 622 static DEVICE_ATTR(netdev_group, 0644, group_show, group_store); 623 624 static int change_proto_down(struct net_device *dev, unsigned long proto_down) 625 { 626 return dev_change_proto_down(dev, (bool)proto_down); 627 } 628 629 static ssize_t proto_down_store(struct device *dev, 630 struct device_attribute *attr, 631 const char *buf, size_t len) 632 { 633 return netdev_store(dev, attr, buf, len, change_proto_down); 634 } 635 NETDEVICE_SHOW_RW(proto_down, fmt_dec); 636 637 static ssize_t phys_port_id_show(struct device *dev, 638 struct device_attribute *attr, char *buf) 639 { 640 struct net_device *netdev = to_net_dev(dev); 641 struct netdev_phys_item_id ppid; 642 ssize_t ret; 643 644 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 645 if (ret) 646 return ret; 647 648 ret = dev_get_phys_port_id(netdev, &ppid); 649 if (!ret) 650 ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id); 651 652 rtnl_unlock(); 653 654 return ret; 655 } 656 static DEVICE_ATTR_RO(phys_port_id); 657 658 static ssize_t phys_port_name_show(struct device *dev, 659 struct device_attribute *attr, char *buf) 660 { 661 struct net_device *netdev = to_net_dev(dev); 662 char name[IFNAMSIZ]; 663 ssize_t ret; 664 665 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 666 if (ret) 667 return ret; 668 669 ret = dev_get_phys_port_name(netdev, name, sizeof(name)); 670 if (!ret) 671 ret = sysfs_emit(buf, "%s\n", name); 672 673 rtnl_unlock(); 674 675 return ret; 676 } 677 static DEVICE_ATTR_RO(phys_port_name); 678 679 static ssize_t phys_switch_id_show(struct device *dev, 680 struct device_attribute *attr, char *buf) 681 { 682 struct net_device *netdev = to_net_dev(dev); 683 struct netdev_phys_item_id ppid = { }; 684 ssize_t ret; 685 686 ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev); 687 if (ret) 688 return ret; 689 690 ret = dev_get_port_parent_id(netdev, &ppid, false); 691 if (!ret) 692 ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id); 693 694 rtnl_unlock(); 695 696 return ret; 697 } 698 static DEVICE_ATTR_RO(phys_switch_id); 699 700 static struct attribute *netdev_phys_attrs[] __ro_after_init = { 701 &dev_attr_phys_port_id.attr, 702 &dev_attr_phys_port_name.attr, 703 &dev_attr_phys_switch_id.attr, 704 NULL, 705 }; 706 707 static umode_t netdev_phys_is_visible(struct kobject *kobj, 708 struct attribute *attr, int index) 709 { 710 struct device *dev = kobj_to_dev(kobj); 711 struct net_device *netdev = to_net_dev(dev); 712 713 if (attr == &dev_attr_phys_port_id.attr) { 714 if (!netdev->netdev_ops->ndo_get_phys_port_id) 715 return 0; 716 } else if (attr == &dev_attr_phys_port_name.attr) { 717 if (!netdev->netdev_ops->ndo_get_phys_port_name && 718 !netdev->devlink_port) 719 return 0; 720 } else if (attr == &dev_attr_phys_switch_id.attr) { 721 if (!netdev->netdev_ops->ndo_get_port_parent_id && 722 !netdev->devlink_port) 723 return 0; 724 } 725 726 return attr->mode; 727 } 728 729 static const struct attribute_group netdev_phys_group = { 730 .attrs = netdev_phys_attrs, 731 .is_visible = netdev_phys_is_visible, 732 }; 733 734 static ssize_t threaded_show(struct device *dev, 735 struct device_attribute *attr, char *buf) 736 { 737 struct net_device *netdev = to_net_dev(dev); 738 ssize_t ret = -EINVAL; 739 740 rcu_read_lock(); 741 742 if (dev_isalive(netdev)) 743 ret = sysfs_emit(buf, fmt_dec, READ_ONCE(netdev->threaded)); 744 745 rcu_read_unlock(); 746 747 return ret; 748 } 749 750 static int modify_napi_threaded(struct net_device *dev, unsigned long val) 751 { 752 int ret; 753 754 if (list_empty(&dev->napi_list)) 755 return -EOPNOTSUPP; 756 757 if (val != 0 && val != 1) 758 return -EOPNOTSUPP; 759 760 ret = dev_set_threaded(dev, val); 761 762 return ret; 763 } 764 765 static ssize_t threaded_store(struct device *dev, 766 struct device_attribute *attr, 767 const char *buf, size_t len) 768 { 769 return netdev_lock_store(dev, attr, buf, len, modify_napi_threaded); 770 } 771 static DEVICE_ATTR_RW(threaded); 772 773 static struct attribute *net_class_attrs[] __ro_after_init = { 774 &dev_attr_netdev_group.attr, 775 &dev_attr_type.attr, 776 &dev_attr_dev_id.attr, 777 &dev_attr_dev_port.attr, 778 &dev_attr_iflink.attr, 779 &dev_attr_ifindex.attr, 780 &dev_attr_name_assign_type.attr, 781 &dev_attr_addr_assign_type.attr, 782 &dev_attr_addr_len.attr, 783 &dev_attr_link_mode.attr, 784 &dev_attr_address.attr, 785 &dev_attr_broadcast.attr, 786 &dev_attr_speed.attr, 787 &dev_attr_duplex.attr, 788 &dev_attr_dormant.attr, 789 &dev_attr_testing.attr, 790 &dev_attr_operstate.attr, 791 &dev_attr_carrier_changes.attr, 792 &dev_attr_ifalias.attr, 793 &dev_attr_carrier.attr, 794 &dev_attr_mtu.attr, 795 &dev_attr_flags.attr, 796 &dev_attr_tx_queue_len.attr, 797 &dev_attr_gro_flush_timeout.attr, 798 &dev_attr_napi_defer_hard_irqs.attr, 799 &dev_attr_proto_down.attr, 800 &dev_attr_carrier_up_count.attr, 801 &dev_attr_carrier_down_count.attr, 802 &dev_attr_threaded.attr, 803 NULL, 804 }; 805 ATTRIBUTE_GROUPS(net_class); 806 807 /* Show a given an attribute in the statistics group */ 808 static ssize_t netstat_show(const struct device *d, 809 struct device_attribute *attr, char *buf, 810 unsigned long offset) 811 { 812 struct net_device *dev = to_net_dev(d); 813 ssize_t ret = -EINVAL; 814 815 WARN_ON(offset > sizeof(struct rtnl_link_stats64) || 816 offset % sizeof(u64) != 0); 817 818 rcu_read_lock(); 819 if (dev_isalive(dev)) { 820 struct rtnl_link_stats64 temp; 821 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); 822 823 ret = sysfs_emit(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset)); 824 } 825 rcu_read_unlock(); 826 return ret; 827 } 828 829 /* generate a read-only statistics attribute */ 830 #define NETSTAT_ENTRY(name) \ 831 static ssize_t name##_show(struct device *d, \ 832 struct device_attribute *attr, char *buf) \ 833 { \ 834 return netstat_show(d, attr, buf, \ 835 offsetof(struct rtnl_link_stats64, name)); \ 836 } \ 837 static DEVICE_ATTR_RO(name) 838 839 NETSTAT_ENTRY(rx_packets); 840 NETSTAT_ENTRY(tx_packets); 841 NETSTAT_ENTRY(rx_bytes); 842 NETSTAT_ENTRY(tx_bytes); 843 NETSTAT_ENTRY(rx_errors); 844 NETSTAT_ENTRY(tx_errors); 845 NETSTAT_ENTRY(rx_dropped); 846 NETSTAT_ENTRY(tx_dropped); 847 NETSTAT_ENTRY(multicast); 848 NETSTAT_ENTRY(collisions); 849 NETSTAT_ENTRY(rx_length_errors); 850 NETSTAT_ENTRY(rx_over_errors); 851 NETSTAT_ENTRY(rx_crc_errors); 852 NETSTAT_ENTRY(rx_frame_errors); 853 NETSTAT_ENTRY(rx_fifo_errors); 854 NETSTAT_ENTRY(rx_missed_errors); 855 NETSTAT_ENTRY(tx_aborted_errors); 856 NETSTAT_ENTRY(tx_carrier_errors); 857 NETSTAT_ENTRY(tx_fifo_errors); 858 NETSTAT_ENTRY(tx_heartbeat_errors); 859 NETSTAT_ENTRY(tx_window_errors); 860 NETSTAT_ENTRY(rx_compressed); 861 NETSTAT_ENTRY(tx_compressed); 862 NETSTAT_ENTRY(rx_nohandler); 863 864 static struct attribute *netstat_attrs[] __ro_after_init = { 865 &dev_attr_rx_packets.attr, 866 &dev_attr_tx_packets.attr, 867 &dev_attr_rx_bytes.attr, 868 &dev_attr_tx_bytes.attr, 869 &dev_attr_rx_errors.attr, 870 &dev_attr_tx_errors.attr, 871 &dev_attr_rx_dropped.attr, 872 &dev_attr_tx_dropped.attr, 873 &dev_attr_multicast.attr, 874 &dev_attr_collisions.attr, 875 &dev_attr_rx_length_errors.attr, 876 &dev_attr_rx_over_errors.attr, 877 &dev_attr_rx_crc_errors.attr, 878 &dev_attr_rx_frame_errors.attr, 879 &dev_attr_rx_fifo_errors.attr, 880 &dev_attr_rx_missed_errors.attr, 881 &dev_attr_tx_aborted_errors.attr, 882 &dev_attr_tx_carrier_errors.attr, 883 &dev_attr_tx_fifo_errors.attr, 884 &dev_attr_tx_heartbeat_errors.attr, 885 &dev_attr_tx_window_errors.attr, 886 &dev_attr_rx_compressed.attr, 887 &dev_attr_tx_compressed.attr, 888 &dev_attr_rx_nohandler.attr, 889 NULL 890 }; 891 892 static const struct attribute_group netstat_group = { 893 .name = "statistics", 894 .attrs = netstat_attrs, 895 }; 896 897 static struct attribute *wireless_attrs[] = { 898 NULL 899 }; 900 901 static const struct attribute_group wireless_group = { 902 .name = "wireless", 903 .attrs = wireless_attrs, 904 }; 905 906 static bool wireless_group_needed(struct net_device *ndev) 907 { 908 #if IS_ENABLED(CONFIG_CFG80211) 909 if (ndev->ieee80211_ptr) 910 return true; 911 #endif 912 #if IS_ENABLED(CONFIG_WIRELESS_EXT) 913 if (ndev->wireless_handlers) 914 return true; 915 #endif 916 return false; 917 } 918 919 #else /* CONFIG_SYSFS */ 920 #define net_class_groups NULL 921 #endif /* CONFIG_SYSFS */ 922 923 #ifdef CONFIG_SYSFS 924 #define to_rx_queue_attr(_attr) \ 925 container_of(_attr, struct rx_queue_attribute, attr) 926 927 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) 928 929 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, 930 char *buf) 931 { 932 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 933 struct netdev_rx_queue *queue = to_rx_queue(kobj); 934 935 if (!attribute->show) 936 return -EIO; 937 938 return attribute->show(queue, buf); 939 } 940 941 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, 942 const char *buf, size_t count) 943 { 944 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 945 struct netdev_rx_queue *queue = to_rx_queue(kobj); 946 947 if (!attribute->store) 948 return -EIO; 949 950 return attribute->store(queue, buf, count); 951 } 952 953 static const struct sysfs_ops rx_queue_sysfs_ops = { 954 .show = rx_queue_attr_show, 955 .store = rx_queue_attr_store, 956 }; 957 958 #ifdef CONFIG_RPS 959 static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf) 960 { 961 struct rps_map *map; 962 cpumask_var_t mask; 963 int i, len; 964 965 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 966 return -ENOMEM; 967 968 rcu_read_lock(); 969 map = rcu_dereference(queue->rps_map); 970 if (map) 971 for (i = 0; i < map->len; i++) 972 cpumask_set_cpu(map->cpus[i], mask); 973 974 len = sysfs_emit(buf, "%*pb\n", cpumask_pr_args(mask)); 975 rcu_read_unlock(); 976 free_cpumask_var(mask); 977 978 return len < PAGE_SIZE ? len : -EINVAL; 979 } 980 981 static int netdev_rx_queue_set_rps_mask(struct netdev_rx_queue *queue, 982 cpumask_var_t mask) 983 { 984 static DEFINE_MUTEX(rps_map_mutex); 985 struct rps_map *old_map, *map; 986 int cpu, i; 987 988 map = kzalloc(max_t(unsigned int, 989 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), 990 GFP_KERNEL); 991 if (!map) 992 return -ENOMEM; 993 994 i = 0; 995 for_each_cpu_and(cpu, mask, cpu_online_mask) 996 map->cpus[i++] = cpu; 997 998 if (i) { 999 map->len = i; 1000 } else { 1001 kfree(map); 1002 map = NULL; 1003 } 1004 1005 mutex_lock(&rps_map_mutex); 1006 old_map = rcu_dereference_protected(queue->rps_map, 1007 mutex_is_locked(&rps_map_mutex)); 1008 rcu_assign_pointer(queue->rps_map, map); 1009 1010 if (map) 1011 static_branch_inc(&rps_needed); 1012 if (old_map) 1013 static_branch_dec(&rps_needed); 1014 1015 mutex_unlock(&rps_map_mutex); 1016 1017 if (old_map) 1018 kfree_rcu(old_map, rcu); 1019 return 0; 1020 } 1021 1022 int rps_cpumask_housekeeping(struct cpumask *mask) 1023 { 1024 if (!cpumask_empty(mask)) { 1025 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 1026 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ)); 1027 if (cpumask_empty(mask)) 1028 return -EINVAL; 1029 } 1030 return 0; 1031 } 1032 1033 static ssize_t store_rps_map(struct netdev_rx_queue *queue, 1034 const char *buf, size_t len) 1035 { 1036 cpumask_var_t mask; 1037 int err; 1038 1039 if (!capable(CAP_NET_ADMIN)) 1040 return -EPERM; 1041 1042 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 1043 return -ENOMEM; 1044 1045 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 1046 if (err) 1047 goto out; 1048 1049 err = rps_cpumask_housekeeping(mask); 1050 if (err) 1051 goto out; 1052 1053 err = netdev_rx_queue_set_rps_mask(queue, mask); 1054 1055 out: 1056 free_cpumask_var(mask); 1057 return err ? : len; 1058 } 1059 1060 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 1061 char *buf) 1062 { 1063 struct rps_dev_flow_table *flow_table; 1064 unsigned long val = 0; 1065 1066 rcu_read_lock(); 1067 flow_table = rcu_dereference(queue->rps_flow_table); 1068 if (flow_table) 1069 val = 1UL << flow_table->log; 1070 rcu_read_unlock(); 1071 1072 return sysfs_emit(buf, "%lu\n", val); 1073 } 1074 1075 static void rps_dev_flow_table_release(struct rcu_head *rcu) 1076 { 1077 struct rps_dev_flow_table *table = container_of(rcu, 1078 struct rps_dev_flow_table, rcu); 1079 vfree(table); 1080 } 1081 1082 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 1083 const char *buf, size_t len) 1084 { 1085 unsigned long mask, count; 1086 struct rps_dev_flow_table *table, *old_table; 1087 static DEFINE_SPINLOCK(rps_dev_flow_lock); 1088 int rc; 1089 1090 if (!capable(CAP_NET_ADMIN)) 1091 return -EPERM; 1092 1093 rc = kstrtoul(buf, 0, &count); 1094 if (rc < 0) 1095 return rc; 1096 1097 if (count) { 1098 mask = count - 1; 1099 /* mask = roundup_pow_of_two(count) - 1; 1100 * without overflows... 1101 */ 1102 while ((mask | (mask >> 1)) != mask) 1103 mask |= (mask >> 1); 1104 /* On 64 bit arches, must check mask fits in table->mask (u32), 1105 * and on 32bit arches, must check 1106 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow. 1107 */ 1108 #if BITS_PER_LONG > 32 1109 if (mask > (unsigned long)(u32)mask) 1110 return -EINVAL; 1111 #else 1112 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) 1113 / sizeof(struct rps_dev_flow)) { 1114 /* Enforce a limit to prevent overflow */ 1115 return -EINVAL; 1116 } 1117 #endif 1118 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); 1119 if (!table) 1120 return -ENOMEM; 1121 1122 table->log = ilog2(mask) + 1; 1123 for (count = 0; count <= mask; count++) 1124 table->flows[count].cpu = RPS_NO_CPU; 1125 } else { 1126 table = NULL; 1127 } 1128 1129 spin_lock(&rps_dev_flow_lock); 1130 old_table = rcu_dereference_protected(queue->rps_flow_table, 1131 lockdep_is_held(&rps_dev_flow_lock)); 1132 rcu_assign_pointer(queue->rps_flow_table, table); 1133 spin_unlock(&rps_dev_flow_lock); 1134 1135 if (old_table) 1136 call_rcu(&old_table->rcu, rps_dev_flow_table_release); 1137 1138 return len; 1139 } 1140 1141 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init 1142 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map); 1143 1144 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init 1145 = __ATTR(rps_flow_cnt, 0644, 1146 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); 1147 #endif /* CONFIG_RPS */ 1148 1149 static struct attribute *rx_queue_default_attrs[] __ro_after_init = { 1150 #ifdef CONFIG_RPS 1151 &rps_cpus_attribute.attr, 1152 &rps_dev_flow_table_cnt_attribute.attr, 1153 #endif 1154 NULL 1155 }; 1156 ATTRIBUTE_GROUPS(rx_queue_default); 1157 1158 static void rx_queue_release(struct kobject *kobj) 1159 { 1160 struct netdev_rx_queue *queue = to_rx_queue(kobj); 1161 #ifdef CONFIG_RPS 1162 struct rps_map *map; 1163 struct rps_dev_flow_table *flow_table; 1164 1165 map = rcu_dereference_protected(queue->rps_map, 1); 1166 if (map) { 1167 RCU_INIT_POINTER(queue->rps_map, NULL); 1168 kfree_rcu(map, rcu); 1169 } 1170 1171 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); 1172 if (flow_table) { 1173 RCU_INIT_POINTER(queue->rps_flow_table, NULL); 1174 call_rcu(&flow_table->rcu, rps_dev_flow_table_release); 1175 } 1176 #endif 1177 1178 memset(kobj, 0, sizeof(*kobj)); 1179 netdev_put(queue->dev, &queue->dev_tracker); 1180 } 1181 1182 static const void *rx_queue_namespace(const struct kobject *kobj) 1183 { 1184 struct netdev_rx_queue *queue = to_rx_queue(kobj); 1185 struct device *dev = &queue->dev->dev; 1186 const void *ns = NULL; 1187 1188 if (dev->class && dev->class->namespace) 1189 ns = dev->class->namespace(dev); 1190 1191 return ns; 1192 } 1193 1194 static void rx_queue_get_ownership(const struct kobject *kobj, 1195 kuid_t *uid, kgid_t *gid) 1196 { 1197 const struct net *net = rx_queue_namespace(kobj); 1198 1199 net_ns_get_ownership(net, uid, gid); 1200 } 1201 1202 static const struct kobj_type rx_queue_ktype = { 1203 .sysfs_ops = &rx_queue_sysfs_ops, 1204 .release = rx_queue_release, 1205 .namespace = rx_queue_namespace, 1206 .get_ownership = rx_queue_get_ownership, 1207 }; 1208 1209 static int rx_queue_default_mask(struct net_device *dev, 1210 struct netdev_rx_queue *queue) 1211 { 1212 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL) 1213 struct cpumask *rps_default_mask = READ_ONCE(dev_net(dev)->core.rps_default_mask); 1214 1215 if (rps_default_mask && !cpumask_empty(rps_default_mask)) 1216 return netdev_rx_queue_set_rps_mask(queue, rps_default_mask); 1217 #endif 1218 return 0; 1219 } 1220 1221 static int rx_queue_add_kobject(struct net_device *dev, int index) 1222 { 1223 struct netdev_rx_queue *queue = dev->_rx + index; 1224 struct kobject *kobj = &queue->kobj; 1225 int error = 0; 1226 1227 /* Rx queues are cleared in rx_queue_release to allow later 1228 * re-registration. This is triggered when their kobj refcount is 1229 * dropped. 1230 * 1231 * If a queue is removed while both a read (or write) operation and a 1232 * the re-addition of the same queue are pending (waiting on rntl_lock) 1233 * it might happen that the re-addition will execute before the read, 1234 * making the initial removal to never happen (queue's kobj refcount 1235 * won't drop enough because of the pending read). In such rare case, 1236 * return to allow the removal operation to complete. 1237 */ 1238 if (unlikely(kobj->state_initialized)) { 1239 netdev_warn_once(dev, "Cannot re-add rx queues before their removal completed"); 1240 return -EAGAIN; 1241 } 1242 1243 /* Kobject_put later will trigger rx_queue_release call which 1244 * decreases dev refcount: Take that reference here 1245 */ 1246 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL); 1247 1248 kobj->kset = dev->queues_kset; 1249 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, 1250 "rx-%u", index); 1251 if (error) 1252 goto err; 1253 1254 queue->groups = rx_queue_default_groups; 1255 error = sysfs_create_groups(kobj, queue->groups); 1256 if (error) 1257 goto err; 1258 1259 if (dev->sysfs_rx_queue_group) { 1260 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group); 1261 if (error) 1262 goto err_default_groups; 1263 } 1264 1265 error = rx_queue_default_mask(dev, queue); 1266 if (error) 1267 goto err_default_groups; 1268 1269 kobject_uevent(kobj, KOBJ_ADD); 1270 1271 return error; 1272 1273 err_default_groups: 1274 sysfs_remove_groups(kobj, queue->groups); 1275 err: 1276 kobject_put(kobj); 1277 return error; 1278 } 1279 1280 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid, 1281 kgid_t kgid) 1282 { 1283 struct netdev_rx_queue *queue = dev->_rx + index; 1284 struct kobject *kobj = &queue->kobj; 1285 int error; 1286 1287 error = sysfs_change_owner(kobj, kuid, kgid); 1288 if (error) 1289 return error; 1290 1291 if (dev->sysfs_rx_queue_group) 1292 error = sysfs_group_change_owner( 1293 kobj, dev->sysfs_rx_queue_group, kuid, kgid); 1294 1295 return error; 1296 } 1297 #endif /* CONFIG_SYSFS */ 1298 1299 int 1300 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) 1301 { 1302 #ifdef CONFIG_SYSFS 1303 int i; 1304 int error = 0; 1305 1306 #ifndef CONFIG_RPS 1307 if (!dev->sysfs_rx_queue_group) 1308 return 0; 1309 #endif 1310 for (i = old_num; i < new_num; i++) { 1311 error = rx_queue_add_kobject(dev, i); 1312 if (error) { 1313 new_num = old_num; 1314 break; 1315 } 1316 } 1317 1318 while (--i >= new_num) { 1319 struct netdev_rx_queue *queue = &dev->_rx[i]; 1320 struct kobject *kobj = &queue->kobj; 1321 1322 if (!refcount_read(&dev_net(dev)->ns.count)) 1323 kobj->uevent_suppress = 1; 1324 if (dev->sysfs_rx_queue_group) 1325 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group); 1326 sysfs_remove_groups(kobj, queue->groups); 1327 kobject_put(kobj); 1328 } 1329 1330 return error; 1331 #else 1332 return 0; 1333 #endif 1334 } 1335 1336 static int net_rx_queue_change_owner(struct net_device *dev, int num, 1337 kuid_t kuid, kgid_t kgid) 1338 { 1339 #ifdef CONFIG_SYSFS 1340 int error = 0; 1341 int i; 1342 1343 #ifndef CONFIG_RPS 1344 if (!dev->sysfs_rx_queue_group) 1345 return 0; 1346 #endif 1347 for (i = 0; i < num; i++) { 1348 error = rx_queue_change_owner(dev, i, kuid, kgid); 1349 if (error) 1350 break; 1351 } 1352 1353 return error; 1354 #else 1355 return 0; 1356 #endif 1357 } 1358 1359 #ifdef CONFIG_SYSFS 1360 /* 1361 * netdev_queue sysfs structures and functions. 1362 */ 1363 struct netdev_queue_attribute { 1364 struct attribute attr; 1365 ssize_t (*show)(struct kobject *kobj, struct attribute *attr, 1366 struct netdev_queue *queue, char *buf); 1367 ssize_t (*store)(struct kobject *kobj, struct attribute *attr, 1368 struct netdev_queue *queue, const char *buf, 1369 size_t len); 1370 }; 1371 #define to_netdev_queue_attr(_attr) \ 1372 container_of(_attr, struct netdev_queue_attribute, attr) 1373 1374 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) 1375 1376 static ssize_t netdev_queue_attr_show(struct kobject *kobj, 1377 struct attribute *attr, char *buf) 1378 { 1379 const struct netdev_queue_attribute *attribute 1380 = to_netdev_queue_attr(attr); 1381 struct netdev_queue *queue = to_netdev_queue(kobj); 1382 1383 if (!attribute->show) 1384 return -EIO; 1385 1386 return attribute->show(kobj, attr, queue, buf); 1387 } 1388 1389 static ssize_t netdev_queue_attr_store(struct kobject *kobj, 1390 struct attribute *attr, 1391 const char *buf, size_t count) 1392 { 1393 const struct netdev_queue_attribute *attribute 1394 = to_netdev_queue_attr(attr); 1395 struct netdev_queue *queue = to_netdev_queue(kobj); 1396 1397 if (!attribute->store) 1398 return -EIO; 1399 1400 return attribute->store(kobj, attr, queue, buf, count); 1401 } 1402 1403 static const struct sysfs_ops netdev_queue_sysfs_ops = { 1404 .show = netdev_queue_attr_show, 1405 .store = netdev_queue_attr_store, 1406 }; 1407 1408 static ssize_t tx_timeout_show(struct kobject *kobj, struct attribute *attr, 1409 struct netdev_queue *queue, char *buf) 1410 { 1411 unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout); 1412 1413 return sysfs_emit(buf, fmt_ulong, trans_timeout); 1414 } 1415 1416 static unsigned int get_netdev_queue_index(struct netdev_queue *queue) 1417 { 1418 struct net_device *dev = queue->dev; 1419 unsigned int i; 1420 1421 i = queue - dev->_tx; 1422 BUG_ON(i >= dev->num_tx_queues); 1423 1424 return i; 1425 } 1426 1427 static ssize_t traffic_class_show(struct kobject *kobj, struct attribute *attr, 1428 struct netdev_queue *queue, char *buf) 1429 { 1430 struct net_device *dev = queue->dev; 1431 int num_tc, tc, index, ret; 1432 1433 if (!netif_is_multiqueue(dev)) 1434 return -ENOENT; 1435 1436 ret = sysfs_rtnl_lock(kobj, attr, queue->dev); 1437 if (ret) 1438 return ret; 1439 1440 index = get_netdev_queue_index(queue); 1441 1442 /* If queue belongs to subordinate dev use its TC mapping */ 1443 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; 1444 1445 num_tc = dev->num_tc; 1446 tc = netdev_txq_to_tc(dev, index); 1447 1448 rtnl_unlock(); 1449 1450 if (tc < 0) 1451 return -EINVAL; 1452 1453 /* We can report the traffic class one of two ways: 1454 * Subordinate device traffic classes are reported with the traffic 1455 * class first, and then the subordinate class so for example TC0 on 1456 * subordinate device 2 will be reported as "0-2". If the queue 1457 * belongs to the root device it will be reported with just the 1458 * traffic class, so just "0" for TC 0 for example. 1459 */ 1460 return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) : 1461 sysfs_emit(buf, "%d\n", tc); 1462 } 1463 1464 #ifdef CONFIG_XPS 1465 static ssize_t tx_maxrate_show(struct kobject *kobj, struct attribute *attr, 1466 struct netdev_queue *queue, char *buf) 1467 { 1468 return sysfs_emit(buf, "%lu\n", queue->tx_maxrate); 1469 } 1470 1471 static ssize_t tx_maxrate_store(struct kobject *kobj, struct attribute *attr, 1472 struct netdev_queue *queue, const char *buf, 1473 size_t len) 1474 { 1475 int err, index = get_netdev_queue_index(queue); 1476 struct net_device *dev = queue->dev; 1477 u32 rate = 0; 1478 1479 if (!capable(CAP_NET_ADMIN)) 1480 return -EPERM; 1481 1482 /* The check is also done later; this helps returning early without 1483 * hitting the locking section below. 1484 */ 1485 if (!dev->netdev_ops->ndo_set_tx_maxrate) 1486 return -EOPNOTSUPP; 1487 1488 err = kstrtou32(buf, 10, &rate); 1489 if (err < 0) 1490 return err; 1491 1492 err = sysfs_rtnl_lock(kobj, attr, dev); 1493 if (err) 1494 return err; 1495 1496 err = -EOPNOTSUPP; 1497 netdev_lock_ops(dev); 1498 if (dev->netdev_ops->ndo_set_tx_maxrate) 1499 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate); 1500 netdev_unlock_ops(dev); 1501 1502 if (!err) { 1503 queue->tx_maxrate = rate; 1504 rtnl_unlock(); 1505 return len; 1506 } 1507 1508 rtnl_unlock(); 1509 return err; 1510 } 1511 1512 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init 1513 = __ATTR_RW(tx_maxrate); 1514 #endif 1515 1516 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init 1517 = __ATTR_RO(tx_timeout); 1518 1519 static struct netdev_queue_attribute queue_traffic_class __ro_after_init 1520 = __ATTR_RO(traffic_class); 1521 1522 #ifdef CONFIG_BQL 1523 /* 1524 * Byte queue limits sysfs structures and functions. 1525 */ 1526 static ssize_t bql_show(char *buf, unsigned int value) 1527 { 1528 return sysfs_emit(buf, "%u\n", value); 1529 } 1530 1531 static ssize_t bql_set(const char *buf, const size_t count, 1532 unsigned int *pvalue) 1533 { 1534 unsigned int value; 1535 int err; 1536 1537 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) { 1538 value = DQL_MAX_LIMIT; 1539 } else { 1540 err = kstrtouint(buf, 10, &value); 1541 if (err < 0) 1542 return err; 1543 if (value > DQL_MAX_LIMIT) 1544 return -EINVAL; 1545 } 1546 1547 *pvalue = value; 1548 1549 return count; 1550 } 1551 1552 static ssize_t bql_show_hold_time(struct kobject *kobj, struct attribute *attr, 1553 struct netdev_queue *queue, char *buf) 1554 { 1555 struct dql *dql = &queue->dql; 1556 1557 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); 1558 } 1559 1560 static ssize_t bql_set_hold_time(struct kobject *kobj, struct attribute *attr, 1561 struct netdev_queue *queue, const char *buf, 1562 size_t len) 1563 { 1564 struct dql *dql = &queue->dql; 1565 unsigned int value; 1566 int err; 1567 1568 err = kstrtouint(buf, 10, &value); 1569 if (err < 0) 1570 return err; 1571 1572 dql->slack_hold_time = msecs_to_jiffies(value); 1573 1574 return len; 1575 } 1576 1577 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init 1578 = __ATTR(hold_time, 0644, 1579 bql_show_hold_time, bql_set_hold_time); 1580 1581 static ssize_t bql_show_stall_thrs(struct kobject *kobj, struct attribute *attr, 1582 struct netdev_queue *queue, char *buf) 1583 { 1584 struct dql *dql = &queue->dql; 1585 1586 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->stall_thrs)); 1587 } 1588 1589 static ssize_t bql_set_stall_thrs(struct kobject *kobj, struct attribute *attr, 1590 struct netdev_queue *queue, const char *buf, 1591 size_t len) 1592 { 1593 struct dql *dql = &queue->dql; 1594 unsigned int value; 1595 int err; 1596 1597 err = kstrtouint(buf, 10, &value); 1598 if (err < 0) 1599 return err; 1600 1601 value = msecs_to_jiffies(value); 1602 if (value && (value < 4 || value > 4 / 2 * BITS_PER_LONG)) 1603 return -ERANGE; 1604 1605 if (!dql->stall_thrs && value) 1606 dql->last_reap = jiffies; 1607 /* Force last_reap to be live */ 1608 smp_wmb(); 1609 dql->stall_thrs = value; 1610 1611 return len; 1612 } 1613 1614 static struct netdev_queue_attribute bql_stall_thrs_attribute __ro_after_init = 1615 __ATTR(stall_thrs, 0644, bql_show_stall_thrs, bql_set_stall_thrs); 1616 1617 static ssize_t bql_show_stall_max(struct kobject *kobj, struct attribute *attr, 1618 struct netdev_queue *queue, char *buf) 1619 { 1620 return sysfs_emit(buf, "%u\n", READ_ONCE(queue->dql.stall_max)); 1621 } 1622 1623 static ssize_t bql_set_stall_max(struct kobject *kobj, struct attribute *attr, 1624 struct netdev_queue *queue, const char *buf, 1625 size_t len) 1626 { 1627 WRITE_ONCE(queue->dql.stall_max, 0); 1628 return len; 1629 } 1630 1631 static struct netdev_queue_attribute bql_stall_max_attribute __ro_after_init = 1632 __ATTR(stall_max, 0644, bql_show_stall_max, bql_set_stall_max); 1633 1634 static ssize_t bql_show_stall_cnt(struct kobject *kobj, struct attribute *attr, 1635 struct netdev_queue *queue, char *buf) 1636 { 1637 struct dql *dql = &queue->dql; 1638 1639 return sysfs_emit(buf, "%lu\n", dql->stall_cnt); 1640 } 1641 1642 static struct netdev_queue_attribute bql_stall_cnt_attribute __ro_after_init = 1643 __ATTR(stall_cnt, 0444, bql_show_stall_cnt, NULL); 1644 1645 static ssize_t bql_show_inflight(struct kobject *kobj, struct attribute *attr, 1646 struct netdev_queue *queue, char *buf) 1647 { 1648 struct dql *dql = &queue->dql; 1649 1650 return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed); 1651 } 1652 1653 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init = 1654 __ATTR(inflight, 0444, bql_show_inflight, NULL); 1655 1656 #define BQL_ATTR(NAME, FIELD) \ 1657 static ssize_t bql_show_ ## NAME(struct kobject *kobj, \ 1658 struct attribute *attr, \ 1659 struct netdev_queue *queue, char *buf) \ 1660 { \ 1661 return bql_show(buf, queue->dql.FIELD); \ 1662 } \ 1663 \ 1664 static ssize_t bql_set_ ## NAME(struct kobject *kobj, \ 1665 struct attribute *attr, \ 1666 struct netdev_queue *queue, \ 1667 const char *buf, size_t len) \ 1668 { \ 1669 return bql_set(buf, len, &queue->dql.FIELD); \ 1670 } \ 1671 \ 1672 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \ 1673 = __ATTR(NAME, 0644, \ 1674 bql_show_ ## NAME, bql_set_ ## NAME) 1675 1676 BQL_ATTR(limit, limit); 1677 BQL_ATTR(limit_max, max_limit); 1678 BQL_ATTR(limit_min, min_limit); 1679 1680 static struct attribute *dql_attrs[] __ro_after_init = { 1681 &bql_limit_attribute.attr, 1682 &bql_limit_max_attribute.attr, 1683 &bql_limit_min_attribute.attr, 1684 &bql_hold_time_attribute.attr, 1685 &bql_inflight_attribute.attr, 1686 &bql_stall_thrs_attribute.attr, 1687 &bql_stall_cnt_attribute.attr, 1688 &bql_stall_max_attribute.attr, 1689 NULL 1690 }; 1691 1692 static const struct attribute_group dql_group = { 1693 .name = "byte_queue_limits", 1694 .attrs = dql_attrs, 1695 }; 1696 #else 1697 /* Fake declaration, all the code using it should be dead */ 1698 static const struct attribute_group dql_group = {}; 1699 #endif /* CONFIG_BQL */ 1700 1701 #ifdef CONFIG_XPS 1702 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index, 1703 int tc, char *buf, enum xps_map_type type) 1704 { 1705 struct xps_dev_maps *dev_maps; 1706 unsigned long *mask; 1707 unsigned int nr_ids; 1708 int j, len; 1709 1710 rcu_read_lock(); 1711 dev_maps = rcu_dereference(dev->xps_maps[type]); 1712 1713 /* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0 1714 * when dev_maps hasn't been allocated yet, to be backward compatible. 1715 */ 1716 nr_ids = dev_maps ? dev_maps->nr_ids : 1717 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues); 1718 1719 mask = bitmap_zalloc(nr_ids, GFP_NOWAIT); 1720 if (!mask) { 1721 rcu_read_unlock(); 1722 return -ENOMEM; 1723 } 1724 1725 if (!dev_maps || tc >= dev_maps->num_tc) 1726 goto out_no_maps; 1727 1728 for (j = 0; j < nr_ids; j++) { 1729 int i, tci = j * dev_maps->num_tc + tc; 1730 struct xps_map *map; 1731 1732 map = rcu_dereference(dev_maps->attr_map[tci]); 1733 if (!map) 1734 continue; 1735 1736 for (i = map->len; i--;) { 1737 if (map->queues[i] == index) { 1738 __set_bit(j, mask); 1739 break; 1740 } 1741 } 1742 } 1743 out_no_maps: 1744 rcu_read_unlock(); 1745 1746 len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids); 1747 bitmap_free(mask); 1748 1749 return len < PAGE_SIZE ? len : -EINVAL; 1750 } 1751 1752 static ssize_t xps_cpus_show(struct kobject *kobj, struct attribute *attr, 1753 struct netdev_queue *queue, char *buf) 1754 { 1755 struct net_device *dev = queue->dev; 1756 unsigned int index; 1757 int len, tc, ret; 1758 1759 if (!netif_is_multiqueue(dev)) 1760 return -ENOENT; 1761 1762 index = get_netdev_queue_index(queue); 1763 1764 ret = sysfs_rtnl_lock(kobj, attr, queue->dev); 1765 if (ret) 1766 return ret; 1767 1768 /* If queue belongs to subordinate dev use its map */ 1769 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; 1770 1771 tc = netdev_txq_to_tc(dev, index); 1772 if (tc < 0) { 1773 rtnl_unlock(); 1774 return -EINVAL; 1775 } 1776 1777 /* Increase the net device refcnt to make sure it won't be freed while 1778 * xps_queue_show is running. 1779 */ 1780 dev_hold(dev); 1781 rtnl_unlock(); 1782 1783 len = xps_queue_show(dev, index, tc, buf, XPS_CPUS); 1784 1785 dev_put(dev); 1786 return len; 1787 } 1788 1789 static ssize_t xps_cpus_store(struct kobject *kobj, struct attribute *attr, 1790 struct netdev_queue *queue, const char *buf, 1791 size_t len) 1792 { 1793 struct net_device *dev = queue->dev; 1794 unsigned int index; 1795 cpumask_var_t mask; 1796 int err; 1797 1798 if (!netif_is_multiqueue(dev)) 1799 return -ENOENT; 1800 1801 if (!capable(CAP_NET_ADMIN)) 1802 return -EPERM; 1803 1804 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 1805 return -ENOMEM; 1806 1807 index = get_netdev_queue_index(queue); 1808 1809 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 1810 if (err) { 1811 free_cpumask_var(mask); 1812 return err; 1813 } 1814 1815 err = sysfs_rtnl_lock(kobj, attr, dev); 1816 if (err) { 1817 free_cpumask_var(mask); 1818 return err; 1819 } 1820 1821 err = netif_set_xps_queue(dev, mask, index); 1822 rtnl_unlock(); 1823 1824 free_cpumask_var(mask); 1825 1826 return err ? : len; 1827 } 1828 1829 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init 1830 = __ATTR_RW(xps_cpus); 1831 1832 static ssize_t xps_rxqs_show(struct kobject *kobj, struct attribute *attr, 1833 struct netdev_queue *queue, char *buf) 1834 { 1835 struct net_device *dev = queue->dev; 1836 unsigned int index; 1837 int tc, ret; 1838 1839 index = get_netdev_queue_index(queue); 1840 1841 ret = sysfs_rtnl_lock(kobj, attr, dev); 1842 if (ret) 1843 return ret; 1844 1845 tc = netdev_txq_to_tc(dev, index); 1846 1847 /* Increase the net device refcnt to make sure it won't be freed while 1848 * xps_queue_show is running. 1849 */ 1850 dev_hold(dev); 1851 rtnl_unlock(); 1852 1853 ret = tc >= 0 ? xps_queue_show(dev, index, tc, buf, XPS_RXQS) : -EINVAL; 1854 dev_put(dev); 1855 return ret; 1856 } 1857 1858 static ssize_t xps_rxqs_store(struct kobject *kobj, struct attribute *attr, 1859 struct netdev_queue *queue, const char *buf, 1860 size_t len) 1861 { 1862 struct net_device *dev = queue->dev; 1863 struct net *net = dev_net(dev); 1864 unsigned long *mask; 1865 unsigned int index; 1866 int err; 1867 1868 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1869 return -EPERM; 1870 1871 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL); 1872 if (!mask) 1873 return -ENOMEM; 1874 1875 index = get_netdev_queue_index(queue); 1876 1877 err = bitmap_parse(buf, len, mask, dev->num_rx_queues); 1878 if (err) { 1879 bitmap_free(mask); 1880 return err; 1881 } 1882 1883 err = sysfs_rtnl_lock(kobj, attr, dev); 1884 if (err) { 1885 bitmap_free(mask); 1886 return err; 1887 } 1888 1889 cpus_read_lock(); 1890 err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS); 1891 cpus_read_unlock(); 1892 1893 rtnl_unlock(); 1894 1895 bitmap_free(mask); 1896 return err ? : len; 1897 } 1898 1899 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init 1900 = __ATTR_RW(xps_rxqs); 1901 #endif /* CONFIG_XPS */ 1902 1903 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = { 1904 &queue_trans_timeout.attr, 1905 &queue_traffic_class.attr, 1906 #ifdef CONFIG_XPS 1907 &xps_cpus_attribute.attr, 1908 &xps_rxqs_attribute.attr, 1909 &queue_tx_maxrate.attr, 1910 #endif 1911 NULL 1912 }; 1913 ATTRIBUTE_GROUPS(netdev_queue_default); 1914 1915 static void netdev_queue_release(struct kobject *kobj) 1916 { 1917 struct netdev_queue *queue = to_netdev_queue(kobj); 1918 1919 memset(kobj, 0, sizeof(*kobj)); 1920 netdev_put(queue->dev, &queue->dev_tracker); 1921 } 1922 1923 static const void *netdev_queue_namespace(const struct kobject *kobj) 1924 { 1925 struct netdev_queue *queue = to_netdev_queue(kobj); 1926 struct device *dev = &queue->dev->dev; 1927 const void *ns = NULL; 1928 1929 if (dev->class && dev->class->namespace) 1930 ns = dev->class->namespace(dev); 1931 1932 return ns; 1933 } 1934 1935 static void netdev_queue_get_ownership(const struct kobject *kobj, 1936 kuid_t *uid, kgid_t *gid) 1937 { 1938 const struct net *net = netdev_queue_namespace(kobj); 1939 1940 net_ns_get_ownership(net, uid, gid); 1941 } 1942 1943 static const struct kobj_type netdev_queue_ktype = { 1944 .sysfs_ops = &netdev_queue_sysfs_ops, 1945 .release = netdev_queue_release, 1946 .namespace = netdev_queue_namespace, 1947 .get_ownership = netdev_queue_get_ownership, 1948 }; 1949 1950 static bool netdev_uses_bql(const struct net_device *dev) 1951 { 1952 if (dev->lltx || (dev->priv_flags & IFF_NO_QUEUE)) 1953 return false; 1954 1955 return IS_ENABLED(CONFIG_BQL); 1956 } 1957 1958 static int netdev_queue_add_kobject(struct net_device *dev, int index) 1959 { 1960 struct netdev_queue *queue = dev->_tx + index; 1961 struct kobject *kobj = &queue->kobj; 1962 int error = 0; 1963 1964 /* Tx queues are cleared in netdev_queue_release to allow later 1965 * re-registration. This is triggered when their kobj refcount is 1966 * dropped. 1967 * 1968 * If a queue is removed while both a read (or write) operation and a 1969 * the re-addition of the same queue are pending (waiting on rntl_lock) 1970 * it might happen that the re-addition will execute before the read, 1971 * making the initial removal to never happen (queue's kobj refcount 1972 * won't drop enough because of the pending read). In such rare case, 1973 * return to allow the removal operation to complete. 1974 */ 1975 if (unlikely(kobj->state_initialized)) { 1976 netdev_warn_once(dev, "Cannot re-add tx queues before their removal completed"); 1977 return -EAGAIN; 1978 } 1979 1980 /* Kobject_put later will trigger netdev_queue_release call 1981 * which decreases dev refcount: Take that reference here 1982 */ 1983 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL); 1984 1985 kobj->kset = dev->queues_kset; 1986 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, 1987 "tx-%u", index); 1988 if (error) 1989 goto err; 1990 1991 queue->groups = netdev_queue_default_groups; 1992 error = sysfs_create_groups(kobj, queue->groups); 1993 if (error) 1994 goto err; 1995 1996 if (netdev_uses_bql(dev)) { 1997 error = sysfs_create_group(kobj, &dql_group); 1998 if (error) 1999 goto err_default_groups; 2000 } 2001 2002 kobject_uevent(kobj, KOBJ_ADD); 2003 return 0; 2004 2005 err_default_groups: 2006 sysfs_remove_groups(kobj, queue->groups); 2007 err: 2008 kobject_put(kobj); 2009 return error; 2010 } 2011 2012 static int tx_queue_change_owner(struct net_device *ndev, int index, 2013 kuid_t kuid, kgid_t kgid) 2014 { 2015 struct netdev_queue *queue = ndev->_tx + index; 2016 struct kobject *kobj = &queue->kobj; 2017 int error; 2018 2019 error = sysfs_change_owner(kobj, kuid, kgid); 2020 if (error) 2021 return error; 2022 2023 if (netdev_uses_bql(ndev)) 2024 error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid); 2025 2026 return error; 2027 } 2028 #endif /* CONFIG_SYSFS */ 2029 2030 int 2031 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) 2032 { 2033 #ifdef CONFIG_SYSFS 2034 int i; 2035 int error = 0; 2036 2037 /* Tx queue kobjects are allowed to be updated when a device is being 2038 * unregistered, but solely to remove queues from qdiscs. Any path 2039 * adding queues should be fixed. 2040 */ 2041 WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num, 2042 "New queues can't be registered after device unregistration."); 2043 2044 for (i = old_num; i < new_num; i++) { 2045 error = netdev_queue_add_kobject(dev, i); 2046 if (error) { 2047 new_num = old_num; 2048 break; 2049 } 2050 } 2051 2052 while (--i >= new_num) { 2053 struct netdev_queue *queue = dev->_tx + i; 2054 2055 if (!refcount_read(&dev_net(dev)->ns.count)) 2056 queue->kobj.uevent_suppress = 1; 2057 2058 if (netdev_uses_bql(dev)) 2059 sysfs_remove_group(&queue->kobj, &dql_group); 2060 2061 sysfs_remove_groups(&queue->kobj, queue->groups); 2062 kobject_put(&queue->kobj); 2063 } 2064 2065 return error; 2066 #else 2067 return 0; 2068 #endif /* CONFIG_SYSFS */ 2069 } 2070 2071 static int net_tx_queue_change_owner(struct net_device *dev, int num, 2072 kuid_t kuid, kgid_t kgid) 2073 { 2074 #ifdef CONFIG_SYSFS 2075 int error = 0; 2076 int i; 2077 2078 for (i = 0; i < num; i++) { 2079 error = tx_queue_change_owner(dev, i, kuid, kgid); 2080 if (error) 2081 break; 2082 } 2083 2084 return error; 2085 #else 2086 return 0; 2087 #endif /* CONFIG_SYSFS */ 2088 } 2089 2090 static int register_queue_kobjects(struct net_device *dev) 2091 { 2092 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; 2093 2094 #ifdef CONFIG_SYSFS 2095 dev->queues_kset = kset_create_and_add("queues", 2096 NULL, &dev->dev.kobj); 2097 if (!dev->queues_kset) 2098 return -ENOMEM; 2099 real_rx = dev->real_num_rx_queues; 2100 #endif 2101 real_tx = dev->real_num_tx_queues; 2102 2103 error = net_rx_queue_update_kobjects(dev, 0, real_rx); 2104 if (error) 2105 goto error; 2106 rxq = real_rx; 2107 2108 error = netdev_queue_update_kobjects(dev, 0, real_tx); 2109 if (error) 2110 goto error; 2111 txq = real_tx; 2112 2113 return 0; 2114 2115 error: 2116 netdev_queue_update_kobjects(dev, txq, 0); 2117 net_rx_queue_update_kobjects(dev, rxq, 0); 2118 #ifdef CONFIG_SYSFS 2119 kset_unregister(dev->queues_kset); 2120 #endif 2121 return error; 2122 } 2123 2124 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid) 2125 { 2126 int error = 0, real_rx = 0, real_tx = 0; 2127 2128 #ifdef CONFIG_SYSFS 2129 if (ndev->queues_kset) { 2130 error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid); 2131 if (error) 2132 return error; 2133 } 2134 real_rx = ndev->real_num_rx_queues; 2135 #endif 2136 real_tx = ndev->real_num_tx_queues; 2137 2138 error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid); 2139 if (error) 2140 return error; 2141 2142 error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid); 2143 if (error) 2144 return error; 2145 2146 return 0; 2147 } 2148 2149 static void remove_queue_kobjects(struct net_device *dev) 2150 { 2151 int real_rx = 0, real_tx = 0; 2152 2153 #ifdef CONFIG_SYSFS 2154 real_rx = dev->real_num_rx_queues; 2155 #endif 2156 real_tx = dev->real_num_tx_queues; 2157 2158 net_rx_queue_update_kobjects(dev, real_rx, 0); 2159 netdev_queue_update_kobjects(dev, real_tx, 0); 2160 2161 netdev_lock_ops(dev); 2162 dev->real_num_rx_queues = 0; 2163 dev->real_num_tx_queues = 0; 2164 netdev_unlock_ops(dev); 2165 #ifdef CONFIG_SYSFS 2166 kset_unregister(dev->queues_kset); 2167 #endif 2168 } 2169 2170 static bool net_current_may_mount(void) 2171 { 2172 struct net *net = current->nsproxy->net_ns; 2173 2174 return ns_capable(net->user_ns, CAP_SYS_ADMIN); 2175 } 2176 2177 static void *net_grab_current_ns(void) 2178 { 2179 struct net *ns = current->nsproxy->net_ns; 2180 #ifdef CONFIG_NET_NS 2181 if (ns) 2182 refcount_inc(&ns->passive); 2183 #endif 2184 return ns; 2185 } 2186 2187 static const void *net_initial_ns(void) 2188 { 2189 return &init_net; 2190 } 2191 2192 static const void *net_netlink_ns(struct sock *sk) 2193 { 2194 return sock_net(sk); 2195 } 2196 2197 const struct kobj_ns_type_operations net_ns_type_operations = { 2198 .type = KOBJ_NS_TYPE_NET, 2199 .current_may_mount = net_current_may_mount, 2200 .grab_current_ns = net_grab_current_ns, 2201 .netlink_ns = net_netlink_ns, 2202 .initial_ns = net_initial_ns, 2203 .drop_ns = net_drop_ns, 2204 }; 2205 EXPORT_SYMBOL_GPL(net_ns_type_operations); 2206 2207 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env) 2208 { 2209 const struct net_device *dev = to_net_dev(d); 2210 int retval; 2211 2212 /* pass interface to uevent. */ 2213 retval = add_uevent_var(env, "INTERFACE=%s", dev->name); 2214 if (retval) 2215 goto exit; 2216 2217 /* pass ifindex to uevent. 2218 * ifindex is useful as it won't change (interface name may change) 2219 * and is what RtNetlink uses natively. 2220 */ 2221 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); 2222 2223 exit: 2224 return retval; 2225 } 2226 2227 /* 2228 * netdev_release -- destroy and free a dead device. 2229 * Called when last reference to device kobject is gone. 2230 */ 2231 static void netdev_release(struct device *d) 2232 { 2233 struct net_device *dev = to_net_dev(d); 2234 2235 BUG_ON(dev->reg_state != NETREG_RELEASED); 2236 2237 /* no need to wait for rcu grace period: 2238 * device is dead and about to be freed. 2239 */ 2240 kfree(rcu_access_pointer(dev->ifalias)); 2241 kvfree(dev); 2242 } 2243 2244 static const void *net_namespace(const struct device *d) 2245 { 2246 const struct net_device *dev = to_net_dev(d); 2247 2248 return dev_net(dev); 2249 } 2250 2251 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid) 2252 { 2253 const struct net_device *dev = to_net_dev(d); 2254 const struct net *net = dev_net(dev); 2255 2256 net_ns_get_ownership(net, uid, gid); 2257 } 2258 2259 static const struct class net_class = { 2260 .name = "net", 2261 .dev_release = netdev_release, 2262 .dev_groups = net_class_groups, 2263 .dev_uevent = netdev_uevent, 2264 .ns_type = &net_ns_type_operations, 2265 .namespace = net_namespace, 2266 .get_ownership = net_get_ownership, 2267 }; 2268 2269 #ifdef CONFIG_OF 2270 static int of_dev_node_match(struct device *dev, const void *data) 2271 { 2272 for (; dev; dev = dev->parent) { 2273 if (dev->of_node == data) 2274 return 1; 2275 } 2276 2277 return 0; 2278 } 2279 2280 /* 2281 * of_find_net_device_by_node - lookup the net device for the device node 2282 * @np: OF device node 2283 * 2284 * Looks up the net_device structure corresponding with the device node. 2285 * If successful, returns a pointer to the net_device with the embedded 2286 * struct device refcount incremented by one, or NULL on failure. The 2287 * refcount must be dropped when done with the net_device. 2288 */ 2289 struct net_device *of_find_net_device_by_node(struct device_node *np) 2290 { 2291 struct device *dev; 2292 2293 dev = class_find_device(&net_class, NULL, np, of_dev_node_match); 2294 if (!dev) 2295 return NULL; 2296 2297 return to_net_dev(dev); 2298 } 2299 EXPORT_SYMBOL(of_find_net_device_by_node); 2300 #endif 2301 2302 /* Delete sysfs entries but hold kobject reference until after all 2303 * netdev references are gone. 2304 */ 2305 void netdev_unregister_kobject(struct net_device *ndev) 2306 { 2307 struct device *dev = &ndev->dev; 2308 2309 if (!refcount_read(&dev_net(ndev)->ns.count)) 2310 dev_set_uevent_suppress(dev, 1); 2311 2312 kobject_get(&dev->kobj); 2313 2314 remove_queue_kobjects(ndev); 2315 2316 pm_runtime_set_memalloc_noio(dev, false); 2317 2318 device_del(dev); 2319 } 2320 2321 /* Create sysfs entries for network device. */ 2322 int netdev_register_kobject(struct net_device *ndev) 2323 { 2324 struct device *dev = &ndev->dev; 2325 const struct attribute_group **groups = ndev->sysfs_groups; 2326 int error = 0; 2327 2328 device_initialize(dev); 2329 dev->class = &net_class; 2330 dev->platform_data = ndev; 2331 dev->groups = groups; 2332 2333 dev_set_name(dev, "%s", ndev->name); 2334 2335 #ifdef CONFIG_SYSFS 2336 /* Allow for a device specific group */ 2337 if (*groups) 2338 groups++; 2339 2340 *groups++ = &netstat_group; 2341 *groups++ = &netdev_phys_group; 2342 2343 if (wireless_group_needed(ndev)) 2344 *groups++ = &wireless_group; 2345 #endif /* CONFIG_SYSFS */ 2346 2347 error = device_add(dev); 2348 if (error) 2349 return error; 2350 2351 error = register_queue_kobjects(ndev); 2352 if (error) { 2353 device_del(dev); 2354 return error; 2355 } 2356 2357 pm_runtime_set_memalloc_noio(dev, true); 2358 2359 return error; 2360 } 2361 2362 /* Change owner for sysfs entries when moving network devices across network 2363 * namespaces owned by different user namespaces. 2364 */ 2365 int netdev_change_owner(struct net_device *ndev, const struct net *net_old, 2366 const struct net *net_new) 2367 { 2368 kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID; 2369 kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID; 2370 struct device *dev = &ndev->dev; 2371 int error; 2372 2373 net_ns_get_ownership(net_old, &old_uid, &old_gid); 2374 net_ns_get_ownership(net_new, &new_uid, &new_gid); 2375 2376 /* The network namespace was changed but the owning user namespace is 2377 * identical so there's no need to change the owner of sysfs entries. 2378 */ 2379 if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid)) 2380 return 0; 2381 2382 error = device_change_owner(dev, new_uid, new_gid); 2383 if (error) 2384 return error; 2385 2386 error = queue_change_owner(ndev, new_uid, new_gid); 2387 if (error) 2388 return error; 2389 2390 return 0; 2391 } 2392 2393 int netdev_class_create_file_ns(const struct class_attribute *class_attr, 2394 const void *ns) 2395 { 2396 return class_create_file_ns(&net_class, class_attr, ns); 2397 } 2398 EXPORT_SYMBOL(netdev_class_create_file_ns); 2399 2400 void netdev_class_remove_file_ns(const struct class_attribute *class_attr, 2401 const void *ns) 2402 { 2403 class_remove_file_ns(&net_class, class_attr, ns); 2404 } 2405 EXPORT_SYMBOL(netdev_class_remove_file_ns); 2406 2407 int __init netdev_kobject_init(void) 2408 { 2409 kobj_ns_type_register(&net_ns_type_operations); 2410 return class_register(&net_class); 2411 } 2412