xref: /openbmc/linux/net/core/net-sysfs.c (revision ff80e519)
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 
337ff80e519SJiri Pirko static ssize_t show_phys_port_id(struct device *dev,
338ff80e519SJiri Pirko 				 struct device_attribute *attr, char *buf)
339ff80e519SJiri Pirko {
340ff80e519SJiri Pirko 	struct net_device *netdev = to_net_dev(dev);
341ff80e519SJiri Pirko 	ssize_t ret = -EINVAL;
342ff80e519SJiri Pirko 
343ff80e519SJiri Pirko 	if (!rtnl_trylock())
344ff80e519SJiri Pirko 		return restart_syscall();
345ff80e519SJiri Pirko 
346ff80e519SJiri Pirko 	if (dev_isalive(netdev)) {
347ff80e519SJiri Pirko 		struct netdev_phys_port_id ppid;
348ff80e519SJiri Pirko 
349ff80e519SJiri Pirko 		ret = dev_get_phys_port_id(netdev, &ppid);
350ff80e519SJiri Pirko 		if (!ret)
351ff80e519SJiri Pirko 			ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
352ff80e519SJiri Pirko 	}
353ff80e519SJiri Pirko 	rtnl_unlock();
354ff80e519SJiri Pirko 
355ff80e519SJiri Pirko 	return ret;
356ff80e519SJiri Pirko }
357ff80e519SJiri Pirko 
35843cb76d9SGreg Kroah-Hartman static struct device_attribute net_class_attributes[] = {
359c1f79426SStefan Assmann 	__ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
360fd586bacSKay Sievers 	__ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
3619d29672cSDavid Woodhouse 	__ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
3620b815a1aSStephen Hemminger 	__ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
363fd586bacSKay Sievers 	__ATTR(iflink, S_IRUGO, show_iflink, NULL),
364fd586bacSKay Sievers 	__ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
365fd586bacSKay Sievers 	__ATTR(type, S_IRUGO, show_type, NULL),
366b00055aaSStefan Rompf 	__ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
367fd586bacSKay Sievers 	__ATTR(address, S_IRUGO, show_address, NULL),
368fd586bacSKay Sievers 	__ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
369fdae0fdeSJiri Pirko 	__ATTR(carrier, S_IRUGO | S_IWUSR, show_carrier, store_carrier),
370d519e17eSAndy Gospodarek 	__ATTR(speed, S_IRUGO, show_speed, NULL),
371d519e17eSAndy Gospodarek 	__ATTR(duplex, S_IRUGO, show_duplex, NULL),
372b00055aaSStefan Rompf 	__ATTR(dormant, S_IRUGO, show_dormant, NULL),
373b00055aaSStefan Rompf 	__ATTR(operstate, S_IRUGO, show_operstate, NULL),
374fd586bacSKay Sievers 	__ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
375fd586bacSKay Sievers 	__ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
376fd586bacSKay Sievers 	__ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
377fd586bacSKay Sievers 	       store_tx_queue_len),
378b6644cb7SXiaotian Feng 	__ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
379ff80e519SJiri Pirko 	__ATTR(phys_port_id, S_IRUGO, show_phys_port_id, NULL),
380fd586bacSKay Sievers 	{}
3811da177e4SLinus Torvalds };
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* Show a given an attribute in the statistics group */
38443cb76d9SGreg Kroah-Hartman static ssize_t netstat_show(const struct device *d,
38543cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr, char *buf,
3861da177e4SLinus Torvalds 			    unsigned long offset)
3871da177e4SLinus Torvalds {
38843cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
3891da177e4SLinus Torvalds 	ssize_t ret = -EINVAL;
3901da177e4SLinus Torvalds 
391be1f3c2cSBen Hutchings 	WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
392be1f3c2cSBen Hutchings 			offset % sizeof(u64) != 0);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
39596e74088SPavel Emelyanov 	if (dev_isalive(dev)) {
39628172739SEric Dumazet 		struct rtnl_link_stats64 temp;
39728172739SEric Dumazet 		const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
39828172739SEric Dumazet 
399be1f3c2cSBen Hutchings 		ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
40096e74088SPavel Emelyanov 	}
4011da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4021da177e4SLinus Torvalds 	return ret;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /* generate a read-only statistics attribute */
4061da177e4SLinus Torvalds #define NETSTAT_ENTRY(name)						\
40743cb76d9SGreg Kroah-Hartman static ssize_t show_##name(struct device *d,				\
40843cb76d9SGreg Kroah-Hartman 			   struct device_attribute *attr, char *buf) 	\
4091da177e4SLinus Torvalds {									\
41043cb76d9SGreg Kroah-Hartman 	return netstat_show(d, attr, buf,				\
411be1f3c2cSBen Hutchings 			    offsetof(struct rtnl_link_stats64, name));	\
4121da177e4SLinus Torvalds }									\
41343cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds NETSTAT_ENTRY(rx_packets);
4161da177e4SLinus Torvalds NETSTAT_ENTRY(tx_packets);
4171da177e4SLinus Torvalds NETSTAT_ENTRY(rx_bytes);
4181da177e4SLinus Torvalds NETSTAT_ENTRY(tx_bytes);
4191da177e4SLinus Torvalds NETSTAT_ENTRY(rx_errors);
4201da177e4SLinus Torvalds NETSTAT_ENTRY(tx_errors);
4211da177e4SLinus Torvalds NETSTAT_ENTRY(rx_dropped);
4221da177e4SLinus Torvalds NETSTAT_ENTRY(tx_dropped);
4231da177e4SLinus Torvalds NETSTAT_ENTRY(multicast);
4241da177e4SLinus Torvalds NETSTAT_ENTRY(collisions);
4251da177e4SLinus Torvalds NETSTAT_ENTRY(rx_length_errors);
4261da177e4SLinus Torvalds NETSTAT_ENTRY(rx_over_errors);
4271da177e4SLinus Torvalds NETSTAT_ENTRY(rx_crc_errors);
4281da177e4SLinus Torvalds NETSTAT_ENTRY(rx_frame_errors);
4291da177e4SLinus Torvalds NETSTAT_ENTRY(rx_fifo_errors);
4301da177e4SLinus Torvalds NETSTAT_ENTRY(rx_missed_errors);
4311da177e4SLinus Torvalds NETSTAT_ENTRY(tx_aborted_errors);
4321da177e4SLinus Torvalds NETSTAT_ENTRY(tx_carrier_errors);
4331da177e4SLinus Torvalds NETSTAT_ENTRY(tx_fifo_errors);
4341da177e4SLinus Torvalds NETSTAT_ENTRY(tx_heartbeat_errors);
4351da177e4SLinus Torvalds NETSTAT_ENTRY(tx_window_errors);
4361da177e4SLinus Torvalds NETSTAT_ENTRY(rx_compressed);
4371da177e4SLinus Torvalds NETSTAT_ENTRY(tx_compressed);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static struct attribute *netstat_attrs[] = {
44043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_packets.attr,
44143cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_packets.attr,
44243cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_bytes.attr,
44343cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_bytes.attr,
44443cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_errors.attr,
44543cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_errors.attr,
44643cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_dropped.attr,
44743cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_dropped.attr,
44843cb76d9SGreg Kroah-Hartman 	&dev_attr_multicast.attr,
44943cb76d9SGreg Kroah-Hartman 	&dev_attr_collisions.attr,
45043cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_length_errors.attr,
45143cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_over_errors.attr,
45243cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_crc_errors.attr,
45343cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_frame_errors.attr,
45443cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_fifo_errors.attr,
45543cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_missed_errors.attr,
45643cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_aborted_errors.attr,
45743cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_carrier_errors.attr,
45843cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_fifo_errors.attr,
45943cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_heartbeat_errors.attr,
46043cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_window_errors.attr,
46143cb76d9SGreg Kroah-Hartman 	&dev_attr_rx_compressed.attr,
46243cb76d9SGreg Kroah-Hartman 	&dev_attr_tx_compressed.attr,
4631da177e4SLinus Torvalds 	NULL
4641da177e4SLinus Torvalds };
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static struct attribute_group netstat_group = {
4681da177e4SLinus Torvalds 	.name  = "statistics",
4691da177e4SLinus Torvalds 	.attrs  = netstat_attrs,
4701da177e4SLinus Torvalds };
47138c1a01cSJohannes Berg 
47238c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
47338c1a01cSJohannes Berg static struct attribute *wireless_attrs[] = {
47438c1a01cSJohannes Berg 	NULL
47538c1a01cSJohannes Berg };
47638c1a01cSJohannes Berg 
47738c1a01cSJohannes Berg static struct attribute_group wireless_group = {
47838c1a01cSJohannes Berg 	.name = "wireless",
47938c1a01cSJohannes Berg 	.attrs = wireless_attrs,
48038c1a01cSJohannes Berg };
48138c1a01cSJohannes Berg #endif
482d6523ddfSEric W. Biederman #endif /* CONFIG_SYSFS */
4831da177e4SLinus Torvalds 
48430bde1f5SStephen Rothwell #ifdef CONFIG_RPS
4850a9627f2STom Herbert /*
4860a9627f2STom Herbert  * RX queue sysfs structures and functions.
4870a9627f2STom Herbert  */
4880a9627f2STom Herbert struct rx_queue_attribute {
4890a9627f2STom Herbert 	struct attribute attr;
4900a9627f2STom Herbert 	ssize_t (*show)(struct netdev_rx_queue *queue,
4910a9627f2STom Herbert 	    struct rx_queue_attribute *attr, char *buf);
4920a9627f2STom Herbert 	ssize_t (*store)(struct netdev_rx_queue *queue,
4930a9627f2STom Herbert 	    struct rx_queue_attribute *attr, const char *buf, size_t len);
4940a9627f2STom Herbert };
4950a9627f2STom Herbert #define to_rx_queue_attr(_attr) container_of(_attr,		\
4960a9627f2STom Herbert     struct rx_queue_attribute, attr)
4970a9627f2STom Herbert 
4980a9627f2STom Herbert #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
4990a9627f2STom Herbert 
5000a9627f2STom Herbert static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
5010a9627f2STom Herbert 				  char *buf)
5020a9627f2STom Herbert {
5030a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
5040a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
5050a9627f2STom Herbert 
5060a9627f2STom Herbert 	if (!attribute->show)
5070a9627f2STom Herbert 		return -EIO;
5080a9627f2STom Herbert 
5090a9627f2STom Herbert 	return attribute->show(queue, attribute, buf);
5100a9627f2STom Herbert }
5110a9627f2STom Herbert 
5120a9627f2STom Herbert static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
5130a9627f2STom Herbert 				   const char *buf, size_t count)
5140a9627f2STom Herbert {
5150a9627f2STom Herbert 	struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
5160a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
5170a9627f2STom Herbert 
5180a9627f2STom Herbert 	if (!attribute->store)
5190a9627f2STom Herbert 		return -EIO;
5200a9627f2STom Herbert 
5210a9627f2STom Herbert 	return attribute->store(queue, attribute, buf, count);
5220a9627f2STom Herbert }
5230a9627f2STom Herbert 
524fa50d645Sstephen hemminger static const struct sysfs_ops rx_queue_sysfs_ops = {
5250a9627f2STom Herbert 	.show = rx_queue_attr_show,
5260a9627f2STom Herbert 	.store = rx_queue_attr_store,
5270a9627f2STom Herbert };
5280a9627f2STom Herbert 
5290a9627f2STom Herbert static ssize_t show_rps_map(struct netdev_rx_queue *queue,
5300a9627f2STom Herbert 			    struct rx_queue_attribute *attribute, char *buf)
5310a9627f2STom Herbert {
5320a9627f2STom Herbert 	struct rps_map *map;
5330a9627f2STom Herbert 	cpumask_var_t mask;
5340a9627f2STom Herbert 	size_t len = 0;
5350a9627f2STom Herbert 	int i;
5360a9627f2STom Herbert 
5370a9627f2STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
5380a9627f2STom Herbert 		return -ENOMEM;
5390a9627f2STom Herbert 
5400a9627f2STom Herbert 	rcu_read_lock();
5410a9627f2STom Herbert 	map = rcu_dereference(queue->rps_map);
5420a9627f2STom Herbert 	if (map)
5430a9627f2STom Herbert 		for (i = 0; i < map->len; i++)
5440a9627f2STom Herbert 			cpumask_set_cpu(map->cpus[i], mask);
5450a9627f2STom Herbert 
5460a9627f2STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
5470a9627f2STom Herbert 	if (PAGE_SIZE - len < 3) {
5480a9627f2STom Herbert 		rcu_read_unlock();
5490a9627f2STom Herbert 		free_cpumask_var(mask);
5500a9627f2STom Herbert 		return -EINVAL;
5510a9627f2STom Herbert 	}
5520a9627f2STom Herbert 	rcu_read_unlock();
5530a9627f2STom Herbert 
5540a9627f2STom Herbert 	free_cpumask_var(mask);
5550a9627f2STom Herbert 	len += sprintf(buf + len, "\n");
5560a9627f2STom Herbert 	return len;
5570a9627f2STom Herbert }
5580a9627f2STom Herbert 
559f5acb907SEric Dumazet static ssize_t store_rps_map(struct netdev_rx_queue *queue,
5600a9627f2STom Herbert 		      struct rx_queue_attribute *attribute,
5610a9627f2STom Herbert 		      const char *buf, size_t len)
5620a9627f2STom Herbert {
5630a9627f2STom Herbert 	struct rps_map *old_map, *map;
5640a9627f2STom Herbert 	cpumask_var_t mask;
5650a9627f2STom Herbert 	int err, cpu, i;
5660a9627f2STom Herbert 	static DEFINE_SPINLOCK(rps_map_lock);
5670a9627f2STom Herbert 
5680a9627f2STom Herbert 	if (!capable(CAP_NET_ADMIN))
5690a9627f2STom Herbert 		return -EPERM;
5700a9627f2STom Herbert 
5710a9627f2STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5720a9627f2STom Herbert 		return -ENOMEM;
5730a9627f2STom Herbert 
5740a9627f2STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
5750a9627f2STom Herbert 	if (err) {
5760a9627f2STom Herbert 		free_cpumask_var(mask);
5770a9627f2STom Herbert 		return err;
5780a9627f2STom Herbert 	}
5790a9627f2STom Herbert 
58095c96174SEric Dumazet 	map = kzalloc(max_t(unsigned int,
5810a9627f2STom Herbert 	    RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
5820a9627f2STom Herbert 	    GFP_KERNEL);
5830a9627f2STom Herbert 	if (!map) {
5840a9627f2STom Herbert 		free_cpumask_var(mask);
5850a9627f2STom Herbert 		return -ENOMEM;
5860a9627f2STom Herbert 	}
5870a9627f2STom Herbert 
5880a9627f2STom Herbert 	i = 0;
5890a9627f2STom Herbert 	for_each_cpu_and(cpu, mask, cpu_online_mask)
5900a9627f2STom Herbert 		map->cpus[i++] = cpu;
5910a9627f2STom Herbert 
5920a9627f2STom Herbert 	if (i)
5930a9627f2STom Herbert 		map->len = i;
5940a9627f2STom Herbert 	else {
5950a9627f2STom Herbert 		kfree(map);
5960a9627f2STom Herbert 		map = NULL;
5970a9627f2STom Herbert 	}
5980a9627f2STom Herbert 
5990a9627f2STom Herbert 	spin_lock(&rps_map_lock);
6006e3f7fafSEric Dumazet 	old_map = rcu_dereference_protected(queue->rps_map,
6016e3f7fafSEric Dumazet 					    lockdep_is_held(&rps_map_lock));
6020a9627f2STom Herbert 	rcu_assign_pointer(queue->rps_map, map);
6030a9627f2STom Herbert 	spin_unlock(&rps_map_lock);
6040a9627f2STom Herbert 
605adc9300eSEric Dumazet 	if (map)
606c5905afbSIngo Molnar 		static_key_slow_inc(&rps_needed);
607adc9300eSEric Dumazet 	if (old_map) {
608f6f80238SLai Jiangshan 		kfree_rcu(old_map, rcu);
609c5905afbSIngo Molnar 		static_key_slow_dec(&rps_needed);
610adc9300eSEric Dumazet 	}
6110a9627f2STom Herbert 	free_cpumask_var(mask);
6120a9627f2STom Herbert 	return len;
6130a9627f2STom Herbert }
6140a9627f2STom Herbert 
615fec5e652STom Herbert static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
616fec5e652STom Herbert 					   struct rx_queue_attribute *attr,
617fec5e652STom Herbert 					   char *buf)
618fec5e652STom Herbert {
619fec5e652STom Herbert 	struct rps_dev_flow_table *flow_table;
62060b778ceSEric Dumazet 	unsigned long val = 0;
621fec5e652STom Herbert 
622fec5e652STom Herbert 	rcu_read_lock();
623fec5e652STom Herbert 	flow_table = rcu_dereference(queue->rps_flow_table);
624fec5e652STom Herbert 	if (flow_table)
62560b778ceSEric Dumazet 		val = (unsigned long)flow_table->mask + 1;
626fec5e652STom Herbert 	rcu_read_unlock();
627fec5e652STom Herbert 
62860b778ceSEric Dumazet 	return sprintf(buf, "%lu\n", val);
629fec5e652STom Herbert }
630fec5e652STom Herbert 
631fec5e652STom Herbert static void rps_dev_flow_table_release(struct rcu_head *rcu)
632fec5e652STom Herbert {
633fec5e652STom Herbert 	struct rps_dev_flow_table *table = container_of(rcu,
634fec5e652STom Herbert 	    struct rps_dev_flow_table, rcu);
635243198d0SAl Viro 	vfree(table);
636fec5e652STom Herbert }
637fec5e652STom Herbert 
638f5acb907SEric Dumazet static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
639fec5e652STom Herbert 				     struct rx_queue_attribute *attr,
640fec5e652STom Herbert 				     const char *buf, size_t len)
641fec5e652STom Herbert {
64260b778ceSEric Dumazet 	unsigned long mask, count;
643fec5e652STom Herbert 	struct rps_dev_flow_table *table, *old_table;
644fec5e652STom Herbert 	static DEFINE_SPINLOCK(rps_dev_flow_lock);
64560b778ceSEric Dumazet 	int rc;
646fec5e652STom Herbert 
647fec5e652STom Herbert 	if (!capable(CAP_NET_ADMIN))
648fec5e652STom Herbert 		return -EPERM;
649fec5e652STom Herbert 
65060b778ceSEric Dumazet 	rc = kstrtoul(buf, 0, &count);
65160b778ceSEric Dumazet 	if (rc < 0)
65260b778ceSEric Dumazet 		return rc;
653fec5e652STom Herbert 
654fec5e652STom Herbert 	if (count) {
65560b778ceSEric Dumazet 		mask = count - 1;
65660b778ceSEric Dumazet 		/* mask = roundup_pow_of_two(count) - 1;
65760b778ceSEric Dumazet 		 * without overflows...
65860b778ceSEric Dumazet 		 */
65960b778ceSEric Dumazet 		while ((mask | (mask >> 1)) != mask)
66060b778ceSEric Dumazet 			mask |= (mask >> 1);
66160b778ceSEric Dumazet 		/* On 64 bit arches, must check mask fits in table->mask (u32),
66260b778ceSEric Dumazet 		 * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
66360b778ceSEric Dumazet 		 * doesnt overflow.
66460b778ceSEric Dumazet 		 */
66560b778ceSEric Dumazet #if BITS_PER_LONG > 32
66660b778ceSEric Dumazet 		if (mask > (unsigned long)(u32)mask)
667a0a129f8SXi Wang 			return -EINVAL;
66860b778ceSEric Dumazet #else
66960b778ceSEric Dumazet 		if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
670a0a129f8SXi Wang 				/ sizeof(struct rps_dev_flow)) {
671fec5e652STom Herbert 			/* Enforce a limit to prevent overflow */
672fec5e652STom Herbert 			return -EINVAL;
673fec5e652STom Herbert 		}
67460b778ceSEric Dumazet #endif
67560b778ceSEric Dumazet 		table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
676fec5e652STom Herbert 		if (!table)
677fec5e652STom Herbert 			return -ENOMEM;
678fec5e652STom Herbert 
67960b778ceSEric Dumazet 		table->mask = mask;
68060b778ceSEric Dumazet 		for (count = 0; count <= mask; count++)
68160b778ceSEric Dumazet 			table->flows[count].cpu = RPS_NO_CPU;
682fec5e652STom Herbert 	} else
683fec5e652STom Herbert 		table = NULL;
684fec5e652STom Herbert 
685fec5e652STom Herbert 	spin_lock(&rps_dev_flow_lock);
6866e3f7fafSEric Dumazet 	old_table = rcu_dereference_protected(queue->rps_flow_table,
6876e3f7fafSEric Dumazet 					      lockdep_is_held(&rps_dev_flow_lock));
688fec5e652STom Herbert 	rcu_assign_pointer(queue->rps_flow_table, table);
689fec5e652STom Herbert 	spin_unlock(&rps_dev_flow_lock);
690fec5e652STom Herbert 
691fec5e652STom Herbert 	if (old_table)
692fec5e652STom Herbert 		call_rcu(&old_table->rcu, rps_dev_flow_table_release);
693fec5e652STom Herbert 
694fec5e652STom Herbert 	return len;
695fec5e652STom Herbert }
696fec5e652STom Herbert 
6970a9627f2STom Herbert static struct rx_queue_attribute rps_cpus_attribute =
6980a9627f2STom Herbert 	__ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
6990a9627f2STom Herbert 
700fec5e652STom Herbert 
701fec5e652STom Herbert static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
702fec5e652STom Herbert 	__ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
703fec5e652STom Herbert 	    show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
704fec5e652STom Herbert 
7050a9627f2STom Herbert static struct attribute *rx_queue_default_attrs[] = {
7060a9627f2STom Herbert 	&rps_cpus_attribute.attr,
707fec5e652STom Herbert 	&rps_dev_flow_table_cnt_attribute.attr,
7080a9627f2STom Herbert 	NULL
7090a9627f2STom Herbert };
7100a9627f2STom Herbert 
7110a9627f2STom Herbert static void rx_queue_release(struct kobject *kobj)
7120a9627f2STom Herbert {
7130a9627f2STom Herbert 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
7146e3f7fafSEric Dumazet 	struct rps_map *map;
7156e3f7fafSEric Dumazet 	struct rps_dev_flow_table *flow_table;
7160a9627f2STom Herbert 
717fec5e652STom Herbert 
71833d480ceSEric Dumazet 	map = rcu_dereference_protected(queue->rps_map, 1);
7199ea19481SJohn Fastabend 	if (map) {
7209ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_map, NULL);
721f6f80238SLai Jiangshan 		kfree_rcu(map, rcu);
7229ea19481SJohn Fastabend 	}
7236e3f7fafSEric Dumazet 
72433d480ceSEric Dumazet 	flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
7259ea19481SJohn Fastabend 	if (flow_table) {
7269ea19481SJohn Fastabend 		RCU_INIT_POINTER(queue->rps_flow_table, NULL);
7276e3f7fafSEric Dumazet 		call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
7289ea19481SJohn Fastabend 	}
7290a9627f2STom Herbert 
7309ea19481SJohn Fastabend 	memset(kobj, 0, sizeof(*kobj));
731fe822240STom Herbert 	dev_put(queue->dev);
7320a9627f2STom Herbert }
7330a9627f2STom Herbert 
7340a9627f2STom Herbert static struct kobj_type rx_queue_ktype = {
7350a9627f2STom Herbert 	.sysfs_ops = &rx_queue_sysfs_ops,
7360a9627f2STom Herbert 	.release = rx_queue_release,
7370a9627f2STom Herbert 	.default_attrs = rx_queue_default_attrs,
7380a9627f2STom Herbert };
7390a9627f2STom Herbert 
7400a9627f2STom Herbert static int rx_queue_add_kobject(struct net_device *net, int index)
7410a9627f2STom Herbert {
7420a9627f2STom Herbert 	struct netdev_rx_queue *queue = net->_rx + index;
7430a9627f2STom Herbert 	struct kobject *kobj = &queue->kobj;
7440a9627f2STom Herbert 	int error = 0;
7450a9627f2STom Herbert 
7460a9627f2STom Herbert 	kobj->kset = net->queues_kset;
7470a9627f2STom Herbert 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
7480a9627f2STom Herbert 	    "rx-%u", index);
7490a9627f2STom Herbert 	if (error) {
7500a9627f2STom Herbert 		kobject_put(kobj);
7510a9627f2STom Herbert 		return error;
7520a9627f2STom Herbert 	}
7530a9627f2STom Herbert 
7540a9627f2STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
755fe822240STom Herbert 	dev_hold(queue->dev);
7560a9627f2STom Herbert 
7570a9627f2STom Herbert 	return error;
7580a9627f2STom Herbert }
759bf264145STom Herbert #endif /* CONFIG_RPS */
7600a9627f2STom Herbert 
76162fe0b40SBen Hutchings int
76262fe0b40SBen Hutchings net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
7630a9627f2STom Herbert {
764bf264145STom Herbert #ifdef CONFIG_RPS
7650a9627f2STom Herbert 	int i;
7660a9627f2STom Herbert 	int error = 0;
7670a9627f2STom Herbert 
76862fe0b40SBen Hutchings 	for (i = old_num; i < new_num; i++) {
7690a9627f2STom Herbert 		error = rx_queue_add_kobject(net, i);
77062fe0b40SBen Hutchings 		if (error) {
77162fe0b40SBen Hutchings 			new_num = old_num;
7720a9627f2STom Herbert 			break;
7730a9627f2STom Herbert 		}
77462fe0b40SBen Hutchings 	}
7750a9627f2STom Herbert 
77662fe0b40SBen Hutchings 	while (--i >= new_num)
7770a9627f2STom Herbert 		kobject_put(&net->_rx[i].kobj);
7780a9627f2STom Herbert 
7790a9627f2STom Herbert 	return error;
780bf264145STom Herbert #else
781bf264145STom Herbert 	return 0;
782bf264145STom Herbert #endif
7830a9627f2STom Herbert }
7840a9627f2STom Herbert 
785ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
7861d24eb48STom Herbert /*
7871d24eb48STom Herbert  * netdev_queue sysfs structures and functions.
7881d24eb48STom Herbert  */
7891d24eb48STom Herbert struct netdev_queue_attribute {
7901d24eb48STom Herbert 	struct attribute attr;
7911d24eb48STom Herbert 	ssize_t (*show)(struct netdev_queue *queue,
7921d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, char *buf);
7931d24eb48STom Herbert 	ssize_t (*store)(struct netdev_queue *queue,
7941d24eb48STom Herbert 	    struct netdev_queue_attribute *attr, const char *buf, size_t len);
7951d24eb48STom Herbert };
7961d24eb48STom Herbert #define to_netdev_queue_attr(_attr) container_of(_attr,		\
7971d24eb48STom Herbert     struct netdev_queue_attribute, attr)
7981d24eb48STom Herbert 
7991d24eb48STom Herbert #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
8001d24eb48STom Herbert 
8011d24eb48STom Herbert static ssize_t netdev_queue_attr_show(struct kobject *kobj,
8021d24eb48STom Herbert 				      struct attribute *attr, char *buf)
80362fe0b40SBen Hutchings {
8041d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
8051d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
8061d24eb48STom Herbert 
8071d24eb48STom Herbert 	if (!attribute->show)
8081d24eb48STom Herbert 		return -EIO;
8091d24eb48STom Herbert 
8101d24eb48STom Herbert 	return attribute->show(queue, attribute, buf);
8111d24eb48STom Herbert }
8121d24eb48STom Herbert 
8131d24eb48STom Herbert static ssize_t netdev_queue_attr_store(struct kobject *kobj,
8141d24eb48STom Herbert 				       struct attribute *attr,
8151d24eb48STom Herbert 				       const char *buf, size_t count)
8161d24eb48STom Herbert {
8171d24eb48STom Herbert 	struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
8181d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
8191d24eb48STom Herbert 
8201d24eb48STom Herbert 	if (!attribute->store)
8211d24eb48STom Herbert 		return -EIO;
8221d24eb48STom Herbert 
8231d24eb48STom Herbert 	return attribute->store(queue, attribute, buf, count);
8241d24eb48STom Herbert }
8251d24eb48STom Herbert 
8261d24eb48STom Herbert static const struct sysfs_ops netdev_queue_sysfs_ops = {
8271d24eb48STom Herbert 	.show = netdev_queue_attr_show,
8281d24eb48STom Herbert 	.store = netdev_queue_attr_store,
8291d24eb48STom Herbert };
8301d24eb48STom Herbert 
831ccf5ff69Sdavid decotigny static ssize_t show_trans_timeout(struct netdev_queue *queue,
832ccf5ff69Sdavid decotigny 				  struct netdev_queue_attribute *attribute,
833ccf5ff69Sdavid decotigny 				  char *buf)
834ccf5ff69Sdavid decotigny {
835ccf5ff69Sdavid decotigny 	unsigned long trans_timeout;
836ccf5ff69Sdavid decotigny 
837ccf5ff69Sdavid decotigny 	spin_lock_irq(&queue->_xmit_lock);
838ccf5ff69Sdavid decotigny 	trans_timeout = queue->trans_timeout;
839ccf5ff69Sdavid decotigny 	spin_unlock_irq(&queue->_xmit_lock);
840ccf5ff69Sdavid decotigny 
841ccf5ff69Sdavid decotigny 	return sprintf(buf, "%lu", trans_timeout);
842ccf5ff69Sdavid decotigny }
843ccf5ff69Sdavid decotigny 
844ccf5ff69Sdavid decotigny static struct netdev_queue_attribute queue_trans_timeout =
845ccf5ff69Sdavid decotigny 	__ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
846ccf5ff69Sdavid decotigny 
847114cf580STom Herbert #ifdef CONFIG_BQL
848114cf580STom Herbert /*
849114cf580STom Herbert  * Byte queue limits sysfs structures and functions.
850114cf580STom Herbert  */
851114cf580STom Herbert static ssize_t bql_show(char *buf, unsigned int value)
852114cf580STom Herbert {
853114cf580STom Herbert 	return sprintf(buf, "%u\n", value);
854114cf580STom Herbert }
855114cf580STom Herbert 
856114cf580STom Herbert static ssize_t bql_set(const char *buf, const size_t count,
857114cf580STom Herbert 		       unsigned int *pvalue)
858114cf580STom Herbert {
859114cf580STom Herbert 	unsigned int value;
860114cf580STom Herbert 	int err;
861114cf580STom Herbert 
862114cf580STom Herbert 	if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
863114cf580STom Herbert 		value = DQL_MAX_LIMIT;
864114cf580STom Herbert 	else {
865114cf580STom Herbert 		err = kstrtouint(buf, 10, &value);
866114cf580STom Herbert 		if (err < 0)
867114cf580STom Herbert 			return err;
868114cf580STom Herbert 		if (value > DQL_MAX_LIMIT)
869114cf580STom Herbert 			return -EINVAL;
870114cf580STom Herbert 	}
871114cf580STom Herbert 
872114cf580STom Herbert 	*pvalue = value;
873114cf580STom Herbert 
874114cf580STom Herbert 	return count;
875114cf580STom Herbert }
876114cf580STom Herbert 
877114cf580STom Herbert static ssize_t bql_show_hold_time(struct netdev_queue *queue,
878114cf580STom Herbert 				  struct netdev_queue_attribute *attr,
879114cf580STom Herbert 				  char *buf)
880114cf580STom Herbert {
881114cf580STom Herbert 	struct dql *dql = &queue->dql;
882114cf580STom Herbert 
883114cf580STom Herbert 	return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
884114cf580STom Herbert }
885114cf580STom Herbert 
886114cf580STom Herbert static ssize_t bql_set_hold_time(struct netdev_queue *queue,
887114cf580STom Herbert 				 struct netdev_queue_attribute *attribute,
888114cf580STom Herbert 				 const char *buf, size_t len)
889114cf580STom Herbert {
890114cf580STom Herbert 	struct dql *dql = &queue->dql;
89195c96174SEric Dumazet 	unsigned int value;
892114cf580STom Herbert 	int err;
893114cf580STom Herbert 
894114cf580STom Herbert 	err = kstrtouint(buf, 10, &value);
895114cf580STom Herbert 	if (err < 0)
896114cf580STom Herbert 		return err;
897114cf580STom Herbert 
898114cf580STom Herbert 	dql->slack_hold_time = msecs_to_jiffies(value);
899114cf580STom Herbert 
900114cf580STom Herbert 	return len;
901114cf580STom Herbert }
902114cf580STom Herbert 
903114cf580STom Herbert static struct netdev_queue_attribute bql_hold_time_attribute =
904114cf580STom Herbert 	__ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
905114cf580STom Herbert 	    bql_set_hold_time);
906114cf580STom Herbert 
907114cf580STom Herbert static ssize_t bql_show_inflight(struct netdev_queue *queue,
908114cf580STom Herbert 				 struct netdev_queue_attribute *attr,
909114cf580STom Herbert 				 char *buf)
910114cf580STom Herbert {
911114cf580STom Herbert 	struct dql *dql = &queue->dql;
912114cf580STom Herbert 
913114cf580STom Herbert 	return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
914114cf580STom Herbert }
915114cf580STom Herbert 
916114cf580STom Herbert static struct netdev_queue_attribute bql_inflight_attribute =
917795d9a25SHiroaki SHIMODA 	__ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
918114cf580STom Herbert 
919114cf580STom Herbert #define BQL_ATTR(NAME, FIELD)						\
920114cf580STom Herbert static ssize_t bql_show_ ## NAME(struct netdev_queue *queue,		\
921114cf580STom Herbert 				 struct netdev_queue_attribute *attr,	\
922114cf580STom Herbert 				 char *buf)				\
923114cf580STom Herbert {									\
924114cf580STom Herbert 	return bql_show(buf, queue->dql.FIELD);				\
925114cf580STom Herbert }									\
926114cf580STom Herbert 									\
927114cf580STom Herbert static ssize_t bql_set_ ## NAME(struct netdev_queue *queue,		\
928114cf580STom Herbert 				struct netdev_queue_attribute *attr,	\
929114cf580STom Herbert 				const char *buf, size_t len)		\
930114cf580STom Herbert {									\
931114cf580STom Herbert 	return bql_set(buf, len, &queue->dql.FIELD);			\
932114cf580STom Herbert }									\
933114cf580STom Herbert 									\
934114cf580STom Herbert static struct netdev_queue_attribute bql_ ## NAME ## _attribute =	\
935114cf580STom Herbert 	__ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME,		\
936114cf580STom Herbert 	    bql_set_ ## NAME);
937114cf580STom Herbert 
938114cf580STom Herbert BQL_ATTR(limit, limit)
939114cf580STom Herbert BQL_ATTR(limit_max, max_limit)
940114cf580STom Herbert BQL_ATTR(limit_min, min_limit)
941114cf580STom Herbert 
942114cf580STom Herbert static struct attribute *dql_attrs[] = {
943114cf580STom Herbert 	&bql_limit_attribute.attr,
944114cf580STom Herbert 	&bql_limit_max_attribute.attr,
945114cf580STom Herbert 	&bql_limit_min_attribute.attr,
946114cf580STom Herbert 	&bql_hold_time_attribute.attr,
947114cf580STom Herbert 	&bql_inflight_attribute.attr,
948114cf580STom Herbert 	NULL
949114cf580STom Herbert };
950114cf580STom Herbert 
951114cf580STom Herbert static struct attribute_group dql_group = {
952114cf580STom Herbert 	.name  = "byte_queue_limits",
953114cf580STom Herbert 	.attrs  = dql_attrs,
954114cf580STom Herbert };
955114cf580STom Herbert #endif /* CONFIG_BQL */
956114cf580STom Herbert 
957ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
9581d24eb48STom Herbert static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
9591d24eb48STom Herbert {
9601d24eb48STom Herbert 	struct net_device *dev = queue->dev;
9611d24eb48STom Herbert 	int i;
9621d24eb48STom Herbert 
9631d24eb48STom Herbert 	for (i = 0; i < dev->num_tx_queues; i++)
9641d24eb48STom Herbert 		if (queue == &dev->_tx[i])
9651d24eb48STom Herbert 			break;
9661d24eb48STom Herbert 
9671d24eb48STom Herbert 	BUG_ON(i >= dev->num_tx_queues);
9681d24eb48STom Herbert 
9691d24eb48STom Herbert 	return i;
9701d24eb48STom Herbert }
9711d24eb48STom Herbert 
9721d24eb48STom Herbert 
9731d24eb48STom Herbert static ssize_t show_xps_map(struct netdev_queue *queue,
9741d24eb48STom Herbert 			    struct netdev_queue_attribute *attribute, char *buf)
9751d24eb48STom Herbert {
9761d24eb48STom Herbert 	struct net_device *dev = queue->dev;
9771d24eb48STom Herbert 	struct xps_dev_maps *dev_maps;
9781d24eb48STom Herbert 	cpumask_var_t mask;
9791d24eb48STom Herbert 	unsigned long index;
9801d24eb48STom Herbert 	size_t len = 0;
9811d24eb48STom Herbert 	int i;
9821d24eb48STom Herbert 
9831d24eb48STom Herbert 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
9841d24eb48STom Herbert 		return -ENOMEM;
9851d24eb48STom Herbert 
9861d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
9871d24eb48STom Herbert 
9881d24eb48STom Herbert 	rcu_read_lock();
9891d24eb48STom Herbert 	dev_maps = rcu_dereference(dev->xps_maps);
9901d24eb48STom Herbert 	if (dev_maps) {
9911d24eb48STom Herbert 		for_each_possible_cpu(i) {
9921d24eb48STom Herbert 			struct xps_map *map =
9931d24eb48STom Herbert 			    rcu_dereference(dev_maps->cpu_map[i]);
9941d24eb48STom Herbert 			if (map) {
9951d24eb48STom Herbert 				int j;
9961d24eb48STom Herbert 				for (j = 0; j < map->len; j++) {
9971d24eb48STom Herbert 					if (map->queues[j] == index) {
9981d24eb48STom Herbert 						cpumask_set_cpu(i, mask);
9991d24eb48STom Herbert 						break;
10001d24eb48STom Herbert 					}
10011d24eb48STom Herbert 				}
10021d24eb48STom Herbert 			}
10031d24eb48STom Herbert 		}
10041d24eb48STom Herbert 	}
10051d24eb48STom Herbert 	rcu_read_unlock();
10061d24eb48STom Herbert 
10071d24eb48STom Herbert 	len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
10081d24eb48STom Herbert 	if (PAGE_SIZE - len < 3) {
10091d24eb48STom Herbert 		free_cpumask_var(mask);
10101d24eb48STom Herbert 		return -EINVAL;
10111d24eb48STom Herbert 	}
10121d24eb48STom Herbert 
10131d24eb48STom Herbert 	free_cpumask_var(mask);
10141d24eb48STom Herbert 	len += sprintf(buf + len, "\n");
10151d24eb48STom Herbert 	return len;
10161d24eb48STom Herbert }
10171d24eb48STom Herbert 
10181d24eb48STom Herbert static ssize_t store_xps_map(struct netdev_queue *queue,
10191d24eb48STom Herbert 		      struct netdev_queue_attribute *attribute,
10201d24eb48STom Herbert 		      const char *buf, size_t len)
10211d24eb48STom Herbert {
10221d24eb48STom Herbert 	struct net_device *dev = queue->dev;
10231d24eb48STom Herbert 	unsigned long index;
1024537c00deSAlexander Duyck 	cpumask_var_t mask;
1025537c00deSAlexander Duyck 	int err;
10261d24eb48STom Herbert 
10271d24eb48STom Herbert 	if (!capable(CAP_NET_ADMIN))
10281d24eb48STom Herbert 		return -EPERM;
10291d24eb48STom Herbert 
10301d24eb48STom Herbert 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
10311d24eb48STom Herbert 		return -ENOMEM;
10321d24eb48STom Herbert 
10331d24eb48STom Herbert 	index = get_netdev_queue_index(queue);
10341d24eb48STom Herbert 
10351d24eb48STom Herbert 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
10361d24eb48STom Herbert 	if (err) {
10371d24eb48STom Herbert 		free_cpumask_var(mask);
10381d24eb48STom Herbert 		return err;
10391d24eb48STom Herbert 	}
10401d24eb48STom Herbert 
1041537c00deSAlexander Duyck 	err = netif_set_xps_queue(dev, mask, index);
10421d24eb48STom Herbert 
10431d24eb48STom Herbert 	free_cpumask_var(mask);
10441d24eb48STom Herbert 
1045537c00deSAlexander Duyck 	return err ? : len;
10461d24eb48STom Herbert }
10471d24eb48STom Herbert 
10481d24eb48STom Herbert static struct netdev_queue_attribute xps_cpus_attribute =
10491d24eb48STom Herbert     __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
1050ccf5ff69Sdavid decotigny #endif /* CONFIG_XPS */
10511d24eb48STom Herbert 
10521d24eb48STom Herbert static struct attribute *netdev_queue_default_attrs[] = {
1053ccf5ff69Sdavid decotigny 	&queue_trans_timeout.attr,
1054ccf5ff69Sdavid decotigny #ifdef CONFIG_XPS
10551d24eb48STom Herbert 	&xps_cpus_attribute.attr,
1056ccf5ff69Sdavid decotigny #endif
10571d24eb48STom Herbert 	NULL
10581d24eb48STom Herbert };
10591d24eb48STom Herbert 
10601d24eb48STom Herbert static void netdev_queue_release(struct kobject *kobj)
10611d24eb48STom Herbert {
10621d24eb48STom Herbert 	struct netdev_queue *queue = to_netdev_queue(kobj);
10631d24eb48STom Herbert 
10641d24eb48STom Herbert 	memset(kobj, 0, sizeof(*kobj));
10651d24eb48STom Herbert 	dev_put(queue->dev);
10661d24eb48STom Herbert }
10671d24eb48STom Herbert 
10681d24eb48STom Herbert static struct kobj_type netdev_queue_ktype = {
10691d24eb48STom Herbert 	.sysfs_ops = &netdev_queue_sysfs_ops,
10701d24eb48STom Herbert 	.release = netdev_queue_release,
10711d24eb48STom Herbert 	.default_attrs = netdev_queue_default_attrs,
10721d24eb48STom Herbert };
10731d24eb48STom Herbert 
10741d24eb48STom Herbert static int netdev_queue_add_kobject(struct net_device *net, int index)
10751d24eb48STom Herbert {
10761d24eb48STom Herbert 	struct netdev_queue *queue = net->_tx + index;
10771d24eb48STom Herbert 	struct kobject *kobj = &queue->kobj;
10781d24eb48STom Herbert 	int error = 0;
10791d24eb48STom Herbert 
10801d24eb48STom Herbert 	kobj->kset = net->queues_kset;
10811d24eb48STom Herbert 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
10821d24eb48STom Herbert 	    "tx-%u", index);
1083114cf580STom Herbert 	if (error)
1084114cf580STom Herbert 		goto exit;
1085114cf580STom Herbert 
1086114cf580STom Herbert #ifdef CONFIG_BQL
1087114cf580STom Herbert 	error = sysfs_create_group(kobj, &dql_group);
1088114cf580STom Herbert 	if (error)
1089114cf580STom Herbert 		goto exit;
1090114cf580STom Herbert #endif
10911d24eb48STom Herbert 
10921d24eb48STom Herbert 	kobject_uevent(kobj, KOBJ_ADD);
10931d24eb48STom Herbert 	dev_hold(queue->dev);
10941d24eb48STom Herbert 
1095114cf580STom Herbert 	return 0;
1096114cf580STom Herbert exit:
1097114cf580STom Herbert 	kobject_put(kobj);
10981d24eb48STom Herbert 	return error;
10991d24eb48STom Herbert }
1100ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
11011d24eb48STom Herbert 
11021d24eb48STom Herbert int
11031d24eb48STom Herbert netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
11041d24eb48STom Herbert {
1105ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
11061d24eb48STom Herbert 	int i;
11071d24eb48STom Herbert 	int error = 0;
11081d24eb48STom Herbert 
11091d24eb48STom Herbert 	for (i = old_num; i < new_num; i++) {
11101d24eb48STom Herbert 		error = netdev_queue_add_kobject(net, i);
11111d24eb48STom Herbert 		if (error) {
11121d24eb48STom Herbert 			new_num = old_num;
11131d24eb48STom Herbert 			break;
11141d24eb48STom Herbert 		}
11151d24eb48STom Herbert 	}
11161d24eb48STom Herbert 
1117114cf580STom Herbert 	while (--i >= new_num) {
1118114cf580STom Herbert 		struct netdev_queue *queue = net->_tx + i;
1119114cf580STom Herbert 
1120114cf580STom Herbert #ifdef CONFIG_BQL
1121114cf580STom Herbert 		sysfs_remove_group(&queue->kobj, &dql_group);
1122114cf580STom Herbert #endif
1123114cf580STom Herbert 		kobject_put(&queue->kobj);
1124114cf580STom Herbert 	}
11251d24eb48STom Herbert 
11261d24eb48STom Herbert 	return error;
1127bf264145STom Herbert #else
1128bf264145STom Herbert 	return 0;
1129ccf5ff69Sdavid decotigny #endif /* CONFIG_SYSFS */
11301d24eb48STom Herbert }
11311d24eb48STom Herbert 
11321d24eb48STom Herbert static int register_queue_kobjects(struct net_device *net)
11331d24eb48STom Herbert {
1134bf264145STom Herbert 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
11351d24eb48STom Herbert 
1136ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
113762fe0b40SBen Hutchings 	net->queues_kset = kset_create_and_add("queues",
113862fe0b40SBen Hutchings 	    NULL, &net->dev.kobj);
113962fe0b40SBen Hutchings 	if (!net->queues_kset)
114062fe0b40SBen Hutchings 		return -ENOMEM;
1141bf264145STom Herbert #endif
11421d24eb48STom Herbert 
1143bf264145STom Herbert #ifdef CONFIG_RPS
1144bf264145STom Herbert 	real_rx = net->real_num_rx_queues;
1145bf264145STom Herbert #endif
1146bf264145STom Herbert 	real_tx = net->real_num_tx_queues;
1147bf264145STom Herbert 
1148bf264145STom Herbert 	error = net_rx_queue_update_kobjects(net, 0, real_rx);
11491d24eb48STom Herbert 	if (error)
11501d24eb48STom Herbert 		goto error;
1151bf264145STom Herbert 	rxq = real_rx;
11521d24eb48STom Herbert 
1153bf264145STom Herbert 	error = netdev_queue_update_kobjects(net, 0, real_tx);
11541d24eb48STom Herbert 	if (error)
11551d24eb48STom Herbert 		goto error;
1156bf264145STom Herbert 	txq = real_tx;
11571d24eb48STom Herbert 
11581d24eb48STom Herbert 	return 0;
11591d24eb48STom Herbert 
11601d24eb48STom Herbert error:
11611d24eb48STom Herbert 	netdev_queue_update_kobjects(net, txq, 0);
11621d24eb48STom Herbert 	net_rx_queue_update_kobjects(net, rxq, 0);
11631d24eb48STom Herbert 	return error;
116462fe0b40SBen Hutchings }
116562fe0b40SBen Hutchings 
11661d24eb48STom Herbert static void remove_queue_kobjects(struct net_device *net)
11670a9627f2STom Herbert {
1168bf264145STom Herbert 	int real_rx = 0, real_tx = 0;
1169bf264145STom 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 	net_rx_queue_update_kobjects(net, real_rx, 0);
1176bf264145STom Herbert 	netdev_queue_update_kobjects(net, real_tx, 0);
1177ccf5ff69Sdavid decotigny #ifdef CONFIG_SYSFS
11780a9627f2STom Herbert 	kset_unregister(net->queues_kset);
1179bf264145STom Herbert #endif
11800a9627f2STom Herbert }
1181608b4b95SEric W. Biederman 
1182a685e089SAl Viro static void *net_grab_current_ns(void)
1183608b4b95SEric W. Biederman {
1184a685e089SAl Viro 	struct net *ns = current->nsproxy->net_ns;
1185a685e089SAl Viro #ifdef CONFIG_NET_NS
1186a685e089SAl Viro 	if (ns)
1187a685e089SAl Viro 		atomic_inc(&ns->passive);
1188a685e089SAl Viro #endif
1189a685e089SAl Viro 	return ns;
1190608b4b95SEric W. Biederman }
1191608b4b95SEric W. Biederman 
1192608b4b95SEric W. Biederman static const void *net_initial_ns(void)
1193608b4b95SEric W. Biederman {
1194608b4b95SEric W. Biederman 	return &init_net;
1195608b4b95SEric W. Biederman }
1196608b4b95SEric W. Biederman 
1197608b4b95SEric W. Biederman static const void *net_netlink_ns(struct sock *sk)
1198608b4b95SEric W. Biederman {
1199608b4b95SEric W. Biederman 	return sock_net(sk);
1200608b4b95SEric W. Biederman }
1201608b4b95SEric W. Biederman 
120204600794SJohannes Berg struct kobj_ns_type_operations net_ns_type_operations = {
1203608b4b95SEric W. Biederman 	.type = KOBJ_NS_TYPE_NET,
1204a685e089SAl Viro 	.grab_current_ns = net_grab_current_ns,
1205608b4b95SEric W. Biederman 	.netlink_ns = net_netlink_ns,
1206608b4b95SEric W. Biederman 	.initial_ns = net_initial_ns,
1207a685e089SAl Viro 	.drop_ns = net_drop_ns,
1208608b4b95SEric W. Biederman };
120904600794SJohannes Berg EXPORT_SYMBOL_GPL(net_ns_type_operations);
1210608b4b95SEric W. Biederman 
12117eff2e7aSKay Sievers static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
12121da177e4SLinus Torvalds {
121343cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
12147eff2e7aSKay Sievers 	int retval;
12151da177e4SLinus Torvalds 
1216312c004dSKay Sievers 	/* pass interface to uevent. */
12177eff2e7aSKay Sievers 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1218bf62456eSEric Rannaud 	if (retval)
1219bf62456eSEric Rannaud 		goto exit;
12201da177e4SLinus Torvalds 
1221ca2f37dbSJean Tourrilhes 	/* pass ifindex to uevent.
1222ca2f37dbSJean Tourrilhes 	 * ifindex is useful as it won't change (interface name may change)
1223ca2f37dbSJean Tourrilhes 	 * and is what RtNetlink uses natively. */
12247eff2e7aSKay Sievers 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1225ca2f37dbSJean Tourrilhes 
1226bf62456eSEric Rannaud exit:
1227bf62456eSEric Rannaud 	return retval;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /*
12311da177e4SLinus Torvalds  *	netdev_release -- destroy and free a dead device.
123243cb76d9SGreg Kroah-Hartman  *	Called when last reference to device kobject is gone.
12331da177e4SLinus Torvalds  */
123443cb76d9SGreg Kroah-Hartman static void netdev_release(struct device *d)
12351da177e4SLinus Torvalds {
123643cb76d9SGreg Kroah-Hartman 	struct net_device *dev = to_net_dev(d);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	BUG_ON(dev->reg_state != NETREG_RELEASED);
12391da177e4SLinus Torvalds 
12400b815a1aSStephen Hemminger 	kfree(dev->ifalias);
12411da177e4SLinus Torvalds 	kfree((char *)dev - dev->padded);
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
1244608b4b95SEric W. Biederman static const void *net_namespace(struct device *d)
1245608b4b95SEric W. Biederman {
1246608b4b95SEric W. Biederman 	struct net_device *dev;
1247608b4b95SEric W. Biederman 	dev = container_of(d, struct net_device, dev);
1248608b4b95SEric W. Biederman 	return dev_net(dev);
1249608b4b95SEric W. Biederman }
1250608b4b95SEric W. Biederman 
12511da177e4SLinus Torvalds static struct class net_class = {
12521da177e4SLinus Torvalds 	.name = "net",
125343cb76d9SGreg Kroah-Hartman 	.dev_release = netdev_release,
12548b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
125543cb76d9SGreg Kroah-Hartman 	.dev_attrs = net_class_attributes,
12568b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
125743cb76d9SGreg Kroah-Hartman 	.dev_uevent = netdev_uevent,
1258608b4b95SEric W. Biederman 	.ns_type = &net_ns_type_operations,
1259608b4b95SEric W. Biederman 	.namespace = net_namespace,
12601da177e4SLinus Torvalds };
12611da177e4SLinus Torvalds 
12629093bbb2SStephen Hemminger /* Delete sysfs entries but hold kobject reference until after all
12639093bbb2SStephen Hemminger  * netdev references are gone.
12649093bbb2SStephen Hemminger  */
12658b41d188SEric W. Biederman void netdev_unregister_kobject(struct net_device * net)
12661da177e4SLinus Torvalds {
12679093bbb2SStephen Hemminger 	struct device *dev = &(net->dev);
12689093bbb2SStephen Hemminger 
12699093bbb2SStephen Hemminger 	kobject_get(&dev->kobj);
12703891845eSEric W. Biederman 
12711d24eb48STom Herbert 	remove_queue_kobjects(net);
12720a9627f2STom Herbert 
12739802c8e2SMing Lei 	pm_runtime_set_memalloc_noio(dev, false);
12749802c8e2SMing Lei 
12759093bbb2SStephen Hemminger 	device_del(dev);
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds /* Create sysfs entries for network device. */
12798b41d188SEric W. Biederman int netdev_register_kobject(struct net_device *net)
12801da177e4SLinus Torvalds {
128143cb76d9SGreg Kroah-Hartman 	struct device *dev = &(net->dev);
1282a4dbd674SDavid Brownell 	const struct attribute_group **groups = net->sysfs_groups;
12830a9627f2STom Herbert 	int error = 0;
12841da177e4SLinus Torvalds 
1285a1b3f594SEric W. Biederman 	device_initialize(dev);
128643cb76d9SGreg Kroah-Hartman 	dev->class = &net_class;
128743cb76d9SGreg Kroah-Hartman 	dev->platform_data = net;
128843cb76d9SGreg Kroah-Hartman 	dev->groups = groups;
12891da177e4SLinus Torvalds 
1290a2205472SStephen Hemminger 	dev_set_name(dev, "%s", net->name);
12911da177e4SLinus Torvalds 
12928b41d188SEric W. Biederman #ifdef CONFIG_SYSFS
12930c509a6cSEric W. Biederman 	/* Allow for a device specific group */
12940c509a6cSEric W. Biederman 	if (*groups)
12950c509a6cSEric W. Biederman 		groups++;
12961da177e4SLinus Torvalds 
12970c509a6cSEric W. Biederman 	*groups++ = &netstat_group;
129838c1a01cSJohannes Berg 
129938c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
130038c1a01cSJohannes Berg 	if (net->ieee80211_ptr)
130138c1a01cSJohannes Berg 		*groups++ = &wireless_group;
130238c1a01cSJohannes Berg #if IS_ENABLED(CONFIG_WIRELESS_EXT)
130338c1a01cSJohannes Berg 	else if (net->wireless_handlers)
130438c1a01cSJohannes Berg 		*groups++ = &wireless_group;
130538c1a01cSJohannes Berg #endif
130638c1a01cSJohannes Berg #endif
13078b41d188SEric W. Biederman #endif /* CONFIG_SYSFS */
13081da177e4SLinus Torvalds 
13090a9627f2STom Herbert 	error = device_add(dev);
13100a9627f2STom Herbert 	if (error)
13110a9627f2STom Herbert 		return error;
13120a9627f2STom Herbert 
13131d24eb48STom Herbert 	error = register_queue_kobjects(net);
13140a9627f2STom Herbert 	if (error) {
13150a9627f2STom Herbert 		device_del(dev);
13160a9627f2STom Herbert 		return error;
13170a9627f2STom Herbert 	}
13180a9627f2STom Herbert 
13199802c8e2SMing Lei 	pm_runtime_set_memalloc_noio(dev, true);
13209802c8e2SMing Lei 
13210a9627f2STom Herbert 	return error;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
1324b8a9787eSJay Vosburgh int netdev_class_create_file(struct class_attribute *class_attr)
1325b8a9787eSJay Vosburgh {
1326b8a9787eSJay Vosburgh 	return class_create_file(&net_class, class_attr);
1327b8a9787eSJay Vosburgh }
13289e34a5b5SEric Dumazet EXPORT_SYMBOL(netdev_class_create_file);
1329b8a9787eSJay Vosburgh 
1330b8a9787eSJay Vosburgh void netdev_class_remove_file(struct class_attribute *class_attr)
1331b8a9787eSJay Vosburgh {
1332b8a9787eSJay Vosburgh 	class_remove_file(&net_class, class_attr);
1333b8a9787eSJay Vosburgh }
1334b8a9787eSJay Vosburgh EXPORT_SYMBOL(netdev_class_remove_file);
1335b8a9787eSJay Vosburgh 
13368b41d188SEric W. Biederman int netdev_kobject_init(void)
13371da177e4SLinus Torvalds {
1338608b4b95SEric W. Biederman 	kobj_ns_type_register(&net_ns_type_operations);
13391da177e4SLinus Torvalds 	return class_register(&net_class);
13401da177e4SLinus Torvalds }
1341