xps: Add xps_queue_release function
[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
LT
20#include <linux/rtnetlink.h>
21#include <linux/wireless.h>
fec5e652 22#include <linux/vmalloc.h>
bc3b2d7f 23#include <linux/export.h>
8f1546ca 24#include <net/wext.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} \
43cb76d9
GKH
63static ssize_t show_##field(struct device *dev, \
64 struct device_attribute *attr, char *buf) \
1da177e4 65{ \
43cb76d9 66 return netdev_show(dev, attr, buf, format_##field); \
1da177e4
LT
67}
68
69
70/* use same locking and permission rules as SIF* ioctl's */
43cb76d9 71static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
1da177e4
LT
72 const char *buf, size_t len,
73 int (*set)(struct net_device *, unsigned long))
74{
75 struct net_device *net = to_net_dev(dev);
76 char *endp;
77 unsigned long new;
78 int ret = -EINVAL;
79
80 if (!capable(CAP_NET_ADMIN))
81 return -EPERM;
82
83 new = simple_strtoul(buf, &endp, 0);
84 if (endp == buf)
85 goto err;
86
5a5990d3 87 if (!rtnl_trylock())
336ca57c 88 return restart_syscall();
5a5990d3 89
1da177e4
LT
90 if (dev_isalive(net)) {
91 if ((ret = (*set)(net, new)) == 0)
92 ret = len;
93 }
94 rtnl_unlock();
95 err:
96 return ret;
97}
98
9d29672c 99NETDEVICE_SHOW(dev_id, fmt_hex);
c1f79426 100NETDEVICE_SHOW(addr_assign_type, fmt_dec);
fd586bac
KS
101NETDEVICE_SHOW(addr_len, fmt_dec);
102NETDEVICE_SHOW(iflink, fmt_dec);
103NETDEVICE_SHOW(ifindex, fmt_dec);
fd586bac 104NETDEVICE_SHOW(type, fmt_dec);
b00055aa 105NETDEVICE_SHOW(link_mode, fmt_dec);
1da177e4
LT
106
107/* use same locking rules as GIFHWADDR ioctl's */
43cb76d9
GKH
108static ssize_t show_address(struct device *dev, struct device_attribute *attr,
109 char *buf)
1da177e4
LT
110{
111 struct net_device *net = to_net_dev(dev);
112 ssize_t ret = -EINVAL;
113
114 read_lock(&dev_base_lock);
115 if (dev_isalive(net))
7ffc49a6 116 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
1da177e4
LT
117 read_unlock(&dev_base_lock);
118 return ret;
119}
120
43cb76d9
GKH
121static ssize_t show_broadcast(struct device *dev,
122 struct device_attribute *attr, char *buf)
1da177e4
LT
123{
124 struct net_device *net = to_net_dev(dev);
125 if (dev_isalive(net))
7ffc49a6 126 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
1da177e4
LT
127 return -EINVAL;
128}
129
43cb76d9
GKH
130static ssize_t show_carrier(struct device *dev,
131 struct device_attribute *attr, char *buf)
1da177e4
LT
132{
133 struct net_device *netdev = to_net_dev(dev);
134 if (netif_running(netdev)) {
135 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
136 }
137 return -EINVAL;
138}
139
d519e17e
AG
140static ssize_t show_speed(struct device *dev,
141 struct device_attribute *attr, char *buf)
142{
143 struct net_device *netdev = to_net_dev(dev);
144 int ret = -EINVAL;
145
146 if (!rtnl_trylock())
147 return restart_syscall();
148
8ae6daca
DD
149 if (netif_running(netdev)) {
150 struct ethtool_cmd cmd;
4bc71cb9 151 if (!__ethtool_get_settings(netdev, &cmd))
8ae6daca 152 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
d519e17e
AG
153 }
154 rtnl_unlock();
155 return ret;
156}
157
158static ssize_t show_duplex(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
161 struct net_device *netdev = to_net_dev(dev);
162 int ret = -EINVAL;
163
164 if (!rtnl_trylock())
165 return restart_syscall();
166
8ae6daca
DD
167 if (netif_running(netdev)) {
168 struct ethtool_cmd cmd;
4bc71cb9 169 if (!__ethtool_get_settings(netdev, &cmd))
8ae6daca
DD
170 ret = sprintf(buf, "%s\n",
171 cmd.duplex ? "full" : "half");
d519e17e
AG
172 }
173 rtnl_unlock();
174 return ret;
175}
176
43cb76d9
GKH
177static ssize_t show_dormant(struct device *dev,
178 struct device_attribute *attr, char *buf)
b00055aa
SR
179{
180 struct net_device *netdev = to_net_dev(dev);
181
182 if (netif_running(netdev))
183 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
184
185 return -EINVAL;
186}
187
36cbd3dc 188static const char *const operstates[] = {
b00055aa
SR
189 "unknown",
190 "notpresent", /* currently unused */
191 "down",
192 "lowerlayerdown",
193 "testing", /* currently unused */
194 "dormant",
195 "up"
196};
197
43cb76d9
GKH
198static ssize_t show_operstate(struct device *dev,
199 struct device_attribute *attr, char *buf)
b00055aa
SR
200{
201 const struct net_device *netdev = to_net_dev(dev);
202 unsigned char operstate;
203
204 read_lock(&dev_base_lock);
205 operstate = netdev->operstate;
206 if (!netif_running(netdev))
207 operstate = IF_OPER_DOWN;
208 read_unlock(&dev_base_lock);
209
e3a5cd9e 210 if (operstate >= ARRAY_SIZE(operstates))
b00055aa
SR
211 return -EINVAL; /* should not happen */
212
213 return sprintf(buf, "%s\n", operstates[operstate]);
214}
215
1da177e4
LT
216/* read-write attributes */
217NETDEVICE_SHOW(mtu, fmt_dec);
218
219static int change_mtu(struct net_device *net, unsigned long new_mtu)
220{
221 return dev_set_mtu(net, (int) new_mtu);
222}
223
43cb76d9
GKH
224static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
225 const char *buf, size_t len)
1da177e4 226{
43cb76d9 227 return netdev_store(dev, attr, buf, len, change_mtu);
1da177e4
LT
228}
229
1da177e4
LT
230NETDEVICE_SHOW(flags, fmt_hex);
231
232static int change_flags(struct net_device *net, unsigned long new_flags)
233{
234 return dev_change_flags(net, (unsigned) new_flags);
235}
236
43cb76d9
GKH
237static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t len)
1da177e4 239{
43cb76d9 240 return netdev_store(dev, attr, buf, len, change_flags);
1da177e4
LT
241}
242
1da177e4
LT
243NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
244
245static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
246{
247 net->tx_queue_len = new_len;
248 return 0;
249}
250
43cb76d9
GKH
251static ssize_t store_tx_queue_len(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t len)
1da177e4 254{
43cb76d9 255 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
1da177e4
LT
256}
257
0b815a1a
SH
258static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t len)
260{
261 struct net_device *netdev = to_net_dev(dev);
262 size_t count = len;
263 ssize_t ret;
264
265 if (!capable(CAP_NET_ADMIN))
266 return -EPERM;
267
268 /* ignore trailing newline */
269 if (len > 0 && buf[len - 1] == '\n')
270 --count;
271
336ca57c
EB
272 if (!rtnl_trylock())
273 return restart_syscall();
0b815a1a
SH
274 ret = dev_set_alias(netdev, buf, count);
275 rtnl_unlock();
276
277 return ret < 0 ? ret : len;
278}
279
280static ssize_t show_ifalias(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
283 const struct net_device *netdev = to_net_dev(dev);
284 ssize_t ret = 0;
285
336ca57c
EB
286 if (!rtnl_trylock())
287 return restart_syscall();
0b815a1a
SH
288 if (netdev->ifalias)
289 ret = sprintf(buf, "%s\n", netdev->ifalias);
290 rtnl_unlock();
291 return ret;
292}
293
a512b92b
VD
294NETDEVICE_SHOW(group, fmt_dec);
295
296static int change_group(struct net_device *net, unsigned long new_group)
297{
298 dev_set_group(net, (int) new_group);
299 return 0;
300}
301
302static ssize_t store_group(struct device *dev, struct device_attribute *attr,
303 const char *buf, size_t len)
304{
305 return netdev_store(dev, attr, buf, len, change_group);
306}
307
43cb76d9 308static struct device_attribute net_class_attributes[] = {
c1f79426 309 __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
fd586bac 310 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
9d29672c 311 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
0b815a1a 312 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
fd586bac
KS
313 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
314 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
fd586bac 315 __ATTR(type, S_IRUGO, show_type, NULL),
b00055aa 316 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
fd586bac
KS
317 __ATTR(address, S_IRUGO, show_address, NULL),
318 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
319 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
d519e17e
AG
320 __ATTR(speed, S_IRUGO, show_speed, NULL),
321 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
b00055aa
SR
322 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
323 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
fd586bac
KS
324 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
325 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
326 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
327 store_tx_queue_len),
b6644cb7 328 __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
fd586bac 329 {}
1da177e4
LT
330};
331
332/* Show a given an attribute in the statistics group */
43cb76d9
GKH
333static ssize_t netstat_show(const struct device *d,
334 struct device_attribute *attr, char *buf,
1da177e4
LT
335 unsigned long offset)
336{
43cb76d9 337 struct net_device *dev = to_net_dev(d);
1da177e4
LT
338 ssize_t ret = -EINVAL;
339
be1f3c2c
BH
340 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
341 offset % sizeof(u64) != 0);
1da177e4
LT
342
343 read_lock(&dev_base_lock);
96e74088 344 if (dev_isalive(dev)) {
28172739
ED
345 struct rtnl_link_stats64 temp;
346 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
347
be1f3c2c 348 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
96e74088 349 }
1da177e4
LT
350 read_unlock(&dev_base_lock);
351 return ret;
352}
353
354/* generate a read-only statistics attribute */
355#define NETSTAT_ENTRY(name) \
43cb76d9
GKH
356static ssize_t show_##name(struct device *d, \
357 struct device_attribute *attr, char *buf) \
1da177e4 358{ \
43cb76d9 359 return netstat_show(d, attr, buf, \
be1f3c2c 360 offsetof(struct rtnl_link_stats64, name)); \
1da177e4 361} \
43cb76d9 362static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
1da177e4
LT
363
364NETSTAT_ENTRY(rx_packets);
365NETSTAT_ENTRY(tx_packets);
366NETSTAT_ENTRY(rx_bytes);
367NETSTAT_ENTRY(tx_bytes);
368NETSTAT_ENTRY(rx_errors);
369NETSTAT_ENTRY(tx_errors);
370NETSTAT_ENTRY(rx_dropped);
371NETSTAT_ENTRY(tx_dropped);
372NETSTAT_ENTRY(multicast);
373NETSTAT_ENTRY(collisions);
374NETSTAT_ENTRY(rx_length_errors);
375NETSTAT_ENTRY(rx_over_errors);
376NETSTAT_ENTRY(rx_crc_errors);
377NETSTAT_ENTRY(rx_frame_errors);
378NETSTAT_ENTRY(rx_fifo_errors);
379NETSTAT_ENTRY(rx_missed_errors);
380NETSTAT_ENTRY(tx_aborted_errors);
381NETSTAT_ENTRY(tx_carrier_errors);
382NETSTAT_ENTRY(tx_fifo_errors);
383NETSTAT_ENTRY(tx_heartbeat_errors);
384NETSTAT_ENTRY(tx_window_errors);
385NETSTAT_ENTRY(rx_compressed);
386NETSTAT_ENTRY(tx_compressed);
387
388static struct attribute *netstat_attrs[] = {
43cb76d9
GKH
389 &dev_attr_rx_packets.attr,
390 &dev_attr_tx_packets.attr,
391 &dev_attr_rx_bytes.attr,
392 &dev_attr_tx_bytes.attr,
393 &dev_attr_rx_errors.attr,
394 &dev_attr_tx_errors.attr,
395 &dev_attr_rx_dropped.attr,
396 &dev_attr_tx_dropped.attr,
397 &dev_attr_multicast.attr,
398 &dev_attr_collisions.attr,
399 &dev_attr_rx_length_errors.attr,
400 &dev_attr_rx_over_errors.attr,
401 &dev_attr_rx_crc_errors.attr,
402 &dev_attr_rx_frame_errors.attr,
403 &dev_attr_rx_fifo_errors.attr,
404 &dev_attr_rx_missed_errors.attr,
405 &dev_attr_tx_aborted_errors.attr,
406 &dev_attr_tx_carrier_errors.attr,
407 &dev_attr_tx_fifo_errors.attr,
408 &dev_attr_tx_heartbeat_errors.attr,
409 &dev_attr_tx_window_errors.attr,
410 &dev_attr_rx_compressed.attr,
411 &dev_attr_tx_compressed.attr,
1da177e4
LT
412 NULL
413};
414
415
416static struct attribute_group netstat_group = {
417 .name = "statistics",
418 .attrs = netstat_attrs,
419};
420
22bb1be4 421#ifdef CONFIG_WIRELESS_EXT_SYSFS
1da177e4 422/* helper function that does all the locking etc for wireless stats */
43cb76d9 423static ssize_t wireless_show(struct device *d, char *buf,
1da177e4
LT
424 ssize_t (*format)(const struct iw_statistics *,
425 char *))
426{
43cb76d9 427 struct net_device *dev = to_net_dev(d);
8f1546ca 428 const struct iw_statistics *iw;
1da177e4 429 ssize_t ret = -EINVAL;
4ec93edb 430
b8afe641
EB
431 if (!rtnl_trylock())
432 return restart_syscall();
6dd214b5 433 if (dev_isalive(dev)) {
8f1546ca
JB
434 iw = get_wireless_stats(dev);
435 if (iw)
6dd214b5
AB
436 ret = (*format)(iw, buf);
437 }
a160ee69 438 rtnl_unlock();
1da177e4
LT
439
440 return ret;
441}
442
443/* show function template for wireless fields */
444#define WIRELESS_SHOW(name, field, format_string) \
445static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
446{ \
447 return sprintf(buf, format_string, iw->field); \
448} \
43cb76d9
GKH
449static ssize_t show_iw_##name(struct device *d, \
450 struct device_attribute *attr, char *buf) \
1da177e4 451{ \
43cb76d9 452 return wireless_show(d, buf, format_iw_##name); \
1da177e4 453} \
43cb76d9 454static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
1da177e4
LT
455
456WIRELESS_SHOW(status, status, fmt_hex);
457WIRELESS_SHOW(link, qual.qual, fmt_dec);
458WIRELESS_SHOW(level, qual.level, fmt_dec);
459WIRELESS_SHOW(noise, qual.noise, fmt_dec);
460WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
461WIRELESS_SHOW(crypt, discard.code, fmt_dec);
462WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
463WIRELESS_SHOW(misc, discard.misc, fmt_dec);
464WIRELESS_SHOW(retries, discard.retries, fmt_dec);
465WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
466
467static struct attribute *wireless_attrs[] = {
43cb76d9
GKH
468 &dev_attr_status.attr,
469 &dev_attr_link.attr,
470 &dev_attr_level.attr,
471 &dev_attr_noise.attr,
472 &dev_attr_nwid.attr,
473 &dev_attr_crypt.attr,
474 &dev_attr_fragment.attr,
475 &dev_attr_retries.attr,
476 &dev_attr_misc.attr,
477 &dev_attr_beacon.attr,
1da177e4
LT
478 NULL
479};
480
481static struct attribute_group wireless_group = {
482 .name = "wireless",
483 .attrs = wireless_attrs,
484};
485#endif
d6523ddf 486#endif /* CONFIG_SYSFS */
1da177e4 487
30bde1f5 488#ifdef CONFIG_RPS
0a9627f2
TH
489/*
490 * RX queue sysfs structures and functions.
491 */
492struct rx_queue_attribute {
493 struct attribute attr;
494 ssize_t (*show)(struct netdev_rx_queue *queue,
495 struct rx_queue_attribute *attr, char *buf);
496 ssize_t (*store)(struct netdev_rx_queue *queue,
497 struct rx_queue_attribute *attr, const char *buf, size_t len);
498};
499#define to_rx_queue_attr(_attr) container_of(_attr, \
500 struct rx_queue_attribute, attr)
501
502#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
503
504static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
505 char *buf)
506{
507 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
508 struct netdev_rx_queue *queue = to_rx_queue(kobj);
509
510 if (!attribute->show)
511 return -EIO;
512
513 return attribute->show(queue, attribute, buf);
514}
515
516static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
517 const char *buf, size_t count)
518{
519 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
520 struct netdev_rx_queue *queue = to_rx_queue(kobj);
521
522 if (!attribute->store)
523 return -EIO;
524
525 return attribute->store(queue, attribute, buf, count);
526}
527
fa50d645 528static const struct sysfs_ops rx_queue_sysfs_ops = {
0a9627f2
TH
529 .show = rx_queue_attr_show,
530 .store = rx_queue_attr_store,
531};
532
533static ssize_t show_rps_map(struct netdev_rx_queue *queue,
534 struct rx_queue_attribute *attribute, char *buf)
535{
536 struct rps_map *map;
537 cpumask_var_t mask;
538 size_t len = 0;
539 int i;
540
541 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
542 return -ENOMEM;
543
544 rcu_read_lock();
545 map = rcu_dereference(queue->rps_map);
546 if (map)
547 for (i = 0; i < map->len; i++)
548 cpumask_set_cpu(map->cpus[i], mask);
549
550 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
551 if (PAGE_SIZE - len < 3) {
552 rcu_read_unlock();
553 free_cpumask_var(mask);
554 return -EINVAL;
555 }
556 rcu_read_unlock();
557
558 free_cpumask_var(mask);
559 len += sprintf(buf + len, "\n");
560 return len;
561}
562
f5acb907 563static ssize_t store_rps_map(struct netdev_rx_queue *queue,
0a9627f2
TH
564 struct rx_queue_attribute *attribute,
565 const char *buf, size_t len)
566{
567 struct rps_map *old_map, *map;
568 cpumask_var_t mask;
569 int err, cpu, i;
570 static DEFINE_SPINLOCK(rps_map_lock);
571
572 if (!capable(CAP_NET_ADMIN))
573 return -EPERM;
574
575 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
576 return -ENOMEM;
577
578 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
579 if (err) {
580 free_cpumask_var(mask);
581 return err;
582 }
583
584 map = kzalloc(max_t(unsigned,
585 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
586 GFP_KERNEL);
587 if (!map) {
588 free_cpumask_var(mask);
589 return -ENOMEM;
590 }
591
592 i = 0;
593 for_each_cpu_and(cpu, mask, cpu_online_mask)
594 map->cpus[i++] = cpu;
595
596 if (i)
597 map->len = i;
598 else {
599 kfree(map);
600 map = NULL;
601 }
602
603 spin_lock(&rps_map_lock);
6e3f7faf
ED
604 old_map = rcu_dereference_protected(queue->rps_map,
605 lockdep_is_held(&rps_map_lock));
0a9627f2
TH
606 rcu_assign_pointer(queue->rps_map, map);
607 spin_unlock(&rps_map_lock);
608
adc9300e
ED
609 if (map)
610 jump_label_inc(&rps_needed);
611 if (old_map) {
f6f80238 612 kfree_rcu(old_map, rcu);
adc9300e
ED
613 jump_label_dec(&rps_needed);
614 }
0a9627f2
TH
615 free_cpumask_var(mask);
616 return len;
617}
618
fec5e652
TH
619static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
620 struct rx_queue_attribute *attr,
621 char *buf)
622{
623 struct rps_dev_flow_table *flow_table;
624 unsigned int val = 0;
625
626 rcu_read_lock();
627 flow_table = rcu_dereference(queue->rps_flow_table);
628 if (flow_table)
629 val = flow_table->mask + 1;
630 rcu_read_unlock();
631
632 return sprintf(buf, "%u\n", val);
633}
634
635static void rps_dev_flow_table_release_work(struct work_struct *work)
636{
637 struct rps_dev_flow_table *table = container_of(work,
638 struct rps_dev_flow_table, free_work);
639
640 vfree(table);
641}
642
643static void rps_dev_flow_table_release(struct rcu_head *rcu)
644{
645 struct rps_dev_flow_table *table = container_of(rcu,
646 struct rps_dev_flow_table, rcu);
647
648 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
649 schedule_work(&table->free_work);
650}
651
f5acb907 652static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
fec5e652
TH
653 struct rx_queue_attribute *attr,
654 const char *buf, size_t len)
655{
656 unsigned int count;
657 char *endp;
658 struct rps_dev_flow_table *table, *old_table;
659 static DEFINE_SPINLOCK(rps_dev_flow_lock);
660
661 if (!capable(CAP_NET_ADMIN))
662 return -EPERM;
663
664 count = simple_strtoul(buf, &endp, 0);
665 if (endp == buf)
666 return -EINVAL;
667
668 if (count) {
669 int i;
670
671 if (count > 1<<30) {
672 /* Enforce a limit to prevent overflow */
673 return -EINVAL;
674 }
675 count = roundup_pow_of_two(count);
676 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
677 if (!table)
678 return -ENOMEM;
679
680 table->mask = count - 1;
681 for (i = 0; i < count; i++)
682 table->flows[i].cpu = RPS_NO_CPU;
683 } else
684 table = NULL;
685
686 spin_lock(&rps_dev_flow_lock);
6e3f7faf
ED
687 old_table = rcu_dereference_protected(queue->rps_flow_table,
688 lockdep_is_held(&rps_dev_flow_lock));
fec5e652
TH
689 rcu_assign_pointer(queue->rps_flow_table, table);
690 spin_unlock(&rps_dev_flow_lock);
691
692 if (old_table)
693 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
694
695 return len;
696}
697
0a9627f2
TH
698static struct rx_queue_attribute rps_cpus_attribute =
699 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
700
fec5e652
TH
701
702static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
703 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
704 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
705
0a9627f2
TH
706static struct attribute *rx_queue_default_attrs[] = {
707 &rps_cpus_attribute.attr,
fec5e652 708 &rps_dev_flow_table_cnt_attribute.attr,
0a9627f2
TH
709 NULL
710};
711
712static void rx_queue_release(struct kobject *kobj)
713{
714 struct netdev_rx_queue *queue = to_rx_queue(kobj);
6e3f7faf
ED
715 struct rps_map *map;
716 struct rps_dev_flow_table *flow_table;
0a9627f2 717
fec5e652 718
33d480ce 719 map = rcu_dereference_protected(queue->rps_map, 1);
9ea19481
JF
720 if (map) {
721 RCU_INIT_POINTER(queue->rps_map, NULL);
f6f80238 722 kfree_rcu(map, rcu);
9ea19481 723 }
6e3f7faf 724
33d480ce 725 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
9ea19481
JF
726 if (flow_table) {
727 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
6e3f7faf 728 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
9ea19481 729 }
0a9627f2 730
9ea19481 731 memset(kobj, 0, sizeof(*kobj));
fe822240 732 dev_put(queue->dev);
0a9627f2
TH
733}
734
735static struct kobj_type rx_queue_ktype = {
736 .sysfs_ops = &rx_queue_sysfs_ops,
737 .release = rx_queue_release,
738 .default_attrs = rx_queue_default_attrs,
739};
740
741static int rx_queue_add_kobject(struct net_device *net, int index)
742{
743 struct netdev_rx_queue *queue = net->_rx + index;
744 struct kobject *kobj = &queue->kobj;
745 int error = 0;
746
747 kobj->kset = net->queues_kset;
748 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
749 "rx-%u", index);
750 if (error) {
751 kobject_put(kobj);
752 return error;
753 }
754
755 kobject_uevent(kobj, KOBJ_ADD);
fe822240 756 dev_hold(queue->dev);
0a9627f2
TH
757
758 return error;
759}
bf264145 760#endif /* CONFIG_RPS */
0a9627f2 761
62fe0b40
BH
762int
763net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
0a9627f2 764{
bf264145 765#ifdef CONFIG_RPS
0a9627f2
TH
766 int i;
767 int error = 0;
768
62fe0b40 769 for (i = old_num; i < new_num; i++) {
0a9627f2 770 error = rx_queue_add_kobject(net, i);
62fe0b40
BH
771 if (error) {
772 new_num = old_num;
0a9627f2 773 break;
62fe0b40 774 }
0a9627f2
TH
775 }
776
62fe0b40
BH
777 while (--i >= new_num)
778 kobject_put(&net->_rx[i].kobj);
0a9627f2
TH
779
780 return error;
bf264145
TH
781#else
782 return 0;
783#endif
0a9627f2
TH
784}
785
ccf5ff69 786#ifdef CONFIG_SYSFS
1d24eb48
TH
787/*
788 * netdev_queue sysfs structures and functions.
789 */
790struct netdev_queue_attribute {
791 struct attribute attr;
792 ssize_t (*show)(struct netdev_queue *queue,
793 struct netdev_queue_attribute *attr, char *buf);
794 ssize_t (*store)(struct netdev_queue *queue,
795 struct netdev_queue_attribute *attr, const char *buf, size_t len);
796};
797#define to_netdev_queue_attr(_attr) container_of(_attr, \
798 struct netdev_queue_attribute, attr)
799
800#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
801
802static ssize_t netdev_queue_attr_show(struct kobject *kobj,
803 struct attribute *attr, char *buf)
804{
805 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
806 struct netdev_queue *queue = to_netdev_queue(kobj);
807
808 if (!attribute->show)
809 return -EIO;
810
811 return attribute->show(queue, attribute, buf);
812}
813
814static ssize_t netdev_queue_attr_store(struct kobject *kobj,
815 struct attribute *attr,
816 const char *buf, size_t count)
817{
818 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
819 struct netdev_queue *queue = to_netdev_queue(kobj);
820
821 if (!attribute->store)
822 return -EIO;
823
824 return attribute->store(queue, attribute, buf, count);
825}
826
827static const struct sysfs_ops netdev_queue_sysfs_ops = {
828 .show = netdev_queue_attr_show,
829 .store = netdev_queue_attr_store,
830};
831
ccf5ff69 832static ssize_t show_trans_timeout(struct netdev_queue *queue,
833 struct netdev_queue_attribute *attribute,
834 char *buf)
835{
836 unsigned long trans_timeout;
837
838 spin_lock_irq(&queue->_xmit_lock);
839 trans_timeout = queue->trans_timeout;
840 spin_unlock_irq(&queue->_xmit_lock);
841
842 return sprintf(buf, "%lu", trans_timeout);
843}
844
845static struct netdev_queue_attribute queue_trans_timeout =
846 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
847
848#ifdef CONFIG_XPS
1d24eb48 849static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
0a9627f2 850{
1d24eb48
TH
851 struct net_device *dev = queue->dev;
852 int i;
853
854 for (i = 0; i < dev->num_tx_queues; i++)
855 if (queue == &dev->_tx[i])
856 break;
857
858 BUG_ON(i >= dev->num_tx_queues);
859
860 return i;
861}
862
863
864static ssize_t show_xps_map(struct netdev_queue *queue,
865 struct netdev_queue_attribute *attribute, char *buf)
866{
867 struct net_device *dev = queue->dev;
868 struct xps_dev_maps *dev_maps;
869 cpumask_var_t mask;
870 unsigned long index;
871 size_t len = 0;
872 int i;
873
874 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
875 return -ENOMEM;
876
877 index = get_netdev_queue_index(queue);
878
879 rcu_read_lock();
880 dev_maps = rcu_dereference(dev->xps_maps);
881 if (dev_maps) {
882 for_each_possible_cpu(i) {
883 struct xps_map *map =
884 rcu_dereference(dev_maps->cpu_map[i]);
885 if (map) {
886 int j;
887 for (j = 0; j < map->len; j++) {
888 if (map->queues[j] == index) {
889 cpumask_set_cpu(i, mask);
890 break;
891 }
892 }
893 }
894 }
895 }
896 rcu_read_unlock();
897
898 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
899 if (PAGE_SIZE - len < 3) {
900 free_cpumask_var(mask);
901 return -EINVAL;
902 }
903
904 free_cpumask_var(mask);
905 len += sprintf(buf + len, "\n");
906 return len;
907}
908
1d24eb48 909static DEFINE_MUTEX(xps_map_mutex);
a4177869
ED
910#define xmap_dereference(P) \
911 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1d24eb48 912
927fbec1
TH
913static void xps_queue_release(struct netdev_queue *queue)
914{
915 struct net_device *dev = queue->dev;
916 struct xps_dev_maps *dev_maps;
917 struct xps_map *map;
918 unsigned long index;
919 int i, pos, nonempty = 0;
920
921 index = get_netdev_queue_index(queue);
922
923 mutex_lock(&xps_map_mutex);
924 dev_maps = xmap_dereference(dev->xps_maps);
925
926 if (dev_maps) {
927 for_each_possible_cpu(i) {
928 map = xmap_dereference(dev_maps->cpu_map[i]);
929 if (!map)
930 continue;
931
932 for (pos = 0; pos < map->len; pos++)
933 if (map->queues[pos] == index)
934 break;
935
936 if (pos < map->len) {
937 if (map->len > 1)
938 map->queues[pos] =
939 map->queues[--map->len];
940 else {
941 RCU_INIT_POINTER(dev_maps->cpu_map[i],
942 NULL);
943 kfree_rcu(map, rcu);
944 map = NULL;
945 }
946 }
947 if (map)
948 nonempty = 1;
949 }
950
951 if (!nonempty) {
952 RCU_INIT_POINTER(dev->xps_maps, NULL);
953 kfree_rcu(dev_maps, rcu);
954 }
955 }
956 mutex_unlock(&xps_map_mutex);
957}
958
1d24eb48
TH
959static ssize_t store_xps_map(struct netdev_queue *queue,
960 struct netdev_queue_attribute *attribute,
961 const char *buf, size_t len)
962{
963 struct net_device *dev = queue->dev;
964 cpumask_var_t mask;
965 int err, i, cpu, pos, map_len, alloc_len, need_set;
966 unsigned long index;
967 struct xps_map *map, *new_map;
968 struct xps_dev_maps *dev_maps, *new_dev_maps;
969 int nonempty = 0;
19b05f81 970 int numa_node_id = -2;
1d24eb48
TH
971
972 if (!capable(CAP_NET_ADMIN))
973 return -EPERM;
974
975 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
976 return -ENOMEM;
977
978 index = get_netdev_queue_index(queue);
979
980 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
981 if (err) {
982 free_cpumask_var(mask);
983 return err;
984 }
985
986 new_dev_maps = kzalloc(max_t(unsigned,
987 XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
988 if (!new_dev_maps) {
989 free_cpumask_var(mask);
990 return -ENOMEM;
991 }
992
993 mutex_lock(&xps_map_mutex);
994
a4177869 995 dev_maps = xmap_dereference(dev->xps_maps);
1d24eb48
TH
996
997 for_each_possible_cpu(cpu) {
a4177869
ED
998 map = dev_maps ?
999 xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
1000 new_map = map;
1d24eb48
TH
1001 if (map) {
1002 for (pos = 0; pos < map->len; pos++)
1003 if (map->queues[pos] == index)
1004 break;
1005 map_len = map->len;
1006 alloc_len = map->alloc_len;
1007 } else
1008 pos = map_len = alloc_len = 0;
1009
2142c131 1010 need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu);
f2cd2d3e
ED
1011#ifdef CONFIG_NUMA
1012 if (need_set) {
19b05f81 1013 if (numa_node_id == -2)
1014 numa_node_id = cpu_to_node(cpu);
1015 else if (numa_node_id != cpu_to_node(cpu))
1016 numa_node_id = -1;
f2cd2d3e
ED
1017 }
1018#endif
1d24eb48
TH
1019 if (need_set && pos >= map_len) {
1020 /* Need to add queue to this CPU's map */
1021 if (map_len >= alloc_len) {
1022 alloc_len = alloc_len ?
1023 2 * alloc_len : XPS_MIN_MAP_ALLOC;
b02038a1
ED
1024 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
1025 GFP_KERNEL,
1026 cpu_to_node(cpu));
1d24eb48
TH
1027 if (!new_map)
1028 goto error;
1029 new_map->alloc_len = alloc_len;
1030 for (i = 0; i < map_len; i++)
1031 new_map->queues[i] = map->queues[i];
1032 new_map->len = map_len;
1033 }
1034 new_map->queues[new_map->len++] = index;
1035 } else if (!need_set && pos < map_len) {
1036 /* Need to remove queue from this CPU's map */
1037 if (map_len > 1)
1038 new_map->queues[pos] =
1039 new_map->queues[--new_map->len];
1040 else
1041 new_map = NULL;
1042 }
a4177869 1043 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
1d24eb48
TH
1044 }
1045
1046 /* Cleanup old maps */
1047 for_each_possible_cpu(cpu) {
a4177869
ED
1048 map = dev_maps ?
1049 xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
1050 if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
edc86d8a 1051 kfree_rcu(map, rcu);
1d24eb48
TH
1052 if (new_dev_maps->cpu_map[cpu])
1053 nonempty = 1;
1054 }
1055
1056 if (nonempty)
a9b3cd7f 1057 RCU_INIT_POINTER(dev->xps_maps, new_dev_maps);
1d24eb48
TH
1058 else {
1059 kfree(new_dev_maps);
a9b3cd7f 1060 RCU_INIT_POINTER(dev->xps_maps, NULL);
1d24eb48
TH
1061 }
1062
1063 if (dev_maps)
b55071eb 1064 kfree_rcu(dev_maps, rcu);
1d24eb48 1065
19b05f81 1066 netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id :
b236da69 1067 NUMA_NO_NODE);
f2cd2d3e 1068
1d24eb48
TH
1069 mutex_unlock(&xps_map_mutex);
1070
1071 free_cpumask_var(mask);
1072 return len;
1073
1074error:
1075 mutex_unlock(&xps_map_mutex);
1076
1077 if (new_dev_maps)
1078 for_each_possible_cpu(i)
a4177869
ED
1079 kfree(rcu_dereference_protected(
1080 new_dev_maps->cpu_map[i],
1081 1));
1d24eb48
TH
1082 kfree(new_dev_maps);
1083 free_cpumask_var(mask);
1084 return -ENOMEM;
1085}
1086
1087static struct netdev_queue_attribute xps_cpus_attribute =
1088 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
ccf5ff69 1089#endif /* CONFIG_XPS */
1d24eb48
TH
1090
1091static struct attribute *netdev_queue_default_attrs[] = {
ccf5ff69 1092 &queue_trans_timeout.attr,
1093#ifdef CONFIG_XPS
1d24eb48 1094 &xps_cpus_attribute.attr,
ccf5ff69 1095#endif
1d24eb48
TH
1096 NULL
1097};
1098
ccf5ff69 1099#ifdef CONFIG_XPS
1d24eb48
TH
1100static void netdev_queue_release(struct kobject *kobj)
1101{
1102 struct netdev_queue *queue = to_netdev_queue(kobj);
1d24eb48 1103
927fbec1 1104 xps_queue_release(queue);
1d24eb48
TH
1105
1106 memset(kobj, 0, sizeof(*kobj));
1107 dev_put(queue->dev);
1108}
ccf5ff69 1109#endif /* CONFIG_XPS */
1d24eb48
TH
1110
1111static struct kobj_type netdev_queue_ktype = {
1112 .sysfs_ops = &netdev_queue_sysfs_ops,
ccf5ff69 1113#ifdef CONFIG_XPS
1d24eb48 1114 .release = netdev_queue_release,
ccf5ff69 1115#endif
1d24eb48
TH
1116 .default_attrs = netdev_queue_default_attrs,
1117};
1118
1119static int netdev_queue_add_kobject(struct net_device *net, int index)
1120{
1121 struct netdev_queue *queue = net->_tx + index;
1122 struct kobject *kobj = &queue->kobj;
1123 int error = 0;
1124
1125 kobj->kset = net->queues_kset;
1126 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1127 "tx-%u", index);
1128 if (error) {
1129 kobject_put(kobj);
1130 return error;
1131 }
1132
1133 kobject_uevent(kobj, KOBJ_ADD);
1134 dev_hold(queue->dev);
1135
1136 return error;
1137}
ccf5ff69 1138#endif /* CONFIG_SYSFS */
1d24eb48
TH
1139
1140int
1141netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1142{
ccf5ff69 1143#ifdef CONFIG_SYSFS
1d24eb48
TH
1144 int i;
1145 int error = 0;
1146
1147 for (i = old_num; i < new_num; i++) {
1148 error = netdev_queue_add_kobject(net, i);
1149 if (error) {
1150 new_num = old_num;
1151 break;
1152 }
1153 }
1154
1155 while (--i >= new_num)
1156 kobject_put(&net->_tx[i].kobj);
1157
1158 return error;
bf264145
TH
1159#else
1160 return 0;
ccf5ff69 1161#endif /* CONFIG_SYSFS */
1d24eb48
TH
1162}
1163
1164static int register_queue_kobjects(struct net_device *net)
1165{
bf264145 1166 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1d24eb48 1167
ccf5ff69 1168#ifdef CONFIG_SYSFS
62fe0b40
BH
1169 net->queues_kset = kset_create_and_add("queues",
1170 NULL, &net->dev.kobj);
1171 if (!net->queues_kset)
1172 return -ENOMEM;
bf264145
TH
1173#endif
1174
1175#ifdef CONFIG_RPS
1176 real_rx = net->real_num_rx_queues;
1177#endif
1178 real_tx = net->real_num_tx_queues;
1d24eb48 1179
bf264145 1180 error = net_rx_queue_update_kobjects(net, 0, real_rx);
1d24eb48
TH
1181 if (error)
1182 goto error;
bf264145 1183 rxq = real_rx;
1d24eb48 1184
bf264145 1185 error = netdev_queue_update_kobjects(net, 0, real_tx);
1d24eb48
TH
1186 if (error)
1187 goto error;
bf264145 1188 txq = real_tx;
1d24eb48
TH
1189
1190 return 0;
1191
1192error:
1193 netdev_queue_update_kobjects(net, txq, 0);
1194 net_rx_queue_update_kobjects(net, rxq, 0);
1195 return error;
62fe0b40 1196}
0a9627f2 1197
1d24eb48 1198static void remove_queue_kobjects(struct net_device *net)
62fe0b40 1199{
bf264145
TH
1200 int real_rx = 0, real_tx = 0;
1201
1202#ifdef CONFIG_RPS
1203 real_rx = net->real_num_rx_queues;
1204#endif
1205 real_tx = net->real_num_tx_queues;
1206
1207 net_rx_queue_update_kobjects(net, real_rx, 0);
1208 netdev_queue_update_kobjects(net, real_tx, 0);
ccf5ff69 1209#ifdef CONFIG_SYSFS
0a9627f2 1210 kset_unregister(net->queues_kset);
bf264145 1211#endif
0a9627f2 1212}
608b4b95 1213
a685e089 1214static void *net_grab_current_ns(void)
608b4b95 1215{
a685e089
AV
1216 struct net *ns = current->nsproxy->net_ns;
1217#ifdef CONFIG_NET_NS
1218 if (ns)
1219 atomic_inc(&ns->passive);
1220#endif
1221 return ns;
608b4b95
EB
1222}
1223
1224static const void *net_initial_ns(void)
1225{
1226 return &init_net;
1227}
1228
1229static const void *net_netlink_ns(struct sock *sk)
1230{
1231 return sock_net(sk);
1232}
1233
04600794 1234struct kobj_ns_type_operations net_ns_type_operations = {
608b4b95 1235 .type = KOBJ_NS_TYPE_NET,
a685e089 1236 .grab_current_ns = net_grab_current_ns,
608b4b95
EB
1237 .netlink_ns = net_netlink_ns,
1238 .initial_ns = net_initial_ns,
a685e089 1239 .drop_ns = net_drop_ns,
608b4b95 1240};
04600794 1241EXPORT_SYMBOL_GPL(net_ns_type_operations);
608b4b95 1242
1da177e4 1243#ifdef CONFIG_HOTPLUG
7eff2e7a 1244static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1da177e4 1245{
43cb76d9 1246 struct net_device *dev = to_net_dev(d);
7eff2e7a 1247 int retval;
1da177e4 1248
312c004d 1249 /* pass interface to uevent. */
7eff2e7a 1250 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
bf62456e
ER
1251 if (retval)
1252 goto exit;
ca2f37db
JT
1253
1254 /* pass ifindex to uevent.
1255 * ifindex is useful as it won't change (interface name may change)
1256 * and is what RtNetlink uses natively. */
7eff2e7a 1257 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1da177e4 1258
bf62456e 1259exit:
bf62456e 1260 return retval;
1da177e4
LT
1261}
1262#endif
1263
1264/*
4ec93edb 1265 * netdev_release -- destroy and free a dead device.
43cb76d9 1266 * Called when last reference to device kobject is gone.
1da177e4 1267 */
43cb76d9 1268static void netdev_release(struct device *d)
1da177e4 1269{
43cb76d9 1270 struct net_device *dev = to_net_dev(d);
1da177e4
LT
1271
1272 BUG_ON(dev->reg_state != NETREG_RELEASED);
1273
0b815a1a 1274 kfree(dev->ifalias);
1da177e4
LT
1275 kfree((char *)dev - dev->padded);
1276}
1277
608b4b95
EB
1278static const void *net_namespace(struct device *d)
1279{
1280 struct net_device *dev;
1281 dev = container_of(d, struct net_device, dev);
1282 return dev_net(dev);
1283}
1284
1da177e4
LT
1285static struct class net_class = {
1286 .name = "net",
43cb76d9 1287 .dev_release = netdev_release,
8b41d188 1288#ifdef CONFIG_SYSFS
43cb76d9 1289 .dev_attrs = net_class_attributes,
8b41d188 1290#endif /* CONFIG_SYSFS */
1da177e4 1291#ifdef CONFIG_HOTPLUG
43cb76d9 1292 .dev_uevent = netdev_uevent,
1da177e4 1293#endif
608b4b95
EB
1294 .ns_type = &net_ns_type_operations,
1295 .namespace = net_namespace,
1da177e4
LT
1296};
1297
9093bbb2
SH
1298/* Delete sysfs entries but hold kobject reference until after all
1299 * netdev references are gone.
1300 */
8b41d188 1301void netdev_unregister_kobject(struct net_device * net)
1da177e4 1302{
9093bbb2
SH
1303 struct device *dev = &(net->dev);
1304
1305 kobject_get(&dev->kobj);
3891845e 1306
1d24eb48 1307 remove_queue_kobjects(net);
0a9627f2 1308
9093bbb2 1309 device_del(dev);
1da177e4
LT
1310}
1311
1312/* Create sysfs entries for network device. */
8b41d188 1313int netdev_register_kobject(struct net_device *net)
1da177e4 1314{
43cb76d9 1315 struct device *dev = &(net->dev);
a4dbd674 1316 const struct attribute_group **groups = net->sysfs_groups;
0a9627f2 1317 int error = 0;
1da177e4 1318
a1b3f594 1319 device_initialize(dev);
43cb76d9
GKH
1320 dev->class = &net_class;
1321 dev->platform_data = net;
1322 dev->groups = groups;
1da177e4 1323
a2205472 1324 dev_set_name(dev, "%s", net->name);
1da177e4 1325
8b41d188 1326#ifdef CONFIG_SYSFS
0c509a6c
EB
1327 /* Allow for a device specific group */
1328 if (*groups)
1329 groups++;
1da177e4 1330
0c509a6c 1331 *groups++ = &netstat_group;
22bb1be4 1332#ifdef CONFIG_WIRELESS_EXT_SYSFS
3d23e349 1333 if (net->ieee80211_ptr)
fe9925b5 1334 *groups++ = &wireless_group;
3d23e349
JB
1335#ifdef CONFIG_WIRELESS_EXT
1336 else if (net->wireless_handlers)
1337 *groups++ = &wireless_group;
1338#endif
1da177e4 1339#endif
8b41d188 1340#endif /* CONFIG_SYSFS */
1da177e4 1341
0a9627f2
TH
1342 error = device_add(dev);
1343 if (error)
1344 return error;
1345
1d24eb48 1346 error = register_queue_kobjects(net);
0a9627f2
TH
1347 if (error) {
1348 device_del(dev);
1349 return error;
1350 }
1351
1352 return error;
1da177e4
LT
1353}
1354
b8a9787e
JV
1355int netdev_class_create_file(struct class_attribute *class_attr)
1356{
1357 return class_create_file(&net_class, class_attr);
1358}
9e34a5b5 1359EXPORT_SYMBOL(netdev_class_create_file);
b8a9787e
JV
1360
1361void netdev_class_remove_file(struct class_attribute *class_attr)
1362{
1363 class_remove_file(&net_class, class_attr);
1364}
b8a9787e
JV
1365EXPORT_SYMBOL(netdev_class_remove_file);
1366
8b41d188 1367int netdev_kobject_init(void)
1da177e4 1368{
608b4b95 1369 kobj_ns_type_register(&net_ns_type_operations);
1da177e4
LT
1370 return class_register(&net_class);
1371}
This page took 0.70103 seconds and 5 git commands to generate.