]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/net-sysfs.c
ucc_geth driver: add ioctl
[net-next-2.6.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>
8f1546ca 23#include <net/wext.h>
1da177e4 24
342709ef
PE
25#include "net-sysfs.h"
26
8b41d188 27#ifdef CONFIG_SYSFS
1da177e4 28static const char fmt_hex[] = "%#x\n";
d1102b59 29static const char fmt_long_hex[] = "%#lx\n";
1da177e4
LT
30static const char fmt_dec[] = "%d\n";
31static const char fmt_ulong[] = "%lu\n";
32
4ec93edb 33static inline int dev_isalive(const struct net_device *dev)
1da177e4 34{
fe9925b5 35 return dev->reg_state <= NETREG_REGISTERED;
1da177e4
LT
36}
37
38/* use same locking rules as GIF* ioctl's */
43cb76d9
GKH
39static ssize_t netdev_show(const struct device *dev,
40 struct device_attribute *attr, char *buf,
1da177e4
LT
41 ssize_t (*format)(const struct net_device *, char *))
42{
43cb76d9 43 struct net_device *net = to_net_dev(dev);
1da177e4
LT
44 ssize_t ret = -EINVAL;
45
46 read_lock(&dev_base_lock);
47 if (dev_isalive(net))
48 ret = (*format)(net, buf);
49 read_unlock(&dev_base_lock);
50
51 return ret;
52}
53
54/* generate a show function for simple field */
55#define NETDEVICE_SHOW(field, format_string) \
56static ssize_t format_##field(const struct net_device *net, char *buf) \
57{ \
58 return sprintf(buf, format_string, net->field); \
59} \
43cb76d9
GKH
60static ssize_t show_##field(struct device *dev, \
61 struct device_attribute *attr, char *buf) \
1da177e4 62{ \
43cb76d9 63 return netdev_show(dev, attr, buf, format_##field); \
1da177e4
LT
64}
65
66
67/* use same locking and permission rules as SIF* ioctl's */
43cb76d9 68static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
1da177e4
LT
69 const char *buf, size_t len,
70 int (*set)(struct net_device *, unsigned long))
71{
72 struct net_device *net = to_net_dev(dev);
73 char *endp;
74 unsigned long new;
75 int ret = -EINVAL;
76
77 if (!capable(CAP_NET_ADMIN))
78 return -EPERM;
79
80 new = simple_strtoul(buf, &endp, 0);
81 if (endp == buf)
82 goto err;
83
5a5990d3 84 if (!rtnl_trylock())
336ca57c 85 return restart_syscall();
5a5990d3 86
1da177e4
LT
87 if (dev_isalive(net)) {
88 if ((ret = (*set)(net, new)) == 0)
89 ret = len;
90 }
91 rtnl_unlock();
92 err:
93 return ret;
94}
95
9d29672c 96NETDEVICE_SHOW(dev_id, fmt_hex);
fd586bac
KS
97NETDEVICE_SHOW(addr_len, fmt_dec);
98NETDEVICE_SHOW(iflink, fmt_dec);
99NETDEVICE_SHOW(ifindex, fmt_dec);
100NETDEVICE_SHOW(features, fmt_long_hex);
101NETDEVICE_SHOW(type, fmt_dec);
b00055aa 102NETDEVICE_SHOW(link_mode, fmt_dec);
1da177e4
LT
103
104/* use same locking rules as GIFHWADDR ioctl's */
43cb76d9
GKH
105static ssize_t show_address(struct device *dev, struct device_attribute *attr,
106 char *buf)
1da177e4
LT
107{
108 struct net_device *net = to_net_dev(dev);
109 ssize_t ret = -EINVAL;
110
111 read_lock(&dev_base_lock);
112 if (dev_isalive(net))
7ffc49a6 113 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
1da177e4
LT
114 read_unlock(&dev_base_lock);
115 return ret;
116}
117
43cb76d9
GKH
118static ssize_t show_broadcast(struct device *dev,
119 struct device_attribute *attr, char *buf)
1da177e4
LT
120{
121 struct net_device *net = to_net_dev(dev);
122 if (dev_isalive(net))
7ffc49a6 123 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
1da177e4
LT
124 return -EINVAL;
125}
126
43cb76d9
GKH
127static ssize_t show_carrier(struct device *dev,
128 struct device_attribute *attr, char *buf)
1da177e4
LT
129{
130 struct net_device *netdev = to_net_dev(dev);
131 if (netif_running(netdev)) {
132 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
133 }
134 return -EINVAL;
135}
136
d519e17e
AG
137static ssize_t show_speed(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 struct net_device *netdev = to_net_dev(dev);
141 int ret = -EINVAL;
142
143 if (!rtnl_trylock())
144 return restart_syscall();
145
ac5e3af9
ED
146 if (netif_running(netdev) &&
147 netdev->ethtool_ops &&
148 netdev->ethtool_ops->get_settings) {
d519e17e
AG
149 struct ethtool_cmd cmd = { ETHTOOL_GSET };
150
151 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
152 ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
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
ac5e3af9
ED
167 if (netif_running(netdev) &&
168 netdev->ethtool_ops &&
169 netdev->ethtool_ops->get_settings) {
d519e17e
AG
170 struct ethtool_cmd cmd = { ETHTOOL_GSET };
171
172 if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
173 ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
174 }
175 rtnl_unlock();
176 return ret;
177}
178
43cb76d9
GKH
179static ssize_t show_dormant(struct device *dev,
180 struct device_attribute *attr, char *buf)
b00055aa
SR
181{
182 struct net_device *netdev = to_net_dev(dev);
183
184 if (netif_running(netdev))
185 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
186
187 return -EINVAL;
188}
189
36cbd3dc 190static const char *const operstates[] = {
b00055aa
SR
191 "unknown",
192 "notpresent", /* currently unused */
193 "down",
194 "lowerlayerdown",
195 "testing", /* currently unused */
196 "dormant",
197 "up"
198};
199
43cb76d9
GKH
200static ssize_t show_operstate(struct device *dev,
201 struct device_attribute *attr, char *buf)
b00055aa
SR
202{
203 const struct net_device *netdev = to_net_dev(dev);
204 unsigned char operstate;
205
206 read_lock(&dev_base_lock);
207 operstate = netdev->operstate;
208 if (!netif_running(netdev))
209 operstate = IF_OPER_DOWN;
210 read_unlock(&dev_base_lock);
211
e3a5cd9e 212 if (operstate >= ARRAY_SIZE(operstates))
b00055aa
SR
213 return -EINVAL; /* should not happen */
214
215 return sprintf(buf, "%s\n", operstates[operstate]);
216}
217
1da177e4
LT
218/* read-write attributes */
219NETDEVICE_SHOW(mtu, fmt_dec);
220
221static int change_mtu(struct net_device *net, unsigned long new_mtu)
222{
223 return dev_set_mtu(net, (int) new_mtu);
224}
225
43cb76d9
GKH
226static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
227 const char *buf, size_t len)
1da177e4 228{
43cb76d9 229 return netdev_store(dev, attr, buf, len, change_mtu);
1da177e4
LT
230}
231
1da177e4
LT
232NETDEVICE_SHOW(flags, fmt_hex);
233
234static int change_flags(struct net_device *net, unsigned long new_flags)
235{
236 return dev_change_flags(net, (unsigned) new_flags);
237}
238
43cb76d9
GKH
239static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t len)
1da177e4 241{
43cb76d9 242 return netdev_store(dev, attr, buf, len, change_flags);
1da177e4
LT
243}
244
1da177e4
LT
245NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
246
247static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
248{
249 net->tx_queue_len = new_len;
250 return 0;
251}
252
43cb76d9
GKH
253static ssize_t store_tx_queue_len(struct device *dev,
254 struct device_attribute *attr,
255 const char *buf, size_t len)
1da177e4 256{
43cb76d9 257 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
1da177e4
LT
258}
259
0b815a1a
SH
260static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t len)
262{
263 struct net_device *netdev = to_net_dev(dev);
264 size_t count = len;
265 ssize_t ret;
266
267 if (!capable(CAP_NET_ADMIN))
268 return -EPERM;
269
270 /* ignore trailing newline */
271 if (len > 0 && buf[len - 1] == '\n')
272 --count;
273
336ca57c
EB
274 if (!rtnl_trylock())
275 return restart_syscall();
0b815a1a
SH
276 ret = dev_set_alias(netdev, buf, count);
277 rtnl_unlock();
278
279 return ret < 0 ? ret : len;
280}
281
282static ssize_t show_ifalias(struct device *dev,
283 struct device_attribute *attr, char *buf)
284{
285 const struct net_device *netdev = to_net_dev(dev);
286 ssize_t ret = 0;
287
336ca57c
EB
288 if (!rtnl_trylock())
289 return restart_syscall();
0b815a1a
SH
290 if (netdev->ifalias)
291 ret = sprintf(buf, "%s\n", netdev->ifalias);
292 rtnl_unlock();
293 return ret;
294}
295
43cb76d9 296static struct device_attribute net_class_attributes[] = {
fd586bac 297 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
9d29672c 298 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
0b815a1a 299 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
fd586bac
KS
300 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
301 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
302 __ATTR(features, S_IRUGO, show_features, NULL),
303 __ATTR(type, S_IRUGO, show_type, NULL),
b00055aa 304 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
fd586bac
KS
305 __ATTR(address, S_IRUGO, show_address, NULL),
306 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
307 __ATTR(carrier, S_IRUGO, show_carrier, NULL),
d519e17e
AG
308 __ATTR(speed, S_IRUGO, show_speed, NULL),
309 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
b00055aa
SR
310 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
311 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
fd586bac
KS
312 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
313 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
314 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
315 store_tx_queue_len),
fd586bac 316 {}
1da177e4
LT
317};
318
319/* Show a given an attribute in the statistics group */
43cb76d9
GKH
320static ssize_t netstat_show(const struct device *d,
321 struct device_attribute *attr, char *buf,
1da177e4
LT
322 unsigned long offset)
323{
43cb76d9 324 struct net_device *dev = to_net_dev(d);
1da177e4
LT
325 ssize_t ret = -EINVAL;
326
df1b86c5
PE
327 WARN_ON(offset > sizeof(struct net_device_stats) ||
328 offset % sizeof(unsigned long) != 0);
1da177e4
LT
329
330 read_lock(&dev_base_lock);
96e74088 331 if (dev_isalive(dev)) {
eeda3fd6 332 const struct net_device_stats *stats = dev_get_stats(dev);
1da177e4
LT
333 ret = sprintf(buf, fmt_ulong,
334 *(unsigned long *)(((u8 *) stats) + offset));
96e74088 335 }
1da177e4
LT
336 read_unlock(&dev_base_lock);
337 return ret;
338}
339
340/* generate a read-only statistics attribute */
341#define NETSTAT_ENTRY(name) \
43cb76d9
GKH
342static ssize_t show_##name(struct device *d, \
343 struct device_attribute *attr, char *buf) \
1da177e4 344{ \
43cb76d9 345 return netstat_show(d, attr, buf, \
1da177e4
LT
346 offsetof(struct net_device_stats, name)); \
347} \
43cb76d9 348static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
1da177e4
LT
349
350NETSTAT_ENTRY(rx_packets);
351NETSTAT_ENTRY(tx_packets);
352NETSTAT_ENTRY(rx_bytes);
353NETSTAT_ENTRY(tx_bytes);
354NETSTAT_ENTRY(rx_errors);
355NETSTAT_ENTRY(tx_errors);
356NETSTAT_ENTRY(rx_dropped);
357NETSTAT_ENTRY(tx_dropped);
358NETSTAT_ENTRY(multicast);
359NETSTAT_ENTRY(collisions);
360NETSTAT_ENTRY(rx_length_errors);
361NETSTAT_ENTRY(rx_over_errors);
362NETSTAT_ENTRY(rx_crc_errors);
363NETSTAT_ENTRY(rx_frame_errors);
364NETSTAT_ENTRY(rx_fifo_errors);
365NETSTAT_ENTRY(rx_missed_errors);
366NETSTAT_ENTRY(tx_aborted_errors);
367NETSTAT_ENTRY(tx_carrier_errors);
368NETSTAT_ENTRY(tx_fifo_errors);
369NETSTAT_ENTRY(tx_heartbeat_errors);
370NETSTAT_ENTRY(tx_window_errors);
371NETSTAT_ENTRY(rx_compressed);
372NETSTAT_ENTRY(tx_compressed);
373
374static struct attribute *netstat_attrs[] = {
43cb76d9
GKH
375 &dev_attr_rx_packets.attr,
376 &dev_attr_tx_packets.attr,
377 &dev_attr_rx_bytes.attr,
378 &dev_attr_tx_bytes.attr,
379 &dev_attr_rx_errors.attr,
380 &dev_attr_tx_errors.attr,
381 &dev_attr_rx_dropped.attr,
382 &dev_attr_tx_dropped.attr,
383 &dev_attr_multicast.attr,
384 &dev_attr_collisions.attr,
385 &dev_attr_rx_length_errors.attr,
386 &dev_attr_rx_over_errors.attr,
387 &dev_attr_rx_crc_errors.attr,
388 &dev_attr_rx_frame_errors.attr,
389 &dev_attr_rx_fifo_errors.attr,
390 &dev_attr_rx_missed_errors.attr,
391 &dev_attr_tx_aborted_errors.attr,
392 &dev_attr_tx_carrier_errors.attr,
393 &dev_attr_tx_fifo_errors.attr,
394 &dev_attr_tx_heartbeat_errors.attr,
395 &dev_attr_tx_window_errors.attr,
396 &dev_attr_rx_compressed.attr,
397 &dev_attr_tx_compressed.attr,
1da177e4
LT
398 NULL
399};
400
401
402static struct attribute_group netstat_group = {
403 .name = "statistics",
404 .attrs = netstat_attrs,
405};
406
22bb1be4 407#ifdef CONFIG_WIRELESS_EXT_SYSFS
1da177e4 408/* helper function that does all the locking etc for wireless stats */
43cb76d9 409static ssize_t wireless_show(struct device *d, char *buf,
1da177e4
LT
410 ssize_t (*format)(const struct iw_statistics *,
411 char *))
412{
43cb76d9 413 struct net_device *dev = to_net_dev(d);
8f1546ca 414 const struct iw_statistics *iw;
1da177e4 415 ssize_t ret = -EINVAL;
4ec93edb 416
b8afe641
EB
417 if (!rtnl_trylock())
418 return restart_syscall();
6dd214b5 419 if (dev_isalive(dev)) {
8f1546ca
JB
420 iw = get_wireless_stats(dev);
421 if (iw)
6dd214b5
AB
422 ret = (*format)(iw, buf);
423 }
a160ee69 424 rtnl_unlock();
1da177e4
LT
425
426 return ret;
427}
428
429/* show function template for wireless fields */
430#define WIRELESS_SHOW(name, field, format_string) \
431static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
432{ \
433 return sprintf(buf, format_string, iw->field); \
434} \
43cb76d9
GKH
435static ssize_t show_iw_##name(struct device *d, \
436 struct device_attribute *attr, char *buf) \
1da177e4 437{ \
43cb76d9 438 return wireless_show(d, buf, format_iw_##name); \
1da177e4 439} \
43cb76d9 440static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
1da177e4
LT
441
442WIRELESS_SHOW(status, status, fmt_hex);
443WIRELESS_SHOW(link, qual.qual, fmt_dec);
444WIRELESS_SHOW(level, qual.level, fmt_dec);
445WIRELESS_SHOW(noise, qual.noise, fmt_dec);
446WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
447WIRELESS_SHOW(crypt, discard.code, fmt_dec);
448WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
449WIRELESS_SHOW(misc, discard.misc, fmt_dec);
450WIRELESS_SHOW(retries, discard.retries, fmt_dec);
451WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
452
453static struct attribute *wireless_attrs[] = {
43cb76d9
GKH
454 &dev_attr_status.attr,
455 &dev_attr_link.attr,
456 &dev_attr_level.attr,
457 &dev_attr_noise.attr,
458 &dev_attr_nwid.attr,
459 &dev_attr_crypt.attr,
460 &dev_attr_fragment.attr,
461 &dev_attr_retries.attr,
462 &dev_attr_misc.attr,
463 &dev_attr_beacon.attr,
1da177e4
LT
464 NULL
465};
466
467static struct attribute_group wireless_group = {
468 .name = "wireless",
469 .attrs = wireless_attrs,
470};
471#endif
d6523ddf 472#endif /* CONFIG_SYSFS */
1da177e4 473
30bde1f5 474#ifdef CONFIG_RPS
0a9627f2
TH
475/*
476 * RX queue sysfs structures and functions.
477 */
478struct rx_queue_attribute {
479 struct attribute attr;
480 ssize_t (*show)(struct netdev_rx_queue *queue,
481 struct rx_queue_attribute *attr, char *buf);
482 ssize_t (*store)(struct netdev_rx_queue *queue,
483 struct rx_queue_attribute *attr, const char *buf, size_t len);
484};
485#define to_rx_queue_attr(_attr) container_of(_attr, \
486 struct rx_queue_attribute, attr)
487
488#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
489
490static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
491 char *buf)
492{
493 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
494 struct netdev_rx_queue *queue = to_rx_queue(kobj);
495
496 if (!attribute->show)
497 return -EIO;
498
499 return attribute->show(queue, attribute, buf);
500}
501
502static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
503 const char *buf, size_t count)
504{
505 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
506 struct netdev_rx_queue *queue = to_rx_queue(kobj);
507
508 if (!attribute->store)
509 return -EIO;
510
511 return attribute->store(queue, attribute, buf, count);
512}
513
514static struct sysfs_ops rx_queue_sysfs_ops = {
515 .show = rx_queue_attr_show,
516 .store = rx_queue_attr_store,
517};
518
519static ssize_t show_rps_map(struct netdev_rx_queue *queue,
520 struct rx_queue_attribute *attribute, char *buf)
521{
522 struct rps_map *map;
523 cpumask_var_t mask;
524 size_t len = 0;
525 int i;
526
527 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
528 return -ENOMEM;
529
530 rcu_read_lock();
531 map = rcu_dereference(queue->rps_map);
532 if (map)
533 for (i = 0; i < map->len; i++)
534 cpumask_set_cpu(map->cpus[i], mask);
535
536 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
537 if (PAGE_SIZE - len < 3) {
538 rcu_read_unlock();
539 free_cpumask_var(mask);
540 return -EINVAL;
541 }
542 rcu_read_unlock();
543
544 free_cpumask_var(mask);
545 len += sprintf(buf + len, "\n");
546 return len;
547}
548
549static void rps_map_release(struct rcu_head *rcu)
550{
551 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
552
553 kfree(map);
554}
555
f5acb907 556static ssize_t store_rps_map(struct netdev_rx_queue *queue,
0a9627f2
TH
557 struct rx_queue_attribute *attribute,
558 const char *buf, size_t len)
559{
560 struct rps_map *old_map, *map;
561 cpumask_var_t mask;
562 int err, cpu, i;
563 static DEFINE_SPINLOCK(rps_map_lock);
564
565 if (!capable(CAP_NET_ADMIN))
566 return -EPERM;
567
568 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
569 return -ENOMEM;
570
571 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
572 if (err) {
573 free_cpumask_var(mask);
574 return err;
575 }
576
577 map = kzalloc(max_t(unsigned,
578 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
579 GFP_KERNEL);
580 if (!map) {
581 free_cpumask_var(mask);
582 return -ENOMEM;
583 }
584
585 i = 0;
586 for_each_cpu_and(cpu, mask, cpu_online_mask)
587 map->cpus[i++] = cpu;
588
589 if (i)
590 map->len = i;
591 else {
592 kfree(map);
593 map = NULL;
594 }
595
596 spin_lock(&rps_map_lock);
597 old_map = queue->rps_map;
598 rcu_assign_pointer(queue->rps_map, map);
599 spin_unlock(&rps_map_lock);
600
601 if (old_map)
602 call_rcu(&old_map->rcu, rps_map_release);
603
604 free_cpumask_var(mask);
605 return len;
606}
607
fec5e652
TH
608static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
609 struct rx_queue_attribute *attr,
610 char *buf)
611{
612 struct rps_dev_flow_table *flow_table;
613 unsigned int val = 0;
614
615 rcu_read_lock();
616 flow_table = rcu_dereference(queue->rps_flow_table);
617 if (flow_table)
618 val = flow_table->mask + 1;
619 rcu_read_unlock();
620
621 return sprintf(buf, "%u\n", val);
622}
623
624static void rps_dev_flow_table_release_work(struct work_struct *work)
625{
626 struct rps_dev_flow_table *table = container_of(work,
627 struct rps_dev_flow_table, free_work);
628
629 vfree(table);
630}
631
632static void rps_dev_flow_table_release(struct rcu_head *rcu)
633{
634 struct rps_dev_flow_table *table = container_of(rcu,
635 struct rps_dev_flow_table, rcu);
636
637 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
638 schedule_work(&table->free_work);
639}
640
f5acb907 641static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
fec5e652
TH
642 struct rx_queue_attribute *attr,
643 const char *buf, size_t len)
644{
645 unsigned int count;
646 char *endp;
647 struct rps_dev_flow_table *table, *old_table;
648 static DEFINE_SPINLOCK(rps_dev_flow_lock);
649
650 if (!capable(CAP_NET_ADMIN))
651 return -EPERM;
652
653 count = simple_strtoul(buf, &endp, 0);
654 if (endp == buf)
655 return -EINVAL;
656
657 if (count) {
658 int i;
659
660 if (count > 1<<30) {
661 /* Enforce a limit to prevent overflow */
662 return -EINVAL;
663 }
664 count = roundup_pow_of_two(count);
665 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
666 if (!table)
667 return -ENOMEM;
668
669 table->mask = count - 1;
670 for (i = 0; i < count; i++)
671 table->flows[i].cpu = RPS_NO_CPU;
672 } else
673 table = NULL;
674
675 spin_lock(&rps_dev_flow_lock);
676 old_table = queue->rps_flow_table;
677 rcu_assign_pointer(queue->rps_flow_table, table);
678 spin_unlock(&rps_dev_flow_lock);
679
680 if (old_table)
681 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
682
683 return len;
684}
685
0a9627f2
TH
686static struct rx_queue_attribute rps_cpus_attribute =
687 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
688
fec5e652
TH
689
690static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
691 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
692 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
693
0a9627f2
TH
694static struct attribute *rx_queue_default_attrs[] = {
695 &rps_cpus_attribute.attr,
fec5e652 696 &rps_dev_flow_table_cnt_attribute.attr,
0a9627f2
TH
697 NULL
698};
699
700static void rx_queue_release(struct kobject *kobj)
701{
702 struct netdev_rx_queue *queue = to_rx_queue(kobj);
0a9627f2
TH
703 struct netdev_rx_queue *first = queue->first;
704
fec5e652
TH
705 if (queue->rps_map)
706 call_rcu(&queue->rps_map->rcu, rps_map_release);
707
708 if (queue->rps_flow_table)
709 call_rcu(&queue->rps_flow_table->rcu,
710 rps_dev_flow_table_release);
0a9627f2
TH
711
712 if (atomic_dec_and_test(&first->count))
713 kfree(first);
714}
715
716static struct kobj_type rx_queue_ktype = {
717 .sysfs_ops = &rx_queue_sysfs_ops,
718 .release = rx_queue_release,
719 .default_attrs = rx_queue_default_attrs,
720};
721
722static int rx_queue_add_kobject(struct net_device *net, int index)
723{
724 struct netdev_rx_queue *queue = net->_rx + index;
725 struct kobject *kobj = &queue->kobj;
726 int error = 0;
727
728 kobj->kset = net->queues_kset;
729 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
730 "rx-%u", index);
731 if (error) {
732 kobject_put(kobj);
733 return error;
734 }
735
736 kobject_uevent(kobj, KOBJ_ADD);
737
738 return error;
739}
740
741static int rx_queue_register_kobjects(struct net_device *net)
742{
743 int i;
744 int error = 0;
745
746 net->queues_kset = kset_create_and_add("queues",
747 NULL, &net->dev.kobj);
748 if (!net->queues_kset)
749 return -ENOMEM;
750 for (i = 0; i < net->num_rx_queues; i++) {
751 error = rx_queue_add_kobject(net, i);
752 if (error)
753 break;
754 }
755
756 if (error)
757 while (--i >= 0)
758 kobject_put(&net->_rx[i].kobj);
759
760 return error;
761}
762
763static void rx_queue_remove_kobjects(struct net_device *net)
764{
765 int i;
766
767 for (i = 0; i < net->num_rx_queues; i++)
768 kobject_put(&net->_rx[i].kobj);
769 kset_unregister(net->queues_kset);
770}
30bde1f5 771#endif /* CONFIG_RPS */
608b4b95
EB
772
773static const void *net_current_ns(void)
774{
775 return current->nsproxy->net_ns;
776}
777
778static const void *net_initial_ns(void)
779{
780 return &init_net;
781}
782
783static const void *net_netlink_ns(struct sock *sk)
784{
785 return sock_net(sk);
786}
787
788static struct kobj_ns_type_operations net_ns_type_operations = {
789 .type = KOBJ_NS_TYPE_NET,
790 .current_ns = net_current_ns,
791 .netlink_ns = net_netlink_ns,
792 .initial_ns = net_initial_ns,
793};
794
795static void net_kobj_ns_exit(struct net *net)
796{
797 kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
798}
799
d6523ddf 800static struct pernet_operations kobj_net_ops = {
608b4b95
EB
801 .exit = net_kobj_ns_exit,
802};
803
8b41d188 804
1da177e4 805#ifdef CONFIG_HOTPLUG
7eff2e7a 806static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1da177e4 807{
43cb76d9 808 struct net_device *dev = to_net_dev(d);
7eff2e7a 809 int retval;
1da177e4 810
312c004d 811 /* pass interface to uevent. */
7eff2e7a 812 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
bf62456e
ER
813 if (retval)
814 goto exit;
ca2f37db
JT
815
816 /* pass ifindex to uevent.
817 * ifindex is useful as it won't change (interface name may change)
818 * and is what RtNetlink uses natively. */
7eff2e7a 819 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1da177e4 820
bf62456e 821exit:
bf62456e 822 return retval;
1da177e4
LT
823}
824#endif
825
826/*
4ec93edb 827 * netdev_release -- destroy and free a dead device.
43cb76d9 828 * Called when last reference to device kobject is gone.
1da177e4 829 */
43cb76d9 830static void netdev_release(struct device *d)
1da177e4 831{
43cb76d9 832 struct net_device *dev = to_net_dev(d);
1da177e4
LT
833
834 BUG_ON(dev->reg_state != NETREG_RELEASED);
835
0b815a1a 836 kfree(dev->ifalias);
1da177e4
LT
837 kfree((char *)dev - dev->padded);
838}
839
608b4b95
EB
840static const void *net_namespace(struct device *d)
841{
842 struct net_device *dev;
843 dev = container_of(d, struct net_device, dev);
844 return dev_net(dev);
845}
846
1da177e4
LT
847static struct class net_class = {
848 .name = "net",
43cb76d9 849 .dev_release = netdev_release,
8b41d188 850#ifdef CONFIG_SYSFS
43cb76d9 851 .dev_attrs = net_class_attributes,
8b41d188 852#endif /* CONFIG_SYSFS */
1da177e4 853#ifdef CONFIG_HOTPLUG
43cb76d9 854 .dev_uevent = netdev_uevent,
1da177e4 855#endif
608b4b95
EB
856 .ns_type = &net_ns_type_operations,
857 .namespace = net_namespace,
1da177e4
LT
858};
859
9093bbb2
SH
860/* Delete sysfs entries but hold kobject reference until after all
861 * netdev references are gone.
862 */
8b41d188 863void netdev_unregister_kobject(struct net_device * net)
1da177e4 864{
9093bbb2
SH
865 struct device *dev = &(net->dev);
866
867 kobject_get(&dev->kobj);
3891845e 868
30bde1f5 869#ifdef CONFIG_RPS
0a9627f2 870 rx_queue_remove_kobjects(net);
e880eb6c 871#endif
0a9627f2 872
9093bbb2 873 device_del(dev);
1da177e4
LT
874}
875
876/* Create sysfs entries for network device. */
8b41d188 877int netdev_register_kobject(struct net_device *net)
1da177e4 878{
43cb76d9 879 struct device *dev = &(net->dev);
a4dbd674 880 const struct attribute_group **groups = net->sysfs_groups;
0a9627f2 881 int error = 0;
1da177e4 882
a1b3f594 883 device_initialize(dev);
43cb76d9
GKH
884 dev->class = &net_class;
885 dev->platform_data = net;
886 dev->groups = groups;
1da177e4 887
a2205472 888 dev_set_name(dev, "%s", net->name);
1da177e4 889
8b41d188 890#ifdef CONFIG_SYSFS
0c509a6c
EB
891 /* Allow for a device specific group */
892 if (*groups)
893 groups++;
1da177e4 894
0c509a6c 895 *groups++ = &netstat_group;
22bb1be4 896#ifdef CONFIG_WIRELESS_EXT_SYSFS
3d23e349 897 if (net->ieee80211_ptr)
fe9925b5 898 *groups++ = &wireless_group;
3d23e349
JB
899#ifdef CONFIG_WIRELESS_EXT
900 else if (net->wireless_handlers)
901 *groups++ = &wireless_group;
902#endif
1da177e4 903#endif
8b41d188 904#endif /* CONFIG_SYSFS */
1da177e4 905
0a9627f2
TH
906 error = device_add(dev);
907 if (error)
908 return error;
909
30bde1f5 910#ifdef CONFIG_RPS
0a9627f2
TH
911 error = rx_queue_register_kobjects(net);
912 if (error) {
913 device_del(dev);
914 return error;
915 }
e880eb6c 916#endif
0a9627f2
TH
917
918 return error;
1da177e4
LT
919}
920
b8a9787e
JV
921int netdev_class_create_file(struct class_attribute *class_attr)
922{
923 return class_create_file(&net_class, class_attr);
924}
925
926void netdev_class_remove_file(struct class_attribute *class_attr)
927{
928 class_remove_file(&net_class, class_attr);
929}
930
931EXPORT_SYMBOL(netdev_class_create_file);
932EXPORT_SYMBOL(netdev_class_remove_file);
933
8b41d188 934int netdev_kobject_init(void)
1da177e4 935{
608b4b95 936 kobj_ns_type_register(&net_ns_type_operations);
d6523ddf 937 register_pernet_subsys(&kobj_net_ops);
1da177e4
LT
938 return class_register(&net_class);
939}