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