xref: /openbmc/linux/net/core/net-sysfs.c (revision 1c2b0f08)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net-sysfs.c - network device class and attributes
4  *
5  * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
6  */
7 
8 #include <linux/capability.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sched/isolation.h>
15 #include <linux/nsproxy.h>
16 #include <net/sock.h>
17 #include <net/net_namespace.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/vmalloc.h>
20 #include <linux/export.h>
21 #include <linux/jiffies.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of.h>
24 #include <linux/of_net.h>
25 #include <linux/cpu.h>
26 #include <net/netdev_rx_queue.h>
27 
28 #include "dev.h"
29 #include "net-sysfs.h"
30 
31 #ifdef CONFIG_SYSFS
32 static const char fmt_hex[] = "%#x\n";
33 static const char fmt_dec[] = "%d\n";
34 static const char fmt_ulong[] = "%lu\n";
35 static const char fmt_u64[] = "%llu\n";
36 
37 /* Caller holds RTNL or dev_base_lock */
dev_isalive(const struct net_device * dev)38 static inline int dev_isalive(const struct net_device *dev)
39 {
40 	return dev->reg_state <= NETREG_REGISTERED;
41 }
42 
43 /* 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 *))44 static ssize_t netdev_show(const struct device *dev,
45 			   struct device_attribute *attr, char *buf,
46 			   ssize_t (*format)(const struct net_device *, char *))
47 {
48 	struct net_device *ndev = to_net_dev(dev);
49 	ssize_t ret = -EINVAL;
50 
51 	read_lock(&dev_base_lock);
52 	if (dev_isalive(ndev))
53 		ret = (*format)(ndev, buf);
54 	read_unlock(&dev_base_lock);
55 
56 	return ret;
57 }
58 
59 /* generate a show function for simple field */
60 #define NETDEVICE_SHOW(field, format_string)				\
61 static ssize_t format_##field(const struct net_device *dev, char *buf)	\
62 {									\
63 	return sysfs_emit(buf, format_string, dev->field);		\
64 }									\
65 static ssize_t field##_show(struct device *dev,				\
66 			    struct device_attribute *attr, char *buf)	\
67 {									\
68 	return netdev_show(dev, attr, buf, format_##field);		\
69 }									\
70 
71 #define NETDEVICE_SHOW_RO(field, format_string)				\
72 NETDEVICE_SHOW(field, format_string);					\
73 static DEVICE_ATTR_RO(field)
74 
75 #define NETDEVICE_SHOW_RW(field, format_string)				\
76 NETDEVICE_SHOW(field, format_string);					\
77 static DEVICE_ATTR_RW(field)
78 
79 /* use same locking and permission rules as SIF* ioctl's */
netdev_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len,int (* set)(struct net_device *,unsigned long))80 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
81 			    const char *buf, size_t len,
82 			    int (*set)(struct net_device *, unsigned long))
83 {
84 	struct net_device *netdev = to_net_dev(dev);
85 	struct net *net = dev_net(netdev);
86 	unsigned long new;
87 	int ret;
88 
89 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
90 		return -EPERM;
91 
92 	ret = kstrtoul(buf, 0, &new);
93 	if (ret)
94 		goto err;
95 
96 	if (!rtnl_trylock())
97 		return restart_syscall();
98 
99 	if (dev_isalive(netdev)) {
100 		ret = (*set)(netdev, new);
101 		if (ret == 0)
102 			ret = len;
103 	}
104 	rtnl_unlock();
105  err:
106 	return ret;
107 }
108 
109 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
110 NETDEVICE_SHOW_RO(dev_port, fmt_dec);
111 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
112 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
113 NETDEVICE_SHOW_RO(ifindex, fmt_dec);
114 NETDEVICE_SHOW_RO(type, fmt_dec);
115 NETDEVICE_SHOW_RO(link_mode, fmt_dec);
116 
iflink_show(struct device * dev,struct device_attribute * attr,char * buf)117 static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
118 			   char *buf)
119 {
120 	struct net_device *ndev = to_net_dev(dev);
121 
122 	return sysfs_emit(buf, fmt_dec, dev_get_iflink(ndev));
123 }
124 static DEVICE_ATTR_RO(iflink);
125 
format_name_assign_type(const struct net_device * dev,char * buf)126 static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
127 {
128 	return sysfs_emit(buf, fmt_dec, dev->name_assign_type);
129 }
130 
name_assign_type_show(struct device * dev,struct device_attribute * attr,char * buf)131 static ssize_t name_assign_type_show(struct device *dev,
132 				     struct device_attribute *attr,
133 				     char *buf)
134 {
135 	struct net_device *ndev = to_net_dev(dev);
136 	ssize_t ret = -EINVAL;
137 
138 	if (ndev->name_assign_type != NET_NAME_UNKNOWN)
139 		ret = netdev_show(dev, attr, buf, format_name_assign_type);
140 
141 	return ret;
142 }
143 static DEVICE_ATTR_RO(name_assign_type);
144 
145 /* use same locking rules as GIFHWADDR ioctl's */
address_show(struct device * dev,struct device_attribute * attr,char * buf)146 static ssize_t address_show(struct device *dev, struct device_attribute *attr,
147 			    char *buf)
148 {
149 	struct net_device *ndev = to_net_dev(dev);
150 	ssize_t ret = -EINVAL;
151 
152 	read_lock(&dev_base_lock);
153 	if (dev_isalive(ndev))
154 		ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
155 	read_unlock(&dev_base_lock);
156 	return ret;
157 }
158 static DEVICE_ATTR_RO(address);
159 
broadcast_show(struct device * dev,struct device_attribute * attr,char * buf)160 static ssize_t broadcast_show(struct device *dev,
161 			      struct device_attribute *attr, char *buf)
162 {
163 	struct net_device *ndev = to_net_dev(dev);
164 
165 	if (dev_isalive(ndev))
166 		return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
167 	return -EINVAL;
168 }
169 static DEVICE_ATTR_RO(broadcast);
170 
change_carrier(struct net_device * dev,unsigned long new_carrier)171 static int change_carrier(struct net_device *dev, unsigned long new_carrier)
172 {
173 	if (!netif_running(dev))
174 		return -EINVAL;
175 	return dev_change_carrier(dev, (bool)new_carrier);
176 }
177 
carrier_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)178 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
179 			     const char *buf, size_t len)
180 {
181 	struct net_device *netdev = to_net_dev(dev);
182 
183 	/* The check is also done in change_carrier; this helps returning early
184 	 * without hitting the trylock/restart in netdev_store.
185 	 */
186 	if (!netdev->netdev_ops->ndo_change_carrier)
187 		return -EOPNOTSUPP;
188 
189 	return netdev_store(dev, attr, buf, len, change_carrier);
190 }
191 
carrier_show(struct device * dev,struct device_attribute * attr,char * buf)192 static ssize_t carrier_show(struct device *dev,
193 			    struct device_attribute *attr, char *buf)
194 {
195 	struct net_device *netdev = to_net_dev(dev);
196 
197 	if (netif_running(netdev))
198 		return sysfs_emit(buf, fmt_dec, !!netif_carrier_ok(netdev));
199 
200 	return -EINVAL;
201 }
202 static DEVICE_ATTR_RW(carrier);
203 
speed_show(struct device * dev,struct device_attribute * attr,char * buf)204 static ssize_t speed_show(struct device *dev,
205 			  struct device_attribute *attr, char *buf)
206 {
207 	struct net_device *netdev = to_net_dev(dev);
208 	int ret = -EINVAL;
209 
210 	/* The check is also done in __ethtool_get_link_ksettings; this helps
211 	 * returning early without hitting the trylock/restart below.
212 	 */
213 	if (!netdev->ethtool_ops->get_link_ksettings)
214 		return ret;
215 
216 	if (!rtnl_trylock())
217 		return restart_syscall();
218 
219 	if (netif_running(netdev) && netif_device_present(netdev)) {
220 		struct ethtool_link_ksettings cmd;
221 
222 		if (!__ethtool_get_link_ksettings(netdev, &cmd))
223 			ret = sysfs_emit(buf, fmt_dec, cmd.base.speed);
224 	}
225 	rtnl_unlock();
226 	return ret;
227 }
228 static DEVICE_ATTR_RO(speed);
229 
duplex_show(struct device * dev,struct device_attribute * attr,char * buf)230 static ssize_t duplex_show(struct device *dev,
231 			   struct device_attribute *attr, char *buf)
232 {
233 	struct net_device *netdev = to_net_dev(dev);
234 	int ret = -EINVAL;
235 
236 	/* The check is also done in __ethtool_get_link_ksettings; this helps
237 	 * returning early without hitting the trylock/restart below.
238 	 */
239 	if (!netdev->ethtool_ops->get_link_ksettings)
240 		return ret;
241 
242 	if (!rtnl_trylock())
243 		return restart_syscall();
244 
245 	if (netif_running(netdev)) {
246 		struct ethtool_link_ksettings cmd;
247 
248 		if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
249 			const char *duplex;
250 
251 			switch (cmd.base.duplex) {
252 			case DUPLEX_HALF:
253 				duplex = "half";
254 				break;
255 			case DUPLEX_FULL:
256 				duplex = "full";
257 				break;
258 			default:
259 				duplex = "unknown";
260 				break;
261 			}
262 			ret = sysfs_emit(buf, "%s\n", duplex);
263 		}
264 	}
265 	rtnl_unlock();
266 	return ret;
267 }
268 static DEVICE_ATTR_RO(duplex);
269 
testing_show(struct device * dev,struct device_attribute * attr,char * buf)270 static ssize_t testing_show(struct device *dev,
271 			    struct device_attribute *attr, char *buf)
272 {
273 	struct net_device *netdev = to_net_dev(dev);
274 
275 	if (netif_running(netdev))
276 		return sysfs_emit(buf, fmt_dec, !!netif_testing(netdev));
277 
278 	return -EINVAL;
279 }
280 static DEVICE_ATTR_RO(testing);
281 
dormant_show(struct device * dev,struct device_attribute * attr,char * buf)282 static ssize_t dormant_show(struct device *dev,
283 			    struct device_attribute *attr, char *buf)
284 {
285 	struct net_device *netdev = to_net_dev(dev);
286 
287 	if (netif_running(netdev))
288 		return sysfs_emit(buf, fmt_dec, !!netif_dormant(netdev));
289 
290 	return -EINVAL;
291 }
292 static DEVICE_ATTR_RO(dormant);
293 
294 static const char *const operstates[] = {
295 	"unknown",
296 	"notpresent", /* currently unused */
297 	"down",
298 	"lowerlayerdown",
299 	"testing",
300 	"dormant",
301 	"up"
302 };
303 
operstate_show(struct device * dev,struct device_attribute * attr,char * buf)304 static ssize_t operstate_show(struct device *dev,
305 			      struct device_attribute *attr, char *buf)
306 {
307 	const struct net_device *netdev = to_net_dev(dev);
308 	unsigned char operstate;
309 
310 	operstate = READ_ONCE(netdev->operstate);
311 	if (!netif_running(netdev))
312 		operstate = IF_OPER_DOWN;
313 
314 	if (operstate >= ARRAY_SIZE(operstates))
315 		return -EINVAL; /* should not happen */
316 
317 	return sysfs_emit(buf, "%s\n", operstates[operstate]);
318 }
319 static DEVICE_ATTR_RO(operstate);
320 
carrier_changes_show(struct device * dev,struct device_attribute * attr,char * buf)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 sysfs_emit(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 
carrier_up_count_show(struct device * dev,struct device_attribute * attr,char * buf)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 sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
340 }
341 static DEVICE_ATTR_RO(carrier_up_count);
342 
carrier_down_count_show(struct device * dev,struct device_attribute * attr,char * buf)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 sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
350 }
351 static DEVICE_ATTR_RO(carrier_down_count);
352 
353 /* read-write attributes */
354 
change_mtu(struct net_device * dev,unsigned long new_mtu)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 
mtu_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
change_flags(struct net_device * dev,unsigned long new_flags)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 
flags_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
tx_queue_len_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
change_gro_flush_timeout(struct net_device * dev,unsigned long val)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 
gro_flush_timeout_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
change_napi_defer_hard_irqs(struct net_device * dev,unsigned long val)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 
napi_defer_hard_irqs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
ifalias_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
ifalias_show(struct device * dev,struct device_attribute * attr,char * buf)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 = sysfs_emit(buf, "%s\n", tmp);
465 	return ret;
466 }
467 static DEVICE_ATTR_RW(ifalias);
468 
change_group(struct net_device * dev,unsigned long new_group)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 
group_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
change_proto_down(struct net_device * dev,unsigned long proto_down)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 
proto_down_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 
phys_port_id_show(struct device * dev,struct device_attribute * attr,char * buf)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 = sysfs_emit(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 
phys_port_name_show(struct device * dev,struct device_attribute * attr,char * buf)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->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 = sysfs_emit(buf, "%s\n", name);
546 	}
547 	rtnl_unlock();
548 
549 	return ret;
550 }
551 static DEVICE_ATTR_RO(phys_port_name);
552 
phys_switch_id_show(struct device * dev,struct device_attribute * attr,char * buf)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->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 = sysfs_emit(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 
threaded_show(struct device * dev,struct device_attribute * attr,char * buf)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 = sysfs_emit(buf, fmt_dec, netdev->threaded);
594 
595 	rtnl_unlock();
596 	return ret;
597 }
598 
modify_napi_threaded(struct net_device * dev,unsigned long val)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 
threaded_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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 */
netstat_show(const struct device * d,struct device_attribute * attr,char * buf,unsigned long offset)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 = sysfs_emit(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 
wireless_group_needed(struct net_device * ndev)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 
rx_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)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 
rx_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)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
show_rps_map(struct netdev_rx_queue * queue,char * buf)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 = sysfs_emit(buf, "%*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 
netdev_rx_queue_set_rps_mask(struct netdev_rx_queue * queue,cpumask_var_t mask)833 static int netdev_rx_queue_set_rps_mask(struct netdev_rx_queue *queue,
834 					cpumask_var_t mask)
835 {
836 	static DEFINE_MUTEX(rps_map_mutex);
837 	struct rps_map *old_map, *map;
838 	int cpu, i;
839 
840 	map = kzalloc(max_t(unsigned int,
841 			    RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
842 		      GFP_KERNEL);
843 	if (!map)
844 		return -ENOMEM;
845 
846 	i = 0;
847 	for_each_cpu_and(cpu, mask, cpu_online_mask)
848 		map->cpus[i++] = cpu;
849 
850 	if (i) {
851 		map->len = i;
852 	} else {
853 		kfree(map);
854 		map = NULL;
855 	}
856 
857 	mutex_lock(&rps_map_mutex);
858 	old_map = rcu_dereference_protected(queue->rps_map,
859 					    mutex_is_locked(&rps_map_mutex));
860 	rcu_assign_pointer(queue->rps_map, map);
861 
862 	if (map)
863 		static_branch_inc(&rps_needed);
864 	if (old_map)
865 		static_branch_dec(&rps_needed);
866 
867 	mutex_unlock(&rps_map_mutex);
868 
869 	if (old_map)
870 		kfree_rcu(old_map, rcu);
871 	return 0;
872 }
873 
rps_cpumask_housekeeping(struct cpumask * mask)874 int rps_cpumask_housekeeping(struct cpumask *mask)
875 {
876 	if (!cpumask_empty(mask)) {
877 		cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN));
878 		cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ));
879 		if (cpumask_empty(mask))
880 			return -EINVAL;
881 	}
882 	return 0;
883 }
884 
store_rps_map(struct netdev_rx_queue * queue,const char * buf,size_t len)885 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
886 			     const char *buf, size_t len)
887 {
888 	cpumask_var_t mask;
889 	int err;
890 
891 	if (!capable(CAP_NET_ADMIN))
892 		return -EPERM;
893 
894 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
895 		return -ENOMEM;
896 
897 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
898 	if (err)
899 		goto out;
900 
901 	err = rps_cpumask_housekeeping(mask);
902 	if (err)
903 		goto out;
904 
905 	err = netdev_rx_queue_set_rps_mask(queue, mask);
906 
907 out:
908 	free_cpumask_var(mask);
909 	return err ? : len;
910 }
911 
show_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,char * buf)912 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
913 					   char *buf)
914 {
915 	struct rps_dev_flow_table *flow_table;
916 	unsigned long val = 0;
917 
918 	rcu_read_lock();
919 	flow_table = rcu_dereference(queue->rps_flow_table);
920 	if (flow_table)
921 		val = (unsigned long)flow_table->mask + 1;
922 	rcu_read_unlock();
923 
924 	return sysfs_emit(buf, "%lu\n", val);
925 }
926 
rps_dev_flow_table_release(struct rcu_head * rcu)927 static void rps_dev_flow_table_release(struct rcu_head *rcu)
928 {
929 	struct rps_dev_flow_table *table = container_of(rcu,
930 	    struct rps_dev_flow_table, rcu);
931 	vfree(table);
932 }
933 
store_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,const char * buf,size_t len)934 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
935 					    const char *buf, size_t len)
936 {
937 	unsigned long mask, count;
938 	struct rps_dev_flow_table *table, *old_table;
939 	static DEFINE_SPINLOCK(rps_dev_flow_lock);
940 	int rc;
941 
942 	if (!capable(CAP_NET_ADMIN))
943 		return -EPERM;
944 
945 	rc = kstrtoul(buf, 0, &count);
946 	if (rc < 0)
947 		return rc;
948 
949 	if (count) {
950 		mask = count - 1;
951 		/* mask = roundup_pow_of_two(count) - 1;
952 		 * without overflows...
953 		 */
954 		while ((mask | (mask >> 1)) != mask)
955 			mask |= (mask >> 1);
956 		/* On 64 bit arches, must check mask fits in table->mask (u32),
957 		 * and on 32bit arches, must check
958 		 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
959 		 */
960 #if BITS_PER_LONG > 32
961 		if (mask > (unsigned long)(u32)mask)
962 			return -EINVAL;
963 #else
964 		if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
965 				/ sizeof(struct rps_dev_flow)) {
966 			/* Enforce a limit to prevent overflow */
967 			return -EINVAL;
968 		}
969 #endif
970 		table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
971 		if (!table)
972 			return -ENOMEM;
973 
974 		table->mask = mask;
975 		for (count = 0; count <= mask; count++)
976 			table->flows[count].cpu = RPS_NO_CPU;
977 	} else {
978 		table = NULL;
979 	}
980 
981 	spin_lock(&rps_dev_flow_lock);
982 	old_table = rcu_dereference_protected(queue->rps_flow_table,
983 					      lockdep_is_held(&rps_dev_flow_lock));
984 	rcu_assign_pointer(queue->rps_flow_table, table);
985 	spin_unlock(&rps_dev_flow_lock);
986 
987 	if (old_table)
988 		call_rcu(&old_table->rcu, rps_dev_flow_table_release);
989 
990 	return len;
991 }
992 
993 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
994 	= __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
995 
996 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
997 	= __ATTR(rps_flow_cnt, 0644,
998 		 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
999 #endif /* CONFIG_RPS */
1000 
1001 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
1002 #ifdef CONFIG_RPS
1003 	&rps_cpus_attribute.attr,
1004 	&rps_dev_flow_table_cnt_attribute.attr,
1005 #endif
1006 	NULL
1007 };
1008 ATTRIBUTE_GROUPS(rx_queue_default);
1009 
rx_queue_release(struct kobject * kobj)1010 static void rx_queue_release(struct kobject *kobj)
1011 {
1012 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1013 #ifdef CONFIG_RPS
1014 	struct rps_map *map;
1015 	struct rps_dev_flow_table *flow_table;
1016 
1017 	map = rcu_dereference_protected(queue->rps_map, 1);
1018 	if (map) {
1019 		RCU_INIT_POINTER(queue->rps_map, NULL);
1020 		kfree_rcu(map, rcu);
1021 	}
1022 
1023 	flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
1024 	if (flow_table) {
1025 		RCU_INIT_POINTER(queue->rps_flow_table, NULL);
1026 		call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
1027 	}
1028 #endif
1029 
1030 	memset(kobj, 0, sizeof(*kobj));
1031 	netdev_put(queue->dev, &queue->dev_tracker);
1032 }
1033 
rx_queue_namespace(const struct kobject * kobj)1034 static const void *rx_queue_namespace(const struct kobject *kobj)
1035 {
1036 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1037 	struct device *dev = &queue->dev->dev;
1038 	const void *ns = NULL;
1039 
1040 	if (dev->class && dev->class->ns_type)
1041 		ns = dev->class->namespace(dev);
1042 
1043 	return ns;
1044 }
1045 
rx_queue_get_ownership(const struct kobject * kobj,kuid_t * uid,kgid_t * gid)1046 static void rx_queue_get_ownership(const struct kobject *kobj,
1047 				   kuid_t *uid, kgid_t *gid)
1048 {
1049 	const struct net *net = rx_queue_namespace(kobj);
1050 
1051 	net_ns_get_ownership(net, uid, gid);
1052 }
1053 
1054 static const struct kobj_type rx_queue_ktype = {
1055 	.sysfs_ops = &rx_queue_sysfs_ops,
1056 	.release = rx_queue_release,
1057 	.default_groups = rx_queue_default_groups,
1058 	.namespace = rx_queue_namespace,
1059 	.get_ownership = rx_queue_get_ownership,
1060 };
1061 
rx_queue_default_mask(struct net_device * dev,struct netdev_rx_queue * queue)1062 static int rx_queue_default_mask(struct net_device *dev,
1063 				 struct netdev_rx_queue *queue)
1064 {
1065 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL)
1066 	struct cpumask *rps_default_mask = READ_ONCE(dev_net(dev)->core.rps_default_mask);
1067 
1068 	if (rps_default_mask && !cpumask_empty(rps_default_mask))
1069 		return netdev_rx_queue_set_rps_mask(queue, rps_default_mask);
1070 #endif
1071 	return 0;
1072 }
1073 
rx_queue_add_kobject(struct net_device * dev,int index)1074 static int rx_queue_add_kobject(struct net_device *dev, int index)
1075 {
1076 	struct netdev_rx_queue *queue = dev->_rx + index;
1077 	struct kobject *kobj = &queue->kobj;
1078 	int error = 0;
1079 
1080 	/* Kobject_put later will trigger rx_queue_release call which
1081 	 * decreases dev refcount: Take that reference here
1082 	 */
1083 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1084 
1085 	kobj->kset = dev->queues_kset;
1086 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1087 				     "rx-%u", index);
1088 	if (error)
1089 		goto err;
1090 
1091 	if (dev->sysfs_rx_queue_group) {
1092 		error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1093 		if (error)
1094 			goto err;
1095 	}
1096 
1097 	error = rx_queue_default_mask(dev, queue);
1098 	if (error)
1099 		goto err;
1100 
1101 	kobject_uevent(kobj, KOBJ_ADD);
1102 
1103 	return error;
1104 
1105 err:
1106 	kobject_put(kobj);
1107 	return error;
1108 }
1109 
rx_queue_change_owner(struct net_device * dev,int index,kuid_t kuid,kgid_t kgid)1110 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1111 				 kgid_t kgid)
1112 {
1113 	struct netdev_rx_queue *queue = dev->_rx + index;
1114 	struct kobject *kobj = &queue->kobj;
1115 	int error;
1116 
1117 	error = sysfs_change_owner(kobj, kuid, kgid);
1118 	if (error)
1119 		return error;
1120 
1121 	if (dev->sysfs_rx_queue_group)
1122 		error = sysfs_group_change_owner(
1123 			kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1124 
1125 	return error;
1126 }
1127 #endif /* CONFIG_SYSFS */
1128 
1129 int
net_rx_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1130 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1131 {
1132 #ifdef CONFIG_SYSFS
1133 	int i;
1134 	int error = 0;
1135 
1136 #ifndef CONFIG_RPS
1137 	if (!dev->sysfs_rx_queue_group)
1138 		return 0;
1139 #endif
1140 	for (i = old_num; i < new_num; i++) {
1141 		error = rx_queue_add_kobject(dev, i);
1142 		if (error) {
1143 			new_num = old_num;
1144 			break;
1145 		}
1146 	}
1147 
1148 	while (--i >= new_num) {
1149 		struct kobject *kobj = &dev->_rx[i].kobj;
1150 
1151 		if (!refcount_read(&dev_net(dev)->ns.count))
1152 			kobj->uevent_suppress = 1;
1153 		if (dev->sysfs_rx_queue_group)
1154 			sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1155 		kobject_put(kobj);
1156 	}
1157 
1158 	return error;
1159 #else
1160 	return 0;
1161 #endif
1162 }
1163 
net_rx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1164 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1165 				     kuid_t kuid, kgid_t kgid)
1166 {
1167 #ifdef CONFIG_SYSFS
1168 	int error = 0;
1169 	int i;
1170 
1171 #ifndef CONFIG_RPS
1172 	if (!dev->sysfs_rx_queue_group)
1173 		return 0;
1174 #endif
1175 	for (i = 0; i < num; i++) {
1176 		error = rx_queue_change_owner(dev, i, kuid, kgid);
1177 		if (error)
1178 			break;
1179 	}
1180 
1181 	return error;
1182 #else
1183 	return 0;
1184 #endif
1185 }
1186 
1187 #ifdef CONFIG_SYSFS
1188 /*
1189  * netdev_queue sysfs structures and functions.
1190  */
1191 struct netdev_queue_attribute {
1192 	struct attribute attr;
1193 	ssize_t (*show)(struct netdev_queue *queue, char *buf);
1194 	ssize_t (*store)(struct netdev_queue *queue,
1195 			 const char *buf, size_t len);
1196 };
1197 #define to_netdev_queue_attr(_attr) \
1198 	container_of(_attr, struct netdev_queue_attribute, attr)
1199 
1200 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1201 
netdev_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1202 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1203 				      struct attribute *attr, char *buf)
1204 {
1205 	const struct netdev_queue_attribute *attribute
1206 		= to_netdev_queue_attr(attr);
1207 	struct netdev_queue *queue = to_netdev_queue(kobj);
1208 
1209 	if (!attribute->show)
1210 		return -EIO;
1211 
1212 	return attribute->show(queue, buf);
1213 }
1214 
netdev_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1215 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1216 				       struct attribute *attr,
1217 				       const char *buf, size_t count)
1218 {
1219 	const struct netdev_queue_attribute *attribute
1220 		= to_netdev_queue_attr(attr);
1221 	struct netdev_queue *queue = to_netdev_queue(kobj);
1222 
1223 	if (!attribute->store)
1224 		return -EIO;
1225 
1226 	return attribute->store(queue, buf, count);
1227 }
1228 
1229 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1230 	.show = netdev_queue_attr_show,
1231 	.store = netdev_queue_attr_store,
1232 };
1233 
tx_timeout_show(struct netdev_queue * queue,char * buf)1234 static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
1235 {
1236 	unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
1237 
1238 	return sysfs_emit(buf, fmt_ulong, trans_timeout);
1239 }
1240 
get_netdev_queue_index(struct netdev_queue * queue)1241 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1242 {
1243 	struct net_device *dev = queue->dev;
1244 	unsigned int i;
1245 
1246 	i = queue - dev->_tx;
1247 	BUG_ON(i >= dev->num_tx_queues);
1248 
1249 	return i;
1250 }
1251 
traffic_class_show(struct netdev_queue * queue,char * buf)1252 static ssize_t traffic_class_show(struct netdev_queue *queue,
1253 				  char *buf)
1254 {
1255 	struct net_device *dev = queue->dev;
1256 	int num_tc, tc;
1257 	int index;
1258 
1259 	if (!netif_is_multiqueue(dev))
1260 		return -ENOENT;
1261 
1262 	if (!rtnl_trylock())
1263 		return restart_syscall();
1264 
1265 	index = get_netdev_queue_index(queue);
1266 
1267 	/* If queue belongs to subordinate dev use its TC mapping */
1268 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1269 
1270 	num_tc = dev->num_tc;
1271 	tc = netdev_txq_to_tc(dev, index);
1272 
1273 	rtnl_unlock();
1274 
1275 	if (tc < 0)
1276 		return -EINVAL;
1277 
1278 	/* We can report the traffic class one of two ways:
1279 	 * Subordinate device traffic classes are reported with the traffic
1280 	 * class first, and then the subordinate class so for example TC0 on
1281 	 * subordinate device 2 will be reported as "0-2". If the queue
1282 	 * belongs to the root device it will be reported with just the
1283 	 * traffic class, so just "0" for TC 0 for example.
1284 	 */
1285 	return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
1286 			    sysfs_emit(buf, "%d\n", tc);
1287 }
1288 
1289 #ifdef CONFIG_XPS
tx_maxrate_show(struct netdev_queue * queue,char * buf)1290 static ssize_t tx_maxrate_show(struct netdev_queue *queue,
1291 			       char *buf)
1292 {
1293 	return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
1294 }
1295 
tx_maxrate_store(struct netdev_queue * queue,const char * buf,size_t len)1296 static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1297 				const char *buf, size_t len)
1298 {
1299 	struct net_device *dev = queue->dev;
1300 	int err, index = get_netdev_queue_index(queue);
1301 	u32 rate = 0;
1302 
1303 	if (!capable(CAP_NET_ADMIN))
1304 		return -EPERM;
1305 
1306 	/* The check is also done later; this helps returning early without
1307 	 * hitting the trylock/restart below.
1308 	 */
1309 	if (!dev->netdev_ops->ndo_set_tx_maxrate)
1310 		return -EOPNOTSUPP;
1311 
1312 	err = kstrtou32(buf, 10, &rate);
1313 	if (err < 0)
1314 		return err;
1315 
1316 	if (!rtnl_trylock())
1317 		return restart_syscall();
1318 
1319 	err = -EOPNOTSUPP;
1320 	if (dev->netdev_ops->ndo_set_tx_maxrate)
1321 		err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1322 
1323 	rtnl_unlock();
1324 	if (!err) {
1325 		queue->tx_maxrate = rate;
1326 		return len;
1327 	}
1328 	return err;
1329 }
1330 
1331 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1332 	= __ATTR_RW(tx_maxrate);
1333 #endif
1334 
1335 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1336 	= __ATTR_RO(tx_timeout);
1337 
1338 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1339 	= __ATTR_RO(traffic_class);
1340 
1341 #ifdef CONFIG_BQL
1342 /*
1343  * Byte queue limits sysfs structures and functions.
1344  */
bql_show(char * buf,unsigned int value)1345 static ssize_t bql_show(char *buf, unsigned int value)
1346 {
1347 	return sysfs_emit(buf, "%u\n", value);
1348 }
1349 
bql_set(const char * buf,const size_t count,unsigned int * pvalue)1350 static ssize_t bql_set(const char *buf, const size_t count,
1351 		       unsigned int *pvalue)
1352 {
1353 	unsigned int value;
1354 	int err;
1355 
1356 	if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1357 		value = DQL_MAX_LIMIT;
1358 	} else {
1359 		err = kstrtouint(buf, 10, &value);
1360 		if (err < 0)
1361 			return err;
1362 		if (value > DQL_MAX_LIMIT)
1363 			return -EINVAL;
1364 	}
1365 
1366 	*pvalue = value;
1367 
1368 	return count;
1369 }
1370 
bql_show_hold_time(struct netdev_queue * queue,char * buf)1371 static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1372 				  char *buf)
1373 {
1374 	struct dql *dql = &queue->dql;
1375 
1376 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1377 }
1378 
bql_set_hold_time(struct netdev_queue * queue,const char * buf,size_t len)1379 static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1380 				 const char *buf, size_t len)
1381 {
1382 	struct dql *dql = &queue->dql;
1383 	unsigned int value;
1384 	int err;
1385 
1386 	err = kstrtouint(buf, 10, &value);
1387 	if (err < 0)
1388 		return err;
1389 
1390 	dql->slack_hold_time = msecs_to_jiffies(value);
1391 
1392 	return len;
1393 }
1394 
1395 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1396 	= __ATTR(hold_time, 0644,
1397 		 bql_show_hold_time, bql_set_hold_time);
1398 
bql_show_inflight(struct netdev_queue * queue,char * buf)1399 static ssize_t bql_show_inflight(struct netdev_queue *queue,
1400 				 char *buf)
1401 {
1402 	struct dql *dql = &queue->dql;
1403 
1404 	return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
1405 }
1406 
1407 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1408 	__ATTR(inflight, 0444, bql_show_inflight, NULL);
1409 
1410 #define BQL_ATTR(NAME, FIELD)						\
1411 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue,		\
1412 				 char *buf)				\
1413 {									\
1414 	return bql_show(buf, queue->dql.FIELD);				\
1415 }									\
1416 									\
1417 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue,		\
1418 				const char *buf, size_t len)		\
1419 {									\
1420 	return bql_set(buf, len, &queue->dql.FIELD);			\
1421 }									\
1422 									\
1423 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1424 	= __ATTR(NAME, 0644,				\
1425 		 bql_show_ ## NAME, bql_set_ ## NAME)
1426 
1427 BQL_ATTR(limit, limit);
1428 BQL_ATTR(limit_max, max_limit);
1429 BQL_ATTR(limit_min, min_limit);
1430 
1431 static struct attribute *dql_attrs[] __ro_after_init = {
1432 	&bql_limit_attribute.attr,
1433 	&bql_limit_max_attribute.attr,
1434 	&bql_limit_min_attribute.attr,
1435 	&bql_hold_time_attribute.attr,
1436 	&bql_inflight_attribute.attr,
1437 	NULL
1438 };
1439 
1440 static const struct attribute_group dql_group = {
1441 	.name  = "byte_queue_limits",
1442 	.attrs  = dql_attrs,
1443 };
1444 #endif /* CONFIG_BQL */
1445 
1446 #ifdef CONFIG_XPS
xps_queue_show(struct net_device * dev,unsigned int index,int tc,char * buf,enum xps_map_type type)1447 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
1448 			      int tc, char *buf, enum xps_map_type type)
1449 {
1450 	struct xps_dev_maps *dev_maps;
1451 	unsigned long *mask;
1452 	unsigned int nr_ids;
1453 	int j, len;
1454 
1455 	rcu_read_lock();
1456 	dev_maps = rcu_dereference(dev->xps_maps[type]);
1457 
1458 	/* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
1459 	 * when dev_maps hasn't been allocated yet, to be backward compatible.
1460 	 */
1461 	nr_ids = dev_maps ? dev_maps->nr_ids :
1462 		 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
1463 
1464 	mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
1465 	if (!mask) {
1466 		rcu_read_unlock();
1467 		return -ENOMEM;
1468 	}
1469 
1470 	if (!dev_maps || tc >= dev_maps->num_tc)
1471 		goto out_no_maps;
1472 
1473 	for (j = 0; j < nr_ids; j++) {
1474 		int i, tci = j * dev_maps->num_tc + tc;
1475 		struct xps_map *map;
1476 
1477 		map = rcu_dereference(dev_maps->attr_map[tci]);
1478 		if (!map)
1479 			continue;
1480 
1481 		for (i = map->len; i--;) {
1482 			if (map->queues[i] == index) {
1483 				__set_bit(j, mask);
1484 				break;
1485 			}
1486 		}
1487 	}
1488 out_no_maps:
1489 	rcu_read_unlock();
1490 
1491 	len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids);
1492 	bitmap_free(mask);
1493 
1494 	return len < PAGE_SIZE ? len : -EINVAL;
1495 }
1496 
xps_cpus_show(struct netdev_queue * queue,char * buf)1497 static ssize_t xps_cpus_show(struct netdev_queue *queue, char *buf)
1498 {
1499 	struct net_device *dev = queue->dev;
1500 	unsigned int index;
1501 	int len, tc;
1502 
1503 	if (!netif_is_multiqueue(dev))
1504 		return -ENOENT;
1505 
1506 	index = get_netdev_queue_index(queue);
1507 
1508 	if (!rtnl_trylock())
1509 		return restart_syscall();
1510 
1511 	/* If queue belongs to subordinate dev use its map */
1512 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1513 
1514 	tc = netdev_txq_to_tc(dev, index);
1515 	if (tc < 0) {
1516 		rtnl_unlock();
1517 		return -EINVAL;
1518 	}
1519 
1520 	/* Make sure the subordinate device can't be freed */
1521 	get_device(&dev->dev);
1522 	rtnl_unlock();
1523 
1524 	len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
1525 
1526 	put_device(&dev->dev);
1527 	return len;
1528 }
1529 
xps_cpus_store(struct netdev_queue * queue,const char * buf,size_t len)1530 static ssize_t xps_cpus_store(struct netdev_queue *queue,
1531 			      const char *buf, size_t len)
1532 {
1533 	struct net_device *dev = queue->dev;
1534 	unsigned int index;
1535 	cpumask_var_t mask;
1536 	int err;
1537 
1538 	if (!netif_is_multiqueue(dev))
1539 		return -ENOENT;
1540 
1541 	if (!capable(CAP_NET_ADMIN))
1542 		return -EPERM;
1543 
1544 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1545 		return -ENOMEM;
1546 
1547 	index = get_netdev_queue_index(queue);
1548 
1549 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1550 	if (err) {
1551 		free_cpumask_var(mask);
1552 		return err;
1553 	}
1554 
1555 	if (!rtnl_trylock()) {
1556 		free_cpumask_var(mask);
1557 		return restart_syscall();
1558 	}
1559 
1560 	err = netif_set_xps_queue(dev, mask, index);
1561 	rtnl_unlock();
1562 
1563 	free_cpumask_var(mask);
1564 
1565 	return err ? : len;
1566 }
1567 
1568 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1569 	= __ATTR_RW(xps_cpus);
1570 
xps_rxqs_show(struct netdev_queue * queue,char * buf)1571 static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1572 {
1573 	struct net_device *dev = queue->dev;
1574 	unsigned int index;
1575 	int tc;
1576 
1577 	index = get_netdev_queue_index(queue);
1578 
1579 	if (!rtnl_trylock())
1580 		return restart_syscall();
1581 
1582 	tc = netdev_txq_to_tc(dev, index);
1583 	rtnl_unlock();
1584 	if (tc < 0)
1585 		return -EINVAL;
1586 
1587 	return xps_queue_show(dev, index, tc, buf, XPS_RXQS);
1588 }
1589 
xps_rxqs_store(struct netdev_queue * queue,const char * buf,size_t len)1590 static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1591 			      size_t len)
1592 {
1593 	struct net_device *dev = queue->dev;
1594 	struct net *net = dev_net(dev);
1595 	unsigned long *mask;
1596 	unsigned int index;
1597 	int err;
1598 
1599 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1600 		return -EPERM;
1601 
1602 	mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1603 	if (!mask)
1604 		return -ENOMEM;
1605 
1606 	index = get_netdev_queue_index(queue);
1607 
1608 	err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1609 	if (err) {
1610 		bitmap_free(mask);
1611 		return err;
1612 	}
1613 
1614 	if (!rtnl_trylock()) {
1615 		bitmap_free(mask);
1616 		return restart_syscall();
1617 	}
1618 
1619 	cpus_read_lock();
1620 	err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
1621 	cpus_read_unlock();
1622 
1623 	rtnl_unlock();
1624 
1625 	bitmap_free(mask);
1626 	return err ? : len;
1627 }
1628 
1629 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1630 	= __ATTR_RW(xps_rxqs);
1631 #endif /* CONFIG_XPS */
1632 
1633 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1634 	&queue_trans_timeout.attr,
1635 	&queue_traffic_class.attr,
1636 #ifdef CONFIG_XPS
1637 	&xps_cpus_attribute.attr,
1638 	&xps_rxqs_attribute.attr,
1639 	&queue_tx_maxrate.attr,
1640 #endif
1641 	NULL
1642 };
1643 ATTRIBUTE_GROUPS(netdev_queue_default);
1644 
netdev_queue_release(struct kobject * kobj)1645 static void netdev_queue_release(struct kobject *kobj)
1646 {
1647 	struct netdev_queue *queue = to_netdev_queue(kobj);
1648 
1649 	memset(kobj, 0, sizeof(*kobj));
1650 	netdev_put(queue->dev, &queue->dev_tracker);
1651 }
1652 
netdev_queue_namespace(const struct kobject * kobj)1653 static const void *netdev_queue_namespace(const struct kobject *kobj)
1654 {
1655 	struct netdev_queue *queue = to_netdev_queue(kobj);
1656 	struct device *dev = &queue->dev->dev;
1657 	const void *ns = NULL;
1658 
1659 	if (dev->class && dev->class->ns_type)
1660 		ns = dev->class->namespace(dev);
1661 
1662 	return ns;
1663 }
1664 
netdev_queue_get_ownership(const struct kobject * kobj,kuid_t * uid,kgid_t * gid)1665 static void netdev_queue_get_ownership(const struct kobject *kobj,
1666 				       kuid_t *uid, kgid_t *gid)
1667 {
1668 	const struct net *net = netdev_queue_namespace(kobj);
1669 
1670 	net_ns_get_ownership(net, uid, gid);
1671 }
1672 
1673 static const struct kobj_type netdev_queue_ktype = {
1674 	.sysfs_ops = &netdev_queue_sysfs_ops,
1675 	.release = netdev_queue_release,
1676 	.default_groups = netdev_queue_default_groups,
1677 	.namespace = netdev_queue_namespace,
1678 	.get_ownership = netdev_queue_get_ownership,
1679 };
1680 
netdev_queue_add_kobject(struct net_device * dev,int index)1681 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1682 {
1683 	struct netdev_queue *queue = dev->_tx + index;
1684 	struct kobject *kobj = &queue->kobj;
1685 	int error = 0;
1686 
1687 	/* Kobject_put later will trigger netdev_queue_release call
1688 	 * which decreases dev refcount: Take that reference here
1689 	 */
1690 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1691 
1692 	kobj->kset = dev->queues_kset;
1693 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1694 				     "tx-%u", index);
1695 	if (error)
1696 		goto err;
1697 
1698 #ifdef CONFIG_BQL
1699 	error = sysfs_create_group(kobj, &dql_group);
1700 	if (error)
1701 		goto err;
1702 #endif
1703 
1704 	kobject_uevent(kobj, KOBJ_ADD);
1705 	return 0;
1706 
1707 err:
1708 	kobject_put(kobj);
1709 	return error;
1710 }
1711 
tx_queue_change_owner(struct net_device * ndev,int index,kuid_t kuid,kgid_t kgid)1712 static int tx_queue_change_owner(struct net_device *ndev, int index,
1713 				 kuid_t kuid, kgid_t kgid)
1714 {
1715 	struct netdev_queue *queue = ndev->_tx + index;
1716 	struct kobject *kobj = &queue->kobj;
1717 	int error;
1718 
1719 	error = sysfs_change_owner(kobj, kuid, kgid);
1720 	if (error)
1721 		return error;
1722 
1723 #ifdef CONFIG_BQL
1724 	error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
1725 #endif
1726 	return error;
1727 }
1728 #endif /* CONFIG_SYSFS */
1729 
1730 int
netdev_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1731 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1732 {
1733 #ifdef CONFIG_SYSFS
1734 	int i;
1735 	int error = 0;
1736 
1737 	/* Tx queue kobjects are allowed to be updated when a device is being
1738 	 * unregistered, but solely to remove queues from qdiscs. Any path
1739 	 * adding queues should be fixed.
1740 	 */
1741 	WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
1742 	     "New queues can't be registered after device unregistration.");
1743 
1744 	for (i = old_num; i < new_num; i++) {
1745 		error = netdev_queue_add_kobject(dev, i);
1746 		if (error) {
1747 			new_num = old_num;
1748 			break;
1749 		}
1750 	}
1751 
1752 	while (--i >= new_num) {
1753 		struct netdev_queue *queue = dev->_tx + i;
1754 
1755 		if (!refcount_read(&dev_net(dev)->ns.count))
1756 			queue->kobj.uevent_suppress = 1;
1757 #ifdef CONFIG_BQL
1758 		sysfs_remove_group(&queue->kobj, &dql_group);
1759 #endif
1760 		kobject_put(&queue->kobj);
1761 	}
1762 
1763 	return error;
1764 #else
1765 	return 0;
1766 #endif /* CONFIG_SYSFS */
1767 }
1768 
net_tx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1769 static int net_tx_queue_change_owner(struct net_device *dev, int num,
1770 				     kuid_t kuid, kgid_t kgid)
1771 {
1772 #ifdef CONFIG_SYSFS
1773 	int error = 0;
1774 	int i;
1775 
1776 	for (i = 0; i < num; i++) {
1777 		error = tx_queue_change_owner(dev, i, kuid, kgid);
1778 		if (error)
1779 			break;
1780 	}
1781 
1782 	return error;
1783 #else
1784 	return 0;
1785 #endif /* CONFIG_SYSFS */
1786 }
1787 
register_queue_kobjects(struct net_device * dev)1788 static int register_queue_kobjects(struct net_device *dev)
1789 {
1790 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1791 
1792 #ifdef CONFIG_SYSFS
1793 	dev->queues_kset = kset_create_and_add("queues",
1794 					       NULL, &dev->dev.kobj);
1795 	if (!dev->queues_kset)
1796 		return -ENOMEM;
1797 	real_rx = dev->real_num_rx_queues;
1798 #endif
1799 	real_tx = dev->real_num_tx_queues;
1800 
1801 	error = net_rx_queue_update_kobjects(dev, 0, real_rx);
1802 	if (error)
1803 		goto error;
1804 	rxq = real_rx;
1805 
1806 	error = netdev_queue_update_kobjects(dev, 0, real_tx);
1807 	if (error)
1808 		goto error;
1809 	txq = real_tx;
1810 
1811 	return 0;
1812 
1813 error:
1814 	netdev_queue_update_kobjects(dev, txq, 0);
1815 	net_rx_queue_update_kobjects(dev, rxq, 0);
1816 #ifdef CONFIG_SYSFS
1817 	kset_unregister(dev->queues_kset);
1818 #endif
1819 	return error;
1820 }
1821 
queue_change_owner(struct net_device * ndev,kuid_t kuid,kgid_t kgid)1822 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
1823 {
1824 	int error = 0, real_rx = 0, real_tx = 0;
1825 
1826 #ifdef CONFIG_SYSFS
1827 	if (ndev->queues_kset) {
1828 		error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
1829 		if (error)
1830 			return error;
1831 	}
1832 	real_rx = ndev->real_num_rx_queues;
1833 #endif
1834 	real_tx = ndev->real_num_tx_queues;
1835 
1836 	error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
1837 	if (error)
1838 		return error;
1839 
1840 	error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
1841 	if (error)
1842 		return error;
1843 
1844 	return 0;
1845 }
1846 
remove_queue_kobjects(struct net_device * dev)1847 static void remove_queue_kobjects(struct net_device *dev)
1848 {
1849 	int real_rx = 0, real_tx = 0;
1850 
1851 #ifdef CONFIG_SYSFS
1852 	real_rx = dev->real_num_rx_queues;
1853 #endif
1854 	real_tx = dev->real_num_tx_queues;
1855 
1856 	net_rx_queue_update_kobjects(dev, real_rx, 0);
1857 	netdev_queue_update_kobjects(dev, real_tx, 0);
1858 
1859 	dev->real_num_rx_queues = 0;
1860 	dev->real_num_tx_queues = 0;
1861 #ifdef CONFIG_SYSFS
1862 	kset_unregister(dev->queues_kset);
1863 #endif
1864 }
1865 
net_current_may_mount(void)1866 static bool net_current_may_mount(void)
1867 {
1868 	struct net *net = current->nsproxy->net_ns;
1869 
1870 	return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1871 }
1872 
net_grab_current_ns(void)1873 static void *net_grab_current_ns(void)
1874 {
1875 	struct net *ns = current->nsproxy->net_ns;
1876 #ifdef CONFIG_NET_NS
1877 	if (ns)
1878 		refcount_inc(&ns->passive);
1879 #endif
1880 	return ns;
1881 }
1882 
net_initial_ns(void)1883 static const void *net_initial_ns(void)
1884 {
1885 	return &init_net;
1886 }
1887 
net_netlink_ns(struct sock * sk)1888 static const void *net_netlink_ns(struct sock *sk)
1889 {
1890 	return sock_net(sk);
1891 }
1892 
1893 const struct kobj_ns_type_operations net_ns_type_operations = {
1894 	.type = KOBJ_NS_TYPE_NET,
1895 	.current_may_mount = net_current_may_mount,
1896 	.grab_current_ns = net_grab_current_ns,
1897 	.netlink_ns = net_netlink_ns,
1898 	.initial_ns = net_initial_ns,
1899 	.drop_ns = net_drop_ns,
1900 };
1901 EXPORT_SYMBOL_GPL(net_ns_type_operations);
1902 
netdev_uevent(const struct device * d,struct kobj_uevent_env * env)1903 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env)
1904 {
1905 	const struct net_device *dev = to_net_dev(d);
1906 	int retval;
1907 
1908 	/* pass interface to uevent. */
1909 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1910 	if (retval)
1911 		goto exit;
1912 
1913 	/* pass ifindex to uevent.
1914 	 * ifindex is useful as it won't change (interface name may change)
1915 	 * and is what RtNetlink uses natively.
1916 	 */
1917 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1918 
1919 exit:
1920 	return retval;
1921 }
1922 
1923 /*
1924  *	netdev_release -- destroy and free a dead device.
1925  *	Called when last reference to device kobject is gone.
1926  */
netdev_release(struct device * d)1927 static void netdev_release(struct device *d)
1928 {
1929 	struct net_device *dev = to_net_dev(d);
1930 
1931 	BUG_ON(dev->reg_state != NETREG_RELEASED);
1932 
1933 	/* no need to wait for rcu grace period:
1934 	 * device is dead and about to be freed.
1935 	 */
1936 	kfree(rcu_access_pointer(dev->ifalias));
1937 	netdev_freemem(dev);
1938 }
1939 
net_namespace(const struct device * d)1940 static const void *net_namespace(const struct device *d)
1941 {
1942 	const struct net_device *dev = to_net_dev(d);
1943 
1944 	return dev_net(dev);
1945 }
1946 
net_get_ownership(const struct device * d,kuid_t * uid,kgid_t * gid)1947 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid)
1948 {
1949 	const struct net_device *dev = to_net_dev(d);
1950 	const struct net *net = dev_net(dev);
1951 
1952 	net_ns_get_ownership(net, uid, gid);
1953 }
1954 
1955 static struct class net_class __ro_after_init = {
1956 	.name = "net",
1957 	.dev_release = netdev_release,
1958 	.dev_groups = net_class_groups,
1959 	.dev_uevent = netdev_uevent,
1960 	.ns_type = &net_ns_type_operations,
1961 	.namespace = net_namespace,
1962 	.get_ownership = net_get_ownership,
1963 };
1964 
1965 #ifdef CONFIG_OF
of_dev_node_match(struct device * dev,const void * data)1966 static int of_dev_node_match(struct device *dev, const void *data)
1967 {
1968 	for (; dev; dev = dev->parent) {
1969 		if (dev->of_node == data)
1970 			return 1;
1971 	}
1972 
1973 	return 0;
1974 }
1975 
1976 /*
1977  * of_find_net_device_by_node - lookup the net device for the device node
1978  * @np: OF device node
1979  *
1980  * Looks up the net_device structure corresponding with the device node.
1981  * If successful, returns a pointer to the net_device with the embedded
1982  * struct device refcount incremented by one, or NULL on failure. The
1983  * refcount must be dropped when done with the net_device.
1984  */
of_find_net_device_by_node(struct device_node * np)1985 struct net_device *of_find_net_device_by_node(struct device_node *np)
1986 {
1987 	struct device *dev;
1988 
1989 	dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1990 	if (!dev)
1991 		return NULL;
1992 
1993 	return to_net_dev(dev);
1994 }
1995 EXPORT_SYMBOL(of_find_net_device_by_node);
1996 #endif
1997 
1998 /* Delete sysfs entries but hold kobject reference until after all
1999  * netdev references are gone.
2000  */
netdev_unregister_kobject(struct net_device * ndev)2001 void netdev_unregister_kobject(struct net_device *ndev)
2002 {
2003 	struct device *dev = &ndev->dev;
2004 
2005 	if (!refcount_read(&dev_net(ndev)->ns.count))
2006 		dev_set_uevent_suppress(dev, 1);
2007 
2008 	kobject_get(&dev->kobj);
2009 
2010 	remove_queue_kobjects(ndev);
2011 
2012 	pm_runtime_set_memalloc_noio(dev, false);
2013 
2014 	device_del(dev);
2015 }
2016 
2017 /* Create sysfs entries for network device. */
netdev_register_kobject(struct net_device * ndev)2018 int netdev_register_kobject(struct net_device *ndev)
2019 {
2020 	struct device *dev = &ndev->dev;
2021 	const struct attribute_group **groups = ndev->sysfs_groups;
2022 	int error = 0;
2023 
2024 	device_initialize(dev);
2025 	dev->class = &net_class;
2026 	dev->platform_data = ndev;
2027 	dev->groups = groups;
2028 
2029 	dev_set_name(dev, "%s", ndev->name);
2030 
2031 #ifdef CONFIG_SYSFS
2032 	/* Allow for a device specific group */
2033 	if (*groups)
2034 		groups++;
2035 
2036 	*groups++ = &netstat_group;
2037 
2038 	if (wireless_group_needed(ndev))
2039 		*groups++ = &wireless_group;
2040 #endif /* CONFIG_SYSFS */
2041 
2042 	error = device_add(dev);
2043 	if (error)
2044 		return error;
2045 
2046 	error = register_queue_kobjects(ndev);
2047 	if (error) {
2048 		device_del(dev);
2049 		return error;
2050 	}
2051 
2052 	pm_runtime_set_memalloc_noio(dev, true);
2053 
2054 	return error;
2055 }
2056 
2057 /* Change owner for sysfs entries when moving network devices across network
2058  * namespaces owned by different user namespaces.
2059  */
netdev_change_owner(struct net_device * ndev,const struct net * net_old,const struct net * net_new)2060 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2061 			const struct net *net_new)
2062 {
2063 	kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2064 	kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2065 	struct device *dev = &ndev->dev;
2066 	int error;
2067 
2068 	net_ns_get_ownership(net_old, &old_uid, &old_gid);
2069 	net_ns_get_ownership(net_new, &new_uid, &new_gid);
2070 
2071 	/* The network namespace was changed but the owning user namespace is
2072 	 * identical so there's no need to change the owner of sysfs entries.
2073 	 */
2074 	if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2075 		return 0;
2076 
2077 	error = device_change_owner(dev, new_uid, new_gid);
2078 	if (error)
2079 		return error;
2080 
2081 	error = queue_change_owner(ndev, new_uid, new_gid);
2082 	if (error)
2083 		return error;
2084 
2085 	return 0;
2086 }
2087 
netdev_class_create_file_ns(const struct class_attribute * class_attr,const void * ns)2088 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2089 				const void *ns)
2090 {
2091 	return class_create_file_ns(&net_class, class_attr, ns);
2092 }
2093 EXPORT_SYMBOL(netdev_class_create_file_ns);
2094 
netdev_class_remove_file_ns(const struct class_attribute * class_attr,const void * ns)2095 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2096 				 const void *ns)
2097 {
2098 	class_remove_file_ns(&net_class, class_attr, ns);
2099 }
2100 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2101 
netdev_kobject_init(void)2102 int __init netdev_kobject_init(void)
2103 {
2104 	kobj_ns_type_register(&net_ns_type_operations);
2105 	return class_register(&net_class);
2106 }
2107