blob: 74fd402d26ddeba725d7447b855f4d3c1fea8238 [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>
Daniel Borkmann357b6cc2020-03-18 10:33:22 +0100143#include <linux/netfilter_ingress.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 Xud565b0a2008-12-15 23:38:52 -0800156#define MAX_GRO_SKBS 8
157
Herbert Xu5d38a072009-01-04 16:13:40 -0800158/* This should be increased if a protocol with a bigger head is added. */
159#define GRO_MAX_HEAD (MAX_HEADER + 128)
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000162static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000163struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
164struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000165static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000167static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700168static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700169 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000170static int call_netdevice_notifiers_extack(unsigned long val,
171 struct net_device *dev,
172 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200173static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700176 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * semaphore.
178 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800179 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *
181 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700182 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * actual updates. This allows pure readers to access the list even
184 * while a writer is preparing to update it.
185 *
186 * To put it another way, dev_base_lock is held for writing only to
187 * protect against pure readers; the rtnl semaphore provides the
188 * protection against other writers.
189 *
190 * See, for example usages, register_netdevice() and
191 * unregister_netdevice(), which must be called with the rtnl
192 * semaphore held.
193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195EXPORT_SYMBOL(dev_base_lock);
196
Florian Westphal6c557002017-10-02 23:50:05 +0200197static DEFINE_MUTEX(ifalias_mutex);
198
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300199/* protects napi_hash addition/deletion and napi_gen_id */
200static DEFINE_SPINLOCK(napi_hash_lock);
201
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800202static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800203static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300204
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200205static DECLARE_RWSEM(devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000206
Thomas Graf4e985ad2011-06-21 03:11:20 +0000207static inline void dev_base_seq_inc(struct net *net)
208{
tcharding643aa9c2017-02-09 17:56:05 +1100209 while (++net->dev_base_seq == 0)
210 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000211}
212
Eric W. Biederman881d9662007-09-17 11:56:21 -0700213static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700215 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000216
stephen hemminger08e98972009-11-10 07:20:34 +0000217 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Eric W. Biederman881d9662007-09-17 11:56:21 -0700220static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700222 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000225static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000226{
227#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000228 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000229#endif
230}
231
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000232static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000233{
234#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000235 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000236#endif
237}
238
Jiri Pirkoff927412019-09-30 11:48:15 +0200239static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
240 const char *name)
241{
242 struct netdev_name_node *name_node;
243
244 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
245 if (!name_node)
246 return NULL;
247 INIT_HLIST_NODE(&name_node->hlist);
248 name_node->dev = dev;
249 name_node->name = name;
250 return name_node;
251}
252
253static struct netdev_name_node *
254netdev_name_node_head_alloc(struct net_device *dev)
255{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200256 struct netdev_name_node *name_node;
257
258 name_node = netdev_name_node_alloc(dev, dev->name);
259 if (!name_node)
260 return NULL;
261 INIT_LIST_HEAD(&name_node->list);
262 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200263}
264
265static void netdev_name_node_free(struct netdev_name_node *name_node)
266{
267 kfree(name_node);
268}
269
270static void netdev_name_node_add(struct net *net,
271 struct netdev_name_node *name_node)
272{
273 hlist_add_head_rcu(&name_node->hlist,
274 dev_name_hash(net, name_node->name));
275}
276
277static void netdev_name_node_del(struct netdev_name_node *name_node)
278{
279 hlist_del_rcu(&name_node->hlist);
280}
281
282static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
283 const char *name)
284{
285 struct hlist_head *head = dev_name_hash(net, name);
286 struct netdev_name_node *name_node;
287
288 hlist_for_each_entry(name_node, head, hlist)
289 if (!strcmp(name_node->name, name))
290 return name_node;
291 return NULL;
292}
293
294static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
295 const char *name)
296{
297 struct hlist_head *head = dev_name_hash(net, name);
298 struct netdev_name_node *name_node;
299
300 hlist_for_each_entry_rcu(name_node, head, hlist)
301 if (!strcmp(name_node->name, name))
302 return name_node;
303 return NULL;
304}
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
368 write_lock_bh(&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));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200373 write_unlock_bh(&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 */
386 write_lock_bh(&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);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200390 write_unlock_bh(&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
601/**
602 * dev_add_offload - register offload handlers
603 * @po: protocol offload declaration
604 *
605 * Add protocol offload handlers to the networking stack. The passed
606 * &proto_offload is linked into kernel lists and may not be freed until
607 * it has been removed from the kernel lists.
608 *
609 * This call does not sleep therefore it can not
610 * guarantee all CPU's that are in middle of receiving packets
611 * will see the new offload handlers (until the next received packet).
612 */
613void dev_add_offload(struct packet_offload *po)
614{
David S. Millerbdef7de2015-06-01 14:56:09 -0700615 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000616
617 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700618 list_for_each_entry(elem, &offload_base, list) {
619 if (po->priority < elem->priority)
620 break;
621 }
622 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000623 spin_unlock(&offload_lock);
624}
625EXPORT_SYMBOL(dev_add_offload);
626
627/**
628 * __dev_remove_offload - remove offload handler
629 * @po: packet offload declaration
630 *
631 * Remove a protocol offload handler that was previously added to the
632 * kernel offload handlers by dev_add_offload(). The passed &offload_type
633 * is removed from the kernel lists and can be freed or reused once this
634 * function returns.
635 *
636 * The packet type might still be in use by receivers
637 * and must not be freed until after all the CPU's have gone
638 * through a quiescent state.
639 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800640static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000641{
642 struct list_head *head = &offload_base;
643 struct packet_offload *po1;
644
Eric Dumazetc53aa502012-11-16 08:08:23 +0000645 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000646
647 list_for_each_entry(po1, head, list) {
648 if (po == po1) {
649 list_del_rcu(&po->list);
650 goto out;
651 }
652 }
653
654 pr_warn("dev_remove_offload: %p not found\n", po);
655out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000656 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000657}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000658
659/**
660 * dev_remove_offload - remove packet offload handler
661 * @po: packet offload declaration
662 *
663 * Remove a packet offload handler that was previously added to the kernel
664 * offload handlers by dev_add_offload(). The passed &offload_type is
665 * removed from the kernel lists and can be freed or reused once this
666 * function returns.
667 *
668 * This call sleeps to guarantee that no CPU is looking at the packet
669 * type after return.
670 */
671void dev_remove_offload(struct packet_offload *po)
672{
673 __dev_remove_offload(po);
674
675 synchronize_net();
676}
677EXPORT_SYMBOL(dev_remove_offload);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100680 *
681 * Device Interface Subroutines
682 *
683 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200686 * dev_get_iflink - get 'iflink' value of a interface
687 * @dev: targeted interface
688 *
689 * Indicates the ifindex the interface is linked to.
690 * Physical interfaces have the same 'ifindex' and 'iflink' values.
691 */
692
693int dev_get_iflink(const struct net_device *dev)
694{
695 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
696 return dev->netdev_ops->ndo_get_iflink(dev);
697
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200698 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200699}
700EXPORT_SYMBOL(dev_get_iflink);
701
702/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700703 * dev_fill_metadata_dst - Retrieve tunnel egress information.
704 * @dev: targeted interface
705 * @skb: The packet.
706 *
707 * For better visibility of tunnel traffic OVS needs to retrieve
708 * egress tunnel information for a packet. Following API allows
709 * user to get this info.
710 */
711int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
712{
713 struct ip_tunnel_info *info;
714
715 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
716 return -EINVAL;
717
718 info = skb_tunnel_info_unclone(skb);
719 if (!info)
720 return -ENOMEM;
721 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
722 return -EINVAL;
723
724 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
725}
726EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
727
Pablo Neira Ayusoddb94ea2021-03-24 02:30:32 +0100728static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
729{
730 int k = stack->num_paths++;
731
732 if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
733 return NULL;
734
735 return &stack->path[k];
736}
737
738int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
739 struct net_device_path_stack *stack)
740{
741 const struct net_device *last_dev;
742 struct net_device_path_ctx ctx = {
743 .dev = dev,
744 .daddr = daddr,
745 };
746 struct net_device_path *path;
747 int ret = 0;
748
749 stack->num_paths = 0;
750 while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
751 last_dev = ctx.dev;
752 path = dev_fwd_path(stack);
753 if (!path)
754 return -1;
755
756 memset(path, 0, sizeof(struct net_device_path));
757 ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
758 if (ret < 0)
759 return -1;
760
761 if (WARN_ON_ONCE(last_dev == ctx.dev))
762 return -1;
763 }
764 path = dev_fwd_path(stack);
765 if (!path)
766 return -1;
767 path->type = DEV_PATH_ETHERNET;
768 path->dev = ctx.dev;
769
770 return ret;
771}
772EXPORT_SYMBOL_GPL(dev_fill_forward_path);
773
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700774/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700776 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @name: name to find
778 *
779 * Find an interface by name. Must be called under RTNL semaphore
780 * or @dev_base_lock. If the name is found a pointer to the device
781 * is returned. If the name is not found then %NULL is returned. The
782 * reference counters are not incremented so the caller must be
783 * careful with locks.
784 */
785
Eric W. Biederman881d9662007-09-17 11:56:21 -0700786struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jiri Pirkoff927412019-09-30 11:48:15 +0200788 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jiri Pirkoff927412019-09-30 11:48:15 +0200790 node_name = netdev_name_node_lookup(net, name);
791 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700793EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795/**
tcharding722c9a02017-02-09 17:56:04 +1100796 * dev_get_by_name_rcu - find a device by its name
797 * @net: the applicable net namespace
798 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000799 *
tcharding722c9a02017-02-09 17:56:04 +1100800 * Find an interface by name.
801 * If the name is found a pointer to the device is returned.
802 * If the name is not found then %NULL is returned.
803 * The reference counters are not incremented so the caller must be
804 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000805 */
806
807struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
808{
Jiri Pirkoff927412019-09-30 11:48:15 +0200809 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000810
Jiri Pirkoff927412019-09-30 11:48:15 +0200811 node_name = netdev_name_node_lookup_rcu(net, name);
812 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000813}
814EXPORT_SYMBOL(dev_get_by_name_rcu);
815
816/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700818 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * @name: name to find
820 *
821 * Find an interface by name. This can be called from any
822 * context and does its own locking. The returned handle has
823 * the usage count incremented and the caller must use dev_put() to
824 * release it when it is no longer needed. %NULL is returned if no
825 * matching device is found.
826 */
827
Eric W. Biederman881d9662007-09-17 11:56:21 -0700828struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct net_device *dev;
831
Eric Dumazet72c95282009-10-30 07:11:27 +0000832 rcu_read_lock();
833 dev = dev_get_by_name_rcu(net, name);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800834 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000835 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return dev;
837}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700838EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840/**
841 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700842 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * @ifindex: index of device
844 *
845 * Search for an interface by index. Returns %NULL if the device
846 * is not found or a pointer to the device. The device has not
847 * had its reference counter increased so the caller must be careful
848 * about locking. The caller must hold either the RTNL semaphore
849 * or @dev_base_lock.
850 */
851
Eric W. Biederman881d9662007-09-17 11:56:21 -0700852struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700854 struct net_device *dev;
855 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Sasha Levinb67bfe02013-02-27 17:06:00 -0800857 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (dev->ifindex == ifindex)
859 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return NULL;
862}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700863EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000865/**
866 * dev_get_by_index_rcu - find a device by its ifindex
867 * @net: the applicable net namespace
868 * @ifindex: index of device
869 *
870 * Search for an interface by index. Returns %NULL if the device
871 * is not found or a pointer to the device. The device has not
872 * had its reference counter increased so the caller must be careful
873 * about locking. The caller must hold RCU lock.
874 */
875
876struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
877{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000878 struct net_device *dev;
879 struct hlist_head *head = dev_index_hash(net, ifindex);
880
Sasha Levinb67bfe02013-02-27 17:06:00 -0800881 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000882 if (dev->ifindex == ifindex)
883 return dev;
884
885 return NULL;
886}
887EXPORT_SYMBOL(dev_get_by_index_rcu);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890/**
891 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700892 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * @ifindex: index of device
894 *
895 * Search for an interface by index. Returns NULL if the device
896 * is not found or a pointer to the device. The device returned has
897 * had a reference added and the pointer is safe until the user calls
898 * dev_put to indicate they have finished with it.
899 */
900
Eric W. Biederman881d9662007-09-17 11:56:21 -0700901struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct net_device *dev;
904
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000905 rcu_read_lock();
906 dev = dev_get_by_index_rcu(net, ifindex);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800907 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000908 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return dev;
910}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700911EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200914 * dev_get_by_napi_id - find a device by napi_id
915 * @napi_id: ID of the NAPI struct
916 *
917 * Search for an interface by NAPI ID. Returns %NULL if the device
918 * is not found or a pointer to the device. The device has not had
919 * its reference counter increased so the caller must be careful
920 * about locking. The caller must hold RCU lock.
921 */
922
923struct net_device *dev_get_by_napi_id(unsigned int napi_id)
924{
925 struct napi_struct *napi;
926
927 WARN_ON_ONCE(!rcu_read_lock_held());
928
929 if (napi_id < MIN_NAPI_ID)
930 return NULL;
931
932 napi = napi_by_id(napi_id);
933
934 return napi ? napi->dev : NULL;
935}
936EXPORT_SYMBOL(dev_get_by_napi_id);
937
938/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200939 * netdev_get_name - get a netdevice name, knowing its ifindex.
940 * @net: network namespace
941 * @name: a pointer to the buffer where the name will be stored.
942 * @ifindex: the ifindex of the interface to get the name from.
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200943 */
944int netdev_get_name(struct net *net, char *name, int ifindex)
945{
946 struct net_device *dev;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200947 int ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200948
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200949 down_read(&devnet_rename_sem);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200950 rcu_read_lock();
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200951
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200952 dev = dev_get_by_index_rcu(net, ifindex);
953 if (!dev) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200954 ret = -ENODEV;
955 goto out;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200956 }
957
958 strcpy(name, dev->name);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200959
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200960 ret = 0;
961out:
962 rcu_read_unlock();
963 up_read(&devnet_rename_sem);
964 return ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200965}
966
967/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000968 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700969 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * @type: media type of device
971 * @ha: hardware address
972 *
973 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800974 * is not found or a pointer to the device.
975 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000976 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * and the caller must therefore be careful about locking
978 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
980
Eric Dumazet941666c2010-12-05 01:23:53 +0000981struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
982 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct net_device *dev;
985
Eric Dumazet941666c2010-12-05 01:23:53 +0000986 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (dev->type == type &&
988 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700989 return dev;
990
991 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
Eric Dumazet941666c2010-12-05 01:23:53 +0000993EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300994
Eric W. Biederman881d9662007-09-17 11:56:21 -0700995struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000997 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000999 rcu_read_lock();
1000 for_each_netdev_rcu(net, dev)
1001 if (dev->type == type) {
1002 dev_hold(dev);
1003 ret = dev;
1004 break;
1005 }
1006 rcu_read_unlock();
1007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009EXPORT_SYMBOL(dev_getfirstbyhwtype);
1010
1011/**
WANG Cong6c555492014-09-11 15:35:09 -07001012 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001013 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * @if_flags: IFF_* values
1015 * @mask: bitmask of bits in if_flags to check
1016 *
1017 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001018 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001019 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
1021
WANG Cong6c555492014-09-11 15:35:09 -07001022struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1023 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001025 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
WANG Cong6c555492014-09-11 15:35:09 -07001027 ASSERT_RTNL();
1028
Pavel Emelianov7562f872007-05-03 15:13:45 -07001029 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001030 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001032 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034 }
1035 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
WANG Cong6c555492014-09-11 15:35:09 -07001038EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040/**
1041 * dev_valid_name - check if name is okay for network device
1042 * @name: name string
1043 *
1044 * Network device names need to be valid file names to
Randy Dunlap4250b752020-09-17 21:35:15 -07001045 * allow sysfs to work. We also disallow any kind of
David S. Millerc7fa9d12006-08-15 16:34:13 -07001046 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
David S. Miller95f050b2012-03-06 16:12:15 -05001048bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001050 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001051 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001052 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001053 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001054 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001055 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001056
1057 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001058 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001059 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001060 name++;
1061 }
David S. Miller95f050b2012-03-06 16:12:15 -05001062 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001064EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001067 * __dev_alloc_name - allocate a name for a device
1068 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001070 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
1072 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001073 * id. It scans list of devices to build up a free map, then chooses
1074 * the first empty slot. The caller must hold the dev_base or rtnl lock
1075 * while allocating the name and adding the device in order to avoid
1076 * duplicates.
1077 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1078 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
1080
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001081static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 const char *p;
1085 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001086 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct net_device *d;
1088
Rasmus Villemoes93809102017-11-13 00:15:08 +01001089 if (!dev_valid_name(name))
1090 return -EINVAL;
1091
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001092 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (p) {
1094 /*
1095 * Verify the string as this thing may have come from
1096 * the user. There must be either one "%d" and no other "%"
1097 * characters.
1098 */
1099 if (p[1] != 'd' || strchr(p + 2, '%'))
1100 return -EINVAL;
1101
1102 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001103 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!inuse)
1105 return -ENOMEM;
1106
Eric W. Biederman881d9662007-09-17 11:56:21 -07001107 for_each_netdev(net, d) {
Jiri Bohac6c015a22021-03-18 04:42:53 +01001108 struct netdev_name_node *name_node;
1109 list_for_each_entry(name_node, &d->name_node->list, list) {
1110 if (!sscanf(name_node->name, name, &i))
1111 continue;
1112 if (i < 0 || i >= max_netdevices)
1113 continue;
1114
1115 /* avoid cases where sscanf is not exact inverse of printf */
1116 snprintf(buf, IFNAMSIZ, name, i);
1117 if (!strncmp(buf, name_node->name, IFNAMSIZ))
1118 set_bit(i, inuse);
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!sscanf(d->name, name, &i))
1121 continue;
1122 if (i < 0 || i >= max_netdevices)
1123 continue;
1124
1125 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001126 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!strncmp(buf, d->name, IFNAMSIZ))
1128 set_bit(i, inuse);
1129 }
1130
1131 i = find_first_zero_bit(inuse, max_netdevices);
1132 free_page((unsigned long) inuse);
1133 }
1134
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001135 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001136 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* It is possible to run out of possible slots
1140 * when the name is long and there isn't enough space left
1141 * for the digits, or if all bits are used.
1142 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001143 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001146static int dev_alloc_name_ns(struct net *net,
1147 struct net_device *dev,
1148 const char *name)
1149{
1150 char buf[IFNAMSIZ];
1151 int ret;
1152
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001153 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001154 ret = __dev_alloc_name(net, name, buf);
1155 if (ret >= 0)
1156 strlcpy(dev->name, buf, IFNAMSIZ);
1157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001160/**
1161 * dev_alloc_name - allocate a name for a device
1162 * @dev: device
1163 * @name: name format string
1164 *
1165 * Passed a format string - eg "lt%d" it will try and find a suitable
1166 * id. It scans list of devices to build up a free map, then chooses
1167 * the first empty slot. The caller must hold the dev_base or rtnl lock
1168 * while allocating the name and adding the device in order to avoid
1169 * duplicates.
1170 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1171 * Returns the number of the unit assigned or a negative errno code.
1172 */
1173
1174int dev_alloc_name(struct net_device *dev, const char *name)
1175{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001176 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001177}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001178EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001179
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001180static int dev_get_valid_name(struct net *net, struct net_device *dev,
1181 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001182{
David S. Miller55a5ec92018-01-02 11:45:07 -05001183 BUG_ON(!net);
1184
1185 if (!dev_valid_name(name))
1186 return -EINVAL;
1187
1188 if (strchr(name, '%'))
1189 return dev_alloc_name_ns(net, dev, name);
1190 else if (__dev_get_by_name(net, name))
1191 return -EEXIST;
1192 else if (dev->name != name)
1193 strlcpy(dev->name, name, IFNAMSIZ);
1194
1195 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198/**
1199 * dev_change_name - change name of a device
1200 * @dev: device
1201 * @newname: name (or format string) must be at least IFNAMSIZ
1202 *
1203 * Change name of a device, can pass format strings "eth%d".
1204 * for wildcarding.
1205 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001206int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Tom Gundersen238fa362014-07-14 16:37:23 +02001208 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001209 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001211 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001212 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001215 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001217 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001218
1219 /* Some auto-enslaved devices e.g. failover slaves are
1220 * special, as userspace might rename the device after
1221 * the interface had been brought up and running since
1222 * the point kernel initiated auto-enslavement. Allow
1223 * live name change even when these slave devices are
1224 * up and running.
1225 *
1226 * Typically, users of these auto-enslaving devices
1227 * don't actually care about slave name change, as
1228 * they are supposed to operate on master interface
1229 * directly.
1230 */
1231 if (dev->flags & IFF_UP &&
1232 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EBUSY;
1234
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001235 down_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001236
1237 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001238 up_write(&devnet_rename_sem);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001239 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001240 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001241
Herbert Xufcc5a032007-07-30 17:03:38 -07001242 memcpy(oldname, dev->name, IFNAMSIZ);
1243
Gao feng828de4f2012-09-13 20:58:27 +00001244 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001245 if (err < 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001246 up_write(&devnet_rename_sem);
Octavian Purdilad9031022009-11-18 02:36:59 +00001247 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001250 if (oldname[0] && !strchr(oldname, '%'))
1251 netdev_info(dev, "renamed from %s\n", oldname);
1252
Tom Gundersen238fa362014-07-14 16:37:23 +02001253 old_assign_type = dev->name_assign_type;
1254 dev->name_assign_type = NET_NAME_RENAMED;
1255
Herbert Xufcc5a032007-07-30 17:03:38 -07001256rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001257 ret = device_rename(&dev->dev, dev->name);
1258 if (ret) {
1259 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001260 dev->name_assign_type = old_assign_type;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001261 up_write(&devnet_rename_sem);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001262 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001263 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001264
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001265 up_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001266
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001267 netdev_adjacent_rename_links(dev, oldname);
1268
Herbert Xu7f988ea2007-07-30 16:35:46 -07001269 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001270 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001271 write_unlock_bh(&dev_base_lock);
1272
1273 synchronize_rcu();
1274
1275 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001276 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001277 write_unlock_bh(&dev_base_lock);
1278
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001279 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001280 ret = notifier_to_errno(ret);
1281
1282 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001283 /* err >= 0 after dev_alloc_name() or stores the first errno */
1284 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001285 err = ret;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001286 down_write(&devnet_rename_sem);
Herbert Xufcc5a032007-07-30 17:03:38 -07001287 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001288 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001289 dev->name_assign_type = old_assign_type;
1290 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001291 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001292 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001293 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001294 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001295 }
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 return err;
1299}
1300
1301/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001302 * dev_set_alias - change ifalias of a device
1303 * @dev: device
1304 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001305 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001306 *
1307 * Set ifalias for a device,
1308 */
1309int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1310{
Florian Westphal6c557002017-10-02 23:50:05 +02001311 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001312
1313 if (len >= IFALIASZ)
1314 return -EINVAL;
1315
Florian Westphal6c557002017-10-02 23:50:05 +02001316 if (len) {
1317 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1318 if (!new_alias)
1319 return -ENOMEM;
1320
1321 memcpy(new_alias->ifalias, alias, len);
1322 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001323 }
1324
Florian Westphal6c557002017-10-02 23:50:05 +02001325 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001326 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1327 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001328 mutex_unlock(&ifalias_mutex);
1329
1330 if (new_alias)
1331 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001332
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001333 return len;
1334}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001335EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001336
Florian Westphal6c557002017-10-02 23:50:05 +02001337/**
1338 * dev_get_alias - get ifalias of a device
1339 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001340 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001341 * @len: size of buffer
1342 *
1343 * get ifalias for a device. Caller must make sure dev cannot go
1344 * away, e.g. rcu read lock or own a reference count to device.
1345 */
1346int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1347{
1348 const struct dev_ifalias *alias;
1349 int ret = 0;
1350
1351 rcu_read_lock();
1352 alias = rcu_dereference(dev->ifalias);
1353 if (alias)
1354 ret = snprintf(name, len, "%s", alias->ifalias);
1355 rcu_read_unlock();
1356
1357 return ret;
1358}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001359
1360/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001361 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001362 * @dev: device to cause notification
1363 *
1364 * Called to indicate a device has changed features.
1365 */
1366void netdev_features_change(struct net_device *dev)
1367{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001368 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001369}
1370EXPORT_SYMBOL(netdev_features_change);
1371
1372/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * netdev_state_change - device changes state
1374 * @dev: device to cause notification
1375 *
1376 * Called to indicate a device has changed state. This function calls
1377 * the notifier chains for netdev_chain and sends a NEWLINK message
1378 * to the routing socket.
1379 */
1380void netdev_state_change(struct net_device *dev)
1381{
1382 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001383 struct netdev_notifier_change_info change_info = {
1384 .info.dev = dev,
1385 };
Loic Prylli54951192014-07-01 21:39:43 -07001386
David Ahern51d0c0472017-10-04 17:48:45 -07001387 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001388 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001389 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001392EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Amerigo Wangee89bab2012-08-09 22:14:56 +00001394/**
Lijun Pan7061eb82020-12-14 15:19:28 -06001395 * __netdev_notify_peers - notify network peers about existence of @dev,
1396 * to be called when rtnl lock is already held.
1397 * @dev: network device
1398 *
1399 * Generate traffic such that interested network peers are aware of
1400 * @dev, such as by generating a gratuitous ARP. This may be used when
1401 * a device wants to inform the rest of the network about some sort of
1402 * reconfiguration such as a failover event or virtual machine
1403 * migration.
1404 */
1405void __netdev_notify_peers(struct net_device *dev)
1406{
1407 ASSERT_RTNL();
1408 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1409 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1410}
1411EXPORT_SYMBOL(__netdev_notify_peers);
1412
1413/**
tcharding722c9a02017-02-09 17:56:04 +11001414 * netdev_notify_peers - notify network peers about existence of @dev
1415 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001416 *
1417 * Generate traffic such that interested network peers are aware of
1418 * @dev, such as by generating a gratuitous ARP. This may be used when
1419 * a device wants to inform the rest of the network about some sort of
1420 * reconfiguration such as a failover event or virtual machine
1421 * migration.
1422 */
1423void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001424{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001425 rtnl_lock();
Lijun Pan7061eb82020-12-14 15:19:28 -06001426 __netdev_notify_peers(dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001427 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001428}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001429EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001430
Wei Wang29863d42021-02-08 11:34:09 -08001431static int napi_threaded_poll(void *data);
1432
1433static int napi_kthread_create(struct napi_struct *n)
1434{
1435 int err = 0;
1436
1437 /* Create and wake up the kthread once to put it in
1438 * TASK_INTERRUPTIBLE mode to avoid the blocked task
1439 * warning and work with loadavg.
1440 */
1441 n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d",
1442 n->dev->name, n->napi_id);
1443 if (IS_ERR(n->thread)) {
1444 err = PTR_ERR(n->thread);
1445 pr_err("kthread_run failed with err %d\n", err);
1446 n->thread = NULL;
1447 }
1448
1449 return err;
1450}
1451
Petr Machata40c900a2018-12-06 17:05:47 +00001452static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001454 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001455 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001457 ASSERT_RTNL();
1458
Heiner Kallweitbd869242020-06-20 22:35:42 +02001459 if (!netif_device_present(dev)) {
1460 /* may be detached because parent is runtime-suspended */
1461 if (dev->dev.parent)
1462 pm_runtime_resume(dev->dev.parent);
1463 if (!netif_device_present(dev))
1464 return -ENODEV;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Neil Hormanca99ca12013-02-05 08:05:43 +00001467 /* Block netpoll from trying to do any rx path servicing.
1468 * If we don't do this there is a chance ndo_poll_controller
1469 * or ndo_poll may be running while we open the device
1470 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001471 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001472
Petr Machata40c900a2018-12-06 17:05:47 +00001473 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001474 ret = notifier_to_errno(ret);
1475 if (ret)
1476 return ret;
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001479
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001480 if (ops->ndo_validate_addr)
1481 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001482
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001483 if (!ret && ops->ndo_open)
1484 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Eric W. Biederman66b55522014-03-27 15:39:03 -07001486 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001487
Jeff Garzikbada3392007-10-23 20:19:37 -07001488 if (ret)
1489 clear_bit(__LINK_STATE_START, &dev->state);
1490 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001492 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001494 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return ret;
1498}
Patrick McHardybd380812010-02-26 06:34:53 +00001499
1500/**
1501 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001502 * @dev: device to open
1503 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001504 *
1505 * Takes a device from down to up state. The device's private open
1506 * function is invoked and then the multicast lists are loaded. Finally
1507 * the device is moved into the up state and a %NETDEV_UP message is
1508 * sent to the netdev notifier chain.
1509 *
1510 * Calling this function on an active interface is a nop. On a failure
1511 * a negative errno code is returned.
1512 */
Petr Machata00f54e62018-12-06 17:05:36 +00001513int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001514{
1515 int ret;
1516
Patrick McHardybd380812010-02-26 06:34:53 +00001517 if (dev->flags & IFF_UP)
1518 return 0;
1519
Petr Machata40c900a2018-12-06 17:05:47 +00001520 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001521 if (ret < 0)
1522 return ret;
1523
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001524 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001525 call_netdevice_notifiers(NETDEV_UP, dev);
1526
1527 return ret;
1528}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001529EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
stephen hemminger7051b882017-07-18 15:59:27 -07001531static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Octavian Purdila44345722010-12-13 12:44:07 +00001533 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001534
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001535 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001536 might_sleep();
1537
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001538 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001539 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001540 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001541
Octavian Purdila44345722010-12-13 12:44:07 +00001542 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Octavian Purdila44345722010-12-13 12:44:07 +00001544 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Octavian Purdila44345722010-12-13 12:44:07 +00001546 /* Synchronize to scheduled poll. We cannot touch poll list, it
1547 * can be even on different cpu. So just clear netif_running().
1548 *
1549 * dev->stop() will invoke napi_disable() on all of it's
1550 * napi_struct instances on this device.
1551 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001552 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Octavian Purdila44345722010-12-13 12:44:07 +00001555 dev_deactivate_many(head);
1556
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001557 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001558 const struct net_device_ops *ops = dev->netdev_ops;
1559
1560 /*
1561 * Call the device specific close. This cannot fail.
1562 * Only if device is UP
1563 *
1564 * We allow it to be called even after a DETACH hot-plug
1565 * event.
1566 */
1567 if (ops->ndo_stop)
1568 ops->ndo_stop(dev);
1569
Octavian Purdila44345722010-12-13 12:44:07 +00001570 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001571 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001572 }
Octavian Purdila44345722010-12-13 12:44:07 +00001573}
1574
stephen hemminger7051b882017-07-18 15:59:27 -07001575static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001576{
1577 LIST_HEAD(single);
1578
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001579 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001580 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001581 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001582}
1583
stephen hemminger7051b882017-07-18 15:59:27 -07001584void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001585{
1586 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001587
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001588 /* Remove the devices that don't need to be closed */
1589 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001590 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001591 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001592
1593 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001594
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001595 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001596 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001597 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001598 if (unlink)
1599 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
David S. Miller99c4a262015-03-18 22:52:33 -04001602EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001603
1604/**
1605 * dev_close - shutdown an interface.
1606 * @dev: device to shutdown
1607 *
1608 * This function moves an active device into down state. A
1609 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1610 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1611 * chain.
1612 */
stephen hemminger7051b882017-07-18 15:59:27 -07001613void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001614{
Eric Dumazete14a5992011-05-10 12:26:06 -07001615 if (dev->flags & IFF_UP) {
1616 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001617
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001618 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001619 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001620 list_del(&single);
1621 }
Patrick McHardybd380812010-02-26 06:34:53 +00001622}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001623EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001626/**
1627 * dev_disable_lro - disable Large Receive Offload on a device
1628 * @dev: device
1629 *
1630 * Disable Large Receive Offload (LRO) on a net device. Must be
1631 * called under RTNL. This is needed if received packets may be
1632 * forwarded to another interface.
1633 */
1634void dev_disable_lro(struct net_device *dev)
1635{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001636 struct net_device *lower_dev;
1637 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001638
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001639 dev->wanted_features &= ~NETIF_F_LRO;
1640 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001641
Michał Mirosław22d59692011-04-21 12:42:15 +00001642 if (unlikely(dev->features & NETIF_F_LRO))
1643 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001644
1645 netdev_for_each_lower_dev(dev, lower_dev, iter)
1646 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001647}
1648EXPORT_SYMBOL(dev_disable_lro);
1649
Michael Chan56f5aa72017-12-16 03:09:41 -05001650/**
1651 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1652 * @dev: device
1653 *
1654 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1655 * called under RTNL. This is needed if Generic XDP is installed on
1656 * the device.
1657 */
1658static void dev_disable_gro_hw(struct net_device *dev)
1659{
1660 dev->wanted_features &= ~NETIF_F_GRO_HW;
1661 netdev_update_features(dev);
1662
1663 if (unlikely(dev->features & NETIF_F_GRO_HW))
1664 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1665}
1666
Kirill Tkhaiede27622018-03-23 19:47:19 +03001667const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1668{
1669#define N(val) \
1670 case NETDEV_##val: \
1671 return "NETDEV_" __stringify(val);
1672 switch (cmd) {
1673 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1674 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1675 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1676 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1677 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1678 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1679 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001680 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1681 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001682 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001683 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001684#undef N
1685 return "UNKNOWN_NETDEV_EVENT";
1686}
1687EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1688
Jiri Pirko351638e2013-05-28 01:30:21 +00001689static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1690 struct net_device *dev)
1691{
David Ahern51d0c0472017-10-04 17:48:45 -07001692 struct netdev_notifier_info info = {
1693 .dev = dev,
1694 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001695
Jiri Pirko351638e2013-05-28 01:30:21 +00001696 return nb->notifier_call(nb, val, &info);
1697}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001698
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001699static int call_netdevice_register_notifiers(struct notifier_block *nb,
1700 struct net_device *dev)
1701{
1702 int err;
1703
1704 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1705 err = notifier_to_errno(err);
1706 if (err)
1707 return err;
1708
1709 if (!(dev->flags & IFF_UP))
1710 return 0;
1711
1712 call_netdevice_notifier(nb, NETDEV_UP, dev);
1713 return 0;
1714}
1715
1716static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1717 struct net_device *dev)
1718{
1719 if (dev->flags & IFF_UP) {
1720 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1721 dev);
1722 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1723 }
1724 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1725}
1726
1727static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1728 struct net *net)
1729{
1730 struct net_device *dev;
1731 int err;
1732
1733 for_each_netdev(net, dev) {
1734 err = call_netdevice_register_notifiers(nb, dev);
1735 if (err)
1736 goto rollback;
1737 }
1738 return 0;
1739
1740rollback:
1741 for_each_netdev_continue_reverse(net, dev)
1742 call_netdevice_unregister_notifiers(nb, dev);
1743 return err;
1744}
1745
1746static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1747 struct net *net)
1748{
1749 struct net_device *dev;
1750
1751 for_each_netdev(net, dev)
1752 call_netdevice_unregister_notifiers(nb, dev);
1753}
1754
Eric W. Biederman881d9662007-09-17 11:56:21 -07001755static int dev_boot_phase = 1;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/**
tcharding722c9a02017-02-09 17:56:04 +11001758 * register_netdevice_notifier - register a network notifier block
1759 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
tcharding722c9a02017-02-09 17:56:04 +11001761 * Register a notifier to be called when network device events occur.
1762 * The notifier passed is linked into the kernel structures and must
1763 * not be reused until it has been unregistered. A negative errno code
1764 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *
tcharding722c9a02017-02-09 17:56:04 +11001766 * When registered all registration and up events are replayed
1767 * to the new notifier to allow device to have a race free
1768 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
1770
1771int register_netdevice_notifier(struct notifier_block *nb)
1772{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001773 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int err;
1775
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001776 /* Close race with setup_net() and cleanup_net() */
1777 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001779 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001780 if (err)
1781 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001782 if (dev_boot_phase)
1783 goto unlock;
1784 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001785 err = call_netdevice_register_net_notifiers(nb, net);
1786 if (err)
1787 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001789
1790unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001792 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001794
1795rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001796 for_each_net_continue_reverse(net)
1797 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001798
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001799 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001800 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001802EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804/**
tcharding722c9a02017-02-09 17:56:04 +11001805 * unregister_netdevice_notifier - unregister a network notifier block
1806 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 *
tcharding722c9a02017-02-09 17:56:04 +11001808 * Unregister a notifier previously registered by
1809 * register_netdevice_notifier(). The notifier is unlinked into the
1810 * kernel structures and may then be reused. A negative errno code
1811 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001812 *
tcharding722c9a02017-02-09 17:56:04 +11001813 * After unregistering unregister and down device events are synthesized
1814 * for all devices on the device list to the removed notifier to remove
1815 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
1817
1818int unregister_netdevice_notifier(struct notifier_block *nb)
1819{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001820 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001821 int err;
1822
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001823 /* Close race with setup_net() and cleanup_net() */
1824 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001825 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001826 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001827 if (err)
1828 goto unlock;
1829
Jiri Pirko48b3a132020-01-25 12:17:06 +01001830 for_each_net(net)
1831 call_netdevice_unregister_net_notifiers(nb, net);
1832
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001833unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001834 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001835 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001836 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001838EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jiri Pirko1f637702020-01-25 12:17:07 +01001840static int __register_netdevice_notifier_net(struct net *net,
1841 struct notifier_block *nb,
1842 bool ignore_call_fail)
1843{
1844 int err;
1845
1846 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1847 if (err)
1848 return err;
1849 if (dev_boot_phase)
1850 return 0;
1851
1852 err = call_netdevice_register_net_notifiers(nb, net);
1853 if (err && !ignore_call_fail)
1854 goto chain_unregister;
1855
1856 return 0;
1857
1858chain_unregister:
1859 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1860 return err;
1861}
1862
1863static int __unregister_netdevice_notifier_net(struct net *net,
1864 struct notifier_block *nb)
1865{
1866 int err;
1867
1868 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1869 if (err)
1870 return err;
1871
1872 call_netdevice_unregister_net_notifiers(nb, net);
1873 return 0;
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001877 * register_netdevice_notifier_net - register a per-netns network notifier block
1878 * @net: network namespace
1879 * @nb: notifier
1880 *
1881 * Register a notifier to be called when network device events occur.
1882 * The notifier passed is linked into the kernel structures and must
1883 * not be reused until it has been unregistered. A negative errno code
1884 * is returned on a failure.
1885 *
1886 * When registered all registration and up events are replayed
1887 * to the new notifier to allow device to have a race free
1888 * view of the network device list.
1889 */
1890
1891int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1892{
1893 int err;
1894
1895 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001896 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001897 rtnl_unlock();
1898 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001899}
1900EXPORT_SYMBOL(register_netdevice_notifier_net);
1901
1902/**
1903 * unregister_netdevice_notifier_net - unregister a per-netns
1904 * network notifier block
1905 * @net: network namespace
1906 * @nb: notifier
1907 *
1908 * Unregister a notifier previously registered by
1909 * register_netdevice_notifier(). The notifier is unlinked into the
1910 * kernel structures and may then be reused. A negative errno code
1911 * is returned on a failure.
1912 *
1913 * After unregistering unregister and down device events are synthesized
1914 * for all devices on the device list to the removed notifier to remove
1915 * the need for special case cleanup code.
1916 */
1917
1918int unregister_netdevice_notifier_net(struct net *net,
1919 struct notifier_block *nb)
1920{
1921 int err;
1922
1923 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001924 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001925 rtnl_unlock();
1926 return err;
1927}
1928EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1929
Jiri Pirko93642e12020-01-25 12:17:08 +01001930int register_netdevice_notifier_dev_net(struct net_device *dev,
1931 struct notifier_block *nb,
1932 struct netdev_net_notifier *nn)
1933{
1934 int err;
1935
1936 rtnl_lock();
1937 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1938 if (!err) {
1939 nn->nb = nb;
1940 list_add(&nn->list, &dev->net_notifier_list);
1941 }
1942 rtnl_unlock();
1943 return err;
1944}
1945EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1946
1947int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1948 struct notifier_block *nb,
1949 struct netdev_net_notifier *nn)
1950{
1951 int err;
1952
1953 rtnl_lock();
1954 list_del(&nn->list);
1955 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1956 rtnl_unlock();
1957 return err;
1958}
1959EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1960
1961static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1962 struct net *net)
1963{
1964 struct netdev_net_notifier *nn;
1965
1966 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1967 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1968 __register_netdevice_notifier_net(net, nn->nb, true);
1969 }
1970}
1971
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001972/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001973 * call_netdevice_notifiers_info - call all network notifier blocks
1974 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001975 * @info: notifier information data
1976 *
1977 * Call all network notifier blocks. Parameters and return value
1978 * are as for raw_notifier_call_chain().
1979 */
1980
stephen hemminger1d143d92013-12-29 14:01:29 -08001981static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001982 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001983{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001984 struct net *net = dev_net(info->dev);
1985 int ret;
1986
Jiri Pirko351638e2013-05-28 01:30:21 +00001987 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001988
1989 /* Run per-netns notifier block chain first, then run the global one.
1990 * Hopefully, one day, the global one is going to be removed after
1991 * all notifier block registrators get converted to be per-netns.
1992 */
1993 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1994 if (ret & NOTIFY_STOP_MASK)
1995 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00001996 return raw_notifier_call_chain(&netdev_chain, val, info);
1997}
Jiri Pirko351638e2013-05-28 01:30:21 +00001998
Petr Machata26372602018-12-06 17:05:45 +00001999static int call_netdevice_notifiers_extack(unsigned long val,
2000 struct net_device *dev,
2001 struct netlink_ext_ack *extack)
2002{
2003 struct netdev_notifier_info info = {
2004 .dev = dev,
2005 .extack = extack,
2006 };
2007
2008 return call_netdevice_notifiers_info(val, &info);
2009}
2010
Jiri Pirko351638e2013-05-28 01:30:21 +00002011/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * call_netdevice_notifiers - call all network notifier blocks
2013 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002014 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 *
2016 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07002017 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
2019
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07002020int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Petr Machata26372602018-12-06 17:05:45 +00002022 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
stephen hemmingeredf947f2011-03-24 13:24:01 +00002024EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02002026/**
2027 * call_netdevice_notifiers_mtu - call all network notifier blocks
2028 * @val: value passed unmodified to notifier function
2029 * @dev: net_device pointer passed unmodified to notifier function
2030 * @arg: additional u32 argument passed to the notifier function
2031 *
2032 * Call all network notifier blocks. Parameters and return value
2033 * are as for raw_notifier_call_chain().
2034 */
2035static int call_netdevice_notifiers_mtu(unsigned long val,
2036 struct net_device *dev, u32 arg)
2037{
2038 struct netdev_notifier_info_ext info = {
2039 .info.dev = dev,
2040 .ext.mtu = arg,
2041 };
2042
2043 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2044
2045 return call_netdevice_notifiers_info(val, &info.info);
2046}
2047
Pablo Neira1cf519002015-05-13 18:19:37 +02002048#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002049static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002050
2051void net_inc_ingress_queue(void)
2052{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002053 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002054}
2055EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2056
2057void net_dec_ingress_queue(void)
2058{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002059 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002060}
2061EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2062#endif
2063
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002064#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002065static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002066
2067void net_inc_egress_queue(void)
2068{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002069 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002070}
2071EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2072
2073void net_dec_egress_queue(void)
2074{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002075 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002076}
2077EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2078#endif
2079
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002080static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002081#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002082static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002083static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002084static void netstamp_clear(struct work_struct *work)
2085{
2086 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002087 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002088
Eric Dumazet13baa002017-03-01 14:28:39 -08002089 wanted = atomic_add_return(deferred, &netstamp_wanted);
2090 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002091 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002092 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002093 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002094}
2095static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098void net_enable_timestamp(void)
2099{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002100#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002101 int wanted;
2102
2103 while (1) {
2104 wanted = atomic_read(&netstamp_wanted);
2105 if (wanted <= 0)
2106 break;
2107 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2108 return;
2109 }
2110 atomic_inc(&netstamp_needed_deferred);
2111 schedule_work(&netstamp_work);
2112#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002113 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002116EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118void net_disable_timestamp(void)
2119{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002120#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002121 int wanted;
2122
2123 while (1) {
2124 wanted = atomic_read(&netstamp_wanted);
2125 if (wanted <= 1)
2126 break;
2127 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2128 return;
2129 }
2130 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002131 schedule_work(&netstamp_work);
2132#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002133 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002136EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Eric Dumazet3b098e22010-05-15 23:57:10 -07002138static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002140 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002141 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002142 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002145#define net_timestamp_check(COND, SKB) \
2146 if (static_branch_unlikely(&netstamp_needed_key)) { \
2147 if ((COND) && !(SKB)->tstamp) \
2148 __net_timestamp(SKB); \
2149 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002150
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002151bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002152{
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002153 return __is_skb_forwardable(dev, skb, true);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002154}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002155EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002156
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002157static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb,
2158 bool check_mtu)
Herbert Xua0265d22014-04-17 13:45:03 +08002159{
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002160 int ret = ____dev_forward_skb(dev, skb, check_mtu);
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002161
2162 if (likely(!ret)) {
2163 skb->protocol = eth_type_trans(skb, dev);
2164 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002165 }
2166
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002167 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002168}
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002169
2170int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2171{
2172 return __dev_forward_skb2(dev, skb, true);
2173}
Herbert Xua0265d22014-04-17 13:45:03 +08002174EXPORT_SYMBOL_GPL(__dev_forward_skb);
2175
Arnd Bergmann44540962009-11-26 06:07:08 +00002176/**
2177 * dev_forward_skb - loopback an skb to another netif
2178 *
2179 * @dev: destination network device
2180 * @skb: buffer to forward
2181 *
2182 * return values:
2183 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002184 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002185 *
2186 * dev_forward_skb can be used for injecting an skb from the
2187 * start_xmit function of one device into the receive queue
2188 * of another device.
2189 *
2190 * The receiving device may be in another namespace, so
2191 * we have to clear all information in the skb that could
2192 * impact namespace isolation.
2193 */
2194int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2195{
Herbert Xua0265d22014-04-17 13:45:03 +08002196 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002197}
2198EXPORT_SYMBOL_GPL(dev_forward_skb);
2199
Jesper Dangaard Brouer5f7d5722021-02-09 14:38:29 +01002200int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb)
2201{
2202 return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb);
2203}
2204
Changli Gao71d9dec2010-12-15 19:57:25 +00002205static inline int deliver_skb(struct sk_buff *skb,
2206 struct packet_type *pt_prev,
2207 struct net_device *orig_dev)
2208{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002209 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002210 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002211 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002212 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2213}
2214
Salam Noureddine7866a622015-01-27 11:35:48 -08002215static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2216 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002217 struct net_device *orig_dev,
2218 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002219 struct list_head *ptype_list)
2220{
2221 struct packet_type *ptype, *pt_prev = *pt;
2222
2223 list_for_each_entry_rcu(ptype, ptype_list, list) {
2224 if (ptype->type != type)
2225 continue;
2226 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002227 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002228 pt_prev = ptype;
2229 }
2230 *pt = pt_prev;
2231}
2232
Eric Leblondc0de08d2012-08-16 22:02:58 +00002233static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2234{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002235 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002236 return false;
2237
2238 if (ptype->id_match)
2239 return ptype->id_match(ptype, skb->sk);
2240 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2241 return true;
2242
2243 return false;
2244}
2245
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002246/**
2247 * dev_nit_active - return true if any network interface taps are in use
2248 *
2249 * @dev: network device to check for the presence of taps
2250 */
2251bool dev_nit_active(struct net_device *dev)
2252{
2253 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2254}
2255EXPORT_SYMBOL_GPL(dev_nit_active);
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257/*
2258 * Support routine. Sends outgoing frames to any network
2259 * taps currently in use.
2260 */
2261
David Ahern74b20582016-05-10 11:19:50 -07002262void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002265 struct sk_buff *skb2 = NULL;
2266 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002267 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002270again:
2271 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002272 if (ptype->ignore_outgoing)
2273 continue;
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* Never send packets back to the socket
2276 * they originated from - MvS (miquels@drinkel.ow.org)
2277 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002278 if (skb_loop_sk(ptype, skb))
2279 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002280
Salam Noureddine7866a622015-01-27 11:35:48 -08002281 if (pt_prev) {
2282 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002283 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002284 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002286
2287 /* need to clone skb, done only once */
2288 skb2 = skb_clone(skb, GFP_ATOMIC);
2289 if (!skb2)
2290 goto out_unlock;
2291
2292 net_timestamp_set(skb2);
2293
2294 /* skb->nh should be correctly
2295 * set by sender, so that the second statement is
2296 * just protection against buggy protocols.
2297 */
2298 skb_reset_mac_header(skb2);
2299
2300 if (skb_network_header(skb2) < skb2->data ||
2301 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2302 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2303 ntohs(skb2->protocol),
2304 dev->name);
2305 skb_reset_network_header(skb2);
2306 }
2307
2308 skb2->transport_header = skb2->network_header;
2309 skb2->pkt_type = PACKET_OUTGOING;
2310 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002312
2313 if (ptype_list == &ptype_all) {
2314 ptype_list = &dev->ptype_all;
2315 goto again;
2316 }
2317out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002318 if (pt_prev) {
2319 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2320 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2321 else
2322 kfree_skb(skb2);
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 rcu_read_unlock();
2325}
David Ahern74b20582016-05-10 11:19:50 -07002326EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Ben Hutchings2c530402012-07-10 10:55:09 +00002328/**
2329 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002330 * @dev: Network device
2331 * @txq: number of queues available
2332 *
2333 * If real_num_tx_queues is changed the tc mappings may no longer be
2334 * valid. To resolve this verify the tc mapping remains valid and if
2335 * not NULL the mapping. With no priorities mapping to this
2336 * offset/count pair it will no longer be used. In the worst case TC0
2337 * is invalid nothing can be done so disable priority mappings. If is
2338 * expected that drivers will fix this mapping if they can before
2339 * calling netif_set_real_num_tx_queues.
2340 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002341static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002342{
2343 int i;
2344 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2345
2346 /* If TC0 is invalidated disable TC mapping */
2347 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002348 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002349 dev->num_tc = 0;
2350 return;
2351 }
2352
2353 /* Invalidated prio to tc mappings set to TC0 */
2354 for (i = 1; i < TC_BITMASK + 1; i++) {
2355 int q = netdev_get_prio_tc_map(dev, i);
2356
2357 tc = &dev->tc_to_txq[q];
2358 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002359 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2360 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002361 netdev_set_prio_tc_map(dev, i, 0);
2362 }
2363 }
2364}
2365
Alexander Duyck8d059b02016-10-28 11:43:49 -04002366int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2367{
2368 if (dev->num_tc) {
2369 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2370 int i;
2371
Alexander Duyckffcfe252018-07-09 12:19:38 -04002372 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002373 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2374 if ((txq - tc->offset) < tc->count)
2375 return i;
2376 }
2377
Alexander Duyckffcfe252018-07-09 12:19:38 -04002378 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002379 return -1;
2380 }
2381
2382 return 0;
2383}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002384EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002385
Alexander Duyck537c00d2013-01-10 08:57:02 +00002386#ifdef CONFIG_XPS
Vladimir Oltean5da9ace32021-03-22 13:30:19 +02002387static struct static_key xps_needed __read_mostly;
2388static struct static_key xps_rxqs_needed __read_mostly;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002389static DEFINE_MUTEX(xps_map_mutex);
2390#define xmap_dereference(P) \
2391 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2392
Alexander Duyck6234f872016-10-28 11:46:49 -04002393static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002394 struct xps_dev_maps *old_maps, int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002395{
2396 struct xps_map *map = NULL;
2397 int pos;
2398
2399 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002400 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002401 if (!map)
2402 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002403
Alexander Duyck6234f872016-10-28 11:46:49 -04002404 for (pos = map->len; pos--;) {
2405 if (map->queues[pos] != index)
2406 continue;
2407
2408 if (map->len > 1) {
2409 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002410 break;
2411 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002412
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002413 if (old_maps)
2414 RCU_INIT_POINTER(old_maps->attr_map[tci], NULL);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002415 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002416 kfree_rcu(map, rcu);
2417 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002418 }
2419
Alexander Duyck6234f872016-10-28 11:46:49 -04002420 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002421}
2422
Alexander Duyck6234f872016-10-28 11:46:49 -04002423static bool remove_xps_queue_cpu(struct net_device *dev,
2424 struct xps_dev_maps *dev_maps,
2425 int cpu, u16 offset, u16 count)
2426{
Antoine Tenart255c04a2021-03-18 19:37:43 +01002427 int num_tc = dev_maps->num_tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002428 bool active = false;
2429 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002430
Alexander Duyck184c4492016-10-28 11:50:13 -04002431 for (tci = cpu * num_tc; num_tc--; tci++) {
2432 int i, j;
2433
2434 for (i = count, j = offset; i--; j++) {
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002435 if (!remove_xps_queue(dev_maps, NULL, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002436 break;
2437 }
2438
2439 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002440 }
2441
Alexander Duyck184c4492016-10-28 11:50:13 -04002442 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002443}
2444
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002445static void reset_xps_maps(struct net_device *dev,
2446 struct xps_dev_maps *dev_maps,
Antoine Tenart044ab862021-03-18 19:37:46 +01002447 enum xps_map_type type)
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002448{
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002449 static_key_slow_dec_cpuslocked(&xps_needed);
Antoine Tenart044ab862021-03-18 19:37:46 +01002450 if (type == XPS_RXQS)
2451 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2452
2453 RCU_INIT_POINTER(dev->xps_maps[type], NULL);
2454
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002455 kfree_rcu(dev_maps, rcu);
2456}
2457
Antoine Tenart044ab862021-03-18 19:37:46 +01002458static void clean_xps_maps(struct net_device *dev, enum xps_map_type type,
2459 u16 offset, u16 count)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002460{
Antoine Tenart044ab862021-03-18 19:37:46 +01002461 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002462 bool active = false;
2463 int i, j;
2464
Antoine Tenart044ab862021-03-18 19:37:46 +01002465 dev_maps = xmap_dereference(dev->xps_maps[type]);
2466 if (!dev_maps)
2467 return;
2468
Antoine Tenart6f361582021-03-18 19:37:45 +01002469 for (j = 0; j < dev_maps->nr_ids; j++)
2470 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002471 if (!active)
Antoine Tenart044ab862021-03-18 19:37:46 +01002472 reset_xps_maps(dev, dev_maps, type);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002473
Antoine Tenart044ab862021-03-18 19:37:46 +01002474 if (type == XPS_CPUS) {
Antoine Tenart6f361582021-03-18 19:37:45 +01002475 for (i = offset + (count - 1); count--; i--)
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002476 netdev_queue_numa_node_write(
Antoine Tenart6f361582021-03-18 19:37:45 +01002477 netdev_get_tx_queue(dev, i), NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002478 }
2479}
2480
Alexander Duyck6234f872016-10-28 11:46:49 -04002481static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2482 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002483{
Amritha Nambiar04157462018-06-29 21:26:46 -07002484 if (!static_key_false(&xps_needed))
2485 return;
2486
Andrei Vagin4d99f662018-08-08 20:07:35 -07002487 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002488 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002489
Antoine Tenart044ab862021-03-18 19:37:46 +01002490 if (static_key_false(&xps_rxqs_needed))
2491 clean_xps_maps(dev, XPS_RXQS, offset, count);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002492
Antoine Tenart044ab862021-03-18 19:37:46 +01002493 clean_xps_maps(dev, XPS_CPUS, offset, count);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002494
Alexander Duyck537c00d2013-01-10 08:57:02 +00002495 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002496 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002497}
2498
Alexander Duyck6234f872016-10-28 11:46:49 -04002499static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2500{
2501 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2502}
2503
Amritha Nambiar80d19662018-06-29 21:26:41 -07002504static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2505 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002506{
2507 struct xps_map *new_map;
2508 int alloc_len = XPS_MIN_MAP_ALLOC;
2509 int i, pos;
2510
2511 for (pos = 0; map && pos < map->len; pos++) {
2512 if (map->queues[pos] != index)
2513 continue;
2514 return map;
2515 }
2516
Amritha Nambiar80d19662018-06-29 21:26:41 -07002517 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002518 if (map) {
2519 if (pos < map->alloc_len)
2520 return map;
2521
2522 alloc_len = map->alloc_len * 2;
2523 }
2524
Amritha Nambiar80d19662018-06-29 21:26:41 -07002525 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2526 * map
2527 */
2528 if (is_rxqs_map)
2529 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2530 else
2531 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2532 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002533 if (!new_map)
2534 return NULL;
2535
2536 for (i = 0; i < pos; i++)
2537 new_map->queues[i] = map->queues[i];
2538 new_map->alloc_len = alloc_len;
2539 new_map->len = pos;
2540
2541 return new_map;
2542}
2543
Antoine Tenart402fbb92021-03-18 19:37:47 +01002544/* Copy xps maps at a given index */
2545static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps,
2546 struct xps_dev_maps *new_dev_maps, int index,
2547 int tc, bool skip_tc)
2548{
2549 int i, tci = index * dev_maps->num_tc;
2550 struct xps_map *map;
2551
2552 /* copy maps belonging to foreign traffic classes */
2553 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2554 if (i == tc && skip_tc)
2555 continue;
2556
2557 /* fill in the new device map from the old device map */
2558 map = xmap_dereference(dev_maps->attr_map[tci]);
2559 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2560 }
2561}
2562
Andrei Vagin4d99f662018-08-08 20:07:35 -07002563/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002564int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
Antoine Tenart044ab862021-03-18 19:37:46 +01002565 u16 index, enum xps_map_type type)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002566{
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002567 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
Antoine Tenart6f361582021-03-18 19:37:45 +01002568 const unsigned long *online_mask = NULL;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002569 bool active = false, copy = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002570 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002571 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002572 struct xps_map *map, *new_map;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002573 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002574
Alexander Duyck184c4492016-10-28 11:50:13 -04002575 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002576 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002577 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002578 if (num_tc < 0)
2579 return -EINVAL;
2580
2581 /* If queue belongs to subordinate dev use its map */
2582 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2583
Alexander Duyck184c4492016-10-28 11:50:13 -04002584 tc = netdev_txq_to_tc(dev, index);
2585 if (tc < 0)
2586 return -EINVAL;
2587 }
2588
Amritha Nambiar80d19662018-06-29 21:26:41 -07002589 mutex_lock(&xps_map_mutex);
Antoine Tenart044ab862021-03-18 19:37:46 +01002590
2591 dev_maps = xmap_dereference(dev->xps_maps[type]);
2592 if (type == XPS_RXQS) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002593 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002594 nr_ids = dev->num_rx_queues;
2595 } else {
2596 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
Antoine Tenart6f361582021-03-18 19:37:45 +01002597 if (num_possible_cpus() > 1)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002598 online_mask = cpumask_bits(cpu_online_mask);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002599 nr_ids = nr_cpu_ids;
2600 }
2601
Alexander Duyck184c4492016-10-28 11:50:13 -04002602 if (maps_sz < L1_CACHE_BYTES)
2603 maps_sz = L1_CACHE_BYTES;
2604
Antoine Tenart255c04a2021-03-18 19:37:43 +01002605 /* The old dev_maps could be larger or smaller than the one we're
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002606 * setting up now, as dev->num_tc or nr_ids could have been updated in
2607 * between. We could try to be smart, but let's be safe instead and only
2608 * copy foreign traffic classes if the two map sizes match.
Antoine Tenart255c04a2021-03-18 19:37:43 +01002609 */
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002610 if (dev_maps &&
2611 dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids)
Antoine Tenart255c04a2021-03-18 19:37:43 +01002612 copy = true;
2613
Alexander Duyck01c5f862013-01-10 08:57:35 +00002614 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002615 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2616 j < nr_ids;) {
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002617 if (!new_dev_maps) {
Antoine Tenart255c04a2021-03-18 19:37:43 +01002618 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2619 if (!new_dev_maps) {
2620 mutex_unlock(&xps_map_mutex);
2621 return -ENOMEM;
2622 }
2623
Antoine Tenart5478fcd2021-03-18 19:37:44 +01002624 new_dev_maps->nr_ids = nr_ids;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002625 new_dev_maps->num_tc = num_tc;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002626 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002627
Amritha Nambiar80d19662018-06-29 21:26:41 -07002628 tci = j * num_tc + tc;
Antoine Tenart255c04a2021-03-18 19:37:43 +01002629 map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002630
Antoine Tenart044ab862021-03-18 19:37:46 +01002631 map = expand_xps_map(map, j, index, type == XPS_RXQS);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002632 if (!map)
2633 goto error;
2634
Amritha Nambiar80d19662018-06-29 21:26:41 -07002635 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002636 }
2637
2638 if (!new_dev_maps)
2639 goto out_no_new_maps;
2640
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002641 if (!dev_maps) {
2642 /* Increment static keys at most once per type */
2643 static_key_slow_inc_cpuslocked(&xps_needed);
Antoine Tenart044ab862021-03-18 19:37:46 +01002644 if (type == XPS_RXQS)
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002645 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2646 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002647
Antoine Tenart6f361582021-03-18 19:37:45 +01002648 for (j = 0; j < nr_ids; j++) {
Antoine Tenart402fbb92021-03-18 19:37:47 +01002649 bool skip_tc = false;
Alexander Duyck184c4492016-10-28 11:50:13 -04002650
Amritha Nambiar80d19662018-06-29 21:26:41 -07002651 tci = j * num_tc + tc;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002652 if (netif_attr_test_mask(j, mask, nr_ids) &&
2653 netif_attr_test_online(j, online_mask, nr_ids)) {
2654 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002655 int pos = 0;
2656
Antoine Tenart402fbb92021-03-18 19:37:47 +01002657 skip_tc = true;
2658
Amritha Nambiar80d19662018-06-29 21:26:41 -07002659 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002660 while ((pos < map->len) && (map->queues[pos] != index))
2661 pos++;
2662
2663 if (pos == map->len)
2664 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002665#ifdef CONFIG_NUMA
Antoine Tenart044ab862021-03-18 19:37:46 +01002666 if (type == XPS_CPUS) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002667 if (numa_node_id == -2)
2668 numa_node_id = cpu_to_node(j);
2669 else if (numa_node_id != cpu_to_node(j))
2670 numa_node_id = -1;
2671 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002672#endif
Alexander Duyck537c00d2013-01-10 08:57:02 +00002673 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002674
Antoine Tenart402fbb92021-03-18 19:37:47 +01002675 if (copy)
2676 xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc,
2677 skip_tc);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002678 }
2679
Antoine Tenart044ab862021-03-18 19:37:46 +01002680 rcu_assign_pointer(dev->xps_maps[type], new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002681
Alexander Duyck537c00d2013-01-10 08:57:02 +00002682 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002683 if (!dev_maps)
2684 goto out_no_old_maps;
2685
Antoine Tenart6f361582021-03-18 19:37:45 +01002686 for (j = 0; j < dev_maps->nr_ids; j++) {
Antoine Tenart255c04a2021-03-18 19:37:43 +01002687 for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002688 map = xmap_dereference(dev_maps->attr_map[tci]);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002689 if (!map)
2690 continue;
2691
2692 if (copy) {
2693 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2694 if (map == new_map)
2695 continue;
2696 }
2697
Antoine Tenart75b27582021-03-18 19:37:52 +01002698 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002699 kfree_rcu(map, rcu);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002700 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002701 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002702
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002703 old_dev_maps = dev_maps;
Alexander Duyck184c4492016-10-28 11:50:13 -04002704
2705out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002706 dev_maps = new_dev_maps;
2707 active = true;
2708
2709out_no_new_maps:
Antoine Tenart044ab862021-03-18 19:37:46 +01002710 if (type == XPS_CPUS)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002711 /* update Tx queue numa node */
2712 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2713 (numa_node_id >= 0) ?
2714 numa_node_id : NUMA_NO_NODE);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002715
Alexander Duyck01c5f862013-01-10 08:57:35 +00002716 if (!dev_maps)
2717 goto out_no_maps;
2718
Amritha Nambiar80d19662018-06-29 21:26:41 -07002719 /* removes tx-queue from unused CPUs/rx-queues */
Antoine Tenart6f361582021-03-18 19:37:45 +01002720 for (j = 0; j < dev_maps->nr_ids; j++) {
Antoine Tenart132f7432021-03-18 19:37:48 +01002721 tci = j * dev_maps->num_tc;
2722
2723 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2724 if (i == tc &&
2725 netif_attr_test_mask(j, mask, dev_maps->nr_ids) &&
2726 netif_attr_test_online(j, online_mask, dev_maps->nr_ids))
2727 continue;
2728
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002729 active |= remove_xps_queue(dev_maps,
2730 copy ? old_dev_maps : NULL,
2731 tci, index);
Antoine Tenart132f7432021-03-18 19:37:48 +01002732 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002733 }
2734
Antoine Tenart2d05bf02021-03-18 19:37:51 +01002735 if (old_dev_maps)
2736 kfree_rcu(old_dev_maps, rcu);
2737
Alexander Duyck01c5f862013-01-10 08:57:35 +00002738 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002739 if (!active)
Antoine Tenart044ab862021-03-18 19:37:46 +01002740 reset_xps_maps(dev, dev_maps, type);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002741
2742out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002743 mutex_unlock(&xps_map_mutex);
2744
2745 return 0;
2746error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002747 /* remove any maps that we added */
Antoine Tenart6f361582021-03-18 19:37:45 +01002748 for (j = 0; j < nr_ids; j++) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002749 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2750 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Antoine Tenart255c04a2021-03-18 19:37:43 +01002751 map = copy ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002752 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002753 NULL;
2754 if (new_map && new_map != map)
2755 kfree(new_map);
2756 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002757 }
2758
Alexander Duyck537c00d2013-01-10 08:57:02 +00002759 mutex_unlock(&xps_map_mutex);
2760
Alexander Duyck537c00d2013-01-10 08:57:02 +00002761 kfree(new_dev_maps);
2762 return -ENOMEM;
2763}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002764EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002765
2766int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2767 u16 index)
2768{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002769 int ret;
2770
2771 cpus_read_lock();
Antoine Tenart044ab862021-03-18 19:37:46 +01002772 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002773 cpus_read_unlock();
2774
2775 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002776}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002777EXPORT_SYMBOL(netif_set_xps_queue);
2778
2779#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002780static void netdev_unbind_all_sb_channels(struct net_device *dev)
2781{
2782 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2783
2784 /* Unbind any subordinate channels */
2785 while (txq-- != &dev->_tx[0]) {
2786 if (txq->sb_dev)
2787 netdev_unbind_sb_channel(dev, txq->sb_dev);
2788 }
2789}
2790
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002791void netdev_reset_tc(struct net_device *dev)
2792{
Alexander Duyck6234f872016-10-28 11:46:49 -04002793#ifdef CONFIG_XPS
2794 netif_reset_xps_queues_gt(dev, 0);
2795#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002796 netdev_unbind_all_sb_channels(dev);
2797
2798 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002799 dev->num_tc = 0;
2800 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2801 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2802}
2803EXPORT_SYMBOL(netdev_reset_tc);
2804
2805int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2806{
2807 if (tc >= dev->num_tc)
2808 return -EINVAL;
2809
Alexander Duyck6234f872016-10-28 11:46:49 -04002810#ifdef CONFIG_XPS
2811 netif_reset_xps_queues(dev, offset, count);
2812#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002813 dev->tc_to_txq[tc].count = count;
2814 dev->tc_to_txq[tc].offset = offset;
2815 return 0;
2816}
2817EXPORT_SYMBOL(netdev_set_tc_queue);
2818
2819int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2820{
2821 if (num_tc > TC_MAX_QUEUE)
2822 return -EINVAL;
2823
Alexander Duyck6234f872016-10-28 11:46:49 -04002824#ifdef CONFIG_XPS
2825 netif_reset_xps_queues_gt(dev, 0);
2826#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002827 netdev_unbind_all_sb_channels(dev);
2828
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002829 dev->num_tc = num_tc;
2830 return 0;
2831}
2832EXPORT_SYMBOL(netdev_set_num_tc);
2833
Alexander Duyckffcfe252018-07-09 12:19:38 -04002834void netdev_unbind_sb_channel(struct net_device *dev,
2835 struct net_device *sb_dev)
2836{
2837 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2838
2839#ifdef CONFIG_XPS
2840 netif_reset_xps_queues_gt(sb_dev, 0);
2841#endif
2842 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2843 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2844
2845 while (txq-- != &dev->_tx[0]) {
2846 if (txq->sb_dev == sb_dev)
2847 txq->sb_dev = NULL;
2848 }
2849}
2850EXPORT_SYMBOL(netdev_unbind_sb_channel);
2851
2852int netdev_bind_sb_channel_queue(struct net_device *dev,
2853 struct net_device *sb_dev,
2854 u8 tc, u16 count, u16 offset)
2855{
2856 /* Make certain the sb_dev and dev are already configured */
2857 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2858 return -EINVAL;
2859
2860 /* We cannot hand out queues we don't have */
2861 if ((offset + count) > dev->real_num_tx_queues)
2862 return -EINVAL;
2863
2864 /* Record the mapping */
2865 sb_dev->tc_to_txq[tc].count = count;
2866 sb_dev->tc_to_txq[tc].offset = offset;
2867
2868 /* Provide a way for Tx queue to find the tc_to_txq map or
2869 * XPS map for itself.
2870 */
2871 while (count--)
2872 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2873
2874 return 0;
2875}
2876EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2877
2878int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2879{
2880 /* Do not use a multiqueue device to represent a subordinate channel */
2881 if (netif_is_multiqueue(dev))
2882 return -ENODEV;
2883
2884 /* We allow channels 1 - 32767 to be used for subordinate channels.
2885 * Channel 0 is meant to be "native" mode and used only to represent
2886 * the main root device. We allow writing 0 to reset the device back
2887 * to normal mode after being used as a subordinate channel.
2888 */
2889 if (channel > S16_MAX)
2890 return -EINVAL;
2891
2892 dev->num_tc = -channel;
2893
2894 return 0;
2895}
2896EXPORT_SYMBOL(netdev_set_sb_channel);
2897
John Fastabendf0796d52010-07-01 13:21:57 +00002898/*
2899 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002900 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002901 */
Tom Herberte6484932010-10-18 18:04:39 +00002902int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002903{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002904 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002905 int rc;
2906
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002907 disabling = txq < dev->real_num_tx_queues;
2908
Tom Herberte6484932010-10-18 18:04:39 +00002909 if (txq < 1 || txq > dev->num_tx_queues)
2910 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002911
Ben Hutchings5c565802011-02-15 19:39:21 +00002912 if (dev->reg_state == NETREG_REGISTERED ||
2913 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002914 ASSERT_RTNL();
2915
Tom Herbert1d24eb42010-11-21 13:17:27 +00002916 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2917 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002918 if (rc)
2919 return rc;
2920
John Fastabend4f57c082011-01-17 08:06:04 +00002921 if (dev->num_tc)
2922 netif_setup_tc(dev, txq);
2923
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002924 dev->real_num_tx_queues = txq;
2925
2926 if (disabling) {
2927 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002928 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002929#ifdef CONFIG_XPS
2930 netif_reset_xps_queues_gt(dev, txq);
2931#endif
2932 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002933 } else {
2934 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002935 }
Tom Herberte6484932010-10-18 18:04:39 +00002936
Tom Herberte6484932010-10-18 18:04:39 +00002937 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002938}
2939EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002940
Michael Daltona953be52014-01-16 22:23:28 -08002941#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002942/**
2943 * netif_set_real_num_rx_queues - set actual number of RX queues used
2944 * @dev: Network device
2945 * @rxq: Actual number of RX queues
2946 *
2947 * This must be called either with the rtnl_lock held or before
2948 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002949 * negative error code. If called before registration, it always
2950 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002951 */
2952int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2953{
2954 int rc;
2955
Tom Herbertbd25fa72010-10-18 18:00:16 +00002956 if (rxq < 1 || rxq > dev->num_rx_queues)
2957 return -EINVAL;
2958
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002959 if (dev->reg_state == NETREG_REGISTERED) {
2960 ASSERT_RTNL();
2961
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002962 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2963 rxq);
2964 if (rc)
2965 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002966 }
2967
2968 dev->real_num_rx_queues = rxq;
2969 return 0;
2970}
2971EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2972#endif
2973
Ben Hutchings2c530402012-07-10 10:55:09 +00002974/**
Jakub Kicinski271e5b72021-08-03 06:05:26 -07002975 * netif_set_real_num_queues - set actual number of RX and TX queues used
2976 * @dev: Network device
2977 * @txq: Actual number of TX queues
2978 * @rxq: Actual number of RX queues
2979 *
2980 * Set the real number of both TX and RX queues.
2981 * Does nothing if the number of queues is already correct.
2982 */
2983int netif_set_real_num_queues(struct net_device *dev,
2984 unsigned int txq, unsigned int rxq)
2985{
2986 unsigned int old_rxq = dev->real_num_rx_queues;
2987 int err;
2988
2989 if (txq < 1 || txq > dev->num_tx_queues ||
2990 rxq < 1 || rxq > dev->num_rx_queues)
2991 return -EINVAL;
2992
2993 /* Start from increases, so the error path only does decreases -
2994 * decreases can't fail.
2995 */
2996 if (rxq > dev->real_num_rx_queues) {
2997 err = netif_set_real_num_rx_queues(dev, rxq);
2998 if (err)
2999 return err;
3000 }
3001 if (txq > dev->real_num_tx_queues) {
3002 err = netif_set_real_num_tx_queues(dev, txq);
3003 if (err)
3004 goto undo_rx;
3005 }
3006 if (rxq < dev->real_num_rx_queues)
3007 WARN_ON(netif_set_real_num_rx_queues(dev, rxq));
3008 if (txq < dev->real_num_tx_queues)
3009 WARN_ON(netif_set_real_num_tx_queues(dev, txq));
3010
3011 return 0;
3012undo_rx:
3013 WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq));
3014 return err;
3015}
3016EXPORT_SYMBOL(netif_set_real_num_queues);
3017
3018/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003019 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00003020 *
3021 * This routine should set an upper limit on the number of RSS queues
3022 * used by default by multiqueue devices.
3023 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00003024int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00003025{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05303026 return is_kdump_kernel() ?
3027 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00003028}
3029EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3030
Eric Dumazet3bcb8462016-06-04 20:02:28 -07003031static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003032{
3033 struct softnet_data *sd;
3034 unsigned long flags;
3035
3036 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05003037 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00003038 q->next_sched = NULL;
3039 *sd->output_queue_tailp = q;
3040 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003041 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3042 local_irq_restore(flags);
3043}
3044
David S. Miller37437bb2008-07-16 02:15:04 -07003045void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08003046{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003047 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3048 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08003049}
3050EXPORT_SYMBOL(__netif_schedule);
3051
Eric Dumazete6247022013-12-05 04:45:08 -08003052struct dev_kfree_skb_cb {
3053 enum skb_free_reason reason;
3054};
3055
3056static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08003057{
Eric Dumazete6247022013-12-05 04:45:08 -08003058 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08003059}
Denis Vlasenko56079432006-03-29 15:57:29 -08003060
John Fastabend46e5da40a2014-09-12 20:04:52 -07003061void netif_schedule_queue(struct netdev_queue *txq)
3062{
3063 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03003064 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07003065 struct Qdisc *q = rcu_dereference(txq->qdisc);
3066
3067 __netif_schedule(q);
3068 }
3069 rcu_read_unlock();
3070}
3071EXPORT_SYMBOL(netif_schedule_queue);
3072
John Fastabend46e5da40a2014-09-12 20:04:52 -07003073void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3074{
3075 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3076 struct Qdisc *q;
3077
3078 rcu_read_lock();
3079 q = rcu_dereference(dev_queue->qdisc);
3080 __netif_schedule(q);
3081 rcu_read_unlock();
3082 }
3083}
3084EXPORT_SYMBOL(netif_tx_wake_queue);
3085
Eric Dumazete6247022013-12-05 04:45:08 -08003086void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3087{
3088 unsigned long flags;
3089
Myungho Jung98998862017-04-25 11:58:15 -07003090 if (unlikely(!skb))
3091 return;
3092
Reshetova, Elena63354792017-06-30 13:07:58 +03003093 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003094 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003095 refcount_set(&skb->users, 0);
3096 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003097 return;
3098 }
3099 get_kfree_skb_cb(skb)->reason = reason;
3100 local_irq_save(flags);
3101 skb->next = __this_cpu_read(softnet_data.completion_queue);
3102 __this_cpu_write(softnet_data.completion_queue, skb);
3103 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3104 local_irq_restore(flags);
3105}
3106EXPORT_SYMBOL(__dev_kfree_skb_irq);
3107
3108void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003109{
Changbin Duafa79d02021-08-13 22:57:49 +08003110 if (in_hardirq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003111 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003112 else
3113 dev_kfree_skb(skb);
3114}
Eric Dumazete6247022013-12-05 04:45:08 -08003115EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003116
3117
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003118/**
3119 * netif_device_detach - mark device as removed
3120 * @dev: network device
3121 *
3122 * Mark device as removed from system and therefore no longer available.
3123 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003124void netif_device_detach(struct net_device *dev)
3125{
3126 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3127 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003128 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003129 }
3130}
3131EXPORT_SYMBOL(netif_device_detach);
3132
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003133/**
3134 * netif_device_attach - mark device as attached
3135 * @dev: network device
3136 *
3137 * Mark device as attached from system and restart if needed.
3138 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003139void netif_device_attach(struct net_device *dev)
3140{
3141 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3142 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003143 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003144 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003145 }
3146}
3147EXPORT_SYMBOL(netif_device_attach);
3148
Jiri Pirko5605c762015-05-12 14:56:12 +02003149/*
3150 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3151 * to be used as a distribution range.
3152 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003153static u16 skb_tx_hash(const struct net_device *dev,
3154 const struct net_device *sb_dev,
3155 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003156{
3157 u32 hash;
3158 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003159 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003160
Alexander Duyckeadec8772018-07-09 12:19:48 -04003161 if (dev->num_tc) {
3162 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3163
3164 qoffset = sb_dev->tc_to_txq[tc].offset;
3165 qcount = sb_dev->tc_to_txq[tc].count;
3166 }
3167
Jiri Pirko5605c762015-05-12 14:56:12 +02003168 if (skb_rx_queue_recorded(skb)) {
3169 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003170 if (hash >= qoffset)
3171 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003172 while (unlikely(hash >= qcount))
3173 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003174 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003175 }
3176
3177 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3178}
Jiri Pirko5605c762015-05-12 14:56:12 +02003179
Ben Hutchings36c92472012-01-17 07:57:56 +00003180static void skb_warn_bad_offload(const struct sk_buff *skb)
3181{
Wei Tang84d15ae2016-06-16 21:17:49 +08003182 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003183 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003184 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003185
Ben Greearc846ad92013-04-19 10:45:52 +00003186 if (!net_ratelimit())
3187 return;
3188
Bjørn Mork88ad4172015-11-16 19:16:40 +01003189 if (dev) {
3190 if (dev->dev.parent)
3191 name = dev_driver_string(dev->dev.parent);
3192 else
3193 name = netdev_name(dev);
3194 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003195 skb_dump(KERN_WARNING, skb, false);
3196 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003197 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003198 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003199}
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201/*
3202 * Invalidate hardware checksum when packet is to be mangled, and
3203 * complete checksum manually on outgoing path.
3204 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003205int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Al Virod3bc23e2006-11-14 21:24:49 -08003207 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003208 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Patrick McHardy84fa7932006-08-29 16:44:56 -07003210 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003211 goto out_set_summed;
3212
Yi Li3aefd7d2020-10-27 13:59:04 +08003213 if (unlikely(skb_is_gso(skb))) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003214 skb_warn_bad_offload(skb);
3215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217
Eric Dumazetcef401d2013-01-25 20:34:37 +00003218 /* Before computing a checksum, we should make sure no frag could
3219 * be modified by an external entity : checksum could be wrong.
3220 */
3221 if (skb_has_shared_frag(skb)) {
3222 ret = __skb_linearize(skb);
3223 if (ret)
3224 goto out;
3225 }
3226
Michał Mirosław55508d62010-12-14 15:24:08 +00003227 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003228 BUG_ON(offset >= skb_headlen(skb));
3229 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3230
3231 offset += skb->csum_offset;
3232 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3233
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003234 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3235 if (ret)
3236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003238 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003239out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003241out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return ret;
3243}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003244EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Davide Carattib72b5bf2017-05-18 15:44:38 +02003246int skb_crc32c_csum_help(struct sk_buff *skb)
3247{
3248 __le32 crc32c_csum;
3249 int ret = 0, offset, start;
3250
3251 if (skb->ip_summed != CHECKSUM_PARTIAL)
3252 goto out;
3253
3254 if (unlikely(skb_is_gso(skb)))
3255 goto out;
3256
3257 /* Before computing a checksum, we should make sure no frag could
3258 * be modified by an external entity : checksum could be wrong.
3259 */
3260 if (unlikely(skb_has_shared_frag(skb))) {
3261 ret = __skb_linearize(skb);
3262 if (ret)
3263 goto out;
3264 }
3265 start = skb_checksum_start_offset(skb);
3266 offset = start + offsetof(struct sctphdr, checksum);
3267 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3268 ret = -EINVAL;
3269 goto out;
3270 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003271
3272 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3273 if (ret)
3274 goto out;
3275
Davide Carattib72b5bf2017-05-18 15:44:38 +02003276 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3277 skb->len - start, ~(__u32)0,
3278 crc32c_csum_stub));
3279 *(__le32 *)(skb->data + offset) = crc32c_csum;
3280 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003281 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003282out:
3283 return ret;
3284}
3285
Vlad Yasevich53d64712014-03-27 17:26:18 -04003286__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003287{
3288 __be16 type = skb->protocol;
3289
Pravin B Shelar19acc322013-05-07 20:41:07 +00003290 /* Tunnel gso handlers can set protocol to ethernet. */
3291 if (type == htons(ETH_P_TEB)) {
3292 struct ethhdr *eth;
3293
3294 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3295 return 0;
3296
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003297 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003298 type = eth->h_proto;
3299 }
3300
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003301 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003302}
3303
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003304/**
3305 * skb_mac_gso_segment - mac layer segmentation handler.
3306 * @skb: buffer to segment
3307 * @features: features for the output path (see dev->features)
3308 */
3309struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3310 netdev_features_t features)
3311{
3312 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3313 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003314 int vlan_depth = skb->mac_len;
3315 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003316
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003317 if (unlikely(!type))
3318 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003319
Vlad Yasevich53d64712014-03-27 17:26:18 -04003320 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003321
3322 rcu_read_lock();
3323 list_for_each_entry_rcu(ptype, &offload_base, list) {
3324 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003325 segs = ptype->callbacks.gso_segment(skb, features);
3326 break;
3327 }
3328 }
3329 rcu_read_unlock();
3330
3331 __skb_push(skb, skb->data - skb_mac_header(skb));
3332
3333 return segs;
3334}
3335EXPORT_SYMBOL(skb_mac_gso_segment);
3336
3337
Cong Wang12b00042013-02-05 16:36:38 +00003338/* openvswitch calls this on rx path, so we need a different check.
3339 */
3340static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3341{
3342 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003343 return skb->ip_summed != CHECKSUM_PARTIAL &&
3344 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003345
3346 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003347}
3348
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003349/**
Cong Wang12b00042013-02-05 16:36:38 +00003350 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003351 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003352 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003353 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003354 *
3355 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003356 *
3357 * It may return NULL if the skb requires no segmentation. This is
3358 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003359 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003360 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003361 */
Cong Wang12b00042013-02-05 16:36:38 +00003362struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3363 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003364{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003365 struct sk_buff *segs;
3366
Cong Wang12b00042013-02-05 16:36:38 +00003367 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003368 int err;
3369
Eric Dumazetb2504a52017-01-31 10:20:32 -08003370 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003371 err = skb_cow_head(skb, 0);
3372 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003373 return ERR_PTR(err);
3374 }
3375
Alexander Duyck802ab552016-04-10 21:45:03 -04003376 /* Only report GSO partial support if it will enable us to
3377 * support segmentation on this frame without needing additional
3378 * work.
3379 */
3380 if (features & NETIF_F_GSO_PARTIAL) {
3381 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3382 struct net_device *dev = skb->dev;
3383
3384 partial_features |= dev->features & dev->gso_partial_features;
3385 if (!skb_gso_ok(skb, features | partial_features))
3386 features &= ~NETIF_F_GSO_PARTIAL;
3387 }
3388
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003389 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003390 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3391
Pravin B Shelar68c33162013-02-14 14:02:41 +00003392 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003393 SKB_GSO_CB(skb)->encap_level = 0;
3394
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003395 skb_reset_mac_header(skb);
3396 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003397
Eric Dumazetb2504a52017-01-31 10:20:32 -08003398 segs = skb_mac_gso_segment(skb, features);
3399
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003400 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003401 skb_warn_bad_offload(skb);
3402
3403 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003404}
Cong Wang12b00042013-02-05 16:36:38 +00003405EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003406
Herbert Xufb286bb2005-11-10 13:01:24 -08003407/* Take action when hardware reception checksum errors are detected. */
3408#ifdef CONFIG_BUG
Tanner Love127d7352021-06-28 09:50:07 -04003409static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3410{
3411 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
3412 skb_dump(KERN_ERR, skb, true);
3413 dump_stack();
3414}
3415
Cong Wang7fe50ac2018-11-12 14:47:18 -08003416void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003417{
Tanner Love127d7352021-06-28 09:50:07 -04003418 DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08003419}
3420EXPORT_SYMBOL(netdev_rx_csum_fault);
3421#endif
3422
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003423/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003424static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003426#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 int i;
tchardingf4563a72017-02-09 17:56:07 +11003428
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003429 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003430 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3431 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003432
Ian Campbellea2ab692011-08-22 23:44:58 +00003433 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003434 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003435 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003436 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return 0;
3439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Simon Horman3b392dd2014-06-04 08:53:17 +09003441/* If MPLS offload request, verify we are testing hardware MPLS features
3442 * instead of standard features for the netdev.
3443 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003444#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003445static netdev_features_t net_mpls_features(struct sk_buff *skb,
3446 netdev_features_t features,
3447 __be16 type)
3448{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003449 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003450 features &= skb->dev->mpls_features;
3451
3452 return features;
3453}
3454#else
3455static netdev_features_t net_mpls_features(struct sk_buff *skb,
3456 netdev_features_t features,
3457 __be16 type)
3458{
3459 return features;
3460}
3461#endif
3462
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003463static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003464 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003465{
Simon Horman3b392dd2014-06-04 08:53:17 +09003466 __be16 type;
3467
Miaohe Lin9fc95f52020-07-30 19:02:36 +08003468 type = skb_network_protocol(skb, NULL);
Simon Horman3b392dd2014-06-04 08:53:17 +09003469 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003470
Ed Cashinc0d680e2012-09-19 15:49:00 +00003471 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003472 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003473 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003474 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003475 if (illegal_highdma(skb->dev, skb))
3476 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003477
3478 return features;
3479}
3480
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003481netdev_features_t passthru_features_check(struct sk_buff *skb,
3482 struct net_device *dev,
3483 netdev_features_t features)
3484{
3485 return features;
3486}
3487EXPORT_SYMBOL(passthru_features_check);
3488
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003489static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003490 struct net_device *dev,
3491 netdev_features_t features)
3492{
3493 return vlan_features_check(skb, features);
3494}
3495
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003496static netdev_features_t gso_features_check(const struct sk_buff *skb,
3497 struct net_device *dev,
3498 netdev_features_t features)
3499{
3500 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3501
3502 if (gso_segs > dev->gso_max_segs)
3503 return features & ~NETIF_F_GSO_MASK;
3504
Heiner Kallweit1d155df2020-11-21 00:22:20 +01003505 if (!skb_shinfo(skb)->gso_type) {
3506 skb_warn_bad_offload(skb);
3507 return features & ~NETIF_F_GSO_MASK;
3508 }
3509
Alexander Duyck802ab552016-04-10 21:45:03 -04003510 /* Support for GSO partial features requires software
3511 * intervention before we can actually process the packets
3512 * so we need to strip support for any partial features now
3513 * and we can pull them back in after we have partially
3514 * segmented the frame.
3515 */
3516 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3517 features &= ~dev->gso_partial_features;
3518
3519 /* Make sure to clear the IPv4 ID mangling feature if the
3520 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003521 */
3522 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3523 struct iphdr *iph = skb->encapsulation ?
3524 inner_ip_hdr(skb) : ip_hdr(skb);
3525
3526 if (!(iph->frag_off & htons(IP_DF)))
3527 features &= ~NETIF_F_TSO_MANGLEID;
3528 }
3529
3530 return features;
3531}
3532
Florian Westphalc1e756b2014-05-05 15:00:44 +02003533netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003534{
Jesse Gross5f352272014-12-23 22:37:26 -08003535 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003536 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003537
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003538 if (skb_is_gso(skb))
3539 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003540
Jesse Gross5f352272014-12-23 22:37:26 -08003541 /* If encapsulation offload request, verify we are testing
3542 * hardware encapsulation features instead of standard
3543 * features for the netdev
3544 */
3545 if (skb->encapsulation)
3546 features &= dev->hw_enc_features;
3547
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003548 if (skb_vlan_tagged(skb))
3549 features = netdev_intersect_features(features,
3550 dev->vlan_features |
3551 NETIF_F_HW_VLAN_CTAG_TX |
3552 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003553
Jesse Gross5f352272014-12-23 22:37:26 -08003554 if (dev->netdev_ops->ndo_features_check)
3555 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3556 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003557 else
3558 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003559
Florian Westphalc1e756b2014-05-05 15:00:44 +02003560 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003561}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003562EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003563
David S. Miller2ea25512014-08-29 21:10:01 -07003564static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003565 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003566{
David S. Miller2ea25512014-08-29 21:10:01 -07003567 unsigned int len;
3568 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003569
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003570 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003571 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003572
David S. Miller2ea25512014-08-29 21:10:01 -07003573 len = skb->len;
Willy Tarreau37447412020-08-10 10:27:42 +02003574 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
David S. Miller2ea25512014-08-29 21:10:01 -07003575 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003576 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003577 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003578
Patrick McHardy572a9d72009-11-10 06:14:14 +00003579 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003580}
David S. Miller2ea25512014-08-29 21:10:01 -07003581
David S. Miller8dcda222014-09-01 15:06:40 -07003582struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3583 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003584{
3585 struct sk_buff *skb = first;
3586 int rc = NETDEV_TX_OK;
3587
3588 while (skb) {
3589 struct sk_buff *next = skb->next;
3590
David S. Millera8305bf2018-07-29 20:42:53 -07003591 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003592 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003593 if (unlikely(!dev_xmit_complete(rc))) {
3594 skb->next = next;
3595 goto out;
3596 }
3597
3598 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003599 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003600 rc = NETDEV_TX_BUSY;
3601 break;
3602 }
3603 }
3604
3605out:
3606 *ret = rc;
3607 return skb;
3608}
3609
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003610static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3611 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003612{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003613 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003614 !vlan_hw_offload_capable(features, skb->vlan_proto))
3615 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003616 return skb;
3617}
3618
Davide Caratti43c26a12017-05-18 15:44:41 +02003619int skb_csum_hwoffload_help(struct sk_buff *skb,
3620 const netdev_features_t features)
3621{
Xin Longfa821172021-01-16 14:13:37 +08003622 if (unlikely(skb_csum_is_sctp(skb)))
Davide Caratti43c26a12017-05-18 15:44:41 +02003623 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3624 skb_crc32c_csum_help(skb);
3625
Xin Long62fafcd2021-01-28 17:18:31 +08003626 if (features & NETIF_F_HW_CSUM)
3627 return 0;
3628
3629 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3630 switch (skb->csum_offset) {
3631 case offsetof(struct tcphdr, check):
3632 case offsetof(struct udphdr, check):
3633 return 0;
3634 }
3635 }
3636
3637 return skb_checksum_help(skb);
Davide Caratti43c26a12017-05-18 15:44:41 +02003638}
3639EXPORT_SYMBOL(skb_csum_hwoffload_help);
3640
Steffen Klassertf53c7232017-12-20 10:41:36 +01003641static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003642{
3643 netdev_features_t features;
3644
David S. Millereae3f882014-08-30 15:17:13 -07003645 features = netif_skb_features(skb);
3646 skb = validate_xmit_vlan(skb, features);
3647 if (unlikely(!skb))
3648 goto out_null;
3649
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003650 skb = sk_validate_xmit_skb(skb, dev);
3651 if (unlikely(!skb))
3652 goto out_null;
3653
Johannes Berg8b86a612015-04-17 15:45:04 +02003654 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003655 struct sk_buff *segs;
3656
3657 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003658 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003659 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003660 } else if (segs) {
3661 consume_skb(skb);
3662 skb = segs;
3663 }
David S. Millereae3f882014-08-30 15:17:13 -07003664 } else {
3665 if (skb_needs_linearize(skb, features) &&
3666 __skb_linearize(skb))
3667 goto out_kfree_skb;
3668
3669 /* If packet is not checksummed and device does not
3670 * support checksumming for this protocol, complete
3671 * checksumming here.
3672 */
3673 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3674 if (skb->encapsulation)
3675 skb_set_inner_transport_header(skb,
3676 skb_checksum_start_offset(skb));
3677 else
3678 skb_set_transport_header(skb,
3679 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003680 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003681 goto out_kfree_skb;
3682 }
3683 }
3684
Steffen Klassertf53c7232017-12-20 10:41:36 +01003685 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003686
David S. Millereae3f882014-08-30 15:17:13 -07003687 return skb;
3688
3689out_kfree_skb:
3690 kfree_skb(skb);
3691out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003692 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003693 return NULL;
3694}
3695
Steffen Klassertf53c7232017-12-20 10:41:36 +01003696struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003697{
3698 struct sk_buff *next, *head = NULL, *tail;
3699
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003700 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003701 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003702 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003703
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003704 /* in case skb wont be segmented, point to itself */
3705 skb->prev = skb;
3706
Steffen Klassertf53c7232017-12-20 10:41:36 +01003707 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003708 if (!skb)
3709 continue;
3710
3711 if (!head)
3712 head = skb;
3713 else
3714 tail->next = skb;
3715 /* If skb was segmented, skb->prev points to
3716 * the last segment. If not, it still contains skb.
3717 */
3718 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003719 }
3720 return head;
3721}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003722EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003723
Eric Dumazet1def9232013-01-10 12:36:42 +00003724static void qdisc_pkt_len_init(struct sk_buff *skb)
3725{
3726 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3727
3728 qdisc_skb_cb(skb)->pkt_len = skb->len;
3729
3730 /* To get more precise estimation of bytes sent on wire,
3731 * we add to pkt_len the headers size of all segments
3732 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003733 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003734 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003735 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003736
Eric Dumazet757b8b12013-01-15 21:14:21 -08003737 /* mac layer + network layer */
3738 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3739
3740 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003741 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3742 const struct tcphdr *th;
3743 struct tcphdr _tcphdr;
3744
3745 th = skb_header_pointer(skb, skb_transport_offset(skb),
3746 sizeof(_tcphdr), &_tcphdr);
3747 if (likely(th))
3748 hdr_len += __tcp_hdrlen(th);
3749 } else {
3750 struct udphdr _udphdr;
3751
3752 if (skb_header_pointer(skb, skb_transport_offset(skb),
3753 sizeof(_udphdr), &_udphdr))
3754 hdr_len += sizeof(struct udphdr);
3755 }
Jason Wang15e5a032013-03-25 20:19:59 +00003756
3757 if (shinfo->gso_type & SKB_GSO_DODGY)
3758 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3759 shinfo->gso_size);
3760
3761 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003762 }
3763}
3764
Qitao Xu70713dd2021-07-14 23:03:24 -07003765static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q,
3766 struct sk_buff **to_free,
3767 struct netdev_queue *txq)
3768{
3769 int rc;
3770
3771 rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK;
3772 if (rc == NET_XMIT_SUCCESS)
3773 trace_qdisc_enqueue(q, txq, skb);
3774 return rc;
3775}
3776
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003777static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3778 struct net_device *dev,
3779 struct netdev_queue *txq)
3780{
3781 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003782 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003783 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003784 int rc;
3785
Eric Dumazeta2da5702011-01-20 03:48:19 +00003786 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003787
3788 if (q->flags & TCQ_F_NOLOCK) {
Yunsheng Linc4fef012021-06-22 14:49:56 +08003789 if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) &&
3790 qdisc_run_begin(q)) {
3791 /* Retest nolock_qdisc_is_empty() within the protection
3792 * of q->seqlock to protect from racing with requeuing.
3793 */
3794 if (unlikely(!nolock_qdisc_is_empty(q))) {
Qitao Xu70713dd2021-07-14 23:03:24 -07003795 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Yunsheng Linc4fef012021-06-22 14:49:56 +08003796 __qdisc_run(q);
3797 qdisc_run_end(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003798
Yunsheng Linc4fef012021-06-22 14:49:56 +08003799 goto no_lock_out;
3800 }
3801
3802 qdisc_bstats_cpu_update(q, skb);
3803 if (sch_direct_xmit(skb, q, dev, txq, NULL, true) &&
3804 !nolock_qdisc_is_empty(q))
3805 __qdisc_run(q);
3806
3807 qdisc_run_end(q);
3808 return NET_XMIT_SUCCESS;
3809 }
3810
Qitao Xu70713dd2021-07-14 23:03:24 -07003811 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Yunsheng Linc4fef012021-06-22 14:49:56 +08003812 qdisc_run(q);
3813
3814no_lock_out:
John Fastabend6b3ba912017-12-07 09:54:25 -08003815 if (unlikely(to_free))
3816 kfree_skb_list(to_free);
3817 return rc;
3818 }
3819
Eric Dumazet79640a42010-06-02 05:09:29 -07003820 /*
3821 * Heuristic to force contended enqueues to serialize on a
3822 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003823 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003824 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003825 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003826 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003827 if (unlikely(contended))
3828 spin_lock(&q->busylock);
3829
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003830 spin_lock(root_lock);
3831 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003832 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003833 rc = NET_XMIT_DROP;
3834 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003835 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003836 /*
3837 * This is a work-conserving queue; there are no old skbs
3838 * waiting to be sent out; and the qdisc is not running -
3839 * xmit the skb directly.
3840 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003841
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003842 qdisc_bstats_update(q, skb);
3843
Eric Dumazet55a93b32014-10-03 15:31:07 -07003844 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003845 if (unlikely(contended)) {
3846 spin_unlock(&q->busylock);
3847 contended = false;
3848 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003849 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003850 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003851
John Fastabend6c148182017-12-07 09:54:06 -08003852 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003853 rc = NET_XMIT_SUCCESS;
3854 } else {
Qitao Xu70713dd2021-07-14 23:03:24 -07003855 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
Eric Dumazet79640a42010-06-02 05:09:29 -07003856 if (qdisc_run_begin(q)) {
3857 if (unlikely(contended)) {
3858 spin_unlock(&q->busylock);
3859 contended = false;
3860 }
3861 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003862 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003863 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003864 }
3865 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003866 if (unlikely(to_free))
3867 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003868 if (unlikely(contended))
3869 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003870 return rc;
3871}
3872
Daniel Borkmann86f85152013-12-29 17:27:11 +01003873#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003874static void skb_update_prio(struct sk_buff *skb)
3875{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003876 const struct netprio_map *map;
3877 const struct sock *sk;
3878 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003879
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003880 if (skb->priority)
3881 return;
3882 map = rcu_dereference_bh(skb->dev->priomap);
3883 if (!map)
3884 return;
3885 sk = skb_to_full_sk(skb);
3886 if (!sk)
3887 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003888
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003889 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3890
3891 if (prioidx < map->priomap_len)
3892 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003893}
3894#else
3895#define skb_update_prio(skb)
3896#endif
3897
Dave Jonesd29f7492008-07-22 14:09:06 -07003898/**
Michel Machado95603e22012-06-12 10:16:35 +00003899 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003900 * @net: network namespace this loopback is happening in
3901 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003902 * @skb: buffer to transmit
3903 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003904int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003905{
3906 skb_reset_mac_header(skb);
3907 __skb_pull(skb, skb_network_offset(skb));
3908 skb->pkt_type = PACKET_LOOPBACK;
3909 skb->ip_summed = CHECKSUM_UNNECESSARY;
3910 WARN_ON(!skb_dst(skb));
3911 skb_dst_force(skb);
3912 netif_rx_ni(skb);
3913 return 0;
3914}
3915EXPORT_SYMBOL(dev_loopback_xmit);
3916
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003917#ifdef CONFIG_NET_EGRESS
3918static struct sk_buff *
3919sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3920{
Jiri Pirko46209402017-11-03 11:46:25 +01003921 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003922 struct tcf_result cl_res;
3923
Jiri Pirko46209402017-11-03 11:46:25 +01003924 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003925 return skb;
3926
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003927 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
wenxuaadaca92020-11-25 12:01:21 +08003928 qdisc_skb_cb(skb)->mru = 0;
wenxu7baf2422021-01-19 16:31:50 +08003929 qdisc_skb_cb(skb)->post_ct = false;
Jiri Pirko46209402017-11-03 11:46:25 +01003930 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003931
Davide Caratti3aa26052021-07-28 20:08:00 +02003932 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003933 case TC_ACT_OK:
3934 case TC_ACT_RECLASSIFY:
3935 skb->tc_index = TC_H_MIN(cl_res.classid);
3936 break;
3937 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003938 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003939 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003940 kfree_skb(skb);
3941 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003942 case TC_ACT_STOLEN:
3943 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003944 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003945 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003946 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003947 return NULL;
3948 case TC_ACT_REDIRECT:
3949 /* No need to push/pop skb's mac_header here on egress! */
3950 skb_do_redirect(skb);
3951 *ret = NET_XMIT_SUCCESS;
3952 return NULL;
3953 default:
3954 break;
3955 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003956
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003957 return skb;
3958}
3959#endif /* CONFIG_NET_EGRESS */
3960
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003961#ifdef CONFIG_XPS
3962static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3963 struct xps_dev_maps *dev_maps, unsigned int tci)
3964{
Antoine Tenart255c04a2021-03-18 19:37:43 +01003965 int tc = netdev_get_prio_tc_map(dev, skb->priority);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003966 struct xps_map *map;
3967 int queue_index = -1;
3968
Antoine Tenart5478fcd2021-03-18 19:37:44 +01003969 if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids)
Antoine Tenart255c04a2021-03-18 19:37:43 +01003970 return queue_index;
3971
3972 tci *= dev_maps->num_tc;
3973 tci += tc;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003974
3975 map = rcu_dereference(dev_maps->attr_map[tci]);
3976 if (map) {
3977 if (map->len == 1)
3978 queue_index = map->queues[0];
3979 else
3980 queue_index = map->queues[reciprocal_scale(
3981 skb_get_hash(skb), map->len)];
3982 if (unlikely(queue_index >= dev->real_num_tx_queues))
3983 queue_index = -1;
3984 }
3985 return queue_index;
3986}
3987#endif
3988
Alexander Duyckeadec8772018-07-09 12:19:48 -04003989static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3990 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003991{
3992#ifdef CONFIG_XPS
3993 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003994 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003995 int queue_index = -1;
3996
Amritha Nambiar04157462018-06-29 21:26:46 -07003997 if (!static_key_false(&xps_needed))
3998 return -1;
3999
Jiri Pirko638b2a62015-05-12 14:56:13 +02004000 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07004001 if (!static_key_false(&xps_rxqs_needed))
4002 goto get_cpus_map;
4003
Antoine Tenart044ab862021-03-18 19:37:46 +01004004 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004005 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07004006 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04004007
Antoine Tenart5478fcd2021-03-18 19:37:44 +01004008 if (tci >= 0)
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07004009 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
4010 tci);
4011 }
Alexander Duyck184c4492016-10-28 11:50:13 -04004012
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07004013get_cpus_map:
4014 if (queue_index < 0) {
Antoine Tenart044ab862021-03-18 19:37:46 +01004015 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07004016 if (dev_maps) {
4017 unsigned int tci = skb->sender_cpu - 1;
4018
4019 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
4020 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004021 }
4022 }
4023 rcu_read_unlock();
4024
4025 return queue_index;
4026#else
4027 return -1;
4028#endif
4029}
4030
Alexander Duycka4ea8a32018-07-09 12:19:54 -04004031u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01004032 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04004033{
4034 return 0;
4035}
4036EXPORT_SYMBOL(dev_pick_tx_zero);
4037
4038u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01004039 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04004040{
4041 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
4042}
4043EXPORT_SYMBOL(dev_pick_tx_cpu_id);
4044
Paolo Abenib71b5832019-03-20 11:02:05 +01004045u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
4046 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004047{
4048 struct sock *sk = skb->sk;
4049 int queue_index = sk_tx_queue_get(sk);
4050
Alexander Duyckeadec8772018-07-09 12:19:48 -04004051 sb_dev = sb_dev ? : dev;
4052
Jiri Pirko638b2a62015-05-12 14:56:13 +02004053 if (queue_index < 0 || skb->ooo_okay ||
4054 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04004055 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11004056
Jiri Pirko638b2a62015-05-12 14:56:13 +02004057 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04004058 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004059
4060 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07004061 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02004062 rcu_access_pointer(sk->sk_dst_cache))
4063 sk_tx_queue_set(sk, new_index);
4064
4065 queue_index = new_index;
4066 }
4067
4068 return queue_index;
4069}
Paolo Abenib71b5832019-03-20 11:02:05 +01004070EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004071
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004072struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4073 struct sk_buff *skb,
4074 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004075{
4076 int queue_index = 0;
4077
4078#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08004079 u32 sender_cpu = skb->sender_cpu - 1;
4080
4081 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004082 skb->sender_cpu = raw_smp_processor_id() + 1;
4083#endif
4084
4085 if (dev->real_num_tx_queues != 1) {
4086 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11004087
Jiri Pirko638b2a62015-05-12 14:56:13 +02004088 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01004089 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004090 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004091 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004092
Alexander Duyckd5845272017-11-22 10:57:41 -08004093 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004094 }
4095
4096 skb_set_queue_mapping(skb, queue_index);
4097 return netdev_get_tx_queue(dev, queue_index);
4098}
4099
Michel Machado95603e22012-06-12 10:16:35 +00004100/**
Jason Wang9d08dd32014-01-20 11:25:13 +08004101 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07004102 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04004103 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07004104 *
4105 * Queue a buffer for transmission to a network device. The caller must
4106 * have set the device and priority and built the buffer before calling
4107 * this function. The function can be called from an interrupt.
4108 *
4109 * A negative errno code is returned on a failure. A success does not
4110 * guarantee the frame will be transmitted as it may be dropped due
4111 * to congestion or traffic shaping.
4112 *
4113 * -----------------------------------------------------------------------------------
4114 * I notice this method can also return errors from the queue disciplines,
4115 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4116 * be positive.
4117 *
4118 * Regardless of the return value, the skb is consumed, so it is currently
4119 * difficult to retry a send to this method. (You can bump the ref count
4120 * before sending to hold a reference for retry if you are careful.)
4121 *
4122 * When calling this method, interrupts MUST be enabled. This is because
4123 * the BH enable code must have IRQs enabled so that it will not deadlock.
4124 * --BLG
4125 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04004126static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
4128 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07004129 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 struct Qdisc *q;
4131 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01004132 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00004134 skb_reset_mac_header(skb);
4135
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004136 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004137 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004138
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004139 /* Disable soft irqs for various locks below. Also
4140 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004142 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Neil Horman5bc14212011-11-22 05:10:51 +00004144 skb_update_prio(skb);
4145
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004146 qdisc_pkt_len_init(skb);
4147#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004148 skb->tc_at_ingress = 0;
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004149# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004150 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004151 skb = sch_handle_egress(skb, &rc, dev);
4152 if (!skb)
4153 goto out;
4154 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004155# endif
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004156#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004157 /* If device/qdisc don't need skb->dst, release it right now while
4158 * its hot in this cpu cache.
4159 */
4160 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4161 skb_dst_drop(skb);
4162 else
4163 skb_dst_force(skb);
4164
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004165 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004166 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004167
Koki Sanagicf66ba52010-08-23 18:45:02 +09004168 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004170 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173
4174 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004175 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
tchardingeb13da12017-02-09 17:56:06 +11004177 * Really, it is unlikely that netif_tx_lock protection is necessary
4178 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4179 * counters.)
4180 * However, it is possible, that they rely on protection
4181 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
tchardingeb13da12017-02-09 17:56:06 +11004183 * Check this and shot the lock. It is not prone from deadlocks.
4184 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 */
4186 if (dev->flags & IFF_UP) {
4187 int cpu = smp_processor_id(); /* ok because BHs are off */
4188
David S. Millerc773e842008-07-08 23:13:53 -07004189 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004190 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004191 goto recursion_alert;
4192
Steffen Klassertf53c7232017-12-20 10:41:36 +01004193 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004194 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004195 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004196
Willy Tarreau37447412020-08-10 10:27:42 +02004197 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
David S. Millerc773e842008-07-08 23:13:53 -07004198 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Tom Herbert734664982011-11-28 16:32:44 +00004200 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004201 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004202 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004203 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004204 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004205 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 goto out;
4207 }
4208 }
David S. Millerc773e842008-07-08 23:13:53 -07004209 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004210 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4211 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 } else {
4213 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004214 * unfortunately
4215 */
4216recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004217 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4218 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220 }
4221
4222 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004223 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Eric Dumazet015f0682014-03-27 08:45:56 -07004225 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004226 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return rc;
4228out:
Herbert Xud4828d82006-06-22 02:28:18 -07004229 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return rc;
4231}
Jason Wangf663dd92014-01-10 16:18:26 +08004232
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004233int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004234{
4235 return __dev_queue_xmit(skb, NULL);
4236}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004237EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Alexander Duyckeadec8772018-07-09 12:19:48 -04004239int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004240{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004241 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004242}
4243EXPORT_SYMBOL(dev_queue_xmit_accel);
4244
Björn Töpel36ccdf82020-11-23 18:56:00 +01004245int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004246{
4247 struct net_device *dev = skb->dev;
4248 struct sk_buff *orig_skb = skb;
4249 struct netdev_queue *txq;
4250 int ret = NETDEV_TX_BUSY;
4251 bool again = false;
4252
4253 if (unlikely(!netif_running(dev) ||
4254 !netif_carrier_ok(dev)))
4255 goto drop;
4256
4257 skb = validate_xmit_skb_list(skb, dev, &again);
4258 if (skb != orig_skb)
4259 goto drop;
4260
4261 skb_set_queue_mapping(skb, queue_id);
4262 txq = skb_get_tx_queue(dev, skb);
Willy Tarreau37447412020-08-10 10:27:42 +02004263 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004264
4265 local_bh_disable();
4266
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004267 dev_xmit_recursion_inc();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004268 HARD_TX_LOCK(dev, txq, smp_processor_id());
4269 if (!netif_xmit_frozen_or_drv_stopped(txq))
4270 ret = netdev_start_xmit(skb, dev, txq, false);
4271 HARD_TX_UNLOCK(dev, txq);
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004272 dev_xmit_recursion_dec();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004273
4274 local_bh_enable();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004275 return ret;
4276drop:
4277 atomic_long_inc(&dev->tx_dropped);
4278 kfree_skb_list(skb);
4279 return NET_XMIT_DROP;
4280}
Björn Töpel36ccdf82020-11-23 18:56:00 +01004281EXPORT_SYMBOL(__dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
tchardingeb13da12017-02-09 17:56:06 +11004283/*************************************************************************
4284 * Receiver routines
4285 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004287int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004288EXPORT_SYMBOL(netdev_max_backlog);
4289
Eric Dumazet3b098e22010-05-15 23:57:10 -07004290int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004291int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004292/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4293unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004294int weight_p __read_mostly = 64; /* old backlog weight */
4295int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4296int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4297int dev_rx_weight __read_mostly = 64;
4298int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004299/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4300int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004302/* Called with irq disabled */
4303static inline void ____napi_schedule(struct softnet_data *sd,
4304 struct napi_struct *napi)
4305{
Wei Wang29863d42021-02-08 11:34:09 -08004306 struct task_struct *thread;
4307
4308 if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
4309 /* Paired with smp_mb__before_atomic() in
Wei Wang5fdd2f02021-02-08 11:34:10 -08004310 * napi_enable()/dev_set_threaded().
4311 * Use READ_ONCE() to guarantee a complete
4312 * read on napi->thread. Only call
Wei Wang29863d42021-02-08 11:34:09 -08004313 * wake_up_process() when it's not NULL.
4314 */
4315 thread = READ_ONCE(napi->thread);
4316 if (thread) {
Wei Wangcb038352021-03-16 15:36:47 -07004317 /* Avoid doing set_bit() if the thread is in
4318 * INTERRUPTIBLE state, cause napi_thread_wait()
4319 * makes sure to proceed with napi polling
4320 * if the thread is explicitly woken from here.
4321 */
Peter Zijlstra2f064a52021-06-11 10:28:17 +02004322 if (READ_ONCE(thread->__state) != TASK_INTERRUPTIBLE)
Wei Wangcb038352021-03-16 15:36:47 -07004323 set_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
Wei Wang29863d42021-02-08 11:34:09 -08004324 wake_up_process(thread);
4325 return;
4326 }
4327 }
4328
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004329 list_add_tail(&napi->poll_list, &sd->poll_list);
4330 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4331}
4332
Eric Dumazetdf334542010-03-24 19:13:54 +00004333#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004334
4335/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004336struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004337EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004338u32 rps_cpu_mask __read_mostly;
4339EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004340
Eric Dumazetdc053602019-03-22 08:56:38 -07004341struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004342EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004343struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004344EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004345
Ben Hutchingsc4454772011-01-19 11:03:53 +00004346static struct rps_dev_flow *
4347set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4348 struct rps_dev_flow *rflow, u16 next_cpu)
4349{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004350 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004351#ifdef CONFIG_RFS_ACCEL
4352 struct netdev_rx_queue *rxqueue;
4353 struct rps_dev_flow_table *flow_table;
4354 struct rps_dev_flow *old_rflow;
4355 u32 flow_id;
4356 u16 rxq_index;
4357 int rc;
4358
4359 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004360 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4361 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004362 goto out;
4363 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4364 if (rxq_index == skb_get_rx_queue(skb))
4365 goto out;
4366
4367 rxqueue = dev->_rx + rxq_index;
4368 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4369 if (!flow_table)
4370 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004371 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004372 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4373 rxq_index, flow_id);
4374 if (rc < 0)
4375 goto out;
4376 old_rflow = rflow;
4377 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004378 rflow->filter = rc;
4379 if (old_rflow->filter == rflow->filter)
4380 old_rflow->filter = RPS_NO_FILTER;
4381 out:
4382#endif
4383 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004384 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004385 }
4386
Ben Hutchings09994d12011-10-03 04:42:46 +00004387 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004388 return rflow;
4389}
4390
Tom Herbert0a9627f2010-03-16 08:03:29 +00004391/*
4392 * get_rps_cpu is called from netif_receive_skb and returns the target
4393 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004394 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004395 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004396static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4397 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004398{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004399 const struct rps_sock_flow_table *sock_flow_table;
4400 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004401 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004402 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004403 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004404 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004405 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004406
Tom Herbert0a9627f2010-03-16 08:03:29 +00004407 if (skb_rx_queue_recorded(skb)) {
4408 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004409
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004410 if (unlikely(index >= dev->real_num_rx_queues)) {
4411 WARN_ONCE(dev->real_num_rx_queues > 1,
4412 "%s received packet on queue %u, but number "
4413 "of RX queues is %u\n",
4414 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004415 goto done;
4416 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004417 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004418 }
4419
Eric Dumazet567e4b72015-02-06 12:59:01 -08004420 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4421
4422 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4423 map = rcu_dereference(rxqueue->rps_map);
4424 if (!flow_table && !map)
4425 goto done;
4426
Changli Gao2d47b452010-08-17 19:00:56 +00004427 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004428 hash = skb_get_hash(skb);
4429 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004430 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004431
Tom Herbertfec5e652010-04-16 16:01:27 -07004432 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4433 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004434 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004435 u32 next_cpu;
4436 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004437
Eric Dumazet567e4b72015-02-06 12:59:01 -08004438 /* First check into global flow table if there is a match */
4439 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4440 if ((ident ^ hash) & ~rps_cpu_mask)
4441 goto try_rps;
4442
4443 next_cpu = ident & rps_cpu_mask;
4444
4445 /* OK, now we know there is a match,
4446 * we can look at the local (per receive queue) flow table
4447 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004448 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004449 tcpu = rflow->cpu;
4450
Tom Herbertfec5e652010-04-16 16:01:27 -07004451 /*
4452 * If the desired CPU (where last recvmsg was done) is
4453 * different from current CPU (one in the rx-queue flow
4454 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004455 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004456 * - Current CPU is offline.
4457 * - The current CPU's queue tail has advanced beyond the
4458 * last packet that was enqueued using this table entry.
4459 * This guarantees that all previous packets for the flow
4460 * have been dequeued, thus preserving in order delivery.
4461 */
4462 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004463 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004464 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004465 rflow->last_qtail)) >= 0)) {
4466 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004467 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004468 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004469
Eric Dumazeta31196b2015-04-25 09:35:24 -07004470 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004471 *rflowp = rflow;
4472 cpu = tcpu;
4473 goto done;
4474 }
4475 }
4476
Eric Dumazet567e4b72015-02-06 12:59:01 -08004477try_rps:
4478
Tom Herbert0a9627f2010-03-16 08:03:29 +00004479 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004480 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004481 if (cpu_online(tcpu)) {
4482 cpu = tcpu;
4483 goto done;
4484 }
4485 }
4486
4487done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004488 return cpu;
4489}
4490
Ben Hutchingsc4454772011-01-19 11:03:53 +00004491#ifdef CONFIG_RFS_ACCEL
4492
4493/**
4494 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4495 * @dev: Device on which the filter was set
4496 * @rxq_index: RX queue index
4497 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4498 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4499 *
4500 * Drivers that implement ndo_rx_flow_steer() should periodically call
4501 * this function for each installed filter and remove the filters for
4502 * which it returns %true.
4503 */
4504bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4505 u32 flow_id, u16 filter_id)
4506{
4507 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4508 struct rps_dev_flow_table *flow_table;
4509 struct rps_dev_flow *rflow;
4510 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004511 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004512
4513 rcu_read_lock();
4514 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4515 if (flow_table && flow_id <= flow_table->mask) {
4516 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004517 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004518 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004519 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4520 rflow->last_qtail) <
4521 (int)(10 * flow_table->mask)))
4522 expire = false;
4523 }
4524 rcu_read_unlock();
4525 return expire;
4526}
4527EXPORT_SYMBOL(rps_may_expire_flow);
4528
4529#endif /* CONFIG_RFS_ACCEL */
4530
Tom Herbert0a9627f2010-03-16 08:03:29 +00004531/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004532static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004533{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004534 struct softnet_data *sd = data;
4535
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004536 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004537 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004538}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004539
Tom Herbertfec5e652010-04-16 16:01:27 -07004540#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004541
4542/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004543 * Check if this softnet_data structure is another cpu one
4544 * If yes, queue it to our IPI list and return 1
4545 * If no, return 0
4546 */
4547static int rps_ipi_queued(struct softnet_data *sd)
4548{
4549#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004550 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004551
4552 if (sd != mysd) {
4553 sd->rps_ipi_next = mysd->rps_ipi_list;
4554 mysd->rps_ipi_list = sd;
4555
4556 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4557 return 1;
4558 }
4559#endif /* CONFIG_RPS */
4560 return 0;
4561}
4562
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004563#ifdef CONFIG_NET_FLOW_LIMIT
4564int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4565#endif
4566
4567static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4568{
4569#ifdef CONFIG_NET_FLOW_LIMIT
4570 struct sd_flow_limit *fl;
4571 struct softnet_data *sd;
4572 unsigned int old_flow, new_flow;
4573
4574 if (qlen < (netdev_max_backlog >> 1))
4575 return false;
4576
Christoph Lameter903ceff2014-08-17 12:30:35 -05004577 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004578
4579 rcu_read_lock();
4580 fl = rcu_dereference(sd->flow_limit);
4581 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004582 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004583 old_flow = fl->history[fl->history_head];
4584 fl->history[fl->history_head] = new_flow;
4585
4586 fl->history_head++;
4587 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4588
4589 if (likely(fl->buckets[old_flow]))
4590 fl->buckets[old_flow]--;
4591
4592 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4593 fl->count++;
4594 rcu_read_unlock();
4595 return true;
4596 }
4597 }
4598 rcu_read_unlock();
4599#endif
4600 return false;
4601}
4602
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004603/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004604 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4605 * queue (may be a remote CPU queue).
4606 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004607static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4608 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004609{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004610 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004611 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004612 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004613
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004614 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004615
4616 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004617
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004618 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004619 if (!netif_running(skb->dev))
4620 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004621 qlen = skb_queue_len(&sd->input_pkt_queue);
4622 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004623 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004624enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004625 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004626 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004627 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004628 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004629 return NET_RX_SUCCESS;
4630 }
4631
Eric Dumazetebda37c22010-05-06 23:51:21 +00004632 /* Schedule NAPI for backlog device
4633 * We can use non atomic operation since we own the queue lock
4634 */
4635 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004636 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004637 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004638 }
4639 goto enqueue;
4640 }
4641
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004642drop:
Changli Gaodee42872010-05-02 05:42:16 +00004643 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004644 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004645
Tom Herbert0a9627f2010-03-16 08:03:29 +00004646 local_irq_restore(flags);
4647
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004648 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004649 kfree_skb(skb);
4650 return NET_RX_DROP;
4651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004653static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4654{
4655 struct net_device *dev = skb->dev;
4656 struct netdev_rx_queue *rxqueue;
4657
4658 rxqueue = dev->_rx;
4659
4660 if (skb_rx_queue_recorded(skb)) {
4661 u16 index = skb_get_rx_queue(skb);
4662
4663 if (unlikely(index >= dev->real_num_rx_queues)) {
4664 WARN_ONCE(dev->real_num_rx_queues > 1,
4665 "%s received packet on queue %u, but number "
4666 "of RX queues is %u\n",
4667 dev->name, index, dev->real_num_rx_queues);
4668
4669 return rxqueue; /* Return first rxqueue */
4670 }
4671 rxqueue += index;
4672 }
4673 return rxqueue;
4674}
4675
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304676u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
4677 struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004678{
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004679 void *orig_data, *orig_data_end, *hard_start;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004680 struct netdev_rx_queue *rxqueue;
Martin Willi22b60342021-04-19 16:15:59 +02004681 bool orig_bcast, orig_host;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01004682 u32 mac_len, frame_sz;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004683 __be16 orig_eth_type;
4684 struct ethhdr *eth;
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304685 u32 metalen, act;
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004686 int off;
John Fastabendd4455162017-07-17 09:26:45 -07004687
John Fastabendd4455162017-07-17 09:26:45 -07004688 /* The XDP program wants to see the packet starting at the MAC
4689 * header.
4690 */
4691 mac_len = skb->data - skb_mac_header(skb);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004692 hard_start = skb->data - skb_headroom(skb);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004693
4694 /* SKB "head" area always have tailroom for skb_shared_info */
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004695 frame_sz = (void *)skb_end_pointer(skb) - hard_start;
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01004696 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004697
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01004698 rxqueue = netif_get_rxqueue(skb);
4699 xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
4700 xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
4701 skb_headlen(skb) + mac_len, true);
Björn Töpel02671e22018-05-02 13:01:30 +02004702
4703 orig_data_end = xdp->data_end;
4704 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004705 eth = (struct ethhdr *)xdp->data;
Martin Willi22b60342021-04-19 16:15:59 +02004706 orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr);
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004707 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4708 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004709
Björn Töpel02671e22018-05-02 13:01:30 +02004710 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004711
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004712 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004713 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004714 if (off) {
4715 if (off > 0)
4716 __skb_pull(skb, off);
4717 else if (off < 0)
4718 __skb_push(skb, -off);
4719
4720 skb->mac_header += off;
4721 skb_reset_network_header(skb);
4722 }
John Fastabendd4455162017-07-17 09:26:45 -07004723
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004724 /* check if bpf_xdp_adjust_tail was used */
4725 off = xdp->data_end - orig_data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004726 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004727 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004728 skb->len += off; /* positive on grow, negative on shrink */
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004729 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004730
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004731 /* check if XDP changed eth hdr such SKB needs update */
4732 eth = (struct ethhdr *)xdp->data;
4733 if ((orig_eth_type != eth->h_proto) ||
Martin Willi22b60342021-04-19 16:15:59 +02004734 (orig_host != ether_addr_equal_64bits(eth->h_dest,
4735 skb->dev->dev_addr)) ||
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004736 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4737 __skb_push(skb, ETH_HLEN);
Martin Willi22b60342021-04-19 16:15:59 +02004738 skb->pkt_type = PACKET_HOST;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004739 skb->protocol = eth_type_trans(skb, skb->dev);
4740 }
4741
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304742 /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull
4743 * before calling us again on redirect path. We do not call do_redirect
4744 * as we leave that up to the caller.
4745 *
4746 * Caller is responsible for managing lifetime of skb (i.e. calling
4747 * kfree_skb in response to actions it cannot handle/XDP_DROP).
4748 */
John Fastabendd4455162017-07-17 09:26:45 -07004749 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004750 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004751 case XDP_TX:
4752 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004753 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004754 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004755 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004756 if (metalen)
4757 skb_metadata_set(skb, metalen);
4758 break;
Kumar Kartikeya Dwivedife21cb92021-07-02 16:48:21 +05304759 }
4760
4761 return act;
4762}
4763
4764static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4765 struct xdp_buff *xdp,
4766 struct bpf_prog *xdp_prog)
4767{
4768 u32 act = XDP_DROP;
4769
4770 /* Reinjected packets coming from act_mirred or similar should
4771 * not get XDP generic processing.
4772 */
4773 if (skb_is_redirected(skb))
4774 return XDP_PASS;
4775
4776 /* XDP packets must be linear and must have sufficient headroom
4777 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4778 * native XDP provides, thus we need to do it here as well.
4779 */
4780 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
4781 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4782 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4783 int troom = skb->tail + skb->data_len - skb->end;
4784
4785 /* In case we have to go down the path and also linearize,
4786 * then lets do the pskb_expand_head() work just once here.
4787 */
4788 if (pskb_expand_head(skb,
4789 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4790 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4791 goto do_drop;
4792 if (skb_linearize(skb))
4793 goto do_drop;
4794 }
4795
4796 act = bpf_prog_run_generic_xdp(skb, xdp, xdp_prog);
4797 switch (act) {
4798 case XDP_REDIRECT:
4799 case XDP_TX:
4800 case XDP_PASS:
4801 break;
John Fastabendd4455162017-07-17 09:26:45 -07004802 default:
4803 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004804 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004805 case XDP_ABORTED:
4806 trace_xdp_exception(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004807 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004808 case XDP_DROP:
4809 do_drop:
4810 kfree_skb(skb);
4811 break;
4812 }
4813
4814 return act;
4815}
4816
4817/* When doing generic XDP we have to bypass the qdisc layer and the
4818 * network taps in order to match in-driver-XDP behavior.
4819 */
Jason Wang7c497472017-08-11 19:41:17 +08004820void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004821{
4822 struct net_device *dev = skb->dev;
4823 struct netdev_queue *txq;
4824 bool free_skb = true;
4825 int cpu, rc;
4826
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004827 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004828 cpu = smp_processor_id();
4829 HARD_TX_LOCK(dev, txq, cpu);
4830 if (!netif_xmit_stopped(txq)) {
4831 rc = netdev_start_xmit(skb, dev, txq, 0);
4832 if (dev_xmit_complete(rc))
4833 free_skb = false;
4834 }
4835 HARD_TX_UNLOCK(dev, txq);
4836 if (free_skb) {
4837 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4838 kfree_skb(skb);
4839 }
4840}
4841
Davidlohr Bueso02786472018-05-08 09:07:02 -07004842static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004843
Jason Wang7c497472017-08-11 19:41:17 +08004844int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004845{
John Fastabendd4455162017-07-17 09:26:45 -07004846 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004847 struct xdp_buff xdp;
4848 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004849 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004850
Björn Töpel02671e22018-05-02 13:01:30 +02004851 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004852 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004853 switch (act) {
4854 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004855 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004856 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004857 if (err)
4858 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004859 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004860 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004861 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004862 break;
4863 }
John Fastabendd4455162017-07-17 09:26:45 -07004864 return XDP_DROP;
4865 }
4866 }
4867 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004868out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004869 kfree_skb(skb);
4870 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004871}
Jason Wang7c497472017-08-11 19:41:17 +08004872EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004873
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004874static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004876 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Eric Dumazet588f0332011-11-15 04:12:55 +00004878 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Koki Sanagicf66ba52010-08-23 18:45:02 +09004880 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004881
Eric Dumazetdf334542010-03-24 19:13:54 +00004882#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004883 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004884 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004885 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Changli Gaocece1942010-08-07 20:35:43 -07004887 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004888 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004889
4890 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004891 if (cpu < 0)
4892 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004893
4894 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4895
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004896 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004897 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004898 } else
4899#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004900 {
4901 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004902
Tom Herbertfec5e652010-04-16 16:01:27 -07004903 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4904 put_cpu();
4905 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004908
4909/**
4910 * netif_rx - post buffer to the network code
4911 * @skb: buffer to post
4912 *
4913 * This function receives a packet from a device driver and queues it for
4914 * the upper (protocol) levels to process. It always succeeds. The buffer
4915 * may be dropped during processing for congestion control or by the
4916 * protocol layers.
4917 *
4918 * return values:
4919 * NET_RX_SUCCESS (no congestion)
4920 * NET_RX_DROP (packet was dropped)
4921 *
4922 */
4923
4924int netif_rx(struct sk_buff *skb)
4925{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004926 int ret;
4927
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004928 trace_netif_rx_entry(skb);
4929
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004930 ret = netif_rx_internal(skb);
4931 trace_netif_rx_exit(ret);
4932
4933 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004934}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004935EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937int netif_rx_ni(struct sk_buff *skb)
4938{
4939 int err;
4940
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004941 trace_netif_rx_ni_entry(skb);
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004944 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (local_softirq_pending())
4946 do_softirq();
4947 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004948 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 return err;
4951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952EXPORT_SYMBOL(netif_rx_ni);
4953
Sebastian Andrzej Siewiorc11171a2020-09-29 22:25:12 +02004954int netif_rx_any_context(struct sk_buff *skb)
4955{
4956 /*
4957 * If invoked from contexts which do not invoke bottom half
4958 * processing either at return from interrupt or when softrqs are
4959 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4960 * directly.
4961 */
4962 if (in_interrupt())
4963 return netif_rx(skb);
4964 else
4965 return netif_rx_ni(skb);
4966}
4967EXPORT_SYMBOL(netif_rx_any_context);
4968
Emese Revfy0766f782016-06-20 20:42:34 +02004969static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004971 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 if (sd->completion_queue) {
4974 struct sk_buff *clist;
4975
4976 local_irq_disable();
4977 clist = sd->completion_queue;
4978 sd->completion_queue = NULL;
4979 local_irq_enable();
4980
4981 while (clist) {
4982 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 clist = clist->next;
4985
Reshetova, Elena63354792017-06-30 13:07:58 +03004986 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004987 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4988 trace_consume_skb(skb);
4989 else
4990 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004991
4992 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4993 __kfree_skb(skb);
4994 else
4995 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
4997 }
4998
4999 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07005000 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 local_irq_disable();
5003 head = sd->output_queue;
5004 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00005005 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 local_irq_enable();
5007
Yunsheng Lin102b55e2021-05-14 11:17:00 +08005008 rcu_read_lock();
5009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07005011 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08005012 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07005013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 head = head->next_sched;
5015
Eric Dumazet3bcb8462016-06-04 20:02:28 -07005016 /* We need to make sure head->next_sched is read
5017 * before clearing __QDISC_STATE_SCHED
5018 */
5019 smp_mb__before_atomic();
Yunsheng Lin102b55e2021-05-14 11:17:00 +08005020
5021 if (!(q->flags & TCQ_F_NOLOCK)) {
5022 root_lock = qdisc_lock(q);
5023 spin_lock(root_lock);
5024 } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
5025 &q->state))) {
5026 /* There is a synchronize_net() between
5027 * STATE_DEACTIVATED flag being set and
5028 * qdisc_reset()/some_qdisc_is_busy() in
5029 * dev_deactivate(), so we can safely bail out
5030 * early here to avoid data race between
5031 * qdisc_deactivate() and some_qdisc_is_busy()
5032 * for lockless qdisc.
5033 */
5034 clear_bit(__QDISC_STATE_SCHED, &q->state);
5035 continue;
5036 }
5037
Eric Dumazet3bcb8462016-06-04 20:02:28 -07005038 clear_bit(__QDISC_STATE_SCHED, &q->state);
5039 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08005040 if (root_lock)
5041 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
Yunsheng Lin102b55e2021-05-14 11:17:00 +08005043
5044 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01005046
5047 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04005050#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00005051/* This hook is defined here for ATM LANE */
5052int (*br_fdb_test_addr_hook)(struct net_device *dev,
5053 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07005054EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00005055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
Daniel Borkmann1f211a12016-01-07 22:29:47 +01005057static inline struct sk_buff *
5058sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005059 struct net_device *orig_dev, bool *another)
Herbert Xuf697c3e2007-10-14 00:38:47 -07005060{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005061#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01005062 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005063 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00005064
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02005065 /* If there's at least one ingress present somewhere (so
5066 * we get here via enabled static key), remaining devices
5067 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005068 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02005069 */
Jiri Pirko46209402017-11-03 11:46:25 +01005070 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02005071 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01005072
Herbert Xuf697c3e2007-10-14 00:38:47 -07005073 if (*pt_prev) {
5074 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5075 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07005076 }
5077
Florian Westphal33654952015-05-14 00:36:28 +02005078 qdisc_skb_cb(skb)->pkt_len = skb->len;
wenxuaadaca92020-11-25 12:01:21 +08005079 qdisc_skb_cb(skb)->mru = 0;
wenxu7baf2422021-01-19 16:31:50 +08005080 qdisc_skb_cb(skb)->post_ct = false;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05005081 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01005082 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02005083
Davide Caratti3aa26052021-07-28 20:08:00 +02005084 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005085 case TC_ACT_OK:
5086 case TC_ACT_RECLASSIFY:
5087 skb->tc_index = TC_H_MIN(cl_res.classid);
5088 break;
5089 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01005090 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07005091 kfree_skb(skb);
5092 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005093 case TC_ACT_STOLEN:
5094 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02005095 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07005096 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005097 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005098 case TC_ACT_REDIRECT:
5099 /* skb_mac_header check was done by cls/act_bpf, so
5100 * we can safely push the L2 header back before
5101 * redirecting to another netdev
5102 */
5103 __skb_push(skb, skb->mac_len);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005104 if (skb_do_redirect(skb) == -EAGAIN) {
5105 __skb_pull(skb, skb->mac_len);
5106 *another = true;
5107 break;
5108 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005109 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01005110 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02005111 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005112 default:
5113 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07005114 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005115#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07005116 return skb;
5117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005119/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07005120 * netdev_is_rx_handler_busy - check if receive handler is registered
5121 * @dev: device to check
5122 *
5123 * Check if a receive handler is already registered for a given device.
5124 * Return true if there one.
5125 *
5126 * The caller must hold the rtnl_mutex.
5127 */
5128bool netdev_is_rx_handler_busy(struct net_device *dev)
5129{
5130 ASSERT_RTNL();
5131 return dev && rtnl_dereference(dev->rx_handler);
5132}
5133EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5134
5135/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005136 * netdev_rx_handler_register - register receive handler
5137 * @dev: device to register a handler for
5138 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00005139 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005140 *
Masanari Iidae2278672014-02-18 22:54:36 +09005141 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005142 * called from __netif_receive_skb. A negative errno code is returned
5143 * on a failure.
5144 *
5145 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005146 *
5147 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005148 */
5149int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00005150 rx_handler_func_t *rx_handler,
5151 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005152{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08005153 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005154 return -EBUSY;
5155
Paolo Abenif54262502018-03-09 10:39:24 +01005156 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5157 return -EINVAL;
5158
Eric Dumazet00cfec32013-03-29 03:01:22 +00005159 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00005160 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005161 rcu_assign_pointer(dev->rx_handler, rx_handler);
5162
5163 return 0;
5164}
5165EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5166
5167/**
5168 * netdev_rx_handler_unregister - unregister receive handler
5169 * @dev: device to unregister a handler from
5170 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00005171 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005172 *
5173 * The caller must hold the rtnl_mutex.
5174 */
5175void netdev_rx_handler_unregister(struct net_device *dev)
5176{
5177
5178 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005179 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00005180 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5181 * section has a guarantee to see a non NULL rx_handler_data
5182 * as well.
5183 */
5184 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005185 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005186}
5187EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5188
Mel Gormanb4b9e352012-07-31 16:44:26 -07005189/*
5190 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5191 * the special handling of PFMEMALLOC skbs.
5192 */
5193static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5194{
5195 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07005196 case htons(ETH_P_ARP):
5197 case htons(ETH_P_IP):
5198 case htons(ETH_P_IPV6):
5199 case htons(ETH_P_8021Q):
5200 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07005201 return true;
5202 default:
5203 return false;
5204 }
5205}
5206
Pablo Neirae687ad62015-05-13 18:19:38 +02005207static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5208 int *ret, struct net_device *orig_dev)
5209{
5210 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04005211 int ingress_retval;
5212
Pablo Neirae687ad62015-05-13 18:19:38 +02005213 if (*pt_prev) {
5214 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5215 *pt_prev = NULL;
5216 }
5217
Aaron Conole2c1e2702016-09-21 11:35:03 -04005218 rcu_read_lock();
5219 ingress_retval = nf_hook_ingress(skb);
5220 rcu_read_unlock();
5221 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02005222 }
5223 return 0;
5224}
Pablo Neirae687ad62015-05-13 18:19:38 +02005225
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005226static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
Edward Cree88eb1942018-07-02 16:13:56 +01005227 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
5229 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005230 rx_handler_func_t *rx_handler;
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005231 struct sk_buff *skb = *pskb;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07005232 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005233 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08005235 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Eric Dumazet588f0332011-11-15 04:12:55 +00005237 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005238
Koki Sanagicf66ba52010-08-23 18:45:02 +09005239 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005240
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005241 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005242
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005243 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005244 if (!skb_transport_header_was_set(skb))
5245 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005246 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 pt_prev = NULL;
5249
David S. Miller63d8ea72011-02-28 10:48:59 -08005250another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005251 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005252
5253 __this_cpu_inc(softnet_data.processed);
5254
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005255 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5256 int ret2;
5257
Sebastian Andrzej Siewior2b4cd142021-06-17 09:38:17 +02005258 migrate_disable();
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005259 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
Sebastian Andrzej Siewior2b4cd142021-06-17 09:38:17 +02005260 migrate_enable();
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005261
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005262 if (ret2 != XDP_PASS) {
5263 ret = NET_RX_DROP;
5264 goto out;
5265 }
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005266 }
5267
Menglong Dong324cefa2021-01-11 02:42:21 -08005268 if (eth_type_vlan(skb->protocol)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005269 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005270 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005271 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005272 }
5273
Willem de Bruijne7246e12017-01-07 17:06:35 -05005274 if (skb_skip_tc_classify(skb))
5275 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
David S. Miller9754e292013-02-14 15:57:38 -05005277 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005278 goto skip_taps;
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005281 if (pt_prev)
5282 ret = deliver_skb(skb, pt_prev, orig_dev);
5283 pt_prev = ptype;
5284 }
5285
5286 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5287 if (pt_prev)
5288 ret = deliver_skb(skb, pt_prev, orig_dev);
5289 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 }
5291
Mel Gormanb4b9e352012-07-31 16:44:26 -07005292skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005293#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005294 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005295 bool another = false;
5296
5297 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5298 &another);
5299 if (another)
5300 goto another_round;
Daniel Borkmann45771392015-04-10 23:07:54 +02005301 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005302 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005303
5304 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005305 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005306 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005307#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005308 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005309skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005310 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005311 goto drop;
5312
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005313 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005314 if (pt_prev) {
5315 ret = deliver_skb(skb, pt_prev, orig_dev);
5316 pt_prev = NULL;
5317 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005318 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005319 goto another_round;
5320 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005321 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005322 }
5323
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005324 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005325 if (rx_handler) {
5326 if (pt_prev) {
5327 ret = deliver_skb(skb, pt_prev, orig_dev);
5328 pt_prev = NULL;
5329 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005330 switch (rx_handler(&skb)) {
5331 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005332 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005333 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005334 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005335 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005336 case RX_HANDLER_EXACT:
5337 deliver_exact = true;
Gustavo A. R. Silvab1866bf2021-03-09 23:42:43 -06005338 break;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005339 case RX_HANDLER_PASS:
5340 break;
5341 default:
5342 BUG();
5343 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +03005346 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005347check_vlan_id:
5348 if (skb_vlan_tag_get_id(skb)) {
5349 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5350 * find vlan device.
5351 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005352 skb->pkt_type = PACKET_OTHERHOST;
Menglong Dong324cefa2021-01-11 02:42:21 -08005353 } else if (eth_type_vlan(skb->protocol)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005354 /* Outer header is 802.1P with vlan 0, inner header is
5355 * 802.1Q or 802.1AD and vlan_do_receive() above could
5356 * not find vlan dev for vlan id 0.
5357 */
5358 __vlan_hwaccel_clear_tag(skb);
5359 skb = skb_vlan_untag(skb);
5360 if (unlikely(!skb))
5361 goto out;
5362 if (vlan_do_receive(&skb))
5363 /* After stripping off 802.1P header with vlan 0
5364 * vlan dev is found for inner header.
5365 */
5366 goto another_round;
5367 else if (unlikely(!skb))
5368 goto out;
5369 else
5370 /* We have stripped outer 802.1P vlan 0 header.
5371 * But could not find vlan dev.
5372 * check again for vlan id to set OTHERHOST.
5373 */
5374 goto check_vlan_id;
5375 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005376 /* Note: we might in the future use prio bits
5377 * and set skb->priority like in vlan_do_receive()
5378 * For the time being, just ignore Priority Code Point
5379 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005380 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005381 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005384
5385 /* deliver only exact match when indicated */
5386 if (likely(!deliver_exact)) {
5387 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5388 &ptype_base[ntohs(type) &
5389 PTYPE_HASH_MASK]);
5390 }
5391
5392 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5393 &orig_dev->ptype_specific);
5394
5395 if (unlikely(skb->dev != orig_dev)) {
5396 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5397 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 }
5399
5400 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005401 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005402 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005403 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005405drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005406 if (!deliver_exact)
5407 atomic_long_inc(&skb->dev->rx_dropped);
5408 else
5409 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 kfree_skb(skb);
5411 /* Jamal, now you will not able to escape explaining
5412 * me how you were going to use this. :-)
5413 */
5414 ret = NET_RX_DROP;
5415 }
5416
Julian Anastasov2c17d272015-07-09 09:59:10 +03005417out:
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005418 /* The invariant here is that if *ppt_prev is not NULL
5419 * then skb should also be non-NULL.
5420 *
5421 * Apparently *ppt_prev assignment above holds this invariant due to
5422 * skb dereferencing near it.
5423 */
5424 *pskb = skb;
David S. Miller9754e292013-02-14 15:57:38 -05005425 return ret;
5426}
5427
Edward Cree88eb1942018-07-02 16:13:56 +01005428static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5429{
5430 struct net_device *orig_dev = skb->dev;
5431 struct packet_type *pt_prev = NULL;
5432 int ret;
5433
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005434 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree88eb1942018-07-02 16:13:56 +01005435 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005436 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5437 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005438 return ret;
5439}
5440
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005441/**
5442 * netif_receive_skb_core - special purpose version of netif_receive_skb
5443 * @skb: buffer to process
5444 *
5445 * More direct receive version of netif_receive_skb(). It should
5446 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005447 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005448 *
5449 * This function may only be called from softirq context and interrupts
5450 * should be enabled.
5451 *
5452 * Return values (usually ignored):
5453 * NET_RX_SUCCESS: no congestion
5454 * NET_RX_DROP: packet was dropped
5455 */
5456int netif_receive_skb_core(struct sk_buff *skb)
5457{
5458 int ret;
5459
5460 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005461 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005462 rcu_read_unlock();
5463
5464 return ret;
5465}
5466EXPORT_SYMBOL(netif_receive_skb_core);
5467
Edward Cree88eb1942018-07-02 16:13:56 +01005468static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5469 struct packet_type *pt_prev,
5470 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005471{
5472 struct sk_buff *skb, *next;
5473
Edward Cree88eb1942018-07-02 16:13:56 +01005474 if (!pt_prev)
5475 return;
5476 if (list_empty(head))
5477 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005478 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005479 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5480 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005481 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005482 list_for_each_entry_safe(skb, next, head, list) {
5483 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005484 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005485 }
Edward Cree88eb1942018-07-02 16:13:56 +01005486}
5487
5488static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5489{
5490 /* Fast-path assumptions:
5491 * - There is no RX handler.
5492 * - Only one packet_type matches.
5493 * If either of these fails, we will end up doing some per-packet
5494 * processing in-line, then handling the 'last ptype' for the whole
5495 * sublist. This can't cause out-of-order delivery to any single ptype,
5496 * because the 'last ptype' must be constant across the sublist, and all
5497 * other ptypes are handled per-packet.
5498 */
5499 /* Current (common) ptype of sublist */
5500 struct packet_type *pt_curr = NULL;
5501 /* Current (common) orig_dev of sublist */
5502 struct net_device *od_curr = NULL;
5503 struct list_head sublist;
5504 struct sk_buff *skb, *next;
5505
Edward Cree9af86f92018-07-09 18:10:19 +01005506 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005507 list_for_each_entry_safe(skb, next, head, list) {
5508 struct net_device *orig_dev = skb->dev;
5509 struct packet_type *pt_prev = NULL;
5510
Edward Cree22f6bbb2018-12-04 17:37:57 +00005511 skb_list_del_init(skb);
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005512 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005513 if (!pt_prev)
5514 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005515 if (pt_curr != pt_prev || od_curr != orig_dev) {
5516 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005517 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5518 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005519 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005520 pt_curr = pt_prev;
5521 od_curr = orig_dev;
5522 }
Edward Cree9af86f92018-07-09 18:10:19 +01005523 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005524 }
5525
5526 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005527 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005528}
5529
David S. Miller9754e292013-02-14 15:57:38 -05005530static int __netif_receive_skb(struct sk_buff *skb)
5531{
5532 int ret;
5533
5534 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005535 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005536
5537 /*
5538 * PFMEMALLOC skbs are special, they should
5539 * - be delivered to SOCK_MEMALLOC sockets only
5540 * - stay away from userspace
5541 * - have bounded memory usage
5542 *
5543 * Use PF_MEMALLOC as this saves us from propagating the allocation
5544 * context down to all allocation sites.
5545 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005546 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005547 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005548 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005549 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005550 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return ret;
5553}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005554
Edward Cree4ce00172018-07-02 16:13:40 +01005555static void __netif_receive_skb_list(struct list_head *head)
5556{
5557 unsigned long noreclaim_flag = 0;
5558 struct sk_buff *skb, *next;
5559 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5560
5561 list_for_each_entry_safe(skb, next, head, list) {
5562 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5563 struct list_head sublist;
5564
5565 /* Handle the previous sublist */
5566 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005567 if (!list_empty(&sublist))
5568 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005569 pfmemalloc = !pfmemalloc;
5570 /* See comments in __netif_receive_skb */
5571 if (pfmemalloc)
5572 noreclaim_flag = memalloc_noreclaim_save();
5573 else
5574 memalloc_noreclaim_restore(noreclaim_flag);
5575 }
5576 }
5577 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005578 if (!list_empty(head))
5579 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005580 /* Restore pflags */
5581 if (pfmemalloc)
5582 memalloc_noreclaim_restore(noreclaim_flag);
5583}
5584
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005585static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005586{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005587 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005588 struct bpf_prog *new = xdp->prog;
5589 int ret = 0;
5590
5591 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005592 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005593 rcu_assign_pointer(dev->xdp_prog, new);
5594 if (old)
5595 bpf_prog_put(old);
5596
5597 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005598 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005599 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005600 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005601 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005602 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005603 }
5604 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005605
David S. Millerb5cdae32017-04-18 15:36:58 -04005606 default:
5607 ret = -EINVAL;
5608 break;
5609 }
5610
5611 return ret;
5612}
5613
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005614static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005615{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005616 int ret;
5617
Eric Dumazet588f0332011-11-15 04:12:55 +00005618 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005619
Richard Cochranc1f19b52010-07-17 08:49:36 +00005620 if (skb_defer_rx_timestamp(skb))
5621 return NET_RX_SUCCESS;
5622
John Fastabendbbbe2112017-09-08 14:00:30 -07005623 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005624#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005625 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005626 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005627 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005628
Eric Dumazet3b098e22010-05-15 23:57:10 -07005629 if (cpu >= 0) {
5630 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5631 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005632 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005633 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005634 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005635#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005636 ret = __netif_receive_skb(skb);
5637 rcu_read_unlock();
5638 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005639}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005640
Edward Cree7da517a2018-07-02 16:13:24 +01005641static void netif_receive_skb_list_internal(struct list_head *head)
5642{
Edward Cree7da517a2018-07-02 16:13:24 +01005643 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005644 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005645
Edward Cree8c057ef2018-07-09 18:09:54 +01005646 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005647 list_for_each_entry_safe(skb, next, head, list) {
5648 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005649 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005650 if (!skb_defer_rx_timestamp(skb))
5651 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005652 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005653 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005654
Edward Cree7da517a2018-07-02 16:13:24 +01005655 rcu_read_lock();
5656#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005657 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005658 list_for_each_entry_safe(skb, next, head, list) {
5659 struct rps_dev_flow voidflow, *rflow = &voidflow;
5660 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5661
5662 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005663 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005664 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005665 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005666 }
5667 }
5668 }
5669#endif
5670 __netif_receive_skb_list(head);
5671 rcu_read_unlock();
5672}
5673
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005674/**
5675 * netif_receive_skb - process receive buffer from network
5676 * @skb: buffer to process
5677 *
5678 * netif_receive_skb() is the main receive data processing function.
5679 * It always succeeds. The buffer may be dropped during processing
5680 * for congestion control or by the protocol layers.
5681 *
5682 * This function may only be called from softirq context and interrupts
5683 * should be enabled.
5684 *
5685 * Return values (usually ignored):
5686 * NET_RX_SUCCESS: no congestion
5687 * NET_RX_DROP: packet was dropped
5688 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005689int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005690{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005691 int ret;
5692
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005693 trace_netif_receive_skb_entry(skb);
5694
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005695 ret = netif_receive_skb_internal(skb);
5696 trace_netif_receive_skb_exit(ret);
5697
5698 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005699}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005700EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
Edward Creef6ad8c12018-07-02 16:12:45 +01005702/**
5703 * netif_receive_skb_list - process many receive buffers from network
5704 * @head: list of skbs to process.
5705 *
Edward Cree7da517a2018-07-02 16:13:24 +01005706 * Since return value of netif_receive_skb() is normally ignored, and
5707 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005708 *
5709 * This function may only be called from softirq context and interrupts
5710 * should be enabled.
5711 */
5712void netif_receive_skb_list(struct list_head *head)
5713{
Edward Cree7da517a2018-07-02 16:13:24 +01005714 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005715
Edward Creeb9f463d2018-07-02 16:14:44 +01005716 if (list_empty(head))
5717 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005718 if (trace_netif_receive_skb_list_entry_enabled()) {
5719 list_for_each_entry(skb, head, list)
5720 trace_netif_receive_skb_list_entry(skb);
5721 }
Edward Cree7da517a2018-07-02 16:13:24 +01005722 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005723 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005724}
5725EXPORT_SYMBOL(netif_receive_skb_list);
5726
Wei Yongjunce1e2a72020-07-13 22:23:44 +08005727static DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005728
5729/* Network device is going away, flush any packets still pending */
5730static void flush_backlog(struct work_struct *work)
5731{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005732 struct sk_buff *skb, *tmp;
5733 struct softnet_data *sd;
5734
5735 local_bh_disable();
5736 sd = this_cpu_ptr(&softnet_data);
5737
5738 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005739 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005740 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005741 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005742 __skb_unlink(skb, &sd->input_pkt_queue);
Subash Abhinov Kasiviswanathan7df5cb752020-07-23 11:31:48 -06005743 dev_kfree_skb_irq(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005744 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005745 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005746 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005747 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005748 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005749
5750 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005751 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005752 __skb_unlink(skb, &sd->process_queue);
5753 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005754 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005755 }
5756 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005757 local_bh_enable();
5758}
5759
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005760static bool flush_required(int cpu)
5761{
5762#if IS_ENABLED(CONFIG_RPS)
5763 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5764 bool do_flush;
5765
5766 local_irq_disable();
5767 rps_lock(sd);
5768
5769 /* as insertion into process_queue happens with the rps lock held,
5770 * process_queue access may race only with dequeue
5771 */
5772 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5773 !skb_queue_empty_lockless(&sd->process_queue);
5774 rps_unlock(sd);
5775 local_irq_enable();
5776
5777 return do_flush;
5778#endif
5779 /* without RPS we can't safely check input_pkt_queue: during a
5780 * concurrent remote skb_queue_splice() we can detect as empty both
5781 * input_pkt_queue and process_queue even if the latter could end-up
5782 * containing a lot of packets.
5783 */
5784 return true;
5785}
5786
Eric Dumazet41852492016-08-26 12:50:39 -07005787static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005788{
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005789 static cpumask_t flush_cpus;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005790 unsigned int cpu;
5791
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005792 /* since we are under rtnl lock protection we can use static data
5793 * for the cpumask and avoid allocating on stack the possibly
5794 * large mask
5795 */
5796 ASSERT_RTNL();
5797
Sebastian Andrzej Siewior372bbdd2021-08-03 16:16:06 +02005798 cpus_read_lock();
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005799
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005800 cpumask_clear(&flush_cpus);
5801 for_each_online_cpu(cpu) {
5802 if (flush_required(cpu)) {
5803 queue_work_on(cpu, system_highpri_wq,
5804 per_cpu_ptr(&flush_works, cpu));
5805 cpumask_set_cpu(cpu, &flush_cpus);
5806 }
5807 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005808
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005809 /* we can have in flight packet[s] on the cpus we are not flushing,
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -08005810 * synchronize_net() in unregister_netdevice_many() will take care of
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005811 * them
5812 */
5813 for_each_cpu(cpu, &flush_cpus)
Eric Dumazet41852492016-08-26 12:50:39 -07005814 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005815
Sebastian Andrzej Siewior372bbdd2021-08-03 16:16:06 +02005816 cpus_read_unlock();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005817}
5818
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005819/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5820static void gro_normal_list(struct napi_struct *napi)
5821{
5822 if (!napi->rx_count)
5823 return;
5824 netif_receive_skb_list_internal(&napi->rx_list);
5825 INIT_LIST_HEAD(&napi->rx_list);
5826 napi->rx_count = 0;
5827}
5828
5829/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5830 * pass the whole batch up to the stack.
5831 */
Eric Dumazet8dc1c442021-02-04 13:31:46 -08005832static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005833{
5834 list_add_tail(&skb->list, &napi->rx_list);
Eric Dumazet8dc1c442021-02-04 13:31:46 -08005835 napi->rx_count += segs;
5836 if (napi->rx_count >= gro_normal_batch)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005837 gro_normal_list(napi);
5838}
5839
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005840static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005841{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005842 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005843 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005844 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005845 int err = -ENOENT;
5846
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005847 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5848
Herbert Xufc59f9a2009-04-14 15:11:06 -07005849 if (NAPI_GRO_CB(skb)->count == 1) {
5850 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005851 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005852 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005853
5854 rcu_read_lock();
5855 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005856 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005857 continue;
5858
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005859 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5860 ipv6_gro_complete, inet_gro_complete,
5861 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005862 break;
5863 }
5864 rcu_read_unlock();
5865
5866 if (err) {
5867 WARN_ON(&ptype->list == head);
5868 kfree_skb(skb);
5869 return NET_RX_SUCCESS;
5870 }
5871
5872out:
Eric Dumazet8dc1c442021-02-04 13:31:46 -08005873 gro_normal_one(napi, skb, NAPI_GRO_CB(skb)->count);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005874 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005875}
5876
Li RongQing6312fe72018-07-05 14:34:32 +08005877static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005878 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005879{
Li RongQing6312fe72018-07-05 14:34:32 +08005880 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005881 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005882
David Miller07d78362018-06-24 14:14:02 +09005883 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005884 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5885 return;
David S. Miller992cba72018-07-31 15:27:56 -07005886 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005887 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005888 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005889 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005890
5891 if (!napi->gro_hash[index].count)
5892 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005893}
David Miller07d78362018-06-24 14:14:02 +09005894
Li RongQing6312fe72018-07-05 14:34:32 +08005895/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005896 * youngest packets at the head of it.
5897 * Complete skbs in reverse order to reduce latencies.
5898 */
5899void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5900{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005901 unsigned long bitmask = napi->gro_bitmask;
5902 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005903
Eric Dumazet42519ed2018-11-21 11:39:28 -08005904 while ((i = ffs(bitmask)) != 0) {
5905 bitmask >>= i;
5906 base += i;
5907 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005908 }
David Miller07d78362018-06-24 14:14:02 +09005909}
Eric Dumazet86cac582010-08-31 18:25:32 +00005910EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005911
Alexander Lobakin0ccf4d52021-03-13 20:30:05 +00005912static void gro_list_prepare(const struct list_head *head,
5913 const struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005914{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005915 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005916 u32 hash = skb_get_hash_raw(skb);
David Millerd4546c22018-06-24 14:13:49 +09005917 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005918
David Miller07d78362018-06-24 14:14:02 +09005919 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005920 unsigned long diffs;
5921
Tom Herbert0b4cec82014-01-15 08:58:06 -08005922 NAPI_GRO_CB(p)->flush = 0;
5923
5924 if (hash != skb_get_hash_raw(p)) {
5925 NAPI_GRO_CB(p)->same_flow = 0;
5926 continue;
5927 }
5928
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005929 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005930 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5931 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005932 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005933 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005934 if (maclen == ETH_HLEN)
5935 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005936 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005937 else if (!diffs)
5938 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005939 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005940 maclen);
Paul Blakey8550ff82021-07-05 13:54:51 +03005941
Paolo Abenia4329342021-07-30 18:30:53 +02005942 /* in most common scenarions 'slow_gro' is 0
Paolo Abeni9efb4b52021-07-28 18:24:02 +02005943 * otherwise we are already on some slower paths
5944 * either skip all the infrequent tests altogether or
5945 * avoid trying too hard to skip each of them individually
5946 */
5947 if (!diffs && unlikely(skb->slow_gro | p->slow_gro)) {
Florian Fainelli9615fe32021-07-07 21:10:51 -07005948#if IS_ENABLED(CONFIG_SKB_EXTENSIONS) && IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
Paolo Abeni9efb4b52021-07-28 18:24:02 +02005949 struct tc_skb_ext *skb_ext;
5950 struct tc_skb_ext *p_ext;
5951#endif
5952
Paolo Abeni5e10da52021-07-28 18:24:03 +02005953 diffs |= p->sk != skb->sk;
Paolo Abeni9efb4b52021-07-28 18:24:02 +02005954 diffs |= skb_metadata_dst_cmp(p, skb);
5955 diffs |= skb_get_nfct(p) ^ skb_get_nfct(skb);
5956
5957#if IS_ENABLED(CONFIG_SKB_EXTENSIONS) && IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
5958 skb_ext = skb_ext_find(skb, TC_SKB_EXT);
5959 p_ext = skb_ext_find(p, TC_SKB_EXT);
Paul Blakey8550ff82021-07-05 13:54:51 +03005960
5961 diffs |= (!!p_ext) ^ (!!skb_ext);
5962 if (!diffs && unlikely(skb_ext))
5963 diffs |= p_ext->chain ^ skb_ext->chain;
Florian Fainelli9615fe32021-07-07 21:10:51 -07005964#endif
Paolo Abeni9efb4b52021-07-28 18:24:02 +02005965 }
Paul Blakey8550ff82021-07-05 13:54:51 +03005966
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005967 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005968 }
5969}
5970
Alexander Lobakin7ad18ff2021-04-19 12:53:06 +00005971static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
Jerry Chu299603e82013-12-11 20:53:45 -08005972{
5973 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5974 const skb_frag_t *frag0 = &pinfo->frags[0];
5975
5976 NAPI_GRO_CB(skb)->data_offset = 0;
5977 NAPI_GRO_CB(skb)->frag0 = NULL;
5978 NAPI_GRO_CB(skb)->frag0_len = 0;
5979
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005980 if (!skb_headlen(skb) && pinfo->nr_frags &&
Eric Dumazet38ec4942021-04-13 05:41:35 -07005981 !PageHighMem(skb_frag_page(frag0)) &&
Alexander Lobakin7ad18ff2021-04-19 12:53:06 +00005982 (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) {
Jerry Chu299603e82013-12-11 20:53:45 -08005983 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005984 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5985 skb_frag_size(frag0),
5986 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005987 }
5988}
5989
Eric Dumazeta50e2332014-03-29 21:28:21 -07005990static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5991{
5992 struct skb_shared_info *pinfo = skb_shinfo(skb);
5993
5994 BUG_ON(skb->end - skb->tail < grow);
5995
5996 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5997
5998 skb->data_len -= grow;
5999 skb->tail += grow;
6000
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006001 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006002 skb_frag_size_sub(&pinfo->frags[0], grow);
6003
6004 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
6005 skb_frag_unref(skb, 0);
6006 memmove(pinfo->frags, pinfo->frags + 1,
6007 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
6008 }
6009}
6010
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006011static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09006012{
Li RongQing6312fe72018-07-05 14:34:32 +08006013 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09006014
Li RongQing6312fe72018-07-05 14:34:32 +08006015 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09006016
Li RongQing6312fe72018-07-05 14:34:32 +08006017 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09006018 * impossible.
6019 */
6020 if (WARN_ON_ONCE(!oldest))
6021 return;
6022
Li RongQingd9f37d02018-07-13 14:41:36 +08006023 /* Do not adjust napi->gro_hash[].count, caller is adding a new
6024 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09006025 */
David S. Millerece23712018-10-28 10:35:12 -07006026 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006027 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09006028}
6029
Rami Rosenbb728822012-11-28 21:55:25 +00006030static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08006031{
Alexander Lobakind0eed5c2021-03-13 20:30:14 +00006032 u32 bucket = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
6033 struct gro_list *gro_list = &napi->gro_hash[bucket];
David Millerd4546c22018-06-24 14:13:49 +09006034 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00006035 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08006036 __be16 type = skb->protocol;
David Millerd4546c22018-06-24 14:13:49 +09006037 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006038 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09006039 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006040 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08006041
David S. Millerb5cdae32017-04-18 15:36:58 -04006042 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08006043 goto normal;
6044
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006045 gro_list_prepare(&gro_list->list, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00006046
Herbert Xud565b0a2008-12-15 23:38:52 -08006047 rcu_read_lock();
6048 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00006049 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08006050 continue;
6051
Herbert Xu86911732009-01-29 14:19:50 +00006052 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00006053 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08006054 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08006055 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08006056 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07006057 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02006058 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07006059 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04006060 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08006061 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006062
Tom Herbert662880f2014-08-27 21:26:56 -07006063 /* Setup for GRO checksum validation */
6064 switch (skb->ip_summed) {
6065 case CHECKSUM_COMPLETE:
6066 NAPI_GRO_CB(skb)->csum = skb->csum;
6067 NAPI_GRO_CB(skb)->csum_valid = 1;
6068 NAPI_GRO_CB(skb)->csum_cnt = 0;
6069 break;
6070 case CHECKSUM_UNNECESSARY:
6071 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
6072 NAPI_GRO_CB(skb)->csum_valid = 0;
6073 break;
6074 default:
6075 NAPI_GRO_CB(skb)->csum_cnt = 0;
6076 NAPI_GRO_CB(skb)->csum_valid = 0;
6077 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006078
Paolo Abeniaaa5d902018-12-14 11:51:58 +01006079 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
6080 ipv6_gro_receive, inet_gro_receive,
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006081 &gro_list->list, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08006082 break;
6083 }
6084 rcu_read_unlock();
6085
6086 if (&ptype->list == head)
6087 goto normal;
6088
Masahiro Yamada45586c72020-02-03 17:37:45 -08006089 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01006090 ret = GRO_CONSUMED;
6091 goto ok;
6092 }
6093
Herbert Xu0da2afd52008-12-26 14:57:42 -08006094 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006095 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08006096
Herbert Xud565b0a2008-12-15 23:38:52 -08006097 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07006098 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006099 napi_gro_complete(napi, pp);
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006100 gro_list->count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08006101 }
6102
Herbert Xu0da2afd52008-12-26 14:57:42 -08006103 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08006104 goto ok;
6105
Eric Dumazet600adc12014-01-09 14:12:19 -08006106 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08006107 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08006108
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006109 if (unlikely(gro_list->count >= MAX_GRO_SKBS))
6110 gro_flush_oldest(napi, &gro_list->list);
6111 else
6112 gro_list->count++;
6113
Herbert Xud565b0a2008-12-15 23:38:52 -08006114 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00006115 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07006116 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00006117 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006118 list_add(&skb->list, &gro_list->list);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006119 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08006120
Herbert Xuad0f99042009-02-01 01:24:55 -08006121pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006122 grow = skb_gro_offset(skb) - skb_headlen(skb);
6123 if (grow > 0)
6124 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08006125ok:
Alexander Lobakin9dc2c312021-03-13 20:30:10 +00006126 if (gro_list->count) {
Alexander Lobakind0eed5c2021-03-13 20:30:14 +00006127 if (!test_bit(bucket, &napi->gro_bitmask))
6128 __set_bit(bucket, &napi->gro_bitmask);
6129 } else if (test_bit(bucket, &napi->gro_bitmask)) {
6130 __clear_bit(bucket, &napi->gro_bitmask);
Li RongQingd9f37d02018-07-13 14:41:36 +08006131 }
6132
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006133 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006134
6135normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08006136 ret = GRO_NORMAL;
6137 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08006138}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006139
Jerry Chubf5a7552014-01-07 10:23:19 -08006140struct packet_offload *gro_find_receive_by_type(__be16 type)
6141{
6142 struct list_head *offload_head = &offload_base;
6143 struct packet_offload *ptype;
6144
6145 list_for_each_entry_rcu(ptype, offload_head, list) {
6146 if (ptype->type != type || !ptype->callbacks.gro_receive)
6147 continue;
6148 return ptype;
6149 }
6150 return NULL;
6151}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006152EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08006153
6154struct packet_offload *gro_find_complete_by_type(__be16 type)
6155{
6156 struct list_head *offload_head = &offload_base;
6157 struct packet_offload *ptype;
6158
6159 list_for_each_entry_rcu(ptype, offload_head, list) {
6160 if (ptype->type != type || !ptype->callbacks.gro_complete)
6161 continue;
6162 return ptype;
6163 }
6164 return NULL;
6165}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006166EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006167
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006168static gro_result_t napi_skb_finish(struct napi_struct *napi,
6169 struct sk_buff *skb,
6170 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08006171{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006172 switch (ret) {
6173 case GRO_NORMAL:
Eric Dumazet8dc1c442021-02-04 13:31:46 -08006174 gro_normal_one(napi, skb, 1);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006175 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006176
Eric Dumazetdaa86542012-04-19 07:07:40 +00006177 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02006178 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6179 napi_skb_free_stolen_head(skb);
Antoine Tenart28b34f02021-07-09 18:16:09 +02006180 else if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
6181 __kfree_skb(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02006182 else
Alexander Lobakin9243adf2021-02-13 14:13:09 +00006183 __kfree_skb_defer(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00006184 break;
6185
Ben Hutchings5b252f02009-10-29 07:17:09 +00006186 case GRO_HELD:
6187 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006188 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006189 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006190 }
6191
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006192 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006193}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006194
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006195gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006196{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006197 gro_result_t ret;
6198
Eric Dumazet93f93a42015-11-18 06:30:59 -08006199 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006200 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00006201
Alexander Lobakin7ad18ff2021-04-19 12:53:06 +00006202 skb_gro_reset_offset(skb, 0);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006203
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006204 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006205 trace_napi_gro_receive_exit(ret);
6206
6207 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006208}
6209EXPORT_SYMBOL(napi_gro_receive);
6210
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00006211static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006212{
Eric Dumazet93a35f52014-10-23 06:30:30 -07006213 if (unlikely(skb->pfmemalloc)) {
6214 consume_skb(skb);
6215 return;
6216 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006217 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00006218 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
6219 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01006220 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08006221 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08006222 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08006223
6224 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
6225 skb->pkt_type = PACKET_HOST;
6226
Jerry Chuc3caf112014-07-14 15:54:46 -07006227 skb->encapsulation = 0;
6228 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07006229 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Paolo Abeni9efb4b52021-07-28 18:24:02 +02006230 if (unlikely(skb->slow_gro)) {
Paolo Abeni5e10da52021-07-28 18:24:03 +02006231 skb_orphan(skb);
Paolo Abeni9efb4b52021-07-28 18:24:02 +02006232 skb_ext_reset(skb);
6233 nf_reset_ct(skb);
6234 skb->slow_gro = 0;
6235 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006236
6237 napi->skb = skb;
6238}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006239
Herbert Xu76620aa2009-04-16 02:02:07 -07006240struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08006241{
Herbert Xu5d38a072009-01-04 16:13:40 -08006242 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08006243
6244 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08006245 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08006246 if (skb) {
6247 napi->skb = skb;
6248 skb_mark_napi_id(skb, napi);
6249 }
Herbert Xu5d38a072009-01-04 16:13:40 -08006250 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006251 return skb;
6252}
Herbert Xu76620aa2009-04-16 02:02:07 -07006253EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006254
Eric Dumazeta50e2332014-03-29 21:28:21 -07006255static gro_result_t napi_frags_finish(struct napi_struct *napi,
6256 struct sk_buff *skb,
6257 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006258{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006259 switch (ret) {
6260 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006261 case GRO_HELD:
6262 __skb_push(skb, ETH_HLEN);
6263 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01006264 if (ret == GRO_NORMAL)
Eric Dumazet8dc1c442021-02-04 13:31:46 -08006265 gro_normal_one(napi, skb, 1);
Herbert Xu86911732009-01-29 14:19:50 +00006266 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006267
Michal Kubečeke44699d2017-06-29 11:13:36 +02006268 case GRO_MERGED_FREE:
6269 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6270 napi_skb_free_stolen_head(skb);
6271 else
6272 napi_reuse_skb(napi, skb);
6273 break;
6274
Ben Hutchings5b252f02009-10-29 07:17:09 +00006275 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006276 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006277 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006278 }
6279
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006280 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006281}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006282
Eric Dumazeta50e2332014-03-29 21:28:21 -07006283/* Upper GRO stack assumes network header starts at gro_offset=0
6284 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6285 * We copy ethernet header into skb->data to have a common layout.
6286 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00006287static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006288{
Herbert Xu76620aa2009-04-16 02:02:07 -07006289 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006290 const struct ethhdr *eth;
6291 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07006292
6293 napi->skb = NULL;
6294
Eric Dumazeta50e2332014-03-29 21:28:21 -07006295 skb_reset_mac_header(skb);
Alexander Lobakin7ad18ff2021-04-19 12:53:06 +00006296 skb_gro_reset_offset(skb, hlen);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006297
Eric Dumazeta50e2332014-03-29 21:28:21 -07006298 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6299 eth = skb_gro_header_slow(skb, hlen, 0);
6300 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006301 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6302 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006303 napi_reuse_skb(napi, skb);
6304 return NULL;
6305 }
6306 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006307 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006308 gro_pull_from_frag0(skb, hlen);
6309 NAPI_GRO_CB(skb)->frag0 += hlen;
6310 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006311 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006312 __skb_pull(skb, hlen);
6313
6314 /*
6315 * This works because the only protocols we care about don't require
6316 * special handling.
6317 * We'll fix it up properly in napi_frags_finish()
6318 */
6319 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006320
Herbert Xu76620aa2009-04-16 02:02:07 -07006321 return skb;
6322}
Herbert Xu76620aa2009-04-16 02:02:07 -07006323
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006324gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006325{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006326 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006327 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006328
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006329 trace_napi_gro_frags_entry(skb);
6330
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006331 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6332 trace_napi_gro_frags_exit(ret);
6333
6334 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006335}
6336EXPORT_SYMBOL(napi_gro_frags);
6337
Tom Herbert573e8fc2014-08-22 13:33:47 -07006338/* Compute the checksum from gro_offset and return the folded value
6339 * after adding in any pseudo checksum.
6340 */
6341__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6342{
6343 __wsum wsum;
6344 __sum16 sum;
6345
6346 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6347
6348 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6349 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006350 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006351 if (likely(!sum)) {
6352 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6353 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006354 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006355 }
6356
6357 NAPI_GRO_CB(skb)->csum = wsum;
6358 NAPI_GRO_CB(skb)->csum_valid = 1;
6359
6360 return sum;
6361}
6362EXPORT_SYMBOL(__skb_gro_checksum_complete);
6363
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306364static void net_rps_send_ipi(struct softnet_data *remsd)
6365{
6366#ifdef CONFIG_RPS
6367 while (remsd) {
6368 struct softnet_data *next = remsd->rps_ipi_next;
6369
6370 if (cpu_online(remsd->cpu))
6371 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6372 remsd = next;
6373 }
6374#endif
6375}
6376
Eric Dumazete326bed2010-04-22 00:22:45 -07006377/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006378 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006379 * Note: called with local irq disabled, but exits with local irq enabled.
6380 */
6381static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6382{
6383#ifdef CONFIG_RPS
6384 struct softnet_data *remsd = sd->rps_ipi_list;
6385
6386 if (remsd) {
6387 sd->rps_ipi_list = NULL;
6388
6389 local_irq_enable();
6390
6391 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306392 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006393 } else
6394#endif
6395 local_irq_enable();
6396}
6397
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006398static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6399{
6400#ifdef CONFIG_RPS
6401 return sd->rps_ipi_list != NULL;
6402#else
6403 return false;
6404#endif
6405}
6406
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006407static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006409 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006410 bool again = true;
6411 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
Eric Dumazete326bed2010-04-22 00:22:45 -07006413 /* Check if we have pending ipi, its better to send them now,
6414 * not waiting net_rx_action() end.
6415 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006416 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006417 local_irq_disable();
6418 net_rps_action_and_irq_enable(sd);
6419 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006420
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006421 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006422 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
Changli Gao6e7676c2010-04-27 15:07:33 -07006425 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006426 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006427 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006428 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006429 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006430 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006431 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006432
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006435 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006436 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006437 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006438 /*
6439 * Inline a custom version of __napi_complete().
6440 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006441 * and NAPI_STATE_SCHED is the only possible flag set
6442 * on backlog.
6443 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006444 * and we dont need an smp_mb() memory barrier.
6445 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006446 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006447 again = false;
6448 } else {
6449 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6450 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006451 }
6452 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006453 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006456 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457}
6458
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006459/**
6460 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006461 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006462 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006463 * The entry's receive function will be scheduled to run.
6464 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006465 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006466void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006467{
6468 unsigned long flags;
6469
6470 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006471 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006472 local_irq_restore(flags);
6473}
6474EXPORT_SYMBOL(__napi_schedule);
6475
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006476/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006477 * napi_schedule_prep - check if napi can be scheduled
6478 * @n: napi context
6479 *
6480 * Test if NAPI routine is already running, and if not mark
Jonathan Neuschäferee1a4c82020-09-05 20:32:18 +02006481 * it as running. This is used as a condition variable to
Eric Dumazet39e6c822017-02-28 10:34:50 -08006482 * insure only one NAPI poll instance runs. We also make
6483 * sure there is no pending NAPI disable.
6484 */
6485bool napi_schedule_prep(struct napi_struct *n)
6486{
6487 unsigned long val, new;
6488
6489 do {
6490 val = READ_ONCE(n->state);
6491 if (unlikely(val & NAPIF_STATE_DISABLE))
6492 return false;
6493 new = val | NAPIF_STATE_SCHED;
6494
6495 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6496 * This was suggested by Alexander Duyck, as compiler
6497 * emits better code than :
6498 * if (val & NAPIF_STATE_SCHED)
6499 * new |= NAPIF_STATE_MISSED;
6500 */
6501 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6502 NAPIF_STATE_MISSED;
6503 } while (cmpxchg(&n->state, val, new) != val);
6504
6505 return !(val & NAPIF_STATE_SCHED);
6506}
6507EXPORT_SYMBOL(napi_schedule_prep);
6508
6509/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006510 * __napi_schedule_irqoff - schedule for receive
6511 * @n: entry to schedule
6512 *
Sebastian Andrzej Siewior8380c812021-05-12 23:43:24 +02006513 * Variant of __napi_schedule() assuming hard irqs are masked.
6514 *
6515 * On PREEMPT_RT enabled kernels this maps to __napi_schedule()
6516 * because the interrupt disabled assumption might not be true
6517 * due to force-threaded interrupts and spinlock substitution.
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006518 */
6519void __napi_schedule_irqoff(struct napi_struct *n)
6520{
Sebastian Andrzej Siewior8380c812021-05-12 23:43:24 +02006521 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6522 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6523 else
6524 __napi_schedule(n);
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006525}
6526EXPORT_SYMBOL(__napi_schedule_irqoff);
6527
Eric Dumazet364b6052016-11-15 10:15:13 -08006528bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006529{
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006530 unsigned long flags, val, new, timeout = 0;
6531 bool ret = true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006532
6533 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006534 * 1) Don't let napi dequeue from the cpu poll list
6535 * just in case its running on a different cpu.
6536 * 2) If we are busy polling, do nothing here, we have
6537 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006538 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006539 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6540 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006541 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006542
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006543 if (work_done) {
6544 if (n->gro_bitmask)
Eric Dumazet7e417a62020-04-22 09:13:28 -07006545 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6546 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006547 }
6548 if (n->defer_hard_irqs_count > 0) {
6549 n->defer_hard_irqs_count--;
Eric Dumazet7e417a62020-04-22 09:13:28 -07006550 timeout = READ_ONCE(n->dev->gro_flush_timeout);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006551 if (timeout)
6552 ret = false;
6553 }
6554 if (n->gro_bitmask) {
Paolo Abeni605108a2018-11-21 18:21:35 +01006555 /* When the NAPI instance uses a timeout and keeps postponing
6556 * it, we need to bound somehow the time packets are kept in
6557 * the GRO layer
6558 */
6559 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006560 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006561
6562 gro_normal_list(n);
6563
Eric Dumazet02c16022017-02-04 15:25:02 -08006564 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006565 /* If n->poll_list is not empty, we need to mask irqs */
6566 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006567 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006568 local_irq_restore(flags);
6569 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006570
6571 do {
6572 val = READ_ONCE(n->state);
6573
6574 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6575
Björn Töpel7fd32532020-11-30 19:51:56 +01006576 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
Wei Wangcb038352021-03-16 15:36:47 -07006577 NAPIF_STATE_SCHED_THREADED |
Björn Töpel7fd32532020-11-30 19:51:56 +01006578 NAPIF_STATE_PREFER_BUSY_POLL);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006579
6580 /* If STATE_MISSED was set, leave STATE_SCHED set,
6581 * because we will call napi->poll() one more time.
6582 * This C code was suggested by Alexander Duyck to help gcc.
6583 */
6584 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6585 NAPIF_STATE_SCHED;
6586 } while (cmpxchg(&n->state, val, new) != val);
6587
6588 if (unlikely(val & NAPIF_STATE_MISSED)) {
6589 __napi_schedule(n);
6590 return false;
6591 }
6592
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006593 if (timeout)
6594 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6595 HRTIMER_MODE_REL_PINNED);
6596 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006597}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006598EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006599
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006600/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006601static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006602{
6603 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6604 struct napi_struct *napi;
6605
6606 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6607 if (napi->napi_id == napi_id)
6608 return napi;
6609
6610 return NULL;
6611}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006612
6613#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006614
Björn Töpel7fd32532020-11-30 19:51:56 +01006615static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
Eric Dumazet217f6972016-11-15 10:15:11 -08006616{
Björn Töpel7fd32532020-11-30 19:51:56 +01006617 if (!skip_schedule) {
6618 gro_normal_list(napi);
6619 __napi_schedule(napi);
6620 return;
6621 }
6622
6623 if (napi->gro_bitmask) {
6624 /* flush too old packets
6625 * If HZ < 1000, flush all packets.
6626 */
6627 napi_gro_flush(napi, HZ >= 1000);
6628 }
6629
6630 gro_normal_list(napi);
6631 clear_bit(NAPI_STATE_SCHED, &napi->state);
6632}
6633
Björn Töpel7c951caf2020-11-30 19:51:57 +01006634static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll,
6635 u16 budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006636{
6637 bool skip_schedule = false;
6638 unsigned long timeout;
Eric Dumazet217f6972016-11-15 10:15:11 -08006639 int rc;
6640
Eric Dumazet39e6c822017-02-28 10:34:50 -08006641 /* Busy polling means there is a high chance device driver hard irq
6642 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6643 * set in napi_schedule_prep().
6644 * Since we are about to call napi->poll() once more, we can safely
6645 * clear NAPI_STATE_MISSED.
6646 *
6647 * Note: x86 could use a single "lock and ..." instruction
6648 * to perform these two clear_bit()
6649 */
6650 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006651 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6652
6653 local_bh_disable();
6654
Björn Töpel7fd32532020-11-30 19:51:56 +01006655 if (prefer_busy_poll) {
6656 napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
6657 timeout = READ_ONCE(napi->dev->gro_flush_timeout);
6658 if (napi->defer_hard_irqs_count && timeout) {
6659 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6660 skip_schedule = true;
6661 }
6662 }
6663
Eric Dumazet217f6972016-11-15 10:15:11 -08006664 /* All we really want here is to re-enable device interrupts.
6665 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6666 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006667 rc = napi->poll(napi, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006668 /* We can't gro_normal_list() here, because napi->poll() might have
6669 * rearmed the napi (napi_complete_done()) in which case it could
6670 * already be running on another CPU.
6671 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006672 trace_napi_poll(napi, rc, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006673 netpoll_poll_unlock(have_poll_lock);
Björn Töpel7c951caf2020-11-30 19:51:57 +01006674 if (rc == budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006675 __busy_poll_stop(napi, skip_schedule);
Eric Dumazet217f6972016-11-15 10:15:11 -08006676 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006677}
6678
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006679void napi_busy_loop(unsigned int napi_id,
6680 bool (*loop_end)(void *, unsigned long),
Björn Töpel7c951caf2020-11-30 19:51:57 +01006681 void *loop_end_arg, bool prefer_busy_poll, u16 budget)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006682{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006683 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006684 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006685 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006686 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006687
6688restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006689 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006690
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006691 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006692
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006693 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006694 if (!napi)
6695 goto out;
6696
Eric Dumazet217f6972016-11-15 10:15:11 -08006697 preempt_disable();
6698 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006699 int work = 0;
6700
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006701 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006702 if (!napi_poll) {
6703 unsigned long val = READ_ONCE(napi->state);
6704
6705 /* If multiple threads are competing for this napi,
6706 * we avoid dirtying napi->state as much as we can.
6707 */
6708 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
Björn Töpel7fd32532020-11-30 19:51:56 +01006709 NAPIF_STATE_IN_BUSY_POLL)) {
6710 if (prefer_busy_poll)
6711 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006712 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006713 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006714 if (cmpxchg(&napi->state, val,
6715 val | NAPIF_STATE_IN_BUSY_POLL |
Björn Töpel7fd32532020-11-30 19:51:56 +01006716 NAPIF_STATE_SCHED) != val) {
6717 if (prefer_busy_poll)
6718 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006719 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006720 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006721 have_poll_lock = netpoll_poll_lock(napi);
6722 napi_poll = napi->poll;
6723 }
Björn Töpel7c951caf2020-11-30 19:51:57 +01006724 work = napi_poll(napi, budget);
6725 trace_napi_poll(napi, work, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006726 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006727count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006728 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006729 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006730 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006731 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006732
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006733 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006734 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006735
Eric Dumazet217f6972016-11-15 10:15:11 -08006736 if (unlikely(need_resched())) {
6737 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006738 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006739 preempt_enable();
6740 rcu_read_unlock();
6741 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006742 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006743 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006744 goto restart;
6745 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006746 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006747 }
6748 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006749 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006750 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006751out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006752 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006753}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006754EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006755
6756#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006757
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006758static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006759{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006760 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006761 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006762
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006763 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006764
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006765 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006766 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006767 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6768 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006769 } while (napi_by_id(napi_gen_id));
6770 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006771
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006772 hlist_add_head_rcu(&napi->napi_hash_node,
6773 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006774
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006775 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006776}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006777
6778/* Warning : caller is responsible to make sure rcu grace period
6779 * is respected before freeing memory containing @napi
6780 */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006781static void napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006782{
6783 spin_lock(&napi_hash_lock);
6784
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006785 hlist_del_init_rcu(&napi->napi_hash_node);
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006786
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006787 spin_unlock(&napi_hash_lock);
6788}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006789
Eric Dumazet3b47d302014-11-06 21:09:44 -08006790static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6791{
6792 struct napi_struct *napi;
6793
6794 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006795
6796 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6797 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6798 */
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006799 if (!napi_disable_pending(napi) &&
Björn Töpel7fd32532020-11-30 19:51:56 +01006800 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6801 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006802 __napi_schedule_irqoff(napi);
Björn Töpel7fd32532020-11-30 19:51:56 +01006803 }
Eric Dumazet3b47d302014-11-06 21:09:44 -08006804
6805 return HRTIMER_NORESTART;
6806}
6807
David S. Miller7c4ec742018-07-20 23:37:55 -07006808static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006809{
David Miller07d78362018-06-24 14:14:02 +09006810 int i;
6811
Li RongQing6312fe72018-07-05 14:34:32 +08006812 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6813 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6814 napi->gro_hash[i].count = 0;
6815 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006816 napi->gro_bitmask = 0;
6817}
6818
Wei Wang5fdd2f02021-02-08 11:34:10 -08006819int dev_set_threaded(struct net_device *dev, bool threaded)
6820{
6821 struct napi_struct *napi;
6822 int err = 0;
6823
6824 if (dev->threaded == threaded)
6825 return 0;
6826
6827 if (threaded) {
6828 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6829 if (!napi->thread) {
6830 err = napi_kthread_create(napi);
6831 if (err) {
6832 threaded = false;
6833 break;
6834 }
6835 }
6836 }
6837 }
6838
6839 dev->threaded = threaded;
6840
6841 /* Make sure kthread is created before THREADED bit
6842 * is set.
6843 */
6844 smp_mb__before_atomic();
6845
6846 /* Setting/unsetting threaded mode on a napi might not immediately
6847 * take effect, if the current napi instance is actively being
6848 * polled. In this case, the switch between threaded mode and
6849 * softirq mode will happen in the next round of napi_schedule().
6850 * This should not cause hiccups/stalls to the live traffic.
6851 */
6852 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6853 if (threaded)
6854 set_bit(NAPI_STATE_THREADED, &napi->state);
6855 else
6856 clear_bit(NAPI_STATE_THREADED, &napi->state);
6857 }
6858
6859 return err;
6860}
Lorenzo Bianconi8f648602021-03-14 15:49:19 +01006861EXPORT_SYMBOL(dev_set_threaded);
Wei Wang5fdd2f02021-02-08 11:34:10 -08006862
David S. Miller7c4ec742018-07-20 23:37:55 -07006863void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6864 int (*poll)(struct napi_struct *, int), int weight)
6865{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006866 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6867 return;
6868
David S. Miller7c4ec742018-07-20 23:37:55 -07006869 INIT_LIST_HEAD(&napi->poll_list);
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006870 INIT_HLIST_NODE(&napi->napi_hash_node);
David S. Miller7c4ec742018-07-20 23:37:55 -07006871 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6872 napi->timer.function = napi_watchdog;
6873 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006874 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006875 INIT_LIST_HEAD(&napi->rx_list);
6876 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006877 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006878 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006879 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6880 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006881 napi->weight = weight;
Herbert Xud565b0a2008-12-15 23:38:52 -08006882 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006883#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006884 napi->poll_owner = -1;
6885#endif
6886 set_bit(NAPI_STATE_SCHED, &napi->state);
Jakub Kicinski96e97bc2020-08-26 12:40:06 -07006887 set_bit(NAPI_STATE_NPSVC, &napi->state);
6888 list_add_rcu(&napi->dev_list, &dev->napi_list);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006889 napi_hash_add(napi);
Wei Wang29863d42021-02-08 11:34:09 -08006890 /* Create kthread for this napi if dev->threaded is set.
6891 * Clear dev->threaded if kthread creation failed so that
6892 * threaded mode will not be enabled in napi_enable().
6893 */
6894 if (dev->threaded && napi_kthread_create(napi))
6895 dev->threaded = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006896}
6897EXPORT_SYMBOL(netif_napi_add);
6898
Eric Dumazet3b47d302014-11-06 21:09:44 -08006899void napi_disable(struct napi_struct *n)
6900{
6901 might_sleep();
6902 set_bit(NAPI_STATE_DISABLE, &n->state);
6903
6904 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6905 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006906 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6907 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006908
6909 hrtimer_cancel(&n->timer);
6910
Björn Töpel7fd32532020-11-30 19:51:56 +01006911 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006912 clear_bit(NAPI_STATE_DISABLE, &n->state);
Wei Wang29863d42021-02-08 11:34:09 -08006913 clear_bit(NAPI_STATE_THREADED, &n->state);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006914}
6915EXPORT_SYMBOL(napi_disable);
6916
Wei Wang29863d42021-02-08 11:34:09 -08006917/**
6918 * napi_enable - enable NAPI scheduling
6919 * @n: NAPI context
6920 *
6921 * Resume NAPI from being scheduled on this context.
6922 * Must be paired with napi_disable.
6923 */
6924void napi_enable(struct napi_struct *n)
6925{
6926 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
6927 smp_mb__before_atomic();
6928 clear_bit(NAPI_STATE_SCHED, &n->state);
6929 clear_bit(NAPI_STATE_NPSVC, &n->state);
6930 if (n->dev->threaded && n->thread)
6931 set_bit(NAPI_STATE_THREADED, &n->state);
6932}
6933EXPORT_SYMBOL(napi_enable);
6934
David Miller07d78362018-06-24 14:14:02 +09006935static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006936{
David Miller07d78362018-06-24 14:14:02 +09006937 int i;
David Millerd4546c22018-06-24 14:13:49 +09006938
David Miller07d78362018-06-24 14:14:02 +09006939 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6940 struct sk_buff *skb, *n;
6941
Li RongQing6312fe72018-07-05 14:34:32 +08006942 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006943 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006944 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006945 }
David Millerd4546c22018-06-24 14:13:49 +09006946}
6947
Eric Dumazet93d05d42015-11-18 06:31:03 -08006948/* Must be called in process context */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006949void __netif_napi_del(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006950{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006951 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6952 return;
6953
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006954 napi_hash_del(napi);
Jakub Kicinski5251ef82020-09-09 10:37:53 -07006955 list_del_rcu(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006956 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006957
David Miller07d78362018-06-24 14:14:02 +09006958 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006959 napi->gro_bitmask = 0;
Wei Wang29863d42021-02-08 11:34:09 -08006960
6961 if (napi->thread) {
6962 kthread_stop(napi->thread);
6963 napi->thread = NULL;
6964 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006965}
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006966EXPORT_SYMBOL(__netif_napi_del);
Herbert Xud565b0a2008-12-15 23:38:52 -08006967
Felix Fietkau898f8012021-02-08 11:34:08 -08006968static int __napi_poll(struct napi_struct *n, bool *repoll)
Herbert Xu726ce702014-12-21 07:16:21 +11006969{
Herbert Xu726ce702014-12-21 07:16:21 +11006970 int work, weight;
6971
Herbert Xu726ce702014-12-21 07:16:21 +11006972 weight = n->weight;
6973
6974 /* This NAPI_STATE_SCHED test is for avoiding a race
6975 * with netpoll's poll_napi(). Only the entity which
6976 * obtains the lock and sees NAPI_STATE_SCHED set will
6977 * actually make the ->poll() call. Therefore we avoid
6978 * accidentally calling ->poll() when NAPI is not scheduled.
6979 */
6980 work = 0;
6981 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6982 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006983 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006984 }
6985
Eric Dumazet427d5832020-06-17 09:40:51 -07006986 if (unlikely(work > weight))
6987 pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6988 n->poll, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006989
6990 if (likely(work < weight))
Felix Fietkau898f8012021-02-08 11:34:08 -08006991 return work;
Herbert Xu726ce702014-12-21 07:16:21 +11006992
6993 /* Drivers must not modify the NAPI state if they
6994 * consume the entire weight. In such cases this code
6995 * still "owns" the NAPI instance and therefore can
6996 * move the instance around on the list at-will.
6997 */
6998 if (unlikely(napi_disable_pending(n))) {
6999 napi_complete(n);
Felix Fietkau898f8012021-02-08 11:34:08 -08007000 return work;
Herbert Xu726ce702014-12-21 07:16:21 +11007001 }
7002
Björn Töpel7fd32532020-11-30 19:51:56 +01007003 /* The NAPI context has more processing work, but busy-polling
7004 * is preferred. Exit early.
7005 */
7006 if (napi_prefer_busy_poll(n)) {
7007 if (napi_complete_done(n, work)) {
7008 /* If timeout is not set, we need to make sure
7009 * that the NAPI is re-scheduled.
7010 */
7011 napi_schedule(n);
7012 }
Felix Fietkau898f8012021-02-08 11:34:08 -08007013 return work;
Björn Töpel7fd32532020-11-30 19:51:56 +01007014 }
7015
Li RongQingd9f37d02018-07-13 14:41:36 +08007016 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11007017 /* flush too old packets
7018 * If HZ < 1000, flush all packets.
7019 */
7020 napi_gro_flush(n, HZ >= 1000);
7021 }
7022
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00007023 gro_normal_list(n);
7024
Herbert Xu001ce542014-12-21 07:16:22 +11007025 /* Some drivers may have called napi_schedule
7026 * prior to exhausting their budget.
7027 */
7028 if (unlikely(!list_empty(&n->poll_list))) {
7029 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
7030 n->dev ? n->dev->name : "backlog");
Felix Fietkau898f8012021-02-08 11:34:08 -08007031 return work;
Herbert Xu001ce542014-12-21 07:16:22 +11007032 }
7033
Felix Fietkau898f8012021-02-08 11:34:08 -08007034 *repoll = true;
Herbert Xu726ce702014-12-21 07:16:21 +11007035
Felix Fietkau898f8012021-02-08 11:34:08 -08007036 return work;
7037}
7038
7039static int napi_poll(struct napi_struct *n, struct list_head *repoll)
7040{
7041 bool do_repoll = false;
7042 void *have;
7043 int work;
7044
7045 list_del_init(&n->poll_list);
7046
7047 have = netpoll_poll_lock(n);
7048
7049 work = __napi_poll(n, &do_repoll);
7050
7051 if (do_repoll)
7052 list_add_tail(&n->poll_list, repoll);
7053
Herbert Xu726ce702014-12-21 07:16:21 +11007054 netpoll_poll_unlock(have);
7055
7056 return work;
7057}
7058
Wei Wang29863d42021-02-08 11:34:09 -08007059static int napi_thread_wait(struct napi_struct *napi)
7060{
Wei Wangcb038352021-03-16 15:36:47 -07007061 bool woken = false;
7062
Wei Wang29863d42021-02-08 11:34:09 -08007063 set_current_state(TASK_INTERRUPTIBLE);
7064
Paolo Abeni27f0ad72021-04-09 17:24:17 +02007065 while (!kthread_should_stop()) {
Wei Wangcb038352021-03-16 15:36:47 -07007066 /* Testing SCHED_THREADED bit here to make sure the current
7067 * kthread owns this napi and could poll on this napi.
7068 * Testing SCHED bit is not enough because SCHED bit might be
7069 * set by some other busy poll thread or by napi_disable().
7070 */
7071 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
Wei Wang29863d42021-02-08 11:34:09 -08007072 WARN_ON(!list_empty(&napi->poll_list));
7073 __set_current_state(TASK_RUNNING);
7074 return 0;
7075 }
7076
7077 schedule();
Wei Wangcb038352021-03-16 15:36:47 -07007078 /* woken being true indicates this thread owns this napi. */
7079 woken = true;
Wei Wang29863d42021-02-08 11:34:09 -08007080 set_current_state(TASK_INTERRUPTIBLE);
7081 }
7082 __set_current_state(TASK_RUNNING);
Paolo Abeni27f0ad72021-04-09 17:24:17 +02007083
Wei Wang29863d42021-02-08 11:34:09 -08007084 return -1;
7085}
7086
7087static int napi_threaded_poll(void *data)
7088{
7089 struct napi_struct *napi = data;
7090 void *have;
7091
7092 while (!napi_thread_wait(napi)) {
7093 for (;;) {
7094 bool repoll = false;
7095
7096 local_bh_disable();
7097
7098 have = netpoll_poll_lock(napi);
7099 __napi_poll(napi, &repoll);
7100 netpoll_poll_unlock(have);
7101
Wei Wang29863d42021-02-08 11:34:09 -08007102 local_bh_enable();
7103
7104 if (!repoll)
7105 break;
7106
7107 cond_resched();
7108 }
7109 }
7110 return 0;
7111}
7112
Emese Revfy0766f782016-06-20 20:42:34 +02007113static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114{
Christoph Lameter903ceff2014-08-17 12:30:35 -05007115 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04007116 unsigned long time_limit = jiffies +
7117 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07007118 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08007119 LIST_HEAD(list);
7120 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07007121
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08007123 list_splice_init(&sd->poll_list, &list);
7124 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Herbert Xuceb8d5b2014-12-21 07:16:25 +11007126 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007127 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
Herbert Xuceb8d5b2014-12-21 07:16:25 +11007129 if (list_empty(&list)) {
7130 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Alexander Lobakinfec6e492021-02-13 14:12:02 +00007131 return;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11007132 break;
7133 }
7134
Herbert Xu6bd373e2014-12-21 07:16:24 +11007135 n = list_first_entry(&list, struct napi_struct, poll_list);
7136 budget -= napi_poll(n, &repoll);
7137
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08007138 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08007139 * Allow this to run for 2 jiffies since which will allow
7140 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007141 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11007142 if (unlikely(budget <= 0 ||
7143 time_after_eq(jiffies, time_limit))) {
7144 sd->time_squeeze++;
7145 break;
7146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08007148
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08007149 local_irq_disable();
7150
7151 list_splice_tail_init(&sd->poll_list, &list);
7152 list_splice_tail(&repoll, &list);
7153 list_splice(&list, &sd->poll_list);
7154 if (!list_empty(&sd->poll_list))
7155 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
7156
Eric Dumazete326bed2010-04-22 00:22:45 -07007157 net_rps_action_and_irq_enable(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158}
7159
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007160struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007161 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007162
7163 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007164 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007165
Taehee Yoo32b6d342019-10-21 18:47:56 +00007166 /* lookup ignore flag */
7167 bool ignore;
7168
Veaceslav Falico5d261912013-08-28 23:25:05 +02007169 /* counter for the number of times this device was added to us */
7170 u16 ref_nr;
7171
Veaceslav Falico402dae92013-09-25 09:20:09 +02007172 /* private field for the users */
7173 void *private;
7174
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007175 struct list_head list;
7176 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007177};
7178
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007179static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007180 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007181{
Veaceslav Falico5d261912013-08-28 23:25:05 +02007182 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007183
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007184 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02007185 if (adj->dev == adj_dev)
7186 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007187 }
7188 return NULL;
7189}
7190
Taehee Yooeff74232020-09-25 18:13:12 +00007191static int ____netdev_has_upper_dev(struct net_device *upper_dev,
7192 struct netdev_nested_priv *priv)
David Ahernf1170fd2016-10-17 19:15:51 -07007193{
Taehee Yooeff74232020-09-25 18:13:12 +00007194 struct net_device *dev = (struct net_device *)priv->data;
David Ahernf1170fd2016-10-17 19:15:51 -07007195
7196 return upper_dev == dev;
7197}
7198
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007199/**
7200 * netdev_has_upper_dev - Check if device is linked to an upper device
7201 * @dev: device
7202 * @upper_dev: upper device to check
7203 *
7204 * Find out if a device is linked to specified upper device and return true
7205 * in case it is. Note that this checks only immediate upper device,
7206 * not through a complete stack of devices. The caller must hold the RTNL lock.
7207 */
7208bool netdev_has_upper_dev(struct net_device *dev,
7209 struct net_device *upper_dev)
7210{
Taehee Yooeff74232020-09-25 18:13:12 +00007211 struct netdev_nested_priv priv = {
7212 .data = (void *)upper_dev,
7213 };
7214
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007215 ASSERT_RTNL();
7216
Taehee Yoo32b6d342019-10-21 18:47:56 +00007217 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007218 &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007219}
7220EXPORT_SYMBOL(netdev_has_upper_dev);
7221
7222/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01007223 * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
David Ahern1a3f0602016-10-17 19:15:44 -07007224 * @dev: device
7225 * @upper_dev: upper device to check
7226 *
7227 * Find out if a device is linked to specified upper device and return true
7228 * in case it is. Note that this checks the entire upper device chain.
7229 * The caller must hold rcu lock.
7230 */
7231
David Ahern1a3f0602016-10-17 19:15:44 -07007232bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
7233 struct net_device *upper_dev)
7234{
Taehee Yooeff74232020-09-25 18:13:12 +00007235 struct netdev_nested_priv priv = {
7236 .data = (void *)upper_dev,
7237 };
7238
Taehee Yoo32b6d342019-10-21 18:47:56 +00007239 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007240 &priv);
David Ahern1a3f0602016-10-17 19:15:44 -07007241}
7242EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
7243
7244/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007245 * netdev_has_any_upper_dev - Check if device is linked to some device
7246 * @dev: device
7247 *
7248 * Find out if a device is linked to an upper device and return true in case
7249 * it is. The caller must hold the RTNL lock.
7250 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007251bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007252{
7253 ASSERT_RTNL();
7254
David Ahernf1170fd2016-10-17 19:15:51 -07007255 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007256}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007257EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007258
7259/**
7260 * netdev_master_upper_dev_get - Get master upper device
7261 * @dev: device
7262 *
7263 * Find a master upper device and return pointer to it or NULL in case
7264 * it's not there. The caller must hold the RTNL lock.
7265 */
7266struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
7267{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007268 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007269
7270 ASSERT_RTNL();
7271
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007272 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007273 return NULL;
7274
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007275 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007276 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007277 if (likely(upper->master))
7278 return upper->dev;
7279 return NULL;
7280}
7281EXPORT_SYMBOL(netdev_master_upper_dev_get);
7282
Taehee Yoo32b6d342019-10-21 18:47:56 +00007283static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
7284{
7285 struct netdev_adjacent *upper;
7286
7287 ASSERT_RTNL();
7288
7289 if (list_empty(&dev->adj_list.upper))
7290 return NULL;
7291
7292 upper = list_first_entry(&dev->adj_list.upper,
7293 struct netdev_adjacent, list);
7294 if (likely(upper->master) && !upper->ignore)
7295 return upper->dev;
7296 return NULL;
7297}
7298
David Ahern0f524a82016-10-17 19:15:52 -07007299/**
7300 * netdev_has_any_lower_dev - Check if device is linked to some device
7301 * @dev: device
7302 *
7303 * Find out if a device is linked to a lower device and return true in case
7304 * it is. The caller must hold the RTNL lock.
7305 */
7306static bool netdev_has_any_lower_dev(struct net_device *dev)
7307{
7308 ASSERT_RTNL();
7309
7310 return !list_empty(&dev->adj_list.lower);
7311}
7312
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02007313void *netdev_adjacent_get_private(struct list_head *adj_list)
7314{
7315 struct netdev_adjacent *adj;
7316
7317 adj = list_entry(adj_list, struct netdev_adjacent, list);
7318
7319 return adj->private;
7320}
7321EXPORT_SYMBOL(netdev_adjacent_get_private);
7322
Veaceslav Falico31088a12013-09-25 09:20:12 +02007323/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04007324 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7325 * @dev: device
7326 * @iter: list_head ** of the current position
7327 *
7328 * Gets the next device from the dev's upper list, starting from iter
7329 * position. The caller must hold RCU read lock.
7330 */
7331struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7332 struct list_head **iter)
7333{
7334 struct netdev_adjacent *upper;
7335
7336 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7337
7338 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7339
7340 if (&upper->list == &dev->adj_list.upper)
7341 return NULL;
7342
7343 *iter = &upper->list;
7344
7345 return upper->dev;
7346}
7347EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7348
Taehee Yoo32b6d342019-10-21 18:47:56 +00007349static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7350 struct list_head **iter,
7351 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00007352{
7353 struct netdev_adjacent *upper;
7354
7355 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7356
7357 if (&upper->list == &dev->adj_list.upper)
7358 return NULL;
7359
7360 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007361 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007362
7363 return upper->dev;
7364}
7365
David Ahern1a3f0602016-10-17 19:15:44 -07007366static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7367 struct list_head **iter)
7368{
7369 struct netdev_adjacent *upper;
7370
7371 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7372
7373 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7374
7375 if (&upper->list == &dev->adj_list.upper)
7376 return NULL;
7377
7378 *iter = &upper->list;
7379
7380 return upper->dev;
7381}
7382
Taehee Yoo32b6d342019-10-21 18:47:56 +00007383static int __netdev_walk_all_upper_dev(struct net_device *dev,
7384 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007385 struct netdev_nested_priv *priv),
7386 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007387{
7388 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7389 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7390 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007391 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007392
7393 now = dev;
7394 iter = &dev->adj_list.upper;
7395
7396 while (1) {
7397 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007398 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007399 if (ret)
7400 return ret;
7401 }
7402
7403 next = NULL;
7404 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007405 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007406 if (!udev)
7407 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007408 if (ignore)
7409 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007410
7411 next = udev;
7412 niter = &udev->adj_list.upper;
7413 dev_stack[cur] = now;
7414 iter_stack[cur++] = iter;
7415 break;
7416 }
7417
7418 if (!next) {
7419 if (!cur)
7420 return 0;
7421 next = dev_stack[--cur];
7422 niter = iter_stack[cur];
7423 }
7424
7425 now = next;
7426 iter = niter;
7427 }
7428
7429 return 0;
7430}
7431
David Ahern1a3f0602016-10-17 19:15:44 -07007432int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7433 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007434 struct netdev_nested_priv *priv),
7435 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007436{
Taehee Yoo5343da42019-10-21 18:47:50 +00007437 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7438 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7439 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007440
Taehee Yoo5343da42019-10-21 18:47:50 +00007441 now = dev;
7442 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07007443
Taehee Yoo5343da42019-10-21 18:47:50 +00007444 while (1) {
7445 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007446 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007447 if (ret)
7448 return ret;
7449 }
7450
7451 next = NULL;
7452 while (1) {
7453 udev = netdev_next_upper_dev_rcu(now, &iter);
7454 if (!udev)
7455 break;
7456
7457 next = udev;
7458 niter = &udev->adj_list.upper;
7459 dev_stack[cur] = now;
7460 iter_stack[cur++] = iter;
7461 break;
7462 }
7463
7464 if (!next) {
7465 if (!cur)
7466 return 0;
7467 next = dev_stack[--cur];
7468 niter = iter_stack[cur];
7469 }
7470
7471 now = next;
7472 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007473 }
7474
7475 return 0;
7476}
7477EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7478
Taehee Yoo32b6d342019-10-21 18:47:56 +00007479static bool __netdev_has_upper_dev(struct net_device *dev,
7480 struct net_device *upper_dev)
7481{
Taehee Yooeff74232020-09-25 18:13:12 +00007482 struct netdev_nested_priv priv = {
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007483 .flags = 0,
Taehee Yooeff74232020-09-25 18:13:12 +00007484 .data = (void *)upper_dev,
7485 };
7486
Taehee Yoo32b6d342019-10-21 18:47:56 +00007487 ASSERT_RTNL();
7488
7489 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007490 &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007491}
7492
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007493/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02007494 * netdev_lower_get_next_private - Get the next ->private from the
7495 * lower neighbour list
7496 * @dev: device
7497 * @iter: list_head ** of the current position
7498 *
7499 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7500 * list, starting from iter position. The caller must hold either hold the
7501 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007502 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02007503 */
7504void *netdev_lower_get_next_private(struct net_device *dev,
7505 struct list_head **iter)
7506{
7507 struct netdev_adjacent *lower;
7508
7509 lower = list_entry(*iter, struct netdev_adjacent, list);
7510
7511 if (&lower->list == &dev->adj_list.lower)
7512 return NULL;
7513
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007514 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007515
7516 return lower->private;
7517}
7518EXPORT_SYMBOL(netdev_lower_get_next_private);
7519
7520/**
7521 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7522 * lower neighbour list, RCU
7523 * variant
7524 * @dev: device
7525 * @iter: list_head ** of the current position
7526 *
7527 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7528 * list, starting from iter position. The caller must hold RCU read lock.
7529 */
7530void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7531 struct list_head **iter)
7532{
7533 struct netdev_adjacent *lower;
7534
Jussi Maki68918662021-07-31 05:57:36 +00007535 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
Veaceslav Falico31088a12013-09-25 09:20:12 +02007536
7537 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7538
7539 if (&lower->list == &dev->adj_list.lower)
7540 return NULL;
7541
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007542 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007543
7544 return lower->private;
7545}
7546EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7547
7548/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007549 * netdev_lower_get_next - Get the next device from the lower neighbour
7550 * list
7551 * @dev: device
7552 * @iter: list_head ** of the current position
7553 *
7554 * Gets the next netdev_adjacent from the dev's lower neighbour
7555 * list, starting from iter position. The caller must hold RTNL lock or
7556 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007557 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007558 */
7559void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7560{
7561 struct netdev_adjacent *lower;
7562
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007563 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007564
7565 if (&lower->list == &dev->adj_list.lower)
7566 return NULL;
7567
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007568 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007569
7570 return lower->dev;
7571}
7572EXPORT_SYMBOL(netdev_lower_get_next);
7573
David Ahern1a3f0602016-10-17 19:15:44 -07007574static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7575 struct list_head **iter)
7576{
7577 struct netdev_adjacent *lower;
7578
David Ahern46b5ab12016-10-26 13:21:33 -07007579 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007580
7581 if (&lower->list == &dev->adj_list.lower)
7582 return NULL;
7583
David Ahern46b5ab12016-10-26 13:21:33 -07007584 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007585
7586 return lower->dev;
7587}
7588
Taehee Yoo32b6d342019-10-21 18:47:56 +00007589static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7590 struct list_head **iter,
7591 bool *ignore)
7592{
7593 struct netdev_adjacent *lower;
7594
7595 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7596
7597 if (&lower->list == &dev->adj_list.lower)
7598 return NULL;
7599
7600 *iter = &lower->list;
7601 *ignore = lower->ignore;
7602
7603 return lower->dev;
7604}
7605
David Ahern1a3f0602016-10-17 19:15:44 -07007606int netdev_walk_all_lower_dev(struct net_device *dev,
7607 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007608 struct netdev_nested_priv *priv),
7609 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007610{
Taehee Yoo5343da42019-10-21 18:47:50 +00007611 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7612 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7613 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007614
Taehee Yoo5343da42019-10-21 18:47:50 +00007615 now = dev;
7616 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007617
Taehee Yoo5343da42019-10-21 18:47:50 +00007618 while (1) {
7619 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007620 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007621 if (ret)
7622 return ret;
7623 }
7624
7625 next = NULL;
7626 while (1) {
7627 ldev = netdev_next_lower_dev(now, &iter);
7628 if (!ldev)
7629 break;
7630
7631 next = ldev;
7632 niter = &ldev->adj_list.lower;
7633 dev_stack[cur] = now;
7634 iter_stack[cur++] = iter;
7635 break;
7636 }
7637
7638 if (!next) {
7639 if (!cur)
7640 return 0;
7641 next = dev_stack[--cur];
7642 niter = iter_stack[cur];
7643 }
7644
7645 now = next;
7646 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007647 }
7648
7649 return 0;
7650}
7651EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7652
Taehee Yoo32b6d342019-10-21 18:47:56 +00007653static int __netdev_walk_all_lower_dev(struct net_device *dev,
7654 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007655 struct netdev_nested_priv *priv),
7656 struct netdev_nested_priv *priv)
Taehee Yoo32b6d342019-10-21 18:47:56 +00007657{
7658 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7659 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7660 int ret, cur = 0;
7661 bool ignore;
7662
7663 now = dev;
7664 iter = &dev->adj_list.lower;
7665
7666 while (1) {
7667 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007668 ret = fn(now, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007669 if (ret)
7670 return ret;
7671 }
7672
7673 next = NULL;
7674 while (1) {
7675 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7676 if (!ldev)
7677 break;
7678 if (ignore)
7679 continue;
7680
7681 next = ldev;
7682 niter = &ldev->adj_list.lower;
7683 dev_stack[cur] = now;
7684 iter_stack[cur++] = iter;
7685 break;
7686 }
7687
7688 if (!next) {
7689 if (!cur)
7690 return 0;
7691 next = dev_stack[--cur];
7692 niter = iter_stack[cur];
7693 }
7694
7695 now = next;
7696 iter = niter;
7697 }
7698
7699 return 0;
7700}
7701
Taehee Yoo7151aff2020-02-15 10:50:21 +00007702struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7703 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007704{
7705 struct netdev_adjacent *lower;
7706
7707 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7708 if (&lower->list == &dev->adj_list.lower)
7709 return NULL;
7710
7711 *iter = &lower->list;
7712
7713 return lower->dev;
7714}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007715EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007716
Taehee Yoo5343da42019-10-21 18:47:50 +00007717static u8 __netdev_upper_depth(struct net_device *dev)
7718{
7719 struct net_device *udev;
7720 struct list_head *iter;
7721 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007722 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007723
7724 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007725 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007726 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007727 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7728 if (ignore)
7729 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007730 if (max_depth < udev->upper_level)
7731 max_depth = udev->upper_level;
7732 }
7733
7734 return max_depth;
7735}
7736
7737static u8 __netdev_lower_depth(struct net_device *dev)
7738{
7739 struct net_device *ldev;
7740 struct list_head *iter;
7741 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007742 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007743
7744 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007745 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007746 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007747 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7748 if (ignore)
7749 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007750 if (max_depth < ldev->lower_level)
7751 max_depth = ldev->lower_level;
7752 }
7753
7754 return max_depth;
7755}
7756
Taehee Yooeff74232020-09-25 18:13:12 +00007757static int __netdev_update_upper_level(struct net_device *dev,
7758 struct netdev_nested_priv *__unused)
Taehee Yoo5343da42019-10-21 18:47:50 +00007759{
7760 dev->upper_level = __netdev_upper_depth(dev) + 1;
7761 return 0;
7762}
7763
Taehee Yooeff74232020-09-25 18:13:12 +00007764static int __netdev_update_lower_level(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007765 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007766{
7767 dev->lower_level = __netdev_lower_depth(dev) + 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007768
7769#ifdef CONFIG_LOCKDEP
7770 if (!priv)
7771 return 0;
7772
7773 if (priv->flags & NESTED_SYNC_IMM)
7774 dev->nested_level = dev->lower_level - 1;
7775 if (priv->flags & NESTED_SYNC_TODO)
7776 net_unlink_todo(dev);
7777#endif
Taehee Yoo5343da42019-10-21 18:47:50 +00007778 return 0;
7779}
7780
David Ahern1a3f0602016-10-17 19:15:44 -07007781int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7782 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007783 struct netdev_nested_priv *priv),
7784 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007785{
Taehee Yoo5343da42019-10-21 18:47:50 +00007786 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7787 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7788 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007789
Taehee Yoo5343da42019-10-21 18:47:50 +00007790 now = dev;
7791 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007792
Taehee Yoo5343da42019-10-21 18:47:50 +00007793 while (1) {
7794 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007795 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007796 if (ret)
7797 return ret;
7798 }
7799
7800 next = NULL;
7801 while (1) {
7802 ldev = netdev_next_lower_dev_rcu(now, &iter);
7803 if (!ldev)
7804 break;
7805
7806 next = ldev;
7807 niter = &ldev->adj_list.lower;
7808 dev_stack[cur] = now;
7809 iter_stack[cur++] = iter;
7810 break;
7811 }
7812
7813 if (!next) {
7814 if (!cur)
7815 return 0;
7816 next = dev_stack[--cur];
7817 niter = iter_stack[cur];
7818 }
7819
7820 now = next;
7821 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007822 }
7823
7824 return 0;
7825}
7826EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7827
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007828/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007829 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7830 * lower neighbour list, RCU
7831 * variant
7832 * @dev: device
7833 *
7834 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7835 * list. The caller must hold RCU read lock.
7836 */
7837void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7838{
7839 struct netdev_adjacent *lower;
7840
7841 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7842 struct netdev_adjacent, list);
7843 if (lower)
7844 return lower->private;
7845 return NULL;
7846}
7847EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7848
7849/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007850 * netdev_master_upper_dev_get_rcu - Get master upper device
7851 * @dev: device
7852 *
7853 * Find a master upper device and return pointer to it or NULL in case
7854 * it's not there. The caller must hold the RCU read lock.
7855 */
7856struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7857{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007858 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007859
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007860 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007861 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007862 if (upper && likely(upper->master))
7863 return upper->dev;
7864 return NULL;
7865}
7866EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7867
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307868static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007869 struct net_device *adj_dev,
7870 struct list_head *dev_list)
7871{
7872 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007873
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007874 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7875 "upper_%s" : "lower_%s", adj_dev->name);
7876 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7877 linkname);
7878}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307879static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007880 char *name,
7881 struct list_head *dev_list)
7882{
7883 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007884
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007885 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7886 "upper_%s" : "lower_%s", name);
7887 sysfs_remove_link(&(dev->dev.kobj), linkname);
7888}
7889
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007890static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7891 struct net_device *adj_dev,
7892 struct list_head *dev_list)
7893{
7894 return (dev_list == &dev->adj_list.upper ||
7895 dev_list == &dev->adj_list.lower) &&
7896 net_eq(dev_net(dev), dev_net(adj_dev));
7897}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007898
Veaceslav Falico5d261912013-08-28 23:25:05 +02007899static int __netdev_adjacent_dev_insert(struct net_device *dev,
7900 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007901 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007902 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007903{
7904 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007905 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007906
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007907 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007908
7909 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007910 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007911 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7912 dev->name, adj_dev->name, adj->ref_nr);
7913
Veaceslav Falico5d261912013-08-28 23:25:05 +02007914 return 0;
7915 }
7916
7917 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7918 if (!adj)
7919 return -ENOMEM;
7920
7921 adj->dev = adj_dev;
7922 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007923 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007924 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007925 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007926 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007927
David Ahern67b62f92016-10-17 19:15:53 -07007928 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7929 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007930
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007931 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007932 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007933 if (ret)
7934 goto free_adj;
7935 }
7936
Veaceslav Falico7863c052013-09-25 09:20:06 +02007937 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007938 if (master) {
7939 ret = sysfs_create_link(&(dev->dev.kobj),
7940 &(adj_dev->dev.kobj), "master");
7941 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007942 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007943
Veaceslav Falico7863c052013-09-25 09:20:06 +02007944 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007945 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007946 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007947 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007948
7949 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007950
Veaceslav Falico5831d662013-09-25 09:20:32 +02007951remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007952 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007953 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007954free_adj:
7955 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007956 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007957
7958 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007959}
7960
stephen hemminger1d143d92013-12-29 14:01:29 -08007961static void __netdev_adjacent_dev_remove(struct net_device *dev,
7962 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007963 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007964 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007965{
7966 struct netdev_adjacent *adj;
7967
David Ahern67b62f92016-10-17 19:15:53 -07007968 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7969 dev->name, adj_dev->name, ref_nr);
7970
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007971 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007972
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007973 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007974 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007975 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007976 WARN_ON(1);
7977 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007978 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007979
Andrew Collins93409032016-10-03 13:43:02 -06007980 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007981 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7982 dev->name, adj_dev->name, ref_nr,
7983 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007984 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007985 return;
7986 }
7987
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007988 if (adj->master)
7989 sysfs_remove_link(&(dev->dev.kobj), "master");
7990
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007991 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007992 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007993
Veaceslav Falico5d261912013-08-28 23:25:05 +02007994 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007995 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007996 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007997 dev_put(adj_dev);
7998 kfree_rcu(adj, rcu);
7999}
8000
stephen hemminger1d143d92013-12-29 14:01:29 -08008001static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
8002 struct net_device *upper_dev,
8003 struct list_head *up_list,
8004 struct list_head *down_list,
8005 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02008006{
8007 int ret;
8008
David Ahern790510d2016-10-17 19:15:43 -07008009 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06008010 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008011 if (ret)
8012 return ret;
8013
David Ahern790510d2016-10-17 19:15:43 -07008014 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06008015 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008016 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07008017 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008018 return ret;
8019 }
8020
8021 return 0;
8022}
8023
stephen hemminger1d143d92013-12-29 14:01:29 -08008024static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
8025 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06008026 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08008027 struct list_head *up_list,
8028 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02008029{
Andrew Collins93409032016-10-03 13:43:02 -06008030 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
8031 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008032}
8033
stephen hemminger1d143d92013-12-29 14:01:29 -08008034static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
8035 struct net_device *upper_dev,
8036 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008037{
David Ahernf1170fd2016-10-17 19:15:51 -07008038 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
8039 &dev->adj_list.upper,
8040 &upper_dev->adj_list.lower,
8041 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008042}
8043
stephen hemminger1d143d92013-12-29 14:01:29 -08008044static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
8045 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008046{
Andrew Collins93409032016-10-03 13:43:02 -06008047 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008048 &dev->adj_list.upper,
8049 &upper_dev->adj_list.lower);
8050}
Veaceslav Falico5d261912013-08-28 23:25:05 +02008051
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008052static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02008053 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07008054 void *upper_priv, void *upper_info,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008055 struct netdev_nested_priv *priv,
David Ahern42ab19e2017-10-04 17:48:47 -07008056 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008057{
David Ahern51d0c0472017-10-04 17:48:45 -07008058 struct netdev_notifier_changeupper_info changeupper_info = {
8059 .info = {
8060 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07008061 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07008062 },
8063 .upper_dev = upper_dev,
8064 .master = master,
8065 .linking = true,
8066 .upper_info = upper_info,
8067 };
Mike Manning50d629e2018-02-26 23:49:30 +00008068 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02008069 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008070
8071 ASSERT_RTNL();
8072
8073 if (dev == upper_dev)
8074 return -EBUSY;
8075
8076 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00008077 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008078 return -EBUSY;
8079
Taehee Yoo5343da42019-10-21 18:47:50 +00008080 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
8081 return -EMLINK;
8082
Mike Manning50d629e2018-02-26 23:49:30 +00008083 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00008084 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00008085 return -EEXIST;
8086 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00008087 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00008088 if (master_dev)
8089 return master_dev == upper_dev ? -EEXIST : -EBUSY;
8090 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008091
David Ahern51d0c0472017-10-04 17:48:45 -07008092 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02008093 &changeupper_info.info);
8094 ret = notifier_to_errno(ret);
8095 if (ret)
8096 return ret;
8097
Jiri Pirko6dffb042015-12-03 12:12:10 +01008098 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02008099 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008100 if (ret)
8101 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008102
David Ahern51d0c0472017-10-04 17:48:45 -07008103 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01008104 &changeupper_info.info);
8105 ret = notifier_to_errno(ret);
8106 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07008107 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01008108
Taehee Yoo5343da42019-10-21 18:47:50 +00008109 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008110 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00008111
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008112 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008113 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008114 priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00008115
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008116 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02008117
David Ahernf1170fd2016-10-17 19:15:51 -07008118rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008119 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008120
8121 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008122}
8123
8124/**
8125 * netdev_upper_dev_link - Add a link to the upper device
8126 * @dev: device
8127 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08008128 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008129 *
8130 * Adds a link to device which is upper to this one. The caller must hold
8131 * the RTNL lock. On a failure a negative errno code is returned.
8132 * On success the reference counts are adjusted and the function
8133 * returns zero.
8134 */
8135int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07008136 struct net_device *upper_dev,
8137 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008138{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008139 struct netdev_nested_priv priv = {
8140 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8141 .data = NULL,
8142 };
8143
David Ahern42ab19e2017-10-04 17:48:47 -07008144 return __netdev_upper_dev_link(dev, upper_dev, false,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008145 NULL, NULL, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008146}
8147EXPORT_SYMBOL(netdev_upper_dev_link);
8148
8149/**
8150 * netdev_master_upper_dev_link - Add a master link to the upper device
8151 * @dev: device
8152 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01008153 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01008154 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08008155 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008156 *
8157 * Adds a link to device which is upper to this one. In this case, only
8158 * one master upper device can be linked, although other non-master devices
8159 * might be linked as well. The caller must hold the RTNL lock.
8160 * On a failure a negative errno code is returned. On success the reference
8161 * counts are adjusted and the function returns zero.
8162 */
8163int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01008164 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07008165 void *upper_priv, void *upper_info,
8166 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008167{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008168 struct netdev_nested_priv priv = {
8169 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8170 .data = NULL,
8171 };
8172
Jiri Pirko29bf24a2015-12-03 12:12:11 +01008173 return __netdev_upper_dev_link(dev, upper_dev, true,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008174 upper_priv, upper_info, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008175}
8176EXPORT_SYMBOL(netdev_master_upper_dev_link);
8177
Taehee Yoofe8300f2020-09-25 18:13:02 +00008178static void __netdev_upper_dev_unlink(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008179 struct net_device *upper_dev,
8180 struct netdev_nested_priv *priv)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008181{
David Ahern51d0c0472017-10-04 17:48:45 -07008182 struct netdev_notifier_changeupper_info changeupper_info = {
8183 .info = {
8184 .dev = dev,
8185 },
8186 .upper_dev = upper_dev,
8187 .linking = false,
8188 };
tchardingf4563a72017-02-09 17:56:07 +11008189
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008190 ASSERT_RTNL();
8191
Jiri Pirko0e4ead92015-08-27 09:31:18 +02008192 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02008193
David Ahern51d0c0472017-10-04 17:48:45 -07008194 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02008195 &changeupper_info.info);
8196
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008197 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02008198
David Ahern51d0c0472017-10-04 17:48:45 -07008199 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02008200 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00008201
8202 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008203 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00008204
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008205 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008206 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008207 priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008208}
Taehee Yoofe8300f2020-09-25 18:13:02 +00008209
8210/**
8211 * netdev_upper_dev_unlink - Removes a link to upper device
8212 * @dev: device
8213 * @upper_dev: new upper device
8214 *
8215 * Removes a link to device which is upper to this one. The caller must hold
8216 * the RTNL lock.
8217 */
8218void netdev_upper_dev_unlink(struct net_device *dev,
8219 struct net_device *upper_dev)
8220{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008221 struct netdev_nested_priv priv = {
8222 .flags = NESTED_SYNC_TODO,
8223 .data = NULL,
8224 };
8225
8226 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008227}
8228EXPORT_SYMBOL(netdev_upper_dev_unlink);
8229
Taehee Yoo32b6d342019-10-21 18:47:56 +00008230static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
8231 struct net_device *lower_dev,
8232 bool val)
8233{
8234 struct netdev_adjacent *adj;
8235
8236 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
8237 if (adj)
8238 adj->ignore = val;
8239
8240 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
8241 if (adj)
8242 adj->ignore = val;
8243}
8244
8245static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
8246 struct net_device *lower_dev)
8247{
8248 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
8249}
8250
8251static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
8252 struct net_device *lower_dev)
8253{
8254 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
8255}
8256
8257int netdev_adjacent_change_prepare(struct net_device *old_dev,
8258 struct net_device *new_dev,
8259 struct net_device *dev,
8260 struct netlink_ext_ack *extack)
8261{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008262 struct netdev_nested_priv priv = {
8263 .flags = 0,
8264 .data = NULL,
8265 };
Taehee Yoo32b6d342019-10-21 18:47:56 +00008266 int err;
8267
8268 if (!new_dev)
8269 return 0;
8270
8271 if (old_dev && new_dev != old_dev)
8272 netdev_adjacent_dev_disable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008273 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
8274 extack);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008275 if (err) {
8276 if (old_dev && new_dev != old_dev)
8277 netdev_adjacent_dev_enable(dev, old_dev);
8278 return err;
8279 }
8280
8281 return 0;
8282}
8283EXPORT_SYMBOL(netdev_adjacent_change_prepare);
8284
8285void netdev_adjacent_change_commit(struct net_device *old_dev,
8286 struct net_device *new_dev,
8287 struct net_device *dev)
8288{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008289 struct netdev_nested_priv priv = {
8290 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8291 .data = NULL,
8292 };
8293
Taehee Yoo32b6d342019-10-21 18:47:56 +00008294 if (!new_dev || !old_dev)
8295 return;
8296
8297 if (new_dev == old_dev)
8298 return;
8299
8300 netdev_adjacent_dev_enable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008301 __netdev_upper_dev_unlink(old_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008302}
8303EXPORT_SYMBOL(netdev_adjacent_change_commit);
8304
8305void netdev_adjacent_change_abort(struct net_device *old_dev,
8306 struct net_device *new_dev,
8307 struct net_device *dev)
8308{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008309 struct netdev_nested_priv priv = {
8310 .flags = 0,
8311 .data = NULL,
8312 };
8313
Taehee Yoo32b6d342019-10-21 18:47:56 +00008314 if (!new_dev)
8315 return;
8316
8317 if (old_dev && new_dev != old_dev)
8318 netdev_adjacent_dev_enable(dev, old_dev);
8319
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008320 __netdev_upper_dev_unlink(new_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008321}
8322EXPORT_SYMBOL(netdev_adjacent_change_abort);
8323
Moni Shoua61bd3852015-02-03 16:48:29 +02008324/**
8325 * netdev_bonding_info_change - Dispatch event about slave change
8326 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09008327 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02008328 *
8329 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8330 * The caller must hold the RTNL lock.
8331 */
8332void netdev_bonding_info_change(struct net_device *dev,
8333 struct netdev_bonding_info *bonding_info)
8334{
David Ahern51d0c0472017-10-04 17:48:45 -07008335 struct netdev_notifier_bonding_info info = {
8336 .info.dev = dev,
8337 };
Moni Shoua61bd3852015-02-03 16:48:29 +02008338
8339 memcpy(&info.bonding_info, bonding_info,
8340 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07008341 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02008342 &info.info);
8343}
8344EXPORT_SYMBOL(netdev_bonding_info_change);
8345
Maor Gottliebcff9f122020-04-30 22:21:31 +03008346/**
8347 * netdev_get_xmit_slave - Get the xmit slave of master device
Andrew Lunn88425002020-07-13 01:14:59 +02008348 * @dev: device
Maor Gottliebcff9f122020-04-30 22:21:31 +03008349 * @skb: The packet
8350 * @all_slaves: assume all the slaves are active
8351 *
8352 * The reference counters are not incremented so the caller must be
8353 * careful with locks. The caller must hold RCU lock.
8354 * %NULL is returned if no slave is found.
8355 */
8356
8357struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8358 struct sk_buff *skb,
8359 bool all_slaves)
8360{
8361 const struct net_device_ops *ops = dev->netdev_ops;
8362
8363 if (!ops->ndo_get_xmit_slave)
8364 return NULL;
8365 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8366}
8367EXPORT_SYMBOL(netdev_get_xmit_slave);
8368
Tariq Toukan719a4022021-01-17 16:59:42 +02008369static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
8370 struct sock *sk)
8371{
8372 const struct net_device_ops *ops = dev->netdev_ops;
8373
8374 if (!ops->ndo_sk_get_lower_dev)
8375 return NULL;
8376 return ops->ndo_sk_get_lower_dev(dev, sk);
8377}
8378
8379/**
8380 * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket
8381 * @dev: device
8382 * @sk: the socket
8383 *
8384 * %NULL is returned if no lower device is found.
8385 */
8386
8387struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
8388 struct sock *sk)
8389{
8390 struct net_device *lower;
8391
8392 lower = netdev_sk_get_lower_dev(dev, sk);
8393 while (lower) {
8394 dev = lower;
8395 lower = netdev_sk_get_lower_dev(dev, sk);
8396 }
8397
8398 return dev;
8399}
8400EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
8401
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008402static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008403{
8404 struct netdev_adjacent *iter;
8405
8406 struct net *net = dev_net(dev);
8407
8408 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008409 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008410 continue;
8411 netdev_adjacent_sysfs_add(iter->dev, dev,
8412 &iter->dev->adj_list.lower);
8413 netdev_adjacent_sysfs_add(dev, iter->dev,
8414 &dev->adj_list.upper);
8415 }
8416
8417 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008418 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008419 continue;
8420 netdev_adjacent_sysfs_add(iter->dev, dev,
8421 &iter->dev->adj_list.upper);
8422 netdev_adjacent_sysfs_add(dev, iter->dev,
8423 &dev->adj_list.lower);
8424 }
8425}
8426
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008427static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008428{
8429 struct netdev_adjacent *iter;
8430
8431 struct net *net = dev_net(dev);
8432
8433 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008434 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008435 continue;
8436 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8437 &iter->dev->adj_list.lower);
8438 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8439 &dev->adj_list.upper);
8440 }
8441
8442 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008443 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008444 continue;
8445 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8446 &iter->dev->adj_list.upper);
8447 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8448 &dev->adj_list.lower);
8449 }
8450}
8451
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008452void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02008453{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008454 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02008455
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008456 struct net *net = dev_net(dev);
8457
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008458 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008459 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008460 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008461 netdev_adjacent_sysfs_del(iter->dev, oldname,
8462 &iter->dev->adj_list.lower);
8463 netdev_adjacent_sysfs_add(iter->dev, dev,
8464 &iter->dev->adj_list.lower);
8465 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008466
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008467 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008468 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008469 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008470 netdev_adjacent_sysfs_del(iter->dev, oldname,
8471 &iter->dev->adj_list.upper);
8472 netdev_adjacent_sysfs_add(iter->dev, dev,
8473 &iter->dev->adj_list.upper);
8474 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008475}
Veaceslav Falico402dae92013-09-25 09:20:09 +02008476
8477void *netdev_lower_dev_get_private(struct net_device *dev,
8478 struct net_device *lower_dev)
8479{
8480 struct netdev_adjacent *lower;
8481
8482 if (!lower_dev)
8483 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02008484 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02008485 if (!lower)
8486 return NULL;
8487
8488 return lower->private;
8489}
8490EXPORT_SYMBOL(netdev_lower_dev_get_private);
8491
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04008492
Jiri Pirko04d48262015-12-03 12:12:15 +01008493/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01008494 * netdev_lower_state_changed - Dispatch event about lower device state change
Jiri Pirko04d48262015-12-03 12:12:15 +01008495 * @lower_dev: device
8496 * @lower_state_info: state to dispatch
8497 *
8498 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8499 * The caller must hold the RTNL lock.
8500 */
8501void netdev_lower_state_changed(struct net_device *lower_dev,
8502 void *lower_state_info)
8503{
David Ahern51d0c0472017-10-04 17:48:45 -07008504 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8505 .info.dev = lower_dev,
8506 };
Jiri Pirko04d48262015-12-03 12:12:15 +01008507
8508 ASSERT_RTNL();
8509 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07008510 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01008511 &changelowerstate_info.info);
8512}
8513EXPORT_SYMBOL(netdev_lower_state_changed);
8514
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008515static void dev_change_rx_flags(struct net_device *dev, int flags)
8516{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008517 const struct net_device_ops *ops = dev->netdev_ops;
8518
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05008519 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008520 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008521}
8522
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008523static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07008524{
Eric Dumazetb536db92011-11-30 21:42:26 +00008525 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06008526 kuid_t uid;
8527 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07008528
Patrick McHardy24023452007-07-14 18:51:31 -07008529 ASSERT_RTNL();
8530
Wang Chendad9b332008-06-18 01:48:28 -07008531 dev->flags |= IFF_PROMISC;
8532 dev->promiscuity += inc;
8533 if (dev->promiscuity == 0) {
8534 /*
8535 * Avoid overflow.
8536 * If inc causes overflow, untouch promisc and return error.
8537 */
8538 if (inc < 0)
8539 dev->flags &= ~IFF_PROMISC;
8540 else {
8541 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008542 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
8543 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008544 return -EOVERFLOW;
8545 }
8546 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008547 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008548 pr_info("device %s %s promiscuous mode\n",
8549 dev->name,
8550 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11008551 if (audit_enabled) {
8552 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04008553 audit_log(audit_context(), GFP_ATOMIC,
8554 AUDIT_ANOM_PROMISCUOUS,
8555 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8556 dev->name, (dev->flags & IFF_PROMISC),
8557 (old_flags & IFF_PROMISC),
8558 from_kuid(&init_user_ns, audit_get_loginuid(current)),
8559 from_kuid(&init_user_ns, uid),
8560 from_kgid(&init_user_ns, gid),
8561 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11008562 }
Patrick McHardy24023452007-07-14 18:51:31 -07008563
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008564 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07008565 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008566 if (notify)
8567 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07008568 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008569}
8570
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571/**
8572 * dev_set_promiscuity - update promiscuity count on a device
8573 * @dev: device
8574 * @inc: modifier
8575 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07008576 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 * remains above zero the interface remains promiscuous. Once it hits zero
8578 * the device reverts back to normal filtering operation. A negative inc
8579 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07008580 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581 */
Wang Chendad9b332008-06-18 01:48:28 -07008582int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583{
Eric Dumazetb536db92011-11-30 21:42:26 +00008584 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07008585 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008587 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07008588 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07008589 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07008590 if (dev->flags != old_flags)
8591 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07008592 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008594EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008596static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008598 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
Patrick McHardy24023452007-07-14 18:51:31 -07008600 ASSERT_RTNL();
8601
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07008603 dev->allmulti += inc;
8604 if (dev->allmulti == 0) {
8605 /*
8606 * Avoid overflow.
8607 * If inc causes overflow, untouch allmulti and return error.
8608 */
8609 if (inc < 0)
8610 dev->flags &= ~IFF_ALLMULTI;
8611 else {
8612 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008613 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8614 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008615 return -EOVERFLOW;
8616 }
8617 }
Patrick McHardy24023452007-07-14 18:51:31 -07008618 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008619 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008620 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008621 if (notify)
8622 __dev_notify_flags(dev, old_flags,
8623 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008624 }
Wang Chendad9b332008-06-18 01:48:28 -07008625 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008626}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008627
8628/**
8629 * dev_set_allmulti - update allmulti count on a device
8630 * @dev: device
8631 * @inc: modifier
8632 *
8633 * Add or remove reception of all multicast frames to a device. While the
8634 * count in the device remains above zero the interface remains listening
8635 * to all interfaces. Once it hits zero the device reverts back to normal
8636 * filtering operation. A negative @inc value is used to drop the counter
8637 * when releasing a resource needing all multicasts.
8638 * Return 0 if successful or a negative errno code on error.
8639 */
8640
8641int dev_set_allmulti(struct net_device *dev, int inc)
8642{
8643 return __dev_set_allmulti(dev, inc, true);
8644}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008645EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008646
8647/*
8648 * Upload unicast and multicast address lists to device and
8649 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008650 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008651 * are present.
8652 */
8653void __dev_set_rx_mode(struct net_device *dev)
8654{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008655 const struct net_device_ops *ops = dev->netdev_ops;
8656
Patrick McHardy4417da62007-06-27 01:28:10 -07008657 /* dev_open will call this function so the list will stay sane. */
8658 if (!(dev->flags&IFF_UP))
8659 return;
8660
8661 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008662 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008663
Jiri Pirko01789342011-08-16 06:29:00 +00008664 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008665 /* Unicast addresses changes may only happen under the rtnl,
8666 * therefore calling __dev_set_promiscuity here is safe.
8667 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008668 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008669 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008670 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008671 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008672 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008673 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008674 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008675 }
Jiri Pirko01789342011-08-16 06:29:00 +00008676
8677 if (ops->ndo_set_rx_mode)
8678 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008679}
8680
8681void dev_set_rx_mode(struct net_device *dev)
8682{
David S. Millerb9e40852008-07-15 00:15:08 -07008683 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008684 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008685 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686}
8687
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008688/**
8689 * dev_get_flags - get flags reported to userspace
8690 * @dev: device
8691 *
8692 * Get the combination of flag bits exported through APIs to userspace.
8693 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008694unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695{
Eric Dumazet95c96172012-04-15 05:58:06 +00008696 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697
8698 flags = (dev->flags & ~(IFF_PROMISC |
8699 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008700 IFF_RUNNING |
8701 IFF_LOWER_UP |
8702 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 (dev->gflags & (IFF_PROMISC |
8704 IFF_ALLMULTI));
8705
Stefan Rompfb00055a2006-03-20 17:09:11 -08008706 if (netif_running(dev)) {
8707 if (netif_oper_up(dev))
8708 flags |= IFF_RUNNING;
8709 if (netif_carrier_ok(dev))
8710 flags |= IFF_LOWER_UP;
8711 if (netif_dormant(dev))
8712 flags |= IFF_DORMANT;
8713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714
8715 return flags;
8716}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008717EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718
Petr Machata6d040322018-12-06 17:05:43 +00008719int __dev_change_flags(struct net_device *dev, unsigned int flags,
8720 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721{
Eric Dumazetb536db92011-11-30 21:42:26 +00008722 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008723 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724
Patrick McHardy24023452007-07-14 18:51:31 -07008725 ASSERT_RTNL();
8726
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 /*
8728 * Set the flags on our device.
8729 */
8730
8731 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8732 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8733 IFF_AUTOMEDIA)) |
8734 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8735 IFF_ALLMULTI));
8736
8737 /*
8738 * Load in the correct multicast list now the flags have changed.
8739 */
8740
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008741 if ((old_flags ^ flags) & IFF_MULTICAST)
8742 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008743
Patrick McHardy4417da62007-06-27 01:28:10 -07008744 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745
8746 /*
8747 * Have we downed the interface. We handle IFF_UP ourselves
8748 * according to user attempts to set it, rather than blindly
8749 * setting it.
8750 */
8751
8752 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008753 if ((old_flags ^ flags) & IFF_UP) {
8754 if (old_flags & IFF_UP)
8755 __dev_close(dev);
8756 else
Petr Machata40c900a2018-12-06 17:05:47 +00008757 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008761 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008762 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008763
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008765
8766 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8767 if (dev->flags != old_flags)
8768 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769 }
8770
8771 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008772 * is important. Some (broken) drivers set IFF_PROMISC, when
8773 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774 */
8775 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008776 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8777
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008779 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780 }
8781
Patrick McHardybd380812010-02-26 06:34:53 +00008782 return ret;
8783}
8784
Nicolas Dichtela528c212013-09-25 12:02:44 +02008785void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8786 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008787{
8788 unsigned int changes = dev->flags ^ old_flags;
8789
Nicolas Dichtela528c212013-09-25 12:02:44 +02008790 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008791 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008792
Patrick McHardybd380812010-02-26 06:34:53 +00008793 if (changes & IFF_UP) {
8794 if (dev->flags & IFF_UP)
8795 call_netdevice_notifiers(NETDEV_UP, dev);
8796 else
8797 call_netdevice_notifiers(NETDEV_DOWN, dev);
8798 }
8799
8800 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008801 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008802 struct netdev_notifier_change_info change_info = {
8803 .info = {
8804 .dev = dev,
8805 },
8806 .flags_changed = changes,
8807 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008808
David Ahern51d0c0472017-10-04 17:48:45 -07008809 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008810 }
Patrick McHardybd380812010-02-26 06:34:53 +00008811}
8812
8813/**
8814 * dev_change_flags - change device settings
8815 * @dev: device
8816 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008817 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008818 *
8819 * Change settings on device based state flags. The flags are
8820 * in the userspace exported format.
8821 */
Petr Machata567c5e12018-12-06 17:05:42 +00008822int dev_change_flags(struct net_device *dev, unsigned int flags,
8823 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008824{
Eric Dumazetb536db92011-11-30 21:42:26 +00008825 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008826 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008827
Petr Machata6d040322018-12-06 17:05:43 +00008828 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008829 if (ret < 0)
8830 return ret;
8831
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008832 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008833 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 return ret;
8835}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008836EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
WANG Congf51048c2017-07-06 15:01:57 -07008838int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008839{
8840 const struct net_device_ops *ops = dev->netdev_ops;
8841
8842 if (ops->ndo_change_mtu)
8843 return ops->ndo_change_mtu(dev, new_mtu);
8844
Eric Dumazet501a90c2019-12-05 20:43:46 -08008845 /* Pairs with all the lockless reads of dev->mtu in the stack */
8846 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008847 return 0;
8848}
WANG Congf51048c2017-07-06 15:01:57 -07008849EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008850
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008851int dev_validate_mtu(struct net_device *dev, int new_mtu,
8852 struct netlink_ext_ack *extack)
8853{
8854 /* MTU must be positive, and in range */
8855 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8856 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8857 return -EINVAL;
8858 }
8859
8860 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8861 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8862 return -EINVAL;
8863 }
8864 return 0;
8865}
8866
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008867/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008868 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008869 * @dev: device
8870 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008871 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008872 *
8873 * Change the maximum transfer size of the network device.
8874 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008875int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8876 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008878 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879
8880 if (new_mtu == dev->mtu)
8881 return 0;
8882
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008883 err = dev_validate_mtu(dev, new_mtu, extack);
8884 if (err)
8885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886
8887 if (!netif_device_present(dev))
8888 return -ENODEV;
8889
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008890 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8891 err = notifier_to_errno(err);
8892 if (err)
8893 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008894
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008895 orig_mtu = dev->mtu;
8896 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008898 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008899 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8900 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008901 err = notifier_to_errno(err);
8902 if (err) {
8903 /* setting mtu back and notifying everyone again,
8904 * so that they have a chance to revert changes.
8905 */
8906 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008907 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8908 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008909 }
8910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 return err;
8912}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008913
8914int dev_set_mtu(struct net_device *dev, int new_mtu)
8915{
8916 struct netlink_ext_ack extack;
8917 int err;
8918
Li RongQinga6bcfc82018-08-03 15:45:21 +08008919 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008920 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008921 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008922 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8923 return err;
8924}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008925EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008927/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008928 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8929 * @dev: device
8930 * @new_len: new tx queue length
8931 */
8932int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8933{
8934 unsigned int orig_len = dev->tx_queue_len;
8935 int res;
8936
8937 if (new_len != (unsigned int)new_len)
8938 return -ERANGE;
8939
8940 if (new_len != orig_len) {
8941 dev->tx_queue_len = new_len;
8942 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8943 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008944 if (res)
8945 goto err_rollback;
8946 res = dev_qdisc_change_tx_queue_len(dev);
8947 if (res)
8948 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008949 }
8950
8951 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008952
8953err_rollback:
8954 netdev_err(dev, "refused to change device tx_queue_len\n");
8955 dev->tx_queue_len = orig_len;
8956 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008957}
8958
8959/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008960 * dev_set_group - Change group this device belongs to
8961 * @dev: device
8962 * @new_group: group this device should belong to
8963 */
8964void dev_set_group(struct net_device *dev, int new_group)
8965{
8966 dev->group = new_group;
8967}
8968EXPORT_SYMBOL(dev_set_group);
8969
8970/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008971 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8972 * @dev: device
8973 * @addr: new address
8974 * @extack: netlink extended ack
8975 */
8976int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8977 struct netlink_ext_ack *extack)
8978{
8979 struct netdev_notifier_pre_changeaddr_info info = {
8980 .info.dev = dev,
8981 .info.extack = extack,
8982 .dev_addr = addr,
8983 };
8984 int rc;
8985
8986 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8987 return notifier_to_errno(rc);
8988}
8989EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8990
8991/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008992 * dev_set_mac_address - Change Media Access Control Address
8993 * @dev: device
8994 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008995 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008996 *
8997 * Change the hardware (MAC) address of the device
8998 */
Petr Machata3a37a962018-12-13 11:54:30 +00008999int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
9000 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009002 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003 int err;
9004
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009005 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 return -EOPNOTSUPP;
9007 if (sa->sa_family != dev->type)
9008 return -EINVAL;
9009 if (!netif_device_present(dev))
9010 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00009011 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
9012 if (err)
9013 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009014 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00009015 if (err)
9016 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00009017 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00009018 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04009019 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00009020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009022EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009023
Cong Wang3b23a322021-02-11 11:34:10 -08009024static DECLARE_RWSEM(dev_addr_sem);
9025
9026int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
9027 struct netlink_ext_ack *extack)
9028{
9029 int ret;
9030
9031 down_write(&dev_addr_sem);
9032 ret = dev_set_mac_address(dev, sa, extack);
9033 up_write(&dev_addr_sem);
9034 return ret;
9035}
9036EXPORT_SYMBOL(dev_set_mac_address_user);
9037
9038int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
9039{
9040 size_t size = sizeof(sa->sa_data);
9041 struct net_device *dev;
9042 int ret = 0;
9043
9044 down_read(&dev_addr_sem);
9045 rcu_read_lock();
9046
9047 dev = dev_get_by_name_rcu(net, dev_name);
9048 if (!dev) {
9049 ret = -ENODEV;
9050 goto unlock;
9051 }
9052 if (!dev->addr_len)
9053 memset(sa->sa_data, 0, size);
9054 else
9055 memcpy(sa->sa_data, dev->dev_addr,
9056 min_t(size_t, size, dev->addr_len));
9057 sa->sa_family = dev->type;
9058
9059unlock:
9060 rcu_read_unlock();
9061 up_read(&dev_addr_sem);
9062 return ret;
9063}
9064EXPORT_SYMBOL(dev_get_mac_address);
9065
Jiri Pirko4bf84c32012-12-27 23:49:37 +00009066/**
9067 * dev_change_carrier - Change device carrier
9068 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00009069 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00009070 *
9071 * Change device carrier
9072 */
9073int dev_change_carrier(struct net_device *dev, bool new_carrier)
9074{
9075 const struct net_device_ops *ops = dev->netdev_ops;
9076
9077 if (!ops->ndo_change_carrier)
9078 return -EOPNOTSUPP;
9079 if (!netif_device_present(dev))
9080 return -ENODEV;
9081 return ops->ndo_change_carrier(dev, new_carrier);
9082}
9083EXPORT_SYMBOL(dev_change_carrier);
9084
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02009086 * dev_get_phys_port_id - Get device physical port ID
9087 * @dev: device
9088 * @ppid: port ID
9089 *
9090 * Get device physical port ID
9091 */
9092int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009093 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02009094{
9095 const struct net_device_ops *ops = dev->netdev_ops;
9096
9097 if (!ops->ndo_get_phys_port_id)
9098 return -EOPNOTSUPP;
9099 return ops->ndo_get_phys_port_id(dev, ppid);
9100}
9101EXPORT_SYMBOL(dev_get_phys_port_id);
9102
9103/**
David Aherndb24a902015-03-17 20:23:15 -06009104 * dev_get_phys_port_name - Get device physical port name
9105 * @dev: device
9106 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00009107 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06009108 *
9109 * Get device physical port name
9110 */
9111int dev_get_phys_port_name(struct net_device *dev,
9112 char *name, size_t len)
9113{
9114 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01009115 int err;
David Aherndb24a902015-03-17 20:23:15 -06009116
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01009117 if (ops->ndo_get_phys_port_name) {
9118 err = ops->ndo_get_phys_port_name(dev, name, len);
9119 if (err != -EOPNOTSUPP)
9120 return err;
9121 }
9122 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06009123}
9124EXPORT_SYMBOL(dev_get_phys_port_name);
9125
9126/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08009127 * dev_get_port_parent_id - Get the device's port parent identifier
9128 * @dev: network device
9129 * @ppid: pointer to a storage for the port's parent identifier
9130 * @recurse: allow/disallow recursion to lower devices
9131 *
9132 * Get the devices's port parent identifier
9133 */
9134int dev_get_port_parent_id(struct net_device *dev,
9135 struct netdev_phys_item_id *ppid,
9136 bool recurse)
9137{
9138 const struct net_device_ops *ops = dev->netdev_ops;
9139 struct netdev_phys_item_id first = { };
9140 struct net_device *lower_dev;
9141 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009142 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08009143
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009144 if (ops->ndo_get_port_parent_id) {
9145 err = ops->ndo_get_port_parent_id(dev, ppid);
9146 if (err != -EOPNOTSUPP)
9147 return err;
9148 }
9149
9150 err = devlink_compat_switch_id_get(dev, ppid);
9151 if (!err || err != -EOPNOTSUPP)
9152 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08009153
9154 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009155 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08009156
9157 netdev_for_each_lower_dev(dev, lower_dev, iter) {
9158 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
9159 if (err)
9160 break;
9161 if (!first.id_len)
9162 first = *ppid;
9163 else if (memcmp(&first, ppid, sizeof(*ppid)))
Ido Schimmele1b9efe2020-09-10 14:01:26 +03009164 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08009165 }
9166
9167 return err;
9168}
9169EXPORT_SYMBOL(dev_get_port_parent_id);
9170
9171/**
9172 * netdev_port_same_parent_id - Indicate if two network devices have
9173 * the same port parent identifier
9174 * @a: first network device
9175 * @b: second network device
9176 */
9177bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
9178{
9179 struct netdev_phys_item_id a_id = { };
9180 struct netdev_phys_item_id b_id = { };
9181
9182 if (dev_get_port_parent_id(a, &a_id, true) ||
9183 dev_get_port_parent_id(b, &b_id, true))
9184 return false;
9185
9186 return netdev_phys_item_id_same(&a_id, &b_id);
9187}
9188EXPORT_SYMBOL(netdev_port_same_parent_id);
9189
9190/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009191 * dev_change_proto_down - update protocol port state information
9192 * @dev: device
9193 * @proto_down: new value
9194 *
9195 * This info can be used by switch drivers to set the phys state of the
9196 * port.
9197 */
9198int dev_change_proto_down(struct net_device *dev, bool proto_down)
9199{
9200 const struct net_device_ops *ops = dev->netdev_ops;
9201
9202 if (!ops->ndo_change_proto_down)
9203 return -EOPNOTSUPP;
9204 if (!netif_device_present(dev))
9205 return -ENODEV;
9206 return ops->ndo_change_proto_down(dev, proto_down);
9207}
9208EXPORT_SYMBOL(dev_change_proto_down);
9209
Andy Roulinb5899672019-02-22 18:06:36 +00009210/**
9211 * dev_change_proto_down_generic - generic implementation for
9212 * ndo_change_proto_down that sets carrier according to
9213 * proto_down.
9214 *
9215 * @dev: device
9216 * @proto_down: new value
9217 */
9218int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
9219{
9220 if (proto_down)
9221 netif_carrier_off(dev);
9222 else
9223 netif_carrier_on(dev);
9224 dev->proto_down = proto_down;
9225 return 0;
9226}
9227EXPORT_SYMBOL(dev_change_proto_down_generic);
9228
Roopa Prabhu829eb202020-07-31 17:34:01 -07009229/**
9230 * dev_change_proto_down_reason - proto down reason
9231 *
9232 * @dev: device
9233 * @mask: proto down mask
9234 * @value: proto down value
9235 */
9236void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
9237 u32 value)
9238{
9239 int b;
9240
9241 if (!mask) {
9242 dev->proto_down_reason = value;
9243 } else {
9244 for_each_set_bit(b, &mask, 32) {
9245 if (value & (1 << b))
9246 dev->proto_down_reason |= BIT(b);
9247 else
9248 dev->proto_down_reason &= ~BIT(b);
9249 }
9250 }
9251}
9252EXPORT_SYMBOL(dev_change_proto_down_reason);
9253
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009254struct bpf_xdp_link {
9255 struct bpf_link link;
9256 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
9257 int flags;
9258};
9259
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009260static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009261{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009262 if (flags & XDP_FLAGS_HW_MODE)
9263 return XDP_MODE_HW;
9264 if (flags & XDP_FLAGS_DRV_MODE)
9265 return XDP_MODE_DRV;
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009266 if (flags & XDP_FLAGS_SKB_MODE)
9267 return XDP_MODE_SKB;
9268 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009269}
9270
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009271static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009272{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009273 switch (mode) {
9274 case XDP_MODE_SKB:
9275 return generic_xdp_install;
9276 case XDP_MODE_DRV:
9277 case XDP_MODE_HW:
9278 return dev->netdev_ops->ndo_bpf;
9279 default:
9280 return NULL;
Tom Rix5d867242020-11-01 07:36:47 -08009281 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009282}
9283
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009284static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
9285 enum bpf_xdp_mode mode)
9286{
9287 return dev->xdp_state[mode].link;
9288}
9289
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009290static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
9291 enum bpf_xdp_mode mode)
9292{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009293 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
9294
9295 if (link)
9296 return link->link.prog;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009297 return dev->xdp_state[mode].prog;
9298}
9299
Jussi Maki879af962021-07-31 05:57:33 +00009300u8 dev_xdp_prog_count(struct net_device *dev)
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009301{
9302 u8 count = 0;
9303 int i;
9304
9305 for (i = 0; i < __MAX_XDP_MODE; i++)
9306 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
9307 count++;
9308 return count;
9309}
Jussi Maki879af962021-07-31 05:57:33 +00009310EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009311
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009312u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
9313{
9314 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
9315
9316 return prog ? prog->aux->id : 0;
9317}
9318
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009319static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
9320 struct bpf_xdp_link *link)
9321{
9322 dev->xdp_state[mode].link = link;
9323 dev->xdp_state[mode].prog = NULL;
9324}
9325
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009326static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9327 struct bpf_prog *prog)
9328{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009329 dev->xdp_state[mode].link = NULL;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009330 dev->xdp_state[mode].prog = prog;
9331}
9332
9333static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9334 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9335 u32 flags, struct bpf_prog *prog)
9336{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009337 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01009338 int err;
9339
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009340 memset(&xdp, 0, sizeof(xdp));
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009341 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009342 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07009343 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009344 xdp.prog = prog;
9345
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009346 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
9347 * "moved" into driver), so they don't increment it on their own, but
9348 * they do decrement refcnt when program is detached or replaced.
9349 * Given net_device also owns link/prog, we need to bump refcnt here
9350 * to prevent drivers from underflowing it.
9351 */
9352 if (prog)
9353 bpf_prog_inc(prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009354 err = bpf_op(dev, &xdp);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009355 if (err) {
9356 if (prog)
9357 bpf_prog_put(prog);
9358 return err;
9359 }
Björn Töpel7e6897f2019-12-13 18:51:09 +01009360
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009361 if (mode != XDP_MODE_HW)
9362 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009363
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009364 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009365}
9366
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009367static void dev_xdp_uninstall(struct net_device *dev)
9368{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009369 struct bpf_xdp_link *link;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009370 struct bpf_prog *prog;
9371 enum bpf_xdp_mode mode;
9372 bpf_op_t bpf_op;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009373
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009374 ASSERT_RTNL();
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009375
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009376 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9377 prog = dev_xdp_prog(dev, mode);
9378 if (!prog)
9379 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009380
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009381 bpf_op = dev_xdp_bpf_op(dev, mode);
9382 if (!bpf_op)
9383 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009384
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009385 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9386
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009387 /* auto-detach link from net device */
9388 link = dev_xdp_link(dev, mode);
9389 if (link)
9390 link->dev = NULL;
9391 else
9392 bpf_prog_put(prog);
9393
9394 dev_xdp_set_link(dev, mode, NULL);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009395 }
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009396}
9397
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009398static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009399 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9400 struct bpf_prog *old_prog, u32 flags)
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009401{
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009402 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009403 struct bpf_prog *cur_prog;
Jussi Maki879af962021-07-31 05:57:33 +00009404 struct net_device *upper;
9405 struct list_head *iter;
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009406 enum bpf_xdp_mode mode;
9407 bpf_op_t bpf_op;
9408 int err;
9409
9410 ASSERT_RTNL();
9411
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009412 /* either link or prog attachment, never both */
9413 if (link && (new_prog || old_prog))
9414 return -EINVAL;
9415 /* link supports only XDP mode flags */
9416 if (link && (flags & ~XDP_FLAGS_MODES)) {
9417 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9418 return -EINVAL;
9419 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009420 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9421 if (num_modes > 1) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009422 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9423 return -EINVAL;
9424 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009425 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9426 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9427 NL_SET_ERR_MSG(extack,
9428 "More than one program loaded, unset mode is ambiguous");
9429 return -EINVAL;
9430 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009431 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9432 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9433 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9434 return -EINVAL;
9435 }
9436
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009437 mode = dev_xdp_mode(dev, flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009438 /* can't replace attached link */
9439 if (dev_xdp_link(dev, mode)) {
9440 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9441 return -EBUSY;
9442 }
9443
Jussi Maki879af962021-07-31 05:57:33 +00009444 /* don't allow if an upper device already has a program */
9445 netdev_for_each_upper_dev_rcu(dev, upper, iter) {
9446 if (dev_xdp_prog_count(upper) > 0) {
9447 NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program");
9448 return -EEXIST;
9449 }
9450 }
9451
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009452 cur_prog = dev_xdp_prog(dev, mode);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009453 /* can't replace attached prog with link */
9454 if (link && cur_prog) {
9455 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9456 return -EBUSY;
9457 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009458 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9459 NL_SET_ERR_MSG(extack, "Active program does not match expected");
9460 return -EEXIST;
9461 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009462
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009463 /* put effective new program into new_prog */
9464 if (link)
9465 new_prog = link->link.prog;
9466
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009467 if (new_prog) {
9468 bool offload = mode == XDP_MODE_HW;
9469 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9470 ? XDP_MODE_DRV : XDP_MODE_SKB;
9471
Andrii Nakryiko068d9d12020-08-11 19:29:23 -07009472 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9473 NL_SET_ERR_MSG(extack, "XDP program already attached");
9474 return -EBUSY;
9475 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009476 if (!offload && dev_xdp_prog(dev, other_mode)) {
9477 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
9478 return -EEXIST;
9479 }
9480 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
9481 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
9482 return -EINVAL;
9483 }
9484 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
9485 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
9486 return -EINVAL;
9487 }
9488 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9489 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
9490 return -EINVAL;
9491 }
9492 }
9493
9494 /* don't call drivers if the effective program didn't change */
9495 if (new_prog != cur_prog) {
9496 bpf_op = dev_xdp_bpf_op(dev, mode);
9497 if (!bpf_op) {
9498 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9499 return -EOPNOTSUPP;
9500 }
9501
9502 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9503 if (err)
9504 return err;
9505 }
9506
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009507 if (link)
9508 dev_xdp_set_link(dev, mode, link);
9509 else
9510 dev_xdp_set_prog(dev, mode, new_prog);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009511 if (cur_prog)
9512 bpf_prog_put(cur_prog);
9513
9514 return 0;
9515}
9516
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009517static int dev_xdp_attach_link(struct net_device *dev,
9518 struct netlink_ext_ack *extack,
9519 struct bpf_xdp_link *link)
9520{
9521 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9522}
9523
9524static int dev_xdp_detach_link(struct net_device *dev,
9525 struct netlink_ext_ack *extack,
9526 struct bpf_xdp_link *link)
9527{
9528 enum bpf_xdp_mode mode;
9529 bpf_op_t bpf_op;
9530
9531 ASSERT_RTNL();
9532
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009533 mode = dev_xdp_mode(dev, link->flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009534 if (dev_xdp_link(dev, mode) != link)
9535 return -EINVAL;
9536
9537 bpf_op = dev_xdp_bpf_op(dev, mode);
9538 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9539 dev_xdp_set_link(dev, mode, NULL);
9540 return 0;
9541}
9542
9543static void bpf_xdp_link_release(struct bpf_link *link)
9544{
9545 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9546
9547 rtnl_lock();
9548
9549 /* if racing with net_device's tear down, xdp_link->dev might be
9550 * already NULL, in which case link was already auto-detached
9551 */
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009552 if (xdp_link->dev) {
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009553 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009554 xdp_link->dev = NULL;
9555 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009556
9557 rtnl_unlock();
9558}
9559
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009560static int bpf_xdp_link_detach(struct bpf_link *link)
9561{
9562 bpf_xdp_link_release(link);
9563 return 0;
9564}
9565
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009566static void bpf_xdp_link_dealloc(struct bpf_link *link)
9567{
9568 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9569
9570 kfree(xdp_link);
9571}
9572
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009573static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9574 struct seq_file *seq)
9575{
9576 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9577 u32 ifindex = 0;
9578
9579 rtnl_lock();
9580 if (xdp_link->dev)
9581 ifindex = xdp_link->dev->ifindex;
9582 rtnl_unlock();
9583
9584 seq_printf(seq, "ifindex:\t%u\n", ifindex);
9585}
9586
9587static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9588 struct bpf_link_info *info)
9589{
9590 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9591 u32 ifindex = 0;
9592
9593 rtnl_lock();
9594 if (xdp_link->dev)
9595 ifindex = xdp_link->dev->ifindex;
9596 rtnl_unlock();
9597
9598 info->xdp.ifindex = ifindex;
9599 return 0;
9600}
9601
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009602static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9603 struct bpf_prog *old_prog)
9604{
9605 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9606 enum bpf_xdp_mode mode;
9607 bpf_op_t bpf_op;
9608 int err = 0;
9609
9610 rtnl_lock();
9611
9612 /* link might have been auto-released already, so fail */
9613 if (!xdp_link->dev) {
9614 err = -ENOLINK;
9615 goto out_unlock;
9616 }
9617
9618 if (old_prog && link->prog != old_prog) {
9619 err = -EPERM;
9620 goto out_unlock;
9621 }
9622 old_prog = link->prog;
9623 if (old_prog == new_prog) {
9624 /* no-op, don't disturb drivers */
9625 bpf_prog_put(new_prog);
9626 goto out_unlock;
9627 }
9628
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009629 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009630 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9631 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9632 xdp_link->flags, new_prog);
9633 if (err)
9634 goto out_unlock;
9635
9636 old_prog = xchg(&link->prog, new_prog);
9637 bpf_prog_put(old_prog);
9638
9639out_unlock:
9640 rtnl_unlock();
9641 return err;
9642}
9643
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009644static const struct bpf_link_ops bpf_xdp_link_lops = {
9645 .release = bpf_xdp_link_release,
9646 .dealloc = bpf_xdp_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009647 .detach = bpf_xdp_link_detach,
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009648 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9649 .fill_link_info = bpf_xdp_link_fill_link_info,
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009650 .update_prog = bpf_xdp_link_update,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009651};
9652
9653int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9654{
9655 struct net *net = current->nsproxy->net_ns;
9656 struct bpf_link_primer link_primer;
9657 struct bpf_xdp_link *link;
9658 struct net_device *dev;
9659 int err, fd;
9660
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009661 rtnl_lock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009662 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009663 if (!dev) {
9664 rtnl_unlock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009665 return -EINVAL;
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009666 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009667
9668 link = kzalloc(sizeof(*link), GFP_USER);
9669 if (!link) {
9670 err = -ENOMEM;
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009671 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009672 }
9673
9674 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9675 link->dev = dev;
9676 link->flags = attr->link_create.flags;
9677
9678 err = bpf_link_prime(&link->link, &link_primer);
9679 if (err) {
9680 kfree(link);
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009681 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009682 }
9683
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009684 err = dev_xdp_attach_link(dev, NULL, link);
9685 rtnl_unlock();
9686
9687 if (err) {
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009688 link->dev = NULL;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009689 bpf_link_cleanup(&link_primer);
9690 goto out_put_dev;
9691 }
9692
9693 fd = bpf_link_settle(&link_primer);
9694 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9695 dev_put(dev);
9696 return fd;
9697
Xuan Zhuo5acc7d32021-07-10 11:16:35 +08009698unlock:
9699 rtnl_unlock();
9700
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009701out_put_dev:
9702 dev_put(dev);
9703 return err;
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009704}
9705
9706/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07009707 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9708 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07009709 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07009710 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009711 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01009712 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07009713 *
9714 * Set or clear a bpf program for a device
9715 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07009716int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009717 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07009718{
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009719 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009720 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07009721 int err;
9722
Daniel Borkmann85de8572016-11-28 23:16:54 +01009723 ASSERT_RTNL();
9724
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009725 if (fd >= 0) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009726 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9727 mode != XDP_MODE_SKB);
9728 if (IS_ERR(new_prog))
9729 return PTR_ERR(new_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009730 }
9731
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009732 if (expected_fd >= 0) {
9733 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9734 mode != XDP_MODE_SKB);
9735 if (IS_ERR(old_prog)) {
9736 err = PTR_ERR(old_prog);
9737 old_prog = NULL;
9738 goto err_out;
9739 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009740 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07009741
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009742 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009743
9744err_out:
9745 if (err && new_prog)
9746 bpf_prog_put(new_prog);
9747 if (old_prog)
9748 bpf_prog_put(old_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009749 return err;
9750}
Brenden Blancoa7862b42016-07-19 12:16:48 -07009751
9752/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07009754 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 *
9756 * Returns a suitable unique value for a new device interface
9757 * number. The caller must hold the rtnl semaphore or the
9758 * dev_base_lock to be sure it remains unique.
9759 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07009760static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009762 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11009763
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 for (;;) {
9765 if (++ifindex <= 0)
9766 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009767 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009768 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769 }
9770}
9771
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08009773static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07009774DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775
Stephen Hemminger6f05f622007-03-08 20:46:03 -08009776static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009779 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780}
9781
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009782static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9783 struct net_device *upper, netdev_features_t features)
9784{
9785 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9786 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009787 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009788
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009789 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009790 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009791 if (!(upper->wanted_features & feature)
9792 && (features & feature)) {
9793 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9794 &feature, upper->name);
9795 features &= ~feature;
9796 }
9797 }
9798
9799 return features;
9800}
9801
9802static void netdev_sync_lower_features(struct net_device *upper,
9803 struct net_device *lower, netdev_features_t features)
9804{
9805 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9806 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009807 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009808
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009809 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009810 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009811 if (!(features & feature) && (lower->features & feature)) {
9812 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9813 &feature, lower->name);
9814 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07009815 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009816
9817 if (unlikely(lower->features & feature))
9818 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9819 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07009820 else
9821 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009822 }
9823 }
9824}
9825
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009826static netdev_features_t netdev_fix_features(struct net_device *dev,
9827 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07009828{
Michał Mirosław57422dc2011-01-22 12:14:12 +00009829 /* Fix illegal checksum combinations */
9830 if ((features & NETIF_F_HW_CSUM) &&
9831 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009832 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00009833 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9834 }
9835
Herbert Xub63365a2008-10-23 01:11:29 -07009836 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009837 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009838 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009839 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07009840 }
9841
Pravin B Shelarec5f0612013-03-07 09:28:01 +00009842 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9843 !(features & NETIF_F_IP_CSUM)) {
9844 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9845 features &= ~NETIF_F_TSO;
9846 features &= ~NETIF_F_TSO_ECN;
9847 }
9848
9849 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9850 !(features & NETIF_F_IPV6_CSUM)) {
9851 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9852 features &= ~NETIF_F_TSO6;
9853 }
9854
Alexander Duyckb1dc4972016-05-02 09:38:24 -07009855 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9856 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9857 features &= ~NETIF_F_TSO_MANGLEID;
9858
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00009859 /* TSO ECN requires that TSO is present as well. */
9860 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9861 features &= ~NETIF_F_TSO_ECN;
9862
Michał Mirosław212b5732011-02-15 16:59:16 +00009863 /* Software GSO depends on SG. */
9864 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009865 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00009866 features &= ~NETIF_F_GSO;
9867 }
9868
Alexander Duyck802ab552016-04-10 21:45:03 -04009869 /* GSO partial features require GSO partial be set */
9870 if ((features & dev->gso_partial_features) &&
9871 !(features & NETIF_F_GSO_PARTIAL)) {
9872 netdev_dbg(dev,
9873 "Dropping partially supported GSO features since no GSO partial.\n");
9874 features &= ~dev->gso_partial_features;
9875 }
9876
Michael Chanfb1f5f72017-12-16 03:09:40 -05009877 if (!(features & NETIF_F_RXCSUM)) {
9878 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9879 * successfully merged by hardware must also have the
9880 * checksum verified by hardware. If the user does not
9881 * want to enable RXCSUM, logically, we should disable GRO_HW.
9882 */
9883 if (features & NETIF_F_GRO_HW) {
9884 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9885 features &= ~NETIF_F_GRO_HW;
9886 }
9887 }
9888
Gal Pressmande8d5ab2018-03-12 11:48:49 +02009889 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9890 if (features & NETIF_F_RXFCS) {
9891 if (features & NETIF_F_LRO) {
9892 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9893 features &= ~NETIF_F_LRO;
9894 }
9895
9896 if (features & NETIF_F_GRO_HW) {
9897 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9898 features &= ~NETIF_F_GRO_HW;
9899 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02009900 }
9901
Tariq Toukan25537d72021-01-14 17:12:15 +02009902 if (features & NETIF_F_HW_TLS_TX) {
9903 bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) ==
9904 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
9905 bool hw_csum = features & NETIF_F_HW_CSUM;
9906
9907 if (!ip_csum && !hw_csum) {
9908 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
9909 features &= ~NETIF_F_HW_TLS_TX;
9910 }
Tariq Toukanae0b04b2020-12-13 16:39:29 +02009911 }
9912
Tariq Toukana3eb4e92021-01-17 17:15:38 +02009913 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9914 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9915 features &= ~NETIF_F_HW_TLS_RX;
9916 }
9917
Herbert Xub63365a2008-10-23 01:11:29 -07009918 return features;
9919}
Herbert Xub63365a2008-10-23 01:11:29 -07009920
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009921int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009922{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009923 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009924 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009925 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009926 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009927
Michał Mirosław87267482011-04-12 09:56:38 +00009928 ASSERT_RTNL();
9929
Michał Mirosław5455c692011-02-15 16:59:17 +00009930 features = netdev_get_wanted_features(dev);
9931
9932 if (dev->netdev_ops->ndo_fix_features)
9933 features = dev->netdev_ops->ndo_fix_features(dev, features);
9934
9935 /* driver might be less strict about feature dependencies */
9936 features = netdev_fix_features(dev, features);
9937
Randy Dunlap4250b752020-09-17 21:35:15 -07009938 /* some features can't be enabled if they're off on an upper device */
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009939 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9940 features = netdev_sync_upper_features(dev, upper, features);
9941
Michał Mirosław5455c692011-02-15 16:59:17 +00009942 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009943 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009944
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009945 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9946 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009947
9948 if (dev->netdev_ops->ndo_set_features)
9949 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009950 else
9951 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009952
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009953 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009954 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009955 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9956 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009957 /* return non-0 since some features might have changed and
9958 * it's better to fire a spurious notification than miss it
9959 */
9960 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009961 }
9962
Jarod Wilsone7868a82015-11-03 23:09:32 -05009963sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009964 /* some features must be disabled on lower devices when disabled
9965 * on an upper device (think: bonding master or bridge)
9966 */
9967 netdev_for_each_lower_dev(dev, lower, iter)
9968 netdev_sync_lower_features(dev, lower, features);
9969
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009970 if (!err) {
9971 netdev_features_t diff = features ^ dev->features;
9972
9973 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9974 /* udp_tunnel_{get,drop}_rx_info both need
9975 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9976 * device, or they won't do anything.
9977 * Thus we need to update dev->features
9978 * *before* calling udp_tunnel_get_rx_info,
9979 * but *after* calling udp_tunnel_drop_rx_info.
9980 */
9981 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9982 dev->features = features;
9983 udp_tunnel_get_rx_info(dev);
9984 } else {
9985 udp_tunnel_drop_rx_info(dev);
9986 }
9987 }
9988
Gal Pressman9daae9b2018-03-28 17:46:54 +03009989 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9990 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9991 dev->features = features;
9992 err |= vlan_get_rx_ctag_filter_info(dev);
9993 } else {
9994 vlan_drop_rx_ctag_filter_info(dev);
9995 }
9996 }
9997
9998 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9999 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
10000 dev->features = features;
10001 err |= vlan_get_rx_stag_filter_info(dev);
10002 } else {
10003 vlan_drop_rx_stag_filter_info(dev);
10004 }
10005 }
10006
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010007 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +020010008 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010009
Jarod Wilsone7868a82015-11-03 23:09:32 -050010010 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010011}
10012
Michał Mirosławafe12cc2011-05-07 03:22:17 +000010013/**
10014 * netdev_update_features - recalculate device features
10015 * @dev: the device to check
10016 *
10017 * Recalculate dev->features set and send notifications if it
10018 * has changed. Should be called after driver or hardware dependent
10019 * conditions might have changed that influence the features.
10020 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010021void netdev_update_features(struct net_device *dev)
10022{
10023 if (__netdev_update_features(dev))
10024 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +000010025}
10026EXPORT_SYMBOL(netdev_update_features);
10027
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +000010029 * netdev_change_features - recalculate device features
10030 * @dev: the device to check
10031 *
10032 * Recalculate dev->features set and send notifications even
10033 * if they have not changed. Should be called instead of
10034 * netdev_update_features() if also dev->vlan_features might
10035 * have changed to allow the changes to be propagated to stacked
10036 * VLAN devices.
10037 */
10038void netdev_change_features(struct net_device *dev)
10039{
10040 __netdev_update_features(dev);
10041 netdev_features_change(dev);
10042}
10043EXPORT_SYMBOL(netdev_change_features);
10044
10045/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -080010046 * netif_stacked_transfer_operstate - transfer operstate
10047 * @rootdev: the root or lower level device to transfer state from
10048 * @dev: the device to transfer operstate to
10049 *
10050 * Transfer operational state from root to device. This is normally
10051 * called when a stacking relationship exists between the root
10052 * device and the device(a leaf device).
10053 */
10054void netif_stacked_transfer_operstate(const struct net_device *rootdev,
10055 struct net_device *dev)
10056{
10057 if (rootdev->operstate == IF_OPER_DORMANT)
10058 netif_dormant_on(dev);
10059 else
10060 netif_dormant_off(dev);
10061
Andrew Lunneec517cd2020-04-20 00:11:50 +020010062 if (rootdev->operstate == IF_OPER_TESTING)
10063 netif_testing_on(dev);
10064 else
10065 netif_testing_off(dev);
10066
Zhang Shengju0575c862017-04-26 17:49:38 +080010067 if (netif_carrier_ok(rootdev))
10068 netif_carrier_on(dev);
10069 else
10070 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -080010071}
10072EXPORT_SYMBOL(netif_stacked_transfer_operstate);
10073
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010074static int netif_alloc_rx_queues(struct net_device *dev)
10075{
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010076 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +000010077 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +053010078 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010079 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010080
Tom Herbertbd25fa72010-10-18 18:00:16 +000010081 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010082
Vasily Averinc948f512021-07-19 13:44:23 +030010083 rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -070010084 if (!rx)
10085 return -ENOMEM;
10086
Tom Herbertbd25fa72010-10-18 18:00:16 +000010087 dev->_rx = rx;
10088
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010089 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +000010090 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010091
10092 /* XDP RX-queue setup */
Björn Töpelb02e5a02020-11-30 19:52:01 +010010093 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010094 if (err < 0)
10095 goto err_rxq_info;
10096 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010097 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010098
10099err_rxq_info:
10100 /* Rollback successful reg's and free other resources */
10101 while (i--)
10102 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -080010103 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010104 dev->_rx = NULL;
10105 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010106}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010107
10108static void netif_free_rx_queues(struct net_device *dev)
10109{
10110 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010111
10112 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
10113 if (!dev->_rx)
10114 return;
10115
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010116 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -080010117 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
10118
10119 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010120}
Eric Dumazet1b4bf462010-09-23 17:26:35 +000010121
Changli Gaoaa942102010-12-04 02:31:41 +000010122static void netdev_init_one_queue(struct net_device *dev,
10123 struct netdev_queue *queue, void *_unused)
10124{
10125 /* Initialize queue lock */
10126 spin_lock_init(&queue->_xmit_lock);
Cong Wang1a33e102020-05-02 22:22:19 -070010127 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
Changli Gaoaa942102010-12-04 02:31:41 +000010128 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +000010129 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +000010130 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +000010131#ifdef CONFIG_BQL
10132 dql_init(&queue->dql, HZ);
10133#endif
Changli Gaoaa942102010-12-04 02:31:41 +000010134}
10135
Eric Dumazet60877a32013-06-20 01:15:51 -070010136static void netif_free_tx_queues(struct net_device *dev)
10137{
WANG Cong4cb28972014-06-02 15:55:22 -070010138 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -070010139}
10140
Tom Herberte6484932010-10-18 18:04:39 +000010141static int netif_alloc_netdev_queues(struct net_device *dev)
10142{
10143 unsigned int count = dev->num_tx_queues;
10144 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -070010145 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +000010146
Eric Dumazetd3397272015-07-06 17:13:26 +020010147 if (count < 1 || count > 0xffff)
10148 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +000010149
Vasily Averinc948f512021-07-19 13:44:23 +030010150 tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -070010151 if (!tx)
10152 return -ENOMEM;
10153
Tom Herberte6484932010-10-18 18:04:39 +000010154 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +000010155
Tom Herberte6484932010-10-18 18:04:39 +000010156 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
10157 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +000010158
10159 return 0;
Tom Herberte6484932010-10-18 18:04:39 +000010160}
10161
Denys Vlasenkoa2029242015-05-11 21:17:53 +020010162void netif_tx_stop_all_queues(struct net_device *dev)
10163{
10164 unsigned int i;
10165
10166 for (i = 0; i < dev->num_tx_queues; i++) {
10167 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +110010168
Denys Vlasenkoa2029242015-05-11 21:17:53 +020010169 netif_tx_stop_queue(txq);
10170 }
10171}
10172EXPORT_SYMBOL(netif_tx_stop_all_queues);
10173
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -080010174/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 * register_netdevice - register a network device
10176 * @dev: device to register
10177 *
10178 * Take a completed network device structure and add it to the kernel
10179 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10180 * chain. 0 is returned on success. A negative errno code is returned
10181 * on a failure to set up the device, or if the name is a duplicate.
10182 *
10183 * Callers must hold the rtnl semaphore. You may want
10184 * register_netdev() instead of this.
10185 *
10186 * BUGS:
10187 * The locking appears insufficient to guarantee two parallel registers
10188 * will not get the same name.
10189 */
10190
10191int register_netdevice(struct net_device *dev)
10192{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010194 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195
Florian Fainellie283de32018-04-30 14:20:05 -070010196 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
10197 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 BUG_ON(dev_boot_phase);
10199 ASSERT_RTNL();
10200
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010201 might_sleep();
10202
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 /* When net_device's are persistent, this will be fatal. */
10204 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010205 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206
Jakub Kicinski9000edb2020-03-16 13:47:12 -070010207 ret = ethtool_check_ops(dev->ethtool_ops);
10208 if (ret)
10209 return ret;
10210
David S. Millerf1f28aa2008-07-15 00:08:33 -070010211 spin_lock_init(&dev->addr_list_lock);
Cong Wang845e0eb2020-06-08 14:53:01 -070010212 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213
Gao feng828de4f2012-09-13 20:58:27 +000010214 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +000010215 if (ret < 0)
10216 goto out;
10217
Eric Dumazet9077f052019-10-03 08:59:24 -070010218 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +020010219 dev->name_node = netdev_name_node_head_alloc(dev);
10220 if (!dev->name_node)
10221 goto out;
10222
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010224 if (dev->netdev_ops->ndo_init) {
10225 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 if (ret) {
10227 if (ret > 0)
10228 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010229 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 }
10231 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010232
Patrick McHardyf6469682013-04-19 02:04:27 +000010233 if (((dev->hw_features | dev->features) &
10234 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +000010235 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
10236 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
10237 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
10238 ret = -EINVAL;
10239 goto err_uninit;
10240 }
10241
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +000010242 ret = -EBUSY;
10243 if (!dev->ifindex)
10244 dev->ifindex = dev_new_index(net);
10245 else if (__dev_get_by_index(net, dev->ifindex))
10246 goto err_uninit;
10247
Michał Mirosław5455c692011-02-15 16:59:17 +000010248 /* Transfer changeable features to wanted_features and enable
10249 * software offloads (GSO and GRO).
10250 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +010010251 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +000010252 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +020010253
Jakub Kicinski876c4382021-01-06 13:06:34 -080010254 if (dev->udp_tunnel_nic_info) {
Sabrina Dubrocad764a122017-07-21 12:49:28 +020010255 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10256 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10257 }
10258
Michał Mirosław14d12322011-02-22 16:52:28 +000010259 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010261 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +000010262 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010263
Alexander Duyck7f348a62016-04-20 16:51:00 -040010264 /* If IPv4 TCP segmentation offload is supported we should also
10265 * allow the device to enable segmenting the frame with the option
10266 * of ignoring a static IP ID value. This doesn't enable the
10267 * feature itself but allows the user to enable it later.
10268 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010269 if (dev->hw_features & NETIF_F_TSO)
10270 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -040010271 if (dev->vlan_features & NETIF_F_TSO)
10272 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
10273 if (dev->mpls_features & NETIF_F_TSO)
10274 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
10275 if (dev->hw_enc_features & NETIF_F_TSO)
10276 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -070010277
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010278 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +000010279 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010280 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +000010281
Pravin B Shelaree579672013-03-07 09:28:08 +000010282 /* Make NETIF_F_SG inheritable to tunnel devices.
10283 */
Alexander Duyck802ab552016-04-10 21:45:03 -040010284 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +000010285
Simon Horman0d89d202013-05-23 21:02:52 +000010286 /* Make NETIF_F_SG inheritable to MPLS.
10287 */
10288 dev->mpls_features |= NETIF_F_SG;
10289
Johannes Berg7ffbe3f2009-10-02 05:15:27 +000010290 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
10291 ret = notifier_to_errno(ret);
10292 if (ret)
10293 goto err_uninit;
10294
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010295 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010296 if (ret) {
10297 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010298 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010299 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010300 dev->reg_state = NETREG_REGISTERED;
10301
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010302 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +000010303
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304 /*
10305 * Default initial state at registry is that the
10306 * device is present.
10307 */
10308
10309 set_bit(__LINK_STATE_PRESENT, &dev->state);
10310
Ben Hutchings8f4cccb2012-08-20 22:16:51 +010010311 linkwatch_init_dev(dev);
10312
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010315 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -040010316 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317
Jiri Pirko948b3372013-01-08 01:38:25 +000010318 /* If the device has permanent device address, driver should
10319 * set dev_addr and also addr_assign_type should be set to
10320 * NET_ADDR_PERM (default value).
10321 */
10322 if (dev->addr_assign_type == NET_ADDR_PERM)
10323 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10324
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010326 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -070010327 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010328 if (ret) {
Jakub Kicinski766b0512021-01-06 10:40:07 -080010329 /* Expect explicit free_netdev() on failure */
10330 dev->needs_free_netdev = false;
Jakub Kicinski037e56b2021-01-19 12:25:19 -080010331 unregister_netdevice_queue(dev, NULL);
Jakub Kicinski766b0512021-01-06 10:40:07 -080010332 goto out;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010333 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010334 /*
10335 * Prevent userspace races by waiting until the network
10336 * device is fully setup before sending notifications.
10337 */
Patrick McHardya2835762010-02-26 06:34:51 +000010338 if (!dev->rtnl_link_ops ||
10339 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010340 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010341
10342out:
10343 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010344
10345err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010346 if (dev->netdev_ops->ndo_uninit)
10347 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -040010348 if (dev->priv_destructor)
10349 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010350err_free_name:
10351 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010353}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010354EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355
10356/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010357 * init_dummy_netdev - init a dummy network device for NAPI
10358 * @dev: device to init
10359 *
10360 * This takes a network device structure and initialize the minimum
10361 * amount of fields so it can be used to schedule NAPI polls without
10362 * registering a full blown interface. This is to be used by drivers
10363 * that need to tie several hardware interfaces to a single NAPI
10364 * poll scheduler due to HW limitations.
10365 */
10366int init_dummy_netdev(struct net_device *dev)
10367{
10368 /* Clear everything. Note we don't initialize spinlocks
10369 * are they aren't supposed to be taken by any of the
10370 * NAPI code and this dummy netdev is supposed to be
10371 * only ever used for NAPI polls
10372 */
10373 memset(dev, 0, sizeof(struct net_device));
10374
10375 /* make sure we BUG if trying to hit standard
10376 * register/unregister code path
10377 */
10378 dev->reg_state = NETREG_DUMMY;
10379
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010380 /* NAPI wants this */
10381 INIT_LIST_HEAD(&dev->napi_list);
10382
10383 /* a dummy interface is started by default */
10384 set_bit(__LINK_STATE_PRESENT, &dev->state);
10385 set_bit(__LINK_STATE_START, &dev->state);
10386
Josh Elsasser35edfdc2019-01-26 14:38:33 -080010387 /* napi_busy_loop stats accounting wants this */
10388 dev_net_set(dev, &init_net);
10389
Eric Dumazet29b44332010-10-11 10:22:12 +000010390 /* Note : We dont allocate pcpu_refcnt for dummy devices,
10391 * because users of this 'device' dont need to change
10392 * its refcount.
10393 */
10394
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010395 return 0;
10396}
10397EXPORT_SYMBOL_GPL(init_dummy_netdev);
10398
10399
10400/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 * register_netdev - register a network device
10402 * @dev: device to register
10403 *
10404 * Take a completed network device structure and add it to the kernel
10405 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10406 * chain. 0 is returned on success. A negative errno code is returned
10407 * on a failure to set up the device, or if the name is a duplicate.
10408 *
Borislav Petkov38b4da382007-04-20 22:14:10 -070010409 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410 * and expands the device name if you passed a format string to
10411 * alloc_netdev.
10412 */
10413int register_netdev(struct net_device *dev)
10414{
10415 int err;
10416
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +030010417 if (rtnl_lock_killable())
10418 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 rtnl_unlock();
10421 return err;
10422}
10423EXPORT_SYMBOL(register_netdev);
10424
Eric Dumazet29b44332010-10-11 10:22:12 +000010425int netdev_refcnt_read(const struct net_device *dev)
10426{
Eric Dumazet919067c2021-03-19 10:39:33 -070010427#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010428 int i, refcnt = 0;
10429
10430 for_each_possible_cpu(i)
10431 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10432 return refcnt;
Eric Dumazet919067c2021-03-19 10:39:33 -070010433#else
10434 return refcount_read(&dev->dev_refcnt);
10435#endif
Eric Dumazet29b44332010-10-11 10:22:12 +000010436}
10437EXPORT_SYMBOL(netdev_refcnt_read);
10438
Dmitry Vyukov5aa3afe2021-03-23 07:49:23 +010010439int netdev_unregister_timeout_secs __read_mostly = 10;
10440
Mauro Carvalho Chehabde2b5412020-09-22 13:22:52 +020010441#define WAIT_REFS_MIN_MSECS 1
10442#define WAIT_REFS_MAX_MSECS 250
Ben Hutchings2c530402012-07-10 10:55:09 +000010443/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +000010445 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 *
10447 * This is called when unregistering network devices.
10448 *
10449 * Any protocol or device that holds a reference should register
10450 * for netdevice notification, and cleanup and put back the
10451 * reference if they receive an UNREGISTER event.
10452 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010453 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454 */
10455static void netdev_wait_allrefs(struct net_device *dev)
10456{
10457 unsigned long rebroadcast_time, warning_time;
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010458 int wait = 0, refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459
Eric Dumazete014deb2009-11-17 05:59:21 +000010460 linkwatch_forget_dev(dev);
10461
Linus Torvalds1da177e2005-04-16 15:20:36 -070010462 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +000010463 refcnt = netdev_refcnt_read(dev);
10464
Eric Dumazetadd2d732021-03-22 11:21:45 -070010465 while (refcnt != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010467 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468
10469 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010470 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471
Eric Dumazet748e2d92012-08-22 21:50:59 +000010472 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010473 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +000010474 rtnl_lock();
10475
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10477 &dev->state)) {
10478 /* We must not have linkwatch events
10479 * pending on unregister. If this
10480 * happens, we simply run the queue
10481 * unscheduled, resulting in a noop
10482 * for this device.
10483 */
10484 linkwatch_run_queue();
10485 }
10486
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010487 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010488
10489 rebroadcast_time = jiffies;
10490 }
10491
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010492 if (!wait) {
10493 rcu_barrier();
10494 wait = WAIT_REFS_MIN_MSECS;
10495 } else {
10496 msleep(wait);
10497 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499
Eric Dumazet29b44332010-10-11 10:22:12 +000010500 refcnt = netdev_refcnt_read(dev);
10501
Dmitry Vyukov6c996e12021-03-25 15:52:45 +010010502 if (refcnt != 1 &&
Dmitry Vyukov5aa3afe2021-03-23 07:49:23 +010010503 time_after(jiffies, warning_time +
10504 netdev_unregister_timeout_secs * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010505 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10506 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 warning_time = jiffies;
10508 }
10509 }
10510}
10511
10512/* The sequence is:
10513 *
10514 * rtnl_lock();
10515 * ...
10516 * register_netdevice(x1);
10517 * register_netdevice(x2);
10518 * ...
10519 * unregister_netdevice(y1);
10520 * unregister_netdevice(y2);
10521 * ...
10522 * rtnl_unlock();
10523 * free_netdev(y1);
10524 * free_netdev(y2);
10525 *
Herbert Xu58ec3b42008-10-07 15:50:03 -070010526 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010528 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529 * without deadlocking with linkwatch via keventd.
10530 * 2) Since we run with the RTNL semaphore not held, we can sleep
10531 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -070010532 *
10533 * We must not return until all unregister events added during
10534 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536void netdev_run_todo(void)
10537{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010538 struct list_head list;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010539#ifdef CONFIG_LOCKDEP
10540 struct list_head unlink_list;
10541
10542 list_replace_init(&net_unlink_list, &unlink_list);
10543
10544 while (!list_empty(&unlink_list)) {
10545 struct net_device *dev = list_first_entry(&unlink_list,
10546 struct net_device,
10547 unlink_list);
Taehee Yoo0e8b8d62020-10-15 16:26:06 +000010548 list_del_init(&dev->unlink_list);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010549 dev->nested_level = dev->lower_level - 1;
10550 }
10551#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010554 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -070010555
10556 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010557
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010558
10559 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +000010560 if (!list_empty(&list))
10561 rcu_barrier();
10562
Linus Torvalds1da177e2005-04-16 15:20:36 -070010563 while (!list_empty(&list)) {
10564 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +000010565 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010566 list_del(&dev->todo_list);
10567
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010568 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010569 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010571 dump_stack();
10572 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010574
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010575 dev->reg_state = NETREG_UNREGISTERED;
10576
10577 netdev_wait_allrefs(dev);
10578
10579 /* paranoia */
Eric Dumazetadd2d732021-03-22 11:21:45 -070010580 BUG_ON(netdev_refcnt_read(dev) != 1);
Salam Noureddine7866a622015-01-27 11:35:48 -080010581 BUG_ON(!list_empty(&dev->ptype_all));
10582 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +000010583 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10584 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -080010585#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -070010586 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -080010587#endif
David S. Millercf124db2017-05-08 12:52:56 -040010588 if (dev->priv_destructor)
10589 dev->priv_destructor(dev);
10590 if (dev->needs_free_netdev)
10591 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010592
Eric W. Biederman50624c92013-09-23 21:19:49 -070010593 /* Report a network device has been unregistered */
10594 rtnl_lock();
10595 dev_net(dev)->dev_unreg_count--;
10596 __rtnl_unlock();
10597 wake_up(&netdev_unregistering_wq);
10598
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010599 /* Free network device */
10600 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010602}
10603
Jarod Wilson92566452016-02-01 18:51:04 -050010604/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
10605 * all the same fields in the same order as net_device_stats, with only
10606 * the type differing, but rtnl_link_stats64 may have additional fields
10607 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +000010608 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010609void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
10610 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +000010611{
10612#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -050010613 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +020010614 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -050010615 /* zero out counters that only exist in rtnl_link_stats64 */
10616 memset((char *)stats64 + sizeof(*netdev_stats), 0,
10617 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010618#else
Jarod Wilson92566452016-02-01 18:51:04 -050010619 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010620 const unsigned long *src = (const unsigned long *)netdev_stats;
10621 u64 *dst = (u64 *)stats64;
10622
Jarod Wilson92566452016-02-01 18:51:04 -050010623 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010624 for (i = 0; i < n; i++)
10625 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -050010626 /* zero out counters that only exist in rtnl_link_stats64 */
10627 memset((char *)stats64 + n * sizeof(u64), 0,
10628 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010629#endif
10630}
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010631EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010632
Eric Dumazetd83345a2009-11-16 03:36:51 +000010633/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010634 * dev_get_stats - get network device statistics
10635 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -070010636 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010637 *
Ben Hutchingsd7753512010-07-09 09:12:41 +000010638 * Get network statistics from device. Return @storage.
10639 * The device driver may provide its own method by setting
10640 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10641 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010642 */
Ben Hutchingsd7753512010-07-09 09:12:41 +000010643struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10644 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +000010645{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010646 const struct net_device_ops *ops = dev->netdev_ops;
10647
Eric Dumazet28172732010-07-07 14:58:56 -070010648 if (ops->ndo_get_stats64) {
10649 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010650 ops->ndo_get_stats64(dev, storage);
10651 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +000010652 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010653 } else {
10654 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -070010655 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -070010656 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10657 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10658 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -070010659 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -070010660}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010661EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -070010662
Heiner Kallweit44fa32f2020-10-12 10:01:27 +020010663/**
10664 * dev_fetch_sw_netstats - get per-cpu network device statistics
10665 * @s: place to store stats
10666 * @netstats: per-cpu network stats to read from
10667 *
10668 * Read per-cpu network statistics and populate the related fields in @s.
10669 */
10670void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10671 const struct pcpu_sw_netstats __percpu *netstats)
10672{
10673 int cpu;
10674
10675 for_each_possible_cpu(cpu) {
10676 const struct pcpu_sw_netstats *stats;
10677 struct pcpu_sw_netstats tmp;
10678 unsigned int start;
10679
10680 stats = per_cpu_ptr(netstats, cpu);
10681 do {
10682 start = u64_stats_fetch_begin_irq(&stats->syncp);
10683 tmp.rx_packets = stats->rx_packets;
10684 tmp.rx_bytes = stats->rx_bytes;
10685 tmp.tx_packets = stats->tx_packets;
10686 tmp.tx_bytes = stats->tx_bytes;
10687 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10688
10689 s->rx_packets += tmp.rx_packets;
10690 s->rx_bytes += tmp.rx_bytes;
10691 s->tx_packets += tmp.tx_packets;
10692 s->tx_bytes += tmp.tx_bytes;
10693 }
10694}
10695EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10696
Heiner Kallweita1839422020-11-07 21:49:07 +010010697/**
10698 * dev_get_tstats64 - ndo_get_stats64 implementation
10699 * @dev: device to get statistics from
10700 * @s: place to store stats
10701 *
10702 * Populate @s from dev->stats and dev->tstats. Can be used as
10703 * ndo_get_stats64() callback.
10704 */
10705void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
10706{
10707 netdev_stats_to_stats64(s, &dev->stats);
10708 dev_fetch_sw_netstats(s, dev->tstats);
10709}
10710EXPORT_SYMBOL_GPL(dev_get_tstats64);
10711
Eric Dumazet24824a02010-10-02 06:11:55 +000010712struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -070010713{
Eric Dumazet24824a02010-10-02 06:11:55 +000010714 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -070010715
Eric Dumazet24824a02010-10-02 06:11:55 +000010716#ifdef CONFIG_NET_CLS_ACT
10717 if (queue)
10718 return queue;
10719 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10720 if (!queue)
10721 return NULL;
10722 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -080010723 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +000010724 queue->qdisc_sleeping = &noop_qdisc;
10725 rcu_assign_pointer(dev->ingress_queue, queue);
10726#endif
10727 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -070010728}
10729
Eric Dumazet2c60db02012-09-16 09:17:26 +000010730static const struct ethtool_ops default_ethtool_ops;
10731
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +000010732void netdev_set_default_ethtool_ops(struct net_device *dev,
10733 const struct ethtool_ops *ops)
10734{
10735 if (dev->ethtool_ops == &default_ethtool_ops)
10736 dev->ethtool_ops = ops;
10737}
10738EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10739
Eric Dumazet74d332c2013-10-30 13:10:44 -070010740void netdev_freemem(struct net_device *dev)
10741{
10742 char *addr = (char *)dev - dev->padded;
10743
WANG Cong4cb28972014-06-02 15:55:22 -070010744 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010745}
10746
Linus Torvalds1da177e2005-04-16 15:20:36 -070010747/**
tcharding722c9a02017-02-09 17:56:04 +110010748 * alloc_netdev_mqs - allocate network device
10749 * @sizeof_priv: size of private data to allocate space for
10750 * @name: device name format string
10751 * @name_assign_type: origin of device name
10752 * @setup: callback to initialize device
10753 * @txqs: the number of TX subqueues to allocate
10754 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755 *
tcharding722c9a02017-02-09 17:56:04 +110010756 * Allocates a struct net_device with private data area for driver use
10757 * and performs basic initialization. Also allocates subqueue structs
10758 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759 */
Tom Herbert36909ea2011-01-09 19:36:31 +000010760struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +020010761 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +000010762 void (*setup)(struct net_device *),
10763 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +030010766 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010767 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010768
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -070010769 BUG_ON(strlen(name) >= sizeof(dev->name));
10770
Tom Herbert36909ea2011-01-09 19:36:31 +000010771 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010772 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +000010773 return NULL;
10774 }
10775
Tom Herbert36909ea2011-01-09 19:36:31 +000010776 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010777 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +000010778 return NULL;
10779 }
Tom Herbert36909ea2011-01-09 19:36:31 +000010780
David S. Millerfd2ea0a2008-07-17 01:56:23 -070010781 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010782 if (sizeof_priv) {
10783 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010784 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010785 alloc_size += sizeof_priv;
10786 }
10787 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010788 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010789
Vasily Averinc948f512021-07-19 13:44:23 +030010790 p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +000010791 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010792 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010793
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010794 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010795 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010796
Eric Dumazet919067c2021-03-19 10:39:33 -070010797#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010798 dev->pcpu_refcnt = alloc_percpu(int);
10799 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -070010800 goto free_dev;
Eric Dumazetadd2d732021-03-22 11:21:45 -070010801 dev_hold(dev);
10802#else
10803 refcount_set(&dev->dev_refcnt, 1);
Eric Dumazet919067c2021-03-19 10:39:33 -070010804#endif
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010805
Linus Torvalds1da177e2005-04-16 15:20:36 -070010806 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +000010807 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010808
Jiri Pirko22bedad32010-04-01 21:22:57 +000010809 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010810 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +000010811
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010812 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010813
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -070010814 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +000010815 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +000010816 dev->upper_level = 1;
10817 dev->lower_level = 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010818#ifdef CONFIG_LOCKDEP
10819 dev->nested_level = 0;
10820 INIT_LIST_HEAD(&dev->unlink_list);
10821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010822
Herbert Xud565b0a2008-12-15 23:38:52 -080010823 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010824 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -070010825 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +000010826 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +020010827 INIT_LIST_HEAD(&dev->adj_list.upper);
10828 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -080010829 INIT_LIST_HEAD(&dev->ptype_all);
10830 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +010010831 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +020010832#ifdef CONFIG_NET_SCHED
10833 hash_init(dev->qdisc_hash);
10834#endif
Eric Dumazet02875872014-10-05 18:38:35 -070010835 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010836 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010837
Phil Suttera8131042016-02-17 15:37:43 +010010838 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +020010839 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +010010840 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +010010841 }
Phil Sutter906470c2015-08-18 10:30:48 +020010842
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010843 dev->num_tx_queues = txqs;
10844 dev->real_num_tx_queues = txqs;
10845 if (netif_alloc_netdev_queues(dev))
10846 goto free_all;
10847
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010848 dev->num_rx_queues = rxqs;
10849 dev->real_num_rx_queues = rxqs;
10850 if (netif_alloc_rx_queues(dev))
10851 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010852
Linus Torvalds1da177e2005-04-16 15:20:36 -070010853 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +020010854 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +000010855 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +000010856 if (!dev->ethtool_ops)
10857 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +020010858
Daniel Borkmann357b6cc2020-03-18 10:33:22 +010010859 nf_hook_ingress_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +020010860
Linus Torvalds1da177e2005-04-16 15:20:36 -070010861 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010862
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010863free_all:
10864 free_netdev(dev);
10865 return NULL;
10866
Eric Dumazet29b44332010-10-11 10:22:12 +000010867free_pcpu:
Eric Dumazet919067c2021-03-19 10:39:33 -070010868#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010869 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010870free_dev:
Eric Dumazet919067c2021-03-19 10:39:33 -070010871#endif
Eric Dumazet74d332c2013-10-30 13:10:44 -070010872 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010873 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874}
Tom Herbert36909ea2011-01-09 19:36:31 +000010875EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010876
10877/**
tcharding722c9a02017-02-09 17:56:04 +110010878 * free_netdev - free network device
10879 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010880 *
tcharding722c9a02017-02-09 17:56:04 +110010881 * This function does the last stage of destroying an allocated device
10882 * interface. The reference to the device object is released. If this
10883 * is the last reference then it will be freed.Must be called in process
10884 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885 */
10886void free_netdev(struct net_device *dev)
10887{
Herbert Xud565b0a2008-12-15 23:38:52 -080010888 struct napi_struct *p, *n;
10889
Eric Dumazet93d05d42015-11-18 06:31:03 -080010890 might_sleep();
Jakub Kicinskic269a242021-01-06 10:40:06 -080010891
10892 /* When called immediately after register_netdevice() failed the unwind
10893 * handling may still be dismantling the device. Handle that case by
10894 * deferring the free.
10895 */
10896 if (dev->reg_state == NETREG_UNREGISTERING) {
10897 ASSERT_RTNL();
10898 dev->needs_free_netdev = true;
10899 return;
10900 }
10901
Eric Dumazet60877a32013-06-20 01:15:51 -070010902 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010903 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -070010904
Eric Dumazet33d480c2011-08-11 19:30:52 +000010905 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +000010906
Jiri Pirkof001fde2009-05-05 02:48:28 +000010907 /* Flush device addresses */
10908 dev_addr_flush(dev);
10909
Herbert Xud565b0a2008-12-15 23:38:52 -080010910 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10911 netif_napi_del(p);
10912
Eric Dumazet919067c2021-03-19 10:39:33 -070010913#ifdef CONFIG_PCPU_DEV_REFCNT
Eric Dumazet29b44332010-10-11 10:22:12 +000010914 free_percpu(dev->pcpu_refcnt);
10915 dev->pcpu_refcnt = NULL;
Eric Dumazet919067c2021-03-19 10:39:33 -070010916#endif
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010010917 free_percpu(dev->xdp_bulkq);
10918 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +000010919
Stephen Hemminger3041a062006-05-26 13:25:24 -070010920 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -070010922 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923 return;
10924 }
10925
10926 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10927 dev->reg_state = NETREG_RELEASED;
10928
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070010929 /* will free via device release */
10930 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010931}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010932EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010933
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010934/**
10935 * synchronize_net - Synchronize with packet receive processing
10936 *
10937 * Wait for packets currently being received to be done.
10938 * Does not block later packets from starting.
10939 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010940void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010941{
10942 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +000010943 if (rtnl_is_locked())
10944 synchronize_rcu_expedited();
10945 else
10946 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010947}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010948EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010949
10950/**
Eric Dumazet44a08732009-10-27 07:03:04 +000010951 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +000010953 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -080010954 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010955 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010956 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +000010957 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958 *
10959 * Callers must hold the rtnl semaphore. You may want
10960 * unregister_netdev() instead of this.
10961 */
10962
Eric Dumazet44a08732009-10-27 07:03:04 +000010963void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010964{
Herbert Xua6620712007-12-12 19:21:56 -080010965 ASSERT_RTNL();
10966
Eric Dumazet44a08732009-10-27 07:03:04 +000010967 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010968 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +000010969 } else {
Jakub Kicinski037e56b2021-01-19 12:25:19 -080010970 LIST_HEAD(single);
10971
10972 list_add(&dev->unreg_list, &single);
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010973 unregister_netdevice_many(&single);
Eric Dumazet44a08732009-10-27 07:03:04 +000010974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010975}
Eric Dumazet44a08732009-10-27 07:03:04 +000010976EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010977
10978/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010979 * unregister_netdevice_many - unregister many devices
10980 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -070010981 *
10982 * Note: As most callers use a stack allocated list_head,
10983 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010984 */
10985void unregister_netdevice_many(struct list_head *head)
10986{
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010987 struct net_device *dev, *tmp;
10988 LIST_HEAD(close_head);
10989
10990 BUG_ON(dev_boot_phase);
10991 ASSERT_RTNL();
10992
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080010993 if (list_empty(head))
10994 return;
10995
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080010996 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
10997 /* Some devices call without registering
10998 * for initialization unwind. Remove those
10999 * devices and proceed with the remaining.
11000 */
11001 if (dev->reg_state == NETREG_UNINITIALIZED) {
11002 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
11003 dev->name, dev);
11004
11005 WARN_ON(1);
11006 list_del(&dev->unreg_list);
11007 continue;
11008 }
11009 dev->dismantle = true;
11010 BUG_ON(dev->reg_state != NETREG_REGISTERED);
11011 }
11012
11013 /* If device is running, close it first. */
11014 list_for_each_entry(dev, head, unreg_list)
11015 list_add_tail(&dev->close_list, &close_head);
11016 dev_close_many(&close_head, true);
11017
11018 list_for_each_entry(dev, head, unreg_list) {
11019 /* And unlink it from device chain. */
11020 unlist_netdevice(dev);
11021
11022 dev->reg_state = NETREG_UNREGISTERING;
11023 }
11024 flush_all_backlogs();
11025
11026 synchronize_net();
11027
11028 list_for_each_entry(dev, head, unreg_list) {
11029 struct sk_buff *skb = NULL;
11030
11031 /* Shutdown queueing discipline. */
11032 dev_shutdown(dev);
11033
11034 dev_xdp_uninstall(dev);
11035
11036 /* Notify protocols, that we are about to destroy
11037 * this device. They should clean all the things.
11038 */
11039 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
11040
11041 if (!dev->rtnl_link_ops ||
11042 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
11043 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
11044 GFP_KERNEL, NULL, 0);
11045
11046 /*
11047 * Flush the unicast and multicast chains
11048 */
11049 dev_uc_flush(dev);
11050 dev_mc_flush(dev);
11051
11052 netdev_name_node_alt_flush(dev);
11053 netdev_name_node_free(dev->name_node);
11054
11055 if (dev->netdev_ops->ndo_uninit)
11056 dev->netdev_ops->ndo_uninit(dev);
11057
11058 if (skb)
11059 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
11060
11061 /* Notifier chain MUST detach us all upper devices. */
11062 WARN_ON(netdev_has_any_upper_dev(dev));
11063 WARN_ON(netdev_has_any_lower_dev(dev));
11064
11065 /* Remove entries from kobject tree */
11066 netdev_unregister_kobject(dev);
11067#ifdef CONFIG_XPS
11068 /* Remove XPS queueing entries */
11069 netif_reset_xps_queues_gt(dev, 0);
11070#endif
11071 }
11072
11073 synchronize_net();
11074
11075 list_for_each_entry(dev, head, unreg_list) {
11076 dev_put(dev);
11077 net_set_todo(dev);
11078 }
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080011079
11080 list_del(head);
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080011081}
Jakub Kicinski0cbe1e52021-01-19 12:25:21 -080011082EXPORT_SYMBOL(unregister_netdevice_many);
Jakub Kicinskibcfe2f12021-01-19 12:25:20 -080011083
Eric Dumazet9b5e3832009-10-27 07:04:19 +000011084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070011085 * unregister_netdev - remove device from the kernel
11086 * @dev: device
11087 *
11088 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080011089 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011090 *
11091 * This is just a wrapper for unregister_netdevice that takes
11092 * the rtnl semaphore. In general you want to use this and not
11093 * unregister_netdevice.
11094 */
11095void unregister_netdev(struct net_device *dev)
11096{
11097 rtnl_lock();
11098 unregister_netdevice(dev);
11099 rtnl_unlock();
11100}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101EXPORT_SYMBOL(unregister_netdev);
11102
Eric W. Biedermance286d32007-09-12 13:53:49 +020011103/**
Andrei Vagin0854fa82021-04-06 23:40:51 -070011104 * __dev_change_net_namespace - move device to different nethost namespace
Eric W. Biedermance286d32007-09-12 13:53:49 +020011105 * @dev: device
11106 * @net: network namespace
11107 * @pat: If not NULL name pattern to try if the current device name
11108 * is already taken in the destination network namespace.
Andrei Vagineeb85a12021-04-05 00:12:23 -070011109 * @new_ifindex: If not zero, specifies device index in the target
11110 * namespace.
Eric W. Biedermance286d32007-09-12 13:53:49 +020011111 *
11112 * This function shuts down a device interface and moves it
11113 * to a new network namespace. On success 0 is returned, on
11114 * a failure a netagive errno code is returned.
11115 *
11116 * Callers must hold the rtnl semaphore.
11117 */
11118
Andrei Vagin0854fa82021-04-06 23:40:51 -070011119int __dev_change_net_namespace(struct net_device *dev, struct net *net,
11120 const char *pat, int new_ifindex)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011121{
Christian Brauneref6a4c82020-02-27 04:37:19 +010011122 struct net *net_old = dev_net(dev);
Andrei Vagineeb85a12021-04-05 00:12:23 -070011123 int err, new_nsid;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011124
11125 ASSERT_RTNL();
11126
11127 /* Don't allow namespace local devices to be moved. */
11128 err = -EINVAL;
11129 if (dev->features & NETIF_F_NETNS_LOCAL)
11130 goto out;
11131
11132 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020011133 if (dev->reg_state != NETREG_REGISTERED)
11134 goto out;
11135
11136 /* Get out if there is nothing todo */
11137 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010011138 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020011139 goto out;
11140
11141 /* Pick the destination device name, and ensure
11142 * we can use it in the destination network namespace.
11143 */
11144 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000011145 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011146 /* We get here if we can't use the current device name */
11147 if (!pat)
11148 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080011149 err = dev_get_valid_name(net, dev, pat);
11150 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011151 goto out;
11152 }
11153
Andrei Vagineeb85a12021-04-05 00:12:23 -070011154 /* Check that new_ifindex isn't used yet. */
11155 err = -EBUSY;
11156 if (new_ifindex && __dev_get_by_index(net, new_ifindex))
11157 goto out;
11158
Eric W. Biedermance286d32007-09-12 13:53:49 +020011159 /*
11160 * And now a mini version of register_netdevice unregister_netdevice.
11161 */
11162
11163 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070011164 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011165
11166 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020011167 unlist_netdevice(dev);
11168
11169 synchronize_net();
11170
11171 /* Shutdown queueing discipline. */
11172 dev_shutdown(dev);
11173
11174 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110011175 * this device. They should clean all the things.
11176 *
11177 * Note that dev->reg_state stays at NETREG_REGISTERED.
11178 * This is wanted because this way 8021q and macvlan know
11179 * the device is just moving and can keep their slaves up.
11180 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020011181 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000011182 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010011183
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020011184 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010011185 /* If there is an ifindex conflict assign a new one */
Andrei Vagineeb85a12021-04-05 00:12:23 -070011186 if (!new_ifindex) {
11187 if (__dev_get_by_index(net, dev->ifindex))
11188 new_ifindex = dev_new_index(net);
11189 else
11190 new_ifindex = dev->ifindex;
11191 }
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010011192
11193 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
11194 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011195
11196 /*
11197 * Flush the unicast and multicast chains
11198 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000011199 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000011200 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011201
Serge Hallyn4e66ae22012-12-03 16:17:12 +000011202 /* Send a netdev-removed uevent to the old namespace */
11203 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040011204 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000011205
Jiri Pirko93642e12020-01-25 12:17:08 +010011206 /* Move per-net netdevice notifiers that are following the netdevice */
11207 move_netdevice_notifiers_dev_net(dev, net);
11208
Eric W. Biedermance286d32007-09-12 13:53:49 +020011209 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090011210 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010011211 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011212
Serge Hallyn4e66ae22012-12-03 16:17:12 +000011213 /* Send a netdev-add uevent to the new namespace */
11214 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040011215 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000011216
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011217 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070011218 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011219 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011220
Christian Brauneref6a4c82020-02-27 04:37:19 +010011221 /* Adapt owner in case owning user namespace of target network
11222 * namespace is different from the original one.
11223 */
11224 err = netdev_change_owner(dev, net_old, net);
11225 WARN_ON(err);
11226
Eric W. Biedermance286d32007-09-12 13:53:49 +020011227 /* Add the device back in the hashes */
11228 list_netdevice(dev);
11229
11230 /* Notify protocols, that a new device appeared. */
11231 call_netdevice_notifiers(NETDEV_REGISTER, dev);
11232
Eric W. Biedermand90a9092009-12-12 22:11:15 +000011233 /*
11234 * Prevent userspace races by waiting until the network
11235 * device is fully setup before sending notifications.
11236 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070011237 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000011238
Eric W. Biedermance286d32007-09-12 13:53:49 +020011239 synchronize_net();
11240 err = 0;
11241out:
11242 return err;
11243}
Andrei Vagin0854fa82021-04-06 23:40:51 -070011244EXPORT_SYMBOL_GPL(__dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011245
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011246static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011247{
11248 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011249 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011250 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053011251 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011252
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253 local_irq_disable();
11254 cpu = smp_processor_id();
11255 sd = &per_cpu(softnet_data, cpu);
11256 oldsd = &per_cpu(softnet_data, oldcpu);
11257
11258 /* Find end of our completion_queue. */
11259 list_skb = &sd->completion_queue;
11260 while (*list_skb)
11261 list_skb = &(*list_skb)->next;
11262 /* Append completion queue from offline CPU. */
11263 *list_skb = oldsd->completion_queue;
11264 oldsd->completion_queue = NULL;
11265
Linus Torvalds1da177e2005-04-16 15:20:36 -070011266 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000011267 if (oldsd->output_queue) {
11268 *sd->output_queue_tailp = oldsd->output_queue;
11269 sd->output_queue_tailp = oldsd->output_queue_tailp;
11270 oldsd->output_queue = NULL;
11271 oldsd->output_queue_tailp = &oldsd->output_queue;
11272 }
Eric Dumazetac64da02015-01-15 17:04:22 -080011273 /* Append NAPI poll list from offline CPU, with one exception :
11274 * process_backlog() must be called by cpu owning percpu backlog.
11275 * We properly handle process_queue & input_pkt_queue later.
11276 */
11277 while (!list_empty(&oldsd->poll_list)) {
11278 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
11279 struct napi_struct,
11280 poll_list);
11281
11282 list_del_init(&napi->poll_list);
11283 if (napi->poll == process_backlog)
11284 napi->state = 0;
11285 else
11286 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000011287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011288
11289 raise_softirq_irqoff(NET_TX_SOFTIRQ);
11290 local_irq_enable();
11291
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053011292#ifdef CONFIG_RPS
11293 remsd = oldsd->rps_ipi_list;
11294 oldsd->rps_ipi_list = NULL;
11295#endif
11296 /* send out pending IPI's on offline CPU */
11297 net_rps_send_ipi(remsd);
11298
Linus Torvalds1da177e2005-04-16 15:20:36 -070011299 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000011300 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080011301 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000011302 input_queue_head_incr(oldsd);
11303 }
Eric Dumazetac64da02015-01-15 17:04:22 -080011304 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080011305 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000011306 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070011307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011308
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011310}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011311
Herbert Xu7f353bf2007-08-10 15:47:58 -070011312/**
Herbert Xub63365a2008-10-23 01:11:29 -070011313 * netdev_increment_features - increment feature set by one
11314 * @all: current feature set
11315 * @one: new feature set
11316 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070011317 *
11318 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070011319 * @one to the master device with current feature set @all. Will not
11320 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070011321 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000011322netdev_features_t netdev_increment_features(netdev_features_t all,
11323 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070011324{
Tom Herbertc8cd0982015-12-14 11:19:44 -080011325 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080011326 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000011327 mask |= NETIF_F_VLAN_CHALLENGED;
11328
Tom Herberta1882222015-12-14 11:19:43 -080011329 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000011330 all &= one | ~NETIF_F_ALL_FOR_ALL;
11331
Michał Mirosław1742f182011-04-22 06:31:16 +000011332 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080011333 if (all & NETIF_F_HW_CSUM)
11334 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070011335
11336 return all;
11337}
Herbert Xub63365a2008-10-23 01:11:29 -070011338EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070011339
Baruch Siach430f03c2013-06-02 20:43:55 +000011340static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011341{
11342 int i;
11343 struct hlist_head *hash;
11344
Kees Cook6da2ec52018-06-12 13:55:00 -070011345 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011346 if (hash != NULL)
11347 for (i = 0; i < NETDEV_HASHENTRIES; i++)
11348 INIT_HLIST_HEAD(&hash[i]);
11349
11350 return hash;
11351}
11352
Eric W. Biederman881d9662007-09-17 11:56:21 -070011353/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070011354static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011355{
Li RongQingd9f37d02018-07-13 14:41:36 +080011356 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080011357 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080011358
Rustad, Mark D734b6542012-07-18 09:06:07 +000011359 if (net != &init_net)
11360 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070011361
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011362 net->dev_name_head = netdev_create_hash();
11363 if (net->dev_name_head == NULL)
11364 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011365
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011366 net->dev_index_head = netdev_create_hash();
11367 if (net->dev_index_head == NULL)
11368 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011369
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020011370 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
11371
Eric W. Biederman881d9662007-09-17 11:56:21 -070011372 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011373
11374err_idx:
11375 kfree(net->dev_name_head);
11376err_name:
11377 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011378}
11379
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011380/**
11381 * netdev_drivername - network driver for the device
11382 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011383 *
11384 * Determine network driver for device.
11385 */
David S. Miller3019de12011-06-06 16:41:33 -070011386const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070011387{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070011388 const struct device_driver *driver;
11389 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070011390 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070011391
11392 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011393 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070011394 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011395
11396 driver = parent->driver;
11397 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070011398 return driver->name;
11399 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011400}
11401
Joe Perches6ea754e2014-09-22 11:10:50 -070011402static void __netdev_printk(const char *level, const struct net_device *dev,
11403 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000011404{
Joe Perchesb004ff42012-09-12 20:12:19 -070011405 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011406 dev_printk_emit(level[1] - '0',
11407 dev->dev.parent,
11408 "%s %s %s%s: %pV",
11409 dev_driver_string(dev->dev.parent),
11410 dev_name(dev->dev.parent),
11411 netdev_name(dev), netdev_reg_state(dev),
11412 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011413 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011414 printk("%s%s%s: %pV",
11415 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011416 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070011417 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011418 }
Joe Perches256df2f2010-06-27 01:02:35 +000011419}
11420
Joe Perches6ea754e2014-09-22 11:10:50 -070011421void netdev_printk(const char *level, const struct net_device *dev,
11422 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000011423{
11424 struct va_format vaf;
11425 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000011426
11427 va_start(args, format);
11428
11429 vaf.fmt = format;
11430 vaf.va = &args;
11431
Joe Perches6ea754e2014-09-22 11:10:50 -070011432 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011433
Joe Perches256df2f2010-06-27 01:02:35 +000011434 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000011435}
11436EXPORT_SYMBOL(netdev_printk);
11437
11438#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070011439void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000011440{ \
Joe Perches256df2f2010-06-27 01:02:35 +000011441 struct va_format vaf; \
11442 va_list args; \
11443 \
11444 va_start(args, fmt); \
11445 \
11446 vaf.fmt = fmt; \
11447 vaf.va = &args; \
11448 \
Joe Perches6ea754e2014-09-22 11:10:50 -070011449 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070011450 \
Joe Perches256df2f2010-06-27 01:02:35 +000011451 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000011452} \
11453EXPORT_SYMBOL(func);
11454
11455define_netdev_printk_level(netdev_emerg, KERN_EMERG);
11456define_netdev_printk_level(netdev_alert, KERN_ALERT);
11457define_netdev_printk_level(netdev_crit, KERN_CRIT);
11458define_netdev_printk_level(netdev_err, KERN_ERR);
11459define_netdev_printk_level(netdev_warn, KERN_WARNING);
11460define_netdev_printk_level(netdev_notice, KERN_NOTICE);
11461define_netdev_printk_level(netdev_info, KERN_INFO);
11462
Pavel Emelyanov46650792007-10-08 20:38:39 -070011463static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011464{
11465 kfree(net->dev_name_head);
11466 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030011467 if (net != &init_net)
11468 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070011469}
11470
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011471static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070011472 .init = netdev_init,
11473 .exit = netdev_exit,
11474};
11475
Pavel Emelyanov46650792007-10-08 20:38:39 -070011476static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011477{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011478 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011479 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011480 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020011481 * initial network namespace
11482 */
11483 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011484 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011485 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011486 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020011487
11488 /* Ignore unmoveable devices (i.e. loopback) */
11489 if (dev->features & NETIF_F_NETNS_LOCAL)
11490 continue;
11491
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011492 /* Leave virtual devices for the generic cleanup */
Martin Willi3a5ca852021-03-02 13:24:23 +010011493 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011494 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080011495
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011496 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011497 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020011498 if (__dev_get_by_name(&init_net, fb_name))
11499 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Andrei Vagin0854fa82021-04-06 23:40:51 -070011500 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011501 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000011502 pr_emerg("%s: failed to move %s to init_net: %d\n",
11503 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011504 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020011505 }
11506 }
11507 rtnl_unlock();
11508}
11509
Eric W. Biederman50624c92013-09-23 21:19:49 -070011510static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
11511{
11512 /* Return with the rtnl_lock held when there are no network
11513 * devices unregistering in any network namespace in net_list.
11514 */
11515 struct net *net;
11516 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010011517 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011518
Peter Zijlstraff960a72014-10-29 17:04:56 +010011519 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011520 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070011521 unregistering = false;
11522 rtnl_lock();
11523 list_for_each_entry(net, net_list, exit_list) {
11524 if (net->dev_unreg_count > 0) {
11525 unregistering = true;
11526 break;
11527 }
11528 }
11529 if (!unregistering)
11530 break;
11531 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010011532
11533 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011534 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010011535 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011536}
11537
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011538static void __net_exit default_device_exit_batch(struct list_head *net_list)
11539{
11540 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011541 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011542 * Do this across as many network namespaces as possible to
11543 * improve batching efficiency.
11544 */
11545 struct net_device *dev;
11546 struct net *net;
11547 LIST_HEAD(dev_kill_list);
11548
Eric W. Biederman50624c92013-09-23 21:19:49 -070011549 /* To prevent network device cleanup code from dereferencing
11550 * loopback devices or network devices that have been freed
11551 * wait here for all pending unregistrations to complete,
11552 * before unregistring the loopback device and allowing the
11553 * network namespace be freed.
11554 *
11555 * The netdev todo list containing all network devices
11556 * unregistrations that happen in default_device_exit_batch
11557 * will run in the rtnl_unlock() at the end of
11558 * default_device_exit_batch.
11559 */
11560 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011561 list_for_each_entry(net, net_list, exit_list) {
11562 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020011563 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011564 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
11565 else
11566 unregister_netdevice_queue(dev, &dev_kill_list);
11567 }
11568 }
11569 unregister_netdevice_many(&dev_kill_list);
11570 rtnl_unlock();
11571}
11572
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011573static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011574 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011575 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020011576};
11577
Linus Torvalds1da177e2005-04-16 15:20:36 -070011578/*
11579 * Initialize the DEV module. At boot time this walks the device list and
11580 * unhooks any devices that fail to initialise (normally hardware not
11581 * present) and leaves us with a valid list of present and active devices.
11582 *
11583 */
11584
11585/*
11586 * This is called single threaded during boot, so no need
11587 * to take the rtnl semaphore.
11588 */
11589static int __init net_dev_init(void)
11590{
11591 int i, rc = -ENOMEM;
11592
11593 BUG_ON(!dev_boot_phase);
11594
Linus Torvalds1da177e2005-04-16 15:20:36 -070011595 if (dev_proc_init())
11596 goto out;
11597
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011598 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070011599 goto out;
11600
11601 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080011602 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011603 INIT_LIST_HEAD(&ptype_base[i]);
11604
Vlad Yasevich62532da2012-11-15 08:49:10 +000011605 INIT_LIST_HEAD(&offload_base);
11606
Eric W. Biederman881d9662007-09-17 11:56:21 -070011607 if (register_pernet_subsys(&netdev_net_ops))
11608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609
11610 /*
11611 * Initialise the packet receive queues.
11612 */
11613
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070011614 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070011615 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011616 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617
Eric Dumazet41852492016-08-26 12:50:39 -070011618 INIT_WORK(flush, flush_backlog);
11619
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011620 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070011621 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010011622#ifdef CONFIG_XFRM_OFFLOAD
11623 skb_queue_head_init(&sd->xfrm_backlog);
11624#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011625 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000011626 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000011627#ifdef CONFIG_RPS
Peter Zijlstra545b8c82020-06-15 11:29:31 +020011628 INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011629 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070011630#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000011631
David S. Miller7c4ec742018-07-20 23:37:55 -070011632 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011633 sd->backlog.poll = process_backlog;
11634 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635 }
11636
Linus Torvalds1da177e2005-04-16 15:20:36 -070011637 dev_boot_phase = 0;
11638
Eric W. Biederman505d4f72008-11-07 22:54:20 -080011639 /* The loopback device is special if any other network devices
11640 * is present in a network namespace the loopback device must
11641 * be present. Since we now dynamically allocate and free the
11642 * loopback device ensure this invariant is maintained by
11643 * keeping the loopback device as the first device on the
11644 * list of network devices. Ensuring the loopback devices
11645 * is the first device that appears and the last network device
11646 * that disappears.
11647 */
11648 if (register_pernet_device(&loopback_net_ops))
11649 goto out;
11650
11651 if (register_pernet_device(&default_device_ops))
11652 goto out;
11653
Carlos R. Mafra962cf362008-05-15 11:15:37 -030011654 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11655 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011657 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11658 NULL, dev_cpu_dead);
11659 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011660 rc = 0;
11661out:
11662 return rc;
11663}
11664
11665subsys_initcall(net_dev_init);