2 * net-sysfs.c - network device class and attributes
4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
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.
12 #include <linux/capability.h>
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/if_arp.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/wireless.h>
20 #include <net/iw_handler.h>
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)
25 static const char fmt_hex[] = "%#x\n";
26 static const char fmt_long_hex[] = "%#lx\n";
27 static const char fmt_dec[] = "%d\n";
28 static const char fmt_ulong[] = "%lu\n";
30 static inline int dev_isalive(const struct net_device *dev)
32 return dev->reg_state == NETREG_REGISTERED;
35 /* use same locking rules as GIF* ioctl's */
36 static ssize_t netdev_show(const struct class_device *cd, char *buf,
37 ssize_t (*format)(const struct net_device *, char *))
39 struct net_device *net = to_net_dev(cd);
40 ssize_t ret = -EINVAL;
42 read_lock(&dev_base_lock);
44 ret = (*format)(net, buf);
45 read_unlock(&dev_base_lock);
50 /* generate a show function for simple field */
51 #define NETDEVICE_SHOW(field, format_string) \
52 static ssize_t format_##field(const struct net_device *net, char *buf) \
54 return sprintf(buf, format_string, net->field); \
56 static ssize_t show_##field(struct class_device *cd, char *buf) \
58 return netdev_show(cd, buf, format_##field); \
62 /* use same locking and permission rules as SIF* ioctl's */
63 static ssize_t netdev_store(struct class_device *dev,
64 const char *buf, size_t len,
65 int (*set)(struct net_device *, unsigned long))
67 struct net_device *net = to_net_dev(dev);
72 if (!capable(CAP_NET_ADMIN))
75 new = simple_strtoul(buf, &endp, 0);
80 if (dev_isalive(net)) {
81 if ((ret = (*set)(net, new)) == 0)
89 NETDEVICE_SHOW(addr_len, fmt_dec);
90 NETDEVICE_SHOW(iflink, fmt_dec);
91 NETDEVICE_SHOW(ifindex, fmt_dec);
92 NETDEVICE_SHOW(features, fmt_long_hex);
93 NETDEVICE_SHOW(type, fmt_dec);
95 /* use same locking rules as GIFHWADDR ioctl's */
96 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
101 for (i = 0; i < len; i++)
102 cp += sprintf(cp, "%02x%c", addr[i],
103 i == (len - 1) ? '\n' : ':');
107 static ssize_t show_address(struct class_device *dev, char *buf)
109 struct net_device *net = to_net_dev(dev);
110 ssize_t ret = -EINVAL;
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);
119 static ssize_t show_broadcast(struct class_device *dev, char *buf)
121 struct net_device *net = to_net_dev(dev);
122 if (dev_isalive(net))
123 return format_addr(buf, net->broadcast, net->addr_len);
127 static ssize_t show_carrier(struct class_device *dev, char *buf)
129 struct net_device *netdev = to_net_dev(dev);
130 if (netif_running(netdev)) {
131 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
136 /* read-write attributes */
137 NETDEVICE_SHOW(mtu, fmt_dec);
139 static int change_mtu(struct net_device *net, unsigned long new_mtu)
141 return dev_set_mtu(net, (int) new_mtu);
144 static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
146 return netdev_store(dev, buf, len, change_mtu);
149 NETDEVICE_SHOW(flags, fmt_hex);
151 static int change_flags(struct net_device *net, unsigned long new_flags)
153 return dev_change_flags(net, (unsigned) new_flags);
156 static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
158 return netdev_store(dev, buf, len, change_flags);
161 NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
163 static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
165 net->tx_queue_len = new_len;
169 static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len)
171 return netdev_store(dev, buf, len, change_tx_queue_len);
174 NETDEVICE_SHOW(weight, fmt_dec);
176 static int change_weight(struct net_device *net, unsigned long new_weight)
178 net->weight = new_weight;
182 static ssize_t store_weight(struct class_device *dev, const char *buf, size_t len)
184 return netdev_store(dev, buf, len, change_weight);
187 static 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,
200 __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
204 /* Show a given an attribute in the statistics group */
205 static ssize_t netstat_show(const struct class_device *cd, char *buf,
206 unsigned long offset)
208 struct net_device *dev = to_net_dev(cd);
209 struct net_device_stats *stats;
210 ssize_t ret = -EINVAL;
212 if (offset > sizeof(struct net_device_stats) ||
213 offset % sizeof(unsigned long) != 0)
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));
222 read_unlock(&dev_base_lock);
226 /* generate a read-only statistics attribute */
227 #define NETSTAT_ENTRY(name) \
228 static ssize_t show_##name(struct class_device *cd, char *buf) \
230 return netstat_show(cd, buf, \
231 offsetof(struct net_device_stats, name)); \
233 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
235 NETSTAT_ENTRY(rx_packets);
236 NETSTAT_ENTRY(tx_packets);
237 NETSTAT_ENTRY(rx_bytes);
238 NETSTAT_ENTRY(tx_bytes);
239 NETSTAT_ENTRY(rx_errors);
240 NETSTAT_ENTRY(tx_errors);
241 NETSTAT_ENTRY(rx_dropped);
242 NETSTAT_ENTRY(tx_dropped);
243 NETSTAT_ENTRY(multicast);
244 NETSTAT_ENTRY(collisions);
245 NETSTAT_ENTRY(rx_length_errors);
246 NETSTAT_ENTRY(rx_over_errors);
247 NETSTAT_ENTRY(rx_crc_errors);
248 NETSTAT_ENTRY(rx_frame_errors);
249 NETSTAT_ENTRY(rx_fifo_errors);
250 NETSTAT_ENTRY(rx_missed_errors);
251 NETSTAT_ENTRY(tx_aborted_errors);
252 NETSTAT_ENTRY(tx_carrier_errors);
253 NETSTAT_ENTRY(tx_fifo_errors);
254 NETSTAT_ENTRY(tx_heartbeat_errors);
255 NETSTAT_ENTRY(tx_window_errors);
256 NETSTAT_ENTRY(rx_compressed);
257 NETSTAT_ENTRY(tx_compressed);
259 static 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,
287 static struct attribute_group netstat_group = {
288 .name = "statistics",
289 .attrs = netstat_attrs,
293 /* helper function that does all the locking etc for wireless stats */
294 static ssize_t wireless_show(struct class_device *cd, char *buf,
295 ssize_t (*format)(const struct iw_statistics *,
298 struct net_device *dev = to_net_dev(cd);
299 const struct iw_statistics *iw = NULL;
300 ssize_t ret = -EINVAL;
302 read_lock(&dev_base_lock);
303 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);
310 ret = (*format)(iw, buf);
312 read_unlock(&dev_base_lock);
317 /* show function template for wireless fields */
318 #define WIRELESS_SHOW(name, field, format_string) \
319 static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
321 return sprintf(buf, format_string, iw->field); \
323 static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
325 return wireless_show(cd, buf, format_iw_##name); \
327 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
329 WIRELESS_SHOW(status, status, fmt_hex);
330 WIRELESS_SHOW(link, qual.qual, fmt_dec);
331 WIRELESS_SHOW(level, qual.level, fmt_dec);
332 WIRELESS_SHOW(noise, qual.noise, fmt_dec);
333 WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
334 WIRELESS_SHOW(crypt, discard.code, fmt_dec);
335 WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
336 WIRELESS_SHOW(misc, discard.misc, fmt_dec);
337 WIRELESS_SHOW(retries, discard.retries, fmt_dec);
338 WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
340 static 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,
354 static struct attribute_group wireless_group = {
356 .attrs = wireless_attrs,
360 #ifdef CONFIG_HOTPLUG
361 static int netdev_uevent(struct class_device *cd, char **envp,
362 int num_envp, char *buf, int size)
364 struct net_device *dev = to_net_dev(cd);
368 /* pass interface to uevent. */
370 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
374 if ((size <= 0) || (i >= num_envp))
383 * netdev_release -- destroy and free a dead device.
384 * Called when last reference to class_device kobject is gone.
386 static void netdev_release(struct class_device *cd)
388 struct net_device *dev
389 = container_of(cd, struct net_device, class_dev);
391 BUG_ON(dev->reg_state != NETREG_RELEASED);
393 kfree((char *)dev - dev->padded);
396 static struct class net_class = {
398 .release = netdev_release,
399 .class_dev_attrs = net_class_attributes,
400 #ifdef CONFIG_HOTPLUG
401 .uevent = netdev_uevent,
405 void netdev_unregister_sysfs(struct net_device * net)
407 struct class_device * class_dev = &(net->class_dev);
410 sysfs_remove_group(&class_dev->kobj, &netstat_group);
413 if (net->get_wireless_stats || (net->wireless_handlers &&
414 net->wireless_handlers->get_wireless_stats))
415 sysfs_remove_group(&class_dev->kobj, &wireless_group);
417 class_device_del(class_dev);
421 /* Create sysfs entries for network device. */
422 int netdev_register_sysfs(struct net_device *net)
424 struct class_device *class_dev = &(net->class_dev);
427 class_dev->class = &net_class;
428 class_dev->class_data = net;
430 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
431 if ((ret = class_device_register(class_dev)))
434 if (net->get_stats &&
435 (ret = sysfs_create_group(&class_dev->kobj, &netstat_group)))
439 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);
448 sysfs_remove_group(&class_dev->kobj, &netstat_group);
454 printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n",
456 class_device_unregister(class_dev);
461 int netdev_sysfs_init(void)
463 return class_register(&net_class);