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