1
2
3
4
5
6
7
8
9
10
11
12#include <linux/capability.h>
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <net/switchdev.h>
16#include <linux/if_arp.h>
17#include <linux/slab.h>
18#include <linux/nsproxy.h>
19#include <net/sock.h>
20#include <net/net_namespace.h>
21#include <linux/rtnetlink.h>
22#include <linux/vmalloc.h>
23#include <linux/export.h>
24#include <linux/jiffies.h>
25#include <linux/pm_runtime.h>
26
27#include "net-sysfs.h"
28
29#ifdef CONFIG_SYSFS
30static const char fmt_hex[] = "%#x\n";
31static const char fmt_long_hex[] = "%#lx\n";
32static const char fmt_dec[] = "%d\n";
33static const char fmt_ulong[] = "%lu\n";
34static const char fmt_u64[] = "%llu\n";
35
36static inline int dev_isalive(const struct net_device *dev)
37{
38 return dev->reg_state <= NETREG_REGISTERED;
39}
40
41
42static ssize_t netdev_show(const struct device *dev,
43 struct device_attribute *attr, char *buf,
44 ssize_t (*format)(const struct net_device *, char *))
45{
46 struct net_device *net = to_net_dev(dev);
47 ssize_t ret = -EINVAL;
48
49 read_lock(&dev_base_lock);
50 if (dev_isalive(net))
51 ret = (*format)(net, buf);
52 read_unlock(&dev_base_lock);
53
54 return ret;
55}
56
57
58#define NETDEVICE_SHOW(field, format_string) \
59static ssize_t format_##field(const struct net_device *net, char *buf) \
60{ \
61 return sprintf(buf, format_string, net->field); \
62} \
63static ssize_t field##_show(struct device *dev, \
64 struct device_attribute *attr, char *buf) \
65{ \
66 return netdev_show(dev, attr, buf, format_##field); \
67} \
68
69#define NETDEVICE_SHOW_RO(field, format_string) \
70NETDEVICE_SHOW(field, format_string); \
71static DEVICE_ATTR_RO(field)
72
73#define NETDEVICE_SHOW_RW(field, format_string) \
74NETDEVICE_SHOW(field, format_string); \
75static DEVICE_ATTR_RW(field)
76
77
78static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
79 const char *buf, size_t len,
80 int (*set)(struct net_device *, unsigned long))
81{
82 struct net_device *netdev = to_net_dev(dev);
83 struct net *net = dev_net(netdev);
84 unsigned long new;
85 int ret = -EINVAL;
86
87 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
88 return -EPERM;
89
90 ret = kstrtoul(buf, 0, &new);
91 if (ret)
92 goto err;
93
94 if (!rtnl_trylock())
95 return restart_syscall();
96
97 if (dev_isalive(netdev)) {
98 if ((ret = (*set)(netdev, new)) == 0)
99 ret = len;
100 }
101 rtnl_unlock();
102 err:
103 return ret;
104}
105
106NETDEVICE_SHOW_RO(dev_id, fmt_hex);
107NETDEVICE_SHOW_RO(dev_port, fmt_dec);
108NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
109NETDEVICE_SHOW_RO(addr_len, fmt_dec);
110NETDEVICE_SHOW_RO(ifindex, fmt_dec);
111NETDEVICE_SHOW_RO(type, fmt_dec);
112NETDEVICE_SHOW_RO(link_mode, fmt_dec);
113
114static 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}
121static DEVICE_ATTR_RO(iflink);
122
123
124static ssize_t address_show(struct device *dev, struct device_attribute *attr,
125 char *buf)
126{
127 struct net_device *net = to_net_dev(dev);
128 ssize_t ret = -EINVAL;
129
130 read_lock(&dev_base_lock);
131 if (dev_isalive(net))
132 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
133 read_unlock(&dev_base_lock);
134 return ret;
135}
136static DEVICE_ATTR_RO(address);
137
138static ssize_t broadcast_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
140{
141 struct net_device *net = to_net_dev(dev);
142 if (dev_isalive(net))
143 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
144 return -EINVAL;
145}
146static DEVICE_ATTR_RO(broadcast);
147
148static int change_carrier(struct net_device *net, unsigned long new_carrier)
149{
150 if (!netif_running(net))
151 return -EINVAL;
152 return dev_change_carrier(net, (bool) new_carrier);
153}
154
155static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
156 const char *buf, size_t len)
157{
158 return netdev_store(dev, attr, buf, len, change_carrier);
159}
160
161static ssize_t carrier_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 struct net_device *netdev = to_net_dev(dev);
165 if (netif_running(netdev)) {
166 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
167 }
168 return -EINVAL;
169}
170static DEVICE_ATTR_RW(carrier);
171
172static ssize_t speed_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct net_device *netdev = to_net_dev(dev);
176 int ret = -EINVAL;
177
178 if (!rtnl_trylock())
179 return restart_syscall();
180
181 if (netif_running(netdev)) {
182 struct ethtool_link_ksettings cmd;
183
184 if (!__ethtool_get_link_ksettings(netdev, &cmd))
185 ret = sprintf(buf, fmt_dec, cmd.base.speed);
186 }
187 rtnl_unlock();
188 return ret;
189}
190static DEVICE_ATTR_RO(speed);
191
192static ssize_t duplex_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 struct net_device *netdev = to_net_dev(dev);
196 int ret = -EINVAL;
197
198 if (!rtnl_trylock())
199 return restart_syscall();
200
201 if (netif_running(netdev)) {
202 struct ethtool_link_ksettings cmd;
203
204 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
205 const char *duplex;
206
207 switch (cmd.base.duplex) {
208 case DUPLEX_HALF:
209 duplex = "half";
210 break;
211 case DUPLEX_FULL:
212 duplex = "full";
213 break;
214 default:
215 duplex = "unknown";
216 break;
217 }
218 ret = sprintf(buf, "%s\n", duplex);
219 }
220 }
221 rtnl_unlock();
222 return ret;
223}
224static DEVICE_ATTR_RO(duplex);
225
226static ssize_t dormant_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
228{
229 struct net_device *netdev = to_net_dev(dev);
230
231 if (netif_running(netdev))
232 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
233
234 return -EINVAL;
235}
236static DEVICE_ATTR_RO(dormant);
237
238static const char *const operstates[] = {
239 "unknown",
240 "notpresent",
241 "down",
242 "lowerlayerdown",
243 "testing",
244 "dormant",
245 "up"
246};
247
248static ssize_t operstate_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 const struct net_device *netdev = to_net_dev(dev);
252 unsigned char operstate;
253
254 read_lock(&dev_base_lock);
255 operstate = netdev->operstate;
256 if (!netif_running(netdev))
257 operstate = IF_OPER_DOWN;
258 read_unlock(&dev_base_lock);
259
260 if (operstate >= ARRAY_SIZE(operstates))
261 return -EINVAL;
262
263 return sprintf(buf, "%s\n", operstates[operstate]);
264}
265static DEVICE_ATTR_RO(operstate);
266
267static ssize_t carrier_changes_show(struct device *dev,
268 struct device_attribute *attr,
269 char *buf)
270{
271 struct net_device *netdev = to_net_dev(dev);
272 return sprintf(buf, fmt_dec,
273 atomic_read(&netdev->carrier_changes));
274}
275static DEVICE_ATTR_RO(carrier_changes);
276
277
278
279static int change_mtu(struct net_device *net, unsigned long new_mtu)
280{
281 return dev_set_mtu(net, (int) new_mtu);
282}
283
284static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t len)
286{
287 return netdev_store(dev, attr, buf, len, change_mtu);
288}
289NETDEVICE_SHOW_RW(mtu, fmt_dec);
290
291static int change_flags(struct net_device *net, unsigned long new_flags)
292{
293 return dev_change_flags(net, (unsigned int) new_flags);
294}
295
296static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t len)
298{
299 return netdev_store(dev, attr, buf, len, change_flags);
300}
301NETDEVICE_SHOW_RW(flags, fmt_hex);
302
303static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
304{
305 int res, orig_len = dev->tx_queue_len;
306
307 if (new_len != orig_len) {
308 dev->tx_queue_len = new_len;
309 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
310 res = notifier_to_errno(res);
311 if (res) {
312 netdev_err(dev,
313 "refused to change device tx_queue_len\n");
314 dev->tx_queue_len = orig_len;
315 return -EFAULT;
316 }
317 }
318
319 return 0;
320}
321
322static ssize_t tx_queue_len_store(struct device *dev,
323 struct device_attribute *attr,
324 const char *buf, size_t len)
325{
326 if (!capable(CAP_NET_ADMIN))
327 return -EPERM;
328
329 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
330}
331NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
332
333static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
334{
335 dev->gro_flush_timeout = val;
336 return 0;
337}
338
339static ssize_t gro_flush_timeout_store(struct device *dev,
340 struct device_attribute *attr,
341 const char *buf, size_t len)
342{
343 if (!capable(CAP_NET_ADMIN))
344 return -EPERM;
345
346 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
347}
348NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
349
350static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
351 const char *buf, size_t len)
352{
353 struct net_device *netdev = to_net_dev(dev);
354 struct net *net = dev_net(netdev);
355 size_t count = len;
356 ssize_t ret;
357
358 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
359 return -EPERM;
360
361
362 if (len > 0 && buf[len - 1] == '\n')
363 --count;
364
365 if (!rtnl_trylock())
366 return restart_syscall();
367 ret = dev_set_alias(netdev, buf, count);
368 rtnl_unlock();
369
370 return ret < 0 ? ret : len;
371}
372
373static ssize_t ifalias_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
375{
376 const struct net_device *netdev = to_net_dev(dev);
377 ssize_t ret = 0;
378
379 if (!rtnl_trylock())
380 return restart_syscall();
381 if (netdev->ifalias)
382 ret = sprintf(buf, "%s\n", netdev->ifalias);
383 rtnl_unlock();
384 return ret;
385}
386static DEVICE_ATTR_RW(ifalias);
387
388static int change_group(struct net_device *net, unsigned long new_group)
389{
390 dev_set_group(net, (int) new_group);
391 return 0;
392}
393
394static ssize_t group_store(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t len)
396{
397 return netdev_store(dev, attr, buf, len, change_group);
398}
399NETDEVICE_SHOW(group, fmt_dec);
400static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
401
402static int change_proto_down(struct net_device *dev, unsigned long proto_down)
403{
404 return dev_change_proto_down(dev, (bool) proto_down);
405}
406
407static ssize_t proto_down_store(struct device *dev,
408 struct device_attribute *attr,
409 const char *buf, size_t len)
410{
411 return netdev_store(dev, attr, buf, len, change_proto_down);
412}
413NETDEVICE_SHOW_RW(proto_down, fmt_dec);
414
415static ssize_t phys_port_id_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
418 struct net_device *netdev = to_net_dev(dev);
419 ssize_t ret = -EINVAL;
420
421 if (!rtnl_trylock())
422 return restart_syscall();
423
424 if (dev_isalive(netdev)) {
425 struct netdev_phys_item_id ppid;
426
427 ret = dev_get_phys_port_id(netdev, &ppid);
428 if (!ret)
429 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
430 }
431 rtnl_unlock();
432
433 return ret;
434}
435static DEVICE_ATTR_RO(phys_port_id);
436
437static ssize_t phys_port_name_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
440 struct net_device *netdev = to_net_dev(dev);
441 ssize_t ret = -EINVAL;
442
443 if (!rtnl_trylock())
444 return restart_syscall();
445
446 if (dev_isalive(netdev)) {
447 char name[IFNAMSIZ];
448
449 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
450 if (!ret)
451 ret = sprintf(buf, "%s\n", name);
452 }
453 rtnl_unlock();
454
455 return ret;
456}
457static DEVICE_ATTR_RO(phys_port_name);
458
459static ssize_t phys_switch_id_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct net_device *netdev = to_net_dev(dev);
463 ssize_t ret = -EINVAL;
464
465 if (!rtnl_trylock())
466 return restart_syscall();
467
468 if (dev_isalive(netdev)) {
469 struct switchdev_attr attr = {
470 .orig_dev = netdev,
471 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
472 .flags = SWITCHDEV_F_NO_RECURSE,
473 };
474
475 ret = switchdev_port_attr_get(netdev, &attr);
476 if (!ret)
477 ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
478 attr.u.ppid.id);
479 }
480 rtnl_unlock();
481
482 return ret;
483}
484static DEVICE_ATTR_RO(phys_switch_id);
485
486static struct attribute *net_class_attrs[] = {
487 &dev_attr_netdev_group.attr,
488 &dev_attr_type.attr,
489 &dev_attr_dev_id.attr,
490 &dev_attr_dev_port.attr,
491 &dev_attr_iflink.attr,
492 &dev_attr_ifindex.attr,
493 &dev_attr_addr_assign_type.attr,
494 &dev_attr_addr_len.attr,
495 &dev_attr_link_mode.attr,
496 &dev_attr_address.attr,
497 &dev_attr_broadcast.attr,
498 &dev_attr_speed.attr,
499 &dev_attr_duplex.attr,
500 &dev_attr_dormant.attr,
501 &dev_attr_operstate.attr,
502 &dev_attr_carrier_changes.attr,
503 &dev_attr_ifalias.attr,
504 &dev_attr_carrier.attr,
505 &dev_attr_mtu.attr,
506 &dev_attr_flags.attr,
507 &dev_attr_tx_queue_len.attr,
508 &dev_attr_gro_flush_timeout.attr,
509 &dev_attr_phys_port_id.attr,
510 &dev_attr_phys_port_name.attr,
511 &dev_attr_phys_switch_id.attr,
512 &dev_attr_proto_down.attr,
513 NULL,
514};
515ATTRIBUTE_GROUPS(net_class);
516
517
518static ssize_t netstat_show(const struct device *d,
519 struct device_attribute *attr, char *buf,
520 unsigned long offset)
521{
522 struct net_device *dev = to_net_dev(d);
523 ssize_t ret = -EINVAL;
524
525 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
526 offset % sizeof(u64) != 0);
527
528 read_lock(&dev_base_lock);
529 if (dev_isalive(dev)) {
530 struct rtnl_link_stats64 temp;
531 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
532
533 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
534 }
535 read_unlock(&dev_base_lock);
536 return ret;
537}
538
539
540#define NETSTAT_ENTRY(name) \
541static ssize_t name##_show(struct device *d, \
542 struct device_attribute *attr, char *buf) \
543{ \
544 return netstat_show(d, attr, buf, \
545 offsetof(struct rtnl_link_stats64, name)); \
546} \
547static DEVICE_ATTR_RO(name)
548
549NETSTAT_ENTRY(rx_packets);
550NETSTAT_ENTRY(tx_packets);
551NETSTAT_ENTRY(rx_bytes);
552NETSTAT_ENTRY(tx_bytes);
553NETSTAT_ENTRY(rx_errors);
554NETSTAT_ENTRY(tx_errors);
555NETSTAT_ENTRY(rx_dropped);
556NETSTAT_ENTRY(tx_dropped);
557NETSTAT_ENTRY(multicast);
558NETSTAT_ENTRY(collisions);
559NETSTAT_ENTRY(rx_length_errors);
560NETSTAT_ENTRY(rx_over_errors);
561NETSTAT_ENTRY(rx_crc_errors);
562NETSTAT_ENTRY(rx_frame_errors);
563NETSTAT_ENTRY(rx_fifo_errors);
564NETSTAT_ENTRY(rx_missed_errors);
565NETSTAT_ENTRY(tx_aborted_errors);
566NETSTAT_ENTRY(tx_carrier_errors);
567NETSTAT_ENTRY(tx_fifo_errors);
568NETSTAT_ENTRY(tx_heartbeat_errors);
569NETSTAT_ENTRY(tx_window_errors);
570NETSTAT_ENTRY(rx_compressed);
571NETSTAT_ENTRY(tx_compressed);
572NETSTAT_ENTRY(rx_nohandler);
573
574static struct attribute *netstat_attrs[] = {
575 &dev_attr_rx_packets.attr,
576 &dev_attr_tx_packets.attr,
577 &dev_attr_rx_bytes.attr,
578 &dev_attr_tx_bytes.attr,
579 &dev_attr_rx_errors.attr,
580 &dev_attr_tx_errors.attr,
581 &dev_attr_rx_dropped.attr,
582 &dev_attr_tx_dropped.attr,
583 &dev_attr_multicast.attr,
584 &dev_attr_collisions.attr,
585 &dev_attr_rx_length_errors.attr,
586 &dev_attr_rx_over_errors.attr,
587 &dev_attr_rx_crc_errors.attr,
588 &dev_attr_rx_frame_errors.attr,
589 &dev_attr_rx_fifo_errors.attr,
590 &dev_attr_rx_missed_errors.attr,
591 &dev_attr_tx_aborted_errors.attr,
592 &dev_attr_tx_carrier_errors.attr,
593 &dev_attr_tx_fifo_errors.attr,
594 &dev_attr_tx_heartbeat_errors.attr,
595 &dev_attr_tx_window_errors.attr,
596 &dev_attr_rx_compressed.attr,
597 &dev_attr_tx_compressed.attr,
598 &dev_attr_rx_nohandler.attr,
599 NULL
600};
601
602
603static struct attribute_group netstat_group = {
604 .name = "statistics",
605 .attrs = netstat_attrs,
606};
607
608#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
609static struct attribute *wireless_attrs[] = {
610 NULL
611};
612
613static struct attribute_group wireless_group = {
614 .name = "wireless",
615 .attrs = wireless_attrs,
616};
617#endif
618
619#else
620#define net_class_groups NULL
621#endif
622
623#ifdef CONFIG_RPS
624
625
626
627struct rx_queue_attribute {
628 struct attribute attr;
629 ssize_t (*show)(struct netdev_rx_queue *queue,
630 struct rx_queue_attribute *attr, char *buf);
631 ssize_t (*store)(struct netdev_rx_queue *queue,
632 struct rx_queue_attribute *attr, const char *buf, size_t len);
633};
634#define to_rx_queue_attr(_attr) container_of(_attr, \
635 struct rx_queue_attribute, attr)
636
637#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
638
639static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
640 char *buf)
641{
642 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
643 struct netdev_rx_queue *queue = to_rx_queue(kobj);
644
645 if (!attribute->show)
646 return -EIO;
647
648 return attribute->show(queue, attribute, buf);
649}
650
651static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
652 const char *buf, size_t count)
653{
654 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
655 struct netdev_rx_queue *queue = to_rx_queue(kobj);
656
657 if (!attribute->store)
658 return -EIO;
659
660 return attribute->store(queue, attribute, buf, count);
661}
662
663static const struct sysfs_ops rx_queue_sysfs_ops = {
664 .show = rx_queue_attr_show,
665 .store = rx_queue_attr_store,
666};
667
668static ssize_t show_rps_map(struct netdev_rx_queue *queue,
669 struct rx_queue_attribute *attribute, char *buf)
670{
671 struct rps_map *map;
672 cpumask_var_t mask;
673 size_t len = 0;
674 int i;
675
676 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
677 return -ENOMEM;
678
679 rcu_read_lock();
680 map = rcu_dereference(queue->rps_map);
681 if (map)
682 for (i = 0; i < map->len; i++)
683 cpumask_set_cpu(map->cpus[i], mask);
684
685 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
686 if (PAGE_SIZE - len < 3) {
687 rcu_read_unlock();
688 free_cpumask_var(mask);
689 return -EINVAL;
690 }
691 rcu_read_unlock();
692
693 free_cpumask_var(mask);
694 len += sprintf(buf + len, "\n");
695 return len;
696}
697
698static ssize_t store_rps_map(struct netdev_rx_queue *queue,
699 struct rx_queue_attribute *attribute,
700 const char *buf, size_t len)
701{
702 struct rps_map *old_map, *map;
703 cpumask_var_t mask;
704 int err, cpu, i;
705 static DEFINE_SPINLOCK(rps_map_lock);
706
707 if (!capable(CAP_NET_ADMIN))
708 return -EPERM;
709
710 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
711 return -ENOMEM;
712
713 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
714 if (err) {
715 free_cpumask_var(mask);
716 return err;
717 }
718
719 map = kzalloc(max_t(unsigned int,
720 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
721 GFP_KERNEL);
722 if (!map) {
723 free_cpumask_var(mask);
724 return -ENOMEM;
725 }
726
727 i = 0;
728 for_each_cpu_and(cpu, mask, cpu_online_mask)
729 map->cpus[i++] = cpu;
730
731 if (i)
732 map->len = i;
733 else {
734 kfree(map);
735 map = NULL;
736 }
737
738 spin_lock(&rps_map_lock);
739 old_map = rcu_dereference_protected(queue->rps_map,
740 lockdep_is_held(&rps_map_lock));
741 rcu_assign_pointer(queue->rps_map, map);
742 spin_unlock(&rps_map_lock);
743
744 if (map)
745 static_key_slow_inc(&rps_needed);
746 if (old_map) {
747 kfree_rcu(old_map, rcu);
748 static_key_slow_dec(&rps_needed);
749 }
750 free_cpumask_var(mask);
751 return len;
752}
753
754static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
755 struct rx_queue_attribute *attr,
756 char *buf)
757{
758 struct rps_dev_flow_table *flow_table;
759 unsigned long val = 0;
760
761 rcu_read_lock();
762 flow_table = rcu_dereference(queue->rps_flow_table);
763 if (flow_table)
764 val = (unsigned long)flow_table->mask + 1;
765 rcu_read_unlock();
766
767 return sprintf(buf, "%lu\n", val);
768}
769
770static void rps_dev_flow_table_release(struct rcu_head *rcu)
771{
772 struct rps_dev_flow_table *table = container_of(rcu,
773 struct rps_dev_flow_table, rcu);
774 vfree(table);
775}
776
777static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
778 struct rx_queue_attribute *attr,
779 const char *buf, size_t len)
780{
781 unsigned long mask, count;
782 struct rps_dev_flow_table *table, *old_table;
783 static DEFINE_SPINLOCK(rps_dev_flow_lock);
784 int rc;
785
786 if (!capable(CAP_NET_ADMIN))
787 return -EPERM;
788
789 rc = kstrtoul(buf, 0, &count);
790 if (rc < 0)
791 return rc;
792
793 if (count) {
794 mask = count - 1;
795
796
797
798 while ((mask | (mask >> 1)) != mask)
799 mask |= (mask >> 1);
800
801
802
803
804#if BITS_PER_LONG > 32
805 if (mask > (unsigned long)(u32)mask)
806 return -EINVAL;
807#else
808 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
809 / sizeof(struct rps_dev_flow)) {
810
811 return -EINVAL;
812 }
813#endif
814 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
815 if (!table)
816 return -ENOMEM;
817
818 table->mask = mask;
819 for (count = 0; count <= mask; count++)
820 table->flows[count].cpu = RPS_NO_CPU;
821 } else
822 table = NULL;
823
824 spin_lock(&rps_dev_flow_lock);
825 old_table = rcu_dereference_protected(queue->rps_flow_table,
826 lockdep_is_held(&rps_dev_flow_lock));
827 rcu_assign_pointer(queue->rps_flow_table, table);
828 spin_unlock(&rps_dev_flow_lock);
829
830 if (old_table)
831 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
832
833 return len;
834}
835
836static struct rx_queue_attribute rps_cpus_attribute =
837 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
838
839
840static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
841 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
842 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
843
844static struct attribute *rx_queue_default_attrs[] = {
845 &rps_cpus_attribute.attr,
846 &rps_dev_flow_table_cnt_attribute.attr,
847 NULL
848};
849
850static void rx_queue_release(struct kobject *kobj)
851{
852 struct netdev_rx_queue *queue = to_rx_queue(kobj);
853 struct rps_map *map;
854 struct rps_dev_flow_table *flow_table;
855
856
857 map = rcu_dereference_protected(queue->rps_map, 1);
858 if (map) {
859 RCU_INIT_POINTER(queue->rps_map, NULL);
860 kfree_rcu(map, rcu);
861 }
862
863 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
864 if (flow_table) {
865 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
866 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
867 }
868
869 memset(kobj, 0, sizeof(*kobj));
870 dev_put(queue->dev);
871}
872
873static struct kobj_type rx_queue_ktype = {
874 .sysfs_ops = &rx_queue_sysfs_ops,
875 .release = rx_queue_release,
876 .default_attrs = rx_queue_default_attrs,
877};
878
879static int rx_queue_add_kobject(struct net_device *net, int index)
880{
881 struct netdev_rx_queue *queue = net->_rx + index;
882 struct kobject *kobj = &queue->kobj;
883 int error = 0;
884
885 kobj->kset = net->queues_kset;
886 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
887 "rx-%u", index);
888 if (error) {
889 kobject_put(kobj);
890 return error;
891 }
892
893 kobject_uevent(kobj, KOBJ_ADD);
894 dev_hold(queue->dev);
895
896 return error;
897}
898#endif
899
900int
901net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
902{
903#ifdef CONFIG_RPS
904 int i;
905 int error = 0;
906
907 for (i = old_num; i < new_num; i++) {
908 error = rx_queue_add_kobject(net, i);
909 if (error) {
910 new_num = old_num;
911 break;
912 }
913 }
914
915 while (--i >= new_num)
916 kobject_put(&net->_rx[i].kobj);
917
918 return error;
919#else
920 return 0;
921#endif
922}
923
924#ifdef CONFIG_SYSFS
925
926
927
928struct netdev_queue_attribute {
929 struct attribute attr;
930 ssize_t (*show)(struct netdev_queue *queue,
931 struct netdev_queue_attribute *attr, char *buf);
932 ssize_t (*store)(struct netdev_queue *queue,
933 struct netdev_queue_attribute *attr, const char *buf, size_t len);
934};
935#define to_netdev_queue_attr(_attr) container_of(_attr, \
936 struct netdev_queue_attribute, attr)
937
938#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
939
940static ssize_t netdev_queue_attr_show(struct kobject *kobj,
941 struct attribute *attr, char *buf)
942{
943 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
944 struct netdev_queue *queue = to_netdev_queue(kobj);
945
946 if (!attribute->show)
947 return -EIO;
948
949 return attribute->show(queue, attribute, buf);
950}
951
952static ssize_t netdev_queue_attr_store(struct kobject *kobj,
953 struct attribute *attr,
954 const char *buf, size_t count)
955{
956 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
957 struct netdev_queue *queue = to_netdev_queue(kobj);
958
959 if (!attribute->store)
960 return -EIO;
961
962 return attribute->store(queue, attribute, buf, count);
963}
964
965static const struct sysfs_ops netdev_queue_sysfs_ops = {
966 .show = netdev_queue_attr_show,
967 .store = netdev_queue_attr_store,
968};
969
970static ssize_t show_trans_timeout(struct netdev_queue *queue,
971 struct netdev_queue_attribute *attribute,
972 char *buf)
973{
974 unsigned long trans_timeout;
975
976 spin_lock_irq(&queue->_xmit_lock);
977 trans_timeout = queue->trans_timeout;
978 spin_unlock_irq(&queue->_xmit_lock);
979
980 return sprintf(buf, "%lu", trans_timeout);
981}
982
983#ifdef CONFIG_XPS
984static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
985{
986 struct net_device *dev = queue->dev;
987 unsigned int i;
988
989 i = queue - dev->_tx;
990 BUG_ON(i >= dev->num_tx_queues);
991
992 return i;
993}
994
995static ssize_t show_tx_maxrate(struct netdev_queue *queue,
996 struct netdev_queue_attribute *attribute,
997 char *buf)
998{
999 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1000}
1001
1002static ssize_t set_tx_maxrate(struct netdev_queue *queue,
1003 struct netdev_queue_attribute *attribute,
1004 const char *buf, size_t len)
1005{
1006 struct net_device *dev = queue->dev;
1007 int err, index = get_netdev_queue_index(queue);
1008 u32 rate = 0;
1009
1010 err = kstrtou32(buf, 10, &rate);
1011 if (err < 0)
1012 return err;
1013
1014 if (!rtnl_trylock())
1015 return restart_syscall();
1016
1017 err = -EOPNOTSUPP;
1018 if (get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate))
1019 err = get_ndo_ext(dev->netdev_ops, ndo_set_tx_maxrate)(dev, index, rate);
1020
1021 rtnl_unlock();
1022 if (!err) {
1023 queue->tx_maxrate = rate;
1024 return len;
1025 }
1026 return err;
1027}
1028
1029static struct netdev_queue_attribute queue_tx_maxrate =
1030 __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
1031 show_tx_maxrate, set_tx_maxrate);
1032#endif
1033
1034static struct netdev_queue_attribute queue_trans_timeout =
1035 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
1036
1037#ifdef CONFIG_BQL
1038
1039
1040
1041static ssize_t bql_show(char *buf, unsigned int value)
1042{
1043 return sprintf(buf, "%u\n", value);
1044}
1045
1046static ssize_t bql_set(const char *buf, const size_t count,
1047 unsigned int *pvalue)
1048{
1049 unsigned int value;
1050 int err;
1051
1052 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
1053 value = DQL_MAX_LIMIT;
1054 else {
1055 err = kstrtouint(buf, 10, &value);
1056 if (err < 0)
1057 return err;
1058 if (value > DQL_MAX_LIMIT)
1059 return -EINVAL;
1060 }
1061
1062 *pvalue = value;
1063
1064 return count;
1065}
1066
1067static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1068 struct netdev_queue_attribute *attr,
1069 char *buf)
1070{
1071 struct dql *dql = &queue->dql;
1072
1073 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1074}
1075
1076static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1077 struct netdev_queue_attribute *attribute,
1078 const char *buf, size_t len)
1079{
1080 struct dql *dql = &queue->dql;
1081 unsigned int value;
1082 int err;
1083
1084 err = kstrtouint(buf, 10, &value);
1085 if (err < 0)
1086 return err;
1087
1088 dql->slack_hold_time = msecs_to_jiffies(value);
1089
1090 return len;
1091}
1092
1093static struct netdev_queue_attribute bql_hold_time_attribute =
1094 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
1095 bql_set_hold_time);
1096
1097static ssize_t bql_show_inflight(struct netdev_queue *queue,
1098 struct netdev_queue_attribute *attr,
1099 char *buf)
1100{
1101 struct dql *dql = &queue->dql;
1102
1103 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1104}
1105
1106static struct netdev_queue_attribute bql_inflight_attribute =
1107 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
1108
1109#define BQL_ATTR(NAME, FIELD) \
1110static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1111 struct netdev_queue_attribute *attr, \
1112 char *buf) \
1113{ \
1114 return bql_show(buf, queue->dql.FIELD); \
1115} \
1116 \
1117static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1118 struct netdev_queue_attribute *attr, \
1119 const char *buf, size_t len) \
1120{ \
1121 return bql_set(buf, len, &queue->dql.FIELD); \
1122} \
1123 \
1124static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
1125 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
1126 bql_set_ ## NAME);
1127
1128BQL_ATTR(limit, limit)
1129BQL_ATTR(limit_max, max_limit)
1130BQL_ATTR(limit_min, min_limit)
1131
1132static struct attribute *dql_attrs[] = {
1133 &bql_limit_attribute.attr,
1134 &bql_limit_max_attribute.attr,
1135 &bql_limit_min_attribute.attr,
1136 &bql_hold_time_attribute.attr,
1137 &bql_inflight_attribute.attr,
1138 NULL
1139};
1140
1141static struct attribute_group dql_group = {
1142 .name = "byte_queue_limits",
1143 .attrs = dql_attrs,
1144};
1145#endif
1146
1147#ifdef CONFIG_XPS
1148static ssize_t show_xps_map(struct netdev_queue *queue,
1149 struct netdev_queue_attribute *attribute, char *buf)
1150{
1151 struct net_device *dev = queue->dev;
1152 struct xps_dev_maps *dev_maps;
1153 cpumask_var_t mask;
1154 unsigned long index;
1155 size_t len = 0;
1156 int i;
1157
1158 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1159 return -ENOMEM;
1160
1161 index = get_netdev_queue_index(queue);
1162
1163 rcu_read_lock();
1164 dev_maps = rcu_dereference(dev->xps_maps);
1165 if (dev_maps) {
1166 for_each_possible_cpu(i) {
1167 struct xps_map *map =
1168 rcu_dereference(dev_maps->cpu_map[i]);
1169 if (map) {
1170 int j;
1171 for (j = 0; j < map->len; j++) {
1172 if (map->queues[j] == index) {
1173 cpumask_set_cpu(i, mask);
1174 break;
1175 }
1176 }
1177 }
1178 }
1179 }
1180 rcu_read_unlock();
1181
1182 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1183 if (PAGE_SIZE - len < 3) {
1184 free_cpumask_var(mask);
1185 return -EINVAL;
1186 }
1187
1188 free_cpumask_var(mask);
1189 len += sprintf(buf + len, "\n");
1190 return len;
1191}
1192
1193static ssize_t store_xps_map(struct netdev_queue *queue,
1194 struct netdev_queue_attribute *attribute,
1195 const char *buf, size_t len)
1196{
1197 struct net_device *dev = queue->dev;
1198 unsigned long index;
1199 cpumask_var_t mask;
1200 int err;
1201
1202 if (!capable(CAP_NET_ADMIN))
1203 return -EPERM;
1204
1205 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1206 return -ENOMEM;
1207
1208 index = get_netdev_queue_index(queue);
1209
1210 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1211 if (err) {
1212 free_cpumask_var(mask);
1213 return err;
1214 }
1215
1216 err = netif_set_xps_queue(dev, mask, index);
1217
1218 free_cpumask_var(mask);
1219
1220 return err ? : len;
1221}
1222
1223static struct netdev_queue_attribute xps_cpus_attribute =
1224 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1225#endif
1226
1227static struct attribute *netdev_queue_default_attrs[] = {
1228 &queue_trans_timeout.attr,
1229#ifdef CONFIG_XPS
1230 &xps_cpus_attribute.attr,
1231 &queue_tx_maxrate.attr,
1232#endif
1233 NULL
1234};
1235
1236static void netdev_queue_release(struct kobject *kobj)
1237{
1238 struct netdev_queue *queue = to_netdev_queue(kobj);
1239
1240 memset(kobj, 0, sizeof(*kobj));
1241 dev_put(queue->dev);
1242}
1243
1244static struct kobj_type netdev_queue_ktype = {
1245 .sysfs_ops = &netdev_queue_sysfs_ops,
1246 .release = netdev_queue_release,
1247 .default_attrs = netdev_queue_default_attrs,
1248};
1249
1250static int netdev_queue_add_kobject(struct net_device *net, int index)
1251{
1252 struct netdev_queue *queue = net->_tx + index;
1253 struct kobject *kobj = &queue->kobj;
1254 int error = 0;
1255
1256 kobj->kset = net->queues_kset;
1257 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1258 "tx-%u", index);
1259 if (error)
1260 goto exit;
1261
1262#ifdef CONFIG_BQL
1263 error = sysfs_create_group(kobj, &dql_group);
1264 if (error)
1265 goto exit;
1266#endif
1267
1268 kobject_uevent(kobj, KOBJ_ADD);
1269 dev_hold(queue->dev);
1270
1271 return 0;
1272exit:
1273 kobject_put(kobj);
1274 return error;
1275}
1276#endif
1277
1278int
1279netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1280{
1281#ifdef CONFIG_SYSFS
1282 int i;
1283 int error = 0;
1284
1285 for (i = old_num; i < new_num; i++) {
1286 error = netdev_queue_add_kobject(net, i);
1287 if (error) {
1288 new_num = old_num;
1289 break;
1290 }
1291 }
1292
1293 while (--i >= new_num) {
1294 struct netdev_queue *queue = net->_tx + i;
1295
1296#ifdef CONFIG_BQL
1297 sysfs_remove_group(&queue->kobj, &dql_group);
1298#endif
1299 kobject_put(&queue->kobj);
1300 }
1301
1302 return error;
1303#else
1304 return 0;
1305#endif
1306}
1307
1308static int register_queue_kobjects(struct net_device *net)
1309{
1310 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1311
1312#ifdef CONFIG_SYSFS
1313 net->queues_kset = kset_create_and_add("queues",
1314 NULL, &net->dev.kobj);
1315 if (!net->queues_kset)
1316 return -ENOMEM;
1317#endif
1318
1319#ifdef CONFIG_RPS
1320 real_rx = net->real_num_rx_queues;
1321#endif
1322 real_tx = net->real_num_tx_queues;
1323
1324 error = net_rx_queue_update_kobjects(net, 0, real_rx);
1325 if (error)
1326 goto error;
1327 rxq = real_rx;
1328
1329 error = netdev_queue_update_kobjects(net, 0, real_tx);
1330 if (error)
1331 goto error;
1332 txq = real_tx;
1333
1334 return 0;
1335
1336error:
1337 netdev_queue_update_kobjects(net, txq, 0);
1338 net_rx_queue_update_kobjects(net, rxq, 0);
1339 return error;
1340}
1341
1342static void remove_queue_kobjects(struct net_device *net)
1343{
1344 int real_rx = 0, real_tx = 0;
1345
1346#ifdef CONFIG_RPS
1347 real_rx = net->real_num_rx_queues;
1348#endif
1349 real_tx = net->real_num_tx_queues;
1350
1351 net_rx_queue_update_kobjects(net, real_rx, 0);
1352 netdev_queue_update_kobjects(net, real_tx, 0);
1353#ifdef CONFIG_SYSFS
1354 kset_unregister(net->queues_kset);
1355#endif
1356}
1357
1358static void *net_grab_current_ns(void)
1359{
1360 struct net *ns = current->nsproxy->net_ns;
1361#ifdef CONFIG_NET_NS
1362 if (ns)
1363 atomic_inc(&ns->passive);
1364#endif
1365 return ns;
1366}
1367
1368static const void *net_initial_ns(void)
1369{
1370 return &init_net;
1371}
1372
1373static const void *net_netlink_ns(struct sock *sk)
1374{
1375 return sock_net(sk);
1376}
1377
1378struct kobj_ns_type_operations net_ns_type_operations = {
1379 .type = KOBJ_NS_TYPE_NET,
1380 .grab_current_ns = net_grab_current_ns,
1381 .netlink_ns = net_netlink_ns,
1382 .initial_ns = net_initial_ns,
1383 .drop_ns = net_drop_ns,
1384};
1385EXPORT_SYMBOL_GPL(net_ns_type_operations);
1386
1387static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1388{
1389 struct net_device *dev = to_net_dev(d);
1390 int retval;
1391
1392
1393 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1394 if (retval)
1395 goto exit;
1396
1397
1398
1399
1400 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1401
1402exit:
1403 return retval;
1404}
1405
1406
1407
1408
1409
1410static void netdev_release(struct device *d)
1411{
1412 struct net_device *dev = to_net_dev(d);
1413
1414 BUG_ON(dev->reg_state != NETREG_RELEASED);
1415
1416 kfree(dev->ifalias);
1417 netdev_freemem(dev);
1418}
1419
1420static const void *net_namespace(struct device *d)
1421{
1422 struct net_device *dev;
1423 dev = container_of(d, struct net_device, dev);
1424 return dev_net(dev);
1425}
1426
1427static struct class net_class = {
1428 .name = "net",
1429 .dev_release = netdev_release,
1430 .dev_groups = net_class_groups,
1431 .dev_uevent = netdev_uevent,
1432 .ns_type = &net_ns_type_operations,
1433 .namespace = net_namespace,
1434};
1435
1436
1437
1438
1439void netdev_unregister_kobject(struct net_device * net)
1440{
1441 struct device *dev = &(net->dev);
1442
1443 kobject_get(&dev->kobj);
1444
1445 remove_queue_kobjects(net);
1446
1447 pm_runtime_set_memalloc_noio(dev, false);
1448
1449 device_del(dev);
1450}
1451
1452
1453int netdev_register_kobject(struct net_device *net)
1454{
1455 struct device *dev = &(net->dev);
1456 const struct attribute_group **groups = net->sysfs_groups;
1457 int error = 0;
1458
1459 device_initialize(dev);
1460 dev->class = &net_class;
1461 dev->platform_data = net;
1462 dev->groups = groups;
1463
1464 dev_set_name(dev, "%s", net->name);
1465
1466#ifdef CONFIG_SYSFS
1467
1468 if (*groups)
1469 groups++;
1470
1471 *groups++ = &netstat_group;
1472
1473#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1474 if (net->ieee80211_ptr)
1475 *groups++ = &wireless_group;
1476#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1477 else if (net->wireless_handlers)
1478 *groups++ = &wireless_group;
1479#endif
1480#endif
1481#endif
1482
1483 error = device_add(dev);
1484 if (error)
1485 return error;
1486
1487 error = register_queue_kobjects(net);
1488 if (error) {
1489 device_del(dev);
1490 return error;
1491 }
1492
1493 pm_runtime_set_memalloc_noio(dev, true);
1494
1495 return error;
1496}
1497
1498int netdev_class_create_file(struct class_attribute *class_attr)
1499{
1500 return class_create_file(&net_class, class_attr);
1501}
1502EXPORT_SYMBOL(netdev_class_create_file);
1503
1504void netdev_class_remove_file(struct class_attribute *class_attr)
1505{
1506 class_remove_file(&net_class, class_attr);
1507}
1508EXPORT_SYMBOL(netdev_class_remove_file);
1509
1510int netdev_kobject_init(void)
1511{
1512 kobj_ns_type_register(&net_ns_type_operations);
1513 return class_register(&net_class);
1514}
1515