blob: 28c5f5aa7ca7e019b93e07f4d58a6758a7f5a78b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Pavel Emelyanov342709e2007-10-23 21:14:45 -070025#include "net-sysfs.h"
26
Eric W. Biederman8b41d182007-09-26 22:02:53 -070027#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070029static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static const char fmt_dec[] = "%d\n";
David Decotigny8ae6dac2011-04-27 18:32:38 +000031static const char fmt_udec[] = "%u\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static const char fmt_ulong[] = "%lu\n";
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +000033static const char fmt_u64[] = "%llu\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090035static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070037 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070041static ssize_t netdev_show(const struct device *dev,
42 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 ssize_t (*format)(const struct net_device *, char *))
44{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070045 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 ssize_t ret = -EINVAL;
47
48 read_lock(&dev_base_lock);
49 if (dev_isalive(net))
50 ret = (*format)(net, buf);
51 read_unlock(&dev_base_lock);
52
53 return ret;
54}
55
56/* generate a show function for simple field */
57#define NETDEVICE_SHOW(field, format_string) \
58static ssize_t format_##field(const struct net_device *net, char *buf) \
59{ \
60 return sprintf(buf, format_string, net->field); \
61} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070062static ssize_t show_##field(struct device *dev, \
63 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070065 return netdev_show(dev, attr, buf, format_##field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68
69/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070070static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 const char *buf, size_t len,
72 int (*set)(struct net_device *, unsigned long))
73{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000074 struct net_device *netdev = to_net_dev(dev);
75 struct net *net = dev_net(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned long new;
77 int ret = -EINVAL;
78
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000079 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EPERM;
81
Shuah Khane1e420c2012-04-12 09:28:13 +000082 ret = kstrtoul(buf, 0, &new);
83 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto err;
85
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000086 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000087 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000088
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000089 if (dev_isalive(netdev)) {
90 if ((ret = (*set)(netdev, new)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ret = len;
92 }
93 rtnl_unlock();
94 err:
95 return ret;
96}
97
David Woodhouse9d296722008-04-20 16:07:43 -070098NETDEVICE_SHOW(dev_id, fmt_hex);
Stefan Assmannc1f79422010-07-22 02:50:21 +000099NETDEVICE_SHOW(addr_assign_type, fmt_dec);
Kay Sieversfd586ba2005-12-19 01:42:56 +0100100NETDEVICE_SHOW(addr_len, fmt_dec);
101NETDEVICE_SHOW(iflink, fmt_dec);
102NETDEVICE_SHOW(ifindex, fmt_dec);
Kay Sieversfd586ba2005-12-19 01:42:56 +0100103NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800104NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700107static ssize_t show_address(struct device *dev, struct device_attribute *attr,
108 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct net_device *net = to_net_dev(dev);
111 ssize_t ret = -EINVAL;
112
113 read_lock(&dev_base_lock);
114 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800115 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 read_unlock(&dev_base_lock);
117 return ret;
118}
119
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700120static ssize_t show_broadcast(struct device *dev,
121 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct net_device *net = to_net_dev(dev);
124 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800125 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -EINVAL;
127}
128
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700129static ssize_t show_carrier(struct device *dev,
130 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct net_device *netdev = to_net_dev(dev);
133 if (netif_running(netdev)) {
134 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
135 }
136 return -EINVAL;
137}
138
Andy Gospodarekd519e172009-10-02 09:26:12 +0000139static ssize_t show_speed(struct device *dev,
140 struct device_attribute *attr, char *buf)
141{
142 struct net_device *netdev = to_net_dev(dev);
143 int ret = -EINVAL;
144
145 if (!rtnl_trylock())
146 return restart_syscall();
147
David Decotigny8ae6dac2011-04-27 18:32:38 +0000148 if (netif_running(netdev)) {
149 struct ethtool_cmd cmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000150 if (!__ethtool_get_settings(netdev, &cmd))
David Decotigny8ae6dac2011-04-27 18:32:38 +0000151 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
Andy Gospodarekd519e172009-10-02 09:26:12 +0000152 }
153 rtnl_unlock();
154 return ret;
155}
156
157static ssize_t show_duplex(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 struct net_device *netdev = to_net_dev(dev);
161 int ret = -EINVAL;
162
163 if (!rtnl_trylock())
164 return restart_syscall();
165
David Decotigny8ae6dac2011-04-27 18:32:38 +0000166 if (netif_running(netdev)) {
167 struct ethtool_cmd cmd;
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000168 if (!__ethtool_get_settings(netdev, &cmd)) {
169 const char *duplex;
170 switch (cmd.duplex) {
171 case DUPLEX_HALF:
172 duplex = "half";
173 break;
174 case DUPLEX_FULL:
175 duplex = "full";
176 break;
177 default:
178 duplex = "unknown";
179 break;
180 }
181 ret = sprintf(buf, "%s\n", duplex);
182 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000183 }
184 rtnl_unlock();
185 return ret;
186}
187
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700188static ssize_t show_dormant(struct device *dev,
189 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800190{
191 struct net_device *netdev = to_net_dev(dev);
192
193 if (netif_running(netdev))
194 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
195
196 return -EINVAL;
197}
198
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700199static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800200 "unknown",
201 "notpresent", /* currently unused */
202 "down",
203 "lowerlayerdown",
204 "testing", /* currently unused */
205 "dormant",
206 "up"
207};
208
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700209static ssize_t show_operstate(struct device *dev,
210 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800211{
212 const struct net_device *netdev = to_net_dev(dev);
213 unsigned char operstate;
214
215 read_lock(&dev_base_lock);
216 operstate = netdev->operstate;
217 if (!netif_running(netdev))
218 operstate = IF_OPER_DOWN;
219 read_unlock(&dev_base_lock);
220
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700221 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800222 return -EINVAL; /* should not happen */
223
224 return sprintf(buf, "%s\n", operstates[operstate]);
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* read-write attributes */
228NETDEVICE_SHOW(mtu, fmt_dec);
229
230static int change_mtu(struct net_device *net, unsigned long new_mtu)
231{
232 return dev_set_mtu(net, (int) new_mtu);
233}
234
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700235static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
236 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700238 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241NETDEVICE_SHOW(flags, fmt_hex);
242
243static int change_flags(struct net_device *net, unsigned long new_flags)
244{
Eric Dumazet95c96172012-04-15 05:58:06 +0000245 return dev_change_flags(net, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700248static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700251 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
255
256static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
257{
258 net->tx_queue_len = new_len;
259 return 0;
260}
261
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700262static ssize_t store_tx_queue_len(struct device *dev,
263 struct device_attribute *attr,
264 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000266 if (!capable(CAP_NET_ADMIN))
267 return -EPERM;
268
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700269 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700272static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t len)
274{
275 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000276 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700277 size_t count = len;
278 ssize_t ret;
279
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000280 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700281 return -EPERM;
282
283 /* ignore trailing newline */
284 if (len > 0 && buf[len - 1] == '\n')
285 --count;
286
Eric W. Biederman336ca572009-05-13 16:57:25 +0000287 if (!rtnl_trylock())
288 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700289 ret = dev_set_alias(netdev, buf, count);
290 rtnl_unlock();
291
292 return ret < 0 ? ret : len;
293}
294
295static ssize_t show_ifalias(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 const struct net_device *netdev = to_net_dev(dev);
299 ssize_t ret = 0;
300
Eric W. Biederman336ca572009-05-13 16:57:25 +0000301 if (!rtnl_trylock())
302 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700303 if (netdev->ifalias)
304 ret = sprintf(buf, "%s\n", netdev->ifalias);
305 rtnl_unlock();
306 return ret;
307}
308
Vlad Dogarua512b922011-01-24 03:37:29 +0000309NETDEVICE_SHOW(group, fmt_dec);
310
311static int change_group(struct net_device *net, unsigned long new_group)
312{
313 dev_set_group(net, (int) new_group);
314 return 0;
315}
316
317static ssize_t store_group(struct device *dev, struct device_attribute *attr,
318 const char *buf, size_t len)
319{
320 return netdev_store(dev, attr, buf, len, change_group);
321}
322
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700323static struct device_attribute net_class_attributes[] = {
Stefan Assmannc1f79422010-07-22 02:50:21 +0000324 __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100325 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
David Woodhouse9d296722008-04-20 16:07:43 -0700326 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700327 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100328 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
329 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100330 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800331 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100332 __ATTR(address, S_IRUGO, show_address, NULL),
333 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
334 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Andy Gospodarekd519e172009-10-02 09:26:12 +0000335 __ATTR(speed, S_IRUGO, show_speed, NULL),
336 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800337 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
338 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100339 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
340 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
341 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
342 store_tx_queue_len),
Xiaotian Fengb6644cb2011-02-09 19:16:15 -0800343 __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100344 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700348static ssize_t netstat_show(const struct device *d,
349 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned long offset)
351{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700352 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ssize_t ret = -EINVAL;
354
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000355 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
356 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700359 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700360 struct rtnl_link_stats64 temp;
361 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
362
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000363 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 read_unlock(&dev_base_lock);
366 return ret;
367}
368
369/* generate a read-only statistics attribute */
370#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700371static ssize_t show_##name(struct device *d, \
372 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700374 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000375 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700377static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379NETSTAT_ENTRY(rx_packets);
380NETSTAT_ENTRY(tx_packets);
381NETSTAT_ENTRY(rx_bytes);
382NETSTAT_ENTRY(tx_bytes);
383NETSTAT_ENTRY(rx_errors);
384NETSTAT_ENTRY(tx_errors);
385NETSTAT_ENTRY(rx_dropped);
386NETSTAT_ENTRY(tx_dropped);
387NETSTAT_ENTRY(multicast);
388NETSTAT_ENTRY(collisions);
389NETSTAT_ENTRY(rx_length_errors);
390NETSTAT_ENTRY(rx_over_errors);
391NETSTAT_ENTRY(rx_crc_errors);
392NETSTAT_ENTRY(rx_frame_errors);
393NETSTAT_ENTRY(rx_fifo_errors);
394NETSTAT_ENTRY(rx_missed_errors);
395NETSTAT_ENTRY(tx_aborted_errors);
396NETSTAT_ENTRY(tx_carrier_errors);
397NETSTAT_ENTRY(tx_fifo_errors);
398NETSTAT_ENTRY(tx_heartbeat_errors);
399NETSTAT_ENTRY(tx_window_errors);
400NETSTAT_ENTRY(rx_compressed);
401NETSTAT_ENTRY(tx_compressed);
402
403static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 &dev_attr_rx_packets.attr,
405 &dev_attr_tx_packets.attr,
406 &dev_attr_rx_bytes.attr,
407 &dev_attr_tx_bytes.attr,
408 &dev_attr_rx_errors.attr,
409 &dev_attr_tx_errors.attr,
410 &dev_attr_rx_dropped.attr,
411 &dev_attr_tx_dropped.attr,
412 &dev_attr_multicast.attr,
413 &dev_attr_collisions.attr,
414 &dev_attr_rx_length_errors.attr,
415 &dev_attr_rx_over_errors.attr,
416 &dev_attr_rx_crc_errors.attr,
417 &dev_attr_rx_frame_errors.attr,
418 &dev_attr_rx_fifo_errors.attr,
419 &dev_attr_rx_missed_errors.attr,
420 &dev_attr_tx_aborted_errors.attr,
421 &dev_attr_tx_carrier_errors.attr,
422 &dev_attr_tx_fifo_errors.attr,
423 &dev_attr_tx_heartbeat_errors.attr,
424 &dev_attr_tx_window_errors.attr,
425 &dev_attr_rx_compressed.attr,
426 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 NULL
428};
429
430
431static struct attribute_group netstat_group = {
432 .name = "statistics",
433 .attrs = netstat_attrs,
434};
Johannes Berg38c1a012012-11-16 20:46:19 +0100435
436#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
437static struct attribute *wireless_attrs[] = {
438 NULL
439};
440
441static struct attribute_group wireless_group = {
442 .name = "wireless",
443 .attrs = wireless_attrs,
444};
445#endif
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700446#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700448#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000449/*
450 * RX queue sysfs structures and functions.
451 */
452struct rx_queue_attribute {
453 struct attribute attr;
454 ssize_t (*show)(struct netdev_rx_queue *queue,
455 struct rx_queue_attribute *attr, char *buf);
456 ssize_t (*store)(struct netdev_rx_queue *queue,
457 struct rx_queue_attribute *attr, const char *buf, size_t len);
458};
459#define to_rx_queue_attr(_attr) container_of(_attr, \
460 struct rx_queue_attribute, attr)
461
462#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
463
464static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
465 char *buf)
466{
467 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
468 struct netdev_rx_queue *queue = to_rx_queue(kobj);
469
470 if (!attribute->show)
471 return -EIO;
472
473 return attribute->show(queue, attribute, buf);
474}
475
476static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
477 const char *buf, size_t count)
478{
479 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
480 struct netdev_rx_queue *queue = to_rx_queue(kobj);
481
482 if (!attribute->store)
483 return -EIO;
484
485 return attribute->store(queue, attribute, buf, count);
486}
487
stephen hemmingerfa50d642010-08-31 12:14:13 +0000488static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000489 .show = rx_queue_attr_show,
490 .store = rx_queue_attr_store,
491};
492
493static ssize_t show_rps_map(struct netdev_rx_queue *queue,
494 struct rx_queue_attribute *attribute, char *buf)
495{
496 struct rps_map *map;
497 cpumask_var_t mask;
498 size_t len = 0;
499 int i;
500
501 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
502 return -ENOMEM;
503
504 rcu_read_lock();
505 map = rcu_dereference(queue->rps_map);
506 if (map)
507 for (i = 0; i < map->len; i++)
508 cpumask_set_cpu(map->cpus[i], mask);
509
510 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
511 if (PAGE_SIZE - len < 3) {
512 rcu_read_unlock();
513 free_cpumask_var(mask);
514 return -EINVAL;
515 }
516 rcu_read_unlock();
517
518 free_cpumask_var(mask);
519 len += sprintf(buf + len, "\n");
520 return len;
521}
522
Eric Dumazetf5acb902010-04-19 14:40:57 -0700523static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000524 struct rx_queue_attribute *attribute,
525 const char *buf, size_t len)
526{
527 struct rps_map *old_map, *map;
528 cpumask_var_t mask;
529 int err, cpu, i;
530 static DEFINE_SPINLOCK(rps_map_lock);
531
532 if (!capable(CAP_NET_ADMIN))
533 return -EPERM;
534
535 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
536 return -ENOMEM;
537
538 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
539 if (err) {
540 free_cpumask_var(mask);
541 return err;
542 }
543
Eric Dumazet95c96172012-04-15 05:58:06 +0000544 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000545 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
546 GFP_KERNEL);
547 if (!map) {
548 free_cpumask_var(mask);
549 return -ENOMEM;
550 }
551
552 i = 0;
553 for_each_cpu_and(cpu, mask, cpu_online_mask)
554 map->cpus[i++] = cpu;
555
556 if (i)
557 map->len = i;
558 else {
559 kfree(map);
560 map = NULL;
561 }
562
563 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000564 old_map = rcu_dereference_protected(queue->rps_map,
565 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000566 rcu_assign_pointer(queue->rps_map, map);
567 spin_unlock(&rps_map_lock);
568
Eric Dumazetadc93002011-11-17 03:13:26 +0000569 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100570 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000571 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800572 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100573 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000574 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000575 free_cpumask_var(mask);
576 return len;
577}
578
Tom Herbertfec5e652010-04-16 16:01:27 -0700579static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
580 struct rx_queue_attribute *attr,
581 char *buf)
582{
583 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000584 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700585
586 rcu_read_lock();
587 flow_table = rcu_dereference(queue->rps_flow_table);
588 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000589 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700590 rcu_read_unlock();
591
Eric Dumazet60b778c2011-12-24 06:56:49 +0000592 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700593}
594
595static void rps_dev_flow_table_release_work(struct work_struct *work)
596{
597 struct rps_dev_flow_table *table = container_of(work,
598 struct rps_dev_flow_table, free_work);
599
600 vfree(table);
601}
602
603static void rps_dev_flow_table_release(struct rcu_head *rcu)
604{
605 struct rps_dev_flow_table *table = container_of(rcu,
606 struct rps_dev_flow_table, rcu);
607
608 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
609 schedule_work(&table->free_work);
610}
611
Eric Dumazetf5acb902010-04-19 14:40:57 -0700612static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700613 struct rx_queue_attribute *attr,
614 const char *buf, size_t len)
615{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000616 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700617 struct rps_dev_flow_table *table, *old_table;
618 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000619 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700620
621 if (!capable(CAP_NET_ADMIN))
622 return -EPERM;
623
Eric Dumazet60b778c2011-12-24 06:56:49 +0000624 rc = kstrtoul(buf, 0, &count);
625 if (rc < 0)
626 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700627
628 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000629 mask = count - 1;
630 /* mask = roundup_pow_of_two(count) - 1;
631 * without overflows...
632 */
633 while ((mask | (mask >> 1)) != mask)
634 mask |= (mask >> 1);
635 /* On 64 bit arches, must check mask fits in table->mask (u32),
636 * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
637 * doesnt overflow.
638 */
639#if BITS_PER_LONG > 32
640 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000641 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000642#else
643 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000644 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700645 /* Enforce a limit to prevent overflow */
646 return -EINVAL;
647 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000648#endif
649 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700650 if (!table)
651 return -ENOMEM;
652
Eric Dumazet60b778c2011-12-24 06:56:49 +0000653 table->mask = mask;
654 for (count = 0; count <= mask; count++)
655 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700656 } else
657 table = NULL;
658
659 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000660 old_table = rcu_dereference_protected(queue->rps_flow_table,
661 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700662 rcu_assign_pointer(queue->rps_flow_table, table);
663 spin_unlock(&rps_dev_flow_lock);
664
665 if (old_table)
666 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
667
668 return len;
669}
670
Tom Herbert0a9627f2010-03-16 08:03:29 +0000671static struct rx_queue_attribute rps_cpus_attribute =
672 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
673
Tom Herbertfec5e652010-04-16 16:01:27 -0700674
675static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
676 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
677 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
678
Tom Herbert0a9627f2010-03-16 08:03:29 +0000679static struct attribute *rx_queue_default_attrs[] = {
680 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700681 &rps_dev_flow_table_cnt_attribute.attr,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000682 NULL
683};
684
685static void rx_queue_release(struct kobject *kobj)
686{
687 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000688 struct rps_map *map;
689 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000690
Tom Herbertfec5e652010-04-16 16:01:27 -0700691
Eric Dumazet33d480c2011-08-11 19:30:52 +0000692 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000693 if (map) {
694 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800695 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000696 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000697
Eric Dumazet33d480c2011-08-11 19:30:52 +0000698 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000699 if (flow_table) {
700 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000701 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000702 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000703
John Fastabend9ea19482010-11-16 06:31:39 +0000704 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000705 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000706}
707
708static struct kobj_type rx_queue_ktype = {
709 .sysfs_ops = &rx_queue_sysfs_ops,
710 .release = rx_queue_release,
711 .default_attrs = rx_queue_default_attrs,
712};
713
714static int rx_queue_add_kobject(struct net_device *net, int index)
715{
716 struct netdev_rx_queue *queue = net->_rx + index;
717 struct kobject *kobj = &queue->kobj;
718 int error = 0;
719
720 kobj->kset = net->queues_kset;
721 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
722 "rx-%u", index);
723 if (error) {
724 kobject_put(kobj);
725 return error;
726 }
727
728 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000729 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000730
731 return error;
732}
Tom Herbertbf264142010-11-26 08:36:09 +0000733#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000734
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000735int
736net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000737{
Tom Herbertbf264142010-11-26 08:36:09 +0000738#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000739 int i;
740 int error = 0;
741
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000742 for (i = old_num; i < new_num; i++) {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000743 error = rx_queue_add_kobject(net, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000744 if (error) {
745 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000746 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000747 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000748 }
749
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000750 while (--i >= new_num)
751 kobject_put(&net->_rx[i].kobj);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000752
753 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000754#else
755 return 0;
756#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000757}
758
david decotignyccf5ff62011-11-16 12:15:10 +0000759#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000760/*
761 * netdev_queue sysfs structures and functions.
762 */
763struct netdev_queue_attribute {
764 struct attribute attr;
765 ssize_t (*show)(struct netdev_queue *queue,
766 struct netdev_queue_attribute *attr, char *buf);
767 ssize_t (*store)(struct netdev_queue *queue,
768 struct netdev_queue_attribute *attr, const char *buf, size_t len);
769};
770#define to_netdev_queue_attr(_attr) container_of(_attr, \
771 struct netdev_queue_attribute, attr)
772
773#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
774
775static ssize_t netdev_queue_attr_show(struct kobject *kobj,
776 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000777{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000778 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
779 struct netdev_queue *queue = to_netdev_queue(kobj);
780
781 if (!attribute->show)
782 return -EIO;
783
784 return attribute->show(queue, attribute, buf);
785}
786
787static ssize_t netdev_queue_attr_store(struct kobject *kobj,
788 struct attribute *attr,
789 const char *buf, size_t count)
790{
791 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
792 struct netdev_queue *queue = to_netdev_queue(kobj);
793
794 if (!attribute->store)
795 return -EIO;
796
797 return attribute->store(queue, attribute, buf, count);
798}
799
800static const struct sysfs_ops netdev_queue_sysfs_ops = {
801 .show = netdev_queue_attr_show,
802 .store = netdev_queue_attr_store,
803};
804
david decotignyccf5ff62011-11-16 12:15:10 +0000805static ssize_t show_trans_timeout(struct netdev_queue *queue,
806 struct netdev_queue_attribute *attribute,
807 char *buf)
808{
809 unsigned long trans_timeout;
810
811 spin_lock_irq(&queue->_xmit_lock);
812 trans_timeout = queue->trans_timeout;
813 spin_unlock_irq(&queue->_xmit_lock);
814
815 return sprintf(buf, "%lu", trans_timeout);
816}
817
818static struct netdev_queue_attribute queue_trans_timeout =
819 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
820
Tom Herbert114cf582011-11-28 16:33:09 +0000821#ifdef CONFIG_BQL
822/*
823 * Byte queue limits sysfs structures and functions.
824 */
825static ssize_t bql_show(char *buf, unsigned int value)
826{
827 return sprintf(buf, "%u\n", value);
828}
829
830static ssize_t bql_set(const char *buf, const size_t count,
831 unsigned int *pvalue)
832{
833 unsigned int value;
834 int err;
835
836 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
837 value = DQL_MAX_LIMIT;
838 else {
839 err = kstrtouint(buf, 10, &value);
840 if (err < 0)
841 return err;
842 if (value > DQL_MAX_LIMIT)
843 return -EINVAL;
844 }
845
846 *pvalue = value;
847
848 return count;
849}
850
851static ssize_t bql_show_hold_time(struct netdev_queue *queue,
852 struct netdev_queue_attribute *attr,
853 char *buf)
854{
855 struct dql *dql = &queue->dql;
856
857 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
858}
859
860static ssize_t bql_set_hold_time(struct netdev_queue *queue,
861 struct netdev_queue_attribute *attribute,
862 const char *buf, size_t len)
863{
864 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000865 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000866 int err;
867
868 err = kstrtouint(buf, 10, &value);
869 if (err < 0)
870 return err;
871
872 dql->slack_hold_time = msecs_to_jiffies(value);
873
874 return len;
875}
876
877static struct netdev_queue_attribute bql_hold_time_attribute =
878 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
879 bql_set_hold_time);
880
881static ssize_t bql_show_inflight(struct netdev_queue *queue,
882 struct netdev_queue_attribute *attr,
883 char *buf)
884{
885 struct dql *dql = &queue->dql;
886
887 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
888}
889
890static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +0000891 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +0000892
893#define BQL_ATTR(NAME, FIELD) \
894static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
895 struct netdev_queue_attribute *attr, \
896 char *buf) \
897{ \
898 return bql_show(buf, queue->dql.FIELD); \
899} \
900 \
901static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
902 struct netdev_queue_attribute *attr, \
903 const char *buf, size_t len) \
904{ \
905 return bql_set(buf, len, &queue->dql.FIELD); \
906} \
907 \
908static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
909 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
910 bql_set_ ## NAME);
911
912BQL_ATTR(limit, limit)
913BQL_ATTR(limit_max, max_limit)
914BQL_ATTR(limit_min, min_limit)
915
916static struct attribute *dql_attrs[] = {
917 &bql_limit_attribute.attr,
918 &bql_limit_max_attribute.attr,
919 &bql_limit_min_attribute.attr,
920 &bql_hold_time_attribute.attr,
921 &bql_inflight_attribute.attr,
922 NULL
923};
924
925static struct attribute_group dql_group = {
926 .name = "byte_queue_limits",
927 .attrs = dql_attrs,
928};
929#endif /* CONFIG_BQL */
930
david decotignyccf5ff62011-11-16 12:15:10 +0000931#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000932static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
933{
934 struct net_device *dev = queue->dev;
935 int i;
936
937 for (i = 0; i < dev->num_tx_queues; i++)
938 if (queue == &dev->_tx[i])
939 break;
940
941 BUG_ON(i >= dev->num_tx_queues);
942
943 return i;
944}
945
946
947static ssize_t show_xps_map(struct netdev_queue *queue,
948 struct netdev_queue_attribute *attribute, char *buf)
949{
950 struct net_device *dev = queue->dev;
951 struct xps_dev_maps *dev_maps;
952 cpumask_var_t mask;
953 unsigned long index;
954 size_t len = 0;
955 int i;
956
957 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
958 return -ENOMEM;
959
960 index = get_netdev_queue_index(queue);
961
962 rcu_read_lock();
963 dev_maps = rcu_dereference(dev->xps_maps);
964 if (dev_maps) {
965 for_each_possible_cpu(i) {
966 struct xps_map *map =
967 rcu_dereference(dev_maps->cpu_map[i]);
968 if (map) {
969 int j;
970 for (j = 0; j < map->len; j++) {
971 if (map->queues[j] == index) {
972 cpumask_set_cpu(i, mask);
973 break;
974 }
975 }
976 }
977 }
978 }
979 rcu_read_unlock();
980
981 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
982 if (PAGE_SIZE - len < 3) {
983 free_cpumask_var(mask);
984 return -EINVAL;
985 }
986
987 free_cpumask_var(mask);
988 len += sprintf(buf + len, "\n");
989 return len;
990}
991
Tom Herbert1d24eb42010-11-21 13:17:27 +0000992static DEFINE_MUTEX(xps_map_mutex);
Eric Dumazeta4177862010-11-28 21:43:02 +0000993#define xmap_dereference(P) \
994 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
Tom Herbert1d24eb42010-11-21 13:17:27 +0000995
Tom Herbert927fbec2011-11-28 16:33:02 +0000996static void xps_queue_release(struct netdev_queue *queue)
997{
998 struct net_device *dev = queue->dev;
999 struct xps_dev_maps *dev_maps;
1000 struct xps_map *map;
1001 unsigned long index;
1002 int i, pos, nonempty = 0;
1003
1004 index = get_netdev_queue_index(queue);
1005
1006 mutex_lock(&xps_map_mutex);
1007 dev_maps = xmap_dereference(dev->xps_maps);
1008
1009 if (dev_maps) {
1010 for_each_possible_cpu(i) {
1011 map = xmap_dereference(dev_maps->cpu_map[i]);
1012 if (!map)
1013 continue;
1014
1015 for (pos = 0; pos < map->len; pos++)
1016 if (map->queues[pos] == index)
1017 break;
1018
1019 if (pos < map->len) {
1020 if (map->len > 1)
1021 map->queues[pos] =
1022 map->queues[--map->len];
1023 else {
1024 RCU_INIT_POINTER(dev_maps->cpu_map[i],
1025 NULL);
1026 kfree_rcu(map, rcu);
1027 map = NULL;
1028 }
1029 }
1030 if (map)
1031 nonempty = 1;
1032 }
1033
1034 if (!nonempty) {
1035 RCU_INIT_POINTER(dev->xps_maps, NULL);
1036 kfree_rcu(dev_maps, rcu);
1037 }
1038 }
1039 mutex_unlock(&xps_map_mutex);
1040}
1041
Tom Herbert1d24eb42010-11-21 13:17:27 +00001042static ssize_t store_xps_map(struct netdev_queue *queue,
1043 struct netdev_queue_attribute *attribute,
1044 const char *buf, size_t len)
1045{
1046 struct net_device *dev = queue->dev;
1047 cpumask_var_t mask;
1048 int err, i, cpu, pos, map_len, alloc_len, need_set;
1049 unsigned long index;
1050 struct xps_map *map, *new_map;
1051 struct xps_dev_maps *dev_maps, *new_dev_maps;
1052 int nonempty = 0;
david decotigny19b05f82011-11-16 12:15:08 +00001053 int numa_node_id = -2;
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
Eric Dumazet95c96172012-04-15 05:58:06 +00001069 new_dev_maps = kzalloc(max_t(unsigned int,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001070 XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
1071 if (!new_dev_maps) {
1072 free_cpumask_var(mask);
1073 return -ENOMEM;
1074 }
1075
1076 mutex_lock(&xps_map_mutex);
1077
Eric Dumazeta4177862010-11-28 21:43:02 +00001078 dev_maps = xmap_dereference(dev->xps_maps);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001079
1080 for_each_possible_cpu(cpu) {
Eric Dumazeta4177862010-11-28 21:43:02 +00001081 map = dev_maps ?
1082 xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
1083 new_map = map;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001084 if (map) {
1085 for (pos = 0; pos < map->len; pos++)
1086 if (map->queues[pos] == index)
1087 break;
1088 map_len = map->len;
1089 alloc_len = map->alloc_len;
1090 } else
1091 pos = map_len = alloc_len = 0;
1092
KOSAKI Motohiro2142c132011-05-16 11:53:49 -07001093 need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu);
Eric Dumazetf2cd2d32010-11-29 08:14:37 +00001094#ifdef CONFIG_NUMA
1095 if (need_set) {
david decotigny19b05f82011-11-16 12:15:08 +00001096 if (numa_node_id == -2)
1097 numa_node_id = cpu_to_node(cpu);
1098 else if (numa_node_id != cpu_to_node(cpu))
1099 numa_node_id = -1;
Eric Dumazetf2cd2d32010-11-29 08:14:37 +00001100 }
1101#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001102 if (need_set && pos >= map_len) {
1103 /* Need to add queue to this CPU's map */
1104 if (map_len >= alloc_len) {
1105 alloc_len = alloc_len ?
1106 2 * alloc_len : XPS_MIN_MAP_ALLOC;
Eric Dumazetb02038a2010-11-28 05:43:24 +00001107 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
1108 GFP_KERNEL,
1109 cpu_to_node(cpu));
Tom Herbert1d24eb42010-11-21 13:17:27 +00001110 if (!new_map)
1111 goto error;
1112 new_map->alloc_len = alloc_len;
1113 for (i = 0; i < map_len; i++)
1114 new_map->queues[i] = map->queues[i];
1115 new_map->len = map_len;
1116 }
1117 new_map->queues[new_map->len++] = index;
1118 } else if (!need_set && pos < map_len) {
1119 /* Need to remove queue from this CPU's map */
1120 if (map_len > 1)
1121 new_map->queues[pos] =
1122 new_map->queues[--new_map->len];
1123 else
1124 new_map = NULL;
1125 }
Eric Dumazeta4177862010-11-28 21:43:02 +00001126 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001127 }
1128
1129 /* Cleanup old maps */
1130 for_each_possible_cpu(cpu) {
Eric Dumazeta4177862010-11-28 21:43:02 +00001131 map = dev_maps ?
1132 xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
1133 if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
Lai Jiangshanedc86d82011-03-18 12:02:20 +08001134 kfree_rcu(map, rcu);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001135 if (new_dev_maps->cpu_map[cpu])
1136 nonempty = 1;
1137 }
1138
Eric Dumazetcf778b02012-01-12 04:41:32 +00001139 if (nonempty) {
1140 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
1141 } else {
Tom Herbert1d24eb42010-11-21 13:17:27 +00001142 kfree(new_dev_maps);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001143 RCU_INIT_POINTER(dev->xps_maps, NULL);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001144 }
1145
1146 if (dev_maps)
Lai Jiangshanb55071e2011-03-18 12:02:47 +08001147 kfree_rcu(dev_maps, rcu);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001148
david decotigny19b05f82011-11-16 12:15:08 +00001149 netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id :
Changli Gaob236da62010-12-14 03:09:15 +00001150 NUMA_NO_NODE);
Eric Dumazetf2cd2d32010-11-29 08:14:37 +00001151
Tom Herbert1d24eb42010-11-21 13:17:27 +00001152 mutex_unlock(&xps_map_mutex);
1153
1154 free_cpumask_var(mask);
1155 return len;
1156
1157error:
1158 mutex_unlock(&xps_map_mutex);
1159
1160 if (new_dev_maps)
1161 for_each_possible_cpu(i)
Eric Dumazeta4177862010-11-28 21:43:02 +00001162 kfree(rcu_dereference_protected(
1163 new_dev_maps->cpu_map[i],
1164 1));
Tom Herbert1d24eb42010-11-21 13:17:27 +00001165 kfree(new_dev_maps);
1166 free_cpumask_var(mask);
1167 return -ENOMEM;
1168}
1169
1170static struct netdev_queue_attribute xps_cpus_attribute =
1171 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001172#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001173
1174static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001175 &queue_trans_timeout.attr,
1176#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001177 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001178#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001179 NULL
1180};
1181
1182static void netdev_queue_release(struct kobject *kobj)
1183{
1184 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001185
Tom Herbert114cf582011-11-28 16:33:09 +00001186#ifdef CONFIG_XPS
Tom Herbert927fbec2011-11-28 16:33:02 +00001187 xps_queue_release(queue);
Tom Herbert114cf582011-11-28 16:33:09 +00001188#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001189
1190 memset(kobj, 0, sizeof(*kobj));
1191 dev_put(queue->dev);
1192}
1193
1194static struct kobj_type netdev_queue_ktype = {
1195 .sysfs_ops = &netdev_queue_sysfs_ops,
1196 .release = netdev_queue_release,
1197 .default_attrs = netdev_queue_default_attrs,
1198};
1199
1200static int netdev_queue_add_kobject(struct net_device *net, int index)
1201{
1202 struct netdev_queue *queue = net->_tx + index;
1203 struct kobject *kobj = &queue->kobj;
1204 int error = 0;
1205
1206 kobj->kset = net->queues_kset;
1207 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1208 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001209 if (error)
1210 goto exit;
1211
1212#ifdef CONFIG_BQL
1213 error = sysfs_create_group(kobj, &dql_group);
1214 if (error)
1215 goto exit;
1216#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001217
1218 kobject_uevent(kobj, KOBJ_ADD);
1219 dev_hold(queue->dev);
1220
Tom Herbert114cf582011-11-28 16:33:09 +00001221 return 0;
1222exit:
1223 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001224 return error;
1225}
david decotignyccf5ff62011-11-16 12:15:10 +00001226#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001227
1228int
1229netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1230{
david decotignyccf5ff62011-11-16 12:15:10 +00001231#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001232 int i;
1233 int error = 0;
1234
1235 for (i = old_num; i < new_num; i++) {
1236 error = netdev_queue_add_kobject(net, i);
1237 if (error) {
1238 new_num = old_num;
1239 break;
1240 }
1241 }
1242
Tom Herbert114cf582011-11-28 16:33:09 +00001243 while (--i >= new_num) {
1244 struct netdev_queue *queue = net->_tx + i;
1245
1246#ifdef CONFIG_BQL
1247 sysfs_remove_group(&queue->kobj, &dql_group);
1248#endif
1249 kobject_put(&queue->kobj);
1250 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001251
1252 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001253#else
1254 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001255#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001256}
1257
1258static int register_queue_kobjects(struct net_device *net)
1259{
Tom Herbertbf264142010-11-26 08:36:09 +00001260 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001261
david decotignyccf5ff62011-11-16 12:15:10 +00001262#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001263 net->queues_kset = kset_create_and_add("queues",
1264 NULL, &net->dev.kobj);
1265 if (!net->queues_kset)
1266 return -ENOMEM;
Tom Herbertbf264142010-11-26 08:36:09 +00001267#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001268
Tom Herbertbf264142010-11-26 08:36:09 +00001269#ifdef CONFIG_RPS
1270 real_rx = net->real_num_rx_queues;
1271#endif
1272 real_tx = net->real_num_tx_queues;
1273
1274 error = net_rx_queue_update_kobjects(net, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001275 if (error)
1276 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001277 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001278
Tom Herbertbf264142010-11-26 08:36:09 +00001279 error = netdev_queue_update_kobjects(net, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001280 if (error)
1281 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001282 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001283
1284 return 0;
1285
1286error:
1287 netdev_queue_update_kobjects(net, txq, 0);
1288 net_rx_queue_update_kobjects(net, rxq, 0);
1289 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001290}
1291
Tom Herbert1d24eb42010-11-21 13:17:27 +00001292static void remove_queue_kobjects(struct net_device *net)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001293{
Tom Herbertbf264142010-11-26 08:36:09 +00001294 int real_rx = 0, real_tx = 0;
1295
1296#ifdef CONFIG_RPS
1297 real_rx = net->real_num_rx_queues;
1298#endif
1299 real_tx = net->real_num_tx_queues;
1300
1301 net_rx_queue_update_kobjects(net, real_rx, 0);
1302 netdev_queue_update_kobjects(net, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001303#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +00001304 kset_unregister(net->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001305#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001306}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001307
Al Viroa685e082011-06-08 21:13:01 -04001308static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001309{
Al Viroa685e082011-06-08 21:13:01 -04001310 struct net *ns = current->nsproxy->net_ns;
1311#ifdef CONFIG_NET_NS
1312 if (ns)
1313 atomic_inc(&ns->passive);
1314#endif
1315 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001316}
1317
1318static const void *net_initial_ns(void)
1319{
1320 return &init_net;
1321}
1322
1323static const void *net_netlink_ns(struct sock *sk)
1324{
1325 return sock_net(sk);
1326}
1327
Johannes Berg04600792010-08-05 17:45:15 +02001328struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001329 .type = KOBJ_NS_TYPE_NET,
Al Viroa685e082011-06-08 21:13:01 -04001330 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001331 .netlink_ns = net_netlink_ns,
1332 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001333 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001334};
Johannes Berg04600792010-08-05 17:45:15 +02001335EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001336
Kay Sievers7eff2e72007-08-14 15:15:12 +02001337static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001339 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001340 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Kay Sievers312c0042005-11-16 09:00:00 +01001342 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001343 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001344 if (retval)
1345 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001347 /* pass ifindex to uevent.
1348 * ifindex is useful as it won't change (interface name may change)
1349 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001350 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001351
Eric Rannaudbf624562007-03-30 22:23:12 -07001352exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001353 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001357 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001360static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001362 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 BUG_ON(dev->reg_state != NETREG_RELEASED);
1365
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001366 kfree(dev->ifalias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 kfree((char *)dev - dev->padded);
1368}
1369
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001370static const void *net_namespace(struct device *d)
1371{
1372 struct net_device *dev;
1373 dev = container_of(d, struct net_device, dev);
1374 return dev_net(dev);
1375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377static struct class net_class = {
1378 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001379 .dev_release = netdev_release,
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001380#ifdef CONFIG_SYSFS
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001381 .dev_attrs = net_class_attributes,
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001382#endif /* CONFIG_SYSFS */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001383 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001384 .ns_type = &net_ns_type_operations,
1385 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386};
1387
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001388/* Delete sysfs entries but hold kobject reference until after all
1389 * netdev references are gone.
1390 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001391void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001393 struct device *dev = &(net->dev);
1394
1395 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001396
Tom Herbert1d24eb42010-11-21 13:17:27 +00001397 remove_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001398
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001399 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001403int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001405 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -07001406 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001407 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001409 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001410 dev->class = &net_class;
1411 dev->platform_data = net;
1412 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Stephen Hemmingera2205472009-03-09 13:51:55 +00001414 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001416#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001417 /* Allow for a device specific group */
1418 if (*groups)
1419 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001421 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001422
1423#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1424 if (net->ieee80211_ptr)
1425 *groups++ = &wireless_group;
1426#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1427 else if (net->wireless_handlers)
1428 *groups++ = &wireless_group;
1429#endif
1430#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001431#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tom Herbert0a9627f2010-03-16 08:03:29 +00001433 error = device_add(dev);
1434 if (error)
1435 return error;
1436
Tom Herbert1d24eb42010-11-21 13:17:27 +00001437 error = register_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001438 if (error) {
1439 device_del(dev);
1440 return error;
1441 }
1442
1443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Jay Vosburghb8a97872008-06-13 18:12:04 -07001446int netdev_class_create_file(struct class_attribute *class_attr)
1447{
1448 return class_create_file(&net_class, class_attr);
1449}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001450EXPORT_SYMBOL(netdev_class_create_file);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001451
1452void netdev_class_remove_file(struct class_attribute *class_attr)
1453{
1454 class_remove_file(&net_class, class_attr);
1455}
Jay Vosburghb8a97872008-06-13 18:12:04 -07001456EXPORT_SYMBOL(netdev_class_remove_file);
1457
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001458int netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001460 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return class_register(&net_class);
1462}