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 char *endp; 781da177e4SLinus Torvalds unsigned long new; 791da177e4SLinus Torvalds int ret = -EINVAL; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 821da177e4SLinus Torvalds return -EPERM; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds new = simple_strtoul(buf, &endp, 0); 851da177e4SLinus Torvalds if (endp == buf) 861da177e4SLinus Torvalds goto err; 871da177e4SLinus Torvalds 885a5990d3SStephen Hemminger if (!rtnl_trylock()) 89336ca57cSEric W. Biederman return restart_syscall(); 905a5990d3SStephen Hemminger 911da177e4SLinus Torvalds if (dev_isalive(net)) { 921da177e4SLinus Torvalds if ((ret = (*set)(net, new)) == 0) 931da177e4SLinus Torvalds ret = len; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds rtnl_unlock(); 961da177e4SLinus Torvalds err: 971da177e4SLinus Torvalds return ret; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1009d29672cSDavid Woodhouse NETDEVICE_SHOW(dev_id, fmt_hex); 101c1f79426SStefan Assmann NETDEVICE_SHOW(addr_assign_type, fmt_dec); 102fd586bacSKay Sievers NETDEVICE_SHOW(addr_len, fmt_dec); 103fd586bacSKay Sievers NETDEVICE_SHOW(iflink, fmt_dec); 104fd586bacSKay Sievers NETDEVICE_SHOW(ifindex, fmt_dec); 105fd586bacSKay Sievers NETDEVICE_SHOW(type, fmt_dec); 106b00055aaSStefan Rompf NETDEVICE_SHOW(link_mode, fmt_dec); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* use same locking rules as GIFHWADDR ioctl's */ 10943cb76d9SGreg Kroah-Hartman static ssize_t show_address(struct device *dev, struct device_attribute *attr, 11043cb76d9SGreg Kroah-Hartman char *buf) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1131da177e4SLinus Torvalds ssize_t ret = -EINVAL; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds read_lock(&dev_base_lock); 1161da177e4SLinus Torvalds if (dev_isalive(net)) 1177ffc49a6SMichael Chan ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); 1181da177e4SLinus Torvalds read_unlock(&dev_base_lock); 1191da177e4SLinus Torvalds return ret; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 12243cb76d9SGreg Kroah-Hartman static ssize_t show_broadcast(struct device *dev, 12343cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1261da177e4SLinus Torvalds if (dev_isalive(net)) 1277ffc49a6SMichael Chan return sysfs_format_mac(buf, net->broadcast, net->addr_len); 1281da177e4SLinus Torvalds return -EINVAL; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 13143cb76d9SGreg Kroah-Hartman static ssize_t show_carrier(struct device *dev, 13243cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct net_device *netdev = to_net_dev(dev); 1351da177e4SLinus Torvalds if (netif_running(netdev)) { 1361da177e4SLinus Torvalds return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds return -EINVAL; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 141d519e17eSAndy Gospodarek static ssize_t show_speed(struct device *dev, 142d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 143d519e17eSAndy Gospodarek { 144d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 145d519e17eSAndy Gospodarek int ret = -EINVAL; 146d519e17eSAndy Gospodarek 147d519e17eSAndy Gospodarek if (!rtnl_trylock()) 148d519e17eSAndy Gospodarek return restart_syscall(); 149d519e17eSAndy Gospodarek 1508ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1518ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 1524bc71cb9SJiri Pirko if (!__ethtool_get_settings(netdev, &cmd)) 1538ae6dacaSDavid Decotigny ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd)); 154d519e17eSAndy Gospodarek } 155d519e17eSAndy Gospodarek rtnl_unlock(); 156d519e17eSAndy Gospodarek return ret; 157d519e17eSAndy Gospodarek } 158d519e17eSAndy Gospodarek 159d519e17eSAndy Gospodarek static ssize_t show_duplex(struct device *dev, 160d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 161d519e17eSAndy Gospodarek { 162d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 163d519e17eSAndy Gospodarek int ret = -EINVAL; 164d519e17eSAndy Gospodarek 165d519e17eSAndy Gospodarek if (!rtnl_trylock()) 166d519e17eSAndy Gospodarek return restart_syscall(); 167d519e17eSAndy Gospodarek 1688ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1698ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 1704bc71cb9SJiri Pirko if (!__ethtool_get_settings(netdev, &cmd)) 1718ae6dacaSDavid Decotigny ret = sprintf(buf, "%s\n", 1728ae6dacaSDavid Decotigny cmd.duplex ? "full" : "half"); 173d519e17eSAndy Gospodarek } 174d519e17eSAndy Gospodarek rtnl_unlock(); 175d519e17eSAndy Gospodarek return ret; 176d519e17eSAndy Gospodarek } 177d519e17eSAndy Gospodarek 17843cb76d9SGreg Kroah-Hartman static ssize_t show_dormant(struct device *dev, 17943cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 180b00055aaSStefan Rompf { 181b00055aaSStefan Rompf struct net_device *netdev = to_net_dev(dev); 182b00055aaSStefan Rompf 183b00055aaSStefan Rompf if (netif_running(netdev)) 184b00055aaSStefan Rompf return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); 185b00055aaSStefan Rompf 186b00055aaSStefan Rompf return -EINVAL; 187b00055aaSStefan Rompf } 188b00055aaSStefan Rompf 18936cbd3dcSJan Engelhardt static const char *const operstates[] = { 190b00055aaSStefan Rompf "unknown", 191b00055aaSStefan Rompf "notpresent", /* currently unused */ 192b00055aaSStefan Rompf "down", 193b00055aaSStefan Rompf "lowerlayerdown", 194b00055aaSStefan Rompf "testing", /* currently unused */ 195b00055aaSStefan Rompf "dormant", 196b00055aaSStefan Rompf "up" 197b00055aaSStefan Rompf }; 198b00055aaSStefan Rompf 19943cb76d9SGreg Kroah-Hartman static ssize_t show_operstate(struct device *dev, 20043cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 201b00055aaSStefan Rompf { 202b00055aaSStefan Rompf const struct net_device *netdev = to_net_dev(dev); 203b00055aaSStefan Rompf unsigned char operstate; 204b00055aaSStefan Rompf 205b00055aaSStefan Rompf read_lock(&dev_base_lock); 206b00055aaSStefan Rompf operstate = netdev->operstate; 207b00055aaSStefan Rompf if (!netif_running(netdev)) 208b00055aaSStefan Rompf operstate = IF_OPER_DOWN; 209b00055aaSStefan Rompf read_unlock(&dev_base_lock); 210b00055aaSStefan Rompf 211e3a5cd9eSAdrian Bunk if (operstate >= ARRAY_SIZE(operstates)) 212b00055aaSStefan Rompf return -EINVAL; /* should not happen */ 213b00055aaSStefan Rompf 214b00055aaSStefan Rompf return sprintf(buf, "%s\n", operstates[operstate]); 215b00055aaSStefan Rompf } 216b00055aaSStefan Rompf 2171da177e4SLinus Torvalds /* read-write attributes */ 2181da177e4SLinus Torvalds NETDEVICE_SHOW(mtu, fmt_dec); 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int change_mtu(struct net_device *net, unsigned long new_mtu) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds return dev_set_mtu(net, (int) new_mtu); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 22543cb76d9SGreg Kroah-Hartman static ssize_t store_mtu(struct device *dev, struct device_attribute *attr, 22643cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2271da177e4SLinus Torvalds { 22843cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_mtu); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds NETDEVICE_SHOW(flags, fmt_hex); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds static int change_flags(struct net_device *net, unsigned long new_flags) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds return dev_change_flags(net, (unsigned) new_flags); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 23843cb76d9SGreg Kroah-Hartman static ssize_t store_flags(struct device *dev, struct device_attribute *attr, 23943cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2401da177e4SLinus Torvalds { 24143cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_flags); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds NETDEVICE_SHOW(tx_queue_len, fmt_ulong); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static int change_tx_queue_len(struct net_device *net, unsigned long new_len) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds net->tx_queue_len = new_len; 2491da177e4SLinus Torvalds return 0; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 25243cb76d9SGreg Kroah-Hartman static ssize_t store_tx_queue_len(struct device *dev, 25343cb76d9SGreg Kroah-Hartman struct device_attribute *attr, 25443cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2551da177e4SLinus Torvalds { 25643cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_tx_queue_len); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2590b815a1aSStephen Hemminger static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr, 2600b815a1aSStephen Hemminger const char *buf, size_t len) 2610b815a1aSStephen Hemminger { 2620b815a1aSStephen Hemminger struct net_device *netdev = to_net_dev(dev); 2630b815a1aSStephen Hemminger size_t count = len; 2640b815a1aSStephen Hemminger ssize_t ret; 2650b815a1aSStephen Hemminger 2660b815a1aSStephen Hemminger if (!capable(CAP_NET_ADMIN)) 2670b815a1aSStephen Hemminger return -EPERM; 2680b815a1aSStephen Hemminger 2690b815a1aSStephen Hemminger /* ignore trailing newline */ 2700b815a1aSStephen Hemminger if (len > 0 && buf[len - 1] == '\n') 2710b815a1aSStephen Hemminger --count; 2720b815a1aSStephen Hemminger 273336ca57cSEric W. Biederman if (!rtnl_trylock()) 274336ca57cSEric W. Biederman return restart_syscall(); 2750b815a1aSStephen Hemminger ret = dev_set_alias(netdev, buf, count); 2760b815a1aSStephen Hemminger rtnl_unlock(); 2770b815a1aSStephen Hemminger 2780b815a1aSStephen Hemminger return ret < 0 ? ret : len; 2790b815a1aSStephen Hemminger } 2800b815a1aSStephen Hemminger 2810b815a1aSStephen Hemminger static ssize_t show_ifalias(struct device *dev, 2820b815a1aSStephen Hemminger struct device_attribute *attr, char *buf) 2830b815a1aSStephen Hemminger { 2840b815a1aSStephen Hemminger const struct net_device *netdev = to_net_dev(dev); 2850b815a1aSStephen Hemminger ssize_t ret = 0; 2860b815a1aSStephen Hemminger 287336ca57cSEric W. Biederman if (!rtnl_trylock()) 288336ca57cSEric W. Biederman return restart_syscall(); 2890b815a1aSStephen Hemminger if (netdev->ifalias) 2900b815a1aSStephen Hemminger ret = sprintf(buf, "%s\n", netdev->ifalias); 2910b815a1aSStephen Hemminger rtnl_unlock(); 2920b815a1aSStephen Hemminger return ret; 2930b815a1aSStephen Hemminger } 2940b815a1aSStephen Hemminger 295a512b92bSVlad Dogaru NETDEVICE_SHOW(group, fmt_dec); 296a512b92bSVlad Dogaru 297a512b92bSVlad Dogaru static int change_group(struct net_device *net, unsigned long new_group) 298a512b92bSVlad Dogaru { 299a512b92bSVlad Dogaru dev_set_group(net, (int) new_group); 300a512b92bSVlad Dogaru return 0; 301a512b92bSVlad Dogaru } 302a512b92bSVlad Dogaru 303a512b92bSVlad Dogaru static ssize_t store_group(struct device *dev, struct device_attribute *attr, 304a512b92bSVlad Dogaru const char *buf, size_t len) 305a512b92bSVlad Dogaru { 306a512b92bSVlad Dogaru return netdev_store(dev, attr, buf, len, change_group); 307a512b92bSVlad Dogaru } 308a512b92bSVlad Dogaru 30943cb76d9SGreg Kroah-Hartman static struct device_attribute net_class_attributes[] = { 310c1f79426SStefan Assmann __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL), 311fd586bacSKay Sievers __ATTR(addr_len, S_IRUGO, show_addr_len, NULL), 3129d29672cSDavid Woodhouse __ATTR(dev_id, S_IRUGO, show_dev_id, NULL), 3130b815a1aSStephen Hemminger __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias), 314fd586bacSKay Sievers __ATTR(iflink, S_IRUGO, show_iflink, NULL), 315fd586bacSKay Sievers __ATTR(ifindex, S_IRUGO, show_ifindex, NULL), 316fd586bacSKay Sievers __ATTR(type, S_IRUGO, show_type, NULL), 317b00055aaSStefan Rompf __ATTR(link_mode, S_IRUGO, show_link_mode, NULL), 318fd586bacSKay Sievers __ATTR(address, S_IRUGO, show_address, NULL), 319fd586bacSKay Sievers __ATTR(broadcast, S_IRUGO, show_broadcast, NULL), 320fd586bacSKay Sievers __ATTR(carrier, S_IRUGO, show_carrier, NULL), 321d519e17eSAndy Gospodarek __ATTR(speed, S_IRUGO, show_speed, NULL), 322d519e17eSAndy Gospodarek __ATTR(duplex, S_IRUGO, show_duplex, NULL), 323b00055aaSStefan Rompf __ATTR(dormant, S_IRUGO, show_dormant, NULL), 324b00055aaSStefan Rompf __ATTR(operstate, S_IRUGO, show_operstate, NULL), 325fd586bacSKay Sievers __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu), 326fd586bacSKay Sievers __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags), 327fd586bacSKay Sievers __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len, 328fd586bacSKay Sievers store_tx_queue_len), 329b6644cb7SXiaotian Feng __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group), 330fd586bacSKay Sievers {} 3311da177e4SLinus Torvalds }; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */ 33443cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d, 33543cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf, 3361da177e4SLinus Torvalds unsigned long offset) 3371da177e4SLinus Torvalds { 33843cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 3391da177e4SLinus Torvalds ssize_t ret = -EINVAL; 3401da177e4SLinus Torvalds 341be1f3c2cSBen Hutchings WARN_ON(offset > sizeof(struct rtnl_link_stats64) || 342be1f3c2cSBen Hutchings offset % sizeof(u64) != 0); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds read_lock(&dev_base_lock); 34596e74088SPavel Emelyanov if (dev_isalive(dev)) { 34628172739SEric Dumazet struct rtnl_link_stats64 temp; 34728172739SEric Dumazet const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); 34828172739SEric Dumazet 349be1f3c2cSBen Hutchings ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset)); 35096e74088SPavel Emelyanov } 3511da177e4SLinus Torvalds read_unlock(&dev_base_lock); 3521da177e4SLinus Torvalds return ret; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* generate a read-only statistics attribute */ 3561da177e4SLinus Torvalds #define NETSTAT_ENTRY(name) \ 35743cb76d9SGreg Kroah-Hartman static ssize_t show_##name(struct device *d, \ 35843cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 3591da177e4SLinus Torvalds { \ 36043cb76d9SGreg Kroah-Hartman return netstat_show(d, attr, buf, \ 361be1f3c2cSBen Hutchings offsetof(struct rtnl_link_stats64, name)); \ 3621da177e4SLinus Torvalds } \ 36343cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets); 3661da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets); 3671da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes); 3681da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes); 3691da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors); 3701da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors); 3711da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped); 3721da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped); 3731da177e4SLinus Torvalds NETSTAT_ENTRY(multicast); 3741da177e4SLinus Torvalds NETSTAT_ENTRY(collisions); 3751da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors); 3761da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors); 3771da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors); 3781da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors); 3791da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors); 3801da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors); 3811da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors); 3821da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors); 3831da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors); 3841da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors); 3851da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors); 3861da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed); 3871da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = { 39043cb76d9SGreg Kroah-Hartman &dev_attr_rx_packets.attr, 39143cb76d9SGreg Kroah-Hartman &dev_attr_tx_packets.attr, 39243cb76d9SGreg Kroah-Hartman &dev_attr_rx_bytes.attr, 39343cb76d9SGreg Kroah-Hartman &dev_attr_tx_bytes.attr, 39443cb76d9SGreg Kroah-Hartman &dev_attr_rx_errors.attr, 39543cb76d9SGreg Kroah-Hartman &dev_attr_tx_errors.attr, 39643cb76d9SGreg Kroah-Hartman &dev_attr_rx_dropped.attr, 39743cb76d9SGreg Kroah-Hartman &dev_attr_tx_dropped.attr, 39843cb76d9SGreg Kroah-Hartman &dev_attr_multicast.attr, 39943cb76d9SGreg Kroah-Hartman &dev_attr_collisions.attr, 40043cb76d9SGreg Kroah-Hartman &dev_attr_rx_length_errors.attr, 40143cb76d9SGreg Kroah-Hartman &dev_attr_rx_over_errors.attr, 40243cb76d9SGreg Kroah-Hartman &dev_attr_rx_crc_errors.attr, 40343cb76d9SGreg Kroah-Hartman &dev_attr_rx_frame_errors.attr, 40443cb76d9SGreg Kroah-Hartman &dev_attr_rx_fifo_errors.attr, 40543cb76d9SGreg Kroah-Hartman &dev_attr_rx_missed_errors.attr, 40643cb76d9SGreg Kroah-Hartman &dev_attr_tx_aborted_errors.attr, 40743cb76d9SGreg Kroah-Hartman &dev_attr_tx_carrier_errors.attr, 40843cb76d9SGreg Kroah-Hartman &dev_attr_tx_fifo_errors.attr, 40943cb76d9SGreg Kroah-Hartman &dev_attr_tx_heartbeat_errors.attr, 41043cb76d9SGreg Kroah-Hartman &dev_attr_tx_window_errors.attr, 41143cb76d9SGreg Kroah-Hartman &dev_attr_rx_compressed.attr, 41243cb76d9SGreg Kroah-Hartman &dev_attr_tx_compressed.attr, 4131da177e4SLinus Torvalds NULL 4141da177e4SLinus Torvalds }; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds static struct attribute_group netstat_group = { 4181da177e4SLinus Torvalds .name = "statistics", 4191da177e4SLinus Torvalds .attrs = netstat_attrs, 4201da177e4SLinus Torvalds }; 4211da177e4SLinus Torvalds 42222bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS 4231da177e4SLinus Torvalds /* helper function that does all the locking etc for wireless stats */ 42443cb76d9SGreg Kroah-Hartman static ssize_t wireless_show(struct device *d, char *buf, 4251da177e4SLinus Torvalds ssize_t (*format)(const struct iw_statistics *, 4261da177e4SLinus Torvalds char *)) 4271da177e4SLinus Torvalds { 42843cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 4298f1546caSJohannes Berg const struct iw_statistics *iw; 4301da177e4SLinus Torvalds ssize_t ret = -EINVAL; 4311da177e4SLinus Torvalds 432b8afe641SEric W. Biederman if (!rtnl_trylock()) 433b8afe641SEric W. Biederman return restart_syscall(); 4346dd214b5SAndrey Borzenkov if (dev_isalive(dev)) { 4358f1546caSJohannes Berg iw = get_wireless_stats(dev); 4368f1546caSJohannes Berg if (iw) 4371da177e4SLinus Torvalds ret = (*format)(iw, buf); 4386dd214b5SAndrey Borzenkov } 439a160ee69SJohannes Berg rtnl_unlock(); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds return ret; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* show function template for wireless fields */ 4451da177e4SLinus Torvalds #define WIRELESS_SHOW(name, field, format_string) \ 4461da177e4SLinus Torvalds static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \ 4471da177e4SLinus Torvalds { \ 4481da177e4SLinus Torvalds return sprintf(buf, format_string, iw->field); \ 4491da177e4SLinus Torvalds } \ 45043cb76d9SGreg Kroah-Hartman static ssize_t show_iw_##name(struct device *d, \ 45143cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 4521da177e4SLinus Torvalds { \ 45343cb76d9SGreg Kroah-Hartman return wireless_show(d, buf, format_iw_##name); \ 4541da177e4SLinus Torvalds } \ 45543cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL) 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds WIRELESS_SHOW(status, status, fmt_hex); 4581da177e4SLinus Torvalds WIRELESS_SHOW(link, qual.qual, fmt_dec); 4591da177e4SLinus Torvalds WIRELESS_SHOW(level, qual.level, fmt_dec); 4601da177e4SLinus Torvalds WIRELESS_SHOW(noise, qual.noise, fmt_dec); 4611da177e4SLinus Torvalds WIRELESS_SHOW(nwid, discard.nwid, fmt_dec); 4621da177e4SLinus Torvalds WIRELESS_SHOW(crypt, discard.code, fmt_dec); 4631da177e4SLinus Torvalds WIRELESS_SHOW(fragment, discard.fragment, fmt_dec); 4641da177e4SLinus Torvalds WIRELESS_SHOW(misc, discard.misc, fmt_dec); 4651da177e4SLinus Torvalds WIRELESS_SHOW(retries, discard.retries, fmt_dec); 4661da177e4SLinus Torvalds WIRELESS_SHOW(beacon, miss.beacon, fmt_dec); 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds static struct attribute *wireless_attrs[] = { 46943cb76d9SGreg Kroah-Hartman &dev_attr_status.attr, 47043cb76d9SGreg Kroah-Hartman &dev_attr_link.attr, 47143cb76d9SGreg Kroah-Hartman &dev_attr_level.attr, 47243cb76d9SGreg Kroah-Hartman &dev_attr_noise.attr, 47343cb76d9SGreg Kroah-Hartman &dev_attr_nwid.attr, 47443cb76d9SGreg Kroah-Hartman &dev_attr_crypt.attr, 47543cb76d9SGreg Kroah-Hartman &dev_attr_fragment.attr, 47643cb76d9SGreg Kroah-Hartman &dev_attr_retries.attr, 47743cb76d9SGreg Kroah-Hartman &dev_attr_misc.attr, 47843cb76d9SGreg Kroah-Hartman &dev_attr_beacon.attr, 4791da177e4SLinus Torvalds NULL 4801da177e4SLinus Torvalds }; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds static struct attribute_group wireless_group = { 4831da177e4SLinus Torvalds .name = "wireless", 4841da177e4SLinus Torvalds .attrs = wireless_attrs, 4851da177e4SLinus Torvalds }; 4861da177e4SLinus Torvalds #endif 487d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */ 4881da177e4SLinus Torvalds 48930bde1f5SStephen Rothwell #ifdef CONFIG_RPS 4900a9627f2STom Herbert /* 4910a9627f2STom Herbert * RX queue sysfs structures and functions. 4920a9627f2STom Herbert */ 4930a9627f2STom Herbert struct rx_queue_attribute { 4940a9627f2STom Herbert struct attribute attr; 4950a9627f2STom Herbert ssize_t (*show)(struct netdev_rx_queue *queue, 4960a9627f2STom Herbert struct rx_queue_attribute *attr, char *buf); 4970a9627f2STom Herbert ssize_t (*store)(struct netdev_rx_queue *queue, 4980a9627f2STom Herbert struct rx_queue_attribute *attr, const char *buf, size_t len); 4990a9627f2STom Herbert }; 5000a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr, \ 5010a9627f2STom Herbert struct rx_queue_attribute, attr) 5020a9627f2STom Herbert 5030a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) 5040a9627f2STom Herbert 5050a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, 5060a9627f2STom Herbert char *buf) 5070a9627f2STom Herbert { 5080a9627f2STom Herbert struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 5090a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 5100a9627f2STom Herbert 5110a9627f2STom Herbert if (!attribute->show) 5120a9627f2STom Herbert return -EIO; 5130a9627f2STom Herbert 5140a9627f2STom Herbert return attribute->show(queue, attribute, buf); 5150a9627f2STom Herbert } 5160a9627f2STom Herbert 5170a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, 5180a9627f2STom Herbert const char *buf, size_t count) 5190a9627f2STom Herbert { 5200a9627f2STom Herbert struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 5210a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 5220a9627f2STom Herbert 5230a9627f2STom Herbert if (!attribute->store) 5240a9627f2STom Herbert return -EIO; 5250a9627f2STom Herbert 5260a9627f2STom Herbert return attribute->store(queue, attribute, buf, count); 5270a9627f2STom Herbert } 5280a9627f2STom Herbert 529fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = { 5300a9627f2STom Herbert .show = rx_queue_attr_show, 5310a9627f2STom Herbert .store = rx_queue_attr_store, 5320a9627f2STom Herbert }; 5330a9627f2STom Herbert 5340a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue, 5350a9627f2STom Herbert struct rx_queue_attribute *attribute, char *buf) 5360a9627f2STom Herbert { 5370a9627f2STom Herbert struct rps_map *map; 5380a9627f2STom Herbert cpumask_var_t mask; 5390a9627f2STom Herbert size_t len = 0; 5400a9627f2STom Herbert int i; 5410a9627f2STom Herbert 5420a9627f2STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 5430a9627f2STom Herbert return -ENOMEM; 5440a9627f2STom Herbert 5450a9627f2STom Herbert rcu_read_lock(); 5460a9627f2STom Herbert map = rcu_dereference(queue->rps_map); 5470a9627f2STom Herbert if (map) 5480a9627f2STom Herbert for (i = 0; i < map->len; i++) 5490a9627f2STom Herbert cpumask_set_cpu(map->cpus[i], mask); 5500a9627f2STom Herbert 5510a9627f2STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 5520a9627f2STom Herbert if (PAGE_SIZE - len < 3) { 5530a9627f2STom Herbert rcu_read_unlock(); 5540a9627f2STom Herbert free_cpumask_var(mask); 5550a9627f2STom Herbert return -EINVAL; 5560a9627f2STom Herbert } 5570a9627f2STom Herbert rcu_read_unlock(); 5580a9627f2STom Herbert 5590a9627f2STom Herbert free_cpumask_var(mask); 5600a9627f2STom Herbert len += sprintf(buf + len, "\n"); 5610a9627f2STom Herbert return len; 5620a9627f2STom Herbert } 5630a9627f2STom Herbert 564f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue, 5650a9627f2STom Herbert struct rx_queue_attribute *attribute, 5660a9627f2STom Herbert const char *buf, size_t len) 5670a9627f2STom Herbert { 5680a9627f2STom Herbert struct rps_map *old_map, *map; 5690a9627f2STom Herbert cpumask_var_t mask; 5700a9627f2STom Herbert int err, cpu, i; 5710a9627f2STom Herbert static DEFINE_SPINLOCK(rps_map_lock); 5720a9627f2STom Herbert 5730a9627f2STom Herbert if (!capable(CAP_NET_ADMIN)) 5740a9627f2STom Herbert return -EPERM; 5750a9627f2STom Herbert 5760a9627f2STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 5770a9627f2STom Herbert return -ENOMEM; 5780a9627f2STom Herbert 5790a9627f2STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 5800a9627f2STom Herbert if (err) { 5810a9627f2STom Herbert free_cpumask_var(mask); 5820a9627f2STom Herbert return err; 5830a9627f2STom Herbert } 5840a9627f2STom Herbert 5850a9627f2STom Herbert map = kzalloc(max_t(unsigned, 5860a9627f2STom Herbert RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), 5870a9627f2STom Herbert GFP_KERNEL); 5880a9627f2STom Herbert if (!map) { 5890a9627f2STom Herbert free_cpumask_var(mask); 5900a9627f2STom Herbert return -ENOMEM; 5910a9627f2STom Herbert } 5920a9627f2STom Herbert 5930a9627f2STom Herbert i = 0; 5940a9627f2STom Herbert for_each_cpu_and(cpu, mask, cpu_online_mask) 5950a9627f2STom Herbert map->cpus[i++] = cpu; 5960a9627f2STom Herbert 5970a9627f2STom Herbert if (i) 5980a9627f2STom Herbert map->len = i; 5990a9627f2STom Herbert else { 6000a9627f2STom Herbert kfree(map); 6010a9627f2STom Herbert map = NULL; 6020a9627f2STom Herbert } 6030a9627f2STom Herbert 6040a9627f2STom Herbert spin_lock(&rps_map_lock); 6056e3f7fafSEric Dumazet old_map = rcu_dereference_protected(queue->rps_map, 6066e3f7fafSEric Dumazet lockdep_is_held(&rps_map_lock)); 6070a9627f2STom Herbert rcu_assign_pointer(queue->rps_map, map); 6080a9627f2STom Herbert spin_unlock(&rps_map_lock); 6090a9627f2STom Herbert 610adc9300eSEric Dumazet if (map) 611adc9300eSEric Dumazet jump_label_inc(&rps_needed); 612adc9300eSEric Dumazet if (old_map) { 613f6f80238SLai Jiangshan kfree_rcu(old_map, rcu); 614adc9300eSEric Dumazet jump_label_dec(&rps_needed); 615adc9300eSEric Dumazet } 6160a9627f2STom Herbert free_cpumask_var(mask); 6170a9627f2STom Herbert return len; 6180a9627f2STom Herbert } 6190a9627f2STom Herbert 620fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 621fec5e652STom Herbert struct rx_queue_attribute *attr, 622fec5e652STom Herbert char *buf) 623fec5e652STom Herbert { 624fec5e652STom Herbert struct rps_dev_flow_table *flow_table; 62560b778ceSEric Dumazet unsigned long val = 0; 626fec5e652STom Herbert 627fec5e652STom Herbert rcu_read_lock(); 628fec5e652STom Herbert flow_table = rcu_dereference(queue->rps_flow_table); 629fec5e652STom Herbert if (flow_table) 63060b778ceSEric Dumazet val = (unsigned long)flow_table->mask + 1; 631fec5e652STom Herbert rcu_read_unlock(); 632fec5e652STom Herbert 63360b778ceSEric Dumazet return sprintf(buf, "%lu\n", val); 634fec5e652STom Herbert } 635fec5e652STom Herbert 636fec5e652STom Herbert static void rps_dev_flow_table_release_work(struct work_struct *work) 637fec5e652STom Herbert { 638fec5e652STom Herbert struct rps_dev_flow_table *table = container_of(work, 639fec5e652STom Herbert struct rps_dev_flow_table, free_work); 640fec5e652STom Herbert 641fec5e652STom Herbert vfree(table); 642fec5e652STom Herbert } 643fec5e652STom Herbert 644fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu) 645fec5e652STom Herbert { 646fec5e652STom Herbert struct rps_dev_flow_table *table = container_of(rcu, 647fec5e652STom Herbert struct rps_dev_flow_table, rcu); 648fec5e652STom Herbert 649fec5e652STom Herbert INIT_WORK(&table->free_work, rps_dev_flow_table_release_work); 650fec5e652STom Herbert schedule_work(&table->free_work); 651fec5e652STom Herbert } 652fec5e652STom Herbert 653f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 654fec5e652STom Herbert struct rx_queue_attribute *attr, 655fec5e652STom Herbert const char *buf, size_t len) 656fec5e652STom Herbert { 65760b778ceSEric Dumazet unsigned long mask, count; 658fec5e652STom Herbert struct rps_dev_flow_table *table, *old_table; 659fec5e652STom Herbert static DEFINE_SPINLOCK(rps_dev_flow_lock); 66060b778ceSEric Dumazet int rc; 661fec5e652STom Herbert 662fec5e652STom Herbert if (!capable(CAP_NET_ADMIN)) 663fec5e652STom Herbert return -EPERM; 664fec5e652STom Herbert 66560b778ceSEric Dumazet rc = kstrtoul(buf, 0, &count); 66660b778ceSEric Dumazet if (rc < 0) 66760b778ceSEric Dumazet return rc; 668fec5e652STom Herbert 669fec5e652STom Herbert if (count) { 67060b778ceSEric Dumazet mask = count - 1; 67160b778ceSEric Dumazet /* mask = roundup_pow_of_two(count) - 1; 67260b778ceSEric Dumazet * without overflows... 67360b778ceSEric Dumazet */ 67460b778ceSEric Dumazet while ((mask | (mask >> 1)) != mask) 67560b778ceSEric Dumazet mask |= (mask >> 1); 67660b778ceSEric Dumazet /* On 64 bit arches, must check mask fits in table->mask (u32), 67760b778ceSEric Dumazet * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1) 67860b778ceSEric Dumazet * doesnt overflow. 67960b778ceSEric Dumazet */ 68060b778ceSEric Dumazet #if BITS_PER_LONG > 32 68160b778ceSEric Dumazet if (mask > (unsigned long)(u32)mask) 682a0a129f8SXi Wang return -EINVAL; 68360b778ceSEric Dumazet #else 68460b778ceSEric Dumazet if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) 685a0a129f8SXi Wang / sizeof(struct rps_dev_flow)) { 686fec5e652STom Herbert /* Enforce a limit to prevent overflow */ 687fec5e652STom Herbert return -EINVAL; 688fec5e652STom Herbert } 68960b778ceSEric Dumazet #endif 69060b778ceSEric Dumazet table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); 691fec5e652STom Herbert if (!table) 692fec5e652STom Herbert return -ENOMEM; 693fec5e652STom Herbert 69460b778ceSEric Dumazet table->mask = mask; 69560b778ceSEric Dumazet for (count = 0; count <= mask; count++) 69660b778ceSEric Dumazet table->flows[count].cpu = RPS_NO_CPU; 697fec5e652STom Herbert } else 698fec5e652STom Herbert table = NULL; 699fec5e652STom Herbert 700fec5e652STom Herbert spin_lock(&rps_dev_flow_lock); 7016e3f7fafSEric Dumazet old_table = rcu_dereference_protected(queue->rps_flow_table, 7026e3f7fafSEric Dumazet lockdep_is_held(&rps_dev_flow_lock)); 703fec5e652STom Herbert rcu_assign_pointer(queue->rps_flow_table, table); 704fec5e652STom Herbert spin_unlock(&rps_dev_flow_lock); 705fec5e652STom Herbert 706fec5e652STom Herbert if (old_table) 707fec5e652STom Herbert call_rcu(&old_table->rcu, rps_dev_flow_table_release); 708fec5e652STom Herbert 709fec5e652STom Herbert return len; 710fec5e652STom Herbert } 711fec5e652STom Herbert 7120a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute = 7130a9627f2STom Herbert __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); 7140a9627f2STom Herbert 715fec5e652STom Herbert 716fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = 717fec5e652STom Herbert __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, 718fec5e652STom Herbert show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); 719fec5e652STom Herbert 7200a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = { 7210a9627f2STom Herbert &rps_cpus_attribute.attr, 722fec5e652STom Herbert &rps_dev_flow_table_cnt_attribute.attr, 7230a9627f2STom Herbert NULL 7240a9627f2STom Herbert }; 7250a9627f2STom Herbert 7260a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj) 7270a9627f2STom Herbert { 7280a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 7296e3f7fafSEric Dumazet struct rps_map *map; 7306e3f7fafSEric Dumazet struct rps_dev_flow_table *flow_table; 7310a9627f2STom Herbert 732fec5e652STom Herbert 73333d480ceSEric Dumazet map = rcu_dereference_protected(queue->rps_map, 1); 7349ea19481SJohn Fastabend if (map) { 7359ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_map, NULL); 736f6f80238SLai Jiangshan kfree_rcu(map, rcu); 7379ea19481SJohn Fastabend } 7386e3f7fafSEric Dumazet 73933d480ceSEric Dumazet flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); 7409ea19481SJohn Fastabend if (flow_table) { 7419ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_flow_table, NULL); 7426e3f7fafSEric Dumazet call_rcu(&flow_table->rcu, rps_dev_flow_table_release); 7439ea19481SJohn Fastabend } 7440a9627f2STom Herbert 7459ea19481SJohn Fastabend memset(kobj, 0, sizeof(*kobj)); 746fe822240STom Herbert dev_put(queue->dev); 7470a9627f2STom Herbert } 7480a9627f2STom Herbert 7490a9627f2STom Herbert static struct kobj_type rx_queue_ktype = { 7500a9627f2STom Herbert .sysfs_ops = &rx_queue_sysfs_ops, 7510a9627f2STom Herbert .release = rx_queue_release, 7520a9627f2STom Herbert .default_attrs = rx_queue_default_attrs, 7530a9627f2STom Herbert }; 7540a9627f2STom Herbert 7550a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index) 7560a9627f2STom Herbert { 7570a9627f2STom Herbert struct netdev_rx_queue *queue = net->_rx + index; 7580a9627f2STom Herbert struct kobject *kobj = &queue->kobj; 7590a9627f2STom Herbert int error = 0; 7600a9627f2STom Herbert 7610a9627f2STom Herbert kobj->kset = net->queues_kset; 7620a9627f2STom Herbert error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, 7630a9627f2STom Herbert "rx-%u", index); 7640a9627f2STom Herbert if (error) { 7650a9627f2STom Herbert kobject_put(kobj); 7660a9627f2STom Herbert return error; 7670a9627f2STom Herbert } 7680a9627f2STom Herbert 7690a9627f2STom Herbert kobject_uevent(kobj, KOBJ_ADD); 770fe822240STom Herbert dev_hold(queue->dev); 7710a9627f2STom Herbert 7720a9627f2STom Herbert return error; 7730a9627f2STom Herbert } 774bf264145STom Herbert #endif /* CONFIG_RPS */ 7750a9627f2STom Herbert 77662fe0b40SBen Hutchings int 77762fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 7780a9627f2STom Herbert { 779bf264145STom Herbert #ifdef CONFIG_RPS 7800a9627f2STom Herbert int i; 7810a9627f2STom Herbert int error = 0; 7820a9627f2STom Herbert 78362fe0b40SBen Hutchings for (i = old_num; i < new_num; i++) { 7840a9627f2STom Herbert error = rx_queue_add_kobject(net, i); 78562fe0b40SBen Hutchings if (error) { 78662fe0b40SBen Hutchings new_num = old_num; 7870a9627f2STom Herbert break; 7880a9627f2STom Herbert } 78962fe0b40SBen Hutchings } 7900a9627f2STom Herbert 79162fe0b40SBen Hutchings while (--i >= new_num) 7920a9627f2STom Herbert kobject_put(&net->_rx[i].kobj); 7930a9627f2STom Herbert 7940a9627f2STom Herbert return error; 795bf264145STom Herbert #else 796bf264145STom Herbert return 0; 797bf264145STom Herbert #endif 7980a9627f2STom Herbert } 7990a9627f2STom Herbert 800ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 8011d24eb48STom Herbert /* 8021d24eb48STom Herbert * netdev_queue sysfs structures and functions. 8031d24eb48STom Herbert */ 8041d24eb48STom Herbert struct netdev_queue_attribute { 8051d24eb48STom Herbert struct attribute attr; 8061d24eb48STom Herbert ssize_t (*show)(struct netdev_queue *queue, 8071d24eb48STom Herbert struct netdev_queue_attribute *attr, char *buf); 8081d24eb48STom Herbert ssize_t (*store)(struct netdev_queue *queue, 8091d24eb48STom Herbert struct netdev_queue_attribute *attr, const char *buf, size_t len); 8101d24eb48STom Herbert }; 8111d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr, \ 8121d24eb48STom Herbert struct netdev_queue_attribute, attr) 8131d24eb48STom Herbert 8141d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) 8151d24eb48STom Herbert 8161d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj, 8171d24eb48STom Herbert struct attribute *attr, char *buf) 81862fe0b40SBen Hutchings { 8191d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8201d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8211d24eb48STom Herbert 8221d24eb48STom Herbert if (!attribute->show) 8231d24eb48STom Herbert return -EIO; 8241d24eb48STom Herbert 8251d24eb48STom Herbert return attribute->show(queue, attribute, buf); 8261d24eb48STom Herbert } 8271d24eb48STom Herbert 8281d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj, 8291d24eb48STom Herbert struct attribute *attr, 8301d24eb48STom Herbert const char *buf, size_t count) 8311d24eb48STom Herbert { 8321d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8331d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8341d24eb48STom Herbert 8351d24eb48STom Herbert if (!attribute->store) 8361d24eb48STom Herbert return -EIO; 8371d24eb48STom Herbert 8381d24eb48STom Herbert return attribute->store(queue, attribute, buf, count); 8391d24eb48STom Herbert } 8401d24eb48STom Herbert 8411d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = { 8421d24eb48STom Herbert .show = netdev_queue_attr_show, 8431d24eb48STom Herbert .store = netdev_queue_attr_store, 8441d24eb48STom Herbert }; 8451d24eb48STom Herbert 846ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue, 847ccf5ff69Sdavid decotigny struct netdev_queue_attribute *attribute, 848ccf5ff69Sdavid decotigny char *buf) 849ccf5ff69Sdavid decotigny { 850ccf5ff69Sdavid decotigny unsigned long trans_timeout; 851ccf5ff69Sdavid decotigny 852ccf5ff69Sdavid decotigny spin_lock_irq(&queue->_xmit_lock); 853ccf5ff69Sdavid decotigny trans_timeout = queue->trans_timeout; 854ccf5ff69Sdavid decotigny spin_unlock_irq(&queue->_xmit_lock); 855ccf5ff69Sdavid decotigny 856ccf5ff69Sdavid decotigny return sprintf(buf, "%lu", trans_timeout); 857ccf5ff69Sdavid decotigny } 858ccf5ff69Sdavid decotigny 859ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout = 860ccf5ff69Sdavid decotigny __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); 861ccf5ff69Sdavid decotigny 862114cf580STom Herbert #ifdef CONFIG_BQL 863114cf580STom Herbert /* 864114cf580STom Herbert * Byte queue limits sysfs structures and functions. 865114cf580STom Herbert */ 866114cf580STom Herbert static ssize_t bql_show(char *buf, unsigned int value) 867114cf580STom Herbert { 868114cf580STom Herbert return sprintf(buf, "%u\n", value); 869114cf580STom Herbert } 870114cf580STom Herbert 871114cf580STom Herbert static ssize_t bql_set(const char *buf, const size_t count, 872114cf580STom Herbert unsigned int *pvalue) 873114cf580STom Herbert { 874114cf580STom Herbert unsigned int value; 875114cf580STom Herbert int err; 876114cf580STom Herbert 877114cf580STom Herbert if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) 878114cf580STom Herbert value = DQL_MAX_LIMIT; 879114cf580STom Herbert else { 880114cf580STom Herbert err = kstrtouint(buf, 10, &value); 881114cf580STom Herbert if (err < 0) 882114cf580STom Herbert return err; 883114cf580STom Herbert if (value > DQL_MAX_LIMIT) 884114cf580STom Herbert return -EINVAL; 885114cf580STom Herbert } 886114cf580STom Herbert 887114cf580STom Herbert *pvalue = value; 888114cf580STom Herbert 889114cf580STom Herbert return count; 890114cf580STom Herbert } 891114cf580STom Herbert 892114cf580STom Herbert static ssize_t bql_show_hold_time(struct netdev_queue *queue, 893114cf580STom Herbert struct netdev_queue_attribute *attr, 894114cf580STom Herbert char *buf) 895114cf580STom Herbert { 896114cf580STom Herbert struct dql *dql = &queue->dql; 897114cf580STom Herbert 898114cf580STom Herbert return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); 899114cf580STom Herbert } 900114cf580STom Herbert 901114cf580STom Herbert static ssize_t bql_set_hold_time(struct netdev_queue *queue, 902114cf580STom Herbert struct netdev_queue_attribute *attribute, 903114cf580STom Herbert const char *buf, size_t len) 904114cf580STom Herbert { 905114cf580STom Herbert struct dql *dql = &queue->dql; 906114cf580STom Herbert unsigned value; 907114cf580STom Herbert int err; 908114cf580STom Herbert 909114cf580STom Herbert err = kstrtouint(buf, 10, &value); 910114cf580STom Herbert if (err < 0) 911114cf580STom Herbert return err; 912114cf580STom Herbert 913114cf580STom Herbert dql->slack_hold_time = msecs_to_jiffies(value); 914114cf580STom Herbert 915114cf580STom Herbert return len; 916114cf580STom Herbert } 917114cf580STom Herbert 918114cf580STom Herbert static struct netdev_queue_attribute bql_hold_time_attribute = 919114cf580STom Herbert __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time, 920114cf580STom Herbert bql_set_hold_time); 921114cf580STom Herbert 922114cf580STom Herbert static ssize_t bql_show_inflight(struct netdev_queue *queue, 923114cf580STom Herbert struct netdev_queue_attribute *attr, 924114cf580STom Herbert char *buf) 925114cf580STom Herbert { 926114cf580STom Herbert struct dql *dql = &queue->dql; 927114cf580STom Herbert 928114cf580STom Herbert return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); 929114cf580STom Herbert } 930114cf580STom Herbert 931114cf580STom Herbert static struct netdev_queue_attribute bql_inflight_attribute = 932114cf580STom Herbert __ATTR(inflight, S_IRUGO | S_IWUSR, bql_show_inflight, NULL); 933114cf580STom Herbert 934114cf580STom Herbert #define BQL_ATTR(NAME, FIELD) \ 935114cf580STom Herbert static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ 936114cf580STom Herbert struct netdev_queue_attribute *attr, \ 937114cf580STom Herbert char *buf) \ 938114cf580STom Herbert { \ 939114cf580STom Herbert return bql_show(buf, queue->dql.FIELD); \ 940114cf580STom Herbert } \ 941114cf580STom Herbert \ 942114cf580STom Herbert static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ 943114cf580STom Herbert struct netdev_queue_attribute *attr, \ 944114cf580STom Herbert const char *buf, size_t len) \ 945114cf580STom Herbert { \ 946114cf580STom Herbert return bql_set(buf, len, &queue->dql.FIELD); \ 947114cf580STom Herbert } \ 948114cf580STom Herbert \ 949114cf580STom Herbert static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \ 950114cf580STom Herbert __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \ 951114cf580STom Herbert bql_set_ ## NAME); 952114cf580STom Herbert 953114cf580STom Herbert BQL_ATTR(limit, limit) 954114cf580STom Herbert BQL_ATTR(limit_max, max_limit) 955114cf580STom Herbert BQL_ATTR(limit_min, min_limit) 956114cf580STom Herbert 957114cf580STom Herbert static struct attribute *dql_attrs[] = { 958114cf580STom Herbert &bql_limit_attribute.attr, 959114cf580STom Herbert &bql_limit_max_attribute.attr, 960114cf580STom Herbert &bql_limit_min_attribute.attr, 961114cf580STom Herbert &bql_hold_time_attribute.attr, 962114cf580STom Herbert &bql_inflight_attribute.attr, 963114cf580STom Herbert NULL 964114cf580STom Herbert }; 965114cf580STom Herbert 966114cf580STom Herbert static struct attribute_group dql_group = { 967114cf580STom Herbert .name = "byte_queue_limits", 968114cf580STom Herbert .attrs = dql_attrs, 969114cf580STom Herbert }; 970114cf580STom Herbert #endif /* CONFIG_BQL */ 971114cf580STom Herbert 972ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 9731d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue) 9741d24eb48STom Herbert { 9751d24eb48STom Herbert struct net_device *dev = queue->dev; 9761d24eb48STom Herbert int i; 9771d24eb48STom Herbert 9781d24eb48STom Herbert for (i = 0; i < dev->num_tx_queues; i++) 9791d24eb48STom Herbert if (queue == &dev->_tx[i]) 9801d24eb48STom Herbert break; 9811d24eb48STom Herbert 9821d24eb48STom Herbert BUG_ON(i >= dev->num_tx_queues); 9831d24eb48STom Herbert 9841d24eb48STom Herbert return i; 9851d24eb48STom Herbert } 9861d24eb48STom Herbert 9871d24eb48STom Herbert 9881d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue, 9891d24eb48STom Herbert struct netdev_queue_attribute *attribute, char *buf) 9901d24eb48STom Herbert { 9911d24eb48STom Herbert struct net_device *dev = queue->dev; 9921d24eb48STom Herbert struct xps_dev_maps *dev_maps; 9931d24eb48STom Herbert cpumask_var_t mask; 9941d24eb48STom Herbert unsigned long index; 9951d24eb48STom Herbert size_t len = 0; 9961d24eb48STom Herbert int i; 9971d24eb48STom Herbert 9981d24eb48STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 9991d24eb48STom Herbert return -ENOMEM; 10001d24eb48STom Herbert 10011d24eb48STom Herbert index = get_netdev_queue_index(queue); 10021d24eb48STom Herbert 10031d24eb48STom Herbert rcu_read_lock(); 10041d24eb48STom Herbert dev_maps = rcu_dereference(dev->xps_maps); 10051d24eb48STom Herbert if (dev_maps) { 10061d24eb48STom Herbert for_each_possible_cpu(i) { 10071d24eb48STom Herbert struct xps_map *map = 10081d24eb48STom Herbert rcu_dereference(dev_maps->cpu_map[i]); 10091d24eb48STom Herbert if (map) { 10101d24eb48STom Herbert int j; 10111d24eb48STom Herbert for (j = 0; j < map->len; j++) { 10121d24eb48STom Herbert if (map->queues[j] == index) { 10131d24eb48STom Herbert cpumask_set_cpu(i, mask); 10141d24eb48STom Herbert break; 10151d24eb48STom Herbert } 10161d24eb48STom Herbert } 10171d24eb48STom Herbert } 10181d24eb48STom Herbert } 10191d24eb48STom Herbert } 10201d24eb48STom Herbert rcu_read_unlock(); 10211d24eb48STom Herbert 10221d24eb48STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 10231d24eb48STom Herbert if (PAGE_SIZE - len < 3) { 10241d24eb48STom Herbert free_cpumask_var(mask); 10251d24eb48STom Herbert return -EINVAL; 10261d24eb48STom Herbert } 10271d24eb48STom Herbert 10281d24eb48STom Herbert free_cpumask_var(mask); 10291d24eb48STom Herbert len += sprintf(buf + len, "\n"); 10301d24eb48STom Herbert return len; 10311d24eb48STom Herbert } 10321d24eb48STom Herbert 10331d24eb48STom Herbert static DEFINE_MUTEX(xps_map_mutex); 1034a4177869SEric Dumazet #define xmap_dereference(P) \ 1035a4177869SEric Dumazet rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) 10361d24eb48STom Herbert 1037927fbec1STom Herbert static void xps_queue_release(struct netdev_queue *queue) 1038927fbec1STom Herbert { 1039927fbec1STom Herbert struct net_device *dev = queue->dev; 1040927fbec1STom Herbert struct xps_dev_maps *dev_maps; 1041927fbec1STom Herbert struct xps_map *map; 1042927fbec1STom Herbert unsigned long index; 1043927fbec1STom Herbert int i, pos, nonempty = 0; 1044927fbec1STom Herbert 1045927fbec1STom Herbert index = get_netdev_queue_index(queue); 1046927fbec1STom Herbert 1047927fbec1STom Herbert mutex_lock(&xps_map_mutex); 1048927fbec1STom Herbert dev_maps = xmap_dereference(dev->xps_maps); 1049927fbec1STom Herbert 1050927fbec1STom Herbert if (dev_maps) { 1051927fbec1STom Herbert for_each_possible_cpu(i) { 1052927fbec1STom Herbert map = xmap_dereference(dev_maps->cpu_map[i]); 1053927fbec1STom Herbert if (!map) 1054927fbec1STom Herbert continue; 1055927fbec1STom Herbert 1056927fbec1STom Herbert for (pos = 0; pos < map->len; pos++) 1057927fbec1STom Herbert if (map->queues[pos] == index) 1058927fbec1STom Herbert break; 1059927fbec1STom Herbert 1060927fbec1STom Herbert if (pos < map->len) { 1061927fbec1STom Herbert if (map->len > 1) 1062927fbec1STom Herbert map->queues[pos] = 1063927fbec1STom Herbert map->queues[--map->len]; 1064927fbec1STom Herbert else { 1065927fbec1STom Herbert RCU_INIT_POINTER(dev_maps->cpu_map[i], 1066927fbec1STom Herbert NULL); 1067927fbec1STom Herbert kfree_rcu(map, rcu); 1068927fbec1STom Herbert map = NULL; 1069927fbec1STom Herbert } 1070927fbec1STom Herbert } 1071927fbec1STom Herbert if (map) 1072927fbec1STom Herbert nonempty = 1; 1073927fbec1STom Herbert } 1074927fbec1STom Herbert 1075927fbec1STom Herbert if (!nonempty) { 1076927fbec1STom Herbert RCU_INIT_POINTER(dev->xps_maps, NULL); 1077927fbec1STom Herbert kfree_rcu(dev_maps, rcu); 1078927fbec1STom Herbert } 1079927fbec1STom Herbert } 1080927fbec1STom Herbert mutex_unlock(&xps_map_mutex); 1081927fbec1STom Herbert } 1082927fbec1STom Herbert 10831d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue, 10841d24eb48STom Herbert struct netdev_queue_attribute *attribute, 10851d24eb48STom Herbert const char *buf, size_t len) 10861d24eb48STom Herbert { 10871d24eb48STom Herbert struct net_device *dev = queue->dev; 10881d24eb48STom Herbert cpumask_var_t mask; 10891d24eb48STom Herbert int err, i, cpu, pos, map_len, alloc_len, need_set; 10901d24eb48STom Herbert unsigned long index; 10911d24eb48STom Herbert struct xps_map *map, *new_map; 10921d24eb48STom Herbert struct xps_dev_maps *dev_maps, *new_dev_maps; 10931d24eb48STom Herbert int nonempty = 0; 109419b05f81Sdavid decotigny int numa_node_id = -2; 10951d24eb48STom Herbert 10961d24eb48STom Herbert if (!capable(CAP_NET_ADMIN)) 10971d24eb48STom Herbert return -EPERM; 10981d24eb48STom Herbert 10991d24eb48STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 11001d24eb48STom Herbert return -ENOMEM; 11011d24eb48STom Herbert 11021d24eb48STom Herbert index = get_netdev_queue_index(queue); 11031d24eb48STom Herbert 11041d24eb48STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 11051d24eb48STom Herbert if (err) { 11061d24eb48STom Herbert free_cpumask_var(mask); 11071d24eb48STom Herbert return err; 11081d24eb48STom Herbert } 11091d24eb48STom Herbert 11101d24eb48STom Herbert new_dev_maps = kzalloc(max_t(unsigned, 11111d24eb48STom Herbert XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL); 11121d24eb48STom Herbert if (!new_dev_maps) { 11131d24eb48STom Herbert free_cpumask_var(mask); 11141d24eb48STom Herbert return -ENOMEM; 11151d24eb48STom Herbert } 11161d24eb48STom Herbert 11171d24eb48STom Herbert mutex_lock(&xps_map_mutex); 11181d24eb48STom Herbert 1119a4177869SEric Dumazet dev_maps = xmap_dereference(dev->xps_maps); 11201d24eb48STom Herbert 11211d24eb48STom Herbert for_each_possible_cpu(cpu) { 1122a4177869SEric Dumazet map = dev_maps ? 1123a4177869SEric Dumazet xmap_dereference(dev_maps->cpu_map[cpu]) : NULL; 1124a4177869SEric Dumazet new_map = map; 11251d24eb48STom Herbert if (map) { 11261d24eb48STom Herbert for (pos = 0; pos < map->len; pos++) 11271d24eb48STom Herbert if (map->queues[pos] == index) 11281d24eb48STom Herbert break; 11291d24eb48STom Herbert map_len = map->len; 11301d24eb48STom Herbert alloc_len = map->alloc_len; 11311d24eb48STom Herbert } else 11321d24eb48STom Herbert pos = map_len = alloc_len = 0; 11331d24eb48STom Herbert 11342142c131SKOSAKI Motohiro need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu); 1135f2cd2d3eSEric Dumazet #ifdef CONFIG_NUMA 1136f2cd2d3eSEric Dumazet if (need_set) { 113719b05f81Sdavid decotigny if (numa_node_id == -2) 113819b05f81Sdavid decotigny numa_node_id = cpu_to_node(cpu); 113919b05f81Sdavid decotigny else if (numa_node_id != cpu_to_node(cpu)) 114019b05f81Sdavid decotigny numa_node_id = -1; 1141f2cd2d3eSEric Dumazet } 1142f2cd2d3eSEric Dumazet #endif 11431d24eb48STom Herbert if (need_set && pos >= map_len) { 11441d24eb48STom Herbert /* Need to add queue to this CPU's map */ 11451d24eb48STom Herbert if (map_len >= alloc_len) { 11461d24eb48STom Herbert alloc_len = alloc_len ? 11471d24eb48STom Herbert 2 * alloc_len : XPS_MIN_MAP_ALLOC; 1148b02038a1SEric Dumazet new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), 1149b02038a1SEric Dumazet GFP_KERNEL, 1150b02038a1SEric Dumazet cpu_to_node(cpu)); 11511d24eb48STom Herbert if (!new_map) 11521d24eb48STom Herbert goto error; 11531d24eb48STom Herbert new_map->alloc_len = alloc_len; 11541d24eb48STom Herbert for (i = 0; i < map_len; i++) 11551d24eb48STom Herbert new_map->queues[i] = map->queues[i]; 11561d24eb48STom Herbert new_map->len = map_len; 11571d24eb48STom Herbert } 11581d24eb48STom Herbert new_map->queues[new_map->len++] = index; 11591d24eb48STom Herbert } else if (!need_set && pos < map_len) { 11601d24eb48STom Herbert /* Need to remove queue from this CPU's map */ 11611d24eb48STom Herbert if (map_len > 1) 11621d24eb48STom Herbert new_map->queues[pos] = 11631d24eb48STom Herbert new_map->queues[--new_map->len]; 11641d24eb48STom Herbert else 11651d24eb48STom Herbert new_map = NULL; 11661d24eb48STom Herbert } 1167a4177869SEric Dumazet RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map); 11681d24eb48STom Herbert } 11691d24eb48STom Herbert 11701d24eb48STom Herbert /* Cleanup old maps */ 11711d24eb48STom Herbert for_each_possible_cpu(cpu) { 1172a4177869SEric Dumazet map = dev_maps ? 1173a4177869SEric Dumazet xmap_dereference(dev_maps->cpu_map[cpu]) : NULL; 1174a4177869SEric Dumazet if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map) 1175edc86d8aSLai Jiangshan kfree_rcu(map, rcu); 11761d24eb48STom Herbert if (new_dev_maps->cpu_map[cpu]) 11771d24eb48STom Herbert nonempty = 1; 11781d24eb48STom Herbert } 11791d24eb48STom Herbert 1180*cf778b00SEric Dumazet if (nonempty) { 1181*cf778b00SEric Dumazet rcu_assign_pointer(dev->xps_maps, new_dev_maps); 1182*cf778b00SEric Dumazet } else { 11831d24eb48STom Herbert kfree(new_dev_maps); 1184a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->xps_maps, NULL); 11851d24eb48STom Herbert } 11861d24eb48STom Herbert 11871d24eb48STom Herbert if (dev_maps) 1188b55071ebSLai Jiangshan kfree_rcu(dev_maps, rcu); 11891d24eb48STom Herbert 119019b05f81Sdavid decotigny netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id : 1191b236da69SChangli Gao NUMA_NO_NODE); 1192f2cd2d3eSEric Dumazet 11931d24eb48STom Herbert mutex_unlock(&xps_map_mutex); 11941d24eb48STom Herbert 11951d24eb48STom Herbert free_cpumask_var(mask); 11961d24eb48STom Herbert return len; 11971d24eb48STom Herbert 11981d24eb48STom Herbert error: 11991d24eb48STom Herbert mutex_unlock(&xps_map_mutex); 12001d24eb48STom Herbert 12011d24eb48STom Herbert if (new_dev_maps) 12021d24eb48STom Herbert for_each_possible_cpu(i) 1203a4177869SEric Dumazet kfree(rcu_dereference_protected( 1204a4177869SEric Dumazet new_dev_maps->cpu_map[i], 1205a4177869SEric Dumazet 1)); 12061d24eb48STom Herbert kfree(new_dev_maps); 12071d24eb48STom Herbert free_cpumask_var(mask); 12081d24eb48STom Herbert return -ENOMEM; 12091d24eb48STom Herbert } 12101d24eb48STom Herbert 12111d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute = 12121d24eb48STom Herbert __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map); 1213ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */ 12141d24eb48STom Herbert 12151d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = { 1216ccf5ff69Sdavid decotigny &queue_trans_timeout.attr, 1217ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 12181d24eb48STom Herbert &xps_cpus_attribute.attr, 1219ccf5ff69Sdavid decotigny #endif 12201d24eb48STom Herbert NULL 12211d24eb48STom Herbert }; 12221d24eb48STom Herbert 12231d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj) 12241d24eb48STom Herbert { 12251d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 12261d24eb48STom Herbert 1227114cf580STom Herbert #ifdef CONFIG_XPS 1228927fbec1STom Herbert xps_queue_release(queue); 1229114cf580STom Herbert #endif 12301d24eb48STom Herbert 12311d24eb48STom Herbert memset(kobj, 0, sizeof(*kobj)); 12321d24eb48STom Herbert dev_put(queue->dev); 12331d24eb48STom Herbert } 12341d24eb48STom Herbert 12351d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = { 12361d24eb48STom Herbert .sysfs_ops = &netdev_queue_sysfs_ops, 12371d24eb48STom Herbert .release = netdev_queue_release, 12381d24eb48STom Herbert .default_attrs = netdev_queue_default_attrs, 12391d24eb48STom Herbert }; 12401d24eb48STom Herbert 12411d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index) 12421d24eb48STom Herbert { 12431d24eb48STom Herbert struct netdev_queue *queue = net->_tx + index; 12441d24eb48STom Herbert struct kobject *kobj = &queue->kobj; 12451d24eb48STom Herbert int error = 0; 12461d24eb48STom Herbert 12471d24eb48STom Herbert kobj->kset = net->queues_kset; 12481d24eb48STom Herbert error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, 12491d24eb48STom Herbert "tx-%u", index); 1250114cf580STom Herbert if (error) 1251114cf580STom Herbert goto exit; 1252114cf580STom Herbert 1253114cf580STom Herbert #ifdef CONFIG_BQL 1254114cf580STom Herbert error = sysfs_create_group(kobj, &dql_group); 1255114cf580STom Herbert if (error) 1256114cf580STom Herbert goto exit; 1257114cf580STom Herbert #endif 12581d24eb48STom Herbert 12591d24eb48STom Herbert kobject_uevent(kobj, KOBJ_ADD); 12601d24eb48STom Herbert dev_hold(queue->dev); 12611d24eb48STom Herbert 1262114cf580STom Herbert return 0; 1263114cf580STom Herbert exit: 1264114cf580STom Herbert kobject_put(kobj); 12651d24eb48STom Herbert return error; 12661d24eb48STom Herbert } 1267ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 12681d24eb48STom Herbert 12691d24eb48STom Herbert int 12701d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 12711d24eb48STom Herbert { 1272ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 12731d24eb48STom Herbert int i; 12741d24eb48STom Herbert int error = 0; 12751d24eb48STom Herbert 12761d24eb48STom Herbert for (i = old_num; i < new_num; i++) { 12771d24eb48STom Herbert error = netdev_queue_add_kobject(net, i); 12781d24eb48STom Herbert if (error) { 12791d24eb48STom Herbert new_num = old_num; 12801d24eb48STom Herbert break; 12811d24eb48STom Herbert } 12821d24eb48STom Herbert } 12831d24eb48STom Herbert 1284114cf580STom Herbert while (--i >= new_num) { 1285114cf580STom Herbert struct netdev_queue *queue = net->_tx + i; 1286114cf580STom Herbert 1287114cf580STom Herbert #ifdef CONFIG_BQL 1288114cf580STom Herbert sysfs_remove_group(&queue->kobj, &dql_group); 1289114cf580STom Herbert #endif 1290114cf580STom Herbert kobject_put(&queue->kobj); 1291114cf580STom Herbert } 12921d24eb48STom Herbert 12931d24eb48STom Herbert return error; 1294bf264145STom Herbert #else 1295bf264145STom Herbert return 0; 1296ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 12971d24eb48STom Herbert } 12981d24eb48STom Herbert 12991d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net) 13001d24eb48STom Herbert { 1301bf264145STom Herbert int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; 13021d24eb48STom Herbert 1303ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 130462fe0b40SBen Hutchings net->queues_kset = kset_create_and_add("queues", 130562fe0b40SBen Hutchings NULL, &net->dev.kobj); 130662fe0b40SBen Hutchings if (!net->queues_kset) 130762fe0b40SBen Hutchings return -ENOMEM; 1308bf264145STom Herbert #endif 13091d24eb48STom Herbert 1310bf264145STom Herbert #ifdef CONFIG_RPS 1311bf264145STom Herbert real_rx = net->real_num_rx_queues; 1312bf264145STom Herbert #endif 1313bf264145STom Herbert real_tx = net->real_num_tx_queues; 1314bf264145STom Herbert 1315bf264145STom Herbert error = net_rx_queue_update_kobjects(net, 0, real_rx); 13161d24eb48STom Herbert if (error) 13171d24eb48STom Herbert goto error; 1318bf264145STom Herbert rxq = real_rx; 13191d24eb48STom Herbert 1320bf264145STom Herbert error = netdev_queue_update_kobjects(net, 0, real_tx); 13211d24eb48STom Herbert if (error) 13221d24eb48STom Herbert goto error; 1323bf264145STom Herbert txq = real_tx; 13241d24eb48STom Herbert 13251d24eb48STom Herbert return 0; 13261d24eb48STom Herbert 13271d24eb48STom Herbert error: 13281d24eb48STom Herbert netdev_queue_update_kobjects(net, txq, 0); 13291d24eb48STom Herbert net_rx_queue_update_kobjects(net, rxq, 0); 13301d24eb48STom Herbert return error; 133162fe0b40SBen Hutchings } 133262fe0b40SBen Hutchings 13331d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net) 13340a9627f2STom Herbert { 1335bf264145STom Herbert int real_rx = 0, real_tx = 0; 1336bf264145STom Herbert 1337bf264145STom Herbert #ifdef CONFIG_RPS 1338bf264145STom Herbert real_rx = net->real_num_rx_queues; 1339bf264145STom Herbert #endif 1340bf264145STom Herbert real_tx = net->real_num_tx_queues; 1341bf264145STom Herbert 1342bf264145STom Herbert net_rx_queue_update_kobjects(net, real_rx, 0); 1343bf264145STom Herbert netdev_queue_update_kobjects(net, real_tx, 0); 1344ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 13450a9627f2STom Herbert kset_unregister(net->queues_kset); 1346bf264145STom Herbert #endif 13470a9627f2STom Herbert } 1348608b4b95SEric W. Biederman 1349a685e089SAl Viro static void *net_grab_current_ns(void) 1350608b4b95SEric W. Biederman { 1351a685e089SAl Viro struct net *ns = current->nsproxy->net_ns; 1352a685e089SAl Viro #ifdef CONFIG_NET_NS 1353a685e089SAl Viro if (ns) 1354a685e089SAl Viro atomic_inc(&ns->passive); 1355a685e089SAl Viro #endif 1356a685e089SAl Viro return ns; 1357608b4b95SEric W. Biederman } 1358608b4b95SEric W. Biederman 1359608b4b95SEric W. Biederman static const void *net_initial_ns(void) 1360608b4b95SEric W. Biederman { 1361608b4b95SEric W. Biederman return &init_net; 1362608b4b95SEric W. Biederman } 1363608b4b95SEric W. Biederman 1364608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk) 1365608b4b95SEric W. Biederman { 1366608b4b95SEric W. Biederman return sock_net(sk); 1367608b4b95SEric W. Biederman } 1368608b4b95SEric W. Biederman 136904600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = { 1370608b4b95SEric W. Biederman .type = KOBJ_NS_TYPE_NET, 1371a685e089SAl Viro .grab_current_ns = net_grab_current_ns, 1372608b4b95SEric W. Biederman .netlink_ns = net_netlink_ns, 1373608b4b95SEric W. Biederman .initial_ns = net_initial_ns, 1374a685e089SAl Viro .drop_ns = net_drop_ns, 1375608b4b95SEric W. Biederman }; 137604600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations); 1377608b4b95SEric W. Biederman 13781da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG 13797eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) 13801da177e4SLinus Torvalds { 138143cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 13827eff2e7aSKay Sievers int retval; 13831da177e4SLinus Torvalds 1384312c004dSKay Sievers /* pass interface to uevent. */ 13857eff2e7aSKay Sievers retval = add_uevent_var(env, "INTERFACE=%s", dev->name); 1386bf62456eSEric Rannaud if (retval) 1387bf62456eSEric Rannaud goto exit; 13881da177e4SLinus Torvalds 1389ca2f37dbSJean Tourrilhes /* pass ifindex to uevent. 1390ca2f37dbSJean Tourrilhes * ifindex is useful as it won't change (interface name may change) 1391ca2f37dbSJean Tourrilhes * and is what RtNetlink uses natively. */ 13927eff2e7aSKay Sievers retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); 1393ca2f37dbSJean Tourrilhes 1394bf62456eSEric Rannaud exit: 1395bf62456eSEric Rannaud return retval; 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds #endif 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds /* 14001da177e4SLinus Torvalds * netdev_release -- destroy and free a dead device. 140143cb76d9SGreg Kroah-Hartman * Called when last reference to device kobject is gone. 14021da177e4SLinus Torvalds */ 140343cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d) 14041da177e4SLinus Torvalds { 140543cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds BUG_ON(dev->reg_state != NETREG_RELEASED); 14081da177e4SLinus Torvalds 14090b815a1aSStephen Hemminger kfree(dev->ifalias); 14101da177e4SLinus Torvalds kfree((char *)dev - dev->padded); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413608b4b95SEric W. Biederman static const void *net_namespace(struct device *d) 1414608b4b95SEric W. Biederman { 1415608b4b95SEric W. Biederman struct net_device *dev; 1416608b4b95SEric W. Biederman dev = container_of(d, struct net_device, dev); 1417608b4b95SEric W. Biederman return dev_net(dev); 1418608b4b95SEric W. Biederman } 1419608b4b95SEric W. Biederman 14201da177e4SLinus Torvalds static struct class net_class = { 14211da177e4SLinus Torvalds .name = "net", 142243cb76d9SGreg Kroah-Hartman .dev_release = netdev_release, 14238b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 142443cb76d9SGreg Kroah-Hartman .dev_attrs = net_class_attributes, 14258b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */ 14261da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG 142743cb76d9SGreg Kroah-Hartman .dev_uevent = netdev_uevent, 14281da177e4SLinus Torvalds #endif 1429608b4b95SEric W. Biederman .ns_type = &net_ns_type_operations, 1430608b4b95SEric W. Biederman .namespace = net_namespace, 14311da177e4SLinus Torvalds }; 14321da177e4SLinus Torvalds 14339093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all 14349093bbb2SStephen Hemminger * netdev references are gone. 14359093bbb2SStephen Hemminger */ 14368b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net) 14371da177e4SLinus Torvalds { 14389093bbb2SStephen Hemminger struct device *dev = &(net->dev); 14399093bbb2SStephen Hemminger 14409093bbb2SStephen Hemminger kobject_get(&dev->kobj); 14413891845eSEric W. Biederman 14421d24eb48STom Herbert remove_queue_kobjects(net); 14430a9627f2STom Herbert 14449093bbb2SStephen Hemminger device_del(dev); 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds /* Create sysfs entries for network device. */ 14488b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net) 14491da177e4SLinus Torvalds { 145043cb76d9SGreg Kroah-Hartman struct device *dev = &(net->dev); 1451a4dbd674SDavid Brownell const struct attribute_group **groups = net->sysfs_groups; 14520a9627f2STom Herbert int error = 0; 14531da177e4SLinus Torvalds 1454a1b3f594SEric W. Biederman device_initialize(dev); 145543cb76d9SGreg Kroah-Hartman dev->class = &net_class; 145643cb76d9SGreg Kroah-Hartman dev->platform_data = net; 145743cb76d9SGreg Kroah-Hartman dev->groups = groups; 14581da177e4SLinus Torvalds 1459a2205472SStephen Hemminger dev_set_name(dev, "%s", net->name); 14601da177e4SLinus Torvalds 14618b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 14620c509a6cSEric W. Biederman /* Allow for a device specific group */ 14630c509a6cSEric W. Biederman if (*groups) 14640c509a6cSEric W. Biederman groups++; 14651da177e4SLinus Torvalds 14660c509a6cSEric W. Biederman *groups++ = &netstat_group; 146722bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS 14683d23e349SJohannes Berg if (net->ieee80211_ptr) 1469fe9925b5SStephen Hemminger *groups++ = &wireless_group; 14703d23e349SJohannes Berg #ifdef CONFIG_WIRELESS_EXT 14713d23e349SJohannes Berg else if (net->wireless_handlers) 14723d23e349SJohannes Berg *groups++ = &wireless_group; 14733d23e349SJohannes Berg #endif 14741da177e4SLinus Torvalds #endif 14758b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */ 14761da177e4SLinus Torvalds 14770a9627f2STom Herbert error = device_add(dev); 14780a9627f2STom Herbert if (error) 14790a9627f2STom Herbert return error; 14800a9627f2STom Herbert 14811d24eb48STom Herbert error = register_queue_kobjects(net); 14820a9627f2STom Herbert if (error) { 14830a9627f2STom Herbert device_del(dev); 14840a9627f2STom Herbert return error; 14850a9627f2STom Herbert } 14860a9627f2STom Herbert 14870a9627f2STom Herbert return error; 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 1490b8a9787eSJay Vosburgh int netdev_class_create_file(struct class_attribute *class_attr) 1491b8a9787eSJay Vosburgh { 1492b8a9787eSJay Vosburgh return class_create_file(&net_class, class_attr); 1493b8a9787eSJay Vosburgh } 14949e34a5b5SEric Dumazet EXPORT_SYMBOL(netdev_class_create_file); 1495b8a9787eSJay Vosburgh 1496b8a9787eSJay Vosburgh void netdev_class_remove_file(struct class_attribute *class_attr) 1497b8a9787eSJay Vosburgh { 1498b8a9787eSJay Vosburgh class_remove_file(&net_class, class_attr); 1499b8a9787eSJay Vosburgh } 1500b8a9787eSJay Vosburgh EXPORT_SYMBOL(netdev_class_remove_file); 1501b8a9787eSJay Vosburgh 15028b41d188SEric W. Biederman int netdev_kobject_init(void) 15031da177e4SLinus Torvalds { 1504608b4b95SEric W. Biederman kobj_ns_type_register(&net_ns_type_operations); 15051da177e4SLinus Torvalds return class_register(&net_class); 15061da177e4SLinus Torvalds } 1507