LCOV - code coverage report
Current view: top level - net/core - net-sysfs.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 389 0.0 %
Date: 2014-04-07 Functions: 0 110 0.0 %
Branches: 0 195 0.0 %

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

Generated by: LCOV version 1.9