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