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