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 | |
Linus Torvalds | cc998ff | 2013-09-05 14:54:29 -0700 | [diff] [blame] | 350 | static ssize_t phys_port_id_show(struct device *dev, |
Jiri Pirko | ff80e51 | 2013-07-29 18:16:51 +0200 | [diff] [blame] | 351 | struct device_attribute *attr, char *buf) |
| 352 | { |
| 353 | struct net_device *netdev = to_net_dev(dev); |
| 354 | ssize_t ret = -EINVAL; |
| 355 | |
| 356 | if (!rtnl_trylock()) |
| 357 | return restart_syscall(); |
| 358 | |
| 359 | if (dev_isalive(netdev)) { |
| 360 | struct netdev_phys_port_id ppid; |
| 361 | |
| 362 | ret = dev_get_phys_port_id(netdev, &ppid); |
| 363 | if (!ret) |
| 364 | ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id); |
| 365 | } |
| 366 | rtnl_unlock(); |
| 367 | |
| 368 | return ret; |
| 369 | } |
Linus Torvalds | cc998ff | 2013-09-05 14:54:29 -0700 | [diff] [blame] | 370 | static DEVICE_ATTR_RO(phys_port_id); |
Jiri Pirko | ff80e51 | 2013-07-29 18:16:51 +0200 | [diff] [blame] | 371 | |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 372 | static struct attribute *net_class_attrs[] = { |
| 373 | &dev_attr_netdev_group.attr, |
| 374 | &dev_attr_type.attr, |
| 375 | &dev_attr_dev_id.attr, |
| 376 | &dev_attr_iflink.attr, |
| 377 | &dev_attr_ifindex.attr, |
| 378 | &dev_attr_addr_assign_type.attr, |
| 379 | &dev_attr_addr_len.attr, |
| 380 | &dev_attr_link_mode.attr, |
| 381 | &dev_attr_address.attr, |
| 382 | &dev_attr_broadcast.attr, |
| 383 | &dev_attr_speed.attr, |
| 384 | &dev_attr_duplex.attr, |
| 385 | &dev_attr_dormant.attr, |
| 386 | &dev_attr_operstate.attr, |
| 387 | &dev_attr_ifalias.attr, |
| 388 | &dev_attr_carrier.attr, |
| 389 | &dev_attr_mtu.attr, |
| 390 | &dev_attr_flags.attr, |
| 391 | &dev_attr_tx_queue_len.attr, |
Linus Torvalds | cc998ff | 2013-09-05 14:54:29 -0700 | [diff] [blame] | 392 | &dev_attr_phys_port_id.attr, |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 393 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | }; |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 395 | ATTRIBUTE_GROUPS(net_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* Show a given an attribute in the statistics group */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 398 | static ssize_t netstat_show(const struct device *d, |
| 399 | struct device_attribute *attr, char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | unsigned long offset) |
| 401 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 402 | struct net_device *dev = to_net_dev(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | ssize_t ret = -EINVAL; |
| 404 | |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 405 | WARN_ON(offset > sizeof(struct rtnl_link_stats64) || |
| 406 | offset % sizeof(u64) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | read_lock(&dev_base_lock); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 409 | if (dev_isalive(dev)) { |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 410 | struct rtnl_link_stats64 temp; |
| 411 | const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); |
| 412 | |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 413 | ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset)); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | read_unlock(&dev_base_lock); |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* generate a read-only statistics attribute */ |
| 420 | #define NETSTAT_ENTRY(name) \ |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 421 | static ssize_t name##_show(struct device *d, \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 422 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { \ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 424 | return netstat_show(d, attr, buf, \ |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 425 | offsetof(struct rtnl_link_stats64, name)); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } \ |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 427 | static DEVICE_ATTR_RO(name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | NETSTAT_ENTRY(rx_packets); |
| 430 | NETSTAT_ENTRY(tx_packets); |
| 431 | NETSTAT_ENTRY(rx_bytes); |
| 432 | NETSTAT_ENTRY(tx_bytes); |
| 433 | NETSTAT_ENTRY(rx_errors); |
| 434 | NETSTAT_ENTRY(tx_errors); |
| 435 | NETSTAT_ENTRY(rx_dropped); |
| 436 | NETSTAT_ENTRY(tx_dropped); |
| 437 | NETSTAT_ENTRY(multicast); |
| 438 | NETSTAT_ENTRY(collisions); |
| 439 | NETSTAT_ENTRY(rx_length_errors); |
| 440 | NETSTAT_ENTRY(rx_over_errors); |
| 441 | NETSTAT_ENTRY(rx_crc_errors); |
| 442 | NETSTAT_ENTRY(rx_frame_errors); |
| 443 | NETSTAT_ENTRY(rx_fifo_errors); |
| 444 | NETSTAT_ENTRY(rx_missed_errors); |
| 445 | NETSTAT_ENTRY(tx_aborted_errors); |
| 446 | NETSTAT_ENTRY(tx_carrier_errors); |
| 447 | NETSTAT_ENTRY(tx_fifo_errors); |
| 448 | NETSTAT_ENTRY(tx_heartbeat_errors); |
| 449 | NETSTAT_ENTRY(tx_window_errors); |
| 450 | NETSTAT_ENTRY(rx_compressed); |
| 451 | NETSTAT_ENTRY(tx_compressed); |
| 452 | |
| 453 | static struct attribute *netstat_attrs[] = { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 454 | &dev_attr_rx_packets.attr, |
| 455 | &dev_attr_tx_packets.attr, |
| 456 | &dev_attr_rx_bytes.attr, |
| 457 | &dev_attr_tx_bytes.attr, |
| 458 | &dev_attr_rx_errors.attr, |
| 459 | &dev_attr_tx_errors.attr, |
| 460 | &dev_attr_rx_dropped.attr, |
| 461 | &dev_attr_tx_dropped.attr, |
| 462 | &dev_attr_multicast.attr, |
| 463 | &dev_attr_collisions.attr, |
| 464 | &dev_attr_rx_length_errors.attr, |
| 465 | &dev_attr_rx_over_errors.attr, |
| 466 | &dev_attr_rx_crc_errors.attr, |
| 467 | &dev_attr_rx_frame_errors.attr, |
| 468 | &dev_attr_rx_fifo_errors.attr, |
| 469 | &dev_attr_rx_missed_errors.attr, |
| 470 | &dev_attr_tx_aborted_errors.attr, |
| 471 | &dev_attr_tx_carrier_errors.attr, |
| 472 | &dev_attr_tx_fifo_errors.attr, |
| 473 | &dev_attr_tx_heartbeat_errors.attr, |
| 474 | &dev_attr_tx_window_errors.attr, |
| 475 | &dev_attr_rx_compressed.attr, |
| 476 | &dev_attr_tx_compressed.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | NULL |
| 478 | }; |
| 479 | |
| 480 | |
| 481 | static struct attribute_group netstat_group = { |
| 482 | .name = "statistics", |
| 483 | .attrs = netstat_attrs, |
| 484 | }; |
Johannes Berg | 38c1a01 | 2012-11-16 20:46:19 +0100 | [diff] [blame] | 485 | |
| 486 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) |
| 487 | static struct attribute *wireless_attrs[] = { |
| 488 | NULL |
| 489 | }; |
| 490 | |
| 491 | static struct attribute_group wireless_group = { |
| 492 | .name = "wireless", |
| 493 | .attrs = wireless_attrs, |
| 494 | }; |
| 495 | #endif |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 496 | |
| 497 | #else /* CONFIG_SYSFS */ |
| 498 | #define net_class_groups NULL |
Eric W. Biederman | d6523dd | 2010-05-16 21:59:45 -0700 | [diff] [blame] | 499 | #endif /* CONFIG_SYSFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 501 | #ifdef CONFIG_SYSFS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 502 | #define to_rx_queue_attr(_attr) container_of(_attr, \ |
| 503 | struct rx_queue_attribute, attr) |
| 504 | |
| 505 | #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) |
| 506 | |
| 507 | static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, |
| 508 | char *buf) |
| 509 | { |
| 510 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
| 511 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
| 512 | |
| 513 | if (!attribute->show) |
| 514 | return -EIO; |
| 515 | |
| 516 | return attribute->show(queue, attribute, buf); |
| 517 | } |
| 518 | |
| 519 | static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, |
| 520 | const char *buf, size_t count) |
| 521 | { |
| 522 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
| 523 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
| 524 | |
| 525 | if (!attribute->store) |
| 526 | return -EIO; |
| 527 | |
| 528 | return attribute->store(queue, attribute, buf, count); |
| 529 | } |
| 530 | |
stephen hemminger | fa50d64 | 2010-08-31 12:14:13 +0000 | [diff] [blame] | 531 | static const struct sysfs_ops rx_queue_sysfs_ops = { |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 532 | .show = rx_queue_attr_show, |
| 533 | .store = rx_queue_attr_store, |
| 534 | }; |
| 535 | |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 536 | #ifdef CONFIG_RPS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 537 | static ssize_t show_rps_map(struct netdev_rx_queue *queue, |
| 538 | struct rx_queue_attribute *attribute, char *buf) |
| 539 | { |
| 540 | struct rps_map *map; |
| 541 | cpumask_var_t mask; |
| 542 | size_t len = 0; |
| 543 | int i; |
| 544 | |
| 545 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) |
| 546 | return -ENOMEM; |
| 547 | |
| 548 | rcu_read_lock(); |
| 549 | map = rcu_dereference(queue->rps_map); |
| 550 | if (map) |
| 551 | for (i = 0; i < map->len; i++) |
| 552 | cpumask_set_cpu(map->cpus[i], mask); |
| 553 | |
| 554 | len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); |
| 555 | if (PAGE_SIZE - len < 3) { |
| 556 | rcu_read_unlock(); |
| 557 | free_cpumask_var(mask); |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | rcu_read_unlock(); |
| 561 | |
| 562 | free_cpumask_var(mask); |
| 563 | len += sprintf(buf + len, "\n"); |
| 564 | return len; |
| 565 | } |
| 566 | |
Eric Dumazet | f5acb90 | 2010-04-19 14:40:57 -0700 | [diff] [blame] | 567 | static ssize_t store_rps_map(struct netdev_rx_queue *queue, |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 568 | struct rx_queue_attribute *attribute, |
| 569 | const char *buf, size_t len) |
| 570 | { |
| 571 | struct rps_map *old_map, *map; |
| 572 | cpumask_var_t mask; |
| 573 | int err, cpu, i; |
| 574 | static DEFINE_SPINLOCK(rps_map_lock); |
| 575 | |
| 576 | if (!capable(CAP_NET_ADMIN)) |
| 577 | return -EPERM; |
| 578 | |
| 579 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 580 | return -ENOMEM; |
| 581 | |
| 582 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); |
| 583 | if (err) { |
| 584 | free_cpumask_var(mask); |
| 585 | return err; |
| 586 | } |
| 587 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 588 | map = kzalloc(max_t(unsigned int, |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 589 | RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), |
| 590 | GFP_KERNEL); |
| 591 | if (!map) { |
| 592 | free_cpumask_var(mask); |
| 593 | return -ENOMEM; |
| 594 | } |
| 595 | |
| 596 | i = 0; |
| 597 | for_each_cpu_and(cpu, mask, cpu_online_mask) |
| 598 | map->cpus[i++] = cpu; |
| 599 | |
| 600 | if (i) |
| 601 | map->len = i; |
| 602 | else { |
| 603 | kfree(map); |
| 604 | map = NULL; |
| 605 | } |
| 606 | |
| 607 | spin_lock(&rps_map_lock); |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 608 | old_map = rcu_dereference_protected(queue->rps_map, |
| 609 | lockdep_is_held(&rps_map_lock)); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 610 | rcu_assign_pointer(queue->rps_map, map); |
| 611 | spin_unlock(&rps_map_lock); |
| 612 | |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 613 | if (map) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 614 | static_key_slow_inc(&rps_needed); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 615 | if (old_map) { |
Lai Jiangshan | f6f8023 | 2011-03-18 12:01:31 +0800 | [diff] [blame] | 616 | kfree_rcu(old_map, rcu); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 617 | static_key_slow_dec(&rps_needed); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 618 | } |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 619 | free_cpumask_var(mask); |
| 620 | return len; |
| 621 | } |
| 622 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 623 | static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
| 624 | struct rx_queue_attribute *attr, |
| 625 | char *buf) |
| 626 | { |
| 627 | struct rps_dev_flow_table *flow_table; |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 628 | unsigned long val = 0; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 629 | |
| 630 | rcu_read_lock(); |
| 631 | flow_table = rcu_dereference(queue->rps_flow_table); |
| 632 | if (flow_table) |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 633 | val = (unsigned long)flow_table->mask + 1; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 634 | rcu_read_unlock(); |
| 635 | |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 636 | return sprintf(buf, "%lu\n", val); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 639 | static void rps_dev_flow_table_release(struct rcu_head *rcu) |
| 640 | { |
| 641 | struct rps_dev_flow_table *table = container_of(rcu, |
| 642 | struct rps_dev_flow_table, rcu); |
Al Viro | 243198d | 2013-05-05 16:05:55 +0000 | [diff] [blame] | 643 | vfree(table); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Eric Dumazet | f5acb90 | 2010-04-19 14:40:57 -0700 | [diff] [blame] | 646 | 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] | 647 | struct rx_queue_attribute *attr, |
| 648 | const char *buf, size_t len) |
| 649 | { |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 650 | unsigned long mask, count; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 651 | struct rps_dev_flow_table *table, *old_table; |
| 652 | static DEFINE_SPINLOCK(rps_dev_flow_lock); |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 653 | int rc; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 654 | |
| 655 | if (!capable(CAP_NET_ADMIN)) |
| 656 | return -EPERM; |
| 657 | |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 658 | rc = kstrtoul(buf, 0, &count); |
| 659 | if (rc < 0) |
| 660 | return rc; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 661 | |
| 662 | if (count) { |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 663 | mask = count - 1; |
| 664 | /* mask = roundup_pow_of_two(count) - 1; |
| 665 | * without overflows... |
| 666 | */ |
| 667 | while ((mask | (mask >> 1)) != mask) |
| 668 | mask |= (mask >> 1); |
| 669 | /* On 64 bit arches, must check mask fits in table->mask (u32), |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 670 | * and on 32bit arches, must check |
| 671 | * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow. |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 672 | */ |
| 673 | #if BITS_PER_LONG > 32 |
| 674 | if (mask > (unsigned long)(u32)mask) |
Xi Wang | a0a129f | 2011-12-22 13:35:22 +0000 | [diff] [blame] | 675 | return -EINVAL; |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 676 | #else |
| 677 | if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) |
Xi Wang | a0a129f | 2011-12-22 13:35:22 +0000 | [diff] [blame] | 678 | / sizeof(struct rps_dev_flow)) { |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 679 | /* Enforce a limit to prevent overflow */ |
| 680 | return -EINVAL; |
| 681 | } |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 682 | #endif |
| 683 | table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 684 | if (!table) |
| 685 | return -ENOMEM; |
| 686 | |
Eric Dumazet | 60b778c | 2011-12-24 06:56:49 +0000 | [diff] [blame] | 687 | table->mask = mask; |
| 688 | for (count = 0; count <= mask; count++) |
| 689 | table->flows[count].cpu = RPS_NO_CPU; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 690 | } else |
| 691 | table = NULL; |
| 692 | |
| 693 | spin_lock(&rps_dev_flow_lock); |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 694 | old_table = rcu_dereference_protected(queue->rps_flow_table, |
| 695 | lockdep_is_held(&rps_dev_flow_lock)); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 696 | rcu_assign_pointer(queue->rps_flow_table, table); |
| 697 | spin_unlock(&rps_dev_flow_lock); |
| 698 | |
| 699 | if (old_table) |
| 700 | call_rcu(&old_table->rcu, rps_dev_flow_table_release); |
| 701 | |
| 702 | return len; |
| 703 | } |
| 704 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 705 | static struct rx_queue_attribute rps_cpus_attribute = |
| 706 | __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); |
| 707 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 708 | |
| 709 | static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = |
| 710 | __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, |
| 711 | show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 712 | #endif /* CONFIG_RPS */ |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 713 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 714 | static struct attribute *rx_queue_default_attrs[] = { |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 715 | #ifdef CONFIG_RPS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 716 | &rps_cpus_attribute.attr, |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 717 | &rps_dev_flow_table_cnt_attribute.attr, |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 718 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 719 | NULL |
| 720 | }; |
| 721 | |
| 722 | static void rx_queue_release(struct kobject *kobj) |
| 723 | { |
| 724 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 725 | #ifdef CONFIG_RPS |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 726 | struct rps_map *map; |
| 727 | struct rps_dev_flow_table *flow_table; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 728 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 729 | |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 730 | map = rcu_dereference_protected(queue->rps_map, 1); |
John Fastabend | 9ea1948 | 2010-11-16 06:31:39 +0000 | [diff] [blame] | 731 | if (map) { |
| 732 | RCU_INIT_POINTER(queue->rps_map, NULL); |
Lai Jiangshan | f6f8023 | 2011-03-18 12:01:31 +0800 | [diff] [blame] | 733 | kfree_rcu(map, rcu); |
John Fastabend | 9ea1948 | 2010-11-16 06:31:39 +0000 | [diff] [blame] | 734 | } |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 735 | |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 736 | flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); |
John Fastabend | 9ea1948 | 2010-11-16 06:31:39 +0000 | [diff] [blame] | 737 | if (flow_table) { |
| 738 | RCU_INIT_POINTER(queue->rps_flow_table, NULL); |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 739 | call_rcu(&flow_table->rcu, rps_dev_flow_table_release); |
John Fastabend | 9ea1948 | 2010-11-16 06:31:39 +0000 | [diff] [blame] | 740 | } |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 741 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 742 | |
John Fastabend | 9ea1948 | 2010-11-16 06:31:39 +0000 | [diff] [blame] | 743 | memset(kobj, 0, sizeof(*kobj)); |
Tom Herbert | fe82224 | 2010-11-09 10:47:38 +0000 | [diff] [blame] | 744 | dev_put(queue->dev); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Weilong Chen | 82ef3d5 | 2014-01-16 17:24:31 +0800 | [diff] [blame] | 747 | static const void *rx_queue_namespace(struct kobject *kobj) |
| 748 | { |
| 749 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
| 750 | struct device *dev = &queue->dev->dev; |
| 751 | const void *ns = NULL; |
| 752 | |
| 753 | if (dev->class && dev->class->ns_type) |
| 754 | ns = dev->class->namespace(dev); |
| 755 | |
| 756 | return ns; |
| 757 | } |
| 758 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 759 | static struct kobj_type rx_queue_ktype = { |
| 760 | .sysfs_ops = &rx_queue_sysfs_ops, |
| 761 | .release = rx_queue_release, |
| 762 | .default_attrs = rx_queue_default_attrs, |
Weilong Chen | 82ef3d5 | 2014-01-16 17:24:31 +0800 | [diff] [blame] | 763 | .namespace = rx_queue_namespace |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | static int rx_queue_add_kobject(struct net_device *net, int index) |
| 767 | { |
| 768 | struct netdev_rx_queue *queue = net->_rx + index; |
| 769 | struct kobject *kobj = &queue->kobj; |
| 770 | int error = 0; |
| 771 | |
| 772 | kobj->kset = net->queues_kset; |
| 773 | error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, |
| 774 | "rx-%u", index); |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 775 | if (error) |
| 776 | goto exit; |
| 777 | |
| 778 | if (net->sysfs_rx_queue_group) { |
| 779 | error = sysfs_create_group(kobj, net->sysfs_rx_queue_group); |
| 780 | if (error) |
| 781 | goto exit; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | kobject_uevent(kobj, KOBJ_ADD); |
Tom Herbert | fe82224 | 2010-11-09 10:47:38 +0000 | [diff] [blame] | 785 | dev_hold(queue->dev); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 786 | |
| 787 | return error; |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 788 | exit: |
| 789 | kobject_put(kobj); |
| 790 | return error; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 791 | } |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 792 | #endif /* CONFIG_SYFS */ |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 793 | |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 794 | int |
| 795 | 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] | 796 | { |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 797 | #ifdef CONFIG_SYSFS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 798 | int i; |
| 799 | int error = 0; |
| 800 | |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 801 | #ifndef CONFIG_RPS |
| 802 | if (!net->sysfs_rx_queue_group) |
| 803 | return 0; |
| 804 | #endif |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 805 | for (i = old_num; i < new_num; i++) { |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 806 | error = rx_queue_add_kobject(net, i); |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 807 | if (error) { |
| 808 | new_num = old_num; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 809 | break; |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 810 | } |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 813 | while (--i >= new_num) { |
| 814 | if (net->sysfs_rx_queue_group) |
| 815 | sysfs_remove_group(&net->_rx[i].kobj, |
| 816 | net->sysfs_rx_queue_group); |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 817 | kobject_put(&net->_rx[i].kobj); |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 818 | } |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 819 | |
| 820 | return error; |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 821 | #else |
| 822 | return 0; |
| 823 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 824 | } |
| 825 | |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 826 | #ifdef CONFIG_SYSFS |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 827 | /* |
| 828 | * netdev_queue sysfs structures and functions. |
| 829 | */ |
| 830 | struct netdev_queue_attribute { |
| 831 | struct attribute attr; |
| 832 | ssize_t (*show)(struct netdev_queue *queue, |
| 833 | struct netdev_queue_attribute *attr, char *buf); |
| 834 | ssize_t (*store)(struct netdev_queue *queue, |
| 835 | struct netdev_queue_attribute *attr, const char *buf, size_t len); |
| 836 | }; |
| 837 | #define to_netdev_queue_attr(_attr) container_of(_attr, \ |
| 838 | struct netdev_queue_attribute, attr) |
| 839 | |
| 840 | #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) |
| 841 | |
| 842 | static ssize_t netdev_queue_attr_show(struct kobject *kobj, |
| 843 | struct attribute *attr, char *buf) |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 844 | { |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 845 | struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); |
| 846 | struct netdev_queue *queue = to_netdev_queue(kobj); |
| 847 | |
| 848 | if (!attribute->show) |
| 849 | return -EIO; |
| 850 | |
| 851 | return attribute->show(queue, attribute, buf); |
| 852 | } |
| 853 | |
| 854 | static ssize_t netdev_queue_attr_store(struct kobject *kobj, |
| 855 | struct attribute *attr, |
| 856 | const char *buf, size_t count) |
| 857 | { |
| 858 | struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); |
| 859 | struct netdev_queue *queue = to_netdev_queue(kobj); |
| 860 | |
| 861 | if (!attribute->store) |
| 862 | return -EIO; |
| 863 | |
| 864 | return attribute->store(queue, attribute, buf, count); |
| 865 | } |
| 866 | |
| 867 | static const struct sysfs_ops netdev_queue_sysfs_ops = { |
| 868 | .show = netdev_queue_attr_show, |
| 869 | .store = netdev_queue_attr_store, |
| 870 | }; |
| 871 | |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 872 | static ssize_t show_trans_timeout(struct netdev_queue *queue, |
| 873 | struct netdev_queue_attribute *attribute, |
| 874 | char *buf) |
| 875 | { |
| 876 | unsigned long trans_timeout; |
| 877 | |
| 878 | spin_lock_irq(&queue->_xmit_lock); |
| 879 | trans_timeout = queue->trans_timeout; |
| 880 | spin_unlock_irq(&queue->_xmit_lock); |
| 881 | |
| 882 | return sprintf(buf, "%lu", trans_timeout); |
| 883 | } |
| 884 | |
| 885 | static struct netdev_queue_attribute queue_trans_timeout = |
| 886 | __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); |
| 887 | |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 888 | #ifdef CONFIG_BQL |
| 889 | /* |
| 890 | * Byte queue limits sysfs structures and functions. |
| 891 | */ |
| 892 | static ssize_t bql_show(char *buf, unsigned int value) |
| 893 | { |
| 894 | return sprintf(buf, "%u\n", value); |
| 895 | } |
| 896 | |
| 897 | static ssize_t bql_set(const char *buf, const size_t count, |
| 898 | unsigned int *pvalue) |
| 899 | { |
| 900 | unsigned int value; |
| 901 | int err; |
| 902 | |
| 903 | if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) |
| 904 | value = DQL_MAX_LIMIT; |
| 905 | else { |
| 906 | err = kstrtouint(buf, 10, &value); |
| 907 | if (err < 0) |
| 908 | return err; |
| 909 | if (value > DQL_MAX_LIMIT) |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
| 913 | *pvalue = value; |
| 914 | |
| 915 | return count; |
| 916 | } |
| 917 | |
| 918 | static ssize_t bql_show_hold_time(struct netdev_queue *queue, |
| 919 | struct netdev_queue_attribute *attr, |
| 920 | char *buf) |
| 921 | { |
| 922 | struct dql *dql = &queue->dql; |
| 923 | |
| 924 | return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); |
| 925 | } |
| 926 | |
| 927 | static ssize_t bql_set_hold_time(struct netdev_queue *queue, |
| 928 | struct netdev_queue_attribute *attribute, |
| 929 | const char *buf, size_t len) |
| 930 | { |
| 931 | struct dql *dql = &queue->dql; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 932 | unsigned int value; |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 933 | int err; |
| 934 | |
| 935 | err = kstrtouint(buf, 10, &value); |
| 936 | if (err < 0) |
| 937 | return err; |
| 938 | |
| 939 | dql->slack_hold_time = msecs_to_jiffies(value); |
| 940 | |
| 941 | return len; |
| 942 | } |
| 943 | |
| 944 | static struct netdev_queue_attribute bql_hold_time_attribute = |
| 945 | __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time, |
| 946 | bql_set_hold_time); |
| 947 | |
| 948 | static ssize_t bql_show_inflight(struct netdev_queue *queue, |
| 949 | struct netdev_queue_attribute *attr, |
| 950 | char *buf) |
| 951 | { |
| 952 | struct dql *dql = &queue->dql; |
| 953 | |
| 954 | return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); |
| 955 | } |
| 956 | |
| 957 | static struct netdev_queue_attribute bql_inflight_attribute = |
Hiroaki SHIMODA | 795d9a2 | 2012-01-14 07:10:21 +0000 | [diff] [blame] | 958 | __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL); |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 959 | |
| 960 | #define BQL_ATTR(NAME, FIELD) \ |
| 961 | static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ |
| 962 | struct netdev_queue_attribute *attr, \ |
| 963 | char *buf) \ |
| 964 | { \ |
| 965 | return bql_show(buf, queue->dql.FIELD); \ |
| 966 | } \ |
| 967 | \ |
| 968 | static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ |
| 969 | struct netdev_queue_attribute *attr, \ |
| 970 | const char *buf, size_t len) \ |
| 971 | { \ |
| 972 | return bql_set(buf, len, &queue->dql.FIELD); \ |
| 973 | } \ |
| 974 | \ |
| 975 | static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \ |
| 976 | __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \ |
| 977 | bql_set_ ## NAME); |
| 978 | |
| 979 | BQL_ATTR(limit, limit) |
| 980 | BQL_ATTR(limit_max, max_limit) |
| 981 | BQL_ATTR(limit_min, min_limit) |
| 982 | |
| 983 | static struct attribute *dql_attrs[] = { |
| 984 | &bql_limit_attribute.attr, |
| 985 | &bql_limit_max_attribute.attr, |
| 986 | &bql_limit_min_attribute.attr, |
| 987 | &bql_hold_time_attribute.attr, |
| 988 | &bql_inflight_attribute.attr, |
| 989 | NULL |
| 990 | }; |
| 991 | |
| 992 | static struct attribute_group dql_group = { |
| 993 | .name = "byte_queue_limits", |
| 994 | .attrs = dql_attrs, |
| 995 | }; |
| 996 | #endif /* CONFIG_BQL */ |
| 997 | |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 998 | #ifdef CONFIG_XPS |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 999 | static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue) |
| 1000 | { |
| 1001 | struct net_device *dev = queue->dev; |
| 1002 | int i; |
| 1003 | |
| 1004 | for (i = 0; i < dev->num_tx_queues; i++) |
| 1005 | if (queue == &dev->_tx[i]) |
| 1006 | break; |
| 1007 | |
| 1008 | BUG_ON(i >= dev->num_tx_queues); |
| 1009 | |
| 1010 | return i; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | static ssize_t show_xps_map(struct netdev_queue *queue, |
| 1015 | struct netdev_queue_attribute *attribute, char *buf) |
| 1016 | { |
| 1017 | struct net_device *dev = queue->dev; |
| 1018 | struct xps_dev_maps *dev_maps; |
| 1019 | cpumask_var_t mask; |
| 1020 | unsigned long index; |
| 1021 | size_t len = 0; |
| 1022 | int i; |
| 1023 | |
| 1024 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) |
| 1025 | return -ENOMEM; |
| 1026 | |
| 1027 | index = get_netdev_queue_index(queue); |
| 1028 | |
| 1029 | rcu_read_lock(); |
| 1030 | dev_maps = rcu_dereference(dev->xps_maps); |
| 1031 | if (dev_maps) { |
| 1032 | for_each_possible_cpu(i) { |
| 1033 | struct xps_map *map = |
| 1034 | rcu_dereference(dev_maps->cpu_map[i]); |
| 1035 | if (map) { |
| 1036 | int j; |
| 1037 | for (j = 0; j < map->len; j++) { |
| 1038 | if (map->queues[j] == index) { |
| 1039 | cpumask_set_cpu(i, mask); |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | rcu_read_unlock(); |
| 1047 | |
| 1048 | len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); |
| 1049 | if (PAGE_SIZE - len < 3) { |
| 1050 | free_cpumask_var(mask); |
| 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | |
| 1054 | free_cpumask_var(mask); |
| 1055 | len += sprintf(buf + len, "\n"); |
| 1056 | return len; |
| 1057 | } |
| 1058 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1059 | static ssize_t store_xps_map(struct netdev_queue *queue, |
| 1060 | struct netdev_queue_attribute *attribute, |
| 1061 | const char *buf, size_t len) |
| 1062 | { |
| 1063 | struct net_device *dev = queue->dev; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1064 | unsigned long index; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1065 | cpumask_var_t mask; |
| 1066 | int err; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (!capable(CAP_NET_ADMIN)) |
| 1069 | return -EPERM; |
| 1070 | |
| 1071 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 1072 | return -ENOMEM; |
| 1073 | |
| 1074 | index = get_netdev_queue_index(queue); |
| 1075 | |
| 1076 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); |
| 1077 | if (err) { |
| 1078 | free_cpumask_var(mask); |
| 1079 | return err; |
| 1080 | } |
| 1081 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1082 | err = netif_set_xps_queue(dev, mask, index); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1083 | |
| 1084 | free_cpumask_var(mask); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1085 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1086 | return err ? : len; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static struct netdev_queue_attribute xps_cpus_attribute = |
| 1090 | __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] | 1091 | #endif /* CONFIG_XPS */ |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1092 | |
| 1093 | static struct attribute *netdev_queue_default_attrs[] = { |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1094 | &queue_trans_timeout.attr, |
| 1095 | #ifdef CONFIG_XPS |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1096 | &xps_cpus_attribute.attr, |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1097 | #endif |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1098 | NULL |
| 1099 | }; |
| 1100 | |
| 1101 | static void netdev_queue_release(struct kobject *kobj) |
| 1102 | { |
| 1103 | struct netdev_queue *queue = to_netdev_queue(kobj); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1104 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1105 | memset(kobj, 0, sizeof(*kobj)); |
| 1106 | dev_put(queue->dev); |
| 1107 | } |
| 1108 | |
Weilong Chen | 82ef3d5 | 2014-01-16 17:24:31 +0800 | [diff] [blame] | 1109 | static const void *netdev_queue_namespace(struct kobject *kobj) |
| 1110 | { |
| 1111 | struct netdev_queue *queue = to_netdev_queue(kobj); |
| 1112 | struct device *dev = &queue->dev->dev; |
| 1113 | const void *ns = NULL; |
| 1114 | |
| 1115 | if (dev->class && dev->class->ns_type) |
| 1116 | ns = dev->class->namespace(dev); |
| 1117 | |
| 1118 | return ns; |
| 1119 | } |
| 1120 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1121 | static struct kobj_type netdev_queue_ktype = { |
| 1122 | .sysfs_ops = &netdev_queue_sysfs_ops, |
| 1123 | .release = netdev_queue_release, |
| 1124 | .default_attrs = netdev_queue_default_attrs, |
Weilong Chen | 82ef3d5 | 2014-01-16 17:24:31 +0800 | [diff] [blame] | 1125 | .namespace = netdev_queue_namespace, |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1126 | }; |
| 1127 | |
| 1128 | static int netdev_queue_add_kobject(struct net_device *net, int index) |
| 1129 | { |
| 1130 | struct netdev_queue *queue = net->_tx + index; |
| 1131 | struct kobject *kobj = &queue->kobj; |
| 1132 | int error = 0; |
| 1133 | |
| 1134 | kobj->kset = net->queues_kset; |
| 1135 | error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, |
| 1136 | "tx-%u", index); |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 1137 | if (error) |
| 1138 | goto exit; |
| 1139 | |
| 1140 | #ifdef CONFIG_BQL |
| 1141 | error = sysfs_create_group(kobj, &dql_group); |
| 1142 | if (error) |
| 1143 | goto exit; |
| 1144 | #endif |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1145 | |
| 1146 | kobject_uevent(kobj, KOBJ_ADD); |
| 1147 | dev_hold(queue->dev); |
| 1148 | |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 1149 | return 0; |
| 1150 | exit: |
| 1151 | kobject_put(kobj); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1152 | return error; |
| 1153 | } |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1154 | #endif /* CONFIG_SYSFS */ |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1155 | |
| 1156 | int |
| 1157 | netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num) |
| 1158 | { |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1159 | #ifdef CONFIG_SYSFS |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1160 | int i; |
| 1161 | int error = 0; |
| 1162 | |
| 1163 | for (i = old_num; i < new_num; i++) { |
| 1164 | error = netdev_queue_add_kobject(net, i); |
| 1165 | if (error) { |
| 1166 | new_num = old_num; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 1171 | while (--i >= new_num) { |
| 1172 | struct netdev_queue *queue = net->_tx + i; |
| 1173 | |
| 1174 | #ifdef CONFIG_BQL |
| 1175 | sysfs_remove_group(&queue->kobj, &dql_group); |
| 1176 | #endif |
| 1177 | kobject_put(&queue->kobj); |
| 1178 | } |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1179 | |
| 1180 | return error; |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1181 | #else |
| 1182 | return 0; |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1183 | #endif /* CONFIG_SYSFS */ |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static int register_queue_kobjects(struct net_device *net) |
| 1187 | { |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1188 | 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] | 1189 | |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1190 | #ifdef CONFIG_SYSFS |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 1191 | net->queues_kset = kset_create_and_add("queues", |
| 1192 | NULL, &net->dev.kobj); |
| 1193 | if (!net->queues_kset) |
| 1194 | return -ENOMEM; |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1195 | real_rx = net->real_num_rx_queues; |
| 1196 | #endif |
| 1197 | real_tx = net->real_num_tx_queues; |
| 1198 | |
| 1199 | error = net_rx_queue_update_kobjects(net, 0, real_rx); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1200 | if (error) |
| 1201 | goto error; |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1202 | rxq = real_rx; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1203 | |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1204 | error = netdev_queue_update_kobjects(net, 0, real_tx); |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1205 | if (error) |
| 1206 | goto error; |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1207 | txq = real_tx; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1208 | |
| 1209 | return 0; |
| 1210 | |
| 1211 | error: |
| 1212 | netdev_queue_update_kobjects(net, txq, 0); |
| 1213 | net_rx_queue_update_kobjects(net, rxq, 0); |
| 1214 | return error; |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1217 | static void remove_queue_kobjects(struct net_device *net) |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1218 | { |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1219 | int real_rx = 0, real_tx = 0; |
| 1220 | |
Michael Dalton | a953be5 | 2014-01-16 22:23:28 -0800 | [diff] [blame] | 1221 | #ifdef CONFIG_SYSFS |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1222 | real_rx = net->real_num_rx_queues; |
| 1223 | #endif |
| 1224 | real_tx = net->real_num_tx_queues; |
| 1225 | |
| 1226 | net_rx_queue_update_kobjects(net, real_rx, 0); |
| 1227 | netdev_queue_update_kobjects(net, real_tx, 0); |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 1228 | #ifdef CONFIG_SYSFS |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1229 | kset_unregister(net->queues_kset); |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 1230 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1231 | } |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1232 | |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 1233 | static bool net_current_may_mount(void) |
| 1234 | { |
| 1235 | struct net *net = current->nsproxy->net_ns; |
| 1236 | |
| 1237 | return ns_capable(net->user_ns, CAP_SYS_ADMIN); |
| 1238 | } |
| 1239 | |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1240 | static void *net_grab_current_ns(void) |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1241 | { |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1242 | struct net *ns = current->nsproxy->net_ns; |
| 1243 | #ifdef CONFIG_NET_NS |
| 1244 | if (ns) |
| 1245 | atomic_inc(&ns->passive); |
| 1246 | #endif |
| 1247 | return ns; |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | static const void *net_initial_ns(void) |
| 1251 | { |
| 1252 | return &init_net; |
| 1253 | } |
| 1254 | |
| 1255 | static const void *net_netlink_ns(struct sock *sk) |
| 1256 | { |
| 1257 | return sock_net(sk); |
| 1258 | } |
| 1259 | |
Johannes Berg | 0460079 | 2010-08-05 17:45:15 +0200 | [diff] [blame] | 1260 | struct kobj_ns_type_operations net_ns_type_operations = { |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1261 | .type = KOBJ_NS_TYPE_NET, |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 1262 | .current_may_mount = net_current_may_mount, |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1263 | .grab_current_ns = net_grab_current_ns, |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1264 | .netlink_ns = net_netlink_ns, |
| 1265 | .initial_ns = net_initial_ns, |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1266 | .drop_ns = net_drop_ns, |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1267 | }; |
Johannes Berg | 0460079 | 2010-08-05 17:45:15 +0200 | [diff] [blame] | 1268 | EXPORT_SYMBOL_GPL(net_ns_type_operations); |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1269 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1270 | static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1272 | struct net_device *dev = to_net_dev(d); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1273 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1275 | /* pass interface to uevent. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1276 | retval = add_uevent_var(env, "INTERFACE=%s", dev->name); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1277 | if (retval) |
| 1278 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 1280 | /* pass ifindex to uevent. |
| 1281 | * ifindex is useful as it won't change (interface name may change) |
| 1282 | * and is what RtNetlink uses natively. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1283 | retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 1284 | |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1285 | exit: |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1286 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
| 1289 | /* |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1290 | * netdev_release -- destroy and free a dead device. |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1291 | * Called when last reference to device kobject is gone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | */ |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1293 | static void netdev_release(struct device *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1295 | struct net_device *dev = to_net_dev(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | BUG_ON(dev->reg_state != NETREG_RELEASED); |
| 1298 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1299 | kfree(dev->ifalias); |
Eric Dumazet | 74d332c | 2013-10-30 13:10:44 -0700 | [diff] [blame] | 1300 | netdev_freemem(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1303 | static const void *net_namespace(struct device *d) |
| 1304 | { |
| 1305 | struct net_device *dev; |
| 1306 | dev = container_of(d, struct net_device, dev); |
| 1307 | return dev_net(dev); |
| 1308 | } |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | static struct class net_class = { |
| 1311 | .name = "net", |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1312 | .dev_release = netdev_release, |
Greg Kroah-Hartman | 6be8aee | 2013-07-24 15:05:33 -0700 | [diff] [blame] | 1313 | .dev_groups = net_class_groups, |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1314 | .dev_uevent = netdev_uevent, |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1315 | .ns_type = &net_ns_type_operations, |
| 1316 | .namespace = net_namespace, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | }; |
| 1318 | |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 1319 | /* Delete sysfs entries but hold kobject reference until after all |
| 1320 | * netdev references are gone. |
| 1321 | */ |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 1322 | void netdev_unregister_kobject(struct net_device * net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | { |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 1324 | struct device *dev = &(net->dev); |
| 1325 | |
| 1326 | kobject_get(&dev->kobj); |
Eric W. Biederman | 3891845 | 2008-10-27 17:51:47 -0700 | [diff] [blame] | 1327 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1328 | remove_queue_kobjects(net); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1329 | |
Ming Lei | 9802c8e | 2013-02-22 16:34:16 -0800 | [diff] [blame] | 1330 | pm_runtime_set_memalloc_noio(dev, false); |
| 1331 | |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 1332 | device_del(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* Create sysfs entries for network device. */ |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 1336 | int netdev_register_kobject(struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1338 | struct device *dev = &(net->dev); |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 1339 | const struct attribute_group **groups = net->sysfs_groups; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1340 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Eric W. Biederman | a1b3f59 | 2010-05-04 17:36:49 -0700 | [diff] [blame] | 1342 | device_initialize(dev); |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1343 | dev->class = &net_class; |
| 1344 | dev->platform_data = net; |
| 1345 | dev->groups = groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
Stephen Hemminger | a220547 | 2009-03-09 13:51:55 +0000 | [diff] [blame] | 1347 | dev_set_name(dev, "%s", net->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 1349 | #ifdef CONFIG_SYSFS |
Eric W. Biederman | 0c509a6 | 2009-10-29 14:18:21 +0000 | [diff] [blame] | 1350 | /* Allow for a device specific group */ |
| 1351 | if (*groups) |
| 1352 | groups++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
Eric W. Biederman | 0c509a6 | 2009-10-29 14:18:21 +0000 | [diff] [blame] | 1354 | *groups++ = &netstat_group; |
Johannes Berg | 38c1a01 | 2012-11-16 20:46:19 +0100 | [diff] [blame] | 1355 | |
| 1356 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) |
| 1357 | if (net->ieee80211_ptr) |
| 1358 | *groups++ = &wireless_group; |
| 1359 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) |
| 1360 | else if (net->wireless_handlers) |
| 1361 | *groups++ = &wireless_group; |
| 1362 | #endif |
| 1363 | #endif |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 1364 | #endif /* CONFIG_SYSFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1366 | error = device_add(dev); |
| 1367 | if (error) |
| 1368 | return error; |
| 1369 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 1370 | error = register_queue_kobjects(net); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1371 | if (error) { |
| 1372 | device_del(dev); |
| 1373 | return error; |
| 1374 | } |
| 1375 | |
Ming Lei | 9802c8e | 2013-02-22 16:34:16 -0800 | [diff] [blame] | 1376 | pm_runtime_set_memalloc_noio(dev, true); |
| 1377 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 1378 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1381 | int netdev_class_create_file_ns(struct class_attribute *class_attr, |
| 1382 | const void *ns) |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1383 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1384 | return class_create_file_ns(&net_class, class_attr, ns); |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1385 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1386 | EXPORT_SYMBOL(netdev_class_create_file_ns); |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1387 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1388 | void netdev_class_remove_file_ns(struct class_attribute *class_attr, |
| 1389 | const void *ns) |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1390 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1391 | class_remove_file_ns(&net_class, class_attr, ns); |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1392 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 1393 | EXPORT_SYMBOL(netdev_class_remove_file_ns); |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 1394 | |
Daniel Borkmann | a48d4bb | 2014-01-06 01:20:11 +0100 | [diff] [blame] | 1395 | int __init netdev_kobject_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | { |
Eric W. Biederman | 608b4b9 | 2010-05-04 17:36:45 -0700 | [diff] [blame] | 1397 | kobj_ns_type_register(&net_ns_type_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return class_register(&net_class); |
| 1399 | } |