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