blob: 2b3f76fe65f49f8018c63aa44b345262fb40cde7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net-sysfs.c - network device class and attributes
3 *
4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/netdevice.h>
Jiri Pirkoaecbe012014-11-28 14:34:19 +010015#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070018#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/sock.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070020#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/rtnetlink.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070022#include <linux/vmalloc.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040023#include <linux/export.h>
Tom Herbert114cf582011-11-28 16:33:09 +000024#include <linux/jiffies.h>
Ming Lei9802c8e2013-02-22 16:34:16 -080025#include <linux/pm_runtime.h>
Florian Fainelliaa836df2015-03-09 14:31:20 -070026#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Pavel Emelyanov342709e2007-10-23 21:14:45 -070028#include "net-sysfs.h"
29
Eric W. Biederman8b41d182007-09-26 22:02:53 -070030#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static const char fmt_hex[] = "%#x\n";
32static const char fmt_dec[] = "%d\n";
33static const char fmt_ulong[] = "%lu\n";
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +000034static const char fmt_u64[] = "%llu\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090036static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070038 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070042static ssize_t netdev_show(const struct device *dev,
43 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 ssize_t (*format)(const struct net_device *, char *))
45{
WANG Cong6b53daf2014-07-23 16:09:10 -070046 struct net_device *ndev = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t ret = -EINVAL;
48
49 read_lock(&dev_base_lock);
WANG Cong6b53daf2014-07-23 16:09:10 -070050 if (dev_isalive(ndev))
51 ret = (*format)(ndev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 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) \
WANG Cong6b53daf2014-07-23 16:09:10 -070059static ssize_t format_##field(const struct net_device *dev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{ \
WANG Cong6b53daf2014-07-23 16:09:10 -070061 return sprintf(buf, format_string, dev->field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070063static ssize_t field##_show(struct device *dev, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070064 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070066 return netdev_show(dev, attr, buf, format_##field); \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070067} \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070069#define NETDEVICE_SHOW_RO(field, format_string) \
70NETDEVICE_SHOW(field, format_string); \
71static DEVICE_ATTR_RO(field)
72
73#define NETDEVICE_SHOW_RW(field, format_string) \
74NETDEVICE_SHOW(field, format_string); \
75static DEVICE_ATTR_RW(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070078static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 const char *buf, size_t len,
80 int (*set)(struct net_device *, unsigned long))
81{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000082 struct net_device *netdev = to_net_dev(dev);
83 struct net *net = dev_net(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned long new;
85 int ret = -EINVAL;
86
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000087 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return -EPERM;
89
Shuah Khane1e420c2012-04-12 09:28:13 +000090 ret = kstrtoul(buf, 0, &new);
91 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 goto err;
93
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000094 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000095 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000096
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000097 if (dev_isalive(netdev)) {
98 if ((ret = (*set)(netdev, new)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ret = len;
100 }
101 rtnl_unlock();
102 err:
103 return ret;
104}
105
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700106NETDEVICE_SHOW_RO(dev_id, fmt_hex);
Amir Vadai3f859442014-02-25 18:17:50 +0200107NETDEVICE_SHOW_RO(dev_port, fmt_dec);
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700108NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
109NETDEVICE_SHOW_RO(addr_len, fmt_dec);
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700110NETDEVICE_SHOW_RO(ifindex, fmt_dec);
111NETDEVICE_SHOW_RO(type, fmt_dec);
112NETDEVICE_SHOW_RO(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200114static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
115 char *buf)
116{
117 struct net_device *ndev = to_net_dev(dev);
118
119 return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
120}
121static DEVICE_ATTR_RO(iflink);
122
WANG Cong6b53daf2014-07-23 16:09:10 -0700123static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
Tom Gundersen685343f2014-07-14 16:37:22 +0200124{
WANG Cong6b53daf2014-07-23 16:09:10 -0700125 return sprintf(buf, fmt_dec, dev->name_assign_type);
Tom Gundersen685343f2014-07-14 16:37:22 +0200126}
127
128static ssize_t name_assign_type_show(struct device *dev,
129 struct device_attribute *attr,
130 char *buf)
131{
WANG Cong6b53daf2014-07-23 16:09:10 -0700132 struct net_device *ndev = to_net_dev(dev);
Tom Gundersen685343f2014-07-14 16:37:22 +0200133 ssize_t ret = -EINVAL;
134
WANG Cong6b53daf2014-07-23 16:09:10 -0700135 if (ndev->name_assign_type != NET_NAME_UNKNOWN)
Tom Gundersen685343f2014-07-14 16:37:22 +0200136 ret = netdev_show(dev, attr, buf, format_name_assign_type);
137
138 return ret;
139}
140static DEVICE_ATTR_RO(name_assign_type);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700143static ssize_t address_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700144 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
WANG Cong6b53daf2014-07-23 16:09:10 -0700146 struct net_device *ndev = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ssize_t ret = -EINVAL;
148
149 read_lock(&dev_base_lock);
WANG Cong6b53daf2014-07-23 16:09:10 -0700150 if (dev_isalive(ndev))
151 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 read_unlock(&dev_base_lock);
153 return ret;
154}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700155static DEVICE_ATTR_RO(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700157static ssize_t broadcast_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
WANG Cong6b53daf2014-07-23 16:09:10 -0700160 struct net_device *ndev = to_net_dev(dev);
161 if (dev_isalive(ndev))
162 return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return -EINVAL;
164}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700165static DEVICE_ATTR_RO(broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
WANG Cong6b53daf2014-07-23 16:09:10 -0700167static int change_carrier(struct net_device *dev, unsigned long new_carrier)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000168{
WANG Cong6b53daf2014-07-23 16:09:10 -0700169 if (!netif_running(dev))
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000170 return -EINVAL;
WANG Cong6b53daf2014-07-23 16:09:10 -0700171 return dev_change_carrier(dev, (bool) new_carrier);
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000172}
173
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700174static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t len)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000176{
177 return netdev_store(dev, attr, buf, len, change_carrier);
178}
179
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700180static ssize_t carrier_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700181 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct net_device *netdev = to_net_dev(dev);
184 if (netif_running(netdev)) {
185 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
186 }
187 return -EINVAL;
188}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700189static DEVICE_ATTR_RW(carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700191static ssize_t speed_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000192 struct device_attribute *attr, char *buf)
193{
194 struct net_device *netdev = to_net_dev(dev);
195 int ret = -EINVAL;
196
197 if (!rtnl_trylock())
198 return restart_syscall();
199
David Decotigny8ae6dac2011-04-27 18:32:38 +0000200 if (netif_running(netdev)) {
David Decotigny7cad1ba2016-02-24 10:58:10 -0800201 struct ethtool_link_ksettings cmd;
202
203 if (!__ethtool_get_link_ksettings(netdev, &cmd))
204 ret = sprintf(buf, fmt_dec, cmd.base.speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000205 }
206 rtnl_unlock();
207 return ret;
208}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700209static DEVICE_ATTR_RO(speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000210
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700211static ssize_t duplex_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000212 struct device_attribute *attr, char *buf)
213{
214 struct net_device *netdev = to_net_dev(dev);
215 int ret = -EINVAL;
216
217 if (!rtnl_trylock())
218 return restart_syscall();
219
David Decotigny8ae6dac2011-04-27 18:32:38 +0000220 if (netif_running(netdev)) {
David Decotigny7cad1ba2016-02-24 10:58:10 -0800221 struct ethtool_link_ksettings cmd;
222
223 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000224 const char *duplex;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800225
226 switch (cmd.base.duplex) {
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000227 case DUPLEX_HALF:
228 duplex = "half";
229 break;
230 case DUPLEX_FULL:
231 duplex = "full";
232 break;
233 default:
234 duplex = "unknown";
235 break;
236 }
237 ret = sprintf(buf, "%s\n", duplex);
238 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000239 }
240 rtnl_unlock();
241 return ret;
242}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700243static DEVICE_ATTR_RO(duplex);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000244
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700245static ssize_t dormant_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700246 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800247{
248 struct net_device *netdev = to_net_dev(dev);
249
250 if (netif_running(netdev))
251 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
252
253 return -EINVAL;
254}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700255static DEVICE_ATTR_RO(dormant);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800256
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700257static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800258 "unknown",
259 "notpresent", /* currently unused */
260 "down",
261 "lowerlayerdown",
262 "testing", /* currently unused */
263 "dormant",
264 "up"
265};
266
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700267static ssize_t operstate_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700268 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800269{
270 const struct net_device *netdev = to_net_dev(dev);
271 unsigned char operstate;
272
273 read_lock(&dev_base_lock);
274 operstate = netdev->operstate;
275 if (!netif_running(netdev))
276 operstate = IF_OPER_DOWN;
277 read_unlock(&dev_base_lock);
278
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700279 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800280 return -EINVAL; /* should not happen */
281
282 return sprintf(buf, "%s\n", operstates[operstate]);
283}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700284static DEVICE_ATTR_RO(operstate);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800285
david decotigny2d3b4792014-03-29 09:48:35 -0700286static ssize_t carrier_changes_show(struct device *dev,
287 struct device_attribute *attr,
288 char *buf)
289{
290 struct net_device *netdev = to_net_dev(dev);
291 return sprintf(buf, fmt_dec,
292 atomic_read(&netdev->carrier_changes));
293}
294static DEVICE_ATTR_RO(carrier_changes);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* read-write attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
WANG Cong6b53daf2014-07-23 16:09:10 -0700298static int change_mtu(struct net_device *dev, unsigned long new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
WANG Cong6b53daf2014-07-23 16:09:10 -0700300 return dev_set_mtu(dev, (int) new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700303static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700304 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700306 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700308NETDEVICE_SHOW_RW(mtu, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
WANG Cong6b53daf2014-07-23 16:09:10 -0700310static int change_flags(struct net_device *dev, unsigned long new_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
WANG Cong6b53daf2014-07-23 16:09:10 -0700312 return dev_change_flags(dev, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700315static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700316 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700318 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700320NETDEVICE_SHOW_RW(flags, fmt_hex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
WANG Cong6b53daf2014-07-23 16:09:10 -0700322static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
WANG Cong6b53daf2014-07-23 16:09:10 -0700324 dev->tx_queue_len = new_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700328static ssize_t tx_queue_len_store(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700329 struct device_attribute *attr,
330 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000332 if (!capable(CAP_NET_ADMIN))
333 return -EPERM;
334
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700335 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700337NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Eric Dumazet3b47d302014-11-06 21:09:44 -0800339static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
340{
341 dev->gro_flush_timeout = val;
342 return 0;
343}
344
345static ssize_t gro_flush_timeout_store(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t len)
348{
349 if (!capable(CAP_NET_ADMIN))
350 return -EPERM;
351
352 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
353}
354NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
355
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700356static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700357 const char *buf, size_t len)
358{
359 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000360 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700361 size_t count = len;
362 ssize_t ret;
363
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000364 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700365 return -EPERM;
366
367 /* ignore trailing newline */
368 if (len > 0 && buf[len - 1] == '\n')
369 --count;
370
Eric W. Biederman336ca572009-05-13 16:57:25 +0000371 if (!rtnl_trylock())
372 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700373 ret = dev_set_alias(netdev, buf, count);
374 rtnl_unlock();
375
376 return ret < 0 ? ret : len;
377}
378
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700379static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700380 struct device_attribute *attr, char *buf)
381{
382 const struct net_device *netdev = to_net_dev(dev);
383 ssize_t ret = 0;
384
Eric W. Biederman336ca572009-05-13 16:57:25 +0000385 if (!rtnl_trylock())
386 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700387 if (netdev->ifalias)
388 ret = sprintf(buf, "%s\n", netdev->ifalias);
389 rtnl_unlock();
390 return ret;
391}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700392static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000393
WANG Cong6b53daf2014-07-23 16:09:10 -0700394static int change_group(struct net_device *dev, unsigned long new_group)
Vlad Dogarua512b922011-01-24 03:37:29 +0000395{
WANG Cong6b53daf2014-07-23 16:09:10 -0700396 dev_set_group(dev, (int) new_group);
Vlad Dogarua512b922011-01-24 03:37:29 +0000397 return 0;
398}
399
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700400static ssize_t group_store(struct device *dev, struct device_attribute *attr,
401 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000402{
403 return netdev_store(dev, attr, buf, len, change_group);
404}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700405NETDEVICE_SHOW(group, fmt_dec);
406static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000407
Anuradha Karuppiahd746d702015-07-14 13:43:19 -0700408static int change_proto_down(struct net_device *dev, unsigned long proto_down)
409{
410 return dev_change_proto_down(dev, (bool) proto_down);
411}
412
413static ssize_t proto_down_store(struct device *dev,
414 struct device_attribute *attr,
415 const char *buf, size_t len)
416{
417 return netdev_store(dev, attr, buf, len, change_proto_down);
418}
419NETDEVICE_SHOW_RW(proto_down, fmt_dec);
420
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700421static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200422 struct device_attribute *attr, char *buf)
423{
424 struct net_device *netdev = to_net_dev(dev);
425 ssize_t ret = -EINVAL;
426
427 if (!rtnl_trylock())
428 return restart_syscall();
429
430 if (dev_isalive(netdev)) {
Jiri Pirko02637fc2014-11-28 14:34:16 +0100431 struct netdev_phys_item_id ppid;
Jiri Pirkoff80e512013-07-29 18:16:51 +0200432
433 ret = dev_get_phys_port_id(netdev, &ppid);
434 if (!ret)
435 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
436 }
437 rtnl_unlock();
438
439 return ret;
440}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700441static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200442
David Aherndb24a902015-03-17 20:23:15 -0600443static ssize_t phys_port_name_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
446 struct net_device *netdev = to_net_dev(dev);
447 ssize_t ret = -EINVAL;
448
449 if (!rtnl_trylock())
450 return restart_syscall();
451
452 if (dev_isalive(netdev)) {
453 char name[IFNAMSIZ];
454
455 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
456 if (!ret)
457 ret = sprintf(buf, "%s\n", name);
458 }
459 rtnl_unlock();
460
461 return ret;
462}
463static DEVICE_ATTR_RO(phys_port_name);
464
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100465static ssize_t phys_switch_id_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
468 struct net_device *netdev = to_net_dev(dev);
469 ssize_t ret = -EINVAL;
470
471 if (!rtnl_trylock())
472 return restart_syscall();
473
474 if (dev_isalive(netdev)) {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700475 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100476 .orig_dev = netdev,
Jiri Pirko1f868392015-10-01 11:03:42 +0200477 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700478 .flags = SWITCHDEV_F_NO_RECURSE,
479 };
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100480
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700481 ret = switchdev_port_attr_get(netdev, &attr);
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100482 if (!ret)
Scott Feldman42275bd2015-05-13 11:16:50 -0700483 ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
484 attr.u.ppid.id);
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100485 }
486 rtnl_unlock();
487
488 return ret;
489}
490static DEVICE_ATTR_RO(phys_switch_id);
491
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700492static struct attribute *net_class_attrs[] = {
493 &dev_attr_netdev_group.attr,
494 &dev_attr_type.attr,
495 &dev_attr_dev_id.attr,
Amir Vadai3f859442014-02-25 18:17:50 +0200496 &dev_attr_dev_port.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700497 &dev_attr_iflink.attr,
498 &dev_attr_ifindex.attr,
Tom Gundersen685343f2014-07-14 16:37:22 +0200499 &dev_attr_name_assign_type.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700500 &dev_attr_addr_assign_type.attr,
501 &dev_attr_addr_len.attr,
502 &dev_attr_link_mode.attr,
503 &dev_attr_address.attr,
504 &dev_attr_broadcast.attr,
505 &dev_attr_speed.attr,
506 &dev_attr_duplex.attr,
507 &dev_attr_dormant.attr,
508 &dev_attr_operstate.attr,
david decotigny2d3b4792014-03-29 09:48:35 -0700509 &dev_attr_carrier_changes.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700510 &dev_attr_ifalias.attr,
511 &dev_attr_carrier.attr,
512 &dev_attr_mtu.attr,
513 &dev_attr_flags.attr,
514 &dev_attr_tx_queue_len.attr,
Eric Dumazet3b47d302014-11-06 21:09:44 -0800515 &dev_attr_gro_flush_timeout.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700516 &dev_attr_phys_port_id.attr,
David Aherndb24a902015-03-17 20:23:15 -0600517 &dev_attr_phys_port_name.attr,
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100518 &dev_attr_phys_switch_id.attr,
Anuradha Karuppiahd746d702015-07-14 13:43:19 -0700519 &dev_attr_proto_down.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700520 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700522ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700525static ssize_t netstat_show(const struct device *d,
526 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned long offset)
528{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700529 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ssize_t ret = -EINVAL;
531
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000532 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
533 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700536 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700537 struct rtnl_link_stats64 temp;
538 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
539
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000540 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 read_unlock(&dev_base_lock);
543 return ret;
544}
545
546/* generate a read-only statistics attribute */
547#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700548static ssize_t name##_show(struct device *d, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700549 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700551 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000552 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700554static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556NETSTAT_ENTRY(rx_packets);
557NETSTAT_ENTRY(tx_packets);
558NETSTAT_ENTRY(rx_bytes);
559NETSTAT_ENTRY(tx_bytes);
560NETSTAT_ENTRY(rx_errors);
561NETSTAT_ENTRY(tx_errors);
562NETSTAT_ENTRY(rx_dropped);
563NETSTAT_ENTRY(tx_dropped);
564NETSTAT_ENTRY(multicast);
565NETSTAT_ENTRY(collisions);
566NETSTAT_ENTRY(rx_length_errors);
567NETSTAT_ENTRY(rx_over_errors);
568NETSTAT_ENTRY(rx_crc_errors);
569NETSTAT_ENTRY(rx_frame_errors);
570NETSTAT_ENTRY(rx_fifo_errors);
571NETSTAT_ENTRY(rx_missed_errors);
572NETSTAT_ENTRY(tx_aborted_errors);
573NETSTAT_ENTRY(tx_carrier_errors);
574NETSTAT_ENTRY(tx_fifo_errors);
575NETSTAT_ENTRY(tx_heartbeat_errors);
576NETSTAT_ENTRY(tx_window_errors);
577NETSTAT_ENTRY(rx_compressed);
578NETSTAT_ENTRY(tx_compressed);
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500579NETSTAT_ENTRY(rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700582 &dev_attr_rx_packets.attr,
583 &dev_attr_tx_packets.attr,
584 &dev_attr_rx_bytes.attr,
585 &dev_attr_tx_bytes.attr,
586 &dev_attr_rx_errors.attr,
587 &dev_attr_tx_errors.attr,
588 &dev_attr_rx_dropped.attr,
589 &dev_attr_tx_dropped.attr,
590 &dev_attr_multicast.attr,
591 &dev_attr_collisions.attr,
592 &dev_attr_rx_length_errors.attr,
593 &dev_attr_rx_over_errors.attr,
594 &dev_attr_rx_crc_errors.attr,
595 &dev_attr_rx_frame_errors.attr,
596 &dev_attr_rx_fifo_errors.attr,
597 &dev_attr_rx_missed_errors.attr,
598 &dev_attr_tx_aborted_errors.attr,
599 &dev_attr_tx_carrier_errors.attr,
600 &dev_attr_tx_fifo_errors.attr,
601 &dev_attr_tx_heartbeat_errors.attr,
602 &dev_attr_tx_window_errors.attr,
603 &dev_attr_rx_compressed.attr,
604 &dev_attr_tx_compressed.attr,
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500605 &dev_attr_rx_nohandler.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 NULL
607};
608
609
610static struct attribute_group netstat_group = {
611 .name = "statistics",
612 .attrs = netstat_attrs,
613};
Johannes Berg38c1a012012-11-16 20:46:19 +0100614
615#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
616static struct attribute *wireless_attrs[] = {
617 NULL
618};
619
620static struct attribute_group wireless_group = {
621 .name = "wireless",
622 .attrs = wireless_attrs,
623};
624#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700625
626#else /* CONFIG_SYSFS */
627#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700628#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Michael Daltona953be52014-01-16 22:23:28 -0800630#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000631#define to_rx_queue_attr(_attr) container_of(_attr, \
632 struct rx_queue_attribute, attr)
633
634#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
635
636static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
637 char *buf)
638{
639 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
640 struct netdev_rx_queue *queue = to_rx_queue(kobj);
641
642 if (!attribute->show)
643 return -EIO;
644
645 return attribute->show(queue, attribute, buf);
646}
647
648static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
649 const char *buf, size_t count)
650{
651 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
652 struct netdev_rx_queue *queue = to_rx_queue(kobj);
653
654 if (!attribute->store)
655 return -EIO;
656
657 return attribute->store(queue, attribute, buf, count);
658}
659
stephen hemmingerfa50d642010-08-31 12:14:13 +0000660static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000661 .show = rx_queue_attr_show,
662 .store = rx_queue_attr_store,
663};
664
Michael Daltona953be52014-01-16 22:23:28 -0800665#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000666static ssize_t show_rps_map(struct netdev_rx_queue *queue,
667 struct rx_queue_attribute *attribute, char *buf)
668{
669 struct rps_map *map;
670 cpumask_var_t mask;
Tejun Heof0906822015-02-13 14:37:42 -0800671 int i, len;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000672
673 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
674 return -ENOMEM;
675
676 rcu_read_lock();
677 map = rcu_dereference(queue->rps_map);
678 if (map)
679 for (i = 0; i < map->len; i++)
680 cpumask_set_cpu(map->cpus[i], mask);
681
Tejun Heof0906822015-02-13 14:37:42 -0800682 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000683 rcu_read_unlock();
Tom Herbert0a9627f2010-03-16 08:03:29 +0000684 free_cpumask_var(mask);
Tejun Heof0906822015-02-13 14:37:42 -0800685
686 return len < PAGE_SIZE ? len : -EINVAL;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000687}
688
Eric Dumazetf5acb902010-04-19 14:40:57 -0700689static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000690 struct rx_queue_attribute *attribute,
691 const char *buf, size_t len)
692{
693 struct rps_map *old_map, *map;
694 cpumask_var_t mask;
695 int err, cpu, i;
Sasha Levinda65ad12015-08-13 14:03:16 -0400696 static DEFINE_MUTEX(rps_map_mutex);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000697
698 if (!capable(CAP_NET_ADMIN))
699 return -EPERM;
700
701 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
702 return -ENOMEM;
703
704 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
705 if (err) {
706 free_cpumask_var(mask);
707 return err;
708 }
709
Eric Dumazet95c96172012-04-15 05:58:06 +0000710 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000711 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
712 GFP_KERNEL);
713 if (!map) {
714 free_cpumask_var(mask);
715 return -ENOMEM;
716 }
717
718 i = 0;
719 for_each_cpu_and(cpu, mask, cpu_online_mask)
720 map->cpus[i++] = cpu;
721
722 if (i)
723 map->len = i;
724 else {
725 kfree(map);
726 map = NULL;
727 }
728
Sasha Levinda65ad12015-08-13 14:03:16 -0400729 mutex_lock(&rps_map_mutex);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000730 old_map = rcu_dereference_protected(queue->rps_map,
Sasha Levinda65ad12015-08-13 14:03:16 -0400731 mutex_is_locked(&rps_map_mutex));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000732 rcu_assign_pointer(queue->rps_map, map);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000733
Eric Dumazetadc93002011-11-17 03:13:26 +0000734 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100735 static_key_slow_inc(&rps_needed);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700736 if (old_map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100737 static_key_slow_dec(&rps_needed);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700738
Sasha Levinda65ad12015-08-13 14:03:16 -0400739 mutex_unlock(&rps_map_mutex);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700740
741 if (old_map)
742 kfree_rcu(old_map, rcu);
743
Tom Herbert0a9627f2010-03-16 08:03:29 +0000744 free_cpumask_var(mask);
745 return len;
746}
747
Tom Herbertfec5e652010-04-16 16:01:27 -0700748static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
749 struct rx_queue_attribute *attr,
750 char *buf)
751{
752 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000753 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700754
755 rcu_read_lock();
756 flow_table = rcu_dereference(queue->rps_flow_table);
757 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000758 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700759 rcu_read_unlock();
760
Eric Dumazet60b778c2011-12-24 06:56:49 +0000761 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700762}
763
Tom Herbertfec5e652010-04-16 16:01:27 -0700764static void rps_dev_flow_table_release(struct rcu_head *rcu)
765{
766 struct rps_dev_flow_table *table = container_of(rcu,
767 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000768 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700769}
770
Eric Dumazetf5acb902010-04-19 14:40:57 -0700771static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700772 struct rx_queue_attribute *attr,
773 const char *buf, size_t len)
774{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000775 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700776 struct rps_dev_flow_table *table, *old_table;
777 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000778 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700779
780 if (!capable(CAP_NET_ADMIN))
781 return -EPERM;
782
Eric Dumazet60b778c2011-12-24 06:56:49 +0000783 rc = kstrtoul(buf, 0, &count);
784 if (rc < 0)
785 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700786
787 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000788 mask = count - 1;
789 /* mask = roundup_pow_of_two(count) - 1;
790 * without overflows...
791 */
792 while ((mask | (mask >> 1)) != mask)
793 mask |= (mask >> 1);
794 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800795 * and on 32bit arches, must check
796 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000797 */
798#if BITS_PER_LONG > 32
799 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000800 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000801#else
802 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000803 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700804 /* Enforce a limit to prevent overflow */
805 return -EINVAL;
806 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000807#endif
808 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700809 if (!table)
810 return -ENOMEM;
811
Eric Dumazet60b778c2011-12-24 06:56:49 +0000812 table->mask = mask;
813 for (count = 0; count <= mask; count++)
814 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700815 } else
816 table = NULL;
817
818 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000819 old_table = rcu_dereference_protected(queue->rps_flow_table,
820 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700821 rcu_assign_pointer(queue->rps_flow_table, table);
822 spin_unlock(&rps_dev_flow_lock);
823
824 if (old_table)
825 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
826
827 return len;
828}
829
Tom Herbert0a9627f2010-03-16 08:03:29 +0000830static struct rx_queue_attribute rps_cpus_attribute =
831 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
832
Tom Herbertfec5e652010-04-16 16:01:27 -0700833
834static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
835 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
836 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800837#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700838
Tom Herbert0a9627f2010-03-16 08:03:29 +0000839static struct attribute *rx_queue_default_attrs[] = {
Michael Daltona953be52014-01-16 22:23:28 -0800840#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000841 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700842 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800843#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000844 NULL
845};
846
847static void rx_queue_release(struct kobject *kobj)
848{
849 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800850#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000851 struct rps_map *map;
852 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000853
Tom Herbertfec5e652010-04-16 16:01:27 -0700854
Eric Dumazet33d480c2011-08-11 19:30:52 +0000855 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000856 if (map) {
857 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800858 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000859 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000860
Eric Dumazet33d480c2011-08-11 19:30:52 +0000861 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000862 if (flow_table) {
863 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000864 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000865 }
Michael Daltona953be52014-01-16 22:23:28 -0800866#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000867
John Fastabend9ea19482010-11-16 06:31:39 +0000868 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000869 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000870}
871
Weilong Chen82ef3d52014-01-16 17:24:31 +0800872static const void *rx_queue_namespace(struct kobject *kobj)
873{
874 struct netdev_rx_queue *queue = to_rx_queue(kobj);
875 struct device *dev = &queue->dev->dev;
876 const void *ns = NULL;
877
878 if (dev->class && dev->class->ns_type)
879 ns = dev->class->namespace(dev);
880
881 return ns;
882}
883
Tom Herbert0a9627f2010-03-16 08:03:29 +0000884static struct kobj_type rx_queue_ktype = {
885 .sysfs_ops = &rx_queue_sysfs_ops,
886 .release = rx_queue_release,
887 .default_attrs = rx_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +0800888 .namespace = rx_queue_namespace
Tom Herbert0a9627f2010-03-16 08:03:29 +0000889};
890
WANG Cong6b53daf2014-07-23 16:09:10 -0700891static int rx_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000892{
WANG Cong6b53daf2014-07-23 16:09:10 -0700893 struct netdev_rx_queue *queue = dev->_rx + index;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000894 struct kobject *kobj = &queue->kobj;
895 int error = 0;
896
WANG Cong6b53daf2014-07-23 16:09:10 -0700897 kobj->kset = dev->queues_kset;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000898 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
899 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800900 if (error)
901 goto exit;
902
WANG Cong6b53daf2014-07-23 16:09:10 -0700903 if (dev->sysfs_rx_queue_group) {
904 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
Michael Daltona953be52014-01-16 22:23:28 -0800905 if (error)
906 goto exit;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000907 }
908
909 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000910 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000911
912 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800913exit:
914 kobject_put(kobj);
915 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000916}
Paul Bolle80dd6ea2014-02-09 14:07:11 +0100917#endif /* CONFIG_SYSFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000918
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000919int
WANG Cong6b53daf2014-07-23 16:09:10 -0700920net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000921{
Michael Daltona953be52014-01-16 22:23:28 -0800922#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000923 int i;
924 int error = 0;
925
Michael Daltona953be52014-01-16 22:23:28 -0800926#ifndef CONFIG_RPS
WANG Cong6b53daf2014-07-23 16:09:10 -0700927 if (!dev->sysfs_rx_queue_group)
Michael Daltona953be52014-01-16 22:23:28 -0800928 return 0;
929#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000930 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700931 error = rx_queue_add_kobject(dev, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000932 if (error) {
933 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000934 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000935 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000936 }
937
Michael Daltona953be52014-01-16 22:23:28 -0800938 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700939 if (dev->sysfs_rx_queue_group)
940 sysfs_remove_group(&dev->_rx[i].kobj,
941 dev->sysfs_rx_queue_group);
942 kobject_put(&dev->_rx[i].kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800943 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000944
945 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000946#else
947 return 0;
948#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000949}
950
david decotignyccf5ff62011-11-16 12:15:10 +0000951#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000952/*
953 * netdev_queue sysfs structures and functions.
954 */
955struct netdev_queue_attribute {
956 struct attribute attr;
957 ssize_t (*show)(struct netdev_queue *queue,
958 struct netdev_queue_attribute *attr, char *buf);
959 ssize_t (*store)(struct netdev_queue *queue,
960 struct netdev_queue_attribute *attr, const char *buf, size_t len);
961};
962#define to_netdev_queue_attr(_attr) container_of(_attr, \
963 struct netdev_queue_attribute, attr)
964
965#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
966
967static ssize_t netdev_queue_attr_show(struct kobject *kobj,
968 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000969{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000970 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
971 struct netdev_queue *queue = to_netdev_queue(kobj);
972
973 if (!attribute->show)
974 return -EIO;
975
976 return attribute->show(queue, attribute, buf);
977}
978
979static ssize_t netdev_queue_attr_store(struct kobject *kobj,
980 struct attribute *attr,
981 const char *buf, size_t count)
982{
983 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
984 struct netdev_queue *queue = to_netdev_queue(kobj);
985
986 if (!attribute->store)
987 return -EIO;
988
989 return attribute->store(queue, attribute, buf, count);
990}
991
992static const struct sysfs_ops netdev_queue_sysfs_ops = {
993 .show = netdev_queue_attr_show,
994 .store = netdev_queue_attr_store,
995};
996
david decotignyccf5ff62011-11-16 12:15:10 +0000997static ssize_t show_trans_timeout(struct netdev_queue *queue,
998 struct netdev_queue_attribute *attribute,
999 char *buf)
1000{
1001 unsigned long trans_timeout;
1002
1003 spin_lock_irq(&queue->_xmit_lock);
1004 trans_timeout = queue->trans_timeout;
1005 spin_unlock_irq(&queue->_xmit_lock);
1006
1007 return sprintf(buf, "%lu", trans_timeout);
1008}
1009
John Fastabend822b3b22015-03-18 14:57:33 +02001010#ifdef CONFIG_XPS
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001011static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
John Fastabend822b3b22015-03-18 14:57:33 +02001012{
1013 struct net_device *dev = queue->dev;
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001014 unsigned int i;
John Fastabend822b3b22015-03-18 14:57:33 +02001015
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001016 i = queue - dev->_tx;
John Fastabend822b3b22015-03-18 14:57:33 +02001017 BUG_ON(i >= dev->num_tx_queues);
1018
1019 return i;
1020}
1021
1022static ssize_t show_tx_maxrate(struct netdev_queue *queue,
1023 struct netdev_queue_attribute *attribute,
1024 char *buf)
1025{
1026 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1027}
1028
1029static ssize_t set_tx_maxrate(struct netdev_queue *queue,
1030 struct netdev_queue_attribute *attribute,
1031 const char *buf, size_t len)
1032{
1033 struct net_device *dev = queue->dev;
1034 int err, index = get_netdev_queue_index(queue);
1035 u32 rate = 0;
1036
1037 err = kstrtou32(buf, 10, &rate);
1038 if (err < 0)
1039 return err;
1040
1041 if (!rtnl_trylock())
1042 return restart_syscall();
1043
1044 err = -EOPNOTSUPP;
1045 if (dev->netdev_ops->ndo_set_tx_maxrate)
1046 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1047
1048 rtnl_unlock();
1049 if (!err) {
1050 queue->tx_maxrate = rate;
1051 return len;
1052 }
1053 return err;
1054}
1055
1056static struct netdev_queue_attribute queue_tx_maxrate =
1057 __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
1058 show_tx_maxrate, set_tx_maxrate);
1059#endif
1060
david decotignyccf5ff62011-11-16 12:15:10 +00001061static struct netdev_queue_attribute queue_trans_timeout =
1062 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
1063
Tom Herbert114cf582011-11-28 16:33:09 +00001064#ifdef CONFIG_BQL
1065/*
1066 * Byte queue limits sysfs structures and functions.
1067 */
1068static ssize_t bql_show(char *buf, unsigned int value)
1069{
1070 return sprintf(buf, "%u\n", value);
1071}
1072
1073static ssize_t bql_set(const char *buf, const size_t count,
1074 unsigned int *pvalue)
1075{
1076 unsigned int value;
1077 int err;
1078
1079 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
1080 value = DQL_MAX_LIMIT;
1081 else {
1082 err = kstrtouint(buf, 10, &value);
1083 if (err < 0)
1084 return err;
1085 if (value > DQL_MAX_LIMIT)
1086 return -EINVAL;
1087 }
1088
1089 *pvalue = value;
1090
1091 return count;
1092}
1093
1094static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1095 struct netdev_queue_attribute *attr,
1096 char *buf)
1097{
1098 struct dql *dql = &queue->dql;
1099
1100 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1101}
1102
1103static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1104 struct netdev_queue_attribute *attribute,
1105 const char *buf, size_t len)
1106{
1107 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +00001108 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +00001109 int err;
1110
1111 err = kstrtouint(buf, 10, &value);
1112 if (err < 0)
1113 return err;
1114
1115 dql->slack_hold_time = msecs_to_jiffies(value);
1116
1117 return len;
1118}
1119
1120static struct netdev_queue_attribute bql_hold_time_attribute =
1121 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
1122 bql_set_hold_time);
1123
1124static ssize_t bql_show_inflight(struct netdev_queue *queue,
1125 struct netdev_queue_attribute *attr,
1126 char *buf)
1127{
1128 struct dql *dql = &queue->dql;
1129
1130 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1131}
1132
1133static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +00001134 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +00001135
1136#define BQL_ATTR(NAME, FIELD) \
1137static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1138 struct netdev_queue_attribute *attr, \
1139 char *buf) \
1140{ \
1141 return bql_show(buf, queue->dql.FIELD); \
1142} \
1143 \
1144static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1145 struct netdev_queue_attribute *attr, \
1146 const char *buf, size_t len) \
1147{ \
1148 return bql_set(buf, len, &queue->dql.FIELD); \
1149} \
1150 \
1151static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
1152 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
1153 bql_set_ ## NAME);
1154
1155BQL_ATTR(limit, limit)
1156BQL_ATTR(limit_max, max_limit)
1157BQL_ATTR(limit_min, min_limit)
1158
1159static struct attribute *dql_attrs[] = {
1160 &bql_limit_attribute.attr,
1161 &bql_limit_max_attribute.attr,
1162 &bql_limit_min_attribute.attr,
1163 &bql_hold_time_attribute.attr,
1164 &bql_inflight_attribute.attr,
1165 NULL
1166};
1167
1168static struct attribute_group dql_group = {
1169 .name = "byte_queue_limits",
1170 .attrs = dql_attrs,
1171};
1172#endif /* CONFIG_BQL */
1173
david decotignyccf5ff62011-11-16 12:15:10 +00001174#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001175static ssize_t show_xps_map(struct netdev_queue *queue,
1176 struct netdev_queue_attribute *attribute, char *buf)
1177{
1178 struct net_device *dev = queue->dev;
1179 struct xps_dev_maps *dev_maps;
1180 cpumask_var_t mask;
1181 unsigned long index;
Tejun Heof0906822015-02-13 14:37:42 -08001182 int i, len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001183
1184 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1185 return -ENOMEM;
1186
1187 index = get_netdev_queue_index(queue);
1188
1189 rcu_read_lock();
1190 dev_maps = rcu_dereference(dev->xps_maps);
1191 if (dev_maps) {
1192 for_each_possible_cpu(i) {
1193 struct xps_map *map =
1194 rcu_dereference(dev_maps->cpu_map[i]);
1195 if (map) {
1196 int j;
1197 for (j = 0; j < map->len; j++) {
1198 if (map->queues[j] == index) {
1199 cpumask_set_cpu(i, mask);
1200 break;
1201 }
1202 }
1203 }
1204 }
1205 }
1206 rcu_read_unlock();
1207
Tejun Heof0906822015-02-13 14:37:42 -08001208 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
Tom Herbert1d24eb42010-11-21 13:17:27 +00001209 free_cpumask_var(mask);
Tejun Heof0906822015-02-13 14:37:42 -08001210 return len < PAGE_SIZE ? len : -EINVAL;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001211}
1212
Tom Herbert1d24eb42010-11-21 13:17:27 +00001213static ssize_t store_xps_map(struct netdev_queue *queue,
1214 struct netdev_queue_attribute *attribute,
1215 const char *buf, size_t len)
1216{
1217 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001218 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001219 cpumask_var_t mask;
1220 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001221
1222 if (!capable(CAP_NET_ADMIN))
1223 return -EPERM;
1224
1225 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1226 return -ENOMEM;
1227
1228 index = get_netdev_queue_index(queue);
1229
1230 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1231 if (err) {
1232 free_cpumask_var(mask);
1233 return err;
1234 }
1235
Alexander Duyck537c00d2013-01-10 08:57:02 +00001236 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001237
1238 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001239
Alexander Duyck537c00d2013-01-10 08:57:02 +00001240 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001241}
1242
1243static struct netdev_queue_attribute xps_cpus_attribute =
1244 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001245#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001246
1247static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001248 &queue_trans_timeout.attr,
1249#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001250 &xps_cpus_attribute.attr,
John Fastabend822b3b22015-03-18 14:57:33 +02001251 &queue_tx_maxrate.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001252#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001253 NULL
1254};
1255
1256static void netdev_queue_release(struct kobject *kobj)
1257{
1258 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001259
Tom Herbert1d24eb42010-11-21 13:17:27 +00001260 memset(kobj, 0, sizeof(*kobj));
1261 dev_put(queue->dev);
1262}
1263
Weilong Chen82ef3d52014-01-16 17:24:31 +08001264static const void *netdev_queue_namespace(struct kobject *kobj)
1265{
1266 struct netdev_queue *queue = to_netdev_queue(kobj);
1267 struct device *dev = &queue->dev->dev;
1268 const void *ns = NULL;
1269
1270 if (dev->class && dev->class->ns_type)
1271 ns = dev->class->namespace(dev);
1272
1273 return ns;
1274}
1275
Tom Herbert1d24eb42010-11-21 13:17:27 +00001276static struct kobj_type netdev_queue_ktype = {
1277 .sysfs_ops = &netdev_queue_sysfs_ops,
1278 .release = netdev_queue_release,
1279 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001280 .namespace = netdev_queue_namespace,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001281};
1282
WANG Cong6b53daf2014-07-23 16:09:10 -07001283static int netdev_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001284{
WANG Cong6b53daf2014-07-23 16:09:10 -07001285 struct netdev_queue *queue = dev->_tx + index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001286 struct kobject *kobj = &queue->kobj;
1287 int error = 0;
1288
WANG Cong6b53daf2014-07-23 16:09:10 -07001289 kobj->kset = dev->queues_kset;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001290 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1291 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001292 if (error)
1293 goto exit;
1294
1295#ifdef CONFIG_BQL
1296 error = sysfs_create_group(kobj, &dql_group);
1297 if (error)
1298 goto exit;
1299#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001300
1301 kobject_uevent(kobj, KOBJ_ADD);
1302 dev_hold(queue->dev);
1303
Tom Herbert114cf582011-11-28 16:33:09 +00001304 return 0;
1305exit:
1306 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001307 return error;
1308}
david decotignyccf5ff62011-11-16 12:15:10 +00001309#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001310
1311int
WANG Cong6b53daf2014-07-23 16:09:10 -07001312netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001313{
david decotignyccf5ff62011-11-16 12:15:10 +00001314#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001315 int i;
1316 int error = 0;
1317
1318 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001319 error = netdev_queue_add_kobject(dev, i);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001320 if (error) {
1321 new_num = old_num;
1322 break;
1323 }
1324 }
1325
Tom Herbert114cf582011-11-28 16:33:09 +00001326 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001327 struct netdev_queue *queue = dev->_tx + i;
Tom Herbert114cf582011-11-28 16:33:09 +00001328
1329#ifdef CONFIG_BQL
1330 sysfs_remove_group(&queue->kobj, &dql_group);
1331#endif
1332 kobject_put(&queue->kobj);
1333 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001334
1335 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001336#else
1337 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001338#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001339}
1340
WANG Cong6b53daf2014-07-23 16:09:10 -07001341static int register_queue_kobjects(struct net_device *dev)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001342{
Tom Herbertbf264142010-11-26 08:36:09 +00001343 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001344
david decotignyccf5ff62011-11-16 12:15:10 +00001345#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001346 dev->queues_kset = kset_create_and_add("queues",
1347 NULL, &dev->dev.kobj);
1348 if (!dev->queues_kset)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001349 return -ENOMEM;
WANG Cong6b53daf2014-07-23 16:09:10 -07001350 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001351#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001352 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001353
WANG Cong6b53daf2014-07-23 16:09:10 -07001354 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001355 if (error)
1356 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001357 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001358
WANG Cong6b53daf2014-07-23 16:09:10 -07001359 error = netdev_queue_update_kobjects(dev, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001360 if (error)
1361 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001362 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001363
1364 return 0;
1365
1366error:
WANG Cong6b53daf2014-07-23 16:09:10 -07001367 netdev_queue_update_kobjects(dev, txq, 0);
1368 net_rx_queue_update_kobjects(dev, rxq, 0);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001369 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001370}
1371
WANG Cong6b53daf2014-07-23 16:09:10 -07001372static void remove_queue_kobjects(struct net_device *dev)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001373{
Tom Herbertbf264142010-11-26 08:36:09 +00001374 int real_rx = 0, real_tx = 0;
1375
Michael Daltona953be52014-01-16 22:23:28 -08001376#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001377 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001378#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001379 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001380
WANG Cong6b53daf2014-07-23 16:09:10 -07001381 net_rx_queue_update_kobjects(dev, real_rx, 0);
1382 netdev_queue_update_kobjects(dev, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001383#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001384 kset_unregister(dev->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001385#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001386}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001387
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001388static bool net_current_may_mount(void)
1389{
1390 struct net *net = current->nsproxy->net_ns;
1391
1392 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1393}
1394
Al Viroa685e082011-06-08 21:13:01 -04001395static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001396{
Al Viroa685e082011-06-08 21:13:01 -04001397 struct net *ns = current->nsproxy->net_ns;
1398#ifdef CONFIG_NET_NS
1399 if (ns)
1400 atomic_inc(&ns->passive);
1401#endif
1402 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001403}
1404
1405static const void *net_initial_ns(void)
1406{
1407 return &init_net;
1408}
1409
1410static const void *net_netlink_ns(struct sock *sk)
1411{
1412 return sock_net(sk);
1413}
1414
Johannes Berg04600792010-08-05 17:45:15 +02001415struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001416 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001417 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001418 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001419 .netlink_ns = net_netlink_ns,
1420 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001421 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001422};
Johannes Berg04600792010-08-05 17:45:15 +02001423EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001424
Kay Sievers7eff2e72007-08-14 15:15:12 +02001425static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001427 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001428 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Kay Sievers312c0042005-11-16 09:00:00 +01001430 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001431 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001432 if (retval)
1433 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001435 /* pass ifindex to uevent.
1436 * ifindex is useful as it won't change (interface name may change)
1437 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001438 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001439
Eric Rannaudbf624562007-03-30 22:23:12 -07001440exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001441 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001445 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001446 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001448static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001450 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 BUG_ON(dev->reg_state != NETREG_RELEASED);
1453
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001454 kfree(dev->ifalias);
Eric Dumazet74d332c2013-10-30 13:10:44 -07001455 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001458static const void *net_namespace(struct device *d)
1459{
Geliang Tang5c294822015-12-22 23:11:49 +08001460 struct net_device *dev = to_net_dev(d);
1461
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001462 return dev_net(dev);
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static struct class net_class = {
1466 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001467 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001468 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001469 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001470 .ns_type = &net_ns_type_operations,
1471 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472};
1473
Florian Fainelliaa836df2015-03-09 14:31:20 -07001474#ifdef CONFIG_OF_NET
1475static int of_dev_node_match(struct device *dev, const void *data)
1476{
1477 int ret = 0;
1478
1479 if (dev->parent)
1480 ret = dev->parent->of_node == data;
1481
1482 return ret == 0 ? dev->of_node == data : ret;
1483}
1484
Russell King9861f722015-09-24 20:36:33 +01001485/*
1486 * of_find_net_device_by_node - lookup the net device for the device node
1487 * @np: OF device node
1488 *
1489 * Looks up the net_device structure corresponding with the device node.
1490 * If successful, returns a pointer to the net_device with the embedded
1491 * struct device refcount incremented by one, or NULL on failure. The
1492 * refcount must be dropped when done with the net_device.
1493 */
Florian Fainelliaa836df2015-03-09 14:31:20 -07001494struct net_device *of_find_net_device_by_node(struct device_node *np)
1495{
1496 struct device *dev;
1497
1498 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1499 if (!dev)
1500 return NULL;
1501
1502 return to_net_dev(dev);
1503}
1504EXPORT_SYMBOL(of_find_net_device_by_node);
1505#endif
1506
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001507/* Delete sysfs entries but hold kobject reference until after all
1508 * netdev references are gone.
1509 */
WANG Cong6b53daf2014-07-23 16:09:10 -07001510void netdev_unregister_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
WANG Cong6b53daf2014-07-23 16:09:10 -07001512 struct device *dev = &(ndev->dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001513
1514 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001515
WANG Cong6b53daf2014-07-23 16:09:10 -07001516 remove_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001517
Ming Lei9802c8e2013-02-22 16:34:16 -08001518 pm_runtime_set_memalloc_noio(dev, false);
1519
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001520 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523/* Create sysfs entries for network device. */
WANG Cong6b53daf2014-07-23 16:09:10 -07001524int netdev_register_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
WANG Cong6b53daf2014-07-23 16:09:10 -07001526 struct device *dev = &(ndev->dev);
1527 const struct attribute_group **groups = ndev->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001528 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001530 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001531 dev->class = &net_class;
WANG Cong6b53daf2014-07-23 16:09:10 -07001532 dev->platform_data = ndev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001533 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
WANG Cong6b53daf2014-07-23 16:09:10 -07001535 dev_set_name(dev, "%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001537#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001538 /* Allow for a device specific group */
1539 if (*groups)
1540 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001542 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001543
1544#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
WANG Cong6b53daf2014-07-23 16:09:10 -07001545 if (ndev->ieee80211_ptr)
Johannes Berg38c1a012012-11-16 20:46:19 +01001546 *groups++ = &wireless_group;
1547#if IS_ENABLED(CONFIG_WIRELESS_EXT)
WANG Cong6b53daf2014-07-23 16:09:10 -07001548 else if (ndev->wireless_handlers)
Johannes Berg38c1a012012-11-16 20:46:19 +01001549 *groups++ = &wireless_group;
1550#endif
1551#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001552#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Tom Herbert0a9627f2010-03-16 08:03:29 +00001554 error = device_add(dev);
1555 if (error)
1556 return error;
1557
WANG Cong6b53daf2014-07-23 16:09:10 -07001558 error = register_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001559 if (error) {
1560 device_del(dev);
1561 return error;
1562 }
1563
Ming Lei9802c8e2013-02-22 16:34:16 -08001564 pm_runtime_set_memalloc_noio(dev, true);
1565
Tom Herbert0a9627f2010-03-16 08:03:29 +00001566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Tejun Heo58292cbe2013-09-11 22:29:04 -04001569int netdev_class_create_file_ns(struct class_attribute *class_attr,
1570 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001571{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001572 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001573}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001574EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001575
Tejun Heo58292cbe2013-09-11 22:29:04 -04001576void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1577 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001578{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001579 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001580}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001581EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001582
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001583int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001585 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return class_register(&net_class);
1587}