blob: 7eeadeecc5a22ec9d4dd38c177f639a48851032c [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 Decotigny8ae6daca2011-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{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070046 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t ret = -EINVAL;
48
49 read_lock(&dev_base_lock);
50 if (dev_isalive(net))
51 ret = (*format)(net, buf);
52 read_unlock(&dev_base_lock);
53
54 return ret;
55}
56
57/* generate a show function for simple field */
58#define NETDEVICE_SHOW(field, format_string) \
59static ssize_t format_##field(const struct net_device *net, char *buf) \
60{ \
61 return sprintf(buf, format_string, net->field); \
62} \
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);
107NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
108NETDEVICE_SHOW_RO(addr_len, fmt_dec);
109NETDEVICE_SHOW_RO(iflink, fmt_dec);
110NETDEVICE_SHOW_RO(ifindex, fmt_dec);
111NETDEVICE_SHOW_RO(type, fmt_dec);
112NETDEVICE_SHOW_RO(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700115static ssize_t address_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700116 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct net_device *net = to_net_dev(dev);
119 ssize_t ret = -EINVAL;
120
121 read_lock(&dev_base_lock);
122 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800123 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 read_unlock(&dev_base_lock);
125 return ret;
126}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700127static DEVICE_ATTR_RO(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700129static ssize_t broadcast_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct net_device *net = to_net_dev(dev);
133 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800134 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EINVAL;
136}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700137static DEVICE_ATTR_RO(broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000139static int change_carrier(struct net_device *net, unsigned long new_carrier)
140{
141 if (!netif_running(net))
142 return -EINVAL;
143 return dev_change_carrier(net, (bool) new_carrier);
144}
145
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700146static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t len)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000148{
149 return netdev_store(dev, attr, buf, len, change_carrier);
150}
151
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700152static ssize_t carrier_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700153 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct net_device *netdev = to_net_dev(dev);
156 if (netif_running(netdev)) {
157 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
158 }
159 return -EINVAL;
160}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700161static DEVICE_ATTR_RW(carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700163static ssize_t speed_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000164 struct device_attribute *attr, char *buf)
165{
166 struct net_device *netdev = to_net_dev(dev);
167 int ret = -EINVAL;
168
169 if (!rtnl_trylock())
170 return restart_syscall();
171
David Decotigny8ae6daca2011-04-27 18:32:38 +0000172 if (netif_running(netdev)) {
173 struct ethtool_cmd cmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000174 if (!__ethtool_get_settings(netdev, &cmd))
David Decotigny8ae6daca2011-04-27 18:32:38 +0000175 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
Andy Gospodarekd519e172009-10-02 09:26:12 +0000176 }
177 rtnl_unlock();
178 return ret;
179}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700180static DEVICE_ATTR_RO(speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000181
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700182static ssize_t duplex_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000183 struct device_attribute *attr, char *buf)
184{
185 struct net_device *netdev = to_net_dev(dev);
186 int ret = -EINVAL;
187
188 if (!rtnl_trylock())
189 return restart_syscall();
190
David Decotigny8ae6daca2011-04-27 18:32:38 +0000191 if (netif_running(netdev)) {
192 struct ethtool_cmd cmd;
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000193 if (!__ethtool_get_settings(netdev, &cmd)) {
194 const char *duplex;
195 switch (cmd.duplex) {
196 case DUPLEX_HALF:
197 duplex = "half";
198 break;
199 case DUPLEX_FULL:
200 duplex = "full";
201 break;
202 default:
203 duplex = "unknown";
204 break;
205 }
206 ret = sprintf(buf, "%s\n", duplex);
207 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000208 }
209 rtnl_unlock();
210 return ret;
211}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700212static DEVICE_ATTR_RO(duplex);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000213
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700214static ssize_t dormant_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700215 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800216{
217 struct net_device *netdev = to_net_dev(dev);
218
219 if (netif_running(netdev))
220 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
221
222 return -EINVAL;
223}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700224static DEVICE_ATTR_RO(dormant);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800225
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700226static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800227 "unknown",
228 "notpresent", /* currently unused */
229 "down",
230 "lowerlayerdown",
231 "testing", /* currently unused */
232 "dormant",
233 "up"
234};
235
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700236static ssize_t operstate_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700237 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800238{
239 const struct net_device *netdev = to_net_dev(dev);
240 unsigned char operstate;
241
242 read_lock(&dev_base_lock);
243 operstate = netdev->operstate;
244 if (!netif_running(netdev))
245 operstate = IF_OPER_DOWN;
246 read_unlock(&dev_base_lock);
247
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700248 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800249 return -EINVAL; /* should not happen */
250
251 return sprintf(buf, "%s\n", operstates[operstate]);
252}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700253static DEVICE_ATTR_RO(operstate);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* read-write attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257static int change_mtu(struct net_device *net, unsigned long new_mtu)
258{
259 return dev_set_mtu(net, (int) new_mtu);
260}
261
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700262static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700263 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700265 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700267NETDEVICE_SHOW_RW(mtu, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269static int change_flags(struct net_device *net, unsigned long new_flags)
270{
Eric Dumazet95c96172012-04-15 05:58:06 +0000271 return dev_change_flags(net, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700274static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700275 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700277 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700279NETDEVICE_SHOW_RW(flags, fmt_hex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
282{
283 net->tx_queue_len = new_len;
284 return 0;
285}
286
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700287static ssize_t tx_queue_len_store(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700288 struct device_attribute *attr,
289 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000291 if (!capable(CAP_NET_ADMIN))
292 return -EPERM;
293
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700294 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700296NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700298static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700299 const char *buf, size_t len)
300{
301 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000302 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700303 size_t count = len;
304 ssize_t ret;
305
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000306 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700307 return -EPERM;
308
309 /* ignore trailing newline */
310 if (len > 0 && buf[len - 1] == '\n')
311 --count;
312
Eric W. Biederman336ca572009-05-13 16:57:25 +0000313 if (!rtnl_trylock())
314 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700315 ret = dev_set_alias(netdev, buf, count);
316 rtnl_unlock();
317
318 return ret < 0 ? ret : len;
319}
320
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700321static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700322 struct device_attribute *attr, char *buf)
323{
324 const struct net_device *netdev = to_net_dev(dev);
325 ssize_t ret = 0;
326
Eric W. Biederman336ca572009-05-13 16:57:25 +0000327 if (!rtnl_trylock())
328 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700329 if (netdev->ifalias)
330 ret = sprintf(buf, "%s\n", netdev->ifalias);
331 rtnl_unlock();
332 return ret;
333}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700334static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000335
336static int change_group(struct net_device *net, unsigned long new_group)
337{
338 dev_set_group(net, (int) new_group);
339 return 0;
340}
341
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700342static ssize_t group_store(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000344{
345 return netdev_store(dev, attr, buf, len, change_group);
346}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700347NETDEVICE_SHOW(group, fmt_dec);
348static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000349
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700350static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200351 struct device_attribute *attr, char *buf)
352{
353 struct net_device *netdev = to_net_dev(dev);
354 ssize_t ret = -EINVAL;
355
356 if (!rtnl_trylock())
357 return restart_syscall();
358
359 if (dev_isalive(netdev)) {
360 struct netdev_phys_port_id ppid;
361
362 ret = dev_get_phys_port_id(netdev, &ppid);
363 if (!ret)
364 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
365 }
366 rtnl_unlock();
367
368 return ret;
369}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700370static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200371
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700372static struct attribute *net_class_attrs[] = {
373 &dev_attr_netdev_group.attr,
374 &dev_attr_type.attr,
375 &dev_attr_dev_id.attr,
376 &dev_attr_iflink.attr,
377 &dev_attr_ifindex.attr,
378 &dev_attr_addr_assign_type.attr,
379 &dev_attr_addr_len.attr,
380 &dev_attr_link_mode.attr,
381 &dev_attr_address.attr,
382 &dev_attr_broadcast.attr,
383 &dev_attr_speed.attr,
384 &dev_attr_duplex.attr,
385 &dev_attr_dormant.attr,
386 &dev_attr_operstate.attr,
387 &dev_attr_ifalias.attr,
388 &dev_attr_carrier.attr,
389 &dev_attr_mtu.attr,
390 &dev_attr_flags.attr,
391 &dev_attr_tx_queue_len.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700392 &dev_attr_phys_port_id.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700393 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700395ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700398static ssize_t netstat_show(const struct device *d,
399 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 unsigned long offset)
401{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700402 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ssize_t ret = -EINVAL;
404
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000405 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
406 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700409 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700410 struct rtnl_link_stats64 temp;
411 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
412
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000413 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 read_unlock(&dev_base_lock);
416 return ret;
417}
418
419/* generate a read-only statistics attribute */
420#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700421static ssize_t name##_show(struct device *d, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700422 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700424 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000425 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700427static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429NETSTAT_ENTRY(rx_packets);
430NETSTAT_ENTRY(tx_packets);
431NETSTAT_ENTRY(rx_bytes);
432NETSTAT_ENTRY(tx_bytes);
433NETSTAT_ENTRY(rx_errors);
434NETSTAT_ENTRY(tx_errors);
435NETSTAT_ENTRY(rx_dropped);
436NETSTAT_ENTRY(tx_dropped);
437NETSTAT_ENTRY(multicast);
438NETSTAT_ENTRY(collisions);
439NETSTAT_ENTRY(rx_length_errors);
440NETSTAT_ENTRY(rx_over_errors);
441NETSTAT_ENTRY(rx_crc_errors);
442NETSTAT_ENTRY(rx_frame_errors);
443NETSTAT_ENTRY(rx_fifo_errors);
444NETSTAT_ENTRY(rx_missed_errors);
445NETSTAT_ENTRY(tx_aborted_errors);
446NETSTAT_ENTRY(tx_carrier_errors);
447NETSTAT_ENTRY(tx_fifo_errors);
448NETSTAT_ENTRY(tx_heartbeat_errors);
449NETSTAT_ENTRY(tx_window_errors);
450NETSTAT_ENTRY(rx_compressed);
451NETSTAT_ENTRY(tx_compressed);
452
453static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454 &dev_attr_rx_packets.attr,
455 &dev_attr_tx_packets.attr,
456 &dev_attr_rx_bytes.attr,
457 &dev_attr_tx_bytes.attr,
458 &dev_attr_rx_errors.attr,
459 &dev_attr_tx_errors.attr,
460 &dev_attr_rx_dropped.attr,
461 &dev_attr_tx_dropped.attr,
462 &dev_attr_multicast.attr,
463 &dev_attr_collisions.attr,
464 &dev_attr_rx_length_errors.attr,
465 &dev_attr_rx_over_errors.attr,
466 &dev_attr_rx_crc_errors.attr,
467 &dev_attr_rx_frame_errors.attr,
468 &dev_attr_rx_fifo_errors.attr,
469 &dev_attr_rx_missed_errors.attr,
470 &dev_attr_tx_aborted_errors.attr,
471 &dev_attr_tx_carrier_errors.attr,
472 &dev_attr_tx_fifo_errors.attr,
473 &dev_attr_tx_heartbeat_errors.attr,
474 &dev_attr_tx_window_errors.attr,
475 &dev_attr_rx_compressed.attr,
476 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 NULL
478};
479
480
481static struct attribute_group netstat_group = {
482 .name = "statistics",
483 .attrs = netstat_attrs,
484};
Johannes Berg38c1a012012-11-16 20:46:19 +0100485
486#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
487static struct attribute *wireless_attrs[] = {
488 NULL
489};
490
491static struct attribute_group wireless_group = {
492 .name = "wireless",
493 .attrs = wireless_attrs,
494};
495#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700496
497#else /* CONFIG_SYSFS */
498#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700499#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Michael Daltona953be52014-01-16 22:23:28 -0800501#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000502#define to_rx_queue_attr(_attr) container_of(_attr, \
503 struct rx_queue_attribute, attr)
504
505#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
506
507static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
508 char *buf)
509{
510 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
511 struct netdev_rx_queue *queue = to_rx_queue(kobj);
512
513 if (!attribute->show)
514 return -EIO;
515
516 return attribute->show(queue, attribute, buf);
517}
518
519static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
520 const char *buf, size_t count)
521{
522 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
523 struct netdev_rx_queue *queue = to_rx_queue(kobj);
524
525 if (!attribute->store)
526 return -EIO;
527
528 return attribute->store(queue, attribute, buf, count);
529}
530
stephen hemmingerfa50d642010-08-31 12:14:13 +0000531static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000532 .show = rx_queue_attr_show,
533 .store = rx_queue_attr_store,
534};
535
Michael Daltona953be52014-01-16 22:23:28 -0800536#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000537static ssize_t show_rps_map(struct netdev_rx_queue *queue,
538 struct rx_queue_attribute *attribute, char *buf)
539{
540 struct rps_map *map;
541 cpumask_var_t mask;
542 size_t len = 0;
543 int i;
544
545 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
546 return -ENOMEM;
547
548 rcu_read_lock();
549 map = rcu_dereference(queue->rps_map);
550 if (map)
551 for (i = 0; i < map->len; i++)
552 cpumask_set_cpu(map->cpus[i], mask);
553
554 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
555 if (PAGE_SIZE - len < 3) {
556 rcu_read_unlock();
557 free_cpumask_var(mask);
558 return -EINVAL;
559 }
560 rcu_read_unlock();
561
562 free_cpumask_var(mask);
563 len += sprintf(buf + len, "\n");
564 return len;
565}
566
Eric Dumazetf5acb902010-04-19 14:40:57 -0700567static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000568 struct rx_queue_attribute *attribute,
569 const char *buf, size_t len)
570{
571 struct rps_map *old_map, *map;
572 cpumask_var_t mask;
573 int err, cpu, i;
574 static DEFINE_SPINLOCK(rps_map_lock);
575
576 if (!capable(CAP_NET_ADMIN))
577 return -EPERM;
578
579 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
580 return -ENOMEM;
581
582 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
583 if (err) {
584 free_cpumask_var(mask);
585 return err;
586 }
587
Eric Dumazet95c96172012-04-15 05:58:06 +0000588 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000589 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
590 GFP_KERNEL);
591 if (!map) {
592 free_cpumask_var(mask);
593 return -ENOMEM;
594 }
595
596 i = 0;
597 for_each_cpu_and(cpu, mask, cpu_online_mask)
598 map->cpus[i++] = cpu;
599
600 if (i)
601 map->len = i;
602 else {
603 kfree(map);
604 map = NULL;
605 }
606
607 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000608 old_map = rcu_dereference_protected(queue->rps_map,
609 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000610 rcu_assign_pointer(queue->rps_map, map);
611 spin_unlock(&rps_map_lock);
612
Eric Dumazetadc93002011-11-17 03:13:26 +0000613 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100614 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000615 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800616 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100617 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000618 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000619 free_cpumask_var(mask);
620 return len;
621}
622
Tom Herbertfec5e652010-04-16 16:01:27 -0700623static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
624 struct rx_queue_attribute *attr,
625 char *buf)
626{
627 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000628 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700629
630 rcu_read_lock();
631 flow_table = rcu_dereference(queue->rps_flow_table);
632 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000633 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700634 rcu_read_unlock();
635
Eric Dumazet60b778c2011-12-24 06:56:49 +0000636 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700637}
638
Tom Herbertfec5e652010-04-16 16:01:27 -0700639static void rps_dev_flow_table_release(struct rcu_head *rcu)
640{
641 struct rps_dev_flow_table *table = container_of(rcu,
642 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000643 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700644}
645
Eric Dumazetf5acb902010-04-19 14:40:57 -0700646static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700647 struct rx_queue_attribute *attr,
648 const char *buf, size_t len)
649{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000650 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700651 struct rps_dev_flow_table *table, *old_table;
652 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000653 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700654
655 if (!capable(CAP_NET_ADMIN))
656 return -EPERM;
657
Eric Dumazet60b778c2011-12-24 06:56:49 +0000658 rc = kstrtoul(buf, 0, &count);
659 if (rc < 0)
660 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700661
662 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000663 mask = count - 1;
664 /* mask = roundup_pow_of_two(count) - 1;
665 * without overflows...
666 */
667 while ((mask | (mask >> 1)) != mask)
668 mask |= (mask >> 1);
669 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800670 * and on 32bit arches, must check
671 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000672 */
673#if BITS_PER_LONG > 32
674 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000675 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000676#else
677 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000678 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700679 /* Enforce a limit to prevent overflow */
680 return -EINVAL;
681 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000682#endif
683 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700684 if (!table)
685 return -ENOMEM;
686
Eric Dumazet60b778c2011-12-24 06:56:49 +0000687 table->mask = mask;
688 for (count = 0; count <= mask; count++)
689 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700690 } else
691 table = NULL;
692
693 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000694 old_table = rcu_dereference_protected(queue->rps_flow_table,
695 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700696 rcu_assign_pointer(queue->rps_flow_table, table);
697 spin_unlock(&rps_dev_flow_lock);
698
699 if (old_table)
700 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
701
702 return len;
703}
704
Tom Herbert0a9627f2010-03-16 08:03:29 +0000705static struct rx_queue_attribute rps_cpus_attribute =
706 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
707
Tom Herbertfec5e652010-04-16 16:01:27 -0700708
709static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
710 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
711 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800712#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700713
Tom Herbert0a9627f2010-03-16 08:03:29 +0000714static struct attribute *rx_queue_default_attrs[] = {
Michael Daltona953be52014-01-16 22:23:28 -0800715#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000716 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700717 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800718#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000719 NULL
720};
721
722static void rx_queue_release(struct kobject *kobj)
723{
724 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800725#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000726 struct rps_map *map;
727 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000728
Tom Herbertfec5e652010-04-16 16:01:27 -0700729
Eric Dumazet33d480c2011-08-11 19:30:52 +0000730 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000731 if (map) {
732 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800733 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000734 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000735
Eric Dumazet33d480c2011-08-11 19:30:52 +0000736 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000737 if (flow_table) {
738 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000739 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000740 }
Michael Daltona953be52014-01-16 22:23:28 -0800741#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000742
John Fastabend9ea19482010-11-16 06:31:39 +0000743 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000744 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000745}
746
747static struct kobj_type rx_queue_ktype = {
748 .sysfs_ops = &rx_queue_sysfs_ops,
749 .release = rx_queue_release,
750 .default_attrs = rx_queue_default_attrs,
751};
752
753static int rx_queue_add_kobject(struct net_device *net, int index)
754{
755 struct netdev_rx_queue *queue = net->_rx + index;
756 struct kobject *kobj = &queue->kobj;
757 int error = 0;
758
759 kobj->kset = net->queues_kset;
760 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
761 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800762 if (error)
763 goto exit;
764
765 if (net->sysfs_rx_queue_group) {
766 error = sysfs_create_group(kobj, net->sysfs_rx_queue_group);
767 if (error)
768 goto exit;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000769 }
770
771 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000772 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000773
774 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800775exit:
776 kobject_put(kobj);
777 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000778}
Michael Daltona953be52014-01-16 22:23:28 -0800779#endif /* CONFIG_SYFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000780
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000781int
782net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000783{
Michael Daltona953be52014-01-16 22:23:28 -0800784#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000785 int i;
786 int error = 0;
787
Michael Daltona953be52014-01-16 22:23:28 -0800788#ifndef CONFIG_RPS
789 if (!net->sysfs_rx_queue_group)
790 return 0;
791#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000792 for (i = old_num; i < new_num; i++) {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000793 error = rx_queue_add_kobject(net, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000794 if (error) {
795 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000796 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000797 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000798 }
799
Michael Daltona953be52014-01-16 22:23:28 -0800800 while (--i >= new_num) {
801 if (net->sysfs_rx_queue_group)
802 sysfs_remove_group(&net->_rx[i].kobj,
803 net->sysfs_rx_queue_group);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000804 kobject_put(&net->_rx[i].kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800805 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000806
807 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000808#else
809 return 0;
810#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000811}
812
david decotignyccf5ff62011-11-16 12:15:10 +0000813#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000814/*
815 * netdev_queue sysfs structures and functions.
816 */
817struct netdev_queue_attribute {
818 struct attribute attr;
819 ssize_t (*show)(struct netdev_queue *queue,
820 struct netdev_queue_attribute *attr, char *buf);
821 ssize_t (*store)(struct netdev_queue *queue,
822 struct netdev_queue_attribute *attr, const char *buf, size_t len);
823};
824#define to_netdev_queue_attr(_attr) container_of(_attr, \
825 struct netdev_queue_attribute, attr)
826
827#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
828
829static ssize_t netdev_queue_attr_show(struct kobject *kobj,
830 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000831{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000832 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
833 struct netdev_queue *queue = to_netdev_queue(kobj);
834
835 if (!attribute->show)
836 return -EIO;
837
838 return attribute->show(queue, attribute, buf);
839}
840
841static ssize_t netdev_queue_attr_store(struct kobject *kobj,
842 struct attribute *attr,
843 const char *buf, size_t count)
844{
845 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
846 struct netdev_queue *queue = to_netdev_queue(kobj);
847
848 if (!attribute->store)
849 return -EIO;
850
851 return attribute->store(queue, attribute, buf, count);
852}
853
854static const struct sysfs_ops netdev_queue_sysfs_ops = {
855 .show = netdev_queue_attr_show,
856 .store = netdev_queue_attr_store,
857};
858
david decotignyccf5ff62011-11-16 12:15:10 +0000859static ssize_t show_trans_timeout(struct netdev_queue *queue,
860 struct netdev_queue_attribute *attribute,
861 char *buf)
862{
863 unsigned long trans_timeout;
864
865 spin_lock_irq(&queue->_xmit_lock);
866 trans_timeout = queue->trans_timeout;
867 spin_unlock_irq(&queue->_xmit_lock);
868
869 return sprintf(buf, "%lu", trans_timeout);
870}
871
872static struct netdev_queue_attribute queue_trans_timeout =
873 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
874
Tom Herbert114cf582011-11-28 16:33:09 +0000875#ifdef CONFIG_BQL
876/*
877 * Byte queue limits sysfs structures and functions.
878 */
879static ssize_t bql_show(char *buf, unsigned int value)
880{
881 return sprintf(buf, "%u\n", value);
882}
883
884static ssize_t bql_set(const char *buf, const size_t count,
885 unsigned int *pvalue)
886{
887 unsigned int value;
888 int err;
889
890 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
891 value = DQL_MAX_LIMIT;
892 else {
893 err = kstrtouint(buf, 10, &value);
894 if (err < 0)
895 return err;
896 if (value > DQL_MAX_LIMIT)
897 return -EINVAL;
898 }
899
900 *pvalue = value;
901
902 return count;
903}
904
905static ssize_t bql_show_hold_time(struct netdev_queue *queue,
906 struct netdev_queue_attribute *attr,
907 char *buf)
908{
909 struct dql *dql = &queue->dql;
910
911 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
912}
913
914static ssize_t bql_set_hold_time(struct netdev_queue *queue,
915 struct netdev_queue_attribute *attribute,
916 const char *buf, size_t len)
917{
918 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000919 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000920 int err;
921
922 err = kstrtouint(buf, 10, &value);
923 if (err < 0)
924 return err;
925
926 dql->slack_hold_time = msecs_to_jiffies(value);
927
928 return len;
929}
930
931static struct netdev_queue_attribute bql_hold_time_attribute =
932 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
933 bql_set_hold_time);
934
935static ssize_t bql_show_inflight(struct netdev_queue *queue,
936 struct netdev_queue_attribute *attr,
937 char *buf)
938{
939 struct dql *dql = &queue->dql;
940
941 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
942}
943
944static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +0000945 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +0000946
947#define BQL_ATTR(NAME, FIELD) \
948static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
949 struct netdev_queue_attribute *attr, \
950 char *buf) \
951{ \
952 return bql_show(buf, queue->dql.FIELD); \
953} \
954 \
955static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
956 struct netdev_queue_attribute *attr, \
957 const char *buf, size_t len) \
958{ \
959 return bql_set(buf, len, &queue->dql.FIELD); \
960} \
961 \
962static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
963 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
964 bql_set_ ## NAME);
965
966BQL_ATTR(limit, limit)
967BQL_ATTR(limit_max, max_limit)
968BQL_ATTR(limit_min, min_limit)
969
970static struct attribute *dql_attrs[] = {
971 &bql_limit_attribute.attr,
972 &bql_limit_max_attribute.attr,
973 &bql_limit_min_attribute.attr,
974 &bql_hold_time_attribute.attr,
975 &bql_inflight_attribute.attr,
976 NULL
977};
978
979static struct attribute_group dql_group = {
980 .name = "byte_queue_limits",
981 .attrs = dql_attrs,
982};
983#endif /* CONFIG_BQL */
984
david decotignyccf5ff62011-11-16 12:15:10 +0000985#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000986static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
987{
988 struct net_device *dev = queue->dev;
989 int i;
990
991 for (i = 0; i < dev->num_tx_queues; i++)
992 if (queue == &dev->_tx[i])
993 break;
994
995 BUG_ON(i >= dev->num_tx_queues);
996
997 return i;
998}
999
1000
1001static ssize_t show_xps_map(struct netdev_queue *queue,
1002 struct netdev_queue_attribute *attribute, char *buf)
1003{
1004 struct net_device *dev = queue->dev;
1005 struct xps_dev_maps *dev_maps;
1006 cpumask_var_t mask;
1007 unsigned long index;
1008 size_t len = 0;
1009 int i;
1010
1011 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1012 return -ENOMEM;
1013
1014 index = get_netdev_queue_index(queue);
1015
1016 rcu_read_lock();
1017 dev_maps = rcu_dereference(dev->xps_maps);
1018 if (dev_maps) {
1019 for_each_possible_cpu(i) {
1020 struct xps_map *map =
1021 rcu_dereference(dev_maps->cpu_map[i]);
1022 if (map) {
1023 int j;
1024 for (j = 0; j < map->len; j++) {
1025 if (map->queues[j] == index) {
1026 cpumask_set_cpu(i, mask);
1027 break;
1028 }
1029 }
1030 }
1031 }
1032 }
1033 rcu_read_unlock();
1034
1035 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1036 if (PAGE_SIZE - len < 3) {
1037 free_cpumask_var(mask);
1038 return -EINVAL;
1039 }
1040
1041 free_cpumask_var(mask);
1042 len += sprintf(buf + len, "\n");
1043 return len;
1044}
1045
Tom Herbert1d24eb42010-11-21 13:17:27 +00001046static ssize_t store_xps_map(struct netdev_queue *queue,
1047 struct netdev_queue_attribute *attribute,
1048 const char *buf, size_t len)
1049{
1050 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001051 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001052 cpumask_var_t mask;
1053 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001054
1055 if (!capable(CAP_NET_ADMIN))
1056 return -EPERM;
1057
1058 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1059 return -ENOMEM;
1060
1061 index = get_netdev_queue_index(queue);
1062
1063 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1064 if (err) {
1065 free_cpumask_var(mask);
1066 return err;
1067 }
1068
Alexander Duyck537c00d2013-01-10 08:57:02 +00001069 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001070
1071 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001072
Alexander Duyck537c00d2013-01-10 08:57:02 +00001073 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001074}
1075
1076static struct netdev_queue_attribute xps_cpus_attribute =
1077 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001078#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001079
1080static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001081 &queue_trans_timeout.attr,
1082#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001083 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001084#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001085 NULL
1086};
1087
1088static void netdev_queue_release(struct kobject *kobj)
1089{
1090 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001091
Tom Herbert1d24eb42010-11-21 13:17:27 +00001092 memset(kobj, 0, sizeof(*kobj));
1093 dev_put(queue->dev);
1094}
1095
1096static struct kobj_type netdev_queue_ktype = {
1097 .sysfs_ops = &netdev_queue_sysfs_ops,
1098 .release = netdev_queue_release,
1099 .default_attrs = netdev_queue_default_attrs,
1100};
1101
1102static int netdev_queue_add_kobject(struct net_device *net, int index)
1103{
1104 struct netdev_queue *queue = net->_tx + index;
1105 struct kobject *kobj = &queue->kobj;
1106 int error = 0;
1107
1108 kobj->kset = net->queues_kset;
1109 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1110 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001111 if (error)
1112 goto exit;
1113
1114#ifdef CONFIG_BQL
1115 error = sysfs_create_group(kobj, &dql_group);
1116 if (error)
1117 goto exit;
1118#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001119
1120 kobject_uevent(kobj, KOBJ_ADD);
1121 dev_hold(queue->dev);
1122
Tom Herbert114cf582011-11-28 16:33:09 +00001123 return 0;
1124exit:
1125 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001126 return error;
1127}
david decotignyccf5ff62011-11-16 12:15:10 +00001128#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001129
1130int
1131netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1132{
david decotignyccf5ff62011-11-16 12:15:10 +00001133#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001134 int i;
1135 int error = 0;
1136
1137 for (i = old_num; i < new_num; i++) {
1138 error = netdev_queue_add_kobject(net, i);
1139 if (error) {
1140 new_num = old_num;
1141 break;
1142 }
1143 }
1144
Tom Herbert114cf582011-11-28 16:33:09 +00001145 while (--i >= new_num) {
1146 struct netdev_queue *queue = net->_tx + i;
1147
1148#ifdef CONFIG_BQL
1149 sysfs_remove_group(&queue->kobj, &dql_group);
1150#endif
1151 kobject_put(&queue->kobj);
1152 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001153
1154 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001155#else
1156 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001157#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001158}
1159
1160static int register_queue_kobjects(struct net_device *net)
1161{
Tom Herbertbf264142010-11-26 08:36:09 +00001162 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001163
david decotignyccf5ff62011-11-16 12:15:10 +00001164#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001165 net->queues_kset = kset_create_and_add("queues",
1166 NULL, &net->dev.kobj);
1167 if (!net->queues_kset)
1168 return -ENOMEM;
Tom Herbertbf264142010-11-26 08:36:09 +00001169 real_rx = net->real_num_rx_queues;
1170#endif
1171 real_tx = net->real_num_tx_queues;
1172
1173 error = net_rx_queue_update_kobjects(net, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001174 if (error)
1175 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001176 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001177
Tom Herbertbf264142010-11-26 08:36:09 +00001178 error = netdev_queue_update_kobjects(net, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001179 if (error)
1180 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001181 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001182
1183 return 0;
1184
1185error:
1186 netdev_queue_update_kobjects(net, txq, 0);
1187 net_rx_queue_update_kobjects(net, rxq, 0);
1188 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001189}
1190
Tom Herbert1d24eb42010-11-21 13:17:27 +00001191static void remove_queue_kobjects(struct net_device *net)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001192{
Tom Herbertbf264142010-11-26 08:36:09 +00001193 int real_rx = 0, real_tx = 0;
1194
Michael Daltona953be52014-01-16 22:23:28 -08001195#ifdef CONFIG_SYSFS
Tom Herbertbf264142010-11-26 08:36:09 +00001196 real_rx = net->real_num_rx_queues;
1197#endif
1198 real_tx = net->real_num_tx_queues;
1199
1200 net_rx_queue_update_kobjects(net, real_rx, 0);
1201 netdev_queue_update_kobjects(net, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001202#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +00001203 kset_unregister(net->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001204#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001205}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001206
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001207static bool net_current_may_mount(void)
1208{
1209 struct net *net = current->nsproxy->net_ns;
1210
1211 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1212}
1213
Al Viroa685e082011-06-08 21:13:01 -04001214static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001215{
Al Viroa685e082011-06-08 21:13:01 -04001216 struct net *ns = current->nsproxy->net_ns;
1217#ifdef CONFIG_NET_NS
1218 if (ns)
1219 atomic_inc(&ns->passive);
1220#endif
1221 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001222}
1223
1224static const void *net_initial_ns(void)
1225{
1226 return &init_net;
1227}
1228
1229static const void *net_netlink_ns(struct sock *sk)
1230{
1231 return sock_net(sk);
1232}
1233
Johannes Berg04600792010-08-05 17:45:15 +02001234struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001235 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001236 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001237 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001238 .netlink_ns = net_netlink_ns,
1239 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001240 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001241};
Johannes Berg04600792010-08-05 17:45:15 +02001242EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001243
Kay Sievers7eff2e72007-08-14 15:15:12 +02001244static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001246 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001247 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Kay Sievers312c0042005-11-16 09:00:00 +01001249 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001250 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001251 if (retval)
1252 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001254 /* pass ifindex to uevent.
1255 * ifindex is useful as it won't change (interface name may change)
1256 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001257 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001258
Eric Rannaudbf624562007-03-30 22:23:12 -07001259exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001260 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001264 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001265 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001267static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001269 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 BUG_ON(dev->reg_state != NETREG_RELEASED);
1272
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001273 kfree(dev->ifalias);
Eric Dumazet74d332c2013-10-30 13:10:44 -07001274 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001277static const void *net_namespace(struct device *d)
1278{
1279 struct net_device *dev;
1280 dev = container_of(d, struct net_device, dev);
1281 return dev_net(dev);
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static struct class net_class = {
1285 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001286 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001287 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001288 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001289 .ns_type = &net_ns_type_operations,
1290 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291};
1292
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001293/* Delete sysfs entries but hold kobject reference until after all
1294 * netdev references are gone.
1295 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001296void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001298 struct device *dev = &(net->dev);
1299
1300 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001301
Tom Herbert1d24eb42010-11-21 13:17:27 +00001302 remove_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001303
Ming Lei9802c8e2013-02-22 16:34:16 -08001304 pm_runtime_set_memalloc_noio(dev, false);
1305
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001306 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
1309/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001310int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001312 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -07001313 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001314 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001316 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001317 dev->class = &net_class;
1318 dev->platform_data = net;
1319 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Stephen Hemmingera2205472009-03-09 13:51:55 +00001321 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001323#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001324 /* Allow for a device specific group */
1325 if (*groups)
1326 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001328 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001329
1330#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1331 if (net->ieee80211_ptr)
1332 *groups++ = &wireless_group;
1333#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1334 else if (net->wireless_handlers)
1335 *groups++ = &wireless_group;
1336#endif
1337#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001338#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Tom Herbert0a9627f2010-03-16 08:03:29 +00001340 error = device_add(dev);
1341 if (error)
1342 return error;
1343
Tom Herbert1d24eb42010-11-21 13:17:27 +00001344 error = register_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001345 if (error) {
1346 device_del(dev);
1347 return error;
1348 }
1349
Ming Lei9802c8e2013-02-22 16:34:16 -08001350 pm_runtime_set_memalloc_noio(dev, true);
1351
Tom Herbert0a9627f2010-03-16 08:03:29 +00001352 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Tejun Heo58292cbe2013-09-11 22:29:04 -04001355int netdev_class_create_file_ns(struct class_attribute *class_attr,
1356 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001357{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001358 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001359}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001360EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001361
Tejun Heo58292cbe2013-09-11 22:29:04 -04001362void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1363 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001364{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001365 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001366}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001367EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001368
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001369int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001371 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return class_register(&net_class);
1373}