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        int i, len;
 674
 675        if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
 676                return -ENOMEM;
 677
 678        rcu_read_lock();
 679        map = rcu_dereference(queue->rps_map);
 680        if (map)
 681                for (i = 0; i < map->len; i++)
 682                        cpumask_set_cpu(map->cpus[i], mask);
 683
 684        len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
 685        rcu_read_unlock();
 686        free_cpumask_var(mask);
 687
 688        return len < PAGE_SIZE ? len : -EINVAL;
 689}
 690
 691static ssize_t store_rps_map(struct netdev_rx_queue *queue,
 692                      struct rx_queue_attribute *attribute,
 693                      const char *buf, size_t len)
 694{
 695        struct rps_map *old_map, *map;
 696        cpumask_var_t mask;
 697        int err, cpu, i;
 698        static DEFINE_SPINLOCK(rps_map_lock);
 699
 700        if (!capable(CAP_NET_ADMIN))
 701                return -EPERM;
 702
 703        if (!alloc_cpumask_var(&mask, GFP_KERNEL))
 704                return -ENOMEM;
 705
 706        err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
 707        if (err) {
 708                free_cpumask_var(mask);
 709                return err;
 710        }
 711
 712        map = kzalloc(max_t(unsigned int,
 713            RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
 714            GFP_KERNEL);
 715        if (!map) {
 716                free_cpumask_var(mask);
 717                return -ENOMEM;
 718        }
 719
 720        i = 0;
 721        for_each_cpu_and(cpu, mask, cpu_online_mask)
 722                map->cpus[i++] = cpu;
 723
 724        if (i)
 725                map->len = i;
 726        else {
 727                kfree(map);
 728                map = NULL;
 729        }
 730
 731        spin_lock(&rps_map_lock);
 732        old_map = rcu_dereference_protected(queue->rps_map,
 733                                            lockdep_is_held(&rps_map_lock));
 734        rcu_assign_pointer(queue->rps_map, map);
 735        spin_unlock(&rps_map_lock);
 736
 737        if (map)
 738                static_key_slow_inc(&rps_needed);
 739        if (old_map) {
 740                kfree_rcu(old_map, rcu);
 741                static_key_slow_dec(&rps_needed);
 742        }
 743        free_cpumask_var(mask);
 744        return len;
 745}
 746
 747static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
 748                                           struct rx_queue_attribute *attr,
 749                                           char *buf)
 750{
 751        struct rps_dev_flow_table *flow_table;
 752        unsigned long val = 0;
 753
 754        rcu_read_lock();
 755        flow_table = rcu_dereference(queue->rps_flow_table);
 756        if (flow_table)
 757                val = (unsigned long)flow_table->mask + 1;
 758        rcu_read_unlock();
 759
 760        return sprintf(buf, "%lu\n", val);
 761}
 762
 763static void rps_dev_flow_table_release(struct rcu_head *rcu)
 764{
 765        struct rps_dev_flow_table *table = container_of(rcu,
 766            struct rps_dev_flow_table, rcu);
 767        vfree(table);
 768}
 769
 770static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
 771                                     struct rx_queue_attribute *attr,
 772                                     const char *buf, size_t len)
 773{
 774        unsigned long mask, count;
 775        struct rps_dev_flow_table *table, *old_table;
 776        static DEFINE_SPINLOCK(rps_dev_flow_lock);
 777        int rc;
 778
 779        if (!capable(CAP_NET_ADMIN))
 780                return -EPERM;
 781
 782        rc = kstrtoul(buf, 0, &count);
 783        if (rc < 0)
 784                return rc;
 785
 786        if (count) {
 787                mask = count - 1;
 788                /* mask = roundup_pow_of_two(count) - 1;
 789                 * without overflows...
 790                 */
 791                while ((mask | (mask >> 1)) != mask)
 792                        mask |= (mask >> 1);
 793                /* On 64 bit arches, must check mask fits in table->mask (u32),
 794                 * and on 32bit arches, must check
 795                 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
 796                 */
 797#if BITS_PER_LONG > 32
 798                if (mask > (unsigned long)(u32)mask)
 799                        return -EINVAL;
 800#else
 801                if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
 802                                / sizeof(struct rps_dev_flow)) {
 803                        /* Enforce a limit to prevent overflow */
 804                        return -EINVAL;
 805                }
 806#endif
 807                table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
 808                if (!table)
 809                        return -ENOMEM;
 810
 811                table->mask = mask;
 812                for (count = 0; count <= mask; count++)
 813                        table->flows[count].cpu = RPS_NO_CPU;
 814        } else
 815                table = NULL;
 816
 817        spin_lock(&rps_dev_flow_lock);
 818        old_table = rcu_dereference_protected(queue->rps_flow_table,
 819                                              lockdep_is_held(&rps_dev_flow_lock));
 820        rcu_assign_pointer(queue->rps_flow_table, table);
 821        spin_unlock(&rps_dev_flow_lock);
 822
 823        if (old_table)
 824                call_rcu(&old_table->rcu, rps_dev_flow_table_release);
 825
 826        return len;
 827}
 828
 829static struct rx_queue_attribute rps_cpus_attribute =
 830        __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
 831
 832
 833static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
 834        __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
 835            show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
 836
 837static struct attribute *rx_queue_default_attrs[] = {
 838        &rps_cpus_attribute.attr,
 839        &rps_dev_flow_table_cnt_attribute.attr,
 840        NULL
 841};
 842
 843static void rx_queue_release(struct kobject *kobj)
 844{
 845        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 846        struct rps_map *map;
 847        struct rps_dev_flow_table *flow_table;
 848
 849
 850        map = rcu_dereference_protected(queue->rps_map, 1);
 851        if (map) {
 852                RCU_INIT_POINTER(queue->rps_map, NULL);
 853                kfree_rcu(map, rcu);
 854        }
 855
 856        flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
 857        if (flow_table) {
 858                RCU_INIT_POINTER(queue->rps_flow_table, NULL);
 859                call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
 860        }
 861
 862        memset(kobj, 0, sizeof(*kobj));
 863        dev_put(queue->dev);
 864}
 865
 866static const void *rx_queue_namespace(struct kobject *kobj)
 867{
 868        struct netdev_rx_queue *queue = to_rx_queue(kobj);
 869        struct device *dev = &queue->dev->dev;
 870        const void *ns = NULL;
 871
 872        if (dev->class && dev->class->ns_type)
 873                ns = dev->class->namespace(dev);
 874
 875        return ns;
 876}
 877
 878static struct kobj_type rx_queue_ktype = {
 879        .sysfs_ops = &rx_queue_sysfs_ops,
 880        .release = rx_queue_release,
 881        .default_attrs = rx_queue_default_attrs,
 882        .namespace = rx_queue_namespace
 883};
 884
 885static int rx_queue_add_kobject(struct net_device *net, int index)
 886{
 887        struct netdev_rx_queue *queue = net->_rx + index;
 888        struct kobject *kobj = &queue->kobj;
 889        int error = 0;
 890
 891        kobj->kset = net->queues_kset;
 892        error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
 893            "rx-%u", index);
 894        if (error) {
 895                kobject_put(kobj);
 896                return error;
 897        }
 898
 899        kobject_uevent(kobj, KOBJ_ADD);
 900        dev_hold(queue->dev);
 901
 902        return error;
 903}
 904#endif /* CONFIG_RPS */
 905
 906int
 907net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
 908{
 909#ifdef CONFIG_RPS
 910        int i;
 911        int error = 0;
 912
 913        for (i = old_num; i < new_num; i++) {
 914                error = rx_queue_add_kobject(net, i);
 915                if (error) {
 916                        new_num = old_num;
 917                        break;
 918                }
 919        }
 920
 921        while (--i >= new_num)
 922                kobject_put(&net->_rx[i].kobj);
 923
 924        return error;
 925#else
 926        return 0;
 927#endif
 928}
 929
 930#ifdef CONFIG_SYSFS
 931/*
 932 * netdev_queue sysfs structures and functions.
 933 */
 934struct netdev_queue_attribute {
 935        struct attribute attr;
 936        ssize_t (*show)(struct netdev_queue *queue,
 937            struct netdev_queue_attribute *attr, char *buf);
 938        ssize_t (*store)(struct netdev_queue *queue,
 939            struct netdev_queue_attribute *attr, const char *buf, size_t len);
 940};
 941#define to_netdev_queue_attr(_attr) container_of(_attr,         \
 942    struct netdev_queue_attribute, attr)
 943
 944#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
 945
 946static ssize_t netdev_queue_attr_show(struct kobject *kobj,
 947                                      struct attribute *attr, char *buf)
 948{
 949        struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
 950        struct netdev_queue *queue = to_netdev_queue(kobj);
 951
 952        if (!attribute->show)
 953                return -EIO;
 954
 955        return attribute->show(queue, attribute, buf);
 956}
 957
 958static ssize_t netdev_queue_attr_store(struct kobject *kobj,
 959                                       struct attribute *attr,
 960                                       const char *buf, size_t count)
 961{
 962        struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
 963        struct netdev_queue *queue = to_netdev_queue(kobj);
 964
 965        if (!attribute->store)
 966                return -EIO;
 967
 968        return attribute->store(queue, attribute, buf, count);
 969}
 970
 971static const struct sysfs_ops netdev_queue_sysfs_ops = {
 972        .show = netdev_queue_attr_show,
 973        .store = netdev_queue_attr_store,
 974};
 975
 976static ssize_t show_trans_timeout(struct netdev_queue *queue,
 977                                  struct netdev_queue_attribute *attribute,
 978                                  char *buf)
 979{
 980        unsigned long trans_timeout;
 981
 982        spin_lock_irq(&queue->_xmit_lock);
 983        trans_timeout = queue->trans_timeout;
 984        spin_unlock_irq(&queue->_xmit_lock);
 985
 986        return sprintf(buf, "%lu", trans_timeout);
 987}
 988
 989static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
 990{
 991        struct net_device *dev = queue->dev;
 992        unsigned int i;
 993
 994        i = queue - dev->_tx;
 995        BUG_ON(i >= dev->num_tx_queues);
 996
 997        return i;
 998}
 999
