blob: e8b2acbc8ea2b0473415c938db3f45e3b9233304 [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);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* read-write attributes */
137NETDEVICE_SHOW(mtu, fmt_dec);
138
139static int change_mtu(struct net_device *net, unsigned long new_mtu)
140{
141 return dev_set_mtu(net, (int) new_mtu);
142}
143
144static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
145{
146 return netdev_store(dev, buf, len, change_mtu);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149NETDEVICE_SHOW(flags, fmt_hex);
150
151static int change_flags(struct net_device *net, unsigned long new_flags)
152{
153 return dev_change_flags(net, (unsigned) new_flags);
154}
155
156static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
157{
158 return netdev_store(dev, buf, len, change_flags);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
162
163static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
164{
165 net->tx_queue_len = new_len;
166 return 0;
167}
168
169static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len)
170{
171 return netdev_store(dev, buf, len, change_tx_queue_len);
172}
173
Stephen Hemminger699a4112005-06-08 14:55:42 -0700174NETDEVICE_SHOW(weight, fmt_dec);
175
176static int change_weight(struct net_device *net, unsigned long new_weight)
177{
178 net->weight = new_weight;
179 return 0;
180}
181
182static ssize_t store_weight(struct class_device *dev, const char *buf, size_t len)
183{
184 return netdev_store(dev, buf, len, change_weight);
185}
186
Kay Sieversfd586ba2005-12-19 01:42:56 +0100187static struct class_device_attribute net_class_attributes[] = {
188 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
189 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
190 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
191 __ATTR(features, S_IRUGO, show_features, NULL),
192 __ATTR(type, S_IRUGO, show_type, NULL),
193 __ATTR(address, S_IRUGO, show_address, NULL),
194 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
195 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
196 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
197 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
198 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
199 store_tx_queue_len),
200 __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
201 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
204/* Show a given an attribute in the statistics group */
205static ssize_t netstat_show(const struct class_device *cd, char *buf,
206 unsigned long offset)
207{
208 struct net_device *dev = to_net_dev(cd);
209 struct net_device_stats *stats;
210 ssize_t ret = -EINVAL;
211
212 if (offset > sizeof(struct net_device_stats) ||
213 offset % sizeof(unsigned long) != 0)
214 WARN_ON(1);
215
216 read_lock(&dev_base_lock);
217 if (dev_isalive(dev) && dev->get_stats &&
218 (stats = (*dev->get_stats)(dev)))
219 ret = sprintf(buf, fmt_ulong,
220 *(unsigned long *)(((u8 *) stats) + offset));
221
222 read_unlock(&dev_base_lock);
223 return ret;
224}
225
226/* generate a read-only statistics attribute */
227#define NETSTAT_ENTRY(name) \
228static ssize_t show_##name(struct class_device *cd, char *buf) \
229{ \
230 return netstat_show(cd, buf, \
231 offsetof(struct net_device_stats, name)); \
232} \
233static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
234
235NETSTAT_ENTRY(rx_packets);
236NETSTAT_ENTRY(tx_packets);
237NETSTAT_ENTRY(rx_bytes);
238NETSTAT_ENTRY(tx_bytes);
239NETSTAT_ENTRY(rx_errors);
240NETSTAT_ENTRY(tx_errors);
241NETSTAT_ENTRY(rx_dropped);
242NETSTAT_ENTRY(tx_dropped);
243NETSTAT_ENTRY(multicast);
244NETSTAT_ENTRY(collisions);
245NETSTAT_ENTRY(rx_length_errors);
246NETSTAT_ENTRY(rx_over_errors);
247NETSTAT_ENTRY(rx_crc_errors);
248NETSTAT_ENTRY(rx_frame_errors);
249NETSTAT_ENTRY(rx_fifo_errors);
250NETSTAT_ENTRY(rx_missed_errors);
251NETSTAT_ENTRY(tx_aborted_errors);
252NETSTAT_ENTRY(tx_carrier_errors);
253NETSTAT_ENTRY(tx_fifo_errors);
254NETSTAT_ENTRY(tx_heartbeat_errors);
255NETSTAT_ENTRY(tx_window_errors);
256NETSTAT_ENTRY(rx_compressed);
257NETSTAT_ENTRY(tx_compressed);
258
259static struct attribute *netstat_attrs[] = {
260 &class_device_attr_rx_packets.attr,
261 &class_device_attr_tx_packets.attr,
262 &class_device_attr_rx_bytes.attr,
263 &class_device_attr_tx_bytes.attr,
264 &class_device_attr_rx_errors.attr,
265 &class_device_attr_tx_errors.attr,
266 &class_device_attr_rx_dropped.attr,
267 &class_device_attr_tx_dropped.attr,
268 &class_device_attr_multicast.attr,
269 &class_device_attr_collisions.attr,
270 &class_device_attr_rx_length_errors.attr,
271 &class_device_attr_rx_over_errors.attr,
272 &class_device_attr_rx_crc_errors.attr,
273 &class_device_attr_rx_frame_errors.attr,
274 &class_device_attr_rx_fifo_errors.attr,
275 &class_device_attr_rx_missed_errors.attr,
276 &class_device_attr_tx_aborted_errors.attr,
277 &class_device_attr_tx_carrier_errors.attr,
278 &class_device_attr_tx_fifo_errors.attr,
279 &class_device_attr_tx_heartbeat_errors.attr,
280 &class_device_attr_tx_window_errors.attr,
281 &class_device_attr_rx_compressed.attr,
282 &class_device_attr_tx_compressed.attr,
283 NULL
284};
285
286
287static struct attribute_group netstat_group = {
288 .name = "statistics",
289 .attrs = netstat_attrs,
290};
291
292#ifdef WIRELESS_EXT
293/* helper function that does all the locking etc for wireless stats */
294static ssize_t wireless_show(struct class_device *cd, char *buf,
295 ssize_t (*format)(const struct iw_statistics *,
296 char *))
297{
298 struct net_device *dev = to_net_dev(cd);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800299 const struct iw_statistics *iw = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ssize_t ret = -EINVAL;
301
302 read_lock(&dev_base_lock);
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800303 if (dev_isalive(dev)) {
304 if(dev->wireless_handlers &&
305 dev->wireless_handlers->get_wireless_stats)
306 iw = dev->wireless_handlers->get_wireless_stats(dev);
307 else if (dev->get_wireless_stats)
308 iw = dev->get_wireless_stats(dev);
309 if (iw != NULL)
310 ret = (*format)(iw, buf);
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 read_unlock(&dev_base_lock);
313
314 return ret;
315}
316
317/* show function template for wireless fields */
318#define WIRELESS_SHOW(name, field, format_string) \
319static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
320{ \
321 return sprintf(buf, format_string, iw->field); \
322} \
323static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
324{ \
325 return wireless_show(cd, buf, format_iw_##name); \
326} \
327static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
328
329WIRELESS_SHOW(status, status, fmt_hex);
330WIRELESS_SHOW(link, qual.qual, fmt_dec);
331WIRELESS_SHOW(level, qual.level, fmt_dec);
332WIRELESS_SHOW(noise, qual.noise, fmt_dec);
333WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
334WIRELESS_SHOW(crypt, discard.code, fmt_dec);
335WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
336WIRELESS_SHOW(misc, discard.misc, fmt_dec);
337WIRELESS_SHOW(retries, discard.retries, fmt_dec);
338WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
339
340static struct attribute *wireless_attrs[] = {
341 &class_device_attr_status.attr,
342 &class_device_attr_link.attr,
343 &class_device_attr_level.attr,
344 &class_device_attr_noise.attr,
345 &class_device_attr_nwid.attr,
346 &class_device_attr_crypt.attr,
347 &class_device_attr_fragment.attr,
348 &class_device_attr_retries.attr,
349 &class_device_attr_misc.attr,
350 &class_device_attr_beacon.attr,
351 NULL
352};
353
354static struct attribute_group wireless_group = {
355 .name = "wireless",
356 .attrs = wireless_attrs,
357};
358#endif
359
360#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100361static int netdev_uevent(struct class_device *cd, char **envp,
362 int num_envp, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct net_device *dev = to_net_dev(cd);
365 int i = 0;
366 int n;
367
Kay Sievers312c0042005-11-16 09:00:00 +0100368 /* pass interface to uevent. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 envp[i++] = buf;
370 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
371 buf += n;
372 size -= n;
373
374 if ((size <= 0) || (i >= num_envp))
375 return -ENOMEM;
376
377 envp[i] = NULL;
378 return 0;
379}
380#endif
381
382/*
383 * netdev_release -- destroy and free a dead device.
384 * Called when last reference to class_device kobject is gone.
385 */
386static void netdev_release(struct class_device *cd)
387{
388 struct net_device *dev
389 = container_of(cd, struct net_device, class_dev);
390
391 BUG_ON(dev->reg_state != NETREG_RELEASED);
392
393 kfree((char *)dev - dev->padded);
394}
395
396static struct class net_class = {
397 .name = "net",
398 .release = netdev_release,
Kay Sieversfd586ba2005-12-19 01:42:56 +0100399 .class_dev_attrs = net_class_attributes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#ifdef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100401 .uevent = netdev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#endif
403};
404
405void netdev_unregister_sysfs(struct net_device * net)
406{
407 struct class_device * class_dev = &(net->class_dev);
408
409 if (net->get_stats)
410 sysfs_remove_group(&class_dev->kobj, &netstat_group);
411
412#ifdef WIRELESS_EXT
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800413 if (net->get_wireless_stats || (net->wireless_handlers &&
414 net->wireless_handlers->get_wireless_stats))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sysfs_remove_group(&class_dev->kobj, &wireless_group);
416#endif
417 class_device_del(class_dev);
418
419}
420
421/* Create sysfs entries for network device. */
422int netdev_register_sysfs(struct net_device *net)
423{
424 struct class_device *class_dev = &(net->class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int ret;
426
427 class_dev->class = &net_class;
428 class_dev->class_data = net;
429
430 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
431 if ((ret = class_device_register(class_dev)))
432 goto out;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (net->get_stats &&
435 (ret = sysfs_create_group(&class_dev->kobj, &netstat_group)))
436 goto out_unreg;
437
438#ifdef WIRELESS_EXT
Andrey Borzenkov6dd214b2006-01-09 20:51:28 -0800439 if (net->get_wireless_stats || (net->wireless_handlers &&
440 net->wireless_handlers->get_wireless_stats)) {
441 ret = sysfs_create_group(&class_dev->kobj, &wireless_group);
442 if (ret)
443 goto out_cleanup;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446out_cleanup:
447 if (net->get_stats)
448 sysfs_remove_group(&class_dev->kobj, &netstat_group);
449#else
450 return 0;
451#endif
452
453out_unreg:
454 printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n",
455 net->name, ret);
456 class_device_unregister(class_dev);
457out:
458 return ret;
459}
460
461int netdev_sysfs_init(void)
462{
463 return class_register(&net_class);
464}