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