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