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