blob: b388cdab9316accc4fff2b994a2f593de7548b10 [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>
21#include <linux/wireless.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070022#include <linux/vmalloc.h>
Johannes Berg8f1546c2009-09-28 15:26:43 +020023#include <net/wext.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";
31static const char fmt_ulong[] = "%lu\n";
32
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090033static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070035 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070039static ssize_t netdev_show(const struct device *dev,
40 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 ssize_t (*format)(const struct net_device *, char *))
42{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070043 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 ssize_t ret = -EINVAL;
45
46 read_lock(&dev_base_lock);
47 if (dev_isalive(net))
48 ret = (*format)(net, buf);
49 read_unlock(&dev_base_lock);
50
51 return ret;
52}
53
54/* generate a show function for simple field */
55#define NETDEVICE_SHOW(field, format_string) \
56static ssize_t format_##field(const struct net_device *net, char *buf) \
57{ \
58 return sprintf(buf, format_string, net->field); \
59} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070060static ssize_t show_##field(struct device *dev, \
61 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070063 return netdev_show(dev, attr, buf, format_##field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66
67/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070068static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 const char *buf, size_t len,
70 int (*set)(struct net_device *, unsigned long))
71{
72 struct net_device *net = to_net_dev(dev);
73 char *endp;
74 unsigned long new;
75 int ret = -EINVAL;
76
77 if (!capable(CAP_NET_ADMIN))
78 return -EPERM;
79
80 new = simple_strtoul(buf, &endp, 0);
81 if (endp == buf)
82 goto err;
83
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000084 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000085 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (dev_isalive(net)) {
88 if ((ret = (*set)(net, new)) == 0)
89 ret = len;
90 }
91 rtnl_unlock();
92 err:
93 return ret;
94}
95
David Woodhouse9d296722008-04-20 16:07:43 -070096NETDEVICE_SHOW(dev_id, fmt_hex);
Kay Sieversfd586ba2005-12-19 01:42:56 +010097NETDEVICE_SHOW(addr_len, fmt_dec);
98NETDEVICE_SHOW(iflink, fmt_dec);
99NETDEVICE_SHOW(ifindex, fmt_dec);
100NETDEVICE_SHOW(features, fmt_long_hex);
101NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800102NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700105static ssize_t show_address(struct device *dev, struct device_attribute *attr,
106 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct net_device *net = to_net_dev(dev);
109 ssize_t ret = -EINVAL;
110
111 read_lock(&dev_base_lock);
112 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800113 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 read_unlock(&dev_base_lock);
115 return ret;
116}
117
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700118static ssize_t show_broadcast(struct device *dev,
119 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct net_device *net = to_net_dev(dev);
122 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800123 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EINVAL;
125}
126
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700127static ssize_t show_carrier(struct device *dev,
128 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct net_device *netdev = to_net_dev(dev);
131 if (netif_running(netdev)) {
132 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
133 }
134 return -EINVAL;
135}
136
Andy Gospodarekd519e172009-10-02 09:26:12 +0000137static ssize_t show_speed(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 struct net_device *netdev = to_net_dev(dev);
141 int ret = -EINVAL;
142
143 if (!rtnl_trylock())
144 return restart_syscall();
145
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000146 if (netif_running(netdev) &&
147 netdev->ethtool_ops &&
148 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000149 struct ethtool_cmd cmd = { ETHTOOL_GSET };
150
151 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
152 ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
153 }
154 rtnl_unlock();
155 return ret;
156}
157
158static ssize_t show_duplex(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
161 struct net_device *netdev = to_net_dev(dev);
162 int ret = -EINVAL;
163
164 if (!rtnl_trylock())
165 return restart_syscall();
166
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000167 if (netif_running(netdev) &&
168 netdev->ethtool_ops &&
169 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000170 struct ethtool_cmd cmd = { ETHTOOL_GSET };
171
172 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
173 ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
174 }
175 rtnl_unlock();
176 return ret;
177}
178
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700179static ssize_t show_dormant(struct device *dev,
180 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800181{
182 struct net_device *netdev = to_net_dev(dev);
183
184 if (netif_running(netdev))
185 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
186
187 return -EINVAL;
188}
189
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700190static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800191 "unknown",
192 "notpresent", /* currently unused */
193 "down",
194 "lowerlayerdown",
195 "testing", /* currently unused */
196 "dormant",
197 "up"
198};
199
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200static ssize_t show_operstate(struct device *dev,
201 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800202{
203 const struct net_device *netdev = to_net_dev(dev);
204 unsigned char operstate;
205
206 read_lock(&dev_base_lock);
207 operstate = netdev->operstate;
208 if (!netif_running(netdev))
209 operstate = IF_OPER_DOWN;
210 read_unlock(&dev_base_lock);
211
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700212 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800213 return -EINVAL; /* should not happen */
214
215 return sprintf(buf, "%s\n", operstates[operstate]);
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* read-write attributes */
219NETDEVICE_SHOW(mtu, fmt_dec);
220
221static int change_mtu(struct net_device *net, unsigned long new_mtu)
222{
223 return dev_set_mtu(net, (int) new_mtu);
224}
225
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700226static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
227 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700229 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232NETDEVICE_SHOW(flags, fmt_hex);
233
234static int change_flags(struct net_device *net, unsigned long new_flags)
235{
236 return dev_change_flags(net, (unsigned) new_flags);
237}
238
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700239static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700242 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
246
247static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
248{
249 net->tx_queue_len = new_len;
250 return 0;
251}
252
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700253static ssize_t store_tx_queue_len(struct device *dev,
254 struct device_attribute *attr,
255 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700257 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700260static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t len)
262{
263 struct net_device *netdev = to_net_dev(dev);
264 size_t count = len;
265 ssize_t ret;
266
267 if (!capable(CAP_NET_ADMIN))
268 return -EPERM;
269
270 /* ignore trailing newline */
271 if (len > 0 && buf[len - 1] == '\n')
272 --count;
273
Eric W. Biederman336ca572009-05-13 16:57:25 +0000274 if (!rtnl_trylock())
275 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700276 ret = dev_set_alias(netdev, buf, count);
277 rtnl_unlock();
278
279 return ret < 0 ? ret : len;
280}
281
282static ssize_t show_ifalias(struct device *dev,
283 struct device_attribute *attr, char *buf)
284{
285 const struct net_device *netdev = to_net_dev(dev);
286 ssize_t ret = 0;
287
Eric W. Biederman336ca572009-05-13 16:57:25 +0000288 if (!rtnl_trylock())
289 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700290 if (netdev->ifalias)
291 ret = sprintf(buf, "%s\n", netdev->ifalias);
292 rtnl_unlock();
293 return ret;
294}
295
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700296static struct device_attribute net_class_attributes[] = {
Kay Sieversfd586ba2005-12-19 01:42:56 +0100297 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
David Woodhouse9d296722008-04-20 16:07:43 -0700298 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700299 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100300 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
301 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
302 __ATTR(features, S_IRUGO, show_features, NULL),
303 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800304 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100305 __ATTR(address, S_IRUGO, show_address, NULL),
306 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
307 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Andy Gospodarekd519e172009-10-02 09:26:12 +0000308 __ATTR(speed, S_IRUGO, show_speed, NULL),
309 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800310 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
311 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100312 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
313 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
314 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
315 store_tx_queue_len),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100316 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318
319/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700320static ssize_t netstat_show(const struct device *d,
321 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long offset)
323{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700324 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ssize_t ret = -EINVAL;
326
Pavel Emelyanovdf1b86c2007-11-30 00:42:42 +1100327 WARN_ON(offset > sizeof(struct net_device_stats) ||
328 offset % sizeof(unsigned long) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700331 if (dev_isalive(dev)) {
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800332 const struct net_device_stats *stats = dev_get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 ret = sprintf(buf, fmt_ulong,
334 *(unsigned long *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 read_unlock(&dev_base_lock);
337 return ret;
338}
339
340/* generate a read-only statistics attribute */
341#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700342static ssize_t show_##name(struct device *d, \
343 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700345 return netstat_show(d, attr, buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 offsetof(struct net_device_stats, name)); \
347} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700348static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350NETSTAT_ENTRY(rx_packets);
351NETSTAT_ENTRY(tx_packets);
352NETSTAT_ENTRY(rx_bytes);
353NETSTAT_ENTRY(tx_bytes);
354NETSTAT_ENTRY(rx_errors);
355NETSTAT_ENTRY(tx_errors);
356NETSTAT_ENTRY(rx_dropped);
357NETSTAT_ENTRY(tx_dropped);
358NETSTAT_ENTRY(multicast);
359NETSTAT_ENTRY(collisions);
360NETSTAT_ENTRY(rx_length_errors);
361NETSTAT_ENTRY(rx_over_errors);
362NETSTAT_ENTRY(rx_crc_errors);
363NETSTAT_ENTRY(rx_frame_errors);
364NETSTAT_ENTRY(rx_fifo_errors);
365NETSTAT_ENTRY(rx_missed_errors);
366NETSTAT_ENTRY(tx_aborted_errors);
367NETSTAT_ENTRY(tx_carrier_errors);
368NETSTAT_ENTRY(tx_fifo_errors);
369NETSTAT_ENTRY(tx_heartbeat_errors);
370NETSTAT_ENTRY(tx_window_errors);
371NETSTAT_ENTRY(rx_compressed);
372NETSTAT_ENTRY(tx_compressed);
373
374static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700375 &dev_attr_rx_packets.attr,
376 &dev_attr_tx_packets.attr,
377 &dev_attr_rx_bytes.attr,
378 &dev_attr_tx_bytes.attr,
379 &dev_attr_rx_errors.attr,
380 &dev_attr_tx_errors.attr,
381 &dev_attr_rx_dropped.attr,
382 &dev_attr_tx_dropped.attr,
383 &dev_attr_multicast.attr,
384 &dev_attr_collisions.attr,
385 &dev_attr_rx_length_errors.attr,
386 &dev_attr_rx_over_errors.attr,
387 &dev_attr_rx_crc_errors.attr,
388 &dev_attr_rx_frame_errors.attr,
389 &dev_attr_rx_fifo_errors.attr,
390 &dev_attr_rx_missed_errors.attr,
391 &dev_attr_tx_aborted_errors.attr,
392 &dev_attr_tx_carrier_errors.attr,
393 &dev_attr_tx_fifo_errors.attr,
394 &dev_attr_tx_heartbeat_errors.attr,
395 &dev_attr_tx_window_errors.attr,
396 &dev_attr_rx_compressed.attr,
397 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 NULL
399};
400
401
402static struct attribute_group netstat_group = {
403 .name = "statistics",
404 .attrs = netstat_attrs,
405};
406
Johannes Berg22bb1be2008-07-10 11:16:47 +0200407#ifdef CONFIG_WIRELESS_EXT_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/* helper function that does all the locking etc for wireless stats */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700409static ssize_t wireless_show(struct device *d, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ssize_t (*format)(const struct iw_statistics *,
411 char *))
412{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700413 struct net_device *dev = to_net_dev(d);
Johannes Berg8f1546c2009-09-28 15:26:43 +0200414 const struct iw_statistics *iw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ssize_t ret = -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900416
Eric W. Biedermanb8afe642010-02-19 13:23:47 +0000417 if (!rtnl_trylock())
418 return restart_syscall();
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800419 if (dev_isalive(dev)) {
Johannes Berg8f1546c2009-09-28 15:26:43 +0200420 iw = get_wireless_stats(dev);
421 if (iw)
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800422 ret = (*format)(iw, buf);
423 }
Johannes Berga160ee62009-10-05 02:22:23 -0700424 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return ret;
427}
428
429/* show function template for wireless fields */
430#define WIRELESS_SHOW(name, field, format_string) \
431static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
432{ \
433 return sprintf(buf, format_string, iw->field); \
434} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700435static ssize_t show_iw_##name(struct device *d, \
436 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700438 return wireless_show(d, buf, format_iw_##name); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700440static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442WIRELESS_SHOW(status, status, fmt_hex);
443WIRELESS_SHOW(link, qual.qual, fmt_dec);
444WIRELESS_SHOW(level, qual.level, fmt_dec);
445WIRELESS_SHOW(noise, qual.noise, fmt_dec);
446WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
447WIRELESS_SHOW(crypt, discard.code, fmt_dec);
448WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
449WIRELESS_SHOW(misc, discard.misc, fmt_dec);
450WIRELESS_SHOW(retries, discard.retries, fmt_dec);
451WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
452
453static struct attribute *wireless_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454 &dev_attr_status.attr,
455 &dev_attr_link.attr,
456 &dev_attr_level.attr,
457 &dev_attr_noise.attr,
458 &dev_attr_nwid.attr,
459 &dev_attr_crypt.attr,
460 &dev_attr_fragment.attr,
461 &dev_attr_retries.attr,
462 &dev_attr_misc.attr,
463 &dev_attr_beacon.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 NULL
465};
466
467static struct attribute_group wireless_group = {
468 .name = "wireless",
469 .attrs = wireless_attrs,
470};
471#endif
472
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700473#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000474/*
475 * RX queue sysfs structures and functions.
476 */
477struct rx_queue_attribute {
478 struct attribute attr;
479 ssize_t (*show)(struct netdev_rx_queue *queue,
480 struct rx_queue_attribute *attr, char *buf);
481 ssize_t (*store)(struct netdev_rx_queue *queue,
482 struct rx_queue_attribute *attr, const char *buf, size_t len);
483};
484#define to_rx_queue_attr(_attr) container_of(_attr, \
485 struct rx_queue_attribute, attr)
486
487#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
488
489static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
490 char *buf)
491{
492 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
493 struct netdev_rx_queue *queue = to_rx_queue(kobj);
494
495 if (!attribute->show)
496 return -EIO;
497
498 return attribute->show(queue, attribute, buf);
499}
500
501static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
502 const char *buf, size_t count)
503{
504 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
505 struct netdev_rx_queue *queue = to_rx_queue(kobj);
506
507 if (!attribute->store)
508 return -EIO;
509
510 return attribute->store(queue, attribute, buf, count);
511}
512
513static struct sysfs_ops rx_queue_sysfs_ops = {
514 .show = rx_queue_attr_show,
515 .store = rx_queue_attr_store,
516};
517
518static ssize_t show_rps_map(struct netdev_rx_queue *queue,
519 struct rx_queue_attribute *attribute, char *buf)
520{
521 struct rps_map *map;
522 cpumask_var_t mask;
523 size_t len = 0;
524 int i;
525
526 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
527 return -ENOMEM;
528
529 rcu_read_lock();
530 map = rcu_dereference(queue->rps_map);
531 if (map)
532 for (i = 0; i < map->len; i++)
533 cpumask_set_cpu(map->cpus[i], mask);
534
535 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
536 if (PAGE_SIZE - len < 3) {
537 rcu_read_unlock();
538 free_cpumask_var(mask);
539 return -EINVAL;
540 }
541 rcu_read_unlock();
542
543 free_cpumask_var(mask);
544 len += sprintf(buf + len, "\n");
545 return len;
546}
547
548static void rps_map_release(struct rcu_head *rcu)
549{
550 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
551
552 kfree(map);
553}
554
Eric Dumazetf5acb902010-04-19 14:40:57 -0700555static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000556 struct rx_queue_attribute *attribute,
557 const char *buf, size_t len)
558{
559 struct rps_map *old_map, *map;
560 cpumask_var_t mask;
561 int err, cpu, i;
562 static DEFINE_SPINLOCK(rps_map_lock);
563
564 if (!capable(CAP_NET_ADMIN))
565 return -EPERM;
566
567 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
568 return -ENOMEM;
569
570 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
571 if (err) {
572 free_cpumask_var(mask);
573 return err;
574 }
575
576 map = kzalloc(max_t(unsigned,
577 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
578 GFP_KERNEL);
579 if (!map) {
580 free_cpumask_var(mask);
581 return -ENOMEM;
582 }
583
584 i = 0;
585 for_each_cpu_and(cpu, mask, cpu_online_mask)
586 map->cpus[i++] = cpu;
587
588 if (i)
589 map->len = i;
590 else {
591 kfree(map);
592 map = NULL;
593 }
594
595 spin_lock(&rps_map_lock);
596 old_map = queue->rps_map;
597 rcu_assign_pointer(queue->rps_map, map);
598 spin_unlock(&rps_map_lock);
599
600 if (old_map)
601 call_rcu(&old_map->rcu, rps_map_release);
602
603 free_cpumask_var(mask);
604 return len;
605}
606
Tom Herbertfec5e652010-04-16 16:01:27 -0700607static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
608 struct rx_queue_attribute *attr,
609 char *buf)
610{
611 struct rps_dev_flow_table *flow_table;
612 unsigned int val = 0;
613
614 rcu_read_lock();
615 flow_table = rcu_dereference(queue->rps_flow_table);
616 if (flow_table)
617 val = flow_table->mask + 1;
618 rcu_read_unlock();
619
620 return sprintf(buf, "%u\n", val);
621}
622
623static void rps_dev_flow_table_release_work(struct work_struct *work)
624{
625 struct rps_dev_flow_table *table = container_of(work,
626 struct rps_dev_flow_table, free_work);
627
628 vfree(table);
629}
630
631static void rps_dev_flow_table_release(struct rcu_head *rcu)
632{
633 struct rps_dev_flow_table *table = container_of(rcu,
634 struct rps_dev_flow_table, rcu);
635
636 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
637 schedule_work(&table->free_work);
638}
639
Eric Dumazetf5acb902010-04-19 14:40:57 -0700640static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700641 struct rx_queue_attribute *attr,
642 const char *buf, size_t len)
643{
644 unsigned int count;
645 char *endp;
646 struct rps_dev_flow_table *table, *old_table;
647 static DEFINE_SPINLOCK(rps_dev_flow_lock);
648
649 if (!capable(CAP_NET_ADMIN))
650 return -EPERM;
651
652 count = simple_strtoul(buf, &endp, 0);
653 if (endp == buf)
654 return -EINVAL;
655
656 if (count) {
657 int i;
658
659 if (count > 1<<30) {
660 /* Enforce a limit to prevent overflow */
661 return -EINVAL;
662 }
663 count = roundup_pow_of_two(count);
664 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
665 if (!table)
666 return -ENOMEM;
667
668 table->mask = count - 1;
669 for (i = 0; i < count; i++)
670 table->flows[i].cpu = RPS_NO_CPU;
671 } else
672 table = NULL;
673
674 spin_lock(&rps_dev_flow_lock);
675 old_table = queue->rps_flow_table;
676 rcu_assign_pointer(queue->rps_flow_table, table);
677 spin_unlock(&rps_dev_flow_lock);
678
679 if (old_table)
680 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
681
682 return len;
683}
684
Tom Herbert0a9627f2010-03-16 08:03:29 +0000685static struct rx_queue_attribute rps_cpus_attribute =
686 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
687
Tom Herbertfec5e652010-04-16 16:01:27 -0700688
689static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
690 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
691 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
692
Tom Herbert0a9627f2010-03-16 08:03:29 +0000693static struct attribute *rx_queue_default_attrs[] = {
694 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700695 &rps_dev_flow_table_cnt_attribute.attr,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000696 NULL
697};
698
699static void rx_queue_release(struct kobject *kobj)
700{
701 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000702 struct netdev_rx_queue *first = queue->first;
703
Tom Herbertfec5e652010-04-16 16:01:27 -0700704 if (queue->rps_map)
705 call_rcu(&queue->rps_map->rcu, rps_map_release);
706
707 if (queue->rps_flow_table)
708 call_rcu(&queue->rps_flow_table->rcu,
709 rps_dev_flow_table_release);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000710
711 if (atomic_dec_and_test(&first->count))
712 kfree(first);
713}
714
715static struct kobj_type rx_queue_ktype = {
716 .sysfs_ops = &rx_queue_sysfs_ops,
717 .release = rx_queue_release,
718 .default_attrs = rx_queue_default_attrs,
719};
720
721static int rx_queue_add_kobject(struct net_device *net, int index)
722{
723 struct netdev_rx_queue *queue = net->_rx + index;
724 struct kobject *kobj = &queue->kobj;
725 int error = 0;
726
727 kobj->kset = net->queues_kset;
728 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
729 "rx-%u", index);
730 if (error) {
731 kobject_put(kobj);
732 return error;
733 }
734
735 kobject_uevent(kobj, KOBJ_ADD);
736
737 return error;
738}
739
740static int rx_queue_register_kobjects(struct net_device *net)
741{
742 int i;
743 int error = 0;
744
745 net->queues_kset = kset_create_and_add("queues",
746 NULL, &net->dev.kobj);
747 if (!net->queues_kset)
748 return -ENOMEM;
749 for (i = 0; i < net->num_rx_queues; i++) {
750 error = rx_queue_add_kobject(net, i);
751 if (error)
752 break;
753 }
754
755 if (error)
756 while (--i >= 0)
757 kobject_put(&net->_rx[i].kobj);
758
759 return error;
760}
761
762static void rx_queue_remove_kobjects(struct net_device *net)
763{
764 int i;
765
766 for (i = 0; i < net->num_rx_queues; i++)
767 kobject_put(&net->_rx[i].kobj);
768 kset_unregister(net->queues_kset);
769}
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700770#endif /* CONFIG_RPS */
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700771
772static const void *net_current_ns(void)
773{
774 return current->nsproxy->net_ns;
775}
776
777static const void *net_initial_ns(void)
778{
779 return &init_net;
780}
781
782static const void *net_netlink_ns(struct sock *sk)
783{
784 return sock_net(sk);
785}
786
787static struct kobj_ns_type_operations net_ns_type_operations = {
788 .type = KOBJ_NS_TYPE_NET,
789 .current_ns = net_current_ns,
790 .netlink_ns = net_netlink_ns,
791 .initial_ns = net_initial_ns,
792};
793
794static void net_kobj_ns_exit(struct net *net)
795{
796 kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
797}
798
799static struct pernet_operations sysfs_net_ops = {
800 .exit = net_kobj_ns_exit,
801};
802
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700803#endif /* CONFIG_SYSFS */
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200806static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700808 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200809 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Daniel Lezcano09bb5212008-11-25 16:46:37 -0800811 if (!net_eq(dev_net(dev), &init_net))
812 return 0;
813
Kay Sievers312c0042005-11-16 09:00:00 +0100814 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200815 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -0700816 if (retval)
817 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800819 /* pass ifindex to uevent.
820 * ifindex is useful as it won't change (interface name may change)
821 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200822 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800823
Eric Rannaudbf624562007-03-30 22:23:12 -0700824exit:
Eric Rannaudbf624562007-03-30 22:23:12 -0700825 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827#endif
828
829/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900830 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700831 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700833static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700835 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 BUG_ON(dev->reg_state != NETREG_RELEASED);
838
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700839 kfree(dev->ifalias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 kfree((char *)dev - dev->padded);
841}
842
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700843static const void *net_namespace(struct device *d)
844{
845 struct net_device *dev;
846 dev = container_of(d, struct net_device, dev);
847 return dev_net(dev);
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static struct class net_class = {
851 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700852 .dev_release = netdev_release,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700853#ifdef CONFIG_SYSFS
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700854 .dev_attrs = net_class_attributes,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700855#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#ifdef CONFIG_HOTPLUG
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700857 .dev_uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#endif
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700859 .ns_type = &net_ns_type_operations,
860 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700863/* Delete sysfs entries but hold kobject reference until after all
864 * netdev references are gone.
865 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700866void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700868 struct device *dev = &(net->dev);
869
870 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -0700871
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800872 if (!net_eq(dev_net(net), &init_net))
Eric W. Biederman38918452008-10-27 17:51:47 -0700873 return;
874
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700875#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000876 rx_queue_remove_kobjects(net);
Tom Herberte880eb62010-03-22 18:06:47 -0700877#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000878
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700879 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700883int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700885 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -0700886 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000887 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700889 dev->class = &net_class;
890 dev->platform_data = net;
891 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Stephen Hemmingera2205472009-03-09 13:51:55 +0000893 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700895#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000896 /* Allow for a device specific group */
897 if (*groups)
898 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000900 *groups++ = &netstat_group;
Johannes Berg22bb1be2008-07-10 11:16:47 +0200901#ifdef CONFIG_WIRELESS_EXT_SYSFS
Johannes Berg3d23e342009-09-29 23:27:28 +0200902 if (net->ieee80211_ptr)
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700903 *groups++ = &wireless_group;
Johannes Berg3d23e342009-09-29 23:27:28 +0200904#ifdef CONFIG_WIRELESS_EXT
905 else if (net->wireless_handlers)
906 *groups++ = &wireless_group;
907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700909#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800911 if (!net_eq(dev_net(net), &init_net))
Eric W. Biederman38918452008-10-27 17:51:47 -0700912 return 0;
913
Tom Herbert0a9627f2010-03-16 08:03:29 +0000914 error = device_add(dev);
915 if (error)
916 return error;
917
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700918#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000919 error = rx_queue_register_kobjects(net);
920 if (error) {
921 device_del(dev);
922 return error;
923 }
Tom Herberte880eb62010-03-22 18:06:47 -0700924#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000925
926 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Jay Vosburghb8a97872008-06-13 18:12:04 -0700929int netdev_class_create_file(struct class_attribute *class_attr)
930{
931 return class_create_file(&net_class, class_attr);
932}
933
934void netdev_class_remove_file(struct class_attribute *class_attr)
935{
936 class_remove_file(&net_class, class_attr);
937}
938
939EXPORT_SYMBOL(netdev_class_create_file);
940EXPORT_SYMBOL(netdev_class_remove_file);
941
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -0700942void netdev_initialize_kobject(struct net_device *net)
943{
944 struct device *device = &(net->dev);
945 device_initialize(device);
946}
947
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700948int netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700950 kobj_ns_type_register(&net_ns_type_operations);
951#ifdef CONFIG_SYSFS
952 register_pernet_subsys(&sysfs_net_ops);
953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return class_register(&net_class);
955}