1000static ssize_t show_traffic_class(struct netdev_queue *queue,
1001                                  struct netdev_queue_attribute *attribute,
1002                                  char *buf)
1003{
1004        struct net_device *dev = queue->dev;
1005        int index = get_netdev_queue_index(queue);
1006        int tc = netdev_txq_to_tc(dev, index);
1007
1008        if (tc < 0)
1009                return -EINVAL;
1010
1011        return sprintf(buf, "%u\n", tc);
1012}
1013
1014#ifdef CONFIG_XPS
1015static ssize_t show_tx_maxrate(struct netdev_queue *queue,
1016                               struct netdev_queue_attribute *attribute,
1017                               char *buf)
1018{
1019        return sprintf(buf, "%lu\n", queue->tx_maxrate);
1020}
1021
1022static ssize_t set_tx_maxrate(struct netdev_queue *queue,
1023                              struct netdev_queue_attribute *attribute,
1024                              const char *buf, size_t len)
1025{
1026        struct net_device *dev = queue->dev;
1027        int err, index = get_netdev_queue_index(queue);
1028        u32 rate = 0;
1029
1030        err = kstrtou32(buf, 10, &rate);
1031        if (err < 0)
1032                return err;
1033
1034        if (!rtnl_trylock())
1035                return restart_syscall();
1036
1037        err = -EOPNOTSUPP;
1038        if (get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate))
1039                err = get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate)(dev, index, rate);
1040
1041        rtnl_unlock();
1042        if (!err) {
1043                queue->tx_maxrate = rate;
1044                return len;
1045        }
1046        return err;
1047}
1048
1049static struct netdev_queue_attribute queue_tx_maxrate =
1050        __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
1051               show_tx_maxrate, set_tx_maxrate);
1052#endif
1053
1054static struct netdev_queue_attribute queue_trans_timeout =
1055        __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
1056
1057static struct netdev_queue_attribute queue_traffic_class =
1058        __ATTR(traffic_class, S_IRUGO, show_traffic_class, NULL);
1059
1060#ifdef CONFIG_BQL
1061/*
1062 * Byte queue limits sysfs structures and functions.
1063 */
1064static ssize_t bql_show(char *buf, unsigned int value)
1065{
1066        return sprintf(buf, "%u\n", value);
1067}
1068
1069static ssize_t bql_set(const char *buf, const size_t count,
1070                       unsigned int *pvalue)
1071{
1072        unsigned int value;
1073        int err;
1074
1075        if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
1076                value = DQL_MAX_LIMIT;
1077        else {
1078                err = kstrtouint(buf, 10, &value);
1079                if (err < 0)
1080                        return err;
1081                if (value > DQL_MAX_LIMIT)
1082                        return -EINVAL;
1083        }
1084
1085        *pvalue = value;
1086
1087        return count;
1088}
1089
1090static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1091                                  struct netdev_queue_attribute *attr,
1092                                  char *buf)
1093{
1094        struct dql *dql = &queue->dql;
1095
1096        return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1097}
1098
1099static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1100                                 struct netdev_queue_attribute *attribute,
1101                                 const char *buf, size_t len)
1102{
1103        struct dql *dql = &queue->dql;
1104        unsigned int value;
1105        int err;
1106
1107        err = kstrtouint(buf, 10, &value);
1108        if (err < 0)
1109                return err;
1110
1111        dql->slack_hold_time = msecs_to_jiffies(value);
1112
1113        return len;
1114}
1115
1116static struct netdev_queue_attribute bql_hold_time_attribute =
1117        __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
1118            bql_set_hold_time);
1119
1120static ssize_t bql_show_inflight(struct netdev_queue *queue,
1121                                 struct netdev_queue_attribute *attr,
1122                                 char *buf)
1123{
1124        struct dql *dql = &queue->dql;
1125
1126        return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1127}
1128
1129static struct netdev_queue_attribute bql_inflight_attribute =
1130        __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
1131
1132#define BQL_ATTR(NAME, FIELD)                                           \
1133static ssize_t bql_show_ ## NAME(struct netdev_queue *queue,            \
1134                                 struct netdev_queue_attribute *attr,   \
1135                                 char *buf)                             \
1136{                                                                       \
1137        return bql_show(buf, queue->dql.FIELD);                         \
1138}                                                                       \
1139                                                                        \
1140static ssize_t bql_set_ ## NAME(struct netdev_queue *queue,             \
1141                                struct netdev_queue_attribute *attr,    \
1142                                const char *buf, size_t len)            \
1143{                                                                       \
1144        return bql_set(buf, len, &queue->dql.FIELD);                    \
1145}                                                                       \
1146                                                                        \
1147static struct netdev_queue_attribute bql_ ## NAME ## _attribute =       \
1148        __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME,              \
1149            bql_set_ ## NAME);
1150
1151BQL_ATTR(limit, limit)
1152BQL_ATTR(limit_max, max_limit)
1153BQL_ATTR(limit_min, min_limit)
1154
1155static struct attribute *dql_attrs[] = {
1156        &bql_limit_attribute.attr,
1157        &bql_limit_max_attribute.attr,
1158        &bql_limit_min_attribute.attr,
1159        &bql_hold_time_attribute.attr,
1160        &bql_inflight_attribute.attr,
1161        NULL
1162};
1163
1164static struct attribute_group dql_group = {
1165        .name  = "byte_queue_limits",
1166        .attrs  = dql_attrs,
1167};
1168#endif /* CONFIG_BQL */
1169
1170#ifdef CONFIG_XPS
1171static ssize_t show_xps_map(struct netdev_queue *queue,
1172                            struct netdev_queue_attribute *attribute, char *buf)
1173{
1174        struct net_device *dev = queue->dev;
1175        int cpu, len, num_tc = 1, tc = 0;
1176        struct xps_dev_maps *dev_maps;
1177        cpumask_var_t mask;
1178        unsigned long index;
1179
1180        index = get_netdev_queue_index(queue);
1181
1182        if (dev->num_tc) {
1183                num_tc = dev->num_tc;
1184                tc = netdev_txq_to_tc(dev, index);
1185                if (tc < 0)
1186                        return -EINVAL;
1187        }
1188
1189        if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1190                return -ENOMEM;
1191
1192        rcu_read_lock();
1193        dev_maps = rcu_dereference(dev->xps_maps);
1194        if (dev_maps) {
1195                for_each_possible_cpu(cpu) {
1196                        int i, tci = cpu * num_tc + tc;
1197                        struct xps_map *map;
1198
1199                        map = rcu_dereference(dev_maps->cpu_map[tci]);
1200                        if (!map)
1201                                continue;
1202
1203                        for (i = map->len; i--;) {
1204                                if (map->queues[i] == index) {
1205                                        cpumask_set_cpu(cpu, mask);
1206                                        break;
1207                                }
1208                        }
1209                }
1210        }
1211        rcu_read_unlock();
1212
1213        len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
1214        free_cpumask_var(mask);
1215        return len < PAGE_SIZE ? len : -EINVAL;
1216}
1217
1218static ssize_t store_xps_map(struct netdev_queue *queue,
1219                      struct netdev_queue_attribute *attribute,
1220                      const char *buf, size_t len)
1221{
1222        struct net_device *dev = queue->dev;
1223        unsigned long index;
1224        cpumask_var_t mask;
1225        int err;
1226
1227        if (!capable(CAP_NET_ADMIN))
1228                return -EPERM;
1229
1230        if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1231                return -ENOMEM;
1232
1233        index = get_netdev_queue_index(queue);
1234
1235        err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1236        if (err) {
1237                free_cpumask_var(mask);
1238                return err;
1239        }
1240
1241        err = netif_set_xps_queue(dev, mask, index);
1242
1243        free_cpumask_var(mask);
1244
1245        return err ? : len;
1246}
1247
1248static struct netdev_queue_attribute xps_cpus_attribute =
1249    __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1250#endif /* CONFIG_XPS */
1251
1252static struct attribute *netdev_queue_default_attrs[] = {
1253        &queue_trans_timeout.attr,
1254        &queue_traffic_class.attr,
1255#ifdef CONFIG_XPS
1256        &xps_cpus_attribute.attr,
1257        &queue_tx_maxrate.attr,
1258#endif
1259        NULL
1260};
1261
1262static void netdev_queue_release(struct kobject *kobj)
1263{
1264        struct netdev_queue *queue = to_netdev_queue(kobj);
1265
1266        memset(kobj, 0, sizeof(*kobj));
1267        dev_put(queue->dev);
1268}
1269
1270static const void *netdev_queue_namespace(struct kobject *kobj)
1271{
1272        struct netdev_queue *queue = to_netdev_queue(kobj);
1273        struct device *dev = &queue->dev->dev;
1274        const void *ns = NULL;
1275
1276        if (dev->class && dev->class->ns_type)
1277                ns = dev->class->namespace(dev);
1278
1279        return ns;
1280}
1281
1282static struct kobj_type netdev_queue_ktype = {
1283        .sysfs_ops = &netdev_queue_sysfs_ops,
1284        .release = netdev_queue_release,
1285        .default_attrs = netdev_queue_default_attrs,
1286        .namespace = netdev_queue_namespace,
1287};
1288
1289static int netdev_queue_add_kobject(struct net_device *net, int index)
1290{
1291        struct netdev_queue *queue = net->_tx + index;
1292        struct kobject *kobj = &queue->kobj;
1293        int error = 0;
1294
1295        kobj->kset = net->queues_kset;
1296        error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1297            "tx-%u", index);
1298        if (error)
1299                goto exit;
1300
1301#ifdef CONFIG_BQL
1302        error = sysfs_create_group(kobj, &dql_group);
1303        if (error)
1304                goto exit;
1305#endif
1306
1307        kobject_uevent(kobj, KOBJ_ADD);
1308        dev_hold(queue->dev);
1309
1310        return 0;
1311exit:
1312        kobject_put(kobj);
1313        return error;
1314}
1315#endif /* CONFIG_SYSFS */
1316
1317int
1318netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1319{
1320#ifdef CONFIG_SYSFS
1321        int i;
1322        int error = 0;
1323
1324        for (i = old_num; i < new_num; i++) {
1325                error = netdev_queue_add_kobject(net, i);
1326                if (error) {
1327                        new_num = old_num;
1328                        break;
1329                }
1330        }
1331
1332        while (--i >= new_num) {
1333                struct netdev_queue *queue = net->_tx + i;
1334
1335#ifdef CONFIG_BQL
1336                sysfs_remove_group(&queue->kobj, &dql_group);
1337#endif
1338                kobject_put(&queue->kobj);
1339        }
1340
1341        return error;
1342#else
1343        return 0;
1344#endif /* CONFIG_SYSFS */
1345}
1346
1347static int register_queue_kobjects(struct net_device *net)
1348{
1349        int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1350
1351#ifdef CONFIG_SYSFS
1352        net->queues_kset = kset_create_and_add("queues",
1353            NULL, &net->dev.kobj);
1354        if (!net->queues_kset)
1355                return -ENOMEM;
1356#endif
1357
1358#ifdef CONFIG_RPS
1359        real_rx = net->real_num_rx_queues;
1360#endif
1361        real_tx = net->real_num_tx_queues;
1362
1363        error = net_rx_queue_update_kobjects(net, 0, real_rx);
1364        if (error)
1365                goto error;
1366        rxq = real_rx;
1367
1368        error = netdev_queue_update_kobjects(net, 0, real_tx);
1369        if (error)
1370                goto error;
1371        txq = real_tx;
1372
1373        return 0;
1374
1375error:
1376        netdev_queue_update_kobjects(net, txq, 0);
1377        net_rx_queue_update_kobjects(net, rxq, 0);
1378        return error;
1379}
1380
1381static void remove_queue_kobjects(struct net_device *net)
1382{
1383        int real_rx = 0, real_tx = 0;
1384
1385#ifdef CONFIG_RPS
1386        real_rx = net->real_num_rx_queues;
1387#endif
1388        real_tx = net->real_num_tx_queues;
1389
1390        net_rx_queue_update_kobjects(net, real_rx, 0);
1391        netdev_queue_update_kobjects(net, real_tx, 0);
1392#ifdef CONFIG_SYSFS
1393        kset_unregister(net->queues_kset);
1394#endif
1395}
1396
1397static bool net_current_may_mount(void)
1398{
1399        struct net *net = current->nsproxy->net_ns;
1400
1401        return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1402}
1403
1404static void *net_grab_current_ns(void)
1405{
1406        struct net *ns = current->nsproxy->net_ns;
1407#ifdef CONFIG_NET_NS
1408        if (ns)
1409                atomic_inc(&ns->passive);
1410#endif
1411        return ns;
1412}
1413
1414static const void *net_initial_ns(void)
1415{
1416        return &init_net;
1417}
1418
1419static const void *net_netlink_ns(struct sock *sk)
1420{
1421        return sock_net(sk);
1422}
1423
1424struct kobj_ns_type_operations net_ns_type_operations = {
1425        .type = KOBJ_NS_TYPE_NET,
1426        .current_may_mount = net_current_may_mount,
1427        .grab_current_ns = net_grab_current_ns,
1428        .netlink_ns = net_netlink_ns,
1429        .initial_ns = net_initial_ns,
1430        .drop_ns = net_drop_ns,
1431};
1432EXPORT_SYMBOL_GPL(net_ns_type_operations);
1433
1434static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1435{
1436        struct net_device *dev = to_net_dev(d);
1437        int retval;
1438
1439        /* pass interface to uevent. */
1440        retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1441        if (retval)
1442                goto exit;
1443
1444        /* pass ifindex to uevent.
1445         * ifindex is useful as it won't change (interface name may change)
1446         * and is what RtNetlink uses natively. */
1447        retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1448
1449exit:
1450        return retval;
1451}
1452
1453/*
1454 *      netdev_release -- destroy and free a dead device.
1455 *      Called when last reference to device kobject is gone.
1456 */
1457static void netdev_release(struct device *d)
1458{
1459        struct net_device *dev = to_net_dev(d);
1460
1461        BUG_ON(dev->reg_state != NETREG_RELEASED);
1462
1463        kfree(dev->ifalias);
1464        netdev_freemem(dev);
1465}
1466
1467static const void *net_namespace(struct device *d)
1468{
1469        struct net_device *dev;
1470        dev = container_of(d, struct net_device, dev);
1471        return dev_net(dev);
1472}
1473
1474static struct class net_class = {
1475        .name = "net",
1476        .dev_release = netdev_release,
1477        .dev_groups = net_class_groups,
1478        .dev_uevent = netdev_uevent,
1479        .ns_type = &net_ns_type_operations,
1480        .namespace = net_namespace,
1481};
1482
1483/* Delete sysfs entries but hold kobject reference until after all
1484 * netdev references are gone.
1485 */
1486void netdev_unregister_kobject(struct net_device * net)
1487{
1488        struct device *dev = &(net->dev);
1489
1490        kobject_get(&dev->kobj);
1491
1492        remove_queue_kobjects(net);
1493
1494        pm_runtime_set_memalloc_noio(dev, false);
1495
1496        device_del(dev);
1497}
1498
1499/* Create sysfs entries for network device. */
1500int netdev_register_kobject(struct net_device *net)
1501{
1502        struct device *dev = &(net->dev);
1503        const struct attribute_group **groups = net->sysfs_groups;
1504        int error = 0;
1505
1506        device_initialize(dev);
1507        dev->class = &net_class;
1508        dev->platform_data = net;
1509        dev->groups = groups;
1510
1511        dev_set_name(dev, "%s", net->name);
1512
1513#ifdef CONFIG_SYSFS
1514        /* Allow for a device specific group */
1515        if (*groups)
1516                groups++;
1517
1518        *groups++ = &netstat_group;
1519
1520#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1521        if (net->ieee80211_ptr)
1522                *groups++ = &wireless_group;
1523#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1524        else if (net->wireless_handlers)
1525                *groups++ = &wireless_group;
1526#endif
1527#endif
1528#endif /* CONFIG_SYSFS */
1529
1530        error = device_add(dev);
1531        if (error)
1532                return error;
1533
1534        error = register_queue_kobjects(net);
1535        if (error) {
1536                device_del(dev);
1537                return error;
1538        }
1539
1540        pm_runtime_set_memalloc_noio(dev, true);
1541
1542        return error;
1543}
1544
1545int netdev_class_create_file_ns(struct class_attribute *class_attr,
1546                                const void *ns)
1547{
1548        return class_create_file_ns(&net_class, class_attr, ns);
1549}
1550EXPORT_SYMBOL(netdev_class_create_file_ns);
1551
1552void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1553                                 const void *ns)
1554{
1555        class_remove_file_ns(&net_class, class_attr, ns);
1556}
1557EXPORT_SYMBOL(netdev_class_remove_file_ns);
1558
1559int netdev_kobject_init(void)
1560{
1561        kobj_ns_type_register(&net_ns_type_operations);
1562        return class_register(&net_class);
1563}
1564