xref: /openbmc/linux/net/core/net-sysfs.c (revision adc9300e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net-sysfs.c - network device class and attributes
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
124fc268d2SRandy Dunlap #include <linux/capability.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/netdevice.h>
151da177e4SLinus Torvalds #include <linux/if_arp.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
17608b4b95SEric W. Biederman #include <linux/nsproxy.h>
181da177e4SLinus Torvalds #include <net/sock.h>
19608b4b95SEric W. Biederman #include <net/net_namespace.h>
201da177e4SLinus Torvalds #include <linux/rtnetlink.h>
211da177e4SLinus Torvalds #include <linux/wireless.h>
22fec5e652STom Herbert #include <linux/vmalloc.h>
23bc3b2d7fSPaul Gortmaker #include <linux/export.h>
248f1546caSJohannes Berg #include <net/wext.h>
251da177e4SLinus Torvalds 
26342709efSPavel Emelyanov #include "net-sysfs.h"
27342709efSPavel Emelyanov 
288b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
291da177e4SLinus Torvalds static const char fmt_hex[] = "%#x\n";
30d1102b59SDavid S. Miller static const char fmt_long_hex[] = "%#lx\n";
311da177e4SLinus Torvalds static const char fmt_dec[] = "%d\n";
328ae6dacaSDavid Decotigny static const char fmt_udec[] = "%u\n";
331da177e4SLinus Torvalds static const char fmt_ulong[] = "%lu\n";
34be1f3c2cSBen Hutchings static const char fmt_u64[] = "%llu\n";
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds static inline int dev_isalive(const struct net_device *dev)
371da177e4SLinus Torvalds {
38fe9925b5SStephen Hemminger 	return dev->reg_state <= NETREG_REGISTERED;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* use same locking rules as GIF* ioctl's */
4243cb76d9SGreg Kroah-Hartman static ssize_t netdev_show(const struct device *dev,
4343cb76d9SGreg Kroah-Hartman 			   struct device_attribute *attr, char *buf,
441da177e4SLinus Torvalds 			   ssize_t (*format)(const struct net_device *, char *))
451da177e4SLinus Torvalds {
4643cb76d9SGreg Kroah-Hartman 	struct net_device *net = to_net_dev(dev);
471da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
501da177e4SLinus Torvalds 	if (dev_isalive(net))
511da177e4SLinus Torvalds 		ret = (*format)(net, buf);
521da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	return ret;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* generate a show function for simple field */
581da177e4SLinus Torvalds #define NETDEVICE_SHOW(field, format_string)				\
591da177e4SLinus Torvalds static ssize_t format_##field(const struct net_device *net, char *buf)	\
601da177e4SLinus Torvalds {									\
611da177e4SLinus Torvalds 	return sprintf(buf, format_string, net->field);			\
621da177e4SLinus Torvalds }									\
6343cb76d9SGreg Kroah-Hartman static ssize_t show_##field(struct device *dev,				\
6443cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)	\
651da177e4SLinus Torvalds {									\
6643cb76d9SGreg Kroah-Hartman 	return netdev_show(dev, attr, buf, format_##field);		\
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* use same locking and permission rules as SIF* ioctl's */
7143cb76d9SGreg Kroah-Hartman static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
721da177e4SLinus Torvalds 			    const char *buf, size_t len,
731da177e4SLinus Torvalds 			    int (*set)(struct net_device *, unsigned long))
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct net_device *net = to_net_dev(dev);
761da177e4SLinus Torvalds 	char *endp;
771da177e4SLinus Torvalds 	unsigned long new;
781da177e4SLinus Torvalds 	int ret = -EINVAL;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
811da177e4SLinus Torvalds 		return -EPERM;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	new = simple_strtoul(buf, &endp, 0);
841da177e4SLinus Torvalds 	if (endp == buf)
851da177e4SLinus Torvalds 		goto err;
861da177e4SLinus Torvalds 
875a5990d3SStephen Hemminger 	if (!rtnl_trylock())
88336ca57cSEric W. Biederman 		return restart_syscall();
895a5990d3SStephen Hemminger 
901da177e4SLinus Torvalds 	if (dev_isalive(net)) {
911da177e4SLinus Torvalds 		if ((ret = (*set)(net, new)) == 0)
921da177e4SLinus Torvalds 			ret = len;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	rtnl_unlock();
951da177e4SLinus Torvalds  err:
961da177e4SLinus Torvalds 	return ret;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
999d29672cSDavid Woodhouse NETDEVICE_SHOW(dev_id, fmt_hex);
100c1f79426SStefan Assmann NETDEVICE_SHOW(addr_assign_type, fmt_dec);
101fd586bacSKay Sievers NETDEVICE_SHOW(addr_len, fmt_dec);
102fd586bacSKay Sievers NETDEVICE_SHOW(iflink, fmt_dec);
103fd586bacSKay Sievers NETDEVICE_SHOW(ifindex, fmt_dec);
104fd586bacSKay Sievers NETDEVICE_SHOW(type, fmt_dec);
105b00055aaSStefan Rompf NETDEVICE_SHOW(link_mode, fmt_dec);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* use same locking rules as GIFHWADDR ioctl's */
10843cb76d9SGreg Kroah-Hartman static ssize_t show_address(struct device *dev, struct device_attribute *attr,
10943cb76d9SGreg Kroah-Hartman 			    char *buf)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	struct net_device *net = to_net_dev(dev);
1121da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
1151da177e4SLinus Torvalds 	if (dev_isalive(net))
1167ffc49a6SMichael Chan 		ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
1171da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
1181da177e4SLinus Torvalds 	return ret;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
12143cb76d9SGreg Kroah-Hartman static ssize_t show_broadcast(struct device *dev,
12243cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	struct net_device *net = to_net_dev(dev);
1251da177e4SLinus Torvalds 	if (dev_isalive(net))
1267ffc49a6SMichael Chan 		return sysfs_format_mac(buf, net->broadcast, net->addr_len);
1271da177e4SLinus Torvalds 	return -EINVAL;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
13043cb76d9SGreg Kroah-Hartman static ssize_t show_carrier(struct device *dev,
13143cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct net_device *netdev = to_net_dev(dev);
1341da177e4SLinus Torvalds 	if (netif_running(netdev)) {
1351da177e4SLinus Torvalds 		return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	return -EINVAL;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140d519e17eSAndy Gospodarek static ssize_t show_speed(struct device *dev,
141d519e17eSAndy Gospodarek 			  struct device_attribute *attr, char *buf)
142d519e17eSAndy Gospodarek {
143d519e17eSAndy Gospodarek 	struct net_device *netdev = to_net_dev(dev);
144d519e17eSAndy Gospodarek 	int ret = -EINVAL;
145d519e17eSAndy Gospodarek 
146d519e17eSAndy Gospodarek 	if (!rtnl_trylock())
147d519e17eSAndy Gospodarek 		return restart_syscall();
148d519e17eSAndy Gospodarek 
1498ae6dacaSDavid Decotigny 	if (netif_running(netdev)) {
1508ae6dacaSDavid Decotigny 		struct ethtool_cmd cmd;
1514bc71cb9SJiri Pirko 		if (!__ethtool_get_settings(netdev, &cmd))
1528ae6dacaSDavid Decotigny 			ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
153d519e17eSAndy Gospodarek 	}
154d519e17eSAndy Gospodarek 	rtnl_unlock();
155d519e17eSAndy Gospodarek 	return ret;
156d519e17eSAndy Gospodarek }
157d519e17eSAndy Gospodarek 
158d519e17eSAndy Gospodarek static ssize_t show_duplex(struct device *dev,
159d519e17eSAndy Gospodarek 			   struct device_attribute *attr, char *buf)
160d519e17eSAndy Gospodarek {
161d519e17eSAndy Gospodarek 	struct net_device *netdev = to_net_dev(dev);
162d519e17eSAndy Gospodarek 	int ret = -EINVAL;
163d519e17eSAndy Gospodarek 
164d519e17eSAndy Gospodarek 	if (!rtnl_trylock())
165d519e17eSAndy Gospodarek 		return restart_syscall();
166d519e17eSAndy Gospodarek 
1678ae6dacaSDavid Decotigny 	if (netif_running(netdev)) {
1688ae6dacaSDavid Decotigny 		struct ethtool_cmd cmd;
1694bc71cb9SJiri Pirko 		if (!__ethtool_get_settings(netdev, &cmd))
1708ae6dacaSDavid Decotigny 			ret = sprintf(buf, "%s\n",
1718ae6dacaSDavid Decotigny 				      cmd.duplex ? "full" : "half");
172d519e17eSAndy Gospodarek 	}
173d519e17eSAndy Gospodarek 	rtnl_unlock();
174d519e17eSAndy Gospodarek 	return ret;
175d519e17eSAndy Gospodarek }
176d519e17eSAndy Gospodarek 
17743cb76d9SGreg Kroah-Hartman static ssize_t show_dormant(struct device *dev,
17843cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)
179b00055aaSStefan Rompf {
180b00055aaSStefan Rompf 	struct net_device *netdev = to_net_dev(dev);
181b00055aaSStefan Rompf 
182b00055aaSStefan Rompf 	if (netif_running(netdev))
183b00055aaSStefan Rompf 		return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
184b00055aaSStefan Rompf 
185b00055aaSStefan Rompf 	return -EINVAL;
186b00055aaSStefan Rompf }
187b00055aaSStefan Rompf 
18836cbd3dcSJan Engelhardt static const char *const operstates[] = {
189b00055aaSStefan Rompf 	"unknown",
190b00055aaSStefan Rompf 	"notpresent", /* currently unused */
191b00055aaSStefan Rompf 	"down",
192b00055aaSStefan Rompf 	"lowerlayerdown",
193b00055aaSStefan Rompf 	"testing", /* currently unused */
194b00055aaSStefan Rompf 	"dormant",
195b00055aaSStefan Rompf 	"up"
196b00055aaSStefan Rompf };
197b00055aaSStefan Rompf 
19843cb76d9SGreg Kroah-Hartman static ssize_t show_operstate(struct device *dev,
19943cb76d9SGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)
200b00055aaSStefan Rompf {
201b00055aaSStefan Rompf 	const struct net_device *netdev = to_net_dev(dev);
202b00055aaSStefan Rompf 	unsigned char operstate;
203b00055aaSStefan Rompf 
204b00055aaSStefan Rompf 	read_lock(&dev_base_lock);
205b00055aaSStefan Rompf 	operstate = netdev->operstate;
206b00055aaSStefan Rompf 	if (!netif_running(netdev))
207b00055aaSStefan Rompf 		operstate = IF_OPER_DOWN;
208b00055aaSStefan Rompf 	read_unlock(&dev_base_lock);
209b00055aaSStefan Rompf 
210e3a5cd9eSAdrian Bunk 	if (operstate >= ARRAY_SIZE(operstates))
211b00055aaSStefan Rompf 		return -EINVAL; /* should not happen */
212b00055aaSStefan Rompf 
213b00055aaSStefan Rompf 	return sprintf(buf, "%s\n", operstates[operstate]);
214b00055aaSStefan Rompf }
215b00055aaSStefan Rompf 
2161da177e4SLinus Torvalds /* read-write attributes */
2171da177e4SLinus Torvalds NETDEVICE_SHOW(mtu, fmt_dec);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static int change_mtu(struct net_device *net, unsigned long new_mtu)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	return dev_set_mtu(net, (int) new_mtu);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
22443cb76d9SGreg Kroah-Hartman static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
22543cb76d9SGreg Kroah-Hartman 			 const char *buf, size_t len)
2261da177e4SLinus Torvalds {
22743cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_mtu);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds NETDEVICE_SHOW(flags, fmt_hex);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static int change_flags(struct net_device *net, unsigned long new_flags)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	return dev_change_flags(net, (unsigned) new_flags);
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
23743cb76d9SGreg Kroah-Hartman static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
23843cb76d9SGreg Kroah-Hartman 			   const char *buf, size_t len)
2391da177e4SLinus Torvalds {
24043cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_flags);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	net->tx_queue_len = new_len;
2481da177e4SLinus Torvalds 	return 0;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
25143cb76d9SGreg Kroah-Hartman static ssize_t store_tx_queue_len(struct device *dev,
25243cb76d9SGreg Kroah-Hartman 				  struct device_attribute *attr,
25343cb76d9SGreg Kroah-Hartman 				  const char *buf, size_t len)
2541da177e4SLinus Torvalds {
25543cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_tx_queue_len);
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2580b815a1aSStephen Hemminger static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
2590b815a1aSStephen Hemminger 			     const char *buf, size_t len)
2600b815a1aSStephen Hemminger {
2610b815a1aSStephen Hemminger 	struct net_device *netdev = to_net_dev(dev);
2620b815a1aSStephen Hemminger 	size_t count = len;
2630b815a1aSStephen Hemminger 	ssize_t ret;
2640b815a1aSStephen Hemminger 
2650b815a1aSStephen Hemminger 	if (!capable(CAP_NET_ADMIN))
2660b815a1aSStephen Hemminger 		return -EPERM;
2670b815a1aSStephen Hemminger 
2680b815a1aSStephen Hemminger 	/* ignore trailing newline */
2690b815a1aSStephen Hemminger 	if (len >  0 && buf[len - 1] == '\n')
2700b815a1aSStephen Hemminger 		--count;
2710b815a1aSStephen Hemminger 
272336ca57cSEric W. Biederman 	if (!rtnl_trylock())
273336ca57cSEric W. Biederman 		return restart_syscall();
2740b815a1aSStephen Hemminger 	ret = dev_set_alias(netdev, buf, count);
2750b815a1aSStephen Hemminger 	rtnl_unlock();
2760b815a1aSStephen Hemminger 
2770b815a1aSStephen Hemminger 	return ret < 0 ? ret : len;
2780b815a1aSStephen Hemminger }
2790b815a1aSStephen Hemminger 
2800b815a1aSStephen Hemminger static ssize_t show_ifalias(struct device *dev,
2810b815a1aSStephen Hemminger 			    struct device_attribute *attr, char *buf)
2820b815a1aSStephen Hemminger {
2830b815a1aSStephen Hemminger 	const struct net_device *netdev = to_net_dev(dev);
2840b815a1aSStephen Hemminger 	ssize_t ret = 0;
2850b815a1aSStephen Hemminger 
286336ca57cSEric W. Biederman 	if (!rtnl_trylock())
287336ca57cSEric W. Biederman 		return restart_syscall();
2880b815a1aSStephen Hemminger 	if (netdev->ifalias)
2890b815a1aSStephen Hemminger 		ret = sprintf(buf, "%s\n", netdev->ifalias);
2900b815a1aSStephen Hemminger 	rtnl_unlock();
2910b815a1aSStephen Hemminger 	return ret;
2920b815a1aSStephen Hemminger }
2930b815a1aSStephen Hemminger 
294a512b92bSVlad Dogaru NETDEVICE_SHOW(group, fmt_dec);
295a512b92bSVlad Dogaru 
296a512b92bSVlad Dogaru static int change_group(struct net_device *net, unsigned long new_group)
297a512b92bSVlad Dogaru {
298a512b92bSVlad Dogaru 	dev_set_group(net, (int) new_group);
299a512b92bSVlad Dogaru 	return 0;
300a512b92bSVlad Dogaru }
301a512b92bSVlad Dogaru 
302a512b92bSVlad Dogaru static ssize_t store_group(struct device *dev, struct device_attribute *attr,
303a512b92bSVlad Dogaru 			 const char *buf, size_t len)
304a512b92bSVlad Dogaru {
305a512b92bSVlad Dogaru 	return netdev_store(dev, attr, buf, len, change_group);
306a512b92bSVlad Dogaru }
307a512b92bSVlad Dogaru 
30843cb76d9SGreg Kroah-Hartman static struct device_attribute net_class_attributes[] = {
309c1f79426SStefan Assmann 	__ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
310fd586bacSKay Sievers 	__ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
3119d29672cSDavid Woodhouse 	__ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
3120b815a1aSStephen Hemminger 	__ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
313fd586bacSKay Sievers 	__ATTR(iflink, S_IRUGO, show_iflink, NULL),
314fd586bacSKay Sievers 	__ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
315fd586bacSKay Sievers 	__ATTR(type, S_IRUGO, show_type, NULL),
316b00055aaSStefan Rompf 	__ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
317fd586bacSKay Sievers 	__ATTR(address, S_IRUGO, show_address, NULL),
318fd586bacSKay Sievers 	__ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
319fd586bacSKay Sievers 	__ATTR(carrier, S_IRUGO, show_carrier, NULL),
320d519e17eSAndy Gospodarek 	__ATTR(speed, S_IRUGO, show_speed, NULL),
321d519e17eSAndy Gospodarek 	__ATTR(duplex, S_IRUGO, show_duplex, NULL),
322b00055aaSStefan Rompf 	__ATTR(dormant, S_IRUGO, show_dormant, NULL),
323b00055aaSStefan Rompf 	__ATTR(operstate, S_IRUGO, show_operstate, NULL),
324fd586bacSKay Sievers 	__ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
325fd586bacSKay Sievers 	__ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
326fd586bacSKay Sievers 	__ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
327fd586bacSKay Sievers 	       store_tx_queue_len),
328b6644cb7SXiaotian Feng 	__ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
329fd586bacSKay Sievers 	{}
3301da177e4SLinus Torvalds };
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */
33343cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d,
33443cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf,
3351da177e4SLinus Torvalds 			    unsigned long offset)
3361da177e4SLinus Torvalds {
33743cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
3381da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
3391da177e4SLinus Torvalds 
340be1f3c2cSBen Hutchings 	WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
341be1f3c2cSBen Hutchings 			offset % sizeof(u64) != 0);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
34496e74088SPavel Emelyanov 	if (dev_isalive(dev)) {
34528172739SEric Dumazet 		struct rtnl_link_stats64 temp;
34628172739SEric Dumazet 		const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
34728172739SEric Dumazet 
348be1f3c2cSBen Hutchings 		ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
34996e74088SPavel Emelyanov 	}
3501da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
3511da177e4SLinus Torvalds 	return ret;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /* generate a read-only statistics attribute */
3551da177e4SLinus Torvalds #define NETSTAT_ENTRY(name)						\
35643cb76d9SGreg Kroah-Hartman static ssize_t show_##name(struct device *d,				\
35743cb76d9SGreg Kroah-Hartman 			   struct device_attribute *attr, char *buf) 	\
3581da177e4SLinus Torvalds {									\
35943cb76d9SGreg Kroah-Hartman 	return netstat_show(d, attr, buf,				\
360be1f3c2cSBen Hutchings 			    offsetof(struct rtnl_link_stats64, name));	\
3611da177e4SLinus Torvalds }									\
36243cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets);
3651da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets);
3661da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes);
3671da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes);
3681da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors);
3691da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors);
3701da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped);
3711da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped);
3721da177e4SLinus Torvalds NETSTAT_ENTRY(multicast);
3731da177e4SLinus Torvalds NETSTAT_ENTRY(collisions);
3741da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors);
3751da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors);
3761da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors);
3771da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors);
3781da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors);
3791da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors);
3801da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors);
3811da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors);
3821da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors);
3831da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors);
3841da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors);
3851da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed);
3861da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = {
38943cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_packets.attr,
39043cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_packets.attr,
39143cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_bytes.attr,
39243cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_bytes.attr,
39343cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_errors.attr,
39443cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_errors.attr,
39543cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_dropped.attr,
39643cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_dropped.attr,
39743cb76d9SGreg Kroah-Hartman 	&dev_attr_multicast.attr,
39843cb76d9SGreg Kroah-Hartman 	&dev_attr_collisions.attr,
39943cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_length_errors.attr,
40043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_over_errors.attr,
40143cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_crc_errors.attr,
40243cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_frame_errors.attr,
40343cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_fifo_errors.attr,
40443cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_missed_errors.attr,
40543cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_aborted_errors.attr,
40643cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_carrier_errors.attr,
40743cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_fifo_errors.attr,
40843cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_heartbeat_errors.attr,
40943cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_window_errors.attr,
41043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_compressed.attr,
41143cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_compressed.attr,
4121da177e4SLinus Torvalds 	NULL
4131da177e4SLinus Torvalds };
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds static struct attribute_group netstat_group = {
4171da177e4SLinus Torvalds 	.name  = "statistics",
4181da177e4SLinus Torvalds 	.attrs  = netstat_attrs,
4191da177e4SLinus Torvalds };
4201da177e4SLinus Torvalds 
42122bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS
4221da177e4SLinus Torvalds /* helper function that does all the locking etc for wireless stats */
42343cb76d9SGreg Kroah-Hartman static ssize_t wireless_show(struct device *d, char *buf,
4241da177e4SLinus Torvalds 			     ssize_t (*format)(const struct iw_statistics *,
4251da177e4SLinus Torvalds 					       char *))
4261da177e4SLinus Torvalds {
42743cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
4288f1546caSJohannes Berg 	const struct iw_statistics *iw;
4291da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
4301da177e4SLinus Torvalds 
431b8afe641SEric W. Biederman 	if (!rtnl_trylock())
432b8afe641SEric W. Biederman 		return restart_syscall();
4336dd214b5SAndrey Borzenkov 	if (dev_isalive(dev)) {
4348f1546caSJohannes Berg 		iw = get_wireless_stats(dev);
4358f1546caSJohannes Berg 		if (iw)
4361da177e4SLinus Torvalds 			ret = (*format)(iw, buf);
4376dd214b5SAndrey Borzenkov 	}
438a160ee69SJohannes Berg 	rtnl_unlock();
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	return ret;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /* show function template for wireless fields */
4441da177e4SLinus Torvalds #define WIRELESS_SHOW(name, field, format_string)			\
4451da177e4SLinus Torvalds static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
4461da177e4SLinus Torvalds {									\
4471da177e4SLinus Torvalds 	return sprintf(buf, format_string, iw->field);			\
4481da177e4SLinus Torvalds }									\
44943cb76d9SGreg Kroah-Hartman static ssize_t show_iw_##name(struct device *d,				\
45043cb76d9SGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)	\
4511da177e4SLinus Torvalds {									\
45243cb76d9SGreg Kroah-Hartman 	return wireless_show(d, buf, format_iw_##name);			\
4531da177e4SLinus Torvalds }									\
45443cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds WIRELESS_SHOW(status, status, fmt_hex);
4571da177e4SLinus Torvalds WIRELESS_SHOW(link, qual.qual, fmt_dec);
4581da177e4SLinus Torvalds WIRELESS_SHOW(level, qual.level, fmt_dec);
4591da177e4SLinus Torvalds WIRELESS_SHOW(noise, qual.noise, fmt_dec);
4601da177e4SLinus Torvalds WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
4611da177e4SLinus Torvalds WIRELESS_SHOW(crypt, discard.code, fmt_dec);
4621da177e4SLinus Torvalds WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
4631da177e4SLinus Torvalds WIRELESS_SHOW(misc, discard.misc, fmt_dec);
4641da177e4SLinus Torvalds WIRELESS_SHOW(retries, discard.retries, fmt_dec);
4651da177e4SLinus Torvalds WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static struct attribute *wireless_attrs[] = {
46843cb76d9SGreg Kroah-Hartman 	&dev_attr_status.attr,
46943cb76d9SGreg Kroah-Hartman 	&dev_attr_link.attr,
47043cb76d9SGreg Kroah-Hartman 	&dev_attr_level.attr,
47143cb76d9SGreg Kroah-Hartman 	&dev_attr_noise.attr,
47243cb76d9SGreg Kroah-Hartman 	&dev_attr_nwid.attr,
47343cb76d9SGreg Kroah-Hartman 	&dev_attr_crypt.attr,
47443cb76d9SGreg Kroah-Hartman 	&dev_attr_fragment.attr,
47543cb76d9SGreg Kroah-Hartman 	&dev_attr_retries.attr,
47643cb76d9SGreg Kroah-Hartman 	&dev_attr_misc.attr,
47743cb76d9SGreg Kroah-Hartman 	&dev_attr_beacon.attr,
4781da177e4SLinus Torvalds 	NULL
4791da177e4SLinus Torvalds };
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds static struct attribute_group wireless_group = {
4821da177e4SLinus Torvalds 	.name = "wireless",
4831da177e4SLinus Torvalds 	.attrs = wireless_attrs,
4841da177e4SLinus Torvalds };
4851da177e4SLinus Torvalds #endif
486d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */
4871da177e4SLinus Torvalds 
48830bde1f5SStephen Rothwell #ifdef CONFIG_RPS
4890a9627f2STom Herbert /*
4900a9627f2STom Herbert  * RX queue sysfs structures and functions.
4910a9627f2STom Herbert  */
4920a9627f2STom Herbert struct rx_queue_attribute {
4930a9627f2STom Herbert 	struct attribute attr;
4940a9627f2STom Herbert 	ssize_t (*show)(struct netdev_rx_queue *queue,
4950a9627f2STom Herbert 	    struct rx_queue_attribute *attr, char *buf);
4960a9627f2STom Herbert 	ssize_t (*store)(struct netdev_rx_queue *queue,
4970a9627f2STom Herbert 	    struct rx_queue_attribute *attr, const char *buf, size_t len);
4980a9627f2STom Herbert };
4990a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr,		\
5000a9627f2STom Herbert     struct rx_queue_attribute, attr)
5010a9627f2STom Herbert 
5020a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
5030a9627f2STom Herbert 
5040a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
5050a9627f2STom Herbert 				  char *buf)
5060a9627f2STom Herbert {
5070a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
5080a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
5090a9627f2STom Herbert 
5100a9627f2STom Herbert 	if (!attribute->show)
5110a9627f2STom Herbert 		return -EIO;
5120a9627f2STom Herbert 
5130a9627f2STom Herbert 	return attribute->show(queue, attribute, buf);
5140a9627f2STom Herbert }
5150a9627f2STom Herbert 
5160a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
5170a9627f2STom Herbert 				   const char *buf, size_t count)
5180a9627f2STom Herbert {
5190a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
5200a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
5210a9627f2STom Herbert 
5220a9627f2STom Herbert 	if (!attribute->store)
5230a9627f2STom Herbert 		return -EIO;
5240a9627f2STom Herbert 
5250a9627f2STom Herbert 	return attribute->store(queue, attribute, buf, count);
5260a9627f2STom Herbert }
5270a9627f2STom Herbert 
528fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = {
5290a9627f2STom Herbert 	.show = rx_queue_attr_show,
5300a9627f2STom Herbert 	.store = rx_queue_attr_store,
5310a9627f2STom Herbert };
5320a9627f2STom Herbert 
5330a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue,
5340a9627f2STom Herbert 			    struct rx_queue_attribute *attribute, char *buf)
5350a9627f2STom Herbert {
5360a9627f2STom Herbert 	struct rps_map *map;
5370a9627f2STom Herbert 	cpumask_var_t mask;
5380a9627f2STom Herbert 	size_t len = 0;
5390a9627f2STom Herbert 	int i;
5400a9627f2STom Herbert 
5410a9627f2STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
5420a9627f2STom Herbert 		return -ENOMEM;
5430a9627f2STom Herbert 
5440a9627f2STom Herbert 	rcu_read_lock();
5450a9627f2STom Herbert 	map = rcu_dereference(queue->rps_map);
5460a9627f2STom Herbert 	if (map)
5470a9627f2STom Herbert 		for (i = 0; i < map->len; i++)
5480a9627f2STom Herbert 			cpumask_set_cpu(map->cpus[i], mask);
5490a9627f2STom Herbert 
5500a9627f2STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
5510a9627f2STom Herbert 	if (PAGE_SIZE - len < 3) {
5520a9627f2STom Herbert 		rcu_read_unlock();
5530a9627f2STom Herbert 		free_cpumask_var(mask);
5540a9627f2STom Herbert 		return -EINVAL;
5550a9627f2STom Herbert 	}
5560a9627f2STom Herbert 	rcu_read_unlock();
5570a9627f2STom Herbert 
5580a9627f2STom Herbert 	free_cpumask_var(mask);
5590a9627f2STom Herbert 	len += sprintf(buf + len, "\n");
5600a9627f2STom Herbert 	return len;
5610a9627f2STom Herbert }
5620a9627f2STom Herbert 
563f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue,
5640a9627f2STom Herbert 		      struct rx_queue_attribute *attribute,
5650a9627f2STom Herbert 		      const char *buf, size_t len)
5660a9627f2STom Herbert {
5670a9627f2STom Herbert 	struct rps_map *old_map, *map;
5680a9627f2STom Herbert 	cpumask_var_t mask;
5690a9627f2STom Herbert 	int err, cpu, i;
5700a9627f2STom Herbert 	static DEFINE_SPINLOCK(rps_map_lock);
5710a9627f2STom Herbert 
5720a9627f2STom Herbert 	if (!capable(CAP_NET_ADMIN))
5730a9627f2STom Herbert 		return -EPERM;
5740a9627f2STom Herbert 
5750a9627f2STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5760a9627f2STom Herbert 		return -ENOMEM;
5770a9627f2STom Herbert 
5780a9627f2STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
5790a9627f2STom Herbert 	if (err) {
5800a9627f2STom Herbert 		free_cpumask_var(mask);
5810a9627f2STom Herbert 		return err;
5820a9627f2STom Herbert 	}
5830a9627f2STom Herbert 
5840a9627f2STom Herbert 	map = kzalloc(max_t(unsigned,
5850a9627f2STom Herbert 	    RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
5860a9627f2STom Herbert 	    GFP_KERNEL);
5870a9627f2STom Herbert 	if (!map) {
5880a9627f2STom Herbert 		free_cpumask_var(mask);
5890a9627f2STom Herbert 		return -ENOMEM;
5900a9627f2STom Herbert 	}
5910a9627f2STom Herbert 
5920a9627f2STom Herbert 	i = 0;
5930a9627f2STom Herbert 	for_each_cpu_and(cpu, mask, cpu_online_mask)
5940a9627f2STom Herbert 		map->cpus[i++] = cpu;
5950a9627f2STom Herbert 
5960a9627f2STom Herbert 	if (i)
5970a9627f2STom Herbert 		map->len = i;
5980a9627f2STom Herbert 	else {
5990a9627f2STom Herbert 		kfree(map);
6000a9627f2STom Herbert 		map = NULL;
6010a9627f2STom Herbert 	}
6020a9627f2STom Herbert 
6030a9627f2STom Herbert 	spin_lock(&rps_map_lock);
6046e3f7fafSEric Dumazet 	old_map = rcu_dereference_protected(queue->rps_map,
6056e3f7fafSEric Dumazet 					    lockdep_is_held(&rps_map_lock));
6060a9627f2STom Herbert 	rcu_assign_pointer(queue->rps_map, map);
6070a9627f2STom Herbert 	spin_unlock(&rps_map_lock);
6080a9627f2STom Herbert 
609adc9300eSEric Dumazet 	if (map)
610adc9300eSEric Dumazet 		jump_label_inc(&rps_needed);
611adc9300eSEric Dumazet 	if (old_map) {
612f6f80238SLai Jiangshan 		kfree_rcu(old_map, rcu);
613adc9300eSEric Dumazet 		jump_label_dec(&rps_needed);
614adc9300eSEric Dumazet 	}
6150a9627f2STom Herbert 	free_cpumask_var(mask);
6160a9627f2STom Herbert 	return len;
6170a9627f2STom Herbert }
6180a9627f2STom Herbert 
619fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
620fec5e652STom Herbert 					   struct rx_queue_attribute *attr,
621fec5e652STom Herbert 					   char *buf)
622fec5e652STom Herbert {
623fec5e652STom Herbert 	struct rps_dev_flow_table *flow_table;
624fec5e652STom Herbert 	unsigned int val = 0;
625fec5e652STom Herbert 
626fec5e652STom Herbert 	rcu_read_lock();
627fec5e652STom Herbert 	flow_table = rcu_dereference(queue->rps_flow_table);
628fec5e652STom Herbert 	if (flow_table)
629fec5e652STom Herbert 		val = flow_table->mask + 1;
630fec5e652STom Herbert 	rcu_read_unlock();
631fec5e652STom Herbert 
632fec5e652STom Herbert 	return sprintf(buf, "%u\n", val);
633fec5e652STom Herbert }
634fec5e652STom Herbert 
635fec5e652STom Herbert static void rps_dev_flow_table_release_work(struct work_struct *work)
636fec5e652STom Herbert {
637fec5e652STom Herbert 	struct rps_dev_flow_table *table = container_of(work,
638fec5e652STom Herbert 	    struct rps_dev_flow_table, free_work);
639fec5e652STom Herbert 
640fec5e652STom Herbert 	vfree(table);
641fec5e652STom Herbert }
642fec5e652STom Herbert 
643fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu)
644fec5e652STom Herbert {
645fec5e652STom Herbert 	struct rps_dev_flow_table *table = container_of(rcu,
646fec5e652STom Herbert 	    struct rps_dev_flow_table, rcu);
647fec5e652STom Herbert 
648fec5e652STom Herbert 	INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
649fec5e652STom Herbert 	schedule_work(&table->free_work);
650fec5e652STom Herbert }
651fec5e652STom Herbert 
652f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
653fec5e652STom Herbert 				     struct rx_queue_attribute *attr,
654fec5e652STom Herbert 				     const char *buf, size_t len)
655fec5e652STom Herbert {
656fec5e652STom Herbert 	unsigned int count;
657fec5e652STom Herbert 	char *endp;
658fec5e652STom Herbert 	struct rps_dev_flow_table *table, *old_table;
659fec5e652STom Herbert 	static DEFINE_SPINLOCK(rps_dev_flow_lock);
660fec5e652STom Herbert 
661fec5e652STom Herbert 	if (!capable(CAP_NET_ADMIN))
662fec5e652STom Herbert 		return -EPERM;
663fec5e652STom Herbert 
664fec5e652STom Herbert 	count = simple_strtoul(buf, &endp, 0);
665fec5e652STom Herbert 	if (endp == buf)
666fec5e652STom Herbert 		return -EINVAL;
667fec5e652STom Herbert 
668fec5e652STom Herbert 	if (count) {
669fec5e652STom Herbert 		int i;
670fec5e652STom Herbert 
671fec5e652STom Herbert 		if (count > 1<<30) {
672fec5e652STom Herbert 			/* Enforce a limit to prevent overflow */
673fec5e652STom Herbert 			return -EINVAL;
674fec5e652STom Herbert 		}
675fec5e652STom Herbert 		count = roundup_pow_of_two(count);
676fec5e652STom Herbert 		table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
677fec5e652STom Herbert 		if (!table)
678fec5e652STom Herbert 			return -ENOMEM;
679fec5e652STom Herbert 
680fec5e652STom Herbert 		table->mask = count - 1;
681fec5e652STom Herbert 		for (i = 0; i < count; i++)
682fec5e652STom Herbert 			table->flows[i].cpu = RPS_NO_CPU;
683fec5e652STom Herbert 	} else
684fec5e652STom Herbert 		table = NULL;
685fec5e652STom Herbert 
686fec5e652STom Herbert 	spin_lock(&rps_dev_flow_lock);
6876e3f7fafSEric Dumazet 	old_table = rcu_dereference_protected(queue->rps_flow_table,
6886e3f7fafSEric Dumazet 					      lockdep_is_held(&rps_dev_flow_lock));
689fec5e652STom Herbert 	rcu_assign_pointer(queue->rps_flow_table, table);
690fec5e652STom Herbert 	spin_unlock(&rps_dev_flow_lock);
691fec5e652STom Herbert 
692fec5e652STom Herbert 	if (old_table)
693fec5e652STom Herbert 		call_rcu(&old_table->rcu, rps_dev_flow_table_release);
694fec5e652STom Herbert 
695fec5e652STom Herbert 	return len;
696fec5e652STom Herbert }
697fec5e652STom Herbert 
6980a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute =
6990a9627f2STom Herbert 	__ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
7000a9627f2STom Herbert 
701fec5e652STom Herbert 
702fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
703fec5e652STom Herbert 	__ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
704fec5e652STom Herbert 	    show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
705fec5e652STom Herbert 
7060a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = {
7070a9627f2STom Herbert 	&rps_cpus_attribute.attr,
708fec5e652STom Herbert 	&rps_dev_flow_table_cnt_attribute.attr,
7090a9627f2STom Herbert 	NULL
7100a9627f2STom Herbert };
7110a9627f2STom Herbert 
7120a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj)
7130a9627f2STom Herbert {
7140a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
7156e3f7fafSEric Dumazet 	struct rps_map *map;
7166e3f7fafSEric Dumazet 	struct rps_dev_flow_table *flow_table;
7170a9627f2STom Herbert 
718fec5e652STom Herbert 
71933d480ceSEric Dumazet 	map = rcu_dereference_protected(queue->rps_map, 1);
7209ea19481SJohn Fastabend 	if (map) {
7219ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_map, NULL);
722f6f80238SLai Jiangshan 		kfree_rcu(map, rcu);
7239ea19481SJohn Fastabend 	}
7246e3f7fafSEric Dumazet 
72533d480ceSEric Dumazet 	flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
7269ea19481SJohn Fastabend 	if (flow_table) {
7279ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_flow_table, NULL);
7286e3f7fafSEric Dumazet 		call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
7299ea19481SJohn Fastabend 	}
7300a9627f2STom Herbert 
7319ea19481SJohn Fastabend 	memset(kobj, 0, sizeof(*kobj));
732fe822240STom Herbert 	dev_put(queue->dev);
7330a9627f2STom Herbert }
7340a9627f2STom Herbert 
7350a9627f2STom Herbert static struct kobj_type rx_queue_ktype = {
7360a9627f2STom Herbert 	.sysfs_ops = &rx_queue_sysfs_ops,
7370a9627f2STom Herbert 	.release = rx_queue_release,
7380a9627f2STom Herbert 	.default_attrs = rx_queue_default_attrs,
7390a9627f2STom Herbert };
7400a9627f2STom Herbert 
7410a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index)
7420a9627f2STom Herbert {
7430a9627f2STom Herbert 	struct netdev_rx_queue *queue = net->_rx + index;
7440a9627f2STom Herbert 	struct kobject *kobj = &queue->kobj;
7450a9627f2STom Herbert 	int error = 0;
7460a9627f2STom Herbert 
7470a9627f2STom Herbert 	kobj->kset = net->queues_kset;
7480a9627f2STom Herbert 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
7490a9627f2STom Herbert 	    "rx-%u", index);
7500a9627f2STom Herbert 	if (error) {
7510a9627f2STom Herbert 		kobject_put(kobj);
7520a9627f2STom Herbert 		return error;
7530a9627f2STom Herbert 	}
7540a9627f2STom Herbert 
7550a9627f2STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
756fe822240STom Herbert 	dev_hold(queue->dev);
7570a9627f2STom Herbert 
7580a9627f2STom Herbert 	return error;
7590a9627f2STom Herbert }
760bf264145STom Herbert #endif /* CONFIG_RPS */
7610a9627f2STom Herbert 
76262fe0b40SBen Hutchings int
76362fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
7640a9627f2STom Herbert {
765bf264145STom Herbert #ifdef CONFIG_RPS
7660a9627f2STom Herbert 	int i;
7670a9627f2STom Herbert 	int error = 0;
7680a9627f2STom Herbert 
76962fe0b40SBen Hutchings 	for (i = old_num; i < new_num; i++) {
7700a9627f2STom Herbert 		error = rx_queue_add_kobject(net, i);
77162fe0b40SBen Hutchings 		if (error) {
77262fe0b40SBen Hutchings 			new_num = old_num;
7730a9627f2STom Herbert 			break;
7740a9627f2STom Herbert 		}
77562fe0b40SBen Hutchings 	}
7760a9627f2STom Herbert 
77762fe0b40SBen Hutchings 	while (--i >= new_num)
7780a9627f2STom Herbert 		kobject_put(&net->_rx[i].kobj);
7790a9627f2STom Herbert 
7800a9627f2STom Herbert 	return error;
781bf264145STom Herbert #else
782bf264145STom Herbert 	return 0;
783bf264145STom Herbert #endif
7840a9627f2STom Herbert }
7850a9627f2STom Herbert 
786ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
7871d24eb48STom Herbert /*
7881d24eb48STom Herbert  * netdev_queue sysfs structures and functions.
7891d24eb48STom Herbert  */
7901d24eb48STom Herbert struct netdev_queue_attribute {
7911d24eb48STom Herbert 	struct attribute attr;
7921d24eb48STom Herbert 	ssize_t (*show)(struct netdev_queue *queue,
7931d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, char *buf);
7941d24eb48STom Herbert 	ssize_t (*store)(struct netdev_queue *queue,
7951d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, const char *buf, size_t len);
7961d24eb48STom Herbert };
7971d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr,		\
7981d24eb48STom Herbert     struct netdev_queue_attribute, attr)
7991d24eb48STom Herbert 
8001d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
8011d24eb48STom Herbert 
8021d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj,
8031d24eb48STom Herbert 				      struct attribute *attr, char *buf)
80462fe0b40SBen Hutchings {
8051d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
8061d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
8071d24eb48STom Herbert 
8081d24eb48STom Herbert 	if (!attribute->show)
8091d24eb48STom Herbert 		return -EIO;
8101d24eb48STom Herbert 
8111d24eb48STom Herbert 	return attribute->show(queue, attribute, buf);
8121d24eb48STom Herbert }
8131d24eb48STom Herbert 
8141d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj,
8151d24eb48STom Herbert 				       struct attribute *attr,
8161d24eb48STom Herbert 				       const char *buf, size_t count)
8171d24eb48STom Herbert {
8181d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
8191d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
8201d24eb48STom Herbert 
8211d24eb48STom Herbert 	if (!attribute->store)
8221d24eb48STom Herbert 		return -EIO;
8231d24eb48STom Herbert 
8241d24eb48STom Herbert 	return attribute->store(queue, attribute, buf, count);
8251d24eb48STom Herbert }
8261d24eb48STom Herbert 
8271d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = {
8281d24eb48STom Herbert 	.show = netdev_queue_attr_show,
8291d24eb48STom Herbert 	.store = netdev_queue_attr_store,
8301d24eb48STom Herbert };
8311d24eb48STom Herbert 
832ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue,
833ccf5ff69Sdavid decotigny 				  struct netdev_queue_attribute *attribute,
834ccf5ff69Sdavid decotigny 				  char *buf)
835ccf5ff69Sdavid decotigny {
836ccf5ff69Sdavid decotigny 	unsigned long trans_timeout;
837ccf5ff69Sdavid decotigny 
838ccf5ff69Sdavid decotigny 	spin_lock_irq(&queue->_xmit_lock);
839ccf5ff69Sdavid decotigny 	trans_timeout = queue->trans_timeout;
840ccf5ff69Sdavid decotigny 	spin_unlock_irq(&queue->_xmit_lock);
841ccf5ff69Sdavid decotigny 
842ccf5ff69Sdavid decotigny 	return sprintf(buf, "%lu", trans_timeout);
843ccf5ff69Sdavid decotigny }
844ccf5ff69Sdavid decotigny 
845ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout =
846ccf5ff69Sdavid decotigny 	__ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
847ccf5ff69Sdavid decotigny 
848ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
8491d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
8501d24eb48STom Herbert {
8511d24eb48STom Herbert 	struct net_device *dev = queue->dev;
8521d24eb48STom Herbert 	int i;
8531d24eb48STom Herbert 
8541d24eb48STom Herbert 	for (i = 0; i < dev->num_tx_queues; i++)
8551d24eb48STom Herbert 		if (queue == &dev->_tx[i])
8561d24eb48STom Herbert 			break;
8571d24eb48STom Herbert 
8581d24eb48STom Herbert 	BUG_ON(i >= dev->num_tx_queues);
8591d24eb48STom Herbert 
8601d24eb48STom Herbert 	return i;
8611d24eb48STom Herbert }
8621d24eb48STom Herbert 
8631d24eb48STom Herbert 
8641d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue,
8651d24eb48STom Herbert 			    struct netdev_queue_attribute *attribute, char *buf)
8661d24eb48STom Herbert {
8671d24eb48STom Herbert 	struct net_device *dev = queue->dev;
8681d24eb48STom Herbert 	struct xps_dev_maps *dev_maps;
8691d24eb48STom Herbert 	cpumask_var_t mask;
8701d24eb48STom Herbert 	unsigned long index;
8711d24eb48STom Herbert 	size_t len = 0;
8721d24eb48STom Herbert 	int i;
8731d24eb48STom Herbert 
8741d24eb48STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
8751d24eb48STom Herbert 		return -ENOMEM;
8761d24eb48STom Herbert 
8771d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
8781d24eb48STom Herbert 
8791d24eb48STom Herbert 	rcu_read_lock();
8801d24eb48STom Herbert 	dev_maps = rcu_dereference(dev->xps_maps);
8811d24eb48STom Herbert 	if (dev_maps) {
8821d24eb48STom Herbert 		for_each_possible_cpu(i) {
8831d24eb48STom Herbert 			struct xps_map *map =
8841d24eb48STom Herbert 			    rcu_dereference(dev_maps->cpu_map[i]);
8851d24eb48STom Herbert 			if (map) {
8861d24eb48STom Herbert 				int j;
8871d24eb48STom Herbert 				for (j = 0; j < map->len; j++) {
8881d24eb48STom Herbert 					if (map->queues[j] == index) {
8891d24eb48STom Herbert 						cpumask_set_cpu(i, mask);
8901d24eb48STom Herbert 						break;
8911d24eb48STom Herbert 					}
8921d24eb48STom Herbert 				}
8931d24eb48STom Herbert 			}
8941d24eb48STom Herbert 		}
8951d24eb48STom Herbert 	}
8961d24eb48STom Herbert 	rcu_read_unlock();
8971d24eb48STom Herbert 
8981d24eb48STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
8991d24eb48STom Herbert 	if (PAGE_SIZE - len < 3) {
9001d24eb48STom Herbert 		free_cpumask_var(mask);
9011d24eb48STom Herbert 		return -EINVAL;
9021d24eb48STom Herbert 	}
9031d24eb48STom Herbert 
9041d24eb48STom Herbert 	free_cpumask_var(mask);
9051d24eb48STom Herbert 	len += sprintf(buf + len, "\n");
9061d24eb48STom Herbert 	return len;
9071d24eb48STom Herbert }
9081d24eb48STom Herbert 
9091d24eb48STom Herbert static DEFINE_MUTEX(xps_map_mutex);
910a4177869SEric Dumazet #define xmap_dereference(P)		\
911a4177869SEric Dumazet 	rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
9121d24eb48STom Herbert 
9131d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue,
9141d24eb48STom Herbert 		      struct netdev_queue_attribute *attribute,
9151d24eb48STom Herbert 		      const char *buf, size_t len)
9161d24eb48STom Herbert {
9171d24eb48STom Herbert 	struct net_device *dev = queue->dev;
9181d24eb48STom Herbert 	cpumask_var_t mask;
9191d24eb48STom Herbert 	int err, i, cpu, pos, map_len, alloc_len, need_set;
9201d24eb48STom Herbert 	unsigned long index;
9211d24eb48STom Herbert 	struct xps_map *map, *new_map;
9221d24eb48STom Herbert 	struct xps_dev_maps *dev_maps, *new_dev_maps;
9231d24eb48STom Herbert 	int nonempty = 0;
92419b05f81Sdavid decotigny 	int numa_node_id = -2;
9251d24eb48STom Herbert 
9261d24eb48STom Herbert 	if (!capable(CAP_NET_ADMIN))
9271d24eb48STom Herbert 		return -EPERM;
9281d24eb48STom Herbert 
9291d24eb48STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
9301d24eb48STom Herbert 		return -ENOMEM;
9311d24eb48STom Herbert 
9321d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
9331d24eb48STom Herbert 
9341d24eb48STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
9351d24eb48STom Herbert 	if (err) {
9361d24eb48STom Herbert 		free_cpumask_var(mask);
9371d24eb48STom Herbert 		return err;
9381d24eb48STom Herbert 	}
9391d24eb48STom Herbert 
9401d24eb48STom Herbert 	new_dev_maps = kzalloc(max_t(unsigned,
9411d24eb48STom Herbert 	    XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
9421d24eb48STom Herbert 	if (!new_dev_maps) {
9431d24eb48STom Herbert 		free_cpumask_var(mask);
9441d24eb48STom Herbert 		return -ENOMEM;
9451d24eb48STom Herbert 	}
9461d24eb48STom Herbert 
9471d24eb48STom Herbert 	mutex_lock(&xps_map_mutex);
9481d24eb48STom Herbert 
949a4177869SEric Dumazet 	dev_maps = xmap_dereference(dev->xps_maps);
9501d24eb48STom Herbert 
9511d24eb48STom Herbert 	for_each_possible_cpu(cpu) {
952a4177869SEric Dumazet 		map = dev_maps ?
953a4177869SEric Dumazet 			xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
954a4177869SEric Dumazet 		new_map = map;
9551d24eb48STom Herbert 		if (map) {
9561d24eb48STom Herbert 			for (pos = 0; pos < map->len; pos++)
9571d24eb48STom Herbert 				if (map->queues[pos] == index)
9581d24eb48STom Herbert 					break;
9591d24eb48STom Herbert 			map_len = map->len;
9601d24eb48STom Herbert 			alloc_len = map->alloc_len;
9611d24eb48STom Herbert 		} else
9621d24eb48STom Herbert 			pos = map_len = alloc_len = 0;
9631d24eb48STom Herbert 
9642142c131SKOSAKI Motohiro 		need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu);
965f2cd2d3eSEric Dumazet #ifdef CONFIG_NUMA
966f2cd2d3eSEric Dumazet 		if (need_set) {
96719b05f81Sdavid decotigny 			if (numa_node_id == -2)
96819b05f81Sdavid decotigny 				numa_node_id = cpu_to_node(cpu);
96919b05f81Sdavid decotigny 			else if (numa_node_id != cpu_to_node(cpu))
97019b05f81Sdavid decotigny 				numa_node_id = -1;
971f2cd2d3eSEric Dumazet 		}
972f2cd2d3eSEric Dumazet #endif
9731d24eb48STom Herbert 		if (need_set && pos >= map_len) {
9741d24eb48STom Herbert 			/* Need to add queue to this CPU's map */
9751d24eb48STom Herbert 			if (map_len >= alloc_len) {
9761d24eb48STom Herbert 				alloc_len = alloc_len ?
9771d24eb48STom Herbert 				    2 * alloc_len : XPS_MIN_MAP_ALLOC;
978b02038a1SEric Dumazet 				new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
979b02038a1SEric Dumazet 						       GFP_KERNEL,
980b02038a1SEric Dumazet 						       cpu_to_node(cpu));
9811d24eb48STom Herbert 				if (!new_map)
9821d24eb48STom Herbert 					goto error;
9831d24eb48STom Herbert 				new_map->alloc_len = alloc_len;
9841d24eb48STom Herbert 				for (i = 0; i < map_len; i++)
9851d24eb48STom Herbert 					new_map->queues[i] = map->queues[i];
9861d24eb48STom Herbert 				new_map->len = map_len;
9871d24eb48STom Herbert 			}
9881d24eb48STom Herbert 			new_map->queues[new_map->len++] = index;
9891d24eb48STom Herbert 		} else if (!need_set && pos < map_len) {
9901d24eb48STom Herbert 			/* Need to remove queue from this CPU's map */
9911d24eb48STom Herbert 			if (map_len > 1)
9921d24eb48STom Herbert 				new_map->queues[pos] =
9931d24eb48STom Herbert 				    new_map->queues[--new_map->len];
9941d24eb48STom Herbert 			else
9951d24eb48STom Herbert 				new_map = NULL;
9961d24eb48STom Herbert 		}
997a4177869SEric Dumazet 		RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
9981d24eb48STom Herbert 	}
9991d24eb48STom Herbert 
10001d24eb48STom Herbert 	/* Cleanup old maps */
10011d24eb48STom Herbert 	for_each_possible_cpu(cpu) {
1002a4177869SEric Dumazet 		map = dev_maps ?
1003a4177869SEric Dumazet 			xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
1004a4177869SEric Dumazet 		if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
1005edc86d8aSLai Jiangshan 			kfree_rcu(map, rcu);
10061d24eb48STom Herbert 		if (new_dev_maps->cpu_map[cpu])
10071d24eb48STom Herbert 			nonempty = 1;
10081d24eb48STom Herbert 	}
10091d24eb48STom Herbert 
10101d24eb48STom Herbert 	if (nonempty)
1011a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->xps_maps, new_dev_maps);
10121d24eb48STom Herbert 	else {
10131d24eb48STom Herbert 		kfree(new_dev_maps);
1014a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->xps_maps, NULL);
10151d24eb48STom Herbert 	}
10161d24eb48STom Herbert 
10171d24eb48STom Herbert 	if (dev_maps)
1018b55071ebSLai Jiangshan 		kfree_rcu(dev_maps, rcu);
10191d24eb48STom Herbert 
102019b05f81Sdavid decotigny 	netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id :
1021b236da69SChangli Gao 					    NUMA_NO_NODE);
1022f2cd2d3eSEric Dumazet 
10231d24eb48STom Herbert 	mutex_unlock(&xps_map_mutex);
10241d24eb48STom Herbert 
10251d24eb48STom Herbert 	free_cpumask_var(mask);
10261d24eb48STom Herbert 	return len;
10271d24eb48STom Herbert 
10281d24eb48STom Herbert error:
10291d24eb48STom Herbert 	mutex_unlock(&xps_map_mutex);
10301d24eb48STom Herbert 
10311d24eb48STom Herbert 	if (new_dev_maps)
10321d24eb48STom Herbert 		for_each_possible_cpu(i)
1033a4177869SEric Dumazet 			kfree(rcu_dereference_protected(
1034a4177869SEric Dumazet 				new_dev_maps->cpu_map[i],
1035a4177869SEric Dumazet 				1));
10361d24eb48STom Herbert 	kfree(new_dev_maps);
10371d24eb48STom Herbert 	free_cpumask_var(mask);
10381d24eb48STom Herbert 	return -ENOMEM;
10391d24eb48STom Herbert }
10401d24eb48STom Herbert 
10411d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute =
10421d24eb48STom Herbert     __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1043ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */
10441d24eb48STom Herbert 
10451d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = {
1046ccf5ff69Sdavid decotigny 	&queue_trans_timeout.attr,
1047ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
10481d24eb48STom Herbert 	&xps_cpus_attribute.attr,
1049ccf5ff69Sdavid decotigny #endif
10501d24eb48STom Herbert 	NULL
10511d24eb48STom Herbert };
10521d24eb48STom Herbert 
1053ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
10541d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj)
10551d24eb48STom Herbert {
10561d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
10571d24eb48STom Herbert 	struct net_device *dev = queue->dev;
10581d24eb48STom Herbert 	struct xps_dev_maps *dev_maps;
10591d24eb48STom Herbert 	struct xps_map *map;
10601d24eb48STom Herbert 	unsigned long index;
10611d24eb48STom Herbert 	int i, pos, nonempty = 0;
10621d24eb48STom Herbert 
10631d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
10641d24eb48STom Herbert 
10651d24eb48STom Herbert 	mutex_lock(&xps_map_mutex);
1066a4177869SEric Dumazet 	dev_maps = xmap_dereference(dev->xps_maps);
10671d24eb48STom Herbert 
10681d24eb48STom Herbert 	if (dev_maps) {
10691d24eb48STom Herbert 		for_each_possible_cpu(i) {
1070a4177869SEric Dumazet 			map = xmap_dereference(dev_maps->cpu_map[i]);
10711d24eb48STom Herbert 			if (!map)
10721d24eb48STom Herbert 				continue;
10731d24eb48STom Herbert 
10741d24eb48STom Herbert 			for (pos = 0; pos < map->len; pos++)
10751d24eb48STom Herbert 				if (map->queues[pos] == index)
10761d24eb48STom Herbert 					break;
10771d24eb48STom Herbert 
10781d24eb48STom Herbert 			if (pos < map->len) {
10791d24eb48STom Herbert 				if (map->len > 1)
10801d24eb48STom Herbert 					map->queues[pos] =
10811d24eb48STom Herbert 					    map->queues[--map->len];
10821d24eb48STom Herbert 				else {
10831d24eb48STom Herbert 					RCU_INIT_POINTER(dev_maps->cpu_map[i],
10841d24eb48STom Herbert 					    NULL);
1085edc86d8aSLai Jiangshan 					kfree_rcu(map, rcu);
10861d24eb48STom Herbert 					map = NULL;
10871d24eb48STom Herbert 				}
10881d24eb48STom Herbert 			}
10891d24eb48STom Herbert 			if (map)
10901d24eb48STom Herbert 				nonempty = 1;
10911d24eb48STom Herbert 		}
10921d24eb48STom Herbert 
10931d24eb48STom Herbert 		if (!nonempty) {
10941d24eb48STom Herbert 			RCU_INIT_POINTER(dev->xps_maps, NULL);
1095b55071ebSLai Jiangshan 			kfree_rcu(dev_maps, rcu);
10961d24eb48STom Herbert 		}
10971d24eb48STom Herbert 	}
10981d24eb48STom Herbert 
10991d24eb48STom Herbert 	mutex_unlock(&xps_map_mutex);
11001d24eb48STom Herbert 
11011d24eb48STom Herbert 	memset(kobj, 0, sizeof(*kobj));
11021d24eb48STom Herbert 	dev_put(queue->dev);
11031d24eb48STom Herbert }
1104ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */
11051d24eb48STom Herbert 
11061d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = {
11071d24eb48STom Herbert 	.sysfs_ops = &netdev_queue_sysfs_ops,
1108ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
11091d24eb48STom Herbert 	.release = netdev_queue_release,
1110ccf5ff69Sdavid decotigny #endif
11111d24eb48STom Herbert 	.default_attrs = netdev_queue_default_attrs,
11121d24eb48STom Herbert };
11131d24eb48STom Herbert 
11141d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index)
11151d24eb48STom Herbert {
11161d24eb48STom Herbert 	struct netdev_queue *queue = net->_tx + index;
11171d24eb48STom Herbert 	struct kobject *kobj = &queue->kobj;
11181d24eb48STom Herbert 	int error = 0;
11191d24eb48STom Herbert 
11201d24eb48STom Herbert 	kobj->kset = net->queues_kset;
11211d24eb48STom Herbert 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
11221d24eb48STom Herbert 	    "tx-%u", index);
11231d24eb48STom Herbert 	if (error) {
11241d24eb48STom Herbert 		kobject_put(kobj);
11251d24eb48STom Herbert 		return error;
11261d24eb48STom Herbert 	}
11271d24eb48STom Herbert 
11281d24eb48STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
11291d24eb48STom Herbert 	dev_hold(queue->dev);
11301d24eb48STom Herbert 
11311d24eb48STom Herbert 	return error;
11321d24eb48STom Herbert }
1133ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
11341d24eb48STom Herbert 
11351d24eb48STom Herbert int
11361d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
11371d24eb48STom Herbert {
1138ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
11391d24eb48STom Herbert 	int i;
11401d24eb48STom Herbert 	int error = 0;
11411d24eb48STom Herbert 
11421d24eb48STom Herbert 	for (i = old_num; i < new_num; i++) {
11431d24eb48STom Herbert 		error = netdev_queue_add_kobject(net, i);
11441d24eb48STom Herbert 		if (error) {
11451d24eb48STom Herbert 			new_num = old_num;
11461d24eb48STom Herbert 			break;
11471d24eb48STom Herbert 		}
11481d24eb48STom Herbert 	}
11491d24eb48STom Herbert 
11501d24eb48STom Herbert 	while (--i >= new_num)
11511d24eb48STom Herbert 		kobject_put(&net->_tx[i].kobj);
11521d24eb48STom Herbert 
11531d24eb48STom Herbert 	return error;
1154bf264145STom Herbert #else
1155bf264145STom Herbert 	return 0;
1156ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
11571d24eb48STom Herbert }
11581d24eb48STom Herbert 
11591d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net)
11601d24eb48STom Herbert {
1161bf264145STom Herbert 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
11621d24eb48STom Herbert 
1163ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
116462fe0b40SBen Hutchings 	net->queues_kset = kset_create_and_add("queues",
116562fe0b40SBen Hutchings 	    NULL, &net->dev.kobj);
116662fe0b40SBen Hutchings 	if (!net->queues_kset)
116762fe0b40SBen Hutchings 		return -ENOMEM;
1168bf264145STom Herbert #endif
11691d24eb48STom Herbert 
1170bf264145STom Herbert #ifdef CONFIG_RPS
1171bf264145STom Herbert 	real_rx = net->real_num_rx_queues;
1172bf264145STom Herbert #endif
1173bf264145STom Herbert 	real_tx = net->real_num_tx_queues;
1174bf264145STom Herbert 
1175bf264145STom Herbert 	error = net_rx_queue_update_kobjects(net, 0, real_rx);
11761d24eb48STom Herbert 	if (error)
11771d24eb48STom Herbert 		goto error;
1178bf264145STom Herbert 	rxq = real_rx;
11791d24eb48STom Herbert 
1180bf264145STom Herbert 	error = netdev_queue_update_kobjects(net, 0, real_tx);
11811d24eb48STom Herbert 	if (error)
11821d24eb48STom Herbert 		goto error;
1183bf264145STom Herbert 	txq = real_tx;
11841d24eb48STom Herbert 
11851d24eb48STom Herbert 	return 0;
11861d24eb48STom Herbert 
11871d24eb48STom Herbert error:
11881d24eb48STom Herbert 	netdev_queue_update_kobjects(net, txq, 0);
11891d24eb48STom Herbert 	net_rx_queue_update_kobjects(net, rxq, 0);
11901d24eb48STom Herbert 	return error;
119162fe0b40SBen Hutchings }
119262fe0b40SBen Hutchings 
11931d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net)
11940a9627f2STom Herbert {
1195bf264145STom Herbert 	int real_rx = 0, real_tx = 0;
1196bf264145STom Herbert 
1197bf264145STom Herbert #ifdef CONFIG_RPS
1198bf264145STom Herbert 	real_rx = net->real_num_rx_queues;
1199bf264145STom Herbert #endif
1200bf264145STom Herbert 	real_tx = net->real_num_tx_queues;
1201bf264145STom Herbert 
1202bf264145STom Herbert 	net_rx_queue_update_kobjects(net, real_rx, 0);
1203bf264145STom Herbert 	netdev_queue_update_kobjects(net, real_tx, 0);
1204ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
12050a9627f2STom Herbert 	kset_unregister(net->queues_kset);
1206bf264145STom Herbert #endif
12070a9627f2STom Herbert }
1208608b4b95SEric W. Biederman 
1209a685e089SAl Viro static void *net_grab_current_ns(void)
1210608b4b95SEric W. Biederman {
1211a685e089SAl Viro 	struct net *ns = current->nsproxy->net_ns;
1212a685e089SAl Viro #ifdef CONFIG_NET_NS
1213a685e089SAl Viro 	if (ns)
1214a685e089SAl Viro 		atomic_inc(&ns->passive);
1215a685e089SAl Viro #endif
1216a685e089SAl Viro 	return ns;
1217608b4b95SEric W. Biederman }
1218608b4b95SEric W. Biederman 
1219608b4b95SEric W. Biederman static const void *net_initial_ns(void)
1220608b4b95SEric W. Biederman {
1221608b4b95SEric W. Biederman 	return &init_net;
1222608b4b95SEric W. Biederman }
1223608b4b95SEric W. Biederman 
1224608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk)
1225608b4b95SEric W. Biederman {
1226608b4b95SEric W. Biederman 	return sock_net(sk);
1227608b4b95SEric W. Biederman }
1228608b4b95SEric W. Biederman 
122904600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = {
1230608b4b95SEric W. Biederman 	.type = KOBJ_NS_TYPE_NET,
1231a685e089SAl Viro 	.grab_current_ns = net_grab_current_ns,
1232608b4b95SEric W. Biederman 	.netlink_ns = net_netlink_ns,
1233608b4b95SEric W. Biederman 	.initial_ns = net_initial_ns,
1234a685e089SAl Viro 	.drop_ns = net_drop_ns,
1235608b4b95SEric W. Biederman };
123604600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations);
1237608b4b95SEric W. Biederman 
12381da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG
12397eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
12401da177e4SLinus Torvalds {
124143cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
12427eff2e7aSKay Sievers 	int retval;
12431da177e4SLinus Torvalds 
1244312c004dSKay Sievers 	/* pass interface to uevent. */
12457eff2e7aSKay Sievers 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1246bf62456eSEric Rannaud 	if (retval)
1247bf62456eSEric Rannaud 		goto exit;
12481da177e4SLinus Torvalds 
1249ca2f37dbSJean Tourrilhes 	/* pass ifindex to uevent.
1250ca2f37dbSJean Tourrilhes 	 * ifindex is useful as it won't change (interface name may change)
1251ca2f37dbSJean Tourrilhes 	 * and is what RtNetlink uses natively. */
12527eff2e7aSKay Sievers 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1253ca2f37dbSJean Tourrilhes 
1254bf62456eSEric Rannaud exit:
1255bf62456eSEric Rannaud 	return retval;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds #endif
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds /*
12601da177e4SLinus Torvalds  *	netdev_release -- destroy and free a dead device.
126143cb76d9SGreg Kroah-Hartman  *	Called when last reference to device kobject is gone.
12621da177e4SLinus Torvalds  */
126343cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d)
12641da177e4SLinus Torvalds {
126543cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	BUG_ON(dev->reg_state != NETREG_RELEASED);
12681da177e4SLinus Torvalds 
12690b815a1aSStephen Hemminger 	kfree(dev->ifalias);
12701da177e4SLinus Torvalds 	kfree((char *)dev - dev->padded);
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
1273608b4b95SEric W. Biederman static const void *net_namespace(struct device *d)
1274608b4b95SEric W. Biederman {
1275608b4b95SEric W. Biederman 	struct net_device *dev;
1276608b4b95SEric W. Biederman 	dev = container_of(d, struct net_device, dev);
1277608b4b95SEric W. Biederman 	return dev_net(dev);
1278608b4b95SEric W. Biederman }
1279608b4b95SEric W. Biederman 
12801da177e4SLinus Torvalds static struct class net_class = {
12811da177e4SLinus Torvalds 	.name = "net",
128243cb76d9SGreg Kroah-Hartman 	.dev_release = netdev_release,
12838b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
128443cb76d9SGreg Kroah-Hartman 	.dev_attrs = net_class_attributes,
12858b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
12861da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG
128743cb76d9SGreg Kroah-Hartman 	.dev_uevent = netdev_uevent,
12881da177e4SLinus Torvalds #endif
1289608b4b95SEric W. Biederman 	.ns_type = &net_ns_type_operations,
1290608b4b95SEric W. Biederman 	.namespace = net_namespace,
12911da177e4SLinus Torvalds };
12921da177e4SLinus Torvalds 
12939093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all
12949093bbb2SStephen Hemminger  * netdev references are gone.
12959093bbb2SStephen Hemminger  */
12968b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net)
12971da177e4SLinus Torvalds {
12989093bbb2SStephen Hemminger 	struct device *dev = &(net->dev);
12999093bbb2SStephen Hemminger 
13009093bbb2SStephen Hemminger 	kobject_get(&dev->kobj);
13013891845eSEric W. Biederman 
13021d24eb48STom Herbert 	remove_queue_kobjects(net);
13030a9627f2STom Herbert 
13049093bbb2SStephen Hemminger 	device_del(dev);
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /* Create sysfs entries for network device. */
13088b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net)
13091da177e4SLinus Torvalds {
131043cb76d9SGreg Kroah-Hartman 	struct device *dev = &(net->dev);
1311a4dbd674SDavid Brownell 	const struct attribute_group **groups = net->sysfs_groups;
13120a9627f2STom Herbert 	int error = 0;
13131da177e4SLinus Torvalds 
1314a1b3f594SEric W. Biederman 	device_initialize(dev);
131543cb76d9SGreg Kroah-Hartman 	dev->class = &net_class;
131643cb76d9SGreg Kroah-Hartman 	dev->platform_data = net;
131743cb76d9SGreg Kroah-Hartman 	dev->groups = groups;
13181da177e4SLinus Torvalds 
1319a2205472SStephen Hemminger 	dev_set_name(dev, "%s", net->name);
13201da177e4SLinus Torvalds 
13218b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
13220c509a6cSEric W. Biederman 	/* Allow for a device specific group */
13230c509a6cSEric W. Biederman 	if (*groups)
13240c509a6cSEric W. Biederman 		groups++;
13251da177e4SLinus Torvalds 
13260c509a6cSEric W. Biederman 	*groups++ = &netstat_group;
132722bb1be4SJohannes Berg #ifdef CONFIG_WIRELESS_EXT_SYSFS
13283d23e349SJohannes Berg 	if (net->ieee80211_ptr)
1329fe9925b5SStephen Hemminger 		*groups++ = &wireless_group;
13303d23e349SJohannes Berg #ifdef CONFIG_WIRELESS_EXT
13313d23e349SJohannes Berg 	else if (net->wireless_handlers)
13323d23e349SJohannes Berg 		*groups++ = &wireless_group;
13333d23e349SJohannes Berg #endif
13341da177e4SLinus Torvalds #endif
13358b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
13361da177e4SLinus Torvalds 
13370a9627f2STom Herbert 	error = device_add(dev);
13380a9627f2STom Herbert 	if (error)
13390a9627f2STom Herbert 		return error;
13400a9627f2STom Herbert 
13411d24eb48STom Herbert 	error = register_queue_kobjects(net);
13420a9627f2STom Herbert 	if (error) {
13430a9627f2STom Herbert 		device_del(dev);
13440a9627f2STom Herbert 		return error;
13450a9627f2STom Herbert 	}
13460a9627f2STom Herbert 
13470a9627f2STom Herbert 	return error;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
1350b8a9787eSJay Vosburgh int netdev_class_create_file(struct class_attribute *class_attr)
1351b8a9787eSJay Vosburgh {
1352b8a9787eSJay Vosburgh 	return class_create_file(&net_class, class_attr);
1353b8a9787eSJay Vosburgh }
13549e34a5b5SEric Dumazet EXPORT_SYMBOL(netdev_class_create_file);
1355b8a9787eSJay Vosburgh 
1356b8a9787eSJay Vosburgh void netdev_class_remove_file(struct class_attribute *class_attr)
1357b8a9787eSJay Vosburgh {
1358b8a9787eSJay Vosburgh 	class_remove_file(&net_class, class_attr);
1359b8a9787eSJay Vosburgh }
1360b8a9787eSJay Vosburgh EXPORT_SYMBOL(netdev_class_remove_file);
1361b8a9787eSJay Vosburgh 
13628b41d188SEric W. Biederman int netdev_kobject_init(void)
13631da177e4SLinus Torvalds {
1364608b4b95SEric W. Biederman 	kobj_ns_type_register(&net_ns_type_operations);
13651da177e4SLinus Torvalds 	return class_register(&net_class);
13661da177e4SLinus Torvalds }
1367