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> 21fec5e652STom Herbert #include <linux/vmalloc.h> 22bc3b2d7fSPaul Gortmaker #include <linux/export.h> 23114cf580STom Herbert #include <linux/jiffies.h> 249802c8e2SMing Lei #include <linux/pm_runtime.h> 251da177e4SLinus Torvalds 26342709efSPavel Emelyanov #include "net-sysfs.h" 27342709efSPavel Emelyanov 288b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 291da177e4SLinus Torvalds static const char fmt_hex[] = "%#x\n"; 30d1102b59SDavid S. Miller static const char fmt_long_hex[] = "%#lx\n"; 311da177e4SLinus Torvalds static const char fmt_dec[] = "%d\n"; 328ae6dacaSDavid Decotigny static const char fmt_udec[] = "%u\n"; 331da177e4SLinus Torvalds static const char fmt_ulong[] = "%lu\n"; 34be1f3c2cSBen Hutchings static const char fmt_u64[] = "%llu\n"; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds static inline int dev_isalive(const struct net_device *dev) 371da177e4SLinus Torvalds { 38fe9925b5SStephen Hemminger return dev->reg_state <= NETREG_REGISTERED; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* use same locking rules as GIF* ioctl's */ 4243cb76d9SGreg Kroah-Hartman static ssize_t netdev_show(const struct device *dev, 4343cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf, 441da177e4SLinus Torvalds ssize_t (*format)(const struct net_device *, char *)) 451da177e4SLinus Torvalds { 4643cb76d9SGreg Kroah-Hartman struct net_device *net = to_net_dev(dev); 471da177e4SLinus Torvalds ssize_t ret = -EINVAL; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds read_lock(&dev_base_lock); 501da177e4SLinus Torvalds if (dev_isalive(net)) 511da177e4SLinus Torvalds ret = (*format)(net, buf); 521da177e4SLinus Torvalds read_unlock(&dev_base_lock); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds return ret; 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* generate a show function for simple field */ 581da177e4SLinus Torvalds #define NETDEVICE_SHOW(field, format_string) \ 591da177e4SLinus Torvalds static ssize_t format_##field(const struct net_device *net, char *buf) \ 601da177e4SLinus Torvalds { \ 611da177e4SLinus Torvalds return sprintf(buf, format_string, net->field); \ 621da177e4SLinus Torvalds } \ 636be8aeefSGreg Kroah-Hartman static ssize_t field##_show(struct device *dev, \ 6443cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 651da177e4SLinus Torvalds { \ 6643cb76d9SGreg Kroah-Hartman return netdev_show(dev, attr, buf, format_##field); \ 676be8aeefSGreg Kroah-Hartman } \ 681da177e4SLinus Torvalds 696be8aeefSGreg Kroah-Hartman #define NETDEVICE_SHOW_RO(field, format_string) \ 706be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW(field, format_string); \ 716be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(field) 726be8aeefSGreg Kroah-Hartman 736be8aeefSGreg Kroah-Hartman #define NETDEVICE_SHOW_RW(field, format_string) \ 746be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW(field, format_string); \ 756be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RW(field) 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* use same locking and permission rules as SIF* ioctl's */ 7843cb76d9SGreg Kroah-Hartman static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, 791da177e4SLinus Torvalds const char *buf, size_t len, 801da177e4SLinus Torvalds int (*set)(struct net_device *, unsigned long)) 811da177e4SLinus Torvalds { 825e1fccc0SEric W. Biederman struct net_device *netdev = to_net_dev(dev); 835e1fccc0SEric W. Biederman struct net *net = dev_net(netdev); 841da177e4SLinus Torvalds unsigned long new; 851da177e4SLinus Torvalds int ret = -EINVAL; 861da177e4SLinus Torvalds 875e1fccc0SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 881da177e4SLinus Torvalds return -EPERM; 891da177e4SLinus Torvalds 90e1e420c7SShuah Khan ret = kstrtoul(buf, 0, &new); 91e1e420c7SShuah Khan if (ret) 921da177e4SLinus Torvalds goto err; 931da177e4SLinus Torvalds 945a5990d3SStephen Hemminger if (!rtnl_trylock()) 95336ca57cSEric W. Biederman return restart_syscall(); 965a5990d3SStephen Hemminger 975e1fccc0SEric W. Biederman if (dev_isalive(netdev)) { 985e1fccc0SEric W. Biederman if ((ret = (*set)(netdev, new)) == 0) 991da177e4SLinus Torvalds ret = len; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds rtnl_unlock(); 1021da177e4SLinus Torvalds err: 1031da177e4SLinus Torvalds return ret; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1066be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(dev_id, fmt_hex); 1076be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec); 1086be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(addr_len, fmt_dec); 1096be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(iflink, fmt_dec); 1106be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(ifindex, fmt_dec); 1116be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(type, fmt_dec); 1126be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RO(link_mode, fmt_dec); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* use same locking rules as GIFHWADDR ioctl's */ 1156be8aeefSGreg Kroah-Hartman static ssize_t address_show(struct device *dev, struct device_attribute *attr, 11643cb76d9SGreg Kroah-Hartman char *buf) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1191da177e4SLinus Torvalds ssize_t ret = -EINVAL; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds read_lock(&dev_base_lock); 1221da177e4SLinus Torvalds if (dev_isalive(net)) 1237ffc49a6SMichael Chan ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); 1241da177e4SLinus Torvalds read_unlock(&dev_base_lock); 1251da177e4SLinus Torvalds return ret; 1261da177e4SLinus Torvalds } 1276be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(address); 1281da177e4SLinus Torvalds 1296be8aeefSGreg Kroah-Hartman static ssize_t broadcast_show(struct device *dev, 13043cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct net_device *net = to_net_dev(dev); 1331da177e4SLinus Torvalds if (dev_isalive(net)) 1347ffc49a6SMichael Chan return sysfs_format_mac(buf, net->broadcast, net->addr_len); 1351da177e4SLinus Torvalds return -EINVAL; 1361da177e4SLinus Torvalds } 1376be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(broadcast); 1381da177e4SLinus Torvalds 139fdae0fdeSJiri Pirko static int change_carrier(struct net_device *net, unsigned long new_carrier) 140fdae0fdeSJiri Pirko { 141fdae0fdeSJiri Pirko if (!netif_running(net)) 142fdae0fdeSJiri Pirko return -EINVAL; 143fdae0fdeSJiri Pirko return dev_change_carrier(net, (bool) new_carrier); 144fdae0fdeSJiri Pirko } 145fdae0fdeSJiri Pirko 1466be8aeefSGreg Kroah-Hartman static ssize_t carrier_store(struct device *dev, struct device_attribute *attr, 147fdae0fdeSJiri Pirko const char *buf, size_t len) 148fdae0fdeSJiri Pirko { 149fdae0fdeSJiri Pirko return netdev_store(dev, attr, buf, len, change_carrier); 150fdae0fdeSJiri Pirko } 151fdae0fdeSJiri Pirko 1526be8aeefSGreg Kroah-Hartman static ssize_t carrier_show(struct device *dev, 15343cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds struct net_device *netdev = to_net_dev(dev); 1561da177e4SLinus Torvalds if (netif_running(netdev)) { 1571da177e4SLinus Torvalds return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds return -EINVAL; 1601da177e4SLinus Torvalds } 1616be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RW(carrier); 1621da177e4SLinus Torvalds 1636be8aeefSGreg Kroah-Hartman static ssize_t speed_show(struct device *dev, 164d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 165d519e17eSAndy Gospodarek { 166d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 167d519e17eSAndy Gospodarek int ret = -EINVAL; 168d519e17eSAndy Gospodarek 169d519e17eSAndy Gospodarek if (!rtnl_trylock()) 170d519e17eSAndy Gospodarek return restart_syscall(); 171d519e17eSAndy Gospodarek 1728ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1738ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 1744bc71cb9SJiri Pirko if (!__ethtool_get_settings(netdev, &cmd)) 1758ae6dacaSDavid Decotigny ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd)); 176d519e17eSAndy Gospodarek } 177d519e17eSAndy Gospodarek rtnl_unlock(); 178d519e17eSAndy Gospodarek return ret; 179d519e17eSAndy Gospodarek } 1806be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(speed); 181d519e17eSAndy Gospodarek 1826be8aeefSGreg Kroah-Hartman static ssize_t duplex_show(struct device *dev, 183d519e17eSAndy Gospodarek struct device_attribute *attr, char *buf) 184d519e17eSAndy Gospodarek { 185d519e17eSAndy Gospodarek struct net_device *netdev = to_net_dev(dev); 186d519e17eSAndy Gospodarek int ret = -EINVAL; 187d519e17eSAndy Gospodarek 188d519e17eSAndy Gospodarek if (!rtnl_trylock()) 189d519e17eSAndy Gospodarek return restart_syscall(); 190d519e17eSAndy Gospodarek 1918ae6dacaSDavid Decotigny if (netif_running(netdev)) { 1928ae6dacaSDavid Decotigny struct ethtool_cmd cmd; 193c6c13965SNikolay Aleksandrov if (!__ethtool_get_settings(netdev, &cmd)) { 194c6c13965SNikolay Aleksandrov const char *duplex; 195c6c13965SNikolay Aleksandrov switch (cmd.duplex) { 196c6c13965SNikolay Aleksandrov case DUPLEX_HALF: 197c6c13965SNikolay Aleksandrov duplex = "half"; 198c6c13965SNikolay Aleksandrov break; 199c6c13965SNikolay Aleksandrov case DUPLEX_FULL: 200c6c13965SNikolay Aleksandrov duplex = "full"; 201c6c13965SNikolay Aleksandrov break; 202c6c13965SNikolay Aleksandrov default: 203c6c13965SNikolay Aleksandrov duplex = "unknown"; 204c6c13965SNikolay Aleksandrov break; 205c6c13965SNikolay Aleksandrov } 206c6c13965SNikolay Aleksandrov ret = sprintf(buf, "%s\n", duplex); 207c6c13965SNikolay Aleksandrov } 208d519e17eSAndy Gospodarek } 209d519e17eSAndy Gospodarek rtnl_unlock(); 210d519e17eSAndy Gospodarek return ret; 211d519e17eSAndy Gospodarek } 2126be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(duplex); 213d519e17eSAndy Gospodarek 2146be8aeefSGreg Kroah-Hartman static ssize_t dormant_show(struct device *dev, 21543cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 216b00055aaSStefan Rompf { 217b00055aaSStefan Rompf struct net_device *netdev = to_net_dev(dev); 218b00055aaSStefan Rompf 219b00055aaSStefan Rompf if (netif_running(netdev)) 220b00055aaSStefan Rompf return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); 221b00055aaSStefan Rompf 222b00055aaSStefan Rompf return -EINVAL; 223b00055aaSStefan Rompf } 2246be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(dormant); 225b00055aaSStefan Rompf 22636cbd3dcSJan Engelhardt static const char *const operstates[] = { 227b00055aaSStefan Rompf "unknown", 228b00055aaSStefan Rompf "notpresent", /* currently unused */ 229b00055aaSStefan Rompf "down", 230b00055aaSStefan Rompf "lowerlayerdown", 231b00055aaSStefan Rompf "testing", /* currently unused */ 232b00055aaSStefan Rompf "dormant", 233b00055aaSStefan Rompf "up" 234b00055aaSStefan Rompf }; 235b00055aaSStefan Rompf 2366be8aeefSGreg Kroah-Hartman static ssize_t operstate_show(struct device *dev, 23743cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 238b00055aaSStefan Rompf { 239b00055aaSStefan Rompf const struct net_device *netdev = to_net_dev(dev); 240b00055aaSStefan Rompf unsigned char operstate; 241b00055aaSStefan Rompf 242b00055aaSStefan Rompf read_lock(&dev_base_lock); 243b00055aaSStefan Rompf operstate = netdev->operstate; 244b00055aaSStefan Rompf if (!netif_running(netdev)) 245b00055aaSStefan Rompf operstate = IF_OPER_DOWN; 246b00055aaSStefan Rompf read_unlock(&dev_base_lock); 247b00055aaSStefan Rompf 248e3a5cd9eSAdrian Bunk if (operstate >= ARRAY_SIZE(operstates)) 249b00055aaSStefan Rompf return -EINVAL; /* should not happen */ 250b00055aaSStefan Rompf 251b00055aaSStefan Rompf return sprintf(buf, "%s\n", operstates[operstate]); 252b00055aaSStefan Rompf } 2536be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(operstate); 254b00055aaSStefan Rompf 2551da177e4SLinus Torvalds /* read-write attributes */ 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static int change_mtu(struct net_device *net, unsigned long new_mtu) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds return dev_set_mtu(net, (int) new_mtu); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2626be8aeefSGreg Kroah-Hartman static ssize_t mtu_store(struct device *dev, struct device_attribute *attr, 26343cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2641da177e4SLinus Torvalds { 26543cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_mtu); 2661da177e4SLinus Torvalds } 2676be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RW(mtu, fmt_dec); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds static int change_flags(struct net_device *net, unsigned long new_flags) 2701da177e4SLinus Torvalds { 27195c96174SEric Dumazet return dev_change_flags(net, (unsigned int) new_flags); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2746be8aeefSGreg Kroah-Hartman static ssize_t flags_store(struct device *dev, struct device_attribute *attr, 27543cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2761da177e4SLinus Torvalds { 27743cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_flags); 2781da177e4SLinus Torvalds } 2796be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RW(flags, fmt_hex); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds static int change_tx_queue_len(struct net_device *net, unsigned long new_len) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds net->tx_queue_len = new_len; 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2876be8aeefSGreg Kroah-Hartman static ssize_t tx_queue_len_store(struct device *dev, 28843cb76d9SGreg Kroah-Hartman struct device_attribute *attr, 28943cb76d9SGreg Kroah-Hartman const char *buf, size_t len) 2901da177e4SLinus Torvalds { 2915e1fccc0SEric W. Biederman if (!capable(CAP_NET_ADMIN)) 2925e1fccc0SEric W. Biederman return -EPERM; 2935e1fccc0SEric W. Biederman 29443cb76d9SGreg Kroah-Hartman return netdev_store(dev, attr, buf, len, change_tx_queue_len); 2951da177e4SLinus Torvalds } 2966be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong); 2971da177e4SLinus Torvalds 2986be8aeefSGreg Kroah-Hartman static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr, 2990b815a1aSStephen Hemminger const char *buf, size_t len) 3000b815a1aSStephen Hemminger { 3010b815a1aSStephen Hemminger struct net_device *netdev = to_net_dev(dev); 3025e1fccc0SEric W. Biederman struct net *net = dev_net(netdev); 3030b815a1aSStephen Hemminger size_t count = len; 3040b815a1aSStephen Hemminger ssize_t ret; 3050b815a1aSStephen Hemminger 3065e1fccc0SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3070b815a1aSStephen Hemminger return -EPERM; 3080b815a1aSStephen Hemminger 3090b815a1aSStephen Hemminger /* ignore trailing newline */ 3100b815a1aSStephen Hemminger if (len > 0 && buf[len - 1] == '\n') 3110b815a1aSStephen Hemminger --count; 3120b815a1aSStephen Hemminger 313336ca57cSEric W. Biederman if (!rtnl_trylock()) 314336ca57cSEric W. Biederman return restart_syscall(); 3150b815a1aSStephen Hemminger ret = dev_set_alias(netdev, buf, count); 3160b815a1aSStephen Hemminger rtnl_unlock(); 3170b815a1aSStephen Hemminger 3180b815a1aSStephen Hemminger return ret < 0 ? ret : len; 3190b815a1aSStephen Hemminger } 3200b815a1aSStephen Hemminger 3216be8aeefSGreg Kroah-Hartman static ssize_t ifalias_show(struct device *dev, 3220b815a1aSStephen Hemminger struct device_attribute *attr, char *buf) 3230b815a1aSStephen Hemminger { 3240b815a1aSStephen Hemminger const struct net_device *netdev = to_net_dev(dev); 3250b815a1aSStephen Hemminger ssize_t ret = 0; 3260b815a1aSStephen Hemminger 327336ca57cSEric W. Biederman if (!rtnl_trylock()) 328336ca57cSEric W. Biederman return restart_syscall(); 3290b815a1aSStephen Hemminger if (netdev->ifalias) 3300b815a1aSStephen Hemminger ret = sprintf(buf, "%s\n", netdev->ifalias); 3310b815a1aSStephen Hemminger rtnl_unlock(); 3320b815a1aSStephen Hemminger return ret; 3330b815a1aSStephen Hemminger } 3346be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RW(ifalias); 335a512b92bSVlad Dogaru 336a512b92bSVlad Dogaru static int change_group(struct net_device *net, unsigned long new_group) 337a512b92bSVlad Dogaru { 338a512b92bSVlad Dogaru dev_set_group(net, (int) new_group); 339a512b92bSVlad Dogaru return 0; 340a512b92bSVlad Dogaru } 341a512b92bSVlad Dogaru 3426be8aeefSGreg Kroah-Hartman static ssize_t group_store(struct device *dev, struct device_attribute *attr, 343a512b92bSVlad Dogaru const char *buf, size_t len) 344a512b92bSVlad Dogaru { 345a512b92bSVlad Dogaru return netdev_store(dev, attr, buf, len, change_group); 346a512b92bSVlad Dogaru } 3476be8aeefSGreg Kroah-Hartman NETDEVICE_SHOW(group, fmt_dec); 3486be8aeefSGreg Kroah-Hartman static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store); 349a512b92bSVlad Dogaru 350cc998ff8SLinus Torvalds static ssize_t phys_port_id_show(struct device *dev, 351ff80e519SJiri Pirko struct device_attribute *attr, char *buf) 352ff80e519SJiri Pirko { 353ff80e519SJiri Pirko struct net_device *netdev = to_net_dev(dev); 354ff80e519SJiri Pirko ssize_t ret = -EINVAL; 355ff80e519SJiri Pirko 356ff80e519SJiri Pirko if (!rtnl_trylock()) 357ff80e519SJiri Pirko return restart_syscall(); 358ff80e519SJiri Pirko 359ff80e519SJiri Pirko if (dev_isalive(netdev)) { 360ff80e519SJiri Pirko struct netdev_phys_port_id ppid; 361ff80e519SJiri Pirko 362ff80e519SJiri Pirko ret = dev_get_phys_port_id(netdev, &ppid); 363ff80e519SJiri Pirko if (!ret) 364ff80e519SJiri Pirko ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id); 365ff80e519SJiri Pirko } 366ff80e519SJiri Pirko rtnl_unlock(); 367ff80e519SJiri Pirko 368ff80e519SJiri Pirko return ret; 369ff80e519SJiri Pirko } 370cc998ff8SLinus Torvalds static DEVICE_ATTR_RO(phys_port_id); 371ff80e519SJiri Pirko 3726be8aeefSGreg Kroah-Hartman static struct attribute *net_class_attrs[] = { 3736be8aeefSGreg Kroah-Hartman &dev_attr_netdev_group.attr, 3746be8aeefSGreg Kroah-Hartman &dev_attr_type.attr, 3756be8aeefSGreg Kroah-Hartman &dev_attr_dev_id.attr, 3766be8aeefSGreg Kroah-Hartman &dev_attr_iflink.attr, 3776be8aeefSGreg Kroah-Hartman &dev_attr_ifindex.attr, 3786be8aeefSGreg Kroah-Hartman &dev_attr_addr_assign_type.attr, 3796be8aeefSGreg Kroah-Hartman &dev_attr_addr_len.attr, 3806be8aeefSGreg Kroah-Hartman &dev_attr_link_mode.attr, 3816be8aeefSGreg Kroah-Hartman &dev_attr_address.attr, 3826be8aeefSGreg Kroah-Hartman &dev_attr_broadcast.attr, 3836be8aeefSGreg Kroah-Hartman &dev_attr_speed.attr, 3846be8aeefSGreg Kroah-Hartman &dev_attr_duplex.attr, 3856be8aeefSGreg Kroah-Hartman &dev_attr_dormant.attr, 3866be8aeefSGreg Kroah-Hartman &dev_attr_operstate.attr, 3876be8aeefSGreg Kroah-Hartman &dev_attr_ifalias.attr, 3886be8aeefSGreg Kroah-Hartman &dev_attr_carrier.attr, 3896be8aeefSGreg Kroah-Hartman &dev_attr_mtu.attr, 3906be8aeefSGreg Kroah-Hartman &dev_attr_flags.attr, 3916be8aeefSGreg Kroah-Hartman &dev_attr_tx_queue_len.attr, 392cc998ff8SLinus Torvalds &dev_attr_phys_port_id.attr, 3936be8aeefSGreg Kroah-Hartman NULL, 3941da177e4SLinus Torvalds }; 3956be8aeefSGreg Kroah-Hartman ATTRIBUTE_GROUPS(net_class); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */ 39843cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d, 39943cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf, 4001da177e4SLinus Torvalds unsigned long offset) 4011da177e4SLinus Torvalds { 40243cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 4031da177e4SLinus Torvalds ssize_t ret = -EINVAL; 4041da177e4SLinus Torvalds 405be1f3c2cSBen Hutchings WARN_ON(offset > sizeof(struct rtnl_link_stats64) || 406be1f3c2cSBen Hutchings offset % sizeof(u64) != 0); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds read_lock(&dev_base_lock); 40996e74088SPavel Emelyanov if (dev_isalive(dev)) { 41028172739SEric Dumazet struct rtnl_link_stats64 temp; 41128172739SEric Dumazet const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); 41228172739SEric Dumazet 413be1f3c2cSBen Hutchings ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset)); 41496e74088SPavel Emelyanov } 4151da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4161da177e4SLinus Torvalds return ret; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* generate a read-only statistics attribute */ 4201da177e4SLinus Torvalds #define NETSTAT_ENTRY(name) \ 4216be8aeefSGreg Kroah-Hartman static ssize_t name##_show(struct device *d, \ 42243cb76d9SGreg Kroah-Hartman struct device_attribute *attr, char *buf) \ 4231da177e4SLinus Torvalds { \ 42443cb76d9SGreg Kroah-Hartman return netstat_show(d, attr, buf, \ 425be1f3c2cSBen Hutchings offsetof(struct rtnl_link_stats64, name)); \ 4261da177e4SLinus Torvalds } \ 4276be8aeefSGreg Kroah-Hartman static DEVICE_ATTR_RO(name) 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets); 4301da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets); 4311da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes); 4321da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes); 4331da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors); 4341da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors); 4351da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped); 4361da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped); 4371da177e4SLinus Torvalds NETSTAT_ENTRY(multicast); 4381da177e4SLinus Torvalds NETSTAT_ENTRY(collisions); 4391da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors); 4401da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors); 4411da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors); 4421da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors); 4431da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors); 4441da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors); 4451da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors); 4461da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors); 4471da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors); 4481da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors); 4491da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors); 4501da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed); 4511da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = { 45443cb76d9SGreg Kroah-Hartman &dev_attr_rx_packets.attr, 45543cb76d9SGreg Kroah-Hartman &dev_attr_tx_packets.attr, 45643cb76d9SGreg Kroah-Hartman &dev_attr_rx_bytes.attr, 45743cb76d9SGreg Kroah-Hartman &dev_attr_tx_bytes.attr, 45843cb76d9SGreg Kroah-Hartman &dev_attr_rx_errors.attr, 45943cb76d9SGreg Kroah-Hartman &dev_attr_tx_errors.attr, 46043cb76d9SGreg Kroah-Hartman &dev_attr_rx_dropped.attr, 46143cb76d9SGreg Kroah-Hartman &dev_attr_tx_dropped.attr, 46243cb76d9SGreg Kroah-Hartman &dev_attr_multicast.attr, 46343cb76d9SGreg Kroah-Hartman &dev_attr_collisions.attr, 46443cb76d9SGreg Kroah-Hartman &dev_attr_rx_length_errors.attr, 46543cb76d9SGreg Kroah-Hartman &dev_attr_rx_over_errors.attr, 46643cb76d9SGreg Kroah-Hartman &dev_attr_rx_crc_errors.attr, 46743cb76d9SGreg Kroah-Hartman &dev_attr_rx_frame_errors.attr, 46843cb76d9SGreg Kroah-Hartman &dev_attr_rx_fifo_errors.attr, 46943cb76d9SGreg Kroah-Hartman &dev_attr_rx_missed_errors.attr, 47043cb76d9SGreg Kroah-Hartman &dev_attr_tx_aborted_errors.attr, 47143cb76d9SGreg Kroah-Hartman &dev_attr_tx_carrier_errors.attr, 47243cb76d9SGreg Kroah-Hartman &dev_attr_tx_fifo_errors.attr, 47343cb76d9SGreg Kroah-Hartman &dev_attr_tx_heartbeat_errors.attr, 47443cb76d9SGreg Kroah-Hartman &dev_attr_tx_window_errors.attr, 47543cb76d9SGreg Kroah-Hartman &dev_attr_rx_compressed.attr, 47643cb76d9SGreg Kroah-Hartman &dev_attr_tx_compressed.attr, 4771da177e4SLinus Torvalds NULL 4781da177e4SLinus Torvalds }; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static struct attribute_group netstat_group = { 4821da177e4SLinus Torvalds .name = "statistics", 4831da177e4SLinus Torvalds .attrs = netstat_attrs, 4841da177e4SLinus Torvalds }; 48538c1a01cSJohannes Berg 48638c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) 48738c1a01cSJohannes Berg static struct attribute *wireless_attrs[] = { 48838c1a01cSJohannes Berg NULL 48938c1a01cSJohannes Berg }; 49038c1a01cSJohannes Berg 49138c1a01cSJohannes Berg static struct attribute_group wireless_group = { 49238c1a01cSJohannes Berg .name = "wireless", 49338c1a01cSJohannes Berg .attrs = wireless_attrs, 49438c1a01cSJohannes Berg }; 49538c1a01cSJohannes Berg #endif 4966be8aeefSGreg Kroah-Hartman 4976be8aeefSGreg Kroah-Hartman #else /* CONFIG_SYSFS */ 4986be8aeefSGreg Kroah-Hartman #define net_class_groups NULL 499d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */ 5001da177e4SLinus Torvalds 50130bde1f5SStephen Rothwell #ifdef CONFIG_RPS 5020a9627f2STom Herbert /* 5030a9627f2STom Herbert * RX queue sysfs structures and functions. 5040a9627f2STom Herbert */ 5050a9627f2STom Herbert struct rx_queue_attribute { 5060a9627f2STom Herbert struct attribute attr; 5070a9627f2STom Herbert ssize_t (*show)(struct netdev_rx_queue *queue, 5080a9627f2STom Herbert struct rx_queue_attribute *attr, char *buf); 5090a9627f2STom Herbert ssize_t (*store)(struct netdev_rx_queue *queue, 5100a9627f2STom Herbert struct rx_queue_attribute *attr, const char *buf, size_t len); 5110a9627f2STom Herbert }; 5120a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr, \ 5130a9627f2STom Herbert struct rx_queue_attribute, attr) 5140a9627f2STom Herbert 5150a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) 5160a9627f2STom Herbert 5170a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, 5180a9627f2STom Herbert char *buf) 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->show) 5240a9627f2STom Herbert return -EIO; 5250a9627f2STom Herbert 5260a9627f2STom Herbert return attribute->show(queue, attribute, buf); 5270a9627f2STom Herbert } 5280a9627f2STom Herbert 5290a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, 5300a9627f2STom Herbert const char *buf, size_t count) 5310a9627f2STom Herbert { 5320a9627f2STom Herbert struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); 5330a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 5340a9627f2STom Herbert 5350a9627f2STom Herbert if (!attribute->store) 5360a9627f2STom Herbert return -EIO; 5370a9627f2STom Herbert 5380a9627f2STom Herbert return attribute->store(queue, attribute, buf, count); 5390a9627f2STom Herbert } 5400a9627f2STom Herbert 541fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = { 5420a9627f2STom Herbert .show = rx_queue_attr_show, 5430a9627f2STom Herbert .store = rx_queue_attr_store, 5440a9627f2STom Herbert }; 5450a9627f2STom Herbert 5460a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue, 5470a9627f2STom Herbert struct rx_queue_attribute *attribute, char *buf) 5480a9627f2STom Herbert { 5490a9627f2STom Herbert struct rps_map *map; 5500a9627f2STom Herbert cpumask_var_t mask; 5510a9627f2STom Herbert size_t len = 0; 5520a9627f2STom Herbert int i; 5530a9627f2STom Herbert 5540a9627f2STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 5550a9627f2STom Herbert return -ENOMEM; 5560a9627f2STom Herbert 5570a9627f2STom Herbert rcu_read_lock(); 5580a9627f2STom Herbert map = rcu_dereference(queue->rps_map); 5590a9627f2STom Herbert if (map) 5600a9627f2STom Herbert for (i = 0; i < map->len; i++) 5610a9627f2STom Herbert cpumask_set_cpu(map->cpus[i], mask); 5620a9627f2STom Herbert 5630a9627f2STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 5640a9627f2STom Herbert if (PAGE_SIZE - len < 3) { 5650a9627f2STom Herbert rcu_read_unlock(); 5660a9627f2STom Herbert free_cpumask_var(mask); 5670a9627f2STom Herbert return -EINVAL; 5680a9627f2STom Herbert } 5690a9627f2STom Herbert rcu_read_unlock(); 5700a9627f2STom Herbert 5710a9627f2STom Herbert free_cpumask_var(mask); 5720a9627f2STom Herbert len += sprintf(buf + len, "\n"); 5730a9627f2STom Herbert return len; 5740a9627f2STom Herbert } 5750a9627f2STom Herbert 576f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue, 5770a9627f2STom Herbert struct rx_queue_attribute *attribute, 5780a9627f2STom Herbert const char *buf, size_t len) 5790a9627f2STom Herbert { 5800a9627f2STom Herbert struct rps_map *old_map, *map; 5810a9627f2STom Herbert cpumask_var_t mask; 5820a9627f2STom Herbert int err, cpu, i; 5830a9627f2STom Herbert static DEFINE_SPINLOCK(rps_map_lock); 5840a9627f2STom Herbert 5850a9627f2STom Herbert if (!capable(CAP_NET_ADMIN)) 5860a9627f2STom Herbert return -EPERM; 5870a9627f2STom Herbert 5880a9627f2STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 5890a9627f2STom Herbert return -ENOMEM; 5900a9627f2STom Herbert 5910a9627f2STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 5920a9627f2STom Herbert if (err) { 5930a9627f2STom Herbert free_cpumask_var(mask); 5940a9627f2STom Herbert return err; 5950a9627f2STom Herbert } 5960a9627f2STom Herbert 59795c96174SEric Dumazet map = kzalloc(max_t(unsigned int, 5980a9627f2STom Herbert RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), 5990a9627f2STom Herbert GFP_KERNEL); 6000a9627f2STom Herbert if (!map) { 6010a9627f2STom Herbert free_cpumask_var(mask); 6020a9627f2STom Herbert return -ENOMEM; 6030a9627f2STom Herbert } 6040a9627f2STom Herbert 6050a9627f2STom Herbert i = 0; 6060a9627f2STom Herbert for_each_cpu_and(cpu, mask, cpu_online_mask) 6070a9627f2STom Herbert map->cpus[i++] = cpu; 6080a9627f2STom Herbert 6090a9627f2STom Herbert if (i) 6100a9627f2STom Herbert map->len = i; 6110a9627f2STom Herbert else { 6120a9627f2STom Herbert kfree(map); 6130a9627f2STom Herbert map = NULL; 6140a9627f2STom Herbert } 6150a9627f2STom Herbert 6160a9627f2STom Herbert spin_lock(&rps_map_lock); 6176e3f7fafSEric Dumazet old_map = rcu_dereference_protected(queue->rps_map, 6186e3f7fafSEric Dumazet lockdep_is_held(&rps_map_lock)); 6190a9627f2STom Herbert rcu_assign_pointer(queue->rps_map, map); 6200a9627f2STom Herbert spin_unlock(&rps_map_lock); 6210a9627f2STom Herbert 622adc9300eSEric Dumazet if (map) 623c5905afbSIngo Molnar static_key_slow_inc(&rps_needed); 624adc9300eSEric Dumazet if (old_map) { 625f6f80238SLai Jiangshan kfree_rcu(old_map, rcu); 626c5905afbSIngo Molnar static_key_slow_dec(&rps_needed); 627adc9300eSEric Dumazet } 6280a9627f2STom Herbert free_cpumask_var(mask); 6290a9627f2STom Herbert return len; 6300a9627f2STom Herbert } 6310a9627f2STom Herbert 632fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 633fec5e652STom Herbert struct rx_queue_attribute *attr, 634fec5e652STom Herbert char *buf) 635fec5e652STom Herbert { 636fec5e652STom Herbert struct rps_dev_flow_table *flow_table; 63760b778ceSEric Dumazet unsigned long val = 0; 638fec5e652STom Herbert 639fec5e652STom Herbert rcu_read_lock(); 640fec5e652STom Herbert flow_table = rcu_dereference(queue->rps_flow_table); 641fec5e652STom Herbert if (flow_table) 64260b778ceSEric Dumazet val = (unsigned long)flow_table->mask + 1; 643fec5e652STom Herbert rcu_read_unlock(); 644fec5e652STom Herbert 64560b778ceSEric Dumazet return sprintf(buf, "%lu\n", val); 646fec5e652STom Herbert } 647fec5e652STom Herbert 648fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu) 649fec5e652STom Herbert { 650fec5e652STom Herbert struct rps_dev_flow_table *table = container_of(rcu, 651fec5e652STom Herbert struct rps_dev_flow_table, rcu); 652243198d0SAl Viro vfree(table); 653fec5e652STom Herbert } 654fec5e652STom Herbert 655f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, 656fec5e652STom Herbert struct rx_queue_attribute *attr, 657fec5e652STom Herbert const char *buf, size_t len) 658fec5e652STom Herbert { 65960b778ceSEric Dumazet unsigned long mask, count; 660fec5e652STom Herbert struct rps_dev_flow_table *table, *old_table; 661fec5e652STom Herbert static DEFINE_SPINLOCK(rps_dev_flow_lock); 66260b778ceSEric Dumazet int rc; 663fec5e652STom Herbert 664fec5e652STom Herbert if (!capable(CAP_NET_ADMIN)) 665fec5e652STom Herbert return -EPERM; 666fec5e652STom Herbert 66760b778ceSEric Dumazet rc = kstrtoul(buf, 0, &count); 66860b778ceSEric Dumazet if (rc < 0) 66960b778ceSEric Dumazet return rc; 670fec5e652STom Herbert 671fec5e652STom Herbert if (count) { 67260b778ceSEric Dumazet mask = count - 1; 67360b778ceSEric Dumazet /* mask = roundup_pow_of_two(count) - 1; 67460b778ceSEric Dumazet * without overflows... 67560b778ceSEric Dumazet */ 67660b778ceSEric Dumazet while ((mask | (mask >> 1)) != mask) 67760b778ceSEric Dumazet mask |= (mask >> 1); 67860b778ceSEric Dumazet /* On 64 bit arches, must check mask fits in table->mask (u32), 6798e3bff96Sstephen hemminger * and on 32bit arches, must check 6808e3bff96Sstephen hemminger * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow. 68160b778ceSEric Dumazet */ 68260b778ceSEric Dumazet #if BITS_PER_LONG > 32 68360b778ceSEric Dumazet if (mask > (unsigned long)(u32)mask) 684a0a129f8SXi Wang return -EINVAL; 68560b778ceSEric Dumazet #else 68660b778ceSEric Dumazet if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) 687a0a129f8SXi Wang / sizeof(struct rps_dev_flow)) { 688fec5e652STom Herbert /* Enforce a limit to prevent overflow */ 689fec5e652STom Herbert return -EINVAL; 690fec5e652STom Herbert } 69160b778ceSEric Dumazet #endif 69260b778ceSEric Dumazet table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); 693fec5e652STom Herbert if (!table) 694fec5e652STom Herbert return -ENOMEM; 695fec5e652STom Herbert 69660b778ceSEric Dumazet table->mask = mask; 69760b778ceSEric Dumazet for (count = 0; count <= mask; count++) 69860b778ceSEric Dumazet table->flows[count].cpu = RPS_NO_CPU; 699fec5e652STom Herbert } else 700fec5e652STom Herbert table = NULL; 701fec5e652STom Herbert 702fec5e652STom Herbert spin_lock(&rps_dev_flow_lock); 7036e3f7fafSEric Dumazet old_table = rcu_dereference_protected(queue->rps_flow_table, 7046e3f7fafSEric Dumazet lockdep_is_held(&rps_dev_flow_lock)); 705fec5e652STom Herbert rcu_assign_pointer(queue->rps_flow_table, table); 706fec5e652STom Herbert spin_unlock(&rps_dev_flow_lock); 707fec5e652STom Herbert 708fec5e652STom Herbert if (old_table) 709fec5e652STom Herbert call_rcu(&old_table->rcu, rps_dev_flow_table_release); 710fec5e652STom Herbert 711fec5e652STom Herbert return len; 712fec5e652STom Herbert } 713fec5e652STom Herbert 7140a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute = 7150a9627f2STom Herbert __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); 7160a9627f2STom Herbert 717fec5e652STom Herbert 718fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = 719fec5e652STom Herbert __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, 720fec5e652STom Herbert show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); 721fec5e652STom Herbert 7220a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = { 7230a9627f2STom Herbert &rps_cpus_attribute.attr, 724fec5e652STom Herbert &rps_dev_flow_table_cnt_attribute.attr, 7250a9627f2STom Herbert NULL 7260a9627f2STom Herbert }; 7270a9627f2STom Herbert 7280a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj) 7290a9627f2STom Herbert { 7300a9627f2STom Herbert struct netdev_rx_queue *queue = to_rx_queue(kobj); 7316e3f7fafSEric Dumazet struct rps_map *map; 7326e3f7fafSEric Dumazet struct rps_dev_flow_table *flow_table; 7330a9627f2STom Herbert 734fec5e652STom Herbert 73533d480ceSEric Dumazet map = rcu_dereference_protected(queue->rps_map, 1); 7369ea19481SJohn Fastabend if (map) { 7379ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_map, NULL); 738f6f80238SLai Jiangshan kfree_rcu(map, rcu); 7399ea19481SJohn Fastabend } 7406e3f7fafSEric Dumazet 74133d480ceSEric Dumazet flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); 7429ea19481SJohn Fastabend if (flow_table) { 7439ea19481SJohn Fastabend RCU_INIT_POINTER(queue->rps_flow_table, NULL); 7446e3f7fafSEric Dumazet call_rcu(&flow_table->rcu, rps_dev_flow_table_release); 7459ea19481SJohn Fastabend } 7460a9627f2STom Herbert 7479ea19481SJohn Fastabend memset(kobj, 0, sizeof(*kobj)); 748fe822240STom Herbert dev_put(queue->dev); 7490a9627f2STom Herbert } 7500a9627f2STom Herbert 7510a9627f2STom Herbert static struct kobj_type rx_queue_ktype = { 7520a9627f2STom Herbert .sysfs_ops = &rx_queue_sysfs_ops, 7530a9627f2STom Herbert .release = rx_queue_release, 7540a9627f2STom Herbert .default_attrs = rx_queue_default_attrs, 7550a9627f2STom Herbert }; 7560a9627f2STom Herbert 7570a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index) 7580a9627f2STom Herbert { 7590a9627f2STom Herbert struct netdev_rx_queue *queue = net->_rx + index; 7600a9627f2STom Herbert struct kobject *kobj = &queue->kobj; 7610a9627f2STom Herbert int error = 0; 7620a9627f2STom Herbert 7630a9627f2STom Herbert kobj->kset = net->queues_kset; 7640a9627f2STom Herbert error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, 7650a9627f2STom Herbert "rx-%u", index); 7660a9627f2STom Herbert if (error) { 7670a9627f2STom Herbert kobject_put(kobj); 7680a9627f2STom Herbert return error; 7690a9627f2STom Herbert } 7700a9627f2STom Herbert 7710a9627f2STom Herbert kobject_uevent(kobj, KOBJ_ADD); 772fe822240STom Herbert dev_hold(queue->dev); 7730a9627f2STom Herbert 7740a9627f2STom Herbert return error; 7750a9627f2STom Herbert } 776bf264145STom Herbert #endif /* CONFIG_RPS */ 7770a9627f2STom Herbert 77862fe0b40SBen Hutchings int 77962fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 7800a9627f2STom Herbert { 781bf264145STom Herbert #ifdef CONFIG_RPS 7820a9627f2STom Herbert int i; 7830a9627f2STom Herbert int error = 0; 7840a9627f2STom Herbert 78562fe0b40SBen Hutchings for (i = old_num; i < new_num; i++) { 7860a9627f2STom Herbert error = rx_queue_add_kobject(net, i); 78762fe0b40SBen Hutchings if (error) { 78862fe0b40SBen Hutchings new_num = old_num; 7890a9627f2STom Herbert break; 7900a9627f2STom Herbert } 79162fe0b40SBen Hutchings } 7920a9627f2STom Herbert 79362fe0b40SBen Hutchings while (--i >= new_num) 7940a9627f2STom Herbert kobject_put(&net->_rx[i].kobj); 7950a9627f2STom Herbert 7960a9627f2STom Herbert return error; 797bf264145STom Herbert #else 798bf264145STom Herbert return 0; 799bf264145STom Herbert #endif 8000a9627f2STom Herbert } 8010a9627f2STom Herbert 802ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 8031d24eb48STom Herbert /* 8041d24eb48STom Herbert * netdev_queue sysfs structures and functions. 8051d24eb48STom Herbert */ 8061d24eb48STom Herbert struct netdev_queue_attribute { 8071d24eb48STom Herbert struct attribute attr; 8081d24eb48STom Herbert ssize_t (*show)(struct netdev_queue *queue, 8091d24eb48STom Herbert struct netdev_queue_attribute *attr, char *buf); 8101d24eb48STom Herbert ssize_t (*store)(struct netdev_queue *queue, 8111d24eb48STom Herbert struct netdev_queue_attribute *attr, const char *buf, size_t len); 8121d24eb48STom Herbert }; 8131d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr, \ 8141d24eb48STom Herbert struct netdev_queue_attribute, attr) 8151d24eb48STom Herbert 8161d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) 8171d24eb48STom Herbert 8181d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj, 8191d24eb48STom Herbert struct attribute *attr, char *buf) 82062fe0b40SBen Hutchings { 8211d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8221d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8231d24eb48STom Herbert 8241d24eb48STom Herbert if (!attribute->show) 8251d24eb48STom Herbert return -EIO; 8261d24eb48STom Herbert 8271d24eb48STom Herbert return attribute->show(queue, attribute, buf); 8281d24eb48STom Herbert } 8291d24eb48STom Herbert 8301d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj, 8311d24eb48STom Herbert struct attribute *attr, 8321d24eb48STom Herbert const char *buf, size_t count) 8331d24eb48STom Herbert { 8341d24eb48STom Herbert struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); 8351d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 8361d24eb48STom Herbert 8371d24eb48STom Herbert if (!attribute->store) 8381d24eb48STom Herbert return -EIO; 8391d24eb48STom Herbert 8401d24eb48STom Herbert return attribute->store(queue, attribute, buf, count); 8411d24eb48STom Herbert } 8421d24eb48STom Herbert 8431d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = { 8441d24eb48STom Herbert .show = netdev_queue_attr_show, 8451d24eb48STom Herbert .store = netdev_queue_attr_store, 8461d24eb48STom Herbert }; 8471d24eb48STom Herbert 848ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue, 849ccf5ff69Sdavid decotigny struct netdev_queue_attribute *attribute, 850ccf5ff69Sdavid decotigny char *buf) 851ccf5ff69Sdavid decotigny { 852ccf5ff69Sdavid decotigny unsigned long trans_timeout; 853ccf5ff69Sdavid decotigny 854ccf5ff69Sdavid decotigny spin_lock_irq(&queue->_xmit_lock); 855ccf5ff69Sdavid decotigny trans_timeout = queue->trans_timeout; 856ccf5ff69Sdavid decotigny spin_unlock_irq(&queue->_xmit_lock); 857ccf5ff69Sdavid decotigny 858ccf5ff69Sdavid decotigny return sprintf(buf, "%lu", trans_timeout); 859ccf5ff69Sdavid decotigny } 860ccf5ff69Sdavid decotigny 861ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout = 862ccf5ff69Sdavid decotigny __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); 863ccf5ff69Sdavid decotigny 864114cf580STom Herbert #ifdef CONFIG_BQL 865114cf580STom Herbert /* 866114cf580STom Herbert * Byte queue limits sysfs structures and functions. 867114cf580STom Herbert */ 868114cf580STom Herbert static ssize_t bql_show(char *buf, unsigned int value) 869114cf580STom Herbert { 870114cf580STom Herbert return sprintf(buf, "%u\n", value); 871114cf580STom Herbert } 872114cf580STom Herbert 873114cf580STom Herbert static ssize_t bql_set(const char *buf, const size_t count, 874114cf580STom Herbert unsigned int *pvalue) 875114cf580STom Herbert { 876114cf580STom Herbert unsigned int value; 877114cf580STom Herbert int err; 878114cf580STom Herbert 879114cf580STom Herbert if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) 880114cf580STom Herbert value = DQL_MAX_LIMIT; 881114cf580STom Herbert else { 882114cf580STom Herbert err = kstrtouint(buf, 10, &value); 883114cf580STom Herbert if (err < 0) 884114cf580STom Herbert return err; 885114cf580STom Herbert if (value > DQL_MAX_LIMIT) 886114cf580STom Herbert return -EINVAL; 887114cf580STom Herbert } 888114cf580STom Herbert 889114cf580STom Herbert *pvalue = value; 890114cf580STom Herbert 891114cf580STom Herbert return count; 892114cf580STom Herbert } 893114cf580STom Herbert 894114cf580STom Herbert static ssize_t bql_show_hold_time(struct netdev_queue *queue, 895114cf580STom Herbert struct netdev_queue_attribute *attr, 896114cf580STom Herbert char *buf) 897114cf580STom Herbert { 898114cf580STom Herbert struct dql *dql = &queue->dql; 899114cf580STom Herbert 900114cf580STom Herbert return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); 901114cf580STom Herbert } 902114cf580STom Herbert 903114cf580STom Herbert static ssize_t bql_set_hold_time(struct netdev_queue *queue, 904114cf580STom Herbert struct netdev_queue_attribute *attribute, 905114cf580STom Herbert const char *buf, size_t len) 906114cf580STom Herbert { 907114cf580STom Herbert struct dql *dql = &queue->dql; 90895c96174SEric Dumazet unsigned int value; 909114cf580STom Herbert int err; 910114cf580STom Herbert 911114cf580STom Herbert err = kstrtouint(buf, 10, &value); 912114cf580STom Herbert if (err < 0) 913114cf580STom Herbert return err; 914114cf580STom Herbert 915114cf580STom Herbert dql->slack_hold_time = msecs_to_jiffies(value); 916114cf580STom Herbert 917114cf580STom Herbert return len; 918114cf580STom Herbert } 919114cf580STom Herbert 920114cf580STom Herbert static struct netdev_queue_attribute bql_hold_time_attribute = 921114cf580STom Herbert __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time, 922114cf580STom Herbert bql_set_hold_time); 923114cf580STom Herbert 924114cf580STom Herbert static ssize_t bql_show_inflight(struct netdev_queue *queue, 925114cf580STom Herbert struct netdev_queue_attribute *attr, 926114cf580STom Herbert char *buf) 927114cf580STom Herbert { 928114cf580STom Herbert struct dql *dql = &queue->dql; 929114cf580STom Herbert 930114cf580STom Herbert return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); 931114cf580STom Herbert } 932114cf580STom Herbert 933114cf580STom Herbert static struct netdev_queue_attribute bql_inflight_attribute = 934795d9a25SHiroaki SHIMODA __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL); 935114cf580STom Herbert 936114cf580STom Herbert #define BQL_ATTR(NAME, FIELD) \ 937114cf580STom Herbert static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ 938114cf580STom Herbert struct netdev_queue_attribute *attr, \ 939114cf580STom Herbert char *buf) \ 940114cf580STom Herbert { \ 941114cf580STom Herbert return bql_show(buf, queue->dql.FIELD); \ 942114cf580STom Herbert } \ 943114cf580STom Herbert \ 944114cf580STom Herbert static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ 945114cf580STom Herbert struct netdev_queue_attribute *attr, \ 946114cf580STom Herbert const char *buf, size_t len) \ 947114cf580STom Herbert { \ 948114cf580STom Herbert return bql_set(buf, len, &queue->dql.FIELD); \ 949114cf580STom Herbert } \ 950114cf580STom Herbert \ 951114cf580STom Herbert static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \ 952114cf580STom Herbert __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \ 953114cf580STom Herbert bql_set_ ## NAME); 954114cf580STom Herbert 955114cf580STom Herbert BQL_ATTR(limit, limit) 956114cf580STom Herbert BQL_ATTR(limit_max, max_limit) 957114cf580STom Herbert BQL_ATTR(limit_min, min_limit) 958114cf580STom Herbert 959114cf580STom Herbert static struct attribute *dql_attrs[] = { 960114cf580STom Herbert &bql_limit_attribute.attr, 961114cf580STom Herbert &bql_limit_max_attribute.attr, 962114cf580STom Herbert &bql_limit_min_attribute.attr, 963114cf580STom Herbert &bql_hold_time_attribute.attr, 964114cf580STom Herbert &bql_inflight_attribute.attr, 965114cf580STom Herbert NULL 966114cf580STom Herbert }; 967114cf580STom Herbert 968114cf580STom Herbert static struct attribute_group dql_group = { 969114cf580STom Herbert .name = "byte_queue_limits", 970114cf580STom Herbert .attrs = dql_attrs, 971114cf580STom Herbert }; 972114cf580STom Herbert #endif /* CONFIG_BQL */ 973114cf580STom Herbert 974ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 9751d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue) 9761d24eb48STom Herbert { 9771d24eb48STom Herbert struct net_device *dev = queue->dev; 9781d24eb48STom Herbert int i; 9791d24eb48STom Herbert 9801d24eb48STom Herbert for (i = 0; i < dev->num_tx_queues; i++) 9811d24eb48STom Herbert if (queue == &dev->_tx[i]) 9821d24eb48STom Herbert break; 9831d24eb48STom Herbert 9841d24eb48STom Herbert BUG_ON(i >= dev->num_tx_queues); 9851d24eb48STom Herbert 9861d24eb48STom Herbert return i; 9871d24eb48STom Herbert } 9881d24eb48STom Herbert 9891d24eb48STom Herbert 9901d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue, 9911d24eb48STom Herbert struct netdev_queue_attribute *attribute, char *buf) 9921d24eb48STom Herbert { 9931d24eb48STom Herbert struct net_device *dev = queue->dev; 9941d24eb48STom Herbert struct xps_dev_maps *dev_maps; 9951d24eb48STom Herbert cpumask_var_t mask; 9961d24eb48STom Herbert unsigned long index; 9971d24eb48STom Herbert size_t len = 0; 9981d24eb48STom Herbert int i; 9991d24eb48STom Herbert 10001d24eb48STom Herbert if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 10011d24eb48STom Herbert return -ENOMEM; 10021d24eb48STom Herbert 10031d24eb48STom Herbert index = get_netdev_queue_index(queue); 10041d24eb48STom Herbert 10051d24eb48STom Herbert rcu_read_lock(); 10061d24eb48STom Herbert dev_maps = rcu_dereference(dev->xps_maps); 10071d24eb48STom Herbert if (dev_maps) { 10081d24eb48STom Herbert for_each_possible_cpu(i) { 10091d24eb48STom Herbert struct xps_map *map = 10101d24eb48STom Herbert rcu_dereference(dev_maps->cpu_map[i]); 10111d24eb48STom Herbert if (map) { 10121d24eb48STom Herbert int j; 10131d24eb48STom Herbert for (j = 0; j < map->len; j++) { 10141d24eb48STom Herbert if (map->queues[j] == index) { 10151d24eb48STom Herbert cpumask_set_cpu(i, mask); 10161d24eb48STom Herbert break; 10171d24eb48STom Herbert } 10181d24eb48STom Herbert } 10191d24eb48STom Herbert } 10201d24eb48STom Herbert } 10211d24eb48STom Herbert } 10221d24eb48STom Herbert rcu_read_unlock(); 10231d24eb48STom Herbert 10241d24eb48STom Herbert len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); 10251d24eb48STom Herbert if (PAGE_SIZE - len < 3) { 10261d24eb48STom Herbert free_cpumask_var(mask); 10271d24eb48STom Herbert return -EINVAL; 10281d24eb48STom Herbert } 10291d24eb48STom Herbert 10301d24eb48STom Herbert free_cpumask_var(mask); 10311d24eb48STom Herbert len += sprintf(buf + len, "\n"); 10321d24eb48STom Herbert return len; 10331d24eb48STom Herbert } 10341d24eb48STom Herbert 10351d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue, 10361d24eb48STom Herbert struct netdev_queue_attribute *attribute, 10371d24eb48STom Herbert const char *buf, size_t len) 10381d24eb48STom Herbert { 10391d24eb48STom Herbert struct net_device *dev = queue->dev; 10401d24eb48STom Herbert unsigned long index; 1041537c00deSAlexander Duyck cpumask_var_t mask; 1042537c00deSAlexander Duyck int err; 10431d24eb48STom Herbert 10441d24eb48STom Herbert if (!capable(CAP_NET_ADMIN)) 10451d24eb48STom Herbert return -EPERM; 10461d24eb48STom Herbert 10471d24eb48STom Herbert if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 10481d24eb48STom Herbert return -ENOMEM; 10491d24eb48STom Herbert 10501d24eb48STom Herbert index = get_netdev_queue_index(queue); 10511d24eb48STom Herbert 10521d24eb48STom Herbert err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); 10531d24eb48STom Herbert if (err) { 10541d24eb48STom Herbert free_cpumask_var(mask); 10551d24eb48STom Herbert return err; 10561d24eb48STom Herbert } 10571d24eb48STom Herbert 1058537c00deSAlexander Duyck err = netif_set_xps_queue(dev, mask, index); 10591d24eb48STom Herbert 10601d24eb48STom Herbert free_cpumask_var(mask); 10611d24eb48STom Herbert 1062537c00deSAlexander Duyck return err ? : len; 10631d24eb48STom Herbert } 10641d24eb48STom Herbert 10651d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute = 10661d24eb48STom Herbert __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map); 1067ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */ 10681d24eb48STom Herbert 10691d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = { 1070ccf5ff69Sdavid decotigny &queue_trans_timeout.attr, 1071ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS 10721d24eb48STom Herbert &xps_cpus_attribute.attr, 1073ccf5ff69Sdavid decotigny #endif 10741d24eb48STom Herbert NULL 10751d24eb48STom Herbert }; 10761d24eb48STom Herbert 10771d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj) 10781d24eb48STom Herbert { 10791d24eb48STom Herbert struct netdev_queue *queue = to_netdev_queue(kobj); 10801d24eb48STom Herbert 10811d24eb48STom Herbert memset(kobj, 0, sizeof(*kobj)); 10821d24eb48STom Herbert dev_put(queue->dev); 10831d24eb48STom Herbert } 10841d24eb48STom Herbert 10851d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = { 10861d24eb48STom Herbert .sysfs_ops = &netdev_queue_sysfs_ops, 10871d24eb48STom Herbert .release = netdev_queue_release, 10881d24eb48STom Herbert .default_attrs = netdev_queue_default_attrs, 10891d24eb48STom Herbert }; 10901d24eb48STom Herbert 10911d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index) 10921d24eb48STom Herbert { 10931d24eb48STom Herbert struct netdev_queue *queue = net->_tx + index; 10941d24eb48STom Herbert struct kobject *kobj = &queue->kobj; 10951d24eb48STom Herbert int error = 0; 10961d24eb48STom Herbert 10971d24eb48STom Herbert kobj->kset = net->queues_kset; 10981d24eb48STom Herbert error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, 10991d24eb48STom Herbert "tx-%u", index); 1100114cf580STom Herbert if (error) 1101114cf580STom Herbert goto exit; 1102114cf580STom Herbert 1103114cf580STom Herbert #ifdef CONFIG_BQL 1104114cf580STom Herbert error = sysfs_create_group(kobj, &dql_group); 1105114cf580STom Herbert if (error) 1106114cf580STom Herbert goto exit; 1107114cf580STom Herbert #endif 11081d24eb48STom Herbert 11091d24eb48STom Herbert kobject_uevent(kobj, KOBJ_ADD); 11101d24eb48STom Herbert dev_hold(queue->dev); 11111d24eb48STom Herbert 1112114cf580STom Herbert return 0; 1113114cf580STom Herbert exit: 1114114cf580STom Herbert kobject_put(kobj); 11151d24eb48STom Herbert return error; 11161d24eb48STom Herbert } 1117ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 11181d24eb48STom Herbert 11191d24eb48STom Herbert int 11201d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num) 11211d24eb48STom Herbert { 1122ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 11231d24eb48STom Herbert int i; 11241d24eb48STom Herbert int error = 0; 11251d24eb48STom Herbert 11261d24eb48STom Herbert for (i = old_num; i < new_num; i++) { 11271d24eb48STom Herbert error = netdev_queue_add_kobject(net, i); 11281d24eb48STom Herbert if (error) { 11291d24eb48STom Herbert new_num = old_num; 11301d24eb48STom Herbert break; 11311d24eb48STom Herbert } 11321d24eb48STom Herbert } 11331d24eb48STom Herbert 1134114cf580STom Herbert while (--i >= new_num) { 1135114cf580STom Herbert struct netdev_queue *queue = net->_tx + i; 1136114cf580STom Herbert 1137114cf580STom Herbert #ifdef CONFIG_BQL 1138114cf580STom Herbert sysfs_remove_group(&queue->kobj, &dql_group); 1139114cf580STom Herbert #endif 1140114cf580STom Herbert kobject_put(&queue->kobj); 1141114cf580STom Herbert } 11421d24eb48STom Herbert 11431d24eb48STom Herbert return error; 1144bf264145STom Herbert #else 1145bf264145STom Herbert return 0; 1146ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */ 11471d24eb48STom Herbert } 11481d24eb48STom Herbert 11491d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net) 11501d24eb48STom Herbert { 1151bf264145STom Herbert int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; 11521d24eb48STom Herbert 1153ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 115462fe0b40SBen Hutchings net->queues_kset = kset_create_and_add("queues", 115562fe0b40SBen Hutchings NULL, &net->dev.kobj); 115662fe0b40SBen Hutchings if (!net->queues_kset) 115762fe0b40SBen Hutchings return -ENOMEM; 1158bf264145STom Herbert #endif 11591d24eb48STom Herbert 1160bf264145STom Herbert #ifdef CONFIG_RPS 1161bf264145STom Herbert real_rx = net->real_num_rx_queues; 1162bf264145STom Herbert #endif 1163bf264145STom Herbert real_tx = net->real_num_tx_queues; 1164bf264145STom Herbert 1165bf264145STom Herbert error = net_rx_queue_update_kobjects(net, 0, real_rx); 11661d24eb48STom Herbert if (error) 11671d24eb48STom Herbert goto error; 1168bf264145STom Herbert rxq = real_rx; 11691d24eb48STom Herbert 1170bf264145STom Herbert error = netdev_queue_update_kobjects(net, 0, real_tx); 11711d24eb48STom Herbert if (error) 11721d24eb48STom Herbert goto error; 1173bf264145STom Herbert txq = real_tx; 11741d24eb48STom Herbert 11751d24eb48STom Herbert return 0; 11761d24eb48STom Herbert 11771d24eb48STom Herbert error: 11781d24eb48STom Herbert netdev_queue_update_kobjects(net, txq, 0); 11791d24eb48STom Herbert net_rx_queue_update_kobjects(net, rxq, 0); 11801d24eb48STom Herbert return error; 118162fe0b40SBen Hutchings } 118262fe0b40SBen Hutchings 11831d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net) 11840a9627f2STom Herbert { 1185bf264145STom Herbert int real_rx = 0, real_tx = 0; 1186bf264145STom Herbert 1187bf264145STom Herbert #ifdef CONFIG_RPS 1188bf264145STom Herbert real_rx = net->real_num_rx_queues; 1189bf264145STom Herbert #endif 1190bf264145STom Herbert real_tx = net->real_num_tx_queues; 1191bf264145STom Herbert 1192bf264145STom Herbert net_rx_queue_update_kobjects(net, real_rx, 0); 1193bf264145STom Herbert netdev_queue_update_kobjects(net, real_tx, 0); 1194ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS 11950a9627f2STom Herbert kset_unregister(net->queues_kset); 1196bf264145STom Herbert #endif 11970a9627f2STom Herbert } 1198608b4b95SEric W. Biederman 11997dc5dbc8SEric W. Biederman static bool net_current_may_mount(void) 12007dc5dbc8SEric W. Biederman { 12017dc5dbc8SEric W. Biederman struct net *net = current->nsproxy->net_ns; 12027dc5dbc8SEric W. Biederman 12037dc5dbc8SEric W. Biederman return ns_capable(net->user_ns, CAP_SYS_ADMIN); 12047dc5dbc8SEric W. Biederman } 12057dc5dbc8SEric W. Biederman 1206a685e089SAl Viro static void *net_grab_current_ns(void) 1207608b4b95SEric W. Biederman { 1208a685e089SAl Viro struct net *ns = current->nsproxy->net_ns; 1209a685e089SAl Viro #ifdef CONFIG_NET_NS 1210a685e089SAl Viro if (ns) 1211a685e089SAl Viro atomic_inc(&ns->passive); 1212a685e089SAl Viro #endif 1213a685e089SAl Viro return ns; 1214608b4b95SEric W. Biederman } 1215608b4b95SEric W. Biederman 1216608b4b95SEric W. Biederman static const void *net_initial_ns(void) 1217608b4b95SEric W. Biederman { 1218608b4b95SEric W. Biederman return &init_net; 1219608b4b95SEric W. Biederman } 1220608b4b95SEric W. Biederman 1221608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk) 1222608b4b95SEric W. Biederman { 1223608b4b95SEric W. Biederman return sock_net(sk); 1224608b4b95SEric W. Biederman } 1225608b4b95SEric W. Biederman 122604600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = { 1227608b4b95SEric W. Biederman .type = KOBJ_NS_TYPE_NET, 12287dc5dbc8SEric W. Biederman .current_may_mount = net_current_may_mount, 1229a685e089SAl Viro .grab_current_ns = net_grab_current_ns, 1230608b4b95SEric W. Biederman .netlink_ns = net_netlink_ns, 1231608b4b95SEric W. Biederman .initial_ns = net_initial_ns, 1232a685e089SAl Viro .drop_ns = net_drop_ns, 1233608b4b95SEric W. Biederman }; 123404600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations); 1235608b4b95SEric W. Biederman 12367eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) 12371da177e4SLinus Torvalds { 123843cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 12397eff2e7aSKay Sievers int retval; 12401da177e4SLinus Torvalds 1241312c004dSKay Sievers /* pass interface to uevent. */ 12427eff2e7aSKay Sievers retval = add_uevent_var(env, "INTERFACE=%s", dev->name); 1243bf62456eSEric Rannaud if (retval) 1244bf62456eSEric Rannaud goto exit; 12451da177e4SLinus Torvalds 1246ca2f37dbSJean Tourrilhes /* pass ifindex to uevent. 1247ca2f37dbSJean Tourrilhes * ifindex is useful as it won't change (interface name may change) 1248ca2f37dbSJean Tourrilhes * and is what RtNetlink uses natively. */ 12497eff2e7aSKay Sievers retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); 1250ca2f37dbSJean Tourrilhes 1251bf62456eSEric Rannaud exit: 1252bf62456eSEric Rannaud return retval; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* 12561da177e4SLinus Torvalds * netdev_release -- destroy and free a dead device. 125743cb76d9SGreg Kroah-Hartman * Called when last reference to device kobject is gone. 12581da177e4SLinus Torvalds */ 125943cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d) 12601da177e4SLinus Torvalds { 126143cb76d9SGreg Kroah-Hartman struct net_device *dev = to_net_dev(d); 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds BUG_ON(dev->reg_state != NETREG_RELEASED); 12641da177e4SLinus Torvalds 12650b815a1aSStephen Hemminger kfree(dev->ifalias); 126674d332c1SEric Dumazet netdev_freemem(dev); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 1269608b4b95SEric W. Biederman static const void *net_namespace(struct device *d) 1270608b4b95SEric W. Biederman { 1271608b4b95SEric W. Biederman struct net_device *dev; 1272608b4b95SEric W. Biederman dev = container_of(d, struct net_device, dev); 1273608b4b95SEric W. Biederman return dev_net(dev); 1274608b4b95SEric W. Biederman } 1275608b4b95SEric W. Biederman 12761da177e4SLinus Torvalds static struct class net_class = { 12771da177e4SLinus Torvalds .name = "net", 127843cb76d9SGreg Kroah-Hartman .dev_release = netdev_release, 12796be8aeefSGreg Kroah-Hartman .dev_groups = net_class_groups, 128043cb76d9SGreg Kroah-Hartman .dev_uevent = netdev_uevent, 1281608b4b95SEric W. Biederman .ns_type = &net_ns_type_operations, 1282608b4b95SEric W. Biederman .namespace = net_namespace, 12831da177e4SLinus Torvalds }; 12841da177e4SLinus Torvalds 12859093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all 12869093bbb2SStephen Hemminger * netdev references are gone. 12879093bbb2SStephen Hemminger */ 12888b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net) 12891da177e4SLinus Torvalds { 12909093bbb2SStephen Hemminger struct device *dev = &(net->dev); 12919093bbb2SStephen Hemminger 12929093bbb2SStephen Hemminger kobject_get(&dev->kobj); 12933891845eSEric W. Biederman 12941d24eb48STom Herbert remove_queue_kobjects(net); 12950a9627f2STom Herbert 12969802c8e2SMing Lei pm_runtime_set_memalloc_noio(dev, false); 12979802c8e2SMing Lei 12989093bbb2SStephen Hemminger device_del(dev); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* Create sysfs entries for network device. */ 13028b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net) 13031da177e4SLinus Torvalds { 130443cb76d9SGreg Kroah-Hartman struct device *dev = &(net->dev); 1305a4dbd674SDavid Brownell const struct attribute_group **groups = net->sysfs_groups; 13060a9627f2STom Herbert int error = 0; 13071da177e4SLinus Torvalds 1308a1b3f594SEric W. Biederman device_initialize(dev); 130943cb76d9SGreg Kroah-Hartman dev->class = &net_class; 131043cb76d9SGreg Kroah-Hartman dev->platform_data = net; 131143cb76d9SGreg Kroah-Hartman dev->groups = groups; 13121da177e4SLinus Torvalds 1313a2205472SStephen Hemminger dev_set_name(dev, "%s", net->name); 13141da177e4SLinus Torvalds 13158b41d188SEric W. Biederman #ifdef CONFIG_SYSFS 13160c509a6cSEric W. Biederman /* Allow for a device specific group */ 13170c509a6cSEric W. Biederman if (*groups) 13180c509a6cSEric W. Biederman groups++; 13191da177e4SLinus Torvalds 13200c509a6cSEric W. Biederman *groups++ = &netstat_group; 132138c1a01cSJohannes Berg 132238c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) 132338c1a01cSJohannes Berg if (net->ieee80211_ptr) 132438c1a01cSJohannes Berg *groups++ = &wireless_group; 132538c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) 132638c1a01cSJohannes Berg else if (net->wireless_handlers) 132738c1a01cSJohannes Berg *groups++ = &wireless_group; 132838c1a01cSJohannes Berg #endif 132938c1a01cSJohannes Berg #endif 13308b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */ 13311da177e4SLinus Torvalds 13320a9627f2STom Herbert error = device_add(dev); 13330a9627f2STom Herbert if (error) 13340a9627f2STom Herbert return error; 13350a9627f2STom Herbert 13361d24eb48STom Herbert error = register_queue_kobjects(net); 13370a9627f2STom Herbert if (error) { 13380a9627f2STom Herbert device_del(dev); 13390a9627f2STom Herbert return error; 13400a9627f2STom Herbert } 13410a9627f2STom Herbert 13429802c8e2SMing Lei pm_runtime_set_memalloc_noio(dev, true); 13439802c8e2SMing Lei 13440a9627f2STom Herbert return error; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 134758292cbeSTejun Heo int netdev_class_create_file_ns(struct class_attribute *class_attr, 134858292cbeSTejun Heo const void *ns) 1349b8a9787eSJay Vosburgh { 135058292cbeSTejun Heo return class_create_file_ns(&net_class, class_attr, ns); 1351b8a9787eSJay Vosburgh } 135258292cbeSTejun Heo EXPORT_SYMBOL(netdev_class_create_file_ns); 1353b8a9787eSJay Vosburgh 135458292cbeSTejun Heo void netdev_class_remove_file_ns(struct class_attribute *class_attr, 135558292cbeSTejun Heo const void *ns) 1356b8a9787eSJay Vosburgh { 135758292cbeSTejun Heo class_remove_file_ns(&net_class, class_attr, ns); 1358b8a9787eSJay Vosburgh } 135958292cbeSTejun Heo EXPORT_SYMBOL(netdev_class_remove_file_ns); 1360b8a9787eSJay Vosburgh 13618b41d188SEric W. Biederman int netdev_kobject_init(void) 13621da177e4SLinus Torvalds { 1363608b4b95SEric W. Biederman kobj_ns_type_register(&net_ns_type_operations); 13641da177e4SLinus Torvalds return class_register(&net_class); 13651da177e4SLinus Torvalds } 1366