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