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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | #include <linux/rtnetlink.h> |
| 19 | #include <linux/wireless.h> |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 20 | #include <linux/vmalloc.h> |
Johannes Berg | 8f1546c | 2009-09-28 15:26:43 +0200 | [diff] [blame] | 21 | #include <net/wext.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Pavel Emelyanov | 342709e | 2007-10-23 21:14:45 -0700 | [diff] [blame] | 23 | #include "net-sysfs.h" |
| 24 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_SYSFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static const char fmt_hex[] = "%#x\n"; |
David S. Miller | d1102b5 | 2005-05-29 20:28:25 -0700 | [diff] [blame] | 27 | static const char fmt_long_hex[] = "%#lx\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static const char fmt_dec[] = "%d\n"; |
| 29 | static const char fmt_ulong[] = "%lu\n"; |
| 30 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 31 | static inline int dev_isalive(const struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Stephen Hemminger | fe9925b | 2006-05-06 17:56:03 -0700 | [diff] [blame] | 33 | return dev->reg_state <= NETREG_REGISTERED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* use same locking rules as GIF* ioctl's */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 37 | static ssize_t netdev_show(const struct device *dev, |
| 38 | struct device_attribute *attr, char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | ssize_t (*format)(const struct net_device *, char *)) |
| 40 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 41 | struct net_device *net = to_net_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | ssize_t ret = -EINVAL; |
| 43 | |
| 44 | read_lock(&dev_base_lock); |
| 45 | if (dev_isalive(net)) |
| 46 | ret = (*format)(net, buf); |
| 47 | read_unlock(&dev_base_lock); |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | /* generate a show function for simple field */ |
| 53 | #define NETDEVICE_SHOW(field, format_string) \ |
| 54 | static ssize_t format_##field(const struct net_device *net, char *buf) \ |
| 55 | { \ |
| 56 | return sprintf(buf, format_string, net->field); \ |
| 57 | } \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 58 | static ssize_t show_##field(struct device *dev, \ |
| 59 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 61 | return netdev_show(dev, attr, buf, format_##field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | /* use same locking and permission rules as SIF* ioctl's */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 66 | static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | const char *buf, size_t len, |
| 68 | int (*set)(struct net_device *, unsigned long)) |
| 69 | { |
| 70 | struct net_device *net = to_net_dev(dev); |
| 71 | char *endp; |
| 72 | unsigned long new; |
| 73 | int ret = -EINVAL; |
| 74 | |
| 75 | if (!capable(CAP_NET_ADMIN)) |
| 76 | return -EPERM; |
| 77 | |
| 78 | new = simple_strtoul(buf, &endp, 0); |
| 79 | if (endp == buf) |
| 80 | goto err; |
| 81 | |
Stephen Hemminger | 5a5990d | 2009-02-26 06:49:24 +0000 | [diff] [blame] | 82 | if (!rtnl_trylock()) |
Eric W. Biederman | 336ca57 | 2009-05-13 16:57:25 +0000 | [diff] [blame] | 83 | return restart_syscall(); |
Stephen Hemminger | 5a5990d | 2009-02-26 06:49:24 +0000 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (dev_isalive(net)) { |
| 86 | if ((ret = (*set)(net, new)) == 0) |
| 87 | ret = len; |
| 88 | } |
| 89 | rtnl_unlock(); |
| 90 | err: |
| 91 | return ret; |
| 92 | } |
| 93 | |
David Woodhouse | 9d29672 | 2008-04-20 16:07:43 -0700 | [diff] [blame] | 94 | NETDEVICE_SHOW(dev_id, fmt_hex); |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 95 | NETDEVICE_SHOW(addr_len, fmt_dec); |
| 96 | NETDEVICE_SHOW(iflink, fmt_dec); |
| 97 | NETDEVICE_SHOW(ifindex, fmt_dec); |
| 98 | NETDEVICE_SHOW(features, fmt_long_hex); |
| 99 | NETDEVICE_SHOW(type, fmt_dec); |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 100 | NETDEVICE_SHOW(link_mode, fmt_dec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* use same locking rules as GIFHWADDR ioctl's */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 103 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, |
| 104 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | struct net_device *net = to_net_dev(dev); |
| 107 | ssize_t ret = -EINVAL; |
| 108 | |
| 109 | read_lock(&dev_base_lock); |
| 110 | if (dev_isalive(net)) |
Michael Chan | 7ffc49a | 2007-12-24 21:28:09 -0800 | [diff] [blame] | 111 | ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | read_unlock(&dev_base_lock); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 116 | static ssize_t show_broadcast(struct device *dev, |
| 117 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | struct net_device *net = to_net_dev(dev); |
| 120 | if (dev_isalive(net)) |
Michael Chan | 7ffc49a | 2007-12-24 21:28:09 -0800 | [diff] [blame] | 121 | return sysfs_format_mac(buf, net->broadcast, net->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | } |
| 124 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 125 | static ssize_t show_carrier(struct device *dev, |
| 126 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
| 128 | struct net_device *netdev = to_net_dev(dev); |
| 129 | if (netif_running(netdev)) { |
| 130 | return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); |
| 131 | } |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
Andy Gospodarek | d519e17 | 2009-10-02 09:26:12 +0000 | [diff] [blame] | 135 | static ssize_t show_speed(struct device *dev, |
| 136 | struct device_attribute *attr, char *buf) |
| 137 | { |
| 138 | struct net_device *netdev = to_net_dev(dev); |
| 139 | int ret = -EINVAL; |
| 140 | |
| 141 | if (!rtnl_trylock()) |
| 142 | return restart_syscall(); |
| 143 | |
Eric Dumazet | ac5e3af | 2009-10-26 01:23:33 +0000 | [diff] [blame] | 144 | if (netif_running(netdev) && |
| 145 | netdev->ethtool_ops && |
| 146 | netdev->ethtool_ops->get_settings) { |
Andy Gospodarek | d519e17 | 2009-10-02 09:26:12 +0000 | [diff] [blame] | 147 | struct ethtool_cmd cmd = { ETHTOOL_GSET }; |
| 148 | |
| 149 | if (!netdev->ethtool_ops->get_settings(netdev, &cmd)) |
| 150 | ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd)); |
| 151 | } |
| 152 | rtnl_unlock(); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static ssize_t show_duplex(struct device *dev, |
| 157 | struct device_attribute *attr, char *buf) |
| 158 | { |
| 159 | struct net_device *netdev = to_net_dev(dev); |
| 160 | int ret = -EINVAL; |
| 161 | |
| 162 | if (!rtnl_trylock()) |
| 163 | return restart_syscall(); |
| 164 | |
Eric Dumazet | ac5e3af | 2009-10-26 01:23:33 +0000 | [diff] [blame] | 165 | if (netif_running(netdev) && |
| 166 | netdev->ethtool_ops && |
| 167 | netdev->ethtool_ops->get_settings) { |
Andy Gospodarek | d519e17 | 2009-10-02 09:26:12 +0000 | [diff] [blame] | 168 | struct ethtool_cmd cmd = { ETHTOOL_GSET }; |
| 169 | |
| 170 | if (!netdev->ethtool_ops->get_settings(netdev, &cmd)) |
| 171 | ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half"); |
| 172 | } |
| 173 | rtnl_unlock(); |
| 174 | return ret; |
| 175 | } |
| 176 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 177 | static ssize_t show_dormant(struct device *dev, |
| 178 | struct device_attribute *attr, char *buf) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 179 | { |
| 180 | struct net_device *netdev = to_net_dev(dev); |
| 181 | |
| 182 | if (netif_running(netdev)) |
| 183 | return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); |
| 184 | |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 188 | static const char *const operstates[] = { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 189 | "unknown", |
| 190 | "notpresent", /* currently unused */ |
| 191 | "down", |
| 192 | "lowerlayerdown", |
| 193 | "testing", /* currently unused */ |
| 194 | "dormant", |
| 195 | "up" |
| 196 | }; |
| 197 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 198 | static ssize_t show_operstate(struct device *dev, |
| 199 | struct device_attribute *attr, char *buf) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 200 | { |
| 201 | const struct net_device *netdev = to_net_dev(dev); |
| 202 | unsigned char operstate; |
| 203 | |
| 204 | read_lock(&dev_base_lock); |
| 205 | operstate = netdev->operstate; |
| 206 | if (!netif_running(netdev)) |
| 207 | operstate = IF_OPER_DOWN; |
| 208 | read_unlock(&dev_base_lock); |
| 209 | |
Adrian Bunk | e3a5cd9 | 2006-04-05 22:19:47 -0700 | [diff] [blame] | 210 | if (operstate >= ARRAY_SIZE(operstates)) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 211 | return -EINVAL; /* should not happen */ |
| 212 | |
| 213 | return sprintf(buf, "%s\n", operstates[operstate]); |
| 214 | } |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* read-write attributes */ |
| 217 | NETDEVICE_SHOW(mtu, fmt_dec); |
| 218 | |
| 219 | static int change_mtu(struct net_device *net, unsigned long new_mtu) |
| 220 | { |
| 221 | return dev_set_mtu(net, (int) new_mtu); |
| 222 | } |
| 223 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 224 | static ssize_t store_mtu(struct device *dev, struct device_attribute *attr, |
| 225 | const char *buf, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 227 | return netdev_store(dev, attr, buf, len, change_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | NETDEVICE_SHOW(flags, fmt_hex); |
| 231 | |
| 232 | static int change_flags(struct net_device *net, unsigned long new_flags) |
| 233 | { |
| 234 | return dev_change_flags(net, (unsigned) new_flags); |
| 235 | } |
| 236 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 237 | static ssize_t store_flags(struct device *dev, struct device_attribute *attr, |
| 238 | const char *buf, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 240 | return netdev_store(dev, attr, buf, len, change_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | NETDEVICE_SHOW(tx_queue_len, fmt_ulong); |
| 244 | |
| 245 | static int change_tx_queue_len(struct net_device *net, unsigned long new_len) |
| 246 | { |
| 247 | net->tx_queue_len = new_len; |
| 248 | return 0; |
| 249 | } |
| 250 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 251 | static ssize_t store_tx_queue_len(struct device *dev, |
| 252 | struct device_attribute *attr, |
| 253 | const char *buf, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 255 | return netdev_store(dev, attr, buf, len, change_tx_queue_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 258 | static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr, |
| 259 | const char *buf, size_t len) |
| 260 | { |
| 261 | struct net_device *netdev = to_net_dev(dev); |
| 262 | size_t count = len; |
| 263 | ssize_t ret; |
| 264 | |
| 265 | if (!capable(CAP_NET_ADMIN)) |
| 266 | return -EPERM; |
| 267 | |
| 268 | /* ignore trailing newline */ |
| 269 | if (len > 0 && buf[len - 1] == '\n') |
| 270 | --count; |
| 271 | |
Eric W. Biederman | 336ca57 | 2009-05-13 16:57:25 +0000 | [diff] [blame] | 272 | if (!rtnl_trylock()) |
| 273 | return restart_syscall(); |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 274 | ret = dev_set_alias(netdev, buf, count); |
| 275 | rtnl_unlock(); |
| 276 | |
| 277 | return ret < 0 ? ret : len; |
| 278 | } |
| 279 | |
| 280 | static ssize_t show_ifalias(struct device *dev, |
| 281 | struct device_attribute *attr, char *buf) |
| 282 | { |
| 283 | const struct net_device *netdev = to_net_dev(dev); |
| 284 | ssize_t ret = 0; |
| 285 | |
Eric W. Biederman | 336ca57 | 2009-05-13 16:57:25 +0000 | [diff] [blame] | 286 | if (!rtnl_trylock()) |
| 287 | return restart_syscall(); |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 288 | if (netdev->ifalias) |
| 289 | ret = sprintf(buf, "%s\n", netdev->ifalias); |
| 290 | rtnl_unlock(); |
| 291 | return ret; |
| 292 | } |
| 293 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 294 | static struct device_attribute net_class_attributes[] = { |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 295 | __ATTR(addr_len, S_IRUGO, show_addr_len, NULL), |
David Woodhouse | 9d29672 | 2008-04-20 16:07:43 -0700 | [diff] [blame] | 296 | __ATTR(dev_id, S_IRUGO, show_dev_id, NULL), |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 297 | __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias), |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 298 | __ATTR(iflink, S_IRUGO, show_iflink, NULL), |
| 299 | __ATTR(ifindex, S_IRUGO, show_ifindex, NULL), |
| 300 | __ATTR(features, S_IRUGO, show_features, NULL), |
| 301 | __ATTR(type, S_IRUGO, show_type, NULL), |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 302 | __ATTR(link_mode, S_IRUGO, show_link_mode, NULL), |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 303 | __ATTR(address, S_IRUGO, show_address, NULL), |
| 304 | __ATTR(broadcast, S_IRUGO, show_broadcast, NULL), |
| 305 | __ATTR(carrier, S_IRUGO, show_carrier, NULL), |
Andy Gospodarek | d519e17 | 2009-10-02 09:26:12 +0000 | [diff] [blame] | 306 | __ATTR(speed, S_IRUGO, show_speed, NULL), |
| 307 | __ATTR(duplex, S_IRUGO, show_duplex, NULL), |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 308 | __ATTR(dormant, S_IRUGO, show_dormant, NULL), |
| 309 | __ATTR(operstate, S_IRUGO, show_operstate, NULL), |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 310 | __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu), |
| 311 | __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags), |
| 312 | __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len, |
| 313 | store_tx_queue_len), |
Kay Sievers | fd586ba | 2005-12-19 01:42:56 +0100 | [diff] [blame] | 314 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | /* Show a given an attribute in the statistics group */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 318 | static ssize_t netstat_show(const struct device *d, |
| 319 | struct device_attribute *attr, char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | unsigned long offset) |
| 321 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 322 | struct net_device *dev = to_net_dev(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | ssize_t ret = -EINVAL; |
| 324 | |
Pavel Emelyanov | df1b86c | 2007-11-30 00:42:42 +1100 | [diff] [blame] | 325 | WARN_ON(offset > sizeof(struct net_device_stats) || |
| 326 | offset % sizeof(unsigned long) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | read_lock(&dev_base_lock); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 329 | if (dev_isalive(dev)) { |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 330 | const struct net_device_stats *stats = dev_get_stats(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | ret = sprintf(buf, fmt_ulong, |
| 332 | *(unsigned long *)(((u8 *) stats) + offset)); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | read_unlock(&dev_base_lock); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | /* generate a read-only statistics attribute */ |
| 339 | #define NETSTAT_ENTRY(name) \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 340 | static ssize_t show_##name(struct device *d, \ |
| 341 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 343 | return netstat_show(d, attr, buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | offsetof(struct net_device_stats, name)); \ |
| 345 | } \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 346 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | NETSTAT_ENTRY(rx_packets); |
| 349 | NETSTAT_ENTRY(tx_packets); |
| 350 | NETSTAT_ENTRY(rx_bytes); |
| 351 | NETSTAT_ENTRY(tx_bytes); |
| 352 | NETSTAT_ENTRY(rx_errors); |
| 353 | NETSTAT_ENTRY(tx_errors); |
| 354 | NETSTAT_ENTRY(rx_dropped); |
| 355 | NETSTAT_ENTRY(tx_dropped); |
| 356 | NETSTAT_ENTRY(multicast); |
| 357 | NETSTAT_ENTRY(collisions); |
| 358 | NETSTAT_ENTRY(rx_length_errors); |
| 359 | NETSTAT_ENTRY(rx_over_errors); |
| 360 | NETSTAT_ENTRY(rx_crc_errors); |
| 361 | NETSTAT_ENTRY(rx_frame_errors); |
| 362 | NETSTAT_ENTRY(rx_fifo_errors); |
| 363 | NETSTAT_ENTRY(rx_missed_errors); |
| 364 | NETSTAT_ENTRY(tx_aborted_errors); |
| 365 | NETSTAT_ENTRY(tx_carrier_errors); |
| 366 | NETSTAT_ENTRY(tx_fifo_errors); |
| 367 | NETSTAT_ENTRY(tx_heartbeat_errors); |
| 368 | NETSTAT_ENTRY(tx_window_errors); |
| 369 | NETSTAT_ENTRY(rx_compressed); |
| 370 | NETSTAT_ENTRY(tx_compressed); |
| 371 | |
| 372 | static struct attribute *netstat_attrs[] = { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 373 | &dev_attr_rx_packets.attr, |
| 374 | &dev_attr_tx_packets.attr, |
| 375 | &dev_attr_rx_bytes.attr, |
| 376 | &dev_attr_tx_bytes.attr, |
| 377 | &dev_attr_rx_errors.attr, |
| 378 | &dev_attr_tx_errors.attr, |
| 379 | &dev_attr_rx_dropped.attr, |
| 380 | &dev_attr_tx_dropped.attr, |
| 381 | &dev_attr_multicast.attr, |
| 382 | &dev_attr_collisions.attr, |
| 383 | &dev_attr_rx_length_errors.attr, |
| 384 | &dev_attr_rx_over_errors.attr, |
| 385 | &dev_attr_rx_crc_errors.attr, |
| 386 | &dev_attr_rx_frame_errors.attr, |
| 387 | &dev_attr_rx_fifo_errors.attr, |
| 388 | &dev_attr_rx_missed_errors.attr, |
| 389 | &dev_attr_tx_aborted_errors.attr, |
| 390 | &dev_attr_tx_carrier_errors.attr, |
| 391 | &dev_attr_tx_fifo_errors.attr, |
| 392 | &dev_attr_tx_heartbeat_errors.attr, |
| 393 | &dev_attr_tx_window_errors.attr, |
| 394 | &dev_attr_rx_compressed.attr, |
| 395 | &dev_attr_tx_compressed.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | NULL |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | static struct attribute_group netstat_group = { |
| 401 | .name = "statistics", |
| 402 | .attrs = netstat_attrs, |
| 403 | }; |
| 404 | |
Johannes Berg | 22bb1be | 2008-07-10 11:16:47 +0200 | [diff] [blame] | 405 | #ifdef CONFIG_WIRELESS_EXT_SYSFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* helper function that does all the locking etc for wireless stats */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 407 | static ssize_t wireless_show(struct device *d, char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | ssize_t (*format)(const struct iw_statistics *, |
| 409 | char *)) |
| 410 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 411 | struct net_device *dev = to_net_dev(d); |
Johannes Berg | 8f1546c | 2009-09-28 15:26:43 +0200 | [diff] [blame] | 412 | const struct iw_statistics *iw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | ssize_t ret = -EINVAL; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 414 | |
Eric W. Biederman | b8afe64 | 2010-02-19 13:23:47 +0000 | [diff] [blame] | 415 | if (!rtnl_trylock()) |
| 416 | return restart_syscall(); |
Andrey Borzenkov | 6dd214b | 2006-01-09 20:51:28 -0800 | [diff] [blame] | 417 | if (dev_isalive(dev)) { |
Johannes Berg | 8f1546c | 2009-09-28 15:26:43 +0200 | [diff] [blame] | 418 | iw = get_wireless_stats(dev); |
| 419 | if (iw) |
Andrey Borzenkov | 6dd214b | 2006-01-09 20:51:28 -0800 | [diff] [blame] | 420 | ret = (*format)(iw, buf); |
| 421 | } |
Johannes Berg | a160ee6 | 2009-10-05 02:22:23 -0700 | [diff] [blame] | 422 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | /* show function template for wireless fields */ |
| 428 | #define WIRELESS_SHOW(name, field, format_string) \ |
| 429 | static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \ |
| 430 | { \ |
| 431 | return sprintf(buf, format_string, iw->field); \ |
| 432 | } \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 433 | static ssize_t show_iw_##name(struct device *d, \ |
| 434 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 436 | return wireless_show(d, buf, format_iw_##name); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 438 | static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | WIRELESS_SHOW(status, status, fmt_hex); |
| 441 | WIRELESS_SHOW(link, qual.qual, fmt_dec); |
| 442 | WIRELESS_SHOW(level, qual.level, fmt_dec); |
| 443 | WIRELESS_SHOW(noise, qual.noise, fmt_dec); |
| 444 | WIRELESS_SHOW(nwid, discard.nwid, fmt_dec); |
| 445 | WIRELESS_SHOW(crypt, discard.code, fmt_dec); |
| 446 | WIRELESS_SHOW(fragment, discard.fragment, fmt_dec); |
| 447 | WIRELESS_SHOW(misc, discard.misc, fmt_dec); |
| 448 | WIRELESS_SHOW(retries, discard.retries, fmt_dec); |
| 449 | WIRELESS_SHOW(beacon, miss.beacon, fmt_dec); |
| 450 | |
| 451 | static struct attribute *wireless_attrs[] = { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 452 | &dev_attr_status.attr, |
| 453 | &dev_attr_link.attr, |
| 454 | &dev_attr_level.attr, |
| 455 | &dev_attr_noise.attr, |
| 456 | &dev_attr_nwid.attr, |
| 457 | &dev_attr_crypt.attr, |
| 458 | &dev_attr_fragment.attr, |
| 459 | &dev_attr_retries.attr, |
| 460 | &dev_attr_misc.attr, |
| 461 | &dev_attr_beacon.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | NULL |
| 463 | }; |
| 464 | |
| 465 | static struct attribute_group wireless_group = { |
| 466 | .name = "wireless", |
| 467 | .attrs = wireless_attrs, |
| 468 | }; |
| 469 | #endif |
| 470 | |
Stephen Rothwell | 30bde1f | 2010-03-29 01:00:44 -0700 | [diff] [blame] | 471 | #ifdef CONFIG_RPS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 472 | /* |
| 473 | * RX queue sysfs structures and functions. |
| 474 | */ |
| 475 | struct rx_queue_attribute { |
| 476 | struct attribute attr; |
| 477 | ssize_t (*show)(struct netdev_rx_queue *queue, |
| 478 | struct rx_queue_attribute *attr, char *buf); |
| 479 | ssize_t (*store)(struct netdev_rx_queue *queue, |
| 480 | struct rx_queue_attribute *attr, const char *buf, size_t len); |
| 481 | }; |
| 482 | #define to_rx_queue_attr(_attr) container_of(_attr, \ |
| 483 | struct rx_queue_attribute, attr) |
| 484 | |
| 485 | #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) |
| 486 | |
| 487 | static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, |
| 488 | char *buf) |
| 489 | { |
| 490 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
| 491 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
| 492 | |
| 493 | if (!attribute->show) |
| 494 | return -EIO; |
| 495 | |
| 496 | return attribute->show(queue, attribute, buf); |
| 497 | } |
| 498 | |
| 499 | static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, |
| 500 | const char *buf, size_t count) |
| 501 | { |
| 502 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
| 503 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
| 504 | |
| 505 | if (!attribute->store) |
| 506 | return -EIO; |
| 507 | |
| 508 | return attribute->store(queue, attribute, buf, count); |
| 509 | } |
| 510 | |
| 511 | static struct sysfs_ops rx_queue_sysfs_ops = { |
| 512 | .show = rx_queue_attr_show, |
| 513 | .store = rx_queue_attr_store, |
| 514 | }; |
| 515 | |
| 516 | static ssize_t show_rps_map(struct netdev_rx_queue *queue, |
| 517 | struct rx_queue_attribute *attribute, char *buf) |
| 518 | { |
| 519 | struct rps_map *map; |
| 520 | cpumask_var_t mask; |
| 521 | size_t len = 0; |
| 522 | int i; |
| 523 | |
| 524 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | rcu_read_lock(); |
| 528 | map = rcu_dereference(queue->rps_map); |
| 529 | if (map) |
| 530 | for (i = 0; i < map->len; i++) |
| 531 | cpumask_set_cpu(map->cpus[i], mask); |
| 532 | |
| 533 | len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); |
| 534 | if (PAGE_SIZE - len < 3) { |
| 535 | rcu_read_unlock(); |
| 536 | free_cpumask_var(mask); |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | rcu_read_unlock(); |
| 540 | |
| 541 | free_cpumask_var(mask); |
| 542 | len += sprintf(buf + len, "\n"); |
| 543 | return len; |
| 544 | } |
| 545 | |
| 546 | static void rps_map_release(struct rcu_head *rcu) |
| 547 | { |
| 548 | struct rps_map *map = container_of(rcu, struct rps_map, rcu); |
| 549 | |
| 550 | kfree(map); |
| 551 | } |
| 552 | |
Eric Dumazet | f5acb90 | 2010-04-19 14:40:57 -0700 | [diff] [blame^] | 553 | static ssize_t store_rps_map(struct netdev_rx_queue *queue, |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 554 | struct rx_queue_attribute *attribute, |
| 555 | const char *buf, size_t len) |
| 556 | { |
| 557 | struct rps_map *old_map, *map; |
| 558 | cpumask_var_t mask; |
| 559 | int err, cpu, i; |
| 560 | static DEFINE_SPINLOCK(rps_map_lock); |
| 561 | |
| 562 | if (!capable(CAP_NET_ADMIN)) |
| 563 | return -EPERM; |
| 564 | |
| 565 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); |
| 569 | if (err) { |
| 570 | free_cpumask_var(mask); |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | map = kzalloc(max_t(unsigned, |
| 575 | RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), |
| 576 | GFP_KERNEL); |
| 577 | if (!map) { |
| 578 | free_cpumask_var(mask); |
| 579 | return -ENOMEM; |
| 580 | } |
| 581 | |
| 582 | i = 0; |
| 583 | for_each_cpu_and(cpu, mask, cpu_online_mask) |
| 584 | map->cpus[i++] = cpu; |
| 585 | |
| 586 | if (i) |
| 587 | map->len = i; |
| 588 | else { |
| 589 | kfree(map); |
| 590 | map = NULL; |
| 591 | } |
| 592 | |
| 593 | spin_lock(&rps_map_lock); |
| 594 | old_map = queue->rps_map; |
| 595 | rcu_assign_pointer(queue->rps_map, map); |
| 596 | spin_unlock(&rps_map_lock); |
| 597 | |
| 598 | if (old_map) |
| 599 | call_rcu(&old_map->rcu, rps_map_release); |
| 600 | |
| 601 | free_cpumask_var(mask); |
| 602 | return len; |
| 603 | } |
| 604 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 605 | static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
| 606 | struct rx_queue_attribute *attr, |
| 607 | char *buf) |
| 608 | { |
| 609 | struct rps_dev_flow_table *flow_table; |
| 610 | unsigned int val = 0; |
| 611 | |
| 612 | rcu_read_lock(); |
| 613 | flow_table = rcu_dereference(queue->rps_flow_table); |
| 614 | if (flow_table) |
| 615 | val = flow_table->mask + 1; |
| 616 | rcu_read_unlock(); |
| 617 | |
| 618 | return sprintf(buf, "%u\n", val); |
| 619 | } |
| 620 | |
| 621 | static void rps_dev_flow_table_release_work(struct work_struct *work) |
| 622 | { |
| 623 | struct rps_dev_flow_table *table = container_of(work, |
| 624 | struct rps_dev_flow_table, free_work); |
| 625 | |
| 626 | vfree(table); |
| 627 | } |
| 628 | |
| 629 | static void rps_dev_flow_table_release(struct rcu_head *rcu) |
| 630 | { |
| 631 | struct rps_dev_flow_table *table = container_of(rcu, |
| 632 | struct rps_dev_flow_table, rcu); |
| 633 | |
| 634 | INIT_WORK(&table->free_work, rps_dev_flow_table_release_work); |
| 635 | schedule_work(&table->free_work); |
| 636 | } |
| 637 | |
Eric Dumazet | f5acb90 | 2010-04-19 14:40:57 -0700 | [diff] [blame^] | 638 | 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] | 639 | struct rx_queue_attribute *attr, |
| 640 | const char *buf, size_t len) |
| 641 | { |
| 642 | unsigned int count; |
| 643 | char *endp; |
| 644 | struct rps_dev_flow_table *table, *old_table; |
| 645 | static DEFINE_SPINLOCK(rps_dev_flow_lock); |
| 646 | |
| 647 | if (!capable(CAP_NET_ADMIN)) |
| 648 | return -EPERM; |
| 649 | |
| 650 | count = simple_strtoul(buf, &endp, 0); |
| 651 | if (endp == buf) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | if (count) { |
| 655 | int i; |
| 656 | |
| 657 | if (count > 1<<30) { |
| 658 | /* Enforce a limit to prevent overflow */ |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | count = roundup_pow_of_two(count); |
| 662 | table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count)); |
| 663 | if (!table) |
| 664 | return -ENOMEM; |
| 665 | |
| 666 | table->mask = count - 1; |
| 667 | for (i = 0; i < count; i++) |
| 668 | table->flows[i].cpu = RPS_NO_CPU; |
| 669 | } else |
| 670 | table = NULL; |
| 671 | |
| 672 | spin_lock(&rps_dev_flow_lock); |
| 673 | old_table = queue->rps_flow_table; |
| 674 | rcu_assign_pointer(queue->rps_flow_table, table); |
| 675 | spin_unlock(&rps_dev_flow_lock); |
| 676 | |
| 677 | if (old_table) |
| 678 | call_rcu(&old_table->rcu, rps_dev_flow_table_release); |
| 679 | |
| 680 | return len; |
| 681 | } |
| 682 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 683 | static struct rx_queue_attribute rps_cpus_attribute = |
| 684 | __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); |
| 685 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 686 | |
| 687 | static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = |
| 688 | __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, |
| 689 | show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); |
| 690 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 691 | static struct attribute *rx_queue_default_attrs[] = { |
| 692 | &rps_cpus_attribute.attr, |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 693 | &rps_dev_flow_table_cnt_attribute.attr, |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 694 | NULL |
| 695 | }; |
| 696 | |
| 697 | static void rx_queue_release(struct kobject *kobj) |
| 698 | { |
| 699 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 700 | struct netdev_rx_queue *first = queue->first; |
| 701 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 702 | if (queue->rps_map) |
| 703 | call_rcu(&queue->rps_map->rcu, rps_map_release); |
| 704 | |
| 705 | if (queue->rps_flow_table) |
| 706 | call_rcu(&queue->rps_flow_table->rcu, |
| 707 | rps_dev_flow_table_release); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 708 | |
| 709 | if (atomic_dec_and_test(&first->count)) |
| 710 | kfree(first); |
| 711 | } |
| 712 | |
| 713 | static struct kobj_type rx_queue_ktype = { |
| 714 | .sysfs_ops = &rx_queue_sysfs_ops, |
| 715 | .release = rx_queue_release, |
| 716 | .default_attrs = rx_queue_default_attrs, |
| 717 | }; |
| 718 | |
| 719 | static int rx_queue_add_kobject(struct net_device *net, int index) |
| 720 | { |
| 721 | struct netdev_rx_queue *queue = net->_rx + index; |
| 722 | struct kobject *kobj = &queue->kobj; |
| 723 | int error = 0; |
| 724 | |
| 725 | kobj->kset = net->queues_kset; |
| 726 | error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, |
| 727 | "rx-%u", index); |
| 728 | if (error) { |
| 729 | kobject_put(kobj); |
| 730 | return error; |
| 731 | } |
| 732 | |
| 733 | kobject_uevent(kobj, KOBJ_ADD); |
| 734 | |
| 735 | return error; |
| 736 | } |
| 737 | |
| 738 | static int rx_queue_register_kobjects(struct net_device *net) |
| 739 | { |
| 740 | int i; |
| 741 | int error = 0; |
| 742 | |
| 743 | net->queues_kset = kset_create_and_add("queues", |
| 744 | NULL, &net->dev.kobj); |
| 745 | if (!net->queues_kset) |
| 746 | return -ENOMEM; |
| 747 | for (i = 0; i < net->num_rx_queues; i++) { |
| 748 | error = rx_queue_add_kobject(net, i); |
| 749 | if (error) |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | if (error) |
| 754 | while (--i >= 0) |
| 755 | kobject_put(&net->_rx[i].kobj); |
| 756 | |
| 757 | return error; |
| 758 | } |
| 759 | |
| 760 | static void rx_queue_remove_kobjects(struct net_device *net) |
| 761 | { |
| 762 | int i; |
| 763 | |
| 764 | for (i = 0; i < net->num_rx_queues; i++) |
| 765 | kobject_put(&net->_rx[i].kobj); |
| 766 | kset_unregister(net->queues_kset); |
| 767 | } |
Stephen Rothwell | 30bde1f | 2010-03-29 01:00:44 -0700 | [diff] [blame] | 768 | #endif /* CONFIG_RPS */ |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 769 | #endif /* CONFIG_SYSFS */ |
| 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | #ifdef CONFIG_HOTPLUG |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 772 | static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 774 | struct net_device *dev = to_net_dev(d); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 775 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Daniel Lezcano | 09bb521 | 2008-11-25 16:46:37 -0800 | [diff] [blame] | 777 | if (!net_eq(dev_net(dev), &init_net)) |
| 778 | return 0; |
| 779 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 780 | /* pass interface to uevent. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 781 | retval = add_uevent_var(env, "INTERFACE=%s", dev->name); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 782 | if (retval) |
| 783 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 785 | /* pass ifindex to uevent. |
| 786 | * ifindex is useful as it won't change (interface name may change) |
| 787 | * and is what RtNetlink uses natively. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 788 | retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 789 | |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 790 | exit: |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 791 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | #endif |
| 794 | |
| 795 | /* |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 796 | * netdev_release -- destroy and free a dead device. |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 797 | * Called when last reference to device kobject is gone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 799 | static void netdev_release(struct device *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 801 | struct net_device *dev = to_net_dev(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | BUG_ON(dev->reg_state != NETREG_RELEASED); |
| 804 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 805 | kfree(dev->ifalias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | kfree((char *)dev - dev->padded); |
| 807 | } |
| 808 | |
| 809 | static struct class net_class = { |
| 810 | .name = "net", |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 811 | .dev_release = netdev_release, |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 812 | #ifdef CONFIG_SYSFS |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 813 | .dev_attrs = net_class_attributes, |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 814 | #endif /* CONFIG_SYSFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | #ifdef CONFIG_HOTPLUG |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 816 | .dev_uevent = netdev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | #endif |
| 818 | }; |
| 819 | |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 820 | /* Delete sysfs entries but hold kobject reference until after all |
| 821 | * netdev references are gone. |
| 822 | */ |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 823 | void netdev_unregister_kobject(struct net_device * net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 825 | struct device *dev = &(net->dev); |
| 826 | |
| 827 | kobject_get(&dev->kobj); |
Eric W. Biederman | 3891845 | 2008-10-27 17:51:47 -0700 | [diff] [blame] | 828 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 829 | if (!net_eq(dev_net(net), &init_net)) |
Eric W. Biederman | 3891845 | 2008-10-27 17:51:47 -0700 | [diff] [blame] | 830 | return; |
| 831 | |
Stephen Rothwell | 30bde1f | 2010-03-29 01:00:44 -0700 | [diff] [blame] | 832 | #ifdef CONFIG_RPS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 833 | rx_queue_remove_kobjects(net); |
Tom Herbert | e880eb6 | 2010-03-22 18:06:47 -0700 | [diff] [blame] | 834 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 835 | |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 836 | device_del(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* Create sysfs entries for network device. */ |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 840 | int netdev_register_kobject(struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 842 | struct device *dev = &(net->dev); |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 843 | const struct attribute_group **groups = net->sysfs_groups; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 844 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 846 | dev->class = &net_class; |
| 847 | dev->platform_data = net; |
| 848 | dev->groups = groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Stephen Hemminger | a220547 | 2009-03-09 13:51:55 +0000 | [diff] [blame] | 850 | dev_set_name(dev, "%s", net->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 852 | #ifdef CONFIG_SYSFS |
Eric W. Biederman | 0c509a6 | 2009-10-29 14:18:21 +0000 | [diff] [blame] | 853 | /* Allow for a device specific group */ |
| 854 | if (*groups) |
| 855 | groups++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Eric W. Biederman | 0c509a6 | 2009-10-29 14:18:21 +0000 | [diff] [blame] | 857 | *groups++ = &netstat_group; |
Johannes Berg | 22bb1be | 2008-07-10 11:16:47 +0200 | [diff] [blame] | 858 | #ifdef CONFIG_WIRELESS_EXT_SYSFS |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 859 | if (net->ieee80211_ptr) |
Stephen Hemminger | fe9925b | 2006-05-06 17:56:03 -0700 | [diff] [blame] | 860 | *groups++ = &wireless_group; |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 861 | #ifdef CONFIG_WIRELESS_EXT |
| 862 | else if (net->wireless_handlers) |
| 863 | *groups++ = &wireless_group; |
| 864 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | #endif |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 866 | #endif /* CONFIG_SYSFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 868 | if (!net_eq(dev_net(net), &init_net)) |
Eric W. Biederman | 3891845 | 2008-10-27 17:51:47 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 871 | error = device_add(dev); |
| 872 | if (error) |
| 873 | return error; |
| 874 | |
Stephen Rothwell | 30bde1f | 2010-03-29 01:00:44 -0700 | [diff] [blame] | 875 | #ifdef CONFIG_RPS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 876 | error = rx_queue_register_kobjects(net); |
| 877 | if (error) { |
| 878 | device_del(dev); |
| 879 | return error; |
| 880 | } |
Tom Herbert | e880eb6 | 2010-03-22 18:06:47 -0700 | [diff] [blame] | 881 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 882 | |
| 883 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 886 | int netdev_class_create_file(struct class_attribute *class_attr) |
| 887 | { |
| 888 | return class_create_file(&net_class, class_attr); |
| 889 | } |
| 890 | |
| 891 | void netdev_class_remove_file(struct class_attribute *class_attr) |
| 892 | { |
| 893 | class_remove_file(&net_class, class_attr); |
| 894 | } |
| 895 | |
| 896 | EXPORT_SYMBOL(netdev_class_create_file); |
| 897 | EXPORT_SYMBOL(netdev_class_remove_file); |
| 898 | |
Daniel Lezcano | aaf8cdc | 2008-05-02 17:00:58 -0700 | [diff] [blame] | 899 | void netdev_initialize_kobject(struct net_device *net) |
| 900 | { |
| 901 | struct device *device = &(net->dev); |
| 902 | device_initialize(device); |
| 903 | } |
| 904 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 905 | int netdev_kobject_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
| 907 | return class_register(&net_class); |
| 908 | } |