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