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