linux/net/core/net-sysfs.c
<<
>>
Prefs
   1/*
   2 * net-sysfs.c - network device class and attributes
   3 *
   4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
   5 *
   6 *      This program is free software; you can redistribute it and/or
   7 *      modify it under the terms of the GNU General Public License
   8 *      as published by the Free Software Foundation; either version
   9 *      2 of the License, or (at your option) any later version.
  10 */
  11
  12#include <linux/capability.h>
  13#include <linux/kernel.h>
  14#include <linux/netdevice.h>
  15#include <net/switchdev.h>
  16#include <linux/if_arp.h>
  17#include <linux/slab.h>
  18#include <linux/nsproxy.h>
  19#include <net/sock.h>
  20#include <net/net_namespace.h>
  21#include <linux/rtnetlink.h>
  22#include <linux/vmalloc.h>
  23#include <linux/export.h>
  24#include <linux/jiffies.h>
  25#include <linux/pm_runtime.h>
  26
  27#include "net-sysfs.h"
  28
  29#ifdef CONFIG_SYSFS
  30static const char fmt_hex[] = "%#x\n";
  31static const char fmt_long_hex[] = "%#lx\n";
  32static const char fmt_dec[] = "%d\n";
  33static const char fmt_ulong[] = "%lu\n";
  34static const char fmt_u64[] = "%llu\n";
  35
  36static inline int dev_isalive(const struct net_device *dev)
  37{
  38        return dev->reg_state <= NETREG_REGISTERED;
  39}
  40
  41/* use same locking rules as GIF* ioctl's */
  42static ssize_t netdev_show(const struct device *dev,
  43                           struct device_attribute *attr, char *buf,
  44                           ssize_t (*format)(const struct net_device *, char *))
  45{
  46        struct net_device *net = to_net_dev(dev);
  47        ssize_t ret = -EINVAL;
  48
  49        read_lock(&dev_base_lock);
  50        if (dev_isalive(net))
  51                ret = (*format)(net, buf);
  52        read_unlock(&dev_base_lock);
  53
  54        return ret;
  55}
  56
  57/* generate a show function for simple field */
  58#define NETDEVICE_SHOW(field, format_string)                            \
  59static ssize_t format_##field(const struct net_device *net, char *buf)  \
  60{                                                                       \
  61        return sprintf(buf, format_string, net->field);                 \
  62}                                                                       \
  63static ssize_t field##_show(struct device *dev,                         \
  64                            struct device_attribute *attr, char *buf)   \
  65{                                                                       \
  66        return netdev_show(dev, attr, buf, format_##field);             \
  67}                                                                       \
  68
  69#define NETDEVICE_SHOW_RO(field, format_string)                         \
  70NETDEVICE_SHOW(field, format_string);                                   \
  71static DEVICE_ATTR_RO(field)
  72
  73#define NETDEVICE_SHOW_RW(field, format_string)                         \
  74NETDEVICE_SHOW(field, format_string);                                   \
  75static DEVICE_ATTR_RW(field)
  76
  77/* use same locking and permission rules as SIF* ioctl's */
  78static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  79                            const char *buf, size_t len,
  80                            int (*set)(struct net_device *, unsigned long))
  81{
  82        struct net_device *netdev = to_net_dev(dev);
  83        struct net *net = dev_net(netdev);
  84        unsigned long new;
  85        int ret = -EINVAL;
  86
  87        if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  88                return -EPERM;
  89
  90        ret = kstrtoul(buf, 0, &new);
  91        if (ret)
  92                goto err;
  93
  94        if (!rtnl_trylock())
  95                return restart_syscall();
  96
  97        if (dev_isalive(netdev)) {
  98                if ((ret = (*set)(netdev, new)) == 0)
  99                        ret = len;
 100        }
 101        rtnl_unlock();
 102 err:
 103        return ret;
 104}
 105
 106NETDEVICE_SHOW_RO(dev_id, fmt_hex);
 107NETDEVICE_SHOW_RO(dev_port, fmt_dec);
 108NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
 109NETDEVICE_SHOW_RO(addr_len, fmt_dec);
 110NETDEVICE_SHOW_RO(ifindex, fmt_dec);
 111NETDEVICE_SHOW_RO(type, fmt_dec);
 112NETDEVICE_SHOW_RO(link_mode, fmt_dec);
 113
 114static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
 115                           char *buf)
 116{
 117        struct net_device *ndev = to_net_dev(dev);
 118
 119        return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
 120}
 121static DEVICE_ATTR_RO(iflink);
 122
 123/* use same locking rules as GIFHWADDR ioctl's */
 124static ssize_t address_show(struct device *dev, struct device_attribute *attr,
 125                            char *buf)
 126{
 127        struct net_device *net = to_net_dev(dev);
 128        ssize_t ret = -EINVAL;
 129
 130        read_lock(&dev_base_lock);
 131        if (dev_isalive(net))
 132                ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
 133        read_unlock(&dev_base_lock);
 134        return ret;
 135}
 136static DEVICE_ATTR_RO(address);
 137
 138static ssize_t broadcast_show(struct device *dev,
 139                              struct device_attribute *attr, char *buf)
 140{
 141        struct net_device *net = to_net_dev(dev);
 142        if (dev_isalive(net))
 143                return sysfs_format_mac(buf, net->broadcast, net->addr_len);
 144        return -EINVAL;
 145}
 146static DEVICE_ATTR_RO(broadcast);
 147
 148static int change_carrier(struct net_device *net, unsigned long new_carrier)
 149{
 150        if (!netif_running(net))
 151                return -EINVAL;
 152        return dev_change_carrier(net, (bool) new_carrier);
 153}
 154
 155static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
 156                             const char *buf, size_t len)
 157{
 158        return netdev_store(dev, attr, buf, len, change_carrier);
 159}
 160
 161static ssize_t carrier_show(struct device *dev,
 162                            struct device_attribute *attr, char *buf)
 163{
 164        struct net_device *netdev = to_net_dev(dev);
 165        if (netif_running(netdev)) {
 166                return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
 167        }
 168        return -EINVAL;
 169}
 170static DEVICE_ATTR_RW(carrier);
 171
 172static ssize_t speed_show(struct device *dev,
 173                          struct device_attribute *attr, char *buf)
 174{
 175        struct net_device *netdev = to_net_dev(dev);
 176        int ret = -EINVAL;
 177
 178        if (!rtnl_trylock())
 179                return restart_syscall();
 180
 181        if (netif_running(netdev)) {
 182                struct ethtool_link_ksettings cmd;
 183
 184                if (!__ethtool_get_link_ksettings(netdev, &cmd))
 185                        ret = sprintf(buf, fmt_dec, cmd.base.speed);
 186        }
 187        rtnl_unlock();
 188        return ret;
 189}
 190static DEVICE_ATTR_RO(speed);
 191
 192static ssize_t duplex_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                        const char *duplex;
 206
 207                        switch (cmd.base.duplex) {
 208                        case DUPLEX_HALF:
 209                                duplex = "half";
 210                                break;
 211                        case DUPLEX_FULL:
 212                                duplex = "full";
 213                                break;
 214                        default:
 215                                duplex = "unknown";
 216                                break;
 217                        }
 218                        ret = sprintf(buf, "%s\n", duplex);
 219                }
 220        }
 221        rtnl_unlock();
 222        return ret;
 223}
 224static DEVICE_ATTR_RO(duplex);
 225
 226static ssize_t dormant_show(struct device *dev,
 227                            struct device_attribute *attr, char *buf)
 228{
 229        struct net_device *netdev = to_net_dev(dev);
 230
 231        if (netif_running(netdev))
 232                return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
 233
 234        return -EINVAL;
 235}
 236static DEVICE_ATTR_RO(dormant);
 237
 238static const char *const operstates[] = {
 239        "unknown",
 240        "notpresent", /* currently unused */
 241        "down",
 242        "lowerlayerdown",
 243        "testing", /* currently unused */
 244        "dormant",
 245        "up"
 246};
 247
 248static ssize_t operstate_show(struct device *dev,
 249                              struct device_attribute *attr, char *buf)
 250{
 251        const struct net_device *netdev = to_net_dev(dev);
 252        unsigned char operstate;
 253
 254        read_lock(&dev_base_lock);
 255        operstate = netdev->operstate;
 256        if (!netif_running(netdev))
 257                operstate = IF_OPER_DOWN;
 258        read_unlock(&dev_base_lock);
 259
 260        if (operstate >= ARRAY_SIZE(operstates))
 261                return -EINVAL; /* should not happen */
 262
 263        return sprintf(buf, "%s\n", operstates[operstate]);
 264}
 265static DEVICE_ATTR_RO(operstate);
 266
 267static ssize_t carrier_changes_show(struct device *dev,
 268                                    struct device_attribute *attr,
 269                                    char *buf)
 270{
 271        struct net_device *netdev = to_net_dev(dev);
 272        return sprintf(buf, fmt_dec,
 273                       atomic_read(&netdev->carrier_changes));
 274}
 275static DEVICE_ATTR_RO(carrier_changes);
 276
 277/* read-write attributes */
 278
 279static int change_mtu(struct net_device *net, unsigned long new_mtu)
 280{
 281        return dev_set_mtu(net, (int) new_mtu);
 282}
 283
 284static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
 285                         const char *buf, size_t len)
 286{
 287        return netdev_store(dev, attr, buf, len, change_mtu);
 288}
 289NETDEVICE_SHOW_RW(mtu, fmt_dec);
 290
 291static int change_flags(struct net_device *net, unsigned long new_flags)
 292{
 293        return dev_change_flags(net, (unsigned int) new_flags);
 294}
 295
 296static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
 297                           const char *buf, size_t len)
 298{
 299        return netdev_store(dev, attr, buf, len, change_flags);
 300}
 301NETDEVICE_SHOW_RW(flags, fmt_hex);
 302
 303static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
 304{
 305        int res, orig_len = dev->tx_queue_len;
 306
 307        if (new_len != orig_len) {
 308                dev->tx_queue_len = new_len;
 309                res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
 310                res = notifier_to_errno(res);
 311                if (res) {
 312                        netdev_err(dev,
 313                                   "refused to change device tx_queue_len\n");
 314                        dev->tx_queue_len = orig_len;
 315                        return -EFAULT;
 316                }
 317        }
 318
 319        return 0;
 320}
 321
 322static ssize_t tx_queue_len_store(struct device *dev,
 323                                  struct device_attribute *attr,
 324                                  const char *buf, size_t len)
 325{
 326        if (!capable(CAP_NET_ADMIN))
 327                return -EPERM;
 328
 329        return netdev_store(dev, attr, buf, len, change_tx_queue_len);
 330}
 331NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
 332
 333static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
 334{
 335        dev->gro_flush_timeout = val;
 336        return 0;
 337}
 338
 339static ssize_t gro_flush_timeout_store(struct device *dev,
 340                                  struct device_attribute *attr,
 341                                  const char *buf, size_t len)
 342{
 343        if (!capable(CAP_NET_ADMIN))
 344                return -EPERM;
 345
 346        return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
 347}
 348NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
 349
 350static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
 351                             const char *buf, size_t len)
 352{
 353        struct net_device *netdev = to_net_dev(dev);
 354        struct net *net = dev_net(netdev);
 355        size_t count = len;
 356        ssize_t ret;
 357
 358        if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
 359                return -EPERM;
 360
 361        /* ignore trailing newline */
 362        if (len >  0 && buf[len - 1] == '\n')
 363                --count;
 364
 365        if (!rtnl_trylock())
 366                return restart_syscall();
 367        ret = dev_set_alias(netdev, buf, count);
 368        rtnl_unlock();
 369
 370        return ret < 0 ? ret : len;
 371}
 372
 373static ssize_t ifalias_show(struct device *dev,
 374                            struct device_attribute *attr, char *buf)
 375{
 376        const struct net_device *netdev = to_net_dev(dev);
 377        ssize_t ret = 0;
 378
 379        if (!rtnl_trylock())
 380                return restart_syscall();
 381        if (netdev->ifalias)
 382                ret = sprintf(buf, "%s\n", netdev->ifalias);
 383        rtnl_unlock();
 384        return ret;
 385}
 386static DEVICE_ATTR_RW(ifalias);
 387
 388static int change_group(struct net_device *net, unsigned long new_group)
 389{
 390        dev_set_group(net, (int) new_group);
 391        return 0;
 392}
 393
 394static ssize_t group_store(struct device *dev, struct device_attribute *attr,
 395                           const char *buf, size_t len)
 396{
 397        return netdev_store(dev, attr, buf, len, change_group);
 398}
 399NETDEVICE_SHOW(group, fmt_dec);
 400static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
 401
 402static int change_proto_down(struct net_device *dev, unsigned long proto_down)
 403{
 404        return dev_change_proto_down(dev, (bool) proto_down);
 405}
 406
 407static ssize_t proto_down_store(struct device *dev,
 408                                struct device_attribute *attr,
 409                                const char *buf, size_t len)
 410{
 411        return netdev_store(dev, attr, buf, len, change_proto_down);
 412}
 413NETDEVICE_SHOW_RW(proto_down, fmt_dec);
 414
 415static ssize_t phys_port_id_show(struct device *dev,
 416                                 struct device_attribute *attr, char *buf)
 417{
 418        struct net_device *netdev = to_net_dev(dev);
 419        ssize_t ret = -EINVAL;
 420
 421        if (!rtnl_trylock())
 422                return restart_syscall();
 423
 424        if (dev_isalive(netdev)) {
 425                struct netdev_phys_item_id ppid;
 426
 427                ret = dev_get_phys_port_id(netdev, &ppid);
 428                if (!ret)
 429                        ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
 430        }
 431        rtnl_unlock();
 432
 433        return ret;
 434}
 435static DEVICE_ATTR_RO(phys_port_id);
 436
 437static ssize_t phys_port_name_show(struct device *dev,
 438                                   struct device_attribute *attr, char *buf)
 439{
 440        struct net_device *netdev = to_net_dev(dev);
 441        ssize_t ret = -EINVAL;
 442
 443        if (!rtnl_trylock())
 444                return restart_syscall();
 445
 446        if (dev_isalive(netdev)) {
 447                char name[IFNAMSIZ];
 448
 449                ret = dev_get_phys_port_name(netdev, name, sizeof(name));
 450                if (!ret)
 451                        ret = sprintf(buf, "%s\n", name);
 452        }
 453        rtnl_unlock();
 454
 455        return ret;
 456}
 457static DEVICE_ATTR_RO(phys_port_name);
 458
 459static ssize_t phys_switch_id_show(struct device *dev,
 460                                   struct device_attribute *attr, char *buf)
 461{
 462        struct net_device *netdev = to_net_dev(dev);
 463        ssize_t ret = -EINVAL;
 464
 465        if (!rtnl_trylock())
 466                return restart_syscall();
 467
 468        if (dev_isalive(netdev)) {
 469                struct switchdev_attr attr = {
 470                        .orig_dev = netdev,
 471                        .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
 472                        .flags = SWITCHDEV_F_NO_RECURSE,
 473                };
 474
 475                ret = switchdev_port_attr_get(netdev, &attr);
 476                if (!ret)
 477                        ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
 478                                      attr.u.ppid.id);
 479        }
 480        rtnl_unlock();
 481
 482        return ret;
 483}
 484static DEVICE_ATTR_RO(phys_switch_id);
 485
 486static struct attribute *net_class_attrs[] = {
 487        &dev_attr_netdev_group.attr,
 488        &dev_attr_type.attr,
 489        &dev_attr_dev_id.attr,
 490        &dev_attr_dev_port.attr,
 491        &dev_attr_iflink.attr,
 492        &dev_attr_ifindex.attr,
 493        &dev_attr_addr_assign_type.attr,
 494        &dev_attr_addr_len.attr,
 495        &dev_attr_link_mode.attr,
 496        &dev_attr_address.attr,
 497        &dev_attr_broadcast.attr,
 498        &dev_attr_speed.attr,
 499        &dev_attr_duplex.attr,
 500        &dev_attr_dormant.attr,
 501        &dev_attr_operstate.attr,
 502        &dev_attr_carrier_changes.attr,
 503        &dev_attr_ifalias.attr,
 504        &dev_attr_carrier.attr,
 505        &dev_attr_mtu.attr,
 506        &dev_attr_flags.attr,
 507        &dev_attr_tx_queue_len.attr,
 508        &dev_attr_gro_flush_timeout.attr,
 509        &dev_attr_phys_port_id.attr,
 510        &dev_attr_phys_port_name.attr,
 511        &dev_attr_phys_switch_id.attr,
 512        &dev_attr_proto_down.attr,
 513        NULL,
 514};
 515ATTRIBUTE_GROUPS(net_class);
 516
 517/* Show a given an attribute in the statistics group */
 518static ssize_t netstat_show(const struct device *d,
 519                            struct device_attribute *attr, char *buf,
 520                            unsigned long offset)
 521{
 522        struct net_device *dev = to_net_dev(d);
 523        ssize_t ret = -EINVAL;
 524
 525        WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
 526                        offset % sizeof(u64) != 0);
 527
 528        read_lock(&dev_base_lock);
 529        if (dev_isalive(dev)) {
 530                struct rtnl_link_stats64 temp;
 531                const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
 532
 533                ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
 534        }
 535        read_unlock(&dev_base_lock);
 536        return ret;
 537}
 538
 539/* generate a read-only statistics attribute */
 540#define NETSTAT_ENTRY(name)                                             \
 541static ssize_t name##_show(struct device *d,                            \
 542                           struct device_attribute *attr, char *buf)    \
 543{                                                                       \
 544        return netstat_show(d, attr, buf,                               \
 545                            offsetof(struct rtnl_link_stats64, name));  \
 546}                                                                       \
 547static DEVICE_ATTR_RO(name)
 548
 549NETSTAT_ENTRY(rx_packets);
 550NETSTAT_ENTRY(tx_packets);
 551NETSTAT_ENTRY(rx_bytes);
 552NETSTAT_ENTRY(tx_bytes);
 553NETSTAT_ENTRY(rx_errors);
 554NETSTAT_ENTRY(tx_errors);
 555NETSTAT_ENTRY(rx_dropped);
 556NETSTAT_ENTRY(tx_dropped);
 557NETSTAT_ENTRY(multicast);
 558NETSTAT_ENTRY(collisions);
 559NETSTAT_ENTRY(rx_length_errors);
 560NETSTAT_ENTRY(rx_over_errors);
 561NETSTAT_ENTRY(rx_crc_errors);
 562NETSTAT_ENTRY(rx_frame_errors);
 563NETSTAT_ENTRY(rx_fifo_errors);
 564NETSTAT_ENTRY(rx_missed_errors);
 565NETSTAT_ENTRY(tx_aborted_errors);
 566NETSTAT_ENTRY(tx_carrier_errors);
 567NETSTAT_ENTRY(tx_fifo_errors);
 568NETSTAT_ENTRY(tx_heartbeat_errors);
 569NETSTAT_ENTRY(tx_window_errors);
 570NETSTAT_ENTRY(rx_compressed);
 571NETSTAT_ENTRY(tx_compressed);
 572NETSTAT_ENTRY(rx_nohandler);
 573
 574static struct attribute *netstat_attrs[] = {
 575        &dev_attr_rx_packets.attr,
 576        &dev_attr_tx_packets.attr,
 577        &dev_attr_rx_bytes.attr,
 578        &dev_attr_tx_bytes.attr,
 579        &dev_attr_rx_errors.attr,
 580        &dev_attr_tx_errors.attr,
 581        &dev_attr_rx_dropped.attr,
 582        &dev_attr_tx_dropped.attr,
 583        &dev_attr_multicast.attr,
 584        &dev_attr_collisions.attr,
 585        &dev_attr_rx_length_errors.attr,
 586        &dev_attr_rx_over_errors.attr,
 587        &dev_attr_rx_crc_errors.attr,
 588        &dev_attr_rx_frame_errors.attr,
 589        &dev_attr_rx_fifo_errors.attr,
 590        &dev_attr_rx_missed_errors.attr,
 591        &dev_attr_tx_aborted_errors.attr,
 592        &dev_attr_tx_carrier_errors.attr,
 593        &dev_attr_tx_fifo_errors.attr,
 594        &dev_attr_tx_heartbeat_errors.attr,
 595        &dev_attr_tx_window_errors.attr,
 596        &dev_attr_rx_compressed.attr,
 597        &dev_attr_tx_compressed.attr,
 598        &dev_attr_rx_nohandler.attr,
 599        NULL
 600};
 601
 602
 603static struct attribute_group netstat_group = {
 604        .name  = "statistics",
 605        .attrs  = netstat_attrs,
 606};
 607
 608#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
 609static struct attribute *wireless_attrs[] = {
 610        NULL
 611};
 612
 613static struct attribute_group wireless_group = {
 614        .name = "wireless",
 615        .attrs = wireless_attrs,
 616};
 617#endif
 618
 619#else /* CONFIG_SYSFS */
 620#define net_class_groups        NULL
 621#endif /* CONFIG_SYSFS */
 622
 623#ifdef CONFIG_RPS
 624/*
 625 * RX queue sysfs structures and functions.
 626 */
 627struct rx_queue_attribute {
 628        struct attribute attr;
 629        ssize_t (*show)(struct netdev_rx_queue *queue,
 630            struct rx_queue_attribute *attr, char *buf);
 631        ssize_t (*store)(struct netdev_rx_queue *queue,
 632            struct rx_queue_attribute *attr, const char *buf, size_t len);
 633};
 634#define to_rx_queue_attr(_attr) container_of(_attr,             \
 635    struct rx_queue_attribute, attr)
 636
 637#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
 638
 639static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
 640                                  char *buf)
 641{
 642        struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
 643        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 644
 645        if (!attribute->show)
 646                return -EIO;
 647
 648        return attribute->show(queue, attribute, buf);
 649}
 650
 651static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
 652                                   const char *buf, size_t count)
 653{
 654        struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
 655        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 656
 657        if (!attribute->store)
 658                return -EIO;
 659
 660        return attribute->store(queue, attribute, buf, count);
 661}
 662
 663static const struct sysfs_ops rx_queue_sysfs_ops = {
 664        .show = rx_queue_attr_show,
 665        .store = rx_queue_attr_store,
 666};
 667
 668static ssize_t show_rps_map(struct netdev_rx_queue *queue,
 669                            struct rx_queue_attribute *attribute, char *buf)
 670{
 671        struct rps_map *map;
 672        cpumask_var_t mask;
 673        size_t len = 0;
 674        int i;
 675
 676        if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
 677                return -ENOMEM;
 678
 679        rcu_read_lock();
 680        map = rcu_dereference(queue->rps_map);
 681        if (map)
 682                for (i = 0; i < map->len; i++)
 683                        cpumask_set_cpu(map->cpus[i], mask);
 684
 685        len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
 686        if (PAGE_SIZE - len < 3) {
 687                rcu_read_unlock();
 688                free_cpumask_var(mask);
 689                return -EINVAL;
 690        }
 691        rcu_read_unlock();
 692
 693        free_cpumask_var(mask);
 694        len += sprintf(buf + len, "\n");
 695        return len;
 696}
 697
 698static ssize_t store_rps_map(struct netdev_rx_queue *queue,
 699                      struct rx_queue_attribute *attribute,
 700                      const char *buf, size_t len)
 701{
 702        struct rps_map *old_map, *map;
 703        cpumask_var_t mask;
 704        int err, cpu, i;
 705        static DEFINE_SPINLOCK(rps_map_lock);
 706
 707        if (!capable(CAP_NET_ADMIN))
 708                return -EPERM;
 709
 710        if (!alloc_cpumask_var(&mask, GFP_KERNEL))
 711                return -ENOMEM;
 712
 713        err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
 714        if (err) {
 715                free_cpumask_var(mask);
 716                return err;
 717        }
 718
 719        map = kzalloc(max_t(unsigned int,
 720            RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
 721            GFP_KERNEL);
 722        if (!map) {
 723                free_cpumask_var(mask);
 724                return -ENOMEM;
 725        }
 726
 727        i = 0;
 728        for_each_cpu_and(cpu, mask, cpu_online_mask)
 729                map->cpus[i++] = cpu;
 730
 731        if (i)
 732                map->len = i;
 733        else {
 734                kfree(map);
 735                map = NULL;
 736        }
 737
 738        spin_lock(&rps_map_lock);
 739        old_map = rcu_dereference_protected(queue->rps_map,
 740                                            lockdep_is_held(&rps_map_lock));
 741        rcu_assign_pointer(queue->rps_map, map);
 742        spin_unlock(&rps_map_lock);
 743
 744        if (map)
 745                static_key_slow_inc(&rps_needed);
 746        if (old_map) {
 747                kfree_rcu(old_map, rcu);
 748                static_key_slow_dec(&rps_needed);
 749        }
 750        free_cpumask_var(mask);
 751        return len;
 752}
 753
 754static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
 755                                           struct rx_queue_attribute *attr,
 756                                           char *buf)
 757{
 758        struct rps_dev_flow_table *flow_table;
 759        unsigned long val = 0;
 760
 761        rcu_read_lock();
 762        flow_table = rcu_dereference(queue->rps_flow_table);
 763        if (flow_table)
 764                val = (unsigned long)flow_table->mask + 1;
 765        rcu_read_unlock();
 766
 767        return sprintf(buf, "%lu\n", val);
 768}
 769
 770static void rps_dev_flow_table_release(struct rcu_head *rcu)
 771{
 772        struct rps_dev_flow_table *table = container_of(rcu,
 773            struct rps_dev_flow_table, rcu);
 774        vfree(table);
 775}
 776
 777static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
 778                                     struct rx_queue_attribute *attr,
 779                                     const char *buf, size_t len)
 780{
 781        unsigned long mask, count;
 782        struct rps_dev_flow_table *table, *old_table;
 783        static DEFINE_SPINLOCK(rps_dev_flow_lock);
 784        int rc;
 785
 786        if (!capable(CAP_NET_ADMIN))
 787                return -EPERM;
 788
 789        rc = kstrtoul(buf, 0, &count);
 790        if (rc < 0)
 791                return rc;
 792
 793        if (count) {
 794                mask = count - 1;
 795                /* mask = roundup_pow_of_two(count) - 1;
 796                 * without overflows...
 797                 */
 798                while ((mask | (mask >> 1)) != mask)
 799                        mask |= (mask >> 1);
 800                /* On 64 bit arches, must check mask fits in table->mask (u32),
 801                 * and on 32bit arches, must check
 802                 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
 803                 */
 804#if BITS_PER_LONG > 32
 805                if (mask > (unsigned long)(u32)mask)
 806                        return -EINVAL;
 807#else
 808                if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
 809                                / sizeof(struct rps_dev_flow)) {
 810                        /* Enforce a limit to prevent overflow */
 811                        return -EINVAL;
 812                }
 813#endif
 814                table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
 815                if (!table)
 816                        return -ENOMEM;
 817
 818                table->mask = mask;
 819                for (count = 0; count <= mask; count++)
 820                        table->flows[count].cpu = RPS_NO_CPU;
 821        } else
 822                table = NULL;
 823
 824        spin_lock(&rps_dev_flow_lock);
 825        old_table = rcu_dereference_protected(queue->rps_flow_table,
 826                                              lockdep_is_held(&rps_dev_flow_lock));
 827        rcu_assign_pointer(queue->rps_flow_table, table);
 828        spin_unlock(&rps_dev_flow_lock);
 829
 830        if (old_table)
 831                call_rcu(&old_table->rcu, rps_dev_flow_table_release);
 832
 833        return len;
 834}
 835
 836static struct rx_queue_attribute rps_cpus_attribute =
 837        __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
 838
 839
 840static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
 841        __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
 842            show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
 843
 844static struct attribute *rx_queue_default_attrs[] = {
 845        &rps_cpus_attribute.attr,
 846        &rps_dev_flow_table_cnt_attribute.attr,
 847        NULL
 848};
 849
 850static void rx_queue_release(struct kobject *kobj)
 851{
 852        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 853        struct rps_map *map;
 854        struct rps_dev_flow_table *flow_table;
 855
 856
 857        map = rcu_dereference_protected(queue->rps_map, 1);
 858        if (map) {
 859                RCU_INIT_POINTER(queue->rps_map, NULL);
 860                kfree_rcu(map, rcu);
 861        }
 862
 863        flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
 864        if (flow_table) {
 865                RCU_INIT_POINTER(queue->rps_flow_table, NULL);
 866                call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
 867        }
 868
 869        memset(kobj, 0, sizeof(*kobj));
 870        dev_put(queue->dev);
 871}
 872
 873static const void *rx_queue_namespace(struct kobject *kobj)
 874{
 875        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 876        struct device *dev = &queue->dev->dev;
 877        const void *ns = NULL;
 878
 879        if (dev->class && dev->class->ns_type)
 880                ns = dev->class->namespace(dev);
 881
 882        return ns;
 883}
 884
 885static struct kobj_type rx_queue_ktype = {
 886        .sysfs_ops = &rx_queue_sysfs_ops,
 887        .release = rx_queue_release,
 888        .default_attrs = rx_queue_default_attrs,
 889        .namespace = rx_queue_namespace
 890};
 891
 892static int rx_queue_add_kobject(struct net_device *net, int index)
 893{
 894        struct netdev_rx_queue *queue = net->_rx + index;
 895        struct kobject *kobj = &queue->kobj;
 896        int error = 0;
 897
 898        kobj->kset = net->queues_kset;
 899        error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
 900            "rx-%u", index);
 901        if (error) {
 902                kobject_put(kobj);
 903                return error;
 904        }
 905
 906        kobject_uevent(kobj, KOBJ_ADD);
 907        dev_hold(queue->dev);
 908
 909        return error;
 910}
 911#endif /* CONFIG_RPS */
 912
 913int
 914net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
 915{
 916#ifdef CONFIG_RPS
 917        int i;
 918        int error = 0;
 919
 920        for (i = old_num; i < new_num; i++) {
 921                error = rx_queue_add_kobject(net, i);
 922                if (error) {
 923                        new_num = old_num;
 924                        break;
 925                }
 926        }
 927
 928        while (--i >= new_num)
 929                kobject_put(&net->_rx[i].kobj);
 930
 931        return error;
 932#else
 933        return 0;
 934#endif
 935}
 936
 937#ifdef CONFIG_SYSFS
 938/*
 939 * netdev_queue sysfs structures and functions.
 940 */
 941struct netdev_queue_attribute {
 942        struct attribute attr;
 943        ssize_t (*show)(struct netdev_queue *queue,
 944            struct netdev_queue_attribute *attr, char *buf);
 945        ssize_t (*store)(struct netdev_queue *queue,
 946            struct netdev_queue_attribute *attr, const char *buf, size_t len);
 947};
 948#define to_netdev_queue_attr(_attr) container_of(_attr,         \
 949    struct netdev_queue_attribute, attr)
 950
 951#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
 952
 953static ssize_t netdev_queue_attr_show(struct kobject *kobj,
 954                                      struct attribute *attr, char *buf)
 955{
 956        struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
 957        struct netdev_queue *queue = to_netdev_queue(kobj);
 958
 959        if (!attribute->show)
 960                return -EIO;
 961
 962        return attribute->show(queue, attribute, buf);
 963}
 964
 965static ssize_t netdev_queue_attr_store(struct kobject *kobj,
 966                                       struct attribute *attr,
 967                                       const char *buf, size_t count)
 968{
 969        struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
 970        struct netdev_queue *queue = to_netdev_queue(kobj);
 971
 972        if (!attribute->store)
 973                return -EIO;
 974
 975        return attribute->store(queue, attribute, buf, count);
 976}
 977
 978static const struct sysfs_ops netdev_queue_sysfs_ops = {
 979        .show = netdev_queue_attr_show,
 980        .store = netdev_queue_attr_store,
 981};
 982
 983static ssize_t show_trans_timeout(struct netdev_queue *queue,
 984                                  struct netdev_queue_attribute *attribute,
 985                                  char *buf)
 986{
 987        unsigned long trans_timeout;
 988
 989        spin_lock_irq(&queue->_xmit_lock);
 990        trans_timeout = queue->trans_timeout;
 991        spin_unlock_irq(&queue->_xmit_lock);
 992
 993        return sprintf(buf, "%lu", trans_timeout);
 994}
 995
 996#ifdef CONFIG_XPS
 997static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
 998{
 999        struct net_device *dev = queue->dev;
1000        unsigned int i;
1001
1002        i = queue - dev->_tx;
1003        BUG_ON(i >= dev->num_tx_queues);
1004
1005        return i;
1006}
1007
1008static ssize_t show_tx_maxrate(struct netdev_queue *queue,
1009                               struct netdev_queue_attribute *attribute,
1010                               char *buf)
1011{
1012        return sprintf(buf, "%lu\n", queue->tx_maxrate);
1013}
1014
1015static ssize_t set_tx_maxrate(struct netdev_queue *queue,
1016                              struct netdev_queue_attribute *attribute,
1017                              const char *buf, size_t len)
1018{
1019        struct net_device *dev = queue->dev;
1020        int err, index = get_netdev_queue_index(queue);
1021        u32 rate = 0;
1022
1023        err = kstrtou32(buf, 10, &rate);
1024        if (err < 0)
1025                return err;
1026
1027        if (!rtnl_trylock())
1028                return restart_syscall();
1029
1030        err = -EOPNOTSUPP;
1031        if (get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate))
1032                err = get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate)(dev, index, rate);
1033
1034        rtnl_unlock();
1035        if (!err) {
1036                queue->tx_maxrate = rate;
1037                return len;
1038        }
1039        return err;
1040}
1041
1042static struct netdev_queue_attribute queue_tx_maxrate =
1043        __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
1044               show_tx_maxrate, set_tx_maxrate);
1045#endif
1046
1047static struct netdev_queue_attribute queue_trans_timeout =
1048        __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
1049
1050#ifdef CONFIG_BQL
1051/*
1052 * Byte queue limits sysfs structures and functions.
1053 */
1054static ssize_t bql_show(char *buf, unsigned int value)
1055{
1056        return sprintf(buf, "%u\n", value);
1057}
1058
1059static ssize_t bql_set(const char *buf, const size_t count,
1060                       unsigned int *pvalue)
1061{
1062        unsigned int value;
1063        int err;
1064
1065        if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
1066                value = DQL_MAX_LIMIT;
1067        else {
1068                err = kstrtouint(buf, 10, &value);
1069                if (err < 0)
1070                        return err;
1071                if (value > DQL_MAX_LIMIT)
1072                        return -EINVAL;
1073        }
1074
1075        *pvalue = value;
1076
1077        return count;
1078}
1079
1080static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1081                                  struct netdev_queue_attribute *attr,
1082                                  char *buf)
1083{
1084        struct dql *dql = &queue->dql;
1085
1086        return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1087}
1088
1089static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1090                                 struct netdev_queue_attribute *attribute,
1091                                 const char *buf, size_t len)
1092{
1093        struct dql *dql = &queue->dql;
1094        unsigned int value;
1095        int err;
1096
1097        err = kstrtouint(buf, 10, &value);
1098        if (err < 0)
1099                return err;
1100
1101        dql->slack_hold_time = msecs_to_jiffies(value);
1102
1103        return len;
1104}
1105
1106static struct netdev_queue_attribute bql_hold_time_attribute =
1107        __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
1108            bql_set_hold_time);
1109
1110static ssize_t bql_show_inflight(struct netdev_queue *queue,
1111                                 struct netdev_queue_attribute *attr,
1112                                 char *buf)
1113{
1114        struct dql *dql = &queue->dql;
1115
1116        return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1117}
1118
1119static struct netdev_queue_attribute bql_inflight_attribute =
1120        __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
1121
1122#define BQL_ATTR(NAME, FIELD)                                           \
1123static ssize_t bql_show_ ## NAME(struct netdev_queue *queue,            \
1124                                 struct netdev_queue_attribute *attr,   \
1125                                 char *buf)                             \
1126{                                                                       \
1127        return bql_show(buf, queue->dql.FIELD);                         \
1128}                                                                       \
1129                                                                        \
1130static ssize_t bql_set_ ## NAME(struct netdev_queue *queue,             \
1131                                struct netdev_queue_attribute *attr,    \
1132                                const char *buf, size_t len)            \
1133{                                                                       \
1134        return bql_set(buf, len, &queue->dql.FIELD);                    \
1135}                                                                       \
1136                                                                        \
1137static struct netdev_queue_attribute bql_ ## NAME ## _attribute =       \
1138        __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME,              \
1139            bql_set_ ## NAME);
1140
1141BQL_ATTR(limit, limit)
1142BQL_ATTR(limit_max, max_limit)
1143BQL_ATTR(limit_min, min_limit)
1144
1145static struct attribute *dql_attrs[] = {
1146        &bql_limit_attribute.attr,
1147        &bql_limit_max_attribute.attr,
1148        &bql_limit_min_attribute.attr,
1149        &bql_hold_time_attribute.attr,
1150        &bql_inflight_attribute.attr,
1151        NULL
1152};
1153
1154static struct attribute_group dql_group = {
1155        .name  = "byte_queue_limits",
1156        .attrs  = dql_attrs,
1157};
1158#endif /* CONFIG_BQL */
1159
1160#ifdef CONFIG_XPS
1161static ssize_t show_xps_map(struct netdev_queue *queue,
1162                            struct netdev_queue_attribute *attribute, char *buf)
1163{
1164        struct net_device *dev = queue->dev;
1165        struct xps_dev_maps *dev_maps;
1166        cpumask_var_t mask;
1167        unsigned long index;
1168        size_t len = 0;
1169        int i;
1170
1171        if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1172                return -ENOMEM;
1173
1174        index = get_netdev_queue_index(queue);
1175
1176        rcu_read_lock();
1177        dev_maps = rcu_dereference(dev->xps_maps);
1178        if (dev_maps) {
1179                for_each_possible_cpu(i) {
1180                        struct xps_map *map =
1181                            rcu_dereference(dev_maps->cpu_map[i]);
1182                        if (map) {
1183                                int j;
1184                                for (j = 0; j < map->len; j++) {
1185                                        if (map->queues[j] == index) {
1186                                                cpumask_set_cpu(i, mask);
1187                                                break;
1188                                        }
1189                                }
1190                        }
1191                }
1192        }
1193        rcu_read_unlock();
1194
1195        len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1196        if (PAGE_SIZE - len < 3) {
1197                free_cpumask_var(mask);
1198                return -EINVAL;
1199        }
1200
1201        free_cpumask_var(mask);
1202        len += sprintf(buf + len, "\n");
1203        return len;
1204}
1205
1206static ssize_t store_xps_map(struct netdev_queue *queue,
1207                      struct netdev_queue_attribute *attribute,
1208                      const char *buf, size_t len)
1209{
1210        struct net_device *dev = queue->dev;
1211        unsigned long index;
1212        cpumask_var_t mask;
1213        int err;
1214
1215        if (!capable(CAP_NET_ADMIN))
1216                return -EPERM;
1217
1218        if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1219                return -ENOMEM;
1220
1221        index = get_netdev_queue_index(queue);
1222
1223        err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1224        if (err) {
1225                free_cpumask_var(mask);
1226                return err;
1227        }
1228
1229        err = netif_set_xps_queue(dev, mask, index);
1230
1231        free_cpumask_var(mask);
1232
1233        return err ? : len;
1234}
1235
1236static struct netdev_queue_attribute xps_cpus_attribute =
1237    __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1238#endif /* CONFIG_XPS */
1239
1240static struct attribute *netdev_queue_default_attrs[] = {
1241        &queue_trans_timeout.attr,
1242#ifdef CONFIG_XPS
1243        &xps_cpus_attribute.attr,
1244        &queue_tx_maxrate.attr,
1245#endif
1246        NULL
1247};
1248
1249static void netdev_queue_release(struct kobject *kobj)
1250{
1251        struct netdev_queue *queue = to_netdev_queue(kobj);
1252
1253        memset(kobj, 0, sizeof(*kobj));
1254        dev_put(queue->dev);
1255}
1256
1257static const void *netdev_queue_namespace(struct kobject *kobj)
1258{
1259        struct netdev_queue *queue = to_netdev_queue(kobj);
1260        struct device *dev = &queue->dev->dev;
1261        const void *ns = NULL;
1262
1263        if (dev->class && dev->class->ns_type)
1264                ns = dev->class->namespace(dev);
1265
1266        return ns;
1267}
1268
1269static struct kobj_type netdev_queue_ktype = {
1270        .sysfs_ops = &netdev_queue_sysfs_ops,
1271        .release = netdev_queue_release,
1272        .default_attrs = netdev_queue_default_attrs,
1273        .namespace = netdev_queue_namespace,
1274};
1275
1276static int netdev_queue_add_kobject(struct net_device *net, int index)
1277{
1278        struct netdev_queue *queue = net->_tx + index;
1279        struct kobject *kobj = &queue->kobj;
1280        int error = 0;
1281
1282        kobj->kset = net->queues_kset;
1283        error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1284            "tx-%u", index);
1285        if (error)
1286                goto exit;
1287
1288#ifdef CONFIG_BQL
1289        error = sysfs_create_group(kobj, &dql_group);
1290        if (error)
1291                goto exit;
1292#endif
1293
1294        kobject_uevent(kobj, KOBJ_ADD);
1295        dev_hold(queue->dev);
1296
1297        return 0;
1298exit:
1299        kobject_put(kobj);
1300        return error;
1301}
1302#endif /* CONFIG_SYSFS */
1303
1304int
1305netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1306{
1307#ifdef CONFIG_SYSFS
1308        int i;
1309        int error = 0;
1310
1311        for (i = old_num; i < new_num; i++) {
1312                error = netdev_queue_add_kobject(net, i);
1313                if (error) {
1314                        new_num = old_num;
1315                        break;
1316                }
1317        }
1318
1319        while (--i >= new_num) {
1320                struct netdev_queue *queue = net->_tx + i;
1321
1322#ifdef CONFIG_BQL
1323                sysfs_remove_group(&queue->kobj, &dql_group);
1324#endif
1325                kobject_put(&queue->kobj);
1326        }
1327
1328        return error;
1329#else
1330        return 0;
1331#endif /* CONFIG_SYSFS */
1332}
1333
1334static int register_queue_kobjects(struct net_device *net)
1335{
1336        int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1337
1338#ifdef CONFIG_SYSFS
1339        net->queues_kset = kset_create_and_add("queues",
1340            NULL, &net->dev.kobj);
1341        if (!net->queues_kset)
1342                return -ENOMEM;
1343#endif
1344
1345#ifdef CONFIG_RPS
1346        real_rx = net->real_num_rx_queues;
1347#endif
1348        real_tx = net->real_num_tx_queues;
1349
1350        error = net_rx_queue_update_kobjects(net, 0, real_rx);
1351        if (error)
1352                goto error;
1353        rxq = real_rx;
1354
1355        error = netdev_queue_update_kobjects(net, 0, real_tx);
1356        if (error)
1357                goto error;
1358        txq = real_tx;
1359
1360        return 0;
1361
1362error:
1363        netdev_queue_update_kobjects(net, txq, 0);
1364        net_rx_queue_update_kobjects(net, rxq, 0);
1365        return error;
1366}
1367
1368static void remove_queue_kobjects(struct net_device *net)
1369{
1370        int real_rx = 0, real_tx = 0;
1371
1372#ifdef CONFIG_RPS
1373        real_rx = net->real_num_rx_queues;
1374#endif
1375        real_tx = net->real_num_tx_queues;
1376
1377        net_rx_queue_update_kobjects(net, real_rx, 0);
1378        netdev_queue_update_kobjects(net, real_tx, 0);
1379#ifdef CONFIG_SYSFS
1380        kset_unregister(net->queues_kset);
1381#endif
1382}
1383
1384static bool net_current_may_mount(void)
1385{
1386        struct net *net = current->nsproxy->net_ns;
1387
1388        return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1389}
1390
1391static void *net_grab_current_ns(void)
1392{
1393        struct net *ns = current->nsproxy->net_ns;
1394#ifdef CONFIG_NET_NS
1395        if (ns)
1396                atomic_inc(&ns->passive);
1397#endif
1398        return ns;
1399}
1400
1401static const void *net_initial_ns(void)
1402{
1403        return &init_net;
1404}
1405
1406static const void *net_netlink_ns(struct sock *sk)
1407{
1408        return sock_net(sk);
1409}
1410
1411struct kobj_ns_type_operations net_ns_type_operations = {
1412        .type = KOBJ_NS_TYPE_NET,
1413        .current_may_mount = net_current_may_mount,
1414        .grab_current_ns = net_grab_current_ns,
1415        .netlink_ns = net_netlink_ns,
1416        .initial_ns = net_initial_ns,
1417        .drop_ns = net_drop_ns,
1418};
1419EXPORT_SYMBOL_GPL(net_ns_type_operations);
1420
1421static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1422{
1423        struct net_device *dev = to_net_dev(d);
1424        int retval;
1425
1426        /* pass interface to uevent. */
1427        retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1428        if (retval)
1429                goto exit;
1430
1431        /* pass ifindex to uevent.
1432         * ifindex is useful as it won't change (interface name may change)
1433         * and is what RtNetlink uses natively. */
1434        retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1435
1436exit:
1437        return retval;
1438}
1439
1440/*
1441 *      netdev_release -- destroy and free a dead device.
1442 *      Called when last reference to device kobject is gone.
1443 */
1444static void netdev_release(struct device *d)
1445{
1446        struct net_device *dev = to_net_dev(d);
1447
1448        BUG_ON(dev->reg_state != NETREG_RELEASED);
1449
1450        kfree(dev->ifalias);
1451        netdev_freemem(dev);
1452}
1453
1454static const void *net_namespace(struct device *d)
1455{
1456        struct net_device *dev;
1457        dev = container_of(d, struct net_device, dev);
1458        return dev_net(dev);
1459}
1460
1461static struct class net_class = {
1462        .name = "net",
1463        .dev_release = netdev_release,
1464        .dev_groups = net_class_groups,
1465        .dev_uevent = netdev_uevent,
1466        .ns_type = &net_ns_type_operations,
1467        .namespace = net_namespace,
1468};
1469
1470/* Delete sysfs entries but hold kobject reference until after all
1471 * netdev references are gone.
1472 */
1473void netdev_unregister_kobject(struct net_device * net)
1474{
1475        struct device *dev = &(net->dev);
1476
1477        kobject_get(&dev->kobj);
1478
1479        remove_queue_kobjects(net);
1480
1481        pm_runtime_set_memalloc_noio(dev, false);
1482
1483        device_del(dev);
1484}
1485
1486/* Create sysfs entries for network device. */
1487int netdev_register_kobject(struct net_device *net)
1488{
1489        struct device *dev = &(net->dev);
1490        const struct attribute_group **groups = net->sysfs_groups;
1491        int error = 0;
1492
1493        device_initialize(dev);
1494        dev->class = &net_class;
1495        dev->platform_data = net;
1496        dev->groups = groups;
1497
1498        dev_set_name(dev, "%s", net->name);
1499
1500#ifdef CONFIG_SYSFS
1501        /* Allow for a device specific group */
1502        if (*groups)
1503                groups++;
1504
1505        *groups++ = &netstat_group;
1506
1507#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1508        if (net->ieee80211_ptr)
1509                *groups++ = &wireless_group;
1510#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1511        else if (net->wireless_handlers)
1512                *groups++ = &wireless_group;
1513#endif
1514#endif
1515#endif /* CONFIG_SYSFS */
1516
1517        error = device_add(dev);
1518        if (error)
1519                return error;
1520
1521        error = register_queue_kobjects(net);
1522        if (error) {
1523                device_del(dev);
1524                return error;
1525        }
1526
1527        pm_runtime_set_memalloc_noio(dev, true);
1528
1529        return error;
1530}
1531
1532int netdev_class_create_file_ns(struct class_attribute *class_attr,
1533                                const void *ns)
1534{
1535        return class_create_file_ns(&net_class, class_attr, ns);
1536}
1537EXPORT_SYMBOL(netdev_class_create_file_ns);
1538
1539void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1540                                 const void *ns)
1541{
1542        class_remove_file_ns(&net_class, class_attr, ns);
1543}
1544EXPORT_SYMBOL(netdev_class_remove_file_ns);
1545
1546int netdev_kobject_init(void)
1547{
1548        kobj_ns_type_register(&net_ns_type_operations);
1549        return class_register(&net_class);
1550}
1551