blob: 228e4a7f311b00ecbe2e266da35cc0fef94985f3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * net-sysfs.c - network device class and attributes
4 *
5 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
6 */
7
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/nsproxy.h>
15#include <net/sock.h>
16#include <net/net_namespace.h>
17#include <linux/rtnetlink.h>
18#include <linux/vmalloc.h>
19#include <linux/export.h>
20#include <linux/jiffies.h>
21#include <linux/pm_runtime.h>
22#include <linux/of.h>
23#include <linux/of_net.h>
24#include <linux/cpu.h>
25
26#include "net-sysfs.h"
27
28#ifdef CONFIG_SYSFS
29static const char fmt_hex[] = "%#x\n";
30static const char fmt_dec[] = "%d\n";
31static const char fmt_ulong[] = "%lu\n";
32static const char fmt_u64[] = "%llu\n";
33
34static inline int dev_isalive(const struct net_device *dev)
35{
36 return dev->reg_state <= NETREG_REGISTERED;
37}
38
39/* use same locking rules as GIF* ioctl's */
40static ssize_t netdev_show(const struct device *dev,
41 struct device_attribute *attr, char *buf,
42 ssize_t (*format)(const struct net_device *, char *))
43{
44 struct net_device *ndev = to_net_dev(dev);
45 ssize_t ret = -EINVAL;
46
47 read_lock(&dev_base_lock);
48 if (dev_isalive(ndev))
49 ret = (*format)(ndev, buf);
50 read_unlock(&dev_base_lock);
51
52 return ret;
53}
54
55/* generate a show function for simple field */
56#define NETDEVICE_SHOW(field, format_string) \
57static ssize_t format_##field(const struct net_device *dev, char *buf) \
58{ \
59 return sprintf(buf, format_string, dev->field); \
60} \
61static ssize_t field##_show(struct device *dev, \
62 struct device_attribute *attr, char *buf) \
63{ \
64 return netdev_show(dev, attr, buf, format_##field); \
65} \
66
67#define NETDEVICE_SHOW_RO(field, format_string) \
68NETDEVICE_SHOW(field, format_string); \
69static DEVICE_ATTR_RO(field)
70
71#define NETDEVICE_SHOW_RW(field, format_string) \
72NETDEVICE_SHOW(field, format_string); \
73static DEVICE_ATTR_RW(field)
74
75/* use same locking and permission rules as SIF* ioctl's */
76static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
77 const char *buf, size_t len,
78 int (*set)(struct net_device *, unsigned long))
79{
80 struct net_device *netdev = to_net_dev(dev);
81 struct net *net = dev_net(netdev);
82 unsigned long new;
83 int ret = -EINVAL;
84
85 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
86 return -EPERM;
87
88 ret = kstrtoul(buf, 0, &new);
89 if (ret)
90 goto err;
91
92 if (!rtnl_trylock())
93 return restart_syscall();
94
95 if (dev_isalive(netdev)) {
96 ret = (*set)(netdev, new);
97 if (ret == 0)
98 ret = len;
99 }
100 rtnl_unlock();
101 err:
102 return ret;
103}
104
105NETDEVICE_SHOW_RO(dev_id, fmt_hex);
106NETDEVICE_SHOW_RO(dev_port, fmt_dec);
107NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
108NETDEVICE_SHOW_RO(addr_len, fmt_dec);
109NETDEVICE_SHOW_RO(ifindex, fmt_dec);
110NETDEVICE_SHOW_RO(type, fmt_dec);
111NETDEVICE_SHOW_RO(link_mode, fmt_dec);
112
113static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
114 char *buf)
115{
116 struct net_device *ndev = to_net_dev(dev);
117
118 return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
119}
120static DEVICE_ATTR_RO(iflink);
121
122static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
123{
124 return sprintf(buf, fmt_dec, dev->name_assign_type);
125}
126
127static ssize_t name_assign_type_show(struct device *dev,
128 struct device_attribute *attr,
129 char *buf)
130{
131 struct net_device *ndev = to_net_dev(dev);
132 ssize_t ret = -EINVAL;
133
134 if (ndev->name_assign_type != NET_NAME_UNKNOWN)
135 ret = netdev_show(dev, attr, buf, format_name_assign_type);
136
137 return ret;
138}
139static DEVICE_ATTR_RO(name_assign_type);
140
141/* use same locking rules as GIFHWADDR ioctl's */
142static ssize_t address_show(struct device *dev, struct device_attribute *attr,
143 char *buf)
144{
145 struct net_device *ndev = to_net_dev(dev);
146 ssize_t ret = -EINVAL;
147
148 read_lock(&dev_base_lock);
149 if (dev_isalive(ndev))
150 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
151 read_unlock(&dev_base_lock);
152 return ret;
153}
154static DEVICE_ATTR_RO(address);
155
156static ssize_t broadcast_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 struct net_device *ndev = to_net_dev(dev);
160
161 if (dev_isalive(ndev))
162 return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
163 return -EINVAL;
164}
165static DEVICE_ATTR_RO(broadcast);
166
167static int change_carrier(struct net_device *dev, unsigned long new_carrier)
168{
169 if (!netif_running(dev))
170 return -EINVAL;
171 return dev_change_carrier(dev, (bool)new_carrier);
172}
173
174static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t len)
176{
177 struct net_device *netdev = to_net_dev(dev);
178
179 /* The check is also done in change_carrier; this helps returning early
180 * without hitting the trylock/restart in netdev_store.
181 */
182 if (!netdev->netdev_ops->ndo_change_carrier)
183 return -EOPNOTSUPP;
184
185 return netdev_store(dev, attr, buf, len, change_carrier);
186}
187
188static ssize_t carrier_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct net_device *netdev = to_net_dev(dev);
192
193 if (netif_running(netdev))
194 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
195
196 return -EINVAL;
197}
198static DEVICE_ATTR_RW(carrier);
199
200static ssize_t speed_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203 struct net_device *netdev = to_net_dev(dev);
204 int ret = -EINVAL;
205
206 /* The check is also done in __ethtool_get_link_ksettings; this helps
207 * returning early without hitting the trylock/restart below.
208 */
209 if (!netdev->ethtool_ops->get_link_ksettings)
210 return ret;
211
212 if (!rtnl_trylock())
213 return restart_syscall();
214
215 if (netif_running(netdev)) {
216 struct ethtool_link_ksettings cmd;
217
218 if (!__ethtool_get_link_ksettings(netdev, &cmd))
219 ret = sprintf(buf, fmt_dec, cmd.base.speed);
220 }
221 rtnl_unlock();
222 return ret;
223}
224static DEVICE_ATTR_RO(speed);
225
226static ssize_t duplex_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
228{
229 struct net_device *netdev = to_net_dev(dev);
230 int ret = -EINVAL;
231
232 /* The check is also done in __ethtool_get_link_ksettings; this helps
233 * returning early without hitting the trylock/restart below.
234 */
235 if (!netdev->ethtool_ops->get_link_ksettings)
236 return ret;
237
238 if (!rtnl_trylock())
239 return restart_syscall();
240
241 if (netif_running(netdev)) {
242 struct ethtool_link_ksettings cmd;
243
244 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
245 const char *duplex;
246
247 switch (cmd.base.duplex) {
248 case DUPLEX_HALF:
249 duplex = "half";
250 break;
251 case DUPLEX_FULL:
252 duplex = "full";
253 break;
254 default:
255 duplex = "unknown";
256 break;
257 }
258 ret = sprintf(buf, "%s\n", duplex);
259 }
260 }
261 rtnl_unlock();
262 return ret;
263}
264static DEVICE_ATTR_RO(duplex);
265
266static ssize_t dormant_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 struct net_device *netdev = to_net_dev(dev);
270
271 if (netif_running(netdev))
272 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
273
274 return -EINVAL;
275}
276static DEVICE_ATTR_RO(dormant);
277
278static const char *const operstates[] = {
279 "unknown",
280 "notpresent", /* currently unused */
281 "down",
282 "lowerlayerdown",
283 "testing", /* currently unused */
284 "dormant",
285 "up"
286};
287
288static ssize_t operstate_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
291 const struct net_device *netdev = to_net_dev(dev);
292 unsigned char operstate;
293
294 read_lock(&dev_base_lock);
295 operstate = netdev->operstate;
296 if (!netif_running(netdev))
297 operstate = IF_OPER_DOWN;
298 read_unlock(&dev_base_lock);
299
300 if (operstate >= ARRAY_SIZE(operstates))
301 return -EINVAL; /* should not happen */
302
303 return sprintf(buf, "%s\n", operstates[operstate]);
304}
305static DEVICE_ATTR_RO(operstate);
306
307static ssize_t carrier_changes_show(struct device *dev,
308 struct device_attribute *attr,
309 char *buf)
310{
311 struct net_device *netdev = to_net_dev(dev);
312
313 return sprintf(buf, fmt_dec,
314 atomic_read(&netdev->carrier_up_count) +
315 atomic_read(&netdev->carrier_down_count));
316}
317static DEVICE_ATTR_RO(carrier_changes);
318
319static ssize_t carrier_up_count_show(struct device *dev,
320 struct device_attribute *attr,
321 char *buf)
322{
323 struct net_device *netdev = to_net_dev(dev);
324
325 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
326}
327static DEVICE_ATTR_RO(carrier_up_count);
328
329static ssize_t carrier_down_count_show(struct device *dev,
330 struct device_attribute *attr,
331 char *buf)
332{
333 struct net_device *netdev = to_net_dev(dev);
334
335 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
336}
337static DEVICE_ATTR_RO(carrier_down_count);
338
339/* read-write attributes */
340
341static int change_mtu(struct net_device *dev, unsigned long new_mtu)
342{
343 return dev_set_mtu(dev, (int)new_mtu);
344}
345
346static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
347 const char *buf, size_t len)
348{
349 return netdev_store(dev, attr, buf, len, change_mtu);
350}
351NETDEVICE_SHOW_RW(mtu, fmt_dec);
352
353static int change_flags(struct net_device *dev, unsigned long new_flags)
354{
355 return dev_change_flags(dev, (unsigned int)new_flags, NULL);
356}
357
358static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
359 const char *buf, size_t len)
360{
361 return netdev_store(dev, attr, buf, len, change_flags);
362}
363NETDEVICE_SHOW_RW(flags, fmt_hex);
364
365static ssize_t tx_queue_len_store(struct device *dev,
366 struct device_attribute *attr,
367 const char *buf, size_t len)
368{
369 if (!capable(CAP_NET_ADMIN))
370 return -EPERM;
371
372 return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
373}
374NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
375
376static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
377{
378 dev->gro_flush_timeout = val;
379 return 0;
380}
381
382static ssize_t gro_flush_timeout_store(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t len)
385{
386 if (!capable(CAP_NET_ADMIN))
387 return -EPERM;
388
389 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
390}
391NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
392
393static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t len)
395{
396 struct net_device *netdev = to_net_dev(dev);
397 struct net *net = dev_net(netdev);
398 size_t count = len;
399 ssize_t ret = 0;
400
401 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
402 return -EPERM;
403
404 /* ignore trailing newline */
405 if (len > 0 && buf[len - 1] == '\n')
406 --count;
407
408 if (!rtnl_trylock())
409 return restart_syscall();
410
411 if (dev_isalive(netdev)) {
412 ret = dev_set_alias(netdev, buf, count);
413 if (ret < 0)
414 goto err;
415 ret = len;
416 netdev_state_change(netdev);
417 }
418err:
419 rtnl_unlock();
420
421 return ret;
422}
423
424static ssize_t ifalias_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
427 const struct net_device *netdev = to_net_dev(dev);
428 char tmp[IFALIASZ];
429 ssize_t ret = 0;
430
431 ret = dev_get_alias(netdev, tmp, sizeof(tmp));
432 if (ret > 0)
433 ret = sprintf(buf, "%s\n", tmp);
434 return ret;
435}
436static DEVICE_ATTR_RW(ifalias);
437
438static int change_group(struct net_device *dev, unsigned long new_group)
439{
440 dev_set_group(dev, (int)new_group);
441 return 0;
442}
443
444static ssize_t group_store(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t len)
446{
447 return netdev_store(dev, attr, buf, len, change_group);
448}
449NETDEVICE_SHOW(group, fmt_dec);
450static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
451
452static int change_proto_down(struct net_device *dev, unsigned long proto_down)
453{
454 return dev_change_proto_down(dev, (bool)proto_down);
455}
456
457static ssize_t proto_down_store(struct device *dev,
458 struct device_attribute *attr,
459 const char *buf, size_t len)
460{
461 struct net_device *netdev = to_net_dev(dev);
462
463 /* The check is also done in change_proto_down; this helps returning
464 * early without hitting the trylock/restart in netdev_store.
465 */
466 if (!netdev->netdev_ops->ndo_change_proto_down)
467 return -EOPNOTSUPP;
468
469 return netdev_store(dev, attr, buf, len, change_proto_down);
470}
471NETDEVICE_SHOW_RW(proto_down, fmt_dec);
472
473static ssize_t phys_port_id_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
476 struct net_device *netdev = to_net_dev(dev);
477 ssize_t ret = -EINVAL;
478
479 /* The check is also done in dev_get_phys_port_id; this helps returning
480 * early without hitting the trylock/restart below.
481 */
482 if (!netdev->netdev_ops->ndo_get_phys_port_id)
483 return -EOPNOTSUPP;
484
485 if (!rtnl_trylock())
486 return restart_syscall();
487
488 if (dev_isalive(netdev)) {
489 struct netdev_phys_item_id ppid;
490
491 ret = dev_get_phys_port_id(netdev, &ppid);
492 if (!ret)
493 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
494 }
495 rtnl_unlock();
496
497 return ret;
498}
499static DEVICE_ATTR_RO(phys_port_id);
500
501static ssize_t phys_port_name_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
504 struct net_device *netdev = to_net_dev(dev);
505 ssize_t ret = -EINVAL;
506
507 /* The checks are also done in dev_get_phys_port_name; this helps
508 * returning early without hitting the trylock/restart below.
509 */
510 if (!netdev->netdev_ops->ndo_get_phys_port_name &&
511 !netdev->netdev_ops->ndo_get_devlink_port)
512 return -EOPNOTSUPP;
513
514 if (!rtnl_trylock())
515 return restart_syscall();
516
517 if (dev_isalive(netdev)) {
518 char name[IFNAMSIZ];
519
520 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
521 if (!ret)
522 ret = sprintf(buf, "%s\n", name);
523 }
524 rtnl_unlock();
525
526 return ret;
527}
528static DEVICE_ATTR_RO(phys_port_name);
529
530static ssize_t phys_switch_id_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 struct net_device *netdev = to_net_dev(dev);
534 ssize_t ret = -EINVAL;
535
536 /* The checks are also done in dev_get_phys_port_name; this helps
537 * returning early without hitting the trylock/restart below. This works
538 * because recurse is false when calling dev_get_port_parent_id.
539 */
540 if (!netdev->netdev_ops->ndo_get_port_parent_id &&
541 !netdev->netdev_ops->ndo_get_devlink_port)
542 return -EOPNOTSUPP;
543
544 if (!rtnl_trylock())
545 return restart_syscall();
546
547 if (dev_isalive(netdev)) {
548 struct netdev_phys_item_id ppid = { };
549
550 ret = dev_get_port_parent_id(netdev, &ppid, false);
551 if (!ret)
552 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
553 }
554 rtnl_unlock();
555
556 return ret;
557}
558static DEVICE_ATTR_RO(phys_switch_id);
559
560static ssize_t threaded_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct net_device *netdev = to_net_dev(dev);
564 ssize_t ret = -EINVAL;
565
566 if (!rtnl_trylock())
567 return restart_syscall();
568
569 if (dev_isalive(netdev))
570 ret = sprintf(buf, fmt_dec, netdev->threaded);
571
572 rtnl_unlock();
573 return ret;
574}
575
576static int modify_napi_threaded(struct net_device *dev, unsigned long val)
577{
578 int ret;
579
580 if (list_empty(&dev->napi_list))
581 return -EOPNOTSUPP;
582
583 if (val != 0 && val != 1)
584 return -EOPNOTSUPP;
585
586 ret = dev_set_threaded(dev, val);
587
588 return ret;
589}
590
591static ssize_t threaded_store(struct device *dev,
592 struct device_attribute *attr,
593 const char *buf, size_t len)
594{
595 return netdev_store(dev, attr, buf, len, modify_napi_threaded);
596}
597static DEVICE_ATTR_RW(threaded);
598
599static struct attribute *net_class_attrs[] __ro_after_init = {
600 &dev_attr_netdev_group.attr,
601 &dev_attr_type.attr,
602 &dev_attr_dev_id.attr,
603 &dev_attr_dev_port.attr,
604 &dev_attr_iflink.attr,
605 &dev_attr_ifindex.attr,
606 &dev_attr_name_assign_type.attr,
607 &dev_attr_addr_assign_type.attr,
608 &dev_attr_addr_len.attr,
609 &dev_attr_link_mode.attr,
610 &dev_attr_address.attr,
611 &dev_attr_broadcast.attr,
612 &dev_attr_speed.attr,
613 &dev_attr_duplex.attr,
614 &dev_attr_dormant.attr,
615 &dev_attr_operstate.attr,
616 &dev_attr_carrier_changes.attr,
617 &dev_attr_ifalias.attr,
618 &dev_attr_carrier.attr,
619 &dev_attr_mtu.attr,
620 &dev_attr_flags.attr,
621 &dev_attr_tx_queue_len.attr,
622 &dev_attr_gro_flush_timeout.attr,
623 &dev_attr_phys_port_id.attr,
624 &dev_attr_phys_port_name.attr,
625 &dev_attr_phys_switch_id.attr,
626 &dev_attr_proto_down.attr,
627 &dev_attr_carrier_up_count.attr,
628 &dev_attr_carrier_down_count.attr,
629 &dev_attr_threaded.attr,
630 NULL,
631};
632ATTRIBUTE_GROUPS(net_class);
633
634/* Show a given an attribute in the statistics group */
635static ssize_t netstat_show(const struct device *d,
636 struct device_attribute *attr, char *buf,
637 unsigned long offset)
638{
639 struct net_device *dev = to_net_dev(d);
640 ssize_t ret = -EINVAL;
641
642 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
643 offset % sizeof(u64) != 0);
644
645 read_lock(&dev_base_lock);
646 if (dev_isalive(dev)) {
647 struct rtnl_link_stats64 temp;
648 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
649
650 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
651 }
652 read_unlock(&dev_base_lock);
653 return ret;
654}
655
656/* generate a read-only statistics attribute */
657#define NETSTAT_ENTRY(name) \
658static ssize_t name##_show(struct device *d, \
659 struct device_attribute *attr, char *buf) \
660{ \
661 return netstat_show(d, attr, buf, \
662 offsetof(struct rtnl_link_stats64, name)); \
663} \
664static DEVICE_ATTR_RO(name)
665
666NETSTAT_ENTRY(rx_packets);
667NETSTAT_ENTRY(tx_packets);
668NETSTAT_ENTRY(rx_bytes);
669NETSTAT_ENTRY(tx_bytes);
670NETSTAT_ENTRY(rx_errors);
671NETSTAT_ENTRY(tx_errors);
672NETSTAT_ENTRY(rx_dropped);
673NETSTAT_ENTRY(tx_dropped);
674NETSTAT_ENTRY(multicast);
675NETSTAT_ENTRY(collisions);
676NETSTAT_ENTRY(rx_length_errors);
677NETSTAT_ENTRY(rx_over_errors);
678NETSTAT_ENTRY(rx_crc_errors);
679NETSTAT_ENTRY(rx_frame_errors);
680NETSTAT_ENTRY(rx_fifo_errors);
681NETSTAT_ENTRY(rx_missed_errors);
682NETSTAT_ENTRY(tx_aborted_errors);
683NETSTAT_ENTRY(tx_carrier_errors);
684NETSTAT_ENTRY(tx_fifo_errors);
685NETSTAT_ENTRY(tx_heartbeat_errors);
686NETSTAT_ENTRY(tx_window_errors);
687NETSTAT_ENTRY(rx_compressed);
688NETSTAT_ENTRY(tx_compressed);
689NETSTAT_ENTRY(rx_nohandler);
690
691static struct attribute *netstat_attrs[] __ro_after_init = {
692 &dev_attr_rx_packets.attr,
693 &dev_attr_tx_packets.attr,
694 &dev_attr_rx_bytes.attr,
695 &dev_attr_tx_bytes.attr,
696 &dev_attr_rx_errors.attr,
697 &dev_attr_tx_errors.attr,
698 &dev_attr_rx_dropped.attr,
699 &dev_attr_tx_dropped.attr,
700 &dev_attr_multicast.attr,
701 &dev_attr_collisions.attr,
702 &dev_attr_rx_length_errors.attr,
703 &dev_attr_rx_over_errors.attr,
704 &dev_attr_rx_crc_errors.attr,
705 &dev_attr_rx_frame_errors.attr,
706 &dev_attr_rx_fifo_errors.attr,
707 &dev_attr_rx_missed_errors.attr,
708 &dev_attr_tx_aborted_errors.attr,
709 &dev_attr_tx_carrier_errors.attr,
710 &dev_attr_tx_fifo_errors.attr,
711 &dev_attr_tx_heartbeat_errors.attr,
712 &dev_attr_tx_window_errors.attr,
713 &dev_attr_rx_compressed.attr,
714 &dev_attr_tx_compressed.attr,
715 &dev_attr_rx_nohandler.attr,
716 NULL
717};
718
719static const struct attribute_group netstat_group = {
720 .name = "statistics",
721 .attrs = netstat_attrs,
722};
723
724#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
725static struct attribute *wireless_attrs[] = {
726 NULL
727};
728
729static const struct attribute_group wireless_group = {
730 .name = "wireless",
731 .attrs = wireless_attrs,
732};
733#endif
734
735#else /* CONFIG_SYSFS */
736#define net_class_groups NULL
737#endif /* CONFIG_SYSFS */
738
739#ifdef CONFIG_SYSFS
740#define to_rx_queue_attr(_attr) \
741 container_of(_attr, struct rx_queue_attribute, attr)
742
743#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
744
745static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
746 char *buf)
747{
748 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
749 struct netdev_rx_queue *queue = to_rx_queue(kobj);
750
751 if (!attribute->show)
752 return -EIO;
753
754 return attribute->show(queue, buf);
755}
756
757static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
758 const char *buf, size_t count)
759{
760 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
761 struct netdev_rx_queue *queue = to_rx_queue(kobj);
762
763 if (!attribute->store)
764 return -EIO;
765
766 return attribute->store(queue, buf, count);
767}
768
769static const struct sysfs_ops rx_queue_sysfs_ops = {
770 .show = rx_queue_attr_show,
771 .store = rx_queue_attr_store,
772};
773
774#ifdef CONFIG_RPS
775static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
776{
777 struct rps_map *map;
778 cpumask_var_t mask;
779 int i, len;
780
781 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
782 return -ENOMEM;
783
784 rcu_read_lock();
785 map = rcu_dereference(queue->rps_map);
786 if (map)
787 for (i = 0; i < map->len; i++)
788 cpumask_set_cpu(map->cpus[i], mask);
789
790 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
791 rcu_read_unlock();
792 free_cpumask_var(mask);
793
794 return len < PAGE_SIZE ? len : -EINVAL;
795}
796
797static ssize_t store_rps_map(struct netdev_rx_queue *queue,
798 const char *buf, size_t len)
799{
800 struct rps_map *old_map, *map;
801 cpumask_var_t mask;
802 int err, cpu, i;
803 static DEFINE_MUTEX(rps_map_mutex);
804
805 if (!capable(CAP_NET_ADMIN))
806 return -EPERM;
807
808 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
809 return -ENOMEM;
810
811 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
812 if (err) {
813 free_cpumask_var(mask);
814 return err;
815 }
816
817 map = kzalloc(max_t(unsigned int,
818 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
819 GFP_KERNEL);
820 if (!map) {
821 free_cpumask_var(mask);
822 return -ENOMEM;
823 }
824
825 i = 0;
826 for_each_cpu_and(cpu, mask, cpu_online_mask)
827 map->cpus[i++] = cpu;
828
829 if (i) {
830 map->len = i;
831 } else {
832 kfree(map);
833 map = NULL;
834 }
835
836 mutex_lock(&rps_map_mutex);
837 old_map = rcu_dereference_protected(queue->rps_map,
838 mutex_is_locked(&rps_map_mutex));
839 rcu_assign_pointer(queue->rps_map, map);
840
841 if (map)
842 static_branch_inc(&rps_needed);
843 if (old_map)
844 static_branch_dec(&rps_needed);
845
846 mutex_unlock(&rps_map_mutex);
847
848 if (old_map)
849 kfree_rcu(old_map, rcu);
850
851 free_cpumask_var(mask);
852 return len;
853}
854
855static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
856 char *buf)
857{
858 struct rps_dev_flow_table *flow_table;
859 unsigned long val = 0;
860
861 rcu_read_lock();
862 flow_table = rcu_dereference(queue->rps_flow_table);
863 if (flow_table)
864 val = (unsigned long)flow_table->mask + 1;
865 rcu_read_unlock();
866
867 return sprintf(buf, "%lu\n", val);
868}
869
870static void rps_dev_flow_table_release(struct rcu_head *rcu)
871{
872 struct rps_dev_flow_table *table = container_of(rcu,
873 struct rps_dev_flow_table, rcu);
874 vfree(table);
875}
876
877static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
878 const char *buf, size_t len)
879{
880 unsigned long mask, count;
881 struct rps_dev_flow_table *table, *old_table;
882 static DEFINE_SPINLOCK(rps_dev_flow_lock);
883 int rc;
884
885 if (!capable(CAP_NET_ADMIN))
886 return -EPERM;
887
888 rc = kstrtoul(buf, 0, &count);
889 if (rc < 0)
890 return rc;
891
892 if (count) {
893 mask = count - 1;
894 /* mask = roundup_pow_of_two(count) - 1;
895 * without overflows...
896 */
897 while ((mask | (mask >> 1)) != mask)
898 mask |= (mask >> 1);
899 /* On 64 bit arches, must check mask fits in table->mask (u32),
900 * and on 32bit arches, must check
901 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
902 */
903#if BITS_PER_LONG > 32
904 if (mask > (unsigned long)(u32)mask)
905 return -EINVAL;
906#else
907 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
908 / sizeof(struct rps_dev_flow)) {
909 /* Enforce a limit to prevent overflow */
910 return -EINVAL;
911 }
912#endif
913 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
914 if (!table)
915 return -ENOMEM;
916
917 table->mask = mask;
918 for (count = 0; count <= mask; count++)
919 table->flows[count].cpu = RPS_NO_CPU;
920 } else {
921 table = NULL;
922 }
923
924 spin_lock(&rps_dev_flow_lock);
925 old_table = rcu_dereference_protected(queue->rps_flow_table,
926 lockdep_is_held(&rps_dev_flow_lock));
927 rcu_assign_pointer(queue->rps_flow_table, table);
928 spin_unlock(&rps_dev_flow_lock);
929
930 if (old_table)
931 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
932
933 return len;
934}
935
936static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
937 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
938
939static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
940 = __ATTR(rps_flow_cnt, 0644,
941 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
942#endif /* CONFIG_RPS */
943
944static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
945#ifdef CONFIG_RPS
946 &rps_cpus_attribute.attr,
947 &rps_dev_flow_table_cnt_attribute.attr,
948#endif
949 NULL
950};
951ATTRIBUTE_GROUPS(rx_queue_default);
952
953static void rx_queue_release(struct kobject *kobj)
954{
955 struct netdev_rx_queue *queue = to_rx_queue(kobj);
956#ifdef CONFIG_RPS
957 struct rps_map *map;
958 struct rps_dev_flow_table *flow_table;
959
960 map = rcu_dereference_protected(queue->rps_map, 1);
961 if (map) {
962 RCU_INIT_POINTER(queue->rps_map, NULL);
963 kfree_rcu(map, rcu);
964 }
965
966 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
967 if (flow_table) {
968 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
969 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
970 }
971#endif
972
973 memset(kobj, 0, sizeof(*kobj));
974 dev_put(queue->dev);
975}
976
977static const void *rx_queue_namespace(struct kobject *kobj)
978{
979 struct netdev_rx_queue *queue = to_rx_queue(kobj);
980 struct device *dev = &queue->dev->dev;
981 const void *ns = NULL;
982
983 if (dev->class && dev->class->ns_type)
984 ns = dev->class->namespace(dev);
985
986 return ns;
987}
988
989static void rx_queue_get_ownership(struct kobject *kobj,
990 kuid_t *uid, kgid_t *gid)
991{
992 const struct net *net = rx_queue_namespace(kobj);
993
994 net_ns_get_ownership(net, uid, gid);
995}
996
997static struct kobj_type rx_queue_ktype __ro_after_init = {
998 .sysfs_ops = &rx_queue_sysfs_ops,
999 .release = rx_queue_release,
1000 .default_groups = rx_queue_default_groups,
1001 .namespace = rx_queue_namespace,
1002 .get_ownership = rx_queue_get_ownership,
1003};
1004
1005static int rx_queue_add_kobject(struct net_device *dev, int index)
1006{
1007 struct netdev_rx_queue *queue = dev->_rx + index;
1008 struct kobject *kobj = &queue->kobj;
1009 int error = 0;
1010
1011 /* Kobject_put later will trigger rx_queue_release call which
1012 * decreases dev refcount: Take that reference here
1013 */
1014 dev_hold(queue->dev);
1015
1016 kobj->kset = dev->queues_kset;
1017 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1018 "rx-%u", index);
1019 if (error)
1020 goto err;
1021
1022 if (dev->sysfs_rx_queue_group) {
1023 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1024 if (error)
1025 goto err;
1026 }
1027
1028 kobject_uevent(kobj, KOBJ_ADD);
1029
1030 return error;
1031
1032err:
1033 kobject_put(kobj);
1034 return error;
1035}
1036#endif /* CONFIG_SYSFS */
1037
1038int
1039net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1040{
1041#ifdef CONFIG_SYSFS
1042 int i;
1043 int error = 0;
1044
1045#ifndef CONFIG_RPS
1046 if (!dev->sysfs_rx_queue_group)
1047 return 0;
1048#endif
1049 for (i = old_num; i < new_num; i++) {
1050 error = rx_queue_add_kobject(dev, i);
1051 if (error) {
1052 new_num = old_num;
1053 break;
1054 }
1055 }
1056
1057 while (--i >= new_num) {
1058 struct kobject *kobj = &dev->_rx[i].kobj;
1059
1060 if (!refcount_read(&dev_net(dev)->count))
1061 kobj->uevent_suppress = 1;
1062 if (dev->sysfs_rx_queue_group)
1063 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1064 kobject_put(kobj);
1065 }
1066
1067 return error;
1068#else
1069 return 0;
1070#endif
1071}
1072
1073#ifdef CONFIG_SYSFS
1074/*
1075 * netdev_queue sysfs structures and functions.
1076 */
1077struct netdev_queue_attribute {
1078 struct attribute attr;
1079 ssize_t (*show)(struct netdev_queue *queue, char *buf);
1080 ssize_t (*store)(struct netdev_queue *queue,
1081 const char *buf, size_t len);
1082};
1083#define to_netdev_queue_attr(_attr) \
1084 container_of(_attr, struct netdev_queue_attribute, attr)
1085
1086#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1087
1088static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1089 struct attribute *attr, char *buf)
1090{
1091 const struct netdev_queue_attribute *attribute
1092 = to_netdev_queue_attr(attr);
1093 struct netdev_queue *queue = to_netdev_queue(kobj);
1094
1095 if (!attribute->show)
1096 return -EIO;
1097
1098 return attribute->show(queue, buf);
1099}
1100
1101static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1102 struct attribute *attr,
1103 const char *buf, size_t count)
1104{
1105 const struct netdev_queue_attribute *attribute
1106 = to_netdev_queue_attr(attr);
1107 struct netdev_queue *queue = to_netdev_queue(kobj);
1108
1109 if (!attribute->store)
1110 return -EIO;
1111
1112 return attribute->store(queue, buf, count);
1113}
1114
1115static const struct sysfs_ops netdev_queue_sysfs_ops = {
1116 .show = netdev_queue_attr_show,
1117 .store = netdev_queue_attr_store,
1118};
1119
1120static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
1121{
1122 unsigned long trans_timeout;
1123
1124 spin_lock_irq(&queue->_xmit_lock);
1125 trans_timeout = queue->trans_timeout;
1126 spin_unlock_irq(&queue->_xmit_lock);
1127
1128 return sprintf(buf, fmt_ulong, trans_timeout);
1129}
1130
1131static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1132{
1133 struct net_device *dev = queue->dev;
1134 unsigned int i;
1135
1136 i = queue - dev->_tx;
1137 BUG_ON(i >= dev->num_tx_queues);
1138
1139 return i;
1140}
1141
1142static ssize_t traffic_class_show(struct netdev_queue *queue,
1143 char *buf)
1144{
1145 struct net_device *dev = queue->dev;
1146 int index;
1147 int tc;
1148
1149 if (!netif_is_multiqueue(dev))
1150 return -ENOENT;
1151
1152 index = get_netdev_queue_index(queue);
1153
1154 /* If queue belongs to subordinate dev use its TC mapping */
1155 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1156
1157 tc = netdev_txq_to_tc(dev, index);
1158 if (tc < 0)
1159 return -EINVAL;
1160
1161 /* We can report the traffic class one of two ways:
1162 * Subordinate device traffic classes are reported with the traffic
1163 * class first, and then the subordinate class so for example TC0 on
1164 * subordinate device 2 will be reported as "0-2". If the queue
1165 * belongs to the root device it will be reported with just the
1166 * traffic class, so just "0" for TC 0 for example.
1167 */
1168 return dev->num_tc < 0 ? sprintf(buf, "%u%d\n", tc, dev->num_tc) :
1169 sprintf(buf, "%u\n", tc);
1170}
1171
1172#ifdef CONFIG_XPS
1173static ssize_t tx_maxrate_show(struct netdev_queue *queue,
1174 char *buf)
1175{
1176 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1177}
1178
1179static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1180 const char *buf, size_t len)
1181{
1182 struct net_device *dev = queue->dev;
1183 int err, index = get_netdev_queue_index(queue);
1184 u32 rate = 0;
1185
1186 if (!capable(CAP_NET_ADMIN))
1187 return -EPERM;
1188
1189 /* The check is also done later; this helps returning early without
1190 * hitting the trylock/restart below.
1191 */
1192 if (!dev->netdev_ops->ndo_set_tx_maxrate)
1193 return -EOPNOTSUPP;
1194
1195 err = kstrtou32(buf, 10, &rate);
1196 if (err < 0)
1197 return err;
1198
1199 if (!rtnl_trylock())
1200 return restart_syscall();
1201
1202 err = -EOPNOTSUPP;
1203 if (dev->netdev_ops->ndo_set_tx_maxrate)
1204 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1205
1206 rtnl_unlock();
1207 if (!err) {
1208 queue->tx_maxrate = rate;
1209 return len;
1210 }
1211 return err;
1212}
1213
1214static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1215 = __ATTR_RW(tx_maxrate);
1216#endif
1217
1218static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1219 = __ATTR_RO(tx_timeout);
1220
1221static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1222 = __ATTR_RO(traffic_class);
1223
1224#ifdef CONFIG_BQL
1225/*
1226 * Byte queue limits sysfs structures and functions.
1227 */
1228static ssize_t bql_show(char *buf, unsigned int value)
1229{
1230 return sprintf(buf, "%u\n", value);
1231}
1232
1233static ssize_t bql_set(const char *buf, const size_t count,
1234 unsigned int *pvalue)
1235{
1236 unsigned int value;
1237 int err;
1238
1239 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1240 value = DQL_MAX_LIMIT;
1241 } else {
1242 err = kstrtouint(buf, 10, &value);
1243 if (err < 0)
1244 return err;
1245 if (value > DQL_MAX_LIMIT)
1246 return -EINVAL;
1247 }
1248
1249 *pvalue = value;
1250
1251 return count;
1252}
1253
1254static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1255 char *buf)
1256{
1257 struct dql *dql = &queue->dql;
1258
1259 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1260}
1261
1262static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1263 const char *buf, size_t len)
1264{
1265 struct dql *dql = &queue->dql;
1266 unsigned int value;
1267 int err;
1268
1269 err = kstrtouint(buf, 10, &value);
1270 if (err < 0)
1271 return err;
1272
1273 dql->slack_hold_time = msecs_to_jiffies(value);
1274
1275 return len;
1276}
1277
1278static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1279 = __ATTR(hold_time, 0644,
1280 bql_show_hold_time, bql_set_hold_time);
1281
1282static ssize_t bql_show_inflight(struct netdev_queue *queue,
1283 char *buf)
1284{
1285 struct dql *dql = &queue->dql;
1286
1287 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1288}
1289
1290static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1291 __ATTR(inflight, 0444, bql_show_inflight, NULL);
1292
1293#define BQL_ATTR(NAME, FIELD) \
1294static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1295 char *buf) \
1296{ \
1297 return bql_show(buf, queue->dql.FIELD); \
1298} \
1299 \
1300static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1301 const char *buf, size_t len) \
1302{ \
1303 return bql_set(buf, len, &queue->dql.FIELD); \
1304} \
1305 \
1306static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1307 = __ATTR(NAME, 0644, \
1308 bql_show_ ## NAME, bql_set_ ## NAME)
1309
1310BQL_ATTR(limit, limit);
1311BQL_ATTR(limit_max, max_limit);
1312BQL_ATTR(limit_min, min_limit);
1313
1314static struct attribute *dql_attrs[] __ro_after_init = {
1315 &bql_limit_attribute.attr,
1316 &bql_limit_max_attribute.attr,
1317 &bql_limit_min_attribute.attr,
1318 &bql_hold_time_attribute.attr,
1319 &bql_inflight_attribute.attr,
1320 NULL
1321};
1322
1323static const struct attribute_group dql_group = {
1324 .name = "byte_queue_limits",
1325 .attrs = dql_attrs,
1326};
1327#endif /* CONFIG_BQL */
1328
1329#ifdef CONFIG_XPS
1330static ssize_t xps_cpus_show(struct netdev_queue *queue,
1331 char *buf)
1332{
1333 int cpu, len, ret, num_tc = 1, tc = 0;
1334 struct net_device *dev = queue->dev;
1335 struct xps_dev_maps *dev_maps;
1336 cpumask_var_t mask;
1337 unsigned long index;
1338
1339 if (!netif_is_multiqueue(dev))
1340 return -ENOENT;
1341
1342 index = get_netdev_queue_index(queue);
1343
1344 if (!rtnl_trylock())
1345 return restart_syscall();
1346
1347 if (dev->num_tc) {
1348 /* Do not allow XPS on subordinate device directly */
1349 num_tc = dev->num_tc;
1350 if (num_tc < 0) {
1351 ret = -EINVAL;
1352 goto err_rtnl_unlock;
1353 }
1354
1355 /* If queue belongs to subordinate dev use its map */
1356 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1357
1358 tc = netdev_txq_to_tc(dev, index);
1359 if (tc < 0) {
1360 ret = -EINVAL;
1361 goto err_rtnl_unlock;
1362 }
1363 }
1364
1365 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
1366 ret = -ENOMEM;
1367 goto err_rtnl_unlock;
1368 }
1369
1370 rcu_read_lock();
1371 dev_maps = rcu_dereference(dev->xps_cpus_map);
1372 if (dev_maps) {
1373 for_each_possible_cpu(cpu) {
1374 int i, tci = cpu * num_tc + tc;
1375 struct xps_map *map;
1376
1377 map = rcu_dereference(dev_maps->attr_map[tci]);
1378 if (!map)
1379 continue;
1380
1381 for (i = map->len; i--;) {
1382 if (map->queues[i] == index) {
1383 cpumask_set_cpu(cpu, mask);
1384 break;
1385 }
1386 }
1387 }
1388 }
1389 rcu_read_unlock();
1390
1391 rtnl_unlock();
1392
1393 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
1394 free_cpumask_var(mask);
1395 return len < PAGE_SIZE ? len : -EINVAL;
1396
1397err_rtnl_unlock:
1398 rtnl_unlock();
1399 return ret;
1400}
1401
1402static ssize_t xps_cpus_store(struct netdev_queue *queue,
1403 const char *buf, size_t len)
1404{
1405 struct net_device *dev = queue->dev;
1406 unsigned long index;
1407 cpumask_var_t mask;
1408 int err;
1409
1410 if (!netif_is_multiqueue(dev))
1411 return -ENOENT;
1412
1413 if (!capable(CAP_NET_ADMIN))
1414 return -EPERM;
1415
1416 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1417 return -ENOMEM;
1418
1419 index = get_netdev_queue_index(queue);
1420
1421 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1422 if (err) {
1423 free_cpumask_var(mask);
1424 return err;
1425 }
1426
1427 if (!rtnl_trylock()) {
1428 free_cpumask_var(mask);
1429 return restart_syscall();
1430 }
1431
1432 err = netif_set_xps_queue(dev, mask, index);
1433 rtnl_unlock();
1434
1435 free_cpumask_var(mask);
1436
1437 return err ? : len;
1438}
1439
1440static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1441 = __ATTR_RW(xps_cpus);
1442
1443static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1444{
1445 int j, len, ret, num_tc = 1, tc = 0;
1446 struct net_device *dev = queue->dev;
1447 struct xps_dev_maps *dev_maps;
1448 unsigned long *mask, index;
1449
1450 index = get_netdev_queue_index(queue);
1451
1452 if (!rtnl_trylock())
1453 return restart_syscall();
1454
1455 if (dev->num_tc) {
1456 num_tc = dev->num_tc;
1457 tc = netdev_txq_to_tc(dev, index);
1458 if (tc < 0) {
1459 ret = -EINVAL;
1460 goto err_rtnl_unlock;
1461 }
1462 }
1463 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1464 if (!mask) {
1465 ret = -ENOMEM;
1466 goto err_rtnl_unlock;
1467 }
1468
1469 rcu_read_lock();
1470 dev_maps = rcu_dereference(dev->xps_rxqs_map);
1471 if (!dev_maps)
1472 goto out_no_maps;
1473
1474 for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues),
1475 j < dev->num_rx_queues;) {
1476 int i, tci = j * num_tc + tc;
1477 struct xps_map *map;
1478
1479 map = rcu_dereference(dev_maps->attr_map[tci]);
1480 if (!map)
1481 continue;
1482
1483 for (i = map->len; i--;) {
1484 if (map->queues[i] == index) {
1485 set_bit(j, mask);
1486 break;
1487 }
1488 }
1489 }
1490out_no_maps:
1491 rcu_read_unlock();
1492
1493 rtnl_unlock();
1494
1495 len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues);
1496 bitmap_free(mask);
1497
1498 return len < PAGE_SIZE ? len : -EINVAL;
1499
1500err_rtnl_unlock:
1501 rtnl_unlock();
1502 return ret;
1503}
1504
1505static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1506 size_t len)
1507{
1508 struct net_device *dev = queue->dev;
1509 struct net *net = dev_net(dev);
1510 unsigned long *mask, index;
1511 int err;
1512
1513 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1514 return -EPERM;
1515
1516 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1517 if (!mask)
1518 return -ENOMEM;
1519
1520 index = get_netdev_queue_index(queue);
1521
1522 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1523 if (err) {
1524 bitmap_free(mask);
1525 return err;
1526 }
1527
1528 if (!rtnl_trylock()) {
1529 bitmap_free(mask);
1530 return restart_syscall();
1531 }
1532
1533 cpus_read_lock();
1534 err = __netif_set_xps_queue(dev, mask, index, true);
1535 cpus_read_unlock();
1536
1537 rtnl_unlock();
1538
1539 bitmap_free(mask);
1540 return err ? : len;
1541}
1542
1543static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1544 = __ATTR_RW(xps_rxqs);
1545#endif /* CONFIG_XPS */
1546
1547static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1548 &queue_trans_timeout.attr,
1549 &queue_traffic_class.attr,
1550#ifdef CONFIG_XPS
1551 &xps_cpus_attribute.attr,
1552 &xps_rxqs_attribute.attr,
1553 &queue_tx_maxrate.attr,
1554#endif
1555 NULL
1556};
1557ATTRIBUTE_GROUPS(netdev_queue_default);
1558
1559static void netdev_queue_release(struct kobject *kobj)
1560{
1561 struct netdev_queue *queue = to_netdev_queue(kobj);
1562
1563 memset(kobj, 0, sizeof(*kobj));
1564 dev_put(queue->dev);
1565}
1566
1567static const void *netdev_queue_namespace(struct kobject *kobj)
1568{
1569 struct netdev_queue *queue = to_netdev_queue(kobj);
1570 struct device *dev = &queue->dev->dev;
1571 const void *ns = NULL;
1572
1573 if (dev->class && dev->class->ns_type)
1574 ns = dev->class->namespace(dev);
1575
1576 return ns;
1577}
1578
1579static void netdev_queue_get_ownership(struct kobject *kobj,
1580 kuid_t *uid, kgid_t *gid)
1581{
1582 const struct net *net = netdev_queue_namespace(kobj);
1583
1584 net_ns_get_ownership(net, uid, gid);
1585}
1586
1587static struct kobj_type netdev_queue_ktype __ro_after_init = {
1588 .sysfs_ops = &netdev_queue_sysfs_ops,
1589 .release = netdev_queue_release,
1590 .default_groups = netdev_queue_default_groups,
1591 .namespace = netdev_queue_namespace,
1592 .get_ownership = netdev_queue_get_ownership,
1593};
1594
1595static int netdev_queue_add_kobject(struct net_device *dev, int index)
1596{
1597 struct netdev_queue *queue = dev->_tx + index;
1598 struct kobject *kobj = &queue->kobj;
1599 int error = 0;
1600
1601 /* Kobject_put later will trigger netdev_queue_release call
1602 * which decreases dev refcount: Take that reference here
1603 */
1604 dev_hold(queue->dev);
1605
1606 kobj->kset = dev->queues_kset;
1607 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1608 "tx-%u", index);
1609 if (error)
1610 goto err;
1611
1612#ifdef CONFIG_BQL
1613 error = sysfs_create_group(kobj, &dql_group);
1614 if (error)
1615 goto err;
1616#endif
1617
1618 kobject_uevent(kobj, KOBJ_ADD);
1619 return 0;
1620
1621err:
1622 kobject_put(kobj);
1623 return error;
1624}
1625#endif /* CONFIG_SYSFS */
1626
1627int
1628netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1629{
1630#ifdef CONFIG_SYSFS
1631 int i;
1632 int error = 0;
1633
1634 for (i = old_num; i < new_num; i++) {
1635 error = netdev_queue_add_kobject(dev, i);
1636 if (error) {
1637 new_num = old_num;
1638 break;
1639 }
1640 }
1641
1642 while (--i >= new_num) {
1643 struct netdev_queue *queue = dev->_tx + i;
1644
1645 if (!refcount_read(&dev_net(dev)->count))
1646 queue->kobj.uevent_suppress = 1;
1647#ifdef CONFIG_BQL
1648 sysfs_remove_group(&queue->kobj, &dql_group);
1649#endif
1650 kobject_put(&queue->kobj);
1651 }
1652
1653 return error;
1654#else
1655 return 0;
1656#endif /* CONFIG_SYSFS */
1657}
1658
1659static int register_queue_kobjects(struct net_device *dev)
1660{
1661 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1662
1663#ifdef CONFIG_SYSFS
1664 dev->queues_kset = kset_create_and_add("queues",
1665 NULL, &dev->dev.kobj);
1666 if (!dev->queues_kset)
1667 return -ENOMEM;
1668 real_rx = dev->real_num_rx_queues;
1669#endif
1670 real_tx = dev->real_num_tx_queues;
1671
1672 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
1673 if (error)
1674 goto error;
1675 rxq = real_rx;
1676
1677 error = netdev_queue_update_kobjects(dev, 0, real_tx);
1678 if (error)
1679 goto error;
1680 txq = real_tx;
1681
1682 return 0;
1683
1684error:
1685 netdev_queue_update_kobjects(dev, txq, 0);
1686 net_rx_queue_update_kobjects(dev, rxq, 0);
1687#ifdef CONFIG_SYSFS
1688 kset_unregister(dev->queues_kset);
1689#endif
1690 return error;
1691}
1692
1693static void remove_queue_kobjects(struct net_device *dev)
1694{
1695 int real_rx = 0, real_tx = 0;
1696
1697#ifdef CONFIG_SYSFS
1698 real_rx = dev->real_num_rx_queues;
1699#endif
1700 real_tx = dev->real_num_tx_queues;
1701
1702 net_rx_queue_update_kobjects(dev, real_rx, 0);
1703 netdev_queue_update_kobjects(dev, real_tx, 0);
1704
1705 dev->real_num_rx_queues = 0;
1706 dev->real_num_tx_queues = 0;
1707#ifdef CONFIG_SYSFS
1708 kset_unregister(dev->queues_kset);
1709#endif
1710}
1711
1712static bool net_current_may_mount(void)
1713{
1714 struct net *net = current->nsproxy->net_ns;
1715
1716 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1717}
1718
1719static void *net_grab_current_ns(void)
1720{
1721 struct net *ns = current->nsproxy->net_ns;
1722#ifdef CONFIG_NET_NS
1723 if (ns)
1724 refcount_inc(&ns->passive);
1725#endif
1726 return ns;
1727}
1728
1729static const void *net_initial_ns(void)
1730{
1731 return &init_net;
1732}
1733
1734static const void *net_netlink_ns(struct sock *sk)
1735{
1736 return sock_net(sk);
1737}
1738
1739const struct kobj_ns_type_operations net_ns_type_operations = {
1740 .type = KOBJ_NS_TYPE_NET,
1741 .current_may_mount = net_current_may_mount,
1742 .grab_current_ns = net_grab_current_ns,
1743 .netlink_ns = net_netlink_ns,
1744 .initial_ns = net_initial_ns,
1745 .drop_ns = net_drop_ns,
1746};
1747EXPORT_SYMBOL_GPL(net_ns_type_operations);
1748
1749static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1750{
1751 struct net_device *dev = to_net_dev(d);
1752 int retval;
1753
1754 /* pass interface to uevent. */
1755 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1756 if (retval)
1757 goto exit;
1758
1759 /* pass ifindex to uevent.
1760 * ifindex is useful as it won't change (interface name may change)
1761 * and is what RtNetlink uses natively.
1762 */
1763 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1764
1765exit:
1766 return retval;
1767}
1768
1769/*
1770 * netdev_release -- destroy and free a dead device.
1771 * Called when last reference to device kobject is gone.
1772 */
1773static void netdev_release(struct device *d)
1774{
1775 struct net_device *dev = to_net_dev(d);
1776
1777 BUG_ON(dev->reg_state != NETREG_RELEASED);
1778
1779 /* no need to wait for rcu grace period:
1780 * device is dead and about to be freed.
1781 */
1782 kfree(rcu_access_pointer(dev->ifalias));
1783 netdev_freemem(dev);
1784}
1785
1786static const void *net_namespace(struct device *d)
1787{
1788 struct net_device *dev = to_net_dev(d);
1789
1790 return dev_net(dev);
1791}
1792
1793static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
1794{
1795 struct net_device *dev = to_net_dev(d);
1796 const struct net *net = dev_net(dev);
1797
1798 net_ns_get_ownership(net, uid, gid);
1799}
1800
1801static struct class net_class __ro_after_init = {
1802 .name = "net",
1803 .dev_release = netdev_release,
1804 .dev_groups = net_class_groups,
1805 .dev_uevent = netdev_uevent,
1806 .ns_type = &net_ns_type_operations,
1807 .namespace = net_namespace,
1808 .get_ownership = net_get_ownership,
1809};
1810
1811#ifdef CONFIG_OF_NET
1812static int of_dev_node_match(struct device *dev, const void *data)
1813{
1814 int ret = 0;
1815
1816 if (dev->parent)
1817 ret = dev->parent->of_node == data;
1818
1819 return ret == 0 ? dev->of_node == data : ret;
1820}
1821
1822/*
1823 * of_find_net_device_by_node - lookup the net device for the device node
1824 * @np: OF device node
1825 *
1826 * Looks up the net_device structure corresponding with the device node.
1827 * If successful, returns a pointer to the net_device with the embedded
1828 * struct device refcount incremented by one, or NULL on failure. The
1829 * refcount must be dropped when done with the net_device.
1830 */
1831struct net_device *of_find_net_device_by_node(struct device_node *np)
1832{
1833 struct device *dev;
1834
1835 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1836 if (!dev)
1837 return NULL;
1838
1839 return to_net_dev(dev);
1840}
1841EXPORT_SYMBOL(of_find_net_device_by_node);
1842#endif
1843
1844/* Delete sysfs entries but hold kobject reference until after all
1845 * netdev references are gone.
1846 */
1847void netdev_unregister_kobject(struct net_device *ndev)
1848{
1849 struct device *dev = &ndev->dev;
1850
1851 if (!refcount_read(&dev_net(ndev)->count))
1852 dev_set_uevent_suppress(dev, 1);
1853
1854 kobject_get(&dev->kobj);
1855
1856 remove_queue_kobjects(ndev);
1857
1858 pm_runtime_set_memalloc_noio(dev, false);
1859
1860 device_del(dev);
1861}
1862
1863/* Create sysfs entries for network device. */
1864int netdev_register_kobject(struct net_device *ndev)
1865{
1866 struct device *dev = &ndev->dev;
1867 const struct attribute_group **groups = ndev->sysfs_groups;
1868 int error = 0;
1869
1870 device_initialize(dev);
1871 dev->class = &net_class;
1872 dev->platform_data = ndev;
1873 dev->groups = groups;
1874
1875 dev_set_name(dev, "%s", ndev->name);
1876
1877#ifdef CONFIG_SYSFS
1878 /* Allow for a device specific group */
1879 if (*groups)
1880 groups++;
1881
1882 *groups++ = &netstat_group;
1883
1884#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1885 if (ndev->ieee80211_ptr)
1886 *groups++ = &wireless_group;
1887#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1888 else if (ndev->wireless_handlers)
1889 *groups++ = &wireless_group;
1890#endif
1891#endif
1892#endif /* CONFIG_SYSFS */
1893
1894 error = device_add(dev);
1895 if (error)
1896 return error;
1897
1898 error = register_queue_kobjects(ndev);
1899 if (error) {
1900 device_del(dev);
1901 return error;
1902 }
1903
1904 pm_runtime_set_memalloc_noio(dev, true);
1905
1906 return error;
1907}
1908
1909int netdev_class_create_file_ns(const struct class_attribute *class_attr,
1910 const void *ns)
1911{
1912 return class_create_file_ns(&net_class, class_attr, ns);
1913}
1914EXPORT_SYMBOL(netdev_class_create_file_ns);
1915
1916void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
1917 const void *ns)
1918{
1919 class_remove_file_ns(&net_class, class_attr, ns);
1920}
1921EXPORT_SYMBOL(netdev_class_remove_file_ns);
1922
1923int __init netdev_kobject_init(void)
1924{
1925 kobj_ns_type_register(&net_ns_type_operations);
1926 return class_register(&net_class);
1927}