1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net-sysfs.c - network device class and attributes
5 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
8 #include <linux/capability.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sched/isolation.h>
15 #include <linux/nsproxy.h>
17 #include <net/net_namespace.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/vmalloc.h>
20 #include <linux/export.h>
21 #include <linux/jiffies.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/of_net.h>
25 #include <linux/cpu.h>
26 #include <net/netdev_rx_queue.h>
30 #include "net-sysfs.h"
33 static const char fmt_hex[] = "%#x\n";
34 static const char fmt_dec[] = "%d\n";
35 static const char fmt_ulong[] = "%lu\n";
36 static const char fmt_u64[] = "%llu\n";
38 /* Caller holds RTNL or RCU */
39 static inline int dev_isalive(const struct net_device *dev)
41 return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
44 /* use same locking rules as GIF* ioctl's */
45 static ssize_t netdev_show(const struct device *dev,
46 struct device_attribute *attr, char *buf,
47 ssize_t (*format)(const struct net_device *, char *))
49 struct net_device *ndev = to_net_dev(dev);
50 ssize_t ret = -EINVAL;
53 if (dev_isalive(ndev))
54 ret = (*format)(ndev, buf);
60 /* generate a show function for simple field */
61 #define NETDEVICE_SHOW(field, format_string) \
62 static ssize_t format_##field(const struct net_device *dev, char *buf) \
64 return sysfs_emit(buf, format_string, READ_ONCE(dev->field)); \
66 static ssize_t field##_show(struct device *dev, \
67 struct device_attribute *attr, char *buf) \
69 return netdev_show(dev, attr, buf, format_##field); \
72 #define NETDEVICE_SHOW_RO(field, format_string) \
73 NETDEVICE_SHOW(field, format_string); \
74 static DEVICE_ATTR_RO(field)
76 #define NETDEVICE_SHOW_RW(field, format_string) \
77 NETDEVICE_SHOW(field, format_string); \
78 static DEVICE_ATTR_RW(field)
80 /* use same locking and permission rules as SIF* ioctl's */
81 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
82 const char *buf, size_t len,
83 int (*set)(struct net_device *, unsigned long))
85 struct net_device *netdev = to_net_dev(dev);
86 struct net *net = dev_net(netdev);
90 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
93 ret = kstrtoul(buf, 0, &new);
98 return restart_syscall();
100 if (dev_isalive(netdev)) {
101 ret = (*set)(netdev, new);
110 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
111 NETDEVICE_SHOW_RO(dev_port, fmt_dec);
112 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
113 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
114 NETDEVICE_SHOW_RO(ifindex, fmt_dec);
115 NETDEVICE_SHOW_RO(type, fmt_dec);
116 NETDEVICE_SHOW_RO(link_mode, fmt_dec);
118 static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
121 struct net_device *ndev = to_net_dev(dev);
123 return sysfs_emit(buf, fmt_dec, dev_get_iflink(ndev));
125 static DEVICE_ATTR_RO(iflink);
127 static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
129 return sysfs_emit(buf, fmt_dec, READ_ONCE(dev->name_assign_type));
132 static ssize_t name_assign_type_show(struct device *dev,
133 struct device_attribute *attr,
136 struct net_device *ndev = to_net_dev(dev);
137 ssize_t ret = -EINVAL;
139 if (READ_ONCE(ndev->name_assign_type) != NET_NAME_UNKNOWN)
140 ret = netdev_show(dev, attr, buf, format_name_assign_type);
144 static DEVICE_ATTR_RO(name_assign_type);
146 /* use same locking rules as GIFHWADDR ioctl's (dev_get_mac_address()) */
147 static ssize_t address_show(struct device *dev, struct device_attribute *attr,
150 struct net_device *ndev = to_net_dev(dev);
151 ssize_t ret = -EINVAL;
153 down_read(&dev_addr_sem);
156 if (dev_isalive(ndev))
157 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
160 up_read(&dev_addr_sem);
163 static DEVICE_ATTR_RO(address);
165 static ssize_t broadcast_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct net_device *ndev = to_net_dev(dev);
172 if (dev_isalive(ndev))
173 ret = sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
177 static DEVICE_ATTR_RO(broadcast);
179 static int change_carrier(struct net_device *dev, unsigned long new_carrier)
181 if (!netif_running(dev))
183 return dev_change_carrier(dev, (bool)new_carrier);
186 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
187 const char *buf, size_t len)
189 struct net_device *netdev = to_net_dev(dev);
191 /* The check is also done in change_carrier; this helps returning early
192 * without hitting the trylock/restart in netdev_store.
194 if (!netdev->netdev_ops->ndo_change_carrier)
197 return netdev_store(dev, attr, buf, len, change_carrier);
200 static ssize_t carrier_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct net_device *netdev = to_net_dev(dev);
207 return restart_syscall();
209 if (netif_running(netdev)) {
210 /* Synchronize carrier state with link watch,
211 * see also rtnl_getlink().
213 linkwatch_sync_dev(netdev);
215 ret = sysfs_emit(buf, fmt_dec, !!netif_carrier_ok(netdev));
221 static DEVICE_ATTR_RW(carrier);
223 static ssize_t speed_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
226 struct net_device *netdev = to_net_dev(dev);
229 /* The check is also done in __ethtool_get_link_ksettings; this helps
230 * returning early without hitting the trylock/restart below.
232 if (!netdev->ethtool_ops->get_link_ksettings)
236 return restart_syscall();
238 if (netif_running(netdev) && netif_device_present(netdev)) {
239 struct ethtool_link_ksettings cmd;
241 if (!__ethtool_get_link_ksettings(netdev, &cmd))
242 ret = sysfs_emit(buf, fmt_dec, cmd.base.speed);
247 static DEVICE_ATTR_RO(speed);
249 static ssize_t duplex_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct net_device *netdev = to_net_dev(dev);
255 /* The check is also done in __ethtool_get_link_ksettings; this helps
256 * returning early without hitting the trylock/restart below.
258 if (!netdev->ethtool_ops->get_link_ksettings)
262 return restart_syscall();
264 if (netif_running(netdev)) {
265 struct ethtool_link_ksettings cmd;
267 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
270 switch (cmd.base.duplex) {
281 ret = sysfs_emit(buf, "%s\n", duplex);
287 static DEVICE_ATTR_RO(duplex);
289 static ssize_t testing_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct net_device *netdev = to_net_dev(dev);
294 if (netif_running(netdev))
295 return sysfs_emit(buf, fmt_dec, !!netif_testing(netdev));
299 static DEVICE_ATTR_RO(testing);
301 static ssize_t dormant_show(struct device *dev,
302 struct device_attribute *attr, char *buf)
304 struct net_device *netdev = to_net_dev(dev);
306 if (netif_running(netdev))
307 return sysfs_emit(buf, fmt_dec, !!netif_dormant(netdev));
311 static DEVICE_ATTR_RO(dormant);
313 static const char *const operstates[] = {
315 "notpresent", /* currently unused */
323 static ssize_t operstate_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 const struct net_device *netdev = to_net_dev(dev);
327 unsigned char operstate;
329 operstate = READ_ONCE(netdev->operstate);
330 if (!netif_running(netdev))
331 operstate = IF_OPER_DOWN;
333 if (operstate >= ARRAY_SIZE(operstates))
334 return -EINVAL; /* should not happen */
336 return sysfs_emit(buf, "%s\n", operstates[operstate]);
338 static DEVICE_ATTR_RO(operstate);
340 static ssize_t carrier_changes_show(struct device *dev,
341 struct device_attribute *attr,
344 struct net_device *netdev = to_net_dev(dev);
346 return sysfs_emit(buf, fmt_dec,
347 atomic_read(&netdev->carrier_up_count) +
348 atomic_read(&netdev->carrier_down_count));
350 static DEVICE_ATTR_RO(carrier_changes);
352 static ssize_t carrier_up_count_show(struct device *dev,
353 struct device_attribute *attr,
356 struct net_device *netdev = to_net_dev(dev);
358 return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
360 static DEVICE_ATTR_RO(carrier_up_count);
362 static ssize_t carrier_down_count_show(struct device *dev,
363 struct device_attribute *attr,
366 struct net_device *netdev = to_net_dev(dev);
368 return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
370 static DEVICE_ATTR_RO(carrier_down_count);
372 /* read-write attributes */
374 static int change_mtu(struct net_device *dev, unsigned long new_mtu)
376 return dev_set_mtu(dev, (int)new_mtu);
379 static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t len)
382 return netdev_store(dev, attr, buf, len, change_mtu);
384 NETDEVICE_SHOW_RW(mtu, fmt_dec);
386 static int change_flags(struct net_device *dev, unsigned long new_flags)
388 return dev_change_flags(dev, (unsigned int)new_flags, NULL);
391 static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t len)
394 return netdev_store(dev, attr, buf, len, change_flags);
396 NETDEVICE_SHOW_RW(flags, fmt_hex);
398 static ssize_t tx_queue_len_store(struct device *dev,
399 struct device_attribute *attr,
400 const char *buf, size_t len)
402 if (!capable(CAP_NET_ADMIN))
405 return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
407 NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
409 static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
411 WRITE_ONCE(dev->gro_flush_timeout, val);
415 static ssize_t gro_flush_timeout_store(struct device *dev,
416 struct device_attribute *attr,
417 const char *buf, size_t len)
419 if (!capable(CAP_NET_ADMIN))
422 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
424 NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
426 static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val)
428 WRITE_ONCE(dev->napi_defer_hard_irqs, val);
432 static ssize_t napi_defer_hard_irqs_store(struct device *dev,
433 struct device_attribute *attr,
434 const char *buf, size_t len)
436 if (!capable(CAP_NET_ADMIN))
439 return netdev_store(dev, attr, buf, len, change_napi_defer_hard_irqs);
441 NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_dec);
443 static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
444 const char *buf, size_t len)
446 struct net_device *netdev = to_net_dev(dev);
447 struct net *net = dev_net(netdev);
451 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
454 /* ignore trailing newline */
455 if (len > 0 && buf[len - 1] == '\n')
459 return restart_syscall();
461 if (dev_isalive(netdev)) {
462 ret = dev_set_alias(netdev, buf, count);
466 netdev_state_change(netdev);
474 static ssize_t ifalias_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 const struct net_device *netdev = to_net_dev(dev);
481 ret = dev_get_alias(netdev, tmp, sizeof(tmp));
483 ret = sysfs_emit(buf, "%s\n", tmp);
486 static DEVICE_ATTR_RW(ifalias);
488 static int change_group(struct net_device *dev, unsigned long new_group)
490 dev_set_group(dev, (int)new_group);
494 static ssize_t group_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t len)
497 return netdev_store(dev, attr, buf, len, change_group);
499 NETDEVICE_SHOW(group, fmt_dec);
500 static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
502 static int change_proto_down(struct net_device *dev, unsigned long proto_down)
504 return dev_change_proto_down(dev, (bool)proto_down);
507 static ssize_t proto_down_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t len)
511 return netdev_store(dev, attr, buf, len, change_proto_down);
513 NETDEVICE_SHOW_RW(proto_down, fmt_dec);
515 static ssize_t phys_port_id_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct net_device *netdev = to_net_dev(dev);
519 ssize_t ret = -EINVAL;
521 /* The check is also done in dev_get_phys_port_id; this helps returning
522 * early without hitting the trylock/restart below.
524 if (!netdev->netdev_ops->ndo_get_phys_port_id)
528 return restart_syscall();
530 if (dev_isalive(netdev)) {
531 struct netdev_phys_item_id ppid;
533 ret = dev_get_phys_port_id(netdev, &ppid);
535 ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
541 static DEVICE_ATTR_RO(phys_port_id);
543 static ssize_t phys_port_name_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct net_device *netdev = to_net_dev(dev);
547 ssize_t ret = -EINVAL;
549 /* The checks are also done in dev_get_phys_port_name; this helps
550 * returning early without hitting the trylock/restart below.
552 if (!netdev->netdev_ops->ndo_get_phys_port_name &&
553 !netdev->devlink_port)
557 return restart_syscall();
559 if (dev_isalive(netdev)) {
562 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
564 ret = sysfs_emit(buf, "%s\n", name);
570 static DEVICE_ATTR_RO(phys_port_name);
572 static ssize_t phys_switch_id_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
575 struct net_device *netdev = to_net_dev(dev);
576 ssize_t ret = -EINVAL;
578 /* The checks are also done in dev_get_phys_port_name; this helps
579 * returning early without hitting the trylock/restart below. This works
580 * because recurse is false when calling dev_get_port_parent_id.
582 if (!netdev->netdev_ops->ndo_get_port_parent_id &&
583 !netdev->devlink_port)
587 return restart_syscall();
589 if (dev_isalive(netdev)) {
590 struct netdev_phys_item_id ppid = { };
592 ret = dev_get_port_parent_id(netdev, &ppid, false);
594 ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
600 static DEVICE_ATTR_RO(phys_switch_id);
602 static ssize_t threaded_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
605 struct net_device *netdev = to_net_dev(dev);
606 ssize_t ret = -EINVAL;
610 if (dev_isalive(netdev))
611 ret = sysfs_emit(buf, fmt_dec, READ_ONCE(netdev->threaded));
618 static int modify_napi_threaded(struct net_device *dev, unsigned long val)
622 if (list_empty(&dev->napi_list))
625 if (val != 0 && val != 1)
628 ret = dev_set_threaded(dev, val);
633 static ssize_t threaded_store(struct device *dev,
634 struct device_attribute *attr,
635 const char *buf, size_t len)
637 return netdev_store(dev, attr, buf, len, modify_napi_threaded);
639 static DEVICE_ATTR_RW(threaded);
641 static struct attribute *net_class_attrs[] __ro_after_init = {
642 &dev_attr_netdev_group.attr,
644 &dev_attr_dev_id.attr,
645 &dev_attr_dev_port.attr,
646 &dev_attr_iflink.attr,
647 &dev_attr_ifindex.attr,
648 &dev_attr_name_assign_type.attr,
649 &dev_attr_addr_assign_type.attr,
650 &dev_attr_addr_len.attr,
651 &dev_attr_link_mode.attr,
652 &dev_attr_address.attr,
653 &dev_attr_broadcast.attr,
654 &dev_attr_speed.attr,
655 &dev_attr_duplex.attr,
656 &dev_attr_dormant.attr,
657 &dev_attr_testing.attr,
658 &dev_attr_operstate.attr,
659 &dev_attr_carrier_changes.attr,
660 &dev_attr_ifalias.attr,
661 &dev_attr_carrier.attr,
663 &dev_attr_flags.attr,
664 &dev_attr_tx_queue_len.attr,
665 &dev_attr_gro_flush_timeout.attr,
666 &dev_attr_napi_defer_hard_irqs.attr,
667 &dev_attr_phys_port_id.attr,
668 &dev_attr_phys_port_name.attr,
669 &dev_attr_phys_switch_id.attr,
670 &dev_attr_proto_down.attr,
671 &dev_attr_carrier_up_count.attr,
672 &dev_attr_carrier_down_count.attr,
673 &dev_attr_threaded.attr,
676 ATTRIBUTE_GROUPS(net_class);
678 /* Show a given an attribute in the statistics group */
679 static ssize_t netstat_show(const struct device *d,
680 struct device_attribute *attr, char *buf,
681 unsigned long offset)
683 struct net_device *dev = to_net_dev(d);
684 ssize_t ret = -EINVAL;
686 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
687 offset % sizeof(u64) != 0);
690 if (dev_isalive(dev)) {
691 struct rtnl_link_stats64 temp;
692 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
694 ret = sysfs_emit(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
700 /* generate a read-only statistics attribute */
701 #define NETSTAT_ENTRY(name) \
702 static ssize_t name##_show(struct device *d, \
703 struct device_attribute *attr, char *buf) \
705 return netstat_show(d, attr, buf, \
706 offsetof(struct rtnl_link_stats64, name)); \
708 static DEVICE_ATTR_RO(name)
710 NETSTAT_ENTRY(rx_packets);
711 NETSTAT_ENTRY(tx_packets);
712 NETSTAT_ENTRY(rx_bytes);
713 NETSTAT_ENTRY(tx_bytes);
714 NETSTAT_ENTRY(rx_errors);
715 NETSTAT_ENTRY(tx_errors);
716 NETSTAT_ENTRY(rx_dropped);
717 NETSTAT_ENTRY(tx_dropped);
718 NETSTAT_ENTRY(multicast);
719 NETSTAT_ENTRY(collisions);
720 NETSTAT_ENTRY(rx_length_errors);
721 NETSTAT_ENTRY(rx_over_errors);
722 NETSTAT_ENTRY(rx_crc_errors);
723 NETSTAT_ENTRY(rx_frame_errors);
724 NETSTAT_ENTRY(rx_fifo_errors);
725 NETSTAT_ENTRY(rx_missed_errors);
726 NETSTAT_ENTRY(tx_aborted_errors);
727 NETSTAT_ENTRY(tx_carrier_errors);
728 NETSTAT_ENTRY(tx_fifo_errors);
729 NETSTAT_ENTRY(tx_heartbeat_errors);
730 NETSTAT_ENTRY(tx_window_errors);
731 NETSTAT_ENTRY(rx_compressed);
732 NETSTAT_ENTRY(tx_compressed);
733 NETSTAT_ENTRY(rx_nohandler);
735 static struct attribute *netstat_attrs[] __ro_after_init = {
736 &dev_attr_rx_packets.attr,
737 &dev_attr_tx_packets.attr,
738 &dev_attr_rx_bytes.attr,
739 &dev_attr_tx_bytes.attr,
740 &dev_attr_rx_errors.attr,
741 &dev_attr_tx_errors.attr,
742 &dev_attr_rx_dropped.attr,
743 &dev_attr_tx_dropped.attr,
744 &dev_attr_multicast.attr,
745 &dev_attr_collisions.attr,
746 &dev_attr_rx_length_errors.attr,
747 &dev_attr_rx_over_errors.attr,
748 &dev_attr_rx_crc_errors.attr,
749 &dev_attr_rx_frame_errors.attr,
750 &dev_attr_rx_fifo_errors.attr,
751 &dev_attr_rx_missed_errors.attr,
752 &dev_attr_tx_aborted_errors.attr,
753 &dev_attr_tx_carrier_errors.attr,
754 &dev_attr_tx_fifo_errors.attr,
755 &dev_attr_tx_heartbeat_errors.attr,
756 &dev_attr_tx_window_errors.attr,
757 &dev_attr_rx_compressed.attr,
758 &dev_attr_tx_compressed.attr,
759 &dev_attr_rx_nohandler.attr,
763 static const struct attribute_group netstat_group = {
764 .name = "statistics",
765 .attrs = netstat_attrs,
768 static struct attribute *wireless_attrs[] = {
772 static const struct attribute_group wireless_group = {
774 .attrs = wireless_attrs,
777 static bool wireless_group_needed(struct net_device *ndev)
779 #if IS_ENABLED(CONFIG_CFG80211)
780 if (ndev->ieee80211_ptr)
783 #if IS_ENABLED(CONFIG_WIRELESS_EXT)
784 if (ndev->wireless_handlers)
790 #else /* CONFIG_SYSFS */
791 #define net_class_groups NULL
792 #endif /* CONFIG_SYSFS */
795 #define to_rx_queue_attr(_attr) \
796 container_of(_attr, struct rx_queue_attribute, attr)
798 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
800 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
803 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
804 struct netdev_rx_queue *queue = to_rx_queue(kobj);
806 if (!attribute->show)
809 return attribute->show(queue, buf);
812 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
813 const char *buf, size_t count)
815 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
816 struct netdev_rx_queue *queue = to_rx_queue(kobj);
818 if (!attribute->store)
821 return attribute->store(queue, buf, count);
824 static const struct sysfs_ops rx_queue_sysfs_ops = {
825 .show = rx_queue_attr_show,
826 .store = rx_queue_attr_store,
830 static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
836 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
840 map = rcu_dereference(queue->rps_map);
842 for (i = 0; i < map->len; i++)
843 cpumask_set_cpu(map->cpus[i], mask);
845 len = sysfs_emit(buf, "%*pb\n", cpumask_pr_args(mask));
847 free_cpumask_var(mask);
849 return len < PAGE_SIZE ? len : -EINVAL;
852 static int netdev_rx_queue_set_rps_mask(struct netdev_rx_queue *queue,
855 static DEFINE_MUTEX(rps_map_mutex);
856 struct rps_map *old_map, *map;
859 map = kzalloc(max_t(unsigned int,
860 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
866 for_each_cpu_and(cpu, mask, cpu_online_mask)
867 map->cpus[i++] = cpu;
876 mutex_lock(&rps_map_mutex);
877 old_map = rcu_dereference_protected(queue->rps_map,
878 mutex_is_locked(&rps_map_mutex));
879 rcu_assign_pointer(queue->rps_map, map);
882 static_branch_inc(&rps_needed);
884 static_branch_dec(&rps_needed);
886 mutex_unlock(&rps_map_mutex);
889 kfree_rcu(old_map, rcu);
893 int rps_cpumask_housekeeping(struct cpumask *mask)
895 if (!cpumask_empty(mask)) {
896 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN));
897 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ));
898 if (cpumask_empty(mask))
904 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
905 const char *buf, size_t len)
910 if (!capable(CAP_NET_ADMIN))
913 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
916 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
920 err = rps_cpumask_housekeeping(mask);
924 err = netdev_rx_queue_set_rps_mask(queue, mask);
927 free_cpumask_var(mask);
931 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
934 struct rps_dev_flow_table *flow_table;
935 unsigned long val = 0;
938 flow_table = rcu_dereference(queue->rps_flow_table);
940 val = (unsigned long)flow_table->mask + 1;
943 return sysfs_emit(buf, "%lu\n", val);
946 static void rps_dev_flow_table_release(struct rcu_head *rcu)
948 struct rps_dev_flow_table *table = container_of(rcu,
949 struct rps_dev_flow_table, rcu);
953 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
954 const char *buf, size_t len)
956 unsigned long mask, count;
957 struct rps_dev_flow_table *table, *old_table;
958 static DEFINE_SPINLOCK(rps_dev_flow_lock);
961 if (!capable(CAP_NET_ADMIN))
964 rc = kstrtoul(buf, 0, &count);
970 /* mask = roundup_pow_of_two(count) - 1;
971 * without overflows...
973 while ((mask | (mask >> 1)) != mask)
975 /* On 64 bit arches, must check mask fits in table->mask (u32),
976 * and on 32bit arches, must check
977 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
979 #if BITS_PER_LONG > 32
980 if (mask > (unsigned long)(u32)mask)
983 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
984 / sizeof(struct rps_dev_flow)) {
985 /* Enforce a limit to prevent overflow */
989 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
994 for (count = 0; count <= mask; count++)
995 table->flows[count].cpu = RPS_NO_CPU;
1000 spin_lock(&rps_dev_flow_lock);
1001 old_table = rcu_dereference_protected(queue->rps_flow_table,
1002 lockdep_is_held(&rps_dev_flow_lock));
1003 rcu_assign_pointer(queue->rps_flow_table, table);
1004 spin_unlock(&rps_dev_flow_lock);
1007 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
1012 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
1013 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
1015 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
1016 = __ATTR(rps_flow_cnt, 0644,
1017 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
1018 #endif /* CONFIG_RPS */
1020 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
1022 &rps_cpus_attribute.attr,
1023 &rps_dev_flow_table_cnt_attribute.attr,
1027 ATTRIBUTE_GROUPS(rx_queue_default);
1029 static void rx_queue_release(struct kobject *kobj)
1031 struct netdev_rx_queue *queue = to_rx_queue(kobj);
1033 struct rps_map *map;
1034 struct rps_dev_flow_table *flow_table;
1036 map = rcu_dereference_protected(queue->rps_map, 1);
1038 RCU_INIT_POINTER(queue->rps_map, NULL);
1039 kfree_rcu(map, rcu);
1042 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
1044 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
1045 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
1049 memset(kobj, 0, sizeof(*kobj));
1050 netdev_put(queue->dev, &queue->dev_tracker);
1053 static const void *rx_queue_namespace(const struct kobject *kobj)
1055 struct netdev_rx_queue *queue = to_rx_queue(kobj);
1056 struct device *dev = &queue->dev->dev;
1057 const void *ns = NULL;
1059 if (dev->class && dev->class->ns_type)
1060 ns = dev->class->namespace(dev);
1065 static void rx_queue_get_ownership(const struct kobject *kobj,
1066 kuid_t *uid, kgid_t *gid)
1068 const struct net *net = rx_queue_namespace(kobj);
1070 net_ns_get_ownership(net, uid, gid);
1073 static const struct kobj_type rx_queue_ktype = {
1074 .sysfs_ops = &rx_queue_sysfs_ops,
1075 .release = rx_queue_release,
1076 .default_groups = rx_queue_default_groups,
1077 .namespace = rx_queue_namespace,
1078 .get_ownership = rx_queue_get_ownership,
1081 static int rx_queue_default_mask(struct net_device *dev,
1082 struct netdev_rx_queue *queue)
1084 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL)
1085 struct cpumask *rps_default_mask = READ_ONCE(dev_net(dev)->core.rps_default_mask);
1087 if (rps_default_mask && !cpumask_empty(rps_default_mask))
1088 return netdev_rx_queue_set_rps_mask(queue, rps_default_mask);
1093 static int rx_queue_add_kobject(struct net_device *dev, int index)
1095 struct netdev_rx_queue *queue = dev->_rx + index;
1096 struct kobject *kobj = &queue->kobj;
1099 /* Kobject_put later will trigger rx_queue_release call which
1100 * decreases dev refcount: Take that reference here
1102 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1104 kobj->kset = dev->queues_kset;
1105 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1110 if (dev->sysfs_rx_queue_group) {
1111 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1116 error = rx_queue_default_mask(dev, queue);
1120 kobject_uevent(kobj, KOBJ_ADD);
1129 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1132 struct netdev_rx_queue *queue = dev->_rx + index;
1133 struct kobject *kobj = &queue->kobj;
1136 error = sysfs_change_owner(kobj, kuid, kgid);
1140 if (dev->sysfs_rx_queue_group)
1141 error = sysfs_group_change_owner(
1142 kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1146 #endif /* CONFIG_SYSFS */
1149 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1156 if (!dev->sysfs_rx_queue_group)
1159 for (i = old_num; i < new_num; i++) {
1160 error = rx_queue_add_kobject(dev, i);
1167 while (--i >= new_num) {
1168 struct kobject *kobj = &dev->_rx[i].kobj;
1170 if (!refcount_read(&dev_net(dev)->ns.count))
1171 kobj->uevent_suppress = 1;
1172 if (dev->sysfs_rx_queue_group)
1173 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1183 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1184 kuid_t kuid, kgid_t kgid)
1191 if (!dev->sysfs_rx_queue_group)
1194 for (i = 0; i < num; i++) {
1195 error = rx_queue_change_owner(dev, i, kuid, kgid);
1208 * netdev_queue sysfs structures and functions.
1210 struct netdev_queue_attribute {
1211 struct attribute attr;
1212 ssize_t (*show)(struct netdev_queue *queue, char *buf);
1213 ssize_t (*store)(struct netdev_queue *queue,
1214 const char *buf, size_t len);
1216 #define to_netdev_queue_attr(_attr) \
1217 container_of(_attr, struct netdev_queue_attribute, attr)
1219 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1221 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1222 struct attribute *attr, char *buf)
1224 const struct netdev_queue_attribute *attribute
1225 = to_netdev_queue_attr(attr);
1226 struct netdev_queue *queue = to_netdev_queue(kobj);
1228 if (!attribute->show)
1231 return attribute->show(queue, buf);
1234 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1235 struct attribute *attr,
1236 const char *buf, size_t count)
1238 const struct netdev_queue_attribute *attribute
1239 = to_netdev_queue_attr(attr);
1240 struct netdev_queue *queue = to_netdev_queue(kobj);
1242 if (!attribute->store)
1245 return attribute->store(queue, buf, count);
1248 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1249 .show = netdev_queue_attr_show,
1250 .store = netdev_queue_attr_store,
1253 static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
1255 unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
1257 return sysfs_emit(buf, fmt_ulong, trans_timeout);
1260 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1262 struct net_device *dev = queue->dev;
1265 i = queue - dev->_tx;
1266 BUG_ON(i >= dev->num_tx_queues);
1271 static ssize_t traffic_class_show(struct netdev_queue *queue,
1274 struct net_device *dev = queue->dev;
1278 if (!netif_is_multiqueue(dev))
1281 if (!rtnl_trylock())
1282 return restart_syscall();
1284 index = get_netdev_queue_index(queue);
1286 /* If queue belongs to subordinate dev use its TC mapping */
1287 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1289 num_tc = dev->num_tc;
1290 tc = netdev_txq_to_tc(dev, index);
1297 /* We can report the traffic class one of two ways:
1298 * Subordinate device traffic classes are reported with the traffic
1299 * class first, and then the subordinate class so for example TC0 on
1300 * subordinate device 2 will be reported as "0-2". If the queue
1301 * belongs to the root device it will be reported with just the
1302 * traffic class, so just "0" for TC 0 for example.
1304 return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
1305 sysfs_emit(buf, "%d\n", tc);
1309 static ssize_t tx_maxrate_show(struct netdev_queue *queue,
1312 return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
1315 static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1316 const char *buf, size_t len)
1318 struct net_device *dev = queue->dev;
1319 int err, index = get_netdev_queue_index(queue);
1322 if (!capable(CAP_NET_ADMIN))
1325 /* The check is also done later; this helps returning early without
1326 * hitting the trylock/restart below.
1328 if (!dev->netdev_ops->ndo_set_tx_maxrate)
1331 err = kstrtou32(buf, 10, &rate);
1335 if (!rtnl_trylock())
1336 return restart_syscall();
1339 if (dev->netdev_ops->ndo_set_tx_maxrate)
1340 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1344 queue->tx_maxrate = rate;
1350 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1351 = __ATTR_RW(tx_maxrate);
1354 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1355 = __ATTR_RO(tx_timeout);
1357 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1358 = __ATTR_RO(traffic_class);
1362 * Byte queue limits sysfs structures and functions.
1364 static ssize_t bql_show(char *buf, unsigned int value)
1366 return sysfs_emit(buf, "%u\n", value);
1369 static ssize_t bql_set(const char *buf, const size_t count,
1370 unsigned int *pvalue)
1375 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1376 value = DQL_MAX_LIMIT;
1378 err = kstrtouint(buf, 10, &value);
1381 if (value > DQL_MAX_LIMIT)
1390 static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1393 struct dql *dql = &queue->dql;
1395 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1398 static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1399 const char *buf, size_t len)
1401 struct dql *dql = &queue->dql;
1405 err = kstrtouint(buf, 10, &value);
1409 dql->slack_hold_time = msecs_to_jiffies(value);
1414 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1415 = __ATTR(hold_time, 0644,
1416 bql_show_hold_time, bql_set_hold_time);
1418 static ssize_t bql_show_stall_thrs(struct netdev_queue *queue, char *buf)
1420 struct dql *dql = &queue->dql;
1422 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->stall_thrs));
1425 static ssize_t bql_set_stall_thrs(struct netdev_queue *queue,
1426 const char *buf, size_t len)
1428 struct dql *dql = &queue->dql;
1432 err = kstrtouint(buf, 10, &value);
1436 value = msecs_to_jiffies(value);
1437 if (value && (value < 4 || value > 4 / 2 * BITS_PER_LONG))
1440 if (!dql->stall_thrs && value)
1441 dql->last_reap = jiffies;
1442 /* Force last_reap to be live */
1444 dql->stall_thrs = value;
1449 static struct netdev_queue_attribute bql_stall_thrs_attribute __ro_after_init =
1450 __ATTR(stall_thrs, 0644, bql_show_stall_thrs, bql_set_stall_thrs);
1452 static ssize_t bql_show_stall_max(struct netdev_queue *queue, char *buf)
1454 return sysfs_emit(buf, "%u\n", READ_ONCE(queue->dql.stall_max));
1457 static ssize_t bql_set_stall_max(struct netdev_queue *queue,
1458 const char *buf, size_t len)
1460 WRITE_ONCE(queue->dql.stall_max, 0);
1464 static struct netdev_queue_attribute bql_stall_max_attribute __ro_after_init =
1465 __ATTR(stall_max, 0644, bql_show_stall_max, bql_set_stall_max);
1467 static ssize_t bql_show_stall_cnt(struct netdev_queue *queue, char *buf)
1469 struct dql *dql = &queue->dql;
1471 return sysfs_emit(buf, "%lu\n", dql->stall_cnt);
1474 static struct netdev_queue_attribute bql_stall_cnt_attribute __ro_after_init =
1475 __ATTR(stall_cnt, 0444, bql_show_stall_cnt, NULL);
1477 static ssize_t bql_show_inflight(struct netdev_queue *queue,
1480 struct dql *dql = &queue->dql;
1482 return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
1485 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1486 __ATTR(inflight, 0444, bql_show_inflight, NULL);
1488 #define BQL_ATTR(NAME, FIELD) \
1489 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1492 return bql_show(buf, queue->dql.FIELD); \
1495 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1496 const char *buf, size_t len) \
1498 return bql_set(buf, len, &queue->dql.FIELD); \
1501 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1502 = __ATTR(NAME, 0644, \
1503 bql_show_ ## NAME, bql_set_ ## NAME)
1505 BQL_ATTR(limit, limit);
1506 BQL_ATTR(limit_max, max_limit);
1507 BQL_ATTR(limit_min, min_limit);
1509 static struct attribute *dql_attrs[] __ro_after_init = {
1510 &bql_limit_attribute.attr,
1511 &bql_limit_max_attribute.attr,
1512 &bql_limit_min_attribute.attr,
1513 &bql_hold_time_attribute.attr,
1514 &bql_inflight_attribute.attr,
1515 &bql_stall_thrs_attribute.attr,
1516 &bql_stall_cnt_attribute.attr,
1517 &bql_stall_max_attribute.attr,
1521 static const struct attribute_group dql_group = {
1522 .name = "byte_queue_limits",
1526 /* Fake declaration, all the code using it should be dead */
1527 extern const struct attribute_group dql_group;
1528 #endif /* CONFIG_BQL */
1531 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
1532 int tc, char *buf, enum xps_map_type type)
1534 struct xps_dev_maps *dev_maps;
1535 unsigned long *mask;
1536 unsigned int nr_ids;
1540 dev_maps = rcu_dereference(dev->xps_maps[type]);
1542 /* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
1543 * when dev_maps hasn't been allocated yet, to be backward compatible.
1545 nr_ids = dev_maps ? dev_maps->nr_ids :
1546 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
1548 mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
1554 if (!dev_maps || tc >= dev_maps->num_tc)
1557 for (j = 0; j < nr_ids; j++) {
1558 int i, tci = j * dev_maps->num_tc + tc;
1559 struct xps_map *map;
1561 map = rcu_dereference(dev_maps->attr_map[tci]);
1565 for (i = map->len; i--;) {
1566 if (map->queues[i] == index) {
1575 len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids);
1578 return len < PAGE_SIZE ? len : -EINVAL;
1581 static ssize_t xps_cpus_show(struct netdev_queue *queue, char *buf)
1583 struct net_device *dev = queue->dev;
1587 if (!netif_is_multiqueue(dev))
1590 index = get_netdev_queue_index(queue);
1592 if (!rtnl_trylock())
1593 return restart_syscall();
1595 /* If queue belongs to subordinate dev use its map */
1596 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1598 tc = netdev_txq_to_tc(dev, index);
1604 /* Make sure the subordinate device can't be freed */
1605 get_device(&dev->dev);
1608 len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
1610 put_device(&dev->dev);
1614 static ssize_t xps_cpus_store(struct netdev_queue *queue,
1615 const char *buf, size_t len)
1617 struct net_device *dev = queue->dev;
1622 if (!netif_is_multiqueue(dev))
1625 if (!capable(CAP_NET_ADMIN))
1628 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1631 index = get_netdev_queue_index(queue);
1633 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1635 free_cpumask_var(mask);
1639 if (!rtnl_trylock()) {
1640 free_cpumask_var(mask);
1641 return restart_syscall();
1644 err = netif_set_xps_queue(dev, mask, index);
1647 free_cpumask_var(mask);
1652 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1653 = __ATTR_RW(xps_cpus);
1655 static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1657 struct net_device *dev = queue->dev;
1661 index = get_netdev_queue_index(queue);
1663 if (!rtnl_trylock())
1664 return restart_syscall();
1666 tc = netdev_txq_to_tc(dev, index);
1671 return xps_queue_show(dev, index, tc, buf, XPS_RXQS);
1674 static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1677 struct net_device *dev = queue->dev;
1678 struct net *net = dev_net(dev);
1679 unsigned long *mask;
1683 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1686 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1690 index = get_netdev_queue_index(queue);
1692 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1698 if (!rtnl_trylock()) {
1700 return restart_syscall();
1704 err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
1713 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1714 = __ATTR_RW(xps_rxqs);
1715 #endif /* CONFIG_XPS */
1717 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1718 &queue_trans_timeout.attr,
1719 &queue_traffic_class.attr,
1721 &xps_cpus_attribute.attr,
1722 &xps_rxqs_attribute.attr,
1723 &queue_tx_maxrate.attr,
1727 ATTRIBUTE_GROUPS(netdev_queue_default);
1729 static void netdev_queue_release(struct kobject *kobj)
1731 struct netdev_queue *queue = to_netdev_queue(kobj);
1733 memset(kobj, 0, sizeof(*kobj));
1734 netdev_put(queue->dev, &queue->dev_tracker);
1737 static const void *netdev_queue_namespace(const struct kobject *kobj)
1739 struct netdev_queue *queue = to_netdev_queue(kobj);
1740 struct device *dev = &queue->dev->dev;
1741 const void *ns = NULL;
1743 if (dev->class && dev->class->ns_type)
1744 ns = dev->class->namespace(dev);
1749 static void netdev_queue_get_ownership(const struct kobject *kobj,
1750 kuid_t *uid, kgid_t *gid)
1752 const struct net *net = netdev_queue_namespace(kobj);
1754 net_ns_get_ownership(net, uid, gid);
1757 static const struct kobj_type netdev_queue_ktype = {
1758 .sysfs_ops = &netdev_queue_sysfs_ops,
1759 .release = netdev_queue_release,
1760 .default_groups = netdev_queue_default_groups,
1761 .namespace = netdev_queue_namespace,
1762 .get_ownership = netdev_queue_get_ownership,
1765 static bool netdev_uses_bql(const struct net_device *dev)
1767 if (dev->features & NETIF_F_LLTX ||
1768 dev->priv_flags & IFF_NO_QUEUE)
1771 return IS_ENABLED(CONFIG_BQL);
1774 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1776 struct netdev_queue *queue = dev->_tx + index;
1777 struct kobject *kobj = &queue->kobj;
1780 /* Kobject_put later will trigger netdev_queue_release call
1781 * which decreases dev refcount: Take that reference here
1783 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1785 kobj->kset = dev->queues_kset;
1786 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1791 if (netdev_uses_bql(dev)) {
1792 error = sysfs_create_group(kobj, &dql_group);
1797 kobject_uevent(kobj, KOBJ_ADD);
1805 static int tx_queue_change_owner(struct net_device *ndev, int index,
1806 kuid_t kuid, kgid_t kgid)
1808 struct netdev_queue *queue = ndev->_tx + index;
1809 struct kobject *kobj = &queue->kobj;
1812 error = sysfs_change_owner(kobj, kuid, kgid);
1816 if (netdev_uses_bql(ndev))
1817 error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
1821 #endif /* CONFIG_SYSFS */
1824 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1830 /* Tx queue kobjects are allowed to be updated when a device is being
1831 * unregistered, but solely to remove queues from qdiscs. Any path
1832 * adding queues should be fixed.
1834 WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
1835 "New queues can't be registered after device unregistration.");
1837 for (i = old_num; i < new_num; i++) {
1838 error = netdev_queue_add_kobject(dev, i);
1845 while (--i >= new_num) {
1846 struct netdev_queue *queue = dev->_tx + i;
1848 if (!refcount_read(&dev_net(dev)->ns.count))
1849 queue->kobj.uevent_suppress = 1;
1851 if (netdev_uses_bql(dev))
1852 sysfs_remove_group(&queue->kobj, &dql_group);
1854 kobject_put(&queue->kobj);
1860 #endif /* CONFIG_SYSFS */
1863 static int net_tx_queue_change_owner(struct net_device *dev, int num,
1864 kuid_t kuid, kgid_t kgid)
1870 for (i = 0; i < num; i++) {
1871 error = tx_queue_change_owner(dev, i, kuid, kgid);
1879 #endif /* CONFIG_SYSFS */
1882 static int register_queue_kobjects(struct net_device *dev)
1884 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1887 dev->queues_kset = kset_create_and_add("queues",
1888 NULL, &dev->dev.kobj);
1889 if (!dev->queues_kset)
1891 real_rx = dev->real_num_rx_queues;
1893 real_tx = dev->real_num_tx_queues;
1895 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
1900 error = netdev_queue_update_kobjects(dev, 0, real_tx);
1908 netdev_queue_update_kobjects(dev, txq, 0);
1909 net_rx_queue_update_kobjects(dev, rxq, 0);
1911 kset_unregister(dev->queues_kset);
1916 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
1918 int error = 0, real_rx = 0, real_tx = 0;
1921 if (ndev->queues_kset) {
1922 error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
1926 real_rx = ndev->real_num_rx_queues;
1928 real_tx = ndev->real_num_tx_queues;
1930 error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
1934 error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
1941 static void remove_queue_kobjects(struct net_device *dev)
1943 int real_rx = 0, real_tx = 0;
1946 real_rx = dev->real_num_rx_queues;
1948 real_tx = dev->real_num_tx_queues;
1950 net_rx_queue_update_kobjects(dev, real_rx, 0);
1951 netdev_queue_update_kobjects(dev, real_tx, 0);
1953 dev->real_num_rx_queues = 0;
1954 dev->real_num_tx_queues = 0;
1956 kset_unregister(dev->queues_kset);
1960 static bool net_current_may_mount(void)
1962 struct net *net = current->nsproxy->net_ns;
1964 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1967 static void *net_grab_current_ns(void)
1969 struct net *ns = current->nsproxy->net_ns;
1970 #ifdef CONFIG_NET_NS
1972 refcount_inc(&ns->passive);
1977 static const void *net_initial_ns(void)
1982 static const void *net_netlink_ns(struct sock *sk)
1984 return sock_net(sk);
1987 const struct kobj_ns_type_operations net_ns_type_operations = {
1988 .type = KOBJ_NS_TYPE_NET,
1989 .current_may_mount = net_current_may_mount,
1990 .grab_current_ns = net_grab_current_ns,
1991 .netlink_ns = net_netlink_ns,
1992 .initial_ns = net_initial_ns,
1993 .drop_ns = net_drop_ns,
1995 EXPORT_SYMBOL_GPL(net_ns_type_operations);
1997 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env)
1999 const struct net_device *dev = to_net_dev(d);
2002 /* pass interface to uevent. */
2003 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
2007 /* pass ifindex to uevent.
2008 * ifindex is useful as it won't change (interface name may change)
2009 * and is what RtNetlink uses natively.
2011 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
2018 * netdev_release -- destroy and free a dead device.
2019 * Called when last reference to device kobject is gone.
2021 static void netdev_release(struct device *d)
2023 struct net_device *dev = to_net_dev(d);
2025 BUG_ON(dev->reg_state != NETREG_RELEASED);
2027 /* no need to wait for rcu grace period:
2028 * device is dead and about to be freed.
2030 kfree(rcu_access_pointer(dev->ifalias));
2031 netdev_freemem(dev);
2034 static const void *net_namespace(const struct device *d)
2036 const struct net_device *dev = to_net_dev(d);
2038 return dev_net(dev);
2041 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid)
2043 const struct net_device *dev = to_net_dev(d);
2044 const struct net *net = dev_net(dev);
2046 net_ns_get_ownership(net, uid, gid);
2049 static const struct class net_class = {
2051 .dev_release = netdev_release,
2052 .dev_groups = net_class_groups,
2053 .dev_uevent = netdev_uevent,
2054 .ns_type = &net_ns_type_operations,
2055 .namespace = net_namespace,
2056 .get_ownership = net_get_ownership,
2060 static int of_dev_node_match(struct device *dev, const void *data)
2062 for (; dev; dev = dev->parent) {
2063 if (dev->of_node == data)
2071 * of_find_net_device_by_node - lookup the net device for the device node
2072 * @np: OF device node
2074 * Looks up the net_device structure corresponding with the device node.
2075 * If successful, returns a pointer to the net_device with the embedded
2076 * struct device refcount incremented by one, or NULL on failure. The
2077 * refcount must be dropped when done with the net_device.
2079 struct net_device *of_find_net_device_by_node(struct device_node *np)
2083 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
2087 return to_net_dev(dev);
2089 EXPORT_SYMBOL(of_find_net_device_by_node);
2092 /* Delete sysfs entries but hold kobject reference until after all
2093 * netdev references are gone.
2095 void netdev_unregister_kobject(struct net_device *ndev)
2097 struct device *dev = &ndev->dev;
2099 if (!refcount_read(&dev_net(ndev)->ns.count))
2100 dev_set_uevent_suppress(dev, 1);
2102 kobject_get(&dev->kobj);
2104 remove_queue_kobjects(ndev);
2106 pm_runtime_set_memalloc_noio(dev, false);
2111 /* Create sysfs entries for network device. */
2112 int netdev_register_kobject(struct net_device *ndev)
2114 struct device *dev = &ndev->dev;
2115 const struct attribute_group **groups = ndev->sysfs_groups;
2118 device_initialize(dev);
2119 dev->class = &net_class;
2120 dev->platform_data = ndev;
2121 dev->groups = groups;
2123 dev_set_name(dev, "%s", ndev->name);
2126 /* Allow for a device specific group */
2130 *groups++ = &netstat_group;
2132 if (wireless_group_needed(ndev))
2133 *groups++ = &wireless_group;
2134 #endif /* CONFIG_SYSFS */
2136 error = device_add(dev);
2140 error = register_queue_kobjects(ndev);
2146 pm_runtime_set_memalloc_noio(dev, true);
2151 /* Change owner for sysfs entries when moving network devices across network
2152 * namespaces owned by different user namespaces.
2154 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2155 const struct net *net_new)
2157 kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2158 kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2159 struct device *dev = &ndev->dev;
2162 net_ns_get_ownership(net_old, &old_uid, &old_gid);
2163 net_ns_get_ownership(net_new, &new_uid, &new_gid);
2165 /* The network namespace was changed but the owning user namespace is
2166 * identical so there's no need to change the owner of sysfs entries.
2168 if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2171 error = device_change_owner(dev, new_uid, new_gid);
2175 error = queue_change_owner(ndev, new_uid, new_gid);
2182 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2185 return class_create_file_ns(&net_class, class_attr, ns);
2187 EXPORT_SYMBOL(netdev_class_create_file_ns);
2189 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2192 class_remove_file_ns(&net_class, class_attr, ns);
2194 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2196 int __init netdev_kobject_init(void)
2198 kobj_ns_type_register(&net_ns_type_operations);
2199 return class_register(&net_class);