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