blob: daed9a64c6f6b350ef4518fc91b8b6718d58624d [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{
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);
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
115/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700116static ssize_t address_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700117 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct net_device *net = to_net_dev(dev);
120 ssize_t ret = -EINVAL;
121
122 read_lock(&dev_base_lock);
123 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800124 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 read_unlock(&dev_base_lock);
126 return ret;
127}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700128static DEVICE_ATTR_RO(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700130static ssize_t broadcast_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct net_device *net = to_net_dev(dev);
134 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800135 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EINVAL;
137}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700138static DEVICE_ATTR_RO(broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000140static int change_carrier(struct net_device *net, unsigned long new_carrier)
141{
142 if (!netif_running(net))
143 return -EINVAL;
144 return dev_change_carrier(net, (bool) new_carrier);
145}
146
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700147static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t len)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000149{
150 return netdev_store(dev, attr, buf, len, change_carrier);
151}
152
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700153static ssize_t carrier_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700154 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct net_device *netdev = to_net_dev(dev);
157 if (netif_running(netdev)) {
158 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
159 }
160 return -EINVAL;
161}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700162static DEVICE_ATTR_RW(carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700164static ssize_t speed_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000165 struct device_attribute *attr, char *buf)
166{
167 struct net_device *netdev = to_net_dev(dev);
168 int ret = -EINVAL;
169
170 if (!rtnl_trylock())
171 return restart_syscall();
172
David Decotigny8ae6dac2011-04-27 18:32:38 +0000173 if (netif_running(netdev)) {
174 struct ethtool_cmd cmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000175 if (!__ethtool_get_settings(netdev, &cmd))
David Decotigny8ae6dac2011-04-27 18:32:38 +0000176 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
Andy Gospodarekd519e172009-10-02 09:26:12 +0000177 }
178 rtnl_unlock();
179 return ret;
180}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700181static DEVICE_ATTR_RO(speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000182
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700183static ssize_t duplex_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;
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000194 if (!__ethtool_get_settings(netdev, &cmd)) {
195 const char *duplex;
196 switch (cmd.duplex) {
197 case DUPLEX_HALF:
198 duplex = "half";
199 break;
200 case DUPLEX_FULL:
201 duplex = "full";
202 break;
203 default:
204 duplex = "unknown";
205 break;
206 }
207 ret = sprintf(buf, "%s\n", duplex);
208 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000209 }
210 rtnl_unlock();
211 return ret;
212}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700213static DEVICE_ATTR_RO(duplex);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000214
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700215static ssize_t dormant_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700216 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800217{
218 struct net_device *netdev = to_net_dev(dev);
219
220 if (netif_running(netdev))
221 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
222
223 return -EINVAL;
224}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700225static DEVICE_ATTR_RO(dormant);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800226
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700227static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800228 "unknown",
229 "notpresent", /* currently unused */
230 "down",
231 "lowerlayerdown",
232 "testing", /* currently unused */
233 "dormant",
234 "up"
235};
236
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700237static ssize_t operstate_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700238 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800239{
240 const struct net_device *netdev = to_net_dev(dev);
241 unsigned char operstate;
242
243 read_lock(&dev_base_lock);
244 operstate = netdev->operstate;
245 if (!netif_running(netdev))
246 operstate = IF_OPER_DOWN;
247 read_unlock(&dev_base_lock);
248
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700249 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800250 return -EINVAL; /* should not happen */
251
252 return sprintf(buf, "%s\n", operstates[operstate]);
253}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700254static DEVICE_ATTR_RO(operstate);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/* read-write attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258static int change_mtu(struct net_device *net, unsigned long new_mtu)
259{
260 return dev_set_mtu(net, (int) new_mtu);
261}
262
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700263static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700264 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700266 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700268NETDEVICE_SHOW_RW(mtu, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270static int change_flags(struct net_device *net, unsigned long new_flags)
271{
Eric Dumazet95c96172012-04-15 05:58:06 +0000272 return dev_change_flags(net, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700275static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700276 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700278 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700280NETDEVICE_SHOW_RW(flags, fmt_hex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
283{
284 net->tx_queue_len = new_len;
285 return 0;
286}
287
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700288static ssize_t tx_queue_len_store(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700289 struct device_attribute *attr,
290 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000292 if (!capable(CAP_NET_ADMIN))
293 return -EPERM;
294
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700295 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700297NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700299static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700300 const char *buf, size_t len)
301{
302 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000303 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700304 size_t count = len;
305 ssize_t ret;
306
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000307 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700308 return -EPERM;
309
310 /* ignore trailing newline */
311 if (len > 0 && buf[len - 1] == '\n')
312 --count;
313
Eric W. Biederman336ca572009-05-13 16:57:25 +0000314 if (!rtnl_trylock())
315 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700316 ret = dev_set_alias(netdev, buf, count);
317 rtnl_unlock();
318
319 return ret < 0 ? ret : len;
320}
321
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700322static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700323 struct device_attribute *attr, char *buf)
324{
325 const struct net_device *netdev = to_net_dev(dev);
326 ssize_t ret = 0;
327
Eric W. Biederman336ca572009-05-13 16:57:25 +0000328 if (!rtnl_trylock())
329 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700330 if (netdev->ifalias)
331 ret = sprintf(buf, "%s\n", netdev->ifalias);
332 rtnl_unlock();
333 return ret;
334}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700335static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000336
337static int change_group(struct net_device *net, unsigned long new_group)
338{
339 dev_set_group(net, (int) new_group);
340 return 0;
341}
342
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700343static ssize_t group_store(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000345{
346 return netdev_store(dev, attr, buf, len, change_group);
347}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700348NETDEVICE_SHOW(group, fmt_dec);
349static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000350
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700351static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200352 struct device_attribute *attr, char *buf)
353{
354 struct net_device *netdev = to_net_dev(dev);
355 ssize_t ret = -EINVAL;
356
357 if (!rtnl_trylock())
358 return restart_syscall();
359
360 if (dev_isalive(netdev)) {
361 struct netdev_phys_port_id ppid;
362
363 ret = dev_get_phys_port_id(netdev, &ppid);
364 if (!ret)
365 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
366 }
367 rtnl_unlock();
368
369 return ret;
370}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700371static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200372
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700373static struct attribute *net_class_attrs[] = {
374 &dev_attr_netdev_group.attr,
375 &dev_attr_type.attr,
376 &dev_attr_dev_id.attr,
Amir Vadai3f859442014-02-25 18:17:50 +0200377 &dev_attr_dev_port.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700378 &dev_attr_iflink.attr,
379 &dev_attr_ifindex.attr,
380 &dev_attr_addr_assign_type.attr,
381 &dev_attr_addr_len.attr,
382 &dev_attr_link_mode.attr,
383 &dev_attr_address.attr,
384 &dev_attr_broadcast.attr,
385 &dev_attr_speed.attr,
386 &dev_attr_duplex.attr,
387 &dev_attr_dormant.attr,
388 &dev_attr_operstate.attr,
389 &dev_attr_ifalias.attr,
390 &dev_attr_carrier.attr,
391 &dev_attr_mtu.attr,
392 &dev_attr_flags.attr,
393 &dev_attr_tx_queue_len.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700394 &dev_attr_phys_port_id.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700395 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700397ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t netstat_show(const struct device *d,
401 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unsigned long offset)
403{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ssize_t ret = -EINVAL;
406
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000407 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
408 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700411 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700412 struct rtnl_link_stats64 temp;
413 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
414
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000415 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 read_unlock(&dev_base_lock);
418 return ret;
419}
420
421/* generate a read-only statistics attribute */
422#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700423static ssize_t name##_show(struct device *d, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700424 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700426 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000427 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700429static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431NETSTAT_ENTRY(rx_packets);
432NETSTAT_ENTRY(tx_packets);
433NETSTAT_ENTRY(rx_bytes);
434NETSTAT_ENTRY(tx_bytes);
435NETSTAT_ENTRY(rx_errors);
436NETSTAT_ENTRY(tx_errors);
437NETSTAT_ENTRY(rx_dropped);
438NETSTAT_ENTRY(tx_dropped);
439NETSTAT_ENTRY(multicast);
440NETSTAT_ENTRY(collisions);
441NETSTAT_ENTRY(rx_length_errors);
442NETSTAT_ENTRY(rx_over_errors);
443NETSTAT_ENTRY(rx_crc_errors);
444NETSTAT_ENTRY(rx_frame_errors);
445NETSTAT_ENTRY(rx_fifo_errors);
446NETSTAT_ENTRY(rx_missed_errors);
447NETSTAT_ENTRY(tx_aborted_errors);
448NETSTAT_ENTRY(tx_carrier_errors);
449NETSTAT_ENTRY(tx_fifo_errors);
450NETSTAT_ENTRY(tx_heartbeat_errors);
451NETSTAT_ENTRY(tx_window_errors);
452NETSTAT_ENTRY(rx_compressed);
453NETSTAT_ENTRY(tx_compressed);
454
455static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700456 &dev_attr_rx_packets.attr,
457 &dev_attr_tx_packets.attr,
458 &dev_attr_rx_bytes.attr,
459 &dev_attr_tx_bytes.attr,
460 &dev_attr_rx_errors.attr,
461 &dev_attr_tx_errors.attr,
462 &dev_attr_rx_dropped.attr,
463 &dev_attr_tx_dropped.attr,
464 &dev_attr_multicast.attr,
465 &dev_attr_collisions.attr,
466 &dev_attr_rx_length_errors.attr,
467 &dev_attr_rx_over_errors.attr,
468 &dev_attr_rx_crc_errors.attr,
469 &dev_attr_rx_frame_errors.attr,
470 &dev_attr_rx_fifo_errors.attr,
471 &dev_attr_rx_missed_errors.attr,
472 &dev_attr_tx_aborted_errors.attr,
473 &dev_attr_tx_carrier_errors.attr,
474 &dev_attr_tx_fifo_errors.attr,
475 &dev_attr_tx_heartbeat_errors.attr,
476 &dev_attr_tx_window_errors.attr,
477 &dev_attr_rx_compressed.attr,
478 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 NULL
480};
481
482
483static struct attribute_group netstat_group = {
484 .name = "statistics",
485 .attrs = netstat_attrs,
486};
Johannes Berg38c1a012012-11-16 20:46:19 +0100487
488#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
489static struct attribute *wireless_attrs[] = {
490 NULL
491};
492
493static struct attribute_group wireless_group = {
494 .name = "wireless",
495 .attrs = wireless_attrs,
496};
497#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700498
499#else /* CONFIG_SYSFS */
500#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700501#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Michael Daltona953be52014-01-16 22:23:28 -0800503#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000504#define to_rx_queue_attr(_attr) container_of(_attr, \
505 struct rx_queue_attribute, attr)
506
507#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
508
509static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
510 char *buf)
511{
512 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
513 struct netdev_rx_queue *queue = to_rx_queue(kobj);
514
515 if (!attribute->show)
516 return -EIO;
517
518 return attribute->show(queue, attribute, buf);
519}
520
521static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
522 const char *buf, size_t count)
523{
524 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
525 struct netdev_rx_queue *queue = to_rx_queue(kobj);
526
527 if (!attribute->store)
528 return -EIO;
529
530 return attribute->store(queue, attribute, buf, count);
531}
532
stephen hemmingerfa50d642010-08-31 12:14:13 +0000533static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000534 .show = rx_queue_attr_show,
535 .store = rx_queue_attr_store,
536};
537
Michael Daltona953be52014-01-16 22:23:28 -0800538#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000539static ssize_t show_rps_map(struct netdev_rx_queue *queue,
540 struct rx_queue_attribute *attribute, char *buf)
541{
542 struct rps_map *map;
543 cpumask_var_t mask;
544 size_t len = 0;
545 int i;
546
547 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
548 return -ENOMEM;
549
550 rcu_read_lock();
551 map = rcu_dereference(queue->rps_map);
552 if (map)
553 for (i = 0; i < map->len; i++)
554 cpumask_set_cpu(map->cpus[i], mask);
555
556 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
557 if (PAGE_SIZE - len < 3) {
558 rcu_read_unlock();
559 free_cpumask_var(mask);
560 return -EINVAL;
561 }
562 rcu_read_unlock();
563
564 free_cpumask_var(mask);
565 len += sprintf(buf + len, "\n");
566 return len;
567}
568
Eric Dumazetf5acb902010-04-19 14:40:57 -0700569static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000570 struct rx_queue_attribute *attribute,
571 const char *buf, size_t len)
572{
573 struct rps_map *old_map, *map;
574 cpumask_var_t mask;
575 int err, cpu, i;
576 static DEFINE_SPINLOCK(rps_map_lock);
577
578 if (!capable(CAP_NET_ADMIN))
579 return -EPERM;
580
581 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
582 return -ENOMEM;
583
584 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
585 if (err) {
586 free_cpumask_var(mask);
587 return err;
588 }
589
Eric Dumazet95c96172012-04-15 05:58:06 +0000590 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000591 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
592 GFP_KERNEL);
593 if (!map) {
594 free_cpumask_var(mask);
595 return -ENOMEM;
596 }
597
598 i = 0;
599 for_each_cpu_and(cpu, mask, cpu_online_mask)
600 map->cpus[i++] = cpu;
601
602 if (i)
603 map->len = i;
604 else {
605 kfree(map);
606 map = NULL;
607 }
608
609 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000610 old_map = rcu_dereference_protected(queue->rps_map,
611 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000612 rcu_assign_pointer(queue->rps_map, map);
613 spin_unlock(&rps_map_lock);
614
Eric Dumazetadc93002011-11-17 03:13:26 +0000615 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100616 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000617 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800618 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100619 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000620 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000621 free_cpumask_var(mask);
622 return len;
623}
624
Tom Herbertfec5e652010-04-16 16:01:27 -0700625static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
626 struct rx_queue_attribute *attr,
627 char *buf)
628{
629 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000630 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700631
632 rcu_read_lock();
633 flow_table = rcu_dereference(queue->rps_flow_table);
634 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000635 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700636 rcu_read_unlock();
637
Eric Dumazet60b778c2011-12-24 06:56:49 +0000638 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700639}
640
Tom Herbertfec5e652010-04-16 16:01:27 -0700641static void rps_dev_flow_table_release(struct rcu_head *rcu)
642{
643 struct rps_dev_flow_table *table = container_of(rcu,
644 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000645 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700646}
647
Eric Dumazetf5acb902010-04-19 14:40:57 -0700648static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700649 struct rx_queue_attribute *attr,
650 const char *buf, size_t len)
651{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000652 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700653 struct rps_dev_flow_table *table, *old_table;
654 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000655 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700656
657 if (!capable(CAP_NET_ADMIN))
658 return -EPERM;
659
Eric Dumazet60b778c2011-12-24 06:56:49 +0000660 rc = kstrtoul(buf, 0, &count);
661 if (rc < 0)
662 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700663
664 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000665 mask = count - 1;
666 /* mask = roundup_pow_of_two(count) - 1;
667 * without overflows...
668 */
669 while ((mask | (mask >> 1)) != mask)
670 mask |= (mask >> 1);
671 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800672 * and on 32bit arches, must check
673 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000674 */
675#if BITS_PER_LONG > 32
676 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000677 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000678#else
679 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000680 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700681 /* Enforce a limit to prevent overflow */
682 return -EINVAL;
683 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000684#endif
685 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700686 if (!table)
687 return -ENOMEM;
688
Eric Dumazet60b778c2011-12-24 06:56:49 +0000689 table->mask = mask;
690 for (count = 0; count <= mask; count++)
691 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700692 } else
693 table = NULL;
694
695 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000696 old_table = rcu_dereference_protected(queue->rps_flow_table,
697 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700698 rcu_assign_pointer(queue->rps_flow_table, table);
699 spin_unlock(&rps_dev_flow_lock);
700
701 if (old_table)
702 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
703
704 return len;
705}
706
Tom Herbert0a9627f2010-03-16 08:03:29 +0000707static struct rx_queue_attribute rps_cpus_attribute =
708 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
709
Tom Herbertfec5e652010-04-16 16:01:27 -0700710
711static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
712 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
713 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800714#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700715
Tom Herbert0a9627f2010-03-16 08:03:29 +0000716static struct attribute *rx_queue_default_attrs[] = {
Michael Daltona953be52014-01-16 22:23:28 -0800717#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000718 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700719 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800720#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000721 NULL
722};
723
724static void rx_queue_release(struct kobject *kobj)
725{
726 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800727#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000728 struct rps_map *map;
729 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000730
Tom Herbertfec5e652010-04-16 16:01:27 -0700731
Eric Dumazet33d480c2011-08-11 19:30:52 +0000732 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000733 if (map) {
734 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800735 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000736 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000737
Eric Dumazet33d480c2011-08-11 19:30:52 +0000738 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000739 if (flow_table) {
740 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000741 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000742 }
Michael Daltona953be52014-01-16 22:23:28 -0800743#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000744
John Fastabend9ea19482010-11-16 06:31:39 +0000745 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000746 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000747}
748
Weilong Chen82ef3d52014-01-16 17:24:31 +0800749static const void *rx_queue_namespace(struct kobject *kobj)
750{
751 struct netdev_rx_queue *queue = to_rx_queue(kobj);
752 struct device *dev = &queue->dev->dev;
753 const void *ns = NULL;
754
755 if (dev->class && dev->class->ns_type)
756 ns = dev->class->namespace(dev);
757
758 return ns;
759}
760
Tom Herbert0a9627f2010-03-16 08:03:29 +0000761static struct kobj_type rx_queue_ktype = {
762 .sysfs_ops = &rx_queue_sysfs_ops,
763 .release = rx_queue_release,
764 .default_attrs = rx_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +0800765 .namespace = rx_queue_namespace
Tom Herbert0a9627f2010-03-16 08:03:29 +0000766};
767
768static int rx_queue_add_kobject(struct net_device *net, int index)
769{
770 struct netdev_rx_queue *queue = net->_rx + index;
771 struct kobject *kobj = &queue->kobj;
772 int error = 0;
773
774 kobj->kset = net->queues_kset;
775 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
776 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800777 if (error)
778 goto exit;
779
780 if (net->sysfs_rx_queue_group) {
781 error = sysfs_create_group(kobj, net->sysfs_rx_queue_group);
782 if (error)
783 goto exit;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000784 }
785
786 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000787 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000788
789 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800790exit:
791 kobject_put(kobj);
792 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000793}
Michael Daltona953be52014-01-16 22:23:28 -0800794#endif /* CONFIG_SYFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000795
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000796int
797net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000798{
Michael Daltona953be52014-01-16 22:23:28 -0800799#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000800 int i;
801 int error = 0;
802
Michael Daltona953be52014-01-16 22:23:28 -0800803#ifndef CONFIG_RPS
804 if (!net->sysfs_rx_queue_group)
805 return 0;
806#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000807 for (i = old_num; i < new_num; i++) {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000808 error = rx_queue_add_kobject(net, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000809 if (error) {
810 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000811 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000812 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000813 }
814
Michael Daltona953be52014-01-16 22:23:28 -0800815 while (--i >= new_num) {
816 if (net->sysfs_rx_queue_group)
817 sysfs_remove_group(&net->_rx[i].kobj,
818 net->sysfs_rx_queue_group);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000819 kobject_put(&net->_rx[i].kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800820 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000821
822 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000823#else
824 return 0;
825#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000826}
827
david decotignyccf5ff62011-11-16 12:15:10 +0000828#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000829/*
830 * netdev_queue sysfs structures and functions.
831 */
832struct netdev_queue_attribute {
833 struct attribute attr;
834 ssize_t (*show)(struct netdev_queue *queue,
835 struct netdev_queue_attribute *attr, char *buf);
836 ssize_t (*store)(struct netdev_queue *queue,
837 struct netdev_queue_attribute *attr, const char *buf, size_t len);
838};
839#define to_netdev_queue_attr(_attr) container_of(_attr, \
840 struct netdev_queue_attribute, attr)
841
842#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
843
844static ssize_t netdev_queue_attr_show(struct kobject *kobj,
845 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000846{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000847 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
848 struct netdev_queue *queue = to_netdev_queue(kobj);
849
850 if (!attribute->show)
851 return -EIO;
852
853 return attribute->show(queue, attribute, buf);
854}
855
856static ssize_t netdev_queue_attr_store(struct kobject *kobj,
857 struct attribute *attr,
858 const char *buf, size_t count)
859{
860 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
861 struct netdev_queue *queue = to_netdev_queue(kobj);
862
863 if (!attribute->store)
864 return -EIO;
865
866 return attribute->store(queue, attribute, buf, count);
867}
868
869static const struct sysfs_ops netdev_queue_sysfs_ops = {
870 .show = netdev_queue_attr_show,
871 .store = netdev_queue_attr_store,
872};
873
david decotignyccf5ff62011-11-16 12:15:10 +0000874static ssize_t show_trans_timeout(struct netdev_queue *queue,
875 struct netdev_queue_attribute *attribute,
876 char *buf)
877{
878 unsigned long trans_timeout;
879
880 spin_lock_irq(&queue->_xmit_lock);
881 trans_timeout = queue->trans_timeout;
882 spin_unlock_irq(&queue->_xmit_lock);
883
884 return sprintf(buf, "%lu", trans_timeout);
885}
886
887static struct netdev_queue_attribute queue_trans_timeout =
888 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
889
Tom Herbert114cf582011-11-28 16:33:09 +0000890#ifdef CONFIG_BQL
891/*
892 * Byte queue limits sysfs structures and functions.
893 */
894static ssize_t bql_show(char *buf, unsigned int value)
895{
896 return sprintf(buf, "%u\n", value);
897}
898
899static ssize_t bql_set(const char *buf, const size_t count,
900 unsigned int *pvalue)
901{
902 unsigned int value;
903 int err;
904
905 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
906 value = DQL_MAX_LIMIT;
907 else {
908 err = kstrtouint(buf, 10, &value);
909 if (err < 0)
910 return err;
911 if (value > DQL_MAX_LIMIT)
912 return -EINVAL;
913 }
914
915 *pvalue = value;
916
917 return count;
918}
919
920static ssize_t bql_show_hold_time(struct netdev_queue *queue,
921 struct netdev_queue_attribute *attr,
922 char *buf)
923{
924 struct dql *dql = &queue->dql;
925
926 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
927}
928
929static ssize_t bql_set_hold_time(struct netdev_queue *queue,
930 struct netdev_queue_attribute *attribute,
931 const char *buf, size_t len)
932{
933 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000934 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000935 int err;
936
937 err = kstrtouint(buf, 10, &value);
938 if (err < 0)
939 return err;
940
941 dql->slack_hold_time = msecs_to_jiffies(value);
942
943 return len;
944}
945
946static struct netdev_queue_attribute bql_hold_time_attribute =
947 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
948 bql_set_hold_time);
949
950static ssize_t bql_show_inflight(struct netdev_queue *queue,
951 struct netdev_queue_attribute *attr,
952 char *buf)
953{
954 struct dql *dql = &queue->dql;
955
956 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
957}
958
959static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +0000960 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +0000961
962#define BQL_ATTR(NAME, FIELD) \
963static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
964 struct netdev_queue_attribute *attr, \
965 char *buf) \
966{ \
967 return bql_show(buf, queue->dql.FIELD); \
968} \
969 \
970static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
971 struct netdev_queue_attribute *attr, \
972 const char *buf, size_t len) \
973{ \
974 return bql_set(buf, len, &queue->dql.FIELD); \
975} \
976 \
977static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
978 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
979 bql_set_ ## NAME);
980
981BQL_ATTR(limit, limit)
982BQL_ATTR(limit_max, max_limit)
983BQL_ATTR(limit_min, min_limit)
984
985static struct attribute *dql_attrs[] = {
986 &bql_limit_attribute.attr,
987 &bql_limit_max_attribute.attr,
988 &bql_limit_min_attribute.attr,
989 &bql_hold_time_attribute.attr,
990 &bql_inflight_attribute.attr,
991 NULL
992};
993
994static struct attribute_group dql_group = {
995 .name = "byte_queue_limits",
996 .attrs = dql_attrs,
997};
998#endif /* CONFIG_BQL */
999
david decotignyccf5ff62011-11-16 12:15:10 +00001000#ifdef CONFIG_XPS
Eric Dumazeted1acc82014-02-13 10:07:13 -08001001static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001002{
1003 struct net_device *dev = queue->dev;
Eric Dumazeted1acc82014-02-13 10:07:13 -08001004 unsigned int i;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001005
Eric Dumazeted1acc82014-02-13 10:07:13 -08001006 i = queue - dev->_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001007 BUG_ON(i >= dev->num_tx_queues);
1008
1009 return i;
1010}
1011
1012
1013static ssize_t show_xps_map(struct netdev_queue *queue,
1014 struct netdev_queue_attribute *attribute, char *buf)
1015{
1016 struct net_device *dev = queue->dev;
1017 struct xps_dev_maps *dev_maps;
1018 cpumask_var_t mask;
1019 unsigned long index;
1020 size_t len = 0;
1021 int i;
1022
1023 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1024 return -ENOMEM;
1025
1026 index = get_netdev_queue_index(queue);
1027
1028 rcu_read_lock();
1029 dev_maps = rcu_dereference(dev->xps_maps);
1030 if (dev_maps) {
1031 for_each_possible_cpu(i) {
1032 struct xps_map *map =
1033 rcu_dereference(dev_maps->cpu_map[i]);
1034 if (map) {
1035 int j;
1036 for (j = 0; j < map->len; j++) {
1037 if (map->queues[j] == index) {
1038 cpumask_set_cpu(i, mask);
1039 break;
1040 }
1041 }
1042 }
1043 }
1044 }
1045 rcu_read_unlock();
1046
1047 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1048 if (PAGE_SIZE - len < 3) {
1049 free_cpumask_var(mask);
1050 return -EINVAL;
1051 }
1052
1053 free_cpumask_var(mask);
1054 len += sprintf(buf + len, "\n");
1055 return len;
1056}
1057
Tom Herbert1d24eb42010-11-21 13:17:27 +00001058static ssize_t store_xps_map(struct netdev_queue *queue,
1059 struct netdev_queue_attribute *attribute,
1060 const char *buf, size_t len)
1061{
1062 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001063 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001064 cpumask_var_t mask;
1065 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001066
1067 if (!capable(CAP_NET_ADMIN))
1068 return -EPERM;
1069
1070 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1071 return -ENOMEM;
1072
1073 index = get_netdev_queue_index(queue);
1074
1075 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1076 if (err) {
1077 free_cpumask_var(mask);
1078 return err;
1079 }
1080
Alexander Duyck537c00d2013-01-10 08:57:02 +00001081 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001082
1083 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001084
Alexander Duyck537c00d2013-01-10 08:57:02 +00001085 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001086}
1087
1088static struct netdev_queue_attribute xps_cpus_attribute =
1089 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001090#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001091
1092static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001093 &queue_trans_timeout.attr,
1094#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001095 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001096#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001097 NULL
1098};
1099
1100static void netdev_queue_release(struct kobject *kobj)
1101{
1102 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001103
Tom Herbert1d24eb42010-11-21 13:17:27 +00001104 memset(kobj, 0, sizeof(*kobj));
1105 dev_put(queue->dev);
1106}
1107
Weilong Chen82ef3d52014-01-16 17:24:31 +08001108static const void *netdev_queue_namespace(struct kobject *kobj)
1109{
1110 struct netdev_queue *queue = to_netdev_queue(kobj);
1111 struct device *dev = &queue->dev->dev;
1112 const void *ns = NULL;
1113
1114 if (dev->class && dev->class->ns_type)
1115 ns = dev->class->namespace(dev);
1116
1117 return ns;
1118}
1119
Tom Herbert1d24eb42010-11-21 13:17:27 +00001120static struct kobj_type netdev_queue_ktype = {
1121 .sysfs_ops = &netdev_queue_sysfs_ops,
1122 .release = netdev_queue_release,
1123 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001124 .namespace = netdev_queue_namespace,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001125};
1126
1127static int netdev_queue_add_kobject(struct net_device *net, int index)
1128{
1129 struct netdev_queue *queue = net->_tx + index;
1130 struct kobject *kobj = &queue->kobj;
1131 int error = 0;
1132
1133 kobj->kset = net->queues_kset;
1134 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1135 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001136 if (error)
1137 goto exit;
1138
1139#ifdef CONFIG_BQL
1140 error = sysfs_create_group(kobj, &dql_group);
1141 if (error)
1142 goto exit;
1143#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001144
1145 kobject_uevent(kobj, KOBJ_ADD);
1146 dev_hold(queue->dev);
1147
Tom Herbert114cf582011-11-28 16:33:09 +00001148 return 0;
1149exit:
1150 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001151 return error;
1152}
david decotignyccf5ff62011-11-16 12:15:10 +00001153#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001154
1155int
1156netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1157{
david decotignyccf5ff62011-11-16 12:15:10 +00001158#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001159 int i;
1160 int error = 0;
1161
1162 for (i = old_num; i < new_num; i++) {
1163 error = netdev_queue_add_kobject(net, i);
1164 if (error) {
1165 new_num = old_num;
1166 break;
1167 }
1168 }
1169
Tom Herbert114cf582011-11-28 16:33:09 +00001170 while (--i >= new_num) {
1171 struct netdev_queue *queue = net->_tx + i;
1172
1173#ifdef CONFIG_BQL
1174 sysfs_remove_group(&queue->kobj, &dql_group);
1175#endif
1176 kobject_put(&queue->kobj);
1177 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001178
1179 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001180#else
1181 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001182#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001183}
1184
1185static int register_queue_kobjects(struct net_device *net)
1186{
Tom Herbertbf264142010-11-26 08:36:09 +00001187 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001188
david decotignyccf5ff62011-11-16 12:15:10 +00001189#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001190 net->queues_kset = kset_create_and_add("queues",
1191 NULL, &net->dev.kobj);
1192 if (!net->queues_kset)
1193 return -ENOMEM;
Tom Herbertbf264142010-11-26 08:36:09 +00001194 real_rx = net->real_num_rx_queues;
1195#endif
1196 real_tx = net->real_num_tx_queues;
1197
1198 error = net_rx_queue_update_kobjects(net, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001199 if (error)
1200 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001201 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001202
Tom Herbertbf264142010-11-26 08:36:09 +00001203 error = netdev_queue_update_kobjects(net, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001204 if (error)
1205 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001206 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001207
1208 return 0;
1209
1210error:
1211 netdev_queue_update_kobjects(net, txq, 0);
1212 net_rx_queue_update_kobjects(net, rxq, 0);
1213 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001214}
1215
Tom Herbert1d24eb42010-11-21 13:17:27 +00001216static void remove_queue_kobjects(struct net_device *net)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001217{
Tom Herbertbf264142010-11-26 08:36:09 +00001218 int real_rx = 0, real_tx = 0;
1219
Michael Daltona953be52014-01-16 22:23:28 -08001220#ifdef CONFIG_SYSFS
Tom Herbertbf264142010-11-26 08:36:09 +00001221 real_rx = net->real_num_rx_queues;
1222#endif
1223 real_tx = net->real_num_tx_queues;
1224
1225 net_rx_queue_update_kobjects(net, real_rx, 0);
1226 netdev_queue_update_kobjects(net, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001227#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +00001228 kset_unregister(net->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001229#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001230}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001231
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001232static bool net_current_may_mount(void)
1233{
1234 struct net *net = current->nsproxy->net_ns;
1235
1236 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1237}
1238
Al Viroa685e082011-06-08 21:13:01 -04001239static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001240{
Al Viroa685e082011-06-08 21:13:01 -04001241 struct net *ns = current->nsproxy->net_ns;
1242#ifdef CONFIG_NET_NS
1243 if (ns)
1244 atomic_inc(&ns->passive);
1245#endif
1246 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001247}
1248
1249static const void *net_initial_ns(void)
1250{
1251 return &init_net;
1252}
1253
1254static const void *net_netlink_ns(struct sock *sk)
1255{
1256 return sock_net(sk);
1257}
1258
Johannes Berg04600792010-08-05 17:45:15 +02001259struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001260 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001261 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001262 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001263 .netlink_ns = net_netlink_ns,
1264 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001265 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001266};
Johannes Berg04600792010-08-05 17:45:15 +02001267EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001268
Kay Sievers7eff2e72007-08-14 15:15:12 +02001269static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001271 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001272 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Kay Sievers312c0042005-11-16 09:00:00 +01001274 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001275 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001276 if (retval)
1277 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001279 /* pass ifindex to uevent.
1280 * ifindex is useful as it won't change (interface name may change)
1281 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001282 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001283
Eric Rannaudbf624562007-03-30 22:23:12 -07001284exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001285 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001289 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001290 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001292static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001294 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 BUG_ON(dev->reg_state != NETREG_RELEASED);
1297
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001298 kfree(dev->ifalias);
Eric Dumazet74d332c2013-10-30 13:10:44 -07001299 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001302static const void *net_namespace(struct device *d)
1303{
1304 struct net_device *dev;
1305 dev = container_of(d, struct net_device, dev);
1306 return dev_net(dev);
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static struct class net_class = {
1310 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001311 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001312 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001313 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001314 .ns_type = &net_ns_type_operations,
1315 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316};
1317
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001318/* Delete sysfs entries but hold kobject reference until after all
1319 * netdev references are gone.
1320 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001321void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001323 struct device *dev = &(net->dev);
1324
1325 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001326
Tom Herbert1d24eb42010-11-21 13:17:27 +00001327 remove_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001328
Ming Lei9802c8e2013-02-22 16:34:16 -08001329 pm_runtime_set_memalloc_noio(dev, false);
1330
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001331 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001335int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001337 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -07001338 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001339 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001341 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001342 dev->class = &net_class;
1343 dev->platform_data = net;
1344 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Stephen Hemmingera2205472009-03-09 13:51:55 +00001346 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001348#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001349 /* Allow for a device specific group */
1350 if (*groups)
1351 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001353 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001354
1355#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1356 if (net->ieee80211_ptr)
1357 *groups++ = &wireless_group;
1358#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1359 else if (net->wireless_handlers)
1360 *groups++ = &wireless_group;
1361#endif
1362#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001363#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Tom Herbert0a9627f2010-03-16 08:03:29 +00001365 error = device_add(dev);
1366 if (error)
1367 return error;
1368
Tom Herbert1d24eb42010-11-21 13:17:27 +00001369 error = register_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001370 if (error) {
1371 device_del(dev);
1372 return error;
1373 }
1374
Ming Lei9802c8e2013-02-22 16:34:16 -08001375 pm_runtime_set_memalloc_noio(dev, true);
1376
Tom Herbert0a9627f2010-03-16 08:03:29 +00001377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Tejun Heo58292cbe2013-09-11 22:29:04 -04001380int netdev_class_create_file_ns(struct class_attribute *class_attr,
1381 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001382{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001383 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001384}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001385EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001386
Tejun Heo58292cbe2013-09-11 22:29:04 -04001387void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1388 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001389{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001390 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001391}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001392EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001393
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001394int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001396 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return class_register(&net_class);
1398}