blob: 9dd06699b09c9434e60aa40a948adf69d5505d2d [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
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700328static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700329 const char *buf, size_t len)
330{
331 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000332 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700333 size_t count = len;
334 ssize_t ret;
335
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000336 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700337 return -EPERM;
338
339 /* ignore trailing newline */
340 if (len > 0 && buf[len - 1] == '\n')
341 --count;
342
Eric W. Biederman336ca572009-05-13 16:57:25 +0000343 if (!rtnl_trylock())
344 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700345 ret = dev_set_alias(netdev, buf, count);
346 rtnl_unlock();
347
348 return ret < 0 ? ret : len;
349}
350
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700351static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700352 struct device_attribute *attr, char *buf)
353{
354 const struct net_device *netdev = to_net_dev(dev);
355 ssize_t ret = 0;
356
Eric W. Biederman336ca572009-05-13 16:57:25 +0000357 if (!rtnl_trylock())
358 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700359 if (netdev->ifalias)
360 ret = sprintf(buf, "%s\n", netdev->ifalias);
361 rtnl_unlock();
362 return ret;
363}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700364static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000365
WANG Cong6b53daf2014-07-23 16:09:10 -0700366static int change_group(struct net_device *dev, unsigned long new_group)
Vlad Dogarua512b922011-01-24 03:37:29 +0000367{
WANG Cong6b53daf2014-07-23 16:09:10 -0700368 dev_set_group(dev, (int) new_group);
Vlad Dogarua512b922011-01-24 03:37:29 +0000369 return 0;
370}
371
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700372static ssize_t group_store(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000374{
375 return netdev_store(dev, attr, buf, len, change_group);
376}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700377NETDEVICE_SHOW(group, fmt_dec);
378static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000379
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700380static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200381 struct device_attribute *attr, char *buf)
382{
383 struct net_device *netdev = to_net_dev(dev);
384 ssize_t ret = -EINVAL;
385
386 if (!rtnl_trylock())
387 return restart_syscall();
388
389 if (dev_isalive(netdev)) {
390 struct netdev_phys_port_id ppid;
391
392 ret = dev_get_phys_port_id(netdev, &ppid);
393 if (!ret)
394 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
395 }
396 rtnl_unlock();
397
398 return ret;
399}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700400static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200401
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700402static struct attribute *net_class_attrs[] = {
403 &dev_attr_netdev_group.attr,
404 &dev_attr_type.attr,
405 &dev_attr_dev_id.attr,
Amir Vadai3f859442014-02-25 18:17:50 +0200406 &dev_attr_dev_port.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700407 &dev_attr_iflink.attr,
408 &dev_attr_ifindex.attr,
Tom Gundersen685343f2014-07-14 16:37:22 +0200409 &dev_attr_name_assign_type.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700410 &dev_attr_addr_assign_type.attr,
411 &dev_attr_addr_len.attr,
412 &dev_attr_link_mode.attr,
413 &dev_attr_address.attr,
414 &dev_attr_broadcast.attr,
415 &dev_attr_speed.attr,
416 &dev_attr_duplex.attr,
417 &dev_attr_dormant.attr,
418 &dev_attr_operstate.attr,
david decotigny2d3b4792014-03-29 09:48:35 -0700419 &dev_attr_carrier_changes.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700420 &dev_attr_ifalias.attr,
421 &dev_attr_carrier.attr,
422 &dev_attr_mtu.attr,
423 &dev_attr_flags.attr,
424 &dev_attr_tx_queue_len.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700425 &dev_attr_phys_port_id.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700426 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700428ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700431static ssize_t netstat_show(const struct device *d,
432 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned long offset)
434{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700435 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ssize_t ret = -EINVAL;
437
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000438 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
439 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700442 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700443 struct rtnl_link_stats64 temp;
444 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
445
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000446 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 read_unlock(&dev_base_lock);
449 return ret;
450}
451
452/* generate a read-only statistics attribute */
453#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700454static ssize_t name##_show(struct device *d, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700455 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700457 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000458 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700460static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462NETSTAT_ENTRY(rx_packets);
463NETSTAT_ENTRY(tx_packets);
464NETSTAT_ENTRY(rx_bytes);
465NETSTAT_ENTRY(tx_bytes);
466NETSTAT_ENTRY(rx_errors);
467NETSTAT_ENTRY(tx_errors);
468NETSTAT_ENTRY(rx_dropped);
469NETSTAT_ENTRY(tx_dropped);
470NETSTAT_ENTRY(multicast);
471NETSTAT_ENTRY(collisions);
472NETSTAT_ENTRY(rx_length_errors);
473NETSTAT_ENTRY(rx_over_errors);
474NETSTAT_ENTRY(rx_crc_errors);
475NETSTAT_ENTRY(rx_frame_errors);
476NETSTAT_ENTRY(rx_fifo_errors);
477NETSTAT_ENTRY(rx_missed_errors);
478NETSTAT_ENTRY(tx_aborted_errors);
479NETSTAT_ENTRY(tx_carrier_errors);
480NETSTAT_ENTRY(tx_fifo_errors);
481NETSTAT_ENTRY(tx_heartbeat_errors);
482NETSTAT_ENTRY(tx_window_errors);
483NETSTAT_ENTRY(rx_compressed);
484NETSTAT_ENTRY(tx_compressed);
485
486static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700487 &dev_attr_rx_packets.attr,
488 &dev_attr_tx_packets.attr,
489 &dev_attr_rx_bytes.attr,
490 &dev_attr_tx_bytes.attr,
491 &dev_attr_rx_errors.attr,
492 &dev_attr_tx_errors.attr,
493 &dev_attr_rx_dropped.attr,
494 &dev_attr_tx_dropped.attr,
495 &dev_attr_multicast.attr,
496 &dev_attr_collisions.attr,
497 &dev_attr_rx_length_errors.attr,
498 &dev_attr_rx_over_errors.attr,
499 &dev_attr_rx_crc_errors.attr,
500 &dev_attr_rx_frame_errors.attr,
501 &dev_attr_rx_fifo_errors.attr,
502 &dev_attr_rx_missed_errors.attr,
503 &dev_attr_tx_aborted_errors.attr,
504 &dev_attr_tx_carrier_errors.attr,
505 &dev_attr_tx_fifo_errors.attr,
506 &dev_attr_tx_heartbeat_errors.attr,
507 &dev_attr_tx_window_errors.attr,
508 &dev_attr_rx_compressed.attr,
509 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 NULL
511};
512
513
514static struct attribute_group netstat_group = {
515 .name = "statistics",
516 .attrs = netstat_attrs,
517};
Johannes Berg38c1a012012-11-16 20:46:19 +0100518
519#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
520static struct attribute *wireless_attrs[] = {
521 NULL
522};
523
524static struct attribute_group wireless_group = {
525 .name = "wireless",
526 .attrs = wireless_attrs,
527};
528#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700529
530#else /* CONFIG_SYSFS */
531#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700532#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Michael Daltona953be52014-01-16 22:23:28 -0800534#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000535#define to_rx_queue_attr(_attr) container_of(_attr, \
536 struct rx_queue_attribute, attr)
537
538#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
539
540static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
541 char *buf)
542{
543 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
544 struct netdev_rx_queue *queue = to_rx_queue(kobj);
545
546 if (!attribute->show)
547 return -EIO;
548
549 return attribute->show(queue, attribute, buf);
550}
551
552static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
553 const char *buf, size_t count)
554{
555 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
556 struct netdev_rx_queue *queue = to_rx_queue(kobj);
557
558 if (!attribute->store)
559 return -EIO;
560
561 return attribute->store(queue, attribute, buf, count);
562}
563
stephen hemmingerfa50d642010-08-31 12:14:13 +0000564static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000565 .show = rx_queue_attr_show,
566 .store = rx_queue_attr_store,
567};
568
Michael Daltona953be52014-01-16 22:23:28 -0800569#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000570static ssize_t show_rps_map(struct netdev_rx_queue *queue,
571 struct rx_queue_attribute *attribute, char *buf)
572{
573 struct rps_map *map;
574 cpumask_var_t mask;
575 size_t len = 0;
576 int i;
577
578 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
579 return -ENOMEM;
580
581 rcu_read_lock();
582 map = rcu_dereference(queue->rps_map);
583 if (map)
584 for (i = 0; i < map->len; i++)
585 cpumask_set_cpu(map->cpus[i], mask);
586
587 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
588 if (PAGE_SIZE - len < 3) {
589 rcu_read_unlock();
590 free_cpumask_var(mask);
591 return -EINVAL;
592 }
593 rcu_read_unlock();
594
595 free_cpumask_var(mask);
596 len += sprintf(buf + len, "\n");
597 return len;
598}
599
Eric Dumazetf5acb902010-04-19 14:40:57 -0700600static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000601 struct rx_queue_attribute *attribute,
602 const char *buf, size_t len)
603{
604 struct rps_map *old_map, *map;
605 cpumask_var_t mask;
606 int err, cpu, i;
607 static DEFINE_SPINLOCK(rps_map_lock);
608
609 if (!capable(CAP_NET_ADMIN))
610 return -EPERM;
611
612 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
613 return -ENOMEM;
614
615 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
616 if (err) {
617 free_cpumask_var(mask);
618 return err;
619 }
620
Eric Dumazet95c96172012-04-15 05:58:06 +0000621 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000622 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
623 GFP_KERNEL);
624 if (!map) {
625 free_cpumask_var(mask);
626 return -ENOMEM;
627 }
628
629 i = 0;
630 for_each_cpu_and(cpu, mask, cpu_online_mask)
631 map->cpus[i++] = cpu;
632
633 if (i)
634 map->len = i;
635 else {
636 kfree(map);
637 map = NULL;
638 }
639
640 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000641 old_map = rcu_dereference_protected(queue->rps_map,
642 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000643 rcu_assign_pointer(queue->rps_map, map);
644 spin_unlock(&rps_map_lock);
645
Eric Dumazetadc93002011-11-17 03:13:26 +0000646 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100647 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000648 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800649 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100650 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000651 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000652 free_cpumask_var(mask);
653 return len;
654}
655
Tom Herbertfec5e652010-04-16 16:01:27 -0700656static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
657 struct rx_queue_attribute *attr,
658 char *buf)
659{
660 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000661 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700662
663 rcu_read_lock();
664 flow_table = rcu_dereference(queue->rps_flow_table);
665 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000666 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700667 rcu_read_unlock();
668
Eric Dumazet60b778c2011-12-24 06:56:49 +0000669 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700670}
671
Tom Herbertfec5e652010-04-16 16:01:27 -0700672static void rps_dev_flow_table_release(struct rcu_head *rcu)
673{
674 struct rps_dev_flow_table *table = container_of(rcu,
675 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000676 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700677}
678
Eric Dumazetf5acb902010-04-19 14:40:57 -0700679static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700680 struct rx_queue_attribute *attr,
681 const char *buf, size_t len)
682{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000683 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700684 struct rps_dev_flow_table *table, *old_table;
685 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000686 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700687
688 if (!capable(CAP_NET_ADMIN))
689 return -EPERM;
690
Eric Dumazet60b778c2011-12-24 06:56:49 +0000691 rc = kstrtoul(buf, 0, &count);
692 if (rc < 0)
693 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700694
695 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000696 mask = count - 1;
697 /* mask = roundup_pow_of_two(count) - 1;
698 * without overflows...
699 */
700 while ((mask | (mask >> 1)) != mask)
701 mask |= (mask >> 1);
702 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800703 * and on 32bit arches, must check
704 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000705 */
706#if BITS_PER_LONG > 32
707 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000708 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000709#else
710 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000711 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700712 /* Enforce a limit to prevent overflow */
713 return -EINVAL;
714 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000715#endif
716 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700717 if (!table)
718 return -ENOMEM;
719
Eric Dumazet60b778c2011-12-24 06:56:49 +0000720 table->mask = mask;
721 for (count = 0; count <= mask; count++)
722 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700723 } else
724 table = NULL;
725
726 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000727 old_table = rcu_dereference_protected(queue->rps_flow_table,
728 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700729 rcu_assign_pointer(queue->rps_flow_table, table);
730 spin_unlock(&rps_dev_flow_lock);
731
732 if (old_table)
733 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
734
735 return len;
736}
737
Tom Herbert0a9627f2010-03-16 08:03:29 +0000738static struct rx_queue_attribute rps_cpus_attribute =
739 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
740
Tom Herbertfec5e652010-04-16 16:01:27 -0700741
742static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
743 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
744 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800745#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700746
Tom Herbert0a9627f2010-03-16 08:03:29 +0000747static struct attribute *rx_queue_default_attrs[] = {
Michael Daltona953be52014-01-16 22:23:28 -0800748#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000749 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700750 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800751#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000752 NULL
753};
754
755static void rx_queue_release(struct kobject *kobj)
756{
757 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800758#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000759 struct rps_map *map;
760 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000761
Tom Herbertfec5e652010-04-16 16:01:27 -0700762
Eric Dumazet33d480c2011-08-11 19:30:52 +0000763 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000764 if (map) {
765 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800766 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000767 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000768
Eric Dumazet33d480c2011-08-11 19:30:52 +0000769 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000770 if (flow_table) {
771 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000772 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000773 }
Michael Daltona953be52014-01-16 22:23:28 -0800774#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000775
John Fastabend9ea19482010-11-16 06:31:39 +0000776 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000777 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000778}
779
Weilong Chen82ef3d52014-01-16 17:24:31 +0800780static const void *rx_queue_namespace(struct kobject *kobj)
781{
782 struct netdev_rx_queue *queue = to_rx_queue(kobj);
783 struct device *dev = &queue->dev->dev;
784 const void *ns = NULL;
785
786 if (dev->class && dev->class->ns_type)
787 ns = dev->class->namespace(dev);
788
789 return ns;
790}
791
Tom Herbert0a9627f2010-03-16 08:03:29 +0000792static struct kobj_type rx_queue_ktype = {
793 .sysfs_ops = &rx_queue_sysfs_ops,
794 .release = rx_queue_release,
795 .default_attrs = rx_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +0800796 .namespace = rx_queue_namespace
Tom Herbert0a9627f2010-03-16 08:03:29 +0000797};
798
WANG Cong6b53daf2014-07-23 16:09:10 -0700799static int rx_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000800{
WANG Cong6b53daf2014-07-23 16:09:10 -0700801 struct netdev_rx_queue *queue = dev->_rx + index;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000802 struct kobject *kobj = &queue->kobj;
803 int error = 0;
804
WANG Cong6b53daf2014-07-23 16:09:10 -0700805 kobj->kset = dev->queues_kset;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000806 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
807 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800808 if (error)
809 goto exit;
810
WANG Cong6b53daf2014-07-23 16:09:10 -0700811 if (dev->sysfs_rx_queue_group) {
812 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
Michael Daltona953be52014-01-16 22:23:28 -0800813 if (error)
814 goto exit;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000815 }
816
817 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000818 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000819
820 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800821exit:
822 kobject_put(kobj);
823 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000824}
Paul Bolle80dd6ea2014-02-09 14:07:11 +0100825#endif /* CONFIG_SYSFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000826
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000827int
WANG Cong6b53daf2014-07-23 16:09:10 -0700828net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000829{
Michael Daltona953be52014-01-16 22:23:28 -0800830#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000831 int i;
832 int error = 0;
833
Michael Daltona953be52014-01-16 22:23:28 -0800834#ifndef CONFIG_RPS
WANG Cong6b53daf2014-07-23 16:09:10 -0700835 if (!dev->sysfs_rx_queue_group)
Michael Daltona953be52014-01-16 22:23:28 -0800836 return 0;
837#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000838 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700839 error = rx_queue_add_kobject(dev, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000840 if (error) {
841 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000842 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000843 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000844 }
845
Michael Daltona953be52014-01-16 22:23:28 -0800846 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700847 if (dev->sysfs_rx_queue_group)
848 sysfs_remove_group(&dev->_rx[i].kobj,
849 dev->sysfs_rx_queue_group);
850 kobject_put(&dev->_rx[i].kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800851 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000852
853 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000854#else
855 return 0;
856#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000857}
858
david decotignyccf5ff62011-11-16 12:15:10 +0000859#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000860/*
861 * netdev_queue sysfs structures and functions.
862 */
863struct netdev_queue_attribute {
864 struct attribute attr;
865 ssize_t (*show)(struct netdev_queue *queue,
866 struct netdev_queue_attribute *attr, char *buf);
867 ssize_t (*store)(struct netdev_queue *queue,
868 struct netdev_queue_attribute *attr, const char *buf, size_t len);
869};
870#define to_netdev_queue_attr(_attr) container_of(_attr, \
871 struct netdev_queue_attribute, attr)
872
873#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
874
875static ssize_t netdev_queue_attr_show(struct kobject *kobj,
876 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000877{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000878 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
879 struct netdev_queue *queue = to_netdev_queue(kobj);
880
881 if (!attribute->show)
882 return -EIO;
883
884 return attribute->show(queue, attribute, buf);
885}
886
887static ssize_t netdev_queue_attr_store(struct kobject *kobj,
888 struct attribute *attr,
889 const char *buf, size_t count)
890{
891 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
892 struct netdev_queue *queue = to_netdev_queue(kobj);
893
894 if (!attribute->store)
895 return -EIO;
896
897 return attribute->store(queue, attribute, buf, count);
898}
899
900static const struct sysfs_ops netdev_queue_sysfs_ops = {
901 .show = netdev_queue_attr_show,
902 .store = netdev_queue_attr_store,
903};
904
david decotignyccf5ff62011-11-16 12:15:10 +0000905static ssize_t show_trans_timeout(struct netdev_queue *queue,
906 struct netdev_queue_attribute *attribute,
907 char *buf)
908{
909 unsigned long trans_timeout;
910
911 spin_lock_irq(&queue->_xmit_lock);
912 trans_timeout = queue->trans_timeout;
913 spin_unlock_irq(&queue->_xmit_lock);
914
915 return sprintf(buf, "%lu", trans_timeout);
916}
917
918static struct netdev_queue_attribute queue_trans_timeout =
919 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
920
Tom Herbert114cf582011-11-28 16:33:09 +0000921#ifdef CONFIG_BQL
922/*
923 * Byte queue limits sysfs structures and functions.
924 */
925static ssize_t bql_show(char *buf, unsigned int value)
926{
927 return sprintf(buf, "%u\n", value);
928}
929
930static ssize_t bql_set(const char *buf, const size_t count,
931 unsigned int *pvalue)
932{
933 unsigned int value;
934 int err;
935
936 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
937 value = DQL_MAX_LIMIT;
938 else {
939 err = kstrtouint(buf, 10, &value);
940 if (err < 0)
941 return err;
942 if (value > DQL_MAX_LIMIT)
943 return -EINVAL;
944 }
945
946 *pvalue = value;
947
948 return count;
949}
950
951static ssize_t bql_show_hold_time(struct netdev_queue *queue,
952 struct netdev_queue_attribute *attr,
953 char *buf)
954{
955 struct dql *dql = &queue->dql;
956
957 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
958}
959
960static ssize_t bql_set_hold_time(struct netdev_queue *queue,
961 struct netdev_queue_attribute *attribute,
962 const char *buf, size_t len)
963{
964 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000965 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000966 int err;
967
968 err = kstrtouint(buf, 10, &value);
969 if (err < 0)
970 return err;
971
972 dql->slack_hold_time = msecs_to_jiffies(value);
973
974 return len;
975}
976
977static struct netdev_queue_attribute bql_hold_time_attribute =
978 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
979 bql_set_hold_time);
980
981static ssize_t bql_show_inflight(struct netdev_queue *queue,
982 struct netdev_queue_attribute *attr,
983 char *buf)
984{
985 struct dql *dql = &queue->dql;
986
987 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
988}
989
990static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +0000991 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +0000992
993#define BQL_ATTR(NAME, FIELD) \
994static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
995 struct netdev_queue_attribute *attr, \
996 char *buf) \
997{ \
998 return bql_show(buf, queue->dql.FIELD); \
999} \
1000 \
1001static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1002 struct netdev_queue_attribute *attr, \
1003 const char *buf, size_t len) \
1004{ \
1005 return bql_set(buf, len, &queue->dql.FIELD); \
1006} \
1007 \
1008static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
1009 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
1010 bql_set_ ## NAME);
1011
1012BQL_ATTR(limit, limit)
1013BQL_ATTR(limit_max, max_limit)
1014BQL_ATTR(limit_min, min_limit)
1015
1016static struct attribute *dql_attrs[] = {
1017 &bql_limit_attribute.attr,
1018 &bql_limit_max_attribute.attr,
1019 &bql_limit_min_attribute.attr,
1020 &bql_hold_time_attribute.attr,
1021 &bql_inflight_attribute.attr,
1022 NULL
1023};
1024
1025static struct attribute_group dql_group = {
1026 .name = "byte_queue_limits",
1027 .attrs = dql_attrs,
1028};
1029#endif /* CONFIG_BQL */
1030
david decotignyccf5ff62011-11-16 12:15:10 +00001031#ifdef CONFIG_XPS
Eric Dumazeted1acc82014-02-13 10:07:13 -08001032static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001033{
1034 struct net_device *dev = queue->dev;
Eric Dumazeted1acc82014-02-13 10:07:13 -08001035 unsigned int i;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001036
Eric Dumazeted1acc82014-02-13 10:07:13 -08001037 i = queue - dev->_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001038 BUG_ON(i >= dev->num_tx_queues);
1039
1040 return i;
1041}
1042
1043
1044static ssize_t show_xps_map(struct netdev_queue *queue,
1045 struct netdev_queue_attribute *attribute, char *buf)
1046{
1047 struct net_device *dev = queue->dev;
1048 struct xps_dev_maps *dev_maps;
1049 cpumask_var_t mask;
1050 unsigned long index;
1051 size_t len = 0;
1052 int i;
1053
1054 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1055 return -ENOMEM;
1056
1057 index = get_netdev_queue_index(queue);
1058
1059 rcu_read_lock();
1060 dev_maps = rcu_dereference(dev->xps_maps);
1061 if (dev_maps) {
1062 for_each_possible_cpu(i) {
1063 struct xps_map *map =
1064 rcu_dereference(dev_maps->cpu_map[i]);
1065 if (map) {
1066 int j;
1067 for (j = 0; j < map->len; j++) {
1068 if (map->queues[j] == index) {
1069 cpumask_set_cpu(i, mask);
1070 break;
1071 }
1072 }
1073 }
1074 }
1075 }
1076 rcu_read_unlock();
1077
1078 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1079 if (PAGE_SIZE - len < 3) {
1080 free_cpumask_var(mask);
1081 return -EINVAL;
1082 }
1083
1084 free_cpumask_var(mask);
1085 len += sprintf(buf + len, "\n");
1086 return len;
1087}
1088
Tom Herbert1d24eb42010-11-21 13:17:27 +00001089static ssize_t store_xps_map(struct netdev_queue *queue,
1090 struct netdev_queue_attribute *attribute,
1091 const char *buf, size_t len)
1092{
1093 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001094 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001095 cpumask_var_t mask;
1096 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001097
1098 if (!capable(CAP_NET_ADMIN))
1099 return -EPERM;
1100
1101 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1102 return -ENOMEM;
1103
1104 index = get_netdev_queue_index(queue);
1105
1106 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1107 if (err) {
1108 free_cpumask_var(mask);
1109 return err;
1110 }
1111
Alexander Duyck537c00d2013-01-10 08:57:02 +00001112 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001113
1114 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001115
Alexander Duyck537c00d2013-01-10 08:57:02 +00001116 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001117}
1118
1119static struct netdev_queue_attribute xps_cpus_attribute =
1120 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001121#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001122
1123static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001124 &queue_trans_timeout.attr,
1125#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001126 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001127#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001128 NULL
1129};
1130
1131static void netdev_queue_release(struct kobject *kobj)
1132{
1133 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001134
Tom Herbert1d24eb42010-11-21 13:17:27 +00001135 memset(kobj, 0, sizeof(*kobj));
1136 dev_put(queue->dev);
1137}
1138
Weilong Chen82ef3d52014-01-16 17:24:31 +08001139static const void *netdev_queue_namespace(struct kobject *kobj)
1140{
1141 struct netdev_queue *queue = to_netdev_queue(kobj);
1142 struct device *dev = &queue->dev->dev;
1143 const void *ns = NULL;
1144
1145 if (dev->class && dev->class->ns_type)
1146 ns = dev->class->namespace(dev);
1147
1148 return ns;
1149}
1150
Tom Herbert1d24eb42010-11-21 13:17:27 +00001151static struct kobj_type netdev_queue_ktype = {
1152 .sysfs_ops = &netdev_queue_sysfs_ops,
1153 .release = netdev_queue_release,
1154 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001155 .namespace = netdev_queue_namespace,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001156};
1157
WANG Cong6b53daf2014-07-23 16:09:10 -07001158static int netdev_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001159{
WANG Cong6b53daf2014-07-23 16:09:10 -07001160 struct netdev_queue *queue = dev->_tx + index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001161 struct kobject *kobj = &queue->kobj;
1162 int error = 0;
1163
WANG Cong6b53daf2014-07-23 16:09:10 -07001164 kobj->kset = dev->queues_kset;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001165 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1166 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001167 if (error)
1168 goto exit;
1169
1170#ifdef CONFIG_BQL
1171 error = sysfs_create_group(kobj, &dql_group);
1172 if (error)
1173 goto exit;
1174#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001175
1176 kobject_uevent(kobj, KOBJ_ADD);
1177 dev_hold(queue->dev);
1178
Tom Herbert114cf582011-11-28 16:33:09 +00001179 return 0;
1180exit:
1181 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001182 return error;
1183}
david decotignyccf5ff62011-11-16 12:15:10 +00001184#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001185
1186int
WANG Cong6b53daf2014-07-23 16:09:10 -07001187netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001188{
david decotignyccf5ff62011-11-16 12:15:10 +00001189#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001190 int i;
1191 int error = 0;
1192
1193 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001194 error = netdev_queue_add_kobject(dev, i);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001195 if (error) {
1196 new_num = old_num;
1197 break;
1198 }
1199 }
1200
Tom Herbert114cf582011-11-28 16:33:09 +00001201 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001202 struct netdev_queue *queue = dev->_tx + i;
Tom Herbert114cf582011-11-28 16:33:09 +00001203
1204#ifdef CONFIG_BQL
1205 sysfs_remove_group(&queue->kobj, &dql_group);
1206#endif
1207 kobject_put(&queue->kobj);
1208 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001209
1210 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001211#else
1212 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001213#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001214}
1215
WANG Cong6b53daf2014-07-23 16:09:10 -07001216static int register_queue_kobjects(struct net_device *dev)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001217{
Tom Herbertbf264142010-11-26 08:36:09 +00001218 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001219
david decotignyccf5ff62011-11-16 12:15:10 +00001220#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001221 dev->queues_kset = kset_create_and_add("queues",
1222 NULL, &dev->dev.kobj);
1223 if (!dev->queues_kset)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001224 return -ENOMEM;
WANG Cong6b53daf2014-07-23 16:09:10 -07001225 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001226#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001227 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001228
WANG Cong6b53daf2014-07-23 16:09:10 -07001229 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001230 if (error)
1231 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001232 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001233
WANG Cong6b53daf2014-07-23 16:09:10 -07001234 error = netdev_queue_update_kobjects(dev, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001235 if (error)
1236 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001237 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001238
1239 return 0;
1240
1241error:
WANG Cong6b53daf2014-07-23 16:09:10 -07001242 netdev_queue_update_kobjects(dev, txq, 0);
1243 net_rx_queue_update_kobjects(dev, rxq, 0);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001244 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001245}
1246
WANG Cong6b53daf2014-07-23 16:09:10 -07001247static void remove_queue_kobjects(struct net_device *dev)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001248{
Tom Herbertbf264142010-11-26 08:36:09 +00001249 int real_rx = 0, real_tx = 0;
1250
Michael Daltona953be52014-01-16 22:23:28 -08001251#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001252 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001253#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001254 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001255
WANG Cong6b53daf2014-07-23 16:09:10 -07001256 net_rx_queue_update_kobjects(dev, real_rx, 0);
1257 netdev_queue_update_kobjects(dev, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001258#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001259 kset_unregister(dev->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001260#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001261}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001262
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001263static bool net_current_may_mount(void)
1264{
1265 struct net *net = current->nsproxy->net_ns;
1266
1267 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1268}
1269
Al Viroa685e082011-06-08 21:13:01 -04001270static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001271{
Al Viroa685e082011-06-08 21:13:01 -04001272 struct net *ns = current->nsproxy->net_ns;
1273#ifdef CONFIG_NET_NS
1274 if (ns)
1275 atomic_inc(&ns->passive);
1276#endif
1277 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001278}
1279
1280static const void *net_initial_ns(void)
1281{
1282 return &init_net;
1283}
1284
1285static const void *net_netlink_ns(struct sock *sk)
1286{
1287 return sock_net(sk);
1288}
1289
Johannes Berg04600792010-08-05 17:45:15 +02001290struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001291 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001292 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001293 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001294 .netlink_ns = net_netlink_ns,
1295 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001296 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001297};
Johannes Berg04600792010-08-05 17:45:15 +02001298EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001299
Kay Sievers7eff2e72007-08-14 15:15:12 +02001300static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001302 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001303 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Kay Sievers312c0042005-11-16 09:00:00 +01001305 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001306 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001307 if (retval)
1308 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001310 /* pass ifindex to uevent.
1311 * ifindex is useful as it won't change (interface name may change)
1312 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001313 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001314
Eric Rannaudbf624562007-03-30 22:23:12 -07001315exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001316 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001320 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001321 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001323static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001325 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 BUG_ON(dev->reg_state != NETREG_RELEASED);
1328
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001329 kfree(dev->ifalias);
Eric Dumazet74d332c2013-10-30 13:10:44 -07001330 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001333static const void *net_namespace(struct device *d)
1334{
1335 struct net_device *dev;
1336 dev = container_of(d, struct net_device, dev);
1337 return dev_net(dev);
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static struct class net_class = {
1341 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001342 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001343 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001344 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001345 .ns_type = &net_ns_type_operations,
1346 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347};
1348
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001349/* Delete sysfs entries but hold kobject reference until after all
1350 * netdev references are gone.
1351 */
WANG Cong6b53daf2014-07-23 16:09:10 -07001352void netdev_unregister_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
WANG Cong6b53daf2014-07-23 16:09:10 -07001354 struct device *dev = &(ndev->dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001355
1356 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001357
WANG Cong6b53daf2014-07-23 16:09:10 -07001358 remove_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001359
Ming Lei9802c8e2013-02-22 16:34:16 -08001360 pm_runtime_set_memalloc_noio(dev, false);
1361
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001362 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
1365/* Create sysfs entries for network device. */
WANG Cong6b53daf2014-07-23 16:09:10 -07001366int netdev_register_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
WANG Cong6b53daf2014-07-23 16:09:10 -07001368 struct device *dev = &(ndev->dev);
1369 const struct attribute_group **groups = ndev->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001370 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001372 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001373 dev->class = &net_class;
WANG Cong6b53daf2014-07-23 16:09:10 -07001374 dev->platform_data = ndev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
WANG Cong6b53daf2014-07-23 16:09:10 -07001377 dev_set_name(dev, "%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001379#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001380 /* Allow for a device specific group */
1381 if (*groups)
1382 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001384 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001385
1386#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
WANG Cong6b53daf2014-07-23 16:09:10 -07001387 if (ndev->ieee80211_ptr)
Johannes Berg38c1a012012-11-16 20:46:19 +01001388 *groups++ = &wireless_group;
1389#if IS_ENABLED(CONFIG_WIRELESS_EXT)
WANG Cong6b53daf2014-07-23 16:09:10 -07001390 else if (ndev->wireless_handlers)
Johannes Berg38c1a012012-11-16 20:46:19 +01001391 *groups++ = &wireless_group;
1392#endif
1393#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001394#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Tom Herbert0a9627f2010-03-16 08:03:29 +00001396 error = device_add(dev);
1397 if (error)
1398 return error;
1399
WANG Cong6b53daf2014-07-23 16:09:10 -07001400 error = register_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001401 if (error) {
1402 device_del(dev);
1403 return error;
1404 }
1405
Ming Lei9802c8e2013-02-22 16:34:16 -08001406 pm_runtime_set_memalloc_noio(dev, true);
1407
Tom Herbert0a9627f2010-03-16 08:03:29 +00001408 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Tejun Heo58292cbe2013-09-11 22:29:04 -04001411int netdev_class_create_file_ns(struct class_attribute *class_attr,
1412 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001413{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001414 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001415}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001416EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001417
Tejun Heo58292cbe2013-09-11 22:29:04 -04001418void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1419 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001420{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001421 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001422}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001423EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001424
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001425int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001427 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return class_register(&net_class);
1429}