blob: 7427ab5e27d80dcf1f2af0edb42ccf75e7ffb86d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net-sysfs.c - network device class and attributes
3 *
4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070017#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/sock.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070019#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/rtnetlink.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070021#include <linux/vmalloc.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040022#include <linux/export.h>
Tom Herbert114cf582011-11-28 16:33:09 +000023#include <linux/jiffies.h>
Ming Lei9802c8e2013-02-22 16:34:16 -080024#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Pavel Emelyanov342709e2007-10-23 21:14:45 -070026#include "net-sysfs.h"
27
Eric W. Biederman8b41d182007-09-26 22:02:53 -070028#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static const char fmt_hex[] = "%#x\n";
David S. Millerd1102b52005-05-29 20:28:25 -070030static const char fmt_long_hex[] = "%#lx\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static const char fmt_dec[] = "%d\n";
David Decotigny8ae6dac2011-04-27 18:32:38 +000032static const char fmt_udec[] = "%u\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static const char fmt_ulong[] = "%lu\n";
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +000034static const char fmt_u64[] = "%llu\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090036static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070038 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070042static ssize_t netdev_show(const struct device *dev,
43 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 ssize_t (*format)(const struct net_device *, char *))
45{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070046 struct net_device *net = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 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} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070063static ssize_t show_##field(struct device *dev, \
64 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070066 return netdev_show(dev, attr, buf, format_##field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69
70/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070071static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 const char *buf, size_t len,
73 int (*set)(struct net_device *, unsigned long))
74{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000075 struct net_device *netdev = to_net_dev(dev);
76 struct net *net = dev_net(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long new;
78 int ret = -EINVAL;
79
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000080 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EPERM;
82
Shuah Khane1e420c2012-04-12 09:28:13 +000083 ret = kstrtoul(buf, 0, &new);
84 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto err;
86
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000087 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000088 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000089
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000090 if (dev_isalive(netdev)) {
91 if ((ret = (*set)(netdev, new)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 ret = len;
93 }
94 rtnl_unlock();
95 err:
96 return ret;
97}
98
David Woodhouse9d296722008-04-20 16:07:43 -070099NETDEVICE_SHOW(dev_id, fmt_hex);
Stefan Assmannc1f79422010-07-22 02:50:21 +0000100NETDEVICE_SHOW(addr_assign_type, fmt_dec);
Kay Sieversfd586ba2005-12-19 01:42:56 +0100101NETDEVICE_SHOW(addr_len, fmt_dec);
102NETDEVICE_SHOW(iflink, fmt_dec);
103NETDEVICE_SHOW(ifindex, fmt_dec);
Kay Sieversfd586ba2005-12-19 01:42:56 +0100104NETDEVICE_SHOW(type, fmt_dec);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800105NETDEVICE_SHOW(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700108static ssize_t show_address(struct device *dev, struct device_attribute *attr,
109 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
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))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800116 ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 read_unlock(&dev_base_lock);
118 return ret;
119}
120
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700121static ssize_t show_broadcast(struct device *dev,
122 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct net_device *net = to_net_dev(dev);
125 if (dev_isalive(net))
Michael Chan7ffc49a2007-12-24 21:28:09 -0800126 return sysfs_format_mac(buf, net->broadcast, net->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EINVAL;
128}
129
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000130static int change_carrier(struct net_device *net, unsigned long new_carrier)
131{
132 if (!netif_running(net))
133 return -EINVAL;
134 return dev_change_carrier(net, (bool) new_carrier);
135}
136
137static ssize_t store_carrier(struct device *dev, struct device_attribute *attr,
138 const char *buf, size_t len)
139{
140 return netdev_store(dev, attr, buf, len, change_carrier);
141}
142
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700143static ssize_t show_carrier(struct device *dev,
144 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct net_device *netdev = to_net_dev(dev);
147 if (netif_running(netdev)) {
148 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
149 }
150 return -EINVAL;
151}
152
Andy Gospodarekd519e172009-10-02 09:26:12 +0000153static ssize_t show_speed(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
156 struct net_device *netdev = to_net_dev(dev);
157 int ret = -EINVAL;
158
159 if (!rtnl_trylock())
160 return restart_syscall();
161
David Decotigny8ae6dac2011-04-27 18:32:38 +0000162 if (netif_running(netdev)) {
163 struct ethtool_cmd cmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000164 if (!__ethtool_get_settings(netdev, &cmd))
David Decotigny8ae6dac2011-04-27 18:32:38 +0000165 ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
Andy Gospodarekd519e172009-10-02 09:26:12 +0000166 }
167 rtnl_unlock();
168 return ret;
169}
170
171static ssize_t show_duplex(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
174 struct net_device *netdev = to_net_dev(dev);
175 int ret = -EINVAL;
176
177 if (!rtnl_trylock())
178 return restart_syscall();
179
David Decotigny8ae6dac2011-04-27 18:32:38 +0000180 if (netif_running(netdev)) {
181 struct ethtool_cmd cmd;
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000182 if (!__ethtool_get_settings(netdev, &cmd)) {
183 const char *duplex;
184 switch (cmd.duplex) {
185 case DUPLEX_HALF:
186 duplex = "half";
187 break;
188 case DUPLEX_FULL:
189 duplex = "full";
190 break;
191 default:
192 duplex = "unknown";
193 break;
194 }
195 ret = sprintf(buf, "%s\n", duplex);
196 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000197 }
198 rtnl_unlock();
199 return ret;
200}
201
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700202static ssize_t show_dormant(struct device *dev,
203 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800204{
205 struct net_device *netdev = to_net_dev(dev);
206
207 if (netif_running(netdev))
208 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
209
210 return -EINVAL;
211}
212
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700213static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800214 "unknown",
215 "notpresent", /* currently unused */
216 "down",
217 "lowerlayerdown",
218 "testing", /* currently unused */
219 "dormant",
220 "up"
221};
222
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700223static ssize_t show_operstate(struct device *dev,
224 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800225{
226 const struct net_device *netdev = to_net_dev(dev);
227 unsigned char operstate;
228
229 read_lock(&dev_base_lock);
230 operstate = netdev->operstate;
231 if (!netif_running(netdev))
232 operstate = IF_OPER_DOWN;
233 read_unlock(&dev_base_lock);
234
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700235 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800236 return -EINVAL; /* should not happen */
237
238 return sprintf(buf, "%s\n", operstates[operstate]);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* read-write attributes */
242NETDEVICE_SHOW(mtu, fmt_dec);
243
244static int change_mtu(struct net_device *net, unsigned long new_mtu)
245{
246 return dev_set_mtu(net, (int) new_mtu);
247}
248
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700249static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700252 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255NETDEVICE_SHOW(flags, fmt_hex);
256
257static int change_flags(struct net_device *net, unsigned long new_flags)
258{
Eric Dumazet95c96172012-04-15 05:58:06 +0000259 return dev_change_flags(net, (unsigned int) new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700262static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700265 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
269
270static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
271{
272 net->tx_queue_len = new_len;
273 return 0;
274}
275
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700276static ssize_t store_tx_queue_len(struct device *dev,
277 struct device_attribute *attr,
278 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000280 if (!capable(CAP_NET_ADMIN))
281 return -EPERM;
282
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700283 return netdev_store(dev, attr, buf, len, change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700286static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t len)
288{
289 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000290 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700291 size_t count = len;
292 ssize_t ret;
293
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000294 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700295 return -EPERM;
296
297 /* ignore trailing newline */
298 if (len > 0 && buf[len - 1] == '\n')
299 --count;
300
Eric W. Biederman336ca572009-05-13 16:57:25 +0000301 if (!rtnl_trylock())
302 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700303 ret = dev_set_alias(netdev, buf, count);
304 rtnl_unlock();
305
306 return ret < 0 ? ret : len;
307}
308
309static ssize_t show_ifalias(struct device *dev,
310 struct device_attribute *attr, char *buf)
311{
312 const struct net_device *netdev = to_net_dev(dev);
313 ssize_t ret = 0;
314
Eric W. Biederman336ca572009-05-13 16:57:25 +0000315 if (!rtnl_trylock())
316 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700317 if (netdev->ifalias)
318 ret = sprintf(buf, "%s\n", netdev->ifalias);
319 rtnl_unlock();
320 return ret;
321}
322
Vlad Dogarua512b922011-01-24 03:37:29 +0000323NETDEVICE_SHOW(group, fmt_dec);
324
325static int change_group(struct net_device *net, unsigned long new_group)
326{
327 dev_set_group(net, (int) new_group);
328 return 0;
329}
330
331static ssize_t store_group(struct device *dev, struct device_attribute *attr,
332 const char *buf, size_t len)
333{
334 return netdev_store(dev, attr, buf, len, change_group);
335}
336
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700337static struct device_attribute net_class_attributes[] = {
Stefan Assmannc1f79422010-07-22 02:50:21 +0000338 __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100339 __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
David Woodhouse9d296722008-04-20 16:07:43 -0700340 __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700341 __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100342 __ATTR(iflink, S_IRUGO, show_iflink, NULL),
343 __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100344 __ATTR(type, S_IRUGO, show_type, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800345 __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100346 __ATTR(address, S_IRUGO, show_address, NULL),
347 __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000348 __ATTR(carrier, S_IRUGO | S_IWUSR, show_carrier, store_carrier),
Andy Gospodarekd519e172009-10-02 09:26:12 +0000349 __ATTR(speed, S_IRUGO, show_speed, NULL),
350 __ATTR(duplex, S_IRUGO, show_duplex, NULL),
Stefan Rompfb00055a2006-03-20 17:09:11 -0800351 __ATTR(dormant, S_IRUGO, show_dormant, NULL),
352 __ATTR(operstate, S_IRUGO, show_operstate, NULL),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100353 __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
354 __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
355 __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
356 store_tx_queue_len),
Xiaotian Fengb6644cb2011-02-09 19:16:15 -0800357 __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
Kay Sieversfd586ba2005-12-19 01:42:56 +0100358 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700362static ssize_t netstat_show(const struct device *d,
363 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned long offset)
365{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700366 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ssize_t ret = -EINVAL;
368
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000369 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
370 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700373 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700374 struct rtnl_link_stats64 temp;
375 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
376
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000377 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 read_unlock(&dev_base_lock);
380 return ret;
381}
382
383/* generate a read-only statistics attribute */
384#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700385static ssize_t show_##name(struct device *d, \
386 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700388 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000389 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390} \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700391static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393NETSTAT_ENTRY(rx_packets);
394NETSTAT_ENTRY(tx_packets);
395NETSTAT_ENTRY(rx_bytes);
396NETSTAT_ENTRY(tx_bytes);
397NETSTAT_ENTRY(rx_errors);
398NETSTAT_ENTRY(tx_errors);
399NETSTAT_ENTRY(rx_dropped);
400NETSTAT_ENTRY(tx_dropped);
401NETSTAT_ENTRY(multicast);
402NETSTAT_ENTRY(collisions);
403NETSTAT_ENTRY(rx_length_errors);
404NETSTAT_ENTRY(rx_over_errors);
405NETSTAT_ENTRY(rx_crc_errors);
406NETSTAT_ENTRY(rx_frame_errors);
407NETSTAT_ENTRY(rx_fifo_errors);
408NETSTAT_ENTRY(rx_missed_errors);
409NETSTAT_ENTRY(tx_aborted_errors);
410NETSTAT_ENTRY(tx_carrier_errors);
411NETSTAT_ENTRY(tx_fifo_errors);
412NETSTAT_ENTRY(tx_heartbeat_errors);
413NETSTAT_ENTRY(tx_window_errors);
414NETSTAT_ENTRY(rx_compressed);
415NETSTAT_ENTRY(tx_compressed);
416
417static struct attribute *netstat_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700418 &dev_attr_rx_packets.attr,
419 &dev_attr_tx_packets.attr,
420 &dev_attr_rx_bytes.attr,
421 &dev_attr_tx_bytes.attr,
422 &dev_attr_rx_errors.attr,
423 &dev_attr_tx_errors.attr,
424 &dev_attr_rx_dropped.attr,
425 &dev_attr_tx_dropped.attr,
426 &dev_attr_multicast.attr,
427 &dev_attr_collisions.attr,
428 &dev_attr_rx_length_errors.attr,
429 &dev_attr_rx_over_errors.attr,
430 &dev_attr_rx_crc_errors.attr,
431 &dev_attr_rx_frame_errors.attr,
432 &dev_attr_rx_fifo_errors.attr,
433 &dev_attr_rx_missed_errors.attr,
434 &dev_attr_tx_aborted_errors.attr,
435 &dev_attr_tx_carrier_errors.attr,
436 &dev_attr_tx_fifo_errors.attr,
437 &dev_attr_tx_heartbeat_errors.attr,
438 &dev_attr_tx_window_errors.attr,
439 &dev_attr_rx_compressed.attr,
440 &dev_attr_tx_compressed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 NULL
442};
443
444
445static struct attribute_group netstat_group = {
446 .name = "statistics",
447 .attrs = netstat_attrs,
448};
Johannes Berg38c1a012012-11-16 20:46:19 +0100449
450#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
451static struct attribute *wireless_attrs[] = {
452 NULL
453};
454
455static struct attribute_group wireless_group = {
456 .name = "wireless",
457 .attrs = wireless_attrs,
458};
459#endif
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700460#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Stephen Rothwell30bde1f2010-03-29 01:00:44 -0700462#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000463/*
464 * RX queue sysfs structures and functions.
465 */
466struct rx_queue_attribute {
467 struct attribute attr;
468 ssize_t (*show)(struct netdev_rx_queue *queue,
469 struct rx_queue_attribute *attr, char *buf);
470 ssize_t (*store)(struct netdev_rx_queue *queue,
471 struct rx_queue_attribute *attr, const char *buf, size_t len);
472};
473#define to_rx_queue_attr(_attr) container_of(_attr, \
474 struct rx_queue_attribute, attr)
475
476#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
477
478static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
479 char *buf)
480{
481 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
482 struct netdev_rx_queue *queue = to_rx_queue(kobj);
483
484 if (!attribute->show)
485 return -EIO;
486
487 return attribute->show(queue, attribute, buf);
488}
489
490static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
491 const char *buf, size_t count)
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->store)
497 return -EIO;
498
499 return attribute->store(queue, attribute, buf, count);
500}
501
stephen hemmingerfa50d642010-08-31 12:14:13 +0000502static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000503 .show = rx_queue_attr_show,
504 .store = rx_queue_attr_store,
505};
506
507static ssize_t show_rps_map(struct netdev_rx_queue *queue,
508 struct rx_queue_attribute *attribute, char *buf)
509{
510 struct rps_map *map;
511 cpumask_var_t mask;
512 size_t len = 0;
513 int i;
514
515 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
516 return -ENOMEM;
517
518 rcu_read_lock();
519 map = rcu_dereference(queue->rps_map);
520 if (map)
521 for (i = 0; i < map->len; i++)
522 cpumask_set_cpu(map->cpus[i], mask);
523
524 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
525 if (PAGE_SIZE - len < 3) {
526 rcu_read_unlock();
527 free_cpumask_var(mask);
528 return -EINVAL;
529 }
530 rcu_read_unlock();
531
532 free_cpumask_var(mask);
533 len += sprintf(buf + len, "\n");
534 return len;
535}
536
Eric Dumazetf5acb902010-04-19 14:40:57 -0700537static ssize_t store_rps_map(struct netdev_rx_queue *queue,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000538 struct rx_queue_attribute *attribute,
539 const char *buf, size_t len)
540{
541 struct rps_map *old_map, *map;
542 cpumask_var_t mask;
543 int err, cpu, i;
544 static DEFINE_SPINLOCK(rps_map_lock);
545
546 if (!capable(CAP_NET_ADMIN))
547 return -EPERM;
548
549 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
550 return -ENOMEM;
551
552 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
553 if (err) {
554 free_cpumask_var(mask);
555 return err;
556 }
557
Eric Dumazet95c96172012-04-15 05:58:06 +0000558 map = kzalloc(max_t(unsigned int,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000559 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
560 GFP_KERNEL);
561 if (!map) {
562 free_cpumask_var(mask);
563 return -ENOMEM;
564 }
565
566 i = 0;
567 for_each_cpu_and(cpu, mask, cpu_online_mask)
568 map->cpus[i++] = cpu;
569
570 if (i)
571 map->len = i;
572 else {
573 kfree(map);
574 map = NULL;
575 }
576
577 spin_lock(&rps_map_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000578 old_map = rcu_dereference_protected(queue->rps_map,
579 lockdep_is_held(&rps_map_lock));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000580 rcu_assign_pointer(queue->rps_map, map);
581 spin_unlock(&rps_map_lock);
582
Eric Dumazetadc93002011-11-17 03:13:26 +0000583 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100584 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000585 if (old_map) {
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800586 kfree_rcu(old_map, rcu);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100587 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +0000588 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000589 free_cpumask_var(mask);
590 return len;
591}
592
Tom Herbertfec5e652010-04-16 16:01:27 -0700593static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
594 struct rx_queue_attribute *attr,
595 char *buf)
596{
597 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000598 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700599
600 rcu_read_lock();
601 flow_table = rcu_dereference(queue->rps_flow_table);
602 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000603 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700604 rcu_read_unlock();
605
Eric Dumazet60b778c2011-12-24 06:56:49 +0000606 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700607}
608
609static void rps_dev_flow_table_release_work(struct work_struct *work)
610{
611 struct rps_dev_flow_table *table = container_of(work,
612 struct rps_dev_flow_table, free_work);
613
614 vfree(table);
615}
616
617static void rps_dev_flow_table_release(struct rcu_head *rcu)
618{
619 struct rps_dev_flow_table *table = container_of(rcu,
620 struct rps_dev_flow_table, rcu);
621
622 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
623 schedule_work(&table->free_work);
624}
625
Eric Dumazetf5acb902010-04-19 14:40:57 -0700626static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700627 struct rx_queue_attribute *attr,
628 const char *buf, size_t len)
629{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000630 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700631 struct rps_dev_flow_table *table, *old_table;
632 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000633 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700634
635 if (!capable(CAP_NET_ADMIN))
636 return -EPERM;
637
Eric Dumazet60b778c2011-12-24 06:56:49 +0000638 rc = kstrtoul(buf, 0, &count);
639 if (rc < 0)
640 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700641
642 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000643 mask = count - 1;
644 /* mask = roundup_pow_of_two(count) - 1;
645 * without overflows...
646 */
647 while ((mask | (mask >> 1)) != mask)
648 mask |= (mask >> 1);
649 /* On 64 bit arches, must check mask fits in table->mask (u32),
650 * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
651 * doesnt overflow.
652 */
653#if BITS_PER_LONG > 32
654 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000655 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000656#else
657 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000658 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700659 /* Enforce a limit to prevent overflow */
660 return -EINVAL;
661 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000662#endif
663 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700664 if (!table)
665 return -ENOMEM;
666
Eric Dumazet60b778c2011-12-24 06:56:49 +0000667 table->mask = mask;
668 for (count = 0; count <= mask; count++)
669 table->flows[count].cpu = RPS_NO_CPU;
Tom Herbertfec5e652010-04-16 16:01:27 -0700670 } else
671 table = NULL;
672
673 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000674 old_table = rcu_dereference_protected(queue->rps_flow_table,
675 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700676 rcu_assign_pointer(queue->rps_flow_table, table);
677 spin_unlock(&rps_dev_flow_lock);
678
679 if (old_table)
680 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
681
682 return len;
683}
684
Tom Herbert0a9627f2010-03-16 08:03:29 +0000685static struct rx_queue_attribute rps_cpus_attribute =
686 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
687
Tom Herbertfec5e652010-04-16 16:01:27 -0700688
689static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
690 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
691 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
692
Tom Herbert0a9627f2010-03-16 08:03:29 +0000693static struct attribute *rx_queue_default_attrs[] = {
694 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700695 &rps_dev_flow_table_cnt_attribute.attr,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000696 NULL
697};
698
699static void rx_queue_release(struct kobject *kobj)
700{
701 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000702 struct rps_map *map;
703 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000704
Tom Herbertfec5e652010-04-16 16:01:27 -0700705
Eric Dumazet33d480c2011-08-11 19:30:52 +0000706 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000707 if (map) {
708 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800709 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000710 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000711
Eric Dumazet33d480c2011-08-11 19:30:52 +0000712 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000713 if (flow_table) {
714 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000715 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000716 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000717
John Fastabend9ea19482010-11-16 06:31:39 +0000718 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000719 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000720}
721
722static struct kobj_type rx_queue_ktype = {
723 .sysfs_ops = &rx_queue_sysfs_ops,
724 .release = rx_queue_release,
725 .default_attrs = rx_queue_default_attrs,
726};
727
728static int rx_queue_add_kobject(struct net_device *net, int index)
729{
730 struct netdev_rx_queue *queue = net->_rx + index;
731 struct kobject *kobj = &queue->kobj;
732 int error = 0;
733
734 kobj->kset = net->queues_kset;
735 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
736 "rx-%u", index);
737 if (error) {
738 kobject_put(kobj);
739 return error;
740 }
741
742 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000743 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000744
745 return error;
746}
Tom Herbertbf264142010-11-26 08:36:09 +0000747#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000748
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000749int
750net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000751{
Tom Herbertbf264142010-11-26 08:36:09 +0000752#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000753 int i;
754 int error = 0;
755
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000756 for (i = old_num; i < new_num; i++) {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000757 error = rx_queue_add_kobject(net, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000758 if (error) {
759 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000760 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000761 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000762 }
763
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000764 while (--i >= new_num)
765 kobject_put(&net->_rx[i].kobj);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000766
767 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000768#else
769 return 0;
770#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000771}
772
david decotignyccf5ff62011-11-16 12:15:10 +0000773#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000774/*
775 * netdev_queue sysfs structures and functions.
776 */
777struct netdev_queue_attribute {
778 struct attribute attr;
779 ssize_t (*show)(struct netdev_queue *queue,
780 struct netdev_queue_attribute *attr, char *buf);
781 ssize_t (*store)(struct netdev_queue *queue,
782 struct netdev_queue_attribute *attr, const char *buf, size_t len);
783};
784#define to_netdev_queue_attr(_attr) container_of(_attr, \
785 struct netdev_queue_attribute, attr)
786
787#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
788
789static ssize_t netdev_queue_attr_show(struct kobject *kobj,
790 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000791{
Tom Herbert1d24eb42010-11-21 13:17:27 +0000792 struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
793 struct netdev_queue *queue = to_netdev_queue(kobj);
794
795 if (!attribute->show)
796 return -EIO;
797
798 return attribute->show(queue, attribute, buf);
799}
800
801static ssize_t netdev_queue_attr_store(struct kobject *kobj,
802 struct attribute *attr,
803 const char *buf, size_t count)
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->store)
809 return -EIO;
810
811 return attribute->store(queue, attribute, buf, count);
812}
813
814static const struct sysfs_ops netdev_queue_sysfs_ops = {
815 .show = netdev_queue_attr_show,
816 .store = netdev_queue_attr_store,
817};
818
david decotignyccf5ff62011-11-16 12:15:10 +0000819static ssize_t show_trans_timeout(struct netdev_queue *queue,
820 struct netdev_queue_attribute *attribute,
821 char *buf)
822{
823 unsigned long trans_timeout;
824
825 spin_lock_irq(&queue->_xmit_lock);
826 trans_timeout = queue->trans_timeout;
827 spin_unlock_irq(&queue->_xmit_lock);
828
829 return sprintf(buf, "%lu", trans_timeout);
830}
831
832static struct netdev_queue_attribute queue_trans_timeout =
833 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
834
Tom Herbert114cf582011-11-28 16:33:09 +0000835#ifdef CONFIG_BQL
836/*
837 * Byte queue limits sysfs structures and functions.
838 */
839static ssize_t bql_show(char *buf, unsigned int value)
840{
841 return sprintf(buf, "%u\n", value);
842}
843
844static ssize_t bql_set(const char *buf, const size_t count,
845 unsigned int *pvalue)
846{
847 unsigned int value;
848 int err;
849
850 if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
851 value = DQL_MAX_LIMIT;
852 else {
853 err = kstrtouint(buf, 10, &value);
854 if (err < 0)
855 return err;
856 if (value > DQL_MAX_LIMIT)
857 return -EINVAL;
858 }
859
860 *pvalue = value;
861
862 return count;
863}
864
865static ssize_t bql_show_hold_time(struct netdev_queue *queue,
866 struct netdev_queue_attribute *attr,
867 char *buf)
868{
869 struct dql *dql = &queue->dql;
870
871 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
872}
873
874static ssize_t bql_set_hold_time(struct netdev_queue *queue,
875 struct netdev_queue_attribute *attribute,
876 const char *buf, size_t len)
877{
878 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +0000879 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +0000880 int err;
881
882 err = kstrtouint(buf, 10, &value);
883 if (err < 0)
884 return err;
885
886 dql->slack_hold_time = msecs_to_jiffies(value);
887
888 return len;
889}
890
891static struct netdev_queue_attribute bql_hold_time_attribute =
892 __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
893 bql_set_hold_time);
894
895static ssize_t bql_show_inflight(struct netdev_queue *queue,
896 struct netdev_queue_attribute *attr,
897 char *buf)
898{
899 struct dql *dql = &queue->dql;
900
901 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
902}
903
904static struct netdev_queue_attribute bql_inflight_attribute =
Hiroaki SHIMODA795d9a22012-01-14 07:10:21 +0000905 __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +0000906
907#define BQL_ATTR(NAME, FIELD) \
908static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
909 struct netdev_queue_attribute *attr, \
910 char *buf) \
911{ \
912 return bql_show(buf, queue->dql.FIELD); \
913} \
914 \
915static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
916 struct netdev_queue_attribute *attr, \
917 const char *buf, size_t len) \
918{ \
919 return bql_set(buf, len, &queue->dql.FIELD); \
920} \
921 \
922static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
923 __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
924 bql_set_ ## NAME);
925
926BQL_ATTR(limit, limit)
927BQL_ATTR(limit_max, max_limit)
928BQL_ATTR(limit_min, min_limit)
929
930static struct attribute *dql_attrs[] = {
931 &bql_limit_attribute.attr,
932 &bql_limit_max_attribute.attr,
933 &bql_limit_min_attribute.attr,
934 &bql_hold_time_attribute.attr,
935 &bql_inflight_attribute.attr,
936 NULL
937};
938
939static struct attribute_group dql_group = {
940 .name = "byte_queue_limits",
941 .attrs = dql_attrs,
942};
943#endif /* CONFIG_BQL */
944
david decotignyccf5ff62011-11-16 12:15:10 +0000945#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000946static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
947{
948 struct net_device *dev = queue->dev;
949 int i;
950
951 for (i = 0; i < dev->num_tx_queues; i++)
952 if (queue == &dev->_tx[i])
953 break;
954
955 BUG_ON(i >= dev->num_tx_queues);
956
957 return i;
958}
959
960
961static ssize_t show_xps_map(struct netdev_queue *queue,
962 struct netdev_queue_attribute *attribute, char *buf)
963{
964 struct net_device *dev = queue->dev;
965 struct xps_dev_maps *dev_maps;
966 cpumask_var_t mask;
967 unsigned long index;
968 size_t len = 0;
969 int i;
970
971 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
972 return -ENOMEM;
973
974 index = get_netdev_queue_index(queue);
975
976 rcu_read_lock();
977 dev_maps = rcu_dereference(dev->xps_maps);
978 if (dev_maps) {
979 for_each_possible_cpu(i) {
980 struct xps_map *map =
981 rcu_dereference(dev_maps->cpu_map[i]);
982 if (map) {
983 int j;
984 for (j = 0; j < map->len; j++) {
985 if (map->queues[j] == index) {
986 cpumask_set_cpu(i, mask);
987 break;
988 }
989 }
990 }
991 }
992 }
993 rcu_read_unlock();
994
995 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
996 if (PAGE_SIZE - len < 3) {
997 free_cpumask_var(mask);
998 return -EINVAL;
999 }
1000
1001 free_cpumask_var(mask);
1002 len += sprintf(buf + len, "\n");
1003 return len;
1004}
1005
Tom Herbert1d24eb42010-11-21 13:17:27 +00001006static ssize_t store_xps_map(struct netdev_queue *queue,
1007 struct netdev_queue_attribute *attribute,
1008 const char *buf, size_t len)
1009{
1010 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001011 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001012 cpumask_var_t mask;
1013 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001014
1015 if (!capable(CAP_NET_ADMIN))
1016 return -EPERM;
1017
1018 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1019 return -ENOMEM;
1020
1021 index = get_netdev_queue_index(queue);
1022
1023 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1024 if (err) {
1025 free_cpumask_var(mask);
1026 return err;
1027 }
1028
Alexander Duyck537c00d2013-01-10 08:57:02 +00001029 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001030
1031 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001032
Alexander Duyck537c00d2013-01-10 08:57:02 +00001033 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001034}
1035
1036static struct netdev_queue_attribute xps_cpus_attribute =
1037 __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
david decotignyccf5ff62011-11-16 12:15:10 +00001038#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001039
1040static struct attribute *netdev_queue_default_attrs[] = {
david decotignyccf5ff62011-11-16 12:15:10 +00001041 &queue_trans_timeout.attr,
1042#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001043 &xps_cpus_attribute.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001044#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001045 NULL
1046};
1047
1048static void netdev_queue_release(struct kobject *kobj)
1049{
1050 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001051
Tom Herbert1d24eb42010-11-21 13:17:27 +00001052 memset(kobj, 0, sizeof(*kobj));
1053 dev_put(queue->dev);
1054}
1055
1056static struct kobj_type netdev_queue_ktype = {
1057 .sysfs_ops = &netdev_queue_sysfs_ops,
1058 .release = netdev_queue_release,
1059 .default_attrs = netdev_queue_default_attrs,
1060};
1061
1062static int netdev_queue_add_kobject(struct net_device *net, int index)
1063{
1064 struct netdev_queue *queue = net->_tx + index;
1065 struct kobject *kobj = &queue->kobj;
1066 int error = 0;
1067
1068 kobj->kset = net->queues_kset;
1069 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1070 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001071 if (error)
1072 goto exit;
1073
1074#ifdef CONFIG_BQL
1075 error = sysfs_create_group(kobj, &dql_group);
1076 if (error)
1077 goto exit;
1078#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001079
1080 kobject_uevent(kobj, KOBJ_ADD);
1081 dev_hold(queue->dev);
1082
Tom Herbert114cf582011-11-28 16:33:09 +00001083 return 0;
1084exit:
1085 kobject_put(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001086 return error;
1087}
david decotignyccf5ff62011-11-16 12:15:10 +00001088#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001089
1090int
1091netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
1092{
david decotignyccf5ff62011-11-16 12:15:10 +00001093#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001094 int i;
1095 int error = 0;
1096
1097 for (i = old_num; i < new_num; i++) {
1098 error = netdev_queue_add_kobject(net, i);
1099 if (error) {
1100 new_num = old_num;
1101 break;
1102 }
1103 }
1104
Tom Herbert114cf582011-11-28 16:33:09 +00001105 while (--i >= new_num) {
1106 struct netdev_queue *queue = net->_tx + i;
1107
1108#ifdef CONFIG_BQL
1109 sysfs_remove_group(&queue->kobj, &dql_group);
1110#endif
1111 kobject_put(&queue->kobj);
1112 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001113
1114 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001115#else
1116 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001117#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001118}
1119
1120static int register_queue_kobjects(struct net_device *net)
1121{
Tom Herbertbf264142010-11-26 08:36:09 +00001122 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001123
david decotignyccf5ff62011-11-16 12:15:10 +00001124#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001125 net->queues_kset = kset_create_and_add("queues",
1126 NULL, &net->dev.kobj);
1127 if (!net->queues_kset)
1128 return -ENOMEM;
Tom Herbertbf264142010-11-26 08:36:09 +00001129#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001130
Tom Herbertbf264142010-11-26 08:36:09 +00001131#ifdef CONFIG_RPS
1132 real_rx = net->real_num_rx_queues;
1133#endif
1134 real_tx = net->real_num_tx_queues;
1135
1136 error = net_rx_queue_update_kobjects(net, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001137 if (error)
1138 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001139 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001140
Tom Herbertbf264142010-11-26 08:36:09 +00001141 error = netdev_queue_update_kobjects(net, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001142 if (error)
1143 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001144 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001145
1146 return 0;
1147
1148error:
1149 netdev_queue_update_kobjects(net, txq, 0);
1150 net_rx_queue_update_kobjects(net, rxq, 0);
1151 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001152}
1153
Tom Herbert1d24eb42010-11-21 13:17:27 +00001154static void remove_queue_kobjects(struct net_device *net)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001155{
Tom Herbertbf264142010-11-26 08:36:09 +00001156 int real_rx = 0, real_tx = 0;
1157
1158#ifdef CONFIG_RPS
1159 real_rx = net->real_num_rx_queues;
1160#endif
1161 real_tx = net->real_num_tx_queues;
1162
1163 net_rx_queue_update_kobjects(net, real_rx, 0);
1164 netdev_queue_update_kobjects(net, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001165#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +00001166 kset_unregister(net->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001167#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001168}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001169
Al Viroa685e082011-06-08 21:13:01 -04001170static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001171{
Al Viroa685e082011-06-08 21:13:01 -04001172 struct net *ns = current->nsproxy->net_ns;
1173#ifdef CONFIG_NET_NS
1174 if (ns)
1175 atomic_inc(&ns->passive);
1176#endif
1177 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001178}
1179
1180static const void *net_initial_ns(void)
1181{
1182 return &init_net;
1183}
1184
1185static const void *net_netlink_ns(struct sock *sk)
1186{
1187 return sock_net(sk);
1188}
1189
Johannes Berg04600792010-08-05 17:45:15 +02001190struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001191 .type = KOBJ_NS_TYPE_NET,
Al Viroa685e082011-06-08 21:13:01 -04001192 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001193 .netlink_ns = net_netlink_ns,
1194 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001195 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001196};
Johannes Berg04600792010-08-05 17:45:15 +02001197EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001198
Kay Sievers7eff2e72007-08-14 15:15:12 +02001199static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001201 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001202 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Kay Sievers312c0042005-11-16 09:00:00 +01001204 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001205 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001206 if (retval)
1207 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001209 /* pass ifindex to uevent.
1210 * ifindex is useful as it won't change (interface name may change)
1211 * and is what RtNetlink uses natively. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001212 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001213
Eric Rannaudbf624562007-03-30 22:23:12 -07001214exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001215 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001219 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001220 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001222static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001224 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 BUG_ON(dev->reg_state != NETREG_RELEASED);
1227
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001228 kfree(dev->ifalias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 kfree((char *)dev - dev->padded);
1230}
1231
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001232static const void *net_namespace(struct device *d)
1233{
1234 struct net_device *dev;
1235 dev = container_of(d, struct net_device, dev);
1236 return dev_net(dev);
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static struct class net_class = {
1240 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001241 .dev_release = netdev_release,
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001242#ifdef CONFIG_SYSFS
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001243 .dev_attrs = net_class_attributes,
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001244#endif /* CONFIG_SYSFS */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001245 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001246 .ns_type = &net_ns_type_operations,
1247 .namespace = net_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248};
1249
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001250/* Delete sysfs entries but hold kobject reference until after all
1251 * netdev references are gone.
1252 */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001253void netdev_unregister_kobject(struct net_device * net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001255 struct device *dev = &(net->dev);
1256
1257 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001258
Tom Herbert1d24eb42010-11-21 13:17:27 +00001259 remove_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001260
Ming Lei9802c8e2013-02-22 16:34:16 -08001261 pm_runtime_set_memalloc_noio(dev, false);
1262
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001263 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266/* Create sysfs entries for network device. */
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001267int netdev_register_kobject(struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001269 struct device *dev = &(net->dev);
David Brownella4dbd672009-06-24 10:06:31 -07001270 const struct attribute_group **groups = net->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001271 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001273 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001274 dev->class = &net_class;
1275 dev->platform_data = net;
1276 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Stephen Hemmingera2205472009-03-09 13:51:55 +00001278 dev_set_name(dev, "%s", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001280#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001281 /* Allow for a device specific group */
1282 if (*groups)
1283 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001285 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001286
1287#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1288 if (net->ieee80211_ptr)
1289 *groups++ = &wireless_group;
1290#if IS_ENABLED(CONFIG_WIRELESS_EXT)
1291 else if (net->wireless_handlers)
1292 *groups++ = &wireless_group;
1293#endif
1294#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001295#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Tom Herbert0a9627f2010-03-16 08:03:29 +00001297 error = device_add(dev);
1298 if (error)
1299 return error;
1300
Tom Herbert1d24eb42010-11-21 13:17:27 +00001301 error = register_queue_kobjects(net);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001302 if (error) {
1303 device_del(dev);
1304 return error;
1305 }
1306
Ming Lei9802c8e2013-02-22 16:34:16 -08001307 pm_runtime_set_memalloc_noio(dev, true);
1308
Tom Herbert0a9627f2010-03-16 08:03:29 +00001309 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Jay Vosburghb8a97872008-06-13 18:12:04 -07001312int netdev_class_create_file(struct class_attribute *class_attr)
1313{
1314 return class_create_file(&net_class, class_attr);
1315}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001316EXPORT_SYMBOL(netdev_class_create_file);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001317
1318void netdev_class_remove_file(struct class_attribute *class_attr)
1319{
1320 class_remove_file(&net_class, class_attr);
1321}
Jay Vosburghb8a97872008-06-13 18:12:04 -07001322EXPORT_SYMBOL(netdev_class_remove_file);
1323
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001324int netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001326 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return class_register(&net_class);
1328}