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 <linux/nsproxy.h>
19 #include <net/net_namespace.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/vmalloc.h>
22 #include <linux/export.h>
23 #include <linux/jiffies.h>
24 #include <linux/pm_runtime.h>
26 #include "net-sysfs.h"
29 static const char fmt_hex[] = "%#x\n";
30 static const char fmt_long_hex[] = "%#lx\n";
31 static const char fmt_dec[] = "%d\n";
32 static const char fmt_udec[] = "%u\n";
33 static const char fmt_ulong[] = "%lu\n";
34 static const char fmt_u64[] = "%llu\n";
36 static inline int dev_isalive(const struct net_device *dev)
38 return dev->reg_state <= NETREG_REGISTERED;
41 /* use same locking rules as GIF* ioctl's */
42 static ssize_t netdev_show(const struct device *dev,
43 struct device_attribute *attr, char *buf,
44 ssize_t (*format)(const struct net_device *, char *))
46 struct net_device *net = to_net_dev(dev);
47 ssize_t ret = -EINVAL;
49 read_lock(&dev_base_lock);
51 ret = (*format)(net, buf);
52 read_unlock(&dev_base_lock);
57 /* generate a show function for simple field */
58 #define NETDEVICE_SHOW(field, format_string) \
59 static ssize_t format_##field(const struct net_device *net, char *buf) \
61 return sprintf(buf, format_string, net->field); \
63 static ssize_t field##_show(struct device *dev, \
64 struct device_attribute *attr, char *buf) \
66 return netdev_show(dev, attr, buf, format_##field); \
69 #define NETDEVICE_SHOW_RO(field, format_string) \
70 NETDEVICE_SHOW(field, format_string); \
71 static DEVICE_ATTR_RO(field)
73 #define NETDEVICE_SHOW_RW(field, format_string) \
74 NETDEVICE_SHOW(field, format_string); \
75 static DEVICE_ATTR_RW(field)
77 /* use same locking and permission rules as SIF* ioctl's */
78 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
79 const char *buf, size_t len,
80 int (*set)(struct net_device *, unsigned long))
82 struct net_device *netdev = to_net_dev(dev);
83 struct net *net = dev_net(netdev);
87 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
90 ret = kstrtoul(buf, 0, &new);
95 return restart_syscall();
97 if (dev_isalive(netdev)) {
98 if ((ret = (*set)(netdev, new)) == 0)
106 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
107 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
108 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
109 NETDEVICE_SHOW_RO(iflink, fmt_dec);
110 NETDEVICE_SHOW_RO(ifindex, fmt_dec);
111 NETDEVICE_SHOW_RO(type, fmt_dec);
112 NETDEVICE_SHOW_RO(link_mode, fmt_dec);
114 /* use same locking rules as GIFHWADDR ioctl's */
115 static ssize_t address_show(struct device *dev, struct device_attribute *attr,
118 struct net_device *net = to_net_dev(dev);
119 ssize_t ret = -EINVAL;
121 read_lock(&dev_base_lock);
122 if (dev_isalive(net))
123 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
124 read_unlock(&dev_base_lock);
127 static DEVICE_ATTR_RO(address);
129 static ssize_t broadcast_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
132 struct net_device *net = to_net_dev(dev);
133 if (dev_isalive(net))
134 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
137 static DEVICE_ATTR_RO(broadcast);
139 static int change_carrier(struct net_device *net, unsigned long new_carrier)
141 if (!netif_running(net))
143 return dev_change_carrier(net, (bool) new_carrier);
146 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t len)
149 return netdev_store(dev, attr, buf, len, change_carrier);
152 static ssize_t carrier_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct net_device *netdev = to_net_dev(dev);
156 if (netif_running(netdev)) {
157 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
161 static DEVICE_ATTR_RW(carrier);
163 static ssize_t speed_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct net_device *netdev = to_net_dev(dev);
170 return restart_syscall();
172 if (netif_running(netdev)) {
173 struct ethtool_cmd cmd;
174 if (!__ethtool_get_settings(netdev, &cmd))
175 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
180 static DEVICE_ATTR_RO(speed);
182 static ssize_t duplex_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct net_device *netdev = to_net_dev(dev);
189 return restart_syscall();
191 if (netif_running(netdev)) {
192 struct ethtool_cmd cmd;
193 if (!__ethtool_get_settings(netdev, &cmd)) {
195 switch (cmd.duplex) {
206 ret = sprintf(buf, "%s\n", duplex);
212 static DEVICE_ATTR_RO(duplex);
214 static ssize_t dormant_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct net_device *netdev = to_net_dev(dev);
219 if (netif_running(netdev))
220 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
224 static DEVICE_ATTR_RO(dormant);
226 static const char *const operstates[] = {
228 "notpresent", /* currently unused */
231 "testing", /* currently unused */
236 static ssize_t operstate_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
239 const struct net_device *netdev = to_net_dev(dev);
240 unsigned char operstate;
242 read_lock(&dev_base_lock);
243 operstate = netdev->operstate;
244 if (!netif_running(netdev))
245 operstate = IF_OPER_DOWN;
246 read_unlock(&dev_base_lock);
248 if (operstate >= ARRAY_SIZE(operstates))
249 return -EINVAL; /* should not happen */
251 return sprintf(buf, "%s\n", operstates[operstate]);
253 static DEVICE_ATTR_RO(operstate);
255 /* read-write attributes */
257 static int change_mtu(struct net_device *net, unsigned long new_mtu)
259 return dev_set_mtu(net, (int) new_mtu);
262 static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t len)
265 return netdev_store(dev, attr, buf, len, change_mtu);
267 NETDEVICE_SHOW_RW(mtu, fmt_dec);
269 static int change_flags(struct net_device *net, unsigned long new_flags)
271 return dev_change_flags(net, (unsigned int) new_flags);
274 static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t len)
277 return netdev_store(dev, attr, buf, len, change_flags);
279 NETDEVICE_SHOW_RW(flags, fmt_hex);
281 static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
283 net->tx_queue_len = new_len;
287 static ssize_t tx_queue_len_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t len)
291 if (!capable(CAP_NET_ADMIN))
294 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
296 NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
298 static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t len)
301 struct net_device *netdev = to_net_dev(dev);
302 struct net *net = dev_net(netdev);
306 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
309 /* ignore trailing newline */
310 if (len > 0 && buf[len - 1] == '\n')
314 return restart_syscall();
315 ret = dev_set_alias(netdev, buf, count);
318 return ret < 0 ? ret : len;
321 static ssize_t ifalias_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 const struct net_device *netdev = to_net_dev(dev);
328 return restart_syscall();
330 ret = sprintf(buf, "%s\n", netdev->ifalias);
334 static DEVICE_ATTR_RW(ifalias);
336 static int change_group(struct net_device *net, unsigned long new_group)
338 dev_set_group(net, (int) new_group);
342 static ssize_t group_store(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t len)
345 return netdev_store(dev, attr, buf, len, change_group);
347 NETDEVICE_SHOW(group, fmt_dec);
348 static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
350 static ssize_t phys_port_id_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
353 struct net_device *netdev = to_net_dev(dev);
354 ssize_t ret = -EINVAL;
357 return restart_syscall();
359 if (dev_isalive(netdev)) {
360 struct netdev_phys_port_id ppid;
362 ret = dev_get_phys_port_id(netdev, &ppid);
364 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
370 static DEVICE_ATTR_RO(phys_port_id);
372 static struct attribute *net_class_attrs[] = {
373 &dev_attr_netdev_group.attr,
375 &dev_attr_dev_id.attr,
376 &dev_attr_iflink.attr,
377 &dev_attr_ifindex.attr,
378 &dev_attr_addr_assign_type.attr,
379 &dev_attr_addr_len.attr,
380 &dev_attr_link_mode.attr,
381 &dev_attr_address.attr,
382 &dev_attr_broadcast.attr,
383 &dev_attr_speed.attr,
384 &dev_attr_duplex.attr,
385 &dev_attr_dormant.attr,
386 &dev_attr_operstate.attr,
387 &dev_attr_ifalias.attr,
388 &dev_attr_carrier.attr,
390 &dev_attr_flags.attr,
391 &dev_attr_tx_queue_len.attr,
392 &dev_attr_phys_port_id.attr,
395 ATTRIBUTE_GROUPS(net_class);
397 /* Show a given an attribute in the statistics group */
398 static ssize_t netstat_show(const struct device *d,
399 struct device_attribute *attr, char *buf,
400 unsigned long offset)
402 struct net_device *dev = to_net_dev(d);
403 ssize_t ret = -EINVAL;
405 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
406 offset % sizeof(u64) != 0);
408 read_lock(&dev_base_lock);
409 if (dev_isalive(dev)) {
410 struct rtnl_link_stats64 temp;
411 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
413 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
415 read_unlock(&dev_base_lock);
419 /* generate a read-only statistics attribute */
420 #define NETSTAT_ENTRY(name) \
421 static ssize_t name##_show(struct device *d, \
422 struct device_attribute *attr, char *buf) \
424 return netstat_show(d, attr, buf, \
425 offsetof(struct rtnl_link_stats64, name)); \
427 static DEVICE_ATTR_RO(name)
429 NETSTAT_ENTRY(rx_packets);
430 NETSTAT_ENTRY(tx_packets);
431 NETSTAT_ENTRY(rx_bytes);
432 NETSTAT_ENTRY(tx_bytes);
433 NETSTAT_ENTRY(rx_errors);
434 NETSTAT_ENTRY(tx_errors);
435 NETSTAT_ENTRY(rx_dropped);
436 NETSTAT_ENTRY(tx_dropped);
437 NETSTAT_ENTRY(multicast);
438 NETSTAT_ENTRY(collisions);
439 NETSTAT_ENTRY(rx_length_errors);
440 NETSTAT_ENTRY(rx_over_errors);
441 NETSTAT_ENTRY(rx_crc_errors);
442 NETSTAT_ENTRY(rx_frame_errors);
443 NETSTAT_ENTRY(rx_fifo_errors);
444 NETSTAT_ENTRY(rx_missed_errors);
445 NETSTAT_ENTRY(tx_aborted_errors);
446 NETSTAT_ENTRY(tx_carrier_errors);
447 NETSTAT_ENTRY(tx_fifo_errors);
448 NETSTAT_ENTRY(tx_heartbeat_errors);
449 NETSTAT_ENTRY(tx_window_errors);
450 NETSTAT_ENTRY(rx_compressed);
451 NETSTAT_ENTRY(tx_compressed);
453 static struct attribute *netstat_attrs[] = {
454 &dev_attr_rx_packets.attr,
455 &dev_attr_tx_packets.attr,
456 &dev_attr_rx_bytes.attr,
457 &dev_attr_tx_bytes.attr,
458 &dev_attr_rx_errors.attr,
459 &dev_attr_tx_errors.attr,
460 &dev_attr_rx_dropped.attr,
461 &dev_attr_tx_dropped.attr,
462 &dev_attr_multicast.attr,
463 &dev_attr_collisions.attr,
464 &dev_attr_rx_length_errors.attr,
465 &dev_attr_rx_over_errors.attr,
466 &dev_attr_rx_crc_errors.attr,
467 &dev_attr_rx_frame_errors.attr,
468 &dev_attr_rx_fifo_errors.attr,
469 &dev_attr_rx_missed_errors.attr,
470 &dev_attr_tx_aborted_errors.attr,
471 &dev_attr_tx_carrier_errors.attr,
472 &dev_attr_tx_fifo_errors.attr,
473 &dev_attr_tx_heartbeat_errors.attr,
474 &dev_attr_tx_window_errors.attr,
475 &dev_attr_rx_compressed.attr,
476 &dev_attr_tx_compressed.attr,
481 static struct attribute_group netstat_group = {
482 .name = "statistics",
483 .attrs = netstat_attrs,
486 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
487 static struct attribute *wireless_attrs[] = {
491 static struct attribute_group wireless_group = {
493 .attrs = wireless_attrs,
497 #else /* CONFIG_SYSFS */
498 #define net_class_groups NULL
499 #endif /* CONFIG_SYSFS */
502 #define to_rx_queue_attr(_attr) container_of(_attr, \
503 struct rx_queue_attribute, attr)
505 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
507 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
510 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
511 struct netdev_rx_queue *queue = to_rx_queue(kobj);
513 if (!attribute->show)
516 return attribute->show(queue, attribute, buf);
519 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
520 const char *buf, size_t count)
522 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
523 struct netdev_rx_queue *queue = to_rx_queue(kobj);
525 if (!attribute->store)
528 return attribute->store(queue, attribute, buf, count);
531 static const struct sysfs_ops rx_queue_sysfs_ops = {
532 .show = rx_queue_attr_show,
533 .store = rx_queue_attr_store,
537 static ssize_t show_rps_map(struct netdev_rx_queue *queue,
538 struct rx_queue_attribute *attribute, char *buf)
545 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
549 map = rcu_dereference(queue->rps_map);
551 for (i = 0; i < map->len; i++)
552 cpumask_set_cpu(map->cpus[i], mask);
554 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
555 if (PAGE_SIZE - len < 3) {
557 free_cpumask_var(mask);
562 free_cpumask_var(mask);
563 len += sprintf(buf + len, "\n");
567 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
568 struct rx_queue_attribute *attribute,
569 const char *buf, size_t len)
571 struct rps_map *old_map, *map;
574 static DEFINE_SPINLOCK(rps_map_lock);
576 if (!capable(CAP_NET_ADMIN))
579 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
582 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
584 free_cpumask_var(mask);
588 map = kzalloc(max_t(unsigned int,
589 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
592 free_cpumask_var(mask);
597 for_each_cpu_and(cpu, mask, cpu_online_mask)
598 map->cpus[i++] = cpu;
607 spin_lock(&rps_map_lock);
608 old_map = rcu_dereference_protected(queue->rps_map,
609 lockdep_is_held(&rps_map_lock));
610 rcu_assign_pointer(queue->rps_map, map);
611 spin_unlock(&rps_map_lock);
614 static_key_slow_inc(&rps_needed);
616 kfree_rcu(old_map, rcu);
617 static_key_slow_dec(&rps_needed);
619 free_cpumask_var(mask);
623 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
624 struct rx_queue_attribute *attr,
627 struct rps_dev_flow_table *flow_table;
628 unsigned long val = 0;
631 flow_table = rcu_dereference(queue->rps_flow_table);
633 val = (unsigned long)flow_table->mask + 1;
636 return sprintf(buf, "%lu\n", val);
639 static void rps_dev_flow_table_release(struct rcu_head *rcu)
641 struct rps_dev_flow_table *table = container_of(rcu,
642 struct rps_dev_flow_table, rcu);
646 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
647 struct rx_queue_attribute *attr,
648 const char *buf, size_t len)
650 unsigned long mask, count;
651 struct rps_dev_flow_table *table, *old_table;
652 static DEFINE_SPINLOCK(rps_dev_flow_lock);
655 if (!capable(CAP_NET_ADMIN))
658 rc = kstrtoul(buf, 0, &count);
664 /* mask = roundup_pow_of_two(count) - 1;
665 * without overflows...
667 while ((mask | (mask >> 1)) != mask)
669 /* On 64 bit arches, must check mask fits in table->mask (u32),
670 * and on 32bit arches, must check
671 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
673 #if BITS_PER_LONG > 32
674 if (mask > (unsigned long)(u32)mask)
677 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
678 / sizeof(struct rps_dev_flow)) {
679 /* Enforce a limit to prevent overflow */
683 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
688 for (count = 0; count <= mask; count++)
689 table->flows[count].cpu = RPS_NO_CPU;
693 spin_lock(&rps_dev_flow_lock);
694 old_table = rcu_dereference_protected(queue->rps_flow_table,
695 lockdep_is_held(&rps_dev_flow_lock));
696 rcu_assign_pointer(queue->rps_flow_table, table);
697 spin_unlock(&rps_dev_flow_lock);
700 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
705 static struct rx_queue_attribute rps_cpus_attribute =
706 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
709 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
710 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
711 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
712 #endif /* CONFIG_RPS */
714 static struct attribute *rx_queue_default_attrs[] = {
716 &rps_cpus_attribute.attr,
717 &rps_dev_flow_table_cnt_attribute.attr,
722 static void rx_queue_release(struct kobject *kobj)
724 struct netdev_rx_queue *queue = to_rx_queue(kobj);
727 struct rps_dev_flow_table *flow_table;
730 map = rcu_dereference_protected(queue->rps_map, 1);
732 RCU_INIT_POINTER(queue->rps_map, NULL);
736 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
738 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
739 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
743 memset(kobj, 0, sizeof(*kobj));
747 static const void *rx_queue_namespace(struct kobject *kobj)
749 struct netdev_rx_queue *queue = to_rx_queue(kobj);
750 struct device *dev = &queue->dev->dev;
751 const void *ns = NULL;
753 if (dev->class && dev->class->ns_type)
754 ns = dev->class->namespace(dev);
759 static struct kobj_type rx_queue_ktype = {
760 .sysfs_ops = &rx_queue_sysfs_ops,
761 .release = rx_queue_release,
762 .default_attrs = rx_queue_default_attrs,
763 .namespace = rx_queue_namespace
766 static int rx_queue_add_kobject(struct net_device *net, int index)
768 struct netdev_rx_queue *queue = net->_rx + index;
769 struct kobject *kobj = &queue->kobj;
772 kobj->kset = net->queues_kset;
773 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
778 if (net->sysfs_rx_queue_group) {
779 error = sysfs_create_group(kobj, net->sysfs_rx_queue_group);
784 kobject_uevent(kobj, KOBJ_ADD);
785 dev_hold(queue->dev);
792 #endif /* CONFIG_SYFS */
795 net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
802 if (!net->sysfs_rx_queue_group)
805 for (i = old_num; i < new_num; i++) {
806 error = rx_queue_add_kobject(net, i);
813 while (--i >= new_num) {
814 if (net->sysfs_rx_queue_group)
815 sysfs_remove_group(&net->_rx[i].kobj,
816 net->sysfs_rx_queue_group);
817 kobject_put(&net->_rx[i].kobj);
828 * netdev_queue sysfs structures and functions.
830 struct netdev_queue_attribute {
831 struct attribute attr;
832 ssize_t (*show)(struct netdev_queue *queue,
833 struct netdev_queue_attribute *attr, char *buf);
834 ssize_t (*store)(struct netdev_queue *queue,
835 struct netdev_queue_attribute *attr, const char *buf, size_t len);
837 #define to_netdev_queue_attr(_attr) container_of(_attr, \
838 struct netdev_queue_attribute, attr)
840 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
842 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
843 struct attribute *attr, char *buf)
845 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
846 struct netdev_queue *queue = to_netdev_queue(kobj);
848 if (!attribute->show)
851 return attribute->show(queue, attribute, buf);
854 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
855 struct attribute *attr,
856 const char *buf, size_t count)
858 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
859 struct netdev_queue *queue = to_netdev_queue(kobj);
861 if (!attribute->store)
864 return attribute->store(queue, attribute, buf, count);
867 static const struct sysfs_ops netdev_queue_sysfs_ops = {
868 .show = netdev_queue_attr_show,
869 .store = netdev_queue_attr_store,
872 static ssize_t show_trans_timeout(struct netdev_queue *queue,
873 struct netdev_queue_attribute *attribute,
876 unsigned long trans_timeout;
878 spin_lock_irq(&queue->_xmit_lock);
879 trans_timeout = queue->trans_timeout;
880 spin_unlock_irq(&queue->_xmit_lock);
882 return sprintf(buf, "%lu", trans_timeout);
885 static struct netdev_queue_attribute queue_trans_timeout =
886 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
890 * Byte queue limits sysfs structures and functions.
892 static ssize_t bql_show(char *buf, unsigned int value)
894 return sprintf(buf, "%u\n", value);
897 static ssize_t bql_set(const char *buf, const size_t count,
898 unsigned int *pvalue)
903 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
904 value = DQL_MAX_LIMIT;
906 err = kstrtouint(buf, 10, &value);
909 if (value > DQL_MAX_LIMIT)
918 static ssize_t bql_show_hold_time(struct netdev_queue *queue,
919 struct netdev_queue_attribute *attr,
922 struct dql *dql = &queue->dql;
924 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
927 static ssize_t bql_set_hold_time(struct netdev_queue *queue,
928 struct netdev_queue_attribute *attribute,
929 const char *buf, size_t len)
931 struct dql *dql = &queue->dql;
935 err = kstrtouint(buf, 10, &value);
939 dql->slack_hold_time = msecs_to_jiffies(value);
944 static struct netdev_queue_attribute bql_hold_time_attribute =
945 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
948 static ssize_t bql_show_inflight(struct netdev_queue *queue,
949 struct netdev_queue_attribute *attr,
952 struct dql *dql = &queue->dql;
954 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
957 static struct netdev_queue_attribute bql_inflight_attribute =
958 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
960 #define BQL_ATTR(NAME, FIELD) \
961 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
962 struct netdev_queue_attribute *attr, \
965 return bql_show(buf, queue->dql.FIELD); \
968 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
969 struct netdev_queue_attribute *attr, \
970 const char *buf, size_t len) \
972 return bql_set(buf, len, &queue->dql.FIELD); \
975 static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
976 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
979 BQL_ATTR(limit, limit)
980 BQL_ATTR(limit_max, max_limit)
981 BQL_ATTR(limit_min, min_limit)
983 static struct attribute *dql_attrs[] = {
984 &bql_limit_attribute.attr,
985 &bql_limit_max_attribute.attr,
986 &bql_limit_min_attribute.attr,
987 &bql_hold_time_attribute.attr,
988 &bql_inflight_attribute.attr,
992 static struct attribute_group dql_group = {
993 .name = "byte_queue_limits",
996 #endif /* CONFIG_BQL */
999 static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1001 struct net_device *dev = queue->dev;
1004 for (i = 0; i < dev->num_tx_queues; i++)
1005 if (queue == &dev->_tx[i])
1008 BUG_ON(i >= dev->num_tx_queues);
1014 static ssize_t show_xps_map(struct netdev_queue *queue,
1015 struct netdev_queue_attribute *attribute, char *buf)
1017 struct net_device *dev = queue->dev;
1018 struct xps_dev_maps *dev_maps;
1020 unsigned long index;
1024 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1027 index = get_netdev_queue_index(queue);
1030 dev_maps = rcu_dereference(dev->xps_maps);
1032 for_each_possible_cpu(i) {
1033 struct xps_map *map =
1034 rcu_dereference(dev_maps->cpu_map[i]);
1037 for (j = 0; j < map->len; j++) {
1038 if (map->queues[j] == index) {
1039 cpumask_set_cpu(i, mask);
1048 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1049 if (PAGE_SIZE - len < 3) {
1050 free_cpumask_var(mask);
1054 free_cpumask_var(mask);
1055 len += sprintf(buf + len, "\n");
1059 static ssize_t store_xps_map(struct netdev_queue *queue,
1060 struct netdev_queue_attribute *attribute,
1061 const char *buf, size_t len)
1063 struct net_device *dev = queue->dev;
1064 unsigned long index;
1068 if (!capable(CAP_NET_ADMIN))
1071 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1074 index = get_netdev_queue_index(queue);
1076 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1078 free_cpumask_var(mask);
1082 err = netif_set_xps_queue(dev, mask, index);
1084 free_cpumask_var(mask);
1089 static struct netdev_queue_attribute xps_cpus_attribute =
1090 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1091 #endif /* CONFIG_XPS */
1093 static struct attribute *netdev_queue_default_attrs[] = {
1094 &queue_trans_timeout.attr,
1096 &xps_cpus_attribute.attr,
1101 static void netdev_queue_release(struct kobject *kobj)
1103 struct netdev_queue *queue = to_netdev_queue(kobj);
1105 memset(kobj, 0, sizeof(*kobj));
1106 dev_put(queue->dev);
1109 static const void *netdev_queue_namespace(struct kobject *kobj)
1111 struct netdev_queue *queue = to_netdev_queue(kobj);
1112 struct device *dev = &queue->dev->dev;
1113 const void *ns = NULL;
1115 if (dev->class && dev->class->ns_type)
1116 ns = dev->class->namespace(dev);
1121 static struct kobj_type netdev_queue_ktype = {
1122 .sysfs_ops = &netdev_queue_sysfs_ops,
1123 .release = netdev_queue_release,
1124 .default_attrs = netdev_queue_default_attrs,
1125 .namespace = netdev_queue_namespace,
1128 static int netdev_queue_add_kobject(struct net_device *net, int index)
1130 struct netdev_queue *queue = net->_tx + index;
1131 struct kobject *kobj = &queue->kobj;
1134 kobj->kset = net->queues_kset;
1135 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1141 error = sysfs_create_group(kobj, &dql_group);
1146 kobject_uevent(kobj, KOBJ_ADD);
1147 dev_hold(queue->dev);
1154 #endif /* CONFIG_SYSFS */
1157 netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1163 for (i = old_num; i < new_num; i++) {
1164 error = netdev_queue_add_kobject(net, i);
1171 while (--i >= new_num) {
1172 struct netdev_queue *queue = net->_tx + i;
1175 sysfs_remove_group(&queue->kobj, &dql_group);
1177 kobject_put(&queue->kobj);
1183 #endif /* CONFIG_SYSFS */
1186 static int register_queue_kobjects(struct net_device *net)
1188 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1191 net->queues_kset = kset_create_and_add("queues",
1192 NULL, &net->dev.kobj);
1193 if (!net->queues_kset)
1195 real_rx = net->real_num_rx_queues;
1197 real_tx = net->real_num_tx_queues;
1199 error = net_rx_queue_update_kobjects(net, 0, real_rx);
1204 error = netdev_queue_update_kobjects(net, 0, real_tx);
1212 netdev_queue_update_kobjects(net, txq, 0);
1213 net_rx_queue_update_kobjects(net, rxq, 0);
1217 static void remove_queue_kobjects(struct net_device *net)
1219 int real_rx = 0, real_tx = 0;
1222 real_rx = net->real_num_rx_queues;
1224 real_tx = net->real_num_tx_queues;
1226 net_rx_queue_update_kobjects(net, real_rx, 0);
1227 netdev_queue_update_kobjects(net, real_tx, 0);
1229 kset_unregister(net->queues_kset);
1233 static bool net_current_may_mount(void)
1235 struct net *net = current->nsproxy->net_ns;
1237 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1240 static void *net_grab_current_ns(void)
1242 struct net *ns = current->nsproxy->net_ns;
1243 #ifdef CONFIG_NET_NS
1245 atomic_inc(&ns->passive);
1250 static const void *net_initial_ns(void)
1255 static const void *net_netlink_ns(struct sock *sk)
1257 return sock_net(sk);
1260 struct kobj_ns_type_operations net_ns_type_operations = {
1261 .type = KOBJ_NS_TYPE_NET,
1262 .current_may_mount = net_current_may_mount,
1263 .grab_current_ns = net_grab_current_ns,
1264 .netlink_ns = net_netlink_ns,
1265 .initial_ns = net_initial_ns,
1266 .drop_ns = net_drop_ns,
1268 EXPORT_SYMBOL_GPL(net_ns_type_operations);
1270 static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1272 struct net_device *dev = to_net_dev(d);
1275 /* pass interface to uevent. */
1276 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1280 /* pass ifindex to uevent.
1281 * ifindex is useful as it won't change (interface name may change)
1282 * and is what RtNetlink uses natively. */
1283 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1290 * netdev_release -- destroy and free a dead device.
1291 * Called when last reference to device kobject is gone.
1293 static void netdev_release(struct device *d)
1295 struct net_device *dev = to_net_dev(d);
1297 BUG_ON(dev->reg_state != NETREG_RELEASED);
1299 kfree(dev->ifalias);
1300 netdev_freemem(dev);
1303 static const void *net_namespace(struct device *d)
1305 struct net_device *dev;
1306 dev = container_of(d, struct net_device, dev);
1307 return dev_net(dev);
1310 static struct class net_class = {
1312 .dev_release = netdev_release,
1313 .dev_groups = net_class_groups,
1314 .dev_uevent = netdev_uevent,
1315 .ns_type = &net_ns_type_operations,
1316 .namespace = net_namespace,
1319 /* Delete sysfs entries but hold kobject reference until after all
1320 * netdev references are gone.
1322 void netdev_unregister_kobject(struct net_device * net)
1324 struct device *dev = &(net->dev);
1326 kobject_get(&dev->kobj);
1328 remove_queue_kobjects(net);
1330 pm_runtime_set_memalloc_noio(dev, false);
1335 /* Create sysfs entries for network device. */
1336 int netdev_register_kobject(struct net_device *net)
1338 struct device *dev = &(net->dev);
1339 const struct attribute_group **groups = net->sysfs_groups;
1342 device_initialize(dev);
1343 dev->class = &net_class;
1344 dev->platform_data = net;
1345 dev->groups = groups;
1347 dev_set_name(dev, "%s", net->name);
1350 /* Allow for a device specific group */
1354 *groups++ = &netstat_group;
1356 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1357 if (net->ieee80211_ptr)
1358 *groups++ = &wireless_group;
1359 #if IS_ENABLED(CONFIG_WIRELESS_EXT)
1360 else if (net->wireless_handlers)
1361 *groups++ = &wireless_group;
1364 #endif /* CONFIG_SYSFS */
1366 error = device_add(dev);
1370 error = register_queue_kobjects(net);
1376 pm_runtime_set_memalloc_noio(dev, true);
1381 int netdev_class_create_file_ns(struct class_attribute *class_attr,
1384 return class_create_file_ns(&net_class, class_attr, ns);
1386 EXPORT_SYMBOL(netdev_class_create_file_ns);
1388 void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1391 class_remove_file_ns(&net_class, class_attr, ns);
1393 EXPORT_SYMBOL(netdev_class_remove_file_ns);
1395 int __init netdev_kobject_init(void)
1397 kobj_ns_type_register(&net_ns_type_operations);
1398 return class_register(&net_class);