Merge branch 'bnx2x'
[linux-block.git] / net / core / net-sysfs.c
CommitLineData
1da177e4
LT
1/*
2 * net-sysfs.c - network device class and attributes
3 *
4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
4ec93edb 5 *
1da177e4
LT
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
4fc268d2 12#include <linux/capability.h>
1da177e4
LT
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/if_arp.h>
5a0e3ad6 16#include <linux/slab.h>
608b4b95 17#include <linux/nsproxy.h>
1da177e4 18#include <net/sock.h>
608b4b95 19#include <net/net_namespace.h>
1da177e4 20#include <linux/rtnetlink.h>
fec5e652 21#include <linux/vmalloc.h>
bc3b2d7f 22#include <linux/export.h>
114cf580 23#include <linux/jiffies.h>
9802c8e2 24#include <linux/pm_runtime.h>
1da177e4 25
342709ef
PE
26#include "net-sysfs.h"
27
8b41d188 28#ifdef CONFIG_SYSFS
1da177e4 29static const char fmt_hex[] = "%#x\n";
d1102b59 30static const char fmt_long_hex[] = "%#lx\n";
1da177e4 31static const char fmt_dec[] = "%d\n";
8ae6daca 32static const char fmt_udec[] = "%u\n";
1da177e4 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{
43cb76d9 46 struct net_device *net = to_net_dev(dev);
1da177e4
LT
47 ssize_t ret = -EINVAL;
48
49 read_lock(&dev_base_lock);
50 if (dev_isalive(net))
51 ret = (*format)(net, buf);
52 read_unlock(&dev_base_lock);
53
54 return ret;
55}
56
57/* generate a show function for simple field */
58#define NETDEVICE_SHOW(field, format_string) \
59static ssize_t format_##field(const struct net_device *net, char *buf) \
60{ \
61 return sprintf(buf, format_string, net->field); \
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
LT
84 unsigned long new;
85 int ret = -EINVAL;
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
EB
97 if (dev_isalive(netdev)) {
98 if ((ret = (*set)(netdev, new)) == 0)
1da177e4
LT
99 ret = len;
100 }
101 rtnl_unlock();
102 err:
103 return ret;
104}
105
6be8aeef
GKH
106NETDEVICE_SHOW_RO(dev_id, fmt_hex);
107NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
108NETDEVICE_SHOW_RO(addr_len, fmt_dec);
109NETDEVICE_SHOW_RO(iflink, fmt_dec);
110NETDEVICE_SHOW_RO(ifindex, fmt_dec);
111NETDEVICE_SHOW_RO(type, fmt_dec);
112NETDEVICE_SHOW_RO(link_mode, fmt_dec);
1da177e4
LT
113
114/* use same locking rules as GIFHWADDR ioctl's */
6be8aeef 115static ssize_t address_show(struct device *dev, struct device_attribute *attr,
43cb76d9 116 char *buf)
1da177e4
LT
117{
118 struct net_device *net = to_net_dev(dev);
119 ssize_t ret = -EINVAL;
120
121 read_lock(&dev_base_lock);
122 if (dev_isalive(net))
7ffc49a6 123 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
1da177e4
LT
124 read_unlock(&dev_base_lock);
125 return ret;
126}
6be8aeef 127static DEVICE_ATTR_RO(address);
1da177e4 128
6be8aeef
GKH
129static ssize_t broadcast_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
1da177e4
LT
131{
132 struct net_device *net = to_net_dev(dev);
133 if (dev_isalive(net))
7ffc49a6 134 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
1da177e4
LT
135 return -EINVAL;
136}
6be8aeef 137static DEVICE_ATTR_RO(broadcast);
1da177e4 138
fdae0fde
JP
139static int change_carrier(struct net_device *net, unsigned long new_carrier)
140{
141 if (!netif_running(net))
142 return -EINVAL;
143 return dev_change_carrier(net, (bool) new_carrier);
144}
145
6be8aeef
GKH
146static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t len)
fdae0fde
JP
148{
149 return netdev_store(dev, attr, buf, len, change_carrier);
150}
151
6be8aeef 152static ssize_t carrier_show(struct device *dev,
43cb76d9 153 struct device_attribute *attr, char *buf)
1da177e4
LT
154{
155 struct net_device *netdev = to_net_dev(dev);
156 if (netif_running(netdev)) {
157 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
158 }
159 return -EINVAL;
160}
6be8aeef 161static DEVICE_ATTR_RW(carrier);
1da177e4 162
6be8aeef 163static ssize_t speed_show(struct device *dev,
d519e17e
AG
164 struct device_attribute *attr, char *buf)
165{
166 struct net_device *netdev = to_net_dev(dev);
167 int ret = -EINVAL;
168
169 if (!rtnl_trylock())
170 return restart_syscall();
171
8ae6daca
DD
172 if (netif_running(netdev)) {
173 struct ethtool_cmd cmd;
4bc71cb9 174 if (!__ethtool_get_settings(netdev, &cmd))
8ae6daca 175 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
d519e17e
AG
176 }
177 rtnl_unlock();
178 return ret;
179}
6be8aeef 180static DEVICE_ATTR_RO(speed);
d519e17e 181
6be8aeef 182static ssize_t duplex_show(struct device *dev,
d519e17e
AG
183 struct device_attribute *attr, char *buf)
184{
185 struct net_device *netdev = to_net_dev(dev);
186 int ret = -EINVAL;
187
188 if (!rtnl_trylock())
189 return restart_syscall();
190
8ae6daca
DD
191 if (netif_running(netdev)) {
192 struct ethtool_cmd cmd;
c6c13965
NA
193 if (!__ethtool_get_settings(netdev, &cmd)) {
194 const char *duplex;
195 switch (cmd.duplex) {
196 case DUPLEX_HALF:
197 duplex = "half";
198 break;
199 case DUPLEX_FULL:
200 duplex = "full";
201 break;
202 default:
203 duplex = "unknown";
204 break;
205 }
206 ret = sprintf(buf, "%s\n", duplex);
207 }
d519e17e
AG
208 }
209 rtnl_unlock();
210 return ret;
211}
6be8aeef 212static DEVICE_ATTR_RO(duplex);
d519e17e 213
6be8aeef 214static ssize_t dormant_show(struct device *dev,
43cb76d9 215 struct device_attribute *attr, char *buf)
b00055aa
SR
216{
217 struct net_device *netdev = to_net_dev(dev);
218
219 if (netif_running(netdev))
220 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
221
222 return -EINVAL;
223}
6be8aeef 224static DEVICE_ATTR_RO(dormant);
b00055aa 225
36cbd3dc 226static const char *const operstates[] = {
b00055aa
SR
227 "unknown",
228 "notpresent", /* currently unused */
229 "down",
230 "lowerlayerdown",
231 "testing", /* currently unused */
232 "dormant",
233 "up"
234};
235
6be8aeef 236static ssize_t operstate_show(struct device *dev,
43cb76d9 237 struct device_attribute *attr, char *buf)
b00055aa
SR
238{
239 const struct net_device *netdev = to_net_dev(dev);
240 unsigned char operstate;
241
242 read_lock(&dev_base_lock);
243 operstate = netdev->operstate;
244 if (!netif_running(netdev))
245 operstate = IF_OPER_DOWN;
246 read_unlock(&dev_base_lock);
247
e3a5cd9e 248 if (operstate >= ARRAY_SIZE(operstates))
b00055aa
SR
249 return -EINVAL; /* should not happen */
250
251 return sprintf(buf, "%s\n", operstates[operstate]);
252}
6be8aeef 253static DEVICE_ATTR_RO(operstate);
b00055aa 254
1da177e4 255/* read-write attributes */
1da177e4
LT
256
257static int change_mtu(struct net_device *net, unsigned long new_mtu)
258{
259 return dev_set_mtu(net, (int) new_mtu);
260}
261
6be8aeef 262static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
43cb76d9 263 const char *buf, size_t len)
1da177e4 264{
43cb76d9 265 return netdev_store(dev, attr, buf, len, change_mtu);
1da177e4 266}
6be8aeef 267NETDEVICE_SHOW_RW(mtu, fmt_dec);
1da177e4
LT
268
269static int change_flags(struct net_device *net, unsigned long new_flags)
270{
95c96174 271 return dev_change_flags(net, (unsigned int) new_flags);
1da177e4
LT
272}
273
6be8aeef 274static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
43cb76d9 275 const char *buf, size_t len)
1da177e4 276{
43cb76d9 277 return netdev_store(dev, attr, buf, len, change_flags);
1da177e4 278}
6be8aeef 279NETDEVICE_SHOW_RW(flags, fmt_hex);
1da177e4
LT
280
281static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
282{
283 net->tx_queue_len = new_len;
284 return 0;
285}
286
6be8aeef 287static ssize_t tx_queue_len_store(struct device *dev,
43cb76d9
GKH
288 struct device_attribute *attr,
289 const char *buf, size_t len)
1da177e4 290{
5e1fccc0
EB
291 if (!capable(CAP_NET_ADMIN))
292 return -EPERM;
293
43cb76d9 294 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
1da177e4 295}
6be8aeef 296NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
1da177e4 297
6be8aeef 298static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
0b815a1a
SH
299 const char *buf, size_t len)
300{
301 struct net_device *netdev = to_net_dev(dev);
5e1fccc0 302 struct net *net = dev_net(netdev);
0b815a1a
SH
303 size_t count = len;
304 ssize_t ret;
305
5e1fccc0 306 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
0b815a1a
SH
307 return -EPERM;
308
309 /* ignore trailing newline */
310 if (len > 0 && buf[len - 1] == '\n')
311 --count;
312
336ca57c
EB
313 if (!rtnl_trylock())
314 return restart_syscall();
0b815a1a
SH
315 ret = dev_set_alias(netdev, buf, count);
316 rtnl_unlock();
317
318 return ret < 0 ? ret : len;
319}
320
6be8aeef 321static ssize_t ifalias_show(struct device *dev,
0b815a1a
SH
322 struct device_attribute *attr, char *buf)
323{
324 const struct net_device *netdev = to_net_dev(dev);
325 ssize_t ret = 0;
326
336ca57c
EB
327 if (!rtnl_trylock())
328 return restart_syscall();
0b815a1a
SH
329 if (netdev->ifalias)
330 ret = sprintf(buf, "%s\n", netdev->ifalias);
331 rtnl_unlock();
332 return ret;
333}
6be8aeef 334static DEVICE_ATTR_RW(ifalias);
a512b92b
VD
335
336static int change_group(struct net_device *net, unsigned long new_group)
337{
338 dev_set_group(net, (int) new_group);
339 return 0;
340}
341
6be8aeef
GKH
342static ssize_t group_store(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t len)
a512b92b
VD
344{
345 return netdev_store(dev, attr, buf, len, change_group);
346}
6be8aeef
GKH
347NETDEVICE_SHOW(group, fmt_dec);
348static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
349
cc998ff8 350static ssize_t phys_port_id_show(struct device *dev,
ff80e519
JP
351 struct device_attribute *attr, char *buf)
352{
353 struct net_device *netdev = to_net_dev(dev);
354 ssize_t ret = -EINVAL;
355
356 if (!rtnl_trylock())
357 return restart_syscall();
358
359 if (dev_isalive(netdev)) {
360 struct netdev_phys_port_id ppid;
361
362 ret = dev_get_phys_port_id(netdev, &ppid);
363 if (!ret)
364 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
365 }
366 rtnl_unlock();
367
368 return ret;
369}
cc998ff8
LT
370static DEVICE_ATTR_RO(phys_port_id);
371
6be8aeef
GKH
372static struct attribute *net_class_attrs[] = {
373 &dev_attr_netdev_group.attr,
374 &dev_attr_type.attr,
375 &dev_attr_dev_id.attr,
376 &dev_attr_iflink.attr,
377 &dev_attr_ifindex.attr,
378 &dev_attr_addr_assign_type.attr,
379 &dev_attr_addr_len.attr,
380 &dev_attr_link_mode.attr,
381 &dev_attr_address.attr,
382 &dev_attr_broadcast.attr,
383 &dev_attr_speed.attr,
384 &dev_attr_duplex.attr,
385 &dev_attr_dormant.attr,
386 &dev_attr_operstate.attr,
387 &dev_attr_ifalias.attr,
388 &dev_attr_carrier.attr,
389 &dev_attr_mtu.attr,
390 &dev_attr_flags.attr,
391 &dev_attr_tx_queue_len.attr,
cc998ff8 392 &dev_attr_phys_port_id.attr,
6be8aeef 393 NULL,
1da177e4 394};
6be8aeef 395ATTRIBUTE_GROUPS(net_class);
1da177e4
LT
396
397/* Show a given an attribute in the statistics group */
43cb76d9
GKH
398static ssize_t netstat_show(const struct device *d,
399 struct device_attribute *attr, char *buf,
1da177e4
LT
400 unsigned long offset)
401{
43cb76d9 402 struct net_device *dev = to_net_dev(d);
1da177e4
LT
403 ssize_t ret = -EINVAL;
404
be1f3c2c
BH
405 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
406 offset % sizeof(u64) != 0);
1da177e4
LT
407
408 read_lock(&dev_base_lock);
96e74088 409 if (dev_isalive(dev)) {
28172739
ED
410 struct rtnl_link_stats64 temp;
411 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
412
be1f3c2c 413 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
96e74088 414 }
1da177e4
LT
415 read_unlock(&dev_base_lock);
416 return ret;
417}
418
419/* generate a read-only statistics attribute */
420#define NETSTAT_ENTRY(name) \
6be8aeef 421static ssize_t name##_show(struct device *d, \
43cb76d9 422 struct device_attribute *attr, char *buf) \
1da177e4 423{ \
43cb76d9 424 return netstat_show(d, attr, buf, \
be1f3c2c 425 offsetof(struct rtnl_link_stats64, name)); \
1da177e4 426} \
6be8aeef 427static DEVICE_ATTR_RO(name)
1da177e4
LT
428
429NETSTAT_ENTRY(rx_packets);
430NETSTAT_ENTRY(tx_packets);
431NETSTAT_ENTRY(rx_bytes);
432NETSTAT_ENTRY(tx_bytes);
433NETSTAT_ENTRY(rx_errors);
434NETSTAT_ENTRY(tx_errors);
435NETSTAT_ENTRY(rx_dropped);
436NETSTAT_ENTRY(tx_dropped);
437NETSTAT_ENTRY(multicast);
438NETSTAT_ENTRY(collisions);
439NETSTAT_ENTRY(rx_length_errors);
440NETSTAT_ENTRY(rx_over_errors);
441NETSTAT_ENTRY(rx_crc_errors);
442NETSTAT_ENTRY(rx_frame_errors);
443NETSTAT_ENTRY(rx_fifo_errors);
444NETSTAT_ENTRY(rx_missed_errors);
445NETSTAT_ENTRY(tx_aborted_errors);
446NETSTAT_ENTRY(tx_carrier_errors);
447NETSTAT_ENTRY(tx_fifo_errors);
448NETSTAT_ENTRY(tx_heartbeat_errors);
449NETSTAT_ENTRY(tx_window_errors);
450NETSTAT_ENTRY(rx_compressed);
451NETSTAT_ENTRY(tx_compressed);
452
453static struct attribute *netstat_attrs[] = {
43cb76d9
GKH
454 &dev_attr_rx_packets.attr,
455 &dev_attr_tx_packets.attr,
456 &dev_attr_rx_bytes.attr,
457 &dev_attr_tx_bytes.attr,
458 &dev_attr_rx_errors.attr,
459 &dev_attr_tx_errors.attr,
460 &dev_attr_rx_dropped.attr,
461 &dev_attr_tx_dropped.attr,
462 &dev_attr_multicast.attr,
463 &dev_attr_collisions.attr,
464 &dev_attr_rx_length_errors.attr,
465 &dev_attr_rx_over_errors.attr,
466 &dev_attr_rx_crc_errors.attr,
467 &dev_attr_rx_frame_errors.attr,
468 &dev_attr_rx_fifo_errors.attr,
469 &dev_attr_rx_missed_errors.attr,
470 &dev_attr_tx_aborted_errors.attr,
471 &dev_attr_tx_carrier_errors.attr,
472 &dev_attr_tx_fifo_errors.attr,
473 &dev_attr_tx_heartbeat_errors.attr,
474 &dev_attr_tx_window_errors.attr,
475 &dev_attr_rx_compressed.attr,
476 &dev_attr_tx_compressed.attr,
1da177e4
LT
477 NULL
478};
479
480
481static struct attribute_group netstat_group = {
482 .name = "statistics",
483 .attrs = netstat_attrs,
484};
38c1a01c
JB
485
486#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
487static struct attribute *wireless_attrs[] = {
488 NULL
489};
490
491static struct attribute_group wireless_group = {
492 .name = "wireless",
493 .attrs = wireless_attrs,
494};
495#endif
6be8aeef
GKH
496
497#else /* CONFIG_SYSFS */
498#define net_class_groups NULL
d6523ddf 499#endif /* CONFIG_SYSFS */
1da177e4 500
a953be53 501#ifdef CONFIG_SYSFS
0a9627f2
TH
502#define to_rx_queue_attr(_attr) container_of(_attr, \
503 struct rx_queue_attribute, attr)
504
505#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
506
507static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
508 char *buf)
509{
510 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
511 struct netdev_rx_queue *queue = to_rx_queue(kobj);
512
513 if (!attribute->show)
514 return -EIO;
515
516 return attribute->show(queue, attribute, buf);
517}
518
519static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
520 const char *buf, size_t count)
521{
522 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
523 struct netdev_rx_queue *queue = to_rx_queue(kobj);
524
525 if (!attribute->store)
526 return -EIO;
527
528 return attribute->store(queue, attribute, buf, count);
529}
530
fa50d645 531static const struct sysfs_ops rx_queue_sysfs_ops = {
0a9627f2
TH
532 .show = rx_queue_attr_show,
533 .store = rx_queue_attr_store,
534};
535
a953be53 536#ifdef CONFIG_RPS
0a9627f2
TH
537static ssize_t show_rps_map(struct netdev_rx_queue *queue,
538 struct rx_queue_attribute *attribute, char *buf)
539{
540 struct rps_map *map;
541 cpumask_var_t mask;
542 size_t len = 0;
543 int i;
544
545 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
546 return -ENOMEM;
547
548 rcu_read_lock();
549 map = rcu_dereference(queue->rps_map);
550 if (map)
551 for (i = 0; i < map->len; i++)
552 cpumask_set_cpu(map->cpus[i], mask);
553
554 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
555 if (PAGE_SIZE - len < 3) {
556 rcu_read_unlock();
557 free_cpumask_var(mask);
558 return -EINVAL;
559 }
560 rcu_read_unlock();
561
562 free_cpumask_var(mask);
563 len += sprintf(buf + len, "\n");
564 return len;
565}
566
f5acb907 567static ssize_t store_rps_map(struct netdev_rx_queue *queue,
0a9627f2
TH
568 struct rx_queue_attribute *attribute,
569 const char *buf, size_t len)
570{
571 struct rps_map *old_map, *map;
572 cpumask_var_t mask;
573 int err, cpu, i;
574 static DEFINE_SPINLOCK(rps_map_lock);
575
576 if (!capable(CAP_NET_ADMIN))
577 return -EPERM;
578
579 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
580 return -ENOMEM;
581
582 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
583 if (err) {
584 free_cpumask_var(mask);
585 return err;
586 }
587
95c96174 588 map = kzalloc(max_t(unsigned int,
0a9627f2
TH
589 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
590 GFP_KERNEL);
591 if (!map) {
592 free_cpumask_var(mask);
593 return -ENOMEM;
594 }
595
596 i = 0;
597 for_each_cpu_and(cpu, mask, cpu_online_mask)
598 map->cpus[i++] = cpu;
599
600 if (i)
601 map->len = i;
602 else {
603 kfree(map);
604 map = NULL;
605 }
606
607 spin_lock(&rps_map_lock);
6e3f7faf
ED
608 old_map = rcu_dereference_protected(queue->rps_map,
609 lockdep_is_held(&rps_map_lock));
0a9627f2
TH
610 rcu_assign_pointer(queue->rps_map, map);
611 spin_unlock(&rps_map_lock);
612
adc9300e 613 if (map)
c5905afb 614 static_key_slow_inc(&rps_needed);
adc9300e 615 if (old_map) {
f6f80238 616 kfree_rcu(old_map, rcu);
c5905afb 617 static_key_slow_dec(&rps_needed);
adc9300e 618 }
0a9627f2
TH
619 free_cpumask_var(mask);
620 return len;
621}
622
fec5e652
TH
623static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
624 struct rx_queue_attribute *attr,
625 char *buf)
626{
627 struct rps_dev_flow_table *flow_table;
60b778ce 628 unsigned long val = 0;
fec5e652
TH
629
630 rcu_read_lock();
631 flow_table = rcu_dereference(queue->rps_flow_table);
632 if (flow_table)
60b778ce 633 val = (unsigned long)flow_table->mask + 1;
fec5e652
TH
634 rcu_read_unlock();
635
60b778ce 636 return sprintf(buf, "%lu\n", val);
fec5e652
TH
637}
638
fec5e652
TH
639static void rps_dev_flow_table_release(struct rcu_head *rcu)
640{
641 struct rps_dev_flow_table *table = container_of(rcu,
642 struct rps_dev_flow_table, rcu);
243198d0 643 vfree(table);
fec5e652
TH
644}
645
f5acb907 646static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
fec5e652
TH
647 struct rx_queue_attribute *attr,
648 const char *buf, size_t len)
649{
60b778ce 650 unsigned long mask, count;
fec5e652
TH
651 struct rps_dev_flow_table *table, *old_table;
652 static DEFINE_SPINLOCK(rps_dev_flow_lock);
60b778ce 653 int rc;
fec5e652
TH
654
655 if (!capable(CAP_NET_ADMIN))
656 return -EPERM;
657
60b778ce
ED
658 rc = kstrtoul(buf, 0, &count);
659 if (rc < 0)
660 return rc;
fec5e652
TH
661
662 if (count) {
60b778ce
ED
663 mask = count - 1;
664 /* mask = roundup_pow_of_two(count) - 1;
665 * without overflows...
666 */
667 while ((mask | (mask >> 1)) != mask)
668 mask |= (mask >> 1);
669 /* On 64 bit arches, must check mask fits in table->mask (u32),
8e3bff96 670 * and on 32bit arches, must check
671 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
60b778ce
ED
672 */
673#if BITS_PER_LONG > 32
674 if (mask > (unsigned long)(u32)mask)
a0a129f8 675 return -EINVAL;
60b778ce
ED
676#else
677 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
a0a129f8 678 / sizeof(struct rps_dev_flow)) {
fec5e652
TH
679 /* Enforce a limit to prevent overflow */
680 return -EINVAL;
681 }
60b778ce
ED
682#endif
683 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
fec5e652
TH
684 if (!table)
685 return -ENOMEM;
686
60b778ce
ED
687 table->mask = mask;
688 for (count = 0; count <= mask; count++)
689 table->flows[count].cpu = RPS_NO_CPU;
fec5e652
TH
690 } else
691 table = NULL;
692
693 spin_lock(&rps_dev_flow_lock);
6e3f7faf
ED
694 old_table = rcu_dereference_protected(queue->rps_flow_table,
695 lockdep_is_held(&rps_dev_flow_lock));
fec5e652
TH
696 rcu_assign_pointer(queue->rps_flow_table, table);
697 spin_unlock(&rps_dev_flow_lock);
698
699 if (old_table)
700 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
701
702 return len;
703}
704
0a9627f2
TH
705static struct rx_queue_attribute rps_cpus_attribute =
706 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
707
fec5e652
TH
708
709static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
710 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
711 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
a953be53 712#endif /* CONFIG_RPS */
fec5e652 713
0a9627f2 714static struct attribute *rx_queue_default_attrs[] = {
a953be53 715#ifdef CONFIG_RPS
0a9627f2 716 &rps_cpus_attribute.attr,
fec5e652 717 &rps_dev_flow_table_cnt_attribute.attr,
a953be53 718#endif
0a9627f2
TH
719 NULL
720};
721
722static void rx_queue_release(struct kobject *kobj)
723{
724 struct netdev_rx_queue *queue = to_rx_queue(kobj);
a953be53 725#ifdef CONFIG_RPS
6e3f7faf
ED
726 struct rps_map *map;
727 struct rps_dev_flow_table *flow_table;
0a9627f2 728
fec5e652 729
33d480ce 730 map = rcu_dereference_protected(queue->rps_map, 1);
9ea19481
JF
731 if (map) {
732 RCU_INIT_POINTER(queue->rps_map, NULL);
f6f80238 733 kfree_rcu(map, rcu);
9ea19481 734 }
6e3f7faf 735
33d480ce 736 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
9ea19481
JF
737 if (flow_table) {
738 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
6e3f7faf 739 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
9ea19481 740 }
a953be53 741#endif
0a9627f2 742
9ea19481 743 memset(kobj, 0, sizeof(*kobj));
fe822240 744 dev_put(queue->dev);
0a9627f2
TH
745}
746
82ef3d5d
WC
747static const void *rx_queue_namespace(struct kobject *kobj)
748{
749 struct netdev_rx_queue *queue = to_rx_queue(kobj);
750 struct device *dev = &queue->dev->dev;
751 const void *ns = NULL;
752
753 if (dev->class && dev->class->ns_type)
754 ns = dev->class->namespace(dev);
755
756 return ns;
757}
758
0a9627f2
TH
759static struct kobj_type rx_queue_ktype = {
760 .sysfs_ops = &rx_queue_sysfs_ops,
761 .release = rx_queue_release,
762 .default_attrs = rx_queue_default_attrs,
82ef3d5d 763 .namespace = rx_queue_namespace
0a9627f2
TH
764};
765
766static int rx_queue_add_kobject(struct net_device *net, int index)
767{
768 struct netdev_rx_queue *queue = net->_rx + index;
769 struct kobject *kobj = &queue->kobj;
770 int error = 0;
771
772 kobj->kset = net->queues_kset;
773 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
774 "rx-%u", index);
a953be53
MD
775 if (error)
776 goto exit;
777
778 if (net->sysfs_rx_queue_group) {
779 error = sysfs_create_group(kobj, net->sysfs_rx_queue_group);
780 if (error)
781 goto exit;
0a9627f2
TH
782 }
783
784 kobject_uevent(kobj, KOBJ_ADD);
fe822240 785 dev_hold(queue->dev);
0a9627f2 786
a953be53
MD
787 return error;
788exit:
789 kobject_put(kobj);
0a9627f2
TH
790 return error;
791}
a953be53 792#endif /* CONFIG_SYFS */
0a9627f2 793
62fe0b40
BH
794int
795net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
0a9627f2 796{
a953be53 797#ifdef CONFIG_SYSFS
0a9627f2
TH
798 int i;
799 int error = 0;
800
a953be53
MD
801#ifndef CONFIG_RPS
802 if (!net->sysfs_rx_queue_group)
803 return 0;
804#endif
62fe0b40 805 for (i = old_num; i < new_num; i++) {
0a9627f2 806 error = rx_queue_add_kobject(net, i);
62fe0b40
BH
807 if (error) {
808 new_num = old_num;
0a9627f2 809 break;
62fe0b40 810 }
0a9627f2
TH
811 }
812
a953be53
MD
813 while (--i >= new_num) {
814 if (net->sysfs_rx_queue_group)
815 sysfs_remove_group(&net->_rx[i].kobj,
816 net->sysfs_rx_queue_group);
62fe0b40 817 kobject_put(&net->_rx[i].kobj);
a953be53 818 }
0a9627f2
TH
819
820 return error;
bf264145
TH
821#else
822 return 0;
823#endif
0a9627f2
TH
824}
825
ccf5ff69 826#ifdef CONFIG_SYSFS
1d24eb48
TH
827/*
828 * netdev_queue sysfs structures and functions.
829 */
830struct netdev_queue_attribute {
831 struct attribute attr;
832 ssize_t (*show)(struct netdev_queue *queue,
833 struct netdev_queue_attribute *attr, char *buf);
834 ssize_t (*store)(struct netdev_queue *queue,
835 struct netdev_queue_attribute *attr, const char *buf, size_t len);
836};
837#define to_netdev_queue_attr(_attr) container_of(_attr, \
838 struct netdev_queue_attribute, attr)
839
840#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
841
842static ssize_t netdev_queue_attr_show(struct kobject *kobj,
843 struct attribute *attr, char *buf)
844{
845 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
846 struct netdev_queue *queue = to_netdev_queue(kobj);
847
848 if (!attribute->show)
849 return -EIO;
850
851 return attribute->show(queue, attribute, buf);
852}
853
854static ssize_t netdev_queue_attr_store(struct kobject *kobj,
855 struct attribute *attr,
856 const char *buf, size_t count)
857{
858 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
859 struct netdev_queue *queue = to_netdev_queue(kobj);
860
861 if (!attribute->store)
862 return -EIO;
863
864 return attribute->store(queue, attribute, buf, count);
865}
866
867static const struct sysfs_ops netdev_queue_sysfs_ops = {
868 .show = netdev_queue_attr_show,
869 .store = netdev_queue_attr_store,
870};
871
ccf5ff69 872static ssize_t show_trans_timeout(struct netdev_queue *queue,
873 struct netdev_queue_attribute *attribute,
874 char *buf)
875{
876 unsigned long trans_timeout;
877
878 spin_lock_irq(&queue->_xmit_lock);
879 trans_timeout = queue->trans_timeout;
880 spin_unlock_irq(&queue->_xmit_lock);
881
882 return sprintf(buf, "%lu", trans_timeout);
883}
884
885static struct netdev_queue_attribute queue_trans_timeout =
886 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
887
114cf580
TH
888#ifdef CONFIG_BQL
889/*
890 * Byte queue limits sysfs structures and functions.
891 */
892static ssize_t bql_show(char *buf, unsigned int value)
893{
894 return sprintf(buf, "%u\n", value);
895}
896
897static ssize_t bql_set(const char *buf, const size_t count,
898 unsigned int *pvalue)
899{
900 unsigned int value;
901 int err;
902
903 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
904 value = DQL_MAX_LIMIT;
905 else {
906 err = kstrtouint(buf, 10, &value);
907 if (err < 0)
908 return err;
909 if (value > DQL_MAX_LIMIT)
910 return -EINVAL;
911 }
912
913 *pvalue = value;
914
915 return count;
916}
917
918static ssize_t bql_show_hold_time(struct netdev_queue *queue,
919 struct netdev_queue_attribute *attr,
920 char *buf)
921{
922 struct dql *dql = &queue->dql;
923
924 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
925}
926
927static ssize_t bql_set_hold_time(struct netdev_queue *queue,
928 struct netdev_queue_attribute *attribute,
929 const char *buf, size_t len)
930{
931 struct dql *dql = &queue->dql;
95c96174 932 unsigned int value;
114cf580
TH
933 int err;
934
935 err = kstrtouint(buf, 10, &value);
936 if (err < 0)
937 return err;
938
939 dql->slack_hold_time = msecs_to_jiffies(value);
940
941 return len;
942}
943
944static struct netdev_queue_attribute bql_hold_time_attribute =
945 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
946 bql_set_hold_time);
947
948static ssize_t bql_show_inflight(struct netdev_queue *queue,
949 struct netdev_queue_attribute *attr,
950 char *buf)
951{
952 struct dql *dql = &queue->dql;
953
954 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
955}
956
957static struct netdev_queue_attribute bql_inflight_attribute =
795d9a25 958 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
114cf580
TH
959
960#define BQL_ATTR(NAME, FIELD) \
961static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
962 struct netdev_queue_attribute *attr, \
963 char *buf) \
964{ \
965 return bql_show(buf, queue->dql.FIELD); \
966} \
967 \
968static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
969 struct netdev_queue_attribute *attr, \
970 const char *buf, size_t len) \
971{ \
972 return bql_set(buf, len, &queue->dql.FIELD); \
973} \
974 \
975static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
976 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
977 bql_set_ ## NAME);
978
979BQL_ATTR(limit, limit)
980BQL_ATTR(limit_max, max_limit)
981BQL_ATTR(limit_min, min_limit)
982
983static struct attribute *dql_attrs[] = {
984 &bql_limit_attribute.attr,
985 &bql_limit_max_attribute.attr,
986 &bql_limit_min_attribute.attr,
987 &bql_hold_time_attribute.attr,
988 &bql_inflight_attribute.attr,
989 NULL
990};
991
992static struct attribute_group dql_group = {
993 .name = "byte_queue_limits",
994 .attrs = dql_attrs,
995};
996#endif /* CONFIG_BQL */
997
ccf5ff69 998#ifdef CONFIG_XPS
ed1acc8c 999static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
0a9627f2 1000{
1d24eb48 1001 struct net_device *dev = queue->dev;
ed1acc8c 1002 unsigned int i;
1d24eb48 1003
ed1acc8c 1004 i = queue - dev->_tx;
1d24eb48
TH
1005 BUG_ON(i >= dev->num_tx_queues);
1006
1007 return i;
1008}
1009
1010
1011static ssize_t show_xps_map(struct netdev_queue *queue,
1012 struct netdev_queue_attribute *attribute, char *buf)
1013{
1014 struct net_device *dev = queue->dev;
1015 struct xps_dev_maps *dev_maps;
1016 cpumask_var_t mask;
1017 unsigned long index;
1018 size_t len = 0;
1019 int i;
1020
1021 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1022 return -ENOMEM;
1023
1024 index = get_netdev_queue_index(queue);
1025
1026 rcu_read_lock();
1027 dev_maps = rcu_dereference(dev->xps_maps);
1028 if (dev_maps) {
1029 for_each_possible_cpu(i) {
1030 struct xps_map *map =
1031 rcu_dereference(dev_maps->cpu_map[i]);
1032 if (map) {
1033 int j;
1034 for (j = 0; j < map->len; j++) {
1035 if (map->queues[j] == index) {
1036 cpumask_set_cpu(i, mask);
1037 break;
1038 }
1039 }
1040 }
1041 }
1042 }
1043 rcu_read_unlock();
1044
1045 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
1046 if (PAGE_SIZE - len < 3) {
1047 free_cpumask_var(mask);
1048 return -EINVAL;
1049 }
1050
1051 free_cpumask_var(mask);
1052 len += sprintf(buf + len, "\n");
1053 return len;
1054}
1055
1d24eb48
TH
1056static ssize_t store_xps_map(struct netdev_queue *queue,
1057 struct netdev_queue_attribute *attribute,
1058 const char *buf, size_t len)
1059{
1060 struct net_device *dev = queue->dev;
1d24eb48 1061 unsigned long index;
537c00de
AD
1062 cpumask_var_t mask;
1063 int err;
1d24eb48
TH
1064
1065 if (!capable(CAP_NET_ADMIN))
1066 return -EPERM;
1067
1068 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1069 return -ENOMEM;
1070
1071 index = get_netdev_queue_index(queue);
1072
1073 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1074 if (err) {
1075 free_cpumask_var(mask);
1076 return err;
1077 }
1078
537c00de 1079 err = netif_set_xps_queue(dev, mask, index);
1d24eb48
TH
1080
1081 free_cpumask_var(mask);
1d24eb48 1082
537c00de 1083 return err ? : len;
1d24eb48
TH
1084}
1085
1086static struct netdev_queue_attribute xps_cpus_attribute =
1087 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
ccf5ff69 1088#endif /* CONFIG_XPS */
1d24eb48
TH
1089
1090static struct attribute *netdev_queue_default_attrs[] = {
ccf5ff69 1091 &queue_trans_timeout.attr,
1092#ifdef CONFIG_XPS
1d24eb48 1093 &xps_cpus_attribute.attr,
ccf5ff69 1094#endif
1d24eb48
TH
1095 NULL
1096};
1097
1098static void netdev_queue_release(struct kobject *kobj)
1099{
1100 struct netdev_queue *queue = to_netdev_queue(kobj);
1d24eb48 1101
1d24eb48
TH
1102 memset(kobj, 0, sizeof(*kobj));
1103 dev_put(queue->dev);
1104}
1105
82ef3d5d
WC
1106static const void *netdev_queue_namespace(struct kobject *kobj)
1107{
1108 struct netdev_queue *queue = to_netdev_queue(kobj);
1109 struct device *dev = &queue->dev->dev;
1110 const void *ns = NULL;
1111
1112 if (dev->class && dev->class->ns_type)
1113 ns = dev->class->namespace(dev);
1114
1115 return ns;
1116}
1117
1d24eb48
TH
1118static struct kobj_type netdev_queue_ktype = {
1119 .sysfs_ops = &netdev_queue_sysfs_ops,
1120 .release = netdev_queue_release,
1121 .default_attrs = netdev_queue_default_attrs,
82ef3d5d 1122 .namespace = netdev_queue_namespace,
1d24eb48
TH
1123};
1124
1125static int netdev_queue_add_kobject(struct net_device *net, int index)
1126{
1127 struct netdev_queue *queue = net->_tx + index;
1128 struct kobject *kobj = &queue->kobj;
1129 int error = 0;
1130
1131 kobj->kset = net->queues_kset;
1132 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1133 "tx-%u", index);
114cf580
TH
1134 if (error)
1135 goto exit;
1136
1137#ifdef CONFIG_BQL
1138 error = sysfs_create_group(kobj, &dql_group);
1139 if (error)
1140 goto exit;
1141#endif
1d24eb48
TH
1142
1143 kobject_uevent(kobj, KOBJ_ADD);
1144 dev_hold(queue->dev);
1145
114cf580
TH
1146 return 0;
1147exit:
1148 kobject_put(kobj);
1d24eb48
TH
1149 return error;
1150}
ccf5ff69 1151#endif /* CONFIG_SYSFS */
1d24eb48
TH
1152
1153int
1154netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1155{
ccf5ff69 1156#ifdef CONFIG_SYSFS
1d24eb48
TH
1157 int i;
1158 int error = 0;
1159
1160 for (i = old_num; i < new_num; i++) {
1161 error = netdev_queue_add_kobject(net, i);
1162 if (error) {
1163 new_num = old_num;
1164 break;
1165 }
1166 }
1167
114cf580
TH
1168 while (--i >= new_num) {
1169 struct netdev_queue *queue = net->_tx + i;
1170
1171#ifdef CONFIG_BQL
1172 sysfs_remove_group(&queue->kobj, &dql_group);
1173#endif
1174 kobject_put(&queue->kobj);
1175 }
1d24eb48
TH
1176
1177 return error;
bf264145
TH
1178#else
1179 return 0;
ccf5ff69 1180#endif /* CONFIG_SYSFS */
1d24eb48
TH
1181}
1182
1183static int register_queue_kobjects(struct net_device *net)
1184{
bf264145 1185 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1d24eb48 1186
ccf5ff69 1187#ifdef CONFIG_SYSFS
62fe0b40
BH
1188 net->queues_kset = kset_create_and_add("queues",
1189 NULL, &net->dev.kobj);
1190 if (!net->queues_kset)
1191 return -ENOMEM;
bf264145
TH
1192 real_rx = net->real_num_rx_queues;
1193#endif
1194 real_tx = net->real_num_tx_queues;
1d24eb48 1195
bf264145 1196 error = net_rx_queue_update_kobjects(net, 0, real_rx);
1d24eb48
TH
1197 if (error)
1198 goto error;
bf264145 1199 rxq = real_rx;
1d24eb48 1200
bf264145 1201 error = netdev_queue_update_kobjects(net, 0, real_tx);
1d24eb48
TH
1202 if (error)
1203 goto error;
bf264145 1204 txq = real_tx;
1d24eb48
TH
1205
1206 return 0;
1207
1208error:
1209 netdev_queue_update_kobjects(net, txq, 0);
1210 net_rx_queue_update_kobjects(net, rxq, 0);
1211 return error;
62fe0b40 1212}
0a9627f2 1213
1d24eb48 1214static void remove_queue_kobjects(struct net_device *net)
62fe0b40 1215{
bf264145
TH
1216 int real_rx = 0, real_tx = 0;
1217
a953be53 1218#ifdef CONFIG_SYSFS
bf264145
TH
1219 real_rx = net->real_num_rx_queues;
1220#endif
1221 real_tx = net->real_num_tx_queues;
1222
1223 net_rx_queue_update_kobjects(net, real_rx, 0);
1224 netdev_queue_update_kobjects(net, real_tx, 0);
ccf5ff69 1225#ifdef CONFIG_SYSFS
0a9627f2 1226 kset_unregister(net->queues_kset);
bf264145 1227#endif
0a9627f2 1228}
608b4b95 1229
7dc5dbc8
EB
1230static bool net_current_may_mount(void)
1231{
1232 struct net *net = current->nsproxy->net_ns;
1233
1234 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1235}
1236
a685e089 1237static void *net_grab_current_ns(void)
608b4b95 1238{
a685e089
AV
1239 struct net *ns = current->nsproxy->net_ns;
1240#ifdef CONFIG_NET_NS
1241 if (ns)
1242 atomic_inc(&ns->passive);
1243#endif
1244 return ns;
608b4b95
EB
1245}
1246
1247static const void *net_initial_ns(void)
1248{
1249 return &init_net;
1250}
1251
1252static const void *net_netlink_ns(struct sock *sk)
1253{
1254 return sock_net(sk);
1255}
1256
04600794 1257struct kobj_ns_type_operations net_ns_type_operations = {
608b4b95 1258 .type = KOBJ_NS_TYPE_NET,
7dc5dbc8 1259 .current_may_mount = net_current_may_mount,
a685e089 1260 .grab_current_ns = net_grab_current_ns,
608b4b95
EB
1261 .netlink_ns = net_netlink_ns,
1262 .initial_ns = net_initial_ns,
a685e089 1263 .drop_ns = net_drop_ns,
608b4b95 1264};
04600794 1265EXPORT_SYMBOL_GPL(net_ns_type_operations);
608b4b95 1266
7eff2e7a 1267static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1da177e4 1268{
43cb76d9 1269 struct net_device *dev = to_net_dev(d);
7eff2e7a 1270 int retval;
1da177e4 1271
312c004d 1272 /* pass interface to uevent. */
7eff2e7a 1273 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
bf62456e
ER
1274 if (retval)
1275 goto exit;
ca2f37db
JT
1276
1277 /* pass ifindex to uevent.
1278 * ifindex is useful as it won't change (interface name may change)
1279 * and is what RtNetlink uses natively. */
7eff2e7a 1280 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1da177e4 1281
bf62456e 1282exit:
bf62456e 1283 return retval;
1da177e4 1284}
1da177e4
LT
1285
1286/*
4ec93edb 1287 * netdev_release -- destroy and free a dead device.
43cb76d9 1288 * Called when last reference to device kobject is gone.
1da177e4 1289 */
43cb76d9 1290static void netdev_release(struct device *d)
1da177e4 1291{
43cb76d9 1292 struct net_device *dev = to_net_dev(d);
1da177e4
LT
1293
1294 BUG_ON(dev->reg_state != NETREG_RELEASED);
1295
0b815a1a 1296 kfree(dev->ifalias);
74d332c1 1297 netdev_freemem(dev);
1da177e4
LT
1298}
1299
608b4b95
EB
1300static const void *net_namespace(struct device *d)
1301{
1302 struct net_device *dev;
1303 dev = container_of(d, struct net_device, dev);
1304 return dev_net(dev);
1305}
1306
1da177e4
LT
1307static struct class net_class = {
1308 .name = "net",
43cb76d9 1309 .dev_release = netdev_release,
6be8aeef 1310 .dev_groups = net_class_groups,
43cb76d9 1311 .dev_uevent = netdev_uevent,
608b4b95
EB
1312 .ns_type = &net_ns_type_operations,
1313 .namespace = net_namespace,
1da177e4
LT
1314};
1315
9093bbb2
SH
1316/* Delete sysfs entries but hold kobject reference until after all
1317 * netdev references are gone.
1318 */
8b41d188 1319void netdev_unregister_kobject(struct net_device * net)
1da177e4 1320{
9093bbb2
SH
1321 struct device *dev = &(net->dev);
1322
1323 kobject_get(&dev->kobj);
3891845e 1324
1d24eb48 1325 remove_queue_kobjects(net);
0a9627f2 1326
9802c8e2
ML
1327 pm_runtime_set_memalloc_noio(dev, false);
1328
9093bbb2 1329 device_del(dev);
1da177e4
LT
1330}
1331
1332/* Create sysfs entries for network device. */
8b41d188 1333int netdev_register_kobject(struct net_device *net)
1da177e4 1334{
43cb76d9 1335 struct device *dev = &(net->dev);
a4dbd674 1336 const struct attribute_group **groups = net->sysfs_groups;
0a9627f2 1337 int error = 0;
1da177e4 1338
a1b3f594 1339 device_initialize(dev);
43cb76d9
GKH
1340 dev->class = &net_class;
1341 dev->platform_data = net;
1342 dev->groups = groups;
1da177e4 1343
a2205472 1344 dev_set_name(dev, "%s", net->name);
1da177e4 1345
8b41d188 1346#ifdef CONFIG_SYSFS
0c509a6c
EB
1347 /* Allow for a device specific group */
1348 if (*groups)
1349 groups++;
1da177e4 1350
0c509a6c 1351 *groups++ = &netstat_group;
38c1a01c
JB
1352
1353#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1354 if (net->ieee80211_ptr)
1355 *groups++ = &wireless_group;
1356#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1357 else if (net->wireless_handlers)
1358 *groups++ = &wireless_group;
1359#endif
1360#endif
8b41d188 1361#endif /* CONFIG_SYSFS */
1da177e4 1362
0a9627f2
TH
1363 error = device_add(dev);
1364 if (error)
1365 return error;
1366
1d24eb48 1367 error = register_queue_kobjects(net);
0a9627f2
TH
1368 if (error) {
1369 device_del(dev);
1370 return error;
1371 }
1372
9802c8e2
ML
1373 pm_runtime_set_memalloc_noio(dev, true);
1374
0a9627f2 1375 return error;
1da177e4
LT
1376}
1377
58292cbe
TH
1378int netdev_class_create_file_ns(struct class_attribute *class_attr,
1379 const void *ns)
b8a9787e 1380{
58292cbe 1381 return class_create_file_ns(&net_class, class_attr, ns);
b8a9787e 1382}
58292cbe 1383EXPORT_SYMBOL(netdev_class_create_file_ns);
b8a9787e 1384
58292cbe
TH
1385void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1386 const void *ns)
b8a9787e 1387{
58292cbe 1388 class_remove_file_ns(&net_class, class_attr, ns);
b8a9787e 1389}
58292cbe 1390EXPORT_SYMBOL(netdev_class_remove_file_ns);
b8a9787e 1391
a48d4bb0 1392int __init netdev_kobject_init(void)
1da177e4 1393{
608b4b95 1394 kobj_ns_type_register(&net_ns_type_operations);
1da177e4
LT
1395 return class_register(&net_class);
1396}