blob: 7614a4f42bfc0ffb8e7a8d2f8ba8d18ca3514d43 [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 Decotigny8ae6dac2011-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 Decotigny8ae6dac2011-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
Jouni Hogander33c540f2019-12-17 13:46:34 +0200931 /* Kobject_put later will trigger rx_queue_release call which
932 * decreases dev refcount: Take that reference here
933 */
934 dev_hold(queue->dev);
935
WANG Cong6b53daf2014-07-23 16:09:10 -0700936 kobj->kset = dev->queues_kset;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000937 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
stephen hemminger6648c652017-08-18 13:46:28 -0700938 "rx-%u", index);
Michael Daltona953be52014-01-16 22:23:28 -0800939 if (error)
Jouni Hogander60e71542019-11-20 09:08:16 +0200940 goto err;
Michael Daltona953be52014-01-16 22:23:28 -0800941
WANG Cong6b53daf2014-07-23 16:09:10 -0700942 if (dev->sysfs_rx_queue_group) {
943 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
Jouni Hogander60e71542019-11-20 09:08:16 +0200944 if (error)
945 goto err;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000946 }
947
948 kobject_uevent(kobj, KOBJ_ADD);
949
950 return error;
Jouni Hogander60e71542019-11-20 09:08:16 +0200951
952err:
953 kobject_put(kobj);
954 return error;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000955}
Paul Bolle80dd6ea2014-02-09 14:07:11 +0100956#endif /* CONFIG_SYSFS */
Tom Herbert0a9627f2010-03-16 08:03:29 +0000957
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000958int
WANG Cong6b53daf2014-07-23 16:09:10 -0700959net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert0a9627f2010-03-16 08:03:29 +0000960{
Michael Daltona953be52014-01-16 22:23:28 -0800961#ifdef CONFIG_SYSFS
Tom Herbert0a9627f2010-03-16 08:03:29 +0000962 int i;
963 int error = 0;
964
Michael Daltona953be52014-01-16 22:23:28 -0800965#ifndef CONFIG_RPS
WANG Cong6b53daf2014-07-23 16:09:10 -0700966 if (!dev->sysfs_rx_queue_group)
Michael Daltona953be52014-01-16 22:23:28 -0800967 return 0;
968#endif
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000969 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -0700970 error = rx_queue_add_kobject(dev, i);
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000971 if (error) {
972 new_num = old_num;
Tom Herbert0a9627f2010-03-16 08:03:29 +0000973 break;
Ben Hutchings62fe0b42010-09-27 08:24:33 +0000974 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000975 }
976
Michael Daltona953be52014-01-16 22:23:28 -0800977 while (--i >= new_num) {
Andrey Vagin002d8a12016-10-24 19:09:53 -0700978 struct kobject *kobj = &dev->_rx[i].kobj;
979
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300980 if (!refcount_read(&dev_net(dev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -0700981 kobj->uevent_suppress = 1;
WANG Cong6b53daf2014-07-23 16:09:10 -0700982 if (dev->sysfs_rx_queue_group)
Andrey Vagin002d8a12016-10-24 19:09:53 -0700983 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
984 kobject_put(kobj);
Michael Daltona953be52014-01-16 22:23:28 -0800985 }
Tom Herbert0a9627f2010-03-16 08:03:29 +0000986
987 return error;
Tom Herbertbf264142010-11-26 08:36:09 +0000988#else
989 return 0;
990#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +0000991}
992
david decotignyccf5ff62011-11-16 12:15:10 +0000993#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +0000994/*
995 * netdev_queue sysfs structures and functions.
996 */
997struct netdev_queue_attribute {
998 struct attribute attr;
stephen hemminger718ad682017-08-18 13:46:24 -0700999 ssize_t (*show)(struct netdev_queue *queue, char *buf);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001000 ssize_t (*store)(struct netdev_queue *queue,
stephen hemminger718ad682017-08-18 13:46:24 -07001001 const char *buf, size_t len);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001002};
stephen hemminger6648c652017-08-18 13:46:28 -07001003#define to_netdev_queue_attr(_attr) \
1004 container_of(_attr, struct netdev_queue_attribute, attr)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001005
1006#define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1007
1008static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1009 struct attribute *attr, char *buf)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001010{
stephen hemminger667e4272017-08-18 13:46:27 -07001011 const struct netdev_queue_attribute *attribute
1012 = to_netdev_queue_attr(attr);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001013 struct netdev_queue *queue = to_netdev_queue(kobj);
1014
1015 if (!attribute->show)
1016 return -EIO;
1017
stephen hemminger718ad682017-08-18 13:46:24 -07001018 return attribute->show(queue, buf);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001019}
1020
1021static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1022 struct attribute *attr,
1023 const char *buf, size_t count)
1024{
stephen hemminger667e4272017-08-18 13:46:27 -07001025 const struct netdev_queue_attribute *attribute
1026 = to_netdev_queue_attr(attr);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001027 struct netdev_queue *queue = to_netdev_queue(kobj);
1028
1029 if (!attribute->store)
1030 return -EIO;
1031
stephen hemminger718ad682017-08-18 13:46:24 -07001032 return attribute->store(queue, buf, count);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001033}
1034
1035static const struct sysfs_ops netdev_queue_sysfs_ops = {
1036 .show = netdev_queue_attr_show,
1037 .store = netdev_queue_attr_store,
1038};
1039
stephen hemminger2b9c7582017-08-18 13:46:26 -07001040static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
david decotignyccf5ff62011-11-16 12:15:10 +00001041{
1042 unsigned long trans_timeout;
1043
1044 spin_lock_irq(&queue->_xmit_lock);
1045 trans_timeout = queue->trans_timeout;
1046 spin_unlock_irq(&queue->_xmit_lock);
1047
1048 return sprintf(buf, "%lu", trans_timeout);
1049}
1050
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001051static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
John Fastabend822b3b22015-03-18 14:57:33 +02001052{
1053 struct net_device *dev = queue->dev;
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001054 unsigned int i;
John Fastabend822b3b22015-03-18 14:57:33 +02001055
Thadeu Lima de Souza Cascardoc4047f52015-09-15 18:28:00 -03001056 i = queue - dev->_tx;
John Fastabend822b3b22015-03-18 14:57:33 +02001057 BUG_ON(i >= dev->num_tx_queues);
1058
1059 return i;
1060}
1061
stephen hemminger2b9c7582017-08-18 13:46:26 -07001062static ssize_t traffic_class_show(struct netdev_queue *queue,
Alexander Duyck8d059b02016-10-28 11:43:49 -04001063 char *buf)
1064{
1065 struct net_device *dev = queue->dev;
Alexander Duyckd7be9772018-07-09 12:19:32 -04001066 int index;
1067 int tc;
Alexander Duyck8d059b02016-10-28 11:43:49 -04001068
Alexander Duyckd7be9772018-07-09 12:19:32 -04001069 if (!netif_is_multiqueue(dev))
1070 return -ENOENT;
1071
1072 index = get_netdev_queue_index(queue);
Alexander Duyckffcfe252018-07-09 12:19:38 -04001073
1074 /* If queue belongs to subordinate dev use its TC mapping */
1075 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1076
Alexander Duyckd7be9772018-07-09 12:19:32 -04001077 tc = netdev_txq_to_tc(dev, index);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001078 if (tc < 0)
1079 return -EINVAL;
1080
Alexander Duyckffcfe252018-07-09 12:19:38 -04001081 /* We can report the traffic class one of two ways:
1082 * Subordinate device traffic classes are reported with the traffic
1083 * class first, and then the subordinate class so for example TC0 on
1084 * subordinate device 2 will be reported as "0-2". If the queue
1085 * belongs to the root device it will be reported with just the
1086 * traffic class, so just "0" for TC 0 for example.
1087 */
1088 return dev->num_tc < 0 ? sprintf(buf, "%u%d\n", tc, dev->num_tc) :
1089 sprintf(buf, "%u\n", tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001090}
1091
1092#ifdef CONFIG_XPS
stephen hemminger2b9c7582017-08-18 13:46:26 -07001093static ssize_t tx_maxrate_show(struct netdev_queue *queue,
John Fastabend822b3b22015-03-18 14:57:33 +02001094 char *buf)
1095{
1096 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1097}
1098
stephen hemminger2b9c7582017-08-18 13:46:26 -07001099static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1100 const char *buf, size_t len)
John Fastabend822b3b22015-03-18 14:57:33 +02001101{
1102 struct net_device *dev = queue->dev;
1103 int err, index = get_netdev_queue_index(queue);
1104 u32 rate = 0;
1105
Tyler Hicks3033fce2018-07-20 21:56:51 +00001106 if (!capable(CAP_NET_ADMIN))
1107 return -EPERM;
1108
John Fastabend822b3b22015-03-18 14:57:33 +02001109 err = kstrtou32(buf, 10, &rate);
1110 if (err < 0)
1111 return err;
1112
1113 if (!rtnl_trylock())
1114 return restart_syscall();
1115
1116 err = -EOPNOTSUPP;
1117 if (dev->netdev_ops->ndo_set_tx_maxrate)
1118 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1119
1120 rtnl_unlock();
1121 if (!err) {
1122 queue->tx_maxrate = rate;
1123 return len;
1124 }
1125 return err;
1126}
1127
stephen hemminger2b9c7582017-08-18 13:46:26 -07001128static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1129 = __ATTR_RW(tx_maxrate);
John Fastabend822b3b22015-03-18 14:57:33 +02001130#endif
1131
stephen hemminger2b9c7582017-08-18 13:46:26 -07001132static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1133 = __ATTR_RO(tx_timeout);
david decotignyccf5ff62011-11-16 12:15:10 +00001134
stephen hemminger2b9c7582017-08-18 13:46:26 -07001135static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1136 = __ATTR_RO(traffic_class);
Alexander Duyck8d059b02016-10-28 11:43:49 -04001137
Tom Herbert114cf582011-11-28 16:33:09 +00001138#ifdef CONFIG_BQL
1139/*
1140 * Byte queue limits sysfs structures and functions.
1141 */
1142static ssize_t bql_show(char *buf, unsigned int value)
1143{
1144 return sprintf(buf, "%u\n", value);
1145}
1146
1147static ssize_t bql_set(const char *buf, const size_t count,
1148 unsigned int *pvalue)
1149{
1150 unsigned int value;
1151 int err;
1152
stephen hemminger6648c652017-08-18 13:46:28 -07001153 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
Tom Herbert114cf582011-11-28 16:33:09 +00001154 value = DQL_MAX_LIMIT;
stephen hemminger6648c652017-08-18 13:46:28 -07001155 } else {
Tom Herbert114cf582011-11-28 16:33:09 +00001156 err = kstrtouint(buf, 10, &value);
1157 if (err < 0)
1158 return err;
1159 if (value > DQL_MAX_LIMIT)
1160 return -EINVAL;
1161 }
1162
1163 *pvalue = value;
1164
1165 return count;
1166}
1167
1168static ssize_t bql_show_hold_time(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001169 char *buf)
1170{
1171 struct dql *dql = &queue->dql;
1172
1173 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1174}
1175
1176static ssize_t bql_set_hold_time(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001177 const char *buf, size_t len)
1178{
1179 struct dql *dql = &queue->dql;
Eric Dumazet95c96172012-04-15 05:58:06 +00001180 unsigned int value;
Tom Herbert114cf582011-11-28 16:33:09 +00001181 int err;
1182
1183 err = kstrtouint(buf, 10, &value);
1184 if (err < 0)
1185 return err;
1186
1187 dql->slack_hold_time = msecs_to_jiffies(value);
1188
1189 return len;
1190}
1191
stephen hemminger170c6582017-08-18 13:46:25 -07001192static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
Joe Perchesd6444062018-03-23 15:54:38 -07001193 = __ATTR(hold_time, 0644,
stephen hemminger170c6582017-08-18 13:46:25 -07001194 bql_show_hold_time, bql_set_hold_time);
Tom Herbert114cf582011-11-28 16:33:09 +00001195
1196static ssize_t bql_show_inflight(struct netdev_queue *queue,
Tom Herbert114cf582011-11-28 16:33:09 +00001197 char *buf)
1198{
1199 struct dql *dql = &queue->dql;
1200
1201 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1202}
1203
stephen hemminger170c6582017-08-18 13:46:25 -07001204static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
Joe Perchesd6444062018-03-23 15:54:38 -07001205 __ATTR(inflight, 0444, bql_show_inflight, NULL);
Tom Herbert114cf582011-11-28 16:33:09 +00001206
1207#define BQL_ATTR(NAME, FIELD) \
1208static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
Tom Herbert114cf582011-11-28 16:33:09 +00001209 char *buf) \
1210{ \
1211 return bql_show(buf, queue->dql.FIELD); \
1212} \
1213 \
1214static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
Tom Herbert114cf582011-11-28 16:33:09 +00001215 const char *buf, size_t len) \
1216{ \
1217 return bql_set(buf, len, &queue->dql.FIELD); \
1218} \
1219 \
stephen hemminger170c6582017-08-18 13:46:25 -07001220static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
Joe Perchesd6444062018-03-23 15:54:38 -07001221 = __ATTR(NAME, 0644, \
stephen hemminger170c6582017-08-18 13:46:25 -07001222 bql_show_ ## NAME, bql_set_ ## NAME)
Tom Herbert114cf582011-11-28 16:33:09 +00001223
stephen hemminger170c6582017-08-18 13:46:25 -07001224BQL_ATTR(limit, limit);
1225BQL_ATTR(limit_max, max_limit);
1226BQL_ATTR(limit_min, min_limit);
Tom Herbert114cf582011-11-28 16:33:09 +00001227
stephen hemminger170c6582017-08-18 13:46:25 -07001228static struct attribute *dql_attrs[] __ro_after_init = {
Tom Herbert114cf582011-11-28 16:33:09 +00001229 &bql_limit_attribute.attr,
1230 &bql_limit_max_attribute.attr,
1231 &bql_limit_min_attribute.attr,
1232 &bql_hold_time_attribute.attr,
1233 &bql_inflight_attribute.attr,
1234 NULL
1235};
1236
Arvind Yadav38ef00c2017-06-29 16:31:26 +05301237static const struct attribute_group dql_group = {
Tom Herbert114cf582011-11-28 16:33:09 +00001238 .name = "byte_queue_limits",
1239 .attrs = dql_attrs,
1240};
1241#endif /* CONFIG_BQL */
1242
david decotignyccf5ff62011-11-16 12:15:10 +00001243#ifdef CONFIG_XPS
stephen hemminger2b9c7582017-08-18 13:46:26 -07001244static ssize_t xps_cpus_show(struct netdev_queue *queue,
1245 char *buf)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001246{
1247 struct net_device *dev = queue->dev;
Alexander Duyck184c4492016-10-28 11:50:13 -04001248 int cpu, len, num_tc = 1, tc = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001249 struct xps_dev_maps *dev_maps;
1250 cpumask_var_t mask;
1251 unsigned long index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001252
Alexander Duyckd7be9772018-07-09 12:19:32 -04001253 if (!netif_is_multiqueue(dev))
1254 return -ENOENT;
1255
Tom Herbert1d24eb42010-11-21 13:17:27 +00001256 index = get_netdev_queue_index(queue);
1257
Alexander Duyck184c4492016-10-28 11:50:13 -04001258 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04001259 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04001260 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04001261 if (num_tc < 0)
1262 return -EINVAL;
1263
1264 /* If queue belongs to subordinate dev use its map */
1265 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1266
Alexander Duyck184c4492016-10-28 11:50:13 -04001267 tc = netdev_txq_to_tc(dev, index);
1268 if (tc < 0)
1269 return -EINVAL;
1270 }
1271
Alexander Duyck664088f2018-05-31 15:59:46 -04001272 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1273 return -ENOMEM;
1274
Tom Herbert1d24eb42010-11-21 13:17:27 +00001275 rcu_read_lock();
Amritha Nambiar80d19662018-06-29 21:26:41 -07001276 dev_maps = rcu_dereference(dev->xps_cpus_map);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001277 if (dev_maps) {
Alexander Duyck184c4492016-10-28 11:50:13 -04001278 for_each_possible_cpu(cpu) {
1279 int i, tci = cpu * num_tc + tc;
1280 struct xps_map *map;
1281
Amritha Nambiar80d19662018-06-29 21:26:41 -07001282 map = rcu_dereference(dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04001283 if (!map)
1284 continue;
1285
1286 for (i = map->len; i--;) {
1287 if (map->queues[i] == index) {
1288 cpumask_set_cpu(cpu, mask);
1289 break;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001290 }
1291 }
1292 }
1293 }
1294 rcu_read_unlock();
1295
Tejun Heof0906822015-02-13 14:37:42 -08001296 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
Tom Herbert1d24eb42010-11-21 13:17:27 +00001297 free_cpumask_var(mask);
Tejun Heof0906822015-02-13 14:37:42 -08001298 return len < PAGE_SIZE ? len : -EINVAL;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001299}
1300
stephen hemminger2b9c7582017-08-18 13:46:26 -07001301static ssize_t xps_cpus_store(struct netdev_queue *queue,
1302 const char *buf, size_t len)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001303{
1304 struct net_device *dev = queue->dev;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001305 unsigned long index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001306 cpumask_var_t mask;
1307 int err;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001308
Alexander Duyckd7be9772018-07-09 12:19:32 -04001309 if (!netif_is_multiqueue(dev))
1310 return -ENOENT;
1311
Tom Herbert1d24eb42010-11-21 13:17:27 +00001312 if (!capable(CAP_NET_ADMIN))
1313 return -EPERM;
1314
1315 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1316 return -ENOMEM;
1317
1318 index = get_netdev_queue_index(queue);
1319
1320 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1321 if (err) {
1322 free_cpumask_var(mask);
1323 return err;
1324 }
1325
Alexander Duyck537c00d2013-01-10 08:57:02 +00001326 err = netif_set_xps_queue(dev, mask, index);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001327
1328 free_cpumask_var(mask);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001329
Alexander Duyck537c00d2013-01-10 08:57:02 +00001330 return err ? : len;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001331}
1332
stephen hemminger2b9c7582017-08-18 13:46:26 -07001333static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1334 = __ATTR_RW(xps_cpus);
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001335
1336static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1337{
1338 struct net_device *dev = queue->dev;
1339 struct xps_dev_maps *dev_maps;
1340 unsigned long *mask, index;
1341 int j, len, num_tc = 1, tc = 0;
1342
1343 index = get_netdev_queue_index(queue);
1344
1345 if (dev->num_tc) {
1346 num_tc = dev->num_tc;
1347 tc = netdev_txq_to_tc(dev, index);
1348 if (tc < 0)
1349 return -EINVAL;
1350 }
1351 mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
1352 GFP_KERNEL);
1353 if (!mask)
1354 return -ENOMEM;
1355
1356 rcu_read_lock();
1357 dev_maps = rcu_dereference(dev->xps_rxqs_map);
1358 if (!dev_maps)
1359 goto out_no_maps;
1360
1361 for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues),
1362 j < dev->num_rx_queues;) {
1363 int i, tci = j * num_tc + tc;
1364 struct xps_map *map;
1365
1366 map = rcu_dereference(dev_maps->attr_map[tci]);
1367 if (!map)
1368 continue;
1369
1370 for (i = map->len; i--;) {
1371 if (map->queues[i] == index) {
1372 set_bit(j, mask);
1373 break;
1374 }
1375 }
1376 }
1377out_no_maps:
1378 rcu_read_unlock();
1379
1380 len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues);
1381 kfree(mask);
1382
1383 return len < PAGE_SIZE ? len : -EINVAL;
1384}
1385
1386static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1387 size_t len)
1388{
1389 struct net_device *dev = queue->dev;
1390 struct net *net = dev_net(dev);
1391 unsigned long *mask, index;
1392 int err;
1393
1394 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1395 return -EPERM;
1396
1397 mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
1398 GFP_KERNEL);
1399 if (!mask)
1400 return -ENOMEM;
1401
1402 index = get_netdev_queue_index(queue);
1403
1404 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1405 if (err) {
1406 kfree(mask);
1407 return err;
1408 }
1409
Andrei Vagin4d99f662018-08-08 20:07:35 -07001410 cpus_read_lock();
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001411 err = __netif_set_xps_queue(dev, mask, index, true);
Andrei Vagin4d99f662018-08-08 20:07:35 -07001412 cpus_read_unlock();
1413
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001414 kfree(mask);
1415 return err ? : len;
1416}
1417
1418static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1419 = __ATTR_RW(xps_rxqs);
david decotignyccf5ff62011-11-16 12:15:10 +00001420#endif /* CONFIG_XPS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001421
stephen hemminger2b9c7582017-08-18 13:46:26 -07001422static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
david decotignyccf5ff62011-11-16 12:15:10 +00001423 &queue_trans_timeout.attr,
Alexander Duyck8d059b02016-10-28 11:43:49 -04001424 &queue_traffic_class.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001425#ifdef CONFIG_XPS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001426 &xps_cpus_attribute.attr,
Amritha Nambiar8af2c062018-06-29 21:27:07 -07001427 &xps_rxqs_attribute.attr,
John Fastabend822b3b22015-03-18 14:57:33 +02001428 &queue_tx_maxrate.attr,
david decotignyccf5ff62011-11-16 12:15:10 +00001429#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001430 NULL
1431};
1432
1433static void netdev_queue_release(struct kobject *kobj)
1434{
1435 struct netdev_queue *queue = to_netdev_queue(kobj);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001436
Tom Herbert1d24eb42010-11-21 13:17:27 +00001437 memset(kobj, 0, sizeof(*kobj));
1438 dev_put(queue->dev);
1439}
1440
Weilong Chen82ef3d52014-01-16 17:24:31 +08001441static const void *netdev_queue_namespace(struct kobject *kobj)
1442{
1443 struct netdev_queue *queue = to_netdev_queue(kobj);
1444 struct device *dev = &queue->dev->dev;
1445 const void *ns = NULL;
1446
1447 if (dev->class && dev->class->ns_type)
1448 ns = dev->class->namespace(dev);
1449
1450 return ns;
1451}
1452
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001453static void netdev_queue_get_ownership(struct kobject *kobj,
1454 kuid_t *uid, kgid_t *gid)
1455{
1456 const struct net *net = netdev_queue_namespace(kobj);
1457
1458 net_ns_get_ownership(net, uid, gid);
1459}
1460
stephen hemminger2b9c7582017-08-18 13:46:26 -07001461static struct kobj_type netdev_queue_ktype __ro_after_init = {
Tom Herbert1d24eb42010-11-21 13:17:27 +00001462 .sysfs_ops = &netdev_queue_sysfs_ops,
1463 .release = netdev_queue_release,
1464 .default_attrs = netdev_queue_default_attrs,
Weilong Chen82ef3d52014-01-16 17:24:31 +08001465 .namespace = netdev_queue_namespace,
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001466 .get_ownership = netdev_queue_get_ownership,
Tom Herbert1d24eb42010-11-21 13:17:27 +00001467};
1468
WANG Cong6b53daf2014-07-23 16:09:10 -07001469static int netdev_queue_add_kobject(struct net_device *dev, int index)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001470{
WANG Cong6b53daf2014-07-23 16:09:10 -07001471 struct netdev_queue *queue = dev->_tx + index;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001472 struct kobject *kobj = &queue->kobj;
1473 int error = 0;
1474
Jouni Hoganderf8862bc2019-12-05 15:57:07 +02001475 /* Kobject_put later will trigger netdev_queue_release call
1476 * which decreases dev refcount: Take that reference here
1477 */
1478 dev_hold(queue->dev);
1479
WANG Cong6b53daf2014-07-23 16:09:10 -07001480 kobj->kset = dev->queues_kset;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001481 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
stephen hemminger6648c652017-08-18 13:46:28 -07001482 "tx-%u", index);
Tom Herbert114cf582011-11-28 16:33:09 +00001483 if (error)
Jouni Hogander60e71542019-11-20 09:08:16 +02001484 goto err;
Tom Herbert114cf582011-11-28 16:33:09 +00001485
1486#ifdef CONFIG_BQL
1487 error = sysfs_create_group(kobj, &dql_group);
Jouni Hogander60e71542019-11-20 09:08:16 +02001488 if (error)
1489 goto err;
Tom Herbert114cf582011-11-28 16:33:09 +00001490#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001491
1492 kobject_uevent(kobj, KOBJ_ADD);
Eric Dumazet70706952019-11-20 19:19:07 -08001493 return 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001494
Jouni Hogander60e71542019-11-20 09:08:16 +02001495err:
1496 kobject_put(kobj);
1497 return error;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001498}
david decotignyccf5ff62011-11-16 12:15:10 +00001499#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001500
1501int
WANG Cong6b53daf2014-07-23 16:09:10 -07001502netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001503{
david decotignyccf5ff62011-11-16 12:15:10 +00001504#ifdef CONFIG_SYSFS
Tom Herbert1d24eb42010-11-21 13:17:27 +00001505 int i;
1506 int error = 0;
1507
1508 for (i = old_num; i < new_num; i++) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001509 error = netdev_queue_add_kobject(dev, i);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001510 if (error) {
1511 new_num = old_num;
1512 break;
1513 }
1514 }
1515
Tom Herbert114cf582011-11-28 16:33:09 +00001516 while (--i >= new_num) {
WANG Cong6b53daf2014-07-23 16:09:10 -07001517 struct netdev_queue *queue = dev->_tx + i;
Tom Herbert114cf582011-11-28 16:33:09 +00001518
Kirill Tkhai273c28b2018-01-12 18:28:31 +03001519 if (!refcount_read(&dev_net(dev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -07001520 queue->kobj.uevent_suppress = 1;
Tom Herbert114cf582011-11-28 16:33:09 +00001521#ifdef CONFIG_BQL
1522 sysfs_remove_group(&queue->kobj, &dql_group);
1523#endif
1524 kobject_put(&queue->kobj);
1525 }
Tom Herbert1d24eb42010-11-21 13:17:27 +00001526
1527 return error;
Tom Herbertbf264142010-11-26 08:36:09 +00001528#else
1529 return 0;
david decotignyccf5ff62011-11-16 12:15:10 +00001530#endif /* CONFIG_SYSFS */
Tom Herbert1d24eb42010-11-21 13:17:27 +00001531}
1532
WANG Cong6b53daf2014-07-23 16:09:10 -07001533static int register_queue_kobjects(struct net_device *dev)
Tom Herbert1d24eb42010-11-21 13:17:27 +00001534{
Tom Herbertbf264142010-11-26 08:36:09 +00001535 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001536
david decotignyccf5ff62011-11-16 12:15:10 +00001537#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001538 dev->queues_kset = kset_create_and_add("queues",
stephen hemminger6648c652017-08-18 13:46:28 -07001539 NULL, &dev->dev.kobj);
WANG Cong6b53daf2014-07-23 16:09:10 -07001540 if (!dev->queues_kset)
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001541 return -ENOMEM;
WANG Cong6b53daf2014-07-23 16:09:10 -07001542 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001543#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001544 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001545
WANG Cong6b53daf2014-07-23 16:09:10 -07001546 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001547 if (error)
1548 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001549 rxq = real_rx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001550
WANG Cong6b53daf2014-07-23 16:09:10 -07001551 error = netdev_queue_update_kobjects(dev, 0, real_tx);
Tom Herbert1d24eb42010-11-21 13:17:27 +00001552 if (error)
1553 goto error;
Tom Herbertbf264142010-11-26 08:36:09 +00001554 txq = real_tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00001555
1556 return 0;
1557
1558error:
WANG Cong6b53daf2014-07-23 16:09:10 -07001559 netdev_queue_update_kobjects(dev, txq, 0);
1560 net_rx_queue_update_kobjects(dev, rxq, 0);
YueHaibing7ce2a512019-03-02 10:34:55 +08001561#ifdef CONFIG_SYSFS
1562 kset_unregister(dev->queues_kset);
1563#endif
Tom Herbert1d24eb42010-11-21 13:17:27 +00001564 return error;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00001565}
1566
WANG Cong6b53daf2014-07-23 16:09:10 -07001567static void remove_queue_kobjects(struct net_device *dev)
Tom Herbert0a9627f2010-03-16 08:03:29 +00001568{
Tom Herbertbf264142010-11-26 08:36:09 +00001569 int real_rx = 0, real_tx = 0;
1570
Michael Daltona953be52014-01-16 22:23:28 -08001571#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001572 real_rx = dev->real_num_rx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001573#endif
WANG Cong6b53daf2014-07-23 16:09:10 -07001574 real_tx = dev->real_num_tx_queues;
Tom Herbertbf264142010-11-26 08:36:09 +00001575
WANG Cong6b53daf2014-07-23 16:09:10 -07001576 net_rx_queue_update_kobjects(dev, real_rx, 0);
1577 netdev_queue_update_kobjects(dev, real_tx, 0);
david decotignyccf5ff62011-11-16 12:15:10 +00001578#ifdef CONFIG_SYSFS
WANG Cong6b53daf2014-07-23 16:09:10 -07001579 kset_unregister(dev->queues_kset);
Tom Herbertbf264142010-11-26 08:36:09 +00001580#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00001581}
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001582
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001583static bool net_current_may_mount(void)
1584{
1585 struct net *net = current->nsproxy->net_ns;
1586
1587 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1588}
1589
Al Viroa685e082011-06-08 21:13:01 -04001590static void *net_grab_current_ns(void)
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001591{
Al Viroa685e082011-06-08 21:13:01 -04001592 struct net *ns = current->nsproxy->net_ns;
1593#ifdef CONFIG_NET_NS
1594 if (ns)
Reshetova, Elenac122e142017-06-30 13:08:08 +03001595 refcount_inc(&ns->passive);
Al Viroa685e082011-06-08 21:13:01 -04001596#endif
1597 return ns;
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001598}
1599
1600static const void *net_initial_ns(void)
1601{
1602 return &init_net;
1603}
1604
1605static const void *net_netlink_ns(struct sock *sk)
1606{
1607 return sock_net(sk);
1608}
1609
stephen hemminger737aec52017-08-18 13:46:22 -07001610const struct kobj_ns_type_operations net_ns_type_operations = {
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001611 .type = KOBJ_NS_TYPE_NET,
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001612 .current_may_mount = net_current_may_mount,
Al Viroa685e082011-06-08 21:13:01 -04001613 .grab_current_ns = net_grab_current_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001614 .netlink_ns = net_netlink_ns,
1615 .initial_ns = net_initial_ns,
Al Viroa685e082011-06-08 21:13:01 -04001616 .drop_ns = net_drop_ns,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001617};
Johannes Berg04600792010-08-05 17:45:15 +02001618EXPORT_SYMBOL_GPL(net_ns_type_operations);
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001619
Kay Sievers7eff2e72007-08-14 15:15:12 +02001620static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001622 struct net_device *dev = to_net_dev(d);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001623 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Kay Sievers312c0042005-11-16 09:00:00 +01001625 /* pass interface to uevent. */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001626 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
Eric Rannaudbf624562007-03-30 22:23:12 -07001627 if (retval)
1628 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001630 /* pass ifindex to uevent.
1631 * ifindex is useful as it won't change (interface name may change)
stephen hemminger6648c652017-08-18 13:46:28 -07001632 * and is what RtNetlink uses natively.
1633 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001634 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -08001635
Eric Rannaudbf624562007-03-30 22:23:12 -07001636exit:
Eric Rannaudbf624562007-03-30 22:23:12 -07001637 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001641 * netdev_release -- destroy and free a dead device.
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001642 * Called when last reference to device kobject is gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001644static void netdev_release(struct device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001646 struct net_device *dev = to_net_dev(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 BUG_ON(dev->reg_state != NETREG_RELEASED);
1649
Florian Westphal6c557002017-10-02 23:50:05 +02001650 /* no need to wait for rcu grace period:
1651 * device is dead and about to be freed.
1652 */
1653 kfree(rcu_access_pointer(dev->ifalias));
Eric Dumazet74d332c2013-10-30 13:10:44 -07001654 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001657static const void *net_namespace(struct device *d)
1658{
Geliang Tang5c294822015-12-22 23:11:49 +08001659 struct net_device *dev = to_net_dev(d);
1660
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001661 return dev_net(dev);
1662}
1663
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001664static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
1665{
1666 struct net_device *dev = to_net_dev(d);
1667 const struct net *net = dev_net(dev);
1668
1669 net_ns_get_ownership(net, uid, gid);
1670}
1671
stephen hemmingere6d473e2017-08-18 13:46:21 -07001672static struct class net_class __ro_after_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 .name = "net",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001674 .dev_release = netdev_release,
Greg Kroah-Hartman6be8aee2013-07-24 15:05:33 -07001675 .dev_groups = net_class_groups,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001676 .dev_uevent = netdev_uevent,
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001677 .ns_type = &net_ns_type_operations,
1678 .namespace = net_namespace,
Dmitry Torokhovb0e37c02018-07-20 21:56:52 +00001679 .get_ownership = net_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680};
1681
Florian Fainelliaa836df2015-03-09 14:31:20 -07001682#ifdef CONFIG_OF_NET
1683static int of_dev_node_match(struct device *dev, const void *data)
1684{
1685 int ret = 0;
1686
1687 if (dev->parent)
1688 ret = dev->parent->of_node == data;
1689
1690 return ret == 0 ? dev->of_node == data : ret;
1691}
1692
Russell King9861f722015-09-24 20:36:33 +01001693/*
1694 * of_find_net_device_by_node - lookup the net device for the device node
1695 * @np: OF device node
1696 *
1697 * Looks up the net_device structure corresponding with the device node.
1698 * If successful, returns a pointer to the net_device with the embedded
1699 * struct device refcount incremented by one, or NULL on failure. The
1700 * refcount must be dropped when done with the net_device.
1701 */
Florian Fainelliaa836df2015-03-09 14:31:20 -07001702struct net_device *of_find_net_device_by_node(struct device_node *np)
1703{
1704 struct device *dev;
1705
1706 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1707 if (!dev)
1708 return NULL;
1709
1710 return to_net_dev(dev);
1711}
1712EXPORT_SYMBOL(of_find_net_device_by_node);
1713#endif
1714
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001715/* Delete sysfs entries but hold kobject reference until after all
1716 * netdev references are gone.
1717 */
WANG Cong6b53daf2014-07-23 16:09:10 -07001718void netdev_unregister_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
stephen hemminger6648c652017-08-18 13:46:28 -07001720 struct device *dev = &ndev->dev;
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001721
Kirill Tkhai273c28b2018-01-12 18:28:31 +03001722 if (!refcount_read(&dev_net(ndev)->count))
Andrey Vagin002d8a12016-10-24 19:09:53 -07001723 dev_set_uevent_suppress(dev, 1);
1724
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001725 kobject_get(&dev->kobj);
Eric W. Biederman38918452008-10-27 17:51:47 -07001726
WANG Cong6b53daf2014-07-23 16:09:10 -07001727 remove_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001728
Ming Lei9802c8e2013-02-22 16:34:16 -08001729 pm_runtime_set_memalloc_noio(dev, false);
1730
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07001731 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734/* Create sysfs entries for network device. */
WANG Cong6b53daf2014-07-23 16:09:10 -07001735int netdev_register_kobject(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
stephen hemminger6648c652017-08-18 13:46:28 -07001737 struct device *dev = &ndev->dev;
WANG Cong6b53daf2014-07-23 16:09:10 -07001738 const struct attribute_group **groups = ndev->sysfs_groups;
Tom Herbert0a9627f2010-03-16 08:03:29 +00001739 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001741 device_initialize(dev);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001742 dev->class = &net_class;
WANG Cong6b53daf2014-07-23 16:09:10 -07001743 dev->platform_data = ndev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001744 dev->groups = groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
WANG Cong6b53daf2014-07-23 16:09:10 -07001746 dev_set_name(dev, "%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001748#ifdef CONFIG_SYSFS
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001749 /* Allow for a device specific group */
1750 if (*groups)
1751 groups++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Eric W. Biederman0c509a62009-10-29 14:18:21 +00001753 *groups++ = &netstat_group;
Johannes Berg38c1a012012-11-16 20:46:19 +01001754
1755#if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
WANG Cong6b53daf2014-07-23 16:09:10 -07001756 if (ndev->ieee80211_ptr)
Johannes Berg38c1a012012-11-16 20:46:19 +01001757 *groups++ = &wireless_group;
1758#if IS_ENABLED(CONFIG_WIRELESS_EXT)
WANG Cong6b53daf2014-07-23 16:09:10 -07001759 else if (ndev->wireless_handlers)
Johannes Berg38c1a012012-11-16 20:46:19 +01001760 *groups++ = &wireless_group;
1761#endif
1762#endif
Eric W. Biederman8b41d182007-09-26 22:02:53 -07001763#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Tom Herbert0a9627f2010-03-16 08:03:29 +00001765 error = device_add(dev);
1766 if (error)
1767 return error;
1768
WANG Cong6b53daf2014-07-23 16:09:10 -07001769 error = register_queue_kobjects(ndev);
Tom Herbert0a9627f2010-03-16 08:03:29 +00001770 if (error) {
1771 device_del(dev);
1772 return error;
1773 }
1774
Ming Lei9802c8e2013-02-22 16:34:16 -08001775 pm_runtime_set_memalloc_noio(dev, true);
1776
Tom Herbert0a9627f2010-03-16 08:03:29 +00001777 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
stephen hemmingerb793dc52017-08-18 13:46:20 -07001780int netdev_class_create_file_ns(const struct class_attribute *class_attr,
Tejun Heo58292cbe2013-09-11 22:29:04 -04001781 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001782{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001783 return class_create_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001784}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001785EXPORT_SYMBOL(netdev_class_create_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001786
stephen hemmingerb793dc52017-08-18 13:46:20 -07001787void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
Tejun Heo58292cbe2013-09-11 22:29:04 -04001788 const void *ns)
Jay Vosburghb8a97872008-06-13 18:12:04 -07001789{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001790 class_remove_file_ns(&net_class, class_attr, ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001791}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001792EXPORT_SYMBOL(netdev_class_remove_file_ns);
Jay Vosburghb8a97872008-06-13 18:12:04 -07001793
Daniel Borkmanna48d4bb2014-01-06 01:20:11 +01001794int __init netdev_kobject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Eric W. Biederman608b4b92010-05-04 17:36:45 -07001796 kobj_ns_type_register(&net_ns_type_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return class_register(&net_class);
1798}