blob: 13472762b18b4c89f57c7bb27916bd8b67b47339 [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/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/if_arp.h>
16#include <net/sock.h>
17#include <linux/rtnetlink.h>
18#include <linux/wireless.h>
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -080019#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define to_class_dev(obj) container_of(obj,struct class_device,kobj)
22#define to_net_dev(class) container_of(class, struct net_device, class_dev)
23
24static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070025static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static const char fmt_dec[] = "%d\n";
27static const char fmt_ulong[] = "%lu\n";
28
29static inline int dev_isalive(const struct net_device *dev)
30{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070031 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34/* use same locking rules as GIF* ioctl's */
35static ssize_t netdev_show(const struct class_device *cd, char *buf,
36 ssize_t (*format)(const struct net_device *, char *))
37{
38 struct net_device *net = to_net_dev(cd);
39 ssize_t ret = -EINVAL;
40
41 read_lock(&dev_base_lock);
42 if (dev_isalive(net))
43 ret = (*format)(net, buf);
44 read_unlock(&dev_base_lock);
45
46 return ret;
47}
48
49/* generate a show function for simple field */
50#define NETDEVICE_SHOW(field, format_string) \
51static ssize_t format_##field(const struct net_device *net, char *buf) \
52{ \
53 return sprintf(buf, format_string, net->field); \
54} \
55static ssize_t show_##field(struct class_device *cd, char *buf) \
56{ \
57 return netdev_show(cd, buf, format_##field); \
58}
59
60
61/* use same locking and permission rules as SIF* ioctl's */
62static ssize_t netdev_store(struct class_device *dev,
63 const char *buf, size_t len,
64 int (*set)(struct net_device *, unsigned long))
65{
66 struct net_device *net = to_net_dev(dev);
67 char *endp;
68 unsigned long new;
69 int ret = -EINVAL;
70
71 if (!capable(CAP_NET_ADMIN))
72 return -EPERM;
73
74 new = simple_strtoul(buf, &endp, 0);
75 if (endp == buf)
76 goto err;
77
78 rtnl_lock();
79 if (dev_isalive(net)) {
80 if ((ret = (*set)(net, new)) == 0)
81 ret = len;
82 }
83 rtnl_unlock();
84 err:
85 return ret;
86}
87
Kay Sieversfd586ba2005-12-19 01:42:56 +010088NETDEVICE_SHOW(addr_len, fmt_dec);
89NETDEVICE_SHOW(iflink, fmt_dec);
90NETDEVICE_SHOW(ifindex, fmt_dec);
91NETDEVICE_SHOW(features, fmt_long_hex);
92NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -080093NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* use same locking rules as GIFHWADDR ioctl's */
96static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
97{
98 int i;
99 char *cp = buf;
100
101 for (i = 0; i < len; i++)
102 cp += sprintf(cp, "%02x%c", addr[i],
103 i == (len - 1) ? '\n' : ':');
104 return cp - buf;
105}
106
107static ssize_t show_address(struct class_device *dev, char *buf)
108{
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))
114 ret = format_addr(buf, net->dev_addr, net->addr_len);
115 read_unlock(&dev_base_lock);
116 return ret;
117}
118
119static ssize_t show_broadcast(struct class_device *dev, char *buf)
120{
121 struct net_device *net = to_net_dev(dev);
122 if (dev_isalive(net))
123 return format_addr(buf, net->broadcast, net->addr_len);
124 return -EINVAL;
125}
126
127static ssize_t show_carrier(struct class_device *dev, char *buf)
128{
129 struct net_device *netdev = to_net_dev(dev);
130 if (netif_running(netdev)) {
131 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
132 }
133 return -EINVAL;
134}
135
Stefan Rompfb00055a2006-03-20 17:09:11 -0800136static ssize_t show_dormant(struct class_device *dev, char *buf)
137{
138 struct net_device *netdev = to_net_dev(dev);
139
140 if (netif_running(netdev))
141 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
142
143 return -EINVAL;
144}
145
146static const char *operstates[] = {
147 "unknown",
148 "notpresent", /* currently unused */
149 "down",
150 "lowerlayerdown",
151 "testing", /* currently unused */
152 "dormant",
153 "up"
154};
155
156static ssize_t show_operstate(struct class_device *dev, char *buf)
157{
158 const struct net_device *netdev = to_net_dev(dev);
159 unsigned char operstate;
160
161 read_lock(&dev_base_lock);
162 operstate = netdev->operstate;
163 if (!netif_running(netdev))
164 operstate = IF_OPER_DOWN;
165 read_unlock(&dev_base_lock);
166
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700167 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800168 return -EINVAL; /* should not happen */
169
170 return sprintf(buf, "%s\n", operstates[operstate]);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* read-write attributes */
174NETDEVICE_SHOW(mtu, fmt_dec);
175
176static int change_mtu(struct net_device *net, unsigned long new_mtu)
177{
178 return dev_set_mtu(net, (int) new_mtu);
179}
180
181static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
182{
183 return netdev_store(dev, buf, len, change_mtu);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186NETDEVICE_SHOW(flags, fmt_hex);
187
188static int change_flags(struct net_device *net, unsigned long new_flags)
189{
190 return dev_change_flags(net, (unsigned) new_flags);
191}
192
193static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
194{
195 return netdev_store(dev, buf, len, change_flags);
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
199
200static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
201{
202 net->tx_queue_len = new_len;
203 return 0;
204}
205
206static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len)
207{
208 return netdev_store(dev, buf, len, change_tx_queue_len);
209}
210
Stephen Hemminger699a4112005-06-08 14:55:42 -0700211NETDEVICE_SHOW(weight, fmt_dec);
212
213static int change_weight(struct net_device *net, unsigned long new_weight)
214{
215 net->weight = new_weight;
216 return 0;
217}
218
219static ssize_t store_weight(struct class_device *dev, const char *buf, size_t len)
220{
221 return netdev_store(dev, buf, len, change_weight);
222}
223
Kay Sieversfd586ba2005-12-19 01:42:56 +0100224static struct class_device_attribute net_class_attributes[] = {
225 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
226 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
227 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
228 __ATTR(features, S_IRUGO, show_features, NULL),
229 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800230 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100231 __ATTR(address, S_IRUGO, show_address, NULL),
232 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
233 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800234 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
235 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100236 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
237 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
238 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
239 store_tx_queue_len),
240 __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
241 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
244/* Show a given an attribute in the statistics group */
245static ssize_t netstat_show(const struct class_device *cd, char *buf,
246 unsigned long offset)
247{
248 struct net_device *dev = to_net_dev(cd);
249 struct net_device_stats *stats;
250 ssize_t ret = -EINVAL;
251
252 if (offset > sizeof(struct net_device_stats) ||
253 offset % sizeof(unsigned long) != 0)
254 WARN_ON(1);
255
256 read_lock(&dev_base_lock);
257 if (dev_isalive(dev) && dev->get_stats &&
258 (stats = (*dev->get_stats)(dev)))
259 ret = sprintf(buf, fmt_ulong,
260 *(unsigned long *)(((u8 *) stats) + offset));
261
262 read_unlock(&dev_base_lock);
263 return ret;
264}
265
266/* generate a read-only statistics attribute */
267#define NETSTAT_ENTRY(name) \
268static ssize_t show_##name(struct class_device *cd, char *buf) \
269{ \
270 return netstat_show(cd, buf, \
271 offsetof(struct net_device_stats, name)); \
272} \
273static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
274
275NETSTAT_ENTRY(rx_packets);
276NETSTAT_ENTRY(tx_packets);
277NETSTAT_ENTRY(rx_bytes);
278NETSTAT_ENTRY(tx_bytes);
279NETSTAT_ENTRY(rx_errors);
280NETSTAT_ENTRY(tx_errors);
281NETSTAT_ENTRY(rx_dropped);
282NETSTAT_ENTRY(tx_dropped);
283NETSTAT_ENTRY(multicast);
284NETSTAT_ENTRY(collisions);
285NETSTAT_ENTRY(rx_length_errors);
286NETSTAT_ENTRY(rx_over_errors);
287NETSTAT_ENTRY(rx_crc_errors);
288NETSTAT_ENTRY(rx_frame_errors);
289NETSTAT_ENTRY(rx_fifo_errors);
290NETSTAT_ENTRY(rx_missed_errors);
291NETSTAT_ENTRY(tx_aborted_errors);
292NETSTAT_ENTRY(tx_carrier_errors);
293NETSTAT_ENTRY(tx_fifo_errors);
294NETSTAT_ENTRY(tx_heartbeat_errors);
295NETSTAT_ENTRY(tx_window_errors);
296NETSTAT_ENTRY(rx_compressed);
297NETSTAT_ENTRY(tx_compressed);
298
299static struct attribute *netstat_attrs[] = {
300 &class_device_attr_rx_packets.attr,
301 &class_device_attr_tx_packets.attr,
302 &class_device_attr_rx_bytes.attr,
303 &class_device_attr_tx_bytes.attr,
304 &class_device_attr_rx_errors.attr,
305 &class_device_attr_tx_errors.attr,
306 &class_device_attr_rx_dropped.attr,
307 &class_device_attr_tx_dropped.attr,
308 &class_device_attr_multicast.attr,
309 &class_device_attr_collisions.attr,
310 &class_device_attr_rx_length_errors.attr,
311 &class_device_attr_rx_over_errors.attr,
312 &class_device_attr_rx_crc_errors.attr,
313 &class_device_attr_rx_frame_errors.attr,
314 &class_device_attr_rx_fifo_errors.attr,
315 &class_device_attr_rx_missed_errors.attr,
316 &class_device_attr_tx_aborted_errors.attr,
317 &class_device_attr_tx_carrier_errors.attr,
318 &class_device_attr_tx_fifo_errors.attr,
319 &class_device_attr_tx_heartbeat_errors.attr,
320 &class_device_attr_tx_window_errors.attr,
321 &class_device_attr_rx_compressed.attr,
322 &class_device_attr_tx_compressed.attr,
323 NULL
324};
325
326
327static struct attribute_group netstat_group = {
328 .name = "statistics",
329 .attrs = netstat_attrs,
330};
331
332#ifdef WIRELESS_EXT
333/* helper function that does all the locking etc for wireless stats */
334static ssize_t wireless_show(struct class_device *cd, char *buf,
335 ssize_t (*format)(const struct iw_statistics *,
336 char *))
337{
338 struct net_device *dev = to_net_dev(cd);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800339 const struct iw_statistics *iw = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 ssize_t ret = -EINVAL;
341
342 read_lock(&dev_base_lock);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800343 if (dev_isalive(dev)) {
344 if(dev->wireless_handlers &&
345 dev->wireless_handlers->get_wireless_stats)
346 iw = dev->wireless_handlers->get_wireless_stats(dev);
347 else if (dev->get_wireless_stats)
348 iw = dev->get_wireless_stats(dev);
349 if (iw != NULL)
350 ret = (*format)(iw, buf);
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 read_unlock(&dev_base_lock);
353
354 return ret;
355}
356
357/* show function template for wireless fields */
358#define WIRELESS_SHOW(name, field, format_string) \
359static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
360{ \
361 return sprintf(buf, format_string, iw->field); \
362} \
363static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
364{ \
365 return wireless_show(cd, buf, format_iw_##name); \
366} \
367static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
368
369WIRELESS_SHOW(status, status, fmt_hex);
370WIRELESS_SHOW(link, qual.qual, fmt_dec);
371WIRELESS_SHOW(level, qual.level, fmt_dec);
372WIRELESS_SHOW(noise, qual.noise, fmt_dec);
373WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
374WIRELESS_SHOW(crypt, discard.code, fmt_dec);
375WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
376WIRELESS_SHOW(misc, discard.misc, fmt_dec);
377WIRELESS_SHOW(retries, discard.retries, fmt_dec);
378WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
379
380static struct attribute *wireless_attrs[] = {
381 &class_device_attr_status.attr,
382 &class_device_attr_link.attr,
383 &class_device_attr_level.attr,
384 &class_device_attr_noise.attr,
385 &class_device_attr_nwid.attr,
386 &class_device_attr_crypt.attr,
387 &class_device_attr_fragment.attr,
388 &class_device_attr_retries.attr,
389 &class_device_attr_misc.attr,
390 &class_device_attr_beacon.attr,
391 NULL
392};
393
394static struct attribute_group wireless_group = {
395 .name = "wireless",
396 .attrs = wireless_attrs,
397};
398#endif
399
400#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100401static int netdev_uevent(struct class_device *cd, char **envp,
402 int num_envp, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct net_device *dev = to_net_dev(cd);
405 int i = 0;
406 int n;
407
Kay Sievers312c0042005-11-16 09:00:00 +0100408 /* pass interface to uevent. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 envp[i++] = buf;
410 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
411 buf += n;
412 size -= n;
413
414 if ((size <= 0) || (i >= num_envp))
415 return -ENOMEM;
416
417 envp[i] = NULL;
418 return 0;
419}
420#endif
421
422/*
423 * netdev_release -- destroy and free a dead device.
424 * Called when last reference to class_device kobject is gone.
425 */
426static void netdev_release(struct class_device *cd)
427{
428 struct net_device *dev
429 = container_of(cd, struct net_device, class_dev);
430
431 BUG_ON(dev->reg_state != NETREG_RELEASED);
432
433 kfree((char *)dev - dev->padded);
434}
435
436static struct class net_class = {
437 .name = "net",
438 .release = netdev_release,
Kay Sieversfd586ba2005-12-19 01:42:56 +0100439 .class_dev_attrs = net_class_attributes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100441 .uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif
443};
444
445void netdev_unregister_sysfs(struct net_device * net)
446{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700447 class_device_del(&(net->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/* Create sysfs entries for network device. */
451int netdev_register_sysfs(struct net_device *net)
452{
453 struct class_device *class_dev = &(net->class_dev);
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700454 struct attribute_group **groups = net->sysfs_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700456 class_device_initialize(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 class_dev->class = &net_class;
458 class_dev->class_data = net;
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700459 class_dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700461 BUILD_BUG_ON(BUS_ID_SIZE < IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700464 if (net->get_stats)
465 *groups++ = &netstat_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467#ifdef WIRELESS_EXT
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700468 if (net->get_wireless_stats
469 || (net->wireless_handlers && net->wireless_handlers->get_wireless_stats))
470 *groups++ = &wireless_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#endif
472
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -0700473 return class_device_add(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476int netdev_sysfs_init(void)
477{
478 return class_register(&net_class);
479}