caif: Bugfix - use MSG_TRUNC in receive
[linux-2.6/libata-dev.git] / net / core / net-sysfs.c
blobc57c4b228bb5b849599124e058322aabe8d203d8
1 /*
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/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_arp.h>
16 #include <linux/slab.h>
17 #include <net/sock.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/wireless.h>
20 #include <linux/vmalloc.h>
21 #include <net/wext.h>
23 #include "net-sysfs.h"
25 #ifdef CONFIG_SYSFS
26 static const char fmt_hex[] = "%#x\n";
27 static const char fmt_long_hex[] = "%#lx\n";
28 static const char fmt_dec[] = "%d\n";
29 static const char fmt_ulong[] = "%lu\n";
31 static inline int dev_isalive(const struct net_device *dev)
33 return dev->reg_state <= NETREG_REGISTERED;
36 /* use same locking rules as GIF* ioctl's */
37 static ssize_t netdev_show(const struct device *dev,
38 struct device_attribute *attr, char *buf,
39 ssize_t (*format)(const struct net_device *, char *))
41 struct net_device *net = to_net_dev(dev);
42 ssize_t ret = -EINVAL;
44 read_lock(&dev_base_lock);
45 if (dev_isalive(net))
46 ret = (*format)(net, buf);
47 read_unlock(&dev_base_lock);
49 return ret;
52 /* generate a show function for simple field */
53 #define NETDEVICE_SHOW(field, format_string) \
54 static ssize_t format_##field(const struct net_device *net, char *buf) \
55 { \
56 return sprintf(buf, format_string, net->field); \
57 } \
58 static ssize_t show_##field(struct device *dev, \
59 struct device_attribute *attr, char *buf) \
60 { \
61 return netdev_show(dev, attr, buf, format_##field); \
65 /* use same locking and permission rules as SIF* ioctl's */
66 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
67 const char *buf, size_t len,
68 int (*set)(struct net_device *, unsigned long))
70 struct net_device *net = to_net_dev(dev);
71 char *endp;
72 unsigned long new;
73 int ret = -EINVAL;
75 if (!capable(CAP_NET_ADMIN))
76 return -EPERM;
78 new = simple_strtoul(buf, &endp, 0);
79 if (endp == buf)
80 goto err;
82 if (!rtnl_trylock())
83 return restart_syscall();
85 if (dev_isalive(net)) {
86 if ((ret = (*set)(net, new)) == 0)
87 ret = len;
89 rtnl_unlock();
90 err:
91 return ret;
94 NETDEVICE_SHOW(dev_id, fmt_hex);
95 NETDEVICE_SHOW(addr_len, fmt_dec);
96 NETDEVICE_SHOW(iflink, fmt_dec);
97 NETDEVICE_SHOW(ifindex, fmt_dec);
98 NETDEVICE_SHOW(features, fmt_long_hex);
99 NETDEVICE_SHOW(type, fmt_dec);
100 NETDEVICE_SHOW(link_mode, fmt_dec);
102 /* use same locking rules as GIFHWADDR ioctl's */
103 static ssize_t show_address(struct device *dev, struct device_attribute *attr,
104 char *buf)
106 struct net_device *net = to_net_dev(dev);
107 ssize_t ret = -EINVAL;
109 read_lock(&dev_base_lock);
110 if (dev_isalive(net))
111 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
112 read_unlock(&dev_base_lock);
113 return ret;
116 static ssize_t show_broadcast(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct net_device *net = to_net_dev(dev);
120 if (dev_isalive(net))
121 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
122 return -EINVAL;
125 static ssize_t show_carrier(struct device *dev,
126 struct device_attribute *attr, char *buf)
128 struct net_device *netdev = to_net_dev(dev);
129 if (netif_running(netdev)) {
130 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
132 return -EINVAL;
135 static ssize_t show_speed(struct device *dev,
136 struct device_attribute *attr, char *buf)
138 struct net_device *netdev = to_net_dev(dev);
139 int ret = -EINVAL;
141 if (!rtnl_trylock())
142 return restart_syscall();
144 if (netif_running(netdev) &&
145 netdev->ethtool_ops &&
146 netdev->ethtool_ops->get_settings) {
147 struct ethtool_cmd cmd = { ETHTOOL_GSET };
149 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
150 ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
152 rtnl_unlock();
153 return ret;
156 static ssize_t show_duplex(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct net_device *netdev = to_net_dev(dev);
160 int ret = -EINVAL;
162 if (!rtnl_trylock())
163 return restart_syscall();
165 if (netif_running(netdev) &&
166 netdev->ethtool_ops &&
167 netdev->ethtool_ops->get_settings) {
168 struct ethtool_cmd cmd = { ETHTOOL_GSET };
170 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
171 ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
173 rtnl_unlock();
174 return ret;
177 static ssize_t show_dormant(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct net_device *netdev = to_net_dev(dev);
182 if (netif_running(netdev))
183 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
185 return -EINVAL;
188 static const char *const operstates[] = {
189 "unknown",
190 "notpresent", /* currently unused */
191 "down",
192 "lowerlayerdown",
193 "testing", /* currently unused */
194 "dormant",
195 "up"
198 static ssize_t show_operstate(struct device *dev,
199 struct device_attribute *attr, char *buf)
201 const struct net_device *netdev = to_net_dev(dev);
202 unsigned char operstate;
204 read_lock(&dev_base_lock);
205 operstate = netdev->operstate;
206 if (!netif_running(netdev))
207 operstate = IF_OPER_DOWN;
208 read_unlock(&dev_base_lock);
210 if (operstate >= ARRAY_SIZE(operstates))
211 return -EINVAL; /* should not happen */
213 return sprintf(buf, "%s\n", operstates[operstate]);
216 /* read-write attributes */
217 NETDEVICE_SHOW(mtu, fmt_dec);
219 static int change_mtu(struct net_device *net, unsigned long new_mtu)
221 return dev_set_mtu(net, (int) new_mtu);
224 static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
225 const char *buf, size_t len)
227 return netdev_store(dev, attr, buf, len, change_mtu);
230 NETDEVICE_SHOW(flags, fmt_hex);
232 static int change_flags(struct net_device *net, unsigned long new_flags)
234 return dev_change_flags(net, (unsigned) new_flags);
237 static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t len)
240 return netdev_store(dev, attr, buf, len, change_flags);
243 NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
245 static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
247 net->tx_queue_len = new_len;
248 return 0;
251 static ssize_t store_tx_queue_len(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t len)
255 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
258 static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t len)
261 struct net_device *netdev = to_net_dev(dev);
262 size_t count = len;
263 ssize_t ret;
265 if (!capable(CAP_NET_ADMIN))
266 return -EPERM;
268 /* ignore trailing newline */
269 if (len > 0 && buf[len - 1] == '\n')
270 --count;
272 if (!rtnl_trylock())
273 return restart_syscall();
274 ret = dev_set_alias(netdev, buf, count);
275 rtnl_unlock();
277 return ret < 0 ? ret : len;
280 static ssize_t show_ifalias(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 const struct net_device *netdev = to_net_dev(dev);
284 ssize_t ret = 0;
286 if (!rtnl_trylock())
287 return restart_syscall();
288 if (netdev->ifalias)
289 ret = sprintf(buf, "%s\n", netdev->ifalias);
290 rtnl_unlock();
291 return ret;
294 static struct device_attribute net_class_attributes[] = {
295 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
296 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
297 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
298 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
299 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
300 __ATTR(features, S_IRUGO, show_features, NULL),
301 __ATTR(type, S_IRUGO, show_type, NULL),
302 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
303 __ATTR(address, S_IRUGO, show_address, NULL),
304 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
305 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
306 __ATTR(speed, S_IRUGO, show_speed, NULL),
307 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
308 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
309 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
310 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
311 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
312 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
313 store_tx_queue_len),
317 /* Show a given an attribute in the statistics group */
318 static ssize_t netstat_show(const struct device *d,
319 struct device_attribute *attr, char *buf,
320 unsigned long offset)
322 struct net_device *dev = to_net_dev(d);
323 ssize_t ret = -EINVAL;
325 WARN_ON(offset > sizeof(struct net_device_stats) ||
326 offset % sizeof(unsigned long) != 0);
328 read_lock(&dev_base_lock);
329 if (dev_isalive(dev)) {
330 const struct net_device_stats *stats = dev_get_stats(dev);
331 ret = sprintf(buf, fmt_ulong,
332 *(unsigned long *)(((u8 *) stats) + offset));
334 read_unlock(&dev_base_lock);
335 return ret;
338 /* generate a read-only statistics attribute */
339 #define NETSTAT_ENTRY(name) \
340 static ssize_t show_##name(struct device *d, \
341 struct device_attribute *attr, char *buf) \
343 return netstat_show(d, attr, buf, \
344 offsetof(struct net_device_stats, name)); \
346 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
348 NETSTAT_ENTRY(rx_packets);
349 NETSTAT_ENTRY(tx_packets);
350 NETSTAT_ENTRY(rx_bytes);
351 NETSTAT_ENTRY(tx_bytes);
352 NETSTAT_ENTRY(rx_errors);
353 NETSTAT_ENTRY(tx_errors);
354 NETSTAT_ENTRY(rx_dropped);
355 NETSTAT_ENTRY(tx_dropped);
356 NETSTAT_ENTRY(multicast);
357 NETSTAT_ENTRY(collisions);
358 NETSTAT_ENTRY(rx_length_errors);
359 NETSTAT_ENTRY(rx_over_errors);
360 NETSTAT_ENTRY(rx_crc_errors);
361 NETSTAT_ENTRY(rx_frame_errors);
362 NETSTAT_ENTRY(rx_fifo_errors);
363 NETSTAT_ENTRY(rx_missed_errors);
364 NETSTAT_ENTRY(tx_aborted_errors);
365 NETSTAT_ENTRY(tx_carrier_errors);
366 NETSTAT_ENTRY(tx_fifo_errors);
367 NETSTAT_ENTRY(tx_heartbeat_errors);
368 NETSTAT_ENTRY(tx_window_errors);
369 NETSTAT_ENTRY(rx_compressed);
370 NETSTAT_ENTRY(tx_compressed);
372 static struct attribute *netstat_attrs[] = {
373 &dev_attr_rx_packets.attr,
374 &dev_attr_tx_packets.attr,
375 &dev_attr_rx_bytes.attr,
376 &dev_attr_tx_bytes.attr,
377 &dev_attr_rx_errors.attr,
378 &dev_attr_tx_errors.attr,
379 &dev_attr_rx_dropped.attr,
380 &dev_attr_tx_dropped.attr,
381 &dev_attr_multicast.attr,
382 &dev_attr_collisions.attr,
383 &dev_attr_rx_length_errors.attr,
384 &dev_attr_rx_over_errors.attr,
385 &dev_attr_rx_crc_errors.attr,
386 &dev_attr_rx_frame_errors.attr,
387 &dev_attr_rx_fifo_errors.attr,
388 &dev_attr_rx_missed_errors.attr,
389 &dev_attr_tx_aborted_errors.attr,
390 &dev_attr_tx_carrier_errors.attr,
391 &dev_attr_tx_fifo_errors.attr,
392 &dev_attr_tx_heartbeat_errors.attr,
393 &dev_attr_tx_window_errors.attr,
394 &dev_attr_rx_compressed.attr,
395 &dev_attr_tx_compressed.attr,
396 NULL
400 static struct attribute_group netstat_group = {
401 .name = "statistics",
402 .attrs = netstat_attrs,
405 #ifdef CONFIG_WIRELESS_EXT_SYSFS
406 /* helper function that does all the locking etc for wireless stats */
407 static ssize_t wireless_show(struct device *d, char *buf,
408 ssize_t (*format)(const struct iw_statistics *,
409 char *))
411 struct net_device *dev = to_net_dev(d);
412 const struct iw_statistics *iw;
413 ssize_t ret = -EINVAL;
415 if (!rtnl_trylock())
416 return restart_syscall();
417 if (dev_isalive(dev)) {
418 iw = get_wireless_stats(dev);
419 if (iw)
420 ret = (*format)(iw, buf);
422 rtnl_unlock();
424 return ret;
427 /* show function template for wireless fields */
428 #define WIRELESS_SHOW(name, field, format_string) \
429 static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
431 return sprintf(buf, format_string, iw->field); \
433 static ssize_t show_iw_##name(struct device *d, \
434 struct device_attribute *attr, char *buf) \
436 return wireless_show(d, buf, format_iw_##name); \
438 static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
440 WIRELESS_SHOW(status, status, fmt_hex);
441 WIRELESS_SHOW(link, qual.qual, fmt_dec);
442 WIRELESS_SHOW(level, qual.level, fmt_dec);
443 WIRELESS_SHOW(noise, qual.noise, fmt_dec);
444 WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
445 WIRELESS_SHOW(crypt, discard.code, fmt_dec);
446 WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
447 WIRELESS_SHOW(misc, discard.misc, fmt_dec);
448 WIRELESS_SHOW(retries, discard.retries, fmt_dec);
449 WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
451 static struct attribute *wireless_attrs[] = {
452 &dev_attr_status.attr,
453 &dev_attr_link.attr,
454 &dev_attr_level.attr,
455 &dev_attr_noise.attr,
456 &dev_attr_nwid.attr,
457 &dev_attr_crypt.attr,
458 &dev_attr_fragment.attr,
459 &dev_attr_retries.attr,
460 &dev_attr_misc.attr,
461 &dev_attr_beacon.attr,
462 NULL
465 static struct attribute_group wireless_group = {
466 .name = "wireless",
467 .attrs = wireless_attrs,
469 #endif
471 #ifdef CONFIG_RPS
473 * RX queue sysfs structures and functions.
475 struct rx_queue_attribute {
476 struct attribute attr;
477 ssize_t (*show)(struct netdev_rx_queue *queue,
478 struct rx_queue_attribute *attr, char *buf);
479 ssize_t (*store)(struct netdev_rx_queue *queue,
480 struct rx_queue_attribute *attr, const char *buf, size_t len);
482 #define to_rx_queue_attr(_attr) container_of(_attr, \
483 struct rx_queue_attribute, attr)
485 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
487 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
488 char *buf)
490 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
491 struct netdev_rx_queue *queue = to_rx_queue(kobj);
493 if (!attribute->show)
494 return -EIO;
496 return attribute->show(queue, attribute, buf);
499 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
500 const char *buf, size_t count)
502 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
503 struct netdev_rx_queue *queue = to_rx_queue(kobj);
505 if (!attribute->store)
506 return -EIO;
508 return attribute->store(queue, attribute, buf, count);
511 static struct sysfs_ops rx_queue_sysfs_ops = {
512 .show = rx_queue_attr_show,
513 .store = rx_queue_attr_store,
516 static ssize_t show_rps_map(struct netdev_rx_queue *queue,
517 struct rx_queue_attribute *attribute, char *buf)
519 struct rps_map *map;
520 cpumask_var_t mask;
521 size_t len = 0;
522 int i;
524 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
525 return -ENOMEM;
527 rcu_read_lock();
528 map = rcu_dereference(queue->rps_map);
529 if (map)
530 for (i = 0; i < map->len; i++)
531 cpumask_set_cpu(map->cpus[i], mask);
533 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
534 if (PAGE_SIZE - len < 3) {
535 rcu_read_unlock();
536 free_cpumask_var(mask);
537 return -EINVAL;
539 rcu_read_unlock();
541 free_cpumask_var(mask);
542 len += sprintf(buf + len, "\n");
543 return len;
546 static void rps_map_release(struct rcu_head *rcu)
548 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
550 kfree(map);
553 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
554 struct rx_queue_attribute *attribute,
555 const char *buf, size_t len)
557 struct rps_map *old_map, *map;
558 cpumask_var_t mask;
559 int err, cpu, i;
560 static DEFINE_SPINLOCK(rps_map_lock);
562 if (!capable(CAP_NET_ADMIN))
563 return -EPERM;
565 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
566 return -ENOMEM;
568 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
569 if (err) {
570 free_cpumask_var(mask);
571 return err;
574 map = kzalloc(max_t(unsigned,
575 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
576 GFP_KERNEL);
577 if (!map) {
578 free_cpumask_var(mask);
579 return -ENOMEM;
582 i = 0;
583 for_each_cpu_and(cpu, mask, cpu_online_mask)
584 map->cpus[i++] = cpu;
586 if (i)
587 map->len = i;
588 else {
589 kfree(map);
590 map = NULL;
593 spin_lock(&rps_map_lock);
594 old_map = queue->rps_map;
595 rcu_assign_pointer(queue->rps_map, map);
596 spin_unlock(&rps_map_lock);
598 if (old_map)
599 call_rcu(&old_map->rcu, rps_map_release);
601 free_cpumask_var(mask);
602 return len;
605 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
606 struct rx_queue_attribute *attr,
607 char *buf)
609 struct rps_dev_flow_table *flow_table;
610 unsigned int val = 0;
612 rcu_read_lock();
613 flow_table = rcu_dereference(queue->rps_flow_table);
614 if (flow_table)
615 val = flow_table->mask + 1;
616 rcu_read_unlock();
618 return sprintf(buf, "%u\n", val);
621 static void rps_dev_flow_table_release_work(struct work_struct *work)
623 struct rps_dev_flow_table *table = container_of(work,
624 struct rps_dev_flow_table, free_work);
626 vfree(table);
629 static void rps_dev_flow_table_release(struct rcu_head *rcu)
631 struct rps_dev_flow_table *table = container_of(rcu,
632 struct rps_dev_flow_table, rcu);
634 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
635 schedule_work(&table->free_work);
638 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
639 struct rx_queue_attribute *attr,
640 const char *buf, size_t len)
642 unsigned int count;
643 char *endp;
644 struct rps_dev_flow_table *table, *old_table;
645 static DEFINE_SPINLOCK(rps_dev_flow_lock);
647 if (!capable(CAP_NET_ADMIN))
648 return -EPERM;
650 count = simple_strtoul(buf, &endp, 0);
651 if (endp == buf)
652 return -EINVAL;
654 if (count) {
655 int i;
657 if (count > 1<<30) {
658 /* Enforce a limit to prevent overflow */
659 return -EINVAL;
661 count = roundup_pow_of_two(count);
662 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
663 if (!table)
664 return -ENOMEM;
666 table->mask = count - 1;
667 for (i = 0; i < count; i++)
668 table->flows[i].cpu = RPS_NO_CPU;
669 } else
670 table = NULL;
672 spin_lock(&rps_dev_flow_lock);
673 old_table = queue->rps_flow_table;
674 rcu_assign_pointer(queue->rps_flow_table, table);
675 spin_unlock(&rps_dev_flow_lock);
677 if (old_table)
678 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
680 return len;
683 static struct rx_queue_attribute rps_cpus_attribute =
684 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
687 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
688 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
689 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
691 static struct attribute *rx_queue_default_attrs[] = {
692 &rps_cpus_attribute.attr,
693 &rps_dev_flow_table_cnt_attribute.attr,
694 NULL
697 static void rx_queue_release(struct kobject *kobj)
699 struct netdev_rx_queue *queue = to_rx_queue(kobj);
700 struct netdev_rx_queue *first = queue->first;
702 if (queue->rps_map)
703 call_rcu(&queue->rps_map->rcu, rps_map_release);
705 if (queue->rps_flow_table)
706 call_rcu(&queue->rps_flow_table->rcu,
707 rps_dev_flow_table_release);
709 if (atomic_dec_and_test(&first->count))
710 kfree(first);
713 static struct kobj_type rx_queue_ktype = {
714 .sysfs_ops = &rx_queue_sysfs_ops,
715 .release = rx_queue_release,
716 .default_attrs = rx_queue_default_attrs,
719 static int rx_queue_add_kobject(struct net_device *net, int index)
721 struct netdev_rx_queue *queue = net->_rx + index;
722 struct kobject *kobj = &queue->kobj;
723 int error = 0;
725 kobj->kset = net->queues_kset;
726 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
727 "rx-%u", index);
728 if (error) {
729 kobject_put(kobj);
730 return error;
733 kobject_uevent(kobj, KOBJ_ADD);
735 return error;
738 static int rx_queue_register_kobjects(struct net_device *net)
740 int i;
741 int error = 0;
743 net->queues_kset = kset_create_and_add("queues",
744 NULL, &net->dev.kobj);
745 if (!net->queues_kset)
746 return -ENOMEM;
747 for (i = 0; i < net->num_rx_queues; i++) {
748 error = rx_queue_add_kobject(net, i);
749 if (error)
750 break;
753 if (error)
754 while (--i >= 0)
755 kobject_put(&net->_rx[i].kobj);
757 return error;
760 static void rx_queue_remove_kobjects(struct net_device *net)
762 int i;
764 for (i = 0; i < net->num_rx_queues; i++)
765 kobject_put(&net->_rx[i].kobj);
766 kset_unregister(net->queues_kset);
768 #endif /* CONFIG_RPS */
769 #endif /* CONFIG_SYSFS */
771 #ifdef CONFIG_HOTPLUG
772 static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
774 struct net_device *dev = to_net_dev(d);
775 int retval;
777 if (!net_eq(dev_net(dev), &init_net))
778 return 0;
780 /* pass interface to uevent. */
781 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
782 if (retval)
783 goto exit;
785 /* pass ifindex to uevent.
786 * ifindex is useful as it won't change (interface name may change)
787 * and is what RtNetlink uses natively. */
788 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
790 exit:
791 return retval;
793 #endif
796 * netdev_release -- destroy and free a dead device.
797 * Called when last reference to device kobject is gone.
799 static void netdev_release(struct device *d)
801 struct net_device *dev = to_net_dev(d);
803 BUG_ON(dev->reg_state != NETREG_RELEASED);
805 kfree(dev->ifalias);
806 kfree((char *)dev - dev->padded);
809 static struct class net_class = {
810 .name = "net",
811 .dev_release = netdev_release,
812 #ifdef CONFIG_SYSFS
813 .dev_attrs = net_class_attributes,
814 #endif /* CONFIG_SYSFS */
815 #ifdef CONFIG_HOTPLUG
816 .dev_uevent = netdev_uevent,
817 #endif
820 /* Delete sysfs entries but hold kobject reference until after all
821 * netdev references are gone.
823 void netdev_unregister_kobject(struct net_device * net)
825 struct device *dev = &(net->dev);
827 kobject_get(&dev->kobj);
829 if (!net_eq(dev_net(net), &init_net))
830 return;
832 #ifdef CONFIG_RPS
833 rx_queue_remove_kobjects(net);
834 #endif
836 device_del(dev);
839 /* Create sysfs entries for network device. */
840 int netdev_register_kobject(struct net_device *net)
842 struct device *dev = &(net->dev);
843 const struct attribute_group **groups = net->sysfs_groups;
844 int error = 0;
846 dev->class = &net_class;
847 dev->platform_data = net;
848 dev->groups = groups;
850 dev_set_name(dev, "%s", net->name);
852 #ifdef CONFIG_SYSFS
853 /* Allow for a device specific group */
854 if (*groups)
855 groups++;
857 *groups++ = &netstat_group;
858 #ifdef CONFIG_WIRELESS_EXT_SYSFS
859 if (net->ieee80211_ptr)
860 *groups++ = &wireless_group;
861 #ifdef CONFIG_WIRELESS_EXT
862 else if (net->wireless_handlers)
863 *groups++ = &wireless_group;
864 #endif
865 #endif
866 #endif /* CONFIG_SYSFS */
868 if (!net_eq(dev_net(net), &init_net))
869 return 0;
871 error = device_add(dev);
872 if (error)
873 return error;
875 #ifdef CONFIG_RPS
876 error = rx_queue_register_kobjects(net);
877 if (error) {
878 device_del(dev);
879 return error;
881 #endif
883 return error;
886 int netdev_class_create_file(struct class_attribute *class_attr)
888 return class_create_file(&net_class, class_attr);
891 void netdev_class_remove_file(struct class_attribute *class_attr)
893 class_remove_file(&net_class, class_attr);
896 EXPORT_SYMBOL(netdev_class_create_file);
897 EXPORT_SYMBOL(netdev_class_remove_file);
899 void netdev_initialize_kobject(struct net_device *net)
901 struct device *device = &(net->dev);
902 device_initialize(device);
905 int netdev_kobject_init(void)
907 return class_register(&net_class);