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