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