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