blob: bd67c4d0fcfdf6522ce16143523169d506e09d82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net-sysfs.c - network device class and attributes
3 *
4 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/netdevice.h>
Jiri Pirkoaecbe012014-11-28 14:34:19 +010015#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070019#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/sock.h>
Eric W. Biederman608b4b92010-05-04 17:36:45 -070021#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/rtnetlink.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070023#include <linux/vmalloc.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Tom Herbert114cf582011-11-28 16:33:09 +000025#include <linux/jiffies.h>
Ming Lei9802c8e2013-02-22 16:34:16 -080026#include <linux/pm_runtime.h>
Florian Fainelliaa836df2015-03-09 14:31:20 -070027#include <linux/of.h>
Ben Dooks88832a22016-06-07 19:27:51 +010028#include <linux/of_net.h>
Andrei Vagin4d99f662018-08-08 20:07:35 -070029#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Pavel Emelyanov342709e2007-10-23 21:14:45 -070031#include "net-sysfs.h"
32
Eric W. Biederman8b41d182007-09-26 22:02:53 -070033#ifdef CONFIG_SYSFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static const char fmt_hex[] = "%#x\n";
35static const char fmt_dec[] = "%d\n";
36static const char fmt_ulong[] = "%lu\n";
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +000037static const char fmt_u64[] = "%llu\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090039static inline int dev_isalive(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Stephen Hemmingerfe9925b2006-05-06 17:56:03 -070041 return dev->reg_state <= NETREG_REGISTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44/* use same locking rules as GIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070045static ssize_t netdev_show(const struct device *dev,
46 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t (*format)(const struct net_device *, char *))
48{
WANG Cong6b53daf2014-07-23 16:09:10 -070049 struct net_device *ndev = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 ssize_t ret = -EINVAL;
51
52 read_lock(&dev_base_lock);
WANG Cong6b53daf2014-07-23 16:09:10 -070053 if (dev_isalive(ndev))
54 ret = (*format)(ndev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 read_unlock(&dev_base_lock);
56
57 return ret;
58}
59
60/* generate a show function for simple field */
61#define NETDEVICE_SHOW(field, format_string) \
WANG Cong6b53daf2014-07-23 16:09:10 -070062static ssize_t format_##field(const struct net_device *dev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{ \
WANG Cong6b53daf2014-07-23 16:09:10 -070064 return sprintf(buf, format_string, dev->field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070066static ssize_t field##_show(struct device *dev, \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070067 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070069 return netdev_show(dev, attr, buf, format_##field); \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070070} \
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -070072#define NETDEVICE_SHOW_RO(field, format_string) \
73NETDEVICE_SHOW(field, format_string); \
74static DEVICE_ATTR_RO(field)
75
76#define NETDEVICE_SHOW_RW(field, format_string) \
77NETDEVICE_SHOW(field, format_string); \
78static DEVICE_ATTR_RW(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* use same locking and permission rules as SIF* ioctl's */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070081static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 const char *buf, size_t len,
83 int (*set)(struct net_device *, unsigned long))
84{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000085 struct net_device *netdev = to_net_dev(dev);
86 struct net *net = dev_net(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long new;
88 int ret = -EINVAL;
89
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +000090 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EPERM;
92
Shuah Khane1e420c2012-04-12 09:28:13 +000093 ret = kstrtoul(buf, 0, &new);
94 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto err;
96
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000097 if (!rtnl_trylock())
Eric W. Biederman336ca572009-05-13 16:57:25 +000098 return restart_syscall();
Stephen Hemminger5a5990d2009-02-26 06:49:24 +000099
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000100 if (dev_isalive(netdev)) {
stephen hemminger6648c652017-08-18 13:46:28 -0700101 ret = (*set)(netdev, new);
102 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ret = len;
104 }
105 rtnl_unlock();
106 err:
107 return ret;
108}
109
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700110NETDEVICE_SHOW_RO(dev_id, fmt_hex);
Amir Vadai3f859442014-02-25 18:17:50 +0200111NETDEVICE_SHOW_RO(dev_port, fmt_dec);
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700112NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
113NETDEVICE_SHOW_RO(addr_len, fmt_dec);
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700114NETDEVICE_SHOW_RO(ifindex, fmt_dec);
115NETDEVICE_SHOW_RO(type, fmt_dec);
116NETDEVICE_SHOW_RO(link_mode, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200118static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
119 char *buf)
120{
121 struct net_device *ndev = to_net_dev(dev);
122
123 return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
124}
125static DEVICE_ATTR_RO(iflink);
126
WANG Cong6b53daf2014-07-23 16:09:10 -0700127static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
Tom Gundersen685343f2014-07-14 16:37:22 +0200128{
WANG Cong6b53daf2014-07-23 16:09:10 -0700129 return sprintf(buf, fmt_dec, dev->name_assign_type);
Tom Gundersen685343f2014-07-14 16:37:22 +0200130}
131
132static ssize_t name_assign_type_show(struct device *dev,
133 struct device_attribute *attr,
134 char *buf)
135{
WANG Cong6b53daf2014-07-23 16:09:10 -0700136 struct net_device *ndev = to_net_dev(dev);
Tom Gundersen685343f2014-07-14 16:37:22 +0200137 ssize_t ret = -EINVAL;
138
WANG Cong6b53daf2014-07-23 16:09:10 -0700139 if (ndev->name_assign_type != NET_NAME_UNKNOWN)
Tom Gundersen685343f2014-07-14 16:37:22 +0200140 ret = netdev_show(dev, attr, buf, format_name_assign_type);
141
142 return ret;
143}
144static DEVICE_ATTR_RO(name_assign_type);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* use same locking rules as GIFHWADDR ioctl's */
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700147static ssize_t address_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700148 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
WANG Cong6b53daf2014-07-23 16:09:10 -0700150 struct net_device *ndev = to_net_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ssize_t ret = -EINVAL;
152
153 read_lock(&dev_base_lock);
WANG Cong6b53daf2014-07-23 16:09:10 -0700154 if (dev_isalive(ndev))
155 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 read_unlock(&dev_base_lock);
157 return ret;
158}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700159static DEVICE_ATTR_RO(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700161static ssize_t broadcast_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
WANG Cong6b53daf2014-07-23 16:09:10 -0700164 struct net_device *ndev = to_net_dev(dev);
stephen hemminger6648c652017-08-18 13:46:28 -0700165
WANG Cong6b53daf2014-07-23 16:09:10 -0700166 if (dev_isalive(ndev))
167 return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -EINVAL;
169}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700170static DEVICE_ATTR_RO(broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
WANG Cong6b53daf2014-07-23 16:09:10 -0700172static int change_carrier(struct net_device *dev, unsigned long new_carrier)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000173{
WANG Cong6b53daf2014-07-23 16:09:10 -0700174 if (!netif_running(dev))
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000175 return -EINVAL;
stephen hemminger6648c652017-08-18 13:46:28 -0700176 return dev_change_carrier(dev, (bool)new_carrier);
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000177}
178
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700179static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
180 const char *buf, size_t len)
Jiri Pirkofdae0fd2012-12-27 23:49:38 +0000181{
182 return netdev_store(dev, attr, buf, len, change_carrier);
183}
184
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700185static ssize_t carrier_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700186 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct net_device *netdev = to_net_dev(dev);
stephen hemminger6648c652017-08-18 13:46:28 -0700189
190 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
stephen hemminger6648c652017-08-18 13:46:28 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EINVAL;
194}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700195static DEVICE_ATTR_RW(carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700197static ssize_t speed_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000198 struct device_attribute *attr, char *buf)
199{
200 struct net_device *netdev = to_net_dev(dev);
201 int ret = -EINVAL;
202
203 if (!rtnl_trylock())
204 return restart_syscall();
205
David Decotigny8ae6daca2011-04-27 18:32:38 +0000206 if (netif_running(netdev)) {
David Decotigny7cad1ba2016-02-24 10:58:10 -0800207 struct ethtool_link_ksettings cmd;
208
209 if (!__ethtool_get_link_ksettings(netdev, &cmd))
210 ret = sprintf(buf, fmt_dec, cmd.base.speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000211 }
212 rtnl_unlock();
213 return ret;
214}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700215static DEVICE_ATTR_RO(speed);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000216
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700217static ssize_t duplex_show(struct device *dev,
Andy Gospodarekd519e172009-10-02 09:26:12 +0000218 struct device_attribute *attr, char *buf)
219{
220 struct net_device *netdev = to_net_dev(dev);
221 int ret = -EINVAL;
222
223 if (!rtnl_trylock())
224 return restart_syscall();
225
David Decotigny8ae6daca2011-04-27 18:32:38 +0000226 if (netif_running(netdev)) {
David Decotigny7cad1ba2016-02-24 10:58:10 -0800227 struct ethtool_link_ksettings cmd;
228
229 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000230 const char *duplex;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800231
232 switch (cmd.base.duplex) {
Nikolay Aleksandrovc6c13962012-09-05 04:11:28 +0000233 case DUPLEX_HALF:
234 duplex = "half";
235 break;
236 case DUPLEX_FULL:
237 duplex = "full";
238 break;
239 default:
240 duplex = "unknown";
241 break;
242 }
243 ret = sprintf(buf, "%s\n", duplex);
244 }
Andy Gospodarekd519e172009-10-02 09:26:12 +0000245 }
246 rtnl_unlock();
247 return ret;
248}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700249static DEVICE_ATTR_RO(duplex);
Andy Gospodarekd519e172009-10-02 09:26:12 +0000250
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700251static ssize_t dormant_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700252 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800253{
254 struct net_device *netdev = to_net_dev(dev);
255
256 if (netif_running(netdev))
257 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
258
259 return -EINVAL;
260}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700261static DEVICE_ATTR_RO(dormant);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800262
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700263static const char *const operstates[] = {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800264 "unknown",
265 "notpresent", /* currently unused */
266 "down",
267 "lowerlayerdown",
268 "testing", /* currently unused */
269 "dormant",
270 "up"
271};
272
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700273static ssize_t operstate_show(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700274 struct device_attribute *attr, char *buf)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800275{
276 const struct net_device *netdev = to_net_dev(dev);
277 unsigned char operstate;
278
279 read_lock(&dev_base_lock);
280 operstate = netdev->operstate;
281 if (!netif_running(netdev))
282 operstate = IF_OPER_DOWN;
283 read_unlock(&dev_base_lock);
284
Adrian Bunke3a5cd92006-04-05 22:19:47 -0700285 if (operstate >= ARRAY_SIZE(operstates))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800286 return -EINVAL; /* should not happen */
287
288 return sprintf(buf, "%s\n", operstates[operstate]);
289}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700290static DEVICE_ATTR_RO(operstate);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800291
david decotigny2d3b4792014-03-29 09:48:35 -0700292static ssize_t carrier_changes_show(struct device *dev,
293 struct device_attribute *attr,
294 char *buf)
295{
296 struct net_device *netdev = to_net_dev(dev);
stephen hemminger6648c652017-08-18 13:46:28 -0700297
david decotigny2d3b4792014-03-29 09:48:35 -0700298 return sprintf(buf, fmt_dec,
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800299 atomic_read(&netdev->carrier_up_count) +
300 atomic_read(&netdev->carrier_down_count));
david decotigny2d3b4792014-03-29 09:48:35 -0700301}
302static DEVICE_ATTR_RO(carrier_changes);
303
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800304static ssize_t carrier_up_count_show(struct device *dev,
305 struct device_attribute *attr,
306 char *buf)
307{
308 struct net_device *netdev = to_net_dev(dev);
309
310 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
311}
312static DEVICE_ATTR_RO(carrier_up_count);
313
314static ssize_t carrier_down_count_show(struct device *dev,
315 struct device_attribute *attr,
316 char *buf)
317{
318 struct net_device *netdev = to_net_dev(dev);
319
320 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
321}
322static DEVICE_ATTR_RO(carrier_down_count);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* read-write attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
WANG Cong6b53daf2014-07-23 16:09:10 -0700326static int change_mtu(struct net_device *dev, unsigned long new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
stephen hemminger6648c652017-08-18 13:46:28 -0700328 return dev_set_mtu(dev, (int)new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700331static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700332 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700334 return netdev_store(dev, attr, buf, len, change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700336NETDEVICE_SHOW_RW(mtu, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
WANG Cong6b53daf2014-07-23 16:09:10 -0700338static int change_flags(struct net_device *dev, unsigned long new_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
stephen hemminger6648c652017-08-18 13:46:28 -0700340 return dev_change_flags(dev, (unsigned int)new_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700343static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700344 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700346 return netdev_store(dev, attr, buf, len, change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700348NETDEVICE_SHOW_RW(flags, fmt_hex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700350static ssize_t tx_queue_len_store(struct device *dev,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700351 struct device_attribute *attr,
352 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000354 if (!capable(CAP_NET_ADMIN))
355 return -EPERM;
356
Cong Wang6a643dd2018-01-25 18:26:22 -0800357 return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
Alexey Dobriyan0cd29502017-05-17 13:30:44 +0300359NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Eric Dumazet3b47d302014-11-06 21:09:44 -0800361static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
362{
363 dev->gro_flush_timeout = val;
364 return 0;
365}
366
367static ssize_t gro_flush_timeout_store(struct device *dev,
stephen hemminger6648c652017-08-18 13:46:28 -0700368 struct device_attribute *attr,
369 const char *buf, size_t len)
Eric Dumazet3b47d302014-11-06 21:09:44 -0800370{
371 if (!capable(CAP_NET_ADMIN))
372 return -EPERM;
373
374 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
375}
376NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
377
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700378static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700379 const char *buf, size_t len)
380{
381 struct net_device *netdev = to_net_dev(dev);
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000382 struct net *net = dev_net(netdev);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700383 size_t count = len;
Roopa Prabhuc92eb772017-11-13 23:21:36 -0800384 ssize_t ret = 0;
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700385
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +0000386 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700387 return -EPERM;
388
389 /* ignore trailing newline */
390 if (len > 0 && buf[len - 1] == '\n')
391 --count;
392
Roopa Prabhuc92eb772017-11-13 23:21:36 -0800393 if (!rtnl_trylock())
394 return restart_syscall();
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700395
Roopa Prabhuc92eb772017-11-13 23:21:36 -0800396 if (dev_isalive(netdev)) {
397 ret = dev_set_alias(netdev, buf, count);
398 if (ret < 0)
399 goto err;
400 ret = len;
401 netdev_state_change(netdev);
402 }
403err:
404 rtnl_unlock();
405
406 return ret;
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700407}
408
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700409static ssize_t ifalias_show(struct device *dev,
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700410 struct device_attribute *attr, char *buf)
411{
412 const struct net_device *netdev = to_net_dev(dev);
Florian Westphal6c557002017-10-02 23:50:05 +0200413 char tmp[IFALIASZ];
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700414 ssize_t ret = 0;
415
Florian Westphal6c557002017-10-02 23:50:05 +0200416 ret = dev_get_alias(netdev, tmp, sizeof(tmp));
417 if (ret > 0)
418 ret = sprintf(buf, "%s\n", tmp);
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700419 return ret;
420}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700421static DEVICE_ATTR_RW(ifalias);
Vlad Dogarua512b922011-01-24 03:37:29 +0000422
WANG Cong6b53daf2014-07-23 16:09:10 -0700423static int change_group(struct net_device *dev, unsigned long new_group)
Vlad Dogarua512b922011-01-24 03:37:29 +0000424{
stephen hemminger6648c652017-08-18 13:46:28 -0700425 dev_set_group(dev, (int)new_group);
Vlad Dogarua512b922011-01-24 03:37:29 +0000426 return 0;
427}
428
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700429static ssize_t group_store(struct device *dev, struct device_attribute *attr,
430 const char *buf, size_t len)
Vlad Dogarua512b922011-01-24 03:37:29 +0000431{
432 return netdev_store(dev, attr, buf, len, change_group);
433}
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700434NETDEVICE_SHOW(group, fmt_dec);
Joe Perchesd6444062018-03-23 15:54:38 -0700435static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
Vlad Dogarua512b922011-01-24 03:37:29 +0000436
Anuradha Karuppiahd746d702015-07-14 13:43:19 -0700437static int change_proto_down(struct net_device *dev, unsigned long proto_down)
438{
stephen hemminger6648c652017-08-18 13:46:28 -0700439 return dev_change_proto_down(dev, (bool)proto_down);
Anuradha Karuppiahd746d702015-07-14 13:43:19 -0700440}
441
442static ssize_t proto_down_store(struct device *dev,
443 struct device_attribute *attr,
444 const char *buf, size_t len)
445{
446 return netdev_store(dev, attr, buf, len, change_proto_down);
447}
448NETDEVICE_SHOW_RW(proto_down, fmt_dec);
449
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700450static ssize_t phys_port_id_show(struct device *dev,
Jiri Pirkoff80e512013-07-29 18:16:51 +0200451 struct device_attribute *attr, char *buf)
452{
453 struct net_device *netdev = to_net_dev(dev);
454 ssize_t ret = -EINVAL;
455
456 if (!rtnl_trylock())
457 return restart_syscall();
458
459 if (dev_isalive(netdev)) {
Jiri Pirko02637fc2014-11-28 14:34:16 +0100460 struct netdev_phys_item_id ppid;
Jiri Pirkoff80e512013-07-29 18:16:51 +0200461
462 ret = dev_get_phys_port_id(netdev, &ppid);
463 if (!ret)
464 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
465 }
466 rtnl_unlock();
467
468 return ret;
469}
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700470static DEVICE_ATTR_RO(phys_port_id);
Jiri Pirkoff80e512013-07-29 18:16:51 +0200471
David Aherndb24a902015-03-17 20:23:15 -0600472static ssize_t phys_port_name_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
475 struct net_device *netdev = to_net_dev(dev);
476 ssize_t ret = -EINVAL;
477
478 if (!rtnl_trylock())
479 return restart_syscall();
480
481 if (dev_isalive(netdev)) {
482 char name[IFNAMSIZ];
483
484 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
485 if (!ret)
486 ret = sprintf(buf, "%s\n", name);
487 }
488 rtnl_unlock();
489
490 return ret;
491}
492static DEVICE_ATTR_RO(phys_port_name);
493
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100494static ssize_t phys_switch_id_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
497 struct net_device *netdev = to_net_dev(dev);
498 ssize_t ret = -EINVAL;
499
500 if (!rtnl_trylock())
501 return restart_syscall();
502
503 if (dev_isalive(netdev)) {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700504 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100505 .orig_dev = netdev,
Jiri Pirko1f868392015-10-01 11:03:42 +0200506 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700507 .flags = SWITCHDEV_F_NO_RECURSE,
508 };
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100509
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700510 ret = switchdev_port_attr_get(netdev, &attr);
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100511 if (!ret)
Scott Feldman42275bd2015-05-13 11:16:50 -0700512 ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
513 attr.u.ppid.id);
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100514 }
515 rtnl_unlock();
516
517 return ret;
518}
519static DEVICE_ATTR_RO(phys_switch_id);
520
stephen hemmingerec6cc592017-08-18 13:46:23 -0700521static struct attribute *net_class_attrs[] __ro_after_init = {
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700522 &dev_attr_netdev_group.attr,
523 &dev_attr_type.attr,
524 &dev_attr_dev_id.attr,
Amir Vadai3f859442014-02-25 18:17:50 +0200525 &dev_attr_dev_port.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700526 &dev_attr_iflink.attr,
527 &dev_attr_ifindex.attr,
Tom Gundersen685343f2014-07-14 16:37:22 +0200528 &dev_attr_name_assign_type.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700529 &dev_attr_addr_assign_type.attr,
530 &dev_attr_addr_len.attr,
531 &dev_attr_link_mode.attr,
532 &dev_attr_address.attr,
533 &dev_attr_broadcast.attr,
534 &dev_attr_speed.attr,
535 &dev_attr_duplex.attr,
536 &dev_attr_dormant.attr,
537 &dev_attr_operstate.attr,
david decotigny2d3b4792014-03-29 09:48:35 -0700538 &dev_attr_carrier_changes.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700539 &dev_attr_ifalias.attr,
540 &dev_attr_carrier.attr,
541 &dev_attr_mtu.attr,
542 &dev_attr_flags.attr,
543 &dev_attr_tx_queue_len.attr,
Eric Dumazet3b47d302014-11-06 21:09:44 -0800544 &dev_attr_gro_flush_timeout.attr,
Linus Torvaldscc998ff2013-09-05 14:54:29 -0700545 &dev_attr_phys_port_id.attr,
David Aherndb24a902015-03-17 20:23:15 -0600546 &dev_attr_phys_port_name.attr,
Jiri Pirkoaecbe012014-11-28 14:34:19 +0100547 &dev_attr_phys_switch_id.attr,
Anuradha Karuppiahd746d702015-07-14 13:43:19 -0700548 &dev_attr_proto_down.attr,
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800549 &dev_attr_carrier_up_count.attr,
550 &dev_attr_carrier_down_count.attr,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700551 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552};
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700553ATTRIBUTE_GROUPS(net_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/* Show a given an attribute in the statistics group */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700556static ssize_t netstat_show(const struct device *d,
557 struct device_attribute *attr, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long offset)
559{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700560 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ssize_t ret = -EINVAL;
562
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000563 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
stephen hemminger6648c652017-08-18 13:46:28 -0700564 offset % sizeof(u64) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 read_lock(&dev_base_lock);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700567 if (dev_isalive(dev)) {
Eric Dumazet28172732010-07-07 14:58:56 -0700568 struct rtnl_link_stats64 temp;
569 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
570
stephen hemminger6648c652017-08-18 13:46:28 -0700571 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 read_unlock(&dev_base_lock);
574 return ret;
575}
576
577/* generate a read-only statistics attribute */
578#define NETSTAT_ENTRY(name) \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700579static ssize_t name##_show(struct device *d, \
stephen hemminger6648c652017-08-18 13:46:28 -0700580 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{ \
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700582 return netstat_show(d, attr, buf, \
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000583 offsetof(struct rtnl_link_stats64, name)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584} \
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700585static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587NETSTAT_ENTRY(rx_packets);
588NETSTAT_ENTRY(tx_packets);
589NETSTAT_ENTRY(rx_bytes);
590NETSTAT_ENTRY(tx_bytes);
591NETSTAT_ENTRY(rx_errors);
592NETSTAT_ENTRY(tx_errors);
593NETSTAT_ENTRY(rx_dropped);
594NETSTAT_ENTRY(tx_dropped);
595NETSTAT_ENTRY(multicast);
596NETSTAT_ENTRY(collisions);
597NETSTAT_ENTRY(rx_length_errors);
598NETSTAT_ENTRY(rx_over_errors);
599NETSTAT_ENTRY(rx_crc_errors);
600NETSTAT_ENTRY(rx_frame_errors);
601NETSTAT_ENTRY(rx_fifo_errors);
602NETSTAT_ENTRY(rx_missed_errors);
603NETSTAT_ENTRY(tx_aborted_errors);
604NETSTAT_ENTRY(tx_carrier_errors);
605NETSTAT_ENTRY(tx_fifo_errors);
606NETSTAT_ENTRY(tx_heartbeat_errors);
607NETSTAT_ENTRY(tx_window_errors);
608NETSTAT_ENTRY(rx_compressed);
609NETSTAT_ENTRY(tx_compressed);
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500610NETSTAT_ENTRY(rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
stephen hemmingerec6cc592017-08-18 13:46:23 -0700612static struct attribute *netstat_attrs[] __ro_after_init = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700613 &dev_attr_rx_packets.attr,
614 &dev_attr_tx_packets.attr,
615 &dev_attr_rx_bytes.attr,
616 &dev_attr_tx_bytes.attr,
617 &dev_attr_rx_errors.attr,
618 &dev_attr_tx_errors.attr,
619 &dev_attr_rx_dropped.attr,
620 &dev_attr_tx_dropped.attr,
621 &dev_attr_multicast.attr,
622 &dev_attr_collisions.attr,
623 &dev_attr_rx_length_errors.attr,
624 &dev_attr_rx_over_errors.attr,
625 &dev_attr_rx_crc_errors.attr,
626 &dev_attr_rx_frame_errors.attr,
627 &dev_attr_rx_fifo_errors.attr,
628 &dev_attr_rx_missed_errors.attr,
629 &dev_attr_tx_aborted_errors.attr,
630 &dev_attr_tx_carrier_errors.attr,
631 &dev_attr_tx_fifo_errors.attr,
632 &dev_attr_tx_heartbeat_errors.attr,
633 &dev_attr_tx_window_errors.attr,
634 &dev_attr_rx_compressed.attr,
635 &dev_attr_tx_compressed.attr,
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500636 &dev_attr_rx_nohandler.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 NULL
638};
639
Arvind Yadav38ef00c2017-06-29 16:31:26 +0530640static const struct attribute_group netstat_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 .name = "statistics",
642 .attrs = netstat_attrs,
643};
Johannes Berg38c1a012012-11-16 20:46:19 +0100644
645#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
646static struct attribute *wireless_attrs[] = {
647 NULL
648};
649
Arvind Yadav38ef00c2017-06-29 16:31:26 +0530650static const struct attribute_group wireless_group = {
Johannes Berg38c1a012012-11-16 20:46:19 +0100651 .name = "wireless",
652 .attrs = wireless_attrs,
653};
654#endif
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -0700655
656#else /* CONFIG_SYSFS */
657#define net_class_groups NULL
Eric W. Biedermand6523dd2010-05-16 21:59:45 -0700658#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Michael Daltona953be52014-01-16 22:23:28 -0800660#ifdef CONFIG_SYSFS
stephen hemminger6648c652017-08-18 13:46:28 -0700661#define to_rx_queue_attr(_attr) \
662 container_of(_attr, struct rx_queue_attribute, attr)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000663
664#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
665
666static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
667 char *buf)
668{
stephen hemminger667e4272017-08-18 13:46:27 -0700669 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000670 struct netdev_rx_queue *queue = to_rx_queue(kobj);
671
672 if (!attribute->show)
673 return -EIO;
674
stephen hemminger718ad682017-08-18 13:46:24 -0700675 return attribute->show(queue, buf);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000676}
677
678static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
679 const char *buf, size_t count)
680{
stephen hemminger667e4272017-08-18 13:46:27 -0700681 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000682 struct netdev_rx_queue *queue = to_rx_queue(kobj);
683
684 if (!attribute->store)
685 return -EIO;
686
stephen hemminger718ad682017-08-18 13:46:24 -0700687 return attribute->store(queue, buf, count);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000688}
689
stephen hemmingerfa50d642010-08-31 12:14:13 +0000690static const struct sysfs_ops rx_queue_sysfs_ops = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000691 .show = rx_queue_attr_show,
692 .store = rx_queue_attr_store,
693};
694
Michael Daltona953be52014-01-16 22:23:28 -0800695#ifdef CONFIG_RPS
stephen hemminger718ad682017-08-18 13:46:24 -0700696static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000697{
698 struct rps_map *map;
699 cpumask_var_t mask;
Tejun Heof0906822015-02-13 14:37:42 -0800700 int i, len;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000701
702 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
703 return -ENOMEM;
704
705 rcu_read_lock();
706 map = rcu_dereference(queue->rps_map);
707 if (map)
708 for (i = 0; i < map->len; i++)
709 cpumask_set_cpu(map->cpus[i], mask);
710
Tejun Heof0906822015-02-13 14:37:42 -0800711 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000712 rcu_read_unlock();
Tom Herbert0a9627f2010-03-16 08:03:29 +0000713 free_cpumask_var(mask);
Tejun Heof0906822015-02-13 14:37:42 -0800714
715 return len < PAGE_SIZE ? len : -EINVAL;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000716}
717
Eric Dumazetf5acb902010-04-19 14:40:57 -0700718static ssize_t store_rps_map(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -0700719 const char *buf, size_t len)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000720{
721 struct rps_map *old_map, *map;
722 cpumask_var_t mask;
723 int err, cpu, i;
Sasha Levinda65ad12015-08-13 14:03:16 -0400724 static DEFINE_MUTEX(rps_map_mutex);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000725
726 if (!capable(CAP_NET_ADMIN))
727 return -EPERM;
728
729 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
730 return -ENOMEM;
731
732 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
733 if (err) {
734 free_cpumask_var(mask);
735 return err;
736 }
737
Eric Dumazet95c96172012-04-15 05:58:06 +0000738 map = kzalloc(max_t(unsigned int,
stephen hemminger6648c652017-08-18 13:46:28 -0700739 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
740 GFP_KERNEL);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000741 if (!map) {
742 free_cpumask_var(mask);
743 return -ENOMEM;
744 }
745
746 i = 0;
747 for_each_cpu_and(cpu, mask, cpu_online_mask)
748 map->cpus[i++] = cpu;
749
stephen hemminger6648c652017-08-18 13:46:28 -0700750 if (i) {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000751 map->len = i;
stephen hemminger6648c652017-08-18 13:46:28 -0700752 } else {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000753 kfree(map);
754 map = NULL;
755 }
756
Sasha Levinda65ad12015-08-13 14:03:16 -0400757 mutex_lock(&rps_map_mutex);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000758 old_map = rcu_dereference_protected(queue->rps_map,
Sasha Levinda65ad12015-08-13 14:03:16 -0400759 mutex_is_locked(&rps_map_mutex));
Tom Herbert0a9627f2010-03-16 08:03:29 +0000760 rcu_assign_pointer(queue->rps_map, map);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000761
Eric Dumazetadc93002011-11-17 03:13:26 +0000762 if (map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100763 static_key_slow_inc(&rps_needed);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700764 if (old_map)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100765 static_key_slow_dec(&rps_needed);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700766
Sasha Levinda65ad12015-08-13 14:03:16 -0400767 mutex_unlock(&rps_map_mutex);
Tom Herbert10e4ea72015-08-05 09:39:27 -0700768
769 if (old_map)
770 kfree_rcu(old_map, rcu);
771
Tom Herbert0a9627f2010-03-16 08:03:29 +0000772 free_cpumask_var(mask);
773 return len;
774}
775
Tom Herbertfec5e652010-04-16 16:01:27 -0700776static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
Tom Herbertfec5e652010-04-16 16:01:27 -0700777 char *buf)
778{
779 struct rps_dev_flow_table *flow_table;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000780 unsigned long val = 0;
Tom Herbertfec5e652010-04-16 16:01:27 -0700781
782 rcu_read_lock();
783 flow_table = rcu_dereference(queue->rps_flow_table);
784 if (flow_table)
Eric Dumazet60b778c2011-12-24 06:56:49 +0000785 val = (unsigned long)flow_table->mask + 1;
Tom Herbertfec5e652010-04-16 16:01:27 -0700786 rcu_read_unlock();
787
Eric Dumazet60b778c2011-12-24 06:56:49 +0000788 return sprintf(buf, "%lu\n", val);
Tom Herbertfec5e652010-04-16 16:01:27 -0700789}
790
Tom Herbertfec5e652010-04-16 16:01:27 -0700791static void rps_dev_flow_table_release(struct rcu_head *rcu)
792{
793 struct rps_dev_flow_table *table = container_of(rcu,
794 struct rps_dev_flow_table, rcu);
Al Viro243198d2013-05-05 16:05:55 +0000795 vfree(table);
Tom Herbertfec5e652010-04-16 16:01:27 -0700796}
797
Eric Dumazetf5acb902010-04-19 14:40:57 -0700798static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -0700799 const char *buf, size_t len)
Tom Herbertfec5e652010-04-16 16:01:27 -0700800{
Eric Dumazet60b778c2011-12-24 06:56:49 +0000801 unsigned long mask, count;
Tom Herbertfec5e652010-04-16 16:01:27 -0700802 struct rps_dev_flow_table *table, *old_table;
803 static DEFINE_SPINLOCK(rps_dev_flow_lock);
Eric Dumazet60b778c2011-12-24 06:56:49 +0000804 int rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700805
806 if (!capable(CAP_NET_ADMIN))
807 return -EPERM;
808
Eric Dumazet60b778c2011-12-24 06:56:49 +0000809 rc = kstrtoul(buf, 0, &count);
810 if (rc < 0)
811 return rc;
Tom Herbertfec5e652010-04-16 16:01:27 -0700812
813 if (count) {
Eric Dumazet60b778c2011-12-24 06:56:49 +0000814 mask = count - 1;
815 /* mask = roundup_pow_of_two(count) - 1;
816 * without overflows...
817 */
818 while ((mask | (mask >> 1)) != mask)
819 mask |= (mask >> 1);
820 /* On 64 bit arches, must check mask fits in table->mask (u32),
stephen hemminger8e3bff92013-12-08 12:15:44 -0800821 * and on 32bit arches, must check
822 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
Eric Dumazet60b778c2011-12-24 06:56:49 +0000823 */
824#if BITS_PER_LONG > 32
825 if (mask > (unsigned long)(u32)mask)
Xi Wanga0a129f2011-12-22 13:35:22 +0000826 return -EINVAL;
Eric Dumazet60b778c2011-12-24 06:56:49 +0000827#else
828 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
Xi Wanga0a129f2011-12-22 13:35:22 +0000829 / sizeof(struct rps_dev_flow)) {
Tom Herbertfec5e652010-04-16 16:01:27 -0700830 /* Enforce a limit to prevent overflow */
831 return -EINVAL;
832 }
Eric Dumazet60b778c2011-12-24 06:56:49 +0000833#endif
834 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
Tom Herbertfec5e652010-04-16 16:01:27 -0700835 if (!table)
836 return -ENOMEM;
837
Eric Dumazet60b778c2011-12-24 06:56:49 +0000838 table->mask = mask;
839 for (count = 0; count <= mask; count++)
840 table->flows[count].cpu = RPS_NO_CPU;
stephen hemminger6648c652017-08-18 13:46:28 -0700841 } else {
Tom Herbertfec5e652010-04-16 16:01:27 -0700842 table = NULL;
stephen hemminger6648c652017-08-18 13:46:28 -0700843 }
Tom Herbertfec5e652010-04-16 16:01:27 -0700844
845 spin_lock(&rps_dev_flow_lock);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000846 old_table = rcu_dereference_protected(queue->rps_flow_table,
847 lockdep_is_held(&rps_dev_flow_lock));
Tom Herbertfec5e652010-04-16 16:01:27 -0700848 rcu_assign_pointer(queue->rps_flow_table, table);
849 spin_unlock(&rps_dev_flow_lock);
850
851 if (old_table)
852 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
853
854 return len;
855}
856
stephen hemminger667e4272017-08-18 13:46:27 -0700857static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
Joe Perchesd6444062018-03-23 15:54:38 -0700858 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000859
stephen hemminger667e4272017-08-18 13:46:27 -0700860static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
Joe Perchesd6444062018-03-23 15:54:38 -0700861 = __ATTR(rps_flow_cnt, 0644,
stephen hemminger667e4272017-08-18 13:46:27 -0700862 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
Michael Daltona953be52014-01-16 22:23:28 -0800863#endif /* CONFIG_RPS */
Tom Herbertfec5e652010-04-16 16:01:27 -0700864
stephen hemminger667e4272017-08-18 13:46:27 -0700865static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
Michael Daltona953be52014-01-16 22:23:28 -0800866#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000867 &rps_cpus_attribute.attr,
Tom Herbertfec5e652010-04-16 16:01:27 -0700868 &rps_dev_flow_table_cnt_attribute.attr,
Michael Daltona953be52014-01-16 22:23:28 -0800869#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000870 NULL
871};
872
873static void rx_queue_release(struct kobject *kobj)
874{
875 struct netdev_rx_queue *queue = to_rx_queue(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800876#ifdef CONFIG_RPS
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000877 struct rps_map *map;
878 struct rps_dev_flow_table *flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000879
Eric Dumazet33d480c2011-08-11 19:30:52 +0000880 map = rcu_dereference_protected(queue->rps_map, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000881 if (map) {
882 RCU_INIT_POINTER(queue->rps_map, NULL);
Lai Jiangshanf6f80232011-03-18 12:01:31 +0800883 kfree_rcu(map, rcu);
John Fastabend9ea19482010-11-16 06:31:39 +0000884 }
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000885
Eric Dumazet33d480c2011-08-11 19:30:52 +0000886 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
John Fastabend9ea19482010-11-16 06:31:39 +0000887 if (flow_table) {
888 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +0000889 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
John Fastabend9ea19482010-11-16 06:31:39 +0000890 }
Michael Daltona953be52014-01-16 22:23:28 -0800891#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000892
John Fastabend9ea19482010-11-16 06:31:39 +0000893 memset(kobj, 0, sizeof(*kobj));
Tom Herbertfe822242010-11-09 10:47:38 +0000894 dev_put(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000895}
896
Weilong Chen82ef3d52014-01-16 17:24:31 +0800897static const void *rx_queue_namespace(struct kobject *kobj)
898{
899 struct netdev_rx_queue *queue = to_rx_queue(kobj);
900 struct device *dev = &queue->dev->dev;
901 const void *ns = NULL;
902
903 if (dev->class && dev->class->ns_type)
904 ns = dev->class->namespace(dev);
905
906 return ns;
907}
908
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +0000909static void rx_queue_get_ownership(struct kobject *kobj,
910 kuid_t *uid, kgid_t *gid)
911{
912 const struct net *net = rx_queue_namespace(kobj);
913
914 net_ns_get_ownership(net, uid, gid);
915}
916
stephen hemminger667e4272017-08-18 13:46:27 -0700917static struct kobj_type rx_queue_ktype __ro_after_init = {
Tom Herbert0a9627f2010-03-16 08:03:29 +0000918 .sysfs_ops = &rx_queue_sysfs_ops,
919 .release = rx_queue_release,
920 .default_attrs = rx_queue_default_attrs,
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +0000921 .namespace = rx_queue_namespace,
922 .get_ownership = rx_queue_get_ownership,
Tom Herbert0a9627f2010-03-16 08:03:29 +0000923};
924
WANG Cong6b53daf2014-07-23 16:09:10 -0700925static int rx_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000926{
WANG Cong6b53daf2014-07-23 16:09:10 -0700927 struct netdev_rx_queue *queue = dev->_rx + index;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000928 struct kobject *kobj = &queue->kobj;
929 int error = 0;
930
WANG Cong6b53daf2014-07-23 16:09:10 -0700931 kobj->kset = dev->queues_kset;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000932 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
stephen hemminger6648c652017-08-18 13:46:28 -0700933 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800934 if (error)
stephen hemmingerd0d66832017-08-18 13:46:19 -0700935 return error;
Michael Daltona953be52014-01-16 22:23:28 -0800936
WANG Cong6b53daf2014-07-23 16:09:10 -0700937 if (dev->sysfs_rx_queue_group) {
938 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
stephen hemmingerd0d66832017-08-18 13:46:19 -0700939 if (error) {
940 kobject_put(kobj);
941 return error;
942 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000943 }
944
945 kobject_uevent(kobj, KOBJ_ADD);
Tom Herbertfe822242010-11-09 10:47:38 +0000946 dev_hold(queue->dev);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000947
948 return error;
949}
Paul Bolle80dd6ea2014-02-09 14:07:11 +0100950#endif /* CONFIG_SYSFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000951
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000952int
WANG Cong6b53daf2014-07-23 16:09:10 -0700953net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000954{
Michael Daltona953be52014-01-16 22:23:28 -0800955#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000956 int i;
957 int error = 0;
958
Michael Daltona953be52014-01-16 22:23:28 -0800959#ifndef CONFIG_RPS
WANG Cong6b53daf2014-07-23 16:09:10 -0700960 if (!dev->sysfs_rx_queue_group)
Michael Daltona953be52014-01-16 22:23:28 -0800961 return 0;
962#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000963 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700964 error = rx_queue_add_kobject(dev, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000965 if (error) {
966 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000967 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000968 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000969 }
970
Michael Daltona953be52014-01-16 22:23:28 -0800971 while (--i >= new_num) {
Andrey Vagin002d8a12016-10-24 19:09:53 -0700972 struct kobject *kobj = &dev->_rx[i].kobj;
973
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300974 if (!refcount_read(&dev_net(dev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -0700975 kobj->uevent_suppress = 1;
WANG Cong6b53daf2014-07-23 16:09:10 -0700976 if (dev->sysfs_rx_queue_group)
Andrey Vagin002d8a12016-10-24 19:09:53 -0700977 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
978 kobject_put(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800979 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000980
981 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000982#else
983 return 0;
984#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000985}
986
david decotignyccf5ff62011-11-16 12:15:10 +0000987#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000988/*
989 * netdev_queue sysfs structures and functions.
990 */
991struct netdev_queue_attribute {
992 struct attribute attr;
stephen hemminger718ad682017-08-18 13:46:24 -0700993 ssize_t (*show)(struct netdev_queue *queue, char *buf);
Tom Herbert1d24eb42010-11-21 13:17:27 +0000994 ssize_t (*store)(struct netdev_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -0700995 const char *buf, size_t len);
Tom Herbert1d24eb42010-11-21 13:17:27 +0000996};
stephen hemminger6648c652017-08-18 13:46:28 -0700997#define to_netdev_queue_attr(_attr) \
998 container_of(_attr, struct netdev_queue_attribute, attr)
Tom Herbert1d24eb42010-11-21 13:17:27 +0000999
1000#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1001
1002static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1003 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001004{
stephen hemminger667e4272017-08-18 13:46:27 -07001005 const struct netdev_queue_attribute *attribute
1006 = to_netdev_queue_attr(attr);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001007 struct netdev_queue *queue = to_netdev_queue(kobj);
1008
1009 if (!attribute->show)
1010 return -EIO;
1011
stephen hemminger718ad682017-08-18 13:46:24 -07001012 return attribute->show(queue, buf);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001013}
1014
1015static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1016 struct attribute *attr,
1017 const char *buf, size_t count)
1018{
stephen hemminger667e4272017-08-18 13:46:27 -07001019 const struct netdev_queue_attribute *attribute
1020 = to_netdev_queue_attr(attr);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001021 struct netdev_queue *queue = to_netdev_queue(kobj);
1022
1023 if (!attribute->store)
1024 return -EIO;
1025
stephen hemminger718ad682017-08-18 13:46:24 -07001026 return attribute->store(queue, buf, count);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001027}
1028
1029static const struct sysfs_ops netdev_queue_sysfs_ops = {
1030 .show = netdev_queue_attr_show,
1031 .store = netdev_queue_attr_store,
1032};
1033
stephen hemminger2b9c7582017-08-18 13:46:26 -07001034static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
david decotignyccf5ff62011-11-16 12:15:10 +00001035{
1036 unsigned long trans_timeout;
1037
1038 spin_lock_irq(&queue->_xmit_lock);
1039 trans_timeout = queue->trans_timeout;
1040 spin_unlock_irq(&queue->_xmit_lock);
1041
1042 return sprintf(buf, "%lu", trans_timeout);
1043}
1044
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001045static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
John Fastabend822b3b22015-03-18 14:57:33 +02001046{
1047 struct net_device *dev = queue->dev;
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001048 unsigned int i;
John Fastabend822b3b22015-03-18 14:57:33 +02001049
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001050 i = queue - dev->_tx;
John Fastabend822b3b22015-03-18 14:57:33 +02001051 BUG_ON(i >= dev->num_tx_queues);
1052
1053 return i;
1054}
1055
stephen hemminger2b9c7582017-08-18 13:46:26 -07001056static ssize_t traffic_class_show(struct netdev_queue *queue,
Alexander Duyck8d059b02016-10-28 11:43:49 -04001057 char *buf)
1058{
1059 struct net_device *dev = queue->dev;
Alexander Duyckd7be9772018-07-09 12:19:32 -04001060 int index;
1061 int tc;
Alexander Duyck8d059b02016-10-28 11:43:49 -04001062
Alexander Duyckd7be9772018-07-09 12:19:32 -04001063 if (!netif_is_multiqueue(dev))
1064 return -ENOENT;
1065
1066 index = get_netdev_queue_index(queue);
Alexander Duyckffcfe252018-07-09 12:19:38 -04001067
1068 /* If queue belongs to subordinate dev use its TC mapping */
1069 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1070
Alexander Duyckd7be9772018-07-09 12:19:32 -04001071 tc = netdev_txq_to_tc(dev, index);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001072 if (tc < 0)
1073 return -EINVAL;
1074
Alexander Duyckffcfe252018-07-09 12:19:38 -04001075 /* We can report the traffic class one of two ways:
1076 * Subordinate device traffic classes are reported with the traffic
1077 * class first, and then the subordinate class so for example TC0 on
1078 * subordinate device 2 will be reported as "0-2". If the queue
1079 * belongs to the root device it will be reported with just the
1080 * traffic class, so just "0" for TC 0 for example.
1081 */
1082 return dev->num_tc < 0 ? sprintf(buf, "%u%d\n", tc, dev->num_tc) :
1083 sprintf(buf, "%u\n", tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001084}
1085
1086#ifdef CONFIG_XPS
stephen hemminger2b9c7582017-08-18 13:46:26 -07001087static ssize_t tx_maxrate_show(struct netdev_queue *queue,
John Fastabend822b3b22015-03-18 14:57:33 +02001088 char *buf)
1089{
1090 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1091}
1092
stephen hemminger2b9c7582017-08-18 13:46:26 -07001093static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1094 const char *buf, size_t len)
John Fastabend822b3b22015-03-18 14:57:33 +02001095{
1096 struct net_device *dev = queue->dev;
1097 int err, index = get_netdev_queue_index(queue);
1098 u32 rate = 0;
1099
Tyler Hicks3033fce2018-07-20 21:56:51 +00001100 if (!capable(CAP_NET_ADMIN))
1101 return -EPERM;
1102
John Fastabend822b3b22015-03-18 14:57:33 +02001103 err = kstrtou32(buf, 10, &rate);
1104 if (err < 0)
1105 return err;
1106
1107 if (!rtnl_trylock())
1108 return restart_syscall();
1109
1110 err = -EOPNOTSUPP;
1111 if (dev->netdev_ops->ndo_set_tx_maxrate)
1112 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1113
1114 rtnl_unlock();
1115 if (!err) {
1116 queue->tx_maxrate = rate;
1117 return len;
1118 }
1119 return err;
1120}
1121
stephen hemminger2b9c7582017-08-18 13:46:26 -07001122static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1123 = __ATTR_RW(tx_maxrate);
John Fastabend822b3b22015-03-18 14:57:33 +02001124#endif
1125
stephen hemminger2b9c7582017-08-18 13:46:26 -07001126static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1127 = __ATTR_RO(tx_timeout);
david decotignyccf5ff62011-11-16 12:15:10 +00001128
stephen hemminger2b9c7582017-08-18 13:46:26 -07001129static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1130 = __ATTR_RO(traffic_class);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001131
Tom Herbert114cf582011-11-28 16:33:09 +00001132#ifdef CONFIG_BQL
1133/*
1134 * Byte queue limits sysfs structures and functions.
1135 */
1136static ssize_t bql_show(char *buf, unsigned int value)
1137{
1138 return sprintf(buf, "%u\n", value);
1139}
1140
1141static ssize_t bql_set(const char *buf, const size_t count,
1142 unsigned int *pvalue)
1143{
1144 unsigned int value;
1145 int err;
1146
stephen hemminger6648c652017-08-18 13:46:28 -07001147 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
Tom Herbert114cf582011-11-28 16:33:09 +00001148 value = DQL_MAX_LIMIT;
stephen hemminger6648c652017-08-18 13:46:28 -07001149 } else {
Tom Herbert114cf582011-11-28 16:33:09 +00001150 err = kstrtouint(buf, 10, &value);
1151 if (err < 0)
1152 return err;
1153 if (value > DQL_MAX_LIMIT)
1154 return -EINVAL;
1155 }
1156
1157 *pvalue = value;
1158
1159 return count;
1160}
1161
1162static ssize_t bql_show_hold_time(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001163 char *buf)
1164{
1165 struct dql *dql = &queue->dql;
1166
1167 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1168}
1169
1170static ssize_t bql_set_hold_time(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001171 const char *buf, size_t len)
1172{
1173 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +00001174 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +00001175 int err;
1176
1177 err = kstrtouint(buf, 10, &value);
1178 if (err < 0)
1179 return err;
1180
1181 dql->slack_hold_time = msecs_to_jiffies(value);
1182
1183 return len;
1184}
1185
stephen hemminger170c6582017-08-18 13:46:25 -07001186static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
Joe Perchesd6444062018-03-23 15:54:38 -07001187 = __ATTR(hold_time, 0644,
stephen hemminger170c6582017-08-18 13:46:25 -07001188 bql_show_hold_time, bql_set_hold_time);
Tom Herbert114cf582011-11-28 16:33:09 +00001189
1190static ssize_t bql_show_inflight(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001191 char *buf)
1192{
1193 struct dql *dql = &queue->dql;
1194
1195 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1196}
1197
stephen hemminger170c6582017-08-18 13:46:25 -07001198static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
Joe Perchesd6444062018-03-23 15:54:38 -07001199 __ATTR(inflight, 0444, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +00001200
1201#define BQL_ATTR(NAME, FIELD) \
1202static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
Tom Herbert114cf582011-11-28 16:33:09 +00001203 char *buf) \
1204{ \
1205 return bql_show(buf, queue->dql.FIELD); \
1206} \
1207 \
1208static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
Tom Herbert114cf582011-11-28 16:33:09 +00001209 const char *buf, size_t len) \
1210{ \
1211 return bql_set(buf, len, &queue->dql.FIELD); \
1212} \
1213 \
stephen hemminger170c6582017-08-18 13:46:25 -07001214static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
Joe Perchesd6444062018-03-23 15:54:38 -07001215 = __ATTR(NAME, 0644, \
stephen hemminger170c6582017-08-18 13:46:25 -07001216 bql_show_ ## NAME, bql_set_ ## NAME)
Tom Herbert114cf582011-11-28 16:33:09 +00001217
stephen hemminger170c6582017-08-18 13:46:25 -07001218BQL_ATTR(limit, limit);
1219BQL_ATTR(limit_max, max_limit);
1220BQL_ATTR(limit_min, min_limit);
Tom Herbert114cf582011-11-28 16:33:09 +00001221
stephen hemminger170c6582017-08-18 13:46:25 -07001222static struct attribute *dql_attrs[] __ro_after_init = {
Tom Herbert114cf582011-11-28 16:33:09 +00001223 &bql_limit_attribute.attr,
1224 &bql_limit_max_attribute.attr,
1225 &bql_limit_min_attribute.attr,
1226 &bql_hold_time_attribute.attr,
1227 &bql_inflight_attribute.attr,
1228 NULL
1229};
1230
Arvind Yadav38ef00c2017-06-29 16:31:26 +05301231static const struct attribute_group dql_group = {
Tom Herbert114cf582011-11-28 16:33:09 +00001232 .name = "byte_queue_limits",
1233 .attrs = dql_attrs,
1234};
1235#endif /* CONFIG_BQL */
1236
david decotignyccf5ff62011-11-16 12:15:10 +00001237#ifdef CONFIG_XPS
stephen hemminger2b9c7582017-08-18 13:46:26 -07001238static ssize_t xps_cpus_show(struct netdev_queue *queue,
1239 char *buf)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001240{
1241 struct net_device *dev = queue->dev;
Alexander Duyck184c4492016-10-28 11:50:13 -04001242 int cpu, len, num_tc = 1, tc = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001243 struct xps_dev_maps *dev_maps;
1244 cpumask_var_t mask;
1245 unsigned long index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001246
Alexander Duyckd7be9772018-07-09 12:19:32 -04001247 if (!netif_is_multiqueue(dev))
1248 return -ENOENT;
1249
Tom Herbert1d24eb42010-11-21 13:17:27 +00001250 index = get_netdev_queue_index(queue);
1251
Alexander Duyck184c4492016-10-28 11:50:13 -04001252 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04001253 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04001254 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04001255 if (num_tc < 0)
1256 return -EINVAL;
1257
1258 /* If queue belongs to subordinate dev use its map */
1259 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1260
Alexander Duyck184c4492016-10-28 11:50:13 -04001261 tc = netdev_txq_to_tc(dev, index);
1262 if (tc < 0)
1263 return -EINVAL;
1264 }
1265
Alexander Duyck664088f2018-05-31 15:59:46 -04001266 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1267 return -ENOMEM;
1268
Tom Herbert1d24eb42010-11-21 13:17:27 +00001269 rcu_read_lock();
Amritha Nambiar80d19662018-06-29 21:26:41 -07001270 dev_maps = rcu_dereference(dev->xps_cpus_map);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001271 if (dev_maps) {
Alexander Duyck184c4492016-10-28 11:50:13 -04001272 for_each_possible_cpu(cpu) {
1273 int i, tci = cpu * num_tc + tc;
1274 struct xps_map *map;
1275
Amritha Nambiar80d19662018-06-29 21:26:41 -07001276 map = rcu_dereference(dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04001277 if (!map)
1278 continue;
1279
1280 for (i = map->len; i--;) {
1281 if (map->queues[i] == index) {
1282 cpumask_set_cpu(cpu, mask);
1283 break;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001284 }
1285 }
1286 }
1287 }
1288 rcu_read_unlock();
1289
Tejun Heof0906822015-02-13 14:37:42 -08001290 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
Tom Herbert1d24eb42010-11-21 13:17:27 +00001291 free_cpumask_var(mask);
Tejun Heof0906822015-02-13 14:37:42 -08001292 return len < PAGE_SIZE ? len : -EINVAL;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001293}
1294
stephen hemminger2b9c7582017-08-18 13:46:26 -07001295static ssize_t xps_cpus_store(struct netdev_queue *queue,
1296 const char *buf, size_t len)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001297{
1298 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001299 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001300 cpumask_var_t mask;
1301 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001302
Alexander Duyckd7be9772018-07-09 12:19:32 -04001303 if (!netif_is_multiqueue(dev))
1304 return -ENOENT;
1305
Tom Herbert1d24eb42010-11-21 13:17:27 +00001306 if (!capable(CAP_NET_ADMIN))
1307 return -EPERM;
1308
1309 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1310 return -ENOMEM;
1311
1312 index = get_netdev_queue_index(queue);
1313
1314 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1315 if (err) {
1316 free_cpumask_var(mask);
1317 return err;
1318 }
1319
Alexander Duyck537c00d2013-01-10 08:57:02 +00001320 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001321
1322 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001323
Alexander Duyck537c00d2013-01-10 08:57:02 +00001324 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001325}
1326
stephen hemminger2b9c7582017-08-18 13:46:26 -07001327static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1328 = __ATTR_RW(xps_cpus);
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001329
1330static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1331{
1332 struct net_device *dev = queue->dev;
1333 struct xps_dev_maps *dev_maps;
1334 unsigned long *mask, index;
1335 int j, len, num_tc = 1, tc = 0;
1336
1337 index = get_netdev_queue_index(queue);
1338
1339 if (dev->num_tc) {
1340 num_tc = dev->num_tc;
1341 tc = netdev_txq_to_tc(dev, index);
1342 if (tc < 0)
1343 return -EINVAL;
1344 }
1345 mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
1346 GFP_KERNEL);
1347 if (!mask)
1348 return -ENOMEM;
1349
1350 rcu_read_lock();
1351 dev_maps = rcu_dereference(dev->xps_rxqs_map);
1352 if (!dev_maps)
1353 goto out_no_maps;
1354
1355 for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues),
1356 j < dev->num_rx_queues;) {
1357 int i, tci = j * num_tc + tc;
1358 struct xps_map *map;
1359
1360 map = rcu_dereference(dev_maps->attr_map[tci]);
1361 if (!map)
1362 continue;
1363
1364 for (i = map->len; i--;) {
1365 if (map->queues[i] == index) {
1366 set_bit(j, mask);
1367 break;
1368 }
1369 }
1370 }
1371out_no_maps:
1372 rcu_read_unlock();
1373
1374 len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues);
1375 kfree(mask);
1376
1377 return len < PAGE_SIZE ? len : -EINVAL;
1378}
1379
1380static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1381 size_t len)
1382{
1383 struct net_device *dev = queue->dev;
1384 struct net *net = dev_net(dev);
1385 unsigned long *mask, index;
1386 int err;
1387
1388 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1389 return -EPERM;
1390
1391 mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
1392 GFP_KERNEL);
1393 if (!mask)
1394 return -ENOMEM;
1395
1396 index = get_netdev_queue_index(queue);
1397
1398 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1399 if (err) {
1400 kfree(mask);
1401 return err;
1402 }
1403
Andrei Vagin4d99f662018-08-08 20:07:35 -07001404 cpus_read_lock();
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001405 err = __netif_set_xps_queue(dev, mask, index, true);
Andrei Vagin4d99f662018-08-08 20:07:35 -07001406 cpus_read_unlock();
1407
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001408 kfree(mask);
1409 return err ? : len;
1410}
1411
1412static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1413 = __ATTR_RW(xps_rxqs);
david decotignyccf5ff62011-11-16 12:15:10 +00001414#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001415
stephen hemminger2b9c7582017-08-18 13:46:26 -07001416static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
david decotignyccf5ff62011-11-16 12:15:10 +00001417 &queue_trans_timeout.attr,
Alexander Duyck8d059b02016-10-28 11:43:49 -04001418 &queue_traffic_class.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001419#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001420 &xps_cpus_attribute.attr,
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001421 &xps_rxqs_attribute.attr,
John Fastabend822b3b22015-03-18 14:57:33 +02001422 &queue_tx_maxrate.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001423#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001424 NULL
1425};
1426
1427static void netdev_queue_release(struct kobject *kobj)
1428{
1429 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001430
Tom Herbert1d24eb42010-11-21 13:17:27 +00001431 memset(kobj, 0, sizeof(*kobj));
1432 dev_put(queue->dev);
1433}
1434
Weilong Chen82ef3d52014-01-16 17:24:31 +08001435static const void *netdev_queue_namespace(struct kobject *kobj)
1436{
1437 struct netdev_queue *queue = to_netdev_queue(kobj);
1438 struct device *dev = &queue->dev->dev;
1439 const void *ns = NULL;
1440
1441 if (dev->class && dev->class->ns_type)
1442 ns = dev->class->namespace(dev);
1443
1444 return ns;
1445}
1446
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001447static void netdev_queue_get_ownership(struct kobject *kobj,
1448 kuid_t *uid, kgid_t *gid)
1449{
1450 const struct net *net = netdev_queue_namespace(kobj);
1451
1452 net_ns_get_ownership(net, uid, gid);
1453}
1454
stephen hemminger2b9c7582017-08-18 13:46:26 -07001455static struct kobj_type netdev_queue_ktype __ro_after_init = {
Tom Herbert1d24eb42010-11-21 13:17:27 +00001456 .sysfs_ops = &netdev_queue_sysfs_ops,
1457 .release = netdev_queue_release,
1458 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001459 .namespace = netdev_queue_namespace,
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001460 .get_ownership = netdev_queue_get_ownership,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001461};
1462
WANG Cong6b53daf2014-07-23 16:09:10 -07001463static int netdev_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001464{
WANG Cong6b53daf2014-07-23 16:09:10 -07001465 struct netdev_queue *queue = dev->_tx + index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001466 struct kobject *kobj = &queue->kobj;
1467 int error = 0;
1468
WANG Cong6b53daf2014-07-23 16:09:10 -07001469 kobj->kset = dev->queues_kset;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001470 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
stephen hemminger6648c652017-08-18 13:46:28 -07001471 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001472 if (error)
stephen hemmingerd0d66832017-08-18 13:46:19 -07001473 return error;
Tom Herbert114cf582011-11-28 16:33:09 +00001474
1475#ifdef CONFIG_BQL
1476 error = sysfs_create_group(kobj, &dql_group);
stephen hemmingerd0d66832017-08-18 13:46:19 -07001477 if (error) {
1478 kobject_put(kobj);
1479 return error;
1480 }
Tom Herbert114cf582011-11-28 16:33:09 +00001481#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001482
1483 kobject_uevent(kobj, KOBJ_ADD);
1484 dev_hold(queue->dev);
1485
Tom Herbert114cf582011-11-28 16:33:09 +00001486 return 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001487}
david decotignyccf5ff62011-11-16 12:15:10 +00001488#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001489
1490int
WANG Cong6b53daf2014-07-23 16:09:10 -07001491netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001492{
david decotignyccf5ff62011-11-16 12:15:10 +00001493#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001494 int i;
1495 int error = 0;
1496
1497 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001498 error = netdev_queue_add_kobject(dev, i);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001499 if (error) {
1500 new_num = old_num;
1501 break;
1502 }
1503 }
1504
Tom Herbert114cf582011-11-28 16:33:09 +00001505 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001506 struct netdev_queue *queue = dev->_tx + i;
Tom Herbert114cf582011-11-28 16:33:09 +00001507
Kirill Tkhai273c28b2018-01-12 18:28:31 +03001508 if (!refcount_read(&dev_net(dev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -07001509 queue->kobj.uevent_suppress = 1;
Tom Herbert114cf582011-11-28 16:33:09 +00001510#ifdef CONFIG_BQL
1511 sysfs_remove_group(&queue->kobj, &dql_group);
1512#endif
1513 kobject_put(&queue->kobj);
1514 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001515
1516 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001517#else
1518 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001519#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001520}
1521
WANG Cong6b53daf2014-07-23 16:09:10 -07001522static int register_queue_kobjects(struct net_device *dev)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001523{
Tom Herbertbf264142010-11-26 08:36:09 +00001524 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001525
david decotignyccf5ff62011-11-16 12:15:10 +00001526#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001527 dev->queues_kset = kset_create_and_add("queues",
stephen hemminger6648c652017-08-18 13:46:28 -07001528 NULL, &dev->dev.kobj);
WANG Cong6b53daf2014-07-23 16:09:10 -07001529 if (!dev->queues_kset)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001530 return -ENOMEM;
WANG Cong6b53daf2014-07-23 16:09:10 -07001531 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001532#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001533 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001534
WANG Cong6b53daf2014-07-23 16:09:10 -07001535 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001536 if (error)
1537 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001538 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001539
WANG Cong6b53daf2014-07-23 16:09:10 -07001540 error = netdev_queue_update_kobjects(dev, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001541 if (error)
1542 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001543 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001544
1545 return 0;
1546
1547error:
WANG Cong6b53daf2014-07-23 16:09:10 -07001548 netdev_queue_update_kobjects(dev, txq, 0);
1549 net_rx_queue_update_kobjects(dev, rxq, 0);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001550 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001551}
1552
WANG Cong6b53daf2014-07-23 16:09:10 -07001553static void remove_queue_kobjects(struct net_device *dev)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001554{
Tom Herbertbf264142010-11-26 08:36:09 +00001555 int real_rx = 0, real_tx = 0;
1556
Michael Daltona953be52014-01-16 22:23:28 -08001557#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001558 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001559#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001560 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001561
WANG Cong6b53daf2014-07-23 16:09:10 -07001562 net_rx_queue_update_kobjects(dev, real_rx, 0);
1563 netdev_queue_update_kobjects(dev, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001564#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001565 kset_unregister(dev->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001566#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001567}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001568
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001569static bool net_current_may_mount(void)
1570{
1571 struct net *net = current->nsproxy->net_ns;
1572
1573 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1574}
1575
Al Viroa685e082011-06-08 21:13:01 -04001576static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001577{
Al Viroa685e082011-06-08 21:13:01 -04001578 struct net *ns = current->nsproxy->net_ns;
1579#ifdef CONFIG_NET_NS
1580 if (ns)
Reshetova, Elenac122e142017-06-30 13:08:08 +03001581 refcount_inc(&ns->passive);
Al Viroa685e082011-06-08 21:13:01 -04001582#endif
1583 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001584}
1585
1586static const void *net_initial_ns(void)
1587{
1588 return &init_net;
1589}
1590
1591static const void *net_netlink_ns(struct sock *sk)
1592{
1593 return sock_net(sk);
1594}
1595
stephen hemminger737aec52017-08-18 13:46:22 -07001596const struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001597 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001598 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001599 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001600 .netlink_ns = net_netlink_ns,
1601 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001602 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001603};
Johannes Berg04600792010-08-05 17:45:15 +02001604EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001605
Kay Sievers7eff2e72007-08-14 15:15:12 +02001606static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001608 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001609 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Kay Sievers312c0042005-11-16 09:00:00 +01001611 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001612 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001613 if (retval)
1614 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001616 /* pass ifindex to uevent.
1617 * ifindex is useful as it won't change (interface name may change)
stephen hemminger6648c652017-08-18 13:46:28 -07001618 * and is what RtNetlink uses natively.
1619 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001620 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001621
Eric Rannaudbf624562007-03-30 22:23:12 -07001622exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001623 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001627 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001628 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001630static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001632 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 BUG_ON(dev->reg_state != NETREG_RELEASED);
1635
Florian Westphal6c557002017-10-02 23:50:05 +02001636 /* no need to wait for rcu grace period:
1637 * device is dead and about to be freed.
1638 */
1639 kfree(rcu_access_pointer(dev->ifalias));
Eric Dumazet74d332c2013-10-30 13:10:44 -07001640 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001643static const void *net_namespace(struct device *d)
1644{
Geliang Tang5c294822015-12-22 23:11:49 +08001645 struct net_device *dev = to_net_dev(d);
1646
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001647 return dev_net(dev);
1648}
1649
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001650static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
1651{
1652 struct net_device *dev = to_net_dev(d);
1653 const struct net *net = dev_net(dev);
1654
1655 net_ns_get_ownership(net, uid, gid);
1656}
1657
stephen hemmingere6d473e2017-08-18 13:46:21 -07001658static struct class net_class __ro_after_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001660 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001661 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001662 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001663 .ns_type = &net_ns_type_operations,
1664 .namespace = net_namespace,
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001665 .get_ownership = net_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666};
1667
Florian Fainelliaa836df2015-03-09 14:31:20 -07001668#ifdef CONFIG_OF_NET
1669static int of_dev_node_match(struct device *dev, const void *data)
1670{
1671 int ret = 0;
1672
1673 if (dev->parent)
1674 ret = dev->parent->of_node == data;
1675
1676 return ret == 0 ? dev->of_node == data : ret;
1677}
1678
Russell King9861f722015-09-24 20:36:33 +01001679/*
1680 * of_find_net_device_by_node - lookup the net device for the device node
1681 * @np: OF device node
1682 *
1683 * Looks up the net_device structure corresponding with the device node.
1684 * If successful, returns a pointer to the net_device with the embedded
1685 * struct device refcount incremented by one, or NULL on failure. The
1686 * refcount must be dropped when done with the net_device.
1687 */
Florian Fainelliaa836df2015-03-09 14:31:20 -07001688struct net_device *of_find_net_device_by_node(struct device_node *np)
1689{
1690 struct device *dev;
1691
1692 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1693 if (!dev)
1694 return NULL;
1695
1696 return to_net_dev(dev);
1697}
1698EXPORT_SYMBOL(of_find_net_device_by_node);
1699#endif
1700
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001701/* Delete sysfs entries but hold kobject reference until after all
1702 * netdev references are gone.
1703 */
WANG Cong6b53daf2014-07-23 16:09:10 -07001704void netdev_unregister_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
stephen hemminger6648c652017-08-18 13:46:28 -07001706 struct device *dev = &ndev->dev;
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001707
Kirill Tkhai273c28b2018-01-12 18:28:31 +03001708 if (!refcount_read(&dev_net(ndev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -07001709 dev_set_uevent_suppress(dev, 1);
1710
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001711 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001712
WANG Cong6b53daf2014-07-23 16:09:10 -07001713 remove_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001714
Ming Lei9802c8e2013-02-22 16:34:16 -08001715 pm_runtime_set_memalloc_noio(dev, false);
1716
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001717 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/* Create sysfs entries for network device. */
WANG Cong6b53daf2014-07-23 16:09:10 -07001721int netdev_register_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
stephen hemminger6648c652017-08-18 13:46:28 -07001723 struct device *dev = &ndev->dev;
WANG Cong6b53daf2014-07-23 16:09:10 -07001724 const struct attribute_group **groups = ndev->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001725 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001727 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001728 dev->class = &net_class;
WANG Cong6b53daf2014-07-23 16:09:10 -07001729 dev->platform_data = ndev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001730 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
WANG Cong6b53daf2014-07-23 16:09:10 -07001732 dev_set_name(dev, "%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001734#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001735 /* Allow for a device specific group */
1736 if (*groups)
1737 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001739 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001740
1741#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
WANG Cong6b53daf2014-07-23 16:09:10 -07001742 if (ndev->ieee80211_ptr)
Johannes Berg38c1a012012-11-16 20:46:19 +01001743 *groups++ = &wireless_group;
1744#if IS_ENABLED(CONFIG_WIRELESS_EXT)
WANG Cong6b53daf2014-07-23 16:09:10 -07001745 else if (ndev->wireless_handlers)
Johannes Berg38c1a012012-11-16 20:46:19 +01001746 *groups++ = &wireless_group;
1747#endif
1748#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001749#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Tom Herbert0a9627f2010-03-16 08:03:29 +00001751 error = device_add(dev);
1752 if (error)
1753 return error;
1754
WANG Cong6b53daf2014-07-23 16:09:10 -07001755 error = register_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001756 if (error) {
1757 device_del(dev);
1758 return error;
1759 }
1760
Ming Lei9802c8e2013-02-22 16:34:16 -08001761 pm_runtime_set_memalloc_noio(dev, true);
1762
Tom Herbert0a9627f2010-03-16 08:03:29 +00001763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
stephen hemmingerb793dc52017-08-18 13:46:20 -07001766int netdev_class_create_file_ns(const struct class_attribute *class_attr,
Tejun Heo58292cbe2013-09-11 22:29:04 -04001767 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001768{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001769 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001770}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001771EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001772
stephen hemmingerb793dc52017-08-18 13:46:20 -07001773void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
Tejun Heo58292cbe2013-09-11 22:29:04 -04001774 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001775{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001776 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001777}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001778EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001779
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001780int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001782 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return class_register(&net_class);
1784}