Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 23 | static const char fmt_hex[] = "%#x\n"; |
David S. Miller | d1102b5 | 2005-05-29 20:28:25 -0700 | [diff] [blame^] | 24 | static const char fmt_long_hex[] = "%#lx\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static const char fmt_dec[] = "%d\n"; |
| 26 | static const char fmt_ulong[] = "%lu\n"; |
| 27 | |
| 28 | static 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 */ |
| 34 | static 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) \ |
| 50 | static ssize_t format_##field(const struct net_device *net, char *buf) \ |
| 51 | { \ |
| 52 | return sprintf(buf, format_string, net->field); \ |
| 53 | } \ |
| 54 | static 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 */ |
| 61 | static 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 | |
| 87 | /* generate a read-only network device class attribute */ |
| 88 | #define NETDEVICE_ATTR(field, format_string) \ |
| 89 | NETDEVICE_SHOW(field, format_string) \ |
| 90 | static CLASS_DEVICE_ATTR(field, S_IRUGO, show_##field, NULL) \ |
| 91 | |
| 92 | NETDEVICE_ATTR(addr_len, fmt_dec); |
| 93 | NETDEVICE_ATTR(iflink, fmt_dec); |
| 94 | NETDEVICE_ATTR(ifindex, fmt_dec); |
David S. Miller | d1102b5 | 2005-05-29 20:28:25 -0700 | [diff] [blame^] | 95 | NETDEVICE_ATTR(features, fmt_long_hex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | NETDEVICE_ATTR(type, fmt_dec); |
| 97 | |
| 98 | /* use same locking rules as GIFHWADDR ioctl's */ |
| 99 | static ssize_t format_addr(char *buf, const unsigned char *addr, int len) |
| 100 | { |
| 101 | int i; |
| 102 | char *cp = buf; |
| 103 | |
| 104 | for (i = 0; i < len; i++) |
| 105 | cp += sprintf(cp, "%02x%c", addr[i], |
| 106 | i == (len - 1) ? '\n' : ':'); |
| 107 | return cp - buf; |
| 108 | } |
| 109 | |
| 110 | static ssize_t show_address(struct class_device *dev, char *buf) |
| 111 | { |
| 112 | struct net_device *net = to_net_dev(dev); |
| 113 | ssize_t ret = -EINVAL; |
| 114 | |
| 115 | read_lock(&dev_base_lock); |
| 116 | if (dev_isalive(net)) |
| 117 | ret = format_addr(buf, net->dev_addr, net->addr_len); |
| 118 | read_unlock(&dev_base_lock); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static ssize_t show_broadcast(struct class_device *dev, char *buf) |
| 123 | { |
| 124 | struct net_device *net = to_net_dev(dev); |
| 125 | if (dev_isalive(net)) |
| 126 | return format_addr(buf, net->broadcast, net->addr_len); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | static ssize_t show_carrier(struct class_device *dev, char *buf) |
| 131 | { |
| 132 | struct net_device *netdev = to_net_dev(dev); |
| 133 | if (netif_running(netdev)) { |
| 134 | return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); |
| 135 | } |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 140 | static CLASS_DEVICE_ATTR(broadcast, S_IRUGO, show_broadcast, NULL); |
| 141 | static CLASS_DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL); |
| 142 | |
| 143 | /* read-write attributes */ |
| 144 | NETDEVICE_SHOW(mtu, fmt_dec); |
| 145 | |
| 146 | static int change_mtu(struct net_device *net, unsigned long new_mtu) |
| 147 | { |
| 148 | return dev_set_mtu(net, (int) new_mtu); |
| 149 | } |
| 150 | |
| 151 | static ssize_t store_mtu(struct class_device *dev, const char *buf, size_t len) |
| 152 | { |
| 153 | return netdev_store(dev, buf, len, change_mtu); |
| 154 | } |
| 155 | |
| 156 | static CLASS_DEVICE_ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu); |
| 157 | |
| 158 | NETDEVICE_SHOW(flags, fmt_hex); |
| 159 | |
| 160 | static int change_flags(struct net_device *net, unsigned long new_flags) |
| 161 | { |
| 162 | return dev_change_flags(net, (unsigned) new_flags); |
| 163 | } |
| 164 | |
| 165 | static ssize_t store_flags(struct class_device *dev, const char *buf, size_t len) |
| 166 | { |
| 167 | return netdev_store(dev, buf, len, change_flags); |
| 168 | } |
| 169 | |
| 170 | static CLASS_DEVICE_ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags); |
| 171 | |
| 172 | NETDEVICE_SHOW(tx_queue_len, fmt_ulong); |
| 173 | |
| 174 | static int change_tx_queue_len(struct net_device *net, unsigned long new_len) |
| 175 | { |
| 176 | net->tx_queue_len = new_len; |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static ssize_t store_tx_queue_len(struct class_device *dev, const char *buf, size_t len) |
| 181 | { |
| 182 | return netdev_store(dev, buf, len, change_tx_queue_len); |
| 183 | } |
| 184 | |
| 185 | static CLASS_DEVICE_ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len, |
| 186 | store_tx_queue_len); |
| 187 | |
| 188 | |
| 189 | static struct class_device_attribute *net_class_attributes[] = { |
| 190 | &class_device_attr_ifindex, |
| 191 | &class_device_attr_iflink, |
| 192 | &class_device_attr_addr_len, |
| 193 | &class_device_attr_tx_queue_len, |
| 194 | &class_device_attr_features, |
| 195 | &class_device_attr_mtu, |
| 196 | &class_device_attr_flags, |
| 197 | &class_device_attr_type, |
| 198 | &class_device_attr_address, |
| 199 | &class_device_attr_broadcast, |
| 200 | &class_device_attr_carrier, |
| 201 | NULL |
| 202 | }; |
| 203 | |
| 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) |
| 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) \ |
| 228 | static 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 | } \ |
| 233 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 234 | |
| 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); |
| 258 | |
| 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, |
| 283 | NULL |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | static 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 */ |
| 294 | static 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); |
| 299 | const struct iw_statistics *iw; |
| 300 | ssize_t ret = -EINVAL; |
| 301 | |
| 302 | read_lock(&dev_base_lock); |
| 303 | if (dev_isalive(dev) && dev->get_wireless_stats |
| 304 | && (iw = dev->get_wireless_stats(dev)) != NULL) |
| 305 | ret = (*format)(iw, buf); |
| 306 | read_unlock(&dev_base_lock); |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | /* show function template for wireless fields */ |
| 312 | #define WIRELESS_SHOW(name, field, format_string) \ |
| 313 | static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \ |
| 314 | { \ |
| 315 | return sprintf(buf, format_string, iw->field); \ |
| 316 | } \ |
| 317 | static ssize_t show_iw_##name(struct class_device *cd, char *buf) \ |
| 318 | { \ |
| 319 | return wireless_show(cd, buf, format_iw_##name); \ |
| 320 | } \ |
| 321 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL) |
| 322 | |
| 323 | WIRELESS_SHOW(status, status, fmt_hex); |
| 324 | WIRELESS_SHOW(link, qual.qual, fmt_dec); |
| 325 | WIRELESS_SHOW(level, qual.level, fmt_dec); |
| 326 | WIRELESS_SHOW(noise, qual.noise, fmt_dec); |
| 327 | WIRELESS_SHOW(nwid, discard.nwid, fmt_dec); |
| 328 | WIRELESS_SHOW(crypt, discard.code, fmt_dec); |
| 329 | WIRELESS_SHOW(fragment, discard.fragment, fmt_dec); |
| 330 | WIRELESS_SHOW(misc, discard.misc, fmt_dec); |
| 331 | WIRELESS_SHOW(retries, discard.retries, fmt_dec); |
| 332 | WIRELESS_SHOW(beacon, miss.beacon, fmt_dec); |
| 333 | |
| 334 | static struct attribute *wireless_attrs[] = { |
| 335 | &class_device_attr_status.attr, |
| 336 | &class_device_attr_link.attr, |
| 337 | &class_device_attr_level.attr, |
| 338 | &class_device_attr_noise.attr, |
| 339 | &class_device_attr_nwid.attr, |
| 340 | &class_device_attr_crypt.attr, |
| 341 | &class_device_attr_fragment.attr, |
| 342 | &class_device_attr_retries.attr, |
| 343 | &class_device_attr_misc.attr, |
| 344 | &class_device_attr_beacon.attr, |
| 345 | NULL |
| 346 | }; |
| 347 | |
| 348 | static struct attribute_group wireless_group = { |
| 349 | .name = "wireless", |
| 350 | .attrs = wireless_attrs, |
| 351 | }; |
| 352 | #endif |
| 353 | |
| 354 | #ifdef CONFIG_HOTPLUG |
| 355 | static int netdev_hotplug(struct class_device *cd, char **envp, |
| 356 | int num_envp, char *buf, int size) |
| 357 | { |
| 358 | struct net_device *dev = to_net_dev(cd); |
| 359 | int i = 0; |
| 360 | int n; |
| 361 | |
| 362 | /* pass interface in env to hotplug. */ |
| 363 | envp[i++] = buf; |
| 364 | n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1; |
| 365 | buf += n; |
| 366 | size -= n; |
| 367 | |
| 368 | if ((size <= 0) || (i >= num_envp)) |
| 369 | return -ENOMEM; |
| 370 | |
| 371 | envp[i] = NULL; |
| 372 | return 0; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | /* |
| 377 | * netdev_release -- destroy and free a dead device. |
| 378 | * Called when last reference to class_device kobject is gone. |
| 379 | */ |
| 380 | static void netdev_release(struct class_device *cd) |
| 381 | { |
| 382 | struct net_device *dev |
| 383 | = container_of(cd, struct net_device, class_dev); |
| 384 | |
| 385 | BUG_ON(dev->reg_state != NETREG_RELEASED); |
| 386 | |
| 387 | kfree((char *)dev - dev->padded); |
| 388 | } |
| 389 | |
| 390 | static struct class net_class = { |
| 391 | .name = "net", |
| 392 | .release = netdev_release, |
| 393 | #ifdef CONFIG_HOTPLUG |
| 394 | .hotplug = netdev_hotplug, |
| 395 | #endif |
| 396 | }; |
| 397 | |
| 398 | void netdev_unregister_sysfs(struct net_device * net) |
| 399 | { |
| 400 | struct class_device * class_dev = &(net->class_dev); |
| 401 | |
| 402 | if (net->get_stats) |
| 403 | sysfs_remove_group(&class_dev->kobj, &netstat_group); |
| 404 | |
| 405 | #ifdef WIRELESS_EXT |
| 406 | if (net->get_wireless_stats) |
| 407 | sysfs_remove_group(&class_dev->kobj, &wireless_group); |
| 408 | #endif |
| 409 | class_device_del(class_dev); |
| 410 | |
| 411 | } |
| 412 | |
| 413 | /* Create sysfs entries for network device. */ |
| 414 | int netdev_register_sysfs(struct net_device *net) |
| 415 | { |
| 416 | struct class_device *class_dev = &(net->class_dev); |
| 417 | int i; |
| 418 | struct class_device_attribute *attr; |
| 419 | int ret; |
| 420 | |
| 421 | class_dev->class = &net_class; |
| 422 | class_dev->class_data = net; |
| 423 | |
| 424 | strlcpy(class_dev->class_id, net->name, BUS_ID_SIZE); |
| 425 | if ((ret = class_device_register(class_dev))) |
| 426 | goto out; |
| 427 | |
| 428 | for (i = 0; (attr = net_class_attributes[i]) != NULL; i++) { |
| 429 | if ((ret = class_device_create_file(class_dev, attr))) |
| 430 | goto out_unreg; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | if (net->get_stats && |
| 435 | (ret = sysfs_create_group(&class_dev->kobj, &netstat_group))) |
| 436 | goto out_unreg; |
| 437 | |
| 438 | #ifdef WIRELESS_EXT |
| 439 | if (net->get_wireless_stats && |
| 440 | (ret = sysfs_create_group(&class_dev->kobj, &wireless_group))) |
| 441 | goto out_cleanup; |
| 442 | |
| 443 | return 0; |
| 444 | out_cleanup: |
| 445 | if (net->get_stats) |
| 446 | sysfs_remove_group(&class_dev->kobj, &netstat_group); |
| 447 | #else |
| 448 | return 0; |
| 449 | #endif |
| 450 | |
| 451 | out_unreg: |
| 452 | printk(KERN_WARNING "%s: sysfs attribute registration failed %d\n", |
| 453 | net->name, ret); |
| 454 | class_device_unregister(class_dev); |
| 455 | out: |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | int netdev_sysfs_init(void) |
| 460 | { |
| 461 | return class_register(&net_class); |
| 462 | } |