blob: 143052a22b9b36f1682052e511cd85f5a1c9b564 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/sock.h>
18#include <linux/rtnetlink.h>
19#include <linux/wireless.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070020#include <linux/vmalloc.h>
Johannes Berg8f1546c2009-09-28 15:26:43 +020021#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Pavel Emelyanov342709e2007-10-23 21:14:45 -070023#include "net-sysfs.h"
24
Eric W. Biederman8b41d182007-09-26 22:02:53 -070025#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070027static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static const char fmt_dec[] = "%d\n";
29static const char fmt_ulong[] = "%lu\n";
30
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090031static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070033 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070037static ssize_t netdev_show(const struct device *dev,
38 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 ssize_t (*format)(const struct net_device *, char *))
40{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070041 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ssize_t ret = -EINVAL;
43
44 read_lock(&dev_base_lock);
45 if (dev_isalive(net))
46 ret = (*format)(net, buf);
47 read_unlock(&dev_base_lock);
48
49 return ret;
50}
51
52/* generate a show function for simple field */
53#define NETDEVICE_SHOW(field, format_string) \
54static ssize_t format_##field(const struct net_device *net, char *buf) \
55{ \
56 return sprintf(buf, format_string, net->field); \
57} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070058static ssize_t show_##field(struct device *dev, \
59 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070061 return netdev_show(dev, attr, buf, format_##field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64
65/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070066static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 const char *buf, size_t len,
68 int (*set)(struct net_device *, unsigned long))
69{
70 struct net_device *net = to_net_dev(dev);
71 char *endp;
72 unsigned long new;
73 int ret = -EINVAL;
74
75 if (!capable(CAP_NET_ADMIN))
76 return -EPERM;
77
78 new = simple_strtoul(buf, &endp, 0);
79 if (endp == buf)
80 goto err;
81
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000082 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000083 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (dev_isalive(net)) {
86 if ((ret = (*set)(net, new)) == 0)
87 ret = len;
88 }
89 rtnl_unlock();
90 err:
91 return ret;
92}
93
David Woodhouse9d296722008-04-20 16:07:43 -070094NETDEVICE_SHOW(dev_id, fmt_hex);
Kay Sieversfd586ba2005-12-19 01:42:56 +010095NETDEVICE_SHOW(addr_len, fmt_dec);
96NETDEVICE_SHOW(iflink, fmt_dec);
97NETDEVICE_SHOW(ifindex, fmt_dec);
98NETDEVICE_SHOW(features, fmt_long_hex);
99NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800100NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700103static ssize_t show_address(struct device *dev, struct device_attribute *attr,
104 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct net_device *net = to_net_dev(dev);
107 ssize_t ret = -EINVAL;
108
109 read_lock(&dev_base_lock);
110 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800111 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 read_unlock(&dev_base_lock);
113 return ret;
114}
115
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700116static ssize_t show_broadcast(struct device *dev,
117 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct net_device *net = to_net_dev(dev);
120 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800121 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return -EINVAL;
123}
124
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700125static ssize_t show_carrier(struct device *dev,
126 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct net_device *netdev = to_net_dev(dev);
129 if (netif_running(netdev)) {
130 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
131 }
132 return -EINVAL;
133}
134
Andy Gospodarekd519e172009-10-02 09:26:12 +0000135static ssize_t show_speed(struct device *dev,
136 struct device_attribute *attr, char *buf)
137{
138 struct net_device *netdev = to_net_dev(dev);
139 int ret = -EINVAL;
140
141 if (!rtnl_trylock())
142 return restart_syscall();
143
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000144 if (netif_running(netdev) &&
145 netdev->ethtool_ops &&
146 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000147 struct ethtool_cmd cmd = { ETHTOOL_GSET };
148
149 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
150 ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
151 }
152 rtnl_unlock();
153 return ret;
154}
155
156static ssize_t show_duplex(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 struct net_device *netdev = to_net_dev(dev);
160 int ret = -EINVAL;
161
162 if (!rtnl_trylock())
163 return restart_syscall();
164
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000165 if (netif_running(netdev) &&
166 netdev->ethtool_ops &&
167 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000168 struct ethtool_cmd cmd = { ETHTOOL_GSET };
169
170 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
171 ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
172 }
173 rtnl_unlock();
174 return ret;
175}
176
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700177static ssize_t show_dormant(struct device *dev,
178 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800179{
180 struct net_device *netdev = to_net_dev(dev);
181
182 if (netif_running(netdev))
183 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
184
185 return -EINVAL;
186}
187
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700188static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800189 "unknown",
190 "notpresent", /* currently unused */
191 "down",
192 "lowerlayerdown",
193 "testing", /* currently unused */
194 "dormant",
195 "up"
196};
197
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700198static ssize_t show_operstate(struct device *dev,
199 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800200{
201 const struct net_device *netdev = to_net_dev(dev);
202 unsigned char operstate;
203
204 read_lock(&dev_base_lock);
205 operstate = netdev->operstate;
206 if (!netif_running(netdev))
207 operstate = IF_OPER_DOWN;
208 read_unlock(&dev_base_lock);
209
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700210 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800211 return -EINVAL; /* should not happen */
212
213 return sprintf(buf, "%s\n", operstates[operstate]);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* read-write attributes */
217NETDEVICE_SHOW(mtu, fmt_dec);
218
219static int change_mtu(struct net_device *net, unsigned long new_mtu)
220{
221 return dev_set_mtu(net, (int) new_mtu);
222}
223
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700224static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
225 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700227 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230NETDEVICE_SHOW(flags, fmt_hex);
231
232static int change_flags(struct net_device *net, unsigned long new_flags)
233{
234 return dev_change_flags(net, (unsigned) new_flags);
235}
236
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700237static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700240 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
244
245static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
246{
247 net->tx_queue_len = new_len;
248 return 0;
249}
250
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700251static ssize_t store_tx_queue_len(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700258static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t len)
260{
261 struct net_device *netdev = to_net_dev(dev);
262 size_t count = len;
263 ssize_t ret;
264
265 if (!capable(CAP_NET_ADMIN))
266 return -EPERM;
267
268 /* ignore trailing newline */
269 if (len > 0 && buf[len - 1] == '\n')
270 --count;
271
Eric W. Biederman336ca572009-05-13 16:57:25 +0000272 if (!rtnl_trylock())
273 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700274 ret = dev_set_alias(netdev, buf, count);
275 rtnl_unlock();
276
277 return ret < 0 ? ret : len;
278}
279
280static ssize_t show_ifalias(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
283 const struct net_device *netdev = to_net_dev(dev);
284 ssize_t ret = 0;
285
Eric W. Biederman336ca572009-05-13 16:57:25 +0000286 if (!rtnl_trylock())
287 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700288 if (netdev->ifalias)
289 ret = sprintf(buf, "%s\n", netdev->ifalias);
290 rtnl_unlock();
291 return ret;
292}
293
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700294static struct device_attribute net_class_attributes[] = {
Kay Sieversfd586ba2005-12-19 01:42:56 +0100295 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
David Woodhouse9d296722008-04-20 16:07:43 -0700296 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700297 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100298 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
299 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
300 __ATTR(features, S_IRUGO, show_features, NULL),
301 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800302 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100303 __ATTR(address, S_IRUGO, show_address, NULL),
304 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
305 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Andy Gospodarekd519e172009-10-02 09:26:12 +0000306 __ATTR(speed, S_IRUGO, show_speed, NULL),
307 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800308 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
309 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100310 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
311 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
312 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
313 store_tx_queue_len),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100314 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
317/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700318static ssize_t netstat_show(const struct device *d,
319 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned long offset)
321{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700322 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ssize_t ret = -EINVAL;
324
Pavel Emelyanovdf1b86c2007-11-30 00:42:42 +1100325 WARN_ON(offset > sizeof(struct net_device_stats) ||
326 offset % sizeof(unsigned long) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700329 if (dev_isalive(dev)) {
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800330 const struct net_device_stats *stats = dev_get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ret = sprintf(buf, fmt_ulong,
332 *(unsigned long *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 read_unlock(&dev_base_lock);
335 return ret;
336}
337
338/* generate a read-only statistics attribute */
339#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700340static ssize_t show_##name(struct device *d, \
341 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700343 return netstat_show(d, attr, buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 offsetof(struct net_device_stats, name)); \
345} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700346static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348NETSTAT_ENTRY(rx_packets);
349NETSTAT_ENTRY(tx_packets);
350NETSTAT_ENTRY(rx_bytes);
351NETSTAT_ENTRY(tx_bytes);
352NETSTAT_ENTRY(rx_errors);
353NETSTAT_ENTRY(tx_errors);
354NETSTAT_ENTRY(rx_dropped);
355NETSTAT_ENTRY(tx_dropped);
356NETSTAT_ENTRY(multicast);
357NETSTAT_ENTRY(collisions);
358NETSTAT_ENTRY(rx_length_errors);
359NETSTAT_ENTRY(rx_over_errors);
360NETSTAT_ENTRY(rx_crc_errors);
361NETSTAT_ENTRY(rx_frame_errors);
362NETSTAT_ENTRY(rx_fifo_errors);
363NETSTAT_ENTRY(rx_missed_errors);
364NETSTAT_ENTRY(tx_aborted_errors);
365NETSTAT_ENTRY(tx_carrier_errors);
366NETSTAT_ENTRY(tx_fifo_errors);
367NETSTAT_ENTRY(tx_heartbeat_errors);
368NETSTAT_ENTRY(tx_window_errors);
369NETSTAT_ENTRY(rx_compressed);
370NETSTAT_ENTRY(tx_compressed);
371
372static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700373 &dev_attr_rx_packets.attr,
374 &dev_attr_tx_packets.attr,
375 &dev_attr_rx_bytes.attr,
376 &dev_attr_tx_bytes.attr,
377 &dev_attr_rx_errors.attr,
378 &dev_attr_tx_errors.attr,
379 &dev_attr_rx_dropped.attr,
380 &dev_attr_tx_dropped.attr,
381 &dev_attr_multicast.attr,
382 &dev_attr_collisions.attr,
383 &dev_attr_rx_length_errors.attr,
384 &dev_attr_rx_over_errors.attr,
385 &dev_attr_rx_crc_errors.attr,
386 &dev_attr_rx_frame_errors.attr,
387 &dev_attr_rx_fifo_errors.attr,
388 &dev_attr_rx_missed_errors.attr,
389 &dev_attr_tx_aborted_errors.attr,
390 &dev_attr_tx_carrier_errors.attr,
391 &dev_attr_tx_fifo_errors.attr,
392 &dev_attr_tx_heartbeat_errors.attr,
393 &dev_attr_tx_window_errors.attr,
394 &dev_attr_rx_compressed.attr,
395 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 NULL
397};
398
399
400static struct attribute_group netstat_group = {
401 .name = "statistics",
402 .attrs = netstat_attrs,
403};
404
Johannes Berg22bb1be2008-07-10 11:16:47 +0200405#ifdef CONFIG_WIRELESS_EXT_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* helper function that does all the locking etc for wireless stats */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700407static ssize_t wireless_show(struct device *d, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ssize_t (*format)(const struct iw_statistics *,
409 char *))
410{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411 struct net_device *dev = to_net_dev(d);
Johannes Berg8f1546c2009-09-28 15:26:43 +0200412 const struct iw_statistics *iw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ssize_t ret = -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900414
Eric W. Biedermanb8afe642010-02-19 13:23:47 +0000415 if (!rtnl_trylock())
416 return restart_syscall();
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800417 if (dev_isalive(dev)) {
Johannes Berg8f1546c2009-09-28 15:26:43 +0200418 iw = get_wireless_stats(dev);
419 if (iw)
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800420 ret = (*format)(iw, buf);
421 }
Johannes Berga160ee62009-10-05 02:22:23 -0700422 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 return ret;
425}
426
427/* show function template for wireless fields */
428#define WIRELESS_SHOW(name, field, format_string) \
429static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
430{ \
431 return sprintf(buf, format_string, iw->field); \
432} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700433static ssize_t show_iw_##name(struct device *d, \
434 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700436 return wireless_show(d, buf, format_iw_##name); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700438static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440WIRELESS_SHOW(status, status, fmt_hex);
441WIRELESS_SHOW(link, qual.qual, fmt_dec);
442WIRELESS_SHOW(level, qual.level, fmt_dec);
443WIRELESS_SHOW(noise, qual.noise, fmt_dec);
444WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
445WIRELESS_SHOW(crypt, discard.code, fmt_dec);
446WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
447WIRELESS_SHOW(misc, discard.misc, fmt_dec);
448WIRELESS_SHOW(retries, discard.retries, fmt_dec);
449WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
450
451static struct attribute *wireless_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700452 &dev_attr_status.attr,
453 &dev_attr_link.attr,
454 &dev_attr_level.attr,
455 &dev_attr_noise.attr,
456 &dev_attr_nwid.attr,
457 &dev_attr_crypt.attr,
458 &dev_attr_fragment.attr,
459 &dev_attr_retries.attr,
460 &dev_attr_misc.attr,
461 &dev_attr_beacon.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 NULL
463};
464
465static struct attribute_group wireless_group = {
466 .name = "wireless",
467 .attrs = wireless_attrs,
468};
469#endif
470
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700471#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000472/*
473 * RX queue sysfs structures and functions.
474 */
475struct rx_queue_attribute {
476 struct attribute attr;
477 ssize_t (*show)(struct netdev_rx_queue *queue,
478 struct rx_queue_attribute *attr, char *buf);
479 ssize_t (*store)(struct netdev_rx_queue *queue,
480 struct rx_queue_attribute *attr, const char *buf, size_t len);
481};
482#define to_rx_queue_attr(_attr) container_of(_attr, \
483 struct rx_queue_attribute, attr)
484
485#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
486
487static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
488 char *buf)
489{
490 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
491 struct netdev_rx_queue *queue = to_rx_queue(kobj);
492
493 if (!attribute->show)
494 return -EIO;
495
496 return attribute->show(queue, attribute, buf);
497}
498
499static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
500 const char *buf, size_t count)
501{
502 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
503 struct netdev_rx_queue *queue = to_rx_queue(kobj);
504
505 if (!attribute->store)
506 return -EIO;
507
508 return attribute->store(queue, attribute, buf, count);
509}
510
511static struct sysfs_ops rx_queue_sysfs_ops = {
512 .show = rx_queue_attr_show,
513 .store = rx_queue_attr_store,
514};
515
516static ssize_t show_rps_map(struct netdev_rx_queue *queue,
517 struct rx_queue_attribute *attribute, char *buf)
518{
519 struct rps_map *map;
520 cpumask_var_t mask;
521 size_t len = 0;
522 int i;
523
524 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
525 return -ENOMEM;
526
527 rcu_read_lock();
528 map = rcu_dereference(queue->rps_map);
529 if (map)
530 for (i = 0; i < map->len; i++)
531 cpumask_set_cpu(map->cpus[i], mask);
532
533 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
534 if (PAGE_SIZE - len < 3) {
535 rcu_read_unlock();
536 free_cpumask_var(mask);
537 return -EINVAL;
538 }
539 rcu_read_unlock();
540
541 free_cpumask_var(mask);
542 len += sprintf(buf + len, "\n");
543 return len;
544}
545
546static void rps_map_release(struct rcu_head *rcu)
547{
548 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
549
550 kfree(map);
551}
552
553ssize_t store_rps_map(struct netdev_rx_queue *queue,
554 struct rx_queue_attribute *attribute,
555 const char *buf, size_t len)
556{
557 struct rps_map *old_map, *map;
558 cpumask_var_t mask;
559 int err, cpu, i;
560 static DEFINE_SPINLOCK(rps_map_lock);
561
562 if (!capable(CAP_NET_ADMIN))
563 return -EPERM;
564
565 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
566 return -ENOMEM;
567
568 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
569 if (err) {
570 free_cpumask_var(mask);
571 return err;
572 }
573
574 map = kzalloc(max_t(unsigned,
575 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
576 GFP_KERNEL);
577 if (!map) {
578 free_cpumask_var(mask);
579 return -ENOMEM;
580 }
581
582 i = 0;
583 for_each_cpu_and(cpu, mask, cpu_online_mask)
584 map->cpus[i++] = cpu;
585
586 if (i)
587 map->len = i;
588 else {
589 kfree(map);
590 map = NULL;
591 }
592
593 spin_lock(&rps_map_lock);
594 old_map = queue->rps_map;
595 rcu_assign_pointer(queue->rps_map, map);
596 spin_unlock(&rps_map_lock);
597
598 if (old_map)
599 call_rcu(&old_map->rcu, rps_map_release);
600
601 free_cpumask_var(mask);
602 return len;
603}
604
Tom Herbertfec5e652010-04-16 16:01:27 -0700605static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
606 struct rx_queue_attribute *attr,
607 char *buf)
608{
609 struct rps_dev_flow_table *flow_table;
610 unsigned int val = 0;
611
612 rcu_read_lock();
613 flow_table = rcu_dereference(queue->rps_flow_table);
614 if (flow_table)
615 val = flow_table->mask + 1;
616 rcu_read_unlock();
617
618 return sprintf(buf, "%u\n", val);
619}
620
621static void rps_dev_flow_table_release_work(struct work_struct *work)
622{
623 struct rps_dev_flow_table *table = container_of(work,
624 struct rps_dev_flow_table, free_work);
625
626 vfree(table);
627}
628
629static void rps_dev_flow_table_release(struct rcu_head *rcu)
630{
631 struct rps_dev_flow_table *table = container_of(rcu,
632 struct rps_dev_flow_table, rcu);
633
634 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
635 schedule_work(&table->free_work);
636}
637
638ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
639 struct rx_queue_attribute *attr,
640 const char *buf, size_t len)
641{
642 unsigned int count;
643 char *endp;
644 struct rps_dev_flow_table *table, *old_table;
645 static DEFINE_SPINLOCK(rps_dev_flow_lock);
646
647 if (!capable(CAP_NET_ADMIN))
648 return -EPERM;
649
650 count = simple_strtoul(buf, &endp, 0);
651 if (endp == buf)
652 return -EINVAL;
653
654 if (count) {
655 int i;
656
657 if (count > 1<<30) {
658 /* Enforce a limit to prevent overflow */
659 return -EINVAL;
660 }
661 count = roundup_pow_of_two(count);
662 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
663 if (!table)
664 return -ENOMEM;
665
666 table->mask = count - 1;
667 for (i = 0; i < count; i++)
668 table->flows[i].cpu = RPS_NO_CPU;
669 } else
670 table = NULL;
671
672 spin_lock(&rps_dev_flow_lock);
673 old_table = queue->rps_flow_table;
674 rcu_assign_pointer(queue->rps_flow_table, table);
675 spin_unlock(&rps_dev_flow_lock);
676
677 if (old_table)
678 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
679
680 return len;
681}
682
Tom Herbert0a9627f2010-03-16 08:03:29 +0000683static struct rx_queue_attribute rps_cpus_attribute =
684 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
685
Tom Herbertfec5e652010-04-16 16:01:27 -0700686
687static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
688 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
689 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
690
Tom Herbert0a9627f2010-03-16 08:03:29 +0000691static struct attribute *rx_queue_default_attrs[] = {
692 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700693 &rps_dev_flow_table_cnt_attribute.attr,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000694 NULL
695};
696
697static void rx_queue_release(struct kobject *kobj)
698{
699 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000700 struct netdev_rx_queue *first = queue->first;
701
Tom Herbertfec5e652010-04-16 16:01:27 -0700702 if (queue->rps_map)
703 call_rcu(&queue->rps_map->rcu, rps_map_release);
704
705 if (queue->rps_flow_table)
706 call_rcu(&queue->rps_flow_table->rcu,
707 rps_dev_flow_table_release);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000708
709 if (atomic_dec_and_test(&first->count))
710 kfree(first);
711}
712
713static struct kobj_type rx_queue_ktype = {
714 .sysfs_ops = &rx_queue_sysfs_ops,
715 .release = rx_queue_release,
716 .default_attrs = rx_queue_default_attrs,
717};
718
719static int rx_queue_add_kobject(struct net_device *net, int index)
720{
721 struct netdev_rx_queue *queue = net->_rx + index;
722 struct kobject *kobj = &queue->kobj;
723 int error = 0;
724
725 kobj->kset = net->queues_kset;
726 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
727 "rx-%u", index);
728 if (error) {
729 kobject_put(kobj);
730 return error;
731 }
732
733 kobject_uevent(kobj, KOBJ_ADD);
734
735 return error;
736}
737
738static int rx_queue_register_kobjects(struct net_device *net)
739{
740 int i;
741 int error = 0;
742
743 net->queues_kset = kset_create_and_add("queues",
744 NULL, &net->dev.kobj);
745 if (!net->queues_kset)
746 return -ENOMEM;
747 for (i = 0; i < net->num_rx_queues; i++) {
748 error = rx_queue_add_kobject(net, i);
749 if (error)
750 break;
751 }
752
753 if (error)
754 while (--i >= 0)
755 kobject_put(&net->_rx[i].kobj);
756
757 return error;
758}
759
760static void rx_queue_remove_kobjects(struct net_device *net)
761{
762 int i;
763
764 for (i = 0; i < net->num_rx_queues; i++)
765 kobject_put(&net->_rx[i].kobj);
766 kset_unregister(net->queues_kset);
767}
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700768#endif /* CONFIG_RPS */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700769#endif /* CONFIG_SYSFS */
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200772static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700774 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200775 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Daniel Lezcano09bb5212008-11-25 16:46:37 -0800777 if (!net_eq(dev_net(dev), &init_net))
778 return 0;
779
Kay Sievers312c0042005-11-16 09:00:00 +0100780 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200781 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -0700782 if (retval)
783 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800785 /* pass ifindex to uevent.
786 * ifindex is useful as it won't change (interface name may change)
787 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200788 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800789
Eric Rannaudbf624562007-03-30 22:23:12 -0700790exit:
Eric Rannaudbf624562007-03-30 22:23:12 -0700791 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793#endif
794
795/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900796 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700797 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700799static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700801 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 BUG_ON(dev->reg_state != NETREG_RELEASED);
804
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700805 kfree(dev->ifalias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 kfree((char *)dev - dev->padded);
807}
808
809static struct class net_class = {
810 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700811 .dev_release = netdev_release,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700812#ifdef CONFIG_SYSFS
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700813 .dev_attrs = net_class_attributes,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700814#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#ifdef CONFIG_HOTPLUG
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700816 .dev_uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817#endif
818};
819
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700820/* Delete sysfs entries but hold kobject reference until after all
821 * netdev references are gone.
822 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700823void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700825 struct device *dev = &(net->dev);
826
827 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -0700828
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800829 if (!net_eq(dev_net(net), &init_net))
Eric W. Biederman38918452008-10-27 17:51:47 -0700830 return;
831
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700832#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000833 rx_queue_remove_kobjects(net);
Tom Herberte880eb62010-03-22 18:06:47 -0700834#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000835
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700836 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700840int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700842 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -0700843 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000844 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700846 dev->class = &net_class;
847 dev->platform_data = net;
848 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Stephen Hemmingera2205472009-03-09 13:51:55 +0000850 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700852#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000853 /* Allow for a device specific group */
854 if (*groups)
855 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000857 *groups++ = &netstat_group;
Johannes Berg22bb1be2008-07-10 11:16:47 +0200858#ifdef CONFIG_WIRELESS_EXT_SYSFS
Johannes Berg3d23e342009-09-29 23:27:28 +0200859 if (net->ieee80211_ptr)
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700860 *groups++ = &wireless_group;
Johannes Berg3d23e342009-09-29 23:27:28 +0200861#ifdef CONFIG_WIRELESS_EXT
862 else if (net->wireless_handlers)
863 *groups++ = &wireless_group;
864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700866#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800868 if (!net_eq(dev_net(net), &init_net))
Eric W. Biederman38918452008-10-27 17:51:47 -0700869 return 0;
870
Tom Herbert0a9627f2010-03-16 08:03:29 +0000871 error = device_add(dev);
872 if (error)
873 return error;
874
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700875#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000876 error = rx_queue_register_kobjects(net);
877 if (error) {
878 device_del(dev);
879 return error;
880 }
Tom Herberte880eb62010-03-22 18:06:47 -0700881#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000882
883 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Jay Vosburghb8a97872008-06-13 18:12:04 -0700886int netdev_class_create_file(struct class_attribute *class_attr)
887{
888 return class_create_file(&net_class, class_attr);
889}
890
891void netdev_class_remove_file(struct class_attribute *class_attr)
892{
893 class_remove_file(&net_class, class_attr);
894}
895
896EXPORT_SYMBOL(netdev_class_create_file);
897EXPORT_SYMBOL(netdev_class_remove_file);
898
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -0700899void netdev_initialize_kobject(struct net_device *net)
900{
901 struct device *device = &(net->dev);
902 device_initialize(device);
903}
904
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700905int netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 return class_register(&net_class);
908}