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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_arp.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/wireless.h>
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)
23 static const char *fmt_hex = "%#x\n";
24 static const char *fmt_dec = "%d\n";
25 static const char *fmt_ulong = "%lu\n";
27 static inline int dev_isalive(const struct net_device *dev)
29 return dev->reg_state == NETREG_REGISTERED;
32 /* use same locking rules as GIF* ioctl's */
33 static ssize_t netdev_show(const struct class_device *cd, char *buf,
34 ssize_t (*format)(const struct net_device *, char *))
36 struct net_device *net = to_net_dev(cd);
37 ssize_t ret = -EINVAL;
39 read_lock(&dev_base_lock);
41 ret = (*format)(net, buf);
42 read_unlock(&dev_base_lock);
47 /* generate a show function for simple field */
48 #define NETDEVICE_SHOW(field, format_string) \
49 static ssize_t format_##field(const struct net_device *net, char *buf) \
51 return sprintf(buf, format_string, net->field); \
53 static ssize_t show_##field(struct class_device *cd, char *buf) \
55 return netdev_show(cd, buf, format_##field); \
59 /* use same locking and permission rules as SIF* ioctl's */
60 static ssize_t netdev_store(struct class_device *dev,
61 const char *buf, size_t len,
62 int (*set)(struct net_device *, unsigned long))
64 struct net_device *net = to_net_dev(dev);
69 if (!capable(CAP_NET_ADMIN))
72 new = simple_strtoul(buf, &endp, 0);
77 if (dev_isalive(net)) {
78 if ((ret = (*set)(net, new)) == 0)
86 /* generate a read-only network device class attribute */
87 #define NETDEVICE_ATTR(field, format_string) \
88 NETDEVICE_SHOW(field, format_string) \
89 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_##field, NULL) \
91 NETDEVICE_ATTR(addr_len, fmt_dec);
92 NETDEVICE_ATTR(iflink, fmt_dec);
93 NETDEVICE_ATTR(ifindex, fmt_dec);
94 NETDEVICE_ATTR(features, fmt_hex);
95 NETDEVICE_ATTR(type, fmt_dec);
97 /* use same locking rules as GIFHWADDR ioctl's */
98 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
103 for (i = 0; i < len; i++)
104 cp += sprintf(cp, "%02x%c", addr[i],
105 i == (len - 1) ? '\n' : ':');
109 static ssize_t show_address(struct class_device *dev, char *buf)
111 struct net_device *net = to_net_dev(dev);
112 ssize_t ret = -EINVAL;
114 read_lock(&dev_base_lock);
115 if (dev_isalive(net))
116 ret = format_addr(buf, net->dev_addr, net->addr_len);
117 read_unlock(&dev_base_lock);
121 static ssize_t show_broadcast(struct class_device *dev, char *buf)
123 struct net_device *net = to_net_dev(dev);
124 if (dev_isalive(net))
125 return format_addr(buf, net->broadcast, net->addr_len);
129 static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
130 static CLASS_DEVICE_ATTR(broadcast, S_IRUGO, show_broadcast, NULL);
132 /* read-write attributes */
133 NETDEVICE_SHOW(mtu, fmt_dec);
135 static int change_mtu(struct net_device *net, unsigned long new_mtu)
137 return dev_set_mtu(net, (int) new_mtu);
140 static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len)
142 return netdev_store(dev, buf, len, change_mtu);
145 static CLASS_DEVICE_ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu);
147 NETDEVICE_SHOW(flags, fmt_hex);
149 static int change_flags(struct net_device *net, unsigned long new_flags)
151 return dev_change_flags(net, (unsigned) new_flags);
154 static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len)
156 return netdev_store(dev, buf, len, change_flags);
159 static CLASS_DEVICE_ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_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 static CLASS_DEVICE_ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
178 static struct class_device_attribute *net_class_attributes[] = {
179 &class_device_attr_ifindex,
180 &class_device_attr_iflink,
181 &class_device_attr_addr_len,
182 &class_device_attr_tx_queue_len,
183 &class_device_attr_features,
184 &class_device_attr_mtu,
185 &class_device_attr_flags,
186 &class_device_attr_type,
187 &class_device_attr_address,
188 &class_device_attr_broadcast,
192 /* Show a given an attribute in the statistics group */
193 static ssize_t netstat_show(const struct class_device *cd, char *buf,
194 unsigned long offset)
196 struct net_device *dev = to_net_dev(cd);
197 struct net_device_stats *stats;
198 ssize_t ret = -EINVAL;
200 if (offset > sizeof(struct net_device_stats) ||
201 offset % sizeof(unsigned long) != 0)
204 read_lock(&dev_base_lock);
205 if (dev_isalive(dev) && dev->get_stats &&
206 (stats = (*dev->get_stats)(dev)))
207 ret = sprintf(buf, fmt_ulong,
208 *(unsigned long *)(((u8 *) stats) + offset));
210 read_unlock(&dev_base_lock);
214 /* generate a read-only statistics attribute */
215 #define NETSTAT_ENTRY(name) \
216 static ssize_t show_##name(struct class_device *cd, char *buf) \
218 return netstat_show(cd, buf, \
219 offsetof(struct net_device_stats, name)); \
221 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
223 NETSTAT_ENTRY(rx_packets);
224 NETSTAT_ENTRY(tx_packets);
225 NETSTAT_ENTRY(rx_bytes);
226 NETSTAT_ENTRY(tx_bytes);
227 NETSTAT_ENTRY(rx_errors);
228 NETSTAT_ENTRY(tx_errors);
229 NETSTAT_ENTRY(rx_dropped);
230 NETSTAT_ENTRY(tx_dropped);
231 NETSTAT_ENTRY(multicast);
232 NETSTAT_ENTRY(collisions);
233 NETSTAT_ENTRY(rx_length_errors);
234 NETSTAT_ENTRY(rx_over_errors);
235 NETSTAT_ENTRY(rx_crc_errors);
236 NETSTAT_ENTRY(rx_frame_errors);
237 NETSTAT_ENTRY(rx_fifo_errors);
238 NETSTAT_ENTRY(rx_missed_errors);
239 NETSTAT_ENTRY(tx_aborted_errors);
240 NETSTAT_ENTRY(tx_carrier_errors);
241 NETSTAT_ENTRY(tx_fifo_errors);
242 NETSTAT_ENTRY(tx_heartbeat_errors);
243 NETSTAT_ENTRY(tx_window_errors);
244 NETSTAT_ENTRY(rx_compressed);
245 NETSTAT_ENTRY(tx_compressed);
247 static struct attribute *netstat_attrs[] = {
248 &class_device_attr_rx_packets.attr,
249 &class_device_attr_tx_packets.attr,
250 &class_device_attr_rx_bytes.attr,
251 &class_device_attr_tx_bytes.attr,
252 &class_device_attr_rx_errors.attr,
253 &class_device_attr_tx_errors.attr,
254 &class_device_attr_rx_dropped.attr,
255 &class_device_attr_tx_dropped.attr,
256 &class_device_attr_multicast.attr,
257 &class_device_attr_collisions.attr,
258 &class_device_attr_rx_length_errors.attr,
259 &class_device_attr_rx_over_errors.attr,
260 &class_device_attr_rx_crc_errors.attr,
261 &class_device_attr_rx_frame_errors.attr,
262 &class_device_attr_rx_fifo_errors.attr,
263 &class_device_attr_rx_missed_errors.attr,
264 &class_device_attr_tx_aborted_errors.attr,
265 &class_device_attr_tx_carrier_errors.attr,
266 &class_device_attr_tx_fifo_errors.attr,
267 &class_device_attr_tx_heartbeat_errors.attr,
268 &class_device_attr_tx_window_errors.attr,
269 &class_device_attr_rx_compressed.attr,
270 &class_device_attr_tx_compressed.attr,
275 static struct attribute_group netstat_group = {
276 .name = "statistics",
277 .attrs = netstat_attrs,
281 /* helper function that does all the locking etc for wireless stats */
282 static ssize_t wireless_show(struct class_device *cd, char *buf,
283 ssize_t (*format)(const struct iw_statistics *,
286 struct net_device *dev = to_net_dev(cd);
287 const struct iw_statistics *iw;
288 ssize_t ret = -EINVAL;
290 read_lock(&dev_base_lock);
291 if (dev_isalive(dev) && dev->get_wireless_stats
292 && (iw = dev->get_wireless_stats(dev)) != NULL)
293 ret = (*format)(iw, buf);
294 read_unlock(&dev_base_lock);
299 /* show function template for wireless fields */
300 #define WIRELESS_SHOW(name, field, format_string) \
301 static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
303 return sprintf(buf, format_string, iw->field); \
305 static ssize_t show_iw_##name(struct class_device *cd, char *buf) \
307 return wireless_show(cd, buf, format_iw_##name); \
309 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
311 WIRELESS_SHOW(status, status, fmt_hex);
312 WIRELESS_SHOW(link, qual.qual, fmt_dec);
313 WIRELESS_SHOW(level, qual.level, fmt_dec);
314 WIRELESS_SHOW(noise, qual.noise, fmt_dec);
315 WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
316 WIRELESS_SHOW(crypt, discard.code, fmt_dec);
317 WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
318 WIRELESS_SHOW(misc, discard.misc, fmt_dec);
319 WIRELESS_SHOW(retries, discard.retries, fmt_dec);
320 WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
322 static struct attribute *wireless_attrs[] = {
323 &class_device_attr_status.attr,
324 &class_device_attr_link.attr,
325 &class_device_attr_level.attr,
326 &class_device_attr_noise.attr,
327 &class_device_attr_nwid.attr,
328 &class_device_attr_crypt.attr,
329 &class_device_attr_fragment.attr,
330 &class_device_attr_retries.attr,
331 &class_device_attr_misc.attr,
332 &class_device_attr_beacon.attr,
336 static struct attribute_group wireless_group = {
338 .attrs = wireless_attrs,
342 #ifdef CONFIG_HOTPLUG
343 static int netdev_hotplug(struct class_device *cd, char **envp,
344 int num_envp, char *buf, int size)
346 struct net_device *dev = to_net_dev(cd);
350 /* pass interface in env to hotplug. */
352 n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
356 if ((size <= 0) || (i >= num_envp))
365 * netdev_release -- destroy and free a dead device.
366 * Called when last reference to class_device kobject is gone.
368 static void netdev_release(struct class_device *cd)
370 struct net_device *dev
371 = container_of(cd, struct net_device, class_dev);
373 BUG_ON(dev->reg_state != NETREG_RELEASED);
375 kfree((char *)dev - dev->padded);
378 static struct class net_class = {
380 .release = netdev_release,
381 #ifdef CONFIG_HOTPLUG
382 .hotplug = netdev_hotplug,
386 void netdev_unregister_sysfs(struct net_device * net)
388 struct class_device * class_dev = &(net->class_dev);
391 sysfs_remove_group(&class_dev->kobj, &netstat_group);
394 if (net->get_wireless_stats)
395 sysfs_remove_group(&class_dev->kobj, &wireless_group);
397 class_device_del(class_dev);
401 /* Create sysfs entries for network device. */
402 int netdev_register_sysfs(struct net_device *net)
404 struct class_device *class_dev = &(net->class_dev);
406 struct class_device_attribute *attr;
409 class_dev->class = &net_class;
410 class_dev->class_data = net;
411 net->last_stats = net->get_stats;
413 strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE);
414 if ((ret = class_device_register(class_dev)))
417 for (i = 0; (attr = net_class_attributes[i]); i++) {
418 if ((ret = class_device_create_file(class_dev, attr)))
423 if (net->get_stats &&
424 (ret = sysfs_create_group(&class_dev->kobj, &netstat_group)))
428 if (net->get_wireless_stats &&
429 (ret = sysfs_create_group(&class_dev->kobj, &wireless_group)))
435 sysfs_remove_group(&class_dev->kobj, &netstat_group);
441 printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n",
443 class_device_unregister(class_dev);
448 int netdev_sysfs_init(void)
450 return class_register(&net_class);