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>
25 #include "net-sysfs.h"
28 static const char fmt_hex
[] = "%#x\n";
29 static const char fmt_long_hex
[] = "%#lx\n";
30 static const char fmt_dec
[] = "%d\n";
31 static const char fmt_udec
[] = "%u\n";
32 static const char fmt_ulong
[] = "%lu\n";
33 static const char fmt_u64
[] = "%llu\n";
35 static inline int dev_isalive(const struct net_device
*dev
)
37 return dev
->reg_state
<= NETREG_REGISTERED
;
40 /* use same locking rules as GIF* ioctl's */
41 static ssize_t
netdev_show(const struct device
*dev
,
42 struct device_attribute
*attr
, char *buf
,
43 ssize_t (*format
)(const struct net_device
*, char *))
45 struct net_device
*net
= to_net_dev(dev
);
46 ssize_t ret
= -EINVAL
;
48 read_lock(&dev_base_lock
);
50 ret
= (*format
)(net
, buf
);
51 read_unlock(&dev_base_lock
);
56 /* generate a show function for simple field */
57 #define NETDEVICE_SHOW(field, format_string) \
58 static ssize_t format_##field(const struct net_device *net, char *buf) \
60 return sprintf(buf, format_string, net->field); \
62 static ssize_t show_##field(struct device *dev, \
63 struct device_attribute *attr, char *buf) \
65 return netdev_show(dev, attr, buf, format_##field); \
69 /* use same locking and permission rules as SIF* ioctl's */
70 static ssize_t
netdev_store(struct device
*dev
, struct device_attribute
*attr
,
71 const char *buf
, size_t len
,
72 int (*set
)(struct net_device
*, unsigned long))
74 struct net_device
*netdev
= to_net_dev(dev
);
75 struct net
*net
= dev_net(netdev
);
79 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
82 ret
= kstrtoul(buf
, 0, &new);
87 return restart_syscall();
89 if (dev_isalive(netdev
)) {
90 if ((ret
= (*set
)(netdev
, new)) == 0)
98 NETDEVICE_SHOW(dev_id
, fmt_hex
);
99 NETDEVICE_SHOW(addr_assign_type
, fmt_dec
);
100 NETDEVICE_SHOW(addr_len
, fmt_dec
);
101 NETDEVICE_SHOW(iflink
, fmt_dec
);
102 NETDEVICE_SHOW(ifindex
, fmt_dec
);
103 NETDEVICE_SHOW(type
, fmt_dec
);
104 NETDEVICE_SHOW(link_mode
, fmt_dec
);
106 /* use same locking rules as GIFHWADDR ioctl's */
107 static ssize_t
show_address(struct device
*dev
, struct device_attribute
*attr
,
110 struct net_device
*net
= to_net_dev(dev
);
111 ssize_t ret
= -EINVAL
;
113 read_lock(&dev_base_lock
);
114 if (dev_isalive(net
))
115 ret
= sysfs_format_mac(buf
, net
->dev_addr
, net
->addr_len
);
116 read_unlock(&dev_base_lock
);
120 static ssize_t
show_broadcast(struct device
*dev
,
121 struct device_attribute
*attr
, char *buf
)
123 struct net_device
*net
= to_net_dev(dev
);
124 if (dev_isalive(net
))
125 return sysfs_format_mac(buf
, net
->broadcast
, net
->addr_len
);
129 static ssize_t
show_carrier(struct device
*dev
,
130 struct device_attribute
*attr
, char *buf
)
132 struct net_device
*netdev
= to_net_dev(dev
);
133 if (netif_running(netdev
)) {
134 return sprintf(buf
, fmt_dec
, !!netif_carrier_ok(netdev
));
139 static ssize_t
show_speed(struct device
*dev
,
140 struct device_attribute
*attr
, char *buf
)
142 struct net_device
*netdev
= to_net_dev(dev
);
146 return restart_syscall();
148 if (netif_running(netdev
)) {
149 struct ethtool_cmd cmd
;
150 if (!__ethtool_get_settings(netdev
, &cmd
))
151 ret
= sprintf(buf
, fmt_udec
, ethtool_cmd_speed(&cmd
));
157 static ssize_t
show_duplex(struct device
*dev
,
158 struct device_attribute
*attr
, char *buf
)
160 struct net_device
*netdev
= to_net_dev(dev
);
164 return restart_syscall();
166 if (netif_running(netdev
)) {
167 struct ethtool_cmd cmd
;
168 if (!__ethtool_get_settings(netdev
, &cmd
)) {
170 switch (cmd
.duplex
) {
181 ret
= sprintf(buf
, "%s\n", duplex
);
188 static ssize_t
show_dormant(struct device
*dev
,
189 struct device_attribute
*attr
, char *buf
)
191 struct net_device
*netdev
= to_net_dev(dev
);
193 if (netif_running(netdev
))
194 return sprintf(buf
, fmt_dec
, !!netif_dormant(netdev
));
199 static const char *const operstates
[] = {
201 "notpresent", /* currently unused */
204 "testing", /* currently unused */
209 static ssize_t
show_operstate(struct device
*dev
,
210 struct device_attribute
*attr
, char *buf
)
212 const struct net_device
*netdev
= to_net_dev(dev
);
213 unsigned char operstate
;
215 read_lock(&dev_base_lock
);
216 operstate
= netdev
->operstate
;
217 if (!netif_running(netdev
))
218 operstate
= IF_OPER_DOWN
;
219 read_unlock(&dev_base_lock
);
221 if (operstate
>= ARRAY_SIZE(operstates
))
222 return -EINVAL
; /* should not happen */
224 return sprintf(buf
, "%s\n", operstates
[operstate
]);
227 /* read-write attributes */
228 NETDEVICE_SHOW(mtu
, fmt_dec
);
230 static int change_mtu(struct net_device
*net
, unsigned long new_mtu
)
232 return dev_set_mtu(net
, (int) new_mtu
);
235 static ssize_t
store_mtu(struct device
*dev
, struct device_attribute
*attr
,
236 const char *buf
, size_t len
)
238 return netdev_store(dev
, attr
, buf
, len
, change_mtu
);
241 NETDEVICE_SHOW(flags
, fmt_hex
);
243 static int change_flags(struct net_device
*net
, unsigned long new_flags
)
245 return dev_change_flags(net
, (unsigned int) new_flags
);
248 static ssize_t
store_flags(struct device
*dev
, struct device_attribute
*attr
,
249 const char *buf
, size_t len
)
251 return netdev_store(dev
, attr
, buf
, len
, change_flags
);
254 NETDEVICE_SHOW(tx_queue_len
, fmt_ulong
);
256 static int change_tx_queue_len(struct net_device
*net
, unsigned long new_len
)
258 net
->tx_queue_len
= new_len
;
262 static ssize_t
store_tx_queue_len(struct device
*dev
,
263 struct device_attribute
*attr
,
264 const char *buf
, size_t len
)
266 if (!capable(CAP_NET_ADMIN
))
269 return netdev_store(dev
, attr
, buf
, len
, change_tx_queue_len
);
272 static ssize_t
store_ifalias(struct device
*dev
, struct device_attribute
*attr
,
273 const char *buf
, size_t len
)
275 struct net_device
*netdev
= to_net_dev(dev
);
276 struct net
*net
= dev_net(netdev
);
280 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
283 /* ignore trailing newline */
284 if (len
> 0 && buf
[len
- 1] == '\n')
288 return restart_syscall();
289 ret
= dev_set_alias(netdev
, buf
, count
);
292 return ret
< 0 ? ret
: len
;
295 static ssize_t
show_ifalias(struct device
*dev
,
296 struct device_attribute
*attr
, char *buf
)
298 const struct net_device
*netdev
= to_net_dev(dev
);
302 return restart_syscall();
304 ret
= sprintf(buf
, "%s\n", netdev
->ifalias
);
309 NETDEVICE_SHOW(group
, fmt_dec
);
311 static int change_group(struct net_device
*net
, unsigned long new_group
)
313 dev_set_group(net
, (int) new_group
);
317 static ssize_t
store_group(struct device
*dev
, struct device_attribute
*attr
,
318 const char *buf
, size_t len
)
320 return netdev_store(dev
, attr
, buf
, len
, change_group
);
323 static struct device_attribute net_class_attributes
[] = {
324 __ATTR(addr_assign_type
, S_IRUGO
, show_addr_assign_type
, NULL
),
325 __ATTR(addr_len
, S_IRUGO
, show_addr_len
, NULL
),
326 __ATTR(dev_id
, S_IRUGO
, show_dev_id
, NULL
),
327 __ATTR(ifalias
, S_IRUGO
| S_IWUSR
, show_ifalias
, store_ifalias
),
328 __ATTR(iflink
, S_IRUGO
, show_iflink
, NULL
),
329 __ATTR(ifindex
, S_IRUGO
, show_ifindex
, NULL
),
330 __ATTR(type
, S_IRUGO
, show_type
, NULL
),
331 __ATTR(link_mode
, S_IRUGO
, show_link_mode
, NULL
),
332 __ATTR(address
, S_IRUGO
, show_address
, NULL
),
333 __ATTR(broadcast
, S_IRUGO
, show_broadcast
, NULL
),
334 __ATTR(carrier
, S_IRUGO
, show_carrier
, NULL
),
335 __ATTR(speed
, S_IRUGO
, show_speed
, NULL
),
336 __ATTR(duplex
, S_IRUGO
, show_duplex
, NULL
),
337 __ATTR(dormant
, S_IRUGO
, show_dormant
, NULL
),
338 __ATTR(operstate
, S_IRUGO
, show_operstate
, NULL
),
339 __ATTR(mtu
, S_IRUGO
| S_IWUSR
, show_mtu
, store_mtu
),
340 __ATTR(flags
, S_IRUGO
| S_IWUSR
, show_flags
, store_flags
),
341 __ATTR(tx_queue_len
, S_IRUGO
| S_IWUSR
, show_tx_queue_len
,
343 __ATTR(netdev_group
, S_IRUGO
| S_IWUSR
, show_group
, store_group
),
347 /* Show a given an attribute in the statistics group */
348 static ssize_t
netstat_show(const struct device
*d
,
349 struct device_attribute
*attr
, char *buf
,
350 unsigned long offset
)
352 struct net_device
*dev
= to_net_dev(d
);
353 ssize_t ret
= -EINVAL
;
355 WARN_ON(offset
> sizeof(struct rtnl_link_stats64
) ||
356 offset
% sizeof(u64
) != 0);
358 read_lock(&dev_base_lock
);
359 if (dev_isalive(dev
)) {
360 struct rtnl_link_stats64 temp
;
361 const struct rtnl_link_stats64
*stats
= dev_get_stats(dev
, &temp
);
363 ret
= sprintf(buf
, fmt_u64
, *(u64
*)(((u8
*) stats
) + offset
));
365 read_unlock(&dev_base_lock
);
369 /* generate a read-only statistics attribute */
370 #define NETSTAT_ENTRY(name) \
371 static ssize_t show_##name(struct device *d, \
372 struct device_attribute *attr, char *buf) \
374 return netstat_show(d, attr, buf, \
375 offsetof(struct rtnl_link_stats64, name)); \
377 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
379 NETSTAT_ENTRY(rx_packets
);
380 NETSTAT_ENTRY(tx_packets
);
381 NETSTAT_ENTRY(rx_bytes
);
382 NETSTAT_ENTRY(tx_bytes
);
383 NETSTAT_ENTRY(rx_errors
);
384 NETSTAT_ENTRY(tx_errors
);
385 NETSTAT_ENTRY(rx_dropped
);
386 NETSTAT_ENTRY(tx_dropped
);
387 NETSTAT_ENTRY(multicast
);
388 NETSTAT_ENTRY(collisions
);
389 NETSTAT_ENTRY(rx_length_errors
);
390 NETSTAT_ENTRY(rx_over_errors
);
391 NETSTAT_ENTRY(rx_crc_errors
);
392 NETSTAT_ENTRY(rx_frame_errors
);
393 NETSTAT_ENTRY(rx_fifo_errors
);
394 NETSTAT_ENTRY(rx_missed_errors
);
395 NETSTAT_ENTRY(tx_aborted_errors
);
396 NETSTAT_ENTRY(tx_carrier_errors
);
397 NETSTAT_ENTRY(tx_fifo_errors
);
398 NETSTAT_ENTRY(tx_heartbeat_errors
);
399 NETSTAT_ENTRY(tx_window_errors
);
400 NETSTAT_ENTRY(rx_compressed
);
401 NETSTAT_ENTRY(tx_compressed
);
403 static struct attribute
*netstat_attrs
[] = {
404 &dev_attr_rx_packets
.attr
,
405 &dev_attr_tx_packets
.attr
,
406 &dev_attr_rx_bytes
.attr
,
407 &dev_attr_tx_bytes
.attr
,
408 &dev_attr_rx_errors
.attr
,
409 &dev_attr_tx_errors
.attr
,
410 &dev_attr_rx_dropped
.attr
,
411 &dev_attr_tx_dropped
.attr
,
412 &dev_attr_multicast
.attr
,
413 &dev_attr_collisions
.attr
,
414 &dev_attr_rx_length_errors
.attr
,
415 &dev_attr_rx_over_errors
.attr
,
416 &dev_attr_rx_crc_errors
.attr
,
417 &dev_attr_rx_frame_errors
.attr
,
418 &dev_attr_rx_fifo_errors
.attr
,
419 &dev_attr_rx_missed_errors
.attr
,
420 &dev_attr_tx_aborted_errors
.attr
,
421 &dev_attr_tx_carrier_errors
.attr
,
422 &dev_attr_tx_fifo_errors
.attr
,
423 &dev_attr_tx_heartbeat_errors
.attr
,
424 &dev_attr_tx_window_errors
.attr
,
425 &dev_attr_rx_compressed
.attr
,
426 &dev_attr_tx_compressed
.attr
,
431 static struct attribute_group netstat_group
= {
432 .name
= "statistics",
433 .attrs
= netstat_attrs
,
436 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
437 static struct attribute
*wireless_attrs
[] = {
441 static struct attribute_group wireless_group
= {
443 .attrs
= wireless_attrs
,
446 #endif /* CONFIG_SYSFS */
450 * RX queue sysfs structures and functions.
452 struct rx_queue_attribute
{
453 struct attribute attr
;
454 ssize_t (*show
)(struct netdev_rx_queue
*queue
,
455 struct rx_queue_attribute
*attr
, char *buf
);
456 ssize_t (*store
)(struct netdev_rx_queue
*queue
,
457 struct rx_queue_attribute
*attr
, const char *buf
, size_t len
);
459 #define to_rx_queue_attr(_attr) container_of(_attr, \
460 struct rx_queue_attribute, attr)
462 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
464 static ssize_t
rx_queue_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
467 struct rx_queue_attribute
*attribute
= to_rx_queue_attr(attr
);
468 struct netdev_rx_queue
*queue
= to_rx_queue(kobj
);
470 if (!attribute
->show
)
473 return attribute
->show(queue
, attribute
, buf
);
476 static ssize_t
rx_queue_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
477 const char *buf
, size_t count
)
479 struct rx_queue_attribute
*attribute
= to_rx_queue_attr(attr
);
480 struct netdev_rx_queue
*queue
= to_rx_queue(kobj
);
482 if (!attribute
->store
)
485 return attribute
->store(queue
, attribute
, buf
, count
);
488 static const struct sysfs_ops rx_queue_sysfs_ops
= {
489 .show
= rx_queue_attr_show
,
490 .store
= rx_queue_attr_store
,
493 static ssize_t
show_rps_map(struct netdev_rx_queue
*queue
,
494 struct rx_queue_attribute
*attribute
, char *buf
)
501 if (!zalloc_cpumask_var(&mask
, GFP_KERNEL
))
505 map
= rcu_dereference(queue
->rps_map
);
507 for (i
= 0; i
< map
->len
; i
++)
508 cpumask_set_cpu(map
->cpus
[i
], mask
);
510 len
+= cpumask_scnprintf(buf
+ len
, PAGE_SIZE
, mask
);
511 if (PAGE_SIZE
- len
< 3) {
513 free_cpumask_var(mask
);
518 free_cpumask_var(mask
);
519 len
+= sprintf(buf
+ len
, "\n");
523 static ssize_t
store_rps_map(struct netdev_rx_queue
*queue
,
524 struct rx_queue_attribute
*attribute
,
525 const char *buf
, size_t len
)
527 struct rps_map
*old_map
, *map
;
530 static DEFINE_SPINLOCK(rps_map_lock
);
532 if (!capable(CAP_NET_ADMIN
))
535 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
538 err
= bitmap_parse(buf
, len
, cpumask_bits(mask
), nr_cpumask_bits
);
540 free_cpumask_var(mask
);
544 map
= kzalloc(max_t(unsigned int,
545 RPS_MAP_SIZE(cpumask_weight(mask
)), L1_CACHE_BYTES
),
548 free_cpumask_var(mask
);
553 for_each_cpu_and(cpu
, mask
, cpu_online_mask
)
554 map
->cpus
[i
++] = cpu
;
563 spin_lock(&rps_map_lock
);
564 old_map
= rcu_dereference_protected(queue
->rps_map
,
565 lockdep_is_held(&rps_map_lock
));
566 rcu_assign_pointer(queue
->rps_map
, map
);
567 spin_unlock(&rps_map_lock
);
570 static_key_slow_inc(&rps_needed
);
572 kfree_rcu(old_map
, rcu
);
573 static_key_slow_dec(&rps_needed
);
575 free_cpumask_var(mask
);
579 static ssize_t
show_rps_dev_flow_table_cnt(struct netdev_rx_queue
*queue
,
580 struct rx_queue_attribute
*attr
,
583 struct rps_dev_flow_table
*flow_table
;
584 unsigned long val
= 0;
587 flow_table
= rcu_dereference(queue
->rps_flow_table
);
589 val
= (unsigned long)flow_table
->mask
+ 1;
592 return sprintf(buf
, "%lu\n", val
);
595 static void rps_dev_flow_table_release_work(struct work_struct
*work
)
597 struct rps_dev_flow_table
*table
= container_of(work
,
598 struct rps_dev_flow_table
, free_work
);
603 static void rps_dev_flow_table_release(struct rcu_head
*rcu
)
605 struct rps_dev_flow_table
*table
= container_of(rcu
,
606 struct rps_dev_flow_table
, rcu
);
608 INIT_WORK(&table
->free_work
, rps_dev_flow_table_release_work
);
609 schedule_work(&table
->free_work
);
612 static ssize_t
store_rps_dev_flow_table_cnt(struct netdev_rx_queue
*queue
,
613 struct rx_queue_attribute
*attr
,
614 const char *buf
, size_t len
)
616 unsigned long mask
, count
;
617 struct rps_dev_flow_table
*table
, *old_table
;
618 static DEFINE_SPINLOCK(rps_dev_flow_lock
);
621 if (!capable(CAP_NET_ADMIN
))
624 rc
= kstrtoul(buf
, 0, &count
);
630 /* mask = roundup_pow_of_two(count) - 1;
631 * without overflows...
633 while ((mask
| (mask
>> 1)) != mask
)
635 /* On 64 bit arches, must check mask fits in table->mask (u32),
636 * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
639 #if BITS_PER_LONG > 32
640 if (mask
> (unsigned long)(u32
)mask
)
643 if (mask
> (ULONG_MAX
- RPS_DEV_FLOW_TABLE_SIZE(1))
644 / sizeof(struct rps_dev_flow
)) {
645 /* Enforce a limit to prevent overflow */
649 table
= vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask
+ 1));
654 for (count
= 0; count
<= mask
; count
++)
655 table
->flows
[count
].cpu
= RPS_NO_CPU
;
659 spin_lock(&rps_dev_flow_lock
);
660 old_table
= rcu_dereference_protected(queue
->rps_flow_table
,
661 lockdep_is_held(&rps_dev_flow_lock
));
662 rcu_assign_pointer(queue
->rps_flow_table
, table
);
663 spin_unlock(&rps_dev_flow_lock
);
666 call_rcu(&old_table
->rcu
, rps_dev_flow_table_release
);
671 static struct rx_queue_attribute rps_cpus_attribute
=
672 __ATTR(rps_cpus
, S_IRUGO
| S_IWUSR
, show_rps_map
, store_rps_map
);
675 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute
=
676 __ATTR(rps_flow_cnt
, S_IRUGO
| S_IWUSR
,
677 show_rps_dev_flow_table_cnt
, store_rps_dev_flow_table_cnt
);
679 static struct attribute
*rx_queue_default_attrs
[] = {
680 &rps_cpus_attribute
.attr
,
681 &rps_dev_flow_table_cnt_attribute
.attr
,
685 static void rx_queue_release(struct kobject
*kobj
)
687 struct netdev_rx_queue
*queue
= to_rx_queue(kobj
);
689 struct rps_dev_flow_table
*flow_table
;
692 map
= rcu_dereference_protected(queue
->rps_map
, 1);
694 RCU_INIT_POINTER(queue
->rps_map
, NULL
);
698 flow_table
= rcu_dereference_protected(queue
->rps_flow_table
, 1);
700 RCU_INIT_POINTER(queue
->rps_flow_table
, NULL
);
701 call_rcu(&flow_table
->rcu
, rps_dev_flow_table_release
);
704 memset(kobj
, 0, sizeof(*kobj
));
708 static struct kobj_type rx_queue_ktype
= {
709 .sysfs_ops
= &rx_queue_sysfs_ops
,
710 .release
= rx_queue_release
,
711 .default_attrs
= rx_queue_default_attrs
,
714 static int rx_queue_add_kobject(struct net_device
*net
, int index
)
716 struct netdev_rx_queue
*queue
= net
->_rx
+ index
;
717 struct kobject
*kobj
= &queue
->kobj
;
720 kobj
->kset
= net
->queues_kset
;
721 error
= kobject_init_and_add(kobj
, &rx_queue_ktype
, NULL
,
728 kobject_uevent(kobj
, KOBJ_ADD
);
729 dev_hold(queue
->dev
);
733 #endif /* CONFIG_RPS */
736 net_rx_queue_update_kobjects(struct net_device
*net
, int old_num
, int new_num
)
742 for (i
= old_num
; i
< new_num
; i
++) {
743 error
= rx_queue_add_kobject(net
, i
);
750 while (--i
>= new_num
)
751 kobject_put(&net
->_rx
[i
].kobj
);
761 * netdev_queue sysfs structures and functions.
763 struct netdev_queue_attribute
{
764 struct attribute attr
;
765 ssize_t (*show
)(struct netdev_queue
*queue
,
766 struct netdev_queue_attribute
*attr
, char *buf
);
767 ssize_t (*store
)(struct netdev_queue
*queue
,
768 struct netdev_queue_attribute
*attr
, const char *buf
, size_t len
);
770 #define to_netdev_queue_attr(_attr) container_of(_attr, \
771 struct netdev_queue_attribute, attr)
773 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
775 static ssize_t
netdev_queue_attr_show(struct kobject
*kobj
,
776 struct attribute
*attr
, char *buf
)
778 struct netdev_queue_attribute
*attribute
= to_netdev_queue_attr(attr
);
779 struct netdev_queue
*queue
= to_netdev_queue(kobj
);
781 if (!attribute
->show
)
784 return attribute
->show(queue
, attribute
, buf
);
787 static ssize_t
netdev_queue_attr_store(struct kobject
*kobj
,
788 struct attribute
*attr
,
789 const char *buf
, size_t count
)
791 struct netdev_queue_attribute
*attribute
= to_netdev_queue_attr(attr
);
792 struct netdev_queue
*queue
= to_netdev_queue(kobj
);
794 if (!attribute
->store
)
797 return attribute
->store(queue
, attribute
, buf
, count
);
800 static const struct sysfs_ops netdev_queue_sysfs_ops
= {
801 .show
= netdev_queue_attr_show
,
802 .store
= netdev_queue_attr_store
,
805 static ssize_t
show_trans_timeout(struct netdev_queue
*queue
,
806 struct netdev_queue_attribute
*attribute
,
809 unsigned long trans_timeout
;
811 spin_lock_irq(&queue
->_xmit_lock
);
812 trans_timeout
= queue
->trans_timeout
;
813 spin_unlock_irq(&queue
->_xmit_lock
);
815 return sprintf(buf
, "%lu", trans_timeout
);
818 static struct netdev_queue_attribute queue_trans_timeout
=
819 __ATTR(tx_timeout
, S_IRUGO
, show_trans_timeout
, NULL
);
823 * Byte queue limits sysfs structures and functions.
825 static ssize_t
bql_show(char *buf
, unsigned int value
)
827 return sprintf(buf
, "%u\n", value
);
830 static ssize_t
bql_set(const char *buf
, const size_t count
,
831 unsigned int *pvalue
)
836 if (!strcmp(buf
, "max") || !strcmp(buf
, "max\n"))
837 value
= DQL_MAX_LIMIT
;
839 err
= kstrtouint(buf
, 10, &value
);
842 if (value
> DQL_MAX_LIMIT
)
851 static ssize_t
bql_show_hold_time(struct netdev_queue
*queue
,
852 struct netdev_queue_attribute
*attr
,
855 struct dql
*dql
= &queue
->dql
;
857 return sprintf(buf
, "%u\n", jiffies_to_msecs(dql
->slack_hold_time
));
860 static ssize_t
bql_set_hold_time(struct netdev_queue
*queue
,
861 struct netdev_queue_attribute
*attribute
,
862 const char *buf
, size_t len
)
864 struct dql
*dql
= &queue
->dql
;
868 err
= kstrtouint(buf
, 10, &value
);
872 dql
->slack_hold_time
= msecs_to_jiffies(value
);
877 static struct netdev_queue_attribute bql_hold_time_attribute
=
878 __ATTR(hold_time
, S_IRUGO
| S_IWUSR
, bql_show_hold_time
,
881 static ssize_t
bql_show_inflight(struct netdev_queue
*queue
,
882 struct netdev_queue_attribute
*attr
,
885 struct dql
*dql
= &queue
->dql
;
887 return sprintf(buf
, "%u\n", dql
->num_queued
- dql
->num_completed
);
890 static struct netdev_queue_attribute bql_inflight_attribute
=
891 __ATTR(inflight
, S_IRUGO
, bql_show_inflight
, NULL
);
893 #define BQL_ATTR(NAME, FIELD) \
894 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
895 struct netdev_queue_attribute *attr, \
898 return bql_show(buf, queue->dql.FIELD); \
901 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
902 struct netdev_queue_attribute *attr, \
903 const char *buf, size_t len) \
905 return bql_set(buf, len, &queue->dql.FIELD); \
908 static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
909 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
912 BQL_ATTR(limit
, limit
)
913 BQL_ATTR(limit_max
, max_limit
)
914 BQL_ATTR(limit_min
, min_limit
)
916 static struct attribute
*dql_attrs
[] = {
917 &bql_limit_attribute
.attr
,
918 &bql_limit_max_attribute
.attr
,
919 &bql_limit_min_attribute
.attr
,
920 &bql_hold_time_attribute
.attr
,
921 &bql_inflight_attribute
.attr
,
925 static struct attribute_group dql_group
= {
926 .name
= "byte_queue_limits",
929 #endif /* CONFIG_BQL */
932 static inline unsigned int get_netdev_queue_index(struct netdev_queue
*queue
)
934 struct net_device
*dev
= queue
->dev
;
937 for (i
= 0; i
< dev
->num_tx_queues
; i
++)
938 if (queue
== &dev
->_tx
[i
])
941 BUG_ON(i
>= dev
->num_tx_queues
);
947 static ssize_t
show_xps_map(struct netdev_queue
*queue
,
948 struct netdev_queue_attribute
*attribute
, char *buf
)
950 struct net_device
*dev
= queue
->dev
;
951 struct xps_dev_maps
*dev_maps
;
957 if (!zalloc_cpumask_var(&mask
, GFP_KERNEL
))
960 index
= get_netdev_queue_index(queue
);
963 dev_maps
= rcu_dereference(dev
->xps_maps
);
965 for_each_possible_cpu(i
) {
966 struct xps_map
*map
=
967 rcu_dereference(dev_maps
->cpu_map
[i
]);
970 for (j
= 0; j
< map
->len
; j
++) {
971 if (map
->queues
[j
] == index
) {
972 cpumask_set_cpu(i
, mask
);
981 len
+= cpumask_scnprintf(buf
+ len
, PAGE_SIZE
, mask
);
982 if (PAGE_SIZE
- len
< 3) {
983 free_cpumask_var(mask
);
987 free_cpumask_var(mask
);
988 len
+= sprintf(buf
+ len
, "\n");
992 static DEFINE_MUTEX(xps_map_mutex
);
993 #define xmap_dereference(P) \
994 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
996 static void xps_queue_release(struct netdev_queue
*queue
)
998 struct net_device
*dev
= queue
->dev
;
999 struct xps_dev_maps
*dev_maps
;
1000 struct xps_map
*map
;
1001 unsigned long index
;
1002 int i
, pos
, nonempty
= 0;
1004 index
= get_netdev_queue_index(queue
);
1006 mutex_lock(&xps_map_mutex
);
1007 dev_maps
= xmap_dereference(dev
->xps_maps
);
1010 for_each_possible_cpu(i
) {
1011 map
= xmap_dereference(dev_maps
->cpu_map
[i
]);
1015 for (pos
= 0; pos
< map
->len
; pos
++)
1016 if (map
->queues
[pos
] == index
)
1019 if (pos
< map
->len
) {
1022 map
->queues
[--map
->len
];
1024 RCU_INIT_POINTER(dev_maps
->cpu_map
[i
],
1026 kfree_rcu(map
, rcu
);
1035 RCU_INIT_POINTER(dev
->xps_maps
, NULL
);
1036 kfree_rcu(dev_maps
, rcu
);
1039 mutex_unlock(&xps_map_mutex
);
1042 static ssize_t
store_xps_map(struct netdev_queue
*queue
,
1043 struct netdev_queue_attribute
*attribute
,
1044 const char *buf
, size_t len
)
1046 struct net_device
*dev
= queue
->dev
;
1048 int err
, i
, cpu
, pos
, map_len
, alloc_len
, need_set
;
1049 unsigned long index
;
1050 struct xps_map
*map
, *new_map
;
1051 struct xps_dev_maps
*dev_maps
, *new_dev_maps
;
1053 int numa_node_id
= -2;
1055 if (!capable(CAP_NET_ADMIN
))
1058 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
1061 index
= get_netdev_queue_index(queue
);
1063 err
= bitmap_parse(buf
, len
, cpumask_bits(mask
), nr_cpumask_bits
);
1065 free_cpumask_var(mask
);
1069 new_dev_maps
= kzalloc(max_t(unsigned int,
1070 XPS_DEV_MAPS_SIZE
, L1_CACHE_BYTES
), GFP_KERNEL
);
1071 if (!new_dev_maps
) {
1072 free_cpumask_var(mask
);
1076 mutex_lock(&xps_map_mutex
);
1078 dev_maps
= xmap_dereference(dev
->xps_maps
);
1080 for_each_possible_cpu(cpu
) {
1082 xmap_dereference(dev_maps
->cpu_map
[cpu
]) : NULL
;
1085 for (pos
= 0; pos
< map
->len
; pos
++)
1086 if (map
->queues
[pos
] == index
)
1089 alloc_len
= map
->alloc_len
;
1091 pos
= map_len
= alloc_len
= 0;
1093 need_set
= cpumask_test_cpu(cpu
, mask
) && cpu_online(cpu
);
1096 if (numa_node_id
== -2)
1097 numa_node_id
= cpu_to_node(cpu
);
1098 else if (numa_node_id
!= cpu_to_node(cpu
))
1102 if (need_set
&& pos
>= map_len
) {
1103 /* Need to add queue to this CPU's map */
1104 if (map_len
>= alloc_len
) {
1105 alloc_len
= alloc_len
?
1106 2 * alloc_len
: XPS_MIN_MAP_ALLOC
;
1107 new_map
= kzalloc_node(XPS_MAP_SIZE(alloc_len
),
1112 new_map
->alloc_len
= alloc_len
;
1113 for (i
= 0; i
< map_len
; i
++)
1114 new_map
->queues
[i
] = map
->queues
[i
];
1115 new_map
->len
= map_len
;
1117 new_map
->queues
[new_map
->len
++] = index
;
1118 } else if (!need_set
&& pos
< map_len
) {
1119 /* Need to remove queue from this CPU's map */
1121 new_map
->queues
[pos
] =
1122 new_map
->queues
[--new_map
->len
];
1126 RCU_INIT_POINTER(new_dev_maps
->cpu_map
[cpu
], new_map
);
1129 /* Cleanup old maps */
1130 for_each_possible_cpu(cpu
) {
1132 xmap_dereference(dev_maps
->cpu_map
[cpu
]) : NULL
;
1133 if (map
&& xmap_dereference(new_dev_maps
->cpu_map
[cpu
]) != map
)
1134 kfree_rcu(map
, rcu
);
1135 if (new_dev_maps
->cpu_map
[cpu
])
1140 rcu_assign_pointer(dev
->xps_maps
, new_dev_maps
);
1142 kfree(new_dev_maps
);
1143 RCU_INIT_POINTER(dev
->xps_maps
, NULL
);
1147 kfree_rcu(dev_maps
, rcu
);
1149 netdev_queue_numa_node_write(queue
, (numa_node_id
>= 0) ? numa_node_id
:
1152 mutex_unlock(&xps_map_mutex
);
1154 free_cpumask_var(mask
);
1158 mutex_unlock(&xps_map_mutex
);
1161 for_each_possible_cpu(i
)
1162 kfree(rcu_dereference_protected(
1163 new_dev_maps
->cpu_map
[i
],
1165 kfree(new_dev_maps
);
1166 free_cpumask_var(mask
);
1170 static struct netdev_queue_attribute xps_cpus_attribute
=
1171 __ATTR(xps_cpus
, S_IRUGO
| S_IWUSR
, show_xps_map
, store_xps_map
);
1172 #endif /* CONFIG_XPS */
1174 static struct attribute
*netdev_queue_default_attrs
[] = {
1175 &queue_trans_timeout
.attr
,
1177 &xps_cpus_attribute
.attr
,
1182 static void netdev_queue_release(struct kobject
*kobj
)
1184 struct netdev_queue
*queue
= to_netdev_queue(kobj
);
1187 xps_queue_release(queue
);
1190 memset(kobj
, 0, sizeof(*kobj
));
1191 dev_put(queue
->dev
);
1194 static struct kobj_type netdev_queue_ktype
= {
1195 .sysfs_ops
= &netdev_queue_sysfs_ops
,
1196 .release
= netdev_queue_release
,
1197 .default_attrs
= netdev_queue_default_attrs
,
1200 static int netdev_queue_add_kobject(struct net_device
*net
, int index
)
1202 struct netdev_queue
*queue
= net
->_tx
+ index
;
1203 struct kobject
*kobj
= &queue
->kobj
;
1206 kobj
->kset
= net
->queues_kset
;
1207 error
= kobject_init_and_add(kobj
, &netdev_queue_ktype
, NULL
,
1213 error
= sysfs_create_group(kobj
, &dql_group
);
1218 kobject_uevent(kobj
, KOBJ_ADD
);
1219 dev_hold(queue
->dev
);
1226 #endif /* CONFIG_SYSFS */
1229 netdev_queue_update_kobjects(struct net_device
*net
, int old_num
, int new_num
)
1235 for (i
= old_num
; i
< new_num
; i
++) {
1236 error
= netdev_queue_add_kobject(net
, i
);
1243 while (--i
>= new_num
) {
1244 struct netdev_queue
*queue
= net
->_tx
+ i
;
1247 sysfs_remove_group(&queue
->kobj
, &dql_group
);
1249 kobject_put(&queue
->kobj
);
1255 #endif /* CONFIG_SYSFS */
1258 static int register_queue_kobjects(struct net_device
*net
)
1260 int error
= 0, txq
= 0, rxq
= 0, real_rx
= 0, real_tx
= 0;
1263 net
->queues_kset
= kset_create_and_add("queues",
1264 NULL
, &net
->dev
.kobj
);
1265 if (!net
->queues_kset
)
1270 real_rx
= net
->real_num_rx_queues
;
1272 real_tx
= net
->real_num_tx_queues
;
1274 error
= net_rx_queue_update_kobjects(net
, 0, real_rx
);
1279 error
= netdev_queue_update_kobjects(net
, 0, real_tx
);
1287 netdev_queue_update_kobjects(net
, txq
, 0);
1288 net_rx_queue_update_kobjects(net
, rxq
, 0);
1292 static void remove_queue_kobjects(struct net_device
*net
)
1294 int real_rx
= 0, real_tx
= 0;
1297 real_rx
= net
->real_num_rx_queues
;
1299 real_tx
= net
->real_num_tx_queues
;
1301 net_rx_queue_update_kobjects(net
, real_rx
, 0);
1302 netdev_queue_update_kobjects(net
, real_tx
, 0);
1304 kset_unregister(net
->queues_kset
);
1308 static void *net_grab_current_ns(void)
1310 struct net
*ns
= current
->nsproxy
->net_ns
;
1311 #ifdef CONFIG_NET_NS
1313 atomic_inc(&ns
->passive
);
1318 static const void *net_initial_ns(void)
1323 static const void *net_netlink_ns(struct sock
*sk
)
1325 return sock_net(sk
);
1328 struct kobj_ns_type_operations net_ns_type_operations
= {
1329 .type
= KOBJ_NS_TYPE_NET
,
1330 .grab_current_ns
= net_grab_current_ns
,
1331 .netlink_ns
= net_netlink_ns
,
1332 .initial_ns
= net_initial_ns
,
1333 .drop_ns
= net_drop_ns
,
1335 EXPORT_SYMBOL_GPL(net_ns_type_operations
);
1337 static int netdev_uevent(struct device
*d
, struct kobj_uevent_env
*env
)
1339 struct net_device
*dev
= to_net_dev(d
);
1342 /* pass interface to uevent. */
1343 retval
= add_uevent_var(env
, "INTERFACE=%s", dev
->name
);
1347 /* pass ifindex to uevent.
1348 * ifindex is useful as it won't change (interface name may change)
1349 * and is what RtNetlink uses natively. */
1350 retval
= add_uevent_var(env
, "IFINDEX=%d", dev
->ifindex
);
1357 * netdev_release -- destroy and free a dead device.
1358 * Called when last reference to device kobject is gone.
1360 static void netdev_release(struct device
*d
)
1362 struct net_device
*dev
= to_net_dev(d
);
1364 BUG_ON(dev
->reg_state
!= NETREG_RELEASED
);
1366 kfree(dev
->ifalias
);
1367 kfree((char *)dev
- dev
->padded
);
1370 static const void *net_namespace(struct device
*d
)
1372 struct net_device
*dev
;
1373 dev
= container_of(d
, struct net_device
, dev
);
1374 return dev_net(dev
);
1377 static struct class net_class
= {
1379 .dev_release
= netdev_release
,
1381 .dev_attrs
= net_class_attributes
,
1382 #endif /* CONFIG_SYSFS */
1383 .dev_uevent
= netdev_uevent
,
1384 .ns_type
= &net_ns_type_operations
,
1385 .namespace = net_namespace
,
1388 /* Delete sysfs entries but hold kobject reference until after all
1389 * netdev references are gone.
1391 void netdev_unregister_kobject(struct net_device
* net
)
1393 struct device
*dev
= &(net
->dev
);
1395 kobject_get(&dev
->kobj
);
1397 remove_queue_kobjects(net
);
1402 /* Create sysfs entries for network device. */
1403 int netdev_register_kobject(struct net_device
*net
)
1405 struct device
*dev
= &(net
->dev
);
1406 const struct attribute_group
**groups
= net
->sysfs_groups
;
1409 device_initialize(dev
);
1410 dev
->class = &net_class
;
1411 dev
->platform_data
= net
;
1412 dev
->groups
= groups
;
1414 dev_set_name(dev
, "%s", net
->name
);
1417 /* Allow for a device specific group */
1421 *groups
++ = &netstat_group
;
1423 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1424 if (net
->ieee80211_ptr
)
1425 *groups
++ = &wireless_group
;
1426 #if IS_ENABLED(CONFIG_WIRELESS_EXT)
1427 else if (net
->wireless_handlers
)
1428 *groups
++ = &wireless_group
;
1431 #endif /* CONFIG_SYSFS */
1433 error
= device_add(dev
);
1437 error
= register_queue_kobjects(net
);
1446 int netdev_class_create_file(struct class_attribute
*class_attr
)
1448 return class_create_file(&net_class
, class_attr
);
1450 EXPORT_SYMBOL(netdev_class_create_file
);
1452 void netdev_class_remove_file(struct class_attribute
*class_attr
)
1454 class_remove_file(&net_class
, class_attr
);
1456 EXPORT_SYMBOL(netdev_class_remove_file
);
1458 int netdev_kobject_init(void)
1460 kobj_ns_type_register(&net_ns_type_operations
);
1461 return class_register(&net_class
);