xref: /openbmc/linux/net/core/net-sysfs.c (revision 243198d0)
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>
21fec5e652STom Herbert #include <linux/vmalloc.h>
22bc3b2d7fSPaul Gortmaker #include <linux/export.h>
23114cf580STom Herbert #include <linux/jiffies.h>
249802c8e2SMing Lei #include <linux/pm_runtime.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 {
755e1fccc0SEric W. Biederman 	struct net_device *netdev = to_net_dev(dev);
765e1fccc0SEric W. Biederman 	struct net *net = dev_net(netdev);
771da177e4SLinus Torvalds 	unsigned long new;
781da177e4SLinus Torvalds 	int ret = -EINVAL;
791da177e4SLinus Torvalds 
805e1fccc0SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
811da177e4SLinus Torvalds 		return -EPERM;
821da177e4SLinus Torvalds 
83e1e420c7SShuah Khan 	ret = kstrtoul(buf, 0, &new);
84e1e420c7SShuah Khan 	if (ret)
851da177e4SLinus Torvalds 		goto err;
861da177e4SLinus Torvalds 
875a5990d3SStephen Hemminger 	if (!rtnl_trylock())
88336ca57cSEric W. Biederman 		return restart_syscall();
895a5990d3SStephen Hemminger 
905e1fccc0SEric W. Biederman 	if (dev_isalive(netdev)) {
915e1fccc0SEric W. Biederman 		if ((ret = (*set)(netdev, 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 
130fdae0fdeSJiri Pirko static int change_carrier(struct net_device *net, unsigned long new_carrier)
131fdae0fdeSJiri Pirko {
132fdae0fdeSJiri Pirko 	if (!netif_running(net))
133fdae0fdeSJiri Pirko 		return -EINVAL;
134fdae0fdeSJiri Pirko 	return dev_change_carrier(net, (bool) new_carrier);
135fdae0fdeSJiri Pirko }
136fdae0fdeSJiri Pirko 
137fdae0fdeSJiri Pirko static ssize_t store_carrier(struct device *dev, struct device_attribute *attr,
138fdae0fdeSJiri Pirko 			 const char *buf, size_t len)
139fdae0fdeSJiri Pirko {
140fdae0fdeSJiri Pirko 	return netdev_store(dev, attr, buf, len, change_carrier);
141fdae0fdeSJiri Pirko }
142fdae0fdeSJiri Pirko 
14343cb76d9SGreg Kroah-Hartman static ssize_t show_carrier(struct device *dev,
14443cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct net_device *netdev = to_net_dev(dev);
1471da177e4SLinus Torvalds 	if (netif_running(netdev)) {
1481da177e4SLinus Torvalds 		return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 	return -EINVAL;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153d519e17eSAndy Gospodarek static ssize_t show_speed(struct device *dev,
154d519e17eSAndy Gospodarek 			  struct device_attribute *attr, char *buf)
155d519e17eSAndy Gospodarek {
156d519e17eSAndy Gospodarek 	struct net_device *netdev = to_net_dev(dev);
157d519e17eSAndy Gospodarek 	int ret = -EINVAL;
158d519e17eSAndy Gospodarek 
159d519e17eSAndy Gospodarek 	if (!rtnl_trylock())
160d519e17eSAndy Gospodarek 		return restart_syscall();
161d519e17eSAndy Gospodarek 
1628ae6dacaSDavid Decotigny 	if (netif_running(netdev)) {
1638ae6dacaSDavid Decotigny 		struct ethtool_cmd cmd;
1644bc71cb9SJiri Pirko 		if (!__ethtool_get_settings(netdev, &cmd))
1658ae6dacaSDavid Decotigny 			ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
166d519e17eSAndy Gospodarek 	}
167d519e17eSAndy Gospodarek 	rtnl_unlock();
168d519e17eSAndy Gospodarek 	return ret;
169d519e17eSAndy Gospodarek }
170d519e17eSAndy Gospodarek 
171d519e17eSAndy Gospodarek static ssize_t show_duplex(struct device *dev,
172d519e17eSAndy Gospodarek 			   struct device_attribute *attr, char *buf)
173d519e17eSAndy Gospodarek {
174d519e17eSAndy Gospodarek 	struct net_device *netdev = to_net_dev(dev);
175d519e17eSAndy Gospodarek 	int ret = -EINVAL;
176d519e17eSAndy Gospodarek 
177d519e17eSAndy Gospodarek 	if (!rtnl_trylock())
178d519e17eSAndy Gospodarek 		return restart_syscall();
179d519e17eSAndy Gospodarek 
1808ae6dacaSDavid Decotigny 	if (netif_running(netdev)) {
1818ae6dacaSDavid Decotigny 		struct ethtool_cmd cmd;
182c6c13965SNikolay Aleksandrov 		if (!__ethtool_get_settings(netdev, &cmd)) {
183c6c13965SNikolay Aleksandrov 			const char *duplex;
184c6c13965SNikolay Aleksandrov 			switch (cmd.duplex) {
185c6c13965SNikolay Aleksandrov 			case DUPLEX_HALF:
186c6c13965SNikolay Aleksandrov 				duplex = "half";
187c6c13965SNikolay Aleksandrov 				break;
188c6c13965SNikolay Aleksandrov 			case DUPLEX_FULL:
189c6c13965SNikolay Aleksandrov 				duplex = "full";
190c6c13965SNikolay Aleksandrov 				break;
191c6c13965SNikolay Aleksandrov 			default:
192c6c13965SNikolay Aleksandrov 				duplex = "unknown";
193c6c13965SNikolay Aleksandrov 				break;
194c6c13965SNikolay Aleksandrov 			}
195c6c13965SNikolay Aleksandrov 			ret = sprintf(buf, "%s\n", duplex);
196c6c13965SNikolay Aleksandrov 		}
197d519e17eSAndy Gospodarek 	}
198d519e17eSAndy Gospodarek 	rtnl_unlock();
199d519e17eSAndy Gospodarek 	return ret;
200d519e17eSAndy Gospodarek }
201d519e17eSAndy Gospodarek 
20243cb76d9SGreg Kroah-Hartman static ssize_t show_dormant(struct device *dev,
20343cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf)
204b00055aaSStefan Rompf {
205b00055aaSStefan Rompf 	struct net_device *netdev = to_net_dev(dev);
206b00055aaSStefan Rompf 
207b00055aaSStefan Rompf 	if (netif_running(netdev))
208b00055aaSStefan Rompf 		return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
209b00055aaSStefan Rompf 
210b00055aaSStefan Rompf 	return -EINVAL;
211b00055aaSStefan Rompf }
212b00055aaSStefan Rompf 
21336cbd3dcSJan Engelhardt static const char *const operstates[] = {
214b00055aaSStefan Rompf 	"unknown",
215b00055aaSStefan Rompf 	"notpresent", /* currently unused */
216b00055aaSStefan Rompf 	"down",
217b00055aaSStefan Rompf 	"lowerlayerdown",
218b00055aaSStefan Rompf 	"testing", /* currently unused */
219b00055aaSStefan Rompf 	"dormant",
220b00055aaSStefan Rompf 	"up"
221b00055aaSStefan Rompf };
222b00055aaSStefan Rompf 
22343cb76d9SGreg Kroah-Hartman static ssize_t show_operstate(struct device *dev,
22443cb76d9SGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)
225b00055aaSStefan Rompf {
226b00055aaSStefan Rompf 	const struct net_device *netdev = to_net_dev(dev);
227b00055aaSStefan Rompf 	unsigned char operstate;
228b00055aaSStefan Rompf 
229b00055aaSStefan Rompf 	read_lock(&dev_base_lock);
230b00055aaSStefan Rompf 	operstate = netdev->operstate;
231b00055aaSStefan Rompf 	if (!netif_running(netdev))
232b00055aaSStefan Rompf 		operstate = IF_OPER_DOWN;
233b00055aaSStefan Rompf 	read_unlock(&dev_base_lock);
234b00055aaSStefan Rompf 
235e3a5cd9eSAdrian Bunk 	if (operstate >= ARRAY_SIZE(operstates))
236b00055aaSStefan Rompf 		return -EINVAL; /* should not happen */
237b00055aaSStefan Rompf 
238b00055aaSStefan Rompf 	return sprintf(buf, "%s\n", operstates[operstate]);
239b00055aaSStefan Rompf }
240b00055aaSStefan Rompf 
2411da177e4SLinus Torvalds /* read-write attributes */
2421da177e4SLinus Torvalds NETDEVICE_SHOW(mtu, fmt_dec);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static int change_mtu(struct net_device *net, unsigned long new_mtu)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	return dev_set_mtu(net, (int) new_mtu);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24943cb76d9SGreg Kroah-Hartman static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
25043cb76d9SGreg Kroah-Hartman 			 const char *buf, size_t len)
2511da177e4SLinus Torvalds {
25243cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_mtu);
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds NETDEVICE_SHOW(flags, fmt_hex);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int change_flags(struct net_device *net, unsigned long new_flags)
2581da177e4SLinus Torvalds {
25995c96174SEric Dumazet 	return dev_change_flags(net, (unsigned int) new_flags);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
26243cb76d9SGreg Kroah-Hartman static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
26343cb76d9SGreg Kroah-Hartman 			   const char *buf, size_t len)
2641da177e4SLinus Torvalds {
26543cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_flags);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	net->tx_queue_len = new_len;
2731da177e4SLinus Torvalds 	return 0;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
27643cb76d9SGreg Kroah-Hartman static ssize_t store_tx_queue_len(struct device *dev,
27743cb76d9SGreg Kroah-Hartman 				  struct device_attribute *attr,
27843cb76d9SGreg Kroah-Hartman 				  const char *buf, size_t len)
2791da177e4SLinus Torvalds {
2805e1fccc0SEric W. Biederman 	if (!capable(CAP_NET_ADMIN))
2815e1fccc0SEric W. Biederman 		return -EPERM;
2825e1fccc0SEric W. Biederman 
28343cb76d9SGreg Kroah-Hartman 	return netdev_store(dev, attr, buf, len, change_tx_queue_len);
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2860b815a1aSStephen Hemminger static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
2870b815a1aSStephen Hemminger 			     const char *buf, size_t len)
2880b815a1aSStephen Hemminger {
2890b815a1aSStephen Hemminger 	struct net_device *netdev = to_net_dev(dev);
2905e1fccc0SEric W. Biederman 	struct net *net = dev_net(netdev);
2910b815a1aSStephen Hemminger 	size_t count = len;
2920b815a1aSStephen Hemminger 	ssize_t ret;
2930b815a1aSStephen Hemminger 
2945e1fccc0SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2950b815a1aSStephen Hemminger 		return -EPERM;
2960b815a1aSStephen Hemminger 
2970b815a1aSStephen Hemminger 	/* ignore trailing newline */
2980b815a1aSStephen Hemminger 	if (len >  0 && buf[len - 1] == '\n')
2990b815a1aSStephen Hemminger 		--count;
3000b815a1aSStephen Hemminger 
301336ca57cSEric W. Biederman 	if (!rtnl_trylock())
302336ca57cSEric W. Biederman 		return restart_syscall();
3030b815a1aSStephen Hemminger 	ret = dev_set_alias(netdev, buf, count);
3040b815a1aSStephen Hemminger 	rtnl_unlock();
3050b815a1aSStephen Hemminger 
3060b815a1aSStephen Hemminger 	return ret < 0 ? ret : len;
3070b815a1aSStephen Hemminger }
3080b815a1aSStephen Hemminger 
3090b815a1aSStephen Hemminger static ssize_t show_ifalias(struct device *dev,
3100b815a1aSStephen Hemminger 			    struct device_attribute *attr, char *buf)
3110b815a1aSStephen Hemminger {
3120b815a1aSStephen Hemminger 	const struct net_device *netdev = to_net_dev(dev);
3130b815a1aSStephen Hemminger 	ssize_t ret = 0;
3140b815a1aSStephen Hemminger 
315336ca57cSEric W. Biederman 	if (!rtnl_trylock())
316336ca57cSEric W. Biederman 		return restart_syscall();
3170b815a1aSStephen Hemminger 	if (netdev->ifalias)
3180b815a1aSStephen Hemminger 		ret = sprintf(buf, "%s\n", netdev->ifalias);
3190b815a1aSStephen Hemminger 	rtnl_unlock();
3200b815a1aSStephen Hemminger 	return ret;
3210b815a1aSStephen Hemminger }
3220b815a1aSStephen Hemminger 
323a512b92bSVlad Dogaru NETDEVICE_SHOW(group, fmt_dec);
324a512b92bSVlad Dogaru 
325a512b92bSVlad Dogaru static int change_group(struct net_device *net, unsigned long new_group)
326a512b92bSVlad Dogaru {
327a512b92bSVlad Dogaru 	dev_set_group(net, (int) new_group);
328a512b92bSVlad Dogaru 	return 0;
329a512b92bSVlad Dogaru }
330a512b92bSVlad Dogaru 
331a512b92bSVlad Dogaru static ssize_t store_group(struct device *dev, struct device_attribute *attr,
332a512b92bSVlad Dogaru 			 const char *buf, size_t len)
333a512b92bSVlad Dogaru {
334a512b92bSVlad Dogaru 	return netdev_store(dev, attr, buf, len, change_group);
335a512b92bSVlad Dogaru }
336a512b92bSVlad Dogaru 
33743cb76d9SGreg Kroah-Hartman static struct device_attribute net_class_attributes[] = {
338c1f79426SStefan Assmann 	__ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
339fd586bacSKay Sievers 	__ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
3409d29672cSDavid Woodhouse 	__ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
3410b815a1aSStephen Hemminger 	__ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
342fd586bacSKay Sievers 	__ATTR(iflink, S_IRUGO, show_iflink, NULL),
343fd586bacSKay Sievers 	__ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
344fd586bacSKay Sievers 	__ATTR(type, S_IRUGO, show_type, NULL),
345b00055aaSStefan Rompf 	__ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
346fd586bacSKay Sievers 	__ATTR(address, S_IRUGO, show_address, NULL),
347fd586bacSKay Sievers 	__ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
348fdae0fdeSJiri Pirko 	__ATTR(carrier, S_IRUGO | S_IWUSR, show_carrier, store_carrier),
349d519e17eSAndy Gospodarek 	__ATTR(speed, S_IRUGO, show_speed, NULL),
350d519e17eSAndy Gospodarek 	__ATTR(duplex, S_IRUGO, show_duplex, NULL),
351b00055aaSStefan Rompf 	__ATTR(dormant, S_IRUGO, show_dormant, NULL),
352b00055aaSStefan Rompf 	__ATTR(operstate, S_IRUGO, show_operstate, NULL),
353fd586bacSKay Sievers 	__ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
354fd586bacSKay Sievers 	__ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
355fd586bacSKay Sievers 	__ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
356fd586bacSKay Sievers 	       store_tx_queue_len),
357b6644cb7SXiaotian Feng 	__ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
358fd586bacSKay Sievers 	{}
3591da177e4SLinus Torvalds };
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */
36243cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d,
36343cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf,
3641da177e4SLinus Torvalds 			    unsigned long offset)
3651da177e4SLinus Torvalds {
36643cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
3671da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
3681da177e4SLinus Torvalds 
369be1f3c2cSBen Hutchings 	WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
370be1f3c2cSBen Hutchings 			offset % sizeof(u64) != 0);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
37396e74088SPavel Emelyanov 	if (dev_isalive(dev)) {
37428172739SEric Dumazet 		struct rtnl_link_stats64 temp;
37528172739SEric Dumazet 		const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
37628172739SEric Dumazet 
377be1f3c2cSBen Hutchings 		ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
37896e74088SPavel Emelyanov 	}
3791da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
3801da177e4SLinus Torvalds 	return ret;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* generate a read-only statistics attribute */
3841da177e4SLinus Torvalds #define NETSTAT_ENTRY(name)						\
38543cb76d9SGreg Kroah-Hartman static ssize_t show_##name(struct device *d,				\
38643cb76d9SGreg Kroah-Hartman 			   struct device_attribute *attr, char *buf) 	\
3871da177e4SLinus Torvalds {									\
38843cb76d9SGreg Kroah-Hartman 	return netstat_show(d, attr, buf,				\
389be1f3c2cSBen Hutchings 			    offsetof(struct rtnl_link_stats64, name));	\
3901da177e4SLinus Torvalds }									\
39143cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets);
3941da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets);
3951da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes);
3961da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes);
3971da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors);
3981da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors);
3991da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped);
4001da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped);
4011da177e4SLinus Torvalds NETSTAT_ENTRY(multicast);
4021da177e4SLinus Torvalds NETSTAT_ENTRY(collisions);
4031da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors);
4041da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors);
4051da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors);
4061da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors);
4071da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors);
4081da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors);
4091da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors);
4101da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors);
4111da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors);
4121da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors);
4131da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors);
4141da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed);
4151da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = {
41843cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_packets.attr,
41943cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_packets.attr,
42043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_bytes.attr,
42143cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_bytes.attr,
42243cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_errors.attr,
42343cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_errors.attr,
42443cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_dropped.attr,
42543cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_dropped.attr,
42643cb76d9SGreg Kroah-Hartman 	&dev_attr_multicast.attr,
42743cb76d9SGreg Kroah-Hartman 	&dev_attr_collisions.attr,
42843cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_length_errors.attr,
42943cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_over_errors.attr,
43043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_crc_errors.attr,
43143cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_frame_errors.attr,
43243cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_fifo_errors.attr,
43343cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_missed_errors.attr,
43443cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_aborted_errors.attr,
43543cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_carrier_errors.attr,
43643cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_fifo_errors.attr,
43743cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_heartbeat_errors.attr,
43843cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_window_errors.attr,
43943cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_compressed.attr,
44043cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_compressed.attr,
4411da177e4SLinus Torvalds 	NULL
4421da177e4SLinus Torvalds };
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static struct attribute_group netstat_group = {
4461da177e4SLinus Torvalds 	.name  = "statistics",
4471da177e4SLinus Torvalds 	.attrs  = netstat_attrs,
4481da177e4SLinus Torvalds };
44938c1a01cSJohannes Berg 
45038c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
45138c1a01cSJohannes Berg static struct attribute *wireless_attrs[] = {
45238c1a01cSJohannes Berg 	NULL
45338c1a01cSJohannes Berg };
45438c1a01cSJohannes Berg 
45538c1a01cSJohannes Berg static struct attribute_group wireless_group = {
45638c1a01cSJohannes Berg 	.name = "wireless",
45738c1a01cSJohannes Berg 	.attrs = wireless_attrs,
45838c1a01cSJohannes Berg };
45938c1a01cSJohannes Berg #endif
460d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */
4611da177e4SLinus Torvalds 
46230bde1f5SStephen Rothwell #ifdef CONFIG_RPS
4630a9627f2STom Herbert /*
4640a9627f2STom Herbert  * RX queue sysfs structures and functions.
4650a9627f2STom Herbert  */
4660a9627f2STom Herbert struct rx_queue_attribute {
4670a9627f2STom Herbert 	struct attribute attr;
4680a9627f2STom Herbert 	ssize_t (*show)(struct netdev_rx_queue *queue,
4690a9627f2STom Herbert 	    struct rx_queue_attribute *attr, char *buf);
4700a9627f2STom Herbert 	ssize_t (*store)(struct netdev_rx_queue *queue,
4710a9627f2STom Herbert 	    struct rx_queue_attribute *attr, const char *buf, size_t len);
4720a9627f2STom Herbert };
4730a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr,		\
4740a9627f2STom Herbert     struct rx_queue_attribute, attr)
4750a9627f2STom Herbert 
4760a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
4770a9627f2STom Herbert 
4780a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
4790a9627f2STom Herbert 				  char *buf)
4800a9627f2STom Herbert {
4810a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
4820a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
4830a9627f2STom Herbert 
4840a9627f2STom Herbert 	if (!attribute->show)
4850a9627f2STom Herbert 		return -EIO;
4860a9627f2STom Herbert 
4870a9627f2STom Herbert 	return attribute->show(queue, attribute, buf);
4880a9627f2STom Herbert }
4890a9627f2STom Herbert 
4900a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
4910a9627f2STom Herbert 				   const char *buf, size_t count)
4920a9627f2STom Herbert {
4930a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
4940a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
4950a9627f2STom Herbert 
4960a9627f2STom Herbert 	if (!attribute->store)
4970a9627f2STom Herbert 		return -EIO;
4980a9627f2STom Herbert 
4990a9627f2STom Herbert 	return attribute->store(queue, attribute, buf, count);
5000a9627f2STom Herbert }
5010a9627f2STom Herbert 
502fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = {
5030a9627f2STom Herbert 	.show = rx_queue_attr_show,
5040a9627f2STom Herbert 	.store = rx_queue_attr_store,
5050a9627f2STom Herbert };
5060a9627f2STom Herbert 
5070a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue,
5080a9627f2STom Herbert 			    struct rx_queue_attribute *attribute, char *buf)
5090a9627f2STom Herbert {
5100a9627f2STom Herbert 	struct rps_map *map;
5110a9627f2STom Herbert 	cpumask_var_t mask;
5120a9627f2STom Herbert 	size_t len = 0;
5130a9627f2STom Herbert 	int i;
5140a9627f2STom Herbert 
5150a9627f2STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
5160a9627f2STom Herbert 		return -ENOMEM;
5170a9627f2STom Herbert 
5180a9627f2STom Herbert 	rcu_read_lock();
5190a9627f2STom Herbert 	map = rcu_dereference(queue->rps_map);
5200a9627f2STom Herbert 	if (map)
5210a9627f2STom Herbert 		for (i = 0; i < map->len; i++)
5220a9627f2STom Herbert 			cpumask_set_cpu(map->cpus[i], mask);
5230a9627f2STom Herbert 
5240a9627f2STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
5250a9627f2STom Herbert 	if (PAGE_SIZE - len < 3) {
5260a9627f2STom Herbert 		rcu_read_unlock();
5270a9627f2STom Herbert 		free_cpumask_var(mask);
5280a9627f2STom Herbert 		return -EINVAL;
5290a9627f2STom Herbert 	}
5300a9627f2STom Herbert 	rcu_read_unlock();
5310a9627f2STom Herbert 
5320a9627f2STom Herbert 	free_cpumask_var(mask);
5330a9627f2STom Herbert 	len += sprintf(buf + len, "\n");
5340a9627f2STom Herbert 	return len;
5350a9627f2STom Herbert }
5360a9627f2STom Herbert 
537f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue,
5380a9627f2STom Herbert 		      struct rx_queue_attribute *attribute,
5390a9627f2STom Herbert 		      const char *buf, size_t len)
5400a9627f2STom Herbert {
5410a9627f2STom Herbert 	struct rps_map *old_map, *map;
5420a9627f2STom Herbert 	cpumask_var_t mask;
5430a9627f2STom Herbert 	int err, cpu, i;
5440a9627f2STom Herbert 	static DEFINE_SPINLOCK(rps_map_lock);
5450a9627f2STom Herbert 
5460a9627f2STom Herbert 	if (!capable(CAP_NET_ADMIN))
5470a9627f2STom Herbert 		return -EPERM;
5480a9627f2STom Herbert 
5490a9627f2STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5500a9627f2STom Herbert 		return -ENOMEM;
5510a9627f2STom Herbert 
5520a9627f2STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
5530a9627f2STom Herbert 	if (err) {
5540a9627f2STom Herbert 		free_cpumask_var(mask);
5550a9627f2STom Herbert 		return err;
5560a9627f2STom Herbert 	}
5570a9627f2STom Herbert 
55895c96174SEric Dumazet 	map = kzalloc(max_t(unsigned int,
5590a9627f2STom Herbert 	    RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
5600a9627f2STom Herbert 	    GFP_KERNEL);
5610a9627f2STom Herbert 	if (!map) {
5620a9627f2STom Herbert 		free_cpumask_var(mask);
5630a9627f2STom Herbert 		return -ENOMEM;
5640a9627f2STom Herbert 	}
5650a9627f2STom Herbert 
5660a9627f2STom Herbert 	i = 0;
5670a9627f2STom Herbert 	for_each_cpu_and(cpu, mask, cpu_online_mask)
5680a9627f2STom Herbert 		map->cpus[i++] = cpu;
5690a9627f2STom Herbert 
5700a9627f2STom Herbert 	if (i)
5710a9627f2STom Herbert 		map->len = i;
5720a9627f2STom Herbert 	else {
5730a9627f2STom Herbert 		kfree(map);
5740a9627f2STom Herbert 		map = NULL;
5750a9627f2STom Herbert 	}
5760a9627f2STom Herbert 
5770a9627f2STom Herbert 	spin_lock(&rps_map_lock);
5786e3f7fafSEric Dumazet 	old_map = rcu_dereference_protected(queue->rps_map,
5796e3f7fafSEric Dumazet 					    lockdep_is_held(&rps_map_lock));
5800a9627f2STom Herbert 	rcu_assign_pointer(queue->rps_map, map);
5810a9627f2STom Herbert 	spin_unlock(&rps_map_lock);
5820a9627f2STom Herbert 
583adc9300eSEric Dumazet 	if (map)
584c5905afbSIngo Molnar 		static_key_slow_inc(&rps_needed);
585adc9300eSEric Dumazet 	if (old_map) {
586f6f80238SLai Jiangshan 		kfree_rcu(old_map, rcu);
587c5905afbSIngo Molnar 		static_key_slow_dec(&rps_needed);
588adc9300eSEric Dumazet 	}
5890a9627f2STom Herbert 	free_cpumask_var(mask);
5900a9627f2STom Herbert 	return len;
5910a9627f2STom Herbert }
5920a9627f2STom Herbert 
593fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
594fec5e652STom Herbert 					   struct rx_queue_attribute *attr,
595fec5e652STom Herbert 					   char *buf)
596fec5e652STom Herbert {
597fec5e652STom Herbert 	struct rps_dev_flow_table *flow_table;
59860b778ceSEric Dumazet 	unsigned long val = 0;
599fec5e652STom Herbert 
600fec5e652STom Herbert 	rcu_read_lock();
601fec5e652STom Herbert 	flow_table = rcu_dereference(queue->rps_flow_table);
602fec5e652STom Herbert 	if (flow_table)
60360b778ceSEric Dumazet 		val = (unsigned long)flow_table->mask + 1;
604fec5e652STom Herbert 	rcu_read_unlock();
605fec5e652STom Herbert 
60660b778ceSEric Dumazet 	return sprintf(buf, "%lu\n", val);
607fec5e652STom Herbert }
608fec5e652STom Herbert 
609fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu)
610fec5e652STom Herbert {
611fec5e652STom Herbert 	struct rps_dev_flow_table *table = container_of(rcu,
612fec5e652STom Herbert 	    struct rps_dev_flow_table, rcu);
613243198d0SAl Viro 	vfree(table);
614fec5e652STom Herbert }
615fec5e652STom Herbert 
616f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
617fec5e652STom Herbert 				     struct rx_queue_attribute *attr,
618fec5e652STom Herbert 				     const char *buf, size_t len)
619fec5e652STom Herbert {
62060b778ceSEric Dumazet 	unsigned long mask, count;
621fec5e652STom Herbert 	struct rps_dev_flow_table *table, *old_table;
622fec5e652STom Herbert 	static DEFINE_SPINLOCK(rps_dev_flow_lock);
62360b778ceSEric Dumazet 	int rc;
624fec5e652STom Herbert 
625fec5e652STom Herbert 	if (!capable(CAP_NET_ADMIN))
626fec5e652STom Herbert 		return -EPERM;
627fec5e652STom Herbert 
62860b778ceSEric Dumazet 	rc = kstrtoul(buf, 0, &count);
62960b778ceSEric Dumazet 	if (rc < 0)
63060b778ceSEric Dumazet 		return rc;
631fec5e652STom Herbert 
632fec5e652STom Herbert 	if (count) {
63360b778ceSEric Dumazet 		mask = count - 1;
63460b778ceSEric Dumazet 		/* mask = roundup_pow_of_two(count) - 1;
63560b778ceSEric Dumazet 		 * without overflows...
63660b778ceSEric Dumazet 		 */
63760b778ceSEric Dumazet 		while ((mask | (mask >> 1)) != mask)
63860b778ceSEric Dumazet 			mask |= (mask >> 1);
63960b778ceSEric Dumazet 		/* On 64 bit arches, must check mask fits in table->mask (u32),
64060b778ceSEric Dumazet 		 * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
64160b778ceSEric Dumazet 		 * doesnt overflow.
64260b778ceSEric Dumazet 		 */
64360b778ceSEric Dumazet #if BITS_PER_LONG > 32
64460b778ceSEric Dumazet 		if (mask > (unsigned long)(u32)mask)
645a0a129f8SXi Wang 			return -EINVAL;
64660b778ceSEric Dumazet #else
64760b778ceSEric Dumazet 		if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
648a0a129f8SXi Wang 				/ sizeof(struct rps_dev_flow)) {
649fec5e652STom Herbert 			/* Enforce a limit to prevent overflow */
650fec5e652STom Herbert 			return -EINVAL;
651fec5e652STom Herbert 		}
65260b778ceSEric Dumazet #endif
65360b778ceSEric Dumazet 		table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
654fec5e652STom Herbert 		if (!table)
655fec5e652STom Herbert 			return -ENOMEM;
656fec5e652STom Herbert 
65760b778ceSEric Dumazet 		table->mask = mask;
65860b778ceSEric Dumazet 		for (count = 0; count <= mask; count++)
65960b778ceSEric Dumazet 			table->flows[count].cpu = RPS_NO_CPU;
660fec5e652STom Herbert 	} else
661fec5e652STom Herbert 		table = NULL;
662fec5e652STom Herbert 
663fec5e652STom Herbert 	spin_lock(&rps_dev_flow_lock);
6646e3f7fafSEric Dumazet 	old_table = rcu_dereference_protected(queue->rps_flow_table,
6656e3f7fafSEric Dumazet 					      lockdep_is_held(&rps_dev_flow_lock));
666fec5e652STom Herbert 	rcu_assign_pointer(queue->rps_flow_table, table);
667fec5e652STom Herbert 	spin_unlock(&rps_dev_flow_lock);
668fec5e652STom Herbert 
669fec5e652STom Herbert 	if (old_table)
670fec5e652STom Herbert 		call_rcu(&old_table->rcu, rps_dev_flow_table_release);
671fec5e652STom Herbert 
672fec5e652STom Herbert 	return len;
673fec5e652STom Herbert }
674fec5e652STom Herbert 
6750a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute =
6760a9627f2STom Herbert 	__ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
6770a9627f2STom Herbert 
678fec5e652STom Herbert 
679fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
680fec5e652STom Herbert 	__ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
681fec5e652STom Herbert 	    show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
682fec5e652STom Herbert 
6830a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = {
6840a9627f2STom Herbert 	&rps_cpus_attribute.attr,
685fec5e652STom Herbert 	&rps_dev_flow_table_cnt_attribute.attr,
6860a9627f2STom Herbert 	NULL
6870a9627f2STom Herbert };
6880a9627f2STom Herbert 
6890a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj)
6900a9627f2STom Herbert {
6910a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
6926e3f7fafSEric Dumazet 	struct rps_map *map;
6936e3f7fafSEric Dumazet 	struct rps_dev_flow_table *flow_table;
6940a9627f2STom Herbert 
695fec5e652STom Herbert 
69633d480ceSEric Dumazet 	map = rcu_dereference_protected(queue->rps_map, 1);
6979ea19481SJohn Fastabend 	if (map) {
6989ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_map, NULL);
699f6f80238SLai Jiangshan 		kfree_rcu(map, rcu);
7009ea19481SJohn Fastabend 	}
7016e3f7fafSEric Dumazet 
70233d480ceSEric Dumazet 	flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
7039ea19481SJohn Fastabend 	if (flow_table) {
7049ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_flow_table, NULL);
7056e3f7fafSEric Dumazet 		call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
7069ea19481SJohn Fastabend 	}
7070a9627f2STom Herbert 
7089ea19481SJohn Fastabend 	memset(kobj, 0, sizeof(*kobj));
709fe822240STom Herbert 	dev_put(queue->dev);
7100a9627f2STom Herbert }
7110a9627f2STom Herbert 
7120a9627f2STom Herbert static struct kobj_type rx_queue_ktype = {
7130a9627f2STom Herbert 	.sysfs_ops = &rx_queue_sysfs_ops,
7140a9627f2STom Herbert 	.release = rx_queue_release,
7150a9627f2STom Herbert 	.default_attrs = rx_queue_default_attrs,
7160a9627f2STom Herbert };
7170a9627f2STom Herbert 
7180a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index)
7190a9627f2STom Herbert {
7200a9627f2STom Herbert 	struct netdev_rx_queue *queue = net->_rx + index;
7210a9627f2STom Herbert 	struct kobject *kobj = &queue->kobj;
7220a9627f2STom Herbert 	int error = 0;
7230a9627f2STom Herbert 
7240a9627f2STom Herbert 	kobj->kset = net->queues_kset;
7250a9627f2STom Herbert 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
7260a9627f2STom Herbert 	    "rx-%u", index);
7270a9627f2STom Herbert 	if (error) {
7280a9627f2STom Herbert 		kobject_put(kobj);
7290a9627f2STom Herbert 		return error;
7300a9627f2STom Herbert 	}
7310a9627f2STom Herbert 
7320a9627f2STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
733fe822240STom Herbert 	dev_hold(queue->dev);
7340a9627f2STom Herbert 
7350a9627f2STom Herbert 	return error;
7360a9627f2STom Herbert }
737bf264145STom Herbert #endif /* CONFIG_RPS */
7380a9627f2STom Herbert 
73962fe0b40SBen Hutchings int
74062fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
7410a9627f2STom Herbert {
742bf264145STom Herbert #ifdef CONFIG_RPS
7430a9627f2STom Herbert 	int i;
7440a9627f2STom Herbert 	int error = 0;
7450a9627f2STom Herbert 
74662fe0b40SBen Hutchings 	for (i = old_num; i < new_num; i++) {
7470a9627f2STom Herbert 		error = rx_queue_add_kobject(net, i);
74862fe0b40SBen Hutchings 		if (error) {
74962fe0b40SBen Hutchings 			new_num = old_num;
7500a9627f2STom Herbert 			break;
7510a9627f2STom Herbert 		}
75262fe0b40SBen Hutchings 	}
7530a9627f2STom Herbert 
75462fe0b40SBen Hutchings 	while (--i >= new_num)
7550a9627f2STom Herbert 		kobject_put(&net->_rx[i].kobj);
7560a9627f2STom Herbert 
7570a9627f2STom Herbert 	return error;
758bf264145STom Herbert #else
759bf264145STom Herbert 	return 0;
760bf264145STom Herbert #endif
7610a9627f2STom Herbert }
7620a9627f2STom Herbert 
763ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
7641d24eb48STom Herbert /*
7651d24eb48STom Herbert  * netdev_queue sysfs structures and functions.
7661d24eb48STom Herbert  */
7671d24eb48STom Herbert struct netdev_queue_attribute {
7681d24eb48STom Herbert 	struct attribute attr;
7691d24eb48STom Herbert 	ssize_t (*show)(struct netdev_queue *queue,
7701d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, char *buf);
7711d24eb48STom Herbert 	ssize_t (*store)(struct netdev_queue *queue,
7721d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, const char *buf, size_t len);
7731d24eb48STom Herbert };
7741d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr,		\
7751d24eb48STom Herbert     struct netdev_queue_attribute, attr)
7761d24eb48STom Herbert 
7771d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
7781d24eb48STom Herbert 
7791d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj,
7801d24eb48STom Herbert 				      struct attribute *attr, char *buf)
78162fe0b40SBen Hutchings {
7821d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
7831d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
7841d24eb48STom Herbert 
7851d24eb48STom Herbert 	if (!attribute->show)
7861d24eb48STom Herbert 		return -EIO;
7871d24eb48STom Herbert 
7881d24eb48STom Herbert 	return attribute->show(queue, attribute, buf);
7891d24eb48STom Herbert }
7901d24eb48STom Herbert 
7911d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj,
7921d24eb48STom Herbert 				       struct attribute *attr,
7931d24eb48STom Herbert 				       const char *buf, size_t count)
7941d24eb48STom Herbert {
7951d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
7961d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
7971d24eb48STom Herbert 
7981d24eb48STom Herbert 	if (!attribute->store)
7991d24eb48STom Herbert 		return -EIO;
8001d24eb48STom Herbert 
8011d24eb48STom Herbert 	return attribute->store(queue, attribute, buf, count);
8021d24eb48STom Herbert }
8031d24eb48STom Herbert 
8041d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = {
8051d24eb48STom Herbert 	.show = netdev_queue_attr_show,
8061d24eb48STom Herbert 	.store = netdev_queue_attr_store,
8071d24eb48STom Herbert };
8081d24eb48STom Herbert 
809ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue,
810ccf5ff69Sdavid decotigny 				  struct netdev_queue_attribute *attribute,
811ccf5ff69Sdavid decotigny 				  char *buf)
812ccf5ff69Sdavid decotigny {
813ccf5ff69Sdavid decotigny 	unsigned long trans_timeout;
814ccf5ff69Sdavid decotigny 
815ccf5ff69Sdavid decotigny 	spin_lock_irq(&queue->_xmit_lock);
816ccf5ff69Sdavid decotigny 	trans_timeout = queue->trans_timeout;
817ccf5ff69Sdavid decotigny 	spin_unlock_irq(&queue->_xmit_lock);
818ccf5ff69Sdavid decotigny 
819ccf5ff69Sdavid decotigny 	return sprintf(buf, "%lu", trans_timeout);
820ccf5ff69Sdavid decotigny }
821ccf5ff69Sdavid decotigny 
822ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout =
823ccf5ff69Sdavid decotigny 	__ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
824ccf5ff69Sdavid decotigny 
825114cf580STom Herbert #ifdef CONFIG_BQL
826114cf580STom Herbert /*
827114cf580STom Herbert  * Byte queue limits sysfs structures and functions.
828114cf580STom Herbert  */
829114cf580STom Herbert static ssize_t bql_show(char *buf, unsigned int value)
830114cf580STom Herbert {
831114cf580STom Herbert 	return sprintf(buf, "%u\n", value);
832114cf580STom Herbert }
833114cf580STom Herbert 
834114cf580STom Herbert static ssize_t bql_set(const char *buf, const size_t count,
835114cf580STom Herbert 		       unsigned int *pvalue)
836114cf580STom Herbert {
837114cf580STom Herbert 	unsigned int value;
838114cf580STom Herbert 	int err;
839114cf580STom Herbert 
840114cf580STom Herbert 	if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
841114cf580STom Herbert 		value = DQL_MAX_LIMIT;
842114cf580STom Herbert 	else {
843114cf580STom Herbert 		err = kstrtouint(buf, 10, &value);
844114cf580STom Herbert 		if (err < 0)
845114cf580STom Herbert 			return err;
846114cf580STom Herbert 		if (value > DQL_MAX_LIMIT)
847114cf580STom Herbert 			return -EINVAL;
848114cf580STom Herbert 	}
849114cf580STom Herbert 
850114cf580STom Herbert 	*pvalue = value;
851114cf580STom Herbert 
852114cf580STom Herbert 	return count;
853114cf580STom Herbert }
854114cf580STom Herbert 
855114cf580STom Herbert static ssize_t bql_show_hold_time(struct netdev_queue *queue,
856114cf580STom Herbert 				  struct netdev_queue_attribute *attr,
857114cf580STom Herbert 				  char *buf)
858114cf580STom Herbert {
859114cf580STom Herbert 	struct dql *dql = &queue->dql;
860114cf580STom Herbert 
861114cf580STom Herbert 	return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
862114cf580STom Herbert }
863114cf580STom Herbert 
864114cf580STom Herbert static ssize_t bql_set_hold_time(struct netdev_queue *queue,
865114cf580STom Herbert 				 struct netdev_queue_attribute *attribute,
866114cf580STom Herbert 				 const char *buf, size_t len)
867114cf580STom Herbert {
868114cf580STom Herbert 	struct dql *dql = &queue->dql;
86995c96174SEric Dumazet 	unsigned int value;
870114cf580STom Herbert 	int err;
871114cf580STom Herbert 
872114cf580STom Herbert 	err = kstrtouint(buf, 10, &value);
873114cf580STom Herbert 	if (err < 0)
874114cf580STom Herbert 		return err;
875114cf580STom Herbert 
876114cf580STom Herbert 	dql->slack_hold_time = msecs_to_jiffies(value);
877114cf580STom Herbert 
878114cf580STom Herbert 	return len;
879114cf580STom Herbert }
880114cf580STom Herbert 
881114cf580STom Herbert static struct netdev_queue_attribute bql_hold_time_attribute =
882114cf580STom Herbert 	__ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
883114cf580STom Herbert 	    bql_set_hold_time);
884114cf580STom Herbert 
885114cf580STom Herbert static ssize_t bql_show_inflight(struct netdev_queue *queue,
886114cf580STom Herbert 				 struct netdev_queue_attribute *attr,
887114cf580STom Herbert 				 char *buf)
888114cf580STom Herbert {
889114cf580STom Herbert 	struct dql *dql = &queue->dql;
890114cf580STom Herbert 
891114cf580STom Herbert 	return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
892114cf580STom Herbert }
893114cf580STom Herbert 
894114cf580STom Herbert static struct netdev_queue_attribute bql_inflight_attribute =
895795d9a25SHiroaki SHIMODA 	__ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
896114cf580STom Herbert 
897114cf580STom Herbert #define BQL_ATTR(NAME, FIELD)						\
898114cf580STom Herbert static ssize_t bql_show_ ## NAME(struct netdev_queue *queue,		\
899114cf580STom Herbert 				 struct netdev_queue_attribute *attr,	\
900114cf580STom Herbert 				 char *buf)				\
901114cf580STom Herbert {									\
902114cf580STom Herbert 	return bql_show(buf, queue->dql.FIELD);				\
903114cf580STom Herbert }									\
904114cf580STom Herbert 									\
905114cf580STom Herbert static ssize_t bql_set_ ## NAME(struct netdev_queue *queue,		\
906114cf580STom Herbert 				struct netdev_queue_attribute *attr,	\
907114cf580STom Herbert 				const char *buf, size_t len)		\
908114cf580STom Herbert {									\
909114cf580STom Herbert 	return bql_set(buf, len, &queue->dql.FIELD);			\
910114cf580STom Herbert }									\
911114cf580STom Herbert 									\
912114cf580STom Herbert static struct netdev_queue_attribute bql_ ## NAME ## _attribute =	\
913114cf580STom Herbert 	__ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME,		\
914114cf580STom Herbert 	    bql_set_ ## NAME);
915114cf580STom Herbert 
916114cf580STom Herbert BQL_ATTR(limit, limit)
917114cf580STom Herbert BQL_ATTR(limit_max, max_limit)
918114cf580STom Herbert BQL_ATTR(limit_min, min_limit)
919114cf580STom Herbert 
920114cf580STom Herbert static struct attribute *dql_attrs[] = {
921114cf580STom Herbert 	&bql_limit_attribute.attr,
922114cf580STom Herbert 	&bql_limit_max_attribute.attr,
923114cf580STom Herbert 	&bql_limit_min_attribute.attr,
924114cf580STom Herbert 	&bql_hold_time_attribute.attr,
925114cf580STom Herbert 	&bql_inflight_attribute.attr,
926114cf580STom Herbert 	NULL
927114cf580STom Herbert };
928114cf580STom Herbert 
929114cf580STom Herbert static struct attribute_group dql_group = {
930114cf580STom Herbert 	.name  = "byte_queue_limits",
931114cf580STom Herbert 	.attrs  = dql_attrs,
932114cf580STom Herbert };
933114cf580STom Herbert #endif /* CONFIG_BQL */
934114cf580STom Herbert 
935ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
9361d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
9371d24eb48STom Herbert {
9381d24eb48STom Herbert 	struct net_device *dev = queue->dev;
9391d24eb48STom Herbert 	int i;
9401d24eb48STom Herbert 
9411d24eb48STom Herbert 	for (i = 0; i < dev->num_tx_queues; i++)
9421d24eb48STom Herbert 		if (queue == &dev->_tx[i])
9431d24eb48STom Herbert 			break;
9441d24eb48STom Herbert 
9451d24eb48STom Herbert 	BUG_ON(i >= dev->num_tx_queues);
9461d24eb48STom Herbert 
9471d24eb48STom Herbert 	return i;
9481d24eb48STom Herbert }
9491d24eb48STom Herbert 
9501d24eb48STom Herbert 
9511d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue,
9521d24eb48STom Herbert 			    struct netdev_queue_attribute *attribute, char *buf)
9531d24eb48STom Herbert {
9541d24eb48STom Herbert 	struct net_device *dev = queue->dev;
9551d24eb48STom Herbert 	struct xps_dev_maps *dev_maps;
9561d24eb48STom Herbert 	cpumask_var_t mask;
9571d24eb48STom Herbert 	unsigned long index;
9581d24eb48STom Herbert 	size_t len = 0;
9591d24eb48STom Herbert 	int i;
9601d24eb48STom Herbert 
9611d24eb48STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
9621d24eb48STom Herbert 		return -ENOMEM;
9631d24eb48STom Herbert 
9641d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
9651d24eb48STom Herbert 
9661d24eb48STom Herbert 	rcu_read_lock();
9671d24eb48STom Herbert 	dev_maps = rcu_dereference(dev->xps_maps);
9681d24eb48STom Herbert 	if (dev_maps) {
9691d24eb48STom Herbert 		for_each_possible_cpu(i) {
9701d24eb48STom Herbert 			struct xps_map *map =
9711d24eb48STom Herbert 			    rcu_dereference(dev_maps->cpu_map[i]);
9721d24eb48STom Herbert 			if (map) {
9731d24eb48STom Herbert 				int j;
9741d24eb48STom Herbert 				for (j = 0; j < map->len; j++) {
9751d24eb48STom Herbert 					if (map->queues[j] == index) {
9761d24eb48STom Herbert 						cpumask_set_cpu(i, mask);
9771d24eb48STom Herbert 						break;
9781d24eb48STom Herbert 					}
9791d24eb48STom Herbert 				}
9801d24eb48STom Herbert 			}
9811d24eb48STom Herbert 		}
9821d24eb48STom Herbert 	}
9831d24eb48STom Herbert 	rcu_read_unlock();
9841d24eb48STom Herbert 
9851d24eb48STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
9861d24eb48STom Herbert 	if (PAGE_SIZE - len < 3) {
9871d24eb48STom Herbert 		free_cpumask_var(mask);
9881d24eb48STom Herbert 		return -EINVAL;
9891d24eb48STom Herbert 	}
9901d24eb48STom Herbert 
9911d24eb48STom Herbert 	free_cpumask_var(mask);
9921d24eb48STom Herbert 	len += sprintf(buf + len, "\n");
9931d24eb48STom Herbert 	return len;
9941d24eb48STom Herbert }
9951d24eb48STom Herbert 
9961d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue,
9971d24eb48STom Herbert 		      struct netdev_queue_attribute *attribute,
9981d24eb48STom Herbert 		      const char *buf, size_t len)
9991d24eb48STom Herbert {
10001d24eb48STom Herbert 	struct net_device *dev = queue->dev;
10011d24eb48STom Herbert 	unsigned long index;
1002537c00deSAlexander Duyck 	cpumask_var_t mask;
1003537c00deSAlexander Duyck 	int err;
10041d24eb48STom Herbert 
10051d24eb48STom Herbert 	if (!capable(CAP_NET_ADMIN))
10061d24eb48STom Herbert 		return -EPERM;
10071d24eb48STom Herbert 
10081d24eb48STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
10091d24eb48STom Herbert 		return -ENOMEM;
10101d24eb48STom Herbert 
10111d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
10121d24eb48STom Herbert 
10131d24eb48STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
10141d24eb48STom Herbert 	if (err) {
10151d24eb48STom Herbert 		free_cpumask_var(mask);
10161d24eb48STom Herbert 		return err;
10171d24eb48STom Herbert 	}
10181d24eb48STom Herbert 
1019537c00deSAlexander Duyck 	err = netif_set_xps_queue(dev, mask, index);
10201d24eb48STom Herbert 
10211d24eb48STom Herbert 	free_cpumask_var(mask);
10221d24eb48STom Herbert 
1023537c00deSAlexander Duyck 	return err ? : len;
10241d24eb48STom Herbert }
10251d24eb48STom Herbert 
10261d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute =
10271d24eb48STom Herbert     __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1028ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */
10291d24eb48STom Herbert 
10301d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = {
1031ccf5ff69Sdavid decotigny 	&queue_trans_timeout.attr,
1032ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
10331d24eb48STom Herbert 	&xps_cpus_attribute.attr,
1034ccf5ff69Sdavid decotigny #endif
10351d24eb48STom Herbert 	NULL
10361d24eb48STom Herbert };
10371d24eb48STom Herbert 
10381d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj)
10391d24eb48STom Herbert {
10401d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
10411d24eb48STom Herbert 
10421d24eb48STom Herbert 	memset(kobj, 0, sizeof(*kobj));
10431d24eb48STom Herbert 	dev_put(queue->dev);
10441d24eb48STom Herbert }
10451d24eb48STom Herbert 
10461d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = {
10471d24eb48STom Herbert 	.sysfs_ops = &netdev_queue_sysfs_ops,
10481d24eb48STom Herbert 	.release = netdev_queue_release,
10491d24eb48STom Herbert 	.default_attrs = netdev_queue_default_attrs,
10501d24eb48STom Herbert };
10511d24eb48STom Herbert 
10521d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index)
10531d24eb48STom Herbert {
10541d24eb48STom Herbert 	struct netdev_queue *queue = net->_tx + index;
10551d24eb48STom Herbert 	struct kobject *kobj = &queue->kobj;
10561d24eb48STom Herbert 	int error = 0;
10571d24eb48STom Herbert 
10581d24eb48STom Herbert 	kobj->kset = net->queues_kset;
10591d24eb48STom Herbert 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
10601d24eb48STom Herbert 	    "tx-%u", index);
1061114cf580STom Herbert 	if (error)
1062114cf580STom Herbert 		goto exit;
1063114cf580STom Herbert 
1064114cf580STom Herbert #ifdef CONFIG_BQL
1065114cf580STom Herbert 	error = sysfs_create_group(kobj, &dql_group);
1066114cf580STom Herbert 	if (error)
1067114cf580STom Herbert 		goto exit;
1068114cf580STom Herbert #endif
10691d24eb48STom Herbert 
10701d24eb48STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
10711d24eb48STom Herbert 	dev_hold(queue->dev);
10721d24eb48STom Herbert 
1073114cf580STom Herbert 	return 0;
1074114cf580STom Herbert exit:
1075114cf580STom Herbert 	kobject_put(kobj);
10761d24eb48STom Herbert 	return error;
10771d24eb48STom Herbert }
1078ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
10791d24eb48STom Herbert 
10801d24eb48STom Herbert int
10811d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
10821d24eb48STom Herbert {
1083ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
10841d24eb48STom Herbert 	int i;
10851d24eb48STom Herbert 	int error = 0;
10861d24eb48STom Herbert 
10871d24eb48STom Herbert 	for (i = old_num; i < new_num; i++) {
10881d24eb48STom Herbert 		error = netdev_queue_add_kobject(net, i);
10891d24eb48STom Herbert 		if (error) {
10901d24eb48STom Herbert 			new_num = old_num;
10911d24eb48STom Herbert 			break;
10921d24eb48STom Herbert 		}
10931d24eb48STom Herbert 	}
10941d24eb48STom Herbert 
1095114cf580STom Herbert 	while (--i >= new_num) {
1096114cf580STom Herbert 		struct netdev_queue *queue = net->_tx + i;
1097114cf580STom Herbert 
1098114cf580STom Herbert #ifdef CONFIG_BQL
1099114cf580STom Herbert 		sysfs_remove_group(&queue->kobj, &dql_group);
1100114cf580STom Herbert #endif
1101114cf580STom Herbert 		kobject_put(&queue->kobj);
1102114cf580STom Herbert 	}
11031d24eb48STom Herbert 
11041d24eb48STom Herbert 	return error;
1105bf264145STom Herbert #else
1106bf264145STom Herbert 	return 0;
1107ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
11081d24eb48STom Herbert }
11091d24eb48STom Herbert 
11101d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net)
11111d24eb48STom Herbert {
1112bf264145STom Herbert 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
11131d24eb48STom Herbert 
1114ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
111562fe0b40SBen Hutchings 	net->queues_kset = kset_create_and_add("queues",
111662fe0b40SBen Hutchings 	    NULL, &net->dev.kobj);
111762fe0b40SBen Hutchings 	if (!net->queues_kset)
111862fe0b40SBen Hutchings 		return -ENOMEM;
1119bf264145STom Herbert #endif
11201d24eb48STom Herbert 
1121bf264145STom Herbert #ifdef CONFIG_RPS
1122bf264145STom Herbert 	real_rx = net->real_num_rx_queues;
1123bf264145STom Herbert #endif
1124bf264145STom Herbert 	real_tx = net->real_num_tx_queues;
1125bf264145STom Herbert 
1126bf264145STom Herbert 	error = net_rx_queue_update_kobjects(net, 0, real_rx);
11271d24eb48STom Herbert 	if (error)
11281d24eb48STom Herbert 		goto error;
1129bf264145STom Herbert 	rxq = real_rx;
11301d24eb48STom Herbert 
1131bf264145STom Herbert 	error = netdev_queue_update_kobjects(net, 0, real_tx);
11321d24eb48STom Herbert 	if (error)
11331d24eb48STom Herbert 		goto error;
1134bf264145STom Herbert 	txq = real_tx;
11351d24eb48STom Herbert 
11361d24eb48STom Herbert 	return 0;
11371d24eb48STom Herbert 
11381d24eb48STom Herbert error:
11391d24eb48STom Herbert 	netdev_queue_update_kobjects(net, txq, 0);
11401d24eb48STom Herbert 	net_rx_queue_update_kobjects(net, rxq, 0);
11411d24eb48STom Herbert 	return error;
114262fe0b40SBen Hutchings }
114362fe0b40SBen Hutchings 
11441d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net)
11450a9627f2STom Herbert {
1146bf264145STom Herbert 	int real_rx = 0, real_tx = 0;
1147bf264145STom Herbert 
1148bf264145STom Herbert #ifdef CONFIG_RPS
1149bf264145STom Herbert 	real_rx = net->real_num_rx_queues;
1150bf264145STom Herbert #endif
1151bf264145STom Herbert 	real_tx = net->real_num_tx_queues;
1152bf264145STom Herbert 
1153bf264145STom Herbert 	net_rx_queue_update_kobjects(net, real_rx, 0);
1154bf264145STom Herbert 	netdev_queue_update_kobjects(net, real_tx, 0);
1155ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
11560a9627f2STom Herbert 	kset_unregister(net->queues_kset);
1157bf264145STom Herbert #endif
11580a9627f2STom Herbert }
1159608b4b95SEric W. Biederman 
1160a685e089SAl Viro static void *net_grab_current_ns(void)
1161608b4b95SEric W. Biederman {
1162a685e089SAl Viro 	struct net *ns = current->nsproxy->net_ns;
1163a685e089SAl Viro #ifdef CONFIG_NET_NS
1164a685e089SAl Viro 	if (ns)
1165a685e089SAl Viro 		atomic_inc(&ns->passive);
1166a685e089SAl Viro #endif
1167a685e089SAl Viro 	return ns;
1168608b4b95SEric W. Biederman }
1169608b4b95SEric W. Biederman 
1170608b4b95SEric W. Biederman static const void *net_initial_ns(void)
1171608b4b95SEric W. Biederman {
1172608b4b95SEric W. Biederman 	return &init_net;
1173608b4b95SEric W. Biederman }
1174608b4b95SEric W. Biederman 
1175608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk)
1176608b4b95SEric W. Biederman {
1177608b4b95SEric W. Biederman 	return sock_net(sk);
1178608b4b95SEric W. Biederman }
1179608b4b95SEric W. Biederman 
118004600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = {
1181608b4b95SEric W. Biederman 	.type = KOBJ_NS_TYPE_NET,
1182a685e089SAl Viro 	.grab_current_ns = net_grab_current_ns,
1183608b4b95SEric W. Biederman 	.netlink_ns = net_netlink_ns,
1184608b4b95SEric W. Biederman 	.initial_ns = net_initial_ns,
1185a685e089SAl Viro 	.drop_ns = net_drop_ns,
1186608b4b95SEric W. Biederman };
118704600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations);
1188608b4b95SEric W. Biederman 
11897eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
11901da177e4SLinus Torvalds {
119143cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
11927eff2e7aSKay Sievers 	int retval;
11931da177e4SLinus Torvalds 
1194312c004dSKay Sievers 	/* pass interface to uevent. */
11957eff2e7aSKay Sievers 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1196bf62456eSEric Rannaud 	if (retval)
1197bf62456eSEric Rannaud 		goto exit;
11981da177e4SLinus Torvalds 
1199ca2f37dbSJean Tourrilhes 	/* pass ifindex to uevent.
1200ca2f37dbSJean Tourrilhes 	 * ifindex is useful as it won't change (interface name may change)
1201ca2f37dbSJean Tourrilhes 	 * and is what RtNetlink uses natively. */
12027eff2e7aSKay Sievers 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1203ca2f37dbSJean Tourrilhes 
1204bf62456eSEric Rannaud exit:
1205bf62456eSEric Rannaud 	return retval;
12061da177e4SLinus Torvalds }
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds /*
12091da177e4SLinus Torvalds  *	netdev_release -- destroy and free a dead device.
121043cb76d9SGreg Kroah-Hartman  *	Called when last reference to device kobject is gone.
12111da177e4SLinus Torvalds  */
121243cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d)
12131da177e4SLinus Torvalds {
121443cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	BUG_ON(dev->reg_state != NETREG_RELEASED);
12171da177e4SLinus Torvalds 
12180b815a1aSStephen Hemminger 	kfree(dev->ifalias);
12191da177e4SLinus Torvalds 	kfree((char *)dev - dev->padded);
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
1222608b4b95SEric W. Biederman static const void *net_namespace(struct device *d)
1223608b4b95SEric W. Biederman {
1224608b4b95SEric W. Biederman 	struct net_device *dev;
1225608b4b95SEric W. Biederman 	dev = container_of(d, struct net_device, dev);
1226608b4b95SEric W. Biederman 	return dev_net(dev);
1227608b4b95SEric W. Biederman }
1228608b4b95SEric W. Biederman 
12291da177e4SLinus Torvalds static struct class net_class = {
12301da177e4SLinus Torvalds 	.name = "net",
123143cb76d9SGreg Kroah-Hartman 	.dev_release = netdev_release,
12328b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
123343cb76d9SGreg Kroah-Hartman 	.dev_attrs = net_class_attributes,
12348b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
123543cb76d9SGreg Kroah-Hartman 	.dev_uevent = netdev_uevent,
1236608b4b95SEric W. Biederman 	.ns_type = &net_ns_type_operations,
1237608b4b95SEric W. Biederman 	.namespace = net_namespace,
12381da177e4SLinus Torvalds };
12391da177e4SLinus Torvalds 
12409093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all
12419093bbb2SStephen Hemminger  * netdev references are gone.
12429093bbb2SStephen Hemminger  */
12438b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net)
12441da177e4SLinus Torvalds {
12459093bbb2SStephen Hemminger 	struct device *dev = &(net->dev);
12469093bbb2SStephen Hemminger 
12479093bbb2SStephen Hemminger 	kobject_get(&dev->kobj);
12483891845eSEric W. Biederman 
12491d24eb48STom Herbert 	remove_queue_kobjects(net);
12500a9627f2STom Herbert 
12519802c8e2SMing Lei 	pm_runtime_set_memalloc_noio(dev, false);
12529802c8e2SMing Lei 
12539093bbb2SStephen Hemminger 	device_del(dev);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /* Create sysfs entries for network device. */
12578b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net)
12581da177e4SLinus Torvalds {
125943cb76d9SGreg Kroah-Hartman 	struct device *dev = &(net->dev);
1260a4dbd674SDavid Brownell 	const struct attribute_group **groups = net->sysfs_groups;
12610a9627f2STom Herbert 	int error = 0;
12621da177e4SLinus Torvalds 
1263a1b3f594SEric W. Biederman 	device_initialize(dev);
126443cb76d9SGreg Kroah-Hartman 	dev->class = &net_class;
126543cb76d9SGreg Kroah-Hartman 	dev->platform_data = net;
126643cb76d9SGreg Kroah-Hartman 	dev->groups = groups;
12671da177e4SLinus Torvalds 
1268a2205472SStephen Hemminger 	dev_set_name(dev, "%s", net->name);
12691da177e4SLinus Torvalds 
12708b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
12710c509a6cSEric W. Biederman 	/* Allow for a device specific group */
12720c509a6cSEric W. Biederman 	if (*groups)
12730c509a6cSEric W. Biederman 		groups++;
12741da177e4SLinus Torvalds 
12750c509a6cSEric W. Biederman 	*groups++ = &netstat_group;
127638c1a01cSJohannes Berg 
127738c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
127838c1a01cSJohannes Berg 	if (net->ieee80211_ptr)
127938c1a01cSJohannes Berg 		*groups++ = &wireless_group;
128038c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT)
128138c1a01cSJohannes Berg 	else if (net->wireless_handlers)
128238c1a01cSJohannes Berg 		*groups++ = &wireless_group;
128338c1a01cSJohannes Berg #endif
128438c1a01cSJohannes Berg #endif
12858b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
12861da177e4SLinus Torvalds 
12870a9627f2STom Herbert 	error = device_add(dev);
12880a9627f2STom Herbert 	if (error)
12890a9627f2STom Herbert 		return error;
12900a9627f2STom Herbert 
12911d24eb48STom Herbert 	error = register_queue_kobjects(net);
12920a9627f2STom Herbert 	if (error) {
12930a9627f2STom Herbert 		device_del(dev);
12940a9627f2STom Herbert 		return error;
12950a9627f2STom Herbert 	}
12960a9627f2STom Herbert 
12979802c8e2SMing Lei 	pm_runtime_set_memalloc_noio(dev, true);
12989802c8e2SMing Lei 
12990a9627f2STom Herbert 	return error;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
1302b8a9787eSJay Vosburgh int netdev_class_create_file(struct class_attribute *class_attr)
1303b8a9787eSJay Vosburgh {
1304b8a9787eSJay Vosburgh 	return class_create_file(&net_class, class_attr);
1305b8a9787eSJay Vosburgh }
13069e34a5b5SEric Dumazet EXPORT_SYMBOL(netdev_class_create_file);
1307b8a9787eSJay Vosburgh 
1308b8a9787eSJay Vosburgh void netdev_class_remove_file(struct class_attribute *class_attr)
1309b8a9787eSJay Vosburgh {
1310b8a9787eSJay Vosburgh 	class_remove_file(&net_class, class_attr);
1311b8a9787eSJay Vosburgh }
1312b8a9787eSJay Vosburgh EXPORT_SYMBOL(netdev_class_remove_file);
1313b8a9787eSJay Vosburgh 
13148b41d188SEric W. Biederman int netdev_kobject_init(void)
13151da177e4SLinus Torvalds {
1316608b4b95SEric W. Biederman 	kobj_ns_type_register(&net_ns_type_operations);
13171da177e4SLinus Torvalds 	return class_register(&net_class);
13181da177e4SLinus Torvalds }
1319