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 (ndev->ieee80211_ptr)
761 return true;
762 if (ndev->wireless_handlers)
763 return true;
764 return false;
765 }
766
767 #else /* CONFIG_SYSFS */
768 #define net_class_groups NULL
769 #endif /* CONFIG_SYSFS */
770
771 #ifdef CONFIG_SYSFS
772 #define to_rx_queue_attr(_attr) \
773 container_of(_attr, struct rx_queue_attribute, attr)
774
775 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
776
rx_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)777 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
778 char *buf)
779 {
780 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
781 struct netdev_rx_queue *queue = to_rx_queue(kobj);
782
783 if (!attribute->show)
784 return -EIO;
785
786 return attribute->show(queue, buf);
787 }
788
rx_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)789 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
790 const char *buf, size_t count)
791 {
792 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
793 struct netdev_rx_queue *queue = to_rx_queue(kobj);
794
795 if (!attribute->store)
796 return -EIO;
797
798 return attribute->store(queue, buf, count);
799 }
800
801 static const struct sysfs_ops rx_queue_sysfs_ops = {
802 .show = rx_queue_attr_show,
803 .store = rx_queue_attr_store,
804 };
805
806 #ifdef CONFIG_RPS
show_rps_map(struct netdev_rx_queue * queue,char * buf)807 static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
808 {
809 struct rps_map *map;
810 cpumask_var_t mask;
811 int i, len;
812
813 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
814 return -ENOMEM;
815
816 rcu_read_lock();
817 map = rcu_dereference(queue->rps_map);
818 if (map)
819 for (i = 0; i < map->len; i++)
820 cpumask_set_cpu(map->cpus[i], mask);
821
822 len = sysfs_emit(buf, "%*pb\n", cpumask_pr_args(mask));
823 rcu_read_unlock();
824 free_cpumask_var(mask);
825
826 return len < PAGE_SIZE ? len : -EINVAL;
827 }
828
netdev_rx_queue_set_rps_mask(struct netdev_rx_queue * queue,cpumask_var_t mask)829 static int netdev_rx_queue_set_rps_mask(struct netdev_rx_queue *queue,
830 cpumask_var_t mask)
831 {
832 static DEFINE_MUTEX(rps_map_mutex);
833 struct rps_map *old_map, *map;
834 int cpu, i;
835
836 map = kzalloc(max_t(unsigned int,
837 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
838 GFP_KERNEL);
839 if (!map)
840 return -ENOMEM;
841
842 i = 0;
843 for_each_cpu_and(cpu, mask, cpu_online_mask)
844 map->cpus[i++] = cpu;
845
846 if (i) {
847 map->len = i;
848 } else {
849 kfree(map);
850 map = NULL;
851 }
852
853 mutex_lock(&rps_map_mutex);
854 old_map = rcu_dereference_protected(queue->rps_map,
855 mutex_is_locked(&rps_map_mutex));
856 rcu_assign_pointer(queue->rps_map, map);
857
858 if (map)
859 static_branch_inc(&rps_needed);
860 if (old_map)
861 static_branch_dec(&rps_needed);
862
863 mutex_unlock(&rps_map_mutex);
864
865 if (old_map)
866 kfree_rcu(old_map, rcu);
867 return 0;
868 }
869
rps_cpumask_housekeeping(struct cpumask * mask)870 int rps_cpumask_housekeeping(struct cpumask *mask)
871 {
872 if (!cpumask_empty(mask)) {
873 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN));
874 cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ));
875 if (cpumask_empty(mask))
876 return -EINVAL;
877 }
878 return 0;
879 }
880
store_rps_map(struct netdev_rx_queue * queue,const char * buf,size_t len)881 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
882 const char *buf, size_t len)
883 {
884 cpumask_var_t mask;
885 int err;
886
887 if (!capable(CAP_NET_ADMIN))
888 return -EPERM;
889
890 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
891 return -ENOMEM;
892
893 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
894 if (err)
895 goto out;
896
897 err = rps_cpumask_housekeeping(mask);
898 if (err)
899 goto out;
900
901 err = netdev_rx_queue_set_rps_mask(queue, mask);
902
903 out:
904 free_cpumask_var(mask);
905 return err ? : len;
906 }
907
show_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,char * buf)908 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
909 char *buf)
910 {
911 struct rps_dev_flow_table *flow_table;
912 unsigned long val = 0;
913
914 rcu_read_lock();
915 flow_table = rcu_dereference(queue->rps_flow_table);
916 if (flow_table)
917 val = (unsigned long)flow_table->mask + 1;
918 rcu_read_unlock();
919
920 return sysfs_emit(buf, "%lu\n", val);
921 }
922
rps_dev_flow_table_release(struct rcu_head * rcu)923 static void rps_dev_flow_table_release(struct rcu_head *rcu)
924 {
925 struct rps_dev_flow_table *table = container_of(rcu,
926 struct rps_dev_flow_table, rcu);
927 vfree(table);
928 }
929
store_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,const char * buf,size_t len)930 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
931 const char *buf, size_t len)
932 {
933 unsigned long mask, count;
934 struct rps_dev_flow_table *table, *old_table;
935 static DEFINE_SPINLOCK(rps_dev_flow_lock);
936 int rc;
937
938 if (!capable(CAP_NET_ADMIN))
939 return -EPERM;
940
941 rc = kstrtoul(buf, 0, &count);
942 if (rc < 0)
943 return rc;
944
945 if (count) {
946 mask = count - 1;
947 /* mask = roundup_pow_of_two(count) - 1;
948 * without overflows...
949 */
950 while ((mask | (mask >> 1)) != mask)
951 mask |= (mask >> 1);
952 /* On 64 bit arches, must check mask fits in table->mask (u32),
953 * and on 32bit arches, must check
954 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
955 */
956 #if BITS_PER_LONG > 32
957 if (mask > (unsigned long)(u32)mask)
958 return -EINVAL;
959 #else
960 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
961 / sizeof(struct rps_dev_flow)) {
962 /* Enforce a limit to prevent overflow */
963 return -EINVAL;
964 }
965 #endif
966 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
967 if (!table)
968 return -ENOMEM;
969
970 table->mask = mask;
971 for (count = 0; count <= mask; count++)
972 table->flows[count].cpu = RPS_NO_CPU;
973 } else {
974 table = NULL;
975 }
976
977 spin_lock(&rps_dev_flow_lock);
978 old_table = rcu_dereference_protected(queue->rps_flow_table,
979 lockdep_is_held(&rps_dev_flow_lock));
980 rcu_assign_pointer(queue->rps_flow_table, table);
981 spin_unlock(&rps_dev_flow_lock);
982
983 if (old_table)
984 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
985
986 return len;
987 }
988
989 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
990 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
991
992 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
993 = __ATTR(rps_flow_cnt, 0644,
994 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
995 #endif /* CONFIG_RPS */
996
997 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
998 #ifdef CONFIG_RPS
999 &rps_cpus_attribute.attr,
1000 &rps_dev_flow_table_cnt_attribute.attr,
1001 #endif
1002 NULL
1003 };
1004 ATTRIBUTE_GROUPS(rx_queue_default);
1005
rx_queue_release(struct kobject * kobj)1006 static void rx_queue_release(struct kobject *kobj)
1007 {
1008 struct netdev_rx_queue *queue = to_rx_queue(kobj);
1009 #ifdef CONFIG_RPS
1010 struct rps_map *map;
1011 struct rps_dev_flow_table *flow_table;
1012
1013 map = rcu_dereference_protected(queue->rps_map, 1);
1014 if (map) {
1015 RCU_INIT_POINTER(queue->rps_map, NULL);
1016 kfree_rcu(map, rcu);
1017 }
1018
1019 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
1020 if (flow_table) {
1021 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
1022 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
1023 }
1024 #endif
1025
1026 memset(kobj, 0, sizeof(*kobj));
1027 netdev_put(queue->dev, &queue->dev_tracker);
1028 }
1029
rx_queue_namespace(const struct kobject * kobj)1030 static const void *rx_queue_namespace(const struct kobject *kobj)
1031 {
1032 struct netdev_rx_queue *queue = to_rx_queue(kobj);
1033 struct device *dev = &queue->dev->dev;
1034 const void *ns = NULL;
1035
1036 if (dev->class && dev->class->ns_type)
1037 ns = dev->class->namespace(dev);
1038
1039 return ns;
1040 }
1041
rx_queue_get_ownership(const struct kobject * kobj,kuid_t * uid,kgid_t * gid)1042 static void rx_queue_get_ownership(const struct kobject *kobj,
1043 kuid_t *uid, kgid_t *gid)
1044 {
1045 const struct net *net = rx_queue_namespace(kobj);
1046
1047 net_ns_get_ownership(net, uid, gid);
1048 }
1049
1050 static const struct kobj_type rx_queue_ktype = {
1051 .sysfs_ops = &rx_queue_sysfs_ops,
1052 .release = rx_queue_release,
1053 .default_groups = rx_queue_default_groups,
1054 .namespace = rx_queue_namespace,
1055 .get_ownership = rx_queue_get_ownership,
1056 };
1057
rx_queue_default_mask(struct net_device * dev,struct netdev_rx_queue * queue)1058 static int rx_queue_default_mask(struct net_device *dev,
1059 struct netdev_rx_queue *queue)
1060 {
1061 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL)
1062 struct cpumask *rps_default_mask = READ_ONCE(dev_net(dev)->core.rps_default_mask);
1063
1064 if (rps_default_mask && !cpumask_empty(rps_default_mask))
1065 return netdev_rx_queue_set_rps_mask(queue, rps_default_mask);
1066 #endif
1067 return 0;
1068 }
1069
rx_queue_add_kobject(struct net_device * dev,int index)1070 static int rx_queue_add_kobject(struct net_device *dev, int index)
1071 {
1072 struct netdev_rx_queue *queue = dev->_rx + index;
1073 struct kobject *kobj = &queue->kobj;
1074 int error = 0;
1075
1076 /* Kobject_put later will trigger rx_queue_release call which
1077 * decreases dev refcount: Take that reference here
1078 */
1079 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1080
1081 kobj->kset = dev->queues_kset;
1082 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1083 "rx-%u", index);
1084 if (error)
1085 goto err;
1086
1087 if (dev->sysfs_rx_queue_group) {
1088 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1089 if (error)
1090 goto err;
1091 }
1092
1093 error = rx_queue_default_mask(dev, queue);
1094 if (error)
1095 goto err;
1096
1097 kobject_uevent(kobj, KOBJ_ADD);
1098
1099 return error;
1100
1101 err:
1102 kobject_put(kobj);
1103 return error;
1104 }
1105
rx_queue_change_owner(struct net_device * dev,int index,kuid_t kuid,kgid_t kgid)1106 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1107 kgid_t kgid)
1108 {
1109 struct netdev_rx_queue *queue = dev->_rx + index;
1110 struct kobject *kobj = &queue->kobj;
1111 int error;
1112
1113 error = sysfs_change_owner(kobj, kuid, kgid);
1114 if (error)
1115 return error;
1116
1117 if (dev->sysfs_rx_queue_group)
1118 error = sysfs_group_change_owner(
1119 kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1120
1121 return error;
1122 }
1123 #endif /* CONFIG_SYSFS */
1124
1125 int
net_rx_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1126 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1127 {
1128 #ifdef CONFIG_SYSFS
1129 int i;
1130 int error = 0;
1131
1132 #ifndef CONFIG_RPS
1133 if (!dev->sysfs_rx_queue_group)
1134 return 0;
1135 #endif
1136 for (i = old_num; i < new_num; i++) {
1137 error = rx_queue_add_kobject(dev, i);
1138 if (error) {
1139 new_num = old_num;
1140 break;
1141 }
1142 }
1143
1144 while (--i >= new_num) {
1145 struct kobject *kobj = &dev->_rx[i].kobj;
1146
1147 if (!refcount_read(&dev_net(dev)->ns.count))
1148 kobj->uevent_suppress = 1;
1149 if (dev->sysfs_rx_queue_group)
1150 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1151 kobject_put(kobj);
1152 }
1153
1154 return error;
1155 #else
1156 return 0;
1157 #endif
1158 }
1159
net_rx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1160 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1161 kuid_t kuid, kgid_t kgid)
1162 {
1163 #ifdef CONFIG_SYSFS
1164 int error = 0;
1165 int i;
1166
1167 #ifndef CONFIG_RPS
1168 if (!dev->sysfs_rx_queue_group)
1169 return 0;
1170 #endif
1171 for (i = 0; i < num; i++) {
1172 error = rx_queue_change_owner(dev, i, kuid, kgid);
1173 if (error)
1174 break;
1175 }
1176
1177 return error;
1178 #else
1179 return 0;
1180 #endif
1181 }
1182
1183 #ifdef CONFIG_SYSFS
1184 /*
1185 * netdev_queue sysfs structures and functions.
1186 */
1187 struct netdev_queue_attribute {
1188 struct attribute attr;
1189 ssize_t (*show)(struct netdev_queue *queue, char *buf);
1190 ssize_t (*store)(struct netdev_queue *queue,
1191 const char *buf, size_t len);
1192 };
1193 #define to_netdev_queue_attr(_attr) \
1194 container_of(_attr, struct netdev_queue_attribute, attr)
1195
1196 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1197
netdev_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1198 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1199 struct attribute *attr, char *buf)
1200 {
1201 const struct netdev_queue_attribute *attribute
1202 = to_netdev_queue_attr(attr);
1203 struct netdev_queue *queue = to_netdev_queue(kobj);
1204
1205 if (!attribute->show)
1206 return -EIO;
1207
1208 return attribute->show(queue, buf);
1209 }
1210
netdev_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1211 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1212 struct attribute *attr,
1213 const char *buf, size_t count)
1214 {
1215 const struct netdev_queue_attribute *attribute
1216 = to_netdev_queue_attr(attr);
1217 struct netdev_queue *queue = to_netdev_queue(kobj);
1218
1219 if (!attribute->store)
1220 return -EIO;
1221
1222 return attribute->store(queue, buf, count);
1223 }
1224
1225 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1226 .show = netdev_queue_attr_show,
1227 .store = netdev_queue_attr_store,
1228 };
1229
tx_timeout_show(struct netdev_queue * queue,char * buf)1230 static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
1231 {
1232 unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
1233
1234 return sysfs_emit(buf, fmt_ulong, trans_timeout);
1235 }
1236
get_netdev_queue_index(struct netdev_queue * queue)1237 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1238 {
1239 struct net_device *dev = queue->dev;
1240 unsigned int i;
1241
1242 i = queue - dev->_tx;
1243 BUG_ON(i >= dev->num_tx_queues);
1244
1245 return i;
1246 }
1247
traffic_class_show(struct netdev_queue * queue,char * buf)1248 static ssize_t traffic_class_show(struct netdev_queue *queue,
1249 char *buf)
1250 {
1251 struct net_device *dev = queue->dev;
1252 int num_tc, tc;
1253 int index;
1254
1255 if (!netif_is_multiqueue(dev))
1256 return -ENOENT;
1257
1258 if (!rtnl_trylock())
1259 return restart_syscall();
1260
1261 index = get_netdev_queue_index(queue);
1262
1263 /* If queue belongs to subordinate dev use its TC mapping */
1264 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1265
1266 num_tc = dev->num_tc;
1267 tc = netdev_txq_to_tc(dev, index);
1268
1269 rtnl_unlock();
1270
1271 if (tc < 0)
1272 return -EINVAL;
1273
1274 /* We can report the traffic class one of two ways:
1275 * Subordinate device traffic classes are reported with the traffic
1276 * class first, and then the subordinate class so for example TC0 on
1277 * subordinate device 2 will be reported as "0-2". If the queue
1278 * belongs to the root device it will be reported with just the
1279 * traffic class, so just "0" for TC 0 for example.
1280 */
1281 return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
1282 sysfs_emit(buf, "%d\n", tc);
1283 }
1284
1285 #ifdef CONFIG_XPS
tx_maxrate_show(struct netdev_queue * queue,char * buf)1286 static ssize_t tx_maxrate_show(struct netdev_queue *queue,
1287 char *buf)
1288 {
1289 return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
1290 }
1291
tx_maxrate_store(struct netdev_queue * queue,const char * buf,size_t len)1292 static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1293 const char *buf, size_t len)
1294 {
1295 struct net_device *dev = queue->dev;
1296 int err, index = get_netdev_queue_index(queue);
1297 u32 rate = 0;
1298
1299 if (!capable(CAP_NET_ADMIN))
1300 return -EPERM;
1301
1302 /* The check is also done later; this helps returning early without
1303 * hitting the trylock/restart below.
1304 */
1305 if (!dev->netdev_ops->ndo_set_tx_maxrate)
1306 return -EOPNOTSUPP;
1307
1308 err = kstrtou32(buf, 10, &rate);
1309 if (err < 0)
1310 return err;
1311
1312 if (!rtnl_trylock())
1313 return restart_syscall();
1314
1315 err = -EOPNOTSUPP;
1316 if (dev->netdev_ops->ndo_set_tx_maxrate)
1317 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1318
1319 rtnl_unlock();
1320 if (!err) {
1321 queue->tx_maxrate = rate;
1322 return len;
1323 }
1324 return err;
1325 }
1326
1327 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1328 = __ATTR_RW(tx_maxrate);
1329 #endif
1330
1331 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1332 = __ATTR_RO(tx_timeout);
1333
1334 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1335 = __ATTR_RO(traffic_class);
1336
1337 #ifdef CONFIG_BQL
1338 /*
1339 * Byte queue limits sysfs structures and functions.
1340 */
bql_show(char * buf,unsigned int value)1341 static ssize_t bql_show(char *buf, unsigned int value)
1342 {
1343 return sysfs_emit(buf, "%u\n", value);
1344 }
1345
bql_set(const char * buf,const size_t count,unsigned int * pvalue)1346 static ssize_t bql_set(const char *buf, const size_t count,
1347 unsigned int *pvalue)
1348 {
1349 unsigned int value;
1350 int err;
1351
1352 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1353 value = DQL_MAX_LIMIT;
1354 } else {
1355 err = kstrtouint(buf, 10, &value);
1356 if (err < 0)
1357 return err;
1358 if (value > DQL_MAX_LIMIT)
1359 return -EINVAL;
1360 }
1361
1362 *pvalue = value;
1363
1364 return count;
1365 }
1366
bql_show_hold_time(struct netdev_queue * queue,char * buf)1367 static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1368 char *buf)
1369 {
1370 struct dql *dql = &queue->dql;
1371
1372 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1373 }
1374
bql_set_hold_time(struct netdev_queue * queue,const char * buf,size_t len)1375 static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1376 const char *buf, size_t len)
1377 {
1378 struct dql *dql = &queue->dql;
1379 unsigned int value;
1380 int err;
1381
1382 err = kstrtouint(buf, 10, &value);
1383 if (err < 0)
1384 return err;
1385
1386 dql->slack_hold_time = msecs_to_jiffies(value);
1387
1388 return len;
1389 }
1390
1391 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1392 = __ATTR(hold_time, 0644,
1393 bql_show_hold_time, bql_set_hold_time);
1394
bql_show_inflight(struct netdev_queue * queue,char * buf)1395 static ssize_t bql_show_inflight(struct netdev_queue *queue,
1396 char *buf)
1397 {
1398 struct dql *dql = &queue->dql;
1399
1400 return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
1401 }
1402
1403 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1404 __ATTR(inflight, 0444, bql_show_inflight, NULL);
1405
1406 #define BQL_ATTR(NAME, FIELD) \
1407 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1408 char *buf) \
1409 { \
1410 return bql_show(buf, queue->dql.FIELD); \
1411 } \
1412 \
1413 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1414 const char *buf, size_t len) \
1415 { \
1416 return bql_set(buf, len, &queue->dql.FIELD); \
1417 } \
1418 \
1419 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1420 = __ATTR(NAME, 0644, \
1421 bql_show_ ## NAME, bql_set_ ## NAME)
1422
1423 BQL_ATTR(limit, limit);
1424 BQL_ATTR(limit_max, max_limit);
1425 BQL_ATTR(limit_min, min_limit);
1426
1427 static struct attribute *dql_attrs[] __ro_after_init = {
1428 &bql_limit_attribute.attr,
1429 &bql_limit_max_attribute.attr,
1430 &bql_limit_min_attribute.attr,
1431 &bql_hold_time_attribute.attr,
1432 &bql_inflight_attribute.attr,
1433 NULL
1434 };
1435
1436 static const struct attribute_group dql_group = {
1437 .name = "byte_queue_limits",
1438 .attrs = dql_attrs,
1439 };
1440 #endif /* CONFIG_BQL */
1441
1442 #ifdef CONFIG_XPS
xps_queue_show(struct net_device * dev,unsigned int index,int tc,char * buf,enum xps_map_type type)1443 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
1444 int tc, char *buf, enum xps_map_type type)
1445 {
1446 struct xps_dev_maps *dev_maps;
1447 unsigned long *mask;
1448 unsigned int nr_ids;
1449 int j, len;
1450
1451 rcu_read_lock();
1452 dev_maps = rcu_dereference(dev->xps_maps[type]);
1453
1454 /* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
1455 * when dev_maps hasn't been allocated yet, to be backward compatible.
1456 */
1457 nr_ids = dev_maps ? dev_maps->nr_ids :
1458 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
1459
1460 mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
1461 if (!mask) {
1462 rcu_read_unlock();
1463 return -ENOMEM;
1464 }
1465
1466 if (!dev_maps || tc >= dev_maps->num_tc)
1467 goto out_no_maps;
1468
1469 for (j = 0; j < nr_ids; j++) {
1470 int i, tci = j * dev_maps->num_tc + tc;
1471 struct xps_map *map;
1472
1473 map = rcu_dereference(dev_maps->attr_map[tci]);
1474 if (!map)
1475 continue;
1476
1477 for (i = map->len; i--;) {
1478 if (map->queues[i] == index) {
1479 __set_bit(j, mask);
1480 break;
1481 }
1482 }
1483 }
1484 out_no_maps:
1485 rcu_read_unlock();
1486
1487 len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids);
1488 bitmap_free(mask);
1489
1490 return len < PAGE_SIZE ? len : -EINVAL;
1491 }
1492
xps_cpus_show(struct netdev_queue * queue,char * buf)1493 static ssize_t xps_cpus_show(struct netdev_queue *queue, char *buf)
1494 {
1495 struct net_device *dev = queue->dev;
1496 unsigned int index;
1497 int len, tc;
1498
1499 if (!netif_is_multiqueue(dev))
1500 return -ENOENT;
1501
1502 index = get_netdev_queue_index(queue);
1503
1504 if (!rtnl_trylock())
1505 return restart_syscall();
1506
1507 /* If queue belongs to subordinate dev use its map */
1508 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1509
1510 tc = netdev_txq_to_tc(dev, index);
1511 if (tc < 0) {
1512 rtnl_unlock();
1513 return -EINVAL;
1514 }
1515
1516 /* Make sure the subordinate device can't be freed */
1517 get_device(&dev->dev);
1518 rtnl_unlock();
1519
1520 len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
1521
1522 put_device(&dev->dev);
1523 return len;
1524 }
1525
xps_cpus_store(struct netdev_queue * queue,const char * buf,size_t len)1526 static ssize_t xps_cpus_store(struct netdev_queue *queue,
1527 const char *buf, size_t len)
1528 {
1529 struct net_device *dev = queue->dev;
1530 unsigned int index;
1531 cpumask_var_t mask;
1532 int err;
1533
1534 if (!netif_is_multiqueue(dev))
1535 return -ENOENT;
1536
1537 if (!capable(CAP_NET_ADMIN))
1538 return -EPERM;
1539
1540 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1541 return -ENOMEM;
1542
1543 index = get_netdev_queue_index(queue);
1544
1545 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1546 if (err) {
1547 free_cpumask_var(mask);
1548 return err;
1549 }
1550
1551 if (!rtnl_trylock()) {
1552 free_cpumask_var(mask);
1553 return restart_syscall();
1554 }
1555
1556 err = netif_set_xps_queue(dev, mask, index);
1557 rtnl_unlock();
1558
1559 free_cpumask_var(mask);
1560
1561 return err ? : len;
1562 }
1563
1564 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1565 = __ATTR_RW(xps_cpus);
1566
xps_rxqs_show(struct netdev_queue * queue,char * buf)1567 static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1568 {
1569 struct net_device *dev = queue->dev;
1570 unsigned int index;
1571 int tc;
1572
1573 index = get_netdev_queue_index(queue);
1574
1575 if (!rtnl_trylock())
1576 return restart_syscall();
1577
1578 tc = netdev_txq_to_tc(dev, index);
1579 rtnl_unlock();
1580 if (tc < 0)
1581 return -EINVAL;
1582
1583 return xps_queue_show(dev, index, tc, buf, XPS_RXQS);
1584 }
1585
xps_rxqs_store(struct netdev_queue * queue,const char * buf,size_t len)1586 static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1587 size_t len)
1588 {
1589 struct net_device *dev = queue->dev;
1590 struct net *net = dev_net(dev);
1591 unsigned long *mask;
1592 unsigned int index;
1593 int err;
1594
1595 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1596 return -EPERM;
1597
1598 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1599 if (!mask)
1600 return -ENOMEM;
1601
1602 index = get_netdev_queue_index(queue);
1603
1604 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1605 if (err) {
1606 bitmap_free(mask);
1607 return err;
1608 }
1609
1610 if (!rtnl_trylock()) {
1611 bitmap_free(mask);
1612 return restart_syscall();
1613 }
1614
1615 cpus_read_lock();
1616 err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
1617 cpus_read_unlock();
1618
1619 rtnl_unlock();
1620
1621 bitmap_free(mask);
1622 return err ? : len;
1623 }
1624
1625 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1626 = __ATTR_RW(xps_rxqs);
1627 #endif /* CONFIG_XPS */
1628
1629 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1630 &queue_trans_timeout.attr,
1631 &queue_traffic_class.attr,
1632 #ifdef CONFIG_XPS
1633 &xps_cpus_attribute.attr,
1634 &xps_rxqs_attribute.attr,
1635 &queue_tx_maxrate.attr,
1636 #endif
1637 NULL
1638 };
1639 ATTRIBUTE_GROUPS(netdev_queue_default);
1640
netdev_queue_release(struct kobject * kobj)1641 static void netdev_queue_release(struct kobject *kobj)
1642 {
1643 struct netdev_queue *queue = to_netdev_queue(kobj);
1644
1645 memset(kobj, 0, sizeof(*kobj));
1646 netdev_put(queue->dev, &queue->dev_tracker);
1647 }
1648
netdev_queue_namespace(const struct kobject * kobj)1649 static const void *netdev_queue_namespace(const struct kobject *kobj)
1650 {
1651 struct netdev_queue *queue = to_netdev_queue(kobj);
1652 struct device *dev = &queue->dev->dev;
1653 const void *ns = NULL;
1654
1655 if (dev->class && dev->class->ns_type)
1656 ns = dev->class->namespace(dev);
1657
1658 return ns;
1659 }
1660
netdev_queue_get_ownership(const struct kobject * kobj,kuid_t * uid,kgid_t * gid)1661 static void netdev_queue_get_ownership(const struct kobject *kobj,
1662 kuid_t *uid, kgid_t *gid)
1663 {
1664 const struct net *net = netdev_queue_namespace(kobj);
1665
1666 net_ns_get_ownership(net, uid, gid);
1667 }
1668
1669 static const struct kobj_type netdev_queue_ktype = {
1670 .sysfs_ops = &netdev_queue_sysfs_ops,
1671 .release = netdev_queue_release,
1672 .default_groups = netdev_queue_default_groups,
1673 .namespace = netdev_queue_namespace,
1674 .get_ownership = netdev_queue_get_ownership,
1675 };
1676
netdev_queue_add_kobject(struct net_device * dev,int index)1677 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1678 {
1679 struct netdev_queue *queue = dev->_tx + index;
1680 struct kobject *kobj = &queue->kobj;
1681 int error = 0;
1682
1683 /* Kobject_put later will trigger netdev_queue_release call
1684 * which decreases dev refcount: Take that reference here
1685 */
1686 netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1687
1688 kobj->kset = dev->queues_kset;
1689 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1690 "tx-%u", index);
1691 if (error)
1692 goto err;
1693
1694 #ifdef CONFIG_BQL
1695 error = sysfs_create_group(kobj, &dql_group);
1696 if (error)
1697 goto err;
1698 #endif
1699
1700 kobject_uevent(kobj, KOBJ_ADD);
1701 return 0;
1702
1703 err:
1704 kobject_put(kobj);
1705 return error;
1706 }
1707
tx_queue_change_owner(struct net_device * ndev,int index,kuid_t kuid,kgid_t kgid)1708 static int tx_queue_change_owner(struct net_device *ndev, int index,
1709 kuid_t kuid, kgid_t kgid)
1710 {
1711 struct netdev_queue *queue = ndev->_tx + index;
1712 struct kobject *kobj = &queue->kobj;
1713 int error;
1714
1715 error = sysfs_change_owner(kobj, kuid, kgid);
1716 if (error)
1717 return error;
1718
1719 #ifdef CONFIG_BQL
1720 error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
1721 #endif
1722 return error;
1723 }
1724 #endif /* CONFIG_SYSFS */
1725
1726 int
netdev_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1727 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1728 {
1729 #ifdef CONFIG_SYSFS
1730 int i;
1731 int error = 0;
1732
1733 /* Tx queue kobjects are allowed to be updated when a device is being
1734 * unregistered, but solely to remove queues from qdiscs. Any path
1735 * adding queues should be fixed.
1736 */
1737 WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
1738 "New queues can't be registered after device unregistration.");
1739
1740 for (i = old_num; i < new_num; i++) {
1741 error = netdev_queue_add_kobject(dev, i);
1742 if (error) {
1743 new_num = old_num;
1744 break;
1745 }
1746 }
1747
1748 while (--i >= new_num) {
1749 struct netdev_queue *queue = dev->_tx + i;
1750
1751 if (!refcount_read(&dev_net(dev)->ns.count))
1752 queue->kobj.uevent_suppress = 1;
1753 #ifdef CONFIG_BQL
1754 sysfs_remove_group(&queue->kobj, &dql_group);
1755 #endif
1756 kobject_put(&queue->kobj);
1757 }
1758
1759 return error;
1760 #else
1761 return 0;
1762 #endif /* CONFIG_SYSFS */
1763 }
1764
net_tx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1765 static int net_tx_queue_change_owner(struct net_device *dev, int num,
1766 kuid_t kuid, kgid_t kgid)
1767 {
1768 #ifdef CONFIG_SYSFS
1769 int error = 0;
1770 int i;
1771
1772 for (i = 0; i < num; i++) {
1773 error = tx_queue_change_owner(dev, i, kuid, kgid);
1774 if (error)
1775 break;
1776 }
1777
1778 return error;
1779 #else
1780 return 0;
1781 #endif /* CONFIG_SYSFS */
1782 }
1783
register_queue_kobjects(struct net_device * dev)1784 static int register_queue_kobjects(struct net_device *dev)
1785 {
1786 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1787
1788 #ifdef CONFIG_SYSFS
1789 dev->queues_kset = kset_create_and_add("queues",
1790 NULL, &dev->dev.kobj);
1791 if (!dev->queues_kset)
1792 return -ENOMEM;
1793 real_rx = dev->real_num_rx_queues;
1794 #endif
1795 real_tx = dev->real_num_tx_queues;
1796
1797 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
1798 if (error)
1799 goto error;
1800 rxq = real_rx;
1801
1802 error = netdev_queue_update_kobjects(dev, 0, real_tx);
1803 if (error)
1804 goto error;
1805 txq = real_tx;
1806
1807 return 0;
1808
1809 error:
1810 netdev_queue_update_kobjects(dev, txq, 0);
1811 net_rx_queue_update_kobjects(dev, rxq, 0);
1812 #ifdef CONFIG_SYSFS
1813 kset_unregister(dev->queues_kset);
1814 #endif
1815 return error;
1816 }
1817
queue_change_owner(struct net_device * ndev,kuid_t kuid,kgid_t kgid)1818 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
1819 {
1820 int error = 0, real_rx = 0, real_tx = 0;
1821
1822 #ifdef CONFIG_SYSFS
1823 if (ndev->queues_kset) {
1824 error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
1825 if (error)
1826 return error;
1827 }
1828 real_rx = ndev->real_num_rx_queues;
1829 #endif
1830 real_tx = ndev->real_num_tx_queues;
1831
1832 error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
1833 if (error)
1834 return error;
1835
1836 error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
1837 if (error)
1838 return error;
1839
1840 return 0;
1841 }
1842
remove_queue_kobjects(struct net_device * dev)1843 static void remove_queue_kobjects(struct net_device *dev)
1844 {
1845 int real_rx = 0, real_tx = 0;
1846
1847 #ifdef CONFIG_SYSFS
1848 real_rx = dev->real_num_rx_queues;
1849 #endif
1850 real_tx = dev->real_num_tx_queues;
1851
1852 net_rx_queue_update_kobjects(dev, real_rx, 0);
1853 netdev_queue_update_kobjects(dev, real_tx, 0);
1854
1855 dev->real_num_rx_queues = 0;
1856 dev->real_num_tx_queues = 0;
1857 #ifdef CONFIG_SYSFS
1858 kset_unregister(dev->queues_kset);
1859 #endif
1860 }
1861
net_current_may_mount(void)1862 static bool net_current_may_mount(void)
1863 {
1864 struct net *net = current->nsproxy->net_ns;
1865
1866 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1867 }
1868
net_grab_current_ns(void)1869 static void *net_grab_current_ns(void)
1870 {
1871 struct net *ns = current->nsproxy->net_ns;
1872 #ifdef CONFIG_NET_NS
1873 if (ns)
1874 refcount_inc(&ns->passive);
1875 #endif
1876 return ns;
1877 }
1878
net_initial_ns(void)1879 static const void *net_initial_ns(void)
1880 {
1881 return &init_net;
1882 }
1883
net_netlink_ns(struct sock * sk)1884 static const void *net_netlink_ns(struct sock *sk)
1885 {
1886 return sock_net(sk);
1887 }
1888
1889 const struct kobj_ns_type_operations net_ns_type_operations = {
1890 .type = KOBJ_NS_TYPE_NET,
1891 .current_may_mount = net_current_may_mount,
1892 .grab_current_ns = net_grab_current_ns,
1893 .netlink_ns = net_netlink_ns,
1894 .initial_ns = net_initial_ns,
1895 .drop_ns = net_drop_ns,
1896 };
1897 EXPORT_SYMBOL_GPL(net_ns_type_operations);
1898
netdev_uevent(const struct device * d,struct kobj_uevent_env * env)1899 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env)
1900 {
1901 const struct net_device *dev = to_net_dev(d);
1902 int retval;
1903
1904 /* pass interface to uevent. */
1905 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1906 if (retval)
1907 goto exit;
1908
1909 /* pass ifindex to uevent.
1910 * ifindex is useful as it won't change (interface name may change)
1911 * and is what RtNetlink uses natively.
1912 */
1913 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1914
1915 exit:
1916 return retval;
1917 }
1918
1919 /*
1920 * netdev_release -- destroy and free a dead device.
1921 * Called when last reference to device kobject is gone.
1922 */
netdev_release(struct device * d)1923 static void netdev_release(struct device *d)
1924 {
1925 struct net_device *dev = to_net_dev(d);
1926
1927 BUG_ON(dev->reg_state != NETREG_RELEASED);
1928
1929 /* no need to wait for rcu grace period:
1930 * device is dead and about to be freed.
1931 */
1932 kfree(rcu_access_pointer(dev->ifalias));
1933 netdev_freemem(dev);
1934 }
1935
net_namespace(const struct device * d)1936 static const void *net_namespace(const struct device *d)
1937 {
1938 const struct net_device *dev = to_net_dev(d);
1939
1940 return dev_net(dev);
1941 }
1942
net_get_ownership(const struct device * d,kuid_t * uid,kgid_t * gid)1943 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid)
1944 {
1945 const struct net_device *dev = to_net_dev(d);
1946 const struct net *net = dev_net(dev);
1947
1948 net_ns_get_ownership(net, uid, gid);
1949 }
1950
1951 static struct class net_class __ro_after_init = {
1952 .name = "net",
1953 .dev_release = netdev_release,
1954 .dev_groups = net_class_groups,
1955 .dev_uevent = netdev_uevent,
1956 .ns_type = &net_ns_type_operations,
1957 .namespace = net_namespace,
1958 .get_ownership = net_get_ownership,
1959 };
1960
1961 #ifdef CONFIG_OF
of_dev_node_match(struct device * dev,const void * data)1962 static int of_dev_node_match(struct device *dev, const void *data)
1963 {
1964 for (; dev; dev = dev->parent) {
1965 if (dev->of_node == data)
1966 return 1;
1967 }
1968
1969 return 0;
1970 }
1971
1972 /*
1973 * of_find_net_device_by_node - lookup the net device for the device node
1974 * @np: OF device node
1975 *
1976 * Looks up the net_device structure corresponding with the device node.
1977 * If successful, returns a pointer to the net_device with the embedded
1978 * struct device refcount incremented by one, or NULL on failure. The
1979 * refcount must be dropped when done with the net_device.
1980 */
of_find_net_device_by_node(struct device_node * np)1981 struct net_device *of_find_net_device_by_node(struct device_node *np)
1982 {
1983 struct device *dev;
1984
1985 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1986 if (!dev)
1987 return NULL;
1988
1989 return to_net_dev(dev);
1990 }
1991 EXPORT_SYMBOL(of_find_net_device_by_node);
1992 #endif
1993
1994 /* Delete sysfs entries but hold kobject reference until after all
1995 * netdev references are gone.
1996 */
netdev_unregister_kobject(struct net_device * ndev)1997 void netdev_unregister_kobject(struct net_device *ndev)
1998 {
1999 struct device *dev = &ndev->dev;
2000
2001 if (!refcount_read(&dev_net(ndev)->ns.count))
2002 dev_set_uevent_suppress(dev, 1);
2003
2004 kobject_get(&dev->kobj);
2005
2006 remove_queue_kobjects(ndev);
2007
2008 pm_runtime_set_memalloc_noio(dev, false);
2009
2010 device_del(dev);
2011 }
2012
2013 /* Create sysfs entries for network device. */
netdev_register_kobject(struct net_device * ndev)2014 int netdev_register_kobject(struct net_device *ndev)
2015 {
2016 struct device *dev = &ndev->dev;
2017 const struct attribute_group **groups = ndev->sysfs_groups;
2018 int error = 0;
2019
2020 device_initialize(dev);
2021 dev->class = &net_class;
2022 dev->platform_data = ndev;
2023 dev->groups = groups;
2024
2025 dev_set_name(dev, "%s", ndev->name);
2026
2027 #ifdef CONFIG_SYSFS
2028 /* Allow for a device specific group */
2029 if (*groups)
2030 groups++;
2031
2032 *groups++ = &netstat_group;
2033
2034 if (wireless_group_needed(ndev))
2035 *groups++ = &wireless_group;
2036 #endif /* CONFIG_SYSFS */
2037
2038 error = device_add(dev);
2039 if (error)
2040 return error;
2041
2042 error = register_queue_kobjects(ndev);
2043 if (error) {
2044 device_del(dev);
2045 return error;
2046 }
2047
2048 pm_runtime_set_memalloc_noio(dev, true);
2049
2050 return error;
2051 }
2052
2053 /* Change owner for sysfs entries when moving network devices across network
2054 * namespaces owned by different user namespaces.
2055 */
netdev_change_owner(struct net_device * ndev,const struct net * net_old,const struct net * net_new)2056 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2057 const struct net *net_new)
2058 {
2059 kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2060 kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2061 struct device *dev = &ndev->dev;
2062 int error;
2063
2064 net_ns_get_ownership(net_old, &old_uid, &old_gid);
2065 net_ns_get_ownership(net_new, &new_uid, &new_gid);
2066
2067 /* The network namespace was changed but the owning user namespace is
2068 * identical so there's no need to change the owner of sysfs entries.
2069 */
2070 if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2071 return 0;
2072
2073 error = device_change_owner(dev, new_uid, new_gid);
2074 if (error)
2075 return error;
2076
2077 error = queue_change_owner(ndev, new_uid, new_gid);
2078 if (error)
2079 return error;
2080
2081 return 0;
2082 }
2083
netdev_class_create_file_ns(const struct class_attribute * class_attr,const void * ns)2084 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2085 const void *ns)
2086 {
2087 return class_create_file_ns(&net_class, class_attr, ns);
2088 }
2089 EXPORT_SYMBOL(netdev_class_create_file_ns);
2090
netdev_class_remove_file_ns(const struct class_attribute * class_attr,const void * ns)2091 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2092 const void *ns)
2093 {
2094 class_remove_file_ns(&net_class, class_attr, ns);
2095 }
2096 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2097
netdev_kobject_init(void)2098 int __init netdev_kobject_init(void)
2099 {
2100 kobj_ns_type_register(&net_ns_type_operations);
2101 return class_register(&net_class);
2102 }
2103