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