]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/net-sysfs.c
[PATCH] nvidiafb: Fixes for new G5
[net-next-2.6.git] / net / core / net-sysfs.c
CommitLineData
1da177e4
LT
1/*
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
12#include <linux/config.h>
13#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>
19
20#define to_class_dev(obj) container_of(obj,struct class_device,kobj)
21#define to_net_dev(class) container_of(class, struct net_device, class_dev)
22
23static const char fmt_hex[] = "%#x\n";
d1102b59 24static const char fmt_long_hex[] = "%#lx\n";
1da177e4
LT
25static const char fmt_dec[] = "%d\n";
26static const char fmt_ulong[] = "%lu\n";
27
28static inline int dev_isalive(const struct net_device *dev)
29{
30 return dev->reg_state == NETREG_REGISTERED;
31}
32
33/* use same locking rules as GIF* ioctl's */
34static ssize_t netdev_show(const struct class_device *cd, char *buf,
35 ssize_t (*format)(const struct net_device *, char *))
36{
37 struct net_device *net = to_net_dev(cd);
38 ssize_t ret = -EINVAL;
39
40 read_lock(&dev_base_lock);
41 if (dev_isalive(net))
42 ret = (*format)(net, buf);
43 read_unlock(&dev_base_lock);
44
45 return ret;
46}
47
48/* generate a show function for simple field */
49#define NETDEVICE_SHOW(field, format_string) \
50static ssize_t format_##field(const struct net_device *net, char *buf) \
51{ \
52 return sprintf(buf, format_string, net->field); \
53} \
54static ssize_t show_##field(struct class_device *cd, char *buf) \
55{ \
56 return netdev_show(cd, buf, format_##field); \
57}
58
59
60/* use same locking and permission rules as SIF* ioctl's */
61static ssize_t netdev_store(struct class_device *dev,
62 const char *buf, size_t len,
63 int (*set)(struct net_device *, unsigned long))
64{
65 struct net_device *net = to_net_dev(dev);
66 char *endp;
67 unsigned long new;
68 int ret = -EINVAL;
69
70 if (!capable(CAP_NET_ADMIN))
71 return -EPERM;
72
73 new = simple_strtoul(buf, &endp, 0);
74 if (endp == buf)
75 goto err;
76
77 rtnl_lock();
78 if (dev_isalive(net)) {
79 if ((ret = (*set)(net, new)) == 0)
80 ret = len;
81 }
82 rtnl_unlock();
83 err:
84 return ret;
85}
86
fd586bac
KS
87NETDEVICE_SHOW(addr_len, fmt_dec);
88NETDEVICE_SHOW(iflink, fmt_dec);
89NETDEVICE_SHOW(ifindex, fmt_dec);
90NETDEVICE_SHOW(features, fmt_long_hex);
91NETDEVICE_SHOW(type, fmt_dec);
1da177e4
LT
92
93/* use same locking rules as GIFHWADDR ioctl's */
94static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
95{
96 int i;
97 char *cp = buf;
98
99 for (i = 0; i < len; i++)
100 cp += sprintf(cp, "%02x%c", addr[i],
101 i == (len - 1) ? '\n' : ':');
102 return cp - buf;
103}
104
105static ssize_t show_address(struct class_device *dev, char *buf)
106{
107 struct net_device *net = to_net_dev(dev);
108 ssize_t ret = -EINVAL;
109
110 read_lock(&dev_base_lock);
111 if (dev_isalive(net))
112 ret = format_addr(buf, net->dev_addr, net->addr_len);
113 read_unlock(&dev_base_lock);
114 return ret;
115}
116
117static ssize_t show_broadcast(struct class_device *dev, char *buf)
118{
119 struct net_device *net = to_net_dev(dev);
120 if (dev_isalive(net))
121 return format_addr(buf, net->broadcast, net->addr_len);
122 return -EINVAL;
123}
124
125static ssize_t show_carrier(struct class_device *dev, char *buf)
126{
127 struct net_device *netdev = to_net_dev(dev);
128 if (netif_running(netdev)) {
129 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
130 }
131 return -EINVAL;
132}
133
1da177e4
LT
134/* read-write attributes */
135NETDEVICE_SHOW(mtu, fmt_dec);
136
137static int change_mtu(struct net_device *net, unsigned long new_mtu)
138{
139 return dev_set_mtu(net, (int) new_mtu);
140}
141
142static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
143{
144 return netdev_store(dev, buf, len, change_mtu);
145}
146
1da177e4
LT
147NETDEVICE_SHOW(flags, fmt_hex);
148
149static int change_flags(struct net_device *net, unsigned long new_flags)
150{
151 return dev_change_flags(net, (unsigned) new_flags);
152}
153
154static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
155{
156 return netdev_store(dev, buf, len, change_flags);
157}
158
1da177e4
LT
159NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
160
161static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
162{
163 net->tx_queue_len = new_len;
164 return 0;
165}
166
167static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len)
168{
169 return netdev_store(dev, buf, len, change_tx_queue_len);
170}
171
699a4114
SH
172NETDEVICE_SHOW(weight, fmt_dec);
173
174static int change_weight(struct net_device *net, unsigned long new_weight)
175{
176 net->weight = new_weight;
177 return 0;
178}
179
180static ssize_t store_weight(struct class_device *dev, const char *buf, size_t len)
181{
182 return netdev_store(dev, buf, len, change_weight);
183}
184
fd586bac
KS
185static struct class_device_attribute net_class_attributes[] = {
186 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
187 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
188 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
189 __ATTR(features, S_IRUGO, show_features, NULL),
190 __ATTR(type, S_IRUGO, show_type, NULL),
191 __ATTR(address, S_IRUGO, show_address, NULL),
192 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
193 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
194 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
195 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
196 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
197 store_tx_queue_len),
198 __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
199 {}
1da177e4
LT
200};
201
202/* Show a given an attribute in the statistics group */
203static ssize_t netstat_show(const struct class_device *cd, char *buf,
204 unsigned long offset)
205{
206 struct net_device *dev = to_net_dev(cd);
207 struct net_device_stats *stats;
208 ssize_t ret = -EINVAL;
209
210 if (offset > sizeof(struct net_device_stats) ||
211 offset % sizeof(unsigned long) != 0)
212 WARN_ON(1);
213
214 read_lock(&dev_base_lock);
215 if (dev_isalive(dev) && dev->get_stats &&
216 (stats = (*dev->get_stats)(dev)))
217 ret = sprintf(buf, fmt_ulong,
218 *(unsigned long *)(((u8 *) stats) + offset));
219
220 read_unlock(&dev_base_lock);
221 return ret;
222}
223
224/* generate a read-only statistics attribute */
225#define NETSTAT_ENTRY(name) \
226static ssize_t show_##name(struct class_device *cd, char *buf) \
227{ \
228 return netstat_show(cd, buf, \
229 offsetof(struct net_device_stats, name)); \
230} \
231static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
232
233NETSTAT_ENTRY(rx_packets);
234NETSTAT_ENTRY(tx_packets);
235NETSTAT_ENTRY(rx_bytes);
236NETSTAT_ENTRY(tx_bytes);
237NETSTAT_ENTRY(rx_errors);
238NETSTAT_ENTRY(tx_errors);
239NETSTAT_ENTRY(rx_dropped);
240NETSTAT_ENTRY(tx_dropped);
241NETSTAT_ENTRY(multicast);
242NETSTAT_ENTRY(collisions);
243NETSTAT_ENTRY(rx_length_errors);
244NETSTAT_ENTRY(rx_over_errors);
245NETSTAT_ENTRY(rx_crc_errors);
246NETSTAT_ENTRY(rx_frame_errors);
247NETSTAT_ENTRY(rx_fifo_errors);
248NETSTAT_ENTRY(rx_missed_errors);
249NETSTAT_ENTRY(tx_aborted_errors);
250NETSTAT_ENTRY(tx_carrier_errors);
251NETSTAT_ENTRY(tx_fifo_errors);
252NETSTAT_ENTRY(tx_heartbeat_errors);
253NETSTAT_ENTRY(tx_window_errors);
254NETSTAT_ENTRY(rx_compressed);
255NETSTAT_ENTRY(tx_compressed);
256
257static struct attribute *netstat_attrs[] = {
258 &class_device_attr_rx_packets.attr,
259 &class_device_attr_tx_packets.attr,
260 &class_device_attr_rx_bytes.attr,
261 &class_device_attr_tx_bytes.attr,
262 &class_device_attr_rx_errors.attr,
263 &class_device_attr_tx_errors.attr,
264 &class_device_attr_rx_dropped.attr,
265 &class_device_attr_tx_dropped.attr,
266 &class_device_attr_multicast.attr,
267 &class_device_attr_collisions.attr,
268 &class_device_attr_rx_length_errors.attr,
269 &class_device_attr_rx_over_errors.attr,
270 &class_device_attr_rx_crc_errors.attr,
271 &class_device_attr_rx_frame_errors.attr,
272 &class_device_attr_rx_fifo_errors.attr,
273 &class_device_attr_rx_missed_errors.attr,
274 &class_device_attr_tx_aborted_errors.attr,
275 &class_device_attr_tx_carrier_errors.attr,
276 &class_device_attr_tx_fifo_errors.attr,
277 &class_device_attr_tx_heartbeat_errors.attr,
278 &class_device_attr_tx_window_errors.attr,
279 &class_device_attr_rx_compressed.attr,
280 &class_device_attr_tx_compressed.attr,
281 NULL
282};
283
284
285static struct attribute_group netstat_group = {
286 .name = "statistics",
287 .attrs = netstat_attrs,
288};
289
290#ifdef WIRELESS_EXT
291/* helper function that does all the locking etc for wireless stats */
292static ssize_t wireless_show(struct class_device *cd, char *buf,
293 ssize_t (*format)(const struct iw_statistics *,
294 char *))
295{
296 struct net_device *dev = to_net_dev(cd);
297 const struct iw_statistics *iw;
298 ssize_t ret = -EINVAL;
299
300 read_lock(&dev_base_lock);
301 if (dev_isalive(dev) && dev->get_wireless_stats
302 && (iw = dev->get_wireless_stats(dev)) != NULL)
303 ret = (*format)(iw, buf);
304 read_unlock(&dev_base_lock);
305
306 return ret;
307}
308
309/* show function template for wireless fields */
310#define WIRELESS_SHOW(name, field, format_string) \
311static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
312{ \
313 return sprintf(buf, format_string, iw->field); \
314} \
315static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
316{ \
317 return wireless_show(cd, buf, format_iw_##name); \
318} \
319static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
320
321WIRELESS_SHOW(status, status, fmt_hex);
322WIRELESS_SHOW(link, qual.qual, fmt_dec);
323WIRELESS_SHOW(level, qual.level, fmt_dec);
324WIRELESS_SHOW(noise, qual.noise, fmt_dec);
325WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
326WIRELESS_SHOW(crypt, discard.code, fmt_dec);
327WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
328WIRELESS_SHOW(misc, discard.misc, fmt_dec);
329WIRELESS_SHOW(retries, discard.retries, fmt_dec);
330WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
331
332static struct attribute *wireless_attrs[] = {
333 &class_device_attr_status.attr,
334 &class_device_attr_link.attr,
335 &class_device_attr_level.attr,
336 &class_device_attr_noise.attr,
337 &class_device_attr_nwid.attr,
338 &class_device_attr_crypt.attr,
339 &class_device_attr_fragment.attr,
340 &class_device_attr_retries.attr,
341 &class_device_attr_misc.attr,
342 &class_device_attr_beacon.attr,
343 NULL
344};
345
346static struct attribute_group wireless_group = {
347 .name = "wireless",
348 .attrs = wireless_attrs,
349};
350#endif
351
352#ifdef CONFIG_HOTPLUG
312c004d
KS
353static int netdev_uevent(struct class_device *cd, char **envp,
354 int num_envp, char *buf, int size)
1da177e4
LT
355{
356 struct net_device *dev = to_net_dev(cd);
357 int i = 0;
358 int n;
359
312c004d 360 /* pass interface to uevent. */
1da177e4
LT
361 envp[i++] = buf;
362 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
363 buf += n;
364 size -= n;
365
366 if ((size <= 0) || (i >= num_envp))
367 return -ENOMEM;
368
369 envp[i] = NULL;
370 return 0;
371}
372#endif
373
374/*
375 * netdev_release -- destroy and free a dead device.
376 * Called when last reference to class_device kobject is gone.
377 */
378static void netdev_release(struct class_device *cd)
379{
380 struct net_device *dev
381 = container_of(cd, struct net_device, class_dev);
382
383 BUG_ON(dev->reg_state != NETREG_RELEASED);
384
385 kfree((char *)dev - dev->padded);
386}
387
388static struct class net_class = {
389 .name = "net",
390 .release = netdev_release,
fd586bac 391 .class_dev_attrs = net_class_attributes,
1da177e4 392#ifdef CONFIG_HOTPLUG
312c004d 393 .uevent = netdev_uevent,
1da177e4
LT
394#endif
395};
396
397void netdev_unregister_sysfs(struct net_device * net)
398{
399 struct class_device * class_dev = &(net->class_dev);
400
401 if (net->get_stats)
402 sysfs_remove_group(&class_dev->kobj, &netstat_group);
403
404#ifdef WIRELESS_EXT
405 if (net->get_wireless_stats)
406 sysfs_remove_group(&class_dev->kobj, &wireless_group);
407#endif
408 class_device_del(class_dev);
409
410}
411
412/* Create sysfs entries for network device. */
413int netdev_register_sysfs(struct net_device *net)
414{
415 struct class_device *class_dev = &(net->class_dev);
1da177e4
LT
416 int ret;
417
418 class_dev->class = &net_class;
419 class_dev->class_data = net;
420
421 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
422 if ((ret = class_device_register(class_dev)))
423 goto out;
424
1da177e4
LT
425 if (net->get_stats &&
426 (ret = sysfs_create_group(&class_dev->kobj, &netstat_group)))
427 goto out_unreg;
428
429#ifdef WIRELESS_EXT
430 if (net->get_wireless_stats &&
431 (ret = sysfs_create_group(&class_dev->kobj, &wireless_group)))
432 goto out_cleanup;
433
434 return 0;
435out_cleanup:
436 if (net->get_stats)
437 sysfs_remove_group(&class_dev->kobj, &netstat_group);
438#else
439 return 0;
440#endif
441
442out_unreg:
443 printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n",
444 net->name, ret);
445 class_device_unregister(class_dev);
446out:
447 return ret;
448}
449
450int netdev_sysfs_init(void)
451{
452 return class_register(&net_class);
453}