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