blob: d2b596537d416f2b0f3fd3daaae5e9f9cc931be4 [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";
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +000032static const char fmt_u64[] = "%llu\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090034static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070036 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070040static ssize_t netdev_show(const struct device *dev,
41 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ssize_t (*format)(const struct net_device *, char *))
43{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070044 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ssize_t ret = -EINVAL;
46
47 read_lock(&dev_base_lock);
48 if (dev_isalive(net))
49 ret = (*format)(net, buf);
50 read_unlock(&dev_base_lock);
51
52 return ret;
53}
54
55/* generate a show function for simple field */
56#define NETDEVICE_SHOW(field, format_string) \
57static ssize_t format_##field(const struct net_device *net, char *buf) \
58{ \
59 return sprintf(buf, format_string, net->field); \
60} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070061static ssize_t show_##field(struct device *dev, \
62 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070064 return netdev_show(dev, attr, buf, format_##field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67
68/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070069static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 const char *buf, size_t len,
71 int (*set)(struct net_device *, unsigned long))
72{
73 struct net_device *net = to_net_dev(dev);
74 char *endp;
75 unsigned long new;
76 int ret = -EINVAL;
77
78 if (!capable(CAP_NET_ADMIN))
79 return -EPERM;
80
81 new = simple_strtoul(buf, &endp, 0);
82 if (endp == buf)
83 goto err;
84
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000085 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000086 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (dev_isalive(net)) {
89 if ((ret = (*set)(net, new)) == 0)
90 ret = len;
91 }
92 rtnl_unlock();
93 err:
94 return ret;
95}
96
David Woodhouse9d296722008-04-20 16:07:43 -070097NETDEVICE_SHOW(dev_id, fmt_hex);
Kay Sieversfd586ba2005-12-19 01:42:56 +010098NETDEVICE_SHOW(addr_len, fmt_dec);
99NETDEVICE_SHOW(iflink, fmt_dec);
100NETDEVICE_SHOW(ifindex, fmt_dec);
101NETDEVICE_SHOW(features, fmt_long_hex);
102NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800103NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700106static ssize_t show_address(struct device *dev, struct device_attribute *attr,
107 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct net_device *net = to_net_dev(dev);
110 ssize_t ret = -EINVAL;
111
112 read_lock(&dev_base_lock);
113 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800114 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 read_unlock(&dev_base_lock);
116 return ret;
117}
118
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700119static ssize_t show_broadcast(struct device *dev,
120 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct net_device *net = to_net_dev(dev);
123 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800124 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
126}
127
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700128static ssize_t show_carrier(struct device *dev,
129 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct net_device *netdev = to_net_dev(dev);
132 if (netif_running(netdev)) {
133 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
134 }
135 return -EINVAL;
136}
137
Andy Gospodarekd519e172009-10-02 09:26:12 +0000138static ssize_t show_speed(struct device *dev,
139 struct device_attribute *attr, char *buf)
140{
141 struct net_device *netdev = to_net_dev(dev);
142 int ret = -EINVAL;
143
144 if (!rtnl_trylock())
145 return restart_syscall();
146
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000147 if (netif_running(netdev) &&
148 netdev->ethtool_ops &&
149 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000150 struct ethtool_cmd cmd = { ETHTOOL_GSET };
151
152 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
153 ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
154 }
155 rtnl_unlock();
156 return ret;
157}
158
159static ssize_t show_duplex(struct device *dev,
160 struct device_attribute *attr, char *buf)
161{
162 struct net_device *netdev = to_net_dev(dev);
163 int ret = -EINVAL;
164
165 if (!rtnl_trylock())
166 return restart_syscall();
167
Eric Dumazetac5e3af2009-10-26 01:23:33 +0000168 if (netif_running(netdev) &&
169 netdev->ethtool_ops &&
170 netdev->ethtool_ops->get_settings) {
Andy Gospodarekd519e172009-10-02 09:26:12 +0000171 struct ethtool_cmd cmd = { ETHTOOL_GSET };
172
173 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
174 ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
175 }
176 rtnl_unlock();
177 return ret;
178}
179
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700180static ssize_t show_dormant(struct device *dev,
181 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800182{
183 struct net_device *netdev = to_net_dev(dev);
184
185 if (netif_running(netdev))
186 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
187
188 return -EINVAL;
189}
190
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700191static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800192 "unknown",
193 "notpresent", /* currently unused */
194 "down",
195 "lowerlayerdown",
196 "testing", /* currently unused */
197 "dormant",
198 "up"
199};
200
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700201static ssize_t show_operstate(struct device *dev,
202 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800203{
204 const struct net_device *netdev = to_net_dev(dev);
205 unsigned char operstate;
206
207 read_lock(&dev_base_lock);
208 operstate = netdev->operstate;
209 if (!netif_running(netdev))
210 operstate = IF_OPER_DOWN;
211 read_unlock(&dev_base_lock);
212
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700213 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800214 return -EINVAL; /* should not happen */
215
216 return sprintf(buf, "%s\n", operstates[operstate]);
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/* read-write attributes */
220NETDEVICE_SHOW(mtu, fmt_dec);
221
222static int change_mtu(struct net_device *net, unsigned long new_mtu)
223{
224 return dev_set_mtu(net, (int) new_mtu);
225}
226
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700227static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
228 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700230 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233NETDEVICE_SHOW(flags, fmt_hex);
234
235static int change_flags(struct net_device *net, unsigned long new_flags)
236{
237 return dev_change_flags(net, (unsigned) new_flags);
238}
239
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700240static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
241 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700243 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
247
248static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
249{
250 net->tx_queue_len = new_len;
251 return 0;
252}
253
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700254static ssize_t store_tx_queue_len(struct device *dev,
255 struct device_attribute *attr,
256 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700258 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700261static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
262 const char *buf, size_t len)
263{
264 struct net_device *netdev = to_net_dev(dev);
265 size_t count = len;
266 ssize_t ret;
267
268 if (!capable(CAP_NET_ADMIN))
269 return -EPERM;
270
271 /* ignore trailing newline */
272 if (len > 0 && buf[len - 1] == '\n')
273 --count;
274
Eric W. Biederman336ca572009-05-13 16:57:25 +0000275 if (!rtnl_trylock())
276 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700277 ret = dev_set_alias(netdev, buf, count);
278 rtnl_unlock();
279
280 return ret < 0 ? ret : len;
281}
282
283static ssize_t show_ifalias(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
286 const struct net_device *netdev = to_net_dev(dev);
287 ssize_t ret = 0;
288
Eric W. Biederman336ca572009-05-13 16:57:25 +0000289 if (!rtnl_trylock())
290 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700291 if (netdev->ifalias)
292 ret = sprintf(buf, "%s\n", netdev->ifalias);
293 rtnl_unlock();
294 return ret;
295}
296
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700297static struct device_attribute net_class_attributes[] = {
Kay Sieversfd586ba2005-12-19 01:42:56 +0100298 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
David Woodhouse9d296722008-04-20 16:07:43 -0700299 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700300 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100301 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
302 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
303 __ATTR(features, S_IRUGO, show_features, NULL),
304 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800305 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100306 __ATTR(address, S_IRUGO, show_address, NULL),
307 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
308 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Andy Gospodarekd519e172009-10-02 09:26:12 +0000309 __ATTR(speed, S_IRUGO, show_speed, NULL),
310 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800311 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
312 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100313 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
314 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
315 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
316 store_tx_queue_len),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100317 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
320/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700321static ssize_t netstat_show(const struct device *d,
322 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned long offset)
324{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700325 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ssize_t ret = -EINVAL;
327
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000328 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
329 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700332 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700333 struct rtnl_link_stats64 temp;
334 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
335
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000336 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 read_unlock(&dev_base_lock);
339 return ret;
340}
341
342/* generate a read-only statistics attribute */
343#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700344static ssize_t show_##name(struct device *d, \
345 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700347 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000348 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352NETSTAT_ENTRY(rx_packets);
353NETSTAT_ENTRY(tx_packets);
354NETSTAT_ENTRY(rx_bytes);
355NETSTAT_ENTRY(tx_bytes);
356NETSTAT_ENTRY(rx_errors);
357NETSTAT_ENTRY(tx_errors);
358NETSTAT_ENTRY(rx_dropped);
359NETSTAT_ENTRY(tx_dropped);
360NETSTAT_ENTRY(multicast);
361NETSTAT_ENTRY(collisions);
362NETSTAT_ENTRY(rx_length_errors);
363NETSTAT_ENTRY(rx_over_errors);
364NETSTAT_ENTRY(rx_crc_errors);
365NETSTAT_ENTRY(rx_frame_errors);
366NETSTAT_ENTRY(rx_fifo_errors);
367NETSTAT_ENTRY(rx_missed_errors);
368NETSTAT_ENTRY(tx_aborted_errors);
369NETSTAT_ENTRY(tx_carrier_errors);
370NETSTAT_ENTRY(tx_fifo_errors);
371NETSTAT_ENTRY(tx_heartbeat_errors);
372NETSTAT_ENTRY(tx_window_errors);
373NETSTAT_ENTRY(rx_compressed);
374NETSTAT_ENTRY(tx_compressed);
375
376static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700377 &dev_attr_rx_packets.attr,
378 &dev_attr_tx_packets.attr,
379 &dev_attr_rx_bytes.attr,
380 &dev_attr_tx_bytes.attr,
381 &dev_attr_rx_errors.attr,
382 &dev_attr_tx_errors.attr,
383 &dev_attr_rx_dropped.attr,
384 &dev_attr_tx_dropped.attr,
385 &dev_attr_multicast.attr,
386 &dev_attr_collisions.attr,
387 &dev_attr_rx_length_errors.attr,
388 &dev_attr_rx_over_errors.attr,
389 &dev_attr_rx_crc_errors.attr,
390 &dev_attr_rx_frame_errors.attr,
391 &dev_attr_rx_fifo_errors.attr,
392 &dev_attr_rx_missed_errors.attr,
393 &dev_attr_tx_aborted_errors.attr,
394 &dev_attr_tx_carrier_errors.attr,
395 &dev_attr_tx_fifo_errors.attr,
396 &dev_attr_tx_heartbeat_errors.attr,
397 &dev_attr_tx_window_errors.attr,
398 &dev_attr_rx_compressed.attr,
399 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 NULL
401};
402
403
404static struct attribute_group netstat_group = {
405 .name = "statistics",
406 .attrs = netstat_attrs,
407};
408
Johannes Berg22bb1be2008-07-10 11:16:47 +0200409#ifdef CONFIG_WIRELESS_EXT_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* helper function that does all the locking etc for wireless stats */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411static ssize_t wireless_show(struct device *d, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ssize_t (*format)(const struct iw_statistics *,
413 char *))
414{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700415 struct net_device *dev = to_net_dev(d);
Johannes Berg8f1546c2009-09-28 15:26:43 +0200416 const struct iw_statistics *iw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ssize_t ret = -EINVAL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900418
Eric W. Biedermanb8afe642010-02-19 13:23:47 +0000419 if (!rtnl_trylock())
420 return restart_syscall();
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800421 if (dev_isalive(dev)) {
Johannes Berg8f1546c2009-09-28 15:26:43 +0200422 iw = get_wireless_stats(dev);
423 if (iw)
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800424 ret = (*format)(iw, buf);
425 }
Johannes Berga160ee62009-10-05 02:22:23 -0700426 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return ret;
429}
430
431/* show function template for wireless fields */
432#define WIRELESS_SHOW(name, field, format_string) \
433static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
434{ \
435 return sprintf(buf, format_string, iw->field); \
436} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700437static ssize_t show_iw_##name(struct device *d, \
438 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700440 return wireless_show(d, buf, format_iw_##name); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700442static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444WIRELESS_SHOW(status, status, fmt_hex);
445WIRELESS_SHOW(link, qual.qual, fmt_dec);
446WIRELESS_SHOW(level, qual.level, fmt_dec);
447WIRELESS_SHOW(noise, qual.noise, fmt_dec);
448WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
449WIRELESS_SHOW(crypt, discard.code, fmt_dec);
450WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
451WIRELESS_SHOW(misc, discard.misc, fmt_dec);
452WIRELESS_SHOW(retries, discard.retries, fmt_dec);
453WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
454
455static struct attribute *wireless_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700456 &dev_attr_status.attr,
457 &dev_attr_link.attr,
458 &dev_attr_level.attr,
459 &dev_attr_noise.attr,
460 &dev_attr_nwid.attr,
461 &dev_attr_crypt.attr,
462 &dev_attr_fragment.attr,
463 &dev_attr_retries.attr,
464 &dev_attr_misc.attr,
465 &dev_attr_beacon.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 NULL
467};
468
469static struct attribute_group wireless_group = {
470 .name = "wireless",
471 .attrs = wireless_attrs,
472};
473#endif
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700474#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700476#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000477/*
478 * RX queue sysfs structures and functions.
479 */
480struct rx_queue_attribute {
481 struct attribute attr;
482 ssize_t (*show)(struct netdev_rx_queue *queue,
483 struct rx_queue_attribute *attr, char *buf);
484 ssize_t (*store)(struct netdev_rx_queue *queue,
485 struct rx_queue_attribute *attr, const char *buf, size_t len);
486};
487#define to_rx_queue_attr(_attr) container_of(_attr, \
488 struct rx_queue_attribute, attr)
489
490#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
491
492static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
493 char *buf)
494{
495 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
496 struct netdev_rx_queue *queue = to_rx_queue(kobj);
497
498 if (!attribute->show)
499 return -EIO;
500
501 return attribute->show(queue, attribute, buf);
502}
503
504static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
505 const char *buf, size_t count)
506{
507 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
508 struct netdev_rx_queue *queue = to_rx_queue(kobj);
509
510 if (!attribute->store)
511 return -EIO;
512
513 return attribute->store(queue, attribute, buf, count);
514}
515
516static struct sysfs_ops rx_queue_sysfs_ops = {
517 .show = rx_queue_attr_show,
518 .store = rx_queue_attr_store,
519};
520
521static ssize_t show_rps_map(struct netdev_rx_queue *queue,
522 struct rx_queue_attribute *attribute, char *buf)
523{
524 struct rps_map *map;
525 cpumask_var_t mask;
526 size_t len = 0;
527 int i;
528
529 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
530 return -ENOMEM;
531
532 rcu_read_lock();
533 map = rcu_dereference(queue->rps_map);
534 if (map)
535 for (i = 0; i < map->len; i++)
536 cpumask_set_cpu(map->cpus[i], mask);
537
538 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
539 if (PAGE_SIZE - len < 3) {
540 rcu_read_unlock();
541 free_cpumask_var(mask);
542 return -EINVAL;
543 }
544 rcu_read_unlock();
545
546 free_cpumask_var(mask);
547 len += sprintf(buf + len, "\n");
548 return len;
549}
550
551static void rps_map_release(struct rcu_head *rcu)
552{
553 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
554
555 kfree(map);
556}
557
Eric Dumazetf5acb902010-04-19 14:40:57 -0700558static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000559 struct rx_queue_attribute *attribute,
560 const char *buf, size_t len)
561{
562 struct rps_map *old_map, *map;
563 cpumask_var_t mask;
564 int err, cpu, i;
565 static DEFINE_SPINLOCK(rps_map_lock);
566
567 if (!capable(CAP_NET_ADMIN))
568 return -EPERM;
569
570 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
571 return -ENOMEM;
572
573 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
574 if (err) {
575 free_cpumask_var(mask);
576 return err;
577 }
578
579 map = kzalloc(max_t(unsigned,
580 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
581 GFP_KERNEL);
582 if (!map) {
583 free_cpumask_var(mask);
584 return -ENOMEM;
585 }
586
587 i = 0;
588 for_each_cpu_and(cpu, mask, cpu_online_mask)
589 map->cpus[i++] = cpu;
590
591 if (i)
592 map->len = i;
593 else {
594 kfree(map);
595 map = NULL;
596 }
597
598 spin_lock(&rps_map_lock);
599 old_map = queue->rps_map;
600 rcu_assign_pointer(queue->rps_map, map);
601 spin_unlock(&rps_map_lock);
602
603 if (old_map)
604 call_rcu(&old_map->rcu, rps_map_release);
605
606 free_cpumask_var(mask);
607 return len;
608}
609
Tom Herbertfec5e652010-04-16 16:01:27 -0700610static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
611 struct rx_queue_attribute *attr,
612 char *buf)
613{
614 struct rps_dev_flow_table *flow_table;
615 unsigned int val = 0;
616
617 rcu_read_lock();
618 flow_table = rcu_dereference(queue->rps_flow_table);
619 if (flow_table)
620 val = flow_table->mask + 1;
621 rcu_read_unlock();
622
623 return sprintf(buf, "%u\n", val);
624}
625
626static void rps_dev_flow_table_release_work(struct work_struct *work)
627{
628 struct rps_dev_flow_table *table = container_of(work,
629 struct rps_dev_flow_table, free_work);
630
631 vfree(table);
632}
633
634static void rps_dev_flow_table_release(struct rcu_head *rcu)
635{
636 struct rps_dev_flow_table *table = container_of(rcu,
637 struct rps_dev_flow_table, rcu);
638
639 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
640 schedule_work(&table->free_work);
641}
642
Eric Dumazetf5acb902010-04-19 14:40:57 -0700643static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700644 struct rx_queue_attribute *attr,
645 const char *buf, size_t len)
646{
647 unsigned int count;
648 char *endp;
649 struct rps_dev_flow_table *table, *old_table;
650 static DEFINE_SPINLOCK(rps_dev_flow_lock);
651
652 if (!capable(CAP_NET_ADMIN))
653 return -EPERM;
654
655 count = simple_strtoul(buf, &endp, 0);
656 if (endp == buf)
657 return -EINVAL;
658
659 if (count) {
660 int i;
661
662 if (count > 1<<30) {
663 /* Enforce a limit to prevent overflow */
664 return -EINVAL;
665 }
666 count = roundup_pow_of_two(count);
667 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
668 if (!table)
669 return -ENOMEM;
670
671 table->mask = count - 1;
672 for (i = 0; i < count; i++)
673 table->flows[i].cpu = RPS_NO_CPU;
674 } else
675 table = NULL;
676
677 spin_lock(&rps_dev_flow_lock);
678 old_table = queue->rps_flow_table;
679 rcu_assign_pointer(queue->rps_flow_table, table);
680 spin_unlock(&rps_dev_flow_lock);
681
682 if (old_table)
683 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
684
685 return len;
686}
687
Tom Herbert0a9627f2010-03-16 08:03:29 +0000688static struct rx_queue_attribute rps_cpus_attribute =
689 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
690
Tom Herbertfec5e652010-04-16 16:01:27 -0700691
692static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
693 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
694 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
695
Tom Herbert0a9627f2010-03-16 08:03:29 +0000696static struct attribute *rx_queue_default_attrs[] = {
697 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700698 &rps_dev_flow_table_cnt_attribute.attr,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000699 NULL
700};
701
702static void rx_queue_release(struct kobject *kobj)
703{
704 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000705 struct netdev_rx_queue *first = queue->first;
706
Tom Herbertfec5e652010-04-16 16:01:27 -0700707 if (queue->rps_map)
708 call_rcu(&queue->rps_map->rcu, rps_map_release);
709
710 if (queue->rps_flow_table)
711 call_rcu(&queue->rps_flow_table->rcu,
712 rps_dev_flow_table_release);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000713
714 if (atomic_dec_and_test(&first->count))
715 kfree(first);
716}
717
718static struct kobj_type rx_queue_ktype = {
719 .sysfs_ops = &rx_queue_sysfs_ops,
720 .release = rx_queue_release,
721 .default_attrs = rx_queue_default_attrs,
722};
723
724static int rx_queue_add_kobject(struct net_device *net, int index)
725{
726 struct netdev_rx_queue *queue = net->_rx + index;
727 struct kobject *kobj = &queue->kobj;
728 int error = 0;
729
730 kobj->kset = net->queues_kset;
731 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
732 "rx-%u", index);
733 if (error) {
734 kobject_put(kobj);
735 return error;
736 }
737
738 kobject_uevent(kobj, KOBJ_ADD);
739
740 return error;
741}
742
743static int rx_queue_register_kobjects(struct net_device *net)
744{
745 int i;
746 int error = 0;
747
748 net->queues_kset = kset_create_and_add("queues",
749 NULL, &net->dev.kobj);
750 if (!net->queues_kset)
751 return -ENOMEM;
752 for (i = 0; i < net->num_rx_queues; i++) {
753 error = rx_queue_add_kobject(net, i);
754 if (error)
755 break;
756 }
757
758 if (error)
759 while (--i >= 0)
760 kobject_put(&net->_rx[i].kobj);
761
762 return error;
763}
764
765static void rx_queue_remove_kobjects(struct net_device *net)
766{
767 int i;
768
769 for (i = 0; i < net->num_rx_queues; i++)
770 kobject_put(&net->_rx[i].kobj);
771 kset_unregister(net->queues_kset);
772}
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700773#endif /* CONFIG_RPS */
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700774
775static const void *net_current_ns(void)
776{
777 return current->nsproxy->net_ns;
778}
779
780static const void *net_initial_ns(void)
781{
782 return &init_net;
783}
784
785static const void *net_netlink_ns(struct sock *sk)
786{
787 return sock_net(sk);
788}
789
790static struct kobj_ns_type_operations net_ns_type_operations = {
791 .type = KOBJ_NS_TYPE_NET,
792 .current_ns = net_current_ns,
793 .netlink_ns = net_netlink_ns,
794 .initial_ns = net_initial_ns,
795};
796
797static void net_kobj_ns_exit(struct net *net)
798{
799 kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
800}
801
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700802static struct pernet_operations kobj_net_ops = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700803 .exit = net_kobj_ns_exit,
804};
805
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200808static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700810 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200811 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Kay Sievers312c0042005-11-16 09:00:00 +0100813 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200814 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -0700815 if (retval)
816 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800818 /* pass ifindex to uevent.
819 * ifindex is useful as it won't change (interface name may change)
820 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200821 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800822
Eric Rannaudbf624562007-03-30 22:23:12 -0700823exit:
Eric Rannaudbf624562007-03-30 22:23:12 -0700824 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826#endif
827
828/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900829 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700830 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700832static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700834 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 BUG_ON(dev->reg_state != NETREG_RELEASED);
837
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700838 kfree(dev->ifalias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 kfree((char *)dev - dev->padded);
840}
841
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700842static const void *net_namespace(struct device *d)
843{
844 struct net_device *dev;
845 dev = container_of(d, struct net_device, dev);
846 return dev_net(dev);
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static struct class net_class = {
850 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700851 .dev_release = netdev_release,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700852#ifdef CONFIG_SYSFS
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700853 .dev_attrs = net_class_attributes,
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700854#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#ifdef CONFIG_HOTPLUG
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700856 .dev_uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#endif
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700858 .ns_type = &net_ns_type_operations,
859 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700862/* Delete sysfs entries but hold kobject reference until after all
863 * netdev references are gone.
864 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700865void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700867 struct device *dev = &(net->dev);
868
869 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -0700870
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700871#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000872 rx_queue_remove_kobjects(net);
Tom Herberte880eb62010-03-22 18:06:47 -0700873#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000874
Stephen Hemminger9093bbb2007-05-19 15:39:25 -0700875 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700879int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700881 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -0700882 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000883 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Eric W. Biedermana1b3f592010-05-04 17:36:49 -0700885 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700886 dev->class = &net_class;
887 dev->platform_data = net;
888 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Stephen Hemmingera2205472009-03-09 13:51:55 +0000890 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700892#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000893 /* Allow for a device specific group */
894 if (*groups)
895 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric W. Biederman0c509a62009-10-29 14:18:21 +0000897 *groups++ = &netstat_group;
Johannes Berg22bb1be2008-07-10 11:16:47 +0200898#ifdef CONFIG_WIRELESS_EXT_SYSFS
Johannes Berg3d23e342009-09-29 23:27:28 +0200899 if (net->ieee80211_ptr)
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700900 *groups++ = &wireless_group;
Johannes Berg3d23e342009-09-29 23:27:28 +0200901#ifdef CONFIG_WIRELESS_EXT
902 else if (net->wireless_handlers)
903 *groups++ = &wireless_group;
904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700906#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Tom Herbert0a9627f2010-03-16 08:03:29 +0000908 error = device_add(dev);
909 if (error)
910 return error;
911
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700912#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000913 error = rx_queue_register_kobjects(net);
914 if (error) {
915 device_del(dev);
916 return error;
917 }
Tom Herberte880eb62010-03-22 18:06:47 -0700918#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000919
920 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Jay Vosburghb8a97872008-06-13 18:12:04 -0700923int netdev_class_create_file(struct class_attribute *class_attr)
924{
925 return class_create_file(&net_class, class_attr);
926}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000927EXPORT_SYMBOL(netdev_class_create_file);
Jay Vosburghb8a97872008-06-13 18:12:04 -0700928
929void netdev_class_remove_file(struct class_attribute *class_attr)
930{
931 class_remove_file(&net_class, class_attr);
932}
Jay Vosburghb8a97872008-06-13 18:12:04 -0700933EXPORT_SYMBOL(netdev_class_remove_file);
934
Eric W. Biederman8b41d182007-09-26 22:02:53 -0700935int netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Eric W. Biederman608b4b92010-05-04 17:36:45 -0700937 kobj_ns_type_register(&net_ns_type_operations);
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700938 register_pernet_subsys(&kobj_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return class_register(&net_class);
940}