blob: c12990c9c603ebb0403ffaab710139409cde81de [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>
5 *
6 * 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/config.h>
14#include <linux/kernel.h>
15#include <linux/netdevice.h>
16#include <linux/if_arp.h>
17#include <net/sock.h>
18#include <linux/rtnetlink.h>
19#include <linux/wireless.h>
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -080020#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define to_class_dev(obj) container_of(obj,struct class_device,kobj)
23#define to_net_dev(class) container_of(class, struct net_device, class_dev)
24
25static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070026static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static const char fmt_dec[] = "%d\n";
28static const char fmt_ulong[] = "%lu\n";
29
30static inline int dev_isalive(const struct net_device *dev)
31{
32 return dev->reg_state == NETREG_REGISTERED;
33}
34
35/* use same locking rules as GIF* ioctl's */
36static ssize_t netdev_show(const struct class_device *cd, char *buf,
37 ssize_t (*format)(const struct net_device *, char *))
38{
39 struct net_device *net = to_net_dev(cd);
40 ssize_t ret = -EINVAL;
41
42 read_lock(&dev_base_lock);
43 if (dev_isalive(net))
44 ret = (*format)(net, buf);
45 read_unlock(&dev_base_lock);
46
47 return ret;
48}
49
50/* generate a show function for simple field */
51#define NETDEVICE_SHOW(field, format_string) \
52static ssize_t format_##field(const struct net_device *net, char *buf) \
53{ \
54 return sprintf(buf, format_string, net->field); \
55} \
56static ssize_t show_##field(struct class_device *cd, char *buf) \
57{ \
58 return netdev_show(cd, buf, format_##field); \
59}
60
61
62/* use same locking and permission rules as SIF* ioctl's */
63static ssize_t netdev_store(struct class_device *dev,
64 const char *buf, size_t len,
65 int (*set)(struct net_device *, unsigned long))
66{
67 struct net_device *net = to_net_dev(dev);
68 char *endp;
69 unsigned long new;
70 int ret = -EINVAL;
71
72 if (!capable(CAP_NET_ADMIN))
73 return -EPERM;
74
75 new = simple_strtoul(buf, &endp, 0);
76 if (endp == buf)
77 goto err;
78
79 rtnl_lock();
80 if (dev_isalive(net)) {
81 if ((ret = (*set)(net, new)) == 0)
82 ret = len;
83 }
84 rtnl_unlock();
85 err:
86 return ret;
87}
88
Kay Sieversfd586ba2005-12-19 01:42:56 +010089NETDEVICE_SHOW(addr_len, fmt_dec);
90NETDEVICE_SHOW(iflink, fmt_dec);
91NETDEVICE_SHOW(ifindex, fmt_dec);
92NETDEVICE_SHOW(features, fmt_long_hex);
93NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -080094NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* use same locking rules as GIFHWADDR ioctl's */
97static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
98{
99 int i;
100 char *cp = buf;
101
102 for (i = 0; i < len; i++)
103 cp += sprintf(cp, "%02x%c", addr[i],
104 i == (len - 1) ? '\n' : ':');
105 return cp - buf;
106}
107
108static ssize_t show_address(struct class_device *dev, char *buf)
109{
110 struct net_device *net = to_net_dev(dev);
111 ssize_t ret = -EINVAL;
112
113 read_lock(&dev_base_lock);
114 if (dev_isalive(net))
115 ret = format_addr(buf, net->dev_addr, net->addr_len);
116 read_unlock(&dev_base_lock);
117 return ret;
118}
119
120static ssize_t show_broadcast(struct class_device *dev, char *buf)
121{
122 struct net_device *net = to_net_dev(dev);
123 if (dev_isalive(net))
124 return format_addr(buf, net->broadcast, net->addr_len);
125 return -EINVAL;
126}
127
128static ssize_t show_carrier(struct class_device *dev, char *buf)
129{
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
Stefan Rompfb00055a2006-03-20 17:09:11 -0800137static ssize_t show_dormant(struct class_device *dev, char *buf)
138{
139 struct net_device *netdev = to_net_dev(dev);
140
141 if (netif_running(netdev))
142 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
143
144 return -EINVAL;
145}
146
147static const char *operstates[] = {
148 "unknown",
149 "notpresent", /* currently unused */
150 "down",
151 "lowerlayerdown",
152 "testing", /* currently unused */
153 "dormant",
154 "up"
155};
156
157static ssize_t show_operstate(struct class_device *dev, char *buf)
158{
159 const struct net_device *netdev = to_net_dev(dev);
160 unsigned char operstate;
161
162 read_lock(&dev_base_lock);
163 operstate = netdev->operstate;
164 if (!netif_running(netdev))
165 operstate = IF_OPER_DOWN;
166 read_unlock(&dev_base_lock);
167
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700168 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800169 return -EINVAL; /* should not happen */
170
171 return sprintf(buf, "%s\n", operstates[operstate]);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* read-write attributes */
175NETDEVICE_SHOW(mtu, fmt_dec);
176
177static int change_mtu(struct net_device *net, unsigned long new_mtu)
178{
179 return dev_set_mtu(net, (int) new_mtu);
180}
181
182static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
183{
184 return netdev_store(dev, buf, len, change_mtu);
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187NETDEVICE_SHOW(flags, fmt_hex);
188
189static int change_flags(struct net_device *net, unsigned long new_flags)
190{
191 return dev_change_flags(net, (unsigned) new_flags);
192}
193
194static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
195{
196 return netdev_store(dev, buf, len, change_flags);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
200
201static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
202{
203 net->tx_queue_len = new_len;
204 return 0;
205}
206
207static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len)
208{
209 return netdev_store(dev, buf, len, change_tx_queue_len);
210}
211
Stephen Hemminger699a4112005-06-08 14:55:42 -0700212NETDEVICE_SHOW(weight, fmt_dec);
213
214static int change_weight(struct net_device *net, unsigned long new_weight)
215{
216 net->weight = new_weight;
217 return 0;
218}
219
220static ssize_t store_weight(struct class_device *dev, const char *buf, size_t len)
221{
222 return netdev_store(dev, buf, len, change_weight);
223}
224
Kay Sieversfd586ba2005-12-19 01:42:56 +0100225static struct class_device_attribute net_class_attributes[] = {
226 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
227 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
228 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
229 __ATTR(features, S_IRUGO, show_features, NULL),
230 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800231 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100232 __ATTR(address, S_IRUGO, show_address, NULL),
233 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
234 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800235 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
236 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100237 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
238 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
239 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
240 store_tx_queue_len),
241 __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
242 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245/* Show a given an attribute in the statistics group */
246static ssize_t netstat_show(const struct class_device *cd, char *buf,
247 unsigned long offset)
248{
249 struct net_device *dev = to_net_dev(cd);
250 struct net_device_stats *stats;
251 ssize_t ret = -EINVAL;
252
253 if (offset > sizeof(struct net_device_stats) ||
254 offset % sizeof(unsigned long) != 0)
255 WARN_ON(1);
256
257 read_lock(&dev_base_lock);
258 if (dev_isalive(dev) && dev->get_stats &&
259 (stats = (*dev->get_stats)(dev)))
260 ret = sprintf(buf, fmt_ulong,
261 *(unsigned long *)(((u8 *) stats) + offset));
262
263 read_unlock(&dev_base_lock);
264 return ret;
265}
266
267/* generate a read-only statistics attribute */
268#define NETSTAT_ENTRY(name) \
269static ssize_t show_##name(struct class_device *cd, char *buf) \
270{ \
271 return netstat_show(cd, buf, \
272 offsetof(struct net_device_stats, name)); \
273} \
274static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
275
276NETSTAT_ENTRY(rx_packets);
277NETSTAT_ENTRY(tx_packets);
278NETSTAT_ENTRY(rx_bytes);
279NETSTAT_ENTRY(tx_bytes);
280NETSTAT_ENTRY(rx_errors);
281NETSTAT_ENTRY(tx_errors);
282NETSTAT_ENTRY(rx_dropped);
283NETSTAT_ENTRY(tx_dropped);
284NETSTAT_ENTRY(multicast);
285NETSTAT_ENTRY(collisions);
286NETSTAT_ENTRY(rx_length_errors);
287NETSTAT_ENTRY(rx_over_errors);
288NETSTAT_ENTRY(rx_crc_errors);
289NETSTAT_ENTRY(rx_frame_errors);
290NETSTAT_ENTRY(rx_fifo_errors);
291NETSTAT_ENTRY(rx_missed_errors);
292NETSTAT_ENTRY(tx_aborted_errors);
293NETSTAT_ENTRY(tx_carrier_errors);
294NETSTAT_ENTRY(tx_fifo_errors);
295NETSTAT_ENTRY(tx_heartbeat_errors);
296NETSTAT_ENTRY(tx_window_errors);
297NETSTAT_ENTRY(rx_compressed);
298NETSTAT_ENTRY(tx_compressed);
299
300static struct attribute *netstat_attrs[] = {
301 &class_device_attr_rx_packets.attr,
302 &class_device_attr_tx_packets.attr,
303 &class_device_attr_rx_bytes.attr,
304 &class_device_attr_tx_bytes.attr,
305 &class_device_attr_rx_errors.attr,
306 &class_device_attr_tx_errors.attr,
307 &class_device_attr_rx_dropped.attr,
308 &class_device_attr_tx_dropped.attr,
309 &class_device_attr_multicast.attr,
310 &class_device_attr_collisions.attr,
311 &class_device_attr_rx_length_errors.attr,
312 &class_device_attr_rx_over_errors.attr,
313 &class_device_attr_rx_crc_errors.attr,
314 &class_device_attr_rx_frame_errors.attr,
315 &class_device_attr_rx_fifo_errors.attr,
316 &class_device_attr_rx_missed_errors.attr,
317 &class_device_attr_tx_aborted_errors.attr,
318 &class_device_attr_tx_carrier_errors.attr,
319 &class_device_attr_tx_fifo_errors.attr,
320 &class_device_attr_tx_heartbeat_errors.attr,
321 &class_device_attr_tx_window_errors.attr,
322 &class_device_attr_rx_compressed.attr,
323 &class_device_attr_tx_compressed.attr,
324 NULL
325};
326
327
328static struct attribute_group netstat_group = {
329 .name = "statistics",
330 .attrs = netstat_attrs,
331};
332
333#ifdef WIRELESS_EXT
334/* helper function that does all the locking etc for wireless stats */
335static ssize_t wireless_show(struct class_device *cd, char *buf,
336 ssize_t (*format)(const struct iw_statistics *,
337 char *))
338{
339 struct net_device *dev = to_net_dev(cd);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800340 const struct iw_statistics *iw = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ssize_t ret = -EINVAL;
342
343 read_lock(&dev_base_lock);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800344 if (dev_isalive(dev)) {
345 if(dev->wireless_handlers &&
346 dev->wireless_handlers->get_wireless_stats)
347 iw = dev->wireless_handlers->get_wireless_stats(dev);
348 else if (dev->get_wireless_stats)
349 iw = dev->get_wireless_stats(dev);
350 if (iw != NULL)
351 ret = (*format)(iw, buf);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 read_unlock(&dev_base_lock);
354
355 return ret;
356}
357
358/* show function template for wireless fields */
359#define WIRELESS_SHOW(name, field, format_string) \
360static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
361{ \
362 return sprintf(buf, format_string, iw->field); \
363} \
364static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
365{ \
366 return wireless_show(cd, buf, format_iw_##name); \
367} \
368static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
369
370WIRELESS_SHOW(status, status, fmt_hex);
371WIRELESS_SHOW(link, qual.qual, fmt_dec);
372WIRELESS_SHOW(level, qual.level, fmt_dec);
373WIRELESS_SHOW(noise, qual.noise, fmt_dec);
374WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
375WIRELESS_SHOW(crypt, discard.code, fmt_dec);
376WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
377WIRELESS_SHOW(misc, discard.misc, fmt_dec);
378WIRELESS_SHOW(retries, discard.retries, fmt_dec);
379WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
380
381static struct attribute *wireless_attrs[] = {
382 &class_device_attr_status.attr,
383 &class_device_attr_link.attr,
384 &class_device_attr_level.attr,
385 &class_device_attr_noise.attr,
386 &class_device_attr_nwid.attr,
387 &class_device_attr_crypt.attr,
388 &class_device_attr_fragment.attr,
389 &class_device_attr_retries.attr,
390 &class_device_attr_misc.attr,
391 &class_device_attr_beacon.attr,
392 NULL
393};
394
395static struct attribute_group wireless_group = {
396 .name = "wireless",
397 .attrs = wireless_attrs,
398};
399#endif
400
401#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100402static int netdev_uevent(struct class_device *cd, char **envp,
403 int num_envp, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct net_device *dev = to_net_dev(cd);
406 int i = 0;
407 int n;
408
Kay Sievers312c0042005-11-16 09:00:00 +0100409 /* pass interface to uevent. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 envp[i++] = buf;
411 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
412 buf += n;
413 size -= n;
414
415 if ((size <= 0) || (i >= num_envp))
416 return -ENOMEM;
417
418 envp[i] = NULL;
419 return 0;
420}
421#endif
422
423/*
424 * netdev_release -- destroy and free a dead device.
425 * Called when last reference to class_device kobject is gone.
426 */
427static void netdev_release(struct class_device *cd)
428{
429 struct net_device *dev
430 = container_of(cd, struct net_device, class_dev);
431
432 BUG_ON(dev->reg_state != NETREG_RELEASED);
433
434 kfree((char *)dev - dev->padded);
435}
436
437static struct class net_class = {
438 .name = "net",
439 .release = netdev_release,
Kay Sieversfd586ba2005-12-19 01:42:56 +0100440 .class_dev_attrs = net_class_attributes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100442 .uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif
444};
445
446void netdev_unregister_sysfs(struct net_device * net)
447{
448 struct class_device * class_dev = &(net->class_dev);
449
450 if (net->get_stats)
451 sysfs_remove_group(&class_dev->kobj, &netstat_group);
452
453#ifdef WIRELESS_EXT
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800454 if (net->get_wireless_stats || (net->wireless_handlers &&
455 net->wireless_handlers->get_wireless_stats))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sysfs_remove_group(&class_dev->kobj, &wireless_group);
457#endif
458 class_device_del(class_dev);
459
460}
461
462/* Create sysfs entries for network device. */
463int netdev_register_sysfs(struct net_device *net)
464{
465 struct class_device *class_dev = &(net->class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int ret;
467
468 class_dev->class = &net_class;
469 class_dev->class_data = net;
470
471 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
472 if ((ret = class_device_register(class_dev)))
473 goto out;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (net->get_stats &&
476 (ret = sysfs_create_group(&class_dev->kobj, &netstat_group)))
477 goto out_unreg;
478
479#ifdef WIRELESS_EXT
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800480 if (net->get_wireless_stats || (net->wireless_handlers &&
481 net->wireless_handlers->get_wireless_stats)) {
482 ret = sysfs_create_group(&class_dev->kobj, &wireless_group);
483 if (ret)
484 goto out_cleanup;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487out_cleanup:
488 if (net->get_stats)
489 sysfs_remove_group(&class_dev->kobj, &netstat_group);
490#else
491 return 0;
492#endif
493
494out_unreg:
495 printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n",
496 net->name, ret);
497 class_device_unregister(class_dev);
498out:
499 return ret;
500}
501
502int netdev_sysfs_init(void)
503{
504 return class_register(&net_class);
505}