blob: 26c46f4726c57dbee9adea88e89e800c26957ba0 [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>
15#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070017#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/sock.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070019#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/rtnetlink.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070021#include <linux/vmalloc.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040022#include <linux/export.h>
Tom Herbert114cf582011-11-28 16:33:09 +000023#include <linux/jiffies.h>
Ming Lei9802c8e2013-02-22 16:34:16 -080024#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Pavel Emelyanov342709e2007-10-23 21:14:45 -070026#include "net-sysfs.h"
27
Eric W. Biederman8b41d182007-09-26 22:02:53 -070028#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070030static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static const char fmt_dec[] = "%d\n";
David Decotigny8ae6dac2011-04-27 18:32:38 +000032static const char fmt_udec[] = "%u\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static 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);
110NETDEVICE_SHOW_RO(iflink, fmt_dec);
111NETDEVICE_SHOW_RO(ifindex, fmt_dec);
112NETDEVICE_SHOW_RO(type, fmt_dec);
113NETDEVICE_SHOW_RO(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
WANG Cong6b53daf2014-07-23 16:09:10 -0700115static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
Tom Gundersen685343f2014-07-14 16:37:22 +0200116{
WANG Cong6b53daf2014-07-23 16:09:10 -0700117 return sprintf(buf, fmt_dec, dev->name_assign_type);
Tom Gundersen685343f2014-07-14 16:37:22 +0200118}
119
120static ssize_t name_assign_type_show(struct device *dev,
121 struct device_attribute *attr,
122 char *buf)
123{
WANG Cong6b53daf2014-07-23 16:09:10 -0700124 struct net_device *ndev = to_net_dev(dev);
Tom Gundersen685343f2014-07-14 16:37:22 +0200125 ssize_t ret = -EINVAL;
126
WANG Cong6b53daf2014-07-23 16:09:10 -0700127 if (ndev->name_assign_type != NET_NAME_UNKNOWN)
Tom Gundersen685343f2014-07-14 16:37:22 +0200128 ret = netdev_show(dev, attr, buf, format_name_assign_type);
129
130 return ret;
131}
132static DEVICE_ATTR_RO(name_assign_type);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700135static ssize_t address_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700136 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
WANG Cong6b53daf2014-07-23 16:09:10 -0700138 struct net_device *ndev = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ssize_t ret = -EINVAL;
140
141 read_lock(&dev_base_lock);
WANG Cong6b53daf2014-07-23 16:09:10 -0700142 if (dev_isalive(ndev))
143 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 read_unlock(&dev_base_lock);
145 return ret;
146}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700147static DEVICE_ATTR_RO(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700149static ssize_t broadcast_show(struct device *dev,
150 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
WANG Cong6b53daf2014-07-23 16:09:10 -0700152 struct net_device *ndev = to_net_dev(dev);
153 if (dev_isalive(ndev))
154 return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -EINVAL;
156}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700157static DEVICE_ATTR_RO(broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
WANG Cong6b53daf2014-07-23 16:09:10 -0700159static int change_carrier(struct net_device *dev, unsigned long new_carrier)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000160{
WANG Cong6b53daf2014-07-23 16:09:10 -0700161 if (!netif_running(dev))
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000162 return -EINVAL;
WANG Cong6b53daf2014-07-23 16:09:10 -0700163 return dev_change_carrier(dev, (bool) new_carrier);
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000164}
165
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700166static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t len)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000168{
169 return netdev_store(dev, attr, buf, len, change_carrier);
170}
171
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700172static ssize_t carrier_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700173 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct net_device *netdev = to_net_dev(dev);
176 if (netif_running(netdev)) {
177 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
178 }
179 return -EINVAL;
180}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700181static DEVICE_ATTR_RW(carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700183static ssize_t speed_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000184 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 Decotigny8ae6dac2011-04-27 18:32:38 +0000192 if (netif_running(netdev)) {
193 struct ethtool_cmd cmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000194 if (!__ethtool_get_settings(netdev, &cmd))
David Decotigny8ae6dac2011-04-27 18:32:38 +0000195 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
Andy Gospodarekd519e172009-10-02 09:26:12 +0000196 }
197 rtnl_unlock();
198 return ret;
199}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700200static DEVICE_ATTR_RO(speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000201
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700202static ssize_t duplex_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000203 struct device_attribute *attr, char *buf)
204{
205 struct net_device *netdev = to_net_dev(dev);
206 int ret = -EINVAL;
207
208 if (!rtnl_trylock())
209 return restart_syscall();
210
David Decotigny8ae6dac2011-04-27 18:32:38 +0000211 if (netif_running(netdev)) {
212 struct ethtool_cmd cmd;
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000213 if (!__ethtool_get_settings(netdev, &cmd)) {
214 const char *duplex;
215 switch (cmd.duplex) {
216 case DUPLEX_HALF:
217 duplex = "half";
218 break;
219 case DUPLEX_FULL:
220 duplex = "full";
221 break;
222 default:
223 duplex = "unknown";
224 break;
225 }
226 ret = sprintf(buf, "%s\n", duplex);
227 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000228 }
229 rtnl_unlock();
230 return ret;
231}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700232static DEVICE_ATTR_RO(duplex);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000233
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700234static ssize_t dormant_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700235 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800236{
237 struct net_device *netdev = to_net_dev(dev);
238
239 if (netif_running(netdev))
240 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
241
242 return -EINVAL;
243}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700244static DEVICE_ATTR_RO(dormant);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800245
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700246static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800247 "unknown",
248 "notpresent", /* currently unused */
249 "down",
250 "lowerlayerdown",
251 "testing", /* currently unused */
252 "dormant",
253 "up"
254};
255
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700256static ssize_t operstate_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700257 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800258{
259 const struct net_device *netdev = to_net_dev(dev);
260 unsigned char operstate;
261
262 read_lock(&dev_base_lock);
263 operstate = netdev->operstate;
264 if (!netif_running(netdev))
265 operstate = IF_OPER_DOWN;
266 read_unlock(&dev_base_lock);
267
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700268 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800269 return -EINVAL; /* should not happen */
270
271 return sprintf(buf, "%s\n", operstates[operstate]);
272}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700273static DEVICE_ATTR_RO(operstate);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800274
david decotigny2d3b4792014-03-29 09:48:35 -0700275static ssize_t carrier_changes_show(struct device *dev,
276 struct device_attribute *attr,
277 char *buf)
278{
279 struct net_device *netdev = to_net_dev(dev);
280 return sprintf(buf, fmt_dec,
281 atomic_read(&netdev->carrier_changes));
282}
283static DEVICE_ATTR_RO(carrier_changes);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* read-write attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
WANG Cong6b53daf2014-07-23 16:09:10 -0700287static int change_mtu(struct net_device *dev, unsigned long new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
WANG Cong6b53daf2014-07-23 16:09:10 -0700289 return dev_set_mtu(dev, (int) new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700292static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700293 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700295 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700297NETDEVICE_SHOW_RW(mtu, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
WANG Cong6b53daf2014-07-23 16:09:10 -0700299static int change_flags(struct net_device *dev, unsigned long new_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
WANG Cong6b53daf2014-07-23 16:09:10 -0700301 return dev_change_flags(dev, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700304static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700305 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700307 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700309NETDEVICE_SHOW_RW(flags, fmt_hex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
WANG Cong6b53daf2014-07-23 16:09:10 -0700311static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
WANG Cong6b53daf2014-07-23 16:09:10 -0700313 dev->tx_queue_len = new_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315}
316
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700317static ssize_t tx_queue_len_store(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700318 struct device_attribute *attr,
319 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000321 if (!capable(CAP_NET_ADMIN))
322 return -EPERM;
323
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700324 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700326NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Eric Dumazet3b47d302014-11-06 21:09:44 -0800328static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
329{
330 dev->gro_flush_timeout = val;
331 return 0;
332}
333
334static ssize_t gro_flush_timeout_store(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf, size_t len)
337{
338 if (!capable(CAP_NET_ADMIN))
339 return -EPERM;
340
341 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
342}
343NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
344
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700345static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700346 const char *buf, size_t len)
347{
348 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000349 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700350 size_t count = len;
351 ssize_t ret;
352
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000353 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700354 return -EPERM;
355
356 /* ignore trailing newline */
357 if (len > 0 && buf[len - 1] == '\n')
358 --count;
359
Eric W. Biederman336ca572009-05-13 16:57:25 +0000360 if (!rtnl_trylock())
361 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700362 ret = dev_set_alias(netdev, buf, count);
363 rtnl_unlock();
364
365 return ret < 0 ? ret : len;
366}
367
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700368static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700369 struct device_attribute *attr, char *buf)
370{
371 const struct net_device *netdev = to_net_dev(dev);
372 ssize_t ret = 0;
373
Eric W. Biederman336ca572009-05-13 16:57:25 +0000374 if (!rtnl_trylock())
375 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700376 if (netdev->ifalias)
377 ret = sprintf(buf, "%s\n", netdev->ifalias);
378 rtnl_unlock();
379 return ret;
380}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700381static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000382
WANG Cong6b53daf2014-07-23 16:09:10 -0700383static int change_group(struct net_device *dev, unsigned long new_group)
Vlad Dogarua512b922011-01-24 03:37:29 +0000384{
WANG Cong6b53daf2014-07-23 16:09:10 -0700385 dev_set_group(dev, (int) new_group);
Vlad Dogarua512b922011-01-24 03:37:29 +0000386 return 0;
387}
388
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700389static ssize_t group_store(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000391{
392 return netdev_store(dev, attr, buf, len, change_group);
393}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700394NETDEVICE_SHOW(group, fmt_dec);
395static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000396
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700397static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200398 struct device_attribute *attr, char *buf)
399{
400 struct net_device *netdev = to_net_dev(dev);
401 ssize_t ret = -EINVAL;
402
403 if (!rtnl_trylock())
404 return restart_syscall();
405
406 if (dev_isalive(netdev)) {
Jiri Pirko02637fc2014-11-28 14:34:16 +0100407 struct netdev_phys_item_id ppid;
Jiri Pirkoff80e512013-07-29 18:16:51 +0200408
409 ret = dev_get_phys_port_id(netdev, &ppid);
410 if (!ret)
411 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
412 }
413 rtnl_unlock();
414
415 return ret;
416}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700417static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200418
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700419static struct attribute *net_class_attrs[] = {
420 &dev_attr_netdev_group.attr,
421 &dev_attr_type.attr,
422 &dev_attr_dev_id.attr,
Amir Vadai3f859442014-02-25 18:17:50 +0200423 &dev_attr_dev_port.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700424 &dev_attr_iflink.attr,
425 &dev_attr_ifindex.attr,
Tom Gundersen685343f2014-07-14 16:37:22 +0200426 &dev_attr_name_assign_type.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700427 &dev_attr_addr_assign_type.attr,
428 &dev_attr_addr_len.attr,
429 &dev_attr_link_mode.attr,
430 &dev_attr_address.attr,
431 &dev_attr_broadcast.attr,
432 &dev_attr_speed.attr,
433 &dev_attr_duplex.attr,
434 &dev_attr_dormant.attr,
435 &dev_attr_operstate.attr,
david decotigny2d3b4792014-03-29 09:48:35 -0700436 &dev_attr_carrier_changes.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700437 &dev_attr_ifalias.attr,
438 &dev_attr_carrier.attr,
439 &dev_attr_mtu.attr,
440 &dev_attr_flags.attr,
441 &dev_attr_tx_queue_len.attr,
Eric Dumazet3b47d302014-11-06 21:09:44 -0800442 &dev_attr_gro_flush_timeout.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700443 &dev_attr_phys_port_id.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700444 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700446ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700449static ssize_t netstat_show(const struct device *d,
450 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 unsigned long offset)
452{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700453 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ssize_t ret = -EINVAL;
455
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000456 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
457 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700460 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700461 struct rtnl_link_stats64 temp;
462 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
463
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000464 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 read_unlock(&dev_base_lock);
467 return ret;
468}
469
470/* generate a read-only statistics attribute */
471#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700472static ssize_t name##_show(struct device *d, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700473 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700475 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000476 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700478static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480NETSTAT_ENTRY(rx_packets);
481NETSTAT_ENTRY(tx_packets);
482NETSTAT_ENTRY(rx_bytes);
483NETSTAT_ENTRY(tx_bytes);
484NETSTAT_ENTRY(rx_errors);
485NETSTAT_ENTRY(tx_errors);
486NETSTAT_ENTRY(rx_dropped);
487NETSTAT_ENTRY(tx_dropped);
488NETSTAT_ENTRY(multicast);
489NETSTAT_ENTRY(collisions);
490NETSTAT_ENTRY(rx_length_errors);
491NETSTAT_ENTRY(rx_over_errors);
492NETSTAT_ENTRY(rx_crc_errors);
493NETSTAT_ENTRY(rx_frame_errors);
494NETSTAT_ENTRY(rx_fifo_errors);
495NETSTAT_ENTRY(rx_missed_errors);
496NETSTAT_ENTRY(tx_aborted_errors);
497NETSTAT_ENTRY(tx_carrier_errors);
498NETSTAT_ENTRY(tx_fifo_errors);
499NETSTAT_ENTRY(tx_heartbeat_errors);
500NETSTAT_ENTRY(tx_window_errors);
501NETSTAT_ENTRY(rx_compressed);
502NETSTAT_ENTRY(tx_compressed);
503
504static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700505 &dev_attr_rx_packets.attr,
506 &dev_attr_tx_packets.attr,
507 &dev_attr_rx_bytes.attr,
508 &dev_attr_tx_bytes.attr,
509 &dev_attr_rx_errors.attr,
510 &dev_attr_tx_errors.attr,
511 &dev_attr_rx_dropped.attr,
512 &dev_attr_tx_dropped.attr,
513 &dev_attr_multicast.attr,
514 &dev_attr_collisions.attr,
515 &dev_attr_rx_length_errors.attr,
516 &dev_attr_rx_over_errors.attr,
517 &dev_attr_rx_crc_errors.attr,
518 &dev_attr_rx_frame_errors.attr,
519 &dev_attr_rx_fifo_errors.attr,
520 &dev_attr_rx_missed_errors.attr,
521 &dev_attr_tx_aborted_errors.attr,
522 &dev_attr_tx_carrier_errors.attr,
523 &dev_attr_tx_fifo_errors.attr,
524 &dev_attr_tx_heartbeat_errors.attr,
525 &dev_attr_tx_window_errors.attr,
526 &dev_attr_rx_compressed.attr,
527 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 NULL
529};
530
531
532static struct attribute_group netstat_group = {
533 .name = "statistics",
534 .attrs = netstat_attrs,
535};
Johannes Berg38c1a012012-11-16 20:46:19 +0100536
537#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
538static struct attribute *wireless_attrs[] = {
539 NULL
540};
541
542static struct attribute_group wireless_group = {
543 .name = "wireless",
544 .attrs = wireless_attrs,
545};
546#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700547
548#else /* CONFIG_SYSFS */
549#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700550#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Michael Daltona953be52014-01-16 22:23:28 -0800552#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000553#define to_rx_queue_attr(_attr) container_of(_attr, \
554 struct rx_queue_attribute, attr)
555
556#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
557
558static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
559 char *buf)
560{
561 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
562 struct netdev_rx_queue *queue = to_rx_queue(kobj);
563
564 if (!attribute->show)
565 return -EIO;
566
567 return attribute->show(queue, attribute, buf);
568}
569
570static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
571 const char *buf, size_t count)
572{
573 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
574 struct netdev_rx_queue *queue = to_rx_queue(kobj);
575
576 if (!attribute->store)
577 return -EIO;
578
579 return attribute->store(queue, attribute, buf, count);
580}
581
stephen hemmingerfa50d642010-08-31 12:14:13 +0000582static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000583 .show = rx_queue_attr_show,
584 .store = rx_queue_attr_store,
585};
586
Michael Daltona953be52014-01-16 22:23:28 -0800587#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000588static ssize_t show_rps_map(struct netdev_rx_queue *queue,
589 struct rx_queue_attribute *attribute, char *buf)
590{
591 struct rps_map *map;
592 cpumask_var_t mask;
593 size_t len = 0;
594 int i;
595
596 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
597 return -ENOMEM;
598
599 rcu_read_lock();
600 map = rcu_dereference(queue->rps_map);
601 if (map)
602 for (i = 0; i < map->len; i++)
603 cpumask_set_cpu(map->cpus[i], mask);
604
605 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
606 if (PAGE_SIZE - len < 3) {
607 rcu_read_unlock();
608 free_cpumask_var(mask);
609 return -EINVAL;
610 }
611 rcu_read_unlock();
612
613 free_cpumask_var(mask);
614 len += sprintf(buf + len, "\n");
615 return len;
616}
617
Eric Dumazetf5acb902010-04-19 14:40:57 -0700618static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000619 struct rx_queue_attribute *attribute,
620 const char *buf, size_t len)
621{
622 struct rps_map *old_map, *map;
623 cpumask_var_t mask;
624 int err, cpu, i;
625 static DEFINE_SPINLOCK(rps_map_lock);
626
627 if (!capable(CAP_NET_ADMIN))
628 return -EPERM;
629
630 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
631 return -ENOMEM;
632
633 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
634 if (err) {
635 free_cpumask_var(mask);
636 return err;
637 }
638
Eric Dumazet95c96172012-04-15 05:58:06 +0000639 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000640 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
641 GFP_KERNEL);
642 if (!map) {
643 free_cpumask_var(mask);
644 return -ENOMEM;
645 }
646
647 i = 0;
648 for_each_cpu_and(cpu, mask, cpu_online_mask)
649 map->cpus[i++] = cpu;
650
651 if (i)
652 map->len = i;
653 else {
654 kfree(map);
655 map = NULL;
656 }
657
658 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000659 old_map = rcu_dereference_protected(queue->rps_map,
660 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000661 rcu_assign_pointer(queue->rps_map, map);
662 spin_unlock(&rps_map_lock);
663
Eric Dumazetadc93002011-11-17 03:13:26 +0000664 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100665 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000666 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800667 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100668 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000669 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000670 free_cpumask_var(mask);
671 return len;
672}
673
Tom Herbertfec5e652010-04-16 16:01:27 -0700674static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
675 struct rx_queue_attribute *attr,
676 char *buf)
677{
678 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000679 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700680
681 rcu_read_lock();
682 flow_table = rcu_dereference(queue->rps_flow_table);
683 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000684 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700685 rcu_read_unlock();
686
Eric Dumazet60b778c2011-12-24 06:56:49 +0000687 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700688}
689
Tom Herbertfec5e652010-04-16 16:01:27 -0700690static void rps_dev_flow_table_release(struct rcu_head *rcu)
691{
692 struct rps_dev_flow_table *table = container_of(rcu,
693 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000694 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700695}
696
Eric Dumazetf5acb902010-04-19 14:40:57 -0700697static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700698 struct rx_queue_attribute *attr,
699 const char *buf, size_t len)
700{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000701 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700702 struct rps_dev_flow_table *table, *old_table;
703 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000704 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700705
706 if (!capable(CAP_NET_ADMIN))
707 return -EPERM;
708
Eric Dumazet60b778c2011-12-24 06:56:49 +0000709 rc = kstrtoul(buf, 0, &count);
710 if (rc < 0)
711 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700712
713 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000714 mask = count - 1;
715 /* mask = roundup_pow_of_two(count) - 1;
716 * without overflows...
717 */
718 while ((mask | (mask >> 1)) != mask)
719 mask |= (mask >> 1);
720 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800721 * and on 32bit arches, must check
722 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000723 */
724#if BITS_PER_LONG > 32
725 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000726 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000727#else
728 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000729 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700730 /* Enforce a limit to prevent overflow */
731 return -EINVAL;
732 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000733#endif
734 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700735 if (!table)
736 return -ENOMEM;
737
Eric Dumazet60b778c2011-12-24 06:56:49 +0000738 table->mask = mask;
739 for (count = 0; count <= mask; count++)
740 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700741 } else
742 table = NULL;
743
744 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000745 old_table = rcu_dereference_protected(queue->rps_flow_table,
746 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700747 rcu_assign_pointer(queue->rps_flow_table, table);
748 spin_unlock(&rps_dev_flow_lock);
749
750 if (old_table)
751 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
752
753 return len;
754}
755
Tom Herbert0a9627f2010-03-16 08:03:29 +0000756static struct rx_queue_attribute rps_cpus_attribute =
757 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
758
Tom Herbertfec5e652010-04-16 16:01:27 -0700759
760static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
761 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
762 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800763#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700764
Tom Herbert0a9627f2010-03-16 08:03:29 +0000765static struct attribute *rx_queue_default_attrs[] = {
Michael Daltona953be52014-01-16 22:23:28 -0800766#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000767 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700768 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800769#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000770 NULL
771};
772
773static void rx_queue_release(struct kobject *kobj)
774{
775 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800776#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000777 struct rps_map *map;
778 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000779
Tom Herbertfec5e652010-04-16 16:01:27 -0700780
Eric Dumazet33d480c2011-08-11 19:30:52 +0000781 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000782 if (map) {
783 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800784 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000785 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000786
Eric Dumazet33d480c2011-08-11 19:30:52 +0000787 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000788 if (flow_table) {
789 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000790 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000791 }
Michael Daltona953be52014-01-16 22:23:28 -0800792#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000793
John Fastabend9ea19482010-11-16 06:31:39 +0000794 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000795 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000796}
797
Weilong Chen82ef3d52014-01-16 17:24:31 +0800798static const void *rx_queue_namespace(struct kobject *kobj)
799{
800 struct netdev_rx_queue *queue = to_rx_queue(kobj);
801 struct device *dev = &queue->dev->dev;
802 const void *ns = NULL;
803
804 if (dev->class && dev->class->ns_type)
805 ns = dev->class->namespace(dev);
806
807 return ns;
808}
809
Tom Herbert0a9627f2010-03-16 08:03:29 +0000810static struct kobj_type rx_queue_ktype = {
811 .sysfs_ops = &rx_queue_sysfs_ops,
812 .release = rx_queue_release,
813 .default_attrs = rx_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +0800814 .namespace = rx_queue_namespace
Tom Herbert0a9627f2010-03-16 08:03:29 +0000815};
816
WANG Cong6b53daf2014-07-23 16:09:10 -0700817static int rx_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000818{
WANG Cong6b53daf2014-07-23 16:09:10 -0700819 struct netdev_rx_queue *queue = dev->_rx + index;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000820 struct kobject *kobj = &queue->kobj;
821 int error = 0;
822
WANG Cong6b53daf2014-07-23 16:09:10 -0700823 kobj->kset = dev->queues_kset;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000824 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
825 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800826 if (error)
827 goto exit;
828
WANG Cong6b53daf2014-07-23 16:09:10 -0700829 if (dev->sysfs_rx_queue_group) {
830 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
Michael Daltona953be52014-01-16 22:23:28 -0800831 if (error)
832 goto exit;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000833 }
834
835 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000836 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000837
838 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800839exit:
840 kobject_put(kobj);
841 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000842}
Paul Bolle80dd6ea2014-02-09 14:07:11 +0100843#endif /* CONFIG_SYSFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000844
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000845int
WANG Cong6b53daf2014-07-23 16:09:10 -0700846net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000847{
Michael Daltona953be52014-01-16 22:23:28 -0800848#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000849 int i;
850 int error = 0;
851
Michael Daltona953be52014-01-16 22:23:28 -0800852#ifndef CONFIG_RPS
WANG Cong6b53daf2014-07-23 16:09:10 -0700853 if (!dev->sysfs_rx_queue_group)
Michael Daltona953be52014-01-16 22:23:28 -0800854 return 0;
855#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000856 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700857 error = rx_queue_add_kobject(dev, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000858 if (error) {
859 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000860 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000861 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000862 }
863
Michael Daltona953be52014-01-16 22:23:28 -0800864 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700865 if (dev->sysfs_rx_queue_group)
866 sysfs_remove_group(&dev->_rx[i].kobj,
867 dev->sysfs_rx_queue_group);
868 kobject_put(&dev->_rx[i].kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800869 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000870
871 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000872#else
873 return 0;
874#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000875}
876
david decotignyccf5ff62011-11-16 12:15:10 +0000877#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000878/*
879 * netdev_queue sysfs structures and functions.
880 */
881struct netdev_queue_attribute {
882 struct attribute attr;
883 ssize_t (*show)(struct netdev_queue *queue,
884 struct netdev_queue_attribute *attr, char *buf);
885 ssize_t (*store)(struct netdev_queue *queue,
886 struct netdev_queue_attribute *attr, const char *buf, size_t len);
887};
888#define to_netdev_queue_attr(_attr) container_of(_attr, \
889 struct netdev_queue_attribute, attr)
890
891#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
892
893static ssize_t netdev_queue_attr_show(struct kobject *kobj,
894 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000895{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000896 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
897 struct netdev_queue *queue = to_netdev_queue(kobj);
898
899 if (!attribute->show)
900 return -EIO;
901
902 return attribute->show(queue, attribute, buf);
903}
904
905static ssize_t netdev_queue_attr_store(struct kobject *kobj,
906 struct attribute *attr,
907 const char *buf, size_t count)
908{
909 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
910 struct netdev_queue *queue = to_netdev_queue(kobj);
911
912 if (!attribute->store)
913 return -EIO;
914
915 return attribute->store(queue, attribute, buf, count);
916}
917
918static const struct sysfs_ops netdev_queue_sysfs_ops = {
919 .show = netdev_queue_attr_show,
920 .store = netdev_queue_attr_store,
921};
922
david decotignyccf5ff62011-11-16 12:15:10 +0000923static ssize_t show_trans_timeout(struct netdev_queue *queue,
924 struct netdev_queue_attribute *attribute,
925 char *buf)
926{
927 unsigned long trans_timeout;
928
929 spin_lock_irq(&queue->_xmit_lock);
930 trans_timeout = queue->trans_timeout;
931 spin_unlock_irq(&queue->_xmit_lock);
932
933 return sprintf(buf, "%lu", trans_timeout);
934}
935
936static struct netdev_queue_attribute queue_trans_timeout =
937 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
938
Tom Herbert114cf582011-11-28 16:33:09 +0000939#ifdef CONFIG_BQL
940/*
941 * Byte queue limits sysfs structures and functions.
942 */
943static ssize_t bql_show(char *buf, unsigned int value)
944{
945 return sprintf(buf, "%u\n", value);
946}
947
948static ssize_t bql_set(const char *buf, const size_t count,
949 unsigned int *pvalue)
950{
951 unsigned int value;
952 int err;
953
954 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
955 value = DQL_MAX_LIMIT;
956 else {
957 err = kstrtouint(buf, 10, &value);
958 if (err < 0)
959 return err;
960 if (value > DQL_MAX_LIMIT)
961 return -EINVAL;
962 }
963
964 *pvalue = value;
965
966 return count;
967}
968
969static ssize_t bql_show_hold_time(struct netdev_queue *queue,
970 struct netdev_queue_attribute *attr,
971 char *buf)
972{
973 struct dql *dql = &queue->dql;
974
975 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
976}
977
978static ssize_t bql_set_hold_time(struct netdev_queue *queue,
979 struct netdev_queue_attribute *attribute,
980 const char *buf, size_t len)
981{
982 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000983 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000984 int err;
985
986 err = kstrtouint(buf, 10, &value);
987 if (err < 0)
988 return err;
989
990 dql->slack_hold_time = msecs_to_jiffies(value);
991
992 return len;
993}
994
995static struct netdev_queue_attribute bql_hold_time_attribute =
996 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
997 bql_set_hold_time);
998
999static ssize_t bql_show_inflight(struct netdev_queue *queue,
1000 struct netdev_queue_attribute *attr,
1001 char *buf)
1002{
1003 struct dql *dql = &queue->dql;
1004
1005 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1006}
1007
1008static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +00001009 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +00001010
1011#define BQL_ATTR(NAME, FIELD) \
1012static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1013 struct netdev_queue_attribute *attr, \
1014 char *buf) \
1015{ \
1016 return bql_show(buf, queue->dql.FIELD); \
1017} \
1018 \
1019static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1020 struct netdev_queue_attribute *attr, \
1021 const char *buf, size_t len) \
1022{ \
1023 return bql_set(buf, len, &queue->dql.FIELD); \
1024} \
1025 \
1026static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
1027 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
1028 bql_set_ ## NAME);
1029
1030BQL_ATTR(limit, limit)
1031BQL_ATTR(limit_max, max_limit)
1032BQL_ATTR(limit_min, min_limit)
1033
1034static struct attribute *dql_attrs[] = {
1035 &bql_limit_attribute.attr,
1036 &bql_limit_max_attribute.attr,
1037 &bql_limit_min_attribute.attr,
1038 &bql_hold_time_attribute.attr,
1039 &bql_inflight_attribute.attr,
1040 NULL
1041};
1042
1043static struct attribute_group dql_group = {
1044 .name = "byte_queue_limits",
1045 .attrs = dql_attrs,
1046};
1047#endif /* CONFIG_BQL */
1048
david decotignyccf5ff62011-11-16 12:15:10 +00001049#ifdef CONFIG_XPS
Eric Dumazeted1acc82014-02-13 10:07:13 -08001050static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001051{
1052 struct net_device *dev = queue->dev;
Eric Dumazeted1acc82014-02-13 10:07:13 -08001053 unsigned int i;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001054
Eric Dumazeted1acc82014-02-13 10:07:13 -08001055 i = queue - dev->_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001056 BUG_ON(i >= dev->num_tx_queues);
1057
1058 return i;
1059}
1060
1061
1062static ssize_t show_xps_map(struct netdev_queue *queue,
1063 struct netdev_queue_attribute *attribute, char *buf)
1064{
1065 struct net_device *dev = queue->dev;
1066 struct xps_dev_maps *dev_maps;
1067 cpumask_var_t mask;
1068 unsigned long index;
1069 size_t len = 0;
1070 int i;
1071
1072 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1073 return -ENOMEM;
1074
1075 index = get_netdev_queue_index(queue);
1076
1077 rcu_read_lock();
1078 dev_maps = rcu_dereference(dev->xps_maps);
1079 if (dev_maps) {
1080 for_each_possible_cpu(i) {
1081 struct xps_map *map =
1082 rcu_dereference(dev_maps->cpu_map[i]);
1083 if (map) {
1084 int j;
1085 for (j = 0; j < map->len; j++) {
1086 if (map->queues[j] == index) {
1087 cpumask_set_cpu(i, mask);
1088 break;
1089 }
1090 }
1091 }
1092 }
1093 }
1094 rcu_read_unlock();
1095
1096 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1097 if (PAGE_SIZE - len < 3) {
1098 free_cpumask_var(mask);
1099 return -EINVAL;
1100 }
1101
1102 free_cpumask_var(mask);
1103 len += sprintf(buf + len, "\n");
1104 return len;
1105}
1106
Tom Herbert1d24eb42010-11-21 13:17:27 +00001107static ssize_t store_xps_map(struct netdev_queue *queue,
1108 struct netdev_queue_attribute *attribute,
1109 const char *buf, size_t len)
1110{
1111 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001112 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001113 cpumask_var_t mask;
1114 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001115
1116 if (!capable(CAP_NET_ADMIN))
1117 return -EPERM;
1118
1119 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1120 return -ENOMEM;
1121
1122 index = get_netdev_queue_index(queue);
1123
1124 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1125 if (err) {
1126 free_cpumask_var(mask);
1127 return err;
1128 }
1129
Alexander Duyck537c00d2013-01-10 08:57:02 +00001130 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001131
1132 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001133
Alexander Duyck537c00d2013-01-10 08:57:02 +00001134 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001135}
1136
1137static struct netdev_queue_attribute xps_cpus_attribute =
1138 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001139#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001140
1141static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001142 &queue_trans_timeout.attr,
1143#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001144 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001145#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001146 NULL
1147};
1148
1149static void netdev_queue_release(struct kobject *kobj)
1150{
1151 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001152
Tom Herbert1d24eb42010-11-21 13:17:27 +00001153 memset(kobj, 0, sizeof(*kobj));
1154 dev_put(queue->dev);
1155}
1156
Weilong Chen82ef3d52014-01-16 17:24:31 +08001157static const void *netdev_queue_namespace(struct kobject *kobj)
1158{
1159 struct netdev_queue *queue = to_netdev_queue(kobj);
1160 struct device *dev = &queue->dev->dev;
1161 const void *ns = NULL;
1162
1163 if (dev->class && dev->class->ns_type)
1164 ns = dev->class->namespace(dev);
1165
1166 return ns;
1167}
1168
Tom Herbert1d24eb42010-11-21 13:17:27 +00001169static struct kobj_type netdev_queue_ktype = {
1170 .sysfs_ops = &netdev_queue_sysfs_ops,
1171 .release = netdev_queue_release,
1172 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001173 .namespace = netdev_queue_namespace,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001174};
1175
WANG Cong6b53daf2014-07-23 16:09:10 -07001176static int netdev_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001177{
WANG Cong6b53daf2014-07-23 16:09:10 -07001178 struct netdev_queue *queue = dev->_tx + index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001179 struct kobject *kobj = &queue->kobj;
1180 int error = 0;
1181
WANG Cong6b53daf2014-07-23 16:09:10 -07001182 kobj->kset = dev->queues_kset;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001183 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1184 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001185 if (error)
1186 goto exit;
1187
1188#ifdef CONFIG_BQL
1189 error = sysfs_create_group(kobj, &dql_group);
1190 if (error)
1191 goto exit;
1192#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001193
1194 kobject_uevent(kobj, KOBJ_ADD);
1195 dev_hold(queue->dev);
1196
Tom Herbert114cf582011-11-28 16:33:09 +00001197 return 0;
1198exit:
1199 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001200 return error;
1201}
david decotignyccf5ff62011-11-16 12:15:10 +00001202#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001203
1204int
WANG Cong6b53daf2014-07-23 16:09:10 -07001205netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001206{
david decotignyccf5ff62011-11-16 12:15:10 +00001207#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001208 int i;
1209 int error = 0;
1210
1211 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001212 error = netdev_queue_add_kobject(dev, i);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001213 if (error) {
1214 new_num = old_num;
1215 break;
1216 }
1217 }
1218
Tom Herbert114cf582011-11-28 16:33:09 +00001219 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001220 struct netdev_queue *queue = dev->_tx + i;
Tom Herbert114cf582011-11-28 16:33:09 +00001221
1222#ifdef CONFIG_BQL
1223 sysfs_remove_group(&queue->kobj, &dql_group);
1224#endif
1225 kobject_put(&queue->kobj);
1226 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001227
1228 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001229#else
1230 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001231#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001232}
1233
WANG Cong6b53daf2014-07-23 16:09:10 -07001234static int register_queue_kobjects(struct net_device *dev)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001235{
Tom Herbertbf264142010-11-26 08:36:09 +00001236 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001237
david decotignyccf5ff62011-11-16 12:15:10 +00001238#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001239 dev->queues_kset = kset_create_and_add("queues",
1240 NULL, &dev->dev.kobj);
1241 if (!dev->queues_kset)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001242 return -ENOMEM;
WANG Cong6b53daf2014-07-23 16:09:10 -07001243 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001244#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001245 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001246
WANG Cong6b53daf2014-07-23 16:09:10 -07001247 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001248 if (error)
1249 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001250 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001251
WANG Cong6b53daf2014-07-23 16:09:10 -07001252 error = netdev_queue_update_kobjects(dev, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001253 if (error)
1254 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001255 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001256
1257 return 0;
1258
1259error:
WANG Cong6b53daf2014-07-23 16:09:10 -07001260 netdev_queue_update_kobjects(dev, txq, 0);
1261 net_rx_queue_update_kobjects(dev, rxq, 0);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001262 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001263}
1264
WANG Cong6b53daf2014-07-23 16:09:10 -07001265static void remove_queue_kobjects(struct net_device *dev)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001266{
Tom Herbertbf264142010-11-26 08:36:09 +00001267 int real_rx = 0, real_tx = 0;
1268
Michael Daltona953be52014-01-16 22:23:28 -08001269#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001270 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001271#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001272 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001273
WANG Cong6b53daf2014-07-23 16:09:10 -07001274 net_rx_queue_update_kobjects(dev, real_rx, 0);
1275 netdev_queue_update_kobjects(dev, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001276#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001277 kset_unregister(dev->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001278#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001279}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001280
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001281static bool net_current_may_mount(void)
1282{
1283 struct net *net = current->nsproxy->net_ns;
1284
1285 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1286}
1287
Al Viroa685e082011-06-08 21:13:01 -04001288static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001289{
Al Viroa685e082011-06-08 21:13:01 -04001290 struct net *ns = current->nsproxy->net_ns;
1291#ifdef CONFIG_NET_NS
1292 if (ns)
1293 atomic_inc(&ns->passive);
1294#endif
1295 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001296}
1297
1298static const void *net_initial_ns(void)
1299{
1300 return &init_net;
1301}
1302
1303static const void *net_netlink_ns(struct sock *sk)
1304{
1305 return sock_net(sk);
1306}
1307
Johannes Berg04600792010-08-05 17:45:15 +02001308struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001309 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001310 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001311 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001312 .netlink_ns = net_netlink_ns,
1313 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001314 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001315};
Johannes Berg04600792010-08-05 17:45:15 +02001316EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001317
Kay Sievers7eff2e72007-08-14 15:15:12 +02001318static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001320 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001321 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Kay Sievers312c0042005-11-16 09:00:00 +01001323 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001324 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001325 if (retval)
1326 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001328 /* pass ifindex to uevent.
1329 * ifindex is useful as it won't change (interface name may change)
1330 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001331 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001332
Eric Rannaudbf624562007-03-30 22:23:12 -07001333exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001334 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001338 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001339 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001341static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001343 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 BUG_ON(dev->reg_state != NETREG_RELEASED);
1346
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001347 kfree(dev->ifalias);
Eric Dumazet74d332c2013-10-30 13:10:44 -07001348 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001351static const void *net_namespace(struct device *d)
1352{
1353 struct net_device *dev;
1354 dev = container_of(d, struct net_device, dev);
1355 return dev_net(dev);
1356}
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358static struct class net_class = {
1359 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001360 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001361 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001362 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001363 .ns_type = &net_ns_type_operations,
1364 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365};
1366
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001367/* Delete sysfs entries but hold kobject reference until after all
1368 * netdev references are gone.
1369 */
WANG Cong6b53daf2014-07-23 16:09:10 -07001370void netdev_unregister_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
WANG Cong6b53daf2014-07-23 16:09:10 -07001372 struct device *dev = &(ndev->dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001373
1374 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001375
WANG Cong6b53daf2014-07-23 16:09:10 -07001376 remove_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001377
Ming Lei9802c8e2013-02-22 16:34:16 -08001378 pm_runtime_set_memalloc_noio(dev, false);
1379
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001380 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/* Create sysfs entries for network device. */
WANG Cong6b53daf2014-07-23 16:09:10 -07001384int netdev_register_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
WANG Cong6b53daf2014-07-23 16:09:10 -07001386 struct device *dev = &(ndev->dev);
1387 const struct attribute_group **groups = ndev->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001388 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001390 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001391 dev->class = &net_class;
WANG Cong6b53daf2014-07-23 16:09:10 -07001392 dev->platform_data = ndev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001393 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
WANG Cong6b53daf2014-07-23 16:09:10 -07001395 dev_set_name(dev, "%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001397#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001398 /* Allow for a device specific group */
1399 if (*groups)
1400 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001402 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001403
1404#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
WANG Cong6b53daf2014-07-23 16:09:10 -07001405 if (ndev->ieee80211_ptr)
Johannes Berg38c1a012012-11-16 20:46:19 +01001406 *groups++ = &wireless_group;
1407#if IS_ENABLED(CONFIG_WIRELESS_EXT)
WANG Cong6b53daf2014-07-23 16:09:10 -07001408 else if (ndev->wireless_handlers)
Johannes Berg38c1a012012-11-16 20:46:19 +01001409 *groups++ = &wireless_group;
1410#endif
1411#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001412#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Tom Herbert0a9627f2010-03-16 08:03:29 +00001414 error = device_add(dev);
1415 if (error)
1416 return error;
1417
WANG Cong6b53daf2014-07-23 16:09:10 -07001418 error = register_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001419 if (error) {
1420 device_del(dev);
1421 return error;
1422 }
1423
Ming Lei9802c8e2013-02-22 16:34:16 -08001424 pm_runtime_set_memalloc_noio(dev, true);
1425
Tom Herbert0a9627f2010-03-16 08:03:29 +00001426 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Tejun Heo58292cbe2013-09-11 22:29:04 -04001429int netdev_class_create_file_ns(struct class_attribute *class_attr,
1430 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001431{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001432 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001433}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001434EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001435
Tejun Heo58292cbe2013-09-11 22:29:04 -04001436void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1437 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001438{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001439 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001440}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001441EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001442
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001443int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001445 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return class_register(&net_class);
1447}