blob: 1baab07820f65f9bcf88a6d73e2c9ff741d33c18 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
tcharding722c9a02017-02-09 17:56:04 +11003 * NET3 Protocol independent device support routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Derived from the non IP parts of dev.c 1.0.19
tcharding722c9a02017-02-09 17:56:04 +11006 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Florian la Roche <rzsfl@rz.uni-sb.de>
12 * Alan Cox <gw4pts@gw4pts.ampr.org>
13 * David Hinds <dahinds@users.sourceforge.net>
14 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
15 * Adam Sulmicki <adam@cfar.umd.edu>
16 * Pekka Riikonen <priikone@poesidon.pspt.fi>
17 *
18 * Changes:
19 * D.J. Barrow : Fixed bug where dev->refcnt gets set
tcharding722c9a02017-02-09 17:56:04 +110020 * to 2 if register_netdev gets called
21 * before net_dev_init & also removed a
22 * few lines of code in the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant
25 * stunts to keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into
30 * drivers
31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
32 * Alan Cox : 100 backlog just doesn't cut it when
33 * you start doing multicast video 8)
34 * Alan Cox : Rewrote net_bh and list manager.
tcharding722c9a02017-02-09 17:56:04 +110035 * Alan Cox : Fix ETH_P_ALL echoback lengths.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * Alan Cox : Took out transmit every packet pass
37 * Saved a few bytes in the ioctl handler
38 * Alan Cox : Network driver sets packet type before
39 * calling netif_rx. Saves a function
40 * call a packet.
41 * Alan Cox : Hashed net_bh()
42 * Richard Kooijman: Timestamp fixes.
43 * Alan Cox : Wrong field in SIOCGIFDSTADDR
44 * Alan Cox : Device lock protection.
tcharding722c9a02017-02-09 17:56:04 +110045 * Alan Cox : Fixed nasty side effect of device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * changes.
47 * Rudi Cilibrasi : Pass the right thing to
48 * set_mac_address()
49 * Dave Miller : 32bit quantity for the device lock to
50 * make it work out on a Sparc.
51 * Bjorn Ekwall : Added KERNELD hack.
52 * Alan Cox : Cleaned up the backlog initialise.
53 * Craig Metz : SIOCGIFCONF fix if space for under
54 * 1 device.
55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
56 * is no device open function.
57 * Andi Kleen : Fix error reporting for SIOCGIFCONF
58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
59 * Cyrus Durgin : Cleaned for KMOD
60 * Adam Sulmicki : Bug Fix : Network Device Unload
61 * A network device unload needs to purge
62 * the backlog queue.
63 * Paul Rusty Russell : SIOCSIFNAME
64 * Pekka Riikonen : Netdev boot-time settings code
65 * Andrew Morton : Make unregister_netdevice wait
tcharding722c9a02017-02-09 17:56:04 +110066 * indefinitely on dev->refcnt
67 * J Hadi Salim : - Backlog queue sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * - netif_rx() feedback
69 */
70
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080073#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/cpu.h>
75#include <linux/types.h>
76#include <linux/kernel.h>
stephen hemminger08e98972009-11-10 07:20:34 +000077#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090078#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070080#include <linux/sched/mm.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080081#include <linux/mutex.h>
Ahmed S. Darwish11d60112020-06-03 16:49:44 +020082#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/string.h>
84#include <linux/mm.h>
85#include <linux/socket.h>
86#include <linux/sockios.h>
87#include <linux/errno.h>
88#include <linux/interrupt.h>
89#include <linux/if_ether.h>
90#include <linux/netdevice.h>
91#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070092#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/skbuff.h>
Wei Wang29863d42021-02-08 11:34:09 -080094#include <linux/kthread.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070095#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040096#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020097#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -080099#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <linux/stat.h>
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +0300102#include <net/dsa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700104#include <net/dst_metadata.h>
Leon Romanovsky04f00ab2021-02-03 15:51:11 +0200105#include <net/gro.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200107#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000109#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/highmem.h>
111#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <linux/netpoll.h>
114#include <linux/rcupdate.h>
115#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500118#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700119#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700120#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700121#include <linux/ctype.h>
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700122#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700123#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700124#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700125#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700126#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700127#include <linux/ipv6.h>
128#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700129#include <linux/jhash.h>
130#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700131#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900132#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900133#include <trace/events/skb.h>
Qitao Xu70713dd2021-07-14 23:03:24 -0700134#include <trace/events/qdisc.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700135#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000136#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100137#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300138#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700139#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100140#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400141#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800142#include <linux/hrtimer.h>
Lukas Wunner7463acf2021-10-08 22:06:01 +0200143#include <linux/netfilter_netdev.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530144#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200145#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200146#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200147#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100148#include <linux/indirect_call_wrapper.h>
Jiri Pirkoaf3836d2019-03-28 13:56:37 +0100149#include <net/devlink.h>
Heiner Kallweitbd869242020-06-20 22:35:42 +0200150#include <linux/pm_runtime.h>
Willy Tarreau37447412020-08-10 10:27:42 +0200151#include <linux/prandom.h>
Tanner Love127d7352021-06-28 09:50:07 -0400152#include <linux/once_lite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700154#include "net-sysfs.h"
155
Herbert Xu5d38a072009-01-04 16:13:40 -0800156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(ptype_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000158struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
159struct list_head ptype_all __read_mostly; /* Taps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000161static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700162static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700163 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000164static int call_netdevice_notifiers_extack(unsigned long val,
165 struct net_device *dev,
166 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200167static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700170 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * semaphore.
172 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800173 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700176 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * actual updates. This allows pure readers to access the list even
178 * while a writer is preparing to update it.
179 *
180 * To put it another way, dev_base_lock is held for writing only to
181 * protect against pure readers; the rtnl semaphore provides the
182 * protection against other writers.
183 *
184 * See, for example usages, register_netdevice() and
185 * unregister_netdevice(), which must be called with the rtnl
186 * semaphore held.
187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189EXPORT_SYMBOL(dev_base_lock);
190
Florian Westphal6c557002017-10-02 23:50:05 +0200191static DEFINE_MUTEX(ifalias_mutex);
192
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300193/* protects napi_hash addition/deletion and napi_gen_id */
194static DEFINE_SPINLOCK(napi_hash_lock);
195
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800196static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800197static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300198
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200199static DECLARE_RWSEM(devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000200
Thomas Graf4e985ad2011-06-21 03:11:20 +0000201static inline void dev_base_seq_inc(struct net *net)
202{
tcharding643aa9c2017-02-09 17:56:05 +1100203 while (++net->dev_base_seq == 0)
204 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000205}
206
Eric W. Biederman881d9662007-09-17 11:56:21 -0700207static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700209 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000210
stephen hemminger08e98972009-11-10 07:20:34 +0000211 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Eric W. Biederman881d9662007-09-17 11:56:21 -0700214static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700216 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000219static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000220{
221#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000222 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000223#endif
224}
225
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000226static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000227{
228#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000229 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000230#endif
231}
232
Jiri Pirkoff927412019-09-30 11:48:15 +0200233static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
234 const char *name)
235{
236 struct netdev_name_node *name_node;
237
238 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
239 if (!name_node)
240 return NULL;
241 INIT_HLIST_NODE(&name_node->hlist);
242 name_node->dev = dev;
243 name_node->name = name;
244 return name_node;
245}
246
247static struct netdev_name_node *
248netdev_name_node_head_alloc(struct net_device *dev)
249{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200250 struct netdev_name_node *name_node;
251
252 name_node = netdev_name_node_alloc(dev, dev->name);
253 if (!name_node)
254 return NULL;
255 INIT_LIST_HEAD(&name_node->list);
256 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200257}
258
259static void netdev_name_node_free(struct netdev_name_node *name_node)
260{
261 kfree(name_node);
262}
263
264static void netdev_name_node_add(struct net *net,
265 struct netdev_name_node *name_node)
266{
267 hlist_add_head_rcu(&name_node->hlist,
268 dev_name_hash(net, name_node->name));
269}
270
271static void netdev_name_node_del(struct netdev_name_node *name_node)
272{
273 hlist_del_rcu(&name_node->hlist);
274}
275
276static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
277 const char *name)
278{
279 struct hlist_head *head = dev_name_hash(net, name);
280 struct netdev_name_node *name_node;
281
282 hlist_for_each_entry(name_node, head, hlist)
283 if (!strcmp(name_node->name, name))
284 return name_node;
285 return NULL;
286}
287
288static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
289 const char *name)
290{
291 struct hlist_head *head = dev_name_hash(net, name);
292 struct netdev_name_node *name_node;
293
294 hlist_for_each_entry_rcu(name_node, head, hlist)
295 if (!strcmp(name_node->name, name))
296 return name_node;
297 return NULL;
298}
299
Antoine Tenart75ea27d2021-10-07 18:16:50 +0200300bool netdev_name_in_use(struct net *net, const char *name)
301{
302 return netdev_name_node_lookup(net, name);
303}
304EXPORT_SYMBOL(netdev_name_in_use);
305
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200306int netdev_name_node_alt_create(struct net_device *dev, const char *name)
307{
308 struct netdev_name_node *name_node;
309 struct net *net = dev_net(dev);
310
311 name_node = netdev_name_node_lookup(net, name);
312 if (name_node)
313 return -EEXIST;
314 name_node = netdev_name_node_alloc(dev, name);
315 if (!name_node)
316 return -ENOMEM;
317 netdev_name_node_add(net, name_node);
318 /* The node that holds dev->name acts as a head of per-device list. */
319 list_add_tail(&name_node->list, &dev->name_node->list);
320
321 return 0;
322}
323EXPORT_SYMBOL(netdev_name_node_alt_create);
324
325static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
326{
327 list_del(&name_node->list);
328 netdev_name_node_del(name_node);
329 kfree(name_node->name);
330 netdev_name_node_free(name_node);
331}
332
333int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
334{
335 struct netdev_name_node *name_node;
336 struct net *net = dev_net(dev);
337
338 name_node = netdev_name_node_lookup(net, name);
339 if (!name_node)
340 return -ENOENT;
Eric Dumazete08ad802020-02-14 07:53:53 -0800341 /* lookup might have found our primary name or a name belonging
342 * to another device.
343 */
344 if (name_node == dev->name_node || name_node->dev != dev)
345 return -EINVAL;
346
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200347 __netdev_name_node_alt_destroy(name_node);
348
349 return 0;
350}
351EXPORT_SYMBOL(netdev_name_node_alt_destroy);
352
353static void netdev_name_node_alt_flush(struct net_device *dev)
354{
355 struct netdev_name_node *name_node, *tmp;
356
357 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
358 __netdev_name_node_alt_destroy(name_node);
359}
360
Eric W. Biedermance286d32007-09-12 13:53:49 +0200361/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000362static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200363{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900364 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200365
366 ASSERT_RTNL();
367
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100368 write_lock(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800369 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Jiri Pirkoff927412019-09-30 11:48:15 +0200370 netdev_name_node_add(net, dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000371 hlist_add_head_rcu(&dev->index_hlist,
372 dev_index_hash(net, dev->ifindex));
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100373 write_unlock(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000374
375 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200376}
377
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000378/* Device list removal
379 * caller must respect a RCU grace period before freeing/reusing dev
380 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200381static void unlist_netdevice(struct net_device *dev)
382{
383 ASSERT_RTNL();
384
385 /* Unlink dev from the device chain */
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100386 write_lock(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800387 list_del_rcu(&dev->dev_list);
Jiri Pirkoff927412019-09-30 11:48:15 +0200388 netdev_name_node_del(dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000389 hlist_del_rcu(&dev->index_hlist);
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100390 write_unlock(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000391
392 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*
396 * Our notifier list
397 */
398
Alan Sternf07d5b92006-05-09 15:23:03 -0700399static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401/*
402 * Device drivers call our routines to queue packets here. We empty the
403 * queue in the local softnet handler.
404 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700405
Eric Dumazet9958da02010-04-17 04:17:02 +0000406DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700407EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Cong Wang1a33e102020-05-02 22:22:19 -0700409#ifdef CONFIG_LOCKDEP
410/*
411 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
412 * according to dev->type
413 */
414static const unsigned short netdev_lock_type[] = {
415 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
416 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
417 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
418 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
419 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
420 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
421 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
422 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
423 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
424 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
425 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
426 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
427 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
428 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
429 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
430
431static const char *const netdev_lock_name[] = {
432 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
433 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
434 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
435 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
436 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
437 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
438 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
439 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
440 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
441 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
442 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
443 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
444 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
445 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
446 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
447
448static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang845e0eb2020-06-08 14:53:01 -0700449static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang1a33e102020-05-02 22:22:19 -0700450
451static inline unsigned short netdev_lock_pos(unsigned short dev_type)
452{
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
456 if (netdev_lock_type[i] == dev_type)
457 return i;
458 /* the last key is used by default */
459 return ARRAY_SIZE(netdev_lock_type) - 1;
460}
461
462static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
463 unsigned short dev_type)
464{
465 int i;
466
467 i = netdev_lock_pos(dev_type);
468 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
469 netdev_lock_name[i]);
470}
Cong Wang845e0eb2020-06-08 14:53:01 -0700471
472static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
473{
474 int i;
475
476 i = netdev_lock_pos(dev->type);
477 lockdep_set_class_and_name(&dev->addr_list_lock,
478 &netdev_addr_lock_key[i],
479 netdev_lock_name[i]);
480}
Cong Wang1a33e102020-05-02 22:22:19 -0700481#else
482static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
483 unsigned short dev_type)
484{
485}
Cong Wang845e0eb2020-06-08 14:53:01 -0700486
487static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
488{
489}
Cong Wang1a33e102020-05-02 22:22:19 -0700490#endif
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100493 *
494 * Protocol management and registration routines
495 *
496 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * Add a protocol ID to the list. Now that the input handler is
501 * smarter we can dispense with all the messy stuff that used to be
502 * here.
503 *
504 * BEWARE!!! Protocol handlers, mangling input packets,
505 * MUST BE last in hash buckets and checking protocol handlers
506 * MUST start from promiscuous ptype_all chain in net_bh.
507 * It is true now, do not change it.
508 * Explanation follows: if protocol handler, mangling packet, will
509 * be the first on list, it is not able to sense, that packet
510 * is cloned and should be copied-on-write, so that it will
511 * change it and subsequent readers will get broken packet.
512 * --ANK (980803)
513 */
514
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000515static inline struct list_head *ptype_head(const struct packet_type *pt)
516{
517 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800518 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000519 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800520 return pt->dev ? &pt->dev->ptype_specific :
521 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
525 * dev_add_pack - add packet handler
526 * @pt: packet type declaration
527 *
528 * Add a protocol handler to the networking stack. The passed &packet_type
529 * is linked into kernel lists and may not be freed until it has been
530 * removed from the kernel lists.
531 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900532 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * guarantee all CPU's that are in middle of receiving packets
534 * will see the new packet type (until the next received packet).
535 */
536
537void dev_add_pack(struct packet_type *pt)
538{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000539 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000541 spin_lock(&ptype_lock);
542 list_add_rcu(&pt->list, head);
543 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700545EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/**
548 * __dev_remove_pack - remove packet handler
549 * @pt: packet type declaration
550 *
551 * Remove a protocol handler that was previously added to the kernel
552 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
553 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900554 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
556 * The packet type might still be in use by receivers
557 * and must not be freed until after all the CPU's have gone
558 * through a quiescent state.
559 */
560void __dev_remove_pack(struct packet_type *pt)
561{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000562 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct packet_type *pt1;
564
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000565 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 list_for_each_entry(pt1, head, list) {
568 if (pt == pt1) {
569 list_del_rcu(&pt->list);
570 goto out;
571 }
572 }
573
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000574 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000576 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700578EXPORT_SYMBOL(__dev_remove_pack);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/**
581 * dev_remove_pack - remove packet handler
582 * @pt: packet type declaration
583 *
584 * Remove a protocol handler that was previously added to the kernel
585 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
586 * from the kernel lists and can be freed or reused once this function
587 * returns.
588 *
589 * This call sleeps to guarantee that no CPU is looking at the packet
590 * type after return.
591 */
592void dev_remove_pack(struct packet_type *pt)
593{
594 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 synchronize_net();
597}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700598EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Vlad Yasevich62532da2012-11-15 08:49:10 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100602 *
603 * Device Interface Subroutines
604 *
605 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200608 * dev_get_iflink - get 'iflink' value of a interface
609 * @dev: targeted interface
610 *
611 * Indicates the ifindex the interface is linked to.
612 * Physical interfaces have the same 'ifindex' and 'iflink' values.
613 */
614
615int dev_get_iflink(const struct net_device *dev)
616{
617 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
618 return dev->netdev_ops->ndo_get_iflink(dev);
619
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200620 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200621}
622EXPORT_SYMBOL(dev_get_iflink);
623
624/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700625 * dev_fill_metadata_dst - Retrieve tunnel egress information.
626 * @dev: targeted interface
627 * @skb: The packet.
628 *
629 * For better visibility of tunnel traffic OVS needs to retrieve
630 * egress tunnel information for a packet. Following API allows
631 * user to get this info.
632 */
633int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
634{
635 struct ip_tunnel_info *info;
636
637 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
638 return -EINVAL;
639
640 info = skb_tunnel_info_unclone(skb);
641 if (!info)
642 return -ENOMEM;
643 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
644 return -EINVAL;
645
646 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
647}
648EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
649
Pablo Neira Ayusoddb94ea2021-03-24 02:30:32 +0100650static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
651{
652 int k = stack->num_paths++;
653
654 if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
655 return NULL;
656
657 return &stack->path[k];
658}
659
660int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
661 struct net_device_path_stack *stack)
662{
663 const struct net_device *last_dev;
664 struct net_device_path_ctx ctx = {
665 .dev = dev,
666 .daddr = daddr,
667 };
668 struct net_device_path *path;
669 int ret = 0;
670
671 stack->num_paths = 0;
672 while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
673 last_dev = ctx.dev;
674 path = dev_fwd_path(stack);
675 if (!path)
676 return -1;
677
678 memset(path, 0, sizeof(struct net_device_path));
679 ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
680 if (ret < 0)
681 return -1;
682
683 if (WARN_ON_ONCE(last_dev == ctx.dev))
684 return -1;
685 }
686 path = dev_fwd_path(stack);
687 if (!path)
688 return -1;
689 path->type = DEV_PATH_ETHERNET;
690 path->dev = ctx.dev;
691
692 return ret;
693}
694EXPORT_SYMBOL_GPL(dev_fill_forward_path);
695
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700696/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700698 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * @name: name to find
700 *
701 * Find an interface by name. Must be called under RTNL semaphore
702 * or @dev_base_lock. If the name is found a pointer to the device
703 * is returned. If the name is not found then %NULL is returned. The
704 * reference counters are not incremented so the caller must be
705 * careful with locks.
706 */
707
Eric W. Biederman881d9662007-09-17 11:56:21 -0700708struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jiri Pirkoff927412019-09-30 11:48:15 +0200710 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jiri Pirkoff927412019-09-30 11:48:15 +0200712 node_name = netdev_name_node_lookup(net, name);
713 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700715EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717/**
tcharding722c9a02017-02-09 17:56:04 +1100718 * dev_get_by_name_rcu - find a device by its name
719 * @net: the applicable net namespace
720 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000721 *
tcharding722c9a02017-02-09 17:56:04 +1100722 * Find an interface by name.
723 * If the name is found a pointer to the device is returned.
724 * If the name is not found then %NULL is returned.
725 * The reference counters are not incremented so the caller must be
726 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000727 */
728
729struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
730{
Jiri Pirkoff927412019-09-30 11:48:15 +0200731 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000732
Jiri Pirkoff927412019-09-30 11:48:15 +0200733 node_name = netdev_name_node_lookup_rcu(net, name);
734 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000735}
736EXPORT_SYMBOL(dev_get_by_name_rcu);
737
738/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700740 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * @name: name to find
742 *
743 * Find an interface by name. This can be called from any
744 * context and does its own locking. The returned handle has
745 * the usage count incremented and the caller must use dev_put() to
746 * release it when it is no longer needed. %NULL is returned if no
747 * matching device is found.
748 */
749
Eric W. Biederman881d9662007-09-17 11:56:21 -0700750struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct net_device *dev;
753
Eric Dumazet72c95282009-10-30 07:11:27 +0000754 rcu_read_lock();
755 dev = dev_get_by_name_rcu(net, name);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800756 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000757 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return dev;
759}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700760EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762/**
763 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700764 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * @ifindex: index of device
766 *
767 * Search for an interface by index. Returns %NULL if the device
768 * is not found or a pointer to the device. The device has not
769 * had its reference counter increased so the caller must be careful
770 * about locking. The caller must hold either the RTNL semaphore
771 * or @dev_base_lock.
772 */
773
Eric W. Biederman881d9662007-09-17 11:56:21 -0700774struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700776 struct net_device *dev;
777 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Sasha Levinb67bfe02013-02-27 17:06:00 -0800779 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (dev->ifindex == ifindex)
781 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return NULL;
784}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700785EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000787/**
788 * dev_get_by_index_rcu - find a device by its ifindex
789 * @net: the applicable net namespace
790 * @ifindex: index of device
791 *
792 * Search for an interface by index. Returns %NULL if the device
793 * is not found or a pointer to the device. The device has not
794 * had its reference counter increased so the caller must be careful
795 * about locking. The caller must hold RCU lock.
796 */
797
798struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
799{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000800 struct net_device *dev;
801 struct hlist_head *head = dev_index_hash(net, ifindex);
802
Sasha Levinb67bfe02013-02-27 17:06:00 -0800803 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000804 if (dev->ifindex == ifindex)
805 return dev;
806
807 return NULL;
808}
809EXPORT_SYMBOL(dev_get_by_index_rcu);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812/**
813 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700814 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * @ifindex: index of device
816 *
817 * Search for an interface by index. Returns NULL if the device
818 * is not found or a pointer to the device. The device returned has
819 * had a reference added and the pointer is safe until the user calls
820 * dev_put to indicate they have finished with it.
821 */
822
Eric W. Biederman881d9662007-09-17 11:56:21 -0700823struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct net_device *dev;
826
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000827 rcu_read_lock();
828 dev = dev_get_by_index_rcu(net, ifindex);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800829 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000830 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return dev;
832}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700833EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200836 * dev_get_by_napi_id - find a device by napi_id
837 * @napi_id: ID of the NAPI struct
838 *
839 * Search for an interface by NAPI ID. Returns %NULL if the device
840 * is not found or a pointer to the device. The device has not had
841 * its reference counter increased so the caller must be careful
842 * about locking. The caller must hold RCU lock.
843 */
844
845struct net_device *dev_get_by_napi_id(unsigned int napi_id)
846{
847 struct napi_struct *napi;
848
849 WARN_ON_ONCE(!rcu_read_lock_held());
850
851 if (napi_id < MIN_NAPI_ID)
852 return NULL;
853
854 napi = napi_by_id(napi_id);
855
856 return napi ? napi->dev : NULL;
857}
858EXPORT_SYMBOL(dev_get_by_napi_id);
859
860/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200861 * netdev_get_name - get a netdevice name, knowing its ifindex.
862 * @net: network namespace
863 * @name: a pointer to the buffer where the name will be stored.
864 * @ifindex: the ifindex of the interface to get the name from.
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200865 */
866int netdev_get_name(struct net *net, char *name, int ifindex)
867{
868 struct net_device *dev;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200869 int ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200870
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200871 down_read(&devnet_rename_sem);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200872 rcu_read_lock();
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200873
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200874 dev = dev_get_by_index_rcu(net, ifindex);
875 if (!dev) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200876 ret = -ENODEV;
877 goto out;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200878 }
879
880 strcpy(name, dev->name);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200881
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200882 ret = 0;
883out:
884 rcu_read_unlock();
885 up_read(&devnet_rename_sem);
886 return ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200887}
888
889/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000890 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700891 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * @type: media type of device
893 * @ha: hardware address
894 *
895 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800896 * is not found or a pointer to the device.
897 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000898 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * and the caller must therefore be careful about locking
900 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
902
Eric Dumazet941666c2010-12-05 01:23:53 +0000903struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
904 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct net_device *dev;
907
Eric Dumazet941666c2010-12-05 01:23:53 +0000908 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (dev->type == type &&
910 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700911 return dev;
912
913 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Eric Dumazet941666c2010-12-05 01:23:53 +0000915EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300916
Eric W. Biederman881d9662007-09-17 11:56:21 -0700917struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000919 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000921 rcu_read_lock();
922 for_each_netdev_rcu(net, dev)
923 if (dev->type == type) {
924 dev_hold(dev);
925 ret = dev;
926 break;
927 }
928 rcu_read_unlock();
929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931EXPORT_SYMBOL(dev_getfirstbyhwtype);
932
933/**
WANG Cong6c555492014-09-11 15:35:09 -0700934 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700935 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * @if_flags: IFF_* values
937 * @mask: bitmask of bits in if_flags to check
938 *
939 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000940 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700941 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
943
WANG Cong6c555492014-09-11 15:35:09 -0700944struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
945 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700947 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
WANG Cong6c555492014-09-11 15:35:09 -0700949 ASSERT_RTNL();
950
Pavel Emelianov7562f872007-05-03 15:13:45 -0700951 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -0700952 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700954 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 }
957 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
WANG Cong6c555492014-09-11 15:35:09 -0700960EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962/**
963 * dev_valid_name - check if name is okay for network device
964 * @name: name string
965 *
966 * Network device names need to be valid file names to
Randy Dunlap4250b752020-09-17 21:35:15 -0700967 * allow sysfs to work. We also disallow any kind of
David S. Millerc7fa9d12006-08-15 16:34:13 -0700968 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
David S. Miller95f050b2012-03-06 16:12:15 -0500970bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700972 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -0500973 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -0700974 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -0500975 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700976 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -0500977 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700978
979 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -0600980 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -0500981 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700982 name++;
983 }
David S. Miller95f050b2012-03-06 16:12:15 -0500984 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700986EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200989 * __dev_alloc_name - allocate a name for a device
990 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200992 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
994 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700995 * id. It scans list of devices to build up a free map, then chooses
996 * the first empty slot. The caller must hold the dev_base or rtnl lock
997 * while allocating the name and adding the device in order to avoid
998 * duplicates.
999 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1000 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
1002
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001003static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 const char *p;
1007 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001008 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct net_device *d;
1010
Rasmus Villemoes93809102017-11-13 00:15:08 +01001011 if (!dev_valid_name(name))
1012 return -EINVAL;
1013
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001014 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (p) {
1016 /*
1017 * Verify the string as this thing may have come from
1018 * the user. There must be either one "%d" and no other "%"
1019 * characters.
1020 */
1021 if (p[1] != 'd' || strchr(p + 2, '%'))
1022 return -EINVAL;
1023
1024 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001025 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (!inuse)
1027 return -ENOMEM;
1028
Eric W. Biederman881d9662007-09-17 11:56:21 -07001029 for_each_netdev(net, d) {
Jiri Bohac6c015a22021-03-18 04:42:53 +01001030 struct netdev_name_node *name_node;
1031 list_for_each_entry(name_node, &d->name_node->list, list) {
1032 if (!sscanf(name_node->name, name, &i))
1033 continue;
1034 if (i < 0 || i >= max_netdevices)
1035 continue;
1036
1037 /* avoid cases where sscanf is not exact inverse of printf */
1038 snprintf(buf, IFNAMSIZ, name, i);
1039 if (!strncmp(buf, name_node->name, IFNAMSIZ))
1040 set_bit(i, inuse);
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!sscanf(d->name, name, &i))
1043 continue;
1044 if (i < 0 || i >= max_netdevices)
1045 continue;
1046
1047 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001048 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!strncmp(buf, d->name, IFNAMSIZ))
1050 set_bit(i, inuse);
1051 }
1052
1053 i = find_first_zero_bit(inuse, max_netdevices);
1054 free_page((unsigned long) inuse);
1055 }
1056
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001057 snprintf(buf, IFNAMSIZ, name, i);
Antoine Tenart75ea27d2021-10-07 18:16:50 +02001058 if (!netdev_name_in_use(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* It is possible to run out of possible slots
1062 * when the name is long and there isn't enough space left
1063 * for the digits, or if all bits are used.
1064 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001065 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001068static int dev_alloc_name_ns(struct net *net,
1069 struct net_device *dev,
1070 const char *name)
1071{
1072 char buf[IFNAMSIZ];
1073 int ret;
1074
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001075 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001076 ret = __dev_alloc_name(net, name, buf);
1077 if (ret >= 0)
1078 strlcpy(dev->name, buf, IFNAMSIZ);
1079 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001082/**
1083 * dev_alloc_name - allocate a name for a device
1084 * @dev: device
1085 * @name: name format string
1086 *
1087 * Passed a format string - eg "lt%d" it will try and find a suitable
1088 * id. It scans list of devices to build up a free map, then chooses
1089 * the first empty slot. The caller must hold the dev_base or rtnl lock
1090 * while allocating the name and adding the device in order to avoid
1091 * duplicates.
1092 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1093 * Returns the number of the unit assigned or a negative errno code.
1094 */
1095
1096int dev_alloc_name(struct net_device *dev, const char *name)
1097{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001098 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001099}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001100EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001101
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001102static int dev_get_valid_name(struct net *net, struct net_device *dev,
1103 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001104{
David S. Miller55a5ec92018-01-02 11:45:07 -05001105 BUG_ON(!net);
1106
1107 if (!dev_valid_name(name))
1108 return -EINVAL;
1109
1110 if (strchr(name, '%'))
1111 return dev_alloc_name_ns(net, dev, name);
Antoine Tenart75ea27d2021-10-07 18:16:50 +02001112 else if (netdev_name_in_use(net, name))
David S. Miller55a5ec92018-01-02 11:45:07 -05001113 return -EEXIST;
1114 else if (dev->name != name)
1115 strlcpy(dev->name, name, IFNAMSIZ);
1116
1117 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120/**
1121 * dev_change_name - change name of a device
1122 * @dev: device
1123 * @newname: name (or format string) must be at least IFNAMSIZ
1124 *
1125 * Change name of a device, can pass format strings "eth%d".
1126 * for wildcarding.
1127 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001128int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Tom Gundersen238fa362014-07-14 16:37:23 +02001130 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001131 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001133 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001134 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001137 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001139 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001140
1141 /* Some auto-enslaved devices e.g. failover slaves are
1142 * special, as userspace might rename the device after
1143 * the interface had been brought up and running since
1144 * the point kernel initiated auto-enslavement. Allow
1145 * live name change even when these slave devices are
1146 * up and running.
1147 *
1148 * Typically, users of these auto-enslaving devices
1149 * don't actually care about slave name change, as
1150 * they are supposed to operate on master interface
1151 * directly.
1152 */
1153 if (dev->flags & IFF_UP &&
1154 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EBUSY;
1156
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001157 down_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001158
1159 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001160 up_write(&devnet_rename_sem);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001161 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001162 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001163
Herbert Xufcc5a032007-07-30 17:03:38 -07001164 memcpy(oldname, dev->name, IFNAMSIZ);
1165
Gao feng828de4f2012-09-13 20:58:27 +00001166 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001167 if (err < 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001168 up_write(&devnet_rename_sem);
Octavian Purdilad9031022009-11-18 02:36:59 +00001169 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001172 if (oldname[0] && !strchr(oldname, '%'))
1173 netdev_info(dev, "renamed from %s\n", oldname);
1174
Tom Gundersen238fa362014-07-14 16:37:23 +02001175 old_assign_type = dev->name_assign_type;
1176 dev->name_assign_type = NET_NAME_RENAMED;
1177
Herbert Xufcc5a032007-07-30 17:03:38 -07001178rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001179 ret = device_rename(&dev->dev, dev->name);
1180 if (ret) {
1181 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001182 dev->name_assign_type = old_assign_type;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001183 up_write(&devnet_rename_sem);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001184 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001185 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001186
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001187 up_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001188
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001189 netdev_adjacent_rename_links(dev, oldname);
1190
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01001191 write_lock(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001192 netdev_name_node_del(dev->name_node);
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01001193 write_unlock(&dev_base_lock);
Eric Dumazet72c95282009-10-30 07:11:27 +00001194
1195 synchronize_rcu();
1196
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01001197 write_lock(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001198 netdev_name_node_add(net, dev->name_node);
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01001199 write_unlock(&dev_base_lock);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001200
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001201 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001202 ret = notifier_to_errno(ret);
1203
1204 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001205 /* err >= 0 after dev_alloc_name() or stores the first errno */
1206 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001207 err = ret;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001208 down_write(&devnet_rename_sem);
Herbert Xufcc5a032007-07-30 17:03:38 -07001209 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001210 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001211 dev->name_assign_type = old_assign_type;
1212 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001213 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001214 } else {
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07001215 netdev_err(dev, "name change rollback failed: %d\n",
1216 ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001217 }
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 return err;
1221}
1222
1223/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001224 * dev_set_alias - change ifalias of a device
1225 * @dev: device
1226 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001227 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001228 *
1229 * Set ifalias for a device,
1230 */
1231int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1232{
Florian Westphal6c557002017-10-02 23:50:05 +02001233 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001234
1235 if (len >= IFALIASZ)
1236 return -EINVAL;
1237
Florian Westphal6c557002017-10-02 23:50:05 +02001238 if (len) {
1239 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1240 if (!new_alias)
1241 return -ENOMEM;
1242
1243 memcpy(new_alias->ifalias, alias, len);
1244 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001245 }
1246
Florian Westphal6c557002017-10-02 23:50:05 +02001247 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001248 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1249 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001250 mutex_unlock(&ifalias_mutex);
1251
1252 if (new_alias)
1253 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001254
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001255 return len;
1256}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001257EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001258
Florian Westphal6c557002017-10-02 23:50:05 +02001259/**
1260 * dev_get_alias - get ifalias of a device
1261 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001262 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001263 * @len: size of buffer
1264 *
1265 * get ifalias for a device. Caller must make sure dev cannot go
1266 * away, e.g. rcu read lock or own a reference count to device.
1267 */
1268int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1269{
1270 const struct dev_ifalias *alias;
1271 int ret = 0;
1272
1273 rcu_read_lock();
1274 alias = rcu_dereference(dev->ifalias);
1275 if (alias)
1276 ret = snprintf(name, len, "%s", alias->ifalias);
1277 rcu_read_unlock();
1278
1279 return ret;
1280}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001281
1282/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001283 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001284 * @dev: device to cause notification
1285 *
1286 * Called to indicate a device has changed features.
1287 */
1288void netdev_features_change(struct net_device *dev)
1289{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001290 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001291}
1292EXPORT_SYMBOL(netdev_features_change);
1293
1294/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * netdev_state_change - device changes state
1296 * @dev: device to cause notification
1297 *
1298 * Called to indicate a device has changed state. This function calls
1299 * the notifier chains for netdev_chain and sends a NEWLINK message
1300 * to the routing socket.
1301 */
1302void netdev_state_change(struct net_device *dev)
1303{
1304 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001305 struct netdev_notifier_change_info change_info = {
1306 .info.dev = dev,
1307 };
Loic Prylli54951192014-07-01 21:39:43 -07001308
David Ahern51d0c0472017-10-04 17:48:45 -07001309 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001310 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001311 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001314EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Amerigo Wangee89bab2012-08-09 22:14:56 +00001316/**
Lijun Pan7061eb82020-12-14 15:19:28 -06001317 * __netdev_notify_peers - notify network peers about existence of @dev,
1318 * to be called when rtnl lock is already held.
1319 * @dev: network device
1320 *
1321 * Generate traffic such that interested network peers are aware of
1322 * @dev, such as by generating a gratuitous ARP. This may be used when
1323 * a device wants to inform the rest of the network about some sort of
1324 * reconfiguration such as a failover event or virtual machine
1325 * migration.
1326 */
1327void __netdev_notify_peers(struct net_device *dev)
1328{
1329 ASSERT_RTNL();
1330 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1331 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1332}
1333EXPORT_SYMBOL(__netdev_notify_peers);
1334
1335/**
tcharding722c9a02017-02-09 17:56:04 +11001336 * netdev_notify_peers - notify network peers about existence of @dev
1337 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001338 *
1339 * Generate traffic such that interested network peers are aware of
1340 * @dev, such as by generating a gratuitous ARP. This may be used when
1341 * a device wants to inform the rest of the network about some sort of
1342 * reconfiguration such as a failover event or virtual machine
1343 * migration.
1344 */
1345void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001346{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001347 rtnl_lock();
Lijun Pan7061eb82020-12-14 15:19:28 -06001348 __netdev_notify_peers(dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001349 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001350}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001351EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001352
Wei Wang29863d42021-02-08 11:34:09 -08001353static int napi_threaded_poll(void *data);
1354
1355static int napi_kthread_create(struct napi_struct *n)
1356{
1357 int err = 0;
1358
1359 /* Create and wake up the kthread once to put it in
1360 * TASK_INTERRUPTIBLE mode to avoid the blocked task
1361 * warning and work with loadavg.
1362 */
1363 n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d",
1364 n->dev->name, n->napi_id);
1365 if (IS_ERR(n->thread)) {
1366 err = PTR_ERR(n->thread);
1367 pr_err("kthread_run failed with err %d\n", err);
1368 n->thread = NULL;
1369 }
1370
1371 return err;
1372}
1373
Petr Machata40c900a2018-12-06 17:05:47 +00001374static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001376 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001377 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001379 ASSERT_RTNL();
Jakub Kicinskid07b26f2021-11-19 06:21:53 -08001380 dev_addr_check(dev);
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001381
Heiner Kallweitbd869242020-06-20 22:35:42 +02001382 if (!netif_device_present(dev)) {
1383 /* may be detached because parent is runtime-suspended */
1384 if (dev->dev.parent)
1385 pm_runtime_resume(dev->dev.parent);
1386 if (!netif_device_present(dev))
1387 return -ENODEV;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Neil Hormanca99ca12013-02-05 08:05:43 +00001390 /* Block netpoll from trying to do any rx path servicing.
1391 * If we don't do this there is a chance ndo_poll_controller
1392 * or ndo_poll may be running while we open the device
1393 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001394 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001395
Petr Machata40c900a2018-12-06 17:05:47 +00001396 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001397 ret = notifier_to_errno(ret);
1398 if (ret)
1399 return ret;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001402
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001403 if (ops->ndo_validate_addr)
1404 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001405
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001406 if (!ret && ops->ndo_open)
1407 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric W. Biederman66b55522014-03-27 15:39:03 -07001409 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001410
Jeff Garzikbada3392007-10-23 20:19:37 -07001411 if (ret)
1412 clear_bit(__LINK_STATE_START, &dev->state);
1413 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001415 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001417 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return ret;
1421}
Patrick McHardybd380812010-02-26 06:34:53 +00001422
1423/**
1424 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001425 * @dev: device to open
1426 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001427 *
1428 * Takes a device from down to up state. The device's private open
1429 * function is invoked and then the multicast lists are loaded. Finally
1430 * the device is moved into the up state and a %NETDEV_UP message is
1431 * sent to the netdev notifier chain.
1432 *
1433 * Calling this function on an active interface is a nop. On a failure
1434 * a negative errno code is returned.
1435 */
Petr Machata00f54e62018-12-06 17:05:36 +00001436int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001437{
1438 int ret;
1439
Patrick McHardybd380812010-02-26 06:34:53 +00001440 if (dev->flags & IFF_UP)
1441 return 0;
1442
Petr Machata40c900a2018-12-06 17:05:47 +00001443 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001444 if (ret < 0)
1445 return ret;
1446
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001447 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001448 call_netdevice_notifiers(NETDEV_UP, dev);
1449
1450 return ret;
1451}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001452EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
stephen hemminger7051b882017-07-18 15:59:27 -07001454static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Octavian Purdila44345722010-12-13 12:44:07 +00001456 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001457
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001458 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001459 might_sleep();
1460
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001461 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001462 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001463 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001464
Octavian Purdila44345722010-12-13 12:44:07 +00001465 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Octavian Purdila44345722010-12-13 12:44:07 +00001467 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Octavian Purdila44345722010-12-13 12:44:07 +00001469 /* Synchronize to scheduled poll. We cannot touch poll list, it
1470 * can be even on different cpu. So just clear netif_running().
1471 *
1472 * dev->stop() will invoke napi_disable() on all of it's
1473 * napi_struct instances on this device.
1474 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001475 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Octavian Purdila44345722010-12-13 12:44:07 +00001478 dev_deactivate_many(head);
1479
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001480 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001481 const struct net_device_ops *ops = dev->netdev_ops;
1482
1483 /*
1484 * Call the device specific close. This cannot fail.
1485 * Only if device is UP
1486 *
1487 * We allow it to be called even after a DETACH hot-plug
1488 * event.
1489 */
1490 if (ops->ndo_stop)
1491 ops->ndo_stop(dev);
1492
Octavian Purdila44345722010-12-13 12:44:07 +00001493 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001494 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001495 }
Octavian Purdila44345722010-12-13 12:44:07 +00001496}
1497
stephen hemminger7051b882017-07-18 15:59:27 -07001498static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001499{
1500 LIST_HEAD(single);
1501
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001502 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001503 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001504 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001505}
1506
stephen hemminger7051b882017-07-18 15:59:27 -07001507void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001508{
1509 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001510
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001511 /* Remove the devices that don't need to be closed */
1512 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001513 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001514 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001515
1516 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001517
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001518 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001519 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001520 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001521 if (unlink)
1522 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
David S. Miller99c4a262015-03-18 22:52:33 -04001525EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001526
1527/**
1528 * dev_close - shutdown an interface.
1529 * @dev: device to shutdown
1530 *
1531 * This function moves an active device into down state. A
1532 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1533 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1534 * chain.
1535 */
stephen hemminger7051b882017-07-18 15:59:27 -07001536void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001537{
Eric Dumazete14a5992011-05-10 12:26:06 -07001538 if (dev->flags & IFF_UP) {
1539 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001540
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001541 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001542 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001543 list_del(&single);
1544 }
Patrick McHardybd380812010-02-26 06:34:53 +00001545}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001546EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001549/**
1550 * dev_disable_lro - disable Large Receive Offload on a device
1551 * @dev: device
1552 *
1553 * Disable Large Receive Offload (LRO) on a net device. Must be
1554 * called under RTNL. This is needed if received packets may be
1555 * forwarded to another interface.
1556 */
1557void dev_disable_lro(struct net_device *dev)
1558{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001559 struct net_device *lower_dev;
1560 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001561
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001562 dev->wanted_features &= ~NETIF_F_LRO;
1563 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001564
Michał Mirosław22d59692011-04-21 12:42:15 +00001565 if (unlikely(dev->features & NETIF_F_LRO))
1566 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001567
1568 netdev_for_each_lower_dev(dev, lower_dev, iter)
1569 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001570}
1571EXPORT_SYMBOL(dev_disable_lro);
1572
Michael Chan56f5aa72017-12-16 03:09:41 -05001573/**
1574 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1575 * @dev: device
1576 *
1577 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1578 * called under RTNL. This is needed if Generic XDP is installed on
1579 * the device.
1580 */
1581static void dev_disable_gro_hw(struct net_device *dev)
1582{
1583 dev->wanted_features &= ~NETIF_F_GRO_HW;
1584 netdev_update_features(dev);
1585
1586 if (unlikely(dev->features & NETIF_F_GRO_HW))
1587 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1588}
1589
Kirill Tkhaiede27622018-03-23 19:47:19 +03001590const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1591{
1592#define N(val) \
1593 case NETDEV_##val: \
1594 return "NETDEV_" __stringify(val);
1595 switch (cmd) {
1596 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1597 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1598 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1599 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1600 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1601 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1602 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001603 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1604 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001605 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001606 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001607#undef N
1608 return "UNKNOWN_NETDEV_EVENT";
1609}
1610EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1611
Jiri Pirko351638e2013-05-28 01:30:21 +00001612static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1613 struct net_device *dev)
1614{
David Ahern51d0c0472017-10-04 17:48:45 -07001615 struct netdev_notifier_info info = {
1616 .dev = dev,
1617 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001618
Jiri Pirko351638e2013-05-28 01:30:21 +00001619 return nb->notifier_call(nb, val, &info);
1620}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001621
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001622static int call_netdevice_register_notifiers(struct notifier_block *nb,
1623 struct net_device *dev)
1624{
1625 int err;
1626
1627 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1628 err = notifier_to_errno(err);
1629 if (err)
1630 return err;
1631
1632 if (!(dev->flags & IFF_UP))
1633 return 0;
1634
1635 call_netdevice_notifier(nb, NETDEV_UP, dev);
1636 return 0;
1637}
1638
1639static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1640 struct net_device *dev)
1641{
1642 if (dev->flags & IFF_UP) {
1643 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1644 dev);
1645 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1646 }
1647 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1648}
1649
1650static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1651 struct net *net)
1652{
1653 struct net_device *dev;
1654 int err;
1655
1656 for_each_netdev(net, dev) {
1657 err = call_netdevice_register_notifiers(nb, dev);
1658 if (err)
1659 goto rollback;
1660 }
1661 return 0;
1662
1663rollback:
1664 for_each_netdev_continue_reverse(net, dev)
1665 call_netdevice_unregister_notifiers(nb, dev);
1666 return err;
1667}
1668
1669static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1670 struct net *net)
1671{
1672 struct net_device *dev;
1673
1674 for_each_netdev(net, dev)
1675 call_netdevice_unregister_notifiers(nb, dev);
1676}
1677
Eric W. Biederman881d9662007-09-17 11:56:21 -07001678static int dev_boot_phase = 1;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/**
tcharding722c9a02017-02-09 17:56:04 +11001681 * register_netdevice_notifier - register a network notifier block
1682 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 *
tcharding722c9a02017-02-09 17:56:04 +11001684 * Register a notifier to be called when network device events occur.
1685 * The notifier passed is linked into the kernel structures and must
1686 * not be reused until it has been unregistered. A negative errno code
1687 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *
tcharding722c9a02017-02-09 17:56:04 +11001689 * When registered all registration and up events are replayed
1690 * to the new notifier to allow device to have a race free
1691 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
1693
1694int register_netdevice_notifier(struct notifier_block *nb)
1695{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001696 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int err;
1698
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001699 /* Close race with setup_net() and cleanup_net() */
1700 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001702 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001703 if (err)
1704 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001705 if (dev_boot_phase)
1706 goto unlock;
1707 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001708 err = call_netdevice_register_net_notifiers(nb, net);
1709 if (err)
1710 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001712
1713unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001715 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001717
1718rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001719 for_each_net_continue_reverse(net)
1720 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001721
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001722 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001723 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001725EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727/**
tcharding722c9a02017-02-09 17:56:04 +11001728 * unregister_netdevice_notifier - unregister a network notifier block
1729 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *
tcharding722c9a02017-02-09 17:56:04 +11001731 * Unregister a notifier previously registered by
1732 * register_netdevice_notifier(). The notifier is unlinked into the
1733 * kernel structures and may then be reused. A negative errno code
1734 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001735 *
tcharding722c9a02017-02-09 17:56:04 +11001736 * After unregistering unregister and down device events are synthesized
1737 * for all devices on the device list to the removed notifier to remove
1738 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
1740
1741int unregister_netdevice_notifier(struct notifier_block *nb)
1742{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001743 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001744 int err;
1745
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001746 /* Close race with setup_net() and cleanup_net() */
1747 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001748 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001749 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001750 if (err)
1751 goto unlock;
1752
Jiri Pirko48b3a132020-01-25 12:17:06 +01001753 for_each_net(net)
1754 call_netdevice_unregister_net_notifiers(nb, net);
1755
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001756unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001757 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001758 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001761EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jiri Pirko1f637702020-01-25 12:17:07 +01001763static int __register_netdevice_notifier_net(struct net *net,
1764 struct notifier_block *nb,
1765 bool ignore_call_fail)
1766{
1767 int err;
1768
1769 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1770 if (err)
1771 return err;
1772 if (dev_boot_phase)
1773 return 0;
1774
1775 err = call_netdevice_register_net_notifiers(nb, net);
1776 if (err && !ignore_call_fail)
1777 goto chain_unregister;
1778
1779 return 0;
1780
1781chain_unregister:
1782 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1783 return err;
1784}
1785
1786static int __unregister_netdevice_notifier_net(struct net *net,
1787 struct notifier_block *nb)
1788{
1789 int err;
1790
1791 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1792 if (err)
1793 return err;
1794
1795 call_netdevice_unregister_net_notifiers(nb, net);
1796 return 0;
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001800 * register_netdevice_notifier_net - register a per-netns network notifier block
1801 * @net: network namespace
1802 * @nb: notifier
1803 *
1804 * Register a notifier to be called when network device events occur.
1805 * The notifier passed is linked into the kernel structures and must
1806 * not be reused until it has been unregistered. A negative errno code
1807 * is returned on a failure.
1808 *
1809 * When registered all registration and up events are replayed
1810 * to the new notifier to allow device to have a race free
1811 * view of the network device list.
1812 */
1813
1814int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1815{
1816 int err;
1817
1818 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001819 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001820 rtnl_unlock();
1821 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001822}
1823EXPORT_SYMBOL(register_netdevice_notifier_net);
1824
1825/**
1826 * unregister_netdevice_notifier_net - unregister a per-netns
1827 * network notifier block
1828 * @net: network namespace
1829 * @nb: notifier
1830 *
1831 * Unregister a notifier previously registered by
1832 * register_netdevice_notifier(). The notifier is unlinked into the
1833 * kernel structures and may then be reused. A negative errno code
1834 * is returned on a failure.
1835 *
1836 * After unregistering unregister and down device events are synthesized
1837 * for all devices on the device list to the removed notifier to remove
1838 * the need for special case cleanup code.
1839 */
1840
1841int unregister_netdevice_notifier_net(struct net *net,
1842 struct notifier_block *nb)
1843{
1844 int err;
1845
1846 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001847 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001848 rtnl_unlock();
1849 return err;
1850}
1851EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1852
Jiri Pirko93642e12020-01-25 12:17:08 +01001853int register_netdevice_notifier_dev_net(struct net_device *dev,
1854 struct notifier_block *nb,
1855 struct netdev_net_notifier *nn)
1856{
1857 int err;
1858
1859 rtnl_lock();
1860 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1861 if (!err) {
1862 nn->nb = nb;
1863 list_add(&nn->list, &dev->net_notifier_list);
1864 }
1865 rtnl_unlock();
1866 return err;
1867}
1868EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1869
1870int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1871 struct notifier_block *nb,
1872 struct netdev_net_notifier *nn)
1873{
1874 int err;
1875
1876 rtnl_lock();
1877 list_del(&nn->list);
1878 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1879 rtnl_unlock();
1880 return err;
1881}
1882EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1883
1884static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1885 struct net *net)
1886{
1887 struct netdev_net_notifier *nn;
1888
1889 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1890 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1891 __register_netdevice_notifier_net(net, nn->nb, true);
1892 }
1893}
1894
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001895/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001896 * call_netdevice_notifiers_info - call all network notifier blocks
1897 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001898 * @info: notifier information data
1899 *
1900 * Call all network notifier blocks. Parameters and return value
1901 * are as for raw_notifier_call_chain().
1902 */
1903
stephen hemminger1d143d92013-12-29 14:01:29 -08001904static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001905 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001906{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001907 struct net *net = dev_net(info->dev);
1908 int ret;
1909
Jiri Pirko351638e2013-05-28 01:30:21 +00001910 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001911
1912 /* Run per-netns notifier block chain first, then run the global one.
1913 * Hopefully, one day, the global one is going to be removed after
1914 * all notifier block registrators get converted to be per-netns.
1915 */
1916 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1917 if (ret & NOTIFY_STOP_MASK)
1918 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00001919 return raw_notifier_call_chain(&netdev_chain, val, info);
1920}
Jiri Pirko351638e2013-05-28 01:30:21 +00001921
Petr Machata26372602018-12-06 17:05:45 +00001922static int call_netdevice_notifiers_extack(unsigned long val,
1923 struct net_device *dev,
1924 struct netlink_ext_ack *extack)
1925{
1926 struct netdev_notifier_info info = {
1927 .dev = dev,
1928 .extack = extack,
1929 };
1930
1931 return call_netdevice_notifiers_info(val, &info);
1932}
1933
Jiri Pirko351638e2013-05-28 01:30:21 +00001934/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 * call_netdevice_notifiers - call all network notifier blocks
1936 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001937 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 *
1939 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001940 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
1942
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001943int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Petr Machata26372602018-12-06 17:05:45 +00001945 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001947EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001949/**
1950 * call_netdevice_notifiers_mtu - call all network notifier blocks
1951 * @val: value passed unmodified to notifier function
1952 * @dev: net_device pointer passed unmodified to notifier function
1953 * @arg: additional u32 argument passed to the notifier function
1954 *
1955 * Call all network notifier blocks. Parameters and return value
1956 * are as for raw_notifier_call_chain().
1957 */
1958static int call_netdevice_notifiers_mtu(unsigned long val,
1959 struct net_device *dev, u32 arg)
1960{
1961 struct netdev_notifier_info_ext info = {
1962 .info.dev = dev,
1963 .ext.mtu = arg,
1964 };
1965
1966 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1967
1968 return call_netdevice_notifiers_info(val, &info.info);
1969}
1970
Pablo Neira1cf519002015-05-13 18:19:37 +02001971#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001972static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001973
1974void net_inc_ingress_queue(void)
1975{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001976 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001977}
1978EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1979
1980void net_dec_ingress_queue(void)
1981{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001982 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001983}
1984EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1985#endif
1986
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001987#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001988static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001989
1990void net_inc_egress_queue(void)
1991{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001992 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001993}
1994EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1995
1996void net_dec_egress_queue(void)
1997{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001998 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001999}
2000EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2001#endif
2002
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002003static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002004#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002005static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002006static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002007static void netstamp_clear(struct work_struct *work)
2008{
2009 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002010 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002011
Eric Dumazet13baa002017-03-01 14:28:39 -08002012 wanted = atomic_add_return(deferred, &netstamp_wanted);
2013 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002014 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002015 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002016 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002017}
2018static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002019#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021void net_enable_timestamp(void)
2022{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002023#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002024 int wanted;
2025
2026 while (1) {
2027 wanted = atomic_read(&netstamp_wanted);
2028 if (wanted <= 0)
2029 break;
2030 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2031 return;
2032 }
2033 atomic_inc(&netstamp_needed_deferred);
2034 schedule_work(&netstamp_work);
2035#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002036 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002039EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041void net_disable_timestamp(void)
2042{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002043#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002044 int wanted;
2045
2046 while (1) {
2047 wanted = atomic_read(&netstamp_wanted);
2048 if (wanted <= 1)
2049 break;
2050 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2051 return;
2052 }
2053 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002054 schedule_work(&netstamp_work);
2055#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002056 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002059EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Eric Dumazet3b098e22010-05-15 23:57:10 -07002061static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002063 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002064 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002065 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002068#define net_timestamp_check(COND, SKB) \
2069 if (static_branch_unlikely(&netstamp_needed_key)) { \
2070 if ((COND) && !(SKB)->tstamp) \
2071 __net_timestamp(SKB); \
2072 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002073
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002074bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002075{
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002076 return __is_skb_forwardable(dev, skb, true);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002077}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002078EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002079
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002080static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb,
2081 bool check_mtu)
Herbert Xua0265d22014-04-17 13:45:03 +08002082{
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002083 int ret = ____dev_forward_skb(dev, skb, check_mtu);
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002084
2085 if (likely(!ret)) {
2086 skb->protocol = eth_type_trans(skb, dev);
2087 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002088 }
2089
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002090 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002091}
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002092
2093int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2094{
2095 return __dev_forward_skb2(dev, skb, true);
2096}
Herbert Xua0265d22014-04-17 13:45:03 +08002097EXPORT_SYMBOL_GPL(__dev_forward_skb);
2098
Arnd Bergmann44540962009-11-26 06:07:08 +00002099/**
2100 * dev_forward_skb - loopback an skb to another netif
2101 *
2102 * @dev: destination network device
2103 * @skb: buffer to forward
2104 *
2105 * return values:
2106 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002107 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002108 *
2109 * dev_forward_skb can be used for injecting an skb from the
2110 * start_xmit function of one device into the receive queue
2111 * of another device.
2112 *
2113 * The receiving device may be in another namespace, so
2114 * we have to clear all information in the skb that could
2115 * impact namespace isolation.
2116 */
2117int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2118{
Herbert Xua0265d22014-04-17 13:45:03 +08002119 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002120}
2121EXPORT_SYMBOL_GPL(dev_forward_skb);
2122
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002123int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb)
2124{
2125 return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb);
2126}
2127
Changli Gao71d9dec2010-12-15 19:57:25 +00002128static inline int deliver_skb(struct sk_buff *skb,
2129 struct packet_type *pt_prev,
2130 struct net_device *orig_dev)
2131{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002132 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002133 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002134 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002135 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2136}
2137
Salam Noureddine7866a622015-01-27 11:35:48 -08002138static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2139 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002140 struct net_device *orig_dev,
2141 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002142 struct list_head *ptype_list)
2143{
2144 struct packet_type *ptype, *pt_prev = *pt;
2145
2146 list_for_each_entry_rcu(ptype, ptype_list, list) {
2147 if (ptype->type != type)
2148 continue;
2149 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002150 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002151 pt_prev = ptype;
2152 }
2153 *pt = pt_prev;
2154}
2155
Eric Leblondc0de08d2012-08-16 22:02:58 +00002156static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2157{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002158 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002159 return false;
2160
2161 if (ptype->id_match)
2162 return ptype->id_match(ptype, skb->sk);
2163 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2164 return true;
2165
2166 return false;
2167}
2168
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002169/**
2170 * dev_nit_active - return true if any network interface taps are in use
2171 *
2172 * @dev: network device to check for the presence of taps
2173 */
2174bool dev_nit_active(struct net_device *dev)
2175{
2176 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2177}
2178EXPORT_SYMBOL_GPL(dev_nit_active);
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180/*
2181 * Support routine. Sends outgoing frames to any network
2182 * taps currently in use.
2183 */
2184
David Ahern74b20582016-05-10 11:19:50 -07002185void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002188 struct sk_buff *skb2 = NULL;
2189 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002190 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002193again:
2194 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002195 if (ptype->ignore_outgoing)
2196 continue;
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /* Never send packets back to the socket
2199 * they originated from - MvS (miquels@drinkel.ow.org)
2200 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002201 if (skb_loop_sk(ptype, skb))
2202 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002203
Salam Noureddine7866a622015-01-27 11:35:48 -08002204 if (pt_prev) {
2205 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002206 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002207 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002209
2210 /* need to clone skb, done only once */
2211 skb2 = skb_clone(skb, GFP_ATOMIC);
2212 if (!skb2)
2213 goto out_unlock;
2214
2215 net_timestamp_set(skb2);
2216
2217 /* skb->nh should be correctly
2218 * set by sender, so that the second statement is
2219 * just protection against buggy protocols.
2220 */
2221 skb_reset_mac_header(skb2);
2222
2223 if (skb_network_header(skb2) < skb2->data ||
2224 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2225 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2226 ntohs(skb2->protocol),
2227 dev->name);
2228 skb_reset_network_header(skb2);
2229 }
2230
2231 skb2->transport_header = skb2->network_header;
2232 skb2->pkt_type = PACKET_OUTGOING;
2233 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002235
2236 if (ptype_list == &ptype_all) {
2237 ptype_list = &dev->ptype_all;
2238 goto again;
2239 }
2240out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002241 if (pt_prev) {
2242 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2243 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2244 else
2245 kfree_skb(skb2);
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 rcu_read_unlock();
2248}
David Ahern74b20582016-05-10 11:19:50 -07002249EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Ben Hutchings2c530402012-07-10 10:55:09 +00002251/**
2252 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002253 * @dev: Network device
2254 * @txq: number of queues available
2255 *
2256 * If real_num_tx_queues is changed the tc mappings may no longer be
2257 * valid. To resolve this verify the tc mapping remains valid and if
2258 * not NULL the mapping. With no priorities mapping to this
2259 * offset/count pair it will no longer be used. In the worst case TC0
2260 * is invalid nothing can be done so disable priority mappings. If is
2261 * expected that drivers will fix this mapping if they can before
2262 * calling netif_set_real_num_tx_queues.
2263 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002264static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002265{
2266 int i;
2267 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2268
2269 /* If TC0 is invalidated disable TC mapping */
2270 if (tc->offset + tc->count > txq) {
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07002271 netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002272 dev->num_tc = 0;
2273 return;
2274 }
2275
2276 /* Invalidated prio to tc mappings set to TC0 */
2277 for (i = 1; i < TC_BITMASK + 1; i++) {
2278 int q = netdev_get_prio_tc_map(dev, i);
2279
2280 tc = &dev->tc_to_txq[q];
2281 if (tc->offset + tc->count > txq) {
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07002282 netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2283 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002284 netdev_set_prio_tc_map(dev, i, 0);
2285 }
2286 }
2287}
2288
Alexander Duyck8d059b02016-10-28 11:43:49 -04002289int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2290{
2291 if (dev->num_tc) {
2292 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2293 int i;
2294
Alexander Duyckffcfe252018-07-09 12:19:38 -04002295 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002296 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2297 if ((txq - tc->offset) < tc->count)
2298 return i;
2299 }
2300
Alexander Duyckffcfe252018-07-09 12:19:38 -04002301 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002302 return -1;
2303 }
2304
2305 return 0;
2306}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002307EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002308
Alexander Duyck537c00d2013-01-10 08:57:02 +00002309#ifdef CONFIG_XPS
Vladimir Oltean5da9ace32021-03-22 13:30:19 +02002310static struct static_key xps_needed __read_mostly;
2311static struct static_key xps_rxqs_needed __read_mostly;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002312static DEFINE_MUTEX(xps_map_mutex);
2313#define xmap_dereference(P) \
2314 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2315
Alexander Duyck6234f872016-10-28 11:46:49 -04002316static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002317 struct xps_dev_maps *old_maps, int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002318{
2319 struct xps_map *map = NULL;
2320 int pos;
2321
2322 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002323 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002324 if (!map)
2325 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002326
Alexander Duyck6234f872016-10-28 11:46:49 -04002327 for (pos = map->len; pos--;) {
2328 if (map->queues[pos] != index)
2329 continue;
2330
2331 if (map->len > 1) {
2332 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002333 break;
2334 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002335
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002336 if (old_maps)
2337 RCU_INIT_POINTER(old_maps->attr_map[tci], NULL);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002338 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002339 kfree_rcu(map, rcu);
2340 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002341 }
2342
Alexander Duyck6234f872016-10-28 11:46:49 -04002343 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002344}
2345
Alexander Duyck6234f872016-10-28 11:46:49 -04002346static bool remove_xps_queue_cpu(struct net_device *dev,
2347 struct xps_dev_maps *dev_maps,
2348 int cpu, u16 offset, u16 count)
2349{
Antoine Tenart255c04a2021-03-18 19:37:43 +01002350 int num_tc = dev_maps->num_tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002351 bool active = false;
2352 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002353
Alexander Duyck184c4492016-10-28 11:50:13 -04002354 for (tci = cpu * num_tc; num_tc--; tci++) {
2355 int i, j;
2356
2357 for (i = count, j = offset; i--; j++) {
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002358 if (!remove_xps_queue(dev_maps, NULL, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002359 break;
2360 }
2361
2362 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002363 }
2364
Alexander Duyck184c4492016-10-28 11:50:13 -04002365 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002366}
2367
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002368static void reset_xps_maps(struct net_device *dev,
2369 struct xps_dev_maps *dev_maps,
Antoine Tenart044ab862021-03-18 19:37:46 +01002370 enum xps_map_type type)
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002371{
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002372 static_key_slow_dec_cpuslocked(&xps_needed);
Antoine Tenart044ab862021-03-18 19:37:46 +01002373 if (type == XPS_RXQS)
2374 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2375
2376 RCU_INIT_POINTER(dev->xps_maps[type], NULL);
2377
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002378 kfree_rcu(dev_maps, rcu);
2379}
2380
Antoine Tenart044ab862021-03-18 19:37:46 +01002381static void clean_xps_maps(struct net_device *dev, enum xps_map_type type,
2382 u16 offset, u16 count)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002383{
Antoine Tenart044ab862021-03-18 19:37:46 +01002384 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002385 bool active = false;
2386 int i, j;
2387
Antoine Tenart044ab862021-03-18 19:37:46 +01002388 dev_maps = xmap_dereference(dev->xps_maps[type]);
2389 if (!dev_maps)
2390 return;
2391
Antoine Tenart6f361582021-03-18 19:37:45 +01002392 for (j = 0; j < dev_maps->nr_ids; j++)
2393 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002394 if (!active)
Antoine Tenart044ab862021-03-18 19:37:46 +01002395 reset_xps_maps(dev, dev_maps, type);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002396
Antoine Tenart044ab862021-03-18 19:37:46 +01002397 if (type == XPS_CPUS) {
Antoine Tenart6f361582021-03-18 19:37:45 +01002398 for (i = offset + (count - 1); count--; i--)
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002399 netdev_queue_numa_node_write(
Antoine Tenart6f361582021-03-18 19:37:45 +01002400 netdev_get_tx_queue(dev, i), NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002401 }
2402}
2403
Alexander Duyck6234f872016-10-28 11:46:49 -04002404static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2405 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002406{
Amritha Nambiar04157462018-06-29 21:26:46 -07002407 if (!static_key_false(&xps_needed))
2408 return;
2409
Andrei Vagin4d99f662018-08-08 20:07:35 -07002410 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002411 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002412
Antoine Tenart044ab862021-03-18 19:37:46 +01002413 if (static_key_false(&xps_rxqs_needed))
2414 clean_xps_maps(dev, XPS_RXQS, offset, count);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002415
Antoine Tenart044ab862021-03-18 19:37:46 +01002416 clean_xps_maps(dev, XPS_CPUS, offset, count);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002417
Alexander Duyck537c00d2013-01-10 08:57:02 +00002418 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002419 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002420}
2421
Alexander Duyck6234f872016-10-28 11:46:49 -04002422static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2423{
2424 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2425}
2426
Amritha Nambiar80d19662018-06-29 21:26:41 -07002427static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2428 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002429{
2430 struct xps_map *new_map;
2431 int alloc_len = XPS_MIN_MAP_ALLOC;
2432 int i, pos;
2433
2434 for (pos = 0; map && pos < map->len; pos++) {
2435 if (map->queues[pos] != index)
2436 continue;
2437 return map;
2438 }
2439
Amritha Nambiar80d19662018-06-29 21:26:41 -07002440 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002441 if (map) {
2442 if (pos < map->alloc_len)
2443 return map;
2444
2445 alloc_len = map->alloc_len * 2;
2446 }
2447
Amritha Nambiar80d19662018-06-29 21:26:41 -07002448 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2449 * map
2450 */
2451 if (is_rxqs_map)
2452 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2453 else
2454 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2455 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002456 if (!new_map)
2457 return NULL;
2458
2459 for (i = 0; i < pos; i++)
2460 new_map->queues[i] = map->queues[i];
2461 new_map->alloc_len = alloc_len;
2462 new_map->len = pos;
2463
2464 return new_map;
2465}
2466
Antoine Tenart402fbb92021-03-18 19:37:47 +01002467/* Copy xps maps at a given index */
2468static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps,
2469 struct xps_dev_maps *new_dev_maps, int index,
2470 int tc, bool skip_tc)
2471{
2472 int i, tci = index * dev_maps->num_tc;
2473 struct xps_map *map;
2474
2475 /* copy maps belonging to foreign traffic classes */
2476 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2477 if (i == tc && skip_tc)
2478 continue;
2479
2480 /* fill in the new device map from the old device map */
2481 map = xmap_dereference(dev_maps->attr_map[tci]);
2482 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2483 }
2484}
2485
Andrei Vagin4d99f662018-08-08 20:07:35 -07002486/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002487int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
Antoine Tenart044ab862021-03-18 19:37:46 +01002488 u16 index, enum xps_map_type type)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002489{
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002490 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
Antoine Tenart6f361582021-03-18 19:37:45 +01002491 const unsigned long *online_mask = NULL;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002492 bool active = false, copy = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002493 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002494 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002495 struct xps_map *map, *new_map;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002496 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002497
Alexander Duyck184c4492016-10-28 11:50:13 -04002498 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002499 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002500 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002501 if (num_tc < 0)
2502 return -EINVAL;
2503
2504 /* If queue belongs to subordinate dev use its map */
2505 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2506
Alexander Duyck184c4492016-10-28 11:50:13 -04002507 tc = netdev_txq_to_tc(dev, index);
2508 if (tc < 0)
2509 return -EINVAL;
2510 }
2511
Amritha Nambiar80d19662018-06-29 21:26:41 -07002512 mutex_lock(&xps_map_mutex);
Antoine Tenart044ab862021-03-18 19:37:46 +01002513
2514 dev_maps = xmap_dereference(dev->xps_maps[type]);
2515 if (type == XPS_RXQS) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002516 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002517 nr_ids = dev->num_rx_queues;
2518 } else {
2519 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
Antoine Tenart6f361582021-03-18 19:37:45 +01002520 if (num_possible_cpus() > 1)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002521 online_mask = cpumask_bits(cpu_online_mask);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002522 nr_ids = nr_cpu_ids;
2523 }
2524
Alexander Duyck184c4492016-10-28 11:50:13 -04002525 if (maps_sz < L1_CACHE_BYTES)
2526 maps_sz = L1_CACHE_BYTES;
2527
Antoine Tenart255c04a2021-03-18 19:37:43 +01002528 /* The old dev_maps could be larger or smaller than the one we're
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002529 * setting up now, as dev->num_tc or nr_ids could have been updated in
2530 * between. We could try to be smart, but let's be safe instead and only
2531 * copy foreign traffic classes if the two map sizes match.
Antoine Tenart255c04a2021-03-18 19:37:43 +01002532 */
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002533 if (dev_maps &&
2534 dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids)
Antoine Tenart255c04a2021-03-18 19:37:43 +01002535 copy = true;
2536
Alexander Duyck01c5f862013-01-10 08:57:35 +00002537 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002538 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2539 j < nr_ids;) {
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002540 if (!new_dev_maps) {
Antoine Tenart255c04a2021-03-18 19:37:43 +01002541 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2542 if (!new_dev_maps) {
2543 mutex_unlock(&xps_map_mutex);
2544 return -ENOMEM;
2545 }
2546
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002547 new_dev_maps->nr_ids = nr_ids;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002548 new_dev_maps->num_tc = num_tc;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002549 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002550
Amritha Nambiar80d19662018-06-29 21:26:41 -07002551 tci = j * num_tc + tc;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002552 map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002553
Antoine Tenart044ab862021-03-18 19:37:46 +01002554 map = expand_xps_map(map, j, index, type == XPS_RXQS);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002555 if (!map)
2556 goto error;
2557
Amritha Nambiar80d19662018-06-29 21:26:41 -07002558 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002559 }
2560
2561 if (!new_dev_maps)
2562 goto out_no_new_maps;
2563
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002564 if (!dev_maps) {
2565 /* Increment static keys at most once per type */
2566 static_key_slow_inc_cpuslocked(&xps_needed);
Antoine Tenart044ab862021-03-18 19:37:46 +01002567 if (type == XPS_RXQS)
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002568 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2569 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002570
Antoine Tenart6f361582021-03-18 19:37:45 +01002571 for (j = 0; j < nr_ids; j++) {
Antoine Tenart402fbb92021-03-18 19:37:47 +01002572 bool skip_tc = false;
Alexander Duyck184c4492016-10-28 11:50:13 -04002573
Amritha Nambiar80d19662018-06-29 21:26:41 -07002574 tci = j * num_tc + tc;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002575 if (netif_attr_test_mask(j, mask, nr_ids) &&
2576 netif_attr_test_online(j, online_mask, nr_ids)) {
2577 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002578 int pos = 0;
2579
Antoine Tenart402fbb92021-03-18 19:37:47 +01002580 skip_tc = true;
2581
Amritha Nambiar80d19662018-06-29 21:26:41 -07002582 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002583 while ((pos < map->len) && (map->queues[pos] != index))
2584 pos++;
2585
2586 if (pos == map->len)
2587 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002588#ifdef CONFIG_NUMA
Antoine Tenart044ab862021-03-18 19:37:46 +01002589 if (type == XPS_CPUS) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002590 if (numa_node_id == -2)
2591 numa_node_id = cpu_to_node(j);
2592 else if (numa_node_id != cpu_to_node(j))
2593 numa_node_id = -1;
2594 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002595#endif
Alexander Duyck537c00d2013-01-10 08:57:02 +00002596 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002597
Antoine Tenart402fbb92021-03-18 19:37:47 +01002598 if (copy)
2599 xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc,
2600 skip_tc);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002601 }
2602
Antoine Tenart044ab862021-03-18 19:37:46 +01002603 rcu_assign_pointer(dev->xps_maps[type], new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002604
Alexander Duyck537c00d2013-01-10 08:57:02 +00002605 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002606 if (!dev_maps)
2607 goto out_no_old_maps;
2608
Antoine Tenart6f361582021-03-18 19:37:45 +01002609 for (j = 0; j < dev_maps->nr_ids; j++) {
Antoine Tenart255c04a2021-03-18 19:37:43 +01002610 for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002611 map = xmap_dereference(dev_maps->attr_map[tci]);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002612 if (!map)
2613 continue;
2614
2615 if (copy) {
2616 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2617 if (map == new_map)
2618 continue;
2619 }
2620
Antoine Tenart75b27582021-03-18 19:37:52 +01002621 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002622 kfree_rcu(map, rcu);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002623 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002624 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002625
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002626 old_dev_maps = dev_maps;
Alexander Duyck184c4492016-10-28 11:50:13 -04002627
2628out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002629 dev_maps = new_dev_maps;
2630 active = true;
2631
2632out_no_new_maps:
Antoine Tenart044ab862021-03-18 19:37:46 +01002633 if (type == XPS_CPUS)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002634 /* update Tx queue numa node */
2635 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2636 (numa_node_id >= 0) ?
2637 numa_node_id : NUMA_NO_NODE);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002638
Alexander Duyck01c5f862013-01-10 08:57:35 +00002639 if (!dev_maps)
2640 goto out_no_maps;
2641
Amritha Nambiar80d19662018-06-29 21:26:41 -07002642 /* removes tx-queue from unused CPUs/rx-queues */
Antoine Tenart6f361582021-03-18 19:37:45 +01002643 for (j = 0; j < dev_maps->nr_ids; j++) {
Antoine Tenart132f7432021-03-18 19:37:48 +01002644 tci = j * dev_maps->num_tc;
2645
2646 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2647 if (i == tc &&
2648 netif_attr_test_mask(j, mask, dev_maps->nr_ids) &&
2649 netif_attr_test_online(j, online_mask, dev_maps->nr_ids))
2650 continue;
2651
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002652 active |= remove_xps_queue(dev_maps,
2653 copy ? old_dev_maps : NULL,
2654 tci, index);
Antoine Tenart132f7432021-03-18 19:37:48 +01002655 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002656 }
2657
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002658 if (old_dev_maps)
2659 kfree_rcu(old_dev_maps, rcu);
2660
Alexander Duyck01c5f862013-01-10 08:57:35 +00002661 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002662 if (!active)
Antoine Tenart044ab862021-03-18 19:37:46 +01002663 reset_xps_maps(dev, dev_maps, type);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002664
2665out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002666 mutex_unlock(&xps_map_mutex);
2667
2668 return 0;
2669error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002670 /* remove any maps that we added */
Antoine Tenart6f361582021-03-18 19:37:45 +01002671 for (j = 0; j < nr_ids; j++) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002672 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2673 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002674 map = copy ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002675 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002676 NULL;
2677 if (new_map && new_map != map)
2678 kfree(new_map);
2679 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002680 }
2681
Alexander Duyck537c00d2013-01-10 08:57:02 +00002682 mutex_unlock(&xps_map_mutex);
2683
Alexander Duyck537c00d2013-01-10 08:57:02 +00002684 kfree(new_dev_maps);
2685 return -ENOMEM;
2686}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002687EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002688
2689int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2690 u16 index)
2691{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002692 int ret;
2693
2694 cpus_read_lock();
Antoine Tenart044ab862021-03-18 19:37:46 +01002695 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002696 cpus_read_unlock();
2697
2698 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002699}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002700EXPORT_SYMBOL(netif_set_xps_queue);
2701
2702#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002703static void netdev_unbind_all_sb_channels(struct net_device *dev)
2704{
2705 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2706
2707 /* Unbind any subordinate channels */
2708 while (txq-- != &dev->_tx[0]) {
2709 if (txq->sb_dev)
2710 netdev_unbind_sb_channel(dev, txq->sb_dev);
2711 }
2712}
2713
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002714void netdev_reset_tc(struct net_device *dev)
2715{
Alexander Duyck6234f872016-10-28 11:46:49 -04002716#ifdef CONFIG_XPS
2717 netif_reset_xps_queues_gt(dev, 0);
2718#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002719 netdev_unbind_all_sb_channels(dev);
2720
2721 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002722 dev->num_tc = 0;
2723 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2724 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2725}
2726EXPORT_SYMBOL(netdev_reset_tc);
2727
2728int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2729{
2730 if (tc >= dev->num_tc)
2731 return -EINVAL;
2732
Alexander Duyck6234f872016-10-28 11:46:49 -04002733#ifdef CONFIG_XPS
2734 netif_reset_xps_queues(dev, offset, count);
2735#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002736 dev->tc_to_txq[tc].count = count;
2737 dev->tc_to_txq[tc].offset = offset;
2738 return 0;
2739}
2740EXPORT_SYMBOL(netdev_set_tc_queue);
2741
2742int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2743{
2744 if (num_tc > TC_MAX_QUEUE)
2745 return -EINVAL;
2746
Alexander Duyck6234f872016-10-28 11:46:49 -04002747#ifdef CONFIG_XPS
2748 netif_reset_xps_queues_gt(dev, 0);
2749#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002750 netdev_unbind_all_sb_channels(dev);
2751
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002752 dev->num_tc = num_tc;
2753 return 0;
2754}
2755EXPORT_SYMBOL(netdev_set_num_tc);
2756
Alexander Duyckffcfe252018-07-09 12:19:38 -04002757void netdev_unbind_sb_channel(struct net_device *dev,
2758 struct net_device *sb_dev)
2759{
2760 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2761
2762#ifdef CONFIG_XPS
2763 netif_reset_xps_queues_gt(sb_dev, 0);
2764#endif
2765 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2766 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2767
2768 while (txq-- != &dev->_tx[0]) {
2769 if (txq->sb_dev == sb_dev)
2770 txq->sb_dev = NULL;
2771 }
2772}
2773EXPORT_SYMBOL(netdev_unbind_sb_channel);
2774
2775int netdev_bind_sb_channel_queue(struct net_device *dev,
2776 struct net_device *sb_dev,
2777 u8 tc, u16 count, u16 offset)
2778{
2779 /* Make certain the sb_dev and dev are already configured */
2780 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2781 return -EINVAL;
2782
2783 /* We cannot hand out queues we don't have */
2784 if ((offset + count) > dev->real_num_tx_queues)
2785 return -EINVAL;
2786
2787 /* Record the mapping */
2788 sb_dev->tc_to_txq[tc].count = count;
2789 sb_dev->tc_to_txq[tc].offset = offset;
2790
2791 /* Provide a way for Tx queue to find the tc_to_txq map or
2792 * XPS map for itself.
2793 */
2794 while (count--)
2795 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2796
2797 return 0;
2798}
2799EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2800
2801int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2802{
2803 /* Do not use a multiqueue device to represent a subordinate channel */
2804 if (netif_is_multiqueue(dev))
2805 return -ENODEV;
2806
2807 /* We allow channels 1 - 32767 to be used for subordinate channels.
2808 * Channel 0 is meant to be "native" mode and used only to represent
2809 * the main root device. We allow writing 0 to reset the device back
2810 * to normal mode after being used as a subordinate channel.
2811 */
2812 if (channel > S16_MAX)
2813 return -EINVAL;
2814
2815 dev->num_tc = -channel;
2816
2817 return 0;
2818}
2819EXPORT_SYMBOL(netdev_set_sb_channel);
2820
John Fastabendf0796d52010-07-01 13:21:57 +00002821/*
2822 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002823 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002824 */
Tom Herberte6484932010-10-18 18:04:39 +00002825int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002826{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002827 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002828 int rc;
2829
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002830 disabling = txq < dev->real_num_tx_queues;
2831
Tom Herberte6484932010-10-18 18:04:39 +00002832 if (txq < 1 || txq > dev->num_tx_queues)
2833 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002834
Ben Hutchings5c565802011-02-15 19:39:21 +00002835 if (dev->reg_state == NETREG_REGISTERED ||
2836 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002837 ASSERT_RTNL();
2838
Tom Herbert1d24eb42010-11-21 13:17:27 +00002839 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2840 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002841 if (rc)
2842 return rc;
2843
John Fastabend4f57c082011-01-17 08:06:04 +00002844 if (dev->num_tc)
2845 netif_setup_tc(dev, txq);
2846
Jakub Kicinski1e080f12021-09-13 15:53:30 -07002847 dev_qdisc_change_real_num_tx(dev, txq);
2848
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002849 dev->real_num_tx_queues = txq;
2850
2851 if (disabling) {
2852 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002853 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002854#ifdef CONFIG_XPS
2855 netif_reset_xps_queues_gt(dev, txq);
2856#endif
2857 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002858 } else {
2859 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002860 }
Tom Herberte6484932010-10-18 18:04:39 +00002861
Tom Herberte6484932010-10-18 18:04:39 +00002862 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002863}
2864EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002865
Michael Daltona953be52014-01-16 22:23:28 -08002866#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002867/**
2868 * netif_set_real_num_rx_queues - set actual number of RX queues used
2869 * @dev: Network device
2870 * @rxq: Actual number of RX queues
2871 *
2872 * This must be called either with the rtnl_lock held or before
2873 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002874 * negative error code. If called before registration, it always
2875 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002876 */
2877int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2878{
2879 int rc;
2880
Tom Herbertbd25fa72010-10-18 18:00:16 +00002881 if (rxq < 1 || rxq > dev->num_rx_queues)
2882 return -EINVAL;
2883
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002884 if (dev->reg_state == NETREG_REGISTERED) {
2885 ASSERT_RTNL();
2886
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002887 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2888 rxq);
2889 if (rc)
2890 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002891 }
2892
2893 dev->real_num_rx_queues = rxq;
2894 return 0;
2895}
2896EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2897#endif
2898
Ben Hutchings2c530402012-07-10 10:55:09 +00002899/**
Jakub Kicinski271e5b72021-08-03 06:05:26 -07002900 * netif_set_real_num_queues - set actual number of RX and TX queues used
2901 * @dev: Network device
2902 * @txq: Actual number of TX queues
2903 * @rxq: Actual number of RX queues
2904 *
2905 * Set the real number of both TX and RX queues.
2906 * Does nothing if the number of queues is already correct.
2907 */
2908int netif_set_real_num_queues(struct net_device *dev,
2909 unsigned int txq, unsigned int rxq)
2910{
2911 unsigned int old_rxq = dev->real_num_rx_queues;
2912 int err;
2913
2914 if (txq < 1 || txq > dev->num_tx_queues ||
2915 rxq < 1 || rxq > dev->num_rx_queues)
2916 return -EINVAL;
2917
2918 /* Start from increases, so the error path only does decreases -
2919 * decreases can't fail.
2920 */
2921 if (rxq > dev->real_num_rx_queues) {
2922 err = netif_set_real_num_rx_queues(dev, rxq);
2923 if (err)
2924 return err;
2925 }
2926 if (txq > dev->real_num_tx_queues) {
2927 err = netif_set_real_num_tx_queues(dev, txq);
2928 if (err)
2929 goto undo_rx;
2930 }
2931 if (rxq < dev->real_num_rx_queues)
2932 WARN_ON(netif_set_real_num_rx_queues(dev, rxq));
2933 if (txq < dev->real_num_tx_queues)
2934 WARN_ON(netif_set_real_num_tx_queues(dev, txq));
2935
2936 return 0;
2937undo_rx:
2938 WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq));
2939 return err;
2940}
2941EXPORT_SYMBOL(netif_set_real_num_queues);
2942
2943/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002944 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002945 *
2946 * This routine should set an upper limit on the number of RSS queues
2947 * used by default by multiqueue devices.
2948 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002949int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002950{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302951 return is_kdump_kernel() ?
2952 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002953}
2954EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2955
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002956static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002957{
2958 struct softnet_data *sd;
2959 unsigned long flags;
2960
2961 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002962 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002963 q->next_sched = NULL;
2964 *sd->output_queue_tailp = q;
2965 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002966 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2967 local_irq_restore(flags);
2968}
2969
David S. Miller37437bb2008-07-16 02:15:04 -07002970void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002971{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002972 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2973 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002974}
2975EXPORT_SYMBOL(__netif_schedule);
2976
Eric Dumazete6247022013-12-05 04:45:08 -08002977struct dev_kfree_skb_cb {
2978 enum skb_free_reason reason;
2979};
2980
2981static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002982{
Eric Dumazete6247022013-12-05 04:45:08 -08002983 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002984}
Denis Vlasenko56079432006-03-29 15:57:29 -08002985
John Fastabend46e5da40a2014-09-12 20:04:52 -07002986void netif_schedule_queue(struct netdev_queue *txq)
2987{
2988 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03002989 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07002990 struct Qdisc *q = rcu_dereference(txq->qdisc);
2991
2992 __netif_schedule(q);
2993 }
2994 rcu_read_unlock();
2995}
2996EXPORT_SYMBOL(netif_schedule_queue);
2997
John Fastabend46e5da40a2014-09-12 20:04:52 -07002998void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2999{
3000 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3001 struct Qdisc *q;
3002
3003 rcu_read_lock();
3004 q = rcu_dereference(dev_queue->qdisc);
3005 __netif_schedule(q);
3006 rcu_read_unlock();
3007 }
3008}
3009EXPORT_SYMBOL(netif_tx_wake_queue);
3010
Eric Dumazete6247022013-12-05 04:45:08 -08003011void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3012{
3013 unsigned long flags;
3014
Myungho Jung98998862017-04-25 11:58:15 -07003015 if (unlikely(!skb))
3016 return;
3017
Reshetova, Elena63354792017-06-30 13:07:58 +03003018 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003019 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003020 refcount_set(&skb->users, 0);
3021 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003022 return;
3023 }
3024 get_kfree_skb_cb(skb)->reason = reason;
3025 local_irq_save(flags);
3026 skb->next = __this_cpu_read(softnet_data.completion_queue);
3027 __this_cpu_write(softnet_data.completion_queue, skb);
3028 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3029 local_irq_restore(flags);
3030}
3031EXPORT_SYMBOL(__dev_kfree_skb_irq);
3032
3033void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003034{
Changbin Duafa79d02021-08-13 22:57:49 +08003035 if (in_hardirq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003036 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003037 else
3038 dev_kfree_skb(skb);
3039}
Eric Dumazete6247022013-12-05 04:45:08 -08003040EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003041
3042
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003043/**
3044 * netif_device_detach - mark device as removed
3045 * @dev: network device
3046 *
3047 * Mark device as removed from system and therefore no longer available.
3048 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003049void netif_device_detach(struct net_device *dev)
3050{
3051 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3052 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003053 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003054 }
3055}
3056EXPORT_SYMBOL(netif_device_detach);
3057
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003058/**
3059 * netif_device_attach - mark device as attached
3060 * @dev: network device
3061 *
3062 * Mark device as attached from system and restart if needed.
3063 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003064void netif_device_attach(struct net_device *dev)
3065{
3066 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3067 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003068 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003069 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003070 }
3071}
3072EXPORT_SYMBOL(netif_device_attach);
3073
Jiri Pirko5605c762015-05-12 14:56:12 +02003074/*
3075 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3076 * to be used as a distribution range.
3077 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003078static u16 skb_tx_hash(const struct net_device *dev,
3079 const struct net_device *sb_dev,
3080 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003081{
3082 u32 hash;
3083 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003084 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003085
Alexander Duyckeadec8772018-07-09 12:19:48 -04003086 if (dev->num_tc) {
3087 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3088
3089 qoffset = sb_dev->tc_to_txq[tc].offset;
3090 qcount = sb_dev->tc_to_txq[tc].count;
Michael Chan0c57eee2021-10-25 05:05:28 -04003091 if (unlikely(!qcount)) {
3092 net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n",
3093 sb_dev->name, qoffset, tc);
3094 qoffset = 0;
3095 qcount = dev->real_num_tx_queues;
3096 }
Alexander Duyckeadec8772018-07-09 12:19:48 -04003097 }
3098
Jiri Pirko5605c762015-05-12 14:56:12 +02003099 if (skb_rx_queue_recorded(skb)) {
3100 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003101 if (hash >= qoffset)
3102 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003103 while (unlikely(hash >= qcount))
3104 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003105 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003106 }
3107
3108 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3109}
Jiri Pirko5605c762015-05-12 14:56:12 +02003110
Ben Hutchings36c92472012-01-17 07:57:56 +00003111static void skb_warn_bad_offload(const struct sk_buff *skb)
3112{
Wei Tang84d15ae2016-06-16 21:17:49 +08003113 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003114 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003115 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003116
Ben Greearc846ad92013-04-19 10:45:52 +00003117 if (!net_ratelimit())
3118 return;
3119
Bjørn Mork88ad4172015-11-16 19:16:40 +01003120 if (dev) {
3121 if (dev->dev.parent)
3122 name = dev_driver_string(dev->dev.parent);
3123 else
3124 name = netdev_name(dev);
3125 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003126 skb_dump(KERN_WARNING, skb, false);
3127 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003128 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003129 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003130}
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132/*
3133 * Invalidate hardware checksum when packet is to be mangled, and
3134 * complete checksum manually on outgoing path.
3135 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003136int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Al Virod3bc23e2006-11-14 21:24:49 -08003138 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003139 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Patrick McHardy84fa7932006-08-29 16:44:56 -07003141 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003142 goto out_set_summed;
3143
Yi Li3aefd7d2020-10-27 13:59:04 +08003144 if (unlikely(skb_is_gso(skb))) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003145 skb_warn_bad_offload(skb);
3146 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148
Eric Dumazetcef401d2013-01-25 20:34:37 +00003149 /* Before computing a checksum, we should make sure no frag could
3150 * be modified by an external entity : checksum could be wrong.
3151 */
3152 if (skb_has_shared_frag(skb)) {
3153 ret = __skb_linearize(skb);
3154 if (ret)
3155 goto out;
3156 }
3157
Michał Mirosław55508d62010-12-14 15:24:08 +00003158 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003159 BUG_ON(offset >= skb_headlen(skb));
3160 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3161
3162 offset += skb->csum_offset;
3163 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3164
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003165 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3166 if (ret)
3167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003169 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003170out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return ret;
3174}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003175EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Davide Carattib72b5bf2017-05-18 15:44:38 +02003177int skb_crc32c_csum_help(struct sk_buff *skb)
3178{
3179 __le32 crc32c_csum;
3180 int ret = 0, offset, start;
3181
3182 if (skb->ip_summed != CHECKSUM_PARTIAL)
3183 goto out;
3184
3185 if (unlikely(skb_is_gso(skb)))
3186 goto out;
3187
3188 /* Before computing a checksum, we should make sure no frag could
3189 * be modified by an external entity : checksum could be wrong.
3190 */
3191 if (unlikely(skb_has_shared_frag(skb))) {
3192 ret = __skb_linearize(skb);
3193 if (ret)
3194 goto out;
3195 }
3196 start = skb_checksum_start_offset(skb);
3197 offset = start + offsetof(struct sctphdr, checksum);
3198 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3199 ret = -EINVAL;
3200 goto out;
3201 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003202
3203 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3204 if (ret)
3205 goto out;
3206
Davide Carattib72b5bf2017-05-18 15:44:38 +02003207 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3208 skb->len - start, ~(__u32)0,
3209 crc32c_csum_stub));
3210 *(__le32 *)(skb->data + offset) = crc32c_csum;
3211 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003212 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003213out:
3214 return ret;
3215}
3216
Vlad Yasevich53d64712014-03-27 17:26:18 -04003217__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003218{
3219 __be16 type = skb->protocol;
3220
Pravin B Shelar19acc322013-05-07 20:41:07 +00003221 /* Tunnel gso handlers can set protocol to ethernet. */
3222 if (type == htons(ETH_P_TEB)) {
3223 struct ethhdr *eth;
3224
3225 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3226 return 0;
3227
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003228 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003229 type = eth->h_proto;
3230 }
3231
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003232 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003233}
3234
Cong Wang12b00042013-02-05 16:36:38 +00003235/* openvswitch calls this on rx path, so we need a different check.
3236 */
3237static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3238{
3239 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003240 return skb->ip_summed != CHECKSUM_PARTIAL &&
3241 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003242
3243 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003244}
3245
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003246/**
Cong Wang12b00042013-02-05 16:36:38 +00003247 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003248 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003249 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003250 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003251 *
3252 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003253 *
3254 * It may return NULL if the skb requires no segmentation. This is
3255 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003256 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003257 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003258 */
Cong Wang12b00042013-02-05 16:36:38 +00003259struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3260 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003261{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003262 struct sk_buff *segs;
3263
Cong Wang12b00042013-02-05 16:36:38 +00003264 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003265 int err;
3266
Eric Dumazetb2504a52017-01-31 10:20:32 -08003267 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003268 err = skb_cow_head(skb, 0);
3269 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003270 return ERR_PTR(err);
3271 }
3272
Alexander Duyck802ab552016-04-10 21:45:03 -04003273 /* Only report GSO partial support if it will enable us to
3274 * support segmentation on this frame without needing additional
3275 * work.
3276 */
3277 if (features & NETIF_F_GSO_PARTIAL) {
3278 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3279 struct net_device *dev = skb->dev;
3280
3281 partial_features |= dev->features & dev->gso_partial_features;
3282 if (!skb_gso_ok(skb, features | partial_features))
3283 features &= ~NETIF_F_GSO_PARTIAL;
3284 }
3285
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003286 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003287 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3288
Pravin B Shelar68c33162013-02-14 14:02:41 +00003289 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003290 SKB_GSO_CB(skb)->encap_level = 0;
3291
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003292 skb_reset_mac_header(skb);
3293 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003294
Eric Dumazetb2504a52017-01-31 10:20:32 -08003295 segs = skb_mac_gso_segment(skb, features);
3296
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003297 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003298 skb_warn_bad_offload(skb);
3299
3300 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003301}
Cong Wang12b00042013-02-05 16:36:38 +00003302EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003303
Herbert Xufb286bb2005-11-10 13:01:24 -08003304/* Take action when hardware reception checksum errors are detected. */
3305#ifdef CONFIG_BUG
Tanner Love127d7352021-06-28 09:50:07 -04003306static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3307{
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07003308 netdev_err(dev, "hw csum failure\n");
Tanner Love127d7352021-06-28 09:50:07 -04003309 skb_dump(KERN_ERR, skb, true);
3310 dump_stack();
3311}
3312
Cong Wang7fe50ac2018-11-12 14:47:18 -08003313void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003314{
Tanner Love127d7352021-06-28 09:50:07 -04003315 DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08003316}
3317EXPORT_SYMBOL(netdev_rx_csum_fault);
3318#endif
3319
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003320/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003321static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003323#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 int i;
tchardingf4563a72017-02-09 17:56:07 +11003325
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003326 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003327 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3328 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003329
Ian Campbellea2ab692011-08-22 23:44:58 +00003330 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003331 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003332 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003333 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Simon Horman3b392dd2014-06-04 08:53:17 +09003338/* If MPLS offload request, verify we are testing hardware MPLS features
3339 * instead of standard features for the netdev.
3340 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003341#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003342static netdev_features_t net_mpls_features(struct sk_buff *skb,
3343 netdev_features_t features,
3344 __be16 type)
3345{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003346 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003347 features &= skb->dev->mpls_features;
3348
3349 return features;
3350}
3351#else
3352static netdev_features_t net_mpls_features(struct sk_buff *skb,
3353 netdev_features_t features,
3354 __be16 type)
3355{
3356 return features;
3357}
3358#endif
3359
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003360static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003361 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003362{
Simon Horman3b392dd2014-06-04 08:53:17 +09003363 __be16 type;
3364
Miaohe Lin9fc95f52020-07-30 19:02:36 +08003365 type = skb_network_protocol(skb, NULL);
Simon Horman3b392dd2014-06-04 08:53:17 +09003366 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003367
Ed Cashinc0d680e2012-09-19 15:49:00 +00003368 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003369 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003370 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003371 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003372 if (illegal_highdma(skb->dev, skb))
3373 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003374
3375 return features;
3376}
3377
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003378netdev_features_t passthru_features_check(struct sk_buff *skb,
3379 struct net_device *dev,
3380 netdev_features_t features)
3381{
3382 return features;
3383}
3384EXPORT_SYMBOL(passthru_features_check);
3385
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003386static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003387 struct net_device *dev,
3388 netdev_features_t features)
3389{
3390 return vlan_features_check(skb, features);
3391}
3392
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003393static netdev_features_t gso_features_check(const struct sk_buff *skb,
3394 struct net_device *dev,
3395 netdev_features_t features)
3396{
3397 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3398
Eric Dumazet6d872df2021-11-19 07:43:32 -08003399 if (gso_segs > READ_ONCE(dev->gso_max_segs))
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003400 return features & ~NETIF_F_GSO_MASK;
3401
Heiner Kallweit1d155df2020-11-21 00:22:20 +01003402 if (!skb_shinfo(skb)->gso_type) {
3403 skb_warn_bad_offload(skb);
3404 return features & ~NETIF_F_GSO_MASK;
3405 }
3406
Alexander Duyck802ab552016-04-10 21:45:03 -04003407 /* Support for GSO partial features requires software
3408 * intervention before we can actually process the packets
3409 * so we need to strip support for any partial features now
3410 * and we can pull them back in after we have partially
3411 * segmented the frame.
3412 */
3413 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3414 features &= ~dev->gso_partial_features;
3415
3416 /* Make sure to clear the IPv4 ID mangling feature if the
3417 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003418 */
3419 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3420 struct iphdr *iph = skb->encapsulation ?
3421 inner_ip_hdr(skb) : ip_hdr(skb);
3422
3423 if (!(iph->frag_off & htons(IP_DF)))
3424 features &= ~NETIF_F_TSO_MANGLEID;
3425 }
3426
3427 return features;
3428}
3429
Florian Westphalc1e756b2014-05-05 15:00:44 +02003430netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003431{
Jesse Gross5f352272014-12-23 22:37:26 -08003432 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003433 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003434
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003435 if (skb_is_gso(skb))
3436 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003437
Jesse Gross5f352272014-12-23 22:37:26 -08003438 /* If encapsulation offload request, verify we are testing
3439 * hardware encapsulation features instead of standard
3440 * features for the netdev
3441 */
3442 if (skb->encapsulation)
3443 features &= dev->hw_enc_features;
3444
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003445 if (skb_vlan_tagged(skb))
3446 features = netdev_intersect_features(features,
3447 dev->vlan_features |
3448 NETIF_F_HW_VLAN_CTAG_TX |
3449 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003450
Jesse Gross5f352272014-12-23 22:37:26 -08003451 if (dev->netdev_ops->ndo_features_check)
3452 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3453 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003454 else
3455 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003456
Florian Westphalc1e756b2014-05-05 15:00:44 +02003457 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003458}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003459EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003460
David S. Miller2ea25512014-08-29 21:10:01 -07003461static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003462 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003463{
David S. Miller2ea25512014-08-29 21:10:01 -07003464 unsigned int len;
3465 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003466
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003467 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003468 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003469
David S. Miller2ea25512014-08-29 21:10:01 -07003470 len = skb->len;
Willy Tarreau37447412020-08-10 10:27:42 +02003471 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
David S. Miller2ea25512014-08-29 21:10:01 -07003472 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003473 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003474 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003475
Patrick McHardy572a9d72009-11-10 06:14:14 +00003476 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003477}
David S. Miller2ea25512014-08-29 21:10:01 -07003478
David S. Miller8dcda222014-09-01 15:06:40 -07003479struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3480 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003481{
3482 struct sk_buff *skb = first;
3483 int rc = NETDEV_TX_OK;
3484
3485 while (skb) {
3486 struct sk_buff *next = skb->next;
3487
David S. Millera8305bf2018-07-29 20:42:53 -07003488 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003489 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003490 if (unlikely(!dev_xmit_complete(rc))) {
3491 skb->next = next;
3492 goto out;
3493 }
3494
3495 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003496 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003497 rc = NETDEV_TX_BUSY;
3498 break;
3499 }
3500 }
3501
3502out:
3503 *ret = rc;
3504 return skb;
3505}
3506
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003507static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3508 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003509{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003510 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003511 !vlan_hw_offload_capable(features, skb->vlan_proto))
3512 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003513 return skb;
3514}
3515
Davide Caratti43c26a12017-05-18 15:44:41 +02003516int skb_csum_hwoffload_help(struct sk_buff *skb,
3517 const netdev_features_t features)
3518{
Xin Longfa821172021-01-16 14:13:37 +08003519 if (unlikely(skb_csum_is_sctp(skb)))
Davide Caratti43c26a12017-05-18 15:44:41 +02003520 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3521 skb_crc32c_csum_help(skb);
3522
Xin Long62fafcd2021-01-28 17:18:31 +08003523 if (features & NETIF_F_HW_CSUM)
3524 return 0;
3525
3526 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3527 switch (skb->csum_offset) {
3528 case offsetof(struct tcphdr, check):
3529 case offsetof(struct udphdr, check):
3530 return 0;
3531 }
3532 }
3533
3534 return skb_checksum_help(skb);
Davide Caratti43c26a12017-05-18 15:44:41 +02003535}
3536EXPORT_SYMBOL(skb_csum_hwoffload_help);
3537
Steffen Klassertf53c7232017-12-20 10:41:36 +01003538static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003539{
3540 netdev_features_t features;
3541
David S. Millereae3f882014-08-30 15:17:13 -07003542 features = netif_skb_features(skb);
3543 skb = validate_xmit_vlan(skb, features);
3544 if (unlikely(!skb))
3545 goto out_null;
3546
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003547 skb = sk_validate_xmit_skb(skb, dev);
3548 if (unlikely(!skb))
3549 goto out_null;
3550
Johannes Berg8b86a612015-04-17 15:45:04 +02003551 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003552 struct sk_buff *segs;
3553
3554 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003555 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003556 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003557 } else if (segs) {
3558 consume_skb(skb);
3559 skb = segs;
3560 }
David S. Millereae3f882014-08-30 15:17:13 -07003561 } else {
3562 if (skb_needs_linearize(skb, features) &&
3563 __skb_linearize(skb))
3564 goto out_kfree_skb;
3565
3566 /* If packet is not checksummed and device does not
3567 * support checksumming for this protocol, complete
3568 * checksumming here.
3569 */
3570 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3571 if (skb->encapsulation)
3572 skb_set_inner_transport_header(skb,
3573 skb_checksum_start_offset(skb));
3574 else
3575 skb_set_transport_header(skb,
3576 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003577 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003578 goto out_kfree_skb;
3579 }
3580 }
3581
Steffen Klassertf53c7232017-12-20 10:41:36 +01003582 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003583
David S. Millereae3f882014-08-30 15:17:13 -07003584 return skb;
3585
3586out_kfree_skb:
3587 kfree_skb(skb);
3588out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003589 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003590 return NULL;
3591}
3592
Steffen Klassertf53c7232017-12-20 10:41:36 +01003593struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003594{
3595 struct sk_buff *next, *head = NULL, *tail;
3596
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003597 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003598 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003599 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003600
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003601 /* in case skb wont be segmented, point to itself */
3602 skb->prev = skb;
3603
Steffen Klassertf53c7232017-12-20 10:41:36 +01003604 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003605 if (!skb)
3606 continue;
3607
3608 if (!head)
3609 head = skb;
3610 else
3611 tail->next = skb;
3612 /* If skb was segmented, skb->prev points to
3613 * the last segment. If not, it still contains skb.
3614 */
3615 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003616 }
3617 return head;
3618}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003619EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003620
Eric Dumazet1def9232013-01-10 12:36:42 +00003621static void qdisc_pkt_len_init(struct sk_buff *skb)
3622{
3623 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3624
3625 qdisc_skb_cb(skb)->pkt_len = skb->len;
3626
3627 /* To get more precise estimation of bytes sent on wire,
3628 * we add to pkt_len the headers size of all segments
3629 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003630 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003631 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003632 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003633
Eric Dumazet757b8b12013-01-15 21:14:21 -08003634 /* mac layer + network layer */
3635 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3636
3637 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003638 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3639 const struct tcphdr *th;
3640 struct tcphdr _tcphdr;
3641
3642 th = skb_header_pointer(skb, skb_transport_offset(skb),
3643 sizeof(_tcphdr), &_tcphdr);
3644 if (likely(th))
3645 hdr_len += __tcp_hdrlen(th);
3646 } else {
3647 struct udphdr _udphdr;
3648
3649 if (skb_header_pointer(skb, skb_transport_offset(skb),
3650 sizeof(_udphdr), &_udphdr))
3651 hdr_len += sizeof(struct udphdr);
3652 }
Jason Wang15e5a032013-03-25 20:19:59 +00003653
3654 if (shinfo->gso_type & SKB_GSO_DODGY)
3655 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3656 shinfo->gso_size);
3657
3658 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003659 }
3660}
3661
Qitao Xu70713dd2021-07-14 23:03:24 -07003662static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q,
3663 struct sk_buff **to_free,
3664 struct netdev_queue *txq)
3665{
3666 int rc;
3667
3668 rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK;
3669 if (rc == NET_XMIT_SUCCESS)
3670 trace_qdisc_enqueue(q, txq, skb);
3671 return rc;
3672}
3673
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003674static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3675 struct net_device *dev,
3676 struct netdev_queue *txq)
3677{
3678 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003679 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003680 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003681 int rc;
3682
Eric Dumazeta2da5702011-01-20 03:48:19 +00003683 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003684
3685 if (q->flags & TCQ_F_NOLOCK) {
Yunsheng Linc4fef012021-06-22 14:49:56 +08003686 if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) &&
3687 qdisc_run_begin(q)) {
3688 /* Retest nolock_qdisc_is_empty() within the protection
3689 * of q->seqlock to protect from racing with requeuing.
3690 */
3691 if (unlikely(!nolock_qdisc_is_empty(q))) {
Qitao Xu70713dd2021-07-14 23:03:24 -07003692 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Yunsheng Linc4fef012021-06-22 14:49:56 +08003693 __qdisc_run(q);
3694 qdisc_run_end(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003695
Yunsheng Linc4fef012021-06-22 14:49:56 +08003696 goto no_lock_out;
3697 }
3698
3699 qdisc_bstats_cpu_update(q, skb);
3700 if (sch_direct_xmit(skb, q, dev, txq, NULL, true) &&
3701 !nolock_qdisc_is_empty(q))
3702 __qdisc_run(q);
3703
3704 qdisc_run_end(q);
3705 return NET_XMIT_SUCCESS;
3706 }
3707
Qitao Xu70713dd2021-07-14 23:03:24 -07003708 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Yunsheng Linc4fef012021-06-22 14:49:56 +08003709 qdisc_run(q);
3710
3711no_lock_out:
John Fastabend6b3ba912017-12-07 09:54:25 -08003712 if (unlikely(to_free))
3713 kfree_skb_list(to_free);
3714 return rc;
3715 }
3716
Eric Dumazet79640a42010-06-02 05:09:29 -07003717 /*
3718 * Heuristic to force contended enqueues to serialize on a
3719 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003720 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003721 * often and dequeue packets faster.
Sebastian Andrzej Siewior64445dd2021-12-13 11:53:29 +01003722 * On PREEMPT_RT it is possible to preempt the qdisc owner during xmit
3723 * and then other tasks will only enqueue packets. The packets will be
3724 * sent after the qdisc owner is scheduled again. To prevent this
3725 * scenario the task always serialize on the lock.
Eric Dumazet79640a42010-06-02 05:09:29 -07003726 */
Sebastian Andrzej Siewiora9aa5e32021-12-13 17:29:31 +01003727 contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT);
Eric Dumazet79640a42010-06-02 05:09:29 -07003728 if (unlikely(contended))
3729 spin_lock(&q->busylock);
3730
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003731 spin_lock(root_lock);
3732 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003733 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003734 rc = NET_XMIT_DROP;
3735 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003736 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003737 /*
3738 * This is a work-conserving queue; there are no old skbs
3739 * waiting to be sent out; and the qdisc is not running -
3740 * xmit the skb directly.
3741 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003742
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003743 qdisc_bstats_update(q, skb);
3744
Eric Dumazet55a93b32014-10-03 15:31:07 -07003745 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003746 if (unlikely(contended)) {
3747 spin_unlock(&q->busylock);
3748 contended = false;
3749 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003750 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003751 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003752
John Fastabend6c148182017-12-07 09:54:06 -08003753 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003754 rc = NET_XMIT_SUCCESS;
3755 } else {
Qitao Xu70713dd2021-07-14 23:03:24 -07003756 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Eric Dumazet79640a42010-06-02 05:09:29 -07003757 if (qdisc_run_begin(q)) {
3758 if (unlikely(contended)) {
3759 spin_unlock(&q->busylock);
3760 contended = false;
3761 }
3762 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003763 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003764 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003765 }
3766 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003767 if (unlikely(to_free))
3768 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003769 if (unlikely(contended))
3770 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003771 return rc;
3772}
3773
Daniel Borkmann86f85152013-12-29 17:27:11 +01003774#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003775static void skb_update_prio(struct sk_buff *skb)
3776{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003777 const struct netprio_map *map;
3778 const struct sock *sk;
3779 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003780
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003781 if (skb->priority)
3782 return;
3783 map = rcu_dereference_bh(skb->dev->priomap);
3784 if (!map)
3785 return;
3786 sk = skb_to_full_sk(skb);
3787 if (!sk)
3788 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003789
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003790 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3791
3792 if (prioidx < map->priomap_len)
3793 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003794}
3795#else
3796#define skb_update_prio(skb)
3797#endif
3798
Dave Jonesd29f7492008-07-22 14:09:06 -07003799/**
Michel Machado95603e22012-06-12 10:16:35 +00003800 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003801 * @net: network namespace this loopback is happening in
3802 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003803 * @skb: buffer to transmit
3804 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003805int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003806{
3807 skb_reset_mac_header(skb);
3808 __skb_pull(skb, skb_network_offset(skb));
3809 skb->pkt_type = PACKET_LOOPBACK;
Cyril Strejc9122a702021-10-24 22:14:25 +02003810 if (skb->ip_summed == CHECKSUM_NONE)
3811 skb->ip_summed = CHECKSUM_UNNECESSARY;
Michel Machado95603e22012-06-12 10:16:35 +00003812 WARN_ON(!skb_dst(skb));
3813 skb_dst_force(skb);
3814 netif_rx_ni(skb);
3815 return 0;
3816}
3817EXPORT_SYMBOL(dev_loopback_xmit);
3818
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003819#ifdef CONFIG_NET_EGRESS
3820static struct sk_buff *
3821sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3822{
Lukas Wunner42df6e12021-10-08 22:06:03 +02003823#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01003824 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003825 struct tcf_result cl_res;
3826
Jiri Pirko46209402017-11-03 11:46:25 +01003827 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003828 return skb;
3829
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003830 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Paul Blakeyec624fe2021-12-14 19:24:33 +02003831 tc_skb_cb(skb)->mru = 0;
3832 tc_skb_cb(skb)->post_ct = false;
Jiri Pirko46209402017-11-03 11:46:25 +01003833 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003834
Davide Caratti3aa26052021-07-28 20:08:00 +02003835 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003836 case TC_ACT_OK:
3837 case TC_ACT_RECLASSIFY:
3838 skb->tc_index = TC_H_MIN(cl_res.classid);
3839 break;
3840 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003841 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003842 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003843 kfree_skb(skb);
3844 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003845 case TC_ACT_STOLEN:
3846 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003847 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003848 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003849 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003850 return NULL;
3851 case TC_ACT_REDIRECT:
3852 /* No need to push/pop skb's mac_header here on egress! */
3853 skb_do_redirect(skb);
3854 *ret = NET_XMIT_SUCCESS;
3855 return NULL;
3856 default:
3857 break;
3858 }
Lukas Wunner42df6e12021-10-08 22:06:03 +02003859#endif /* CONFIG_NET_CLS_ACT */
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003860
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003861 return skb;
3862}
3863#endif /* CONFIG_NET_EGRESS */
3864
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003865#ifdef CONFIG_XPS
3866static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3867 struct xps_dev_maps *dev_maps, unsigned int tci)
3868{
Antoine Tenart255c04a2021-03-18 19:37:43 +01003869 int tc = netdev_get_prio_tc_map(dev, skb->priority);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003870 struct xps_map *map;
3871 int queue_index = -1;
3872
Antoine Tenart5478fcd2021-03-18 19:37:44 +01003873 if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids)
Antoine Tenart255c04a2021-03-18 19:37:43 +01003874 return queue_index;
3875
3876 tci *= dev_maps->num_tc;
3877 tci += tc;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003878
3879 map = rcu_dereference(dev_maps->attr_map[tci]);
3880 if (map) {
3881 if (map->len == 1)
3882 queue_index = map->queues[0];
3883 else
3884 queue_index = map->queues[reciprocal_scale(
3885 skb_get_hash(skb), map->len)];
3886 if (unlikely(queue_index >= dev->real_num_tx_queues))
3887 queue_index = -1;
3888 }
3889 return queue_index;
3890}
3891#endif
3892
Alexander Duyckeadec8772018-07-09 12:19:48 -04003893static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3894 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003895{
3896#ifdef CONFIG_XPS
3897 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003898 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003899 int queue_index = -1;
3900
Amritha Nambiar04157462018-06-29 21:26:46 -07003901 if (!static_key_false(&xps_needed))
3902 return -1;
3903
Jiri Pirko638b2a62015-05-12 14:56:13 +02003904 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003905 if (!static_key_false(&xps_rxqs_needed))
3906 goto get_cpus_map;
3907
Antoine Tenart044ab862021-03-18 19:37:46 +01003908 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003909 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003910 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003911
Antoine Tenart5478fcd2021-03-18 19:37:44 +01003912 if (tci >= 0)
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003913 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3914 tci);
3915 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003916
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003917get_cpus_map:
3918 if (queue_index < 0) {
Antoine Tenart044ab862021-03-18 19:37:46 +01003919 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003920 if (dev_maps) {
3921 unsigned int tci = skb->sender_cpu - 1;
3922
3923 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3924 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003925 }
3926 }
3927 rcu_read_unlock();
3928
3929 return queue_index;
3930#else
3931 return -1;
3932#endif
3933}
3934
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003935u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003936 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003937{
3938 return 0;
3939}
3940EXPORT_SYMBOL(dev_pick_tx_zero);
3941
3942u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003943 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003944{
3945 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3946}
3947EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3948
Paolo Abenib71b5832019-03-20 11:02:05 +01003949u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3950 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003951{
3952 struct sock *sk = skb->sk;
3953 int queue_index = sk_tx_queue_get(sk);
3954
Alexander Duyckeadec8772018-07-09 12:19:48 -04003955 sb_dev = sb_dev ? : dev;
3956
Jiri Pirko638b2a62015-05-12 14:56:13 +02003957 if (queue_index < 0 || skb->ooo_okay ||
3958 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003959 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003960
Jiri Pirko638b2a62015-05-12 14:56:13 +02003961 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003962 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003963
3964 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003965 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003966 rcu_access_pointer(sk->sk_dst_cache))
3967 sk_tx_queue_set(sk, new_index);
3968
3969 queue_index = new_index;
3970 }
3971
3972 return queue_index;
3973}
Paolo Abenib71b5832019-03-20 11:02:05 +01003974EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003975
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003976struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3977 struct sk_buff *skb,
3978 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003979{
3980 int queue_index = 0;
3981
3982#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003983 u32 sender_cpu = skb->sender_cpu - 1;
3984
3985 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003986 skb->sender_cpu = raw_smp_processor_id() + 1;
3987#endif
3988
3989 if (dev->real_num_tx_queues != 1) {
3990 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003991
Jiri Pirko638b2a62015-05-12 14:56:13 +02003992 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01003993 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003994 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003995 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003996
Alexander Duyckd5845272017-11-22 10:57:41 -08003997 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003998 }
3999
4000 skb_set_queue_mapping(skb, queue_index);
4001 return netdev_get_tx_queue(dev, queue_index);
4002}
4003
Michel Machado95603e22012-06-12 10:16:35 +00004004/**
Jason Wang9d08dd32014-01-20 11:25:13 +08004005 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07004006 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04004007 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07004008 *
4009 * Queue a buffer for transmission to a network device. The caller must
4010 * have set the device and priority and built the buffer before calling
4011 * this function. The function can be called from an interrupt.
4012 *
4013 * A negative errno code is returned on a failure. A success does not
4014 * guarantee the frame will be transmitted as it may be dropped due
4015 * to congestion or traffic shaping.
4016 *
4017 * -----------------------------------------------------------------------------------
4018 * I notice this method can also return errors from the queue disciplines,
4019 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4020 * be positive.
4021 *
4022 * Regardless of the return value, the skb is consumed, so it is currently
4023 * difficult to retry a send to this method. (You can bump the ref count
4024 * before sending to hold a reference for retry if you are careful.)
4025 *
4026 * When calling this method, interrupts MUST be enabled. This is because
4027 * the BH enable code must have IRQs enabled so that it will not deadlock.
4028 * --BLG
4029 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04004030static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
4032 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07004033 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 struct Qdisc *q;
4035 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01004036 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00004038 skb_reset_mac_header(skb);
4039
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004040 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004041 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004042
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004043 /* Disable soft irqs for various locks below. Also
4044 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004046 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Neil Horman5bc14212011-11-22 05:10:51 +00004048 skb_update_prio(skb);
4049
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004050 qdisc_pkt_len_init(skb);
4051#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004052 skb->tc_at_ingress = 0;
Lukas Wunner42df6e12021-10-08 22:06:03 +02004053#endif
4054#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004055 if (static_branch_unlikely(&egress_needed_key)) {
Lukas Wunner42df6e12021-10-08 22:06:03 +02004056 if (nf_hook_egress_active()) {
4057 skb = nf_hook_egress(skb, &rc, dev);
4058 if (!skb)
4059 goto out;
4060 }
4061 nf_skip_egress(skb, true);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004062 skb = sch_handle_egress(skb, &rc, dev);
4063 if (!skb)
4064 goto out;
Lukas Wunner42df6e12021-10-08 22:06:03 +02004065 nf_skip_egress(skb, false);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004066 }
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004067#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004068 /* If device/qdisc don't need skb->dst, release it right now while
4069 * its hot in this cpu cache.
4070 */
4071 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4072 skb_dst_drop(skb);
4073 else
4074 skb_dst_force(skb);
4075
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004076 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004077 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004078
Koki Sanagicf66ba52010-08-23 18:45:02 +09004079 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004081 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 }
4084
4085 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004086 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
tchardingeb13da12017-02-09 17:56:06 +11004088 * Really, it is unlikely that netif_tx_lock protection is necessary
4089 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4090 * counters.)
4091 * However, it is possible, that they rely on protection
4092 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
tchardingeb13da12017-02-09 17:56:06 +11004094 * Check this and shot the lock. It is not prone from deadlocks.
4095 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 */
4097 if (dev->flags & IFF_UP) {
4098 int cpu = smp_processor_id(); /* ok because BHs are off */
4099
Eric Dumazet7a10d8c2021-11-30 09:01:55 -08004100 /* Other cpus might concurrently change txq->xmit_lock_owner
4101 * to -1 or to their cpu id, but not to our id.
4102 */
4103 if (READ_ONCE(txq->xmit_lock_owner) != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004104 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004105 goto recursion_alert;
4106
Steffen Klassertf53c7232017-12-20 10:41:36 +01004107 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004108 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004109 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004110
Willy Tarreau37447412020-08-10 10:27:42 +02004111 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
David S. Millerc773e842008-07-08 23:13:53 -07004112 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Tom Herbert734664982011-11-28 16:32:44 +00004114 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004115 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004116 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004117 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004118 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004119 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 goto out;
4121 }
4122 }
David S. Millerc773e842008-07-08 23:13:53 -07004123 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004124 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4125 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 } else {
4127 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004128 * unfortunately
4129 */
4130recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004131 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4132 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
4134 }
4135
4136 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004137 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Eric Dumazet015f0682014-03-27 08:45:56 -07004139 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004140 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return rc;
4142out:
Herbert Xud4828d82006-06-22 02:28:18 -07004143 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return rc;
4145}
Jason Wangf663dd92014-01-10 16:18:26 +08004146
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004147int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004148{
4149 return __dev_queue_xmit(skb, NULL);
4150}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004151EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Alexander Duyckeadec8772018-07-09 12:19:48 -04004153int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004154{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004155 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004156}
4157EXPORT_SYMBOL(dev_queue_xmit_accel);
4158
Björn Töpel36ccdf82020-11-23 18:56:00 +01004159int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004160{
4161 struct net_device *dev = skb->dev;
4162 struct sk_buff *orig_skb = skb;
4163 struct netdev_queue *txq;
4164 int ret = NETDEV_TX_BUSY;
4165 bool again = false;
4166
4167 if (unlikely(!netif_running(dev) ||
4168 !netif_carrier_ok(dev)))
4169 goto drop;
4170
4171 skb = validate_xmit_skb_list(skb, dev, &again);
4172 if (skb != orig_skb)
4173 goto drop;
4174
4175 skb_set_queue_mapping(skb, queue_id);
4176 txq = skb_get_tx_queue(dev, skb);
Willy Tarreau37447412020-08-10 10:27:42 +02004177 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004178
4179 local_bh_disable();
4180
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004181 dev_xmit_recursion_inc();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004182 HARD_TX_LOCK(dev, txq, smp_processor_id());
4183 if (!netif_xmit_frozen_or_drv_stopped(txq))
4184 ret = netdev_start_xmit(skb, dev, txq, false);
4185 HARD_TX_UNLOCK(dev, txq);
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004186 dev_xmit_recursion_dec();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004187
4188 local_bh_enable();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004189 return ret;
4190drop:
4191 atomic_long_inc(&dev->tx_dropped);
4192 kfree_skb_list(skb);
4193 return NET_XMIT_DROP;
4194}
Björn Töpel36ccdf82020-11-23 18:56:00 +01004195EXPORT_SYMBOL(__dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
tchardingeb13da12017-02-09 17:56:06 +11004197/*************************************************************************
4198 * Receiver routines
4199 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004201int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004202EXPORT_SYMBOL(netdev_max_backlog);
4203
Eric Dumazet3b098e22010-05-15 23:57:10 -07004204int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004205int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004206/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4207unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004208int weight_p __read_mostly = 64; /* old backlog weight */
4209int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4210int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4211int dev_rx_weight __read_mostly = 64;
4212int dev_tx_weight __read_mostly = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004214/* Called with irq disabled */
4215static inline void ____napi_schedule(struct softnet_data *sd,
4216 struct napi_struct *napi)
4217{
Wei Wang29863d42021-02-08 11:34:09 -08004218 struct task_struct *thread;
4219
4220 if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
4221 /* Paired with smp_mb__before_atomic() in
Wei Wang5fdd2f02021-02-08 11:34:10 -08004222 * napi_enable()/dev_set_threaded().
4223 * Use READ_ONCE() to guarantee a complete
4224 * read on napi->thread. Only call
Wei Wang29863d42021-02-08 11:34:09 -08004225 * wake_up_process() when it's not NULL.
4226 */
4227 thread = READ_ONCE(napi->thread);
4228 if (thread) {
Wei Wangcb038352021-03-16 15:36:47 -07004229 /* Avoid doing set_bit() if the thread is in
4230 * INTERRUPTIBLE state, cause napi_thread_wait()
4231 * makes sure to proceed with napi polling
4232 * if the thread is explicitly woken from here.
4233 */
Peter Zijlstra2f064a52021-06-11 10:28:17 +02004234 if (READ_ONCE(thread->__state) != TASK_INTERRUPTIBLE)
Wei Wangcb038352021-03-16 15:36:47 -07004235 set_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
Wei Wang29863d42021-02-08 11:34:09 -08004236 wake_up_process(thread);
4237 return;
4238 }
4239 }
4240
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004241 list_add_tail(&napi->poll_list, &sd->poll_list);
4242 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4243}
4244
Eric Dumazetdf334542010-03-24 19:13:54 +00004245#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004246
4247/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004248struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004249EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004250u32 rps_cpu_mask __read_mostly;
4251EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004252
Eric Dumazetdc053602019-03-22 08:56:38 -07004253struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004254EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004255struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004256EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004257
Ben Hutchingsc4454772011-01-19 11:03:53 +00004258static struct rps_dev_flow *
4259set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4260 struct rps_dev_flow *rflow, u16 next_cpu)
4261{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004262 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004263#ifdef CONFIG_RFS_ACCEL
4264 struct netdev_rx_queue *rxqueue;
4265 struct rps_dev_flow_table *flow_table;
4266 struct rps_dev_flow *old_rflow;
4267 u32 flow_id;
4268 u16 rxq_index;
4269 int rc;
4270
4271 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004272 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4273 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004274 goto out;
4275 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4276 if (rxq_index == skb_get_rx_queue(skb))
4277 goto out;
4278
4279 rxqueue = dev->_rx + rxq_index;
4280 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4281 if (!flow_table)
4282 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004283 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004284 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4285 rxq_index, flow_id);
4286 if (rc < 0)
4287 goto out;
4288 old_rflow = rflow;
4289 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004290 rflow->filter = rc;
4291 if (old_rflow->filter == rflow->filter)
4292 old_rflow->filter = RPS_NO_FILTER;
4293 out:
4294#endif
4295 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004296 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004297 }
4298
Ben Hutchings09994d12011-10-03 04:42:46 +00004299 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004300 return rflow;
4301}
4302
Tom Herbert0a9627f2010-03-16 08:03:29 +00004303/*
4304 * get_rps_cpu is called from netif_receive_skb and returns the target
4305 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004306 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004307 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004308static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4309 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004310{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004311 const struct rps_sock_flow_table *sock_flow_table;
4312 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004313 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004314 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004315 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004316 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004317 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004318
Tom Herbert0a9627f2010-03-16 08:03:29 +00004319 if (skb_rx_queue_recorded(skb)) {
4320 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004321
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004322 if (unlikely(index >= dev->real_num_rx_queues)) {
4323 WARN_ONCE(dev->real_num_rx_queues > 1,
4324 "%s received packet on queue %u, but number "
4325 "of RX queues is %u\n",
4326 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004327 goto done;
4328 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004329 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004330 }
4331
Eric Dumazet567e4b72015-02-06 12:59:01 -08004332 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4333
4334 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4335 map = rcu_dereference(rxqueue->rps_map);
4336 if (!flow_table && !map)
4337 goto done;
4338
Changli Gao2d47b452010-08-17 19:00:56 +00004339 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004340 hash = skb_get_hash(skb);
4341 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004342 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004343
Tom Herbertfec5e652010-04-16 16:01:27 -07004344 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4345 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004346 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004347 u32 next_cpu;
4348 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004349
Eric Dumazet567e4b72015-02-06 12:59:01 -08004350 /* First check into global flow table if there is a match */
4351 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4352 if ((ident ^ hash) & ~rps_cpu_mask)
4353 goto try_rps;
4354
4355 next_cpu = ident & rps_cpu_mask;
4356
4357 /* OK, now we know there is a match,
4358 * we can look at the local (per receive queue) flow table
4359 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004360 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004361 tcpu = rflow->cpu;
4362
Tom Herbertfec5e652010-04-16 16:01:27 -07004363 /*
4364 * If the desired CPU (where last recvmsg was done) is
4365 * different from current CPU (one in the rx-queue flow
4366 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004367 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004368 * - Current CPU is offline.
4369 * - The current CPU's queue tail has advanced beyond the
4370 * last packet that was enqueued using this table entry.
4371 * This guarantees that all previous packets for the flow
4372 * have been dequeued, thus preserving in order delivery.
4373 */
4374 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004375 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004376 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004377 rflow->last_qtail)) >= 0)) {
4378 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004379 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004380 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004381
Eric Dumazeta31196b2015-04-25 09:35:24 -07004382 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004383 *rflowp = rflow;
4384 cpu = tcpu;
4385 goto done;
4386 }
4387 }
4388
Eric Dumazet567e4b72015-02-06 12:59:01 -08004389try_rps:
4390
Tom Herbert0a9627f2010-03-16 08:03:29 +00004391 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004392 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004393 if (cpu_online(tcpu)) {
4394 cpu = tcpu;
4395 goto done;
4396 }
4397 }
4398
4399done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004400 return cpu;
4401}
4402
Ben Hutchingsc4454772011-01-19 11:03:53 +00004403#ifdef CONFIG_RFS_ACCEL
4404
4405/**
4406 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4407 * @dev: Device on which the filter was set
4408 * @rxq_index: RX queue index
4409 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4410 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4411 *
4412 * Drivers that implement ndo_rx_flow_steer() should periodically call
4413 * this function for each installed filter and remove the filters for
4414 * which it returns %true.
4415 */
4416bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4417 u32 flow_id, u16 filter_id)
4418{
4419 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4420 struct rps_dev_flow_table *flow_table;
4421 struct rps_dev_flow *rflow;
4422 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004423 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004424
4425 rcu_read_lock();
4426 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4427 if (flow_table && flow_id <= flow_table->mask) {
4428 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004429 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004430 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004431 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4432 rflow->last_qtail) <
4433 (int)(10 * flow_table->mask)))
4434 expire = false;
4435 }
4436 rcu_read_unlock();
4437 return expire;
4438}
4439EXPORT_SYMBOL(rps_may_expire_flow);
4440
4441#endif /* CONFIG_RFS_ACCEL */
4442
Tom Herbert0a9627f2010-03-16 08:03:29 +00004443/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004444static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004445{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004446 struct softnet_data *sd = data;
4447
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004448 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004449 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004450}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004451
Tom Herbertfec5e652010-04-16 16:01:27 -07004452#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004453
4454/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004455 * Check if this softnet_data structure is another cpu one
4456 * If yes, queue it to our IPI list and return 1
4457 * If no, return 0
4458 */
4459static int rps_ipi_queued(struct softnet_data *sd)
4460{
4461#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004462 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004463
4464 if (sd != mysd) {
4465 sd->rps_ipi_next = mysd->rps_ipi_list;
4466 mysd->rps_ipi_list = sd;
4467
4468 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4469 return 1;
4470 }
4471#endif /* CONFIG_RPS */
4472 return 0;
4473}
4474
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004475#ifdef CONFIG_NET_FLOW_LIMIT
4476int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4477#endif
4478
4479static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4480{
4481#ifdef CONFIG_NET_FLOW_LIMIT
4482 struct sd_flow_limit *fl;
4483 struct softnet_data *sd;
4484 unsigned int old_flow, new_flow;
4485
4486 if (qlen < (netdev_max_backlog >> 1))
4487 return false;
4488
Christoph Lameter903ceff2014-08-17 12:30:35 -05004489 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004490
4491 rcu_read_lock();
4492 fl = rcu_dereference(sd->flow_limit);
4493 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004494 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004495 old_flow = fl->history[fl->history_head];
4496 fl->history[fl->history_head] = new_flow;
4497
4498 fl->history_head++;
4499 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4500
4501 if (likely(fl->buckets[old_flow]))
4502 fl->buckets[old_flow]--;
4503
4504 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4505 fl->count++;
4506 rcu_read_unlock();
4507 return true;
4508 }
4509 }
4510 rcu_read_unlock();
4511#endif
4512 return false;
4513}
4514
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004515/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004516 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4517 * queue (may be a remote CPU queue).
4518 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004519static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4520 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004521{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004522 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004523 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004524 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004525
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004526 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004527
4528 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004529
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004530 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004531 if (!netif_running(skb->dev))
4532 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004533 qlen = skb_queue_len(&sd->input_pkt_queue);
4534 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004535 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004536enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004537 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004538 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004539 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004540 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004541 return NET_RX_SUCCESS;
4542 }
4543
Eric Dumazetebda37c22010-05-06 23:51:21 +00004544 /* Schedule NAPI for backlog device
4545 * We can use non atomic operation since we own the queue lock
4546 */
4547 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004548 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004549 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004550 }
4551 goto enqueue;
4552 }
4553
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004554drop:
Changli Gaodee42872010-05-02 05:42:16 +00004555 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004556 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004557
Tom Herbert0a9627f2010-03-16 08:03:29 +00004558 local_irq_restore(flags);
4559
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004560 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004561 kfree_skb(skb);
4562 return NET_RX_DROP;
4563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004565static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4566{
4567 struct net_device *dev = skb->dev;
4568 struct netdev_rx_queue *rxqueue;
4569
4570 rxqueue = dev->_rx;
4571
4572 if (skb_rx_queue_recorded(skb)) {
4573 u16 index = skb_get_rx_queue(skb);
4574
4575 if (unlikely(index >= dev->real_num_rx_queues)) {
4576 WARN_ONCE(dev->real_num_rx_queues > 1,
4577 "%s received packet on queue %u, but number "
4578 "of RX queues is %u\n",
4579 dev->name, index, dev->real_num_rx_queues);
4580
4581 return rxqueue; /* Return first rxqueue */
4582 }
4583 rxqueue += index;
4584 }
4585 return rxqueue;
4586}
4587
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304588u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
4589 struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004590{
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004591 void *orig_data, *orig_data_end, *hard_start;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004592 struct netdev_rx_queue *rxqueue;
Martin Willi22b60342021-04-19 16:15:59 +02004593 bool orig_bcast, orig_host;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01004594 u32 mac_len, frame_sz;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004595 __be16 orig_eth_type;
4596 struct ethhdr *eth;
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304597 u32 metalen, act;
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004598 int off;
John Fastabendd4455162017-07-17 09:26:45 -07004599
John Fastabendd4455162017-07-17 09:26:45 -07004600 /* The XDP program wants to see the packet starting at the MAC
4601 * header.
4602 */
4603 mac_len = skb->data - skb_mac_header(skb);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004604 hard_start = skb->data - skb_headroom(skb);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004605
4606 /* SKB "head" area always have tailroom for skb_shared_info */
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004607 frame_sz = (void *)skb_end_pointer(skb) - hard_start;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01004608 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004609
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004610 rxqueue = netif_get_rxqueue(skb);
4611 xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
4612 xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
4613 skb_headlen(skb) + mac_len, true);
Björn Töpel02671e22018-05-02 13:01:30 +02004614
4615 orig_data_end = xdp->data_end;
4616 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004617 eth = (struct ethhdr *)xdp->data;
Martin Willi22b60342021-04-19 16:15:59 +02004618 orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr);
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004619 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4620 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004621
Björn Töpel02671e22018-05-02 13:01:30 +02004622 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004623
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004624 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004625 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004626 if (off) {
4627 if (off > 0)
4628 __skb_pull(skb, off);
4629 else if (off < 0)
4630 __skb_push(skb, -off);
4631
4632 skb->mac_header += off;
4633 skb_reset_network_header(skb);
4634 }
John Fastabendd4455162017-07-17 09:26:45 -07004635
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004636 /* check if bpf_xdp_adjust_tail was used */
4637 off = xdp->data_end - orig_data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004638 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004639 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004640 skb->len += off; /* positive on grow, negative on shrink */
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004641 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004642
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004643 /* check if XDP changed eth hdr such SKB needs update */
4644 eth = (struct ethhdr *)xdp->data;
4645 if ((orig_eth_type != eth->h_proto) ||
Martin Willi22b60342021-04-19 16:15:59 +02004646 (orig_host != ether_addr_equal_64bits(eth->h_dest,
4647 skb->dev->dev_addr)) ||
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004648 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4649 __skb_push(skb, ETH_HLEN);
Martin Willi22b60342021-04-19 16:15:59 +02004650 skb->pkt_type = PACKET_HOST;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004651 skb->protocol = eth_type_trans(skb, skb->dev);
4652 }
4653
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304654 /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull
4655 * before calling us again on redirect path. We do not call do_redirect
4656 * as we leave that up to the caller.
4657 *
4658 * Caller is responsible for managing lifetime of skb (i.e. calling
4659 * kfree_skb in response to actions it cannot handle/XDP_DROP).
4660 */
John Fastabendd4455162017-07-17 09:26:45 -07004661 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004662 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004663 case XDP_TX:
4664 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004665 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004666 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004667 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004668 if (metalen)
4669 skb_metadata_set(skb, metalen);
4670 break;
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304671 }
4672
4673 return act;
4674}
4675
4676static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4677 struct xdp_buff *xdp,
4678 struct bpf_prog *xdp_prog)
4679{
4680 u32 act = XDP_DROP;
4681
4682 /* Reinjected packets coming from act_mirred or similar should
4683 * not get XDP generic processing.
4684 */
4685 if (skb_is_redirected(skb))
4686 return XDP_PASS;
4687
4688 /* XDP packets must be linear and must have sufficient headroom
4689 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4690 * native XDP provides, thus we need to do it here as well.
4691 */
4692 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
4693 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4694 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4695 int troom = skb->tail + skb->data_len - skb->end;
4696
4697 /* In case we have to go down the path and also linearize,
4698 * then lets do the pskb_expand_head() work just once here.
4699 */
4700 if (pskb_expand_head(skb,
4701 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4702 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4703 goto do_drop;
4704 if (skb_linearize(skb))
4705 goto do_drop;
4706 }
4707
4708 act = bpf_prog_run_generic_xdp(skb, xdp, xdp_prog);
4709 switch (act) {
4710 case XDP_REDIRECT:
4711 case XDP_TX:
4712 case XDP_PASS:
4713 break;
John Fastabendd4455162017-07-17 09:26:45 -07004714 default:
Paolo Abenic8064e52021-11-30 11:08:07 +01004715 bpf_warn_invalid_xdp_action(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004716 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004717 case XDP_ABORTED:
4718 trace_xdp_exception(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004719 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004720 case XDP_DROP:
4721 do_drop:
4722 kfree_skb(skb);
4723 break;
4724 }
4725
4726 return act;
4727}
4728
4729/* When doing generic XDP we have to bypass the qdisc layer and the
4730 * network taps in order to match in-driver-XDP behavior.
4731 */
Jason Wang7c497472017-08-11 19:41:17 +08004732void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004733{
4734 struct net_device *dev = skb->dev;
4735 struct netdev_queue *txq;
4736 bool free_skb = true;
4737 int cpu, rc;
4738
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004739 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004740 cpu = smp_processor_id();
4741 HARD_TX_LOCK(dev, txq, cpu);
4742 if (!netif_xmit_stopped(txq)) {
4743 rc = netdev_start_xmit(skb, dev, txq, 0);
4744 if (dev_xmit_complete(rc))
4745 free_skb = false;
4746 }
4747 HARD_TX_UNLOCK(dev, txq);
4748 if (free_skb) {
4749 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4750 kfree_skb(skb);
4751 }
4752}
4753
Davidlohr Bueso02786472018-05-08 09:07:02 -07004754static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004755
Jason Wang7c497472017-08-11 19:41:17 +08004756int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004757{
John Fastabendd4455162017-07-17 09:26:45 -07004758 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004759 struct xdp_buff xdp;
4760 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004761 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004762
Björn Töpel02671e22018-05-02 13:01:30 +02004763 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004764 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004765 switch (act) {
4766 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004767 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004768 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004769 if (err)
4770 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004771 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004772 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004773 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004774 break;
4775 }
John Fastabendd4455162017-07-17 09:26:45 -07004776 return XDP_DROP;
4777 }
4778 }
4779 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004780out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004781 kfree_skb(skb);
4782 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004783}
Jason Wang7c497472017-08-11 19:41:17 +08004784EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004785
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004786static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004788 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Eric Dumazet588f0332011-11-15 04:12:55 +00004790 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Koki Sanagicf66ba52010-08-23 18:45:02 +09004792 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004793
Eric Dumazetdf334542010-03-24 19:13:54 +00004794#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004795 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004796 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004797 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Changli Gaocece1942010-08-07 20:35:43 -07004799 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004800 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004801
4802 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004803 if (cpu < 0)
4804 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004805
4806 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4807
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004808 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004809 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004810 } else
4811#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004812 {
4813 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004814
Tom Herbertfec5e652010-04-16 16:01:27 -07004815 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4816 put_cpu();
4817 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004820
4821/**
4822 * netif_rx - post buffer to the network code
4823 * @skb: buffer to post
4824 *
4825 * This function receives a packet from a device driver and queues it for
4826 * the upper (protocol) levels to process. It always succeeds. The buffer
4827 * may be dropped during processing for congestion control or by the
4828 * protocol layers.
4829 *
4830 * return values:
4831 * NET_RX_SUCCESS (no congestion)
4832 * NET_RX_DROP (packet was dropped)
4833 *
4834 */
4835
4836int netif_rx(struct sk_buff *skb)
4837{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004838 int ret;
4839
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004840 trace_netif_rx_entry(skb);
4841
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004842 ret = netif_rx_internal(skb);
4843 trace_netif_rx_exit(ret);
4844
4845 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004846}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004847EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
4849int netif_rx_ni(struct sk_buff *skb)
4850{
4851 int err;
4852
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004853 trace_netif_rx_ni_entry(skb);
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004856 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (local_softirq_pending())
4858 do_softirq();
4859 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004860 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 return err;
4863}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864EXPORT_SYMBOL(netif_rx_ni);
4865
Sebastian Andrzej Siewiorc11171a2020-09-29 22:25:12 +02004866int netif_rx_any_context(struct sk_buff *skb)
4867{
4868 /*
4869 * If invoked from contexts which do not invoke bottom half
4870 * processing either at return from interrupt or when softrqs are
4871 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4872 * directly.
4873 */
4874 if (in_interrupt())
4875 return netif_rx(skb);
4876 else
4877 return netif_rx_ni(skb);
4878}
4879EXPORT_SYMBOL(netif_rx_any_context);
4880
Emese Revfy0766f782016-06-20 20:42:34 +02004881static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004883 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 if (sd->completion_queue) {
4886 struct sk_buff *clist;
4887
4888 local_irq_disable();
4889 clist = sd->completion_queue;
4890 sd->completion_queue = NULL;
4891 local_irq_enable();
4892
4893 while (clist) {
4894 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 clist = clist->next;
4897
Reshetova, Elena63354792017-06-30 13:07:58 +03004898 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004899 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4900 trace_consume_skb(skb);
4901 else
Menglong Dongc504e5c2022-01-09 14:36:26 +08004902 trace_kfree_skb(skb, net_tx_action,
4903 SKB_DROP_REASON_NOT_SPECIFIED);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004904
4905 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4906 __kfree_skb(skb);
4907 else
4908 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 }
4910 }
4911
4912 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004913 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 local_irq_disable();
4916 head = sd->output_queue;
4917 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004918 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 local_irq_enable();
4920
Yunsheng Lin102b55e2021-05-14 11:17:00 +08004921 rcu_read_lock();
4922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004924 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004925 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 head = head->next_sched;
4928
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004929 /* We need to make sure head->next_sched is read
4930 * before clearing __QDISC_STATE_SCHED
4931 */
4932 smp_mb__before_atomic();
Yunsheng Lin102b55e2021-05-14 11:17:00 +08004933
4934 if (!(q->flags & TCQ_F_NOLOCK)) {
4935 root_lock = qdisc_lock(q);
4936 spin_lock(root_lock);
4937 } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
4938 &q->state))) {
4939 /* There is a synchronize_net() between
4940 * STATE_DEACTIVATED flag being set and
4941 * qdisc_reset()/some_qdisc_is_busy() in
4942 * dev_deactivate(), so we can safely bail out
4943 * early here to avoid data race between
4944 * qdisc_deactivate() and some_qdisc_is_busy()
4945 * for lockless qdisc.
4946 */
4947 clear_bit(__QDISC_STATE_SCHED, &q->state);
4948 continue;
4949 }
4950
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004951 clear_bit(__QDISC_STATE_SCHED, &q->state);
4952 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004953 if (root_lock)
4954 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
Yunsheng Lin102b55e2021-05-14 11:17:00 +08004956
4957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004959
4960 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004963#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004964/* This hook is defined here for ATM LANE */
4965int (*br_fdb_test_addr_hook)(struct net_device *dev,
4966 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004967EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004970static inline struct sk_buff *
4971sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004972 struct net_device *orig_dev, bool *another)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004973{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004974#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004975 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004976 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004977
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004978 /* If there's at least one ingress present somewhere (so
4979 * we get here via enabled static key), remaining devices
4980 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004981 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004982 */
Jiri Pirko46209402017-11-03 11:46:25 +01004983 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004984 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004985
Herbert Xuf697c3e2007-10-14 00:38:47 -07004986 if (*pt_prev) {
4987 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4988 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004989 }
4990
Florian Westphal33654952015-05-14 00:36:28 +02004991 qdisc_skb_cb(skb)->pkt_len = skb->len;
Paul Blakeyec624fe2021-12-14 19:24:33 +02004992 tc_skb_cb(skb)->mru = 0;
4993 tc_skb_cb(skb)->post_ct = false;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004994 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004995 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004996
Davide Caratti3aa26052021-07-28 20:08:00 +02004997 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004998 case TC_ACT_OK:
4999 case TC_ACT_RECLASSIFY:
5000 skb->tc_index = TC_H_MIN(cl_res.classid);
5001 break;
5002 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01005003 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07005004 kfree_skb(skb);
5005 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005006 case TC_ACT_STOLEN:
5007 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02005008 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07005009 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005010 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005011 case TC_ACT_REDIRECT:
5012 /* skb_mac_header check was done by cls/act_bpf, so
5013 * we can safely push the L2 header back before
5014 * redirecting to another netdev
5015 */
5016 __skb_push(skb, skb->mac_len);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005017 if (skb_do_redirect(skb) == -EAGAIN) {
5018 __skb_pull(skb, skb->mac_len);
5019 *another = true;
5020 break;
5021 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005022 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01005023 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02005024 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005025 default:
5026 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07005027 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005028#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07005029 return skb;
5030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005032/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07005033 * netdev_is_rx_handler_busy - check if receive handler is registered
5034 * @dev: device to check
5035 *
5036 * Check if a receive handler is already registered for a given device.
5037 * Return true if there one.
5038 *
5039 * The caller must hold the rtnl_mutex.
5040 */
5041bool netdev_is_rx_handler_busy(struct net_device *dev)
5042{
5043 ASSERT_RTNL();
5044 return dev && rtnl_dereference(dev->rx_handler);
5045}
5046EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5047
5048/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005049 * netdev_rx_handler_register - register receive handler
5050 * @dev: device to register a handler for
5051 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00005052 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005053 *
Masanari Iidae2278672014-02-18 22:54:36 +09005054 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005055 * called from __netif_receive_skb. A negative errno code is returned
5056 * on a failure.
5057 *
5058 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005059 *
5060 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005061 */
5062int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00005063 rx_handler_func_t *rx_handler,
5064 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005065{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08005066 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005067 return -EBUSY;
5068
Paolo Abenif54262502018-03-09 10:39:24 +01005069 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5070 return -EINVAL;
5071
Eric Dumazet00cfec32013-03-29 03:01:22 +00005072 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00005073 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005074 rcu_assign_pointer(dev->rx_handler, rx_handler);
5075
5076 return 0;
5077}
5078EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5079
5080/**
5081 * netdev_rx_handler_unregister - unregister receive handler
5082 * @dev: device to unregister a handler from
5083 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00005084 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005085 *
5086 * The caller must hold the rtnl_mutex.
5087 */
5088void netdev_rx_handler_unregister(struct net_device *dev)
5089{
5090
5091 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005092 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00005093 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5094 * section has a guarantee to see a non NULL rx_handler_data
5095 * as well.
5096 */
5097 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005098 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005099}
5100EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5101
Mel Gormanb4b9e352012-07-31 16:44:26 -07005102/*
5103 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5104 * the special handling of PFMEMALLOC skbs.
5105 */
5106static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5107{
5108 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07005109 case htons(ETH_P_ARP):
5110 case htons(ETH_P_IP):
5111 case htons(ETH_P_IPV6):
5112 case htons(ETH_P_8021Q):
5113 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07005114 return true;
5115 default:
5116 return false;
5117 }
5118}
5119
Pablo Neirae687ad62015-05-13 18:19:38 +02005120static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5121 int *ret, struct net_device *orig_dev)
5122{
5123 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04005124 int ingress_retval;
5125
Pablo Neirae687ad62015-05-13 18:19:38 +02005126 if (*pt_prev) {
5127 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5128 *pt_prev = NULL;
5129 }
5130
Aaron Conole2c1e2702016-09-21 11:35:03 -04005131 rcu_read_lock();
5132 ingress_retval = nf_hook_ingress(skb);
5133 rcu_read_unlock();
5134 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02005135 }
5136 return 0;
5137}
Pablo Neirae687ad62015-05-13 18:19:38 +02005138
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005139static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
Edward Cree88eb1942018-07-02 16:13:56 +01005140 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
5142 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005143 rx_handler_func_t *rx_handler;
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005144 struct sk_buff *skb = *pskb;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07005145 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005146 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08005148 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Eric Dumazet588f0332011-11-15 04:12:55 +00005150 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005151
Koki Sanagicf66ba52010-08-23 18:45:02 +09005152 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005153
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005154 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005155
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005156 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005157 if (!skb_transport_header_was_set(skb))
5158 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005159 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 pt_prev = NULL;
5162
David S. Miller63d8ea72011-02-28 10:48:59 -08005163another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005164 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005165
5166 __this_cpu_inc(softnet_data.processed);
5167
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005168 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5169 int ret2;
5170
Sebastian Andrzej Siewior2b4cd142021-06-17 09:38:17 +02005171 migrate_disable();
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005172 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
Sebastian Andrzej Siewior2b4cd142021-06-17 09:38:17 +02005173 migrate_enable();
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005174
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005175 if (ret2 != XDP_PASS) {
5176 ret = NET_RX_DROP;
5177 goto out;
5178 }
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005179 }
5180
Menglong Dong324cefa2021-01-11 02:42:21 -08005181 if (eth_type_vlan(skb->protocol)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005182 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005183 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005184 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005185 }
5186
Willem de Bruijne7246e12017-01-07 17:06:35 -05005187 if (skb_skip_tc_classify(skb))
5188 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
David S. Miller9754e292013-02-14 15:57:38 -05005190 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005191 goto skip_taps;
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005194 if (pt_prev)
5195 ret = deliver_skb(skb, pt_prev, orig_dev);
5196 pt_prev = ptype;
5197 }
5198
5199 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5200 if (pt_prev)
5201 ret = deliver_skb(skb, pt_prev, orig_dev);
5202 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 }
5204
Mel Gormanb4b9e352012-07-31 16:44:26 -07005205skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005206#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005207 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005208 bool another = false;
5209
Lukas Wunner42df6e12021-10-08 22:06:03 +02005210 nf_skip_egress(skb, true);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005211 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5212 &another);
5213 if (another)
5214 goto another_round;
Daniel Borkmann45771392015-04-10 23:07:54 +02005215 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005216 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005217
Lukas Wunner42df6e12021-10-08 22:06:03 +02005218 nf_skip_egress(skb, false);
Pablo Neirae687ad62015-05-13 18:19:38 +02005219 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005220 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005221 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005222#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005223 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005224skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005225 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005226 goto drop;
5227
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005228 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005229 if (pt_prev) {
5230 ret = deliver_skb(skb, pt_prev, orig_dev);
5231 pt_prev = NULL;
5232 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005233 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005234 goto another_round;
5235 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005236 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005237 }
5238
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005239 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005240 if (rx_handler) {
5241 if (pt_prev) {
5242 ret = deliver_skb(skb, pt_prev, orig_dev);
5243 pt_prev = NULL;
5244 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005245 switch (rx_handler(&skb)) {
5246 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005247 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005248 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005249 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005250 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005251 case RX_HANDLER_EXACT:
5252 deliver_exact = true;
Gustavo A. R. Silvab1866bf2021-03-09 23:42:43 -06005253 break;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005254 case RX_HANDLER_PASS:
5255 break;
5256 default:
5257 BUG();
5258 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +03005261 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005262check_vlan_id:
5263 if (skb_vlan_tag_get_id(skb)) {
5264 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5265 * find vlan device.
5266 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005267 skb->pkt_type = PACKET_OTHERHOST;
Menglong Dong324cefa2021-01-11 02:42:21 -08005268 } else if (eth_type_vlan(skb->protocol)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005269 /* Outer header is 802.1P with vlan 0, inner header is
5270 * 802.1Q or 802.1AD and vlan_do_receive() above could
5271 * not find vlan dev for vlan id 0.
5272 */
5273 __vlan_hwaccel_clear_tag(skb);
5274 skb = skb_vlan_untag(skb);
5275 if (unlikely(!skb))
5276 goto out;
5277 if (vlan_do_receive(&skb))
5278 /* After stripping off 802.1P header with vlan 0
5279 * vlan dev is found for inner header.
5280 */
5281 goto another_round;
5282 else if (unlikely(!skb))
5283 goto out;
5284 else
5285 /* We have stripped outer 802.1P vlan 0 header.
5286 * But could not find vlan dev.
5287 * check again for vlan id to set OTHERHOST.
5288 */
5289 goto check_vlan_id;
5290 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005291 /* Note: we might in the future use prio bits
5292 * and set skb->priority like in vlan_do_receive()
5293 * For the time being, just ignore Priority Code Point
5294 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005295 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005296 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005299
5300 /* deliver only exact match when indicated */
5301 if (likely(!deliver_exact)) {
5302 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5303 &ptype_base[ntohs(type) &
5304 PTYPE_HASH_MASK]);
5305 }
5306
5307 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5308 &orig_dev->ptype_specific);
5309
5310 if (unlikely(skb->dev != orig_dev)) {
5311 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5312 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 }
5314
5315 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005316 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005317 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005318 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005320drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005321 if (!deliver_exact)
5322 atomic_long_inc(&skb->dev->rx_dropped);
5323 else
5324 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 kfree_skb(skb);
5326 /* Jamal, now you will not able to escape explaining
5327 * me how you were going to use this. :-)
5328 */
5329 ret = NET_RX_DROP;
5330 }
5331
Julian Anastasov2c17d272015-07-09 09:59:10 +03005332out:
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005333 /* The invariant here is that if *ppt_prev is not NULL
5334 * then skb should also be non-NULL.
5335 *
5336 * Apparently *ppt_prev assignment above holds this invariant due to
5337 * skb dereferencing near it.
5338 */
5339 *pskb = skb;
David S. Miller9754e292013-02-14 15:57:38 -05005340 return ret;
5341}
5342
Edward Cree88eb1942018-07-02 16:13:56 +01005343static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5344{
5345 struct net_device *orig_dev = skb->dev;
5346 struct packet_type *pt_prev = NULL;
5347 int ret;
5348
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005349 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree88eb1942018-07-02 16:13:56 +01005350 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005351 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5352 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005353 return ret;
5354}
5355
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005356/**
5357 * netif_receive_skb_core - special purpose version of netif_receive_skb
5358 * @skb: buffer to process
5359 *
5360 * More direct receive version of netif_receive_skb(). It should
5361 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005362 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005363 *
5364 * This function may only be called from softirq context and interrupts
5365 * should be enabled.
5366 *
5367 * Return values (usually ignored):
5368 * NET_RX_SUCCESS: no congestion
5369 * NET_RX_DROP: packet was dropped
5370 */
5371int netif_receive_skb_core(struct sk_buff *skb)
5372{
5373 int ret;
5374
5375 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005376 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005377 rcu_read_unlock();
5378
5379 return ret;
5380}
5381EXPORT_SYMBOL(netif_receive_skb_core);
5382
Edward Cree88eb1942018-07-02 16:13:56 +01005383static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5384 struct packet_type *pt_prev,
5385 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005386{
5387 struct sk_buff *skb, *next;
5388
Edward Cree88eb1942018-07-02 16:13:56 +01005389 if (!pt_prev)
5390 return;
5391 if (list_empty(head))
5392 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005393 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005394 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5395 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005396 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005397 list_for_each_entry_safe(skb, next, head, list) {
5398 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005399 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005400 }
Edward Cree88eb1942018-07-02 16:13:56 +01005401}
5402
5403static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5404{
5405 /* Fast-path assumptions:
5406 * - There is no RX handler.
5407 * - Only one packet_type matches.
5408 * If either of these fails, we will end up doing some per-packet
5409 * processing in-line, then handling the 'last ptype' for the whole
5410 * sublist. This can't cause out-of-order delivery to any single ptype,
5411 * because the 'last ptype' must be constant across the sublist, and all
5412 * other ptypes are handled per-packet.
5413 */
5414 /* Current (common) ptype of sublist */
5415 struct packet_type *pt_curr = NULL;
5416 /* Current (common) orig_dev of sublist */
5417 struct net_device *od_curr = NULL;
5418 struct list_head sublist;
5419 struct sk_buff *skb, *next;
5420
Edward Cree9af86f92018-07-09 18:10:19 +01005421 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005422 list_for_each_entry_safe(skb, next, head, list) {
5423 struct net_device *orig_dev = skb->dev;
5424 struct packet_type *pt_prev = NULL;
5425
Edward Cree22f6bbb2018-12-04 17:37:57 +00005426 skb_list_del_init(skb);
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005427 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005428 if (!pt_prev)
5429 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005430 if (pt_curr != pt_prev || od_curr != orig_dev) {
5431 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005432 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5433 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005434 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005435 pt_curr = pt_prev;
5436 od_curr = orig_dev;
5437 }
Edward Cree9af86f92018-07-09 18:10:19 +01005438 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005439 }
5440
5441 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005442 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005443}
5444
David S. Miller9754e292013-02-14 15:57:38 -05005445static int __netif_receive_skb(struct sk_buff *skb)
5446{
5447 int ret;
5448
5449 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005450 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005451
5452 /*
5453 * PFMEMALLOC skbs are special, they should
5454 * - be delivered to SOCK_MEMALLOC sockets only
5455 * - stay away from userspace
5456 * - have bounded memory usage
5457 *
5458 * Use PF_MEMALLOC as this saves us from propagating the allocation
5459 * context down to all allocation sites.
5460 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005461 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005462 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005463 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005464 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005465 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 return ret;
5468}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005469
Edward Cree4ce00172018-07-02 16:13:40 +01005470static void __netif_receive_skb_list(struct list_head *head)
5471{
5472 unsigned long noreclaim_flag = 0;
5473 struct sk_buff *skb, *next;
5474 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5475
5476 list_for_each_entry_safe(skb, next, head, list) {
5477 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5478 struct list_head sublist;
5479
5480 /* Handle the previous sublist */
5481 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005482 if (!list_empty(&sublist))
5483 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005484 pfmemalloc = !pfmemalloc;
5485 /* See comments in __netif_receive_skb */
5486 if (pfmemalloc)
5487 noreclaim_flag = memalloc_noreclaim_save();
5488 else
5489 memalloc_noreclaim_restore(noreclaim_flag);
5490 }
5491 }
5492 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005493 if (!list_empty(head))
5494 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005495 /* Restore pflags */
5496 if (pfmemalloc)
5497 memalloc_noreclaim_restore(noreclaim_flag);
5498}
5499
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005500static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005501{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005502 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005503 struct bpf_prog *new = xdp->prog;
5504 int ret = 0;
5505
5506 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005507 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005508 rcu_assign_pointer(dev->xdp_prog, new);
5509 if (old)
5510 bpf_prog_put(old);
5511
5512 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005513 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005514 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005515 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005516 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005517 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005518 }
5519 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005520
David S. Millerb5cdae32017-04-18 15:36:58 -04005521 default:
5522 ret = -EINVAL;
5523 break;
5524 }
5525
5526 return ret;
5527}
5528
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005529static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005530{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005531 int ret;
5532
Eric Dumazet588f0332011-11-15 04:12:55 +00005533 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005534
Richard Cochranc1f19b52010-07-17 08:49:36 +00005535 if (skb_defer_rx_timestamp(skb))
5536 return NET_RX_SUCCESS;
5537
John Fastabendbbbe2112017-09-08 14:00:30 -07005538 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005539#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005540 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005541 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005542 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005543
Eric Dumazet3b098e22010-05-15 23:57:10 -07005544 if (cpu >= 0) {
5545 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5546 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005547 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005548 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005549 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005550#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005551 ret = __netif_receive_skb(skb);
5552 rcu_read_unlock();
5553 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005554}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005555
Eric Dumazet587652b2021-11-15 09:05:54 -08005556void netif_receive_skb_list_internal(struct list_head *head)
Edward Cree7da517a2018-07-02 16:13:24 +01005557{
Edward Cree7da517a2018-07-02 16:13:24 +01005558 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005559 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005560
Edward Cree8c057ef2018-07-09 18:09:54 +01005561 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005562 list_for_each_entry_safe(skb, next, head, list) {
5563 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005564 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005565 if (!skb_defer_rx_timestamp(skb))
5566 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005567 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005568 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005569
Edward Cree7da517a2018-07-02 16:13:24 +01005570 rcu_read_lock();
5571#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005572 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005573 list_for_each_entry_safe(skb, next, head, list) {
5574 struct rps_dev_flow voidflow, *rflow = &voidflow;
5575 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5576
5577 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005578 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005579 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005580 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005581 }
5582 }
5583 }
5584#endif
5585 __netif_receive_skb_list(head);
5586 rcu_read_unlock();
5587}
5588
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005589/**
5590 * netif_receive_skb - process receive buffer from network
5591 * @skb: buffer to process
5592 *
5593 * netif_receive_skb() is the main receive data processing function.
5594 * It always succeeds. The buffer may be dropped during processing
5595 * for congestion control or by the protocol layers.
5596 *
5597 * This function may only be called from softirq context and interrupts
5598 * should be enabled.
5599 *
5600 * Return values (usually ignored):
5601 * NET_RX_SUCCESS: no congestion
5602 * NET_RX_DROP: packet was dropped
5603 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005604int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005605{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005606 int ret;
5607
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005608 trace_netif_receive_skb_entry(skb);
5609
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005610 ret = netif_receive_skb_internal(skb);
5611 trace_netif_receive_skb_exit(ret);
5612
5613 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005614}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005615EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Edward Creef6ad8c12018-07-02 16:12:45 +01005617/**
5618 * netif_receive_skb_list - process many receive buffers from network
5619 * @head: list of skbs to process.
5620 *
Edward Cree7da517a2018-07-02 16:13:24 +01005621 * Since return value of netif_receive_skb() is normally ignored, and
5622 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005623 *
5624 * This function may only be called from softirq context and interrupts
5625 * should be enabled.
5626 */
5627void netif_receive_skb_list(struct list_head *head)
5628{
Edward Cree7da517a2018-07-02 16:13:24 +01005629 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005630
Edward Creeb9f463d2018-07-02 16:14:44 +01005631 if (list_empty(head))
5632 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005633 if (trace_netif_receive_skb_list_entry_enabled()) {
5634 list_for_each_entry(skb, head, list)
5635 trace_netif_receive_skb_list_entry(skb);
5636 }
Edward Cree7da517a2018-07-02 16:13:24 +01005637 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005638 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005639}
5640EXPORT_SYMBOL(netif_receive_skb_list);
5641
Wei Yongjunce1e2a72020-07-13 22:23:44 +08005642static DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005643
5644/* Network device is going away, flush any packets still pending */
5645static void flush_backlog(struct work_struct *work)
5646{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005647 struct sk_buff *skb, *tmp;
5648 struct softnet_data *sd;
5649
5650 local_bh_disable();
5651 sd = this_cpu_ptr(&softnet_data);
5652
5653 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005654 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005655 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005656 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005657 __skb_unlink(skb, &sd->input_pkt_queue);
Subash Abhinov Kasiviswanathan7df5cb752020-07-23 11:31:48 -06005658 dev_kfree_skb_irq(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005659 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005660 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005661 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005662 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005663 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005664
5665 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005666 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005667 __skb_unlink(skb, &sd->process_queue);
5668 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005669 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005670 }
5671 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005672 local_bh_enable();
5673}
5674
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005675static bool flush_required(int cpu)
5676{
5677#if IS_ENABLED(CONFIG_RPS)
5678 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5679 bool do_flush;
5680
5681 local_irq_disable();
5682 rps_lock(sd);
5683
5684 /* as insertion into process_queue happens with the rps lock held,
5685 * process_queue access may race only with dequeue
5686 */
5687 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5688 !skb_queue_empty_lockless(&sd->process_queue);
5689 rps_unlock(sd);
5690 local_irq_enable();
5691
5692 return do_flush;
5693#endif
5694 /* without RPS we can't safely check input_pkt_queue: during a
5695 * concurrent remote skb_queue_splice() we can detect as empty both
5696 * input_pkt_queue and process_queue even if the latter could end-up
5697 * containing a lot of packets.
5698 */
5699 return true;
5700}
5701
Eric Dumazet41852492016-08-26 12:50:39 -07005702static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005703{
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005704 static cpumask_t flush_cpus;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005705 unsigned int cpu;
5706
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005707 /* since we are under rtnl lock protection we can use static data
5708 * for the cpumask and avoid allocating on stack the possibly
5709 * large mask
5710 */
5711 ASSERT_RTNL();
5712
Sebastian Andrzej Siewior372bbdd2021-08-03 16:16:06 +02005713 cpus_read_lock();
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005714
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005715 cpumask_clear(&flush_cpus);
5716 for_each_online_cpu(cpu) {
5717 if (flush_required(cpu)) {
5718 queue_work_on(cpu, system_highpri_wq,
5719 per_cpu_ptr(&flush_works, cpu));
5720 cpumask_set_cpu(cpu, &flush_cpus);
5721 }
5722 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005723
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005724 /* we can have in flight packet[s] on the cpus we are not flushing,
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -08005725 * synchronize_net() in unregister_netdevice_many() will take care of
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005726 * them
5727 */
5728 for_each_cpu(cpu, &flush_cpus)
Eric Dumazet41852492016-08-26 12:50:39 -07005729 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005730
Sebastian Andrzej Siewior372bbdd2021-08-03 16:16:06 +02005731 cpus_read_unlock();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005732}
5733
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305734static void net_rps_send_ipi(struct softnet_data *remsd)
5735{
5736#ifdef CONFIG_RPS
5737 while (remsd) {
5738 struct softnet_data *next = remsd->rps_ipi_next;
5739
5740 if (cpu_online(remsd->cpu))
5741 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5742 remsd = next;
5743 }
5744#endif
5745}
5746
Eric Dumazete326bed2010-04-22 00:22:45 -07005747/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08005748 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07005749 * Note: called with local irq disabled, but exits with local irq enabled.
5750 */
5751static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5752{
5753#ifdef CONFIG_RPS
5754 struct softnet_data *remsd = sd->rps_ipi_list;
5755
5756 if (remsd) {
5757 sd->rps_ipi_list = NULL;
5758
5759 local_irq_enable();
5760
5761 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305762 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07005763 } else
5764#endif
5765 local_irq_enable();
5766}
5767
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005768static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5769{
5770#ifdef CONFIG_RPS
5771 return sd->rps_ipi_list != NULL;
5772#else
5773 return false;
5774#endif
5775}
5776
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005777static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005779 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005780 bool again = true;
5781 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Eric Dumazete326bed2010-04-22 00:22:45 -07005783 /* Check if we have pending ipi, its better to send them now,
5784 * not waiting net_rx_action() end.
5785 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005786 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07005787 local_irq_disable();
5788 net_rps_action_and_irq_enable(sd);
5789 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005790
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01005791 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005792 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
Changli Gao6e7676c2010-04-27 15:07:33 -07005795 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03005796 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07005797 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03005798 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00005799 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005800 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00005801 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005802
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005805 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005806 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07005807 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005808 /*
5809 * Inline a custom version of __napi_complete().
5810 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07005811 * and NAPI_STATE_SCHED is the only possible flag set
5812 * on backlog.
5813 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005814 * and we dont need an smp_mb() memory barrier.
5815 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005816 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005817 again = false;
5818 } else {
5819 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5820 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07005821 }
5822 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005823 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005826 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827}
5828
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005829/**
5830 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07005831 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005832 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005833 * The entry's receive function will be scheduled to run.
5834 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005835 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08005836void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005837{
5838 unsigned long flags;
5839
5840 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05005841 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005842 local_irq_restore(flags);
5843}
5844EXPORT_SYMBOL(__napi_schedule);
5845
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005846/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08005847 * napi_schedule_prep - check if napi can be scheduled
5848 * @n: napi context
5849 *
5850 * Test if NAPI routine is already running, and if not mark
Jonathan Neuschäferee1a4c82020-09-05 20:32:18 +02005851 * it as running. This is used as a condition variable to
Eric Dumazet39e6c822017-02-28 10:34:50 -08005852 * insure only one NAPI poll instance runs. We also make
5853 * sure there is no pending NAPI disable.
5854 */
5855bool napi_schedule_prep(struct napi_struct *n)
5856{
5857 unsigned long val, new;
5858
5859 do {
5860 val = READ_ONCE(n->state);
5861 if (unlikely(val & NAPIF_STATE_DISABLE))
5862 return false;
5863 new = val | NAPIF_STATE_SCHED;
5864
5865 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5866 * This was suggested by Alexander Duyck, as compiler
5867 * emits better code than :
5868 * if (val & NAPIF_STATE_SCHED)
5869 * new |= NAPIF_STATE_MISSED;
5870 */
5871 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5872 NAPIF_STATE_MISSED;
5873 } while (cmpxchg(&n->state, val, new) != val);
5874
5875 return !(val & NAPIF_STATE_SCHED);
5876}
5877EXPORT_SYMBOL(napi_schedule_prep);
5878
5879/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005880 * __napi_schedule_irqoff - schedule for receive
5881 * @n: entry to schedule
5882 *
Sebastian Andrzej Siewior8380c812021-05-12 23:43:24 +02005883 * Variant of __napi_schedule() assuming hard irqs are masked.
5884 *
5885 * On PREEMPT_RT enabled kernels this maps to __napi_schedule()
5886 * because the interrupt disabled assumption might not be true
5887 * due to force-threaded interrupts and spinlock substitution.
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005888 */
5889void __napi_schedule_irqoff(struct napi_struct *n)
5890{
Sebastian Andrzej Siewior8380c812021-05-12 23:43:24 +02005891 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
5892 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5893 else
5894 __napi_schedule(n);
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005895}
5896EXPORT_SYMBOL(__napi_schedule_irqoff);
5897
Eric Dumazet364b6052016-11-15 10:15:13 -08005898bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08005899{
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07005900 unsigned long flags, val, new, timeout = 0;
5901 bool ret = true;
Herbert Xud565b0a2008-12-15 23:38:52 -08005902
5903 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08005904 * 1) Don't let napi dequeue from the cpu poll list
5905 * just in case its running on a different cpu.
5906 * 2) If we are busy polling, do nothing here, we have
5907 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08005908 */
Eric Dumazet217f6972016-11-15 10:15:11 -08005909 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5910 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08005911 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08005912
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07005913 if (work_done) {
5914 if (n->gro_bitmask)
Eric Dumazet7e417a62020-04-22 09:13:28 -07005915 timeout = READ_ONCE(n->dev->gro_flush_timeout);
5916 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07005917 }
5918 if (n->defer_hard_irqs_count > 0) {
5919 n->defer_hard_irqs_count--;
Eric Dumazet7e417a62020-04-22 09:13:28 -07005920 timeout = READ_ONCE(n->dev->gro_flush_timeout);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07005921 if (timeout)
5922 ret = false;
5923 }
5924 if (n->gro_bitmask) {
Paolo Abeni605108a2018-11-21 18:21:35 +01005925 /* When the NAPI instance uses a timeout and keeps postponing
5926 * it, we need to bound somehow the time packets are kept in
5927 * the GRO layer
5928 */
5929 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005930 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005931
5932 gro_normal_list(n);
5933
Eric Dumazet02c16022017-02-04 15:25:02 -08005934 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005935 /* If n->poll_list is not empty, we need to mask irqs */
5936 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08005937 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005938 local_irq_restore(flags);
5939 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08005940
5941 do {
5942 val = READ_ONCE(n->state);
5943
5944 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5945
Björn Töpel7fd32532020-11-30 19:51:56 +01005946 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
Wei Wangcb038352021-03-16 15:36:47 -07005947 NAPIF_STATE_SCHED_THREADED |
Björn Töpel7fd32532020-11-30 19:51:56 +01005948 NAPIF_STATE_PREFER_BUSY_POLL);
Eric Dumazet39e6c822017-02-28 10:34:50 -08005949
5950 /* If STATE_MISSED was set, leave STATE_SCHED set,
5951 * because we will call napi->poll() one more time.
5952 * This C code was suggested by Alexander Duyck to help gcc.
5953 */
5954 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5955 NAPIF_STATE_SCHED;
5956 } while (cmpxchg(&n->state, val, new) != val);
5957
5958 if (unlikely(val & NAPIF_STATE_MISSED)) {
5959 __napi_schedule(n);
5960 return false;
5961 }
5962
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07005963 if (timeout)
5964 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5965 HRTIMER_MODE_REL_PINNED);
5966 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005967}
Eric Dumazet3b47d302014-11-06 21:09:44 -08005968EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08005969
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005970/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08005971static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005972{
5973 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5974 struct napi_struct *napi;
5975
5976 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5977 if (napi->napi_id == napi_id)
5978 return napi;
5979
5980 return NULL;
5981}
Eric Dumazet02d62e82015-11-18 06:30:52 -08005982
5983#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08005984
Björn Töpel7fd32532020-11-30 19:51:56 +01005985static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
Eric Dumazet217f6972016-11-15 10:15:11 -08005986{
Björn Töpel7fd32532020-11-30 19:51:56 +01005987 if (!skip_schedule) {
5988 gro_normal_list(napi);
5989 __napi_schedule(napi);
5990 return;
5991 }
5992
5993 if (napi->gro_bitmask) {
5994 /* flush too old packets
5995 * If HZ < 1000, flush all packets.
5996 */
5997 napi_gro_flush(napi, HZ >= 1000);
5998 }
5999
6000 gro_normal_list(napi);
6001 clear_bit(NAPI_STATE_SCHED, &napi->state);
6002}
6003
Björn Töpel7c951caf2020-11-30 19:51:57 +01006004static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll,
6005 u16 budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006006{
6007 bool skip_schedule = false;
6008 unsigned long timeout;
Eric Dumazet217f6972016-11-15 10:15:11 -08006009 int rc;
6010
Eric Dumazet39e6c822017-02-28 10:34:50 -08006011 /* Busy polling means there is a high chance device driver hard irq
6012 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6013 * set in napi_schedule_prep().
6014 * Since we are about to call napi->poll() once more, we can safely
6015 * clear NAPI_STATE_MISSED.
6016 *
6017 * Note: x86 could use a single "lock and ..." instruction
6018 * to perform these two clear_bit()
6019 */
6020 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006021 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6022
6023 local_bh_disable();
6024
Björn Töpel7fd32532020-11-30 19:51:56 +01006025 if (prefer_busy_poll) {
6026 napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
6027 timeout = READ_ONCE(napi->dev->gro_flush_timeout);
6028 if (napi->defer_hard_irqs_count && timeout) {
6029 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6030 skip_schedule = true;
6031 }
6032 }
6033
Eric Dumazet217f6972016-11-15 10:15:11 -08006034 /* All we really want here is to re-enable device interrupts.
6035 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6036 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006037 rc = napi->poll(napi, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006038 /* We can't gro_normal_list() here, because napi->poll() might have
6039 * rearmed the napi (napi_complete_done()) in which case it could
6040 * already be running on another CPU.
6041 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006042 trace_napi_poll(napi, rc, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006043 netpoll_poll_unlock(have_poll_lock);
Björn Töpel7c951caf2020-11-30 19:51:57 +01006044 if (rc == budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006045 __busy_poll_stop(napi, skip_schedule);
Eric Dumazet217f6972016-11-15 10:15:11 -08006046 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006047}
6048
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006049void napi_busy_loop(unsigned int napi_id,
6050 bool (*loop_end)(void *, unsigned long),
Björn Töpel7c951caf2020-11-30 19:51:57 +01006051 void *loop_end_arg, bool prefer_busy_poll, u16 budget)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006052{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006053 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006054 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006055 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006056 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006057
6058restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006059 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006060
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006061 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006062
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006063 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006064 if (!napi)
6065 goto out;
6066
Eric Dumazet217f6972016-11-15 10:15:11 -08006067 preempt_disable();
6068 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006069 int work = 0;
6070
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006071 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006072 if (!napi_poll) {
6073 unsigned long val = READ_ONCE(napi->state);
6074
6075 /* If multiple threads are competing for this napi,
6076 * we avoid dirtying napi->state as much as we can.
6077 */
6078 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
Björn Töpel7fd32532020-11-30 19:51:56 +01006079 NAPIF_STATE_IN_BUSY_POLL)) {
6080 if (prefer_busy_poll)
6081 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006082 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006083 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006084 if (cmpxchg(&napi->state, val,
6085 val | NAPIF_STATE_IN_BUSY_POLL |
Björn Töpel7fd32532020-11-30 19:51:56 +01006086 NAPIF_STATE_SCHED) != val) {
6087 if (prefer_busy_poll)
6088 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006089 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006090 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006091 have_poll_lock = netpoll_poll_lock(napi);
6092 napi_poll = napi->poll;
6093 }
Björn Töpel7c951caf2020-11-30 19:51:57 +01006094 work = napi_poll(napi, budget);
6095 trace_napi_poll(napi, work, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006096 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006097count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006098 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006099 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006100 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006101 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006102
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006103 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006104 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006105
Eric Dumazet217f6972016-11-15 10:15:11 -08006106 if (unlikely(need_resched())) {
6107 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006108 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006109 preempt_enable();
6110 rcu_read_unlock();
6111 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006112 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006113 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006114 goto restart;
6115 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006116 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006117 }
6118 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006119 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006120 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006121out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006122 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006123}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006124EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006125
6126#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006127
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006128static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006129{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006130 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006131 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006132
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006133 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006134
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006135 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006136 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006137 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6138 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006139 } while (napi_by_id(napi_gen_id));
6140 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006141
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006142 hlist_add_head_rcu(&napi->napi_hash_node,
6143 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006144
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006145 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006146}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006147
6148/* Warning : caller is responsible to make sure rcu grace period
6149 * is respected before freeing memory containing @napi
6150 */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006151static void napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006152{
6153 spin_lock(&napi_hash_lock);
6154
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006155 hlist_del_init_rcu(&napi->napi_hash_node);
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006156
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006157 spin_unlock(&napi_hash_lock);
6158}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006159
Eric Dumazet3b47d302014-11-06 21:09:44 -08006160static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6161{
6162 struct napi_struct *napi;
6163
6164 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006165
6166 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6167 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6168 */
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006169 if (!napi_disable_pending(napi) &&
Björn Töpel7fd32532020-11-30 19:51:56 +01006170 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6171 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006172 __napi_schedule_irqoff(napi);
Björn Töpel7fd32532020-11-30 19:51:56 +01006173 }
Eric Dumazet3b47d302014-11-06 21:09:44 -08006174
6175 return HRTIMER_NORESTART;
6176}
6177
David S. Miller7c4ec742018-07-20 23:37:55 -07006178static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006179{
David Miller07d78362018-06-24 14:14:02 +09006180 int i;
6181
Li RongQing6312fe72018-07-05 14:34:32 +08006182 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6183 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6184 napi->gro_hash[i].count = 0;
6185 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006186 napi->gro_bitmask = 0;
6187}
6188
Wei Wang5fdd2f02021-02-08 11:34:10 -08006189int dev_set_threaded(struct net_device *dev, bool threaded)
6190{
6191 struct napi_struct *napi;
6192 int err = 0;
6193
6194 if (dev->threaded == threaded)
6195 return 0;
6196
6197 if (threaded) {
6198 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6199 if (!napi->thread) {
6200 err = napi_kthread_create(napi);
6201 if (err) {
6202 threaded = false;
6203 break;
6204 }
6205 }
6206 }
6207 }
6208
6209 dev->threaded = threaded;
6210
6211 /* Make sure kthread is created before THREADED bit
6212 * is set.
6213 */
6214 smp_mb__before_atomic();
6215
6216 /* Setting/unsetting threaded mode on a napi might not immediately
6217 * take effect, if the current napi instance is actively being
6218 * polled. In this case, the switch between threaded mode and
6219 * softirq mode will happen in the next round of napi_schedule().
6220 * This should not cause hiccups/stalls to the live traffic.
6221 */
6222 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6223 if (threaded)
6224 set_bit(NAPI_STATE_THREADED, &napi->state);
6225 else
6226 clear_bit(NAPI_STATE_THREADED, &napi->state);
6227 }
6228
6229 return err;
6230}
Lorenzo Bianconi8f648602021-03-14 15:49:19 +01006231EXPORT_SYMBOL(dev_set_threaded);
Wei Wang5fdd2f02021-02-08 11:34:10 -08006232
David S. Miller7c4ec742018-07-20 23:37:55 -07006233void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6234 int (*poll)(struct napi_struct *, int), int weight)
6235{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006236 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6237 return;
6238
David S. Miller7c4ec742018-07-20 23:37:55 -07006239 INIT_LIST_HEAD(&napi->poll_list);
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006240 INIT_HLIST_NODE(&napi->napi_hash_node);
David S. Miller7c4ec742018-07-20 23:37:55 -07006241 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6242 napi->timer.function = napi_watchdog;
6243 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006244 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006245 INIT_LIST_HEAD(&napi->rx_list);
6246 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006247 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006248 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006249 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6250 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006251 napi->weight = weight;
Herbert Xud565b0a2008-12-15 23:38:52 -08006252 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006253#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006254 napi->poll_owner = -1;
6255#endif
6256 set_bit(NAPI_STATE_SCHED, &napi->state);
Jakub Kicinski96e97bc2020-08-26 12:40:06 -07006257 set_bit(NAPI_STATE_NPSVC, &napi->state);
6258 list_add_rcu(&napi->dev_list, &dev->napi_list);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006259 napi_hash_add(napi);
Wei Wang29863d42021-02-08 11:34:09 -08006260 /* Create kthread for this napi if dev->threaded is set.
6261 * Clear dev->threaded if kthread creation failed so that
6262 * threaded mode will not be enabled in napi_enable().
6263 */
6264 if (dev->threaded && napi_kthread_create(napi))
6265 dev->threaded = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006266}
6267EXPORT_SYMBOL(netif_napi_add);
6268
Eric Dumazet3b47d302014-11-06 21:09:44 -08006269void napi_disable(struct napi_struct *n)
6270{
Jakub Kicinski719c5712021-09-24 13:24:53 -07006271 unsigned long val, new;
6272
Eric Dumazet3b47d302014-11-06 21:09:44 -08006273 might_sleep();
6274 set_bit(NAPI_STATE_DISABLE, &n->state);
6275
Alexander Lobakin0315a072021-11-10 20:56:05 +01006276 for ( ; ; ) {
Jakub Kicinski719c5712021-09-24 13:24:53 -07006277 val = READ_ONCE(n->state);
6278 if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
6279 usleep_range(20, 200);
6280 continue;
6281 }
6282
6283 new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
6284 new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL);
Alexander Lobakin0315a072021-11-10 20:56:05 +01006285
6286 if (cmpxchg(&n->state, val, new) == val)
6287 break;
6288 }
Eric Dumazet3b47d302014-11-06 21:09:44 -08006289
6290 hrtimer_cancel(&n->timer);
6291
6292 clear_bit(NAPI_STATE_DISABLE, &n->state);
6293}
6294EXPORT_SYMBOL(napi_disable);
6295
Wei Wang29863d42021-02-08 11:34:09 -08006296/**
6297 * napi_enable - enable NAPI scheduling
6298 * @n: NAPI context
6299 *
6300 * Resume NAPI from being scheduled on this context.
6301 * Must be paired with napi_disable.
6302 */
6303void napi_enable(struct napi_struct *n)
6304{
Xuan Zhuo37659962021-09-18 16:52:32 +08006305 unsigned long val, new;
6306
6307 do {
6308 val = READ_ONCE(n->state);
6309 BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
6310
6311 new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
6312 if (n->dev->threaded && n->thread)
6313 new |= NAPIF_STATE_THREADED;
6314 } while (cmpxchg(&n->state, val, new) != val);
Wei Wang29863d42021-02-08 11:34:09 -08006315}
6316EXPORT_SYMBOL(napi_enable);
6317
David Miller07d78362018-06-24 14:14:02 +09006318static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006319{
David Miller07d78362018-06-24 14:14:02 +09006320 int i;
David Millerd4546c22018-06-24 14:13:49 +09006321
David Miller07d78362018-06-24 14:14:02 +09006322 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6323 struct sk_buff *skb, *n;
6324
Li RongQing6312fe72018-07-05 14:34:32 +08006325 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006326 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006327 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006328 }
David Millerd4546c22018-06-24 14:13:49 +09006329}
6330
Eric Dumazet93d05d42015-11-18 06:31:03 -08006331/* Must be called in process context */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006332void __netif_napi_del(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006333{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006334 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6335 return;
6336
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006337 napi_hash_del(napi);
Jakub Kicinski5251ef82020-09-09 10:37:53 -07006338 list_del_rcu(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006339 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006340
David Miller07d78362018-06-24 14:14:02 +09006341 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006342 napi->gro_bitmask = 0;
Wei Wang29863d42021-02-08 11:34:09 -08006343
6344 if (napi->thread) {
6345 kthread_stop(napi->thread);
6346 napi->thread = NULL;
6347 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006348}
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006349EXPORT_SYMBOL(__netif_napi_del);
Herbert Xud565b0a2008-12-15 23:38:52 -08006350
Felix Fietkau898f8012021-02-08 11:34:08 -08006351static int __napi_poll(struct napi_struct *n, bool *repoll)
Herbert Xu726ce702014-12-21 07:16:21 +11006352{
Herbert Xu726ce702014-12-21 07:16:21 +11006353 int work, weight;
6354
Herbert Xu726ce702014-12-21 07:16:21 +11006355 weight = n->weight;
6356
6357 /* This NAPI_STATE_SCHED test is for avoiding a race
6358 * with netpoll's poll_napi(). Only the entity which
6359 * obtains the lock and sees NAPI_STATE_SCHED set will
6360 * actually make the ->poll() call. Therefore we avoid
6361 * accidentally calling ->poll() when NAPI is not scheduled.
6362 */
6363 work = 0;
6364 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6365 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006366 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006367 }
6368
Eric Dumazet427d5832020-06-17 09:40:51 -07006369 if (unlikely(work > weight))
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07006370 netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6371 n->poll, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006372
6373 if (likely(work < weight))
Felix Fietkau898f8012021-02-08 11:34:08 -08006374 return work;
Herbert Xu726ce702014-12-21 07:16:21 +11006375
6376 /* Drivers must not modify the NAPI state if they
6377 * consume the entire weight. In such cases this code
6378 * still "owns" the NAPI instance and therefore can
6379 * move the instance around on the list at-will.
6380 */
6381 if (unlikely(napi_disable_pending(n))) {
6382 napi_complete(n);
Felix Fietkau898f8012021-02-08 11:34:08 -08006383 return work;
Herbert Xu726ce702014-12-21 07:16:21 +11006384 }
6385
Björn Töpel7fd32532020-11-30 19:51:56 +01006386 /* The NAPI context has more processing work, but busy-polling
6387 * is preferred. Exit early.
6388 */
6389 if (napi_prefer_busy_poll(n)) {
6390 if (napi_complete_done(n, work)) {
6391 /* If timeout is not set, we need to make sure
6392 * that the NAPI is re-scheduled.
6393 */
6394 napi_schedule(n);
6395 }
Felix Fietkau898f8012021-02-08 11:34:08 -08006396 return work;
Björn Töpel7fd32532020-11-30 19:51:56 +01006397 }
6398
Li RongQingd9f37d02018-07-13 14:41:36 +08006399 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006400 /* flush too old packets
6401 * If HZ < 1000, flush all packets.
6402 */
6403 napi_gro_flush(n, HZ >= 1000);
6404 }
6405
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006406 gro_normal_list(n);
6407
Herbert Xu001ce542014-12-21 07:16:22 +11006408 /* Some drivers may have called napi_schedule
6409 * prior to exhausting their budget.
6410 */
6411 if (unlikely(!list_empty(&n->poll_list))) {
6412 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6413 n->dev ? n->dev->name : "backlog");
Felix Fietkau898f8012021-02-08 11:34:08 -08006414 return work;
Herbert Xu001ce542014-12-21 07:16:22 +11006415 }
6416
Felix Fietkau898f8012021-02-08 11:34:08 -08006417 *repoll = true;
Herbert Xu726ce702014-12-21 07:16:21 +11006418
Felix Fietkau898f8012021-02-08 11:34:08 -08006419 return work;
6420}
6421
6422static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6423{
6424 bool do_repoll = false;
6425 void *have;
6426 int work;
6427
6428 list_del_init(&n->poll_list);
6429
6430 have = netpoll_poll_lock(n);
6431
6432 work = __napi_poll(n, &do_repoll);
6433
6434 if (do_repoll)
6435 list_add_tail(&n->poll_list, repoll);
6436
Herbert Xu726ce702014-12-21 07:16:21 +11006437 netpoll_poll_unlock(have);
6438
6439 return work;
6440}
6441
Wei Wang29863d42021-02-08 11:34:09 -08006442static int napi_thread_wait(struct napi_struct *napi)
6443{
Wei Wangcb038352021-03-16 15:36:47 -07006444 bool woken = false;
6445
Wei Wang29863d42021-02-08 11:34:09 -08006446 set_current_state(TASK_INTERRUPTIBLE);
6447
Paolo Abeni27f0ad72021-04-09 17:24:17 +02006448 while (!kthread_should_stop()) {
Wei Wangcb038352021-03-16 15:36:47 -07006449 /* Testing SCHED_THREADED bit here to make sure the current
6450 * kthread owns this napi and could poll on this napi.
6451 * Testing SCHED bit is not enough because SCHED bit might be
6452 * set by some other busy poll thread or by napi_disable().
6453 */
6454 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
Wei Wang29863d42021-02-08 11:34:09 -08006455 WARN_ON(!list_empty(&napi->poll_list));
6456 __set_current_state(TASK_RUNNING);
6457 return 0;
6458 }
6459
6460 schedule();
Wei Wangcb038352021-03-16 15:36:47 -07006461 /* woken being true indicates this thread owns this napi. */
6462 woken = true;
Wei Wang29863d42021-02-08 11:34:09 -08006463 set_current_state(TASK_INTERRUPTIBLE);
6464 }
6465 __set_current_state(TASK_RUNNING);
Paolo Abeni27f0ad72021-04-09 17:24:17 +02006466
Wei Wang29863d42021-02-08 11:34:09 -08006467 return -1;
6468}
6469
6470static int napi_threaded_poll(void *data)
6471{
6472 struct napi_struct *napi = data;
6473 void *have;
6474
6475 while (!napi_thread_wait(napi)) {
6476 for (;;) {
6477 bool repoll = false;
6478
6479 local_bh_disable();
6480
6481 have = netpoll_poll_lock(napi);
6482 __napi_poll(napi, &repoll);
6483 netpoll_poll_unlock(have);
6484
Wei Wang29863d42021-02-08 11:34:09 -08006485 local_bh_enable();
6486
6487 if (!repoll)
6488 break;
6489
6490 cond_resched();
6491 }
6492 }
6493 return 0;
6494}
6495
Emese Revfy0766f782016-06-20 20:42:34 +02006496static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006498 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006499 unsigned long time_limit = jiffies +
6500 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006501 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006502 LIST_HEAD(list);
6503 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006506 list_splice_init(&sd->poll_list, &list);
6507 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006509 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006510 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006512 if (list_empty(&list)) {
6513 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Alexander Lobakinfec6e492021-02-13 14:12:02 +00006514 return;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006515 break;
6516 }
6517
Herbert Xu6bd373e2014-12-21 07:16:24 +11006518 n = list_first_entry(&list, struct napi_struct, poll_list);
6519 budget -= napi_poll(n, &repoll);
6520
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006521 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006522 * Allow this to run for 2 jiffies since which will allow
6523 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006524 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006525 if (unlikely(budget <= 0 ||
6526 time_after_eq(jiffies, time_limit))) {
6527 sd->time_squeeze++;
6528 break;
6529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006531
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006532 local_irq_disable();
6533
6534 list_splice_tail_init(&sd->poll_list, &list);
6535 list_splice_tail(&repoll, &list);
6536 list_splice(&list, &sd->poll_list);
6537 if (!list_empty(&sd->poll_list))
6538 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6539
Eric Dumazete326bed2010-04-22 00:22:45 -07006540 net_rps_action_and_irq_enable(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541}
6542
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006543struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006544 struct net_device *dev;
Eric Dumazetf77159a2021-12-04 20:22:10 -08006545 netdevice_tracker dev_tracker;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006546
6547 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006548 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006549
Taehee Yoo32b6d342019-10-21 18:47:56 +00006550 /* lookup ignore flag */
6551 bool ignore;
6552
Veaceslav Falico5d261912013-08-28 23:25:05 +02006553 /* counter for the number of times this device was added to us */
6554 u16 ref_nr;
6555
Veaceslav Falico402dae92013-09-25 09:20:09 +02006556 /* private field for the users */
6557 void *private;
6558
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006559 struct list_head list;
6560 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006561};
6562
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006563static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006564 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006565{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006566 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006567
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006568 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006569 if (adj->dev == adj_dev)
6570 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006571 }
6572 return NULL;
6573}
6574
Taehee Yooeff74232020-09-25 18:13:12 +00006575static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6576 struct netdev_nested_priv *priv)
David Ahernf1170fd2016-10-17 19:15:51 -07006577{
Taehee Yooeff74232020-09-25 18:13:12 +00006578 struct net_device *dev = (struct net_device *)priv->data;
David Ahernf1170fd2016-10-17 19:15:51 -07006579
6580 return upper_dev == dev;
6581}
6582
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006583/**
6584 * netdev_has_upper_dev - Check if device is linked to an upper device
6585 * @dev: device
6586 * @upper_dev: upper device to check
6587 *
6588 * Find out if a device is linked to specified upper device and return true
6589 * in case it is. Note that this checks only immediate upper device,
6590 * not through a complete stack of devices. The caller must hold the RTNL lock.
6591 */
6592bool netdev_has_upper_dev(struct net_device *dev,
6593 struct net_device *upper_dev)
6594{
Taehee Yooeff74232020-09-25 18:13:12 +00006595 struct netdev_nested_priv priv = {
6596 .data = (void *)upper_dev,
6597 };
6598
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006599 ASSERT_RTNL();
6600
Taehee Yoo32b6d342019-10-21 18:47:56 +00006601 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006602 &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006603}
6604EXPORT_SYMBOL(netdev_has_upper_dev);
6605
6606/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01006607 * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
David Ahern1a3f0602016-10-17 19:15:44 -07006608 * @dev: device
6609 * @upper_dev: upper device to check
6610 *
6611 * Find out if a device is linked to specified upper device and return true
6612 * in case it is. Note that this checks the entire upper device chain.
6613 * The caller must hold rcu lock.
6614 */
6615
David Ahern1a3f0602016-10-17 19:15:44 -07006616bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6617 struct net_device *upper_dev)
6618{
Taehee Yooeff74232020-09-25 18:13:12 +00006619 struct netdev_nested_priv priv = {
6620 .data = (void *)upper_dev,
6621 };
6622
Taehee Yoo32b6d342019-10-21 18:47:56 +00006623 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006624 &priv);
David Ahern1a3f0602016-10-17 19:15:44 -07006625}
6626EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6627
6628/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006629 * netdev_has_any_upper_dev - Check if device is linked to some device
6630 * @dev: device
6631 *
6632 * Find out if a device is linked to an upper device and return true in case
6633 * it is. The caller must hold the RTNL lock.
6634 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006635bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006636{
6637 ASSERT_RTNL();
6638
David Ahernf1170fd2016-10-17 19:15:51 -07006639 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006640}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006641EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006642
6643/**
6644 * netdev_master_upper_dev_get - Get master upper device
6645 * @dev: device
6646 *
6647 * Find a master upper device and return pointer to it or NULL in case
6648 * it's not there. The caller must hold the RTNL lock.
6649 */
6650struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6651{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006652 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006653
6654 ASSERT_RTNL();
6655
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006656 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006657 return NULL;
6658
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006659 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006660 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006661 if (likely(upper->master))
6662 return upper->dev;
6663 return NULL;
6664}
6665EXPORT_SYMBOL(netdev_master_upper_dev_get);
6666
Taehee Yoo32b6d342019-10-21 18:47:56 +00006667static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6668{
6669 struct netdev_adjacent *upper;
6670
6671 ASSERT_RTNL();
6672
6673 if (list_empty(&dev->adj_list.upper))
6674 return NULL;
6675
6676 upper = list_first_entry(&dev->adj_list.upper,
6677 struct netdev_adjacent, list);
6678 if (likely(upper->master) && !upper->ignore)
6679 return upper->dev;
6680 return NULL;
6681}
6682
David Ahern0f524a82016-10-17 19:15:52 -07006683/**
6684 * netdev_has_any_lower_dev - Check if device is linked to some device
6685 * @dev: device
6686 *
6687 * Find out if a device is linked to a lower device and return true in case
6688 * it is. The caller must hold the RTNL lock.
6689 */
6690static bool netdev_has_any_lower_dev(struct net_device *dev)
6691{
6692 ASSERT_RTNL();
6693
6694 return !list_empty(&dev->adj_list.lower);
6695}
6696
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006697void *netdev_adjacent_get_private(struct list_head *adj_list)
6698{
6699 struct netdev_adjacent *adj;
6700
6701 adj = list_entry(adj_list, struct netdev_adjacent, list);
6702
6703 return adj->private;
6704}
6705EXPORT_SYMBOL(netdev_adjacent_get_private);
6706
Veaceslav Falico31088a12013-09-25 09:20:12 +02006707/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006708 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6709 * @dev: device
6710 * @iter: list_head ** of the current position
6711 *
6712 * Gets the next device from the dev's upper list, starting from iter
6713 * position. The caller must hold RCU read lock.
6714 */
6715struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6716 struct list_head **iter)
6717{
6718 struct netdev_adjacent *upper;
6719
6720 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6721
6722 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6723
6724 if (&upper->list == &dev->adj_list.upper)
6725 return NULL;
6726
6727 *iter = &upper->list;
6728
6729 return upper->dev;
6730}
6731EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6732
Taehee Yoo32b6d342019-10-21 18:47:56 +00006733static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6734 struct list_head **iter,
6735 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00006736{
6737 struct netdev_adjacent *upper;
6738
6739 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6740
6741 if (&upper->list == &dev->adj_list.upper)
6742 return NULL;
6743
6744 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006745 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006746
6747 return upper->dev;
6748}
6749
David Ahern1a3f0602016-10-17 19:15:44 -07006750static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6751 struct list_head **iter)
6752{
6753 struct netdev_adjacent *upper;
6754
6755 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6756
6757 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6758
6759 if (&upper->list == &dev->adj_list.upper)
6760 return NULL;
6761
6762 *iter = &upper->list;
6763
6764 return upper->dev;
6765}
6766
Taehee Yoo32b6d342019-10-21 18:47:56 +00006767static int __netdev_walk_all_upper_dev(struct net_device *dev,
6768 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006769 struct netdev_nested_priv *priv),
6770 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00006771{
6772 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6773 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6774 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006775 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006776
6777 now = dev;
6778 iter = &dev->adj_list.upper;
6779
6780 while (1) {
6781 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00006782 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00006783 if (ret)
6784 return ret;
6785 }
6786
6787 next = NULL;
6788 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00006789 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00006790 if (!udev)
6791 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006792 if (ignore)
6793 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00006794
6795 next = udev;
6796 niter = &udev->adj_list.upper;
6797 dev_stack[cur] = now;
6798 iter_stack[cur++] = iter;
6799 break;
6800 }
6801
6802 if (!next) {
6803 if (!cur)
6804 return 0;
6805 next = dev_stack[--cur];
6806 niter = iter_stack[cur];
6807 }
6808
6809 now = next;
6810 iter = niter;
6811 }
6812
6813 return 0;
6814}
6815
David Ahern1a3f0602016-10-17 19:15:44 -07006816int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6817 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006818 struct netdev_nested_priv *priv),
6819 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07006820{
Taehee Yoo5343da42019-10-21 18:47:50 +00006821 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6822 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6823 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07006824
Taehee Yoo5343da42019-10-21 18:47:50 +00006825 now = dev;
6826 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07006827
Taehee Yoo5343da42019-10-21 18:47:50 +00006828 while (1) {
6829 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00006830 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00006831 if (ret)
6832 return ret;
6833 }
6834
6835 next = NULL;
6836 while (1) {
6837 udev = netdev_next_upper_dev_rcu(now, &iter);
6838 if (!udev)
6839 break;
6840
6841 next = udev;
6842 niter = &udev->adj_list.upper;
6843 dev_stack[cur] = now;
6844 iter_stack[cur++] = iter;
6845 break;
6846 }
6847
6848 if (!next) {
6849 if (!cur)
6850 return 0;
6851 next = dev_stack[--cur];
6852 niter = iter_stack[cur];
6853 }
6854
6855 now = next;
6856 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07006857 }
6858
6859 return 0;
6860}
6861EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6862
Taehee Yoo32b6d342019-10-21 18:47:56 +00006863static bool __netdev_has_upper_dev(struct net_device *dev,
6864 struct net_device *upper_dev)
6865{
Taehee Yooeff74232020-09-25 18:13:12 +00006866 struct netdev_nested_priv priv = {
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00006867 .flags = 0,
Taehee Yooeff74232020-09-25 18:13:12 +00006868 .data = (void *)upper_dev,
6869 };
6870
Taehee Yoo32b6d342019-10-21 18:47:56 +00006871 ASSERT_RTNL();
6872
6873 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006874 &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00006875}
6876
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006877/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006878 * netdev_lower_get_next_private - Get the next ->private from the
6879 * lower neighbour list
6880 * @dev: device
6881 * @iter: list_head ** of the current position
6882 *
6883 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6884 * list, starting from iter position. The caller must hold either hold the
6885 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006886 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006887 */
6888void *netdev_lower_get_next_private(struct net_device *dev,
6889 struct list_head **iter)
6890{
6891 struct netdev_adjacent *lower;
6892
6893 lower = list_entry(*iter, struct netdev_adjacent, list);
6894
6895 if (&lower->list == &dev->adj_list.lower)
6896 return NULL;
6897
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006898 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006899
6900 return lower->private;
6901}
6902EXPORT_SYMBOL(netdev_lower_get_next_private);
6903
6904/**
6905 * netdev_lower_get_next_private_rcu - Get the next ->private from the
6906 * lower neighbour list, RCU
6907 * variant
6908 * @dev: device
6909 * @iter: list_head ** of the current position
6910 *
6911 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6912 * list, starting from iter position. The caller must hold RCU read lock.
6913 */
6914void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6915 struct list_head **iter)
6916{
6917 struct netdev_adjacent *lower;
6918
Jussi Maki68918662021-07-31 05:57:36 +00006919 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
Veaceslav Falico31088a12013-09-25 09:20:12 +02006920
6921 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6922
6923 if (&lower->list == &dev->adj_list.lower)
6924 return NULL;
6925
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006926 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006927
6928 return lower->private;
6929}
6930EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6931
6932/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006933 * netdev_lower_get_next - Get the next device from the lower neighbour
6934 * list
6935 * @dev: device
6936 * @iter: list_head ** of the current position
6937 *
6938 * Gets the next netdev_adjacent from the dev's lower neighbour
6939 * list, starting from iter position. The caller must hold RTNL lock or
6940 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006941 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006942 */
6943void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
6944{
6945 struct netdev_adjacent *lower;
6946
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006947 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006948
6949 if (&lower->list == &dev->adj_list.lower)
6950 return NULL;
6951
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006952 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006953
6954 return lower->dev;
6955}
6956EXPORT_SYMBOL(netdev_lower_get_next);
6957
David Ahern1a3f0602016-10-17 19:15:44 -07006958static struct net_device *netdev_next_lower_dev(struct net_device *dev,
6959 struct list_head **iter)
6960{
6961 struct netdev_adjacent *lower;
6962
David Ahern46b5ab12016-10-26 13:21:33 -07006963 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07006964
6965 if (&lower->list == &dev->adj_list.lower)
6966 return NULL;
6967
David Ahern46b5ab12016-10-26 13:21:33 -07006968 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07006969
6970 return lower->dev;
6971}
6972
Taehee Yoo32b6d342019-10-21 18:47:56 +00006973static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
6974 struct list_head **iter,
6975 bool *ignore)
6976{
6977 struct netdev_adjacent *lower;
6978
6979 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
6980
6981 if (&lower->list == &dev->adj_list.lower)
6982 return NULL;
6983
6984 *iter = &lower->list;
6985 *ignore = lower->ignore;
6986
6987 return lower->dev;
6988}
6989
David Ahern1a3f0602016-10-17 19:15:44 -07006990int netdev_walk_all_lower_dev(struct net_device *dev,
6991 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006992 struct netdev_nested_priv *priv),
6993 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07006994{
Taehee Yoo5343da42019-10-21 18:47:50 +00006995 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6996 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6997 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07006998
Taehee Yoo5343da42019-10-21 18:47:50 +00006999 now = dev;
7000 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007001
Taehee Yoo5343da42019-10-21 18:47:50 +00007002 while (1) {
7003 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007004 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007005 if (ret)
7006 return ret;
7007 }
7008
7009 next = NULL;
7010 while (1) {
7011 ldev = netdev_next_lower_dev(now, &iter);
7012 if (!ldev)
7013 break;
7014
7015 next = ldev;
7016 niter = &ldev->adj_list.lower;
7017 dev_stack[cur] = now;
7018 iter_stack[cur++] = iter;
7019 break;
7020 }
7021
7022 if (!next) {
7023 if (!cur)
7024 return 0;
7025 next = dev_stack[--cur];
7026 niter = iter_stack[cur];
7027 }
7028
7029 now = next;
7030 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007031 }
7032
7033 return 0;
7034}
7035EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7036
Taehee Yoo32b6d342019-10-21 18:47:56 +00007037static int __netdev_walk_all_lower_dev(struct net_device *dev,
7038 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007039 struct netdev_nested_priv *priv),
7040 struct netdev_nested_priv *priv)
Taehee Yoo32b6d342019-10-21 18:47:56 +00007041{
7042 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7043 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7044 int ret, cur = 0;
7045 bool ignore;
7046
7047 now = dev;
7048 iter = &dev->adj_list.lower;
7049
7050 while (1) {
7051 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007052 ret = fn(now, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007053 if (ret)
7054 return ret;
7055 }
7056
7057 next = NULL;
7058 while (1) {
7059 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7060 if (!ldev)
7061 break;
7062 if (ignore)
7063 continue;
7064
7065 next = ldev;
7066 niter = &ldev->adj_list.lower;
7067 dev_stack[cur] = now;
7068 iter_stack[cur++] = iter;
7069 break;
7070 }
7071
7072 if (!next) {
7073 if (!cur)
7074 return 0;
7075 next = dev_stack[--cur];
7076 niter = iter_stack[cur];
7077 }
7078
7079 now = next;
7080 iter = niter;
7081 }
7082
7083 return 0;
7084}
7085
Taehee Yoo7151aff2020-02-15 10:50:21 +00007086struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7087 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007088{
7089 struct netdev_adjacent *lower;
7090
7091 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7092 if (&lower->list == &dev->adj_list.lower)
7093 return NULL;
7094
7095 *iter = &lower->list;
7096
7097 return lower->dev;
7098}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007099EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007100
Taehee Yoo5343da42019-10-21 18:47:50 +00007101static u8 __netdev_upper_depth(struct net_device *dev)
7102{
7103 struct net_device *udev;
7104 struct list_head *iter;
7105 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007106 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007107
7108 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007109 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007110 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007111 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7112 if (ignore)
7113 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007114 if (max_depth < udev->upper_level)
7115 max_depth = udev->upper_level;
7116 }
7117
7118 return max_depth;
7119}
7120
7121static u8 __netdev_lower_depth(struct net_device *dev)
7122{
7123 struct net_device *ldev;
7124 struct list_head *iter;
7125 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007126 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007127
7128 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007129 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007130 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007131 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7132 if (ignore)
7133 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007134 if (max_depth < ldev->lower_level)
7135 max_depth = ldev->lower_level;
7136 }
7137
7138 return max_depth;
7139}
7140
Taehee Yooeff74232020-09-25 18:13:12 +00007141static int __netdev_update_upper_level(struct net_device *dev,
7142 struct netdev_nested_priv *__unused)
Taehee Yoo5343da42019-10-21 18:47:50 +00007143{
7144 dev->upper_level = __netdev_upper_depth(dev) + 1;
7145 return 0;
7146}
7147
Taehee Yooeff74232020-09-25 18:13:12 +00007148static int __netdev_update_lower_level(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007149 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007150{
7151 dev->lower_level = __netdev_lower_depth(dev) + 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007152
7153#ifdef CONFIG_LOCKDEP
7154 if (!priv)
7155 return 0;
7156
7157 if (priv->flags & NESTED_SYNC_IMM)
7158 dev->nested_level = dev->lower_level - 1;
7159 if (priv->flags & NESTED_SYNC_TODO)
7160 net_unlink_todo(dev);
7161#endif
Taehee Yoo5343da42019-10-21 18:47:50 +00007162 return 0;
7163}
7164
David Ahern1a3f0602016-10-17 19:15:44 -07007165int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7166 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007167 struct netdev_nested_priv *priv),
7168 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007169{
Taehee Yoo5343da42019-10-21 18:47:50 +00007170 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7171 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7172 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007173
Taehee Yoo5343da42019-10-21 18:47:50 +00007174 now = dev;
7175 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007176
Taehee Yoo5343da42019-10-21 18:47:50 +00007177 while (1) {
7178 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007179 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007180 if (ret)
7181 return ret;
7182 }
7183
7184 next = NULL;
7185 while (1) {
7186 ldev = netdev_next_lower_dev_rcu(now, &iter);
7187 if (!ldev)
7188 break;
7189
7190 next = ldev;
7191 niter = &ldev->adj_list.lower;
7192 dev_stack[cur] = now;
7193 iter_stack[cur++] = iter;
7194 break;
7195 }
7196
7197 if (!next) {
7198 if (!cur)
7199 return 0;
7200 next = dev_stack[--cur];
7201 niter = iter_stack[cur];
7202 }
7203
7204 now = next;
7205 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007206 }
7207
7208 return 0;
7209}
7210EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7211
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007212/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007213 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7214 * lower neighbour list, RCU
7215 * variant
7216 * @dev: device
7217 *
7218 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7219 * list. The caller must hold RCU read lock.
7220 */
7221void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7222{
7223 struct netdev_adjacent *lower;
7224
7225 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7226 struct netdev_adjacent, list);
7227 if (lower)
7228 return lower->private;
7229 return NULL;
7230}
7231EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7232
7233/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007234 * netdev_master_upper_dev_get_rcu - Get master upper device
7235 * @dev: device
7236 *
7237 * Find a master upper device and return pointer to it or NULL in case
7238 * it's not there. The caller must hold the RCU read lock.
7239 */
7240struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7241{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007242 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007243
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007244 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007245 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007246 if (upper && likely(upper->master))
7247 return upper->dev;
7248 return NULL;
7249}
7250EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7251
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307252static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007253 struct net_device *adj_dev,
7254 struct list_head *dev_list)
7255{
7256 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007257
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007258 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7259 "upper_%s" : "lower_%s", adj_dev->name);
7260 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7261 linkname);
7262}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307263static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007264 char *name,
7265 struct list_head *dev_list)
7266{
7267 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007268
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007269 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7270 "upper_%s" : "lower_%s", name);
7271 sysfs_remove_link(&(dev->dev.kobj), linkname);
7272}
7273
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007274static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7275 struct net_device *adj_dev,
7276 struct list_head *dev_list)
7277{
7278 return (dev_list == &dev->adj_list.upper ||
7279 dev_list == &dev->adj_list.lower) &&
7280 net_eq(dev_net(dev), dev_net(adj_dev));
7281}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007282
Veaceslav Falico5d261912013-08-28 23:25:05 +02007283static int __netdev_adjacent_dev_insert(struct net_device *dev,
7284 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007285 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007286 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007287{
7288 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007289 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007290
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007291 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007292
7293 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007294 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007295 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7296 dev->name, adj_dev->name, adj->ref_nr);
7297
Veaceslav Falico5d261912013-08-28 23:25:05 +02007298 return 0;
7299 }
7300
7301 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7302 if (!adj)
7303 return -ENOMEM;
7304
7305 adj->dev = adj_dev;
7306 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007307 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007308 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007309 adj->ignore = false;
Eric Dumazetf77159a2021-12-04 20:22:10 -08007310 dev_hold_track(adj_dev, &adj->dev_tracker, GFP_KERNEL);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007311
David Ahern67b62f92016-10-17 19:15:53 -07007312 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7313 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007314
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007315 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007316 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007317 if (ret)
7318 goto free_adj;
7319 }
7320
Veaceslav Falico7863c052013-09-25 09:20:06 +02007321 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007322 if (master) {
7323 ret = sysfs_create_link(&(dev->dev.kobj),
7324 &(adj_dev->dev.kobj), "master");
7325 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007326 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007327
Veaceslav Falico7863c052013-09-25 09:20:06 +02007328 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007329 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007330 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007331 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007332
7333 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007334
Veaceslav Falico5831d662013-09-25 09:20:32 +02007335remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007336 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007337 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007338free_adj:
Eric Dumazetf77159a2021-12-04 20:22:10 -08007339 dev_put_track(adj_dev, &adj->dev_tracker);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007340 kfree(adj);
7341
7342 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007343}
7344
stephen hemminger1d143d92013-12-29 14:01:29 -08007345static void __netdev_adjacent_dev_remove(struct net_device *dev,
7346 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007347 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007348 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007349{
7350 struct netdev_adjacent *adj;
7351
David Ahern67b62f92016-10-17 19:15:53 -07007352 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7353 dev->name, adj_dev->name, ref_nr);
7354
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007355 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007356
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007357 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007358 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007359 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007360 WARN_ON(1);
7361 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007362 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007363
Andrew Collins93409032016-10-03 13:43:02 -06007364 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007365 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7366 dev->name, adj_dev->name, ref_nr,
7367 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007368 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007369 return;
7370 }
7371
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007372 if (adj->master)
7373 sysfs_remove_link(&(dev->dev.kobj), "master");
7374
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007375 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007376 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007377
Veaceslav Falico5d261912013-08-28 23:25:05 +02007378 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007379 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007380 adj_dev->name, dev->name, adj_dev->name);
Eric Dumazetf77159a2021-12-04 20:22:10 -08007381 dev_put_track(adj_dev, &adj->dev_tracker);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007382 kfree_rcu(adj, rcu);
7383}
7384
stephen hemminger1d143d92013-12-29 14:01:29 -08007385static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7386 struct net_device *upper_dev,
7387 struct list_head *up_list,
7388 struct list_head *down_list,
7389 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007390{
7391 int ret;
7392
David Ahern790510d2016-10-17 19:15:43 -07007393 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007394 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007395 if (ret)
7396 return ret;
7397
David Ahern790510d2016-10-17 19:15:43 -07007398 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007399 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007400 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007401 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007402 return ret;
7403 }
7404
7405 return 0;
7406}
7407
stephen hemminger1d143d92013-12-29 14:01:29 -08007408static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7409 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007410 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007411 struct list_head *up_list,
7412 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007413{
Andrew Collins93409032016-10-03 13:43:02 -06007414 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7415 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007416}
7417
stephen hemminger1d143d92013-12-29 14:01:29 -08007418static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7419 struct net_device *upper_dev,
7420 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007421{
David Ahernf1170fd2016-10-17 19:15:51 -07007422 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7423 &dev->adj_list.upper,
7424 &upper_dev->adj_list.lower,
7425 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007426}
7427
stephen hemminger1d143d92013-12-29 14:01:29 -08007428static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7429 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007430{
Andrew Collins93409032016-10-03 13:43:02 -06007431 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007432 &dev->adj_list.upper,
7433 &upper_dev->adj_list.lower);
7434}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007435
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007436static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007437 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007438 void *upper_priv, void *upper_info,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007439 struct netdev_nested_priv *priv,
David Ahern42ab19e2017-10-04 17:48:47 -07007440 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007441{
David Ahern51d0c0472017-10-04 17:48:45 -07007442 struct netdev_notifier_changeupper_info changeupper_info = {
7443 .info = {
7444 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007445 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007446 },
7447 .upper_dev = upper_dev,
7448 .master = master,
7449 .linking = true,
7450 .upper_info = upper_info,
7451 };
Mike Manning50d629e2018-02-26 23:49:30 +00007452 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007453 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007454
7455 ASSERT_RTNL();
7456
7457 if (dev == upper_dev)
7458 return -EBUSY;
7459
7460 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007461 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007462 return -EBUSY;
7463
Taehee Yoo5343da42019-10-21 18:47:50 +00007464 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7465 return -EMLINK;
7466
Mike Manning50d629e2018-02-26 23:49:30 +00007467 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007468 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007469 return -EEXIST;
7470 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007471 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007472 if (master_dev)
7473 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7474 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007475
David Ahern51d0c0472017-10-04 17:48:45 -07007476 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007477 &changeupper_info.info);
7478 ret = notifier_to_errno(ret);
7479 if (ret)
7480 return ret;
7481
Jiri Pirko6dffb042015-12-03 12:12:10 +01007482 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007483 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007484 if (ret)
7485 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007486
David Ahern51d0c0472017-10-04 17:48:45 -07007487 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007488 &changeupper_info.info);
7489 ret = notifier_to_errno(ret);
7490 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007491 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007492
Taehee Yoo5343da42019-10-21 18:47:50 +00007493 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007494 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007495
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007496 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007497 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007498 priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007499
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007500 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007501
David Ahernf1170fd2016-10-17 19:15:51 -07007502rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007503 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007504
7505 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007506}
7507
7508/**
7509 * netdev_upper_dev_link - Add a link to the upper device
7510 * @dev: device
7511 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007512 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007513 *
7514 * Adds a link to device which is upper to this one. The caller must hold
7515 * the RTNL lock. On a failure a negative errno code is returned.
7516 * On success the reference counts are adjusted and the function
7517 * returns zero.
7518 */
7519int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007520 struct net_device *upper_dev,
7521 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007522{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007523 struct netdev_nested_priv priv = {
7524 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7525 .data = NULL,
7526 };
7527
David Ahern42ab19e2017-10-04 17:48:47 -07007528 return __netdev_upper_dev_link(dev, upper_dev, false,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007529 NULL, NULL, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007530}
7531EXPORT_SYMBOL(netdev_upper_dev_link);
7532
7533/**
7534 * netdev_master_upper_dev_link - Add a master link to the upper device
7535 * @dev: device
7536 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007537 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007538 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007539 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007540 *
7541 * Adds a link to device which is upper to this one. In this case, only
7542 * one master upper device can be linked, although other non-master devices
7543 * might be linked as well. The caller must hold the RTNL lock.
7544 * On a failure a negative errno code is returned. On success the reference
7545 * counts are adjusted and the function returns zero.
7546 */
7547int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007548 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007549 void *upper_priv, void *upper_info,
7550 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007551{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007552 struct netdev_nested_priv priv = {
7553 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7554 .data = NULL,
7555 };
7556
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007557 return __netdev_upper_dev_link(dev, upper_dev, true,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007558 upper_priv, upper_info, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007559}
7560EXPORT_SYMBOL(netdev_master_upper_dev_link);
7561
Taehee Yoofe8300f2020-09-25 18:13:02 +00007562static void __netdev_upper_dev_unlink(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007563 struct net_device *upper_dev,
7564 struct netdev_nested_priv *priv)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007565{
David Ahern51d0c0472017-10-04 17:48:45 -07007566 struct netdev_notifier_changeupper_info changeupper_info = {
7567 .info = {
7568 .dev = dev,
7569 },
7570 .upper_dev = upper_dev,
7571 .linking = false,
7572 };
tchardingf4563a72017-02-09 17:56:07 +11007573
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007574 ASSERT_RTNL();
7575
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007576 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007577
David Ahern51d0c0472017-10-04 17:48:45 -07007578 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007579 &changeupper_info.info);
7580
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007581 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007582
David Ahern51d0c0472017-10-04 17:48:45 -07007583 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007584 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007585
7586 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007587 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007588
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007589 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007590 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007591 priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007592}
Taehee Yoofe8300f2020-09-25 18:13:02 +00007593
7594/**
7595 * netdev_upper_dev_unlink - Removes a link to upper device
7596 * @dev: device
7597 * @upper_dev: new upper device
7598 *
7599 * Removes a link to device which is upper to this one. The caller must hold
7600 * the RTNL lock.
7601 */
7602void netdev_upper_dev_unlink(struct net_device *dev,
7603 struct net_device *upper_dev)
7604{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007605 struct netdev_nested_priv priv = {
7606 .flags = NESTED_SYNC_TODO,
7607 .data = NULL,
7608 };
7609
7610 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007611}
7612EXPORT_SYMBOL(netdev_upper_dev_unlink);
7613
Taehee Yoo32b6d342019-10-21 18:47:56 +00007614static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7615 struct net_device *lower_dev,
7616 bool val)
7617{
7618 struct netdev_adjacent *adj;
7619
7620 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7621 if (adj)
7622 adj->ignore = val;
7623
7624 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7625 if (adj)
7626 adj->ignore = val;
7627}
7628
7629static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7630 struct net_device *lower_dev)
7631{
7632 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7633}
7634
7635static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7636 struct net_device *lower_dev)
7637{
7638 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7639}
7640
7641int netdev_adjacent_change_prepare(struct net_device *old_dev,
7642 struct net_device *new_dev,
7643 struct net_device *dev,
7644 struct netlink_ext_ack *extack)
7645{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007646 struct netdev_nested_priv priv = {
7647 .flags = 0,
7648 .data = NULL,
7649 };
Taehee Yoo32b6d342019-10-21 18:47:56 +00007650 int err;
7651
7652 if (!new_dev)
7653 return 0;
7654
7655 if (old_dev && new_dev != old_dev)
7656 netdev_adjacent_dev_disable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007657 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
7658 extack);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007659 if (err) {
7660 if (old_dev && new_dev != old_dev)
7661 netdev_adjacent_dev_enable(dev, old_dev);
7662 return err;
7663 }
7664
7665 return 0;
7666}
7667EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7668
7669void netdev_adjacent_change_commit(struct net_device *old_dev,
7670 struct net_device *new_dev,
7671 struct net_device *dev)
7672{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007673 struct netdev_nested_priv priv = {
7674 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7675 .data = NULL,
7676 };
7677
Taehee Yoo32b6d342019-10-21 18:47:56 +00007678 if (!new_dev || !old_dev)
7679 return;
7680
7681 if (new_dev == old_dev)
7682 return;
7683
7684 netdev_adjacent_dev_enable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007685 __netdev_upper_dev_unlink(old_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007686}
7687EXPORT_SYMBOL(netdev_adjacent_change_commit);
7688
7689void netdev_adjacent_change_abort(struct net_device *old_dev,
7690 struct net_device *new_dev,
7691 struct net_device *dev)
7692{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007693 struct netdev_nested_priv priv = {
7694 .flags = 0,
7695 .data = NULL,
7696 };
7697
Taehee Yoo32b6d342019-10-21 18:47:56 +00007698 if (!new_dev)
7699 return;
7700
7701 if (old_dev && new_dev != old_dev)
7702 netdev_adjacent_dev_enable(dev, old_dev);
7703
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007704 __netdev_upper_dev_unlink(new_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007705}
7706EXPORT_SYMBOL(netdev_adjacent_change_abort);
7707
Moni Shoua61bd3852015-02-03 16:48:29 +02007708/**
7709 * netdev_bonding_info_change - Dispatch event about slave change
7710 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007711 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007712 *
7713 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7714 * The caller must hold the RTNL lock.
7715 */
7716void netdev_bonding_info_change(struct net_device *dev,
7717 struct netdev_bonding_info *bonding_info)
7718{
David Ahern51d0c0472017-10-04 17:48:45 -07007719 struct netdev_notifier_bonding_info info = {
7720 .info.dev = dev,
7721 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007722
7723 memcpy(&info.bonding_info, bonding_info,
7724 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007725 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007726 &info.info);
7727}
7728EXPORT_SYMBOL(netdev_bonding_info_change);
7729
Maor Gottliebcff9f122020-04-30 22:21:31 +03007730/**
7731 * netdev_get_xmit_slave - Get the xmit slave of master device
Andrew Lunn88425002020-07-13 01:14:59 +02007732 * @dev: device
Maor Gottliebcff9f122020-04-30 22:21:31 +03007733 * @skb: The packet
7734 * @all_slaves: assume all the slaves are active
7735 *
7736 * The reference counters are not incremented so the caller must be
7737 * careful with locks. The caller must hold RCU lock.
7738 * %NULL is returned if no slave is found.
7739 */
7740
7741struct net_device *netdev_get_xmit_slave(struct net_device *dev,
7742 struct sk_buff *skb,
7743 bool all_slaves)
7744{
7745 const struct net_device_ops *ops = dev->netdev_ops;
7746
7747 if (!ops->ndo_get_xmit_slave)
7748 return NULL;
7749 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
7750}
7751EXPORT_SYMBOL(netdev_get_xmit_slave);
7752
Tariq Toukan719a4022021-01-17 16:59:42 +02007753static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
7754 struct sock *sk)
7755{
7756 const struct net_device_ops *ops = dev->netdev_ops;
7757
7758 if (!ops->ndo_sk_get_lower_dev)
7759 return NULL;
7760 return ops->ndo_sk_get_lower_dev(dev, sk);
7761}
7762
7763/**
7764 * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket
7765 * @dev: device
7766 * @sk: the socket
7767 *
7768 * %NULL is returned if no lower device is found.
7769 */
7770
7771struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
7772 struct sock *sk)
7773{
7774 struct net_device *lower;
7775
7776 lower = netdev_sk_get_lower_dev(dev, sk);
7777 while (lower) {
7778 dev = lower;
7779 lower = netdev_sk_get_lower_dev(dev, sk);
7780 }
7781
7782 return dev;
7783}
7784EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
7785
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007786static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007787{
7788 struct netdev_adjacent *iter;
7789
7790 struct net *net = dev_net(dev);
7791
7792 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007793 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007794 continue;
7795 netdev_adjacent_sysfs_add(iter->dev, dev,
7796 &iter->dev->adj_list.lower);
7797 netdev_adjacent_sysfs_add(dev, iter->dev,
7798 &dev->adj_list.upper);
7799 }
7800
7801 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007802 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007803 continue;
7804 netdev_adjacent_sysfs_add(iter->dev, dev,
7805 &iter->dev->adj_list.upper);
7806 netdev_adjacent_sysfs_add(dev, iter->dev,
7807 &dev->adj_list.lower);
7808 }
7809}
7810
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007811static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007812{
7813 struct netdev_adjacent *iter;
7814
7815 struct net *net = dev_net(dev);
7816
7817 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007818 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007819 continue;
7820 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7821 &iter->dev->adj_list.lower);
7822 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7823 &dev->adj_list.upper);
7824 }
7825
7826 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007827 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007828 continue;
7829 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7830 &iter->dev->adj_list.upper);
7831 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7832 &dev->adj_list.lower);
7833 }
7834}
7835
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007836void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007837{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007838 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007839
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007840 struct net *net = dev_net(dev);
7841
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007842 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007843 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007844 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007845 netdev_adjacent_sysfs_del(iter->dev, oldname,
7846 &iter->dev->adj_list.lower);
7847 netdev_adjacent_sysfs_add(iter->dev, dev,
7848 &iter->dev->adj_list.lower);
7849 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007850
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007851 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007852 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007853 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007854 netdev_adjacent_sysfs_del(iter->dev, oldname,
7855 &iter->dev->adj_list.upper);
7856 netdev_adjacent_sysfs_add(iter->dev, dev,
7857 &iter->dev->adj_list.upper);
7858 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007859}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007860
7861void *netdev_lower_dev_get_private(struct net_device *dev,
7862 struct net_device *lower_dev)
7863{
7864 struct netdev_adjacent *lower;
7865
7866 if (!lower_dev)
7867 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007868 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007869 if (!lower)
7870 return NULL;
7871
7872 return lower->private;
7873}
7874EXPORT_SYMBOL(netdev_lower_dev_get_private);
7875
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007876
Jiri Pirko04d48262015-12-03 12:12:15 +01007877/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01007878 * netdev_lower_state_changed - Dispatch event about lower device state change
Jiri Pirko04d48262015-12-03 12:12:15 +01007879 * @lower_dev: device
7880 * @lower_state_info: state to dispatch
7881 *
7882 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7883 * The caller must hold the RTNL lock.
7884 */
7885void netdev_lower_state_changed(struct net_device *lower_dev,
7886 void *lower_state_info)
7887{
David Ahern51d0c0472017-10-04 17:48:45 -07007888 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7889 .info.dev = lower_dev,
7890 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007891
7892 ASSERT_RTNL();
7893 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007894 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007895 &changelowerstate_info.info);
7896}
7897EXPORT_SYMBOL(netdev_lower_state_changed);
7898
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007899static void dev_change_rx_flags(struct net_device *dev, int flags)
7900{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007901 const struct net_device_ops *ops = dev->netdev_ops;
7902
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007903 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007904 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007905}
7906
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007907static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007908{
Eric Dumazetb536db92011-11-30 21:42:26 +00007909 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007910 kuid_t uid;
7911 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007912
Patrick McHardy24023452007-07-14 18:51:31 -07007913 ASSERT_RTNL();
7914
Wang Chendad9b332008-06-18 01:48:28 -07007915 dev->flags |= IFF_PROMISC;
7916 dev->promiscuity += inc;
7917 if (dev->promiscuity == 0) {
7918 /*
7919 * Avoid overflow.
7920 * If inc causes overflow, untouch promisc and return error.
7921 */
7922 if (inc < 0)
7923 dev->flags &= ~IFF_PROMISC;
7924 else {
7925 dev->promiscuity -= inc;
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07007926 netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n");
Wang Chendad9b332008-06-18 01:48:28 -07007927 return -EOVERFLOW;
7928 }
7929 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007930 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007931 pr_info("device %s %s promiscuous mode\n",
7932 dev->name,
7933 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007934 if (audit_enabled) {
7935 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007936 audit_log(audit_context(), GFP_ATOMIC,
7937 AUDIT_ANOM_PROMISCUOUS,
7938 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7939 dev->name, (dev->flags & IFF_PROMISC),
7940 (old_flags & IFF_PROMISC),
7941 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7942 from_kuid(&init_user_ns, uid),
7943 from_kgid(&init_user_ns, gid),
7944 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007945 }
Patrick McHardy24023452007-07-14 18:51:31 -07007946
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007947 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007948 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007949 if (notify)
7950 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007951 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007952}
7953
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954/**
7955 * dev_set_promiscuity - update promiscuity count on a device
7956 * @dev: device
7957 * @inc: modifier
7958 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007959 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960 * remains above zero the interface remains promiscuous. Once it hits zero
7961 * the device reverts back to normal filtering operation. A negative inc
7962 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007963 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 */
Wang Chendad9b332008-06-18 01:48:28 -07007965int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966{
Eric Dumazetb536db92011-11-30 21:42:26 +00007967 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007970 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007971 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007972 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007973 if (dev->flags != old_flags)
7974 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007977EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007979static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007981 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
Patrick McHardy24023452007-07-14 18:51:31 -07007983 ASSERT_RTNL();
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007986 dev->allmulti += inc;
7987 if (dev->allmulti == 0) {
7988 /*
7989 * Avoid overflow.
7990 * If inc causes overflow, untouch allmulti and return error.
7991 */
7992 if (inc < 0)
7993 dev->flags &= ~IFF_ALLMULTI;
7994 else {
7995 dev->allmulti -= inc;
Jesse Brandeburg5b92be62021-10-19 09:42:28 -07007996 netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n");
Wang Chendad9b332008-06-18 01:48:28 -07007997 return -EOVERFLOW;
7998 }
7999 }
Patrick McHardy24023452007-07-14 18:51:31 -07008000 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008001 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008002 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008003 if (notify)
8004 __dev_notify_flags(dev, old_flags,
8005 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008006 }
Wang Chendad9b332008-06-18 01:48:28 -07008007 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008008}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008009
8010/**
8011 * dev_set_allmulti - update allmulti count on a device
8012 * @dev: device
8013 * @inc: modifier
8014 *
8015 * Add or remove reception of all multicast frames to a device. While the
8016 * count in the device remains above zero the interface remains listening
8017 * to all interfaces. Once it hits zero the device reverts back to normal
8018 * filtering operation. A negative @inc value is used to drop the counter
8019 * when releasing a resource needing all multicasts.
8020 * Return 0 if successful or a negative errno code on error.
8021 */
8022
8023int dev_set_allmulti(struct net_device *dev, int inc)
8024{
8025 return __dev_set_allmulti(dev, inc, true);
8026}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008027EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008028
8029/*
8030 * Upload unicast and multicast address lists to device and
8031 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008032 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008033 * are present.
8034 */
8035void __dev_set_rx_mode(struct net_device *dev)
8036{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008037 const struct net_device_ops *ops = dev->netdev_ops;
8038
Patrick McHardy4417da62007-06-27 01:28:10 -07008039 /* dev_open will call this function so the list will stay sane. */
8040 if (!(dev->flags&IFF_UP))
8041 return;
8042
8043 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008044 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008045
Jiri Pirko01789342011-08-16 06:29:00 +00008046 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008047 /* Unicast addresses changes may only happen under the rtnl,
8048 * therefore calling __dev_set_promiscuity here is safe.
8049 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008050 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008051 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008052 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008053 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008054 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008055 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008056 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008057 }
Jiri Pirko01789342011-08-16 06:29:00 +00008058
8059 if (ops->ndo_set_rx_mode)
8060 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008061}
8062
8063void dev_set_rx_mode(struct net_device *dev)
8064{
David S. Millerb9e40852008-07-15 00:15:08 -07008065 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008066 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008067 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068}
8069
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008070/**
8071 * dev_get_flags - get flags reported to userspace
8072 * @dev: device
8073 *
8074 * Get the combination of flag bits exported through APIs to userspace.
8075 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008076unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077{
Eric Dumazet95c96172012-04-15 05:58:06 +00008078 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079
8080 flags = (dev->flags & ~(IFF_PROMISC |
8081 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008082 IFF_RUNNING |
8083 IFF_LOWER_UP |
8084 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085 (dev->gflags & (IFF_PROMISC |
8086 IFF_ALLMULTI));
8087
Stefan Rompfb00055a2006-03-20 17:09:11 -08008088 if (netif_running(dev)) {
8089 if (netif_oper_up(dev))
8090 flags |= IFF_RUNNING;
8091 if (netif_carrier_ok(dev))
8092 flags |= IFF_LOWER_UP;
8093 if (netif_dormant(dev))
8094 flags |= IFF_DORMANT;
8095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096
8097 return flags;
8098}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008099EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100
Petr Machata6d040322018-12-06 17:05:43 +00008101int __dev_change_flags(struct net_device *dev, unsigned int flags,
8102 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103{
Eric Dumazetb536db92011-11-30 21:42:26 +00008104 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106
Patrick McHardy24023452007-07-14 18:51:31 -07008107 ASSERT_RTNL();
8108
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 /*
8110 * Set the flags on our device.
8111 */
8112
8113 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8114 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8115 IFF_AUTOMEDIA)) |
8116 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8117 IFF_ALLMULTI));
8118
8119 /*
8120 * Load in the correct multicast list now the flags have changed.
8121 */
8122
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008123 if ((old_flags ^ flags) & IFF_MULTICAST)
8124 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008125
Patrick McHardy4417da62007-06-27 01:28:10 -07008126 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127
8128 /*
8129 * Have we downed the interface. We handle IFF_UP ourselves
8130 * according to user attempts to set it, rather than blindly
8131 * setting it.
8132 */
8133
8134 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008135 if ((old_flags ^ flags) & IFF_UP) {
8136 if (old_flags & IFF_UP)
8137 __dev_close(dev);
8138 else
Petr Machata40c900a2018-12-06 17:05:47 +00008139 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008143 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008144 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008145
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008147
8148 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8149 if (dev->flags != old_flags)
8150 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 }
8152
8153 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008154 * is important. Some (broken) drivers set IFF_PROMISC, when
8155 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 */
8157 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008158 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8159
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008161 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 }
8163
Patrick McHardybd380812010-02-26 06:34:53 +00008164 return ret;
8165}
8166
Nicolas Dichtela528c212013-09-25 12:02:44 +02008167void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8168 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008169{
8170 unsigned int changes = dev->flags ^ old_flags;
8171
Nicolas Dichtela528c212013-09-25 12:02:44 +02008172 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008173 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008174
Patrick McHardybd380812010-02-26 06:34:53 +00008175 if (changes & IFF_UP) {
8176 if (dev->flags & IFF_UP)
8177 call_netdevice_notifiers(NETDEV_UP, dev);
8178 else
8179 call_netdevice_notifiers(NETDEV_DOWN, dev);
8180 }
8181
8182 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008183 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008184 struct netdev_notifier_change_info change_info = {
8185 .info = {
8186 .dev = dev,
8187 },
8188 .flags_changed = changes,
8189 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008190
David Ahern51d0c0472017-10-04 17:48:45 -07008191 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008192 }
Patrick McHardybd380812010-02-26 06:34:53 +00008193}
8194
8195/**
8196 * dev_change_flags - change device settings
8197 * @dev: device
8198 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008199 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008200 *
8201 * Change settings on device based state flags. The flags are
8202 * in the userspace exported format.
8203 */
Petr Machata567c5e12018-12-06 17:05:42 +00008204int dev_change_flags(struct net_device *dev, unsigned int flags,
8205 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008206{
Eric Dumazetb536db92011-11-30 21:42:26 +00008207 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008208 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008209
Petr Machata6d040322018-12-06 17:05:43 +00008210 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008211 if (ret < 0)
8212 return ret;
8213
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008214 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008215 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 return ret;
8217}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008218EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
WANG Congf51048c2017-07-06 15:01:57 -07008220int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008221{
8222 const struct net_device_ops *ops = dev->netdev_ops;
8223
8224 if (ops->ndo_change_mtu)
8225 return ops->ndo_change_mtu(dev, new_mtu);
8226
Eric Dumazet501a90c2019-12-05 20:43:46 -08008227 /* Pairs with all the lockless reads of dev->mtu in the stack */
8228 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008229 return 0;
8230}
WANG Congf51048c2017-07-06 15:01:57 -07008231EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008232
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008233int dev_validate_mtu(struct net_device *dev, int new_mtu,
8234 struct netlink_ext_ack *extack)
8235{
8236 /* MTU must be positive, and in range */
8237 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8238 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8239 return -EINVAL;
8240 }
8241
8242 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8243 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8244 return -EINVAL;
8245 }
8246 return 0;
8247}
8248
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008249/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008250 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008251 * @dev: device
8252 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008253 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008254 *
8255 * Change the maximum transfer size of the network device.
8256 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008257int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8258 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008260 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261
8262 if (new_mtu == dev->mtu)
8263 return 0;
8264
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008265 err = dev_validate_mtu(dev, new_mtu, extack);
8266 if (err)
8267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268
8269 if (!netif_device_present(dev))
8270 return -ENODEV;
8271
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008272 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8273 err = notifier_to_errno(err);
8274 if (err)
8275 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008276
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008277 orig_mtu = dev->mtu;
8278 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008280 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008281 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8282 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008283 err = notifier_to_errno(err);
8284 if (err) {
8285 /* setting mtu back and notifying everyone again,
8286 * so that they have a chance to revert changes.
8287 */
8288 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008289 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8290 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008291 }
8292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 return err;
8294}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008295
8296int dev_set_mtu(struct net_device *dev, int new_mtu)
8297{
8298 struct netlink_ext_ack extack;
8299 int err;
8300
Li RongQinga6bcfc82018-08-03 15:45:21 +08008301 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008302 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008303 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008304 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8305 return err;
8306}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008307EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008309/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008310 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8311 * @dev: device
8312 * @new_len: new tx queue length
8313 */
8314int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8315{
8316 unsigned int orig_len = dev->tx_queue_len;
8317 int res;
8318
8319 if (new_len != (unsigned int)new_len)
8320 return -ERANGE;
8321
8322 if (new_len != orig_len) {
8323 dev->tx_queue_len = new_len;
8324 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8325 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008326 if (res)
8327 goto err_rollback;
8328 res = dev_qdisc_change_tx_queue_len(dev);
8329 if (res)
8330 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008331 }
8332
8333 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008334
8335err_rollback:
8336 netdev_err(dev, "refused to change device tx_queue_len\n");
8337 dev->tx_queue_len = orig_len;
8338 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008339}
8340
8341/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008342 * dev_set_group - Change group this device belongs to
8343 * @dev: device
8344 * @new_group: group this device should belong to
8345 */
8346void dev_set_group(struct net_device *dev, int new_group)
8347{
8348 dev->group = new_group;
8349}
8350EXPORT_SYMBOL(dev_set_group);
8351
8352/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008353 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8354 * @dev: device
8355 * @addr: new address
8356 * @extack: netlink extended ack
8357 */
8358int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8359 struct netlink_ext_ack *extack)
8360{
8361 struct netdev_notifier_pre_changeaddr_info info = {
8362 .info.dev = dev,
8363 .info.extack = extack,
8364 .dev_addr = addr,
8365 };
8366 int rc;
8367
8368 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8369 return notifier_to_errno(rc);
8370}
8371EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8372
8373/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008374 * dev_set_mac_address - Change Media Access Control Address
8375 * @dev: device
8376 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008377 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008378 *
8379 * Change the hardware (MAC) address of the device
8380 */
Petr Machata3a37a962018-12-13 11:54:30 +00008381int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8382 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008384 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 int err;
8386
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008387 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 return -EOPNOTSUPP;
8389 if (sa->sa_family != dev->type)
8390 return -EINVAL;
8391 if (!netif_device_present(dev))
8392 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008393 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8394 if (err)
8395 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008396 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008397 if (err)
8398 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008399 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008400 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008401 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008404EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405
Cong Wang3b23a322021-02-11 11:34:10 -08008406static DECLARE_RWSEM(dev_addr_sem);
8407
8408int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
8409 struct netlink_ext_ack *extack)
8410{
8411 int ret;
8412
8413 down_write(&dev_addr_sem);
8414 ret = dev_set_mac_address(dev, sa, extack);
8415 up_write(&dev_addr_sem);
8416 return ret;
8417}
8418EXPORT_SYMBOL(dev_set_mac_address_user);
8419
8420int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
8421{
8422 size_t size = sizeof(sa->sa_data);
8423 struct net_device *dev;
8424 int ret = 0;
8425
8426 down_read(&dev_addr_sem);
8427 rcu_read_lock();
8428
8429 dev = dev_get_by_name_rcu(net, dev_name);
8430 if (!dev) {
8431 ret = -ENODEV;
8432 goto unlock;
8433 }
8434 if (!dev->addr_len)
8435 memset(sa->sa_data, 0, size);
8436 else
8437 memcpy(sa->sa_data, dev->dev_addr,
8438 min_t(size_t, size, dev->addr_len));
8439 sa->sa_family = dev->type;
8440
8441unlock:
8442 rcu_read_unlock();
8443 up_read(&dev_addr_sem);
8444 return ret;
8445}
8446EXPORT_SYMBOL(dev_get_mac_address);
8447
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008448/**
8449 * dev_change_carrier - Change device carrier
8450 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008451 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008452 *
8453 * Change device carrier
8454 */
8455int dev_change_carrier(struct net_device *dev, bool new_carrier)
8456{
8457 const struct net_device_ops *ops = dev->netdev_ops;
8458
8459 if (!ops->ndo_change_carrier)
8460 return -EOPNOTSUPP;
8461 if (!netif_device_present(dev))
8462 return -ENODEV;
8463 return ops->ndo_change_carrier(dev, new_carrier);
8464}
8465EXPORT_SYMBOL(dev_change_carrier);
8466
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008468 * dev_get_phys_port_id - Get device physical port ID
8469 * @dev: device
8470 * @ppid: port ID
8471 *
8472 * Get device physical port ID
8473 */
8474int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008475 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008476{
8477 const struct net_device_ops *ops = dev->netdev_ops;
8478
8479 if (!ops->ndo_get_phys_port_id)
8480 return -EOPNOTSUPP;
8481 return ops->ndo_get_phys_port_id(dev, ppid);
8482}
8483EXPORT_SYMBOL(dev_get_phys_port_id);
8484
8485/**
David Aherndb24a902015-03-17 20:23:15 -06008486 * dev_get_phys_port_name - Get device physical port name
8487 * @dev: device
8488 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008489 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008490 *
8491 * Get device physical port name
8492 */
8493int dev_get_phys_port_name(struct net_device *dev,
8494 char *name, size_t len)
8495{
8496 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008497 int err;
David Aherndb24a902015-03-17 20:23:15 -06008498
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008499 if (ops->ndo_get_phys_port_name) {
8500 err = ops->ndo_get_phys_port_name(dev, name, len);
8501 if (err != -EOPNOTSUPP)
8502 return err;
8503 }
8504 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008505}
8506EXPORT_SYMBOL(dev_get_phys_port_name);
8507
8508/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008509 * dev_get_port_parent_id - Get the device's port parent identifier
8510 * @dev: network device
8511 * @ppid: pointer to a storage for the port's parent identifier
8512 * @recurse: allow/disallow recursion to lower devices
8513 *
8514 * Get the devices's port parent identifier
8515 */
8516int dev_get_port_parent_id(struct net_device *dev,
8517 struct netdev_phys_item_id *ppid,
8518 bool recurse)
8519{
8520 const struct net_device_ops *ops = dev->netdev_ops;
8521 struct netdev_phys_item_id first = { };
8522 struct net_device *lower_dev;
8523 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008524 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008525
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008526 if (ops->ndo_get_port_parent_id) {
8527 err = ops->ndo_get_port_parent_id(dev, ppid);
8528 if (err != -EOPNOTSUPP)
8529 return err;
8530 }
8531
8532 err = devlink_compat_switch_id_get(dev, ppid);
Antoine Tenartc0288ae2021-10-08 16:21:03 +02008533 if (!recurse || err != -EOPNOTSUPP)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008534 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008535
Florian Fainellid6abc5962019-02-06 09:45:35 -08008536 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Antoine Tenartc0288ae2021-10-08 16:21:03 +02008537 err = dev_get_port_parent_id(lower_dev, ppid, true);
Florian Fainellid6abc5962019-02-06 09:45:35 -08008538 if (err)
8539 break;
8540 if (!first.id_len)
8541 first = *ppid;
8542 else if (memcmp(&first, ppid, sizeof(*ppid)))
Ido Schimmele1b9efe2020-09-10 14:01:26 +03008543 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008544 }
8545
8546 return err;
8547}
8548EXPORT_SYMBOL(dev_get_port_parent_id);
8549
8550/**
8551 * netdev_port_same_parent_id - Indicate if two network devices have
8552 * the same port parent identifier
8553 * @a: first network device
8554 * @b: second network device
8555 */
8556bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8557{
8558 struct netdev_phys_item_id a_id = { };
8559 struct netdev_phys_item_id b_id = { };
8560
8561 if (dev_get_port_parent_id(a, &a_id, true) ||
8562 dev_get_port_parent_id(b, &b_id, true))
8563 return false;
8564
8565 return netdev_phys_item_id_same(&a_id, &b_id);
8566}
8567EXPORT_SYMBOL(netdev_port_same_parent_id);
8568
8569/**
Jakub Kicinski2106efd2021-11-22 17:24:47 -08008570 * dev_change_proto_down - set carrier according to proto_down.
8571 *
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008572 * @dev: device
8573 * @proto_down: new value
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008574 */
8575int dev_change_proto_down(struct net_device *dev, bool proto_down)
8576{
Jakub Kicinski2106efd2021-11-22 17:24:47 -08008577 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN))
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008578 return -EOPNOTSUPP;
8579 if (!netif_device_present(dev))
8580 return -ENODEV;
Andy Roulinb5899672019-02-22 18:06:36 +00008581 if (proto_down)
8582 netif_carrier_off(dev);
8583 else
8584 netif_carrier_on(dev);
8585 dev->proto_down = proto_down;
8586 return 0;
8587}
Jakub Kicinski2106efd2021-11-22 17:24:47 -08008588EXPORT_SYMBOL(dev_change_proto_down);
Andy Roulinb5899672019-02-22 18:06:36 +00008589
Roopa Prabhu829eb202020-07-31 17:34:01 -07008590/**
8591 * dev_change_proto_down_reason - proto down reason
8592 *
8593 * @dev: device
8594 * @mask: proto down mask
8595 * @value: proto down value
8596 */
8597void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8598 u32 value)
8599{
8600 int b;
8601
8602 if (!mask) {
8603 dev->proto_down_reason = value;
8604 } else {
8605 for_each_set_bit(b, &mask, 32) {
8606 if (value & (1 << b))
8607 dev->proto_down_reason |= BIT(b);
8608 else
8609 dev->proto_down_reason &= ~BIT(b);
8610 }
8611 }
8612}
8613EXPORT_SYMBOL(dev_change_proto_down_reason);
8614
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008615struct bpf_xdp_link {
8616 struct bpf_link link;
8617 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
8618 int flags;
8619};
8620
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008621static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008622{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008623 if (flags & XDP_FLAGS_HW_MODE)
8624 return XDP_MODE_HW;
8625 if (flags & XDP_FLAGS_DRV_MODE)
8626 return XDP_MODE_DRV;
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008627 if (flags & XDP_FLAGS_SKB_MODE)
8628 return XDP_MODE_SKB;
8629 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008630}
8631
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008632static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008633{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008634 switch (mode) {
8635 case XDP_MODE_SKB:
8636 return generic_xdp_install;
8637 case XDP_MODE_DRV:
8638 case XDP_MODE_HW:
8639 return dev->netdev_ops->ndo_bpf;
8640 default:
8641 return NULL;
Tom Rix5d867242020-11-01 07:36:47 -08008642 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008643}
8644
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008645static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
8646 enum bpf_xdp_mode mode)
8647{
8648 return dev->xdp_state[mode].link;
8649}
8650
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008651static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
8652 enum bpf_xdp_mode mode)
8653{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008654 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
8655
8656 if (link)
8657 return link->link.prog;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008658 return dev->xdp_state[mode].prog;
8659}
8660
Jussi Maki879af962021-07-31 05:57:33 +00008661u8 dev_xdp_prog_count(struct net_device *dev)
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008662{
8663 u8 count = 0;
8664 int i;
8665
8666 for (i = 0; i < __MAX_XDP_MODE; i++)
8667 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
8668 count++;
8669 return count;
8670}
Jussi Maki879af962021-07-31 05:57:33 +00008671EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008672
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008673u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
8674{
8675 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
8676
8677 return prog ? prog->aux->id : 0;
8678}
8679
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008680static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
8681 struct bpf_xdp_link *link)
8682{
8683 dev->xdp_state[mode].link = link;
8684 dev->xdp_state[mode].prog = NULL;
8685}
8686
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008687static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
8688 struct bpf_prog *prog)
8689{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008690 dev->xdp_state[mode].link = NULL;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008691 dev->xdp_state[mode].prog = prog;
8692}
8693
8694static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
8695 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
8696 u32 flags, struct bpf_prog *prog)
8697{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008698 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01008699 int err;
8700
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008701 memset(&xdp, 0, sizeof(xdp));
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008702 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008703 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07008704 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008705 xdp.prog = prog;
8706
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008707 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
8708 * "moved" into driver), so they don't increment it on their own, but
8709 * they do decrement refcnt when program is detached or replaced.
8710 * Given net_device also owns link/prog, we need to bump refcnt here
8711 * to prevent drivers from underflowing it.
8712 */
8713 if (prog)
8714 bpf_prog_inc(prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01008715 err = bpf_op(dev, &xdp);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008716 if (err) {
8717 if (prog)
8718 bpf_prog_put(prog);
8719 return err;
8720 }
Björn Töpel7e6897f2019-12-13 18:51:09 +01008721
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008722 if (mode != XDP_MODE_HW)
8723 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01008724
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008725 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008726}
8727
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008728static void dev_xdp_uninstall(struct net_device *dev)
8729{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008730 struct bpf_xdp_link *link;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008731 struct bpf_prog *prog;
8732 enum bpf_xdp_mode mode;
8733 bpf_op_t bpf_op;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008734
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008735 ASSERT_RTNL();
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008736
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008737 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
8738 prog = dev_xdp_prog(dev, mode);
8739 if (!prog)
8740 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008741
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008742 bpf_op = dev_xdp_bpf_op(dev, mode);
8743 if (!bpf_op)
8744 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008745
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008746 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
8747
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008748 /* auto-detach link from net device */
8749 link = dev_xdp_link(dev, mode);
8750 if (link)
8751 link->dev = NULL;
8752 else
8753 bpf_prog_put(prog);
8754
8755 dev_xdp_set_link(dev, mode, NULL);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008756 }
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008757}
8758
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008759static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008760 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
8761 struct bpf_prog *old_prog, u32 flags)
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008762{
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008763 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008764 struct bpf_prog *cur_prog;
Jussi Maki879af962021-07-31 05:57:33 +00008765 struct net_device *upper;
8766 struct list_head *iter;
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008767 enum bpf_xdp_mode mode;
8768 bpf_op_t bpf_op;
8769 int err;
8770
8771 ASSERT_RTNL();
8772
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008773 /* either link or prog attachment, never both */
8774 if (link && (new_prog || old_prog))
8775 return -EINVAL;
8776 /* link supports only XDP mode flags */
8777 if (link && (flags & ~XDP_FLAGS_MODES)) {
8778 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
8779 return -EINVAL;
8780 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008781 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
8782 if (num_modes > 1) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008783 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
8784 return -EINVAL;
8785 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008786 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
8787 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
8788 NL_SET_ERR_MSG(extack,
8789 "More than one program loaded, unset mode is ambiguous");
8790 return -EINVAL;
8791 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008792 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
8793 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
8794 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
8795 return -EINVAL;
8796 }
8797
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008798 mode = dev_xdp_mode(dev, flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008799 /* can't replace attached link */
8800 if (dev_xdp_link(dev, mode)) {
8801 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
8802 return -EBUSY;
8803 }
8804
Jussi Maki879af962021-07-31 05:57:33 +00008805 /* don't allow if an upper device already has a program */
8806 netdev_for_each_upper_dev_rcu(dev, upper, iter) {
8807 if (dev_xdp_prog_count(upper) > 0) {
8808 NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program");
8809 return -EEXIST;
8810 }
8811 }
8812
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008813 cur_prog = dev_xdp_prog(dev, mode);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008814 /* can't replace attached prog with link */
8815 if (link && cur_prog) {
8816 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
8817 return -EBUSY;
8818 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008819 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
8820 NL_SET_ERR_MSG(extack, "Active program does not match expected");
8821 return -EEXIST;
8822 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008823
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008824 /* put effective new program into new_prog */
8825 if (link)
8826 new_prog = link->link.prog;
8827
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008828 if (new_prog) {
8829 bool offload = mode == XDP_MODE_HW;
8830 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
8831 ? XDP_MODE_DRV : XDP_MODE_SKB;
8832
Andrii Nakryiko068d9d12020-08-11 19:29:23 -07008833 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
8834 NL_SET_ERR_MSG(extack, "XDP program already attached");
8835 return -EBUSY;
8836 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008837 if (!offload && dev_xdp_prog(dev, other_mode)) {
8838 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
8839 return -EEXIST;
8840 }
8841 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
8842 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
8843 return -EINVAL;
8844 }
8845 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
8846 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
8847 return -EINVAL;
8848 }
8849 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
8850 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
8851 return -EINVAL;
8852 }
8853 }
8854
8855 /* don't call drivers if the effective program didn't change */
8856 if (new_prog != cur_prog) {
8857 bpf_op = dev_xdp_bpf_op(dev, mode);
8858 if (!bpf_op) {
8859 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
8860 return -EOPNOTSUPP;
8861 }
8862
8863 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
8864 if (err)
8865 return err;
8866 }
8867
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008868 if (link)
8869 dev_xdp_set_link(dev, mode, link);
8870 else
8871 dev_xdp_set_prog(dev, mode, new_prog);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07008872 if (cur_prog)
8873 bpf_prog_put(cur_prog);
8874
8875 return 0;
8876}
8877
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008878static int dev_xdp_attach_link(struct net_device *dev,
8879 struct netlink_ext_ack *extack,
8880 struct bpf_xdp_link *link)
8881{
8882 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
8883}
8884
8885static int dev_xdp_detach_link(struct net_device *dev,
8886 struct netlink_ext_ack *extack,
8887 struct bpf_xdp_link *link)
8888{
8889 enum bpf_xdp_mode mode;
8890 bpf_op_t bpf_op;
8891
8892 ASSERT_RTNL();
8893
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008894 mode = dev_xdp_mode(dev, link->flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008895 if (dev_xdp_link(dev, mode) != link)
8896 return -EINVAL;
8897
8898 bpf_op = dev_xdp_bpf_op(dev, mode);
8899 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
8900 dev_xdp_set_link(dev, mode, NULL);
8901 return 0;
8902}
8903
8904static void bpf_xdp_link_release(struct bpf_link *link)
8905{
8906 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8907
8908 rtnl_lock();
8909
8910 /* if racing with net_device's tear down, xdp_link->dev might be
8911 * already NULL, in which case link was already auto-detached
8912 */
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07008913 if (xdp_link->dev) {
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008914 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07008915 xdp_link->dev = NULL;
8916 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008917
8918 rtnl_unlock();
8919}
8920
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07008921static int bpf_xdp_link_detach(struct bpf_link *link)
8922{
8923 bpf_xdp_link_release(link);
8924 return 0;
8925}
8926
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008927static void bpf_xdp_link_dealloc(struct bpf_link *link)
8928{
8929 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8930
8931 kfree(xdp_link);
8932}
8933
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07008934static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
8935 struct seq_file *seq)
8936{
8937 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8938 u32 ifindex = 0;
8939
8940 rtnl_lock();
8941 if (xdp_link->dev)
8942 ifindex = xdp_link->dev->ifindex;
8943 rtnl_unlock();
8944
8945 seq_printf(seq, "ifindex:\t%u\n", ifindex);
8946}
8947
8948static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
8949 struct bpf_link_info *info)
8950{
8951 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8952 u32 ifindex = 0;
8953
8954 rtnl_lock();
8955 if (xdp_link->dev)
8956 ifindex = xdp_link->dev->ifindex;
8957 rtnl_unlock();
8958
8959 info->xdp.ifindex = ifindex;
8960 return 0;
8961}
8962
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07008963static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
8964 struct bpf_prog *old_prog)
8965{
8966 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8967 enum bpf_xdp_mode mode;
8968 bpf_op_t bpf_op;
8969 int err = 0;
8970
8971 rtnl_lock();
8972
8973 /* link might have been auto-released already, so fail */
8974 if (!xdp_link->dev) {
8975 err = -ENOLINK;
8976 goto out_unlock;
8977 }
8978
8979 if (old_prog && link->prog != old_prog) {
8980 err = -EPERM;
8981 goto out_unlock;
8982 }
8983 old_prog = link->prog;
Toke Høiland-Jørgensen382778e2022-01-07 23:11:13 +01008984 if (old_prog->type != new_prog->type ||
8985 old_prog->expected_attach_type != new_prog->expected_attach_type) {
8986 err = -EINVAL;
8987 goto out_unlock;
8988 }
8989
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07008990 if (old_prog == new_prog) {
8991 /* no-op, don't disturb drivers */
8992 bpf_prog_put(new_prog);
8993 goto out_unlock;
8994 }
8995
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008996 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07008997 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
8998 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
8999 xdp_link->flags, new_prog);
9000 if (err)
9001 goto out_unlock;
9002
9003 old_prog = xchg(&link->prog, new_prog);
9004 bpf_prog_put(old_prog);
9005
9006out_unlock:
9007 rtnl_unlock();
9008 return err;
9009}
9010
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009011static const struct bpf_link_ops bpf_xdp_link_lops = {
9012 .release = bpf_xdp_link_release,
9013 .dealloc = bpf_xdp_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009014 .detach = bpf_xdp_link_detach,
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009015 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9016 .fill_link_info = bpf_xdp_link_fill_link_info,
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009017 .update_prog = bpf_xdp_link_update,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009018};
9019
9020int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9021{
9022 struct net *net = current->nsproxy->net_ns;
9023 struct bpf_link_primer link_primer;
9024 struct bpf_xdp_link *link;
9025 struct net_device *dev;
9026 int err, fd;
9027
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009028 rtnl_lock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009029 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009030 if (!dev) {
9031 rtnl_unlock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009032 return -EINVAL;
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009033 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009034
9035 link = kzalloc(sizeof(*link), GFP_USER);
9036 if (!link) {
9037 err = -ENOMEM;
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009038 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009039 }
9040
9041 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9042 link->dev = dev;
9043 link->flags = attr->link_create.flags;
9044
9045 err = bpf_link_prime(&link->link, &link_primer);
9046 if (err) {
9047 kfree(link);
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009048 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009049 }
9050
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009051 err = dev_xdp_attach_link(dev, NULL, link);
9052 rtnl_unlock();
9053
9054 if (err) {
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009055 link->dev = NULL;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009056 bpf_link_cleanup(&link_primer);
9057 goto out_put_dev;
9058 }
9059
9060 fd = bpf_link_settle(&link_primer);
9061 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9062 dev_put(dev);
9063 return fd;
9064
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009065unlock:
9066 rtnl_unlock();
9067
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009068out_put_dev:
9069 dev_put(dev);
9070 return err;
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009071}
9072
9073/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07009074 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9075 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07009076 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07009077 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009078 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01009079 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07009080 *
9081 * Set or clear a bpf program for a device
9082 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07009083int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009084 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07009085{
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009086 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009087 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07009088 int err;
9089
Daniel Borkmann85de8572016-11-28 23:16:54 +01009090 ASSERT_RTNL();
9091
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009092 if (fd >= 0) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009093 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9094 mode != XDP_MODE_SKB);
9095 if (IS_ERR(new_prog))
9096 return PTR_ERR(new_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009097 }
9098
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009099 if (expected_fd >= 0) {
9100 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9101 mode != XDP_MODE_SKB);
9102 if (IS_ERR(old_prog)) {
9103 err = PTR_ERR(old_prog);
9104 old_prog = NULL;
9105 goto err_out;
9106 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009107 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07009108
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009109 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009110
9111err_out:
9112 if (err && new_prog)
9113 bpf_prog_put(new_prog);
9114 if (old_prog)
9115 bpf_prog_put(old_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009116 return err;
9117}
Brenden Blancoa7862b42016-07-19 12:16:48 -07009118
9119/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07009121 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122 *
9123 * Returns a suitable unique value for a new device interface
9124 * number. The caller must hold the rtnl semaphore or the
9125 * dev_base_lock to be sure it remains unique.
9126 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07009127static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009129 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11009130
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 for (;;) {
9132 if (++ifindex <= 0)
9133 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009134 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009135 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 }
9137}
9138
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08009140static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07009141DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
Stephen Hemminger6f05f622007-03-08 20:46:03 -08009143static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009146 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147}
9148
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009149static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9150 struct net_device *upper, netdev_features_t features)
9151{
9152 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9153 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009154 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009155
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009156 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009157 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009158 if (!(upper->wanted_features & feature)
9159 && (features & feature)) {
9160 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9161 &feature, upper->name);
9162 features &= ~feature;
9163 }
9164 }
9165
9166 return features;
9167}
9168
9169static void netdev_sync_lower_features(struct net_device *upper,
9170 struct net_device *lower, netdev_features_t features)
9171{
9172 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9173 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009174 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009175
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009176 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009177 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009178 if (!(features & feature) && (lower->features & feature)) {
9179 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9180 &feature, lower->name);
9181 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07009182 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009183
9184 if (unlikely(lower->features & feature))
9185 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9186 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07009187 else
9188 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009189 }
9190 }
9191}
9192
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009193static netdev_features_t netdev_fix_features(struct net_device *dev,
9194 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07009195{
Michał Mirosław57422dc2011-01-22 12:14:12 +00009196 /* Fix illegal checksum combinations */
9197 if ((features & NETIF_F_HW_CSUM) &&
9198 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009199 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00009200 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9201 }
9202
Herbert Xub63365a2008-10-23 01:11:29 -07009203 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009204 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009205 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009206 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07009207 }
9208
Pravin B Shelarec5f0612013-03-07 09:28:01 +00009209 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9210 !(features & NETIF_F_IP_CSUM)) {
9211 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9212 features &= ~NETIF_F_TSO;
9213 features &= ~NETIF_F_TSO_ECN;
9214 }
9215
9216 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9217 !(features & NETIF_F_IPV6_CSUM)) {
9218 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9219 features &= ~NETIF_F_TSO6;
9220 }
9221
Alexander Duyckb1dc4972016-05-02 09:38:24 -07009222 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9223 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9224 features &= ~NETIF_F_TSO_MANGLEID;
9225
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00009226 /* TSO ECN requires that TSO is present as well. */
9227 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9228 features &= ~NETIF_F_TSO_ECN;
9229
Michał Mirosław212b5732011-02-15 16:59:16 +00009230 /* Software GSO depends on SG. */
9231 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009232 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00009233 features &= ~NETIF_F_GSO;
9234 }
9235
Alexander Duyck802ab552016-04-10 21:45:03 -04009236 /* GSO partial features require GSO partial be set */
9237 if ((features & dev->gso_partial_features) &&
9238 !(features & NETIF_F_GSO_PARTIAL)) {
9239 netdev_dbg(dev,
9240 "Dropping partially supported GSO features since no GSO partial.\n");
9241 features &= ~dev->gso_partial_features;
9242 }
9243
Michael Chanfb1f5f72017-12-16 03:09:40 -05009244 if (!(features & NETIF_F_RXCSUM)) {
9245 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9246 * successfully merged by hardware must also have the
9247 * checksum verified by hardware. If the user does not
9248 * want to enable RXCSUM, logically, we should disable GRO_HW.
9249 */
9250 if (features & NETIF_F_GRO_HW) {
9251 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9252 features &= ~NETIF_F_GRO_HW;
9253 }
9254 }
9255
Gal Pressmande8d5ab2018-03-12 11:48:49 +02009256 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9257 if (features & NETIF_F_RXFCS) {
9258 if (features & NETIF_F_LRO) {
9259 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9260 features &= ~NETIF_F_LRO;
9261 }
9262
9263 if (features & NETIF_F_GRO_HW) {
9264 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9265 features &= ~NETIF_F_GRO_HW;
9266 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02009267 }
9268
Ben Ben-ishay54b2b3e2020-12-16 12:32:24 +02009269 if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) {
9270 netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n");
9271 features &= ~NETIF_F_LRO;
9272 }
9273
Tariq Toukan25537d72021-01-14 17:12:15 +02009274 if (features & NETIF_F_HW_TLS_TX) {
9275 bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) ==
9276 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
9277 bool hw_csum = features & NETIF_F_HW_CSUM;
9278
9279 if (!ip_csum && !hw_csum) {
9280 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
9281 features &= ~NETIF_F_HW_TLS_TX;
9282 }
Tariq Toukanae0b04b2020-12-13 16:39:29 +02009283 }
9284
Tariq Toukana3eb4e92021-01-17 17:15:38 +02009285 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9286 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9287 features &= ~NETIF_F_HW_TLS_RX;
9288 }
9289
Herbert Xub63365a2008-10-23 01:11:29 -07009290 return features;
9291}
Herbert Xub63365a2008-10-23 01:11:29 -07009292
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009293int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009294{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009295 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009296 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009297 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009298 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009299
Michał Mirosław87267482011-04-12 09:56:38 +00009300 ASSERT_RTNL();
9301
Michał Mirosław5455c692011-02-15 16:59:17 +00009302 features = netdev_get_wanted_features(dev);
9303
9304 if (dev->netdev_ops->ndo_fix_features)
9305 features = dev->netdev_ops->ndo_fix_features(dev, features);
9306
9307 /* driver might be less strict about feature dependencies */
9308 features = netdev_fix_features(dev, features);
9309
Randy Dunlap4250b752020-09-17 21:35:15 -07009310 /* some features can't be enabled if they're off on an upper device */
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009311 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9312 features = netdev_sync_upper_features(dev, upper, features);
9313
Michał Mirosław5455c692011-02-15 16:59:17 +00009314 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009315 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009316
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009317 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9318 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009319
9320 if (dev->netdev_ops->ndo_set_features)
9321 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009322 else
9323 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009324
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009325 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009326 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009327 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9328 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009329 /* return non-0 since some features might have changed and
9330 * it's better to fire a spurious notification than miss it
9331 */
9332 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009333 }
9334
Jarod Wilsone7868a82015-11-03 23:09:32 -05009335sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009336 /* some features must be disabled on lower devices when disabled
9337 * on an upper device (think: bonding master or bridge)
9338 */
9339 netdev_for_each_lower_dev(dev, lower, iter)
9340 netdev_sync_lower_features(dev, lower, features);
9341
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009342 if (!err) {
9343 netdev_features_t diff = features ^ dev->features;
9344
9345 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9346 /* udp_tunnel_{get,drop}_rx_info both need
9347 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9348 * device, or they won't do anything.
9349 * Thus we need to update dev->features
9350 * *before* calling udp_tunnel_get_rx_info,
9351 * but *after* calling udp_tunnel_drop_rx_info.
9352 */
9353 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9354 dev->features = features;
9355 udp_tunnel_get_rx_info(dev);
9356 } else {
9357 udp_tunnel_drop_rx_info(dev);
9358 }
9359 }
9360
Gal Pressman9daae9b2018-03-28 17:46:54 +03009361 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9362 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9363 dev->features = features;
9364 err |= vlan_get_rx_ctag_filter_info(dev);
9365 } else {
9366 vlan_drop_rx_ctag_filter_info(dev);
9367 }
9368 }
9369
9370 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9371 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9372 dev->features = features;
9373 err |= vlan_get_rx_stag_filter_info(dev);
9374 } else {
9375 vlan_drop_rx_stag_filter_info(dev);
9376 }
9377 }
9378
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009379 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009380 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009381
Jarod Wilsone7868a82015-11-03 23:09:32 -05009382 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009383}
9384
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009385/**
9386 * netdev_update_features - recalculate device features
9387 * @dev: the device to check
9388 *
9389 * Recalculate dev->features set and send notifications if it
9390 * has changed. Should be called after driver or hardware dependent
9391 * conditions might have changed that influence the features.
9392 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009393void netdev_update_features(struct net_device *dev)
9394{
9395 if (__netdev_update_features(dev))
9396 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009397}
9398EXPORT_SYMBOL(netdev_update_features);
9399
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009401 * netdev_change_features - recalculate device features
9402 * @dev: the device to check
9403 *
9404 * Recalculate dev->features set and send notifications even
9405 * if they have not changed. Should be called instead of
9406 * netdev_update_features() if also dev->vlan_features might
9407 * have changed to allow the changes to be propagated to stacked
9408 * VLAN devices.
9409 */
9410void netdev_change_features(struct net_device *dev)
9411{
9412 __netdev_update_features(dev);
9413 netdev_features_change(dev);
9414}
9415EXPORT_SYMBOL(netdev_change_features);
9416
9417/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009418 * netif_stacked_transfer_operstate - transfer operstate
9419 * @rootdev: the root or lower level device to transfer state from
9420 * @dev: the device to transfer operstate to
9421 *
9422 * Transfer operational state from root to device. This is normally
9423 * called when a stacking relationship exists between the root
9424 * device and the device(a leaf device).
9425 */
9426void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9427 struct net_device *dev)
9428{
9429 if (rootdev->operstate == IF_OPER_DORMANT)
9430 netif_dormant_on(dev);
9431 else
9432 netif_dormant_off(dev);
9433
Andrew Lunneec517cd2020-04-20 00:11:50 +02009434 if (rootdev->operstate == IF_OPER_TESTING)
9435 netif_testing_on(dev);
9436 else
9437 netif_testing_off(dev);
9438
Zhang Shengju0575c862017-04-26 17:49:38 +08009439 if (netif_carrier_ok(rootdev))
9440 netif_carrier_on(dev);
9441 else
9442 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009443}
9444EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9445
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009446static int netif_alloc_rx_queues(struct net_device *dev)
9447{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009448 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009449 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309450 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009451 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009452
Tom Herbertbd25fa72010-10-18 18:00:16 +00009453 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009454
Vasily Averinc948f512021-07-19 13:44:23 +03009455 rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009456 if (!rx)
9457 return -ENOMEM;
9458
Tom Herbertbd25fa72010-10-18 18:00:16 +00009459 dev->_rx = rx;
9460
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009461 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009462 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009463
9464 /* XDP RX-queue setup */
Björn Töpelb02e5a02020-11-30 19:52:01 +01009465 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009466 if (err < 0)
9467 goto err_rxq_info;
9468 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009469 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009470
9471err_rxq_info:
9472 /* Rollback successful reg's and free other resources */
9473 while (i--)
9474 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009475 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009476 dev->_rx = NULL;
9477 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009478}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009479
9480static void netif_free_rx_queues(struct net_device *dev)
9481{
9482 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009483
9484 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9485 if (!dev->_rx)
9486 return;
9487
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009488 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009489 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9490
9491 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009492}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009493
Changli Gaoaa942102010-12-04 02:31:41 +00009494static void netdev_init_one_queue(struct net_device *dev,
9495 struct netdev_queue *queue, void *_unused)
9496{
9497 /* Initialize queue lock */
9498 spin_lock_init(&queue->_xmit_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07009499 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
Changli Gaoaa942102010-12-04 02:31:41 +00009500 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009501 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009502 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009503#ifdef CONFIG_BQL
9504 dql_init(&queue->dql, HZ);
9505#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009506}
9507
Eric Dumazet60877a32013-06-20 01:15:51 -07009508static void netif_free_tx_queues(struct net_device *dev)
9509{
WANG Cong4cb28972014-06-02 15:55:22 -07009510 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009511}
9512
Tom Herberte6484932010-10-18 18:04:39 +00009513static int netif_alloc_netdev_queues(struct net_device *dev)
9514{
9515 unsigned int count = dev->num_tx_queues;
9516 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009517 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009518
Eric Dumazetd3397272015-07-06 17:13:26 +02009519 if (count < 1 || count > 0xffff)
9520 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009521
Vasily Averinc948f512021-07-19 13:44:23 +03009522 tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009523 if (!tx)
9524 return -ENOMEM;
9525
Tom Herberte6484932010-10-18 18:04:39 +00009526 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009527
Tom Herberte6484932010-10-18 18:04:39 +00009528 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9529 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009530
9531 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009532}
9533
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009534void netif_tx_stop_all_queues(struct net_device *dev)
9535{
9536 unsigned int i;
9537
9538 for (i = 0; i < dev->num_tx_queues; i++) {
9539 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009540
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009541 netif_tx_stop_queue(txq);
9542 }
9543}
9544EXPORT_SYMBOL(netif_tx_stop_all_queues);
9545
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009546/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 * register_netdevice - register a network device
9548 * @dev: device to register
9549 *
9550 * Take a completed network device structure and add it to the kernel
9551 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9552 * chain. 0 is returned on success. A negative errno code is returned
9553 * on a failure to set up the device, or if the name is a duplicate.
9554 *
9555 * Callers must hold the rtnl semaphore. You may want
9556 * register_netdev() instead of this.
9557 *
9558 * BUGS:
9559 * The locking appears insufficient to guarantee two parallel registers
9560 * will not get the same name.
9561 */
9562
9563int register_netdevice(struct net_device *dev)
9564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009566 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567
Florian Fainellie283de32018-04-30 14:20:05 -07009568 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9569 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 BUG_ON(dev_boot_phase);
9571 ASSERT_RTNL();
9572
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009573 might_sleep();
9574
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575 /* When net_device's are persistent, this will be fatal. */
9576 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009577 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009578
Jakub Kicinski9000edb2020-03-16 13:47:12 -07009579 ret = ethtool_check_ops(dev->ethtool_ops);
9580 if (ret)
9581 return ret;
9582
David S. Millerf1f28aa2008-07-15 00:08:33 -07009583 spin_lock_init(&dev->addr_list_lock);
Cong Wang845e0eb2020-06-08 14:53:01 -07009584 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585
Gao feng828de4f2012-09-13 20:58:27 +00009586 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009587 if (ret < 0)
9588 goto out;
9589
Eric Dumazet9077f052019-10-03 08:59:24 -07009590 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009591 dev->name_node = netdev_name_node_head_alloc(dev);
9592 if (!dev->name_node)
9593 goto out;
9594
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009596 if (dev->netdev_ops->ndo_init) {
9597 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598 if (ret) {
9599 if (ret > 0)
9600 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009601 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602 }
9603 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009604
Patrick McHardyf6469682013-04-19 02:04:27 +00009605 if (((dev->hw_features | dev->features) &
9606 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009607 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9608 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9609 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9610 ret = -EINVAL;
9611 goto err_uninit;
9612 }
9613
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009614 ret = -EBUSY;
9615 if (!dev->ifindex)
9616 dev->ifindex = dev_new_index(net);
9617 else if (__dev_get_by_index(net, dev->ifindex))
9618 goto err_uninit;
9619
Michał Mirosław5455c692011-02-15 16:59:17 +00009620 /* Transfer changeable features to wanted_features and enable
9621 * software offloads (GSO and GRO).
9622 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +01009623 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +00009624 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02009625
Jakub Kicinski876c4382021-01-06 13:06:34 -08009626 if (dev->udp_tunnel_nic_info) {
Sabrina Dubrocad764a122017-07-21 12:49:28 +02009627 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9628 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9629 }
9630
Michał Mirosław14d12322011-02-22 16:52:28 +00009631 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009633 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00009634 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009635
Alexander Duyck7f348a62016-04-20 16:51:00 -04009636 /* If IPv4 TCP segmentation offload is supported we should also
9637 * allow the device to enable segmenting the frame with the option
9638 * of ignoring a static IP ID value. This doesn't enable the
9639 * feature itself but allows the user to enable it later.
9640 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009641 if (dev->hw_features & NETIF_F_TSO)
9642 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04009643 if (dev->vlan_features & NETIF_F_TSO)
9644 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9645 if (dev->mpls_features & NETIF_F_TSO)
9646 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9647 if (dev->hw_enc_features & NETIF_F_TSO)
9648 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07009649
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009650 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00009651 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009652 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00009653
Pravin B Shelaree579672013-03-07 09:28:08 +00009654 /* Make NETIF_F_SG inheritable to tunnel devices.
9655 */
Alexander Duyck802ab552016-04-10 21:45:03 -04009656 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00009657
Simon Horman0d89d202013-05-23 21:02:52 +00009658 /* Make NETIF_F_SG inheritable to MPLS.
9659 */
9660 dev->mpls_features |= NETIF_F_SG;
9661
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00009662 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9663 ret = notifier_to_errno(ret);
9664 if (ret)
9665 goto err_uninit;
9666
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009667 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009668 if (ret) {
9669 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009670 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009671 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009672 dev->reg_state = NETREG_REGISTERED;
9673
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009674 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00009675
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 /*
9677 * Default initial state at registry is that the
9678 * device is present.
9679 */
9680
9681 set_bit(__LINK_STATE_PRESENT, &dev->state);
9682
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01009683 linkwatch_init_dev(dev);
9684
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009687 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04009688 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689
Jiri Pirko948b3372013-01-08 01:38:25 +00009690 /* If the device has permanent device address, driver should
9691 * set dev_addr and also addr_assign_type should be set to
9692 * NET_ADDR_PERM (default value).
9693 */
9694 if (dev->addr_assign_type == NET_ADDR_PERM)
9695 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9696
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009698 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07009699 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009700 if (ret) {
Jakub Kicinski766b0512021-01-06 10:40:07 -08009701 /* Expect explicit free_netdev() on failure */
9702 dev->needs_free_netdev = false;
Jakub Kicinski037e56b2021-01-19 12:25:19 -08009703 unregister_netdevice_queue(dev, NULL);
Jakub Kicinski766b0512021-01-06 10:40:07 -08009704 goto out;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009705 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009706 /*
9707 * Prevent userspace races by waiting until the network
9708 * device is fully setup before sending notifications.
9709 */
Patrick McHardya2835762010-02-26 06:34:51 +00009710 if (!dev->rtnl_link_ops ||
9711 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009712 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713
9714out:
9715 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009716
9717err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009718 if (dev->netdev_ops->ndo_uninit)
9719 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04009720 if (dev->priv_destructor)
9721 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009722err_free_name:
9723 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009724 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009726EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727
9728/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009729 * init_dummy_netdev - init a dummy network device for NAPI
9730 * @dev: device to init
9731 *
9732 * This takes a network device structure and initialize the minimum
9733 * amount of fields so it can be used to schedule NAPI polls without
9734 * registering a full blown interface. This is to be used by drivers
9735 * that need to tie several hardware interfaces to a single NAPI
9736 * poll scheduler due to HW limitations.
9737 */
9738int init_dummy_netdev(struct net_device *dev)
9739{
9740 /* Clear everything. Note we don't initialize spinlocks
9741 * are they aren't supposed to be taken by any of the
9742 * NAPI code and this dummy netdev is supposed to be
9743 * only ever used for NAPI polls
9744 */
9745 memset(dev, 0, sizeof(struct net_device));
9746
9747 /* make sure we BUG if trying to hit standard
9748 * register/unregister code path
9749 */
9750 dev->reg_state = NETREG_DUMMY;
9751
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009752 /* NAPI wants this */
9753 INIT_LIST_HEAD(&dev->napi_list);
9754
9755 /* a dummy interface is started by default */
9756 set_bit(__LINK_STATE_PRESENT, &dev->state);
9757 set_bit(__LINK_STATE_START, &dev->state);
9758
Josh Elsasser35edfdc2019-01-26 14:38:33 -08009759 /* napi_busy_loop stats accounting wants this */
9760 dev_net_set(dev, &init_net);
9761
Eric Dumazet29b44332010-10-11 10:22:12 +00009762 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9763 * because users of this 'device' dont need to change
9764 * its refcount.
9765 */
9766
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009767 return 0;
9768}
9769EXPORT_SYMBOL_GPL(init_dummy_netdev);
9770
9771
9772/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 * register_netdev - register a network device
9774 * @dev: device to register
9775 *
9776 * Take a completed network device structure and add it to the kernel
9777 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9778 * chain. 0 is returned on success. A negative errno code is returned
9779 * on a failure to set up the device, or if the name is a duplicate.
9780 *
Borislav Petkov38b4da382007-04-20 22:14:10 -07009781 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 * and expands the device name if you passed a format string to
9783 * alloc_netdev.
9784 */
9785int register_netdev(struct net_device *dev)
9786{
9787 int err;
9788
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03009789 if (rtnl_lock_killable())
9790 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 rtnl_unlock();
9793 return err;
9794}
9795EXPORT_SYMBOL(register_netdev);
9796
Eric Dumazet29b44332010-10-11 10:22:12 +00009797int netdev_refcnt_read(const struct net_device *dev)
9798{
Eric Dumazet919067c2021-03-19 10:39:33 -07009799#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +00009800 int i, refcnt = 0;
9801
9802 for_each_possible_cpu(i)
9803 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9804 return refcnt;
Eric Dumazet919067c2021-03-19 10:39:33 -07009805#else
9806 return refcount_read(&dev->dev_refcnt);
9807#endif
Eric Dumazet29b44332010-10-11 10:22:12 +00009808}
9809EXPORT_SYMBOL(netdev_refcnt_read);
9810
Dmitry Vyukov5aa3afe2021-03-23 07:49:23 +01009811int netdev_unregister_timeout_secs __read_mostly = 10;
9812
Mauro Carvalho Chehabde2b5412020-09-22 13:22:52 +02009813#define WAIT_REFS_MIN_MSECS 1
9814#define WAIT_REFS_MAX_MSECS 250
Ben Hutchings2c530402012-07-10 10:55:09 +00009815/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00009817 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 *
9819 * This is called when unregistering network devices.
9820 *
9821 * Any protocol or device that holds a reference should register
9822 * for netdevice notification, and cleanup and put back the
9823 * reference if they receive an UNREGISTER event.
9824 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009825 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 */
9827static void netdev_wait_allrefs(struct net_device *dev)
9828{
9829 unsigned long rebroadcast_time, warning_time;
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -07009830 int wait = 0, refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831
Eric Dumazete014deb2009-11-17 05:59:21 +00009832 linkwatch_forget_dev(dev);
9833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00009835 refcnt = netdev_refcnt_read(dev);
9836
Eric Dumazetadd2d732021-03-22 11:21:45 -07009837 while (refcnt != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009839 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840
9841 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009842 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
Eric Dumazet748e2d92012-08-22 21:50:59 +00009844 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009845 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00009846 rtnl_lock();
9847
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9849 &dev->state)) {
9850 /* We must not have linkwatch events
9851 * pending on unregister. If this
9852 * happens, we simply run the queue
9853 * unscheduled, resulting in a noop
9854 * for this device.
9855 */
9856 linkwatch_run_queue();
9857 }
9858
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009859 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860
9861 rebroadcast_time = jiffies;
9862 }
9863
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -07009864 if (!wait) {
9865 rcu_barrier();
9866 wait = WAIT_REFS_MIN_MSECS;
9867 } else {
9868 msleep(wait);
9869 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
9870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871
Eric Dumazet29b44332010-10-11 10:22:12 +00009872 refcnt = netdev_refcnt_read(dev);
9873
Dmitry Vyukov6c996e12021-03-25 15:52:45 +01009874 if (refcnt != 1 &&
Dmitry Vyukov5aa3afe2021-03-23 07:49:23 +01009875 time_after(jiffies, warning_time +
9876 netdev_unregister_timeout_secs * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009877 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9878 dev->name, refcnt);
Eric Dumazet4d92b952021-12-04 20:21:57 -08009879 ref_tracker_dir_print(&dev->refcnt_tracker, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009880 warning_time = jiffies;
9881 }
9882 }
9883}
9884
9885/* The sequence is:
9886 *
9887 * rtnl_lock();
9888 * ...
9889 * register_netdevice(x1);
9890 * register_netdevice(x2);
9891 * ...
9892 * unregister_netdevice(y1);
9893 * unregister_netdevice(y2);
9894 * ...
9895 * rtnl_unlock();
9896 * free_netdev(y1);
9897 * free_netdev(y2);
9898 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07009899 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009901 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 * without deadlocking with linkwatch via keventd.
9903 * 2) Since we run with the RTNL semaphore not held, we can sleep
9904 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07009905 *
9906 * We must not return until all unregister events added during
9907 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909void netdev_run_todo(void)
9910{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009911 struct list_head list;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00009912#ifdef CONFIG_LOCKDEP
9913 struct list_head unlink_list;
9914
9915 list_replace_init(&net_unlink_list, &unlink_list);
9916
9917 while (!list_empty(&unlink_list)) {
9918 struct net_device *dev = list_first_entry(&unlink_list,
9919 struct net_device,
9920 unlink_list);
Taehee Yoo0e8b8d62020-10-15 16:26:06 +00009921 list_del_init(&dev->unlink_list);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00009922 dev->nested_level = dev->lower_level - 1;
9923 }
9924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009927 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07009928
9929 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009930
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009931
9932 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00009933 if (!list_empty(&list))
9934 rcu_barrier();
9935
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936 while (!list_empty(&list)) {
9937 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00009938 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 list_del(&dev->todo_list);
9940
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009941 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009942 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009944 dump_stack();
9945 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009947
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009948 dev->reg_state = NETREG_UNREGISTERED;
9949
9950 netdev_wait_allrefs(dev);
9951
9952 /* paranoia */
Eric Dumazetadd2d732021-03-22 11:21:45 -07009953 BUG_ON(netdev_refcnt_read(dev) != 1);
Salam Noureddine7866a622015-01-27 11:35:48 -08009954 BUG_ON(!list_empty(&dev->ptype_all));
9955 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00009956 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9957 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08009958#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07009959 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08009960#endif
David S. Millercf124db2017-05-08 12:52:56 -04009961 if (dev->priv_destructor)
9962 dev->priv_destructor(dev);
9963 if (dev->needs_free_netdev)
9964 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009965
Eric W. Biederman50624c92013-09-23 21:19:49 -07009966 /* Report a network device has been unregistered */
9967 rtnl_lock();
9968 dev_net(dev)->dev_unreg_count--;
9969 __rtnl_unlock();
9970 wake_up(&netdev_unregistering_wq);
9971
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009972 /* Free network device */
9973 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975}
9976
Jarod Wilson92566452016-02-01 18:51:04 -05009977/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9978 * all the same fields in the same order as net_device_stats, with only
9979 * the type differing, but rtnl_link_stats64 may have additional fields
9980 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00009981 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009982void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9983 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00009984{
9985#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05009986 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02009987 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05009988 /* zero out counters that only exist in rtnl_link_stats64 */
9989 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9990 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009991#else
Jarod Wilson92566452016-02-01 18:51:04 -05009992 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009993 const unsigned long *src = (const unsigned long *)netdev_stats;
9994 u64 *dst = (u64 *)stats64;
9995
Jarod Wilson92566452016-02-01 18:51:04 -05009996 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009997 for (i = 0; i < n; i++)
9998 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05009999 /* zero out counters that only exist in rtnl_link_stats64 */
10000 memset((char *)stats64 + n * sizeof(u64), 0,
10001 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010002#endif
10003}
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010004EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010005
Eric Dumazetd83345a2009-11-16 03:36:51 +000010006/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010007 * dev_get_stats - get network device statistics
10008 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -070010009 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010010 *
Ben Hutchingsd7753512010-07-09 09:12:41 +000010011 * Get network statistics from device. Return @storage.
10012 * The device driver may provide its own method by setting
10013 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10014 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010015 */
Ben Hutchingsd7753512010-07-09 09:12:41 +000010016struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10017 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +000010018{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010019 const struct net_device_ops *ops = dev->netdev_ops;
10020
Eric Dumazet28172732010-07-07 14:58:56 -070010021 if (ops->ndo_get_stats64) {
10022 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010023 ops->ndo_get_stats64(dev, storage);
10024 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +000010025 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010026 } else {
10027 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -070010028 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -070010029 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10030 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10031 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -070010032 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -070010033}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010034EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -070010035
Heiner Kallweit44fa32f2020-10-12 10:01:27 +020010036/**
10037 * dev_fetch_sw_netstats - get per-cpu network device statistics
10038 * @s: place to store stats
10039 * @netstats: per-cpu network stats to read from
10040 *
10041 * Read per-cpu network statistics and populate the related fields in @s.
10042 */
10043void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10044 const struct pcpu_sw_netstats __percpu *netstats)
10045{
10046 int cpu;
10047
10048 for_each_possible_cpu(cpu) {
10049 const struct pcpu_sw_netstats *stats;
10050 struct pcpu_sw_netstats tmp;
10051 unsigned int start;
10052
10053 stats = per_cpu_ptr(netstats, cpu);
10054 do {
10055 start = u64_stats_fetch_begin_irq(&stats->syncp);
10056 tmp.rx_packets = stats->rx_packets;
10057 tmp.rx_bytes = stats->rx_bytes;
10058 tmp.tx_packets = stats->tx_packets;
10059 tmp.tx_bytes = stats->tx_bytes;
10060 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10061
10062 s->rx_packets += tmp.rx_packets;
10063 s->rx_bytes += tmp.rx_bytes;
10064 s->tx_packets += tmp.tx_packets;
10065 s->tx_bytes += tmp.tx_bytes;
10066 }
10067}
10068EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10069
Heiner Kallweita1839422020-11-07 21:49:07 +010010070/**
10071 * dev_get_tstats64 - ndo_get_stats64 implementation
10072 * @dev: device to get statistics from
10073 * @s: place to store stats
10074 *
10075 * Populate @s from dev->stats and dev->tstats. Can be used as
10076 * ndo_get_stats64() callback.
10077 */
10078void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
10079{
10080 netdev_stats_to_stats64(s, &dev->stats);
10081 dev_fetch_sw_netstats(s, dev->tstats);
10082}
10083EXPORT_SYMBOL_GPL(dev_get_tstats64);
10084
Eric Dumazet24824a02010-10-02 06:11:55 +000010085struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -070010086{
Eric Dumazet24824a02010-10-02 06:11:55 +000010087 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -070010088
Eric Dumazet24824a02010-10-02 06:11:55 +000010089#ifdef CONFIG_NET_CLS_ACT
10090 if (queue)
10091 return queue;
10092 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10093 if (!queue)
10094 return NULL;
10095 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -080010096 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +000010097 queue->qdisc_sleeping = &noop_qdisc;
10098 rcu_assign_pointer(dev->ingress_queue, queue);
10099#endif
10100 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -070010101}
10102
Eric Dumazet2c60db02012-09-16 09:17:26 +000010103static const struct ethtool_ops default_ethtool_ops;
10104
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +000010105void netdev_set_default_ethtool_ops(struct net_device *dev,
10106 const struct ethtool_ops *ops)
10107{
10108 if (dev->ethtool_ops == &default_ethtool_ops)
10109 dev->ethtool_ops = ops;
10110}
10111EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10112
Eric Dumazet74d332c2013-10-30 13:10:44 -070010113void netdev_freemem(struct net_device *dev)
10114{
10115 char *addr = (char *)dev - dev->padded;
10116
WANG Cong4cb28972014-06-02 15:55:22 -070010117 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010118}
10119
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120/**
tcharding722c9a02017-02-09 17:56:04 +110010121 * alloc_netdev_mqs - allocate network device
10122 * @sizeof_priv: size of private data to allocate space for
10123 * @name: device name format string
10124 * @name_assign_type: origin of device name
10125 * @setup: callback to initialize device
10126 * @txqs: the number of TX subqueues to allocate
10127 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 *
tcharding722c9a02017-02-09 17:56:04 +110010129 * Allocates a struct net_device with private data area for driver use
10130 * and performs basic initialization. Also allocates subqueue structs
10131 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 */
Tom Herbert36909ea2011-01-09 19:36:31 +000010133struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +020010134 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +000010135 void (*setup)(struct net_device *),
10136 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +030010139 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010140 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -070010142 BUG_ON(strlen(name) >= sizeof(dev->name));
10143
Tom Herbert36909ea2011-01-09 19:36:31 +000010144 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010145 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +000010146 return NULL;
10147 }
10148
Tom Herbert36909ea2011-01-09 19:36:31 +000010149 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010150 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +000010151 return NULL;
10152 }
Tom Herbert36909ea2011-01-09 19:36:31 +000010153
David S. Millerfd2ea0a2008-07-17 01:56:23 -070010154 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010155 if (sizeof_priv) {
10156 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010157 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010158 alloc_size += sizeof_priv;
10159 }
10160 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010161 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162
Vasily Averinc948f512021-07-19 13:44:23 +030010163 p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +000010164 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010167 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010169
Eric Dumazet4d92b952021-12-04 20:21:57 -080010170 ref_tracker_dir_init(&dev->refcnt_tracker, 128);
Eric Dumazet919067c2021-03-19 10:39:33 -070010171#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010172 dev->pcpu_refcnt = alloc_percpu(int);
10173 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -070010174 goto free_dev;
Eric Dumazetadd2d732021-03-22 11:21:45 -070010175 dev_hold(dev);
10176#else
10177 refcount_set(&dev->dev_refcnt, 1);
Eric Dumazet919067c2021-03-19 10:39:33 -070010178#endif
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010179
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +000010181 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182
Jiri Pirko22bedad32010-04-01 21:22:57 +000010183 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010184 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +000010185
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010186 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -070010188 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +000010189 dev->gso_max_segs = GSO_MAX_SEGS;
Coco Lieac1b932022-01-05 02:48:38 -080010190 dev->gro_max_size = GRO_MAX_SIZE;
Taehee Yoo5343da42019-10-21 18:47:50 +000010191 dev->upper_level = 1;
10192 dev->lower_level = 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010193#ifdef CONFIG_LOCKDEP
10194 dev->nested_level = 0;
10195 INIT_LIST_HEAD(&dev->unlink_list);
10196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197
Herbert Xud565b0a2008-12-15 23:38:52 -080010198 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010199 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -070010200 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +000010201 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +020010202 INIT_LIST_HEAD(&dev->adj_list.upper);
10203 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -080010204 INIT_LIST_HEAD(&dev->ptype_all);
10205 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +010010206 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +020010207#ifdef CONFIG_NET_SCHED
10208 hash_init(dev->qdisc_hash);
10209#endif
Eric Dumazet02875872014-10-05 18:38:35 -070010210 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010212
Phil Suttera8131042016-02-17 15:37:43 +010010213 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +020010214 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +010010215 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +010010216 }
Phil Sutter906470c2015-08-18 10:30:48 +020010217
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010218 dev->num_tx_queues = txqs;
10219 dev->real_num_tx_queues = txqs;
10220 if (netif_alloc_netdev_queues(dev))
10221 goto free_all;
10222
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010223 dev->num_rx_queues = rxqs;
10224 dev->real_num_rx_queues = rxqs;
10225 if (netif_alloc_rx_queues(dev))
10226 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010227
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +020010229 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +000010230 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +000010231 if (!dev->ethtool_ops)
10232 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +020010233
Lukas Wunner17d20782021-10-08 22:06:02 +020010234 nf_hook_netdev_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +020010235
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010237
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010238free_all:
10239 free_netdev(dev);
10240 return NULL;
10241
Eric Dumazet29b44332010-10-11 10:22:12 +000010242free_pcpu:
Eric Dumazet919067c2021-03-19 10:39:33 -070010243#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010244 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010245free_dev:
Eric Dumazet919067c2021-03-19 10:39:33 -070010246#endif
Eric Dumazet74d332c2013-10-30 13:10:44 -070010247 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010248 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249}
Tom Herbert36909ea2011-01-09 19:36:31 +000010250EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251
10252/**
tcharding722c9a02017-02-09 17:56:04 +110010253 * free_netdev - free network device
10254 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 *
tcharding722c9a02017-02-09 17:56:04 +110010256 * This function does the last stage of destroying an allocated device
10257 * interface. The reference to the device object is released. If this
10258 * is the last reference then it will be freed.Must be called in process
10259 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 */
10261void free_netdev(struct net_device *dev)
10262{
Herbert Xud565b0a2008-12-15 23:38:52 -080010263 struct napi_struct *p, *n;
10264
Eric Dumazet93d05d42015-11-18 06:31:03 -080010265 might_sleep();
Jakub Kicinskic269a242021-01-06 10:40:06 -080010266
10267 /* When called immediately after register_netdevice() failed the unwind
10268 * handling may still be dismantling the device. Handle that case by
10269 * deferring the free.
10270 */
10271 if (dev->reg_state == NETREG_UNREGISTERING) {
10272 ASSERT_RTNL();
10273 dev->needs_free_netdev = true;
10274 return;
10275 }
10276
Eric Dumazet60877a32013-06-20 01:15:51 -070010277 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010278 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -070010279
Eric Dumazet33d480c2011-08-11 19:30:52 +000010280 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +000010281
Jiri Pirkof001fde2009-05-05 02:48:28 +000010282 /* Flush device addresses */
10283 dev_addr_flush(dev);
10284
Herbert Xud565b0a2008-12-15 23:38:52 -080010285 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10286 netif_napi_del(p);
10287
Eric Dumazet4d92b952021-12-04 20:21:57 -080010288 ref_tracker_dir_exit(&dev->refcnt_tracker);
Eric Dumazet919067c2021-03-19 10:39:33 -070010289#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010290 free_percpu(dev->pcpu_refcnt);
10291 dev->pcpu_refcnt = NULL;
Eric Dumazet919067c2021-03-19 10:39:33 -070010292#endif
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010010293 free_percpu(dev->xdp_bulkq);
10294 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +000010295
Stephen Hemminger3041a062006-05-26 13:25:24 -070010296 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -070010298 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 return;
10300 }
10301
10302 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10303 dev->reg_state = NETREG_RELEASED;
10304
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070010305 /* will free via device release */
10306 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010308EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010309
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010310/**
10311 * synchronize_net - Synchronize with packet receive processing
10312 *
10313 * Wait for packets currently being received to be done.
10314 * Does not block later packets from starting.
10315 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010316void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317{
10318 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +000010319 if (rtnl_is_locked())
10320 synchronize_rcu_expedited();
10321 else
10322 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010324EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325
10326/**
Eric Dumazet44a08732009-10-27 07:03:04 +000010327 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +000010329 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -080010330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010332 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +000010333 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010334 *
10335 * Callers must hold the rtnl semaphore. You may want
10336 * unregister_netdev() instead of this.
10337 */
10338
Eric Dumazet44a08732009-10-27 07:03:04 +000010339void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010340{
Herbert Xua6620712007-12-12 19:21:56 -080010341 ASSERT_RTNL();
10342
Eric Dumazet44a08732009-10-27 07:03:04 +000010343 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010344 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +000010345 } else {
Jakub Kicinski037e56b2021-01-19 12:25:19 -080010346 LIST_HEAD(single);
10347
10348 list_add(&dev->unreg_list, &single);
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010349 unregister_netdevice_many(&single);
Eric Dumazet44a08732009-10-27 07:03:04 +000010350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351}
Eric Dumazet44a08732009-10-27 07:03:04 +000010352EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010353
10354/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010355 * unregister_netdevice_many - unregister many devices
10356 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -070010357 *
10358 * Note: As most callers use a stack allocated list_head,
10359 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010360 */
10361void unregister_netdevice_many(struct list_head *head)
10362{
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010363 struct net_device *dev, *tmp;
10364 LIST_HEAD(close_head);
10365
10366 BUG_ON(dev_boot_phase);
10367 ASSERT_RTNL();
10368
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010369 if (list_empty(head))
10370 return;
10371
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010372 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
10373 /* Some devices call without registering
10374 * for initialization unwind. Remove those
10375 * devices and proceed with the remaining.
10376 */
10377 if (dev->reg_state == NETREG_UNINITIALIZED) {
10378 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
10379 dev->name, dev);
10380
10381 WARN_ON(1);
10382 list_del(&dev->unreg_list);
10383 continue;
10384 }
10385 dev->dismantle = true;
10386 BUG_ON(dev->reg_state != NETREG_REGISTERED);
10387 }
10388
10389 /* If device is running, close it first. */
10390 list_for_each_entry(dev, head, unreg_list)
10391 list_add_tail(&dev->close_list, &close_head);
10392 dev_close_many(&close_head, true);
10393
10394 list_for_each_entry(dev, head, unreg_list) {
10395 /* And unlink it from device chain. */
10396 unlist_netdevice(dev);
10397
10398 dev->reg_state = NETREG_UNREGISTERING;
10399 }
10400 flush_all_backlogs();
10401
10402 synchronize_net();
10403
10404 list_for_each_entry(dev, head, unreg_list) {
10405 struct sk_buff *skb = NULL;
10406
10407 /* Shutdown queueing discipline. */
10408 dev_shutdown(dev);
10409
10410 dev_xdp_uninstall(dev);
10411
10412 /* Notify protocols, that we are about to destroy
10413 * this device. They should clean all the things.
10414 */
10415 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
10416
10417 if (!dev->rtnl_link_ops ||
10418 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
10419 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
10420 GFP_KERNEL, NULL, 0);
10421
10422 /*
10423 * Flush the unicast and multicast chains
10424 */
10425 dev_uc_flush(dev);
10426 dev_mc_flush(dev);
10427
10428 netdev_name_node_alt_flush(dev);
10429 netdev_name_node_free(dev->name_node);
10430
10431 if (dev->netdev_ops->ndo_uninit)
10432 dev->netdev_ops->ndo_uninit(dev);
10433
10434 if (skb)
10435 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
10436
10437 /* Notifier chain MUST detach us all upper devices. */
10438 WARN_ON(netdev_has_any_upper_dev(dev));
10439 WARN_ON(netdev_has_any_lower_dev(dev));
10440
10441 /* Remove entries from kobject tree */
10442 netdev_unregister_kobject(dev);
10443#ifdef CONFIG_XPS
10444 /* Remove XPS queueing entries */
10445 netif_reset_xps_queues_gt(dev, 0);
10446#endif
10447 }
10448
10449 synchronize_net();
10450
10451 list_for_each_entry(dev, head, unreg_list) {
10452 dev_put(dev);
10453 net_set_todo(dev);
10454 }
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010455
10456 list_del(head);
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010457}
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010458EXPORT_SYMBOL(unregister_netdevice_many);
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010459
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461 * unregister_netdev - remove device from the kernel
10462 * @dev: device
10463 *
10464 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010465 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466 *
10467 * This is just a wrapper for unregister_netdevice that takes
10468 * the rtnl semaphore. In general you want to use this and not
10469 * unregister_netdevice.
10470 */
10471void unregister_netdev(struct net_device *dev)
10472{
10473 rtnl_lock();
10474 unregister_netdevice(dev);
10475 rtnl_unlock();
10476}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477EXPORT_SYMBOL(unregister_netdev);
10478
Eric W. Biedermance286d32007-09-12 13:53:49 +020010479/**
Andrei Vagin0854fa82021-04-06 23:40:51 -070010480 * __dev_change_net_namespace - move device to different nethost namespace
Eric W. Biedermance286d32007-09-12 13:53:49 +020010481 * @dev: device
10482 * @net: network namespace
10483 * @pat: If not NULL name pattern to try if the current device name
10484 * is already taken in the destination network namespace.
Andrei Vagineeb85a12021-04-05 00:12:23 -070010485 * @new_ifindex: If not zero, specifies device index in the target
10486 * namespace.
Eric W. Biedermance286d32007-09-12 13:53:49 +020010487 *
10488 * This function shuts down a device interface and moves it
10489 * to a new network namespace. On success 0 is returned, on
10490 * a failure a netagive errno code is returned.
10491 *
10492 * Callers must hold the rtnl semaphore.
10493 */
10494
Andrei Vagin0854fa82021-04-06 23:40:51 -070010495int __dev_change_net_namespace(struct net_device *dev, struct net *net,
10496 const char *pat, int new_ifindex)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010497{
Christian Brauneref6a4c82020-02-27 04:37:19 +010010498 struct net *net_old = dev_net(dev);
Andrei Vagineeb85a12021-04-05 00:12:23 -070010499 int err, new_nsid;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010500
10501 ASSERT_RTNL();
10502
10503 /* Don't allow namespace local devices to be moved. */
10504 err = -EINVAL;
10505 if (dev->features & NETIF_F_NETNS_LOCAL)
10506 goto out;
10507
10508 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010509 if (dev->reg_state != NETREG_REGISTERED)
10510 goto out;
10511
10512 /* Get out if there is nothing todo */
10513 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010010514 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010515 goto out;
10516
10517 /* Pick the destination device name, and ensure
10518 * we can use it in the destination network namespace.
10519 */
10520 err = -EEXIST;
Antoine Tenart75ea27d2021-10-07 18:16:50 +020010521 if (netdev_name_in_use(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010522 /* We get here if we can't use the current device name */
10523 if (!pat)
10524 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010525 err = dev_get_valid_name(net, dev, pat);
10526 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010527 goto out;
10528 }
10529
Andrei Vagineeb85a12021-04-05 00:12:23 -070010530 /* Check that new_ifindex isn't used yet. */
10531 err = -EBUSY;
10532 if (new_ifindex && __dev_get_by_index(net, new_ifindex))
10533 goto out;
10534
Eric W. Biedermance286d32007-09-12 13:53:49 +020010535 /*
10536 * And now a mini version of register_netdevice unregister_netdevice.
10537 */
10538
10539 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010540 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010541
10542 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010543 unlist_netdevice(dev);
10544
10545 synchronize_net();
10546
10547 /* Shutdown queueing discipline. */
10548 dev_shutdown(dev);
10549
10550 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010551 * this device. They should clean all the things.
10552 *
10553 * Note that dev->reg_state stays at NETREG_REGISTERED.
10554 * This is wanted because this way 8021q and macvlan know
10555 * the device is just moving and can keep their slaves up.
10556 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010557 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010558 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010559
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010560 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010561 /* If there is an ifindex conflict assign a new one */
Andrei Vagineeb85a12021-04-05 00:12:23 -070010562 if (!new_ifindex) {
10563 if (__dev_get_by_index(net, dev->ifindex))
10564 new_ifindex = dev_new_index(net);
10565 else
10566 new_ifindex = dev->ifindex;
10567 }
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010568
10569 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10570 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010571
10572 /*
10573 * Flush the unicast and multicast chains
10574 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010575 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010576 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010577
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010578 /* Send a netdev-removed uevent to the old namespace */
10579 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010580 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010581
Jiri Pirko93642e12020-01-25 12:17:08 +010010582 /* Move per-net netdevice notifiers that are following the netdevice */
10583 move_netdevice_notifiers_dev_net(dev, net);
10584
Eric W. Biedermance286d32007-09-12 13:53:49 +020010585 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010586 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010587 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010588
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010589 /* Send a netdev-add uevent to the new namespace */
10590 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010591 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010592
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010593 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010594 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010595 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010596
Christian Brauneref6a4c82020-02-27 04:37:19 +010010597 /* Adapt owner in case owning user namespace of target network
10598 * namespace is different from the original one.
10599 */
10600 err = netdev_change_owner(dev, net_old, net);
10601 WARN_ON(err);
10602
Eric W. Biedermance286d32007-09-12 13:53:49 +020010603 /* Add the device back in the hashes */
10604 list_netdevice(dev);
10605
10606 /* Notify protocols, that a new device appeared. */
10607 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10608
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010609 /*
10610 * Prevent userspace races by waiting until the network
10611 * device is fully setup before sending notifications.
10612 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010613 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010614
Eric W. Biedermance286d32007-09-12 13:53:49 +020010615 synchronize_net();
10616 err = 0;
10617out:
10618 return err;
10619}
Andrei Vagin0854fa82021-04-06 23:40:51 -070010620EXPORT_SYMBOL_GPL(__dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010621
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010622static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010623{
10624 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010625 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010626 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010627 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629 local_irq_disable();
10630 cpu = smp_processor_id();
10631 sd = &per_cpu(softnet_data, cpu);
10632 oldsd = &per_cpu(softnet_data, oldcpu);
10633
10634 /* Find end of our completion_queue. */
10635 list_skb = &sd->completion_queue;
10636 while (*list_skb)
10637 list_skb = &(*list_skb)->next;
10638 /* Append completion queue from offline CPU. */
10639 *list_skb = oldsd->completion_queue;
10640 oldsd->completion_queue = NULL;
10641
Linus Torvalds1da177e2005-04-16 15:20:36 -070010642 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010643 if (oldsd->output_queue) {
10644 *sd->output_queue_tailp = oldsd->output_queue;
10645 sd->output_queue_tailp = oldsd->output_queue_tailp;
10646 oldsd->output_queue = NULL;
10647 oldsd->output_queue_tailp = &oldsd->output_queue;
10648 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010649 /* Append NAPI poll list from offline CPU, with one exception :
10650 * process_backlog() must be called by cpu owning percpu backlog.
10651 * We properly handle process_queue & input_pkt_queue later.
10652 */
10653 while (!list_empty(&oldsd->poll_list)) {
10654 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10655 struct napi_struct,
10656 poll_list);
10657
10658 list_del_init(&napi->poll_list);
10659 if (napi->poll == process_backlog)
10660 napi->state = 0;
10661 else
10662 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664
10665 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10666 local_irq_enable();
10667
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010668#ifdef CONFIG_RPS
10669 remsd = oldsd->rps_ipi_list;
10670 oldsd->rps_ipi_list = NULL;
10671#endif
10672 /* send out pending IPI's on offline CPU */
10673 net_rps_send_ipi(remsd);
10674
Linus Torvalds1da177e2005-04-16 15:20:36 -070010675 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010676 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010677 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010678 input_queue_head_incr(oldsd);
10679 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010680 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010681 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010682 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010684
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687
Herbert Xu7f353bf2007-08-10 15:47:58 -070010688/**
Herbert Xub63365a2008-10-23 01:11:29 -070010689 * netdev_increment_features - increment feature set by one
10690 * @all: current feature set
10691 * @one: new feature set
10692 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010693 *
10694 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010695 * @one to the master device with current feature set @all. Will not
10696 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010697 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010698netdev_features_t netdev_increment_features(netdev_features_t all,
10699 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010700{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010701 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010702 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010703 mask |= NETIF_F_VLAN_CHALLENGED;
10704
Tom Herberta1882222015-12-14 11:19:43 -080010705 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010706 all &= one | ~NETIF_F_ALL_FOR_ALL;
10707
Michał Mirosław1742f182011-04-22 06:31:16 +000010708 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010709 if (all & NETIF_F_HW_CSUM)
10710 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010711
10712 return all;
10713}
Herbert Xub63365a2008-10-23 01:11:29 -070010714EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010715
Baruch Siach430f03c2013-06-02 20:43:55 +000010716static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010717{
10718 int i;
10719 struct hlist_head *hash;
10720
Kees Cook6da2ec52018-06-12 13:55:00 -070010721 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010722 if (hash != NULL)
10723 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10724 INIT_HLIST_HEAD(&hash[i]);
10725
10726 return hash;
10727}
10728
Eric W. Biederman881d9662007-09-17 11:56:21 -070010729/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010730static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010731{
Li RongQingd9f37d02018-07-13 14:41:36 +080010732 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080010733 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080010734
Rustad, Mark D734b6542012-07-18 09:06:07 +000010735 if (net != &init_net)
10736 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070010737
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010738 net->dev_name_head = netdev_create_hash();
10739 if (net->dev_name_head == NULL)
10740 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010741
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010742 net->dev_index_head = netdev_create_hash();
10743 if (net->dev_index_head == NULL)
10744 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010745
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020010746 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10747
Eric W. Biederman881d9662007-09-17 11:56:21 -070010748 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010749
10750err_idx:
10751 kfree(net->dev_name_head);
10752err_name:
10753 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010754}
10755
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010756/**
10757 * netdev_drivername - network driver for the device
10758 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010759 *
10760 * Determine network driver for device.
10761 */
David S. Miller3019de12011-06-06 16:41:33 -070010762const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070010763{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070010764 const struct device_driver *driver;
10765 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070010766 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070010767
10768 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010769 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070010770 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010771
10772 driver = parent->driver;
10773 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070010774 return driver->name;
10775 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010776}
10777
Joe Perches6ea754e2014-09-22 11:10:50 -070010778static void __netdev_printk(const char *level, const struct net_device *dev,
10779 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000010780{
Joe Perchesb004ff42012-09-12 20:12:19 -070010781 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010782 dev_printk_emit(level[1] - '0',
10783 dev->dev.parent,
10784 "%s %s %s%s: %pV",
10785 dev_driver_string(dev->dev.parent),
10786 dev_name(dev->dev.parent),
10787 netdev_name(dev), netdev_reg_state(dev),
10788 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010789 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010790 printk("%s%s%s: %pV",
10791 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010792 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070010793 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010794 }
Joe Perches256df2f2010-06-27 01:02:35 +000010795}
10796
Joe Perches6ea754e2014-09-22 11:10:50 -070010797void netdev_printk(const char *level, const struct net_device *dev,
10798 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000010799{
10800 struct va_format vaf;
10801 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000010802
10803 va_start(args, format);
10804
10805 vaf.fmt = format;
10806 vaf.va = &args;
10807
Joe Perches6ea754e2014-09-22 11:10:50 -070010808 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010809
Joe Perches256df2f2010-06-27 01:02:35 +000010810 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000010811}
10812EXPORT_SYMBOL(netdev_printk);
10813
10814#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070010815void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000010816{ \
Joe Perches256df2f2010-06-27 01:02:35 +000010817 struct va_format vaf; \
10818 va_list args; \
10819 \
10820 va_start(args, fmt); \
10821 \
10822 vaf.fmt = fmt; \
10823 vaf.va = &args; \
10824 \
Joe Perches6ea754e2014-09-22 11:10:50 -070010825 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070010826 \
Joe Perches256df2f2010-06-27 01:02:35 +000010827 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000010828} \
10829EXPORT_SYMBOL(func);
10830
10831define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10832define_netdev_printk_level(netdev_alert, KERN_ALERT);
10833define_netdev_printk_level(netdev_crit, KERN_CRIT);
10834define_netdev_printk_level(netdev_err, KERN_ERR);
10835define_netdev_printk_level(netdev_warn, KERN_WARNING);
10836define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10837define_netdev_printk_level(netdev_info, KERN_INFO);
10838
Pavel Emelyanov46650792007-10-08 20:38:39 -070010839static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010840{
10841 kfree(net->dev_name_head);
10842 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030010843 if (net != &init_net)
10844 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070010845}
10846
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010847static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070010848 .init = netdev_init,
10849 .exit = netdev_exit,
10850};
10851
Pavel Emelyanov46650792007-10-08 20:38:39 -070010852static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010853{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010854 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010855 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010856 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020010857 * initial network namespace
10858 */
10859 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010860 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010861 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010862 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020010863
10864 /* Ignore unmoveable devices (i.e. loopback) */
10865 if (dev->features & NETIF_F_NETNS_LOCAL)
10866 continue;
10867
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010868 /* Leave virtual devices for the generic cleanup */
Martin Willi3a5ca852021-03-02 13:24:23 +010010869 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010870 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080010871
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010872 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010873 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Antoine Tenart75ea27d2021-10-07 18:16:50 +020010874 if (netdev_name_in_use(&init_net, fb_name))
Jiri Pirko55b40db2019-07-28 14:56:36 +020010875 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Andrei Vagin0854fa82021-04-06 23:40:51 -070010876 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010877 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010878 pr_emerg("%s: failed to move %s to init_net: %d\n",
10879 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010880 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020010881 }
10882 }
10883 rtnl_unlock();
10884}
10885
Eric W. Biederman50624c92013-09-23 21:19:49 -070010886static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10887{
10888 /* Return with the rtnl_lock held when there are no network
10889 * devices unregistering in any network namespace in net_list.
10890 */
10891 struct net *net;
10892 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010010893 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010894
Peter Zijlstraff960a72014-10-29 17:04:56 +010010895 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010896 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070010897 unregistering = false;
10898 rtnl_lock();
10899 list_for_each_entry(net, net_list, exit_list) {
10900 if (net->dev_unreg_count > 0) {
10901 unregistering = true;
10902 break;
10903 }
10904 }
10905 if (!unregistering)
10906 break;
10907 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010010908
10909 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010910 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010010911 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010912}
10913
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010914static void __net_exit default_device_exit_batch(struct list_head *net_list)
10915{
10916 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010917 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010918 * Do this across as many network namespaces as possible to
10919 * improve batching efficiency.
10920 */
10921 struct net_device *dev;
10922 struct net *net;
10923 LIST_HEAD(dev_kill_list);
10924
Eric W. Biederman50624c92013-09-23 21:19:49 -070010925 /* To prevent network device cleanup code from dereferencing
10926 * loopback devices or network devices that have been freed
10927 * wait here for all pending unregistrations to complete,
10928 * before unregistring the loopback device and allowing the
10929 * network namespace be freed.
10930 *
10931 * The netdev todo list containing all network devices
10932 * unregistrations that happen in default_device_exit_batch
10933 * will run in the rtnl_unlock() at the end of
10934 * default_device_exit_batch.
10935 */
10936 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010937 list_for_each_entry(net, net_list, exit_list) {
10938 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020010939 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010940 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10941 else
10942 unregister_netdevice_queue(dev, &dev_kill_list);
10943 }
10944 }
10945 unregister_netdevice_many(&dev_kill_list);
10946 rtnl_unlock();
10947}
10948
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010949static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010950 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010951 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020010952};
10953
Linus Torvalds1da177e2005-04-16 15:20:36 -070010954/*
10955 * Initialize the DEV module. At boot time this walks the device list and
10956 * unhooks any devices that fail to initialise (normally hardware not
10957 * present) and leaves us with a valid list of present and active devices.
10958 *
10959 */
10960
10961/*
10962 * This is called single threaded during boot, so no need
10963 * to take the rtnl semaphore.
10964 */
10965static int __init net_dev_init(void)
10966{
10967 int i, rc = -ENOMEM;
10968
10969 BUG_ON(!dev_boot_phase);
10970
Linus Torvalds1da177e2005-04-16 15:20:36 -070010971 if (dev_proc_init())
10972 goto out;
10973
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010974 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070010975 goto out;
10976
10977 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080010978 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010979 INIT_LIST_HEAD(&ptype_base[i]);
10980
Eric W. Biederman881d9662007-09-17 11:56:21 -070010981 if (register_pernet_subsys(&netdev_net_ops))
10982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010983
10984 /*
10985 * Initialise the packet receive queues.
10986 */
10987
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070010988 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070010989 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010990 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991
Eric Dumazet41852492016-08-26 12:50:39 -070010992 INIT_WORK(flush, flush_backlog);
10993
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010994 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070010995 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010010996#ifdef CONFIG_XFRM_OFFLOAD
10997 skb_queue_head_init(&sd->xfrm_backlog);
10998#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010999 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000011000 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000011001#ifdef CONFIG_RPS
Peter Zijlstra545b8c82020-06-15 11:29:31 +020011002 INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011003 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070011004#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000011005
David S. Miller7c4ec742018-07-20 23:37:55 -070011006 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011007 sd->backlog.poll = process_backlog;
11008 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 }
11010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011011 dev_boot_phase = 0;
11012
Eric W. Biederman505d4f72008-11-07 22:54:20 -080011013 /* The loopback device is special if any other network devices
11014 * is present in a network namespace the loopback device must
11015 * be present. Since we now dynamically allocate and free the
11016 * loopback device ensure this invariant is maintained by
11017 * keeping the loopback device as the first device on the
11018 * list of network devices. Ensuring the loopback devices
11019 * is the first device that appears and the last network device
11020 * that disappears.
11021 */
11022 if (register_pernet_device(&loopback_net_ops))
11023 goto out;
11024
11025 if (register_pernet_device(&default_device_ops))
11026 goto out;
11027
Carlos R. Mafra962cf362008-05-15 11:15:37 -030011028 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11029 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011030
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011031 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11032 NULL, dev_cpu_dead);
11033 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011034 rc = 0;
11035out:
11036 return rc;
11037}
11038
11039subsys_initcall(net_dev_init);