11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * net-sysfs.c - network device class and attributes 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 124fc268d2SRandy Dunlap #include <linux/capability.h> 131da177e4SLinus Torvalds #include <linux/kernel.h> 141da177e4SLinus Torvalds #include <linux/netdevice.h> 151da177e4SLinus Torvalds #include <linux/if_arp.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 17608b4b95SEric W. Biederman #include <linux/nsproxy.h> 181da177e4SLinus Torvalds #include <net/sock.h> 19608b4b95SEric W. Biederman #include <net/net_namespace.h> 201da177e4SLinus Torvalds #include <linux/rtnetlink.h> 211da177e4SLinus Torvalds #include <linux/wireless.h> 22fec5e652STom Herbert #include <linux/vmalloc.h> 23bc3b2d7fSPaul Gortmaker #include <linux/export.h> 24114cf580STom Herbert #include <linux/jiffies.h> 258f1546caSJohannes Berg #include <net/wext.h> 261da177e4SLinus Torvalds 27342709efSPavel Emelyanov #include "net-sysfs.h" 28342709efSPavel Emelyanov 298b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 301da177e4SLinus Torvalds static const char fmt_hex[] = "%#x\n"; 31d1102b59SDavid S. Miller static const char fmt_long_hex[] = "%#lx\n"; 321da177e4SLinus Torvalds static const char fmt_dec[] = "%d\n"; 338ae6dacaSDavid Decotigny static const char fmt_udec[] = "%u\n"; 341da177e4SLinus Torvalds static const char fmt_ulong[] = "%lu\n"; 35be1f3c2cSBen Hutchings static const char fmt_u64[] = "%llu\n"; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds static inline int dev_isalive(const struct net_device *dev) 381da177e4SLinus Torvalds { 39fe9925b5SStephen Hemminger return dev->reg_state <= NETREG_REGISTERED; 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* use same locking rules as GIF* ioctl's */ 4343cb76d9SGreg Kroah-Hartman static ssize_t netdev_show(const struct device *dev, 4443cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf, 451da177e4SLinus Torvalds ssize_t (*format)(const struct net_device *, char *)) 461da177e4SLinus Torvalds { 4743cb76d9SGreg Kroah-Hartman struct net_device *net = to_net_dev(dev); 481da177e4SLinus Torvalds ssize_t ret = -EINVAL; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds read_lock(&dev_base_lock); 511da177e4SLinus Torvalds if (dev_isalive(net)) 521da177e4SLinus Torvalds ret = (*format)(net, buf); 531da177e4SLinus Torvalds read_unlock(&dev_base_lock); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds return ret; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* generate a show function for simple field */ 591da177e4SLinus Torvalds #define NETDEVICE_SHOW(field, format_string) \ 601da177e4SLinus Torvalds static ssize_t format_##field(const struct net_device *net, char *buf) \ 611da177e4SLinus Torvalds { \ 621da177e4SLinus Torvalds return sprintf(buf, format_string, net->field); \ 631da177e4SLinus Torvalds } \ 6443cb76d9SGreg Kroah-Hartman static ssize_t show_##field(struct device *dev, \ 6543cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 661da177e4SLinus Torvalds { \ 6743cb76d9SGreg Kroah-Hartman return netdev_show(dev, attr, buf, format_##field); \ 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* use same locking and permission rules as SIF* ioctl's */ 7243cb76d9SGreg Kroah-Hartman static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, 731da177e4SLinus Torvalds const char *buf, size_t len, 741da177e4SLinus Torvalds int (*set)(struct net_device *, unsigned long)) 751da177e4SLinus Torvalds { 761da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 771da177e4SLinus Torvalds unsigned long new; 781da177e4SLinus Torvalds int ret = -EINVAL; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 811da177e4SLinus Torvalds return -EPERM; 821da177e4SLinus Torvalds 83e1e420c7SShuah Khan ret = kstrtoul(buf, 0, &new); 84e1e420c7SShuah Khan if (ret) 851da177e4SLinus Torvalds goto err; 861da177e4SLinus Torvalds 875a5990d3SStephen Hemminger if (!rtnl_trylock()) 88336ca57cSEric W. Biederman return restart_syscall(); 895a5990d3SStephen Hemminger 901da177e4SLinus Torvalds if (dev_isalive(net)) { 911da177e4SLinus Torvalds if ((ret = (*set)(net, new)) == 0) 921da177e4SLinus Torvalds ret = len; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds rtnl_unlock(); 951da177e4SLinus Torvalds err: 961da177e4SLinus Torvalds return ret; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 999d29672cSDavid Woodhouse NETDEVICE_SHOW(dev_id, fmt_hex); 100c1f79426SStefan Assmann NETDEVICE_SHOW(addr_assign_type, fmt_dec); 101fd586bacSKay Sievers NETDEVICE_SHOW(addr_len, fmt_dec); 102fd586bacSKay Sievers NETDEVICE_SHOW(iflink, fmt_dec); 103fd586bacSKay Sievers NETDEVICE_SHOW(ifindex, fmt_dec); 104fd586bacSKay Sievers NETDEVICE_SHOW(type, fmt_dec); 105b00055aaSStefan Rompf NETDEVICE_SHOW(link_mode, fmt_dec); 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /* use same locking rules as GIFHWADDR ioctl's */ 10843cb76d9SGreg Kroah-Hartman static ssize_t show_address(struct device *dev, struct device_attribute *attr, 10943cb76d9SGreg Kroah-Hartman char *buf) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1121da177e4SLinus Torvalds ssize_t ret = -EINVAL; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds read_lock(&dev_base_lock); 1151da177e4SLinus Torvalds if (dev_isalive(net)) 1167ffc49a6SMichael Chan ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); 1171da177e4SLinus Torvalds read_unlock(&dev_base_lock); 1181da177e4SLinus Torvalds return ret; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 12143cb76d9SGreg Kroah-Hartman static ssize_t show_broadcast(struct device *dev, 12243cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1251da177e4SLinus Torvalds if (dev_isalive(net)) 1267ffc49a6SMichael Chan return sysfs_format_mac(buf, net->broadcast, net->addr_len); 1271da177e4SLinus Torvalds return -EINVAL; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 13043cb76d9SGreg Kroah-Hartman static ssize_t show_carrier(struct device *dev, 13143cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds struct net_device *netdev = to_net_dev(dev); 1341da177e4SLinus Torvalds if (netif_running(netdev)) { 1351da177e4SLinus Torvalds return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds return -EINVAL; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 140d519e17eSAndy Gospodarek static ssize_t show_speed(struct device *dev, 141d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 142d519e17eSAndy Gospodarek { 143d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 144d519e17eSAndy Gospodarek int ret = -EINVAL; 145d519e17eSAndy Gospodarek 146d519e17eSAndy Gospodarek if (!rtnl_trylock()) 147d519e17eSAndy Gospodarek return restart_syscall(); 148d519e17eSAndy Gospodarek 1498ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1508ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 1514bc71cb9SJiri Pirko if (!__ethtool_get_settings(netdev, &cmd)) 1528ae6dacaSDavid Decotigny ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd)); 153d519e17eSAndy Gospodarek } 154d519e17eSAndy Gospodarek rtnl_unlock(); 155d519e17eSAndy Gospodarek return ret; 156d519e17eSAndy Gospodarek } 157d519e17eSAndy Gospodarek 158d519e17eSAndy Gospodarek static ssize_t show_duplex(struct device *dev, 159d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 160d519e17eSAndy Gospodarek { 161d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 162d519e17eSAndy Gospodarek int ret = -EINVAL; 163d519e17eSAndy Gospodarek 164d519e17eSAndy Gospodarek if (!rtnl_trylock()) 165d519e17eSAndy Gospodarek return restart_syscall(); 166d519e17eSAndy Gospodarek 1678ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1688ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 1694bc71cb9SJiri Pirko if (!__ethtool_get_settings(netdev, &cmd)) 1708ae6dacaSDavid Decotigny ret = sprintf(buf, "%s\n", 1718ae6dacaSDavid Decotigny cmd.duplex ? "full" : "half"); 172d519e17eSAndy Gospodarek } 173d519e17eSAndy Gospodarek rtnl_unlock(); 174d519e17eSAndy Gospodarek return ret; 175d519e17eSAndy Gospodarek } 176d519e17eSAndy Gospodarek 17743cb76d9SGreg Kroah-Hartman static ssize_t show_dormant(struct device *dev, 17843cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 179b00055aaSStefan Rompf { 180b00055aaSStefan Rompf struct net_device *netdev = to_net_dev(dev); 181b00055aaSStefan Rompf 182b00055aaSStefan Rompf if (netif_running(netdev)) 183b00055aaSStefan Rompf return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); 184b00055aaSStefan Rompf 185b00055aaSStefan Rompf return -EINVAL; 186b00055aaSStefan Rompf } 187b00055aaSStefan Rompf 18836cbd3dcSJan Engelhardt static const char *const operstates[] = { 189b00055aaSStefan Rompf "unknown", 190b00055aaSStefan Rompf "notpresent", /* currently unused */ 191b00055aaSStefan Rompf "down", 192b00055aaSStefan Rompf "lowerlayerdown", 193b00055aaSStefan Rompf "testing", /* currently unused */ 194b00055aaSStefan Rompf "dormant", 195b00055aaSStefan Rompf "up" 196b00055aaSStefan Rompf }; 197b00055aaSStefan Rompf 19843cb76d9SGreg Kroah-Hartman static ssize_t show_operstate(struct device *dev, 19943cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 200b00055aaSStefan Rompf { 201b00055aaSStefan Rompf const struct net_device *netdev = to_net_dev(dev); 202b00055aaSStefan Rompf unsigned char operstate; 203b00055aaSStefan Rompf 204b00055aaSStefan Rompf read_lock(&dev_base_lock); 205b00055aaSStefan Rompf operstate = netdev->operstate; 206b00055aaSStefan Rompf if (!netif_running(netdev)) 207b00055aaSStefan Rompf operstate = IF_OPER_DOWN; 208b00055aaSStefan Rompf read_unlock(&dev_base_lock); 209b00055aaSStefan Rompf 210e3a5cd9eSAdrian Bunk if (operstate >= ARRAY_SIZE(operstates)) 211b00055aaSStefan Rompf return -EINVAL; /* should not happen */ 212b00055aaSStefan Rompf 213b00055aaSStefan Rompf return sprintf(buf, "%s\n", operstates[operstate]); 214b00055aaSStefan Rompf } 215b00055aaSStefan Rompf 2161da177e4SLinus Torvalds /* read-write attributes */ 2171da177e4SLinus Torvalds NETDEVICE_SHOW(mtu, fmt_dec); 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds static int change_mtu(struct net_device *net, unsigned long new_mtu) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds return dev_set_mtu(net, (int) new_mtu); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 22443cb76d9SGreg Kroah-Hartman static ssize_t store_mtu(struct device *dev, struct device_attribute *attr, 22543cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2261da177e4SLinus Torvalds { 22743cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_mtu); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds NETDEVICE_SHOW(flags, fmt_hex); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds static int change_flags(struct net_device *net, unsigned long new_flags) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds return dev_change_flags(net, (unsigned) new_flags); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 23743cb76d9SGreg Kroah-Hartman static ssize_t store_flags(struct device *dev, struct device_attribute *attr, 23843cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2391da177e4SLinus Torvalds { 24043cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_flags); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds NETDEVICE_SHOW(tx_queue_len, fmt_ulong); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static int change_tx_queue_len(struct net_device *net, unsigned long new_len) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds net->tx_queue_len = new_len; 2481da177e4SLinus Torvalds return 0; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 25143cb76d9SGreg Kroah-Hartman static ssize_t store_tx_queue_len(struct device *dev, 25243cb76d9SGreg Kroah-Hartman struct device_attribute *attr, 25343cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2541da177e4SLinus Torvalds { 25543cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_tx_queue_len); 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2580b815a1aSStephen Hemminger static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr, 2590b815a1aSStephen Hemminger const char *buf, size_t len) 2600b815a1aSStephen Hemminger { 2610b815a1aSStephen Hemminger struct net_device *netdev = to_net_dev(dev); 2620b815a1aSStephen Hemminger size_t count = len; 2630b815a1aSStephen Hemminger ssize_t ret; 2640b815a1aSStephen Hemminger 2650b815a1aSStephen Hemminger if (!capable(CAP_NET_ADMIN)) 2660b815a1aSStephen Hemminger return -EPERM; 2670b815a1aSStephen Hemminger 2680b815a1aSStephen Hemminger /* ignore trailing newline */ 2690b815a1aSStephen Hemminger if (len > 0 && buf[len - 1] == '\n') 2700b815a1aSStephen Hemminger --count; 2710b815a1aSStephen Hemminger 272336ca57cSEric W. Biederman if (!rtnl_trylock()) 273336ca57cSEric W. Biederman return restart_syscall(); 2740b815a1aSStephen Hemminger ret = dev_set_alias(netdev, buf, count); 2750b815a1aSStephen Hemminger rtnl_unlock(); 2760b815a1aSStephen Hemminger 2770b815a1aSStephen Hemminger return ret < 0 ? ret : len; 2780b815a1aSStephen Hemminger } 2790b815a1aSStephen Hemminger 2800b815a1aSStephen Hemminger static ssize_t show_ifalias(struct device *dev, 2810b815a1aSStephen Hemminger struct device_attribute *attr, char *buf) 2820b815a1aSStephen Hemminger { 2830b815a1aSStephen Hemminger const struct net_device *netdev = to_net_dev(dev); 2840b815a1aSStephen Hemminger ssize_t ret = 0; 2850b815a1aSStephen Hemminger 286336ca57cSEric W. Biederman if (!rtnl_trylock()) 287336ca57cSEric W. Biederman return restart_syscall(); 2880b815a1aSStephen Hemminger if (netdev->ifalias) 2890b815a1aSStephen Hemminger ret = sprintf(buf, "%s\n", netdev->ifalias); 2900b815a1aSStephen Hemminger rtnl_unlock(); 2910b815a1aSStephen Hemminger return ret; 2920b815a1aSStephen Hemminger } 2930b815a1aSStephen Hemminger 294a512b92bSVlad Dogaru NETDEVICE_SHOW(group, fmt_dec); 295a512b92bSVlad Dogaru 296a512b92bSVlad Dogaru static int change_group(struct net_device *net, unsigned long new_group) 297a512b92bSVlad Dogaru { 298a512b92bSVlad Dogaru dev_set_group(net, (int) new_group); 299a512b92bSVlad Dogaru return 0; 300a512b92bSVlad Dogaru } 301a512b92bSVlad Dogaru 302a512b92bSVlad Dogaru static ssize_t store_group(struct device *dev, struct device_attribute *attr, 303a512b92bSVlad Dogaru const char *buf, size_t len) 304a512b92bSVlad Dogaru { 305a512b92bSVlad Dogaru return netdev_store(dev, attr, buf, len, change_group); 306a512b92bSVlad Dogaru } 307a512b92bSVlad Dogaru 30843cb76d9SGreg Kroah-Hartman static struct device_attribute net_class_attributes[] = { 309c1f79426SStefan Assmann __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL), 310fd586bacSKay Sievers __ATTR(addr_len, S_IRUGO, show_addr_len, NULL), 3119d29672cSDavid Woodhouse __ATTR(dev_id, S_IRUGO, show_dev_id, NULL), 3120b815a1aSStephen Hemminger __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias), 313fd586bacSKay Sievers __ATTR(iflink, S_IRUGO, show_iflink, NULL), 314fd586bacSKay Sievers __ATTR(ifindex, S_IRUGO, show_ifindex, NULL), 315fd586bacSKay Sievers __ATTR(type, S_IRUGO, show_type, NULL), 316b00055aaSStefan Rompf __ATTR(link_mode, S_IRUGO, show_link_mode, NULL), 317fd586bacSKay Sievers __ATTR(address, S_IRUGO, show_address, NULL), 318fd586bacSKay Sievers __ATTR(broadcast, S_IRUGO, show_broadcast, NULL), 319fd586bacSKay Sievers __ATTR(carrier, S_IRUGO, show_carrier, NULL), 320d519e17eSAndy Gospodarek __ATTR(speed, S_IRUGO, show_speed, NULL), 321d519e17eSAndy Gospodarek __ATTR(duplex, S_IRUGO, show_duplex, NULL), 322b00055aaSStefan Rompf __ATTR(dormant, S_IRUGO, show_dormant, NULL), 323b00055aaSStefan Rompf __ATTR(operstate, S_IRUGO, show_operstate, NULL), 324fd586bacSKay Sievers __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu), 325fd586bacSKay Sievers __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags), 326fd586bacSKay Sievers __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len, 327fd586bacSKay Sievers store_tx_queue_len), 328b6644cb7SXiaotian Feng __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group), 329fd586bacSKay Sievers {} 3301da177e4SLinus Torvalds }; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */ 33343cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d, 33443cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf, 3351da177e4SLinus Torvalds unsigned long offset) 3361da177e4SLinus Torvalds { 33743cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 3381da177e4SLinus Torvalds ssize_t ret = -EINVAL; 3391da177e4SLinus Torvalds 340be1f3c2cSBen Hutchings WARN_ON(offset > sizeof(struct rtnl_link_stats64) || 341be1f3c2cSBen Hutchings offset % sizeof(u64) != 0); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds read_lock(&dev_base_lock); 34496e74088SPavel Emelyanov if (dev_isalive(dev)) { 34528172739SEric Dumazet struct rtnl_link_stats64 temp; 34628172739SEric Dumazet const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); 34728172739SEric Dumazet 348be1f3c2cSBen Hutchings ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset)); 34996e74088SPavel Emelyanov } 3501da177e4SLinus Torvalds read_unlock(&dev_base_lock); 3511da177e4SLinus Torvalds return ret; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* generate a read-only statistics attribute */ 3551da177e4SLinus Torvalds #define NETSTAT_ENTRY(name) \ 35643cb76d9SGreg Kroah-Hartman static ssize_t show_##name(struct device *d, \ 35743cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 3581da177e4SLinus Torvalds { \ 35943cb76d9SGreg Kroah-Hartman return netstat_show(d, attr, buf, \ 360be1f3c2cSBen Hutchings offsetof(struct rtnl_link_stats64, name)); \ 3611da177e4SLinus Torvalds } \ 36243cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets); 3651da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets); 3661da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes); 3671da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes); 3681da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors); 3691da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors); 3701da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped); 3711da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped); 3721da177e4SLinus Torvalds NETSTAT_ENTRY(multicast); 3731da177e4SLinus Torvalds NETSTAT_ENTRY(collisions); 3741da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors); 3751da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors); 3761da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors); 3771da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors); 3781da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors); 3791da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors); 3801da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors); 3811da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors); 3821da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors); 3831da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors); 3841da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors); 3851da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed); 3861da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = { 38943cb76d9SGreg Kroah-Hartman &dev_attr_rx_packets.attr, 39043cb76d9SGreg Kroah-Hartman &dev_attr_tx_packets.attr, 39143cb76d9SGreg Kroah-Hartman &dev_attr_rx_bytes.attr, 39243cb76d9SGreg Kroah-Hartman &dev_attr_tx_bytes.attr, 39343cb76d9SGreg Kroah-Hartman &dev_attr_rx_errors.attr, 39443cb76d9SGreg Kroah-Hartman &dev_attr_tx_errors.attr, 39543cb76d9SGreg Kroah-Hartman &dev_attr_rx_dropped.attr, 39643cb76d9SGreg Kroah-Hartman &dev_attr_tx_dropped.attr, 39743cb76d9SGreg Kroah-Hartman &dev_attr_multicast.attr, 39843cb76d9SGreg Kroah-Hartman &dev_attr_collisions.attr, 39943cb76d9SGreg Kroah-Hartman &dev_attr_rx_length_errors.attr, 40043cb76d9SGreg Kroah-Hartman &dev_attr_rx_over_errors.attr, 40143cb76d9SGreg Kroah-Hartman &dev_attr_rx_crc_errors.attr, 40243cb76d9SGreg Kroah-Hartman &dev_attr_rx_frame_errors.attr, 40343cb76d9SGreg Kroah-Hartman &dev_attr_rx_fifo_errors.attr, 40443cb76d9SGreg Kroah-Hartman &dev_attr_rx_missed_errors.attr, 40543cb76d9SGreg Kroah-Hartman &dev_attr_tx_aborted_errors.attr, 40643cb76d9SGreg Kroah-Hartman &dev_attr_tx_carrier_errors.attr, 40743cb76d9SGreg Kroah-Hartman &dev_attr_tx_fifo_errors.attr, 40843cb76d9SGreg Kroah-Hartman &dev_attr_tx_heartbeat_errors.attr, 40943cb76d9SGreg Kroah-Hartman &dev_attr_tx_window_errors.attr, 41043cb76d9SGreg Kroah-Hartman &dev_attr_rx_compressed.attr, 41143cb76d9SGreg Kroah-Hartman &dev_attr_tx_compressed.attr, 4121da177e4SLinus Torvalds NULL 4131da177e4SLinus Torvalds }; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds static struct attribute_group netstat_group = { 4171da177e4SLinus Torvalds .name = "statistics", 4181da177e4SLinus Torvalds .attrs = netstat_attrs, 4191da177e4SLinus Torvalds }; 4201da177e4SLinus Torvalds 42122bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS 4221da177e4SLinus Torvalds /* helper function that does all the locking etc for wireless stats */ 42343cb76d9SGreg Kroah-Hartman static ssize_t wireless_show(struct device *d, char *buf, 4241da177e4SLinus Torvalds ssize_t (*format)(const struct iw_statistics *, 4251da177e4SLinus Torvalds char *)) 4261da177e4SLinus Torvalds { 42743cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 4288f1546caSJohannes Berg const struct iw_statistics *iw; 4291da177e4SLinus Torvalds ssize_t ret = -EINVAL; 4301da177e4SLinus Torvalds 431b8afe641SEric W. Biederman if (!rtnl_trylock()) 432b8afe641SEric W. Biederman return restart_syscall(); 4336dd214b5SAndrey Borzenkov if (dev_isalive(dev)) { 4348f1546caSJohannes Berg iw = get_wireless_stats(dev); 4358f1546caSJohannes Berg if (iw) 4361da177e4SLinus Torvalds ret = (*format)(iw, buf); 4376dd214b5SAndrey Borzenkov } 438a160ee69SJohannes Berg rtnl_unlock(); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds return ret; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* show function template for wireless fields */ 4441da177e4SLinus Torvalds #define WIRELESS_SHOW(name, field, format_string) \ 4451da177e4SLinus Torvalds static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \ 4461da177e4SLinus Torvalds { \ 4471da177e4SLinus Torvalds return sprintf(buf, format_string, iw->field); \ 4481da177e4SLinus Torvalds } \ 44943cb76d9SGreg Kroah-Hartman static ssize_t show_iw_##name(struct device *d, \ 45043cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 4511da177e4SLinus Torvalds { \ 45243cb76d9SGreg Kroah-Hartman return wireless_show(d, buf, format_iw_##name); \ 4531da177e4SLinus Torvalds } \ 45443cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL) 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds WIRELESS_SHOW(status, status, fmt_hex); 4571da177e4SLinus Torvalds WIRELESS_SHOW(link, qual.qual, fmt_dec); 4581da177e4SLinus Torvalds WIRELESS_SHOW(level, qual.level, fmt_dec); 4591da177e4SLinus Torvalds WIRELESS_SHOW(noise, qual.noise, fmt_dec); 4601da177e4SLinus Torvalds WIRELESS_SHOW(nwid, discard.nwid, fmt_dec); 4611da177e4SLinus Torvalds WIRELESS_SHOW(crypt, discard.code, fmt_dec); 4621da177e4SLinus Torvalds WIRELESS_SHOW(fragment, discard.fragment, fmt_dec); 4631da177e4SLinus Torvalds WIRELESS_SHOW(misc, discard.misc, fmt_dec); 4641da177e4SLinus Torvalds WIRELESS_SHOW(retries, discard.retries, fmt_dec); 4651da177e4SLinus Torvalds WIRELESS_SHOW(beacon, miss.beacon, fmt_dec); 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds static struct attribute *wireless_attrs[] = { 46843cb76d9SGreg Kroah-Hartman &dev_attr_status.attr, 46943cb76d9SGreg Kroah-Hartman &dev_attr_link.attr, 47043cb76d9SGreg Kroah-Hartman &dev_attr_level.attr, 47143cb76d9SGreg Kroah-Hartman &dev_attr_noise.attr, 47243cb76d9SGreg Kroah-Hartman &dev_attr_nwid.attr, 47343cb76d9SGreg Kroah-Hartman &dev_attr_crypt.attr, 47443cb76d9SGreg Kroah-Hartman &dev_attr_fragment.attr, 47543cb76d9SGreg Kroah-Hartman &dev_attr_retries.attr, 47643cb76d9SGreg Kroah-Hartman &dev_attr_misc.attr, 47743cb76d9SGreg Kroah-Hartman &dev_attr_beacon.attr, 4781da177e4SLinus Torvalds NULL 4791da177e4SLinus Torvalds }; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static struct attribute_group wireless_group = { 4821da177e4SLinus Torvalds .name = "wireless", 4831da177e4SLinus Torvalds .attrs = wireless_attrs, 4841da177e4SLinus Torvalds }; 4851da177e4SLinus Torvalds #endif 486d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */ 4871da177e4SLinus Torvalds 48830bde1f5SStephen Rothwell #ifdef CONFIG_RPS 4890a9627f2STom Herbert /* 4900a9627f2STom Herbert * RX queue sysfs structures and functions. 4910a9627f2STom Herbert */ 4920a9627f2STom Herbert struct rx_queue_attribute { 4930a9627f2STom Herbert struct attribute attr; 4940a9627f2STom Herbert ssize_t (*show)(struct netdev_rx_queue *queue, 4950a9627f2STom Herbert struct rx_queue_attribute *attr, char *buf); 4960a9627f2STom Herbert ssize_t (*store)(struct netdev_rx_queue *queue, 4970a9627f2STom Herbert struct rx_queue_attribute *attr, const char *buf, size_t len); 4980a9627f2STom Herbert }; 4990a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr, \ 5000a9627f2STom Herbert struct rx_queue_attribute, attr) 5010a9627f2STom Herbert 5020a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) 5030a9627f2STom Herbert 5040a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, 5050a9627f2STom Herbert char *buf) 5060a9627f2STom Herbert { 5070a9627f2STom Herbert struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 5080a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 5090a9627f2STom Herbert 5100a9627f2STom Herbert if (!attribute->show) 5110a9627f2STom Herbert return -EIO; 5120a9627f2STom Herbert 5130a9627f2STom Herbert return attribute->show(queue, attribute, buf); 5140a9627f2STom Herbert } 5150a9627f2STom Herbert 5160a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, 5170a9627f2STom Herbert const char *buf, size_t count) 5180a9627f2STom Herbert { 5190a9627f2STom Herbert struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 5200a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 5210a9627f2STom Herbert 5220a9627f2STom Herbert if (!attribute->store) 5230a9627f2STom Herbert return -EIO; 5240a9627f2STom Herbert 5250a9627f2STom Herbert return attribute->store(queue, attribute, buf, count); 5260a9627f2STom Herbert } 5270a9627f2STom Herbert 528fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = { 5290a9627f2STom Herbert .show = rx_queue_attr_show, 5300a9627f2STom Herbert .store = rx_queue_attr_store, 5310a9627f2STom Herbert }; 5320a9627f2STom Herbert 5330a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue, 5340a9627f2STom Herbert struct rx_queue_attribute *attribute, char *buf) 5350a9627f2STom Herbert { 5360a9627f2STom Herbert struct rps_map *map; 5370a9627f2STom Herbert cpumask_var_t mask; 5380a9627f2STom Herbert size_t len = 0; 5390a9627f2STom Herbert int i; 5400a9627f2STom Herbert 5410a9627f2STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 5420a9627f2STom Herbert return -ENOMEM; 5430a9627f2STom Herbert 5440a9627f2STom Herbert rcu_read_lock(); 5450a9627f2STom Herbert map = rcu_dereference(queue->rps_map); 5460a9627f2STom Herbert if (map) 5470a9627f2STom Herbert for (i = 0; i < map->len; i++) 5480a9627f2STom Herbert cpumask_set_cpu(map->cpus[i], mask); 5490a9627f2STom Herbert 5500a9627f2STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 5510a9627f2STom Herbert if (PAGE_SIZE - len < 3) { 5520a9627f2STom Herbert rcu_read_unlock(); 5530a9627f2STom Herbert free_cpumask_var(mask); 5540a9627f2STom Herbert return -EINVAL; 5550a9627f2STom Herbert } 5560a9627f2STom Herbert rcu_read_unlock(); 5570a9627f2STom Herbert 5580a9627f2STom Herbert free_cpumask_var(mask); 5590a9627f2STom Herbert len += sprintf(buf + len, "\n"); 5600a9627f2STom Herbert return len; 5610a9627f2STom Herbert } 5620a9627f2STom Herbert 563f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue, 5640a9627f2STom Herbert struct rx_queue_attribute *attribute, 5650a9627f2STom Herbert const char *buf, size_t len) 5660a9627f2STom Herbert { 5670a9627f2STom Herbert struct rps_map *old_map, *map; 5680a9627f2STom Herbert cpumask_var_t mask; 5690a9627f2STom Herbert int err, cpu, i; 5700a9627f2STom Herbert static DEFINE_SPINLOCK(rps_map_lock); 5710a9627f2STom Herbert 5720a9627f2STom Herbert if (!capable(CAP_NET_ADMIN)) 5730a9627f2STom Herbert return -EPERM; 5740a9627f2STom Herbert 5750a9627f2STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 5760a9627f2STom Herbert return -ENOMEM; 5770a9627f2STom Herbert 5780a9627f2STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 5790a9627f2STom Herbert if (err) { 5800a9627f2STom Herbert free_cpumask_var(mask); 5810a9627f2STom Herbert return err; 5820a9627f2STom Herbert } 5830a9627f2STom Herbert 5840a9627f2STom Herbert map = kzalloc(max_t(unsigned, 5850a9627f2STom Herbert RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), 5860a9627f2STom Herbert GFP_KERNEL); 5870a9627f2STom Herbert if (!map) { 5880a9627f2STom Herbert free_cpumask_var(mask); 5890a9627f2STom Herbert return -ENOMEM; 5900a9627f2STom Herbert } 5910a9627f2STom Herbert 5920a9627f2STom Herbert i = 0; 5930a9627f2STom Herbert for_each_cpu_and(cpu, mask, cpu_online_mask) 5940a9627f2STom Herbert map->cpus[i++] = cpu; 5950a9627f2STom Herbert 5960a9627f2STom Herbert if (i) 5970a9627f2STom Herbert map->len = i; 5980a9627f2STom Herbert else { 5990a9627f2STom Herbert kfree(map); 6000a9627f2STom Herbert map = NULL; 6010a9627f2STom Herbert } 6020a9627f2STom Herbert 6030a9627f2STom Herbert spin_lock(&rps_map_lock); 6046e3f7fafSEric Dumazet old_map = rcu_dereference_protected(queue->rps_map, 6056e3f7fafSEric Dumazet lockdep_is_held(&rps_map_lock)); 6060a9627f2STom Herbert rcu_assign_pointer(queue->rps_map, map); 6070a9627f2STom Herbert spin_unlock(&rps_map_lock); 6080a9627f2STom Herbert 609adc9300eSEric Dumazet if (map) 610c5905afbSIngo Molnar static_key_slow_inc(&rps_needed); 611adc9300eSEric Dumazet if (old_map) { 612f6f80238SLai Jiangshan kfree_rcu(old_map, rcu); 613c5905afbSIngo Molnar static_key_slow_dec(&rps_needed); 614adc9300eSEric Dumazet } 6150a9627f2STom Herbert free_cpumask_var(mask); 6160a9627f2STom Herbert return len; 6170a9627f2STom Herbert } 6180a9627f2STom Herbert 619fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 620fec5e652STom Herbert struct rx_queue_attribute *attr, 621fec5e652STom Herbert char *buf) 622fec5e652STom Herbert { 623fec5e652STom Herbert struct rps_dev_flow_table *flow_table; 62460b778ceSEric Dumazet unsigned long val = 0; 625fec5e652STom Herbert 626fec5e652STom Herbert rcu_read_lock(); 627fec5e652STom Herbert flow_table = rcu_dereference(queue->rps_flow_table); 628fec5e652STom Herbert if (flow_table) 62960b778ceSEric Dumazet val = (unsigned long)flow_table->mask + 1; 630fec5e652STom Herbert rcu_read_unlock(); 631fec5e652STom Herbert 63260b778ceSEric Dumazet return sprintf(buf, "%lu\n", val); 633fec5e652STom Herbert } 634fec5e652STom Herbert 635fec5e652STom Herbert static void rps_dev_flow_table_release_work(struct work_struct *work) 636fec5e652STom Herbert { 637fec5e652STom Herbert struct rps_dev_flow_table *table = container_of(work, 638fec5e652STom Herbert struct rps_dev_flow_table, free_work); 639fec5e652STom Herbert 640fec5e652STom Herbert vfree(table); 641fec5e652STom Herbert } 642fec5e652STom Herbert 643fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu) 644fec5e652STom Herbert { 645fec5e652STom Herbert struct rps_dev_flow_table *table = container_of(rcu, 646fec5e652STom Herbert struct rps_dev_flow_table, rcu); 647fec5e652STom Herbert 648fec5e652STom Herbert INIT_WORK(&table->free_work, rps_dev_flow_table_release_work); 649fec5e652STom Herbert schedule_work(&table->free_work); 650fec5e652STom Herbert } 651fec5e652STom Herbert 652f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 653fec5e652STom Herbert struct rx_queue_attribute *attr, 654fec5e652STom Herbert const char *buf, size_t len) 655fec5e652STom Herbert { 65660b778ceSEric Dumazet unsigned long mask, count; 657fec5e652STom Herbert struct rps_dev_flow_table *table, *old_table; 658fec5e652STom Herbert static DEFINE_SPINLOCK(rps_dev_flow_lock); 65960b778ceSEric Dumazet int rc; 660fec5e652STom Herbert 661fec5e652STom Herbert if (!capable(CAP_NET_ADMIN)) 662fec5e652STom Herbert return -EPERM; 663fec5e652STom Herbert 66460b778ceSEric Dumazet rc = kstrtoul(buf, 0, &count); 66560b778ceSEric Dumazet if (rc < 0) 66660b778ceSEric Dumazet return rc; 667fec5e652STom Herbert 668fec5e652STom Herbert if (count) { 66960b778ceSEric Dumazet mask = count - 1; 67060b778ceSEric Dumazet /* mask = roundup_pow_of_two(count) - 1; 67160b778ceSEric Dumazet * without overflows... 67260b778ceSEric Dumazet */ 67360b778ceSEric Dumazet while ((mask | (mask >> 1)) != mask) 67460b778ceSEric Dumazet mask |= (mask >> 1); 67560b778ceSEric Dumazet /* On 64 bit arches, must check mask fits in table->mask (u32), 67660b778ceSEric Dumazet * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1) 67760b778ceSEric Dumazet * doesnt overflow. 67860b778ceSEric Dumazet */ 67960b778ceSEric Dumazet #if BITS_PER_LONG > 32 68060b778ceSEric Dumazet if (mask > (unsigned long)(u32)mask) 681a0a129f8SXi Wang return -EINVAL; 68260b778ceSEric Dumazet #else 68360b778ceSEric Dumazet if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) 684a0a129f8SXi Wang / sizeof(struct rps_dev_flow)) { 685fec5e652STom Herbert /* Enforce a limit to prevent overflow */ 686fec5e652STom Herbert return -EINVAL; 687fec5e652STom Herbert } 68860b778ceSEric Dumazet #endif 68960b778ceSEric Dumazet table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); 690fec5e652STom Herbert if (!table) 691fec5e652STom Herbert return -ENOMEM; 692fec5e652STom Herbert 69360b778ceSEric Dumazet table->mask = mask; 69460b778ceSEric Dumazet for (count = 0; count <= mask; count++) 69560b778ceSEric Dumazet table->flows[count].cpu = RPS_NO_CPU; 696fec5e652STom Herbert } else 697fec5e652STom Herbert table = NULL; 698fec5e652STom Herbert 699fec5e652STom Herbert spin_lock(&rps_dev_flow_lock); 7006e3f7fafSEric Dumazet old_table = rcu_dereference_protected(queue->rps_flow_table, 7016e3f7fafSEric Dumazet lockdep_is_held(&rps_dev_flow_lock)); 702fec5e652STom Herbert rcu_assign_pointer(queue->rps_flow_table, table); 703fec5e652STom Herbert spin_unlock(&rps_dev_flow_lock); 704fec5e652STom Herbert 705fec5e652STom Herbert if (old_table) 706fec5e652STom Herbert call_rcu(&old_table->rcu, rps_dev_flow_table_release); 707fec5e652STom Herbert 708fec5e652STom Herbert return len; 709fec5e652STom Herbert } 710fec5e652STom Herbert 7110a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute = 7120a9627f2STom Herbert __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); 7130a9627f2STom Herbert 714fec5e652STom Herbert 715fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = 716fec5e652STom Herbert __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, 717fec5e652STom Herbert show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); 718fec5e652STom Herbert 7190a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = { 7200a9627f2STom Herbert &rps_cpus_attribute.attr, 721fec5e652STom Herbert &rps_dev_flow_table_cnt_attribute.attr, 7220a9627f2STom Herbert NULL 7230a9627f2STom Herbert }; 7240a9627f2STom Herbert 7250a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj) 7260a9627f2STom Herbert { 7270a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 7286e3f7fafSEric Dumazet struct rps_map *map; 7296e3f7fafSEric Dumazet struct rps_dev_flow_table *flow_table; 7300a9627f2STom Herbert 731fec5e652STom Herbert 73233d480ceSEric Dumazet map = rcu_dereference_protected(queue->rps_map, 1); 7339ea19481SJohn Fastabend if (map) { 7349ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_map, NULL); 735f6f80238SLai Jiangshan kfree_rcu(map, rcu); 7369ea19481SJohn Fastabend } 7376e3f7fafSEric Dumazet 73833d480ceSEric Dumazet flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); 7399ea19481SJohn Fastabend if (flow_table) { 7409ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_flow_table, NULL); 7416e3f7fafSEric Dumazet call_rcu(&flow_table->rcu, rps_dev_flow_table_release); 7429ea19481SJohn Fastabend } 7430a9627f2STom Herbert 7449ea19481SJohn Fastabend memset(kobj, 0, sizeof(*kobj)); 745fe822240STom Herbert dev_put(queue->dev); 7460a9627f2STom Herbert } 7470a9627f2STom Herbert 7480a9627f2STom Herbert static struct kobj_type rx_queue_ktype = { 7490a9627f2STom Herbert .sysfs_ops = &rx_queue_sysfs_ops, 7500a9627f2STom Herbert .release = rx_queue_release, 7510a9627f2STom Herbert .default_attrs = rx_queue_default_attrs, 7520a9627f2STom Herbert }; 7530a9627f2STom Herbert 7540a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index) 7550a9627f2STom Herbert { 7560a9627f2STom Herbert struct netdev_rx_queue *queue = net->_rx + index; 7570a9627f2STom Herbert struct kobject *kobj = &queue->kobj; 7580a9627f2STom Herbert int error = 0; 7590a9627f2STom Herbert 7600a9627f2STom Herbert kobj->kset = net->queues_kset; 7610a9627f2STom Herbert error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, 7620a9627f2STom Herbert "rx-%u", index); 7630a9627f2STom Herbert if (error) { 7640a9627f2STom Herbert kobject_put(kobj); 7650a9627f2STom Herbert return error; 7660a9627f2STom Herbert } 7670a9627f2STom Herbert 7680a9627f2STom Herbert kobject_uevent(kobj, KOBJ_ADD); 769fe822240STom Herbert dev_hold(queue->dev); 7700a9627f2STom Herbert 7710a9627f2STom Herbert return error; 7720a9627f2STom Herbert } 773bf264145STom Herbert #endif /* CONFIG_RPS */ 7740a9627f2STom Herbert 77562fe0b40SBen Hutchings int 77662fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 7770a9627f2STom Herbert { 778bf264145STom Herbert #ifdef CONFIG_RPS 7790a9627f2STom Herbert int i; 7800a9627f2STom Herbert int error = 0; 7810a9627f2STom Herbert 78262fe0b40SBen Hutchings for (i = old_num; i < new_num; i++) { 7830a9627f2STom Herbert error = rx_queue_add_kobject(net, i); 78462fe0b40SBen Hutchings if (error) { 78562fe0b40SBen Hutchings new_num = old_num; 7860a9627f2STom Herbert break; 7870a9627f2STom Herbert } 78862fe0b40SBen Hutchings } 7890a9627f2STom Herbert 79062fe0b40SBen Hutchings while (--i >= new_num) 7910a9627f2STom Herbert kobject_put(&net->_rx[i].kobj); 7920a9627f2STom Herbert 7930a9627f2STom Herbert return error; 794bf264145STom Herbert #else 795bf264145STom Herbert return 0; 796bf264145STom Herbert #endif 7970a9627f2STom Herbert } 7980a9627f2STom Herbert 799ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 8001d24eb48STom Herbert /* 8011d24eb48STom Herbert * netdev_queue sysfs structures and functions. 8021d24eb48STom Herbert */ 8031d24eb48STom Herbert struct netdev_queue_attribute { 8041d24eb48STom Herbert struct attribute attr; 8051d24eb48STom Herbert ssize_t (*show)(struct netdev_queue *queue, 8061d24eb48STom Herbert struct netdev_queue_attribute *attr, char *buf); 8071d24eb48STom Herbert ssize_t (*store)(struct netdev_queue *queue, 8081d24eb48STom Herbert struct netdev_queue_attribute *attr, const char *buf, size_t len); 8091d24eb48STom Herbert }; 8101d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr, \ 8111d24eb48STom Herbert struct netdev_queue_attribute, attr) 8121d24eb48STom Herbert 8131d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) 8141d24eb48STom Herbert 8151d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj, 8161d24eb48STom Herbert struct attribute *attr, char *buf) 81762fe0b40SBen Hutchings { 8181d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8191d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8201d24eb48STom Herbert 8211d24eb48STom Herbert if (!attribute->show) 8221d24eb48STom Herbert return -EIO; 8231d24eb48STom Herbert 8241d24eb48STom Herbert return attribute->show(queue, attribute, buf); 8251d24eb48STom Herbert } 8261d24eb48STom Herbert 8271d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj, 8281d24eb48STom Herbert struct attribute *attr, 8291d24eb48STom Herbert const char *buf, size_t count) 8301d24eb48STom Herbert { 8311d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8321d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8331d24eb48STom Herbert 8341d24eb48STom Herbert if (!attribute->store) 8351d24eb48STom Herbert return -EIO; 8361d24eb48STom Herbert 8371d24eb48STom Herbert return attribute->store(queue, attribute, buf, count); 8381d24eb48STom Herbert } 8391d24eb48STom Herbert 8401d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = { 8411d24eb48STom Herbert .show = netdev_queue_attr_show, 8421d24eb48STom Herbert .store = netdev_queue_attr_store, 8431d24eb48STom Herbert }; 8441d24eb48STom Herbert 845ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue, 846ccf5ff69Sdavid decotigny struct netdev_queue_attribute *attribute, 847ccf5ff69Sdavid decotigny char *buf) 848ccf5ff69Sdavid decotigny { 849ccf5ff69Sdavid decotigny unsigned long trans_timeout; 850ccf5ff69Sdavid decotigny 851ccf5ff69Sdavid decotigny spin_lock_irq(&queue->_xmit_lock); 852ccf5ff69Sdavid decotigny trans_timeout = queue->trans_timeout; 853ccf5ff69Sdavid decotigny spin_unlock_irq(&queue->_xmit_lock); 854ccf5ff69Sdavid decotigny 855ccf5ff69Sdavid decotigny return sprintf(buf, "%lu", trans_timeout); 856ccf5ff69Sdavid decotigny } 857ccf5ff69Sdavid decotigny 858ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout = 859ccf5ff69Sdavid decotigny __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); 860ccf5ff69Sdavid decotigny 861114cf580STom Herbert #ifdef CONFIG_BQL 862114cf580STom Herbert /* 863114cf580STom Herbert * Byte queue limits sysfs structures and functions. 864114cf580STom Herbert */ 865114cf580STom Herbert static ssize_t bql_show(char *buf, unsigned int value) 866114cf580STom Herbert { 867114cf580STom Herbert return sprintf(buf, "%u\n", value); 868114cf580STom Herbert } 869114cf580STom Herbert 870114cf580STom Herbert static ssize_t bql_set(const char *buf, const size_t count, 871114cf580STom Herbert unsigned int *pvalue) 872114cf580STom Herbert { 873114cf580STom Herbert unsigned int value; 874114cf580STom Herbert int err; 875114cf580STom Herbert 876114cf580STom Herbert if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) 877114cf580STom Herbert value = DQL_MAX_LIMIT; 878114cf580STom Herbert else { 879114cf580STom Herbert err = kstrtouint(buf, 10, &value); 880114cf580STom Herbert if (err < 0) 881114cf580STom Herbert return err; 882114cf580STom Herbert if (value > DQL_MAX_LIMIT) 883114cf580STom Herbert return -EINVAL; 884114cf580STom Herbert } 885114cf580STom Herbert 886114cf580STom Herbert *pvalue = value; 887114cf580STom Herbert 888114cf580STom Herbert return count; 889114cf580STom Herbert } 890114cf580STom Herbert 891114cf580STom Herbert static ssize_t bql_show_hold_time(struct netdev_queue *queue, 892114cf580STom Herbert struct netdev_queue_attribute *attr, 893114cf580STom Herbert char *buf) 894114cf580STom Herbert { 895114cf580STom Herbert struct dql *dql = &queue->dql; 896114cf580STom Herbert 897114cf580STom Herbert return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); 898114cf580STom Herbert } 899114cf580STom Herbert 900114cf580STom Herbert static ssize_t bql_set_hold_time(struct netdev_queue *queue, 901114cf580STom Herbert struct netdev_queue_attribute *attribute, 902114cf580STom Herbert const char *buf, size_t len) 903114cf580STom Herbert { 904114cf580STom Herbert struct dql *dql = &queue->dql; 905114cf580STom Herbert unsigned value; 906114cf580STom Herbert int err; 907114cf580STom Herbert 908114cf580STom Herbert err = kstrtouint(buf, 10, &value); 909114cf580STom Herbert if (err < 0) 910114cf580STom Herbert return err; 911114cf580STom Herbert 912114cf580STom Herbert dql->slack_hold_time = msecs_to_jiffies(value); 913114cf580STom Herbert 914114cf580STom Herbert return len; 915114cf580STom Herbert } 916114cf580STom Herbert 917114cf580STom Herbert static struct netdev_queue_attribute bql_hold_time_attribute = 918114cf580STom Herbert __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time, 919114cf580STom Herbert bql_set_hold_time); 920114cf580STom Herbert 921114cf580STom Herbert static ssize_t bql_show_inflight(struct netdev_queue *queue, 922114cf580STom Herbert struct netdev_queue_attribute *attr, 923114cf580STom Herbert char *buf) 924114cf580STom Herbert { 925114cf580STom Herbert struct dql *dql = &queue->dql; 926114cf580STom Herbert 927114cf580STom Herbert return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); 928114cf580STom Herbert } 929114cf580STom Herbert 930114cf580STom Herbert static struct netdev_queue_attribute bql_inflight_attribute = 931795d9a25SHiroaki SHIMODA __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL); 932114cf580STom Herbert 933114cf580STom Herbert #define BQL_ATTR(NAME, FIELD) \ 934114cf580STom Herbert static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ 935114cf580STom Herbert struct netdev_queue_attribute *attr, \ 936114cf580STom Herbert char *buf) \ 937114cf580STom Herbert { \ 938114cf580STom Herbert return bql_show(buf, queue->dql.FIELD); \ 939114cf580STom Herbert } \ 940114cf580STom Herbert \ 941114cf580STom Herbert static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ 942114cf580STom Herbert struct netdev_queue_attribute *attr, \ 943114cf580STom Herbert const char *buf, size_t len) \ 944114cf580STom Herbert { \ 945114cf580STom Herbert return bql_set(buf, len, &queue->dql.FIELD); \ 946114cf580STom Herbert } \ 947114cf580STom Herbert \ 948114cf580STom Herbert static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \ 949114cf580STom Herbert __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \ 950114cf580STom Herbert bql_set_ ## NAME); 951114cf580STom Herbert 952114cf580STom Herbert BQL_ATTR(limit, limit) 953114cf580STom Herbert BQL_ATTR(limit_max, max_limit) 954114cf580STom Herbert BQL_ATTR(limit_min, min_limit) 955114cf580STom Herbert 956114cf580STom Herbert static struct attribute *dql_attrs[] = { 957114cf580STom Herbert &bql_limit_attribute.attr, 958114cf580STom Herbert &bql_limit_max_attribute.attr, 959114cf580STom Herbert &bql_limit_min_attribute.attr, 960114cf580STom Herbert &bql_hold_time_attribute.attr, 961114cf580STom Herbert &bql_inflight_attribute.attr, 962114cf580STom Herbert NULL 963114cf580STom Herbert }; 964114cf580STom Herbert 965114cf580STom Herbert static struct attribute_group dql_group = { 966114cf580STom Herbert .name = "byte_queue_limits", 967114cf580STom Herbert .attrs = dql_attrs, 968114cf580STom Herbert }; 969114cf580STom Herbert #endif /* CONFIG_BQL */ 970114cf580STom Herbert 971ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 9721d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue) 9731d24eb48STom Herbert { 9741d24eb48STom Herbert struct net_device *dev = queue->dev; 9751d24eb48STom Herbert int i; 9761d24eb48STom Herbert 9771d24eb48STom Herbert for (i = 0; i < dev->num_tx_queues; i++) 9781d24eb48STom Herbert if (queue == &dev->_tx[i]) 9791d24eb48STom Herbert break; 9801d24eb48STom Herbert 9811d24eb48STom Herbert BUG_ON(i >= dev->num_tx_queues); 9821d24eb48STom Herbert 9831d24eb48STom Herbert return i; 9841d24eb48STom Herbert } 9851d24eb48STom Herbert 9861d24eb48STom Herbert 9871d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue, 9881d24eb48STom Herbert struct netdev_queue_attribute *attribute, char *buf) 9891d24eb48STom Herbert { 9901d24eb48STom Herbert struct net_device *dev = queue->dev; 9911d24eb48STom Herbert struct xps_dev_maps *dev_maps; 9921d24eb48STom Herbert cpumask_var_t mask; 9931d24eb48STom Herbert unsigned long index; 9941d24eb48STom Herbert size_t len = 0; 9951d24eb48STom Herbert int i; 9961d24eb48STom Herbert 9971d24eb48STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 9981d24eb48STom Herbert return -ENOMEM; 9991d24eb48STom Herbert 10001d24eb48STom Herbert index = get_netdev_queue_index(queue); 10011d24eb48STom Herbert 10021d24eb48STom Herbert rcu_read_lock(); 10031d24eb48STom Herbert dev_maps = rcu_dereference(dev->xps_maps); 10041d24eb48STom Herbert if (dev_maps) { 10051d24eb48STom Herbert for_each_possible_cpu(i) { 10061d24eb48STom Herbert struct xps_map *map = 10071d24eb48STom Herbert rcu_dereference(dev_maps->cpu_map[i]); 10081d24eb48STom Herbert if (map) { 10091d24eb48STom Herbert int j; 10101d24eb48STom Herbert for (j = 0; j < map->len; j++) { 10111d24eb48STom Herbert if (map->queues[j] == index) { 10121d24eb48STom Herbert cpumask_set_cpu(i, mask); 10131d24eb48STom Herbert break; 10141d24eb48STom Herbert } 10151d24eb48STom Herbert } 10161d24eb48STom Herbert } 10171d24eb48STom Herbert } 10181d24eb48STom Herbert } 10191d24eb48STom Herbert rcu_read_unlock(); 10201d24eb48STom Herbert 10211d24eb48STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 10221d24eb48STom Herbert if (PAGE_SIZE - len < 3) { 10231d24eb48STom Herbert free_cpumask_var(mask); 10241d24eb48STom Herbert return -EINVAL; 10251d24eb48STom Herbert } 10261d24eb48STom Herbert 10271d24eb48STom Herbert free_cpumask_var(mask); 10281d24eb48STom Herbert len += sprintf(buf + len, "\n"); 10291d24eb48STom Herbert return len; 10301d24eb48STom Herbert } 10311d24eb48STom Herbert 10321d24eb48STom Herbert static DEFINE_MUTEX(xps_map_mutex); 1033a4177869SEric Dumazet #define xmap_dereference(P) \ 1034a4177869SEric Dumazet rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) 10351d24eb48STom Herbert 1036927fbec1STom Herbert static void xps_queue_release(struct netdev_queue *queue) 1037927fbec1STom Herbert { 1038927fbec1STom Herbert struct net_device *dev = queue->dev; 1039927fbec1STom Herbert struct xps_dev_maps *dev_maps; 1040927fbec1STom Herbert struct xps_map *map; 1041927fbec1STom Herbert unsigned long index; 1042927fbec1STom Herbert int i, pos, nonempty = 0; 1043927fbec1STom Herbert 1044927fbec1STom Herbert index = get_netdev_queue_index(queue); 1045927fbec1STom Herbert 1046927fbec1STom Herbert mutex_lock(&xps_map_mutex); 1047927fbec1STom Herbert dev_maps = xmap_dereference(dev->xps_maps); 1048927fbec1STom Herbert 1049927fbec1STom Herbert if (dev_maps) { 1050927fbec1STom Herbert for_each_possible_cpu(i) { 1051927fbec1STom Herbert map = xmap_dereference(dev_maps->cpu_map[i]); 1052927fbec1STom Herbert if (!map) 1053927fbec1STom Herbert continue; 1054927fbec1STom Herbert 1055927fbec1STom Herbert for (pos = 0; pos < map->len; pos++) 1056927fbec1STom Herbert if (map->queues[pos] == index) 1057927fbec1STom Herbert break; 1058927fbec1STom Herbert 1059927fbec1STom Herbert if (pos < map->len) { 1060927fbec1STom Herbert if (map->len > 1) 1061927fbec1STom Herbert map->queues[pos] = 1062927fbec1STom Herbert map->queues[--map->len]; 1063927fbec1STom Herbert else { 1064927fbec1STom Herbert RCU_INIT_POINTER(dev_maps->cpu_map[i], 1065927fbec1STom Herbert NULL); 1066927fbec1STom Herbert kfree_rcu(map, rcu); 1067927fbec1STom Herbert map = NULL; 1068927fbec1STom Herbert } 1069927fbec1STom Herbert } 1070927fbec1STom Herbert if (map) 1071927fbec1STom Herbert nonempty = 1; 1072927fbec1STom Herbert } 1073927fbec1STom Herbert 1074927fbec1STom Herbert if (!nonempty) { 1075927fbec1STom Herbert RCU_INIT_POINTER(dev->xps_maps, NULL); 1076927fbec1STom Herbert kfree_rcu(dev_maps, rcu); 1077927fbec1STom Herbert } 1078927fbec1STom Herbert } 1079927fbec1STom Herbert mutex_unlock(&xps_map_mutex); 1080927fbec1STom Herbert } 1081927fbec1STom Herbert 10821d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue, 10831d24eb48STom Herbert struct netdev_queue_attribute *attribute, 10841d24eb48STom Herbert const char *buf, size_t len) 10851d24eb48STom Herbert { 10861d24eb48STom Herbert struct net_device *dev = queue->dev; 10871d24eb48STom Herbert cpumask_var_t mask; 10881d24eb48STom Herbert int err, i, cpu, pos, map_len, alloc_len, need_set; 10891d24eb48STom Herbert unsigned long index; 10901d24eb48STom Herbert struct xps_map *map, *new_map; 10911d24eb48STom Herbert struct xps_dev_maps *dev_maps, *new_dev_maps; 10921d24eb48STom Herbert int nonempty = 0; 109319b05f81Sdavid decotigny int numa_node_id = -2; 10941d24eb48STom Herbert 10951d24eb48STom Herbert if (!capable(CAP_NET_ADMIN)) 10961d24eb48STom Herbert return -EPERM; 10971d24eb48STom Herbert 10981d24eb48STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 10991d24eb48STom Herbert return -ENOMEM; 11001d24eb48STom Herbert 11011d24eb48STom Herbert index = get_netdev_queue_index(queue); 11021d24eb48STom Herbert 11031d24eb48STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 11041d24eb48STom Herbert if (err) { 11051d24eb48STom Herbert free_cpumask_var(mask); 11061d24eb48STom Herbert return err; 11071d24eb48STom Herbert } 11081d24eb48STom Herbert 11091d24eb48STom Herbert new_dev_maps = kzalloc(max_t(unsigned, 11101d24eb48STom Herbert XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL); 11111d24eb48STom Herbert if (!new_dev_maps) { 11121d24eb48STom Herbert free_cpumask_var(mask); 11131d24eb48STom Herbert return -ENOMEM; 11141d24eb48STom Herbert } 11151d24eb48STom Herbert 11161d24eb48STom Herbert mutex_lock(&xps_map_mutex); 11171d24eb48STom Herbert 1118a4177869SEric Dumazet dev_maps = xmap_dereference(dev->xps_maps); 11191d24eb48STom Herbert 11201d24eb48STom Herbert for_each_possible_cpu(cpu) { 1121a4177869SEric Dumazet map = dev_maps ? 1122a4177869SEric Dumazet xmap_dereference(dev_maps->cpu_map[cpu]) : NULL; 1123a4177869SEric Dumazet new_map = map; 11241d24eb48STom Herbert if (map) { 11251d24eb48STom Herbert for (pos = 0; pos < map->len; pos++) 11261d24eb48STom Herbert if (map->queues[pos] == index) 11271d24eb48STom Herbert break; 11281d24eb48STom Herbert map_len = map->len; 11291d24eb48STom Herbert alloc_len = map->alloc_len; 11301d24eb48STom Herbert } else 11311d24eb48STom Herbert pos = map_len = alloc_len = 0; 11321d24eb48STom Herbert 11332142c131SKOSAKI Motohiro need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu); 1134f2cd2d3eSEric Dumazet #ifdef CONFIG_NUMA 1135f2cd2d3eSEric Dumazet if (need_set) { 113619b05f81Sdavid decotigny if (numa_node_id == -2) 113719b05f81Sdavid decotigny numa_node_id = cpu_to_node(cpu); 113819b05f81Sdavid decotigny else if (numa_node_id != cpu_to_node(cpu)) 113919b05f81Sdavid decotigny numa_node_id = -1; 1140f2cd2d3eSEric Dumazet } 1141f2cd2d3eSEric Dumazet #endif 11421d24eb48STom Herbert if (need_set && pos >= map_len) { 11431d24eb48STom Herbert /* Need to add queue to this CPU's map */ 11441d24eb48STom Herbert if (map_len >= alloc_len) { 11451d24eb48STom Herbert alloc_len = alloc_len ? 11461d24eb48STom Herbert 2 * alloc_len : XPS_MIN_MAP_ALLOC; 1147b02038a1SEric Dumazet new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), 1148b02038a1SEric Dumazet GFP_KERNEL, 1149b02038a1SEric Dumazet cpu_to_node(cpu)); 11501d24eb48STom Herbert if (!new_map) 11511d24eb48STom Herbert goto error; 11521d24eb48STom Herbert new_map->alloc_len = alloc_len; 11531d24eb48STom Herbert for (i = 0; i < map_len; i++) 11541d24eb48STom Herbert new_map->queues[i] = map->queues[i]; 11551d24eb48STom Herbert new_map->len = map_len; 11561d24eb48STom Herbert } 11571d24eb48STom Herbert new_map->queues[new_map->len++] = index; 11581d24eb48STom Herbert } else if (!need_set && pos < map_len) { 11591d24eb48STom Herbert /* Need to remove queue from this CPU's map */ 11601d24eb48STom Herbert if (map_len > 1) 11611d24eb48STom Herbert new_map->queues[pos] = 11621d24eb48STom Herbert new_map->queues[--new_map->len]; 11631d24eb48STom Herbert else 11641d24eb48STom Herbert new_map = NULL; 11651d24eb48STom Herbert } 1166a4177869SEric Dumazet RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map); 11671d24eb48STom Herbert } 11681d24eb48STom Herbert 11691d24eb48STom Herbert /* Cleanup old maps */ 11701d24eb48STom Herbert for_each_possible_cpu(cpu) { 1171a4177869SEric Dumazet map = dev_maps ? 1172a4177869SEric Dumazet xmap_dereference(dev_maps->cpu_map[cpu]) : NULL; 1173a4177869SEric Dumazet if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map) 1174edc86d8aSLai Jiangshan kfree_rcu(map, rcu); 11751d24eb48STom Herbert if (new_dev_maps->cpu_map[cpu]) 11761d24eb48STom Herbert nonempty = 1; 11771d24eb48STom Herbert } 11781d24eb48STom Herbert 1179cf778b00SEric Dumazet if (nonempty) { 1180cf778b00SEric Dumazet rcu_assign_pointer(dev->xps_maps, new_dev_maps); 1181cf778b00SEric Dumazet } else { 11821d24eb48STom Herbert kfree(new_dev_maps); 1183a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->xps_maps, NULL); 11841d24eb48STom Herbert } 11851d24eb48STom Herbert 11861d24eb48STom Herbert if (dev_maps) 1187b55071ebSLai Jiangshan kfree_rcu(dev_maps, rcu); 11881d24eb48STom Herbert 118919b05f81Sdavid decotigny netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id : 1190b236da69SChangli Gao NUMA_NO_NODE); 1191f2cd2d3eSEric Dumazet 11921d24eb48STom Herbert mutex_unlock(&xps_map_mutex); 11931d24eb48STom Herbert 11941d24eb48STom Herbert free_cpumask_var(mask); 11951d24eb48STom Herbert return len; 11961d24eb48STom Herbert 11971d24eb48STom Herbert error: 11981d24eb48STom Herbert mutex_unlock(&xps_map_mutex); 11991d24eb48STom Herbert 12001d24eb48STom Herbert if (new_dev_maps) 12011d24eb48STom Herbert for_each_possible_cpu(i) 1202a4177869SEric Dumazet kfree(rcu_dereference_protected( 1203a4177869SEric Dumazet new_dev_maps->cpu_map[i], 1204a4177869SEric Dumazet 1)); 12051d24eb48STom Herbert kfree(new_dev_maps); 12061d24eb48STom Herbert free_cpumask_var(mask); 12071d24eb48STom Herbert return -ENOMEM; 12081d24eb48STom Herbert } 12091d24eb48STom Herbert 12101d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute = 12111d24eb48STom Herbert __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map); 1212ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */ 12131d24eb48STom Herbert 12141d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = { 1215ccf5ff69Sdavid decotigny &queue_trans_timeout.attr, 1216ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 12171d24eb48STom Herbert &xps_cpus_attribute.attr, 1218ccf5ff69Sdavid decotigny #endif 12191d24eb48STom Herbert NULL 12201d24eb48STom Herbert }; 12211d24eb48STom Herbert 12221d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj) 12231d24eb48STom Herbert { 12241d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 12251d24eb48STom Herbert 1226114cf580STom Herbert #ifdef CONFIG_XPS 1227927fbec1STom Herbert xps_queue_release(queue); 1228114cf580STom Herbert #endif 12291d24eb48STom Herbert 12301d24eb48STom Herbert memset(kobj, 0, sizeof(*kobj)); 12311d24eb48STom Herbert dev_put(queue->dev); 12321d24eb48STom Herbert } 12331d24eb48STom Herbert 12341d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = { 12351d24eb48STom Herbert .sysfs_ops = &netdev_queue_sysfs_ops, 12361d24eb48STom Herbert .release = netdev_queue_release, 12371d24eb48STom Herbert .default_attrs = netdev_queue_default_attrs, 12381d24eb48STom Herbert }; 12391d24eb48STom Herbert 12401d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index) 12411d24eb48STom Herbert { 12421d24eb48STom Herbert struct netdev_queue *queue = net->_tx + index; 12431d24eb48STom Herbert struct kobject *kobj = &queue->kobj; 12441d24eb48STom Herbert int error = 0; 12451d24eb48STom Herbert 12461d24eb48STom Herbert kobj->kset = net->queues_kset; 12471d24eb48STom Herbert error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, 12481d24eb48STom Herbert "tx-%u", index); 1249114cf580STom Herbert if (error) 1250114cf580STom Herbert goto exit; 1251114cf580STom Herbert 1252114cf580STom Herbert #ifdef CONFIG_BQL 1253114cf580STom Herbert error = sysfs_create_group(kobj, &dql_group); 1254114cf580STom Herbert if (error) 1255114cf580STom Herbert goto exit; 1256114cf580STom Herbert #endif 12571d24eb48STom Herbert 12581d24eb48STom Herbert kobject_uevent(kobj, KOBJ_ADD); 12591d24eb48STom Herbert dev_hold(queue->dev); 12601d24eb48STom Herbert 1261114cf580STom Herbert return 0; 1262114cf580STom Herbert exit: 1263114cf580STom Herbert kobject_put(kobj); 12641d24eb48STom Herbert return error; 12651d24eb48STom Herbert } 1266ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 12671d24eb48STom Herbert 12681d24eb48STom Herbert int 12691d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 12701d24eb48STom Herbert { 1271ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 12721d24eb48STom Herbert int i; 12731d24eb48STom Herbert int error = 0; 12741d24eb48STom Herbert 12751d24eb48STom Herbert for (i = old_num; i < new_num; i++) { 12761d24eb48STom Herbert error = netdev_queue_add_kobject(net, i); 12771d24eb48STom Herbert if (error) { 12781d24eb48STom Herbert new_num = old_num; 12791d24eb48STom Herbert break; 12801d24eb48STom Herbert } 12811d24eb48STom Herbert } 12821d24eb48STom Herbert 1283114cf580STom Herbert while (--i >= new_num) { 1284114cf580STom Herbert struct netdev_queue *queue = net->_tx + i; 1285114cf580STom Herbert 1286114cf580STom Herbert #ifdef CONFIG_BQL 1287114cf580STom Herbert sysfs_remove_group(&queue->kobj, &dql_group); 1288114cf580STom Herbert #endif 1289114cf580STom Herbert kobject_put(&queue->kobj); 1290114cf580STom Herbert } 12911d24eb48STom Herbert 12921d24eb48STom Herbert return error; 1293bf264145STom Herbert #else 1294bf264145STom Herbert return 0; 1295ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 12961d24eb48STom Herbert } 12971d24eb48STom Herbert 12981d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net) 12991d24eb48STom Herbert { 1300bf264145STom Herbert int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; 13011d24eb48STom Herbert 1302ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 130362fe0b40SBen Hutchings net->queues_kset = kset_create_and_add("queues", 130462fe0b40SBen Hutchings NULL, &net->dev.kobj); 130562fe0b40SBen Hutchings if (!net->queues_kset) 130662fe0b40SBen Hutchings return -ENOMEM; 1307bf264145STom Herbert #endif 13081d24eb48STom Herbert 1309bf264145STom Herbert #ifdef CONFIG_RPS 1310bf264145STom Herbert real_rx = net->real_num_rx_queues; 1311bf264145STom Herbert #endif 1312bf264145STom Herbert real_tx = net->real_num_tx_queues; 1313bf264145STom Herbert 1314bf264145STom Herbert error = net_rx_queue_update_kobjects(net, 0, real_rx); 13151d24eb48STom Herbert if (error) 13161d24eb48STom Herbert goto error; 1317bf264145STom Herbert rxq = real_rx; 13181d24eb48STom Herbert 1319bf264145STom Herbert error = netdev_queue_update_kobjects(net, 0, real_tx); 13201d24eb48STom Herbert if (error) 13211d24eb48STom Herbert goto error; 1322bf264145STom Herbert txq = real_tx; 13231d24eb48STom Herbert 13241d24eb48STom Herbert return 0; 13251d24eb48STom Herbert 13261d24eb48STom Herbert error: 13271d24eb48STom Herbert netdev_queue_update_kobjects(net, txq, 0); 13281d24eb48STom Herbert net_rx_queue_update_kobjects(net, rxq, 0); 13291d24eb48STom Herbert return error; 133062fe0b40SBen Hutchings } 133162fe0b40SBen Hutchings 13321d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net) 13330a9627f2STom Herbert { 1334bf264145STom Herbert int real_rx = 0, real_tx = 0; 1335bf264145STom Herbert 1336bf264145STom Herbert #ifdef CONFIG_RPS 1337bf264145STom Herbert real_rx = net->real_num_rx_queues; 1338bf264145STom Herbert #endif 1339bf264145STom Herbert real_tx = net->real_num_tx_queues; 1340bf264145STom Herbert 1341bf264145STom Herbert net_rx_queue_update_kobjects(net, real_rx, 0); 1342bf264145STom Herbert netdev_queue_update_kobjects(net, real_tx, 0); 1343ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 13440a9627f2STom Herbert kset_unregister(net->queues_kset); 1345bf264145STom Herbert #endif 13460a9627f2STom Herbert } 1347608b4b95SEric W. Biederman 1348a685e089SAl Viro static void *net_grab_current_ns(void) 1349608b4b95SEric W. Biederman { 1350a685e089SAl Viro struct net *ns = current->nsproxy->net_ns; 1351a685e089SAl Viro #ifdef CONFIG_NET_NS 1352a685e089SAl Viro if (ns) 1353a685e089SAl Viro atomic_inc(&ns->passive); 1354a685e089SAl Viro #endif 1355a685e089SAl Viro return ns; 1356608b4b95SEric W. Biederman } 1357608b4b95SEric W. Biederman 1358608b4b95SEric W. Biederman static const void *net_initial_ns(void) 1359608b4b95SEric W. Biederman { 1360608b4b95SEric W. Biederman return &init_net; 1361608b4b95SEric W. Biederman } 1362608b4b95SEric W. Biederman 1363608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk) 1364608b4b95SEric W. Biederman { 1365608b4b95SEric W. Biederman return sock_net(sk); 1366608b4b95SEric W. Biederman } 1367608b4b95SEric W. Biederman 136804600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = { 1369608b4b95SEric W. Biederman .type = KOBJ_NS_TYPE_NET, 1370a685e089SAl Viro .grab_current_ns = net_grab_current_ns, 1371608b4b95SEric W. Biederman .netlink_ns = net_netlink_ns, 1372608b4b95SEric W. Biederman .initial_ns = net_initial_ns, 1373a685e089SAl Viro .drop_ns = net_drop_ns, 1374608b4b95SEric W. Biederman }; 137504600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations); 1376608b4b95SEric W. Biederman 13771da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG 13787eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) 13791da177e4SLinus Torvalds { 138043cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 13817eff2e7aSKay Sievers int retval; 13821da177e4SLinus Torvalds 1383312c004dSKay Sievers /* pass interface to uevent. */ 13847eff2e7aSKay Sievers retval = add_uevent_var(env, "INTERFACE=%s", dev->name); 1385bf62456eSEric Rannaud if (retval) 1386bf62456eSEric Rannaud goto exit; 13871da177e4SLinus Torvalds 1388ca2f37dbSJean Tourrilhes /* pass ifindex to uevent. 1389ca2f37dbSJean Tourrilhes * ifindex is useful as it won't change (interface name may change) 1390ca2f37dbSJean Tourrilhes * and is what RtNetlink uses natively. */ 13917eff2e7aSKay Sievers retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); 1392ca2f37dbSJean Tourrilhes 1393bf62456eSEric Rannaud exit: 1394bf62456eSEric Rannaud return retval; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds #endif 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds /* 13991da177e4SLinus Torvalds * netdev_release -- destroy and free a dead device. 140043cb76d9SGreg Kroah-Hartman * Called when last reference to device kobject is gone. 14011da177e4SLinus Torvalds */ 140243cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d) 14031da177e4SLinus Torvalds { 140443cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds BUG_ON(dev->reg_state != NETREG_RELEASED); 14071da177e4SLinus Torvalds 14080b815a1aSStephen Hemminger kfree(dev->ifalias); 14091da177e4SLinus Torvalds kfree((char *)dev - dev->padded); 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 1412608b4b95SEric W. Biederman static const void *net_namespace(struct device *d) 1413608b4b95SEric W. Biederman { 1414608b4b95SEric W. Biederman struct net_device *dev; 1415608b4b95SEric W. Biederman dev = container_of(d, struct net_device, dev); 1416608b4b95SEric W. Biederman return dev_net(dev); 1417608b4b95SEric W. Biederman } 1418608b4b95SEric W. Biederman 14191da177e4SLinus Torvalds static struct class net_class = { 14201da177e4SLinus Torvalds .name = "net", 142143cb76d9SGreg Kroah-Hartman .dev_release = netdev_release, 14228b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 142343cb76d9SGreg Kroah-Hartman .dev_attrs = net_class_attributes, 14248b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */ 14251da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG 142643cb76d9SGreg Kroah-Hartman .dev_uevent = netdev_uevent, 14271da177e4SLinus Torvalds #endif 1428608b4b95SEric W. Biederman .ns_type = &net_ns_type_operations, 1429608b4b95SEric W. Biederman .namespace = net_namespace, 14301da177e4SLinus Torvalds }; 14311da177e4SLinus Torvalds 14329093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all 14339093bbb2SStephen Hemminger * netdev references are gone. 14349093bbb2SStephen Hemminger */ 14358b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net) 14361da177e4SLinus Torvalds { 14379093bbb2SStephen Hemminger struct device *dev = &(net->dev); 14389093bbb2SStephen Hemminger 14399093bbb2SStephen Hemminger kobject_get(&dev->kobj); 14403891845eSEric W. Biederman 14411d24eb48STom Herbert remove_queue_kobjects(net); 14420a9627f2STom Herbert 14439093bbb2SStephen Hemminger device_del(dev); 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds /* Create sysfs entries for network device. */ 14478b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net) 14481da177e4SLinus Torvalds { 144943cb76d9SGreg Kroah-Hartman struct device *dev = &(net->dev); 1450a4dbd674SDavid Brownell const struct attribute_group **groups = net->sysfs_groups; 14510a9627f2STom Herbert int error = 0; 14521da177e4SLinus Torvalds 1453a1b3f594SEric W. Biederman device_initialize(dev); 145443cb76d9SGreg Kroah-Hartman dev->class = &net_class; 145543cb76d9SGreg Kroah-Hartman dev->platform_data = net; 145643cb76d9SGreg Kroah-Hartman dev->groups = groups; 14571da177e4SLinus Torvalds 1458a2205472SStephen Hemminger dev_set_name(dev, "%s", net->name); 14591da177e4SLinus Torvalds 14608b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 14610c509a6cSEric W. Biederman /* Allow for a device specific group */ 14620c509a6cSEric W. Biederman if (*groups) 14630c509a6cSEric W. Biederman groups++; 14641da177e4SLinus Torvalds 14650c509a6cSEric W. Biederman *groups++ = &netstat_group; 146622bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS 14673d23e349SJohannes Berg if (net->ieee80211_ptr) 1468fe9925b5SStephen Hemminger *groups++ = &wireless_group; 14693d23e349SJohannes Berg #ifdef CONFIG_WIRELESS_EXT 14703d23e349SJohannes Berg else if (net->wireless_handlers) 14713d23e349SJohannes Berg *groups++ = &wireless_group; 14723d23e349SJohannes Berg #endif 14731da177e4SLinus Torvalds #endif 14748b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */ 14751da177e4SLinus Torvalds 14760a9627f2STom Herbert error = device_add(dev); 14770a9627f2STom Herbert if (error) 14780a9627f2STom Herbert return error; 14790a9627f2STom Herbert 14801d24eb48STom Herbert error = register_queue_kobjects(net); 14810a9627f2STom Herbert if (error) { 14820a9627f2STom Herbert device_del(dev); 14830a9627f2STom Herbert return error; 14840a9627f2STom Herbert } 14850a9627f2STom Herbert 14860a9627f2STom Herbert return error; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 1489b8a9787eSJay Vosburgh int netdev_class_create_file(struct class_attribute *class_attr) 1490b8a9787eSJay Vosburgh { 1491b8a9787eSJay Vosburgh return class_create_file(&net_class, class_attr); 1492b8a9787eSJay Vosburgh } 14939e34a5b5SEric Dumazet EXPORT_SYMBOL(netdev_class_create_file); 1494b8a9787eSJay Vosburgh 1495b8a9787eSJay Vosburgh void netdev_class_remove_file(struct class_attribute *class_attr) 1496b8a9787eSJay Vosburgh { 1497b8a9787eSJay Vosburgh class_remove_file(&net_class, class_attr); 1498b8a9787eSJay Vosburgh } 1499b8a9787eSJay Vosburgh EXPORT_SYMBOL(netdev_class_remove_file); 1500b8a9787eSJay Vosburgh 15018b41d188SEric W. Biederman int netdev_kobject_init(void) 15021da177e4SLinus Torvalds { 1503608b4b95SEric W. Biederman kobj_ns_type_register(&net_ns_type_operations); 15041da177e4SLinus Torvalds return class_register(&net_class); 15051da177e4SLinus Torvalds } 1506