blob: b9d19fbb15890d0cdb6d79ede9fe2163cb92188d [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>
Brenden Blancoa7862b42016-07-19 12:16:48 -070094#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040095#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020096#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -080098#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/stat.h>
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +0300101#include <net/dsa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700103#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200105#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000107#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/highmem.h>
109#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500116#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700117#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700118#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700119#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700120#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700121#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700122#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700123#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700124#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700125#include <linux/ipv6.h>
126#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700127#include <linux/jhash.h>
128#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700129#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900130#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900131#include <trace/events/skb.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700132#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000133#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100134#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300135#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700136#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100137#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400138#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800139#include <linux/hrtimer.h>
Daniel Borkmann357b6cc2020-03-18 10:33:22 +0100140#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530141#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200142#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200143#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200144#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100145#include <linux/indirect_call_wrapper.h>
Jiri Pirkoaf3836d2019-03-28 13:56:37 +0100146#include <net/devlink.h>
Heiner Kallweitbd869242020-06-20 22:35:42 +0200147#include <linux/pm_runtime.h>
Willy Tarreau37447412020-08-10 10:27:42 +0200148#include <linux/prandom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700150#include "net-sysfs.h"
151
Herbert Xud565b0a2008-12-15 23:38:52 -0800152#define MAX_GRO_SKBS 8
153
Herbert Xu5d38a072009-01-04 16:13:40 -0800154/* This should be increased if a protocol with a bigger head is added. */
155#define GRO_MAX_HEAD (MAX_HEADER + 128)
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000158static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000159struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
160struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000161static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000163static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700164static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700165 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000166static int call_netdevice_notifiers_extack(unsigned long val,
167 struct net_device *dev,
168 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200169static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700172 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * semaphore.
174 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800175 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700178 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * actual updates. This allows pure readers to access the list even
180 * while a writer is preparing to update it.
181 *
182 * To put it another way, dev_base_lock is held for writing only to
183 * protect against pure readers; the rtnl semaphore provides the
184 * protection against other writers.
185 *
186 * See, for example usages, register_netdevice() and
187 * unregister_netdevice(), which must be called with the rtnl
188 * semaphore held.
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191EXPORT_SYMBOL(dev_base_lock);
192
Florian Westphal6c557002017-10-02 23:50:05 +0200193static DEFINE_MUTEX(ifalias_mutex);
194
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300195/* protects napi_hash addition/deletion and napi_gen_id */
196static DEFINE_SPINLOCK(napi_hash_lock);
197
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800198static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800199static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300200
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200201static DECLARE_RWSEM(devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000202
Thomas Graf4e985ad2011-06-21 03:11:20 +0000203static inline void dev_base_seq_inc(struct net *net)
204{
tcharding643aa9c2017-02-09 17:56:05 +1100205 while (++net->dev_base_seq == 0)
206 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000207}
208
Eric W. Biederman881d9662007-09-17 11:56:21 -0700209static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700211 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000212
stephen hemminger08e98972009-11-10 07:20:34 +0000213 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Eric W. Biederman881d9662007-09-17 11:56:21 -0700216static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700218 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000222{
223#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000224 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000225#endif
226}
227
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000228static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000229{
230#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000231 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000232#endif
233}
234
Jiri Pirkoff927412019-09-30 11:48:15 +0200235static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
236 const char *name)
237{
238 struct netdev_name_node *name_node;
239
240 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
241 if (!name_node)
242 return NULL;
243 INIT_HLIST_NODE(&name_node->hlist);
244 name_node->dev = dev;
245 name_node->name = name;
246 return name_node;
247}
248
249static struct netdev_name_node *
250netdev_name_node_head_alloc(struct net_device *dev)
251{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200252 struct netdev_name_node *name_node;
253
254 name_node = netdev_name_node_alloc(dev, dev->name);
255 if (!name_node)
256 return NULL;
257 INIT_LIST_HEAD(&name_node->list);
258 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200259}
260
261static void netdev_name_node_free(struct netdev_name_node *name_node)
262{
263 kfree(name_node);
264}
265
266static void netdev_name_node_add(struct net *net,
267 struct netdev_name_node *name_node)
268{
269 hlist_add_head_rcu(&name_node->hlist,
270 dev_name_hash(net, name_node->name));
271}
272
273static void netdev_name_node_del(struct netdev_name_node *name_node)
274{
275 hlist_del_rcu(&name_node->hlist);
276}
277
278static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
279 const char *name)
280{
281 struct hlist_head *head = dev_name_hash(net, name);
282 struct netdev_name_node *name_node;
283
284 hlist_for_each_entry(name_node, head, hlist)
285 if (!strcmp(name_node->name, name))
286 return name_node;
287 return NULL;
288}
289
290static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
291 const char *name)
292{
293 struct hlist_head *head = dev_name_hash(net, name);
294 struct netdev_name_node *name_node;
295
296 hlist_for_each_entry_rcu(name_node, head, hlist)
297 if (!strcmp(name_node->name, name))
298 return name_node;
299 return NULL;
300}
301
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200302int netdev_name_node_alt_create(struct net_device *dev, const char *name)
303{
304 struct netdev_name_node *name_node;
305 struct net *net = dev_net(dev);
306
307 name_node = netdev_name_node_lookup(net, name);
308 if (name_node)
309 return -EEXIST;
310 name_node = netdev_name_node_alloc(dev, name);
311 if (!name_node)
312 return -ENOMEM;
313 netdev_name_node_add(net, name_node);
314 /* The node that holds dev->name acts as a head of per-device list. */
315 list_add_tail(&name_node->list, &dev->name_node->list);
316
317 return 0;
318}
319EXPORT_SYMBOL(netdev_name_node_alt_create);
320
321static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
322{
323 list_del(&name_node->list);
324 netdev_name_node_del(name_node);
325 kfree(name_node->name);
326 netdev_name_node_free(name_node);
327}
328
329int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
330{
331 struct netdev_name_node *name_node;
332 struct net *net = dev_net(dev);
333
334 name_node = netdev_name_node_lookup(net, name);
335 if (!name_node)
336 return -ENOENT;
Eric Dumazete08ad802020-02-14 07:53:53 -0800337 /* lookup might have found our primary name or a name belonging
338 * to another device.
339 */
340 if (name_node == dev->name_node || name_node->dev != dev)
341 return -EINVAL;
342
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200343 __netdev_name_node_alt_destroy(name_node);
344
345 return 0;
346}
347EXPORT_SYMBOL(netdev_name_node_alt_destroy);
348
349static void netdev_name_node_alt_flush(struct net_device *dev)
350{
351 struct netdev_name_node *name_node, *tmp;
352
353 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
354 __netdev_name_node_alt_destroy(name_node);
355}
356
Eric W. Biedermance286d32007-09-12 13:53:49 +0200357/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000358static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200359{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900360 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200361
362 ASSERT_RTNL();
363
364 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800365 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Jiri Pirkoff927412019-09-30 11:48:15 +0200366 netdev_name_node_add(net, dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000367 hlist_add_head_rcu(&dev->index_hlist,
368 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200369 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000370
371 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200372}
373
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000374/* Device list removal
375 * caller must respect a RCU grace period before freeing/reusing dev
376 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200377static void unlist_netdevice(struct net_device *dev)
378{
379 ASSERT_RTNL();
380
381 /* Unlink dev from the device chain */
382 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800383 list_del_rcu(&dev->dev_list);
Jiri Pirkoff927412019-09-30 11:48:15 +0200384 netdev_name_node_del(dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000385 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200386 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000387
388 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * Our notifier list
393 */
394
Alan Sternf07d5b92006-05-09 15:23:03 -0700395static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
398 * Device drivers call our routines to queue packets here. We empty the
399 * queue in the local softnet handler.
400 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700401
Eric Dumazet9958da02010-04-17 04:17:02 +0000402DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700403EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Cong Wang1a33e102020-05-02 22:22:19 -0700405#ifdef CONFIG_LOCKDEP
406/*
407 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
408 * according to dev->type
409 */
410static const unsigned short netdev_lock_type[] = {
411 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
412 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
413 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
414 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
415 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
416 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
417 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
418 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
419 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
420 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
421 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
422 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
423 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
424 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
425 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
426
427static const char *const netdev_lock_name[] = {
428 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
429 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
430 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
431 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
432 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
433 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
434 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
435 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
436 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
437 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
438 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
439 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
440 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
441 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
442 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
443
444static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang845e0eb2020-06-08 14:53:01 -0700445static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang1a33e102020-05-02 22:22:19 -0700446
447static inline unsigned short netdev_lock_pos(unsigned short dev_type)
448{
449 int i;
450
451 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
452 if (netdev_lock_type[i] == dev_type)
453 return i;
454 /* the last key is used by default */
455 return ARRAY_SIZE(netdev_lock_type) - 1;
456}
457
458static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
459 unsigned short dev_type)
460{
461 int i;
462
463 i = netdev_lock_pos(dev_type);
464 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
465 netdev_lock_name[i]);
466}
Cong Wang845e0eb2020-06-08 14:53:01 -0700467
468static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
469{
470 int i;
471
472 i = netdev_lock_pos(dev->type);
473 lockdep_set_class_and_name(&dev->addr_list_lock,
474 &netdev_addr_lock_key[i],
475 netdev_lock_name[i]);
476}
Cong Wang1a33e102020-05-02 22:22:19 -0700477#else
478static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
479 unsigned short dev_type)
480{
481}
Cong Wang845e0eb2020-06-08 14:53:01 -0700482
483static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
484{
485}
Cong Wang1a33e102020-05-02 22:22:19 -0700486#endif
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100489 *
490 * Protocol management and registration routines
491 *
492 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Add a protocol ID to the list. Now that the input handler is
497 * smarter we can dispense with all the messy stuff that used to be
498 * here.
499 *
500 * BEWARE!!! Protocol handlers, mangling input packets,
501 * MUST BE last in hash buckets and checking protocol handlers
502 * MUST start from promiscuous ptype_all chain in net_bh.
503 * It is true now, do not change it.
504 * Explanation follows: if protocol handler, mangling packet, will
505 * be the first on list, it is not able to sense, that packet
506 * is cloned and should be copied-on-write, so that it will
507 * change it and subsequent readers will get broken packet.
508 * --ANK (980803)
509 */
510
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000511static inline struct list_head *ptype_head(const struct packet_type *pt)
512{
513 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800514 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000515 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800516 return pt->dev ? &pt->dev->ptype_specific :
517 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * dev_add_pack - add packet handler
522 * @pt: packet type declaration
523 *
524 * Add a protocol handler to the networking stack. The passed &packet_type
525 * is linked into kernel lists and may not be freed until it has been
526 * removed from the kernel lists.
527 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900528 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * guarantee all CPU's that are in middle of receiving packets
530 * will see the new packet type (until the next received packet).
531 */
532
533void dev_add_pack(struct packet_type *pt)
534{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000535 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000537 spin_lock(&ptype_lock);
538 list_add_rcu(&pt->list, head);
539 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700541EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
544 * __dev_remove_pack - remove packet handler
545 * @pt: packet type declaration
546 *
547 * Remove a protocol handler that was previously added to the kernel
548 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
549 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900550 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
552 * The packet type might still be in use by receivers
553 * and must not be freed until after all the CPU's have gone
554 * through a quiescent state.
555 */
556void __dev_remove_pack(struct packet_type *pt)
557{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000558 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct packet_type *pt1;
560
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000561 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 list_for_each_entry(pt1, head, list) {
564 if (pt == pt1) {
565 list_del_rcu(&pt->list);
566 goto out;
567 }
568 }
569
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000570 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000572 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700574EXPORT_SYMBOL(__dev_remove_pack);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**
577 * dev_remove_pack - remove packet handler
578 * @pt: packet type declaration
579 *
580 * Remove a protocol handler that was previously added to the kernel
581 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
582 * from the kernel lists and can be freed or reused once this function
583 * returns.
584 *
585 * This call sleeps to guarantee that no CPU is looking at the packet
586 * type after return.
587 */
588void dev_remove_pack(struct packet_type *pt)
589{
590 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 synchronize_net();
593}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700594EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Vlad Yasevich62532da2012-11-15 08:49:10 +0000596
597/**
598 * dev_add_offload - register offload handlers
599 * @po: protocol offload declaration
600 *
601 * Add protocol offload handlers to the networking stack. The passed
602 * &proto_offload is linked into kernel lists and may not be freed until
603 * it has been removed from the kernel lists.
604 *
605 * This call does not sleep therefore it can not
606 * guarantee all CPU's that are in middle of receiving packets
607 * will see the new offload handlers (until the next received packet).
608 */
609void dev_add_offload(struct packet_offload *po)
610{
David S. Millerbdef7de2015-06-01 14:56:09 -0700611 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000612
613 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700614 list_for_each_entry(elem, &offload_base, list) {
615 if (po->priority < elem->priority)
616 break;
617 }
618 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000619 spin_unlock(&offload_lock);
620}
621EXPORT_SYMBOL(dev_add_offload);
622
623/**
624 * __dev_remove_offload - remove offload handler
625 * @po: packet offload declaration
626 *
627 * Remove a protocol offload handler that was previously added to the
628 * kernel offload handlers by dev_add_offload(). The passed &offload_type
629 * is removed from the kernel lists and can be freed or reused once this
630 * function returns.
631 *
632 * The packet type might still be in use by receivers
633 * and must not be freed until after all the CPU's have gone
634 * through a quiescent state.
635 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800636static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000637{
638 struct list_head *head = &offload_base;
639 struct packet_offload *po1;
640
Eric Dumazetc53aa502012-11-16 08:08:23 +0000641 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000642
643 list_for_each_entry(po1, head, list) {
644 if (po == po1) {
645 list_del_rcu(&po->list);
646 goto out;
647 }
648 }
649
650 pr_warn("dev_remove_offload: %p not found\n", po);
651out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000652 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000653}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000654
655/**
656 * dev_remove_offload - remove packet offload handler
657 * @po: packet offload declaration
658 *
659 * Remove a packet offload handler that was previously added to the kernel
660 * offload handlers by dev_add_offload(). The passed &offload_type is
661 * removed from the kernel lists and can be freed or reused once this
662 * function returns.
663 *
664 * This call sleeps to guarantee that no CPU is looking at the packet
665 * type after return.
666 */
667void dev_remove_offload(struct packet_offload *po)
668{
669 __dev_remove_offload(po);
670
671 synchronize_net();
672}
673EXPORT_SYMBOL(dev_remove_offload);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100676 *
677 * Device Boot-time Settings Routines
678 *
679 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/* Boot time configuration table */
682static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
683
684/**
685 * netdev_boot_setup_add - add new setup entry
686 * @name: name of the device
687 * @map: configured settings for the device
688 *
689 * Adds new setup entry to the dev_boot_setup list. The function
690 * returns 0 on error and 1 on success. This is a generic routine to
691 * all netdevices.
692 */
693static int netdev_boot_setup_add(char *name, struct ifmap *map)
694{
695 struct netdev_boot_setup *s;
696 int i;
697
698 s = dev_boot_setup;
699 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
700 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
701 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700702 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 memcpy(&s[i].map, map, sizeof(s[i].map));
704 break;
705 }
706 }
707
708 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
709}
710
711/**
tcharding722c9a02017-02-09 17:56:04 +1100712 * netdev_boot_setup_check - check boot time settings
713 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *
tcharding722c9a02017-02-09 17:56:04 +1100715 * Check boot time settings for the device.
716 * The found settings are set for the device to be used
717 * later in the device probing.
718 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720int netdev_boot_setup_check(struct net_device *dev)
721{
722 struct netdev_boot_setup *s = dev_boot_setup;
723 int i;
724
725 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
726 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700727 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100728 dev->irq = s[i].map.irq;
729 dev->base_addr = s[i].map.base_addr;
730 dev->mem_start = s[i].map.mem_start;
731 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 1;
733 }
734 }
735 return 0;
736}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700737EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739
740/**
tcharding722c9a02017-02-09 17:56:04 +1100741 * netdev_boot_base - get address from boot time settings
742 * @prefix: prefix for network device
743 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
tcharding722c9a02017-02-09 17:56:04 +1100745 * Check boot time settings for the base address of device.
746 * The found settings are set for the device to be used
747 * later in the device probing.
748 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750unsigned long netdev_boot_base(const char *prefix, int unit)
751{
752 const struct netdev_boot_setup *s = dev_boot_setup;
753 char name[IFNAMSIZ];
754 int i;
755
756 sprintf(name, "%s%d", prefix, unit);
757
758 /*
759 * If device already registered then return base of 1
760 * to indicate not to probe for this interface
761 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700762 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 1;
764
765 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
766 if (!strcmp(name, s[i].name))
767 return s[i].map.base_addr;
768 return 0;
769}
770
771/*
772 * Saves at boot time configured settings for any netdevice.
773 */
774int __init netdev_boot_setup(char *str)
775{
776 int ints[5];
777 struct ifmap map;
778
779 str = get_options(str, ARRAY_SIZE(ints), ints);
780 if (!str || !*str)
781 return 0;
782
783 /* Save settings */
784 memset(&map, 0, sizeof(map));
785 if (ints[0] > 0)
786 map.irq = ints[1];
787 if (ints[0] > 1)
788 map.base_addr = ints[2];
789 if (ints[0] > 2)
790 map.mem_start = ints[3];
791 if (ints[0] > 3)
792 map.mem_end = ints[4];
793
794 /* Add new entry to the list */
795 return netdev_boot_setup_add(str, &map);
796}
797
798__setup("netdev=", netdev_boot_setup);
799
800/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100801 *
802 * Device Interface Subroutines
803 *
804 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200807 * dev_get_iflink - get 'iflink' value of a interface
808 * @dev: targeted interface
809 *
810 * Indicates the ifindex the interface is linked to.
811 * Physical interfaces have the same 'ifindex' and 'iflink' values.
812 */
813
814int dev_get_iflink(const struct net_device *dev)
815{
816 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
817 return dev->netdev_ops->ndo_get_iflink(dev);
818
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200819 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200820}
821EXPORT_SYMBOL(dev_get_iflink);
822
823/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700824 * dev_fill_metadata_dst - Retrieve tunnel egress information.
825 * @dev: targeted interface
826 * @skb: The packet.
827 *
828 * For better visibility of tunnel traffic OVS needs to retrieve
829 * egress tunnel information for a packet. Following API allows
830 * user to get this info.
831 */
832int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
833{
834 struct ip_tunnel_info *info;
835
836 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
837 return -EINVAL;
838
839 info = skb_tunnel_info_unclone(skb);
840 if (!info)
841 return -ENOMEM;
842 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
843 return -EINVAL;
844
845 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
846}
847EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
848
849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700851 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * @name: name to find
853 *
854 * Find an interface by name. Must be called under RTNL semaphore
855 * or @dev_base_lock. If the name is found a pointer to the device
856 * is returned. If the name is not found then %NULL is returned. The
857 * reference counters are not incremented so the caller must be
858 * careful with locks.
859 */
860
Eric W. Biederman881d9662007-09-17 11:56:21 -0700861struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jiri Pirkoff927412019-09-30 11:48:15 +0200863 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jiri Pirkoff927412019-09-30 11:48:15 +0200865 node_name = netdev_name_node_lookup(net, name);
866 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700868EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870/**
tcharding722c9a02017-02-09 17:56:04 +1100871 * dev_get_by_name_rcu - find a device by its name
872 * @net: the applicable net namespace
873 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000874 *
tcharding722c9a02017-02-09 17:56:04 +1100875 * Find an interface by name.
876 * If the name is found a pointer to the device is returned.
877 * If the name is not found then %NULL is returned.
878 * The reference counters are not incremented so the caller must be
879 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000880 */
881
882struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
883{
Jiri Pirkoff927412019-09-30 11:48:15 +0200884 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000885
Jiri Pirkoff927412019-09-30 11:48:15 +0200886 node_name = netdev_name_node_lookup_rcu(net, name);
887 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000888}
889EXPORT_SYMBOL(dev_get_by_name_rcu);
890
891/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700893 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * @name: name to find
895 *
896 * Find an interface by name. This can be called from any
897 * context and does its own locking. The returned handle has
898 * the usage count incremented and the caller must use dev_put() to
899 * release it when it is no longer needed. %NULL is returned if no
900 * matching device is found.
901 */
902
Eric W. Biederman881d9662007-09-17 11:56:21 -0700903struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct net_device *dev;
906
Eric Dumazet72c95282009-10-30 07:11:27 +0000907 rcu_read_lock();
908 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (dev)
910 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000911 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return dev;
913}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700914EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916/**
917 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700918 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * @ifindex: index of device
920 *
921 * Search for an interface by index. Returns %NULL if the device
922 * is not found or a pointer to the device. The device has not
923 * had its reference counter increased so the caller must be careful
924 * about locking. The caller must hold either the RTNL semaphore
925 * or @dev_base_lock.
926 */
927
Eric W. Biederman881d9662007-09-17 11:56:21 -0700928struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700930 struct net_device *dev;
931 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Sasha Levinb67bfe02013-02-27 17:06:00 -0800933 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (dev->ifindex == ifindex)
935 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return NULL;
938}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700939EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000941/**
942 * dev_get_by_index_rcu - find a device by its ifindex
943 * @net: the applicable net namespace
944 * @ifindex: index of device
945 *
946 * Search for an interface by index. Returns %NULL if the device
947 * is not found or a pointer to the device. The device has not
948 * had its reference counter increased so the caller must be careful
949 * about locking. The caller must hold RCU lock.
950 */
951
952struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
953{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000954 struct net_device *dev;
955 struct hlist_head *head = dev_index_hash(net, ifindex);
956
Sasha Levinb67bfe02013-02-27 17:06:00 -0800957 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000958 if (dev->ifindex == ifindex)
959 return dev;
960
961 return NULL;
962}
963EXPORT_SYMBOL(dev_get_by_index_rcu);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966/**
967 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700968 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * @ifindex: index of device
970 *
971 * Search for an interface by index. Returns NULL if the device
972 * is not found or a pointer to the device. The device returned has
973 * had a reference added and the pointer is safe until the user calls
974 * dev_put to indicate they have finished with it.
975 */
976
Eric W. Biederman881d9662007-09-17 11:56:21 -0700977struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct net_device *dev;
980
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000981 rcu_read_lock();
982 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (dev)
984 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000985 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return dev;
987}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700988EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200991 * dev_get_by_napi_id - find a device by napi_id
992 * @napi_id: ID of the NAPI struct
993 *
994 * Search for an interface by NAPI ID. Returns %NULL if the device
995 * is not found or a pointer to the device. The device has not had
996 * its reference counter increased so the caller must be careful
997 * about locking. The caller must hold RCU lock.
998 */
999
1000struct net_device *dev_get_by_napi_id(unsigned int napi_id)
1001{
1002 struct napi_struct *napi;
1003
1004 WARN_ON_ONCE(!rcu_read_lock_held());
1005
1006 if (napi_id < MIN_NAPI_ID)
1007 return NULL;
1008
1009 napi = napi_by_id(napi_id);
1010
1011 return napi ? napi->dev : NULL;
1012}
1013EXPORT_SYMBOL(dev_get_by_napi_id);
1014
1015/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001016 * netdev_get_name - get a netdevice name, knowing its ifindex.
1017 * @net: network namespace
1018 * @name: a pointer to the buffer where the name will be stored.
1019 * @ifindex: the ifindex of the interface to get the name from.
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001020 */
1021int netdev_get_name(struct net *net, char *name, int ifindex)
1022{
1023 struct net_device *dev;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001024 int ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001025
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001026 down_read(&devnet_rename_sem);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001027 rcu_read_lock();
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001028
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001029 dev = dev_get_by_index_rcu(net, ifindex);
1030 if (!dev) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001031 ret = -ENODEV;
1032 goto out;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001033 }
1034
1035 strcpy(name, dev->name);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001036
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001037 ret = 0;
1038out:
1039 rcu_read_unlock();
1040 up_read(&devnet_rename_sem);
1041 return ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001042}
1043
1044/**
Eric Dumazet941666c2010-12-05 01:23:53 +00001045 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001046 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @type: media type of device
1048 * @ha: hardware address
1049 *
1050 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -08001051 * is not found or a pointer to the device.
1052 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +00001053 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * and the caller must therefore be careful about locking
1055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057
Eric Dumazet941666c2010-12-05 01:23:53 +00001058struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
1059 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct net_device *dev;
1062
Eric Dumazet941666c2010-12-05 01:23:53 +00001063 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (dev->type == type &&
1065 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -07001066 return dev;
1067
1068 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
Eric Dumazet941666c2010-12-05 01:23:53 +00001070EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -03001071
Eric W. Biederman881d9662007-09-17 11:56:21 -07001072struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001073{
1074 struct net_device *dev;
1075
1076 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -07001077 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001078 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001079 return dev;
1080
1081 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001082}
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001083EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1084
Eric W. Biederman881d9662007-09-17 11:56:21 -07001085struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001087 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001089 rcu_read_lock();
1090 for_each_netdev_rcu(net, dev)
1091 if (dev->type == type) {
1092 dev_hold(dev);
1093 ret = dev;
1094 break;
1095 }
1096 rcu_read_unlock();
1097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099EXPORT_SYMBOL(dev_getfirstbyhwtype);
1100
1101/**
WANG Cong6c555492014-09-11 15:35:09 -07001102 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001103 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * @if_flags: IFF_* values
1105 * @mask: bitmask of bits in if_flags to check
1106 *
1107 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001108 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001109 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111
WANG Cong6c555492014-09-11 15:35:09 -07001112struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1113 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001115 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
WANG Cong6c555492014-09-11 15:35:09 -07001117 ASSERT_RTNL();
1118
Pavel Emelianov7562f872007-05-03 15:13:45 -07001119 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001120 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001122 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 }
1125 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
WANG Cong6c555492014-09-11 15:35:09 -07001128EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/**
1131 * dev_valid_name - check if name is okay for network device
1132 * @name: name string
1133 *
1134 * Network device names need to be valid file names to
Randy Dunlap4250b752020-09-17 21:35:15 -07001135 * allow sysfs to work. We also disallow any kind of
David S. Millerc7fa9d12006-08-15 16:34:13 -07001136 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
David S. Miller95f050b2012-03-06 16:12:15 -05001138bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001140 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001141 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001142 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001143 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001144 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001145 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001146
1147 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001148 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001149 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001150 name++;
1151 }
David S. Miller95f050b2012-03-06 16:12:15 -05001152 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001154EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001157 * __dev_alloc_name - allocate a name for a device
1158 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001160 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
1162 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001163 * id. It scans list of devices to build up a free map, then chooses
1164 * the first empty slot. The caller must hold the dev_base or rtnl lock
1165 * while allocating the name and adding the device in order to avoid
1166 * duplicates.
1167 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1168 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001171static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 const char *p;
1175 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001176 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct net_device *d;
1178
Rasmus Villemoes93809102017-11-13 00:15:08 +01001179 if (!dev_valid_name(name))
1180 return -EINVAL;
1181
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001182 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (p) {
1184 /*
1185 * Verify the string as this thing may have come from
1186 * the user. There must be either one "%d" and no other "%"
1187 * characters.
1188 */
1189 if (p[1] != 'd' || strchr(p + 2, '%'))
1190 return -EINVAL;
1191
1192 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001193 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!inuse)
1195 return -ENOMEM;
1196
Eric W. Biederman881d9662007-09-17 11:56:21 -07001197 for_each_netdev(net, d) {
Jiri Bohac943e1582021-03-18 04:42:53 +01001198 struct netdev_name_node *name_node;
1199 list_for_each_entry(name_node, &d->name_node->list, list) {
1200 if (!sscanf(name_node->name, name, &i))
1201 continue;
1202 if (i < 0 || i >= max_netdevices)
1203 continue;
1204
1205 /* avoid cases where sscanf is not exact inverse of printf */
1206 snprintf(buf, IFNAMSIZ, name, i);
1207 if (!strncmp(buf, name_node->name, IFNAMSIZ))
1208 set_bit(i, inuse);
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!sscanf(d->name, name, &i))
1211 continue;
1212 if (i < 0 || i >= max_netdevices)
1213 continue;
1214
1215 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001216 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!strncmp(buf, d->name, IFNAMSIZ))
1218 set_bit(i, inuse);
1219 }
1220
1221 i = find_first_zero_bit(inuse, max_netdevices);
1222 free_page((unsigned long) inuse);
1223 }
1224
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001225 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001226 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /* It is possible to run out of possible slots
1230 * when the name is long and there isn't enough space left
1231 * for the digits, or if all bits are used.
1232 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001233 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001236static int dev_alloc_name_ns(struct net *net,
1237 struct net_device *dev,
1238 const char *name)
1239{
1240 char buf[IFNAMSIZ];
1241 int ret;
1242
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001243 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001244 ret = __dev_alloc_name(net, name, buf);
1245 if (ret >= 0)
1246 strlcpy(dev->name, buf, IFNAMSIZ);
1247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001250/**
1251 * dev_alloc_name - allocate a name for a device
1252 * @dev: device
1253 * @name: name format string
1254 *
1255 * Passed a format string - eg "lt%d" it will try and find a suitable
1256 * id. It scans list of devices to build up a free map, then chooses
1257 * the first empty slot. The caller must hold the dev_base or rtnl lock
1258 * while allocating the name and adding the device in order to avoid
1259 * duplicates.
1260 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1261 * Returns the number of the unit assigned or a negative errno code.
1262 */
1263
1264int dev_alloc_name(struct net_device *dev, const char *name)
1265{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001266 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001267}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001268EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001269
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001270static int dev_get_valid_name(struct net *net, struct net_device *dev,
1271 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001272{
David S. Miller55a5ec92018-01-02 11:45:07 -05001273 BUG_ON(!net);
1274
1275 if (!dev_valid_name(name))
1276 return -EINVAL;
1277
1278 if (strchr(name, '%'))
1279 return dev_alloc_name_ns(net, dev, name);
1280 else if (__dev_get_by_name(net, name))
1281 return -EEXIST;
1282 else if (dev->name != name)
1283 strlcpy(dev->name, name, IFNAMSIZ);
1284
1285 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/**
1289 * dev_change_name - change name of a device
1290 * @dev: device
1291 * @newname: name (or format string) must be at least IFNAMSIZ
1292 *
1293 * Change name of a device, can pass format strings "eth%d".
1294 * for wildcarding.
1295 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001296int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Tom Gundersen238fa362014-07-14 16:37:23 +02001298 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001299 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001301 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001302 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001305 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001307 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001308
1309 /* Some auto-enslaved devices e.g. failover slaves are
1310 * special, as userspace might rename the device after
1311 * the interface had been brought up and running since
1312 * the point kernel initiated auto-enslavement. Allow
1313 * live name change even when these slave devices are
1314 * up and running.
1315 *
1316 * Typically, users of these auto-enslaving devices
1317 * don't actually care about slave name change, as
1318 * they are supposed to operate on master interface
1319 * directly.
1320 */
1321 if (dev->flags & IFF_UP &&
1322 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -EBUSY;
1324
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001325 down_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001326
1327 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001328 up_write(&devnet_rename_sem);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001329 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001330 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001331
Herbert Xufcc5a032007-07-30 17:03:38 -07001332 memcpy(oldname, dev->name, IFNAMSIZ);
1333
Gao feng828de4f2012-09-13 20:58:27 +00001334 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001335 if (err < 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001336 up_write(&devnet_rename_sem);
Octavian Purdilad9031022009-11-18 02:36:59 +00001337 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001340 if (oldname[0] && !strchr(oldname, '%'))
1341 netdev_info(dev, "renamed from %s\n", oldname);
1342
Tom Gundersen238fa362014-07-14 16:37:23 +02001343 old_assign_type = dev->name_assign_type;
1344 dev->name_assign_type = NET_NAME_RENAMED;
1345
Herbert Xufcc5a032007-07-30 17:03:38 -07001346rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001347 ret = device_rename(&dev->dev, dev->name);
1348 if (ret) {
1349 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001350 dev->name_assign_type = old_assign_type;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001351 up_write(&devnet_rename_sem);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001352 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001353 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001354
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001355 up_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001356
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001357 netdev_adjacent_rename_links(dev, oldname);
1358
Herbert Xu7f988ea2007-07-30 16:35:46 -07001359 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001360 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001361 write_unlock_bh(&dev_base_lock);
1362
1363 synchronize_rcu();
1364
1365 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001366 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001367 write_unlock_bh(&dev_base_lock);
1368
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001369 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001370 ret = notifier_to_errno(ret);
1371
1372 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001373 /* err >= 0 after dev_alloc_name() or stores the first errno */
1374 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001375 err = ret;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001376 down_write(&devnet_rename_sem);
Herbert Xufcc5a032007-07-30 17:03:38 -07001377 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001378 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001379 dev->name_assign_type = old_assign_type;
1380 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001381 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001382 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001383 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001384 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001385 }
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 return err;
1389}
1390
1391/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001392 * dev_set_alias - change ifalias of a device
1393 * @dev: device
1394 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001395 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001396 *
1397 * Set ifalias for a device,
1398 */
1399int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1400{
Florian Westphal6c557002017-10-02 23:50:05 +02001401 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001402
1403 if (len >= IFALIASZ)
1404 return -EINVAL;
1405
Florian Westphal6c557002017-10-02 23:50:05 +02001406 if (len) {
1407 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1408 if (!new_alias)
1409 return -ENOMEM;
1410
1411 memcpy(new_alias->ifalias, alias, len);
1412 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001413 }
1414
Florian Westphal6c557002017-10-02 23:50:05 +02001415 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001416 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1417 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001418 mutex_unlock(&ifalias_mutex);
1419
1420 if (new_alias)
1421 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001422
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001423 return len;
1424}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001425EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001426
Florian Westphal6c557002017-10-02 23:50:05 +02001427/**
1428 * dev_get_alias - get ifalias of a device
1429 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001430 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001431 * @len: size of buffer
1432 *
1433 * get ifalias for a device. Caller must make sure dev cannot go
1434 * away, e.g. rcu read lock or own a reference count to device.
1435 */
1436int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1437{
1438 const struct dev_ifalias *alias;
1439 int ret = 0;
1440
1441 rcu_read_lock();
1442 alias = rcu_dereference(dev->ifalias);
1443 if (alias)
1444 ret = snprintf(name, len, "%s", alias->ifalias);
1445 rcu_read_unlock();
1446
1447 return ret;
1448}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001449
1450/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001451 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001452 * @dev: device to cause notification
1453 *
1454 * Called to indicate a device has changed features.
1455 */
1456void netdev_features_change(struct net_device *dev)
1457{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001458 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001459}
1460EXPORT_SYMBOL(netdev_features_change);
1461
1462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * netdev_state_change - device changes state
1464 * @dev: device to cause notification
1465 *
1466 * Called to indicate a device has changed state. This function calls
1467 * the notifier chains for netdev_chain and sends a NEWLINK message
1468 * to the routing socket.
1469 */
1470void netdev_state_change(struct net_device *dev)
1471{
1472 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001473 struct netdev_notifier_change_info change_info = {
1474 .info.dev = dev,
1475 };
Loic Prylli54951192014-07-01 21:39:43 -07001476
David Ahern51d0c0472017-10-04 17:48:45 -07001477 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001478 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001479 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001482EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Amerigo Wangee89bab2012-08-09 22:14:56 +00001484/**
tcharding722c9a02017-02-09 17:56:04 +11001485 * netdev_notify_peers - notify network peers about existence of @dev
1486 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001487 *
1488 * Generate traffic such that interested network peers are aware of
1489 * @dev, such as by generating a gratuitous ARP. This may be used when
1490 * a device wants to inform the rest of the network about some sort of
1491 * reconfiguration such as a failover event or virtual machine
1492 * migration.
1493 */
1494void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001495{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001496 rtnl_lock();
1497 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001498 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001499 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001500}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001501EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001502
Petr Machata40c900a2018-12-06 17:05:47 +00001503static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001505 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001508 ASSERT_RTNL();
1509
Heiner Kallweitbd869242020-06-20 22:35:42 +02001510 if (!netif_device_present(dev)) {
1511 /* may be detached because parent is runtime-suspended */
1512 if (dev->dev.parent)
1513 pm_runtime_resume(dev->dev.parent);
1514 if (!netif_device_present(dev))
1515 return -ENODEV;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Neil Hormanca99ca12013-02-05 08:05:43 +00001518 /* Block netpoll from trying to do any rx path servicing.
1519 * If we don't do this there is a chance ndo_poll_controller
1520 * or ndo_poll may be running while we open the device
1521 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001522 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001523
Petr Machata40c900a2018-12-06 17:05:47 +00001524 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001525 ret = notifier_to_errno(ret);
1526 if (ret)
1527 return ret;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001530
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001531 if (ops->ndo_validate_addr)
1532 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001533
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001534 if (!ret && ops->ndo_open)
1535 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric W. Biederman66b55522014-03-27 15:39:03 -07001537 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001538
Jeff Garzikbada3392007-10-23 20:19:37 -07001539 if (ret)
1540 clear_bit(__LINK_STATE_START, &dev->state);
1541 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001543 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001545 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
Patrick McHardybd380812010-02-26 06:34:53 +00001550
1551/**
1552 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001553 * @dev: device to open
1554 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001555 *
1556 * Takes a device from down to up state. The device's private open
1557 * function is invoked and then the multicast lists are loaded. Finally
1558 * the device is moved into the up state and a %NETDEV_UP message is
1559 * sent to the netdev notifier chain.
1560 *
1561 * Calling this function on an active interface is a nop. On a failure
1562 * a negative errno code is returned.
1563 */
Petr Machata00f54e62018-12-06 17:05:36 +00001564int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001565{
1566 int ret;
1567
Patrick McHardybd380812010-02-26 06:34:53 +00001568 if (dev->flags & IFF_UP)
1569 return 0;
1570
Petr Machata40c900a2018-12-06 17:05:47 +00001571 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001572 if (ret < 0)
1573 return ret;
1574
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001575 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001576 call_netdevice_notifiers(NETDEV_UP, dev);
1577
1578 return ret;
1579}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001580EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
stephen hemminger7051b882017-07-18 15:59:27 -07001582static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Octavian Purdila44345722010-12-13 12:44:07 +00001584 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001585
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001586 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001587 might_sleep();
1588
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001589 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001590 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001591 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001592
Octavian Purdila44345722010-12-13 12:44:07 +00001593 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Octavian Purdila44345722010-12-13 12:44:07 +00001595 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Octavian Purdila44345722010-12-13 12:44:07 +00001597 /* Synchronize to scheduled poll. We cannot touch poll list, it
1598 * can be even on different cpu. So just clear netif_running().
1599 *
1600 * dev->stop() will invoke napi_disable() on all of it's
1601 * napi_struct instances on this device.
1602 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001603 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Octavian Purdila44345722010-12-13 12:44:07 +00001606 dev_deactivate_many(head);
1607
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001608 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001609 const struct net_device_ops *ops = dev->netdev_ops;
1610
1611 /*
1612 * Call the device specific close. This cannot fail.
1613 * Only if device is UP
1614 *
1615 * We allow it to be called even after a DETACH hot-plug
1616 * event.
1617 */
1618 if (ops->ndo_stop)
1619 ops->ndo_stop(dev);
1620
Octavian Purdila44345722010-12-13 12:44:07 +00001621 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001622 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001623 }
Octavian Purdila44345722010-12-13 12:44:07 +00001624}
1625
stephen hemminger7051b882017-07-18 15:59:27 -07001626static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001627{
1628 LIST_HEAD(single);
1629
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001630 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001631 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001632 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001633}
1634
stephen hemminger7051b882017-07-18 15:59:27 -07001635void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001636{
1637 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001638
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001639 /* Remove the devices that don't need to be closed */
1640 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001641 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001642 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001643
1644 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001645
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001646 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001647 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001648 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001649 if (unlink)
1650 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
David S. Miller99c4a262015-03-18 22:52:33 -04001653EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001654
1655/**
1656 * dev_close - shutdown an interface.
1657 * @dev: device to shutdown
1658 *
1659 * This function moves an active device into down state. A
1660 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1661 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1662 * chain.
1663 */
stephen hemminger7051b882017-07-18 15:59:27 -07001664void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001665{
Eric Dumazete14a5992011-05-10 12:26:06 -07001666 if (dev->flags & IFF_UP) {
1667 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001668
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001669 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001670 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001671 list_del(&single);
1672 }
Patrick McHardybd380812010-02-26 06:34:53 +00001673}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001674EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001677/**
1678 * dev_disable_lro - disable Large Receive Offload on a device
1679 * @dev: device
1680 *
1681 * Disable Large Receive Offload (LRO) on a net device. Must be
1682 * called under RTNL. This is needed if received packets may be
1683 * forwarded to another interface.
1684 */
1685void dev_disable_lro(struct net_device *dev)
1686{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001687 struct net_device *lower_dev;
1688 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001689
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001690 dev->wanted_features &= ~NETIF_F_LRO;
1691 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001692
Michał Mirosław22d59692011-04-21 12:42:15 +00001693 if (unlikely(dev->features & NETIF_F_LRO))
1694 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001695
1696 netdev_for_each_lower_dev(dev, lower_dev, iter)
1697 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001698}
1699EXPORT_SYMBOL(dev_disable_lro);
1700
Michael Chan56f5aa72017-12-16 03:09:41 -05001701/**
1702 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1703 * @dev: device
1704 *
1705 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1706 * called under RTNL. This is needed if Generic XDP is installed on
1707 * the device.
1708 */
1709static void dev_disable_gro_hw(struct net_device *dev)
1710{
1711 dev->wanted_features &= ~NETIF_F_GRO_HW;
1712 netdev_update_features(dev);
1713
1714 if (unlikely(dev->features & NETIF_F_GRO_HW))
1715 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1716}
1717
Kirill Tkhaiede27622018-03-23 19:47:19 +03001718const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1719{
1720#define N(val) \
1721 case NETDEV_##val: \
1722 return "NETDEV_" __stringify(val);
1723 switch (cmd) {
1724 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1725 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1726 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1727 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1728 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1729 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1730 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001731 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1732 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001733 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001734 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001735#undef N
1736 return "UNKNOWN_NETDEV_EVENT";
1737}
1738EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1739
Jiri Pirko351638e2013-05-28 01:30:21 +00001740static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1741 struct net_device *dev)
1742{
David Ahern51d0c0472017-10-04 17:48:45 -07001743 struct netdev_notifier_info info = {
1744 .dev = dev,
1745 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001746
Jiri Pirko351638e2013-05-28 01:30:21 +00001747 return nb->notifier_call(nb, val, &info);
1748}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001749
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001750static int call_netdevice_register_notifiers(struct notifier_block *nb,
1751 struct net_device *dev)
1752{
1753 int err;
1754
1755 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1756 err = notifier_to_errno(err);
1757 if (err)
1758 return err;
1759
1760 if (!(dev->flags & IFF_UP))
1761 return 0;
1762
1763 call_netdevice_notifier(nb, NETDEV_UP, dev);
1764 return 0;
1765}
1766
1767static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1768 struct net_device *dev)
1769{
1770 if (dev->flags & IFF_UP) {
1771 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1772 dev);
1773 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1774 }
1775 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1776}
1777
1778static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1779 struct net *net)
1780{
1781 struct net_device *dev;
1782 int err;
1783
1784 for_each_netdev(net, dev) {
1785 err = call_netdevice_register_notifiers(nb, dev);
1786 if (err)
1787 goto rollback;
1788 }
1789 return 0;
1790
1791rollback:
1792 for_each_netdev_continue_reverse(net, dev)
1793 call_netdevice_unregister_notifiers(nb, dev);
1794 return err;
1795}
1796
1797static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1798 struct net *net)
1799{
1800 struct net_device *dev;
1801
1802 for_each_netdev(net, dev)
1803 call_netdevice_unregister_notifiers(nb, dev);
1804}
1805
Eric W. Biederman881d9662007-09-17 11:56:21 -07001806static int dev_boot_phase = 1;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/**
tcharding722c9a02017-02-09 17:56:04 +11001809 * register_netdevice_notifier - register a network notifier block
1810 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 *
tcharding722c9a02017-02-09 17:56:04 +11001812 * Register a notifier to be called when network device events occur.
1813 * The notifier passed is linked into the kernel structures and must
1814 * not be reused until it has been unregistered. A negative errno code
1815 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
tcharding722c9a02017-02-09 17:56:04 +11001817 * When registered all registration and up events are replayed
1818 * to the new notifier to allow device to have a race free
1819 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
1821
1822int register_netdevice_notifier(struct notifier_block *nb)
1823{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001824 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 int err;
1826
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001827 /* Close race with setup_net() and cleanup_net() */
1828 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001830 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001831 if (err)
1832 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001833 if (dev_boot_phase)
1834 goto unlock;
1835 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001836 err = call_netdevice_register_net_notifiers(nb, net);
1837 if (err)
1838 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001840
1841unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001843 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001845
1846rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001847 for_each_net_continue_reverse(net)
1848 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001849
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001850 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001851 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001853EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855/**
tcharding722c9a02017-02-09 17:56:04 +11001856 * unregister_netdevice_notifier - unregister a network notifier block
1857 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
tcharding722c9a02017-02-09 17:56:04 +11001859 * Unregister a notifier previously registered by
1860 * register_netdevice_notifier(). The notifier is unlinked into the
1861 * kernel structures and may then be reused. A negative errno code
1862 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001863 *
tcharding722c9a02017-02-09 17:56:04 +11001864 * After unregistering unregister and down device events are synthesized
1865 * for all devices on the device list to the removed notifier to remove
1866 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868
1869int unregister_netdevice_notifier(struct notifier_block *nb)
1870{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001871 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001872 int err;
1873
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001874 /* Close race with setup_net() and cleanup_net() */
1875 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001876 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001877 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001878 if (err)
1879 goto unlock;
1880
Jiri Pirko48b3a132020-01-25 12:17:06 +01001881 for_each_net(net)
1882 call_netdevice_unregister_net_notifiers(nb, net);
1883
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001884unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001885 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001886 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001887 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001889EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Jiri Pirko1f637702020-01-25 12:17:07 +01001891static int __register_netdevice_notifier_net(struct net *net,
1892 struct notifier_block *nb,
1893 bool ignore_call_fail)
1894{
1895 int err;
1896
1897 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1898 if (err)
1899 return err;
1900 if (dev_boot_phase)
1901 return 0;
1902
1903 err = call_netdevice_register_net_notifiers(nb, net);
1904 if (err && !ignore_call_fail)
1905 goto chain_unregister;
1906
1907 return 0;
1908
1909chain_unregister:
1910 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1911 return err;
1912}
1913
1914static int __unregister_netdevice_notifier_net(struct net *net,
1915 struct notifier_block *nb)
1916{
1917 int err;
1918
1919 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1920 if (err)
1921 return err;
1922
1923 call_netdevice_unregister_net_notifiers(nb, net);
1924 return 0;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001928 * register_netdevice_notifier_net - register a per-netns network notifier block
1929 * @net: network namespace
1930 * @nb: notifier
1931 *
1932 * Register a notifier to be called when network device events occur.
1933 * The notifier passed is linked into the kernel structures and must
1934 * not be reused until it has been unregistered. A negative errno code
1935 * is returned on a failure.
1936 *
1937 * When registered all registration and up events are replayed
1938 * to the new notifier to allow device to have a race free
1939 * view of the network device list.
1940 */
1941
1942int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1943{
1944 int err;
1945
1946 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001947 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001948 rtnl_unlock();
1949 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001950}
1951EXPORT_SYMBOL(register_netdevice_notifier_net);
1952
1953/**
1954 * unregister_netdevice_notifier_net - unregister a per-netns
1955 * network notifier block
1956 * @net: network namespace
1957 * @nb: notifier
1958 *
1959 * Unregister a notifier previously registered by
1960 * register_netdevice_notifier(). The notifier is unlinked into the
1961 * kernel structures and may then be reused. A negative errno code
1962 * is returned on a failure.
1963 *
1964 * After unregistering unregister and down device events are synthesized
1965 * for all devices on the device list to the removed notifier to remove
1966 * the need for special case cleanup code.
1967 */
1968
1969int unregister_netdevice_notifier_net(struct net *net,
1970 struct notifier_block *nb)
1971{
1972 int err;
1973
1974 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001975 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001976 rtnl_unlock();
1977 return err;
1978}
1979EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1980
Jiri Pirko93642e12020-01-25 12:17:08 +01001981int register_netdevice_notifier_dev_net(struct net_device *dev,
1982 struct notifier_block *nb,
1983 struct netdev_net_notifier *nn)
1984{
1985 int err;
1986
1987 rtnl_lock();
1988 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1989 if (!err) {
1990 nn->nb = nb;
1991 list_add(&nn->list, &dev->net_notifier_list);
1992 }
1993 rtnl_unlock();
1994 return err;
1995}
1996EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1997
1998int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1999 struct notifier_block *nb,
2000 struct netdev_net_notifier *nn)
2001{
2002 int err;
2003
2004 rtnl_lock();
2005 list_del(&nn->list);
2006 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
2007 rtnl_unlock();
2008 return err;
2009}
2010EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
2011
2012static void move_netdevice_notifiers_dev_net(struct net_device *dev,
2013 struct net *net)
2014{
2015 struct netdev_net_notifier *nn;
2016
2017 list_for_each_entry(nn, &dev->net_notifier_list, list) {
2018 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
2019 __register_netdevice_notifier_net(net, nn->nb, true);
2020 }
2021}
2022
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002023/**
Jiri Pirko351638e2013-05-28 01:30:21 +00002024 * call_netdevice_notifiers_info - call all network notifier blocks
2025 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00002026 * @info: notifier information data
2027 *
2028 * Call all network notifier blocks. Parameters and return value
2029 * are as for raw_notifier_call_chain().
2030 */
2031
stephen hemminger1d143d92013-12-29 14:01:29 -08002032static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08002033 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00002034{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002035 struct net *net = dev_net(info->dev);
2036 int ret;
2037
Jiri Pirko351638e2013-05-28 01:30:21 +00002038 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002039
2040 /* Run per-netns notifier block chain first, then run the global one.
2041 * Hopefully, one day, the global one is going to be removed after
2042 * all notifier block registrators get converted to be per-netns.
2043 */
2044 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
2045 if (ret & NOTIFY_STOP_MASK)
2046 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00002047 return raw_notifier_call_chain(&netdev_chain, val, info);
2048}
Jiri Pirko351638e2013-05-28 01:30:21 +00002049
Petr Machata26372602018-12-06 17:05:45 +00002050static int call_netdevice_notifiers_extack(unsigned long val,
2051 struct net_device *dev,
2052 struct netlink_ext_ack *extack)
2053{
2054 struct netdev_notifier_info info = {
2055 .dev = dev,
2056 .extack = extack,
2057 };
2058
2059 return call_netdevice_notifiers_info(val, &info);
2060}
2061
Jiri Pirko351638e2013-05-28 01:30:21 +00002062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * call_netdevice_notifiers - call all network notifier blocks
2064 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002065 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 *
2067 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07002068 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 */
2070
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07002071int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Petr Machata26372602018-12-06 17:05:45 +00002073 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
stephen hemmingeredf947f2011-03-24 13:24:01 +00002075EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02002077/**
2078 * call_netdevice_notifiers_mtu - call all network notifier blocks
2079 * @val: value passed unmodified to notifier function
2080 * @dev: net_device pointer passed unmodified to notifier function
2081 * @arg: additional u32 argument passed to the notifier function
2082 *
2083 * Call all network notifier blocks. Parameters and return value
2084 * are as for raw_notifier_call_chain().
2085 */
2086static int call_netdevice_notifiers_mtu(unsigned long val,
2087 struct net_device *dev, u32 arg)
2088{
2089 struct netdev_notifier_info_ext info = {
2090 .info.dev = dev,
2091 .ext.mtu = arg,
2092 };
2093
2094 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2095
2096 return call_netdevice_notifiers_info(val, &info.info);
2097}
2098
Pablo Neira1cf519002015-05-13 18:19:37 +02002099#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002100static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002101
2102void net_inc_ingress_queue(void)
2103{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002104 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002105}
2106EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2107
2108void net_dec_ingress_queue(void)
2109{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002110 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002111}
2112EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2113#endif
2114
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002115#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002116static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002117
2118void net_inc_egress_queue(void)
2119{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002120 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002121}
2122EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2123
2124void net_dec_egress_queue(void)
2125{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002126 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002127}
2128EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2129#endif
2130
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002131static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002132#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002133static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002134static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002135static void netstamp_clear(struct work_struct *work)
2136{
2137 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002138 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002139
Eric Dumazet13baa002017-03-01 14:28:39 -08002140 wanted = atomic_add_return(deferred, &netstamp_wanted);
2141 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002142 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002143 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002144 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002145}
2146static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149void net_enable_timestamp(void)
2150{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002151#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002152 int wanted;
2153
2154 while (1) {
2155 wanted = atomic_read(&netstamp_wanted);
2156 if (wanted <= 0)
2157 break;
2158 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2159 return;
2160 }
2161 atomic_inc(&netstamp_needed_deferred);
2162 schedule_work(&netstamp_work);
2163#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002164 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002167EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169void net_disable_timestamp(void)
2170{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002171#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002172 int wanted;
2173
2174 while (1) {
2175 wanted = atomic_read(&netstamp_wanted);
2176 if (wanted <= 1)
2177 break;
2178 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2179 return;
2180 }
2181 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002182 schedule_work(&netstamp_work);
2183#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002184 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002187EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Eric Dumazet3b098e22010-05-15 23:57:10 -07002189static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002191 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002192 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002193 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002196#define net_timestamp_check(COND, SKB) \
2197 if (static_branch_unlikely(&netstamp_needed_key)) { \
2198 if ((COND) && !(SKB)->tstamp) \
2199 __net_timestamp(SKB); \
2200 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002201
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002202bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002203{
2204 unsigned int len;
2205
2206 if (!(dev->flags & IFF_UP))
2207 return false;
2208
2209 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2210 if (skb->len <= len)
2211 return true;
2212
2213 /* if TSO is enabled, we don't care about the length as the packet
2214 * could be forwarded without being segmented before
2215 */
2216 if (skb_is_gso(skb))
2217 return true;
2218
2219 return false;
2220}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002221EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002222
Herbert Xua0265d22014-04-17 13:45:03 +08002223int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2224{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002225 int ret = ____dev_forward_skb(dev, skb);
2226
2227 if (likely(!ret)) {
2228 skb->protocol = eth_type_trans(skb, dev);
2229 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002230 }
2231
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002232 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002233}
2234EXPORT_SYMBOL_GPL(__dev_forward_skb);
2235
Arnd Bergmann44540962009-11-26 06:07:08 +00002236/**
2237 * dev_forward_skb - loopback an skb to another netif
2238 *
2239 * @dev: destination network device
2240 * @skb: buffer to forward
2241 *
2242 * return values:
2243 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002244 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002245 *
2246 * dev_forward_skb can be used for injecting an skb from the
2247 * start_xmit function of one device into the receive queue
2248 * of another device.
2249 *
2250 * The receiving device may be in another namespace, so
2251 * we have to clear all information in the skb that could
2252 * impact namespace isolation.
2253 */
2254int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2255{
Herbert Xua0265d22014-04-17 13:45:03 +08002256 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002257}
2258EXPORT_SYMBOL_GPL(dev_forward_skb);
2259
Changli Gao71d9dec2010-12-15 19:57:25 +00002260static inline int deliver_skb(struct sk_buff *skb,
2261 struct packet_type *pt_prev,
2262 struct net_device *orig_dev)
2263{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002264 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002265 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002266 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002267 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2268}
2269
Salam Noureddine7866a622015-01-27 11:35:48 -08002270static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2271 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002272 struct net_device *orig_dev,
2273 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002274 struct list_head *ptype_list)
2275{
2276 struct packet_type *ptype, *pt_prev = *pt;
2277
2278 list_for_each_entry_rcu(ptype, ptype_list, list) {
2279 if (ptype->type != type)
2280 continue;
2281 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002282 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002283 pt_prev = ptype;
2284 }
2285 *pt = pt_prev;
2286}
2287
Eric Leblondc0de08d2012-08-16 22:02:58 +00002288static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2289{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002290 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002291 return false;
2292
2293 if (ptype->id_match)
2294 return ptype->id_match(ptype, skb->sk);
2295 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2296 return true;
2297
2298 return false;
2299}
2300
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002301/**
2302 * dev_nit_active - return true if any network interface taps are in use
2303 *
2304 * @dev: network device to check for the presence of taps
2305 */
2306bool dev_nit_active(struct net_device *dev)
2307{
2308 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2309}
2310EXPORT_SYMBOL_GPL(dev_nit_active);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * Support routine. Sends outgoing frames to any network
2314 * taps currently in use.
2315 */
2316
David Ahern74b20582016-05-10 11:19:50 -07002317void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002320 struct sk_buff *skb2 = NULL;
2321 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002322 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002325again:
2326 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002327 if (ptype->ignore_outgoing)
2328 continue;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* Never send packets back to the socket
2331 * they originated from - MvS (miquels@drinkel.ow.org)
2332 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002333 if (skb_loop_sk(ptype, skb))
2334 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002335
Salam Noureddine7866a622015-01-27 11:35:48 -08002336 if (pt_prev) {
2337 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002338 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002339 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002341
2342 /* need to clone skb, done only once */
2343 skb2 = skb_clone(skb, GFP_ATOMIC);
2344 if (!skb2)
2345 goto out_unlock;
2346
2347 net_timestamp_set(skb2);
2348
2349 /* skb->nh should be correctly
2350 * set by sender, so that the second statement is
2351 * just protection against buggy protocols.
2352 */
2353 skb_reset_mac_header(skb2);
2354
2355 if (skb_network_header(skb2) < skb2->data ||
2356 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2357 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2358 ntohs(skb2->protocol),
2359 dev->name);
2360 skb_reset_network_header(skb2);
2361 }
2362
2363 skb2->transport_header = skb2->network_header;
2364 skb2->pkt_type = PACKET_OUTGOING;
2365 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002367
2368 if (ptype_list == &ptype_all) {
2369 ptype_list = &dev->ptype_all;
2370 goto again;
2371 }
2372out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002373 if (pt_prev) {
2374 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2375 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2376 else
2377 kfree_skb(skb2);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 rcu_read_unlock();
2380}
David Ahern74b20582016-05-10 11:19:50 -07002381EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Ben Hutchings2c530402012-07-10 10:55:09 +00002383/**
2384 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002385 * @dev: Network device
2386 * @txq: number of queues available
2387 *
2388 * If real_num_tx_queues is changed the tc mappings may no longer be
2389 * valid. To resolve this verify the tc mapping remains valid and if
2390 * not NULL the mapping. With no priorities mapping to this
2391 * offset/count pair it will no longer be used. In the worst case TC0
2392 * is invalid nothing can be done so disable priority mappings. If is
2393 * expected that drivers will fix this mapping if they can before
2394 * calling netif_set_real_num_tx_queues.
2395 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002396static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002397{
2398 int i;
2399 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2400
2401 /* If TC0 is invalidated disable TC mapping */
2402 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002403 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002404 dev->num_tc = 0;
2405 return;
2406 }
2407
2408 /* Invalidated prio to tc mappings set to TC0 */
2409 for (i = 1; i < TC_BITMASK + 1; i++) {
2410 int q = netdev_get_prio_tc_map(dev, i);
2411
2412 tc = &dev->tc_to_txq[q];
2413 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002414 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2415 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002416 netdev_set_prio_tc_map(dev, i, 0);
2417 }
2418 }
2419}
2420
Alexander Duyck8d059b02016-10-28 11:43:49 -04002421int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2422{
2423 if (dev->num_tc) {
2424 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2425 int i;
2426
Alexander Duyckffcfe252018-07-09 12:19:38 -04002427 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002428 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2429 if ((txq - tc->offset) < tc->count)
2430 return i;
2431 }
2432
Alexander Duyckffcfe252018-07-09 12:19:38 -04002433 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002434 return -1;
2435 }
2436
2437 return 0;
2438}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002439EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002440
Alexander Duyck537c00d2013-01-10 08:57:02 +00002441#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002442struct static_key xps_needed __read_mostly;
2443EXPORT_SYMBOL(xps_needed);
2444struct static_key xps_rxqs_needed __read_mostly;
2445EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002446static DEFINE_MUTEX(xps_map_mutex);
2447#define xmap_dereference(P) \
2448 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2449
Alexander Duyck6234f872016-10-28 11:46:49 -04002450static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2451 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002452{
2453 struct xps_map *map = NULL;
2454 int pos;
2455
2456 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002457 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002458 if (!map)
2459 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002460
Alexander Duyck6234f872016-10-28 11:46:49 -04002461 for (pos = map->len; pos--;) {
2462 if (map->queues[pos] != index)
2463 continue;
2464
2465 if (map->len > 1) {
2466 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002467 break;
2468 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002469
Amritha Nambiar80d19662018-06-29 21:26:41 -07002470 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002471 kfree_rcu(map, rcu);
2472 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002473 }
2474
Alexander Duyck6234f872016-10-28 11:46:49 -04002475 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002476}
2477
Alexander Duyck6234f872016-10-28 11:46:49 -04002478static bool remove_xps_queue_cpu(struct net_device *dev,
2479 struct xps_dev_maps *dev_maps,
2480 int cpu, u16 offset, u16 count)
2481{
Alexander Duyck184c4492016-10-28 11:50:13 -04002482 int num_tc = dev->num_tc ? : 1;
2483 bool active = false;
2484 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002485
Alexander Duyck184c4492016-10-28 11:50:13 -04002486 for (tci = cpu * num_tc; num_tc--; tci++) {
2487 int i, j;
2488
2489 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002490 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002491 break;
2492 }
2493
2494 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002495 }
2496
Alexander Duyck184c4492016-10-28 11:50:13 -04002497 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002498}
2499
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002500static void reset_xps_maps(struct net_device *dev,
2501 struct xps_dev_maps *dev_maps,
2502 bool is_rxqs_map)
2503{
2504 if (is_rxqs_map) {
2505 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2506 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2507 } else {
2508 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2509 }
2510 static_key_slow_dec_cpuslocked(&xps_needed);
2511 kfree_rcu(dev_maps, rcu);
2512}
2513
Amritha Nambiar80d19662018-06-29 21:26:41 -07002514static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2515 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2516 u16 offset, u16 count, bool is_rxqs_map)
2517{
2518 bool active = false;
2519 int i, j;
2520
2521 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2522 j < nr_ids;)
2523 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2524 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002525 if (!active)
2526 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002527
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002528 if (!is_rxqs_map) {
2529 for (i = offset + (count - 1); count--; i--) {
2530 netdev_queue_numa_node_write(
2531 netdev_get_tx_queue(dev, i),
2532 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002533 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002534 }
2535}
2536
Alexander Duyck6234f872016-10-28 11:46:49 -04002537static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2538 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002539{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002540 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002541 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002542 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002543
Amritha Nambiar04157462018-06-29 21:26:46 -07002544 if (!static_key_false(&xps_needed))
2545 return;
2546
Andrei Vagin4d99f662018-08-08 20:07:35 -07002547 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002548 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002549
Amritha Nambiar04157462018-06-29 21:26:46 -07002550 if (static_key_false(&xps_rxqs_needed)) {
2551 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2552 if (dev_maps) {
2553 nr_ids = dev->num_rx_queues;
2554 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2555 offset, count, true);
2556 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002557 }
2558
2559 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002560 if (!dev_maps)
2561 goto out_no_maps;
2562
Amritha Nambiar80d19662018-06-29 21:26:41 -07002563 if (num_possible_cpus() > 1)
2564 possible_mask = cpumask_bits(cpu_possible_mask);
2565 nr_ids = nr_cpu_ids;
2566 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2567 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002568
Alexander Duyck537c00d2013-01-10 08:57:02 +00002569out_no_maps:
2570 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002571 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002572}
2573
Alexander Duyck6234f872016-10-28 11:46:49 -04002574static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2575{
2576 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2577}
2578
Amritha Nambiar80d19662018-06-29 21:26:41 -07002579static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2580 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002581{
2582 struct xps_map *new_map;
2583 int alloc_len = XPS_MIN_MAP_ALLOC;
2584 int i, pos;
2585
2586 for (pos = 0; map && pos < map->len; pos++) {
2587 if (map->queues[pos] != index)
2588 continue;
2589 return map;
2590 }
2591
Amritha Nambiar80d19662018-06-29 21:26:41 -07002592 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002593 if (map) {
2594 if (pos < map->alloc_len)
2595 return map;
2596
2597 alloc_len = map->alloc_len * 2;
2598 }
2599
Amritha Nambiar80d19662018-06-29 21:26:41 -07002600 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2601 * map
2602 */
2603 if (is_rxqs_map)
2604 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2605 else
2606 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2607 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002608 if (!new_map)
2609 return NULL;
2610
2611 for (i = 0; i < pos; i++)
2612 new_map->queues[i] = map->queues[i];
2613 new_map->alloc_len = alloc_len;
2614 new_map->len = pos;
2615
2616 return new_map;
2617}
2618
Andrei Vagin4d99f662018-08-08 20:07:35 -07002619/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002620int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2621 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002622{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002623 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002624 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002625 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002626 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002627 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002628 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002629 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002630
Alexander Duyck184c4492016-10-28 11:50:13 -04002631 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002632 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002633 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002634 if (num_tc < 0)
2635 return -EINVAL;
2636
2637 /* If queue belongs to subordinate dev use its map */
2638 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2639
Alexander Duyck184c4492016-10-28 11:50:13 -04002640 tc = netdev_txq_to_tc(dev, index);
2641 if (tc < 0)
2642 return -EINVAL;
2643 }
2644
Amritha Nambiar80d19662018-06-29 21:26:41 -07002645 mutex_lock(&xps_map_mutex);
2646 if (is_rxqs_map) {
2647 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2648 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2649 nr_ids = dev->num_rx_queues;
2650 } else {
2651 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2652 if (num_possible_cpus() > 1) {
2653 online_mask = cpumask_bits(cpu_online_mask);
2654 possible_mask = cpumask_bits(cpu_possible_mask);
2655 }
2656 dev_maps = xmap_dereference(dev->xps_cpus_map);
2657 nr_ids = nr_cpu_ids;
2658 }
2659
Alexander Duyck184c4492016-10-28 11:50:13 -04002660 if (maps_sz < L1_CACHE_BYTES)
2661 maps_sz = L1_CACHE_BYTES;
2662
Alexander Duyck01c5f862013-01-10 08:57:35 +00002663 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002664 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2665 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002666 if (!new_dev_maps)
2667 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002668 if (!new_dev_maps) {
2669 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002670 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002671 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002672
Amritha Nambiar80d19662018-06-29 21:26:41 -07002673 tci = j * num_tc + tc;
2674 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002675 NULL;
2676
Amritha Nambiar80d19662018-06-29 21:26:41 -07002677 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002678 if (!map)
2679 goto error;
2680
Amritha Nambiar80d19662018-06-29 21:26:41 -07002681 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002682 }
2683
2684 if (!new_dev_maps)
2685 goto out_no_new_maps;
2686
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002687 if (!dev_maps) {
2688 /* Increment static keys at most once per type */
2689 static_key_slow_inc_cpuslocked(&xps_needed);
2690 if (is_rxqs_map)
2691 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2692 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002693
Amritha Nambiar80d19662018-06-29 21:26:41 -07002694 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2695 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002696 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002697 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002698 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002699 map = xmap_dereference(dev_maps->attr_map[tci]);
2700 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002701 }
2702
2703 /* We need to explicitly update tci as prevous loop
2704 * could break out early if dev_maps is NULL.
2705 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002706 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002707
Amritha Nambiar80d19662018-06-29 21:26:41 -07002708 if (netif_attr_test_mask(j, mask, nr_ids) &&
2709 netif_attr_test_online(j, online_mask, nr_ids)) {
2710 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002711 int pos = 0;
2712
Amritha Nambiar80d19662018-06-29 21:26:41 -07002713 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002714 while ((pos < map->len) && (map->queues[pos] != index))
2715 pos++;
2716
2717 if (pos == map->len)
2718 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002719#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002720 if (!is_rxqs_map) {
2721 if (numa_node_id == -2)
2722 numa_node_id = cpu_to_node(j);
2723 else if (numa_node_id != cpu_to_node(j))
2724 numa_node_id = -1;
2725 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002726#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002727 } else if (dev_maps) {
2728 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002729 map = xmap_dereference(dev_maps->attr_map[tci]);
2730 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002731 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002732
Alexander Duyck184c4492016-10-28 11:50:13 -04002733 /* copy maps belonging to foreign traffic classes */
2734 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2735 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002736 map = xmap_dereference(dev_maps->attr_map[tci]);
2737 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002738 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002739 }
2740
Amritha Nambiar80d19662018-06-29 21:26:41 -07002741 if (is_rxqs_map)
2742 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2743 else
2744 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002745
Alexander Duyck537c00d2013-01-10 08:57:02 +00002746 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002747 if (!dev_maps)
2748 goto out_no_old_maps;
2749
Amritha Nambiar80d19662018-06-29 21:26:41 -07002750 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2751 j < nr_ids;) {
2752 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2753 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2754 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002755 if (map && map != new_map)
2756 kfree_rcu(map, rcu);
2757 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002758 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002759
Alexander Duyck184c4492016-10-28 11:50:13 -04002760 kfree_rcu(dev_maps, rcu);
2761
2762out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002763 dev_maps = new_dev_maps;
2764 active = true;
2765
2766out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002767 if (!is_rxqs_map) {
2768 /* update Tx queue numa node */
2769 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2770 (numa_node_id >= 0) ?
2771 numa_node_id : NUMA_NO_NODE);
2772 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002773
Alexander Duyck01c5f862013-01-10 08:57:35 +00002774 if (!dev_maps)
2775 goto out_no_maps;
2776
Amritha Nambiar80d19662018-06-29 21:26:41 -07002777 /* removes tx-queue from unused CPUs/rx-queues */
2778 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2779 j < nr_ids;) {
2780 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002781 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002782 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2783 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002784 active |= remove_xps_queue(dev_maps, tci, index);
2785 for (i = num_tc - tc, tci++; --i; tci++)
2786 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002787 }
2788
2789 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002790 if (!active)
2791 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002792
2793out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002794 mutex_unlock(&xps_map_mutex);
2795
2796 return 0;
2797error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002798 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002799 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2800 j < nr_ids;) {
2801 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2802 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002803 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002804 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002805 NULL;
2806 if (new_map && new_map != map)
2807 kfree(new_map);
2808 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002809 }
2810
Alexander Duyck537c00d2013-01-10 08:57:02 +00002811 mutex_unlock(&xps_map_mutex);
2812
Alexander Duyck537c00d2013-01-10 08:57:02 +00002813 kfree(new_dev_maps);
2814 return -ENOMEM;
2815}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002816EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002817
2818int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2819 u16 index)
2820{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002821 int ret;
2822
2823 cpus_read_lock();
2824 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2825 cpus_read_unlock();
2826
2827 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002828}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002829EXPORT_SYMBOL(netif_set_xps_queue);
2830
2831#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002832static void netdev_unbind_all_sb_channels(struct net_device *dev)
2833{
2834 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2835
2836 /* Unbind any subordinate channels */
2837 while (txq-- != &dev->_tx[0]) {
2838 if (txq->sb_dev)
2839 netdev_unbind_sb_channel(dev, txq->sb_dev);
2840 }
2841}
2842
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002843void netdev_reset_tc(struct net_device *dev)
2844{
Alexander Duyck6234f872016-10-28 11:46:49 -04002845#ifdef CONFIG_XPS
2846 netif_reset_xps_queues_gt(dev, 0);
2847#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002848 netdev_unbind_all_sb_channels(dev);
2849
2850 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002851 dev->num_tc = 0;
2852 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2853 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2854}
2855EXPORT_SYMBOL(netdev_reset_tc);
2856
2857int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2858{
2859 if (tc >= dev->num_tc)
2860 return -EINVAL;
2861
Alexander Duyck6234f872016-10-28 11:46:49 -04002862#ifdef CONFIG_XPS
2863 netif_reset_xps_queues(dev, offset, count);
2864#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002865 dev->tc_to_txq[tc].count = count;
2866 dev->tc_to_txq[tc].offset = offset;
2867 return 0;
2868}
2869EXPORT_SYMBOL(netdev_set_tc_queue);
2870
2871int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2872{
2873 if (num_tc > TC_MAX_QUEUE)
2874 return -EINVAL;
2875
Alexander Duyck6234f872016-10-28 11:46:49 -04002876#ifdef CONFIG_XPS
2877 netif_reset_xps_queues_gt(dev, 0);
2878#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002879 netdev_unbind_all_sb_channels(dev);
2880
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002881 dev->num_tc = num_tc;
2882 return 0;
2883}
2884EXPORT_SYMBOL(netdev_set_num_tc);
2885
Alexander Duyckffcfe252018-07-09 12:19:38 -04002886void netdev_unbind_sb_channel(struct net_device *dev,
2887 struct net_device *sb_dev)
2888{
2889 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2890
2891#ifdef CONFIG_XPS
2892 netif_reset_xps_queues_gt(sb_dev, 0);
2893#endif
2894 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2895 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2896
2897 while (txq-- != &dev->_tx[0]) {
2898 if (txq->sb_dev == sb_dev)
2899 txq->sb_dev = NULL;
2900 }
2901}
2902EXPORT_SYMBOL(netdev_unbind_sb_channel);
2903
2904int netdev_bind_sb_channel_queue(struct net_device *dev,
2905 struct net_device *sb_dev,
2906 u8 tc, u16 count, u16 offset)
2907{
2908 /* Make certain the sb_dev and dev are already configured */
2909 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2910 return -EINVAL;
2911
2912 /* We cannot hand out queues we don't have */
2913 if ((offset + count) > dev->real_num_tx_queues)
2914 return -EINVAL;
2915
2916 /* Record the mapping */
2917 sb_dev->tc_to_txq[tc].count = count;
2918 sb_dev->tc_to_txq[tc].offset = offset;
2919
2920 /* Provide a way for Tx queue to find the tc_to_txq map or
2921 * XPS map for itself.
2922 */
2923 while (count--)
2924 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2925
2926 return 0;
2927}
2928EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2929
2930int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2931{
2932 /* Do not use a multiqueue device to represent a subordinate channel */
2933 if (netif_is_multiqueue(dev))
2934 return -ENODEV;
2935
2936 /* We allow channels 1 - 32767 to be used for subordinate channels.
2937 * Channel 0 is meant to be "native" mode and used only to represent
2938 * the main root device. We allow writing 0 to reset the device back
2939 * to normal mode after being used as a subordinate channel.
2940 */
2941 if (channel > S16_MAX)
2942 return -EINVAL;
2943
2944 dev->num_tc = -channel;
2945
2946 return 0;
2947}
2948EXPORT_SYMBOL(netdev_set_sb_channel);
2949
John Fastabendf0796d52010-07-01 13:21:57 +00002950/*
2951 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002952 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002953 */
Tom Herberte6484932010-10-18 18:04:39 +00002954int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002955{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002956 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002957 int rc;
2958
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002959 disabling = txq < dev->real_num_tx_queues;
2960
Tom Herberte6484932010-10-18 18:04:39 +00002961 if (txq < 1 || txq > dev->num_tx_queues)
2962 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002963
Ben Hutchings5c565802011-02-15 19:39:21 +00002964 if (dev->reg_state == NETREG_REGISTERED ||
2965 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002966 ASSERT_RTNL();
2967
Tom Herbert1d24eb42010-11-21 13:17:27 +00002968 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2969 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002970 if (rc)
2971 return rc;
2972
John Fastabend4f57c082011-01-17 08:06:04 +00002973 if (dev->num_tc)
2974 netif_setup_tc(dev, txq);
2975
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002976 dev->real_num_tx_queues = txq;
2977
2978 if (disabling) {
2979 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002980 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002981#ifdef CONFIG_XPS
2982 netif_reset_xps_queues_gt(dev, txq);
2983#endif
2984 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002985 } else {
2986 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002987 }
Tom Herberte6484932010-10-18 18:04:39 +00002988
Tom Herberte6484932010-10-18 18:04:39 +00002989 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002990}
2991EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002992
Michael Daltona953be52014-01-16 22:23:28 -08002993#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002994/**
2995 * netif_set_real_num_rx_queues - set actual number of RX queues used
2996 * @dev: Network device
2997 * @rxq: Actual number of RX queues
2998 *
2999 * This must be called either with the rtnl_lock held or before
3000 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07003001 * negative error code. If called before registration, it always
3002 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003003 */
3004int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
3005{
3006 int rc;
3007
Tom Herbertbd25fa72010-10-18 18:00:16 +00003008 if (rxq < 1 || rxq > dev->num_rx_queues)
3009 return -EINVAL;
3010
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003011 if (dev->reg_state == NETREG_REGISTERED) {
3012 ASSERT_RTNL();
3013
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003014 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
3015 rxq);
3016 if (rc)
3017 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003018 }
3019
3020 dev->real_num_rx_queues = rxq;
3021 return 0;
3022}
3023EXPORT_SYMBOL(netif_set_real_num_rx_queues);
3024#endif
3025
Ben Hutchings2c530402012-07-10 10:55:09 +00003026/**
3027 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00003028 *
3029 * This routine should set an upper limit on the number of RSS queues
3030 * used by default by multiqueue devices.
3031 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00003032int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00003033{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05303034 return is_kdump_kernel() ?
3035 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00003036}
3037EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3038
Eric Dumazet3bcb8462016-06-04 20:02:28 -07003039static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003040{
3041 struct softnet_data *sd;
3042 unsigned long flags;
3043
3044 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05003045 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00003046 q->next_sched = NULL;
3047 *sd->output_queue_tailp = q;
3048 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003049 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3050 local_irq_restore(flags);
3051}
3052
David S. Miller37437bb2008-07-16 02:15:04 -07003053void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08003054{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003055 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3056 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08003057}
3058EXPORT_SYMBOL(__netif_schedule);
3059
Eric Dumazete6247022013-12-05 04:45:08 -08003060struct dev_kfree_skb_cb {
3061 enum skb_free_reason reason;
3062};
3063
3064static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08003065{
Eric Dumazete6247022013-12-05 04:45:08 -08003066 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08003067}
Denis Vlasenko56079432006-03-29 15:57:29 -08003068
John Fastabend46e5da40a2014-09-12 20:04:52 -07003069void netif_schedule_queue(struct netdev_queue *txq)
3070{
3071 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03003072 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07003073 struct Qdisc *q = rcu_dereference(txq->qdisc);
3074
3075 __netif_schedule(q);
3076 }
3077 rcu_read_unlock();
3078}
3079EXPORT_SYMBOL(netif_schedule_queue);
3080
John Fastabend46e5da40a2014-09-12 20:04:52 -07003081void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3082{
3083 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3084 struct Qdisc *q;
3085
3086 rcu_read_lock();
3087 q = rcu_dereference(dev_queue->qdisc);
3088 __netif_schedule(q);
3089 rcu_read_unlock();
3090 }
3091}
3092EXPORT_SYMBOL(netif_tx_wake_queue);
3093
Eric Dumazete6247022013-12-05 04:45:08 -08003094void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3095{
3096 unsigned long flags;
3097
Myungho Jung98998862017-04-25 11:58:15 -07003098 if (unlikely(!skb))
3099 return;
3100
Reshetova, Elena63354792017-06-30 13:07:58 +03003101 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003102 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003103 refcount_set(&skb->users, 0);
3104 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003105 return;
3106 }
3107 get_kfree_skb_cb(skb)->reason = reason;
3108 local_irq_save(flags);
3109 skb->next = __this_cpu_read(softnet_data.completion_queue);
3110 __this_cpu_write(softnet_data.completion_queue, skb);
3111 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3112 local_irq_restore(flags);
3113}
3114EXPORT_SYMBOL(__dev_kfree_skb_irq);
3115
3116void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003117{
3118 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003119 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003120 else
3121 dev_kfree_skb(skb);
3122}
Eric Dumazete6247022013-12-05 04:45:08 -08003123EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003124
3125
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003126/**
3127 * netif_device_detach - mark device as removed
3128 * @dev: network device
3129 *
3130 * Mark device as removed from system and therefore no longer available.
3131 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003132void netif_device_detach(struct net_device *dev)
3133{
3134 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3135 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003136 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003137 }
3138}
3139EXPORT_SYMBOL(netif_device_detach);
3140
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003141/**
3142 * netif_device_attach - mark device as attached
3143 * @dev: network device
3144 *
3145 * Mark device as attached from system and restart if needed.
3146 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003147void netif_device_attach(struct net_device *dev)
3148{
3149 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3150 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003151 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003152 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003153 }
3154}
3155EXPORT_SYMBOL(netif_device_attach);
3156
Jiri Pirko5605c762015-05-12 14:56:12 +02003157/*
3158 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3159 * to be used as a distribution range.
3160 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003161static u16 skb_tx_hash(const struct net_device *dev,
3162 const struct net_device *sb_dev,
3163 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003164{
3165 u32 hash;
3166 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003167 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003168
Alexander Duyckeadec8772018-07-09 12:19:48 -04003169 if (dev->num_tc) {
3170 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3171
3172 qoffset = sb_dev->tc_to_txq[tc].offset;
3173 qcount = sb_dev->tc_to_txq[tc].count;
3174 }
3175
Jiri Pirko5605c762015-05-12 14:56:12 +02003176 if (skb_rx_queue_recorded(skb)) {
3177 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003178 if (hash >= qoffset)
3179 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003180 while (unlikely(hash >= qcount))
3181 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003182 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003183 }
3184
3185 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3186}
Jiri Pirko5605c762015-05-12 14:56:12 +02003187
Ben Hutchings36c92472012-01-17 07:57:56 +00003188static void skb_warn_bad_offload(const struct sk_buff *skb)
3189{
Wei Tang84d15ae2016-06-16 21:17:49 +08003190 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003191 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003192 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003193
Ben Greearc846ad92013-04-19 10:45:52 +00003194 if (!net_ratelimit())
3195 return;
3196
Bjørn Mork88ad4172015-11-16 19:16:40 +01003197 if (dev) {
3198 if (dev->dev.parent)
3199 name = dev_driver_string(dev->dev.parent);
3200 else
3201 name = netdev_name(dev);
3202 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003203 skb_dump(KERN_WARNING, skb, false);
3204 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003205 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003206 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003207}
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209/*
3210 * Invalidate hardware checksum when packet is to be mangled, and
3211 * complete checksum manually on outgoing path.
3212 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003213int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Al Virod3bc23e2006-11-14 21:24:49 -08003215 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003216 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Patrick McHardy84fa7932006-08-29 16:44:56 -07003218 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003219 goto out_set_summed;
3220
3221 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003222 skb_warn_bad_offload(skb);
3223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
Eric Dumazetcef401d2013-01-25 20:34:37 +00003226 /* Before computing a checksum, we should make sure no frag could
3227 * be modified by an external entity : checksum could be wrong.
3228 */
3229 if (skb_has_shared_frag(skb)) {
3230 ret = __skb_linearize(skb);
3231 if (ret)
3232 goto out;
3233 }
3234
Michał Mirosław55508d62010-12-14 15:24:08 +00003235 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003236 BUG_ON(offset >= skb_headlen(skb));
3237 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3238
3239 offset += skb->csum_offset;
3240 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3241
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003242 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3243 if (ret)
3244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003246 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003247out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003249out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return ret;
3251}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003252EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Davide Carattib72b5bf2017-05-18 15:44:38 +02003254int skb_crc32c_csum_help(struct sk_buff *skb)
3255{
3256 __le32 crc32c_csum;
3257 int ret = 0, offset, start;
3258
3259 if (skb->ip_summed != CHECKSUM_PARTIAL)
3260 goto out;
3261
3262 if (unlikely(skb_is_gso(skb)))
3263 goto out;
3264
3265 /* Before computing a checksum, we should make sure no frag could
3266 * be modified by an external entity : checksum could be wrong.
3267 */
3268 if (unlikely(skb_has_shared_frag(skb))) {
3269 ret = __skb_linearize(skb);
3270 if (ret)
3271 goto out;
3272 }
3273 start = skb_checksum_start_offset(skb);
3274 offset = start + offsetof(struct sctphdr, checksum);
3275 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3276 ret = -EINVAL;
3277 goto out;
3278 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003279
3280 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3281 if (ret)
3282 goto out;
3283
Davide Carattib72b5bf2017-05-18 15:44:38 +02003284 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3285 skb->len - start, ~(__u32)0,
3286 crc32c_csum_stub));
3287 *(__le32 *)(skb->data + offset) = crc32c_csum;
3288 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003289 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003290out:
3291 return ret;
3292}
3293
Vlad Yasevich53d64712014-03-27 17:26:18 -04003294__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003295{
3296 __be16 type = skb->protocol;
3297
Pravin B Shelar19acc322013-05-07 20:41:07 +00003298 /* Tunnel gso handlers can set protocol to ethernet. */
3299 if (type == htons(ETH_P_TEB)) {
3300 struct ethhdr *eth;
3301
3302 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3303 return 0;
3304
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003305 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003306 type = eth->h_proto;
3307 }
3308
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003309 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003310}
3311
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003312/**
3313 * skb_mac_gso_segment - mac layer segmentation handler.
3314 * @skb: buffer to segment
3315 * @features: features for the output path (see dev->features)
3316 */
3317struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3318 netdev_features_t features)
3319{
3320 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3321 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003322 int vlan_depth = skb->mac_len;
3323 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003324
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003325 if (unlikely(!type))
3326 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003327
Vlad Yasevich53d64712014-03-27 17:26:18 -04003328 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003329
3330 rcu_read_lock();
3331 list_for_each_entry_rcu(ptype, &offload_base, list) {
3332 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003333 segs = ptype->callbacks.gso_segment(skb, features);
3334 break;
3335 }
3336 }
3337 rcu_read_unlock();
3338
3339 __skb_push(skb, skb->data - skb_mac_header(skb));
3340
3341 return segs;
3342}
3343EXPORT_SYMBOL(skb_mac_gso_segment);
3344
3345
Cong Wang12b00042013-02-05 16:36:38 +00003346/* openvswitch calls this on rx path, so we need a different check.
3347 */
3348static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3349{
3350 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003351 return skb->ip_summed != CHECKSUM_PARTIAL &&
3352 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003353
3354 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003355}
3356
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003357/**
Cong Wang12b00042013-02-05 16:36:38 +00003358 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003359 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003360 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003361 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003362 *
3363 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003364 *
3365 * It may return NULL if the skb requires no segmentation. This is
3366 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003367 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003368 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003369 */
Cong Wang12b00042013-02-05 16:36:38 +00003370struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3371 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003372{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003373 struct sk_buff *segs;
3374
Cong Wang12b00042013-02-05 16:36:38 +00003375 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003376 int err;
3377
Eric Dumazetb2504a52017-01-31 10:20:32 -08003378 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003379 err = skb_cow_head(skb, 0);
3380 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003381 return ERR_PTR(err);
3382 }
3383
Alexander Duyck802ab552016-04-10 21:45:03 -04003384 /* Only report GSO partial support if it will enable us to
3385 * support segmentation on this frame without needing additional
3386 * work.
3387 */
3388 if (features & NETIF_F_GSO_PARTIAL) {
3389 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3390 struct net_device *dev = skb->dev;
3391
3392 partial_features |= dev->features & dev->gso_partial_features;
3393 if (!skb_gso_ok(skb, features | partial_features))
3394 features &= ~NETIF_F_GSO_PARTIAL;
3395 }
3396
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003397 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003398 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3399
Pravin B Shelar68c33162013-02-14 14:02:41 +00003400 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003401 SKB_GSO_CB(skb)->encap_level = 0;
3402
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003403 skb_reset_mac_header(skb);
3404 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003405
Eric Dumazetb2504a52017-01-31 10:20:32 -08003406 segs = skb_mac_gso_segment(skb, features);
3407
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003408 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003409 skb_warn_bad_offload(skb);
3410
3411 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003412}
Cong Wang12b00042013-02-05 16:36:38 +00003413EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003414
Herbert Xufb286bb2005-11-10 13:01:24 -08003415/* Take action when hardware reception checksum errors are detected. */
3416#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003417void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003418{
3419 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003420 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Willem de Bruijn64131392019-07-07 05:51:55 -04003421 skb_dump(KERN_ERR, skb, true);
Herbert Xufb286bb2005-11-10 13:01:24 -08003422 dump_stack();
3423 }
3424}
3425EXPORT_SYMBOL(netdev_rx_csum_fault);
3426#endif
3427
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003428/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003429static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003431#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 int i;
tchardingf4563a72017-02-09 17:56:07 +11003433
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003434 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003435 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3436 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003437
Ian Campbellea2ab692011-08-22 23:44:58 +00003438 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003439 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003440 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003441 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return 0;
3444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Simon Horman3b392dd2014-06-04 08:53:17 +09003446/* If MPLS offload request, verify we are testing hardware MPLS features
3447 * instead of standard features for the netdev.
3448 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003449#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003450static netdev_features_t net_mpls_features(struct sk_buff *skb,
3451 netdev_features_t features,
3452 __be16 type)
3453{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003454 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003455 features &= skb->dev->mpls_features;
3456
3457 return features;
3458}
3459#else
3460static netdev_features_t net_mpls_features(struct sk_buff *skb,
3461 netdev_features_t features,
3462 __be16 type)
3463{
3464 return features;
3465}
3466#endif
3467
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003468static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003469 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003470{
Simon Horman3b392dd2014-06-04 08:53:17 +09003471 __be16 type;
3472
Miaohe Lin9fc95f52020-07-30 19:02:36 +08003473 type = skb_network_protocol(skb, NULL);
Simon Horman3b392dd2014-06-04 08:53:17 +09003474 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003475
Ed Cashinc0d680e2012-09-19 15:49:00 +00003476 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003477 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003478 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003479 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003480 if (illegal_highdma(skb->dev, skb))
3481 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003482
3483 return features;
3484}
3485
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003486netdev_features_t passthru_features_check(struct sk_buff *skb,
3487 struct net_device *dev,
3488 netdev_features_t features)
3489{
3490 return features;
3491}
3492EXPORT_SYMBOL(passthru_features_check);
3493
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003494static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003495 struct net_device *dev,
3496 netdev_features_t features)
3497{
3498 return vlan_features_check(skb, features);
3499}
3500
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003501static netdev_features_t gso_features_check(const struct sk_buff *skb,
3502 struct net_device *dev,
3503 netdev_features_t features)
3504{
3505 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3506
3507 if (gso_segs > dev->gso_max_segs)
3508 return features & ~NETIF_F_GSO_MASK;
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{
3622 if (unlikely(skb->csum_not_inet))
3623 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3624 skb_crc32c_csum_help(skb);
3625
3626 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3627}
3628EXPORT_SYMBOL(skb_csum_hwoffload_help);
3629
Steffen Klassertf53c7232017-12-20 10:41:36 +01003630static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003631{
3632 netdev_features_t features;
3633
David S. Millereae3f882014-08-30 15:17:13 -07003634 features = netif_skb_features(skb);
3635 skb = validate_xmit_vlan(skb, features);
3636 if (unlikely(!skb))
3637 goto out_null;
3638
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003639 skb = sk_validate_xmit_skb(skb, dev);
3640 if (unlikely(!skb))
3641 goto out_null;
3642
Johannes Berg8b86a612015-04-17 15:45:04 +02003643 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003644 struct sk_buff *segs;
3645
3646 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003647 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003648 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003649 } else if (segs) {
3650 consume_skb(skb);
3651 skb = segs;
3652 }
David S. Millereae3f882014-08-30 15:17:13 -07003653 } else {
3654 if (skb_needs_linearize(skb, features) &&
3655 __skb_linearize(skb))
3656 goto out_kfree_skb;
3657
3658 /* If packet is not checksummed and device does not
3659 * support checksumming for this protocol, complete
3660 * checksumming here.
3661 */
3662 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3663 if (skb->encapsulation)
3664 skb_set_inner_transport_header(skb,
3665 skb_checksum_start_offset(skb));
3666 else
3667 skb_set_transport_header(skb,
3668 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003669 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003670 goto out_kfree_skb;
3671 }
3672 }
3673
Steffen Klassertf53c7232017-12-20 10:41:36 +01003674 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003675
David S. Millereae3f882014-08-30 15:17:13 -07003676 return skb;
3677
3678out_kfree_skb:
3679 kfree_skb(skb);
3680out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003681 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003682 return NULL;
3683}
3684
Steffen Klassertf53c7232017-12-20 10:41:36 +01003685struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003686{
3687 struct sk_buff *next, *head = NULL, *tail;
3688
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003689 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003690 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003691 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003692
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003693 /* in case skb wont be segmented, point to itself */
3694 skb->prev = skb;
3695
Steffen Klassertf53c7232017-12-20 10:41:36 +01003696 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003697 if (!skb)
3698 continue;
3699
3700 if (!head)
3701 head = skb;
3702 else
3703 tail->next = skb;
3704 /* If skb was segmented, skb->prev points to
3705 * the last segment. If not, it still contains skb.
3706 */
3707 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003708 }
3709 return head;
3710}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003711EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003712
Eric Dumazet1def9232013-01-10 12:36:42 +00003713static void qdisc_pkt_len_init(struct sk_buff *skb)
3714{
3715 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3716
3717 qdisc_skb_cb(skb)->pkt_len = skb->len;
3718
3719 /* To get more precise estimation of bytes sent on wire,
3720 * we add to pkt_len the headers size of all segments
3721 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003722 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003723 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003724 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003725
Eric Dumazet757b8b12013-01-15 21:14:21 -08003726 /* mac layer + network layer */
3727 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3728
3729 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003730 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3731 const struct tcphdr *th;
3732 struct tcphdr _tcphdr;
3733
3734 th = skb_header_pointer(skb, skb_transport_offset(skb),
3735 sizeof(_tcphdr), &_tcphdr);
3736 if (likely(th))
3737 hdr_len += __tcp_hdrlen(th);
3738 } else {
3739 struct udphdr _udphdr;
3740
3741 if (skb_header_pointer(skb, skb_transport_offset(skb),
3742 sizeof(_udphdr), &_udphdr))
3743 hdr_len += sizeof(struct udphdr);
3744 }
Jason Wang15e5a032013-03-25 20:19:59 +00003745
3746 if (shinfo->gso_type & SKB_GSO_DODGY)
3747 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3748 shinfo->gso_size);
3749
3750 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003751 }
3752}
3753
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003754static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3755 struct net_device *dev,
3756 struct netdev_queue *txq)
3757{
3758 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003759 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003760 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003761 int rc;
3762
Eric Dumazeta2da5702011-01-20 03:48:19 +00003763 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003764
3765 if (q->flags & TCQ_F_NOLOCK) {
Petr Machataac5c66f2020-07-14 20:03:08 +03003766 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Yunsheng Linf9fc21e2021-05-14 11:17:01 +08003767 if (likely(!netif_xmit_frozen_or_stopped(txq)))
3768 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003769
3770 if (unlikely(to_free))
3771 kfree_skb_list(to_free);
3772 return rc;
3773 }
3774
Eric Dumazet79640a42010-06-02 05:09:29 -07003775 /*
3776 * Heuristic to force contended enqueues to serialize on a
3777 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003778 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003779 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003780 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003781 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003782 if (unlikely(contended))
3783 spin_lock(&q->busylock);
3784
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003785 spin_lock(root_lock);
3786 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003787 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003788 rc = NET_XMIT_DROP;
3789 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003790 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003791 /*
3792 * This is a work-conserving queue; there are no old skbs
3793 * waiting to be sent out; and the qdisc is not running -
3794 * xmit the skb directly.
3795 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003796
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003797 qdisc_bstats_update(q, skb);
3798
Eric Dumazet55a93b32014-10-03 15:31:07 -07003799 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003800 if (unlikely(contended)) {
3801 spin_unlock(&q->busylock);
3802 contended = false;
3803 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003804 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003805 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003806
John Fastabend6c148182017-12-07 09:54:06 -08003807 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003808 rc = NET_XMIT_SUCCESS;
3809 } else {
Petr Machataac5c66f2020-07-14 20:03:08 +03003810 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003811 if (qdisc_run_begin(q)) {
3812 if (unlikely(contended)) {
3813 spin_unlock(&q->busylock);
3814 contended = false;
3815 }
3816 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003817 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003818 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003819 }
3820 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003821 if (unlikely(to_free))
3822 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003823 if (unlikely(contended))
3824 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003825 return rc;
3826}
3827
Daniel Borkmann86f85152013-12-29 17:27:11 +01003828#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003829static void skb_update_prio(struct sk_buff *skb)
3830{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003831 const struct netprio_map *map;
3832 const struct sock *sk;
3833 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003834
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003835 if (skb->priority)
3836 return;
3837 map = rcu_dereference_bh(skb->dev->priomap);
3838 if (!map)
3839 return;
3840 sk = skb_to_full_sk(skb);
3841 if (!sk)
3842 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003843
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003844 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3845
3846 if (prioidx < map->priomap_len)
3847 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003848}
3849#else
3850#define skb_update_prio(skb)
3851#endif
3852
Dave Jonesd29f7492008-07-22 14:09:06 -07003853/**
Michel Machado95603e22012-06-12 10:16:35 +00003854 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003855 * @net: network namespace this loopback is happening in
3856 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003857 * @skb: buffer to transmit
3858 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003859int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003860{
3861 skb_reset_mac_header(skb);
3862 __skb_pull(skb, skb_network_offset(skb));
3863 skb->pkt_type = PACKET_LOOPBACK;
3864 skb->ip_summed = CHECKSUM_UNNECESSARY;
3865 WARN_ON(!skb_dst(skb));
3866 skb_dst_force(skb);
3867 netif_rx_ni(skb);
3868 return 0;
3869}
3870EXPORT_SYMBOL(dev_loopback_xmit);
3871
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003872#ifdef CONFIG_NET_EGRESS
3873static struct sk_buff *
3874sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3875{
Jiri Pirko46209402017-11-03 11:46:25 +01003876 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003877 struct tcf_result cl_res;
3878
Jiri Pirko46209402017-11-03 11:46:25 +01003879 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003880 return skb;
3881
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003882 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
wenxu496ef462020-11-25 12:01:21 +08003883 qdisc_skb_cb(skb)->mru = 0;
Jiri Pirko46209402017-11-03 11:46:25 +01003884 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003885
Jiri Pirko46209402017-11-03 11:46:25 +01003886 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003887 case TC_ACT_OK:
3888 case TC_ACT_RECLASSIFY:
3889 skb->tc_index = TC_H_MIN(cl_res.classid);
3890 break;
3891 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003892 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003893 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003894 kfree_skb(skb);
3895 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003896 case TC_ACT_STOLEN:
3897 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003898 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003899 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003900 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003901 return NULL;
3902 case TC_ACT_REDIRECT:
3903 /* No need to push/pop skb's mac_header here on egress! */
3904 skb_do_redirect(skb);
3905 *ret = NET_XMIT_SUCCESS;
3906 return NULL;
3907 default:
3908 break;
3909 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003910
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003911 return skb;
3912}
3913#endif /* CONFIG_NET_EGRESS */
3914
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003915#ifdef CONFIG_XPS
3916static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3917 struct xps_dev_maps *dev_maps, unsigned int tci)
3918{
3919 struct xps_map *map;
3920 int queue_index = -1;
3921
3922 if (dev->num_tc) {
3923 tci *= dev->num_tc;
3924 tci += netdev_get_prio_tc_map(dev, skb->priority);
3925 }
3926
3927 map = rcu_dereference(dev_maps->attr_map[tci]);
3928 if (map) {
3929 if (map->len == 1)
3930 queue_index = map->queues[0];
3931 else
3932 queue_index = map->queues[reciprocal_scale(
3933 skb_get_hash(skb), map->len)];
3934 if (unlikely(queue_index >= dev->real_num_tx_queues))
3935 queue_index = -1;
3936 }
3937 return queue_index;
3938}
3939#endif
3940
Alexander Duyckeadec8772018-07-09 12:19:48 -04003941static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3942 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003943{
3944#ifdef CONFIG_XPS
3945 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003946 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003947 int queue_index = -1;
3948
Amritha Nambiar04157462018-06-29 21:26:46 -07003949 if (!static_key_false(&xps_needed))
3950 return -1;
3951
Jiri Pirko638b2a62015-05-12 14:56:13 +02003952 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003953 if (!static_key_false(&xps_rxqs_needed))
3954 goto get_cpus_map;
3955
Alexander Duyckeadec8772018-07-09 12:19:48 -04003956 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003957 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003958 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003959
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003960 if (tci >= 0 && tci < dev->num_rx_queues)
3961 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3962 tci);
3963 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003964
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003965get_cpus_map:
3966 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003967 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003968 if (dev_maps) {
3969 unsigned int tci = skb->sender_cpu - 1;
3970
3971 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3972 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003973 }
3974 }
3975 rcu_read_unlock();
3976
3977 return queue_index;
3978#else
3979 return -1;
3980#endif
3981}
3982
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003983u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003984 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003985{
3986 return 0;
3987}
3988EXPORT_SYMBOL(dev_pick_tx_zero);
3989
3990u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003991 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003992{
3993 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3994}
3995EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3996
Paolo Abenib71b5832019-03-20 11:02:05 +01003997u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3998 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003999{
4000 struct sock *sk = skb->sk;
4001 int queue_index = sk_tx_queue_get(sk);
4002
Alexander Duyckeadec8772018-07-09 12:19:48 -04004003 sb_dev = sb_dev ? : dev;
4004
Jiri Pirko638b2a62015-05-12 14:56:13 +02004005 if (queue_index < 0 || skb->ooo_okay ||
4006 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04004007 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11004008
Jiri Pirko638b2a62015-05-12 14:56:13 +02004009 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04004010 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004011
4012 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07004013 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02004014 rcu_access_pointer(sk->sk_dst_cache))
4015 sk_tx_queue_set(sk, new_index);
4016
4017 queue_index = new_index;
4018 }
4019
4020 return queue_index;
4021}
Paolo Abenib71b5832019-03-20 11:02:05 +01004022EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004023
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004024struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4025 struct sk_buff *skb,
4026 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004027{
4028 int queue_index = 0;
4029
4030#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08004031 u32 sender_cpu = skb->sender_cpu - 1;
4032
4033 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004034 skb->sender_cpu = raw_smp_processor_id() + 1;
4035#endif
4036
4037 if (dev->real_num_tx_queues != 1) {
4038 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11004039
Jiri Pirko638b2a62015-05-12 14:56:13 +02004040 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01004041 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004042 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004043 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004044
Alexander Duyckd5845272017-11-22 10:57:41 -08004045 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004046 }
4047
4048 skb_set_queue_mapping(skb, queue_index);
4049 return netdev_get_tx_queue(dev, queue_index);
4050}
4051
Michel Machado95603e22012-06-12 10:16:35 +00004052/**
Jason Wang9d08dd32014-01-20 11:25:13 +08004053 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07004054 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04004055 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07004056 *
4057 * Queue a buffer for transmission to a network device. The caller must
4058 * have set the device and priority and built the buffer before calling
4059 * this function. The function can be called from an interrupt.
4060 *
4061 * A negative errno code is returned on a failure. A success does not
4062 * guarantee the frame will be transmitted as it may be dropped due
4063 * to congestion or traffic shaping.
4064 *
4065 * -----------------------------------------------------------------------------------
4066 * I notice this method can also return errors from the queue disciplines,
4067 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4068 * be positive.
4069 *
4070 * Regardless of the return value, the skb is consumed, so it is currently
4071 * difficult to retry a send to this method. (You can bump the ref count
4072 * before sending to hold a reference for retry if you are careful.)
4073 *
4074 * When calling this method, interrupts MUST be enabled. This is because
4075 * the BH enable code must have IRQs enabled so that it will not deadlock.
4076 * --BLG
4077 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04004078static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
4080 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07004081 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 struct Qdisc *q;
4083 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01004084 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00004086 skb_reset_mac_header(skb);
4087
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004088 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4089 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
4090
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004091 /* Disable soft irqs for various locks below. Also
4092 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004094 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Neil Horman5bc14212011-11-22 05:10:51 +00004096 skb_update_prio(skb);
4097
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004098 qdisc_pkt_len_init(skb);
4099#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004100 skb->tc_at_ingress = 0;
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004101# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004102 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004103 skb = sch_handle_egress(skb, &rc, dev);
4104 if (!skb)
4105 goto out;
4106 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004107# endif
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004108#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004109 /* If device/qdisc don't need skb->dst, release it right now while
4110 * its hot in this cpu cache.
4111 */
4112 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4113 skb_dst_drop(skb);
4114 else
4115 skb_dst_force(skb);
4116
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004117 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004118 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004119
Koki Sanagicf66ba52010-08-23 18:45:02 +09004120 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004122 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125
4126 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004127 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
tchardingeb13da12017-02-09 17:56:06 +11004129 * Really, it is unlikely that netif_tx_lock protection is necessary
4130 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4131 * counters.)
4132 * However, it is possible, that they rely on protection
4133 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
tchardingeb13da12017-02-09 17:56:06 +11004135 * Check this and shot the lock. It is not prone from deadlocks.
4136 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 */
4138 if (dev->flags & IFF_UP) {
4139 int cpu = smp_processor_id(); /* ok because BHs are off */
4140
David S. Millerc773e842008-07-08 23:13:53 -07004141 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004142 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004143 goto recursion_alert;
4144
Steffen Klassertf53c7232017-12-20 10:41:36 +01004145 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004146 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004147 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004148
Willy Tarreau37447412020-08-10 10:27:42 +02004149 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
David S. Millerc773e842008-07-08 23:13:53 -07004150 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Tom Herbert734664982011-11-28 16:32:44 +00004152 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004153 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004154 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004155 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004156 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004157 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 goto out;
4159 }
4160 }
David S. Millerc773e842008-07-08 23:13:53 -07004161 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004162 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4163 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 } else {
4165 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004166 * unfortunately
4167 */
4168recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004169 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4170 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
4172 }
4173
4174 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004175 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Eric Dumazet015f0682014-03-27 08:45:56 -07004177 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004178 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return rc;
4180out:
Herbert Xud4828d82006-06-22 02:28:18 -07004181 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return rc;
4183}
Jason Wangf663dd92014-01-10 16:18:26 +08004184
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004185int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004186{
4187 return __dev_queue_xmit(skb, NULL);
4188}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004189EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Alexander Duyckeadec8772018-07-09 12:19:48 -04004191int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004192{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004193 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004194}
4195EXPORT_SYMBOL(dev_queue_xmit_accel);
4196
Björn Töpel36ccdf82020-11-23 18:56:00 +01004197int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004198{
4199 struct net_device *dev = skb->dev;
4200 struct sk_buff *orig_skb = skb;
4201 struct netdev_queue *txq;
4202 int ret = NETDEV_TX_BUSY;
4203 bool again = false;
4204
4205 if (unlikely(!netif_running(dev) ||
4206 !netif_carrier_ok(dev)))
4207 goto drop;
4208
4209 skb = validate_xmit_skb_list(skb, dev, &again);
4210 if (skb != orig_skb)
4211 goto drop;
4212
4213 skb_set_queue_mapping(skb, queue_id);
4214 txq = skb_get_tx_queue(dev, skb);
Willy Tarreau37447412020-08-10 10:27:42 +02004215 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004216
4217 local_bh_disable();
4218
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004219 dev_xmit_recursion_inc();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004220 HARD_TX_LOCK(dev, txq, smp_processor_id());
4221 if (!netif_xmit_frozen_or_drv_stopped(txq))
4222 ret = netdev_start_xmit(skb, dev, txq, false);
4223 HARD_TX_UNLOCK(dev, txq);
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004224 dev_xmit_recursion_dec();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004225
4226 local_bh_enable();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004227 return ret;
4228drop:
4229 atomic_long_inc(&dev->tx_dropped);
4230 kfree_skb_list(skb);
4231 return NET_XMIT_DROP;
4232}
Björn Töpel36ccdf82020-11-23 18:56:00 +01004233EXPORT_SYMBOL(__dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
tchardingeb13da12017-02-09 17:56:06 +11004235/*************************************************************************
4236 * Receiver routines
4237 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004239int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004240EXPORT_SYMBOL(netdev_max_backlog);
4241
Eric Dumazet3b098e22010-05-15 23:57:10 -07004242int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004243int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004244/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4245unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004246int weight_p __read_mostly = 64; /* old backlog weight */
4247int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4248int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4249int dev_rx_weight __read_mostly = 64;
4250int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004251/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4252int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004254/* Called with irq disabled */
4255static inline void ____napi_schedule(struct softnet_data *sd,
4256 struct napi_struct *napi)
4257{
4258 list_add_tail(&napi->poll_list, &sd->poll_list);
4259 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4260}
4261
Eric Dumazetdf334542010-03-24 19:13:54 +00004262#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004263
4264/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004265struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004266EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004267u32 rps_cpu_mask __read_mostly;
4268EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004269
Eric Dumazetdc053602019-03-22 08:56:38 -07004270struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004271EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004272struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004273EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004274
Ben Hutchingsc4454772011-01-19 11:03:53 +00004275static struct rps_dev_flow *
4276set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4277 struct rps_dev_flow *rflow, u16 next_cpu)
4278{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004279 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004280#ifdef CONFIG_RFS_ACCEL
4281 struct netdev_rx_queue *rxqueue;
4282 struct rps_dev_flow_table *flow_table;
4283 struct rps_dev_flow *old_rflow;
4284 u32 flow_id;
4285 u16 rxq_index;
4286 int rc;
4287
4288 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004289 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4290 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004291 goto out;
4292 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4293 if (rxq_index == skb_get_rx_queue(skb))
4294 goto out;
4295
4296 rxqueue = dev->_rx + rxq_index;
4297 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4298 if (!flow_table)
4299 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004300 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004301 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4302 rxq_index, flow_id);
4303 if (rc < 0)
4304 goto out;
4305 old_rflow = rflow;
4306 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004307 rflow->filter = rc;
4308 if (old_rflow->filter == rflow->filter)
4309 old_rflow->filter = RPS_NO_FILTER;
4310 out:
4311#endif
4312 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004313 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004314 }
4315
Ben Hutchings09994d12011-10-03 04:42:46 +00004316 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004317 return rflow;
4318}
4319
Tom Herbert0a9627f2010-03-16 08:03:29 +00004320/*
4321 * get_rps_cpu is called from netif_receive_skb and returns the target
4322 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004323 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004324 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004325static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4326 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004327{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004328 const struct rps_sock_flow_table *sock_flow_table;
4329 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004330 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004331 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004332 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004333 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004334 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004335
Tom Herbert0a9627f2010-03-16 08:03:29 +00004336 if (skb_rx_queue_recorded(skb)) {
4337 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004338
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004339 if (unlikely(index >= dev->real_num_rx_queues)) {
4340 WARN_ONCE(dev->real_num_rx_queues > 1,
4341 "%s received packet on queue %u, but number "
4342 "of RX queues is %u\n",
4343 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004344 goto done;
4345 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004346 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004347 }
4348
Eric Dumazet567e4b72015-02-06 12:59:01 -08004349 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4350
4351 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4352 map = rcu_dereference(rxqueue->rps_map);
4353 if (!flow_table && !map)
4354 goto done;
4355
Changli Gao2d47b452010-08-17 19:00:56 +00004356 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004357 hash = skb_get_hash(skb);
4358 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004359 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004360
Tom Herbertfec5e652010-04-16 16:01:27 -07004361 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4362 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004363 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004364 u32 next_cpu;
4365 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004366
Eric Dumazet567e4b72015-02-06 12:59:01 -08004367 /* First check into global flow table if there is a match */
4368 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4369 if ((ident ^ hash) & ~rps_cpu_mask)
4370 goto try_rps;
4371
4372 next_cpu = ident & rps_cpu_mask;
4373
4374 /* OK, now we know there is a match,
4375 * we can look at the local (per receive queue) flow table
4376 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004377 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004378 tcpu = rflow->cpu;
4379
Tom Herbertfec5e652010-04-16 16:01:27 -07004380 /*
4381 * If the desired CPU (where last recvmsg was done) is
4382 * different from current CPU (one in the rx-queue flow
4383 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004384 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004385 * - Current CPU is offline.
4386 * - The current CPU's queue tail has advanced beyond the
4387 * last packet that was enqueued using this table entry.
4388 * This guarantees that all previous packets for the flow
4389 * have been dequeued, thus preserving in order delivery.
4390 */
4391 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004392 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004393 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004394 rflow->last_qtail)) >= 0)) {
4395 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004396 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004397 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004398
Eric Dumazeta31196b2015-04-25 09:35:24 -07004399 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004400 *rflowp = rflow;
4401 cpu = tcpu;
4402 goto done;
4403 }
4404 }
4405
Eric Dumazet567e4b72015-02-06 12:59:01 -08004406try_rps:
4407
Tom Herbert0a9627f2010-03-16 08:03:29 +00004408 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004409 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004410 if (cpu_online(tcpu)) {
4411 cpu = tcpu;
4412 goto done;
4413 }
4414 }
4415
4416done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004417 return cpu;
4418}
4419
Ben Hutchingsc4454772011-01-19 11:03:53 +00004420#ifdef CONFIG_RFS_ACCEL
4421
4422/**
4423 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4424 * @dev: Device on which the filter was set
4425 * @rxq_index: RX queue index
4426 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4427 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4428 *
4429 * Drivers that implement ndo_rx_flow_steer() should periodically call
4430 * this function for each installed filter and remove the filters for
4431 * which it returns %true.
4432 */
4433bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4434 u32 flow_id, u16 filter_id)
4435{
4436 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4437 struct rps_dev_flow_table *flow_table;
4438 struct rps_dev_flow *rflow;
4439 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004440 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004441
4442 rcu_read_lock();
4443 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4444 if (flow_table && flow_id <= flow_table->mask) {
4445 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004446 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004447 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004448 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4449 rflow->last_qtail) <
4450 (int)(10 * flow_table->mask)))
4451 expire = false;
4452 }
4453 rcu_read_unlock();
4454 return expire;
4455}
4456EXPORT_SYMBOL(rps_may_expire_flow);
4457
4458#endif /* CONFIG_RFS_ACCEL */
4459
Tom Herbert0a9627f2010-03-16 08:03:29 +00004460/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004461static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004462{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004463 struct softnet_data *sd = data;
4464
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004465 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004466 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004467}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004468
Tom Herbertfec5e652010-04-16 16:01:27 -07004469#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004470
4471/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004472 * Check if this softnet_data structure is another cpu one
4473 * If yes, queue it to our IPI list and return 1
4474 * If no, return 0
4475 */
4476static int rps_ipi_queued(struct softnet_data *sd)
4477{
4478#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004479 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004480
4481 if (sd != mysd) {
4482 sd->rps_ipi_next = mysd->rps_ipi_list;
4483 mysd->rps_ipi_list = sd;
4484
4485 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4486 return 1;
4487 }
4488#endif /* CONFIG_RPS */
4489 return 0;
4490}
4491
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004492#ifdef CONFIG_NET_FLOW_LIMIT
4493int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4494#endif
4495
4496static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4497{
4498#ifdef CONFIG_NET_FLOW_LIMIT
4499 struct sd_flow_limit *fl;
4500 struct softnet_data *sd;
4501 unsigned int old_flow, new_flow;
4502
4503 if (qlen < (netdev_max_backlog >> 1))
4504 return false;
4505
Christoph Lameter903ceff2014-08-17 12:30:35 -05004506 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004507
4508 rcu_read_lock();
4509 fl = rcu_dereference(sd->flow_limit);
4510 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004511 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004512 old_flow = fl->history[fl->history_head];
4513 fl->history[fl->history_head] = new_flow;
4514
4515 fl->history_head++;
4516 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4517
4518 if (likely(fl->buckets[old_flow]))
4519 fl->buckets[old_flow]--;
4520
4521 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4522 fl->count++;
4523 rcu_read_unlock();
4524 return true;
4525 }
4526 }
4527 rcu_read_unlock();
4528#endif
4529 return false;
4530}
4531
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004532/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004533 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4534 * queue (may be a remote CPU queue).
4535 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004536static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4537 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004538{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004539 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004540 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004541 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004542
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004543 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004544
4545 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004546
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004547 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004548 if (!netif_running(skb->dev))
4549 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004550 qlen = skb_queue_len(&sd->input_pkt_queue);
4551 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004552 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004553enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004554 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004555 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004556 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004557 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004558 return NET_RX_SUCCESS;
4559 }
4560
Eric Dumazetebda37c22010-05-06 23:51:21 +00004561 /* Schedule NAPI for backlog device
4562 * We can use non atomic operation since we own the queue lock
4563 */
4564 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004565 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004566 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004567 }
4568 goto enqueue;
4569 }
4570
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004571drop:
Changli Gaodee42872010-05-02 05:42:16 +00004572 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004573 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004574
Tom Herbert0a9627f2010-03-16 08:03:29 +00004575 local_irq_restore(flags);
4576
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004577 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004578 kfree_skb(skb);
4579 return NET_RX_DROP;
4580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004582static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4583{
4584 struct net_device *dev = skb->dev;
4585 struct netdev_rx_queue *rxqueue;
4586
4587 rxqueue = dev->_rx;
4588
4589 if (skb_rx_queue_recorded(skb)) {
4590 u16 index = skb_get_rx_queue(skb);
4591
4592 if (unlikely(index >= dev->real_num_rx_queues)) {
4593 WARN_ONCE(dev->real_num_rx_queues > 1,
4594 "%s received packet on queue %u, but number "
4595 "of RX queues is %u\n",
4596 dev->name, index, dev->real_num_rx_queues);
4597
4598 return rxqueue; /* Return first rxqueue */
4599 }
4600 rxqueue += index;
4601 }
4602 return rxqueue;
4603}
4604
John Fastabendd4455162017-07-17 09:26:45 -07004605static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004606 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004607 struct bpf_prog *xdp_prog)
4608{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004609 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004610 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004611 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004612 __be16 orig_eth_type;
4613 struct ethhdr *eth;
4614 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004615 int hlen, off;
4616 u32 mac_len;
4617
4618 /* Reinjected packets coming from act_mirred or similar should
4619 * not get XDP generic processing.
4620 */
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01004621 if (skb_is_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004622 return XDP_PASS;
4623
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004624 /* XDP packets must be linear and must have sufficient headroom
4625 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4626 * native XDP provides, thus we need to do it here as well.
4627 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004628 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004629 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4630 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4631 int troom = skb->tail + skb->data_len - skb->end;
4632
4633 /* In case we have to go down the path and also linearize,
4634 * then lets do the pskb_expand_head() work just once here.
4635 */
4636 if (pskb_expand_head(skb,
4637 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4638 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4639 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004640 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004641 goto do_drop;
4642 }
John Fastabendd4455162017-07-17 09:26:45 -07004643
4644 /* The XDP program wants to see the packet starting at the MAC
4645 * header.
4646 */
4647 mac_len = skb->data - skb_mac_header(skb);
4648 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004649 xdp->data = skb->data - mac_len;
4650 xdp->data_meta = xdp->data;
4651 xdp->data_end = xdp->data + hlen;
4652 xdp->data_hard_start = skb->data - skb_headroom(skb);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004653
4654 /* SKB "head" area always have tailroom for skb_shared_info */
4655 xdp->frame_sz = (void *)skb_end_pointer(skb) - xdp->data_hard_start;
4656 xdp->frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4657
Björn Töpel02671e22018-05-02 13:01:30 +02004658 orig_data_end = xdp->data_end;
4659 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004660 eth = (struct ethhdr *)xdp->data;
4661 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4662 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004663
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004664 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004665 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004666
Björn Töpel02671e22018-05-02 13:01:30 +02004667 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004668
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004669 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004670 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004671 if (off) {
4672 if (off > 0)
4673 __skb_pull(skb, off);
4674 else if (off < 0)
4675 __skb_push(skb, -off);
4676
4677 skb->mac_header += off;
4678 skb_reset_network_header(skb);
4679 }
John Fastabendd4455162017-07-17 09:26:45 -07004680
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004681 /* check if bpf_xdp_adjust_tail was used */
4682 off = xdp->data_end - orig_data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004683 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004684 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004685 skb->len += off; /* positive on grow, negative on shrink */
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004686 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004687
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004688 /* check if XDP changed eth hdr such SKB needs update */
4689 eth = (struct ethhdr *)xdp->data;
4690 if ((orig_eth_type != eth->h_proto) ||
4691 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4692 __skb_push(skb, ETH_HLEN);
4693 skb->protocol = eth_type_trans(skb, skb->dev);
4694 }
4695
John Fastabendd4455162017-07-17 09:26:45 -07004696 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004697 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004698 case XDP_TX:
4699 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004700 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004701 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004702 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004703 if (metalen)
4704 skb_metadata_set(skb, metalen);
4705 break;
John Fastabendd4455162017-07-17 09:26:45 -07004706 default:
4707 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004708 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004709 case XDP_ABORTED:
4710 trace_xdp_exception(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004711 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004712 case XDP_DROP:
4713 do_drop:
4714 kfree_skb(skb);
4715 break;
4716 }
4717
4718 return act;
4719}
4720
4721/* When doing generic XDP we have to bypass the qdisc layer and the
4722 * network taps in order to match in-driver-XDP behavior.
4723 */
Jason Wang7c497472017-08-11 19:41:17 +08004724void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004725{
4726 struct net_device *dev = skb->dev;
4727 struct netdev_queue *txq;
4728 bool free_skb = true;
4729 int cpu, rc;
4730
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004731 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004732 cpu = smp_processor_id();
4733 HARD_TX_LOCK(dev, txq, cpu);
4734 if (!netif_xmit_stopped(txq)) {
4735 rc = netdev_start_xmit(skb, dev, txq, 0);
4736 if (dev_xmit_complete(rc))
4737 free_skb = false;
4738 }
4739 HARD_TX_UNLOCK(dev, txq);
4740 if (free_skb) {
4741 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4742 kfree_skb(skb);
4743 }
4744}
4745
Davidlohr Bueso02786472018-05-08 09:07:02 -07004746static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004747
Jason Wang7c497472017-08-11 19:41:17 +08004748int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004749{
John Fastabendd4455162017-07-17 09:26:45 -07004750 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004751 struct xdp_buff xdp;
4752 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004753 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004754
Björn Töpel02671e22018-05-02 13:01:30 +02004755 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004756 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004757 switch (act) {
4758 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004759 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004760 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004761 if (err)
4762 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004763 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004764 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004765 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004766 break;
4767 }
John Fastabendd4455162017-07-17 09:26:45 -07004768 return XDP_DROP;
4769 }
4770 }
4771 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004772out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004773 kfree_skb(skb);
4774 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004775}
Jason Wang7c497472017-08-11 19:41:17 +08004776EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004777
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004778static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Eric Dumazet588f0332011-11-15 04:12:55 +00004782 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Koki Sanagicf66ba52010-08-23 18:45:02 +09004784 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004785
Eric Dumazetdf334542010-03-24 19:13:54 +00004786#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004787 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004788 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004789 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Changli Gaocece1942010-08-07 20:35:43 -07004791 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004792 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004793
4794 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004795 if (cpu < 0)
4796 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004797
4798 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4799
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004800 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004801 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004802 } else
4803#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004804 {
4805 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004806
Tom Herbertfec5e652010-04-16 16:01:27 -07004807 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4808 put_cpu();
4809 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004812
4813/**
4814 * netif_rx - post buffer to the network code
4815 * @skb: buffer to post
4816 *
4817 * This function receives a packet from a device driver and queues it for
4818 * the upper (protocol) levels to process. It always succeeds. The buffer
4819 * may be dropped during processing for congestion control or by the
4820 * protocol layers.
4821 *
4822 * return values:
4823 * NET_RX_SUCCESS (no congestion)
4824 * NET_RX_DROP (packet was dropped)
4825 *
4826 */
4827
4828int netif_rx(struct sk_buff *skb)
4829{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004830 int ret;
4831
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004832 trace_netif_rx_entry(skb);
4833
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004834 ret = netif_rx_internal(skb);
4835 trace_netif_rx_exit(ret);
4836
4837 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004838}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004839EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841int netif_rx_ni(struct sk_buff *skb)
4842{
4843 int err;
4844
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004845 trace_netif_rx_ni_entry(skb);
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004848 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (local_softirq_pending())
4850 do_softirq();
4851 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004852 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 return err;
4855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856EXPORT_SYMBOL(netif_rx_ni);
4857
Sebastian Andrzej Siewiorc11171a2020-09-29 22:25:12 +02004858int netif_rx_any_context(struct sk_buff *skb)
4859{
4860 /*
4861 * If invoked from contexts which do not invoke bottom half
4862 * processing either at return from interrupt or when softrqs are
4863 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4864 * directly.
4865 */
4866 if (in_interrupt())
4867 return netif_rx(skb);
4868 else
4869 return netif_rx_ni(skb);
4870}
4871EXPORT_SYMBOL(netif_rx_any_context);
4872
Emese Revfy0766f782016-06-20 20:42:34 +02004873static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004875 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 if (sd->completion_queue) {
4878 struct sk_buff *clist;
4879
4880 local_irq_disable();
4881 clist = sd->completion_queue;
4882 sd->completion_queue = NULL;
4883 local_irq_enable();
4884
4885 while (clist) {
4886 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 clist = clist->next;
4889
Reshetova, Elena63354792017-06-30 13:07:58 +03004890 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004891 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4892 trace_consume_skb(skb);
4893 else
4894 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004895
4896 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4897 __kfree_skb(skb);
4898 else
4899 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004901
4902 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
4904
4905 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004906 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 local_irq_disable();
4909 head = sd->output_queue;
4910 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004911 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 local_irq_enable();
4913
Yunsheng Lin2f23d5b2021-05-14 11:17:00 +08004914 rcu_read_lock();
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004917 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004918 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 head = head->next_sched;
4921
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004922 /* We need to make sure head->next_sched is read
4923 * before clearing __QDISC_STATE_SCHED
4924 */
4925 smp_mb__before_atomic();
Yunsheng Lin2f23d5b2021-05-14 11:17:00 +08004926
4927 if (!(q->flags & TCQ_F_NOLOCK)) {
4928 root_lock = qdisc_lock(q);
4929 spin_lock(root_lock);
4930 } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
4931 &q->state))) {
4932 /* There is a synchronize_net() between
4933 * STATE_DEACTIVATED flag being set and
4934 * qdisc_reset()/some_qdisc_is_busy() in
4935 * dev_deactivate(), so we can safely bail out
4936 * early here to avoid data race between
4937 * qdisc_deactivate() and some_qdisc_is_busy()
4938 * for lockless qdisc.
4939 */
4940 clear_bit(__QDISC_STATE_SCHED, &q->state);
4941 continue;
4942 }
4943
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004944 clear_bit(__QDISC_STATE_SCHED, &q->state);
4945 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004946 if (root_lock)
4947 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 }
Yunsheng Lin2f23d5b2021-05-14 11:17:00 +08004949
4950 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004952
4953 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004956#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004957/* This hook is defined here for ATM LANE */
4958int (*br_fdb_test_addr_hook)(struct net_device *dev,
4959 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004960EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004963static inline struct sk_buff *
4964sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004965 struct net_device *orig_dev, bool *another)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004966{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004967#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004968 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004969 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004970
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004971 /* If there's at least one ingress present somewhere (so
4972 * we get here via enabled static key), remaining devices
4973 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004974 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004975 */
Jiri Pirko46209402017-11-03 11:46:25 +01004976 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004977 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004978
Herbert Xuf697c3e2007-10-14 00:38:47 -07004979 if (*pt_prev) {
4980 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4981 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004982 }
4983
Florian Westphal33654952015-05-14 00:36:28 +02004984 qdisc_skb_cb(skb)->pkt_len = skb->len;
wenxu496ef462020-11-25 12:01:21 +08004985 qdisc_skb_cb(skb)->mru = 0;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004986 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004987 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004988
Paul Blakey7d17c542020-02-16 12:01:22 +02004989 switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list,
4990 &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004991 case TC_ACT_OK:
4992 case TC_ACT_RECLASSIFY:
4993 skb->tc_index = TC_H_MIN(cl_res.classid);
4994 break;
4995 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004996 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004997 kfree_skb(skb);
4998 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004999 case TC_ACT_STOLEN:
5000 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02005001 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07005002 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005003 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005004 case TC_ACT_REDIRECT:
5005 /* skb_mac_header check was done by cls/act_bpf, so
5006 * we can safely push the L2 header back before
5007 * redirecting to another netdev
5008 */
5009 __skb_push(skb, skb->mac_len);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005010 if (skb_do_redirect(skb) == -EAGAIN) {
5011 __skb_pull(skb, skb->mac_len);
5012 *another = true;
5013 break;
5014 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07005015 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01005016 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02005017 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02005018 default:
5019 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07005020 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005021#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07005022 return skb;
5023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005025/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07005026 * netdev_is_rx_handler_busy - check if receive handler is registered
5027 * @dev: device to check
5028 *
5029 * Check if a receive handler is already registered for a given device.
5030 * Return true if there one.
5031 *
5032 * The caller must hold the rtnl_mutex.
5033 */
5034bool netdev_is_rx_handler_busy(struct net_device *dev)
5035{
5036 ASSERT_RTNL();
5037 return dev && rtnl_dereference(dev->rx_handler);
5038}
5039EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5040
5041/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005042 * netdev_rx_handler_register - register receive handler
5043 * @dev: device to register a handler for
5044 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00005045 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005046 *
Masanari Iidae2278672014-02-18 22:54:36 +09005047 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005048 * called from __netif_receive_skb. A negative errno code is returned
5049 * on a failure.
5050 *
5051 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005052 *
5053 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005054 */
5055int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00005056 rx_handler_func_t *rx_handler,
5057 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005058{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08005059 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005060 return -EBUSY;
5061
Paolo Abenif54262502018-03-09 10:39:24 +01005062 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5063 return -EINVAL;
5064
Eric Dumazet00cfec32013-03-29 03:01:22 +00005065 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00005066 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005067 rcu_assign_pointer(dev->rx_handler, rx_handler);
5068
5069 return 0;
5070}
5071EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5072
5073/**
5074 * netdev_rx_handler_unregister - unregister receive handler
5075 * @dev: device to unregister a handler from
5076 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00005077 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005078 *
5079 * The caller must hold the rtnl_mutex.
5080 */
5081void netdev_rx_handler_unregister(struct net_device *dev)
5082{
5083
5084 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005085 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00005086 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5087 * section has a guarantee to see a non NULL rx_handler_data
5088 * as well.
5089 */
5090 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005091 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005092}
5093EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5094
Mel Gormanb4b9e352012-07-31 16:44:26 -07005095/*
5096 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5097 * the special handling of PFMEMALLOC skbs.
5098 */
5099static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5100{
5101 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07005102 case htons(ETH_P_ARP):
5103 case htons(ETH_P_IP):
5104 case htons(ETH_P_IPV6):
5105 case htons(ETH_P_8021Q):
5106 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07005107 return true;
5108 default:
5109 return false;
5110 }
5111}
5112
Pablo Neirae687ad62015-05-13 18:19:38 +02005113static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5114 int *ret, struct net_device *orig_dev)
5115{
5116 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04005117 int ingress_retval;
5118
Pablo Neirae687ad62015-05-13 18:19:38 +02005119 if (*pt_prev) {
5120 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5121 *pt_prev = NULL;
5122 }
5123
Aaron Conole2c1e2702016-09-21 11:35:03 -04005124 rcu_read_lock();
5125 ingress_retval = nf_hook_ingress(skb);
5126 rcu_read_unlock();
5127 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02005128 }
5129 return 0;
5130}
Pablo Neirae687ad62015-05-13 18:19:38 +02005131
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005132static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
Edward Cree88eb1942018-07-02 16:13:56 +01005133 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
5135 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005136 rx_handler_func_t *rx_handler;
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005137 struct sk_buff *skb = *pskb;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07005138 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005139 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08005141 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
Eric Dumazet588f0332011-11-15 04:12:55 +00005143 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005144
Koki Sanagicf66ba52010-08-23 18:45:02 +09005145 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005146
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005147 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005148
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005149 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005150 if (!skb_transport_header_was_set(skb))
5151 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005152 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 pt_prev = NULL;
5155
David S. Miller63d8ea72011-02-28 10:48:59 -08005156another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005157 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005158
5159 __this_cpu_inc(softnet_data.processed);
5160
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005161 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5162 int ret2;
5163
5164 preempt_disable();
5165 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5166 preempt_enable();
5167
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005168 if (ret2 != XDP_PASS) {
5169 ret = NET_RX_DROP;
5170 goto out;
5171 }
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005172 skb_reset_mac_len(skb);
5173 }
5174
Patrick McHardy8ad227f2013-04-19 02:04:31 +00005175 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5176 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005177 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005178 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005179 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005180 }
5181
Willem de Bruijne7246e12017-01-07 17:06:35 -05005182 if (skb_skip_tc_classify(skb))
5183 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
David S. Miller9754e292013-02-14 15:57:38 -05005185 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005186 goto skip_taps;
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005189 if (pt_prev)
5190 ret = deliver_skb(skb, pt_prev, orig_dev);
5191 pt_prev = ptype;
5192 }
5193
5194 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5195 if (pt_prev)
5196 ret = deliver_skb(skb, pt_prev, orig_dev);
5197 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199
Mel Gormanb4b9e352012-07-31 16:44:26 -07005200skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005201#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005202 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005203 bool another = false;
5204
5205 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5206 &another);
5207 if (another)
5208 goto another_round;
Daniel Borkmann45771392015-04-10 23:07:54 +02005209 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005210 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005211
5212 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005213 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005214 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005215#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005216 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005217skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005218 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005219 goto drop;
5220
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005221 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005222 if (pt_prev) {
5223 ret = deliver_skb(skb, pt_prev, orig_dev);
5224 pt_prev = NULL;
5225 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005226 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005227 goto another_round;
5228 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005229 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005230 }
5231
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005232 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005233 if (rx_handler) {
5234 if (pt_prev) {
5235 ret = deliver_skb(skb, pt_prev, orig_dev);
5236 pt_prev = NULL;
5237 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005238 switch (rx_handler(&skb)) {
5239 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005240 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005241 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005242 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005243 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005244 case RX_HANDLER_EXACT:
5245 deliver_exact = true;
5246 case RX_HANDLER_PASS:
5247 break;
5248 default:
5249 BUG();
5250 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +03005253 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005254check_vlan_id:
5255 if (skb_vlan_tag_get_id(skb)) {
5256 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5257 * find vlan device.
5258 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005259 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005260 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5261 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5262 /* Outer header is 802.1P with vlan 0, inner header is
5263 * 802.1Q or 802.1AD and vlan_do_receive() above could
5264 * not find vlan dev for vlan id 0.
5265 */
5266 __vlan_hwaccel_clear_tag(skb);
5267 skb = skb_vlan_untag(skb);
5268 if (unlikely(!skb))
5269 goto out;
5270 if (vlan_do_receive(&skb))
5271 /* After stripping off 802.1P header with vlan 0
5272 * vlan dev is found for inner header.
5273 */
5274 goto another_round;
5275 else if (unlikely(!skb))
5276 goto out;
5277 else
5278 /* We have stripped outer 802.1P vlan 0 header.
5279 * But could not find vlan dev.
5280 * check again for vlan id to set OTHERHOST.
5281 */
5282 goto check_vlan_id;
5283 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005284 /* Note: we might in the future use prio bits
5285 * and set skb->priority like in vlan_do_receive()
5286 * For the time being, just ignore Priority Code Point
5287 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005288 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005289 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005292
5293 /* deliver only exact match when indicated */
5294 if (likely(!deliver_exact)) {
5295 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5296 &ptype_base[ntohs(type) &
5297 PTYPE_HASH_MASK]);
5298 }
5299
5300 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5301 &orig_dev->ptype_specific);
5302
5303 if (unlikely(skb->dev != orig_dev)) {
5304 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5305 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
5307
5308 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005309 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005310 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005311 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005313drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005314 if (!deliver_exact)
5315 atomic_long_inc(&skb->dev->rx_dropped);
5316 else
5317 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 kfree_skb(skb);
5319 /* Jamal, now you will not able to escape explaining
5320 * me how you were going to use this. :-)
5321 */
5322 ret = NET_RX_DROP;
5323 }
5324
Julian Anastasov2c17d272015-07-09 09:59:10 +03005325out:
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005326 /* The invariant here is that if *ppt_prev is not NULL
5327 * then skb should also be non-NULL.
5328 *
5329 * Apparently *ppt_prev assignment above holds this invariant due to
5330 * skb dereferencing near it.
5331 */
5332 *pskb = skb;
David S. Miller9754e292013-02-14 15:57:38 -05005333 return ret;
5334}
5335
Edward Cree88eb1942018-07-02 16:13:56 +01005336static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5337{
5338 struct net_device *orig_dev = skb->dev;
5339 struct packet_type *pt_prev = NULL;
5340 int ret;
5341
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005342 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree88eb1942018-07-02 16:13:56 +01005343 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005344 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5345 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005346 return ret;
5347}
5348
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005349/**
5350 * netif_receive_skb_core - special purpose version of netif_receive_skb
5351 * @skb: buffer to process
5352 *
5353 * More direct receive version of netif_receive_skb(). It should
5354 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005355 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005356 *
5357 * This function may only be called from softirq context and interrupts
5358 * should be enabled.
5359 *
5360 * Return values (usually ignored):
5361 * NET_RX_SUCCESS: no congestion
5362 * NET_RX_DROP: packet was dropped
5363 */
5364int netif_receive_skb_core(struct sk_buff *skb)
5365{
5366 int ret;
5367
5368 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005369 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005370 rcu_read_unlock();
5371
5372 return ret;
5373}
5374EXPORT_SYMBOL(netif_receive_skb_core);
5375
Edward Cree88eb1942018-07-02 16:13:56 +01005376static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5377 struct packet_type *pt_prev,
5378 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005379{
5380 struct sk_buff *skb, *next;
5381
Edward Cree88eb1942018-07-02 16:13:56 +01005382 if (!pt_prev)
5383 return;
5384 if (list_empty(head))
5385 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005386 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005387 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5388 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005389 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005390 list_for_each_entry_safe(skb, next, head, list) {
5391 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005392 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005393 }
Edward Cree88eb1942018-07-02 16:13:56 +01005394}
5395
5396static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5397{
5398 /* Fast-path assumptions:
5399 * - There is no RX handler.
5400 * - Only one packet_type matches.
5401 * If either of these fails, we will end up doing some per-packet
5402 * processing in-line, then handling the 'last ptype' for the whole
5403 * sublist. This can't cause out-of-order delivery to any single ptype,
5404 * because the 'last ptype' must be constant across the sublist, and all
5405 * other ptypes are handled per-packet.
5406 */
5407 /* Current (common) ptype of sublist */
5408 struct packet_type *pt_curr = NULL;
5409 /* Current (common) orig_dev of sublist */
5410 struct net_device *od_curr = NULL;
5411 struct list_head sublist;
5412 struct sk_buff *skb, *next;
5413
Edward Cree9af86f932018-07-09 18:10:19 +01005414 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005415 list_for_each_entry_safe(skb, next, head, list) {
5416 struct net_device *orig_dev = skb->dev;
5417 struct packet_type *pt_prev = NULL;
5418
Edward Cree22f6bbb2018-12-04 17:37:57 +00005419 skb_list_del_init(skb);
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005420 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree9af86f932018-07-09 18:10:19 +01005421 if (!pt_prev)
5422 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005423 if (pt_curr != pt_prev || od_curr != orig_dev) {
5424 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005425 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5426 /* start new sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005427 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005428 pt_curr = pt_prev;
5429 od_curr = orig_dev;
5430 }
Edward Cree9af86f932018-07-09 18:10:19 +01005431 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005432 }
5433
5434 /* dispatch final sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005435 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005436}
5437
David S. Miller9754e292013-02-14 15:57:38 -05005438static int __netif_receive_skb(struct sk_buff *skb)
5439{
5440 int ret;
5441
5442 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005443 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005444
5445 /*
5446 * PFMEMALLOC skbs are special, they should
5447 * - be delivered to SOCK_MEMALLOC sockets only
5448 * - stay away from userspace
5449 * - have bounded memory usage
5450 *
5451 * Use PF_MEMALLOC as this saves us from propagating the allocation
5452 * context down to all allocation sites.
5453 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005454 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005455 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005456 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005457 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005458 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return ret;
5461}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005462
Edward Cree4ce00172018-07-02 16:13:40 +01005463static void __netif_receive_skb_list(struct list_head *head)
5464{
5465 unsigned long noreclaim_flag = 0;
5466 struct sk_buff *skb, *next;
5467 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5468
5469 list_for_each_entry_safe(skb, next, head, list) {
5470 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5471 struct list_head sublist;
5472
5473 /* Handle the previous sublist */
5474 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005475 if (!list_empty(&sublist))
5476 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005477 pfmemalloc = !pfmemalloc;
5478 /* See comments in __netif_receive_skb */
5479 if (pfmemalloc)
5480 noreclaim_flag = memalloc_noreclaim_save();
5481 else
5482 memalloc_noreclaim_restore(noreclaim_flag);
5483 }
5484 }
5485 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005486 if (!list_empty(head))
5487 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005488 /* Restore pflags */
5489 if (pfmemalloc)
5490 memalloc_noreclaim_restore(noreclaim_flag);
5491}
5492
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005493static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005494{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005495 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005496 struct bpf_prog *new = xdp->prog;
5497 int ret = 0;
5498
David Ahernfbee97f2020-05-29 16:07:13 -06005499 if (new) {
5500 u32 i;
5501
YiFei Zhu984fe942020-09-15 16:45:39 -07005502 mutex_lock(&new->aux->used_maps_mutex);
5503
David Ahernfbee97f2020-05-29 16:07:13 -06005504 /* generic XDP does not work with DEVMAPs that can
5505 * have a bpf_prog installed on an entry
5506 */
5507 for (i = 0; i < new->aux->used_map_cnt; i++) {
YiFei Zhu984fe942020-09-15 16:45:39 -07005508 if (dev_map_can_have_prog(new->aux->used_maps[i]) ||
5509 cpu_map_prog_allowed(new->aux->used_maps[i])) {
5510 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005511 return -EINVAL;
YiFei Zhu984fe942020-09-15 16:45:39 -07005512 }
David Ahernfbee97f2020-05-29 16:07:13 -06005513 }
YiFei Zhu984fe942020-09-15 16:45:39 -07005514
5515 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005516 }
5517
David S. Millerb5cdae32017-04-18 15:36:58 -04005518 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005519 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005520 rcu_assign_pointer(dev->xdp_prog, new);
5521 if (old)
5522 bpf_prog_put(old);
5523
5524 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005525 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005526 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005527 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005528 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005529 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005530 }
5531 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005532
David S. Millerb5cdae32017-04-18 15:36:58 -04005533 default:
5534 ret = -EINVAL;
5535 break;
5536 }
5537
5538 return ret;
5539}
5540
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005541static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005542{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005543 int ret;
5544
Eric Dumazet588f0332011-11-15 04:12:55 +00005545 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005546
Richard Cochranc1f19b52010-07-17 08:49:36 +00005547 if (skb_defer_rx_timestamp(skb))
5548 return NET_RX_SUCCESS;
5549
John Fastabendbbbe2112017-09-08 14:00:30 -07005550 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005551#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005552 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005553 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005554 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005555
Eric Dumazet3b098e22010-05-15 23:57:10 -07005556 if (cpu >= 0) {
5557 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5558 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005559 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005560 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005561 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005562#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005563 ret = __netif_receive_skb(skb);
5564 rcu_read_unlock();
5565 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005566}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005567
Edward Cree7da517a2018-07-02 16:13:24 +01005568static void netif_receive_skb_list_internal(struct list_head *head)
5569{
Edward Cree7da517a2018-07-02 16:13:24 +01005570 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005571 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005572
Edward Cree8c057ef2018-07-09 18:09:54 +01005573 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005574 list_for_each_entry_safe(skb, next, head, list) {
5575 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005576 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005577 if (!skb_defer_rx_timestamp(skb))
5578 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005579 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005580 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005581
Edward Cree7da517a2018-07-02 16:13:24 +01005582 rcu_read_lock();
5583#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005584 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005585 list_for_each_entry_safe(skb, next, head, list) {
5586 struct rps_dev_flow voidflow, *rflow = &voidflow;
5587 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5588
5589 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005590 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005591 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005592 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005593 }
5594 }
5595 }
5596#endif
5597 __netif_receive_skb_list(head);
5598 rcu_read_unlock();
5599}
5600
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005601/**
5602 * netif_receive_skb - process receive buffer from network
5603 * @skb: buffer to process
5604 *
5605 * netif_receive_skb() is the main receive data processing function.
5606 * It always succeeds. The buffer may be dropped during processing
5607 * for congestion control or by the protocol layers.
5608 *
5609 * This function may only be called from softirq context and interrupts
5610 * should be enabled.
5611 *
5612 * Return values (usually ignored):
5613 * NET_RX_SUCCESS: no congestion
5614 * NET_RX_DROP: packet was dropped
5615 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005616int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005617{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005618 int ret;
5619
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005620 trace_netif_receive_skb_entry(skb);
5621
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005622 ret = netif_receive_skb_internal(skb);
5623 trace_netif_receive_skb_exit(ret);
5624
5625 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005626}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005627EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Edward Creef6ad8c12018-07-02 16:12:45 +01005629/**
5630 * netif_receive_skb_list - process many receive buffers from network
5631 * @head: list of skbs to process.
5632 *
Edward Cree7da517a2018-07-02 16:13:24 +01005633 * Since return value of netif_receive_skb() is normally ignored, and
5634 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005635 *
5636 * This function may only be called from softirq context and interrupts
5637 * should be enabled.
5638 */
5639void netif_receive_skb_list(struct list_head *head)
5640{
Edward Cree7da517a2018-07-02 16:13:24 +01005641 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005642
Edward Creeb9f463d2018-07-02 16:14:44 +01005643 if (list_empty(head))
5644 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005645 if (trace_netif_receive_skb_list_entry_enabled()) {
5646 list_for_each_entry(skb, head, list)
5647 trace_netif_receive_skb_list_entry(skb);
5648 }
Edward Cree7da517a2018-07-02 16:13:24 +01005649 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005650 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005651}
5652EXPORT_SYMBOL(netif_receive_skb_list);
5653
Wei Yongjunce1e2a72020-07-13 22:23:44 +08005654static DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005655
5656/* Network device is going away, flush any packets still pending */
5657static void flush_backlog(struct work_struct *work)
5658{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005659 struct sk_buff *skb, *tmp;
5660 struct softnet_data *sd;
5661
5662 local_bh_disable();
5663 sd = this_cpu_ptr(&softnet_data);
5664
5665 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005666 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005667 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005668 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005669 __skb_unlink(skb, &sd->input_pkt_queue);
Subash Abhinov Kasiviswanathan7df5cb752020-07-23 11:31:48 -06005670 dev_kfree_skb_irq(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005671 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005672 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005673 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005674 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005675 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005676
5677 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005678 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005679 __skb_unlink(skb, &sd->process_queue);
5680 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005681 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005682 }
5683 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005684 local_bh_enable();
5685}
5686
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005687static bool flush_required(int cpu)
5688{
5689#if IS_ENABLED(CONFIG_RPS)
5690 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5691 bool do_flush;
5692
5693 local_irq_disable();
5694 rps_lock(sd);
5695
5696 /* as insertion into process_queue happens with the rps lock held,
5697 * process_queue access may race only with dequeue
5698 */
5699 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5700 !skb_queue_empty_lockless(&sd->process_queue);
5701 rps_unlock(sd);
5702 local_irq_enable();
5703
5704 return do_flush;
5705#endif
5706 /* without RPS we can't safely check input_pkt_queue: during a
5707 * concurrent remote skb_queue_splice() we can detect as empty both
5708 * input_pkt_queue and process_queue even if the latter could end-up
5709 * containing a lot of packets.
5710 */
5711 return true;
5712}
5713
Eric Dumazet41852492016-08-26 12:50:39 -07005714static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005715{
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005716 static cpumask_t flush_cpus;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005717 unsigned int cpu;
5718
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005719 /* since we are under rtnl lock protection we can use static data
5720 * for the cpumask and avoid allocating on stack the possibly
5721 * large mask
5722 */
5723 ASSERT_RTNL();
5724
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005725 get_online_cpus();
5726
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005727 cpumask_clear(&flush_cpus);
5728 for_each_online_cpu(cpu) {
5729 if (flush_required(cpu)) {
5730 queue_work_on(cpu, system_highpri_wq,
5731 per_cpu_ptr(&flush_works, cpu));
5732 cpumask_set_cpu(cpu, &flush_cpus);
5733 }
5734 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005735
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005736 /* we can have in flight packet[s] on the cpus we are not flushing,
5737 * synchronize_net() in rollback_registered_many() will take care of
5738 * them
5739 */
5740 for_each_cpu(cpu, &flush_cpus)
Eric Dumazet41852492016-08-26 12:50:39 -07005741 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005742
5743 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005744}
5745
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005746/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5747static void gro_normal_list(struct napi_struct *napi)
5748{
5749 if (!napi->rx_count)
5750 return;
5751 netif_receive_skb_list_internal(&napi->rx_list);
5752 INIT_LIST_HEAD(&napi->rx_list);
5753 napi->rx_count = 0;
5754}
5755
5756/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5757 * pass the whole batch up to the stack.
5758 */
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005759static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005760{
5761 list_add_tail(&skb->list, &napi->rx_list);
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005762 napi->rx_count += segs;
5763 if (napi->rx_count >= gro_normal_batch)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005764 gro_normal_list(napi);
5765}
5766
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005767INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5768INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005769static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005770{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005771 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005772 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005773 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005774 int err = -ENOENT;
5775
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005776 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5777
Herbert Xufc59f9a2009-04-14 15:11:06 -07005778 if (NAPI_GRO_CB(skb)->count == 1) {
5779 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005780 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005781 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005782
5783 rcu_read_lock();
5784 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005785 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005786 continue;
5787
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005788 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5789 ipv6_gro_complete, inet_gro_complete,
5790 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005791 break;
5792 }
5793 rcu_read_unlock();
5794
5795 if (err) {
5796 WARN_ON(&ptype->list == head);
5797 kfree_skb(skb);
5798 return NET_RX_SUCCESS;
5799 }
5800
5801out:
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005802 gro_normal_one(napi, skb, NAPI_GRO_CB(skb)->count);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005803 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005804}
5805
Li RongQing6312fe72018-07-05 14:34:32 +08005806static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005807 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005808{
Li RongQing6312fe72018-07-05 14:34:32 +08005809 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005810 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005811
David Miller07d78362018-06-24 14:14:02 +09005812 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005813 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5814 return;
David S. Miller992cba72018-07-31 15:27:56 -07005815 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005816 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005817 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005818 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005819
5820 if (!napi->gro_hash[index].count)
5821 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005822}
David Miller07d78362018-06-24 14:14:02 +09005823
Li RongQing6312fe72018-07-05 14:34:32 +08005824/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005825 * youngest packets at the head of it.
5826 * Complete skbs in reverse order to reduce latencies.
5827 */
5828void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5829{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005830 unsigned long bitmask = napi->gro_bitmask;
5831 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005832
Eric Dumazet42519ed2018-11-21 11:39:28 -08005833 while ((i = ffs(bitmask)) != 0) {
5834 bitmask >>= i;
5835 base += i;
5836 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005837 }
David Miller07d78362018-06-24 14:14:02 +09005838}
Eric Dumazet86cac582010-08-31 18:25:32 +00005839EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005840
David Miller07d78362018-06-24 14:14:02 +09005841static struct list_head *gro_list_prepare(struct napi_struct *napi,
5842 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005843{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005844 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005845 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005846 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005847 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005848
Li RongQing6312fe72018-07-05 14:34:32 +08005849 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005850 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005851 unsigned long diffs;
5852
Tom Herbert0b4cec82014-01-15 08:58:06 -08005853 NAPI_GRO_CB(p)->flush = 0;
5854
5855 if (hash != skb_get_hash_raw(p)) {
5856 NAPI_GRO_CB(p)->same_flow = 0;
5857 continue;
5858 }
5859
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005860 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005861 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5862 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005863 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005864 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005865 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005866 if (maclen == ETH_HLEN)
5867 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005868 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005869 else if (!diffs)
5870 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005871 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005872 maclen);
Paul Blakey570341f2021-07-05 13:54:51 +03005873
5874 diffs |= skb_get_nfct(p) ^ skb_get_nfct(skb);
Florian Fainellic9f8e172021-07-07 21:10:51 -07005875#if IS_ENABLED(CONFIG_SKB_EXTENSIONS) && IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
Paul Blakey570341f2021-07-05 13:54:51 +03005876 if (!diffs) {
5877 struct tc_skb_ext *skb_ext = skb_ext_find(skb, TC_SKB_EXT);
5878 struct tc_skb_ext *p_ext = skb_ext_find(p, TC_SKB_EXT);
5879
5880 diffs |= (!!p_ext) ^ (!!skb_ext);
5881 if (!diffs && unlikely(skb_ext))
5882 diffs |= p_ext->chain ^ skb_ext->chain;
5883 }
Florian Fainellic9f8e172021-07-07 21:10:51 -07005884#endif
Paul Blakey570341f2021-07-05 13:54:51 +03005885
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005886 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005887 }
David Miller07d78362018-06-24 14:14:02 +09005888
5889 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005890}
5891
Alexander Lobakinda54cc22021-04-19 12:53:06 +00005892static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
Jerry Chu299603e82013-12-11 20:53:45 -08005893{
5894 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5895 const skb_frag_t *frag0 = &pinfo->frags[0];
5896
5897 NAPI_GRO_CB(skb)->data_offset = 0;
5898 NAPI_GRO_CB(skb)->frag0 = NULL;
5899 NAPI_GRO_CB(skb)->frag0_len = 0;
5900
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005901 if (!skb_headlen(skb) && pinfo->nr_frags &&
Eric Dumazet91431582021-04-13 05:41:35 -07005902 !PageHighMem(skb_frag_page(frag0)) &&
Alexander Lobakinda54cc22021-04-19 12:53:06 +00005903 (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) {
Jerry Chu299603e82013-12-11 20:53:45 -08005904 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005905 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5906 skb_frag_size(frag0),
5907 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005908 }
5909}
5910
Eric Dumazeta50e2332014-03-29 21:28:21 -07005911static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5912{
5913 struct skb_shared_info *pinfo = skb_shinfo(skb);
5914
5915 BUG_ON(skb->end - skb->tail < grow);
5916
5917 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5918
5919 skb->data_len -= grow;
5920 skb->tail += grow;
5921
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005922 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005923 skb_frag_size_sub(&pinfo->frags[0], grow);
5924
5925 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5926 skb_frag_unref(skb, 0);
5927 memmove(pinfo->frags, pinfo->frags + 1,
5928 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5929 }
5930}
5931
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005932static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005933{
Li RongQing6312fe72018-07-05 14:34:32 +08005934 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005935
Li RongQing6312fe72018-07-05 14:34:32 +08005936 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005937
Li RongQing6312fe72018-07-05 14:34:32 +08005938 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005939 * impossible.
5940 */
5941 if (WARN_ON_ONCE(!oldest))
5942 return;
5943
Li RongQingd9f37d02018-07-13 14:41:36 +08005944 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5945 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005946 */
David S. Millerece23712018-10-28 10:35:12 -07005947 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005948 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09005949}
5950
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005951INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5952 struct sk_buff *));
5953INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5954 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005955static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005956{
Li RongQing6312fe72018-07-05 14:34:32 +08005957 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005958 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005959 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005960 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005961 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005962 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005963 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005964 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005965 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005966
David S. Millerb5cdae32017-04-18 15:36:58 -04005967 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005968 goto normal;
5969
David Miller07d78362018-06-24 14:14:02 +09005970 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005971
Herbert Xud565b0a2008-12-15 23:38:52 -08005972 rcu_read_lock();
5973 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005974 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005975 continue;
5976
Herbert Xu86911732009-01-29 14:19:50 +00005977 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005978 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005979 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005980 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005981 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005982 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005983 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005984 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005985 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005986 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005987
Tom Herbert662880f2014-08-27 21:26:56 -07005988 /* Setup for GRO checksum validation */
5989 switch (skb->ip_summed) {
5990 case CHECKSUM_COMPLETE:
5991 NAPI_GRO_CB(skb)->csum = skb->csum;
5992 NAPI_GRO_CB(skb)->csum_valid = 1;
5993 NAPI_GRO_CB(skb)->csum_cnt = 0;
5994 break;
5995 case CHECKSUM_UNNECESSARY:
5996 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5997 NAPI_GRO_CB(skb)->csum_valid = 0;
5998 break;
5999 default:
6000 NAPI_GRO_CB(skb)->csum_cnt = 0;
6001 NAPI_GRO_CB(skb)->csum_valid = 0;
6002 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006003
Paolo Abeniaaa5d902018-12-14 11:51:58 +01006004 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
6005 ipv6_gro_receive, inet_gro_receive,
6006 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08006007 break;
6008 }
6009 rcu_read_unlock();
6010
6011 if (&ptype->list == head)
6012 goto normal;
6013
Masahiro Yamada45586c72020-02-03 17:37:45 -08006014 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01006015 ret = GRO_CONSUMED;
6016 goto ok;
6017 }
6018
Herbert Xu0da2afd52008-12-26 14:57:42 -08006019 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006020 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08006021
Herbert Xud565b0a2008-12-15 23:38:52 -08006022 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07006023 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006024 napi_gro_complete(napi, pp);
Li RongQing6312fe72018-07-05 14:34:32 +08006025 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08006026 }
6027
Herbert Xu0da2afd52008-12-26 14:57:42 -08006028 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08006029 goto ok;
6030
Eric Dumazet600adc12014-01-09 14:12:19 -08006031 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08006032 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08006033
Li RongQing6312fe72018-07-05 14:34:32 +08006034 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006035 gro_flush_oldest(napi, gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08006036 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08006037 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08006038 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006039 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00006040 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07006041 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00006042 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09006043 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006044 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08006045
Herbert Xuad0f9902009-02-01 01:24:55 -08006046pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006047 grow = skb_gro_offset(skb) - skb_headlen(skb);
6048 if (grow > 0)
6049 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08006050ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08006051 if (napi->gro_hash[hash].count) {
6052 if (!test_bit(hash, &napi->gro_bitmask))
6053 __set_bit(hash, &napi->gro_bitmask);
6054 } else if (test_bit(hash, &napi->gro_bitmask)) {
6055 __clear_bit(hash, &napi->gro_bitmask);
6056 }
6057
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006058 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006059
6060normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08006061 ret = GRO_NORMAL;
6062 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08006063}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006064
Jerry Chubf5a7552014-01-07 10:23:19 -08006065struct packet_offload *gro_find_receive_by_type(__be16 type)
6066{
6067 struct list_head *offload_head = &offload_base;
6068 struct packet_offload *ptype;
6069
6070 list_for_each_entry_rcu(ptype, offload_head, list) {
6071 if (ptype->type != type || !ptype->callbacks.gro_receive)
6072 continue;
6073 return ptype;
6074 }
6075 return NULL;
6076}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006077EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08006078
6079struct packet_offload *gro_find_complete_by_type(__be16 type)
6080{
6081 struct list_head *offload_head = &offload_base;
6082 struct packet_offload *ptype;
6083
6084 list_for_each_entry_rcu(ptype, offload_head, list) {
6085 if (ptype->type != type || !ptype->callbacks.gro_complete)
6086 continue;
6087 return ptype;
6088 }
6089 return NULL;
6090}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006091EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006092
Michal Kubečeke44699d2017-06-29 11:13:36 +02006093static void napi_skb_free_stolen_head(struct sk_buff *skb)
6094{
6095 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02006096 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02006097 kmem_cache_free(skbuff_head_cache, skb);
6098}
6099
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006100static gro_result_t napi_skb_finish(struct napi_struct *napi,
6101 struct sk_buff *skb,
6102 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08006103{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006104 switch (ret) {
6105 case GRO_NORMAL:
Eric Dumazet9e6ce472021-02-04 13:31:46 -08006106 gro_normal_one(napi, skb, 1);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006107 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006108
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006109 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08006110 kfree_skb(skb);
6111 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006112
Eric Dumazetdaa86542012-04-19 07:07:40 +00006113 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02006114 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6115 napi_skb_free_stolen_head(skb);
6116 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00006117 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00006118 break;
6119
Ben Hutchings5b252f02009-10-29 07:17:09 +00006120 case GRO_HELD:
6121 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006122 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006123 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006124 }
6125
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006126 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006127}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006128
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006129gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006130{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006131 gro_result_t ret;
6132
Eric Dumazet93f93a42015-11-18 06:30:59 -08006133 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006134 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00006135
Alexander Lobakinda54cc22021-04-19 12:53:06 +00006136 skb_gro_reset_offset(skb, 0);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006137
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006138 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006139 trace_napi_gro_receive_exit(ret);
6140
6141 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006142}
6143EXPORT_SYMBOL(napi_gro_receive);
6144
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00006145static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006146{
Eric Dumazet93a35f52014-10-23 06:30:30 -07006147 if (unlikely(skb->pfmemalloc)) {
6148 consume_skb(skb);
6149 return;
6150 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006151 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00006152 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
6153 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01006154 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08006155 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08006156 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08006157
6158 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
6159 skb->pkt_type = PACKET_HOST;
6160
Jerry Chuc3caf112014-07-14 15:54:46 -07006161 skb->encapsulation = 0;
6162 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07006163 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02006164 skb_ext_reset(skb);
Paul Blakey570341f2021-07-05 13:54:51 +03006165 nf_reset_ct(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006166
6167 napi->skb = skb;
6168}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006169
Herbert Xu76620aa2009-04-16 02:02:07 -07006170struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08006171{
Herbert Xu5d38a072009-01-04 16:13:40 -08006172 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08006173
6174 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08006175 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08006176 if (skb) {
6177 napi->skb = skb;
6178 skb_mark_napi_id(skb, napi);
6179 }
Herbert Xu5d38a072009-01-04 16:13:40 -08006180 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006181 return skb;
6182}
Herbert Xu76620aa2009-04-16 02:02:07 -07006183EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006184
Eric Dumazeta50e2332014-03-29 21:28:21 -07006185static gro_result_t napi_frags_finish(struct napi_struct *napi,
6186 struct sk_buff *skb,
6187 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006188{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006189 switch (ret) {
6190 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006191 case GRO_HELD:
6192 __skb_push(skb, ETH_HLEN);
6193 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01006194 if (ret == GRO_NORMAL)
Eric Dumazet9e6ce472021-02-04 13:31:46 -08006195 gro_normal_one(napi, skb, 1);
Herbert Xu86911732009-01-29 14:19:50 +00006196 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006197
6198 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006199 napi_reuse_skb(napi, skb);
6200 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006201
Michal Kubečeke44699d2017-06-29 11:13:36 +02006202 case GRO_MERGED_FREE:
6203 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6204 napi_skb_free_stolen_head(skb);
6205 else
6206 napi_reuse_skb(napi, skb);
6207 break;
6208
Ben Hutchings5b252f02009-10-29 07:17:09 +00006209 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006210 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006211 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006212 }
6213
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006214 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006215}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006216
Eric Dumazeta50e2332014-03-29 21:28:21 -07006217/* Upper GRO stack assumes network header starts at gro_offset=0
6218 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6219 * We copy ethernet header into skb->data to have a common layout.
6220 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00006221static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006222{
Herbert Xu76620aa2009-04-16 02:02:07 -07006223 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006224 const struct ethhdr *eth;
6225 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07006226
6227 napi->skb = NULL;
6228
Eric Dumazeta50e2332014-03-29 21:28:21 -07006229 skb_reset_mac_header(skb);
Alexander Lobakinda54cc22021-04-19 12:53:06 +00006230 skb_gro_reset_offset(skb, hlen);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006231
Eric Dumazeta50e2332014-03-29 21:28:21 -07006232 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6233 eth = skb_gro_header_slow(skb, hlen, 0);
6234 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006235 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6236 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006237 napi_reuse_skb(napi, skb);
6238 return NULL;
6239 }
6240 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006241 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006242 gro_pull_from_frag0(skb, hlen);
6243 NAPI_GRO_CB(skb)->frag0 += hlen;
6244 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006245 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006246 __skb_pull(skb, hlen);
6247
6248 /*
6249 * This works because the only protocols we care about don't require
6250 * special handling.
6251 * We'll fix it up properly in napi_frags_finish()
6252 */
6253 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006254
Herbert Xu76620aa2009-04-16 02:02:07 -07006255 return skb;
6256}
Herbert Xu76620aa2009-04-16 02:02:07 -07006257
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006258gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006259{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006260 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006261 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006262
6263 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006264 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08006265
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006266 trace_napi_gro_frags_entry(skb);
6267
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006268 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6269 trace_napi_gro_frags_exit(ret);
6270
6271 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006272}
6273EXPORT_SYMBOL(napi_gro_frags);
6274
Tom Herbert573e8fc2014-08-22 13:33:47 -07006275/* Compute the checksum from gro_offset and return the folded value
6276 * after adding in any pseudo checksum.
6277 */
6278__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6279{
6280 __wsum wsum;
6281 __sum16 sum;
6282
6283 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6284
6285 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6286 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006287 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006288 if (likely(!sum)) {
6289 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6290 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006291 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006292 }
6293
6294 NAPI_GRO_CB(skb)->csum = wsum;
6295 NAPI_GRO_CB(skb)->csum_valid = 1;
6296
6297 return sum;
6298}
6299EXPORT_SYMBOL(__skb_gro_checksum_complete);
6300
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306301static void net_rps_send_ipi(struct softnet_data *remsd)
6302{
6303#ifdef CONFIG_RPS
6304 while (remsd) {
6305 struct softnet_data *next = remsd->rps_ipi_next;
6306
6307 if (cpu_online(remsd->cpu))
6308 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6309 remsd = next;
6310 }
6311#endif
6312}
6313
Eric Dumazete326bed2010-04-22 00:22:45 -07006314/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006315 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006316 * Note: called with local irq disabled, but exits with local irq enabled.
6317 */
6318static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6319{
6320#ifdef CONFIG_RPS
6321 struct softnet_data *remsd = sd->rps_ipi_list;
6322
6323 if (remsd) {
6324 sd->rps_ipi_list = NULL;
6325
6326 local_irq_enable();
6327
6328 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306329 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006330 } else
6331#endif
6332 local_irq_enable();
6333}
6334
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006335static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6336{
6337#ifdef CONFIG_RPS
6338 return sd->rps_ipi_list != NULL;
6339#else
6340 return false;
6341#endif
6342}
6343
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006344static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006346 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006347 bool again = true;
6348 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
Eric Dumazete326bed2010-04-22 00:22:45 -07006350 /* Check if we have pending ipi, its better to send them now,
6351 * not waiting net_rx_action() end.
6352 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006353 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006354 local_irq_disable();
6355 net_rps_action_and_irq_enable(sd);
6356 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006357
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006358 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006359 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
Changli Gao6e7676c2010-04-27 15:07:33 -07006362 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006363 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006364 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006365 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006366 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006367 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006368 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006369
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006372 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006373 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006374 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006375 /*
6376 * Inline a custom version of __napi_complete().
6377 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006378 * and NAPI_STATE_SCHED is the only possible flag set
6379 * on backlog.
6380 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006381 * and we dont need an smp_mb() memory barrier.
6382 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006383 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006384 again = false;
6385 } else {
6386 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6387 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006388 }
6389 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006390 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006393 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394}
6395
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006396/**
6397 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006398 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006399 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006400 * The entry's receive function will be scheduled to run.
6401 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006402 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006403void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006404{
6405 unsigned long flags;
6406
6407 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006408 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006409 local_irq_restore(flags);
6410}
6411EXPORT_SYMBOL(__napi_schedule);
6412
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006413/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006414 * napi_schedule_prep - check if napi can be scheduled
6415 * @n: napi context
6416 *
6417 * Test if NAPI routine is already running, and if not mark
Jonathan Neuschäferee1a4c82020-09-05 20:32:18 +02006418 * it as running. This is used as a condition variable to
Eric Dumazet39e6c822017-02-28 10:34:50 -08006419 * insure only one NAPI poll instance runs. We also make
6420 * sure there is no pending NAPI disable.
6421 */
6422bool napi_schedule_prep(struct napi_struct *n)
6423{
6424 unsigned long val, new;
6425
6426 do {
6427 val = READ_ONCE(n->state);
6428 if (unlikely(val & NAPIF_STATE_DISABLE))
6429 return false;
6430 new = val | NAPIF_STATE_SCHED;
6431
6432 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6433 * This was suggested by Alexander Duyck, as compiler
6434 * emits better code than :
6435 * if (val & NAPIF_STATE_SCHED)
6436 * new |= NAPIF_STATE_MISSED;
6437 */
6438 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6439 NAPIF_STATE_MISSED;
6440 } while (cmpxchg(&n->state, val, new) != val);
6441
6442 return !(val & NAPIF_STATE_SCHED);
6443}
6444EXPORT_SYMBOL(napi_schedule_prep);
6445
6446/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006447 * __napi_schedule_irqoff - schedule for receive
6448 * @n: entry to schedule
6449 *
Sebastian Andrzej Siewior33934052021-05-12 23:43:24 +02006450 * Variant of __napi_schedule() assuming hard irqs are masked.
6451 *
6452 * On PREEMPT_RT enabled kernels this maps to __napi_schedule()
6453 * because the interrupt disabled assumption might not be true
6454 * due to force-threaded interrupts and spinlock substitution.
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006455 */
6456void __napi_schedule_irqoff(struct napi_struct *n)
6457{
Sebastian Andrzej Siewior33934052021-05-12 23:43:24 +02006458 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6459 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6460 else
6461 __napi_schedule(n);
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006462}
6463EXPORT_SYMBOL(__napi_schedule_irqoff);
6464
Eric Dumazet364b6052016-11-15 10:15:13 -08006465bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006466{
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006467 unsigned long flags, val, new, timeout = 0;
6468 bool ret = true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006469
6470 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006471 * 1) Don't let napi dequeue from the cpu poll list
6472 * just in case its running on a different cpu.
6473 * 2) If we are busy polling, do nothing here, we have
6474 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006475 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006476 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6477 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006478 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006479
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006480 if (work_done) {
6481 if (n->gro_bitmask)
Eric Dumazet7e417a62020-04-22 09:13:28 -07006482 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6483 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006484 }
6485 if (n->defer_hard_irqs_count > 0) {
6486 n->defer_hard_irqs_count--;
Eric Dumazet7e417a62020-04-22 09:13:28 -07006487 timeout = READ_ONCE(n->dev->gro_flush_timeout);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006488 if (timeout)
6489 ret = false;
6490 }
6491 if (n->gro_bitmask) {
Paolo Abeni605108a2018-11-21 18:21:35 +01006492 /* When the NAPI instance uses a timeout and keeps postponing
6493 * it, we need to bound somehow the time packets are kept in
6494 * the GRO layer
6495 */
6496 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006497 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006498
6499 gro_normal_list(n);
6500
Eric Dumazet02c16022017-02-04 15:25:02 -08006501 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006502 /* If n->poll_list is not empty, we need to mask irqs */
6503 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006504 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006505 local_irq_restore(flags);
6506 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006507
6508 do {
6509 val = READ_ONCE(n->state);
6510
6511 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6512
6513 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6514
6515 /* If STATE_MISSED was set, leave STATE_SCHED set,
6516 * because we will call napi->poll() one more time.
6517 * This C code was suggested by Alexander Duyck to help gcc.
6518 */
6519 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6520 NAPIF_STATE_SCHED;
6521 } while (cmpxchg(&n->state, val, new) != val);
6522
6523 if (unlikely(val & NAPIF_STATE_MISSED)) {
6524 __napi_schedule(n);
6525 return false;
6526 }
6527
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006528 if (timeout)
6529 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6530 HRTIMER_MODE_REL_PINNED);
6531 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006532}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006533EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006534
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006535/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006536static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006537{
6538 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6539 struct napi_struct *napi;
6540
6541 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6542 if (napi->napi_id == napi_id)
6543 return napi;
6544
6545 return NULL;
6546}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006547
6548#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006549
Eric Dumazetce6aea92015-11-18 06:30:54 -08006550#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006551
6552static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6553{
6554 int rc;
6555
Eric Dumazet39e6c822017-02-28 10:34:50 -08006556 /* Busy polling means there is a high chance device driver hard irq
6557 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6558 * set in napi_schedule_prep().
6559 * Since we are about to call napi->poll() once more, we can safely
6560 * clear NAPI_STATE_MISSED.
6561 *
6562 * Note: x86 could use a single "lock and ..." instruction
6563 * to perform these two clear_bit()
6564 */
6565 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006566 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6567
6568 local_bh_disable();
6569
6570 /* All we really want here is to re-enable device interrupts.
6571 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6572 */
6573 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006574 /* We can't gro_normal_list() here, because napi->poll() might have
6575 * rearmed the napi (napi_complete_done()) in which case it could
6576 * already be running on another CPU.
6577 */
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006578 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006579 netpoll_poll_unlock(have_poll_lock);
Edward Cree323ebb62019-08-06 14:53:55 +01006580 if (rc == BUSY_POLL_BUDGET) {
6581 /* As the whole budget was spent, we still own the napi so can
6582 * safely handle the rx_list.
6583 */
6584 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006585 __napi_schedule(napi);
Edward Cree323ebb62019-08-06 14:53:55 +01006586 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006587 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006588}
6589
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006590void napi_busy_loop(unsigned int napi_id,
6591 bool (*loop_end)(void *, unsigned long),
6592 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006593{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006594 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006595 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006596 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006597 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006598
6599restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006600 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006601
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006602 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006603
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006604 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006605 if (!napi)
6606 goto out;
6607
Eric Dumazet217f6972016-11-15 10:15:11 -08006608 preempt_disable();
6609 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006610 int work = 0;
6611
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006612 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006613 if (!napi_poll) {
6614 unsigned long val = READ_ONCE(napi->state);
6615
6616 /* If multiple threads are competing for this napi,
6617 * we avoid dirtying napi->state as much as we can.
6618 */
6619 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6620 NAPIF_STATE_IN_BUSY_POLL))
6621 goto count;
6622 if (cmpxchg(&napi->state, val,
6623 val | NAPIF_STATE_IN_BUSY_POLL |
6624 NAPIF_STATE_SCHED) != val)
6625 goto count;
6626 have_poll_lock = netpoll_poll_lock(napi);
6627 napi_poll = napi->poll;
6628 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006629 work = napi_poll(napi, BUSY_POLL_BUDGET);
6630 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006631 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006632count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006633 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006634 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006635 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006636 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006637
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006638 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006639 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006640
Eric Dumazet217f6972016-11-15 10:15:11 -08006641 if (unlikely(need_resched())) {
6642 if (napi_poll)
6643 busy_poll_stop(napi, have_poll_lock);
6644 preempt_enable();
6645 rcu_read_unlock();
6646 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006647 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006648 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006649 goto restart;
6650 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006651 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006652 }
6653 if (napi_poll)
6654 busy_poll_stop(napi, have_poll_lock);
6655 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006656out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006657 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006658}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006659EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006660
6661#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006662
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006663static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006664{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006665 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006666 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006667
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006668 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006669
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006670 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006671 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006672 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6673 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006674 } while (napi_by_id(napi_gen_id));
6675 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006676
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006677 hlist_add_head_rcu(&napi->napi_hash_node,
6678 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006679
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006680 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006681}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006682
6683/* Warning : caller is responsible to make sure rcu grace period
6684 * is respected before freeing memory containing @napi
6685 */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006686static void napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006687{
6688 spin_lock(&napi_hash_lock);
6689
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006690 hlist_del_init_rcu(&napi->napi_hash_node);
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006691
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006692 spin_unlock(&napi_hash_lock);
6693}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006694
Eric Dumazet3b47d302014-11-06 21:09:44 -08006695static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6696{
6697 struct napi_struct *napi;
6698
6699 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006700
6701 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6702 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6703 */
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006704 if (!napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006705 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6706 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006707
6708 return HRTIMER_NORESTART;
6709}
6710
David S. Miller7c4ec742018-07-20 23:37:55 -07006711static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006712{
David Miller07d78362018-06-24 14:14:02 +09006713 int i;
6714
Li RongQing6312fe72018-07-05 14:34:32 +08006715 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6716 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6717 napi->gro_hash[i].count = 0;
6718 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006719 napi->gro_bitmask = 0;
6720}
6721
6722void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6723 int (*poll)(struct napi_struct *, int), int weight)
6724{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006725 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6726 return;
6727
David S. Miller7c4ec742018-07-20 23:37:55 -07006728 INIT_LIST_HEAD(&napi->poll_list);
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006729 INIT_HLIST_NODE(&napi->napi_hash_node);
David S. Miller7c4ec742018-07-20 23:37:55 -07006730 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6731 napi->timer.function = napi_watchdog;
6732 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006733 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006734 INIT_LIST_HEAD(&napi->rx_list);
6735 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006736 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006737 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006738 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6739 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006740 napi->weight = weight;
Herbert Xud565b0a2008-12-15 23:38:52 -08006741 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006742#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006743 napi->poll_owner = -1;
6744#endif
6745 set_bit(NAPI_STATE_SCHED, &napi->state);
Jakub Kicinski96e97bc2020-08-26 12:40:06 -07006746 set_bit(NAPI_STATE_NPSVC, &napi->state);
6747 list_add_rcu(&napi->dev_list, &dev->napi_list);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006748 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006749}
6750EXPORT_SYMBOL(netif_napi_add);
6751
Eric Dumazet3b47d302014-11-06 21:09:44 -08006752void napi_disable(struct napi_struct *n)
6753{
6754 might_sleep();
6755 set_bit(NAPI_STATE_DISABLE, &n->state);
6756
6757 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6758 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006759 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6760 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006761
6762 hrtimer_cancel(&n->timer);
6763
6764 clear_bit(NAPI_STATE_DISABLE, &n->state);
6765}
6766EXPORT_SYMBOL(napi_disable);
6767
David Miller07d78362018-06-24 14:14:02 +09006768static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006769{
David Miller07d78362018-06-24 14:14:02 +09006770 int i;
David Millerd4546c22018-06-24 14:13:49 +09006771
David Miller07d78362018-06-24 14:14:02 +09006772 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6773 struct sk_buff *skb, *n;
6774
Li RongQing6312fe72018-07-05 14:34:32 +08006775 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006776 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006777 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006778 }
David Millerd4546c22018-06-24 14:13:49 +09006779}
6780
Eric Dumazet93d05d42015-11-18 06:31:03 -08006781/* Must be called in process context */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006782void __netif_napi_del(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006783{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006784 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6785 return;
6786
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006787 napi_hash_del(napi);
Jakub Kicinski5251ef82020-09-09 10:37:53 -07006788 list_del_rcu(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006789 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006790
David Miller07d78362018-06-24 14:14:02 +09006791 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006792 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006793}
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006794EXPORT_SYMBOL(__netif_napi_del);
Herbert Xud565b0a2008-12-15 23:38:52 -08006795
Herbert Xu726ce702014-12-21 07:16:21 +11006796static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6797{
6798 void *have;
6799 int work, weight;
6800
6801 list_del_init(&n->poll_list);
6802
6803 have = netpoll_poll_lock(n);
6804
6805 weight = n->weight;
6806
6807 /* This NAPI_STATE_SCHED test is for avoiding a race
6808 * with netpoll's poll_napi(). Only the entity which
6809 * obtains the lock and sees NAPI_STATE_SCHED set will
6810 * actually make the ->poll() call. Therefore we avoid
6811 * accidentally calling ->poll() when NAPI is not scheduled.
6812 */
6813 work = 0;
6814 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6815 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006816 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006817 }
6818
Eric Dumazet427d5832020-06-17 09:40:51 -07006819 if (unlikely(work > weight))
6820 pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6821 n->poll, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006822
6823 if (likely(work < weight))
6824 goto out_unlock;
6825
6826 /* Drivers must not modify the NAPI state if they
6827 * consume the entire weight. In such cases this code
6828 * still "owns" the NAPI instance and therefore can
6829 * move the instance around on the list at-will.
6830 */
6831 if (unlikely(napi_disable_pending(n))) {
6832 napi_complete(n);
6833 goto out_unlock;
6834 }
6835
Li RongQingd9f37d02018-07-13 14:41:36 +08006836 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006837 /* flush too old packets
6838 * If HZ < 1000, flush all packets.
6839 */
6840 napi_gro_flush(n, HZ >= 1000);
6841 }
6842
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006843 gro_normal_list(n);
6844
Herbert Xu001ce542014-12-21 07:16:22 +11006845 /* Some drivers may have called napi_schedule
6846 * prior to exhausting their budget.
6847 */
6848 if (unlikely(!list_empty(&n->poll_list))) {
6849 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6850 n->dev ? n->dev->name : "backlog");
6851 goto out_unlock;
6852 }
6853
Herbert Xu726ce702014-12-21 07:16:21 +11006854 list_add_tail(&n->poll_list, repoll);
6855
6856out_unlock:
6857 netpoll_poll_unlock(have);
6858
6859 return work;
6860}
6861
Emese Revfy0766f782016-06-20 20:42:34 +02006862static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006864 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006865 unsigned long time_limit = jiffies +
6866 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006867 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006868 LIST_HEAD(list);
6869 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006870
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006872 list_splice_init(&sd->poll_list, &list);
6873 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006875 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006876 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006878 if (list_empty(&list)) {
6879 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006880 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006881 break;
6882 }
6883
Herbert Xu6bd373e2014-12-21 07:16:24 +11006884 n = list_first_entry(&list, struct napi_struct, poll_list);
6885 budget -= napi_poll(n, &repoll);
6886
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006887 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006888 * Allow this to run for 2 jiffies since which will allow
6889 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006890 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006891 if (unlikely(budget <= 0 ||
6892 time_after_eq(jiffies, time_limit))) {
6893 sd->time_squeeze++;
6894 break;
6895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006897
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006898 local_irq_disable();
6899
6900 list_splice_tail_init(&sd->poll_list, &list);
6901 list_splice_tail(&repoll, &list);
6902 list_splice(&list, &sd->poll_list);
6903 if (!list_empty(&sd->poll_list))
6904 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6905
Eric Dumazete326bed2010-04-22 00:22:45 -07006906 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006907out:
6908 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909}
6910
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006911struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006912 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006913
6914 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006915 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006916
Taehee Yoo32b6d342019-10-21 18:47:56 +00006917 /* lookup ignore flag */
6918 bool ignore;
6919
Veaceslav Falico5d261912013-08-28 23:25:05 +02006920 /* counter for the number of times this device was added to us */
6921 u16 ref_nr;
6922
Veaceslav Falico402dae92013-09-25 09:20:09 +02006923 /* private field for the users */
6924 void *private;
6925
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006926 struct list_head list;
6927 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006928};
6929
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006930static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006931 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006932{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006933 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006934
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006935 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006936 if (adj->dev == adj_dev)
6937 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006938 }
6939 return NULL;
6940}
6941
Taehee Yooeff74232020-09-25 18:13:12 +00006942static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6943 struct netdev_nested_priv *priv)
David Ahernf1170fd2016-10-17 19:15:51 -07006944{
Taehee Yooeff74232020-09-25 18:13:12 +00006945 struct net_device *dev = (struct net_device *)priv->data;
David Ahernf1170fd2016-10-17 19:15:51 -07006946
6947 return upper_dev == dev;
6948}
6949
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006950/**
6951 * netdev_has_upper_dev - Check if device is linked to an upper device
6952 * @dev: device
6953 * @upper_dev: upper device to check
6954 *
6955 * Find out if a device is linked to specified upper device and return true
6956 * in case it is. Note that this checks only immediate upper device,
6957 * not through a complete stack of devices. The caller must hold the RTNL lock.
6958 */
6959bool netdev_has_upper_dev(struct net_device *dev,
6960 struct net_device *upper_dev)
6961{
Taehee Yooeff74232020-09-25 18:13:12 +00006962 struct netdev_nested_priv priv = {
6963 .data = (void *)upper_dev,
6964 };
6965
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006966 ASSERT_RTNL();
6967
Taehee Yoo32b6d342019-10-21 18:47:56 +00006968 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006969 &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006970}
6971EXPORT_SYMBOL(netdev_has_upper_dev);
6972
6973/**
David Ahern1a3f0602016-10-17 19:15:44 -07006974 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6975 * @dev: device
6976 * @upper_dev: upper device to check
6977 *
6978 * Find out if a device is linked to specified upper device and return true
6979 * in case it is. Note that this checks the entire upper device chain.
6980 * The caller must hold rcu lock.
6981 */
6982
David Ahern1a3f0602016-10-17 19:15:44 -07006983bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6984 struct net_device *upper_dev)
6985{
Taehee Yooeff74232020-09-25 18:13:12 +00006986 struct netdev_nested_priv priv = {
6987 .data = (void *)upper_dev,
6988 };
6989
Taehee Yoo32b6d342019-10-21 18:47:56 +00006990 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006991 &priv);
David Ahern1a3f0602016-10-17 19:15:44 -07006992}
6993EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6994
6995/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006996 * netdev_has_any_upper_dev - Check if device is linked to some device
6997 * @dev: device
6998 *
6999 * Find out if a device is linked to an upper device and return true in case
7000 * it is. The caller must hold the RTNL lock.
7001 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007002bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007003{
7004 ASSERT_RTNL();
7005
David Ahernf1170fd2016-10-17 19:15:51 -07007006 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007007}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007008EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007009
7010/**
7011 * netdev_master_upper_dev_get - Get master upper device
7012 * @dev: device
7013 *
7014 * Find a master upper device and return pointer to it or NULL in case
7015 * it's not there. The caller must hold the RTNL lock.
7016 */
7017struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
7018{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007019 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007020
7021 ASSERT_RTNL();
7022
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007023 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007024 return NULL;
7025
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007026 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007027 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007028 if (likely(upper->master))
7029 return upper->dev;
7030 return NULL;
7031}
7032EXPORT_SYMBOL(netdev_master_upper_dev_get);
7033
Taehee Yoo32b6d342019-10-21 18:47:56 +00007034static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
7035{
7036 struct netdev_adjacent *upper;
7037
7038 ASSERT_RTNL();
7039
7040 if (list_empty(&dev->adj_list.upper))
7041 return NULL;
7042
7043 upper = list_first_entry(&dev->adj_list.upper,
7044 struct netdev_adjacent, list);
7045 if (likely(upper->master) && !upper->ignore)
7046 return upper->dev;
7047 return NULL;
7048}
7049
David Ahern0f524a82016-10-17 19:15:52 -07007050/**
7051 * netdev_has_any_lower_dev - Check if device is linked to some device
7052 * @dev: device
7053 *
7054 * Find out if a device is linked to a lower device and return true in case
7055 * it is. The caller must hold the RTNL lock.
7056 */
7057static bool netdev_has_any_lower_dev(struct net_device *dev)
7058{
7059 ASSERT_RTNL();
7060
7061 return !list_empty(&dev->adj_list.lower);
7062}
7063
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02007064void *netdev_adjacent_get_private(struct list_head *adj_list)
7065{
7066 struct netdev_adjacent *adj;
7067
7068 adj = list_entry(adj_list, struct netdev_adjacent, list);
7069
7070 return adj->private;
7071}
7072EXPORT_SYMBOL(netdev_adjacent_get_private);
7073
Veaceslav Falico31088a12013-09-25 09:20:12 +02007074/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04007075 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7076 * @dev: device
7077 * @iter: list_head ** of the current position
7078 *
7079 * Gets the next device from the dev's upper list, starting from iter
7080 * position. The caller must hold RCU read lock.
7081 */
7082struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7083 struct list_head **iter)
7084{
7085 struct netdev_adjacent *upper;
7086
7087 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7088
7089 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7090
7091 if (&upper->list == &dev->adj_list.upper)
7092 return NULL;
7093
7094 *iter = &upper->list;
7095
7096 return upper->dev;
7097}
7098EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7099
Taehee Yoo32b6d342019-10-21 18:47:56 +00007100static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7101 struct list_head **iter,
7102 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00007103{
7104 struct netdev_adjacent *upper;
7105
7106 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7107
7108 if (&upper->list == &dev->adj_list.upper)
7109 return NULL;
7110
7111 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007112 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007113
7114 return upper->dev;
7115}
7116
David Ahern1a3f0602016-10-17 19:15:44 -07007117static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7118 struct list_head **iter)
7119{
7120 struct netdev_adjacent *upper;
7121
7122 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7123
7124 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7125
7126 if (&upper->list == &dev->adj_list.upper)
7127 return NULL;
7128
7129 *iter = &upper->list;
7130
7131 return upper->dev;
7132}
7133
Taehee Yoo32b6d342019-10-21 18:47:56 +00007134static int __netdev_walk_all_upper_dev(struct net_device *dev,
7135 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007136 struct netdev_nested_priv *priv),
7137 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007138{
7139 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7140 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7141 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007142 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007143
7144 now = dev;
7145 iter = &dev->adj_list.upper;
7146
7147 while (1) {
7148 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007149 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007150 if (ret)
7151 return ret;
7152 }
7153
7154 next = NULL;
7155 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007156 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007157 if (!udev)
7158 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007159 if (ignore)
7160 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007161
7162 next = udev;
7163 niter = &udev->adj_list.upper;
7164 dev_stack[cur] = now;
7165 iter_stack[cur++] = iter;
7166 break;
7167 }
7168
7169 if (!next) {
7170 if (!cur)
7171 return 0;
7172 next = dev_stack[--cur];
7173 niter = iter_stack[cur];
7174 }
7175
7176 now = next;
7177 iter = niter;
7178 }
7179
7180 return 0;
7181}
7182
David Ahern1a3f0602016-10-17 19:15:44 -07007183int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7184 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007185 struct netdev_nested_priv *priv),
7186 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007187{
Taehee Yoo5343da42019-10-21 18:47:50 +00007188 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7189 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7190 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007191
Taehee Yoo5343da42019-10-21 18:47:50 +00007192 now = dev;
7193 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07007194
Taehee Yoo5343da42019-10-21 18:47:50 +00007195 while (1) {
7196 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007197 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007198 if (ret)
7199 return ret;
7200 }
7201
7202 next = NULL;
7203 while (1) {
7204 udev = netdev_next_upper_dev_rcu(now, &iter);
7205 if (!udev)
7206 break;
7207
7208 next = udev;
7209 niter = &udev->adj_list.upper;
7210 dev_stack[cur] = now;
7211 iter_stack[cur++] = iter;
7212 break;
7213 }
7214
7215 if (!next) {
7216 if (!cur)
7217 return 0;
7218 next = dev_stack[--cur];
7219 niter = iter_stack[cur];
7220 }
7221
7222 now = next;
7223 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007224 }
7225
7226 return 0;
7227}
7228EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7229
Taehee Yoo32b6d342019-10-21 18:47:56 +00007230static bool __netdev_has_upper_dev(struct net_device *dev,
7231 struct net_device *upper_dev)
7232{
Taehee Yooeff74232020-09-25 18:13:12 +00007233 struct netdev_nested_priv priv = {
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007234 .flags = 0,
Taehee Yooeff74232020-09-25 18:13:12 +00007235 .data = (void *)upper_dev,
7236 };
7237
Taehee Yoo32b6d342019-10-21 18:47:56 +00007238 ASSERT_RTNL();
7239
7240 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007241 &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007242}
7243
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007244/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02007245 * netdev_lower_get_next_private - Get the next ->private from the
7246 * lower neighbour list
7247 * @dev: device
7248 * @iter: list_head ** of the current position
7249 *
7250 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7251 * list, starting from iter position. The caller must hold either hold the
7252 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007253 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02007254 */
7255void *netdev_lower_get_next_private(struct net_device *dev,
7256 struct list_head **iter)
7257{
7258 struct netdev_adjacent *lower;
7259
7260 lower = list_entry(*iter, struct netdev_adjacent, list);
7261
7262 if (&lower->list == &dev->adj_list.lower)
7263 return NULL;
7264
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007265 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007266
7267 return lower->private;
7268}
7269EXPORT_SYMBOL(netdev_lower_get_next_private);
7270
7271/**
7272 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7273 * lower neighbour list, RCU
7274 * variant
7275 * @dev: device
7276 * @iter: list_head ** of the current position
7277 *
7278 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7279 * list, starting from iter position. The caller must hold RCU read lock.
7280 */
7281void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7282 struct list_head **iter)
7283{
7284 struct netdev_adjacent *lower;
7285
7286 WARN_ON_ONCE(!rcu_read_lock_held());
7287
7288 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7289
7290 if (&lower->list == &dev->adj_list.lower)
7291 return NULL;
7292
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007293 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007294
7295 return lower->private;
7296}
7297EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7298
7299/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007300 * netdev_lower_get_next - Get the next device from the lower neighbour
7301 * list
7302 * @dev: device
7303 * @iter: list_head ** of the current position
7304 *
7305 * Gets the next netdev_adjacent from the dev's lower neighbour
7306 * list, starting from iter position. The caller must hold RTNL lock or
7307 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007308 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007309 */
7310void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7311{
7312 struct netdev_adjacent *lower;
7313
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007314 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007315
7316 if (&lower->list == &dev->adj_list.lower)
7317 return NULL;
7318
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007319 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007320
7321 return lower->dev;
7322}
7323EXPORT_SYMBOL(netdev_lower_get_next);
7324
David Ahern1a3f0602016-10-17 19:15:44 -07007325static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7326 struct list_head **iter)
7327{
7328 struct netdev_adjacent *lower;
7329
David Ahern46b5ab12016-10-26 13:21:33 -07007330 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007331
7332 if (&lower->list == &dev->adj_list.lower)
7333 return NULL;
7334
David Ahern46b5ab12016-10-26 13:21:33 -07007335 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007336
7337 return lower->dev;
7338}
7339
Taehee Yoo32b6d342019-10-21 18:47:56 +00007340static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7341 struct list_head **iter,
7342 bool *ignore)
7343{
7344 struct netdev_adjacent *lower;
7345
7346 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7347
7348 if (&lower->list == &dev->adj_list.lower)
7349 return NULL;
7350
7351 *iter = &lower->list;
7352 *ignore = lower->ignore;
7353
7354 return lower->dev;
7355}
7356
David Ahern1a3f0602016-10-17 19:15:44 -07007357int netdev_walk_all_lower_dev(struct net_device *dev,
7358 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007359 struct netdev_nested_priv *priv),
7360 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007361{
Taehee Yoo5343da42019-10-21 18:47:50 +00007362 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7363 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7364 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007365
Taehee Yoo5343da42019-10-21 18:47:50 +00007366 now = dev;
7367 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007368
Taehee Yoo5343da42019-10-21 18:47:50 +00007369 while (1) {
7370 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007371 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007372 if (ret)
7373 return ret;
7374 }
7375
7376 next = NULL;
7377 while (1) {
7378 ldev = netdev_next_lower_dev(now, &iter);
7379 if (!ldev)
7380 break;
7381
7382 next = ldev;
7383 niter = &ldev->adj_list.lower;
7384 dev_stack[cur] = now;
7385 iter_stack[cur++] = iter;
7386 break;
7387 }
7388
7389 if (!next) {
7390 if (!cur)
7391 return 0;
7392 next = dev_stack[--cur];
7393 niter = iter_stack[cur];
7394 }
7395
7396 now = next;
7397 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007398 }
7399
7400 return 0;
7401}
7402EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7403
Taehee Yoo32b6d342019-10-21 18:47:56 +00007404static int __netdev_walk_all_lower_dev(struct net_device *dev,
7405 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007406 struct netdev_nested_priv *priv),
7407 struct netdev_nested_priv *priv)
Taehee Yoo32b6d342019-10-21 18:47:56 +00007408{
7409 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7410 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7411 int ret, cur = 0;
7412 bool ignore;
7413
7414 now = dev;
7415 iter = &dev->adj_list.lower;
7416
7417 while (1) {
7418 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007419 ret = fn(now, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007420 if (ret)
7421 return ret;
7422 }
7423
7424 next = NULL;
7425 while (1) {
7426 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7427 if (!ldev)
7428 break;
7429 if (ignore)
7430 continue;
7431
7432 next = ldev;
7433 niter = &ldev->adj_list.lower;
7434 dev_stack[cur] = now;
7435 iter_stack[cur++] = iter;
7436 break;
7437 }
7438
7439 if (!next) {
7440 if (!cur)
7441 return 0;
7442 next = dev_stack[--cur];
7443 niter = iter_stack[cur];
7444 }
7445
7446 now = next;
7447 iter = niter;
7448 }
7449
7450 return 0;
7451}
7452
Taehee Yoo7151aff2020-02-15 10:50:21 +00007453struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7454 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007455{
7456 struct netdev_adjacent *lower;
7457
7458 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7459 if (&lower->list == &dev->adj_list.lower)
7460 return NULL;
7461
7462 *iter = &lower->list;
7463
7464 return lower->dev;
7465}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007466EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007467
Taehee Yoo5343da42019-10-21 18:47:50 +00007468static u8 __netdev_upper_depth(struct net_device *dev)
7469{
7470 struct net_device *udev;
7471 struct list_head *iter;
7472 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007473 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007474
7475 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007476 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007477 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007478 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7479 if (ignore)
7480 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007481 if (max_depth < udev->upper_level)
7482 max_depth = udev->upper_level;
7483 }
7484
7485 return max_depth;
7486}
7487
7488static u8 __netdev_lower_depth(struct net_device *dev)
7489{
7490 struct net_device *ldev;
7491 struct list_head *iter;
7492 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007493 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007494
7495 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007496 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007497 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007498 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7499 if (ignore)
7500 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007501 if (max_depth < ldev->lower_level)
7502 max_depth = ldev->lower_level;
7503 }
7504
7505 return max_depth;
7506}
7507
Taehee Yooeff74232020-09-25 18:13:12 +00007508static int __netdev_update_upper_level(struct net_device *dev,
7509 struct netdev_nested_priv *__unused)
Taehee Yoo5343da42019-10-21 18:47:50 +00007510{
7511 dev->upper_level = __netdev_upper_depth(dev) + 1;
7512 return 0;
7513}
7514
Taehee Yooeff74232020-09-25 18:13:12 +00007515static int __netdev_update_lower_level(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007516 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007517{
7518 dev->lower_level = __netdev_lower_depth(dev) + 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007519
7520#ifdef CONFIG_LOCKDEP
7521 if (!priv)
7522 return 0;
7523
7524 if (priv->flags & NESTED_SYNC_IMM)
7525 dev->nested_level = dev->lower_level - 1;
7526 if (priv->flags & NESTED_SYNC_TODO)
7527 net_unlink_todo(dev);
7528#endif
Taehee Yoo5343da42019-10-21 18:47:50 +00007529 return 0;
7530}
7531
David Ahern1a3f0602016-10-17 19:15:44 -07007532int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7533 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007534 struct netdev_nested_priv *priv),
7535 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007536{
Taehee Yoo5343da42019-10-21 18:47:50 +00007537 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7538 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7539 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007540
Taehee Yoo5343da42019-10-21 18:47:50 +00007541 now = dev;
7542 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007543
Taehee Yoo5343da42019-10-21 18:47:50 +00007544 while (1) {
7545 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007546 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007547 if (ret)
7548 return ret;
7549 }
7550
7551 next = NULL;
7552 while (1) {
7553 ldev = netdev_next_lower_dev_rcu(now, &iter);
7554 if (!ldev)
7555 break;
7556
7557 next = ldev;
7558 niter = &ldev->adj_list.lower;
7559 dev_stack[cur] = now;
7560 iter_stack[cur++] = iter;
7561 break;
7562 }
7563
7564 if (!next) {
7565 if (!cur)
7566 return 0;
7567 next = dev_stack[--cur];
7568 niter = iter_stack[cur];
7569 }
7570
7571 now = next;
7572 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007573 }
7574
7575 return 0;
7576}
7577EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7578
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007579/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007580 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7581 * lower neighbour list, RCU
7582 * variant
7583 * @dev: device
7584 *
7585 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7586 * list. The caller must hold RCU read lock.
7587 */
7588void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7589{
7590 struct netdev_adjacent *lower;
7591
7592 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7593 struct netdev_adjacent, list);
7594 if (lower)
7595 return lower->private;
7596 return NULL;
7597}
7598EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7599
7600/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007601 * netdev_master_upper_dev_get_rcu - Get master upper device
7602 * @dev: device
7603 *
7604 * Find a master upper device and return pointer to it or NULL in case
7605 * it's not there. The caller must hold the RCU read lock.
7606 */
7607struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7608{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007609 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007610
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007611 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007612 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007613 if (upper && likely(upper->master))
7614 return upper->dev;
7615 return NULL;
7616}
7617EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7618
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307619static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007620 struct net_device *adj_dev,
7621 struct list_head *dev_list)
7622{
7623 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007624
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007625 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7626 "upper_%s" : "lower_%s", adj_dev->name);
7627 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7628 linkname);
7629}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307630static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007631 char *name,
7632 struct list_head *dev_list)
7633{
7634 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007635
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007636 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7637 "upper_%s" : "lower_%s", name);
7638 sysfs_remove_link(&(dev->dev.kobj), linkname);
7639}
7640
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007641static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7642 struct net_device *adj_dev,
7643 struct list_head *dev_list)
7644{
7645 return (dev_list == &dev->adj_list.upper ||
7646 dev_list == &dev->adj_list.lower) &&
7647 net_eq(dev_net(dev), dev_net(adj_dev));
7648}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007649
Veaceslav Falico5d261912013-08-28 23:25:05 +02007650static int __netdev_adjacent_dev_insert(struct net_device *dev,
7651 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007652 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007653 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007654{
7655 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007656 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007657
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007658 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007659
7660 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007661 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007662 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7663 dev->name, adj_dev->name, adj->ref_nr);
7664
Veaceslav Falico5d261912013-08-28 23:25:05 +02007665 return 0;
7666 }
7667
7668 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7669 if (!adj)
7670 return -ENOMEM;
7671
7672 adj->dev = adj_dev;
7673 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007674 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007675 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007676 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007677 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007678
David Ahern67b62f92016-10-17 19:15:53 -07007679 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7680 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007681
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007682 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007683 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007684 if (ret)
7685 goto free_adj;
7686 }
7687
Veaceslav Falico7863c052013-09-25 09:20:06 +02007688 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007689 if (master) {
7690 ret = sysfs_create_link(&(dev->dev.kobj),
7691 &(adj_dev->dev.kobj), "master");
7692 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007693 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007694
Veaceslav Falico7863c052013-09-25 09:20:06 +02007695 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007696 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007697 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007698 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007699
7700 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007701
Veaceslav Falico5831d662013-09-25 09:20:32 +02007702remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007703 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007704 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007705free_adj:
7706 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007707 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007708
7709 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007710}
7711
stephen hemminger1d143d92013-12-29 14:01:29 -08007712static void __netdev_adjacent_dev_remove(struct net_device *dev,
7713 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007714 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007715 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007716{
7717 struct netdev_adjacent *adj;
7718
David Ahern67b62f92016-10-17 19:15:53 -07007719 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7720 dev->name, adj_dev->name, ref_nr);
7721
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007722 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007723
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007724 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007725 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007726 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007727 WARN_ON(1);
7728 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007729 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007730
Andrew Collins93409032016-10-03 13:43:02 -06007731 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007732 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7733 dev->name, adj_dev->name, ref_nr,
7734 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007735 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007736 return;
7737 }
7738
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007739 if (adj->master)
7740 sysfs_remove_link(&(dev->dev.kobj), "master");
7741
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007742 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007743 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007744
Veaceslav Falico5d261912013-08-28 23:25:05 +02007745 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007746 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007747 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007748 dev_put(adj_dev);
7749 kfree_rcu(adj, rcu);
7750}
7751
stephen hemminger1d143d92013-12-29 14:01:29 -08007752static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7753 struct net_device *upper_dev,
7754 struct list_head *up_list,
7755 struct list_head *down_list,
7756 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007757{
7758 int ret;
7759
David Ahern790510d2016-10-17 19:15:43 -07007760 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007761 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007762 if (ret)
7763 return ret;
7764
David Ahern790510d2016-10-17 19:15:43 -07007765 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007766 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007767 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007768 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007769 return ret;
7770 }
7771
7772 return 0;
7773}
7774
stephen hemminger1d143d92013-12-29 14:01:29 -08007775static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7776 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007777 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007778 struct list_head *up_list,
7779 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007780{
Andrew Collins93409032016-10-03 13:43:02 -06007781 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7782 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007783}
7784
stephen hemminger1d143d92013-12-29 14:01:29 -08007785static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7786 struct net_device *upper_dev,
7787 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007788{
David Ahernf1170fd2016-10-17 19:15:51 -07007789 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7790 &dev->adj_list.upper,
7791 &upper_dev->adj_list.lower,
7792 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007793}
7794
stephen hemminger1d143d92013-12-29 14:01:29 -08007795static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7796 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007797{
Andrew Collins93409032016-10-03 13:43:02 -06007798 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007799 &dev->adj_list.upper,
7800 &upper_dev->adj_list.lower);
7801}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007802
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007803static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007804 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007805 void *upper_priv, void *upper_info,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007806 struct netdev_nested_priv *priv,
David Ahern42ab19e2017-10-04 17:48:47 -07007807 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007808{
David Ahern51d0c0472017-10-04 17:48:45 -07007809 struct netdev_notifier_changeupper_info changeupper_info = {
7810 .info = {
7811 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007812 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007813 },
7814 .upper_dev = upper_dev,
7815 .master = master,
7816 .linking = true,
7817 .upper_info = upper_info,
7818 };
Mike Manning50d629e2018-02-26 23:49:30 +00007819 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007820 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007821
7822 ASSERT_RTNL();
7823
7824 if (dev == upper_dev)
7825 return -EBUSY;
7826
7827 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007828 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007829 return -EBUSY;
7830
Taehee Yoo5343da42019-10-21 18:47:50 +00007831 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7832 return -EMLINK;
7833
Mike Manning50d629e2018-02-26 23:49:30 +00007834 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007835 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007836 return -EEXIST;
7837 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007838 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007839 if (master_dev)
7840 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7841 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007842
David Ahern51d0c0472017-10-04 17:48:45 -07007843 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007844 &changeupper_info.info);
7845 ret = notifier_to_errno(ret);
7846 if (ret)
7847 return ret;
7848
Jiri Pirko6dffb042015-12-03 12:12:10 +01007849 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007850 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007851 if (ret)
7852 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007853
David Ahern51d0c0472017-10-04 17:48:45 -07007854 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007855 &changeupper_info.info);
7856 ret = notifier_to_errno(ret);
7857 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007858 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007859
Taehee Yoo5343da42019-10-21 18:47:50 +00007860 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007861 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007862
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007863 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007864 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007865 priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007866
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007867 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007868
David Ahernf1170fd2016-10-17 19:15:51 -07007869rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007870 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007871
7872 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007873}
7874
7875/**
7876 * netdev_upper_dev_link - Add a link to the upper device
7877 * @dev: device
7878 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007879 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007880 *
7881 * Adds a link to device which is upper to this one. The caller must hold
7882 * the RTNL lock. On a failure a negative errno code is returned.
7883 * On success the reference counts are adjusted and the function
7884 * returns zero.
7885 */
7886int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007887 struct net_device *upper_dev,
7888 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007889{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007890 struct netdev_nested_priv priv = {
7891 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7892 .data = NULL,
7893 };
7894
David Ahern42ab19e2017-10-04 17:48:47 -07007895 return __netdev_upper_dev_link(dev, upper_dev, false,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007896 NULL, NULL, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007897}
7898EXPORT_SYMBOL(netdev_upper_dev_link);
7899
7900/**
7901 * netdev_master_upper_dev_link - Add a master link to the upper device
7902 * @dev: device
7903 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007904 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007905 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007906 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007907 *
7908 * Adds a link to device which is upper to this one. In this case, only
7909 * one master upper device can be linked, although other non-master devices
7910 * might be linked as well. The caller must hold the RTNL lock.
7911 * On a failure a negative errno code is returned. On success the reference
7912 * counts are adjusted and the function returns zero.
7913 */
7914int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007915 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007916 void *upper_priv, void *upper_info,
7917 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007918{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007919 struct netdev_nested_priv priv = {
7920 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7921 .data = NULL,
7922 };
7923
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007924 return __netdev_upper_dev_link(dev, upper_dev, true,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007925 upper_priv, upper_info, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007926}
7927EXPORT_SYMBOL(netdev_master_upper_dev_link);
7928
Taehee Yoofe8300f2020-09-25 18:13:02 +00007929static void __netdev_upper_dev_unlink(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007930 struct net_device *upper_dev,
7931 struct netdev_nested_priv *priv)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007932{
David Ahern51d0c0472017-10-04 17:48:45 -07007933 struct netdev_notifier_changeupper_info changeupper_info = {
7934 .info = {
7935 .dev = dev,
7936 },
7937 .upper_dev = upper_dev,
7938 .linking = false,
7939 };
tchardingf4563a72017-02-09 17:56:07 +11007940
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007941 ASSERT_RTNL();
7942
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007943 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007944
David Ahern51d0c0472017-10-04 17:48:45 -07007945 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007946 &changeupper_info.info);
7947
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007948 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007949
David Ahern51d0c0472017-10-04 17:48:45 -07007950 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007951 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007952
7953 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007954 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007955
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007956 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007957 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007958 priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007959}
Taehee Yoofe8300f2020-09-25 18:13:02 +00007960
7961/**
7962 * netdev_upper_dev_unlink - Removes a link to upper device
7963 * @dev: device
7964 * @upper_dev: new upper device
7965 *
7966 * Removes a link to device which is upper to this one. The caller must hold
7967 * the RTNL lock.
7968 */
7969void netdev_upper_dev_unlink(struct net_device *dev,
7970 struct net_device *upper_dev)
7971{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007972 struct netdev_nested_priv priv = {
7973 .flags = NESTED_SYNC_TODO,
7974 .data = NULL,
7975 };
7976
7977 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007978}
7979EXPORT_SYMBOL(netdev_upper_dev_unlink);
7980
Taehee Yoo32b6d342019-10-21 18:47:56 +00007981static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7982 struct net_device *lower_dev,
7983 bool val)
7984{
7985 struct netdev_adjacent *adj;
7986
7987 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7988 if (adj)
7989 adj->ignore = val;
7990
7991 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7992 if (adj)
7993 adj->ignore = val;
7994}
7995
7996static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7997 struct net_device *lower_dev)
7998{
7999 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
8000}
8001
8002static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
8003 struct net_device *lower_dev)
8004{
8005 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
8006}
8007
8008int netdev_adjacent_change_prepare(struct net_device *old_dev,
8009 struct net_device *new_dev,
8010 struct net_device *dev,
8011 struct netlink_ext_ack *extack)
8012{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008013 struct netdev_nested_priv priv = {
8014 .flags = 0,
8015 .data = NULL,
8016 };
Taehee Yoo32b6d342019-10-21 18:47:56 +00008017 int err;
8018
8019 if (!new_dev)
8020 return 0;
8021
8022 if (old_dev && new_dev != old_dev)
8023 netdev_adjacent_dev_disable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008024 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
8025 extack);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008026 if (err) {
8027 if (old_dev && new_dev != old_dev)
8028 netdev_adjacent_dev_enable(dev, old_dev);
8029 return err;
8030 }
8031
8032 return 0;
8033}
8034EXPORT_SYMBOL(netdev_adjacent_change_prepare);
8035
8036void netdev_adjacent_change_commit(struct net_device *old_dev,
8037 struct net_device *new_dev,
8038 struct net_device *dev)
8039{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008040 struct netdev_nested_priv priv = {
8041 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8042 .data = NULL,
8043 };
8044
Taehee Yoo32b6d342019-10-21 18:47:56 +00008045 if (!new_dev || !old_dev)
8046 return;
8047
8048 if (new_dev == old_dev)
8049 return;
8050
8051 netdev_adjacent_dev_enable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008052 __netdev_upper_dev_unlink(old_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008053}
8054EXPORT_SYMBOL(netdev_adjacent_change_commit);
8055
8056void netdev_adjacent_change_abort(struct net_device *old_dev,
8057 struct net_device *new_dev,
8058 struct net_device *dev)
8059{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008060 struct netdev_nested_priv priv = {
8061 .flags = 0,
8062 .data = NULL,
8063 };
8064
Taehee Yoo32b6d342019-10-21 18:47:56 +00008065 if (!new_dev)
8066 return;
8067
8068 if (old_dev && new_dev != old_dev)
8069 netdev_adjacent_dev_enable(dev, old_dev);
8070
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008071 __netdev_upper_dev_unlink(new_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008072}
8073EXPORT_SYMBOL(netdev_adjacent_change_abort);
8074
Moni Shoua61bd3852015-02-03 16:48:29 +02008075/**
8076 * netdev_bonding_info_change - Dispatch event about slave change
8077 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09008078 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02008079 *
8080 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8081 * The caller must hold the RTNL lock.
8082 */
8083void netdev_bonding_info_change(struct net_device *dev,
8084 struct netdev_bonding_info *bonding_info)
8085{
David Ahern51d0c0472017-10-04 17:48:45 -07008086 struct netdev_notifier_bonding_info info = {
8087 .info.dev = dev,
8088 };
Moni Shoua61bd3852015-02-03 16:48:29 +02008089
8090 memcpy(&info.bonding_info, bonding_info,
8091 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07008092 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02008093 &info.info);
8094}
8095EXPORT_SYMBOL(netdev_bonding_info_change);
8096
Maor Gottliebcff9f122020-04-30 22:21:31 +03008097/**
8098 * netdev_get_xmit_slave - Get the xmit slave of master device
Andrew Lunn88425002020-07-13 01:14:59 +02008099 * @dev: device
Maor Gottliebcff9f122020-04-30 22:21:31 +03008100 * @skb: The packet
8101 * @all_slaves: assume all the slaves are active
8102 *
8103 * The reference counters are not incremented so the caller must be
8104 * careful with locks. The caller must hold RCU lock.
8105 * %NULL is returned if no slave is found.
8106 */
8107
8108struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8109 struct sk_buff *skb,
8110 bool all_slaves)
8111{
8112 const struct net_device_ops *ops = dev->netdev_ops;
8113
8114 if (!ops->ndo_get_xmit_slave)
8115 return NULL;
8116 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8117}
8118EXPORT_SYMBOL(netdev_get_xmit_slave);
8119
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008120static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008121{
8122 struct netdev_adjacent *iter;
8123
8124 struct net *net = dev_net(dev);
8125
8126 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008127 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008128 continue;
8129 netdev_adjacent_sysfs_add(iter->dev, dev,
8130 &iter->dev->adj_list.lower);
8131 netdev_adjacent_sysfs_add(dev, iter->dev,
8132 &dev->adj_list.upper);
8133 }
8134
8135 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008136 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008137 continue;
8138 netdev_adjacent_sysfs_add(iter->dev, dev,
8139 &iter->dev->adj_list.upper);
8140 netdev_adjacent_sysfs_add(dev, iter->dev,
8141 &dev->adj_list.lower);
8142 }
8143}
8144
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008145static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008146{
8147 struct netdev_adjacent *iter;
8148
8149 struct net *net = dev_net(dev);
8150
8151 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008152 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008153 continue;
8154 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8155 &iter->dev->adj_list.lower);
8156 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8157 &dev->adj_list.upper);
8158 }
8159
8160 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008161 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008162 continue;
8163 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8164 &iter->dev->adj_list.upper);
8165 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8166 &dev->adj_list.lower);
8167 }
8168}
8169
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008170void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02008171{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008172 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02008173
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008174 struct net *net = dev_net(dev);
8175
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008176 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008177 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008178 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008179 netdev_adjacent_sysfs_del(iter->dev, oldname,
8180 &iter->dev->adj_list.lower);
8181 netdev_adjacent_sysfs_add(iter->dev, dev,
8182 &iter->dev->adj_list.lower);
8183 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008184
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008185 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008186 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008187 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008188 netdev_adjacent_sysfs_del(iter->dev, oldname,
8189 &iter->dev->adj_list.upper);
8190 netdev_adjacent_sysfs_add(iter->dev, dev,
8191 &iter->dev->adj_list.upper);
8192 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008193}
Veaceslav Falico402dae92013-09-25 09:20:09 +02008194
8195void *netdev_lower_dev_get_private(struct net_device *dev,
8196 struct net_device *lower_dev)
8197{
8198 struct netdev_adjacent *lower;
8199
8200 if (!lower_dev)
8201 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02008202 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02008203 if (!lower)
8204 return NULL;
8205
8206 return lower->private;
8207}
8208EXPORT_SYMBOL(netdev_lower_dev_get_private);
8209
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04008210
Jiri Pirko04d48262015-12-03 12:12:15 +01008211/**
8212 * netdev_lower_change - Dispatch event about lower device state change
8213 * @lower_dev: device
8214 * @lower_state_info: state to dispatch
8215 *
8216 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8217 * The caller must hold the RTNL lock.
8218 */
8219void netdev_lower_state_changed(struct net_device *lower_dev,
8220 void *lower_state_info)
8221{
David Ahern51d0c0472017-10-04 17:48:45 -07008222 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8223 .info.dev = lower_dev,
8224 };
Jiri Pirko04d48262015-12-03 12:12:15 +01008225
8226 ASSERT_RTNL();
8227 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07008228 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01008229 &changelowerstate_info.info);
8230}
8231EXPORT_SYMBOL(netdev_lower_state_changed);
8232
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008233static void dev_change_rx_flags(struct net_device *dev, int flags)
8234{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008235 const struct net_device_ops *ops = dev->netdev_ops;
8236
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05008237 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008238 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008239}
8240
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008241static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07008242{
Eric Dumazetb536db92011-11-30 21:42:26 +00008243 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06008244 kuid_t uid;
8245 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07008246
Patrick McHardy24023452007-07-14 18:51:31 -07008247 ASSERT_RTNL();
8248
Wang Chendad9b332008-06-18 01:48:28 -07008249 dev->flags |= IFF_PROMISC;
8250 dev->promiscuity += inc;
8251 if (dev->promiscuity == 0) {
8252 /*
8253 * Avoid overflow.
8254 * If inc causes overflow, untouch promisc and return error.
8255 */
8256 if (inc < 0)
8257 dev->flags &= ~IFF_PROMISC;
8258 else {
8259 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008260 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
8261 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008262 return -EOVERFLOW;
8263 }
8264 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008265 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008266 pr_info("device %s %s promiscuous mode\n",
8267 dev->name,
8268 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11008269 if (audit_enabled) {
8270 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04008271 audit_log(audit_context(), GFP_ATOMIC,
8272 AUDIT_ANOM_PROMISCUOUS,
8273 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8274 dev->name, (dev->flags & IFF_PROMISC),
8275 (old_flags & IFF_PROMISC),
8276 from_kuid(&init_user_ns, audit_get_loginuid(current)),
8277 from_kuid(&init_user_ns, uid),
8278 from_kgid(&init_user_ns, gid),
8279 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11008280 }
Patrick McHardy24023452007-07-14 18:51:31 -07008281
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008282 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07008283 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008284 if (notify)
8285 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07008286 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008287}
8288
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289/**
8290 * dev_set_promiscuity - update promiscuity count on a device
8291 * @dev: device
8292 * @inc: modifier
8293 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07008294 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 * remains above zero the interface remains promiscuous. Once it hits zero
8296 * the device reverts back to normal filtering operation. A negative inc
8297 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07008298 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 */
Wang Chendad9b332008-06-18 01:48:28 -07008300int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301{
Eric Dumazetb536db92011-11-30 21:42:26 +00008302 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07008303 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008305 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07008306 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07008307 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07008308 if (dev->flags != old_flags)
8309 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07008310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008312EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008314static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008316 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
Patrick McHardy24023452007-07-14 18:51:31 -07008318 ASSERT_RTNL();
8319
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07008321 dev->allmulti += inc;
8322 if (dev->allmulti == 0) {
8323 /*
8324 * Avoid overflow.
8325 * If inc causes overflow, untouch allmulti and return error.
8326 */
8327 if (inc < 0)
8328 dev->flags &= ~IFF_ALLMULTI;
8329 else {
8330 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008331 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8332 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008333 return -EOVERFLOW;
8334 }
8335 }
Patrick McHardy24023452007-07-14 18:51:31 -07008336 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008337 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008338 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008339 if (notify)
8340 __dev_notify_flags(dev, old_flags,
8341 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008342 }
Wang Chendad9b332008-06-18 01:48:28 -07008343 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008344}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008345
8346/**
8347 * dev_set_allmulti - update allmulti count on a device
8348 * @dev: device
8349 * @inc: modifier
8350 *
8351 * Add or remove reception of all multicast frames to a device. While the
8352 * count in the device remains above zero the interface remains listening
8353 * to all interfaces. Once it hits zero the device reverts back to normal
8354 * filtering operation. A negative @inc value is used to drop the counter
8355 * when releasing a resource needing all multicasts.
8356 * Return 0 if successful or a negative errno code on error.
8357 */
8358
8359int dev_set_allmulti(struct net_device *dev, int inc)
8360{
8361 return __dev_set_allmulti(dev, inc, true);
8362}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008363EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008364
8365/*
8366 * Upload unicast and multicast address lists to device and
8367 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008368 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008369 * are present.
8370 */
8371void __dev_set_rx_mode(struct net_device *dev)
8372{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008373 const struct net_device_ops *ops = dev->netdev_ops;
8374
Patrick McHardy4417da62007-06-27 01:28:10 -07008375 /* dev_open will call this function so the list will stay sane. */
8376 if (!(dev->flags&IFF_UP))
8377 return;
8378
8379 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008380 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008381
Jiri Pirko01789342011-08-16 06:29:00 +00008382 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008383 /* Unicast addresses changes may only happen under the rtnl,
8384 * therefore calling __dev_set_promiscuity here is safe.
8385 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008386 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008387 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008388 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008389 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008390 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008391 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008392 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008393 }
Jiri Pirko01789342011-08-16 06:29:00 +00008394
8395 if (ops->ndo_set_rx_mode)
8396 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008397}
8398
8399void dev_set_rx_mode(struct net_device *dev)
8400{
David S. Millerb9e40852008-07-15 00:15:08 -07008401 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008402 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008403 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404}
8405
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008406/**
8407 * dev_get_flags - get flags reported to userspace
8408 * @dev: device
8409 *
8410 * Get the combination of flag bits exported through APIs to userspace.
8411 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008412unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413{
Eric Dumazet95c96172012-04-15 05:58:06 +00008414 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415
8416 flags = (dev->flags & ~(IFF_PROMISC |
8417 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008418 IFF_RUNNING |
8419 IFF_LOWER_UP |
8420 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 (dev->gflags & (IFF_PROMISC |
8422 IFF_ALLMULTI));
8423
Stefan Rompfb00055a2006-03-20 17:09:11 -08008424 if (netif_running(dev)) {
8425 if (netif_oper_up(dev))
8426 flags |= IFF_RUNNING;
8427 if (netif_carrier_ok(dev))
8428 flags |= IFF_LOWER_UP;
8429 if (netif_dormant(dev))
8430 flags |= IFF_DORMANT;
8431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432
8433 return flags;
8434}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008435EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436
Petr Machata6d040322018-12-06 17:05:43 +00008437int __dev_change_flags(struct net_device *dev, unsigned int flags,
8438 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439{
Eric Dumazetb536db92011-11-30 21:42:26 +00008440 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008441 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
Patrick McHardy24023452007-07-14 18:51:31 -07008443 ASSERT_RTNL();
8444
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 /*
8446 * Set the flags on our device.
8447 */
8448
8449 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8450 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8451 IFF_AUTOMEDIA)) |
8452 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8453 IFF_ALLMULTI));
8454
8455 /*
8456 * Load in the correct multicast list now the flags have changed.
8457 */
8458
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008459 if ((old_flags ^ flags) & IFF_MULTICAST)
8460 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008461
Patrick McHardy4417da62007-06-27 01:28:10 -07008462 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
8464 /*
8465 * Have we downed the interface. We handle IFF_UP ourselves
8466 * according to user attempts to set it, rather than blindly
8467 * setting it.
8468 */
8469
8470 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008471 if ((old_flags ^ flags) & IFF_UP) {
8472 if (old_flags & IFF_UP)
8473 __dev_close(dev);
8474 else
Petr Machata40c900a2018-12-06 17:05:47 +00008475 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008479 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008480 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008481
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008483
8484 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8485 if (dev->flags != old_flags)
8486 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 }
8488
8489 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008490 * is important. Some (broken) drivers set IFF_PROMISC, when
8491 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 */
8493 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008494 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8495
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008497 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 }
8499
Patrick McHardybd380812010-02-26 06:34:53 +00008500 return ret;
8501}
8502
Nicolas Dichtela528c212013-09-25 12:02:44 +02008503void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8504 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008505{
8506 unsigned int changes = dev->flags ^ old_flags;
8507
Nicolas Dichtela528c212013-09-25 12:02:44 +02008508 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008509 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008510
Patrick McHardybd380812010-02-26 06:34:53 +00008511 if (changes & IFF_UP) {
8512 if (dev->flags & IFF_UP)
8513 call_netdevice_notifiers(NETDEV_UP, dev);
8514 else
8515 call_netdevice_notifiers(NETDEV_DOWN, dev);
8516 }
8517
8518 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008519 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008520 struct netdev_notifier_change_info change_info = {
8521 .info = {
8522 .dev = dev,
8523 },
8524 .flags_changed = changes,
8525 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008526
David Ahern51d0c0472017-10-04 17:48:45 -07008527 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008528 }
Patrick McHardybd380812010-02-26 06:34:53 +00008529}
8530
8531/**
8532 * dev_change_flags - change device settings
8533 * @dev: device
8534 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008535 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008536 *
8537 * Change settings on device based state flags. The flags are
8538 * in the userspace exported format.
8539 */
Petr Machata567c5e12018-12-06 17:05:42 +00008540int dev_change_flags(struct net_device *dev, unsigned int flags,
8541 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008542{
Eric Dumazetb536db92011-11-30 21:42:26 +00008543 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008544 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008545
Petr Machata6d040322018-12-06 17:05:43 +00008546 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008547 if (ret < 0)
8548 return ret;
8549
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008550 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008551 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 return ret;
8553}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008554EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555
WANG Congf51048c2017-07-06 15:01:57 -07008556int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008557{
8558 const struct net_device_ops *ops = dev->netdev_ops;
8559
8560 if (ops->ndo_change_mtu)
8561 return ops->ndo_change_mtu(dev, new_mtu);
8562
Eric Dumazet501a90c2019-12-05 20:43:46 -08008563 /* Pairs with all the lockless reads of dev->mtu in the stack */
8564 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008565 return 0;
8566}
WANG Congf51048c2017-07-06 15:01:57 -07008567EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008568
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008569int dev_validate_mtu(struct net_device *dev, int new_mtu,
8570 struct netlink_ext_ack *extack)
8571{
8572 /* MTU must be positive, and in range */
8573 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8574 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8575 return -EINVAL;
8576 }
8577
8578 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8579 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8580 return -EINVAL;
8581 }
8582 return 0;
8583}
8584
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008585/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008586 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008587 * @dev: device
8588 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008589 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008590 *
8591 * Change the maximum transfer size of the network device.
8592 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008593int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8594 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008596 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597
8598 if (new_mtu == dev->mtu)
8599 return 0;
8600
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008601 err = dev_validate_mtu(dev, new_mtu, extack);
8602 if (err)
8603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604
8605 if (!netif_device_present(dev))
8606 return -ENODEV;
8607
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008608 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8609 err = notifier_to_errno(err);
8610 if (err)
8611 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008612
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008613 orig_mtu = dev->mtu;
8614 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008616 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008617 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8618 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008619 err = notifier_to_errno(err);
8620 if (err) {
8621 /* setting mtu back and notifying everyone again,
8622 * so that they have a chance to revert changes.
8623 */
8624 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008625 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8626 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008627 }
8628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629 return err;
8630}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008631
8632int dev_set_mtu(struct net_device *dev, int new_mtu)
8633{
8634 struct netlink_ext_ack extack;
8635 int err;
8636
Li RongQinga6bcfc82018-08-03 15:45:21 +08008637 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008638 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008639 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008640 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8641 return err;
8642}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008643EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008645/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008646 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8647 * @dev: device
8648 * @new_len: new tx queue length
8649 */
8650int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8651{
8652 unsigned int orig_len = dev->tx_queue_len;
8653 int res;
8654
8655 if (new_len != (unsigned int)new_len)
8656 return -ERANGE;
8657
8658 if (new_len != orig_len) {
8659 dev->tx_queue_len = new_len;
8660 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8661 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008662 if (res)
8663 goto err_rollback;
8664 res = dev_qdisc_change_tx_queue_len(dev);
8665 if (res)
8666 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008667 }
8668
8669 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008670
8671err_rollback:
8672 netdev_err(dev, "refused to change device tx_queue_len\n");
8673 dev->tx_queue_len = orig_len;
8674 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008675}
8676
8677/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008678 * dev_set_group - Change group this device belongs to
8679 * @dev: device
8680 * @new_group: group this device should belong to
8681 */
8682void dev_set_group(struct net_device *dev, int new_group)
8683{
8684 dev->group = new_group;
8685}
8686EXPORT_SYMBOL(dev_set_group);
8687
8688/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008689 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8690 * @dev: device
8691 * @addr: new address
8692 * @extack: netlink extended ack
8693 */
8694int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8695 struct netlink_ext_ack *extack)
8696{
8697 struct netdev_notifier_pre_changeaddr_info info = {
8698 .info.dev = dev,
8699 .info.extack = extack,
8700 .dev_addr = addr,
8701 };
8702 int rc;
8703
8704 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8705 return notifier_to_errno(rc);
8706}
8707EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8708
8709/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008710 * dev_set_mac_address - Change Media Access Control Address
8711 * @dev: device
8712 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008713 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008714 *
8715 * Change the hardware (MAC) address of the device
8716 */
Petr Machata3a37a962018-12-13 11:54:30 +00008717int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8718 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008720 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 int err;
8722
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008723 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 return -EOPNOTSUPP;
8725 if (sa->sa_family != dev->type)
8726 return -EINVAL;
8727 if (!netif_device_present(dev))
8728 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008729 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8730 if (err)
8731 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008732 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008733 if (err)
8734 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008735 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008736 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008737 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008740EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741
Cong Wang1fc205d2021-02-11 11:34:10 -08008742static DECLARE_RWSEM(dev_addr_sem);
8743
8744int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
8745 struct netlink_ext_ack *extack)
8746{
8747 int ret;
8748
8749 down_write(&dev_addr_sem);
8750 ret = dev_set_mac_address(dev, sa, extack);
8751 up_write(&dev_addr_sem);
8752 return ret;
8753}
8754EXPORT_SYMBOL(dev_set_mac_address_user);
8755
8756int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
8757{
8758 size_t size = sizeof(sa->sa_data);
8759 struct net_device *dev;
8760 int ret = 0;
8761
8762 down_read(&dev_addr_sem);
8763 rcu_read_lock();
8764
8765 dev = dev_get_by_name_rcu(net, dev_name);
8766 if (!dev) {
8767 ret = -ENODEV;
8768 goto unlock;
8769 }
8770 if (!dev->addr_len)
8771 memset(sa->sa_data, 0, size);
8772 else
8773 memcpy(sa->sa_data, dev->dev_addr,
8774 min_t(size_t, size, dev->addr_len));
8775 sa->sa_family = dev->type;
8776
8777unlock:
8778 rcu_read_unlock();
8779 up_read(&dev_addr_sem);
8780 return ret;
8781}
8782EXPORT_SYMBOL(dev_get_mac_address);
8783
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008784/**
8785 * dev_change_carrier - Change device carrier
8786 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008787 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008788 *
8789 * Change device carrier
8790 */
8791int dev_change_carrier(struct net_device *dev, bool new_carrier)
8792{
8793 const struct net_device_ops *ops = dev->netdev_ops;
8794
8795 if (!ops->ndo_change_carrier)
8796 return -EOPNOTSUPP;
8797 if (!netif_device_present(dev))
8798 return -ENODEV;
8799 return ops->ndo_change_carrier(dev, new_carrier);
8800}
8801EXPORT_SYMBOL(dev_change_carrier);
8802
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008804 * dev_get_phys_port_id - Get device physical port ID
8805 * @dev: device
8806 * @ppid: port ID
8807 *
8808 * Get device physical port ID
8809 */
8810int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008811 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008812{
8813 const struct net_device_ops *ops = dev->netdev_ops;
8814
8815 if (!ops->ndo_get_phys_port_id)
8816 return -EOPNOTSUPP;
8817 return ops->ndo_get_phys_port_id(dev, ppid);
8818}
8819EXPORT_SYMBOL(dev_get_phys_port_id);
8820
8821/**
David Aherndb24a902015-03-17 20:23:15 -06008822 * dev_get_phys_port_name - Get device physical port name
8823 * @dev: device
8824 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008825 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008826 *
8827 * Get device physical port name
8828 */
8829int dev_get_phys_port_name(struct net_device *dev,
8830 char *name, size_t len)
8831{
8832 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008833 int err;
David Aherndb24a902015-03-17 20:23:15 -06008834
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008835 if (ops->ndo_get_phys_port_name) {
8836 err = ops->ndo_get_phys_port_name(dev, name, len);
8837 if (err != -EOPNOTSUPP)
8838 return err;
8839 }
8840 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008841}
8842EXPORT_SYMBOL(dev_get_phys_port_name);
8843
8844/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008845 * dev_get_port_parent_id - Get the device's port parent identifier
8846 * @dev: network device
8847 * @ppid: pointer to a storage for the port's parent identifier
8848 * @recurse: allow/disallow recursion to lower devices
8849 *
8850 * Get the devices's port parent identifier
8851 */
8852int dev_get_port_parent_id(struct net_device *dev,
8853 struct netdev_phys_item_id *ppid,
8854 bool recurse)
8855{
8856 const struct net_device_ops *ops = dev->netdev_ops;
8857 struct netdev_phys_item_id first = { };
8858 struct net_device *lower_dev;
8859 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008860 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008861
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008862 if (ops->ndo_get_port_parent_id) {
8863 err = ops->ndo_get_port_parent_id(dev, ppid);
8864 if (err != -EOPNOTSUPP)
8865 return err;
8866 }
8867
8868 err = devlink_compat_switch_id_get(dev, ppid);
8869 if (!err || err != -EOPNOTSUPP)
8870 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008871
8872 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008873 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008874
8875 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8876 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8877 if (err)
8878 break;
8879 if (!first.id_len)
8880 first = *ppid;
8881 else if (memcmp(&first, ppid, sizeof(*ppid)))
Ido Schimmele1b9efe2020-09-10 14:01:26 +03008882 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008883 }
8884
8885 return err;
8886}
8887EXPORT_SYMBOL(dev_get_port_parent_id);
8888
8889/**
8890 * netdev_port_same_parent_id - Indicate if two network devices have
8891 * the same port parent identifier
8892 * @a: first network device
8893 * @b: second network device
8894 */
8895bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8896{
8897 struct netdev_phys_item_id a_id = { };
8898 struct netdev_phys_item_id b_id = { };
8899
8900 if (dev_get_port_parent_id(a, &a_id, true) ||
8901 dev_get_port_parent_id(b, &b_id, true))
8902 return false;
8903
8904 return netdev_phys_item_id_same(&a_id, &b_id);
8905}
8906EXPORT_SYMBOL(netdev_port_same_parent_id);
8907
8908/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008909 * dev_change_proto_down - update protocol port state information
8910 * @dev: device
8911 * @proto_down: new value
8912 *
8913 * This info can be used by switch drivers to set the phys state of the
8914 * port.
8915 */
8916int dev_change_proto_down(struct net_device *dev, bool proto_down)
8917{
8918 const struct net_device_ops *ops = dev->netdev_ops;
8919
8920 if (!ops->ndo_change_proto_down)
8921 return -EOPNOTSUPP;
8922 if (!netif_device_present(dev))
8923 return -ENODEV;
8924 return ops->ndo_change_proto_down(dev, proto_down);
8925}
8926EXPORT_SYMBOL(dev_change_proto_down);
8927
Andy Roulinb5899672019-02-22 18:06:36 +00008928/**
8929 * dev_change_proto_down_generic - generic implementation for
8930 * ndo_change_proto_down that sets carrier according to
8931 * proto_down.
8932 *
8933 * @dev: device
8934 * @proto_down: new value
8935 */
8936int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8937{
8938 if (proto_down)
8939 netif_carrier_off(dev);
8940 else
8941 netif_carrier_on(dev);
8942 dev->proto_down = proto_down;
8943 return 0;
8944}
8945EXPORT_SYMBOL(dev_change_proto_down_generic);
8946
Roopa Prabhu829eb202020-07-31 17:34:01 -07008947/**
8948 * dev_change_proto_down_reason - proto down reason
8949 *
8950 * @dev: device
8951 * @mask: proto down mask
8952 * @value: proto down value
8953 */
8954void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8955 u32 value)
8956{
8957 int b;
8958
8959 if (!mask) {
8960 dev->proto_down_reason = value;
8961 } else {
8962 for_each_set_bit(b, &mask, 32) {
8963 if (value & (1 << b))
8964 dev->proto_down_reason |= BIT(b);
8965 else
8966 dev->proto_down_reason &= ~BIT(b);
8967 }
8968 }
8969}
8970EXPORT_SYMBOL(dev_change_proto_down_reason);
8971
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008972struct bpf_xdp_link {
8973 struct bpf_link link;
8974 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
8975 int flags;
8976};
8977
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008978static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008979{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008980 if (flags & XDP_FLAGS_HW_MODE)
8981 return XDP_MODE_HW;
8982 if (flags & XDP_FLAGS_DRV_MODE)
8983 return XDP_MODE_DRV;
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008984 if (flags & XDP_FLAGS_SKB_MODE)
8985 return XDP_MODE_SKB;
8986 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008987}
8988
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008989static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008990{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008991 switch (mode) {
8992 case XDP_MODE_SKB:
8993 return generic_xdp_install;
8994 case XDP_MODE_DRV:
8995 case XDP_MODE_HW:
8996 return dev->netdev_ops->ndo_bpf;
8997 default:
8998 return NULL;
8999 };
9000}
9001
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009002static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
9003 enum bpf_xdp_mode mode)
9004{
9005 return dev->xdp_state[mode].link;
9006}
9007
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009008static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
9009 enum bpf_xdp_mode mode)
9010{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009011 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
9012
9013 if (link)
9014 return link->link.prog;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009015 return dev->xdp_state[mode].prog;
9016}
9017
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009018static u8 dev_xdp_prog_count(struct net_device *dev)
9019{
9020 u8 count = 0;
9021 int i;
9022
9023 for (i = 0; i < __MAX_XDP_MODE; i++)
9024 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
9025 count++;
9026 return count;
9027}
9028
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009029u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
9030{
9031 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
9032
9033 return prog ? prog->aux->id : 0;
9034}
9035
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009036static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
9037 struct bpf_xdp_link *link)
9038{
9039 dev->xdp_state[mode].link = link;
9040 dev->xdp_state[mode].prog = NULL;
9041}
9042
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009043static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9044 struct bpf_prog *prog)
9045{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009046 dev->xdp_state[mode].link = NULL;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009047 dev->xdp_state[mode].prog = prog;
9048}
9049
9050static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9051 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9052 u32 flags, struct bpf_prog *prog)
9053{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009054 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01009055 int err;
9056
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009057 memset(&xdp, 0, sizeof(xdp));
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009058 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009059 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07009060 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009061 xdp.prog = prog;
9062
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009063 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
9064 * "moved" into driver), so they don't increment it on their own, but
9065 * they do decrement refcnt when program is detached or replaced.
9066 * Given net_device also owns link/prog, we need to bump refcnt here
9067 * to prevent drivers from underflowing it.
9068 */
9069 if (prog)
9070 bpf_prog_inc(prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009071 err = bpf_op(dev, &xdp);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009072 if (err) {
9073 if (prog)
9074 bpf_prog_put(prog);
9075 return err;
9076 }
Björn Töpel7e6897f2019-12-13 18:51:09 +01009077
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009078 if (mode != XDP_MODE_HW)
9079 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009080
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009081 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009082}
9083
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009084static void dev_xdp_uninstall(struct net_device *dev)
9085{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009086 struct bpf_xdp_link *link;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009087 struct bpf_prog *prog;
9088 enum bpf_xdp_mode mode;
9089 bpf_op_t bpf_op;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009090
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009091 ASSERT_RTNL();
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009092
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009093 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9094 prog = dev_xdp_prog(dev, mode);
9095 if (!prog)
9096 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009097
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009098 bpf_op = dev_xdp_bpf_op(dev, mode);
9099 if (!bpf_op)
9100 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009101
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009102 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9103
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009104 /* auto-detach link from net device */
9105 link = dev_xdp_link(dev, mode);
9106 if (link)
9107 link->dev = NULL;
9108 else
9109 bpf_prog_put(prog);
9110
9111 dev_xdp_set_link(dev, mode, NULL);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009112 }
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009113}
9114
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009115static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009116 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9117 struct bpf_prog *old_prog, u32 flags)
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009118{
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009119 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009120 struct bpf_prog *cur_prog;
9121 enum bpf_xdp_mode mode;
9122 bpf_op_t bpf_op;
9123 int err;
9124
9125 ASSERT_RTNL();
9126
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009127 /* either link or prog attachment, never both */
9128 if (link && (new_prog || old_prog))
9129 return -EINVAL;
9130 /* link supports only XDP mode flags */
9131 if (link && (flags & ~XDP_FLAGS_MODES)) {
9132 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9133 return -EINVAL;
9134 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009135 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9136 if (num_modes > 1) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009137 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9138 return -EINVAL;
9139 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009140 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9141 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9142 NL_SET_ERR_MSG(extack,
9143 "More than one program loaded, unset mode is ambiguous");
9144 return -EINVAL;
9145 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009146 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9147 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9148 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9149 return -EINVAL;
9150 }
9151
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009152 mode = dev_xdp_mode(dev, flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009153 /* can't replace attached link */
9154 if (dev_xdp_link(dev, mode)) {
9155 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9156 return -EBUSY;
9157 }
9158
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009159 cur_prog = dev_xdp_prog(dev, mode);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009160 /* can't replace attached prog with link */
9161 if (link && cur_prog) {
9162 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9163 return -EBUSY;
9164 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009165 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9166 NL_SET_ERR_MSG(extack, "Active program does not match expected");
9167 return -EEXIST;
9168 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009169
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009170 /* put effective new program into new_prog */
9171 if (link)
9172 new_prog = link->link.prog;
9173
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009174 if (new_prog) {
9175 bool offload = mode == XDP_MODE_HW;
9176 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9177 ? XDP_MODE_DRV : XDP_MODE_SKB;
9178
Andrii Nakryiko068d9d12020-08-11 19:29:23 -07009179 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9180 NL_SET_ERR_MSG(extack, "XDP program already attached");
9181 return -EBUSY;
9182 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009183 if (!offload && dev_xdp_prog(dev, other_mode)) {
9184 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
9185 return -EEXIST;
9186 }
9187 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
9188 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
9189 return -EINVAL;
9190 }
9191 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
9192 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
9193 return -EINVAL;
9194 }
9195 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9196 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
9197 return -EINVAL;
9198 }
9199 }
9200
9201 /* don't call drivers if the effective program didn't change */
9202 if (new_prog != cur_prog) {
9203 bpf_op = dev_xdp_bpf_op(dev, mode);
9204 if (!bpf_op) {
9205 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9206 return -EOPNOTSUPP;
9207 }
9208
9209 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9210 if (err)
9211 return err;
9212 }
9213
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009214 if (link)
9215 dev_xdp_set_link(dev, mode, link);
9216 else
9217 dev_xdp_set_prog(dev, mode, new_prog);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009218 if (cur_prog)
9219 bpf_prog_put(cur_prog);
9220
9221 return 0;
9222}
9223
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009224static int dev_xdp_attach_link(struct net_device *dev,
9225 struct netlink_ext_ack *extack,
9226 struct bpf_xdp_link *link)
9227{
9228 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9229}
9230
9231static int dev_xdp_detach_link(struct net_device *dev,
9232 struct netlink_ext_ack *extack,
9233 struct bpf_xdp_link *link)
9234{
9235 enum bpf_xdp_mode mode;
9236 bpf_op_t bpf_op;
9237
9238 ASSERT_RTNL();
9239
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009240 mode = dev_xdp_mode(dev, link->flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009241 if (dev_xdp_link(dev, mode) != link)
9242 return -EINVAL;
9243
9244 bpf_op = dev_xdp_bpf_op(dev, mode);
9245 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9246 dev_xdp_set_link(dev, mode, NULL);
9247 return 0;
9248}
9249
9250static void bpf_xdp_link_release(struct bpf_link *link)
9251{
9252 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9253
9254 rtnl_lock();
9255
9256 /* if racing with net_device's tear down, xdp_link->dev might be
9257 * already NULL, in which case link was already auto-detached
9258 */
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009259 if (xdp_link->dev) {
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009260 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009261 xdp_link->dev = NULL;
9262 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009263
9264 rtnl_unlock();
9265}
9266
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009267static int bpf_xdp_link_detach(struct bpf_link *link)
9268{
9269 bpf_xdp_link_release(link);
9270 return 0;
9271}
9272
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009273static void bpf_xdp_link_dealloc(struct bpf_link *link)
9274{
9275 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9276
9277 kfree(xdp_link);
9278}
9279
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009280static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9281 struct seq_file *seq)
9282{
9283 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9284 u32 ifindex = 0;
9285
9286 rtnl_lock();
9287 if (xdp_link->dev)
9288 ifindex = xdp_link->dev->ifindex;
9289 rtnl_unlock();
9290
9291 seq_printf(seq, "ifindex:\t%u\n", ifindex);
9292}
9293
9294static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9295 struct bpf_link_info *info)
9296{
9297 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9298 u32 ifindex = 0;
9299
9300 rtnl_lock();
9301 if (xdp_link->dev)
9302 ifindex = xdp_link->dev->ifindex;
9303 rtnl_unlock();
9304
9305 info->xdp.ifindex = ifindex;
9306 return 0;
9307}
9308
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009309static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9310 struct bpf_prog *old_prog)
9311{
9312 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9313 enum bpf_xdp_mode mode;
9314 bpf_op_t bpf_op;
9315 int err = 0;
9316
9317 rtnl_lock();
9318
9319 /* link might have been auto-released already, so fail */
9320 if (!xdp_link->dev) {
9321 err = -ENOLINK;
9322 goto out_unlock;
9323 }
9324
9325 if (old_prog && link->prog != old_prog) {
9326 err = -EPERM;
9327 goto out_unlock;
9328 }
9329 old_prog = link->prog;
9330 if (old_prog == new_prog) {
9331 /* no-op, don't disturb drivers */
9332 bpf_prog_put(new_prog);
9333 goto out_unlock;
9334 }
9335
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009336 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009337 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9338 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9339 xdp_link->flags, new_prog);
9340 if (err)
9341 goto out_unlock;
9342
9343 old_prog = xchg(&link->prog, new_prog);
9344 bpf_prog_put(old_prog);
9345
9346out_unlock:
9347 rtnl_unlock();
9348 return err;
9349}
9350
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009351static const struct bpf_link_ops bpf_xdp_link_lops = {
9352 .release = bpf_xdp_link_release,
9353 .dealloc = bpf_xdp_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009354 .detach = bpf_xdp_link_detach,
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009355 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9356 .fill_link_info = bpf_xdp_link_fill_link_info,
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009357 .update_prog = bpf_xdp_link_update,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009358};
9359
9360int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9361{
9362 struct net *net = current->nsproxy->net_ns;
9363 struct bpf_link_primer link_primer;
9364 struct bpf_xdp_link *link;
9365 struct net_device *dev;
9366 int err, fd;
9367
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009368 rtnl_lock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009369 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009370 if (!dev) {
9371 rtnl_unlock();
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009372 return -EINVAL;
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009373 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009374
9375 link = kzalloc(sizeof(*link), GFP_USER);
9376 if (!link) {
9377 err = -ENOMEM;
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009378 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009379 }
9380
9381 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9382 link->dev = dev;
9383 link->flags = attr->link_create.flags;
9384
9385 err = bpf_link_prime(&link->link, &link_primer);
9386 if (err) {
9387 kfree(link);
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009388 goto unlock;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009389 }
9390
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009391 err = dev_xdp_attach_link(dev, NULL, link);
9392 rtnl_unlock();
9393
9394 if (err) {
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009395 link->dev = NULL;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009396 bpf_link_cleanup(&link_primer);
9397 goto out_put_dev;
9398 }
9399
9400 fd = bpf_link_settle(&link_primer);
9401 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9402 dev_put(dev);
9403 return fd;
9404
Xuan Zhuoca9ba1d2021-07-10 11:16:35 +08009405unlock:
9406 rtnl_unlock();
9407
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009408out_put_dev:
9409 dev_put(dev);
9410 return err;
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009411}
9412
9413/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07009414 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9415 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07009416 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07009417 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009418 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01009419 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07009420 *
9421 * Set or clear a bpf program for a device
9422 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07009423int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009424 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07009425{
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009426 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009427 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07009428 int err;
9429
Daniel Borkmann85de8572016-11-28 23:16:54 +01009430 ASSERT_RTNL();
9431
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009432 if (fd >= 0) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009433 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9434 mode != XDP_MODE_SKB);
9435 if (IS_ERR(new_prog))
9436 return PTR_ERR(new_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009437 }
9438
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009439 if (expected_fd >= 0) {
9440 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9441 mode != XDP_MODE_SKB);
9442 if (IS_ERR(old_prog)) {
9443 err = PTR_ERR(old_prog);
9444 old_prog = NULL;
9445 goto err_out;
9446 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009447 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07009448
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009449 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009450
9451err_out:
9452 if (err && new_prog)
9453 bpf_prog_put(new_prog);
9454 if (old_prog)
9455 bpf_prog_put(old_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009456 return err;
9457}
Brenden Blancoa7862b42016-07-19 12:16:48 -07009458
9459/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07009461 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 *
9463 * Returns a suitable unique value for a new device interface
9464 * number. The caller must hold the rtnl semaphore or the
9465 * dev_base_lock to be sure it remains unique.
9466 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07009467static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009469 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11009470
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 for (;;) {
9472 if (++ifindex <= 0)
9473 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009474 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009475 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 }
9477}
9478
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08009480static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07009481DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482
Stephen Hemminger6f05f622007-03-08 20:46:03 -08009483static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009486 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487}
9488
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009489static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009490{
Krishna Kumare93737b2009-12-08 22:26:02 +00009491 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009492 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009493
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009494 BUG_ON(dev_boot_phase);
9495 ASSERT_RTNL();
9496
Krishna Kumare93737b2009-12-08 22:26:02 +00009497 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009498 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00009499 * for initialization unwind. Remove those
9500 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009501 */
9502 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009503 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
9504 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009505
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009506 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00009507 list_del(&dev->unreg_list);
9508 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009509 }
Eric Dumazet449f4542011-05-19 12:24:16 +00009510 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009511 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00009512 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009513
Octavian Purdila44345722010-12-13 12:44:07 +00009514 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009515 list_for_each_entry(dev, head, unreg_list)
9516 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04009517 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009518
Octavian Purdila44345722010-12-13 12:44:07 +00009519 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009520 /* And unlink it from device chain. */
9521 unlist_netdevice(dev);
9522
9523 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009524 }
Eric Dumazet41852492016-08-26 12:50:39 -07009525 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009526
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009527 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009528
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009529 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009530 struct sk_buff *skb = NULL;
9531
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009532 /* Shutdown queueing discipline. */
9533 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009534
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009535 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009536
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009537 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009538 * this device. They should clean all the things.
9539 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009540 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
9541
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009542 if (!dev->rtnl_link_ops ||
9543 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04009544 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009545 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009546
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009547 /*
9548 * Flush the unicast and multicast chains
9549 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009550 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00009551 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009552
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02009553 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02009554 netdev_name_node_free(dev->name_node);
9555
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009556 if (dev->netdev_ops->ndo_uninit)
9557 dev->netdev_ops->ndo_uninit(dev);
9558
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009559 if (skb)
9560 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07009561
Jiri Pirko9ff162a2013-01-03 22:48:49 +00009562 /* Notifier chain MUST detach us all upper devices. */
9563 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07009564 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009565
9566 /* Remove entries from kobject tree */
9567 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00009568#ifdef CONFIG_XPS
9569 /* Remove XPS queueing entries */
9570 netif_reset_xps_queues_gt(dev, 0);
9571#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009572 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009573
Eric W. Biederman850a5452011-10-13 22:25:23 +00009574 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009575
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00009576 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009577 dev_put(dev);
9578}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009579
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009580static void rollback_registered(struct net_device *dev)
9581{
9582 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009583
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009584 list_add(&dev->unreg_list, &single);
9585 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00009586 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009587}
9588
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009589static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9590 struct net_device *upper, netdev_features_t features)
9591{
9592 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9593 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009594 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009595
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009596 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009597 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009598 if (!(upper->wanted_features & feature)
9599 && (features & feature)) {
9600 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9601 &feature, upper->name);
9602 features &= ~feature;
9603 }
9604 }
9605
9606 return features;
9607}
9608
9609static void netdev_sync_lower_features(struct net_device *upper,
9610 struct net_device *lower, netdev_features_t features)
9611{
9612 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9613 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009614 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009615
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009616 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009617 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009618 if (!(features & feature) && (lower->features & feature)) {
9619 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9620 &feature, lower->name);
9621 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07009622 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009623
9624 if (unlikely(lower->features & feature))
9625 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9626 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07009627 else
9628 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009629 }
9630 }
9631}
9632
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009633static netdev_features_t netdev_fix_features(struct net_device *dev,
9634 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07009635{
Michał Mirosław57422dc2011-01-22 12:14:12 +00009636 /* Fix illegal checksum combinations */
9637 if ((features & NETIF_F_HW_CSUM) &&
9638 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009639 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00009640 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9641 }
9642
Herbert Xub63365a2008-10-23 01:11:29 -07009643 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009644 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009645 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009646 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07009647 }
9648
Pravin B Shelarec5f0612013-03-07 09:28:01 +00009649 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9650 !(features & NETIF_F_IP_CSUM)) {
9651 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9652 features &= ~NETIF_F_TSO;
9653 features &= ~NETIF_F_TSO_ECN;
9654 }
9655
9656 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9657 !(features & NETIF_F_IPV6_CSUM)) {
9658 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9659 features &= ~NETIF_F_TSO6;
9660 }
9661
Alexander Duyckb1dc4972016-05-02 09:38:24 -07009662 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9663 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9664 features &= ~NETIF_F_TSO_MANGLEID;
9665
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00009666 /* TSO ECN requires that TSO is present as well. */
9667 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9668 features &= ~NETIF_F_TSO_ECN;
9669
Michał Mirosław212b5732011-02-15 16:59:16 +00009670 /* Software GSO depends on SG. */
9671 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009672 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00009673 features &= ~NETIF_F_GSO;
9674 }
9675
Alexander Duyck802ab552016-04-10 21:45:03 -04009676 /* GSO partial features require GSO partial be set */
9677 if ((features & dev->gso_partial_features) &&
9678 !(features & NETIF_F_GSO_PARTIAL)) {
9679 netdev_dbg(dev,
9680 "Dropping partially supported GSO features since no GSO partial.\n");
9681 features &= ~dev->gso_partial_features;
9682 }
9683
Michael Chanfb1f5f72017-12-16 03:09:40 -05009684 if (!(features & NETIF_F_RXCSUM)) {
9685 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9686 * successfully merged by hardware must also have the
9687 * checksum verified by hardware. If the user does not
9688 * want to enable RXCSUM, logically, we should disable GRO_HW.
9689 */
9690 if (features & NETIF_F_GRO_HW) {
9691 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9692 features &= ~NETIF_F_GRO_HW;
9693 }
9694 }
9695
Gal Pressmande8d5ab2018-03-12 11:48:49 +02009696 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9697 if (features & NETIF_F_RXFCS) {
9698 if (features & NETIF_F_LRO) {
9699 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9700 features &= ~NETIF_F_LRO;
9701 }
9702
9703 if (features & NETIF_F_GRO_HW) {
9704 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9705 features &= ~NETIF_F_GRO_HW;
9706 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02009707 }
9708
Tariq Toukanf0f3d3e2021-01-17 17:15:38 +02009709 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9710 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9711 features &= ~NETIF_F_HW_TLS_RX;
9712 }
9713
Herbert Xub63365a2008-10-23 01:11:29 -07009714 return features;
9715}
Herbert Xub63365a2008-10-23 01:11:29 -07009716
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009717int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009718{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009719 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009720 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009721 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009722 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009723
Michał Mirosław87267482011-04-12 09:56:38 +00009724 ASSERT_RTNL();
9725
Michał Mirosław5455c692011-02-15 16:59:17 +00009726 features = netdev_get_wanted_features(dev);
9727
9728 if (dev->netdev_ops->ndo_fix_features)
9729 features = dev->netdev_ops->ndo_fix_features(dev, features);
9730
9731 /* driver might be less strict about feature dependencies */
9732 features = netdev_fix_features(dev, features);
9733
Randy Dunlap4250b752020-09-17 21:35:15 -07009734 /* some features can't be enabled if they're off on an upper device */
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009735 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9736 features = netdev_sync_upper_features(dev, upper, features);
9737
Michał Mirosław5455c692011-02-15 16:59:17 +00009738 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009739 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009740
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009741 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9742 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009743
9744 if (dev->netdev_ops->ndo_set_features)
9745 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009746 else
9747 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009748
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009749 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009750 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009751 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9752 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009753 /* return non-0 since some features might have changed and
9754 * it's better to fire a spurious notification than miss it
9755 */
9756 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009757 }
9758
Jarod Wilsone7868a82015-11-03 23:09:32 -05009759sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009760 /* some features must be disabled on lower devices when disabled
9761 * on an upper device (think: bonding master or bridge)
9762 */
9763 netdev_for_each_lower_dev(dev, lower, iter)
9764 netdev_sync_lower_features(dev, lower, features);
9765
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009766 if (!err) {
9767 netdev_features_t diff = features ^ dev->features;
9768
9769 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9770 /* udp_tunnel_{get,drop}_rx_info both need
9771 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9772 * device, or they won't do anything.
9773 * Thus we need to update dev->features
9774 * *before* calling udp_tunnel_get_rx_info,
9775 * but *after* calling udp_tunnel_drop_rx_info.
9776 */
9777 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9778 dev->features = features;
9779 udp_tunnel_get_rx_info(dev);
9780 } else {
9781 udp_tunnel_drop_rx_info(dev);
9782 }
9783 }
9784
Gal Pressman9daae9b2018-03-28 17:46:54 +03009785 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9786 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9787 dev->features = features;
9788 err |= vlan_get_rx_ctag_filter_info(dev);
9789 } else {
9790 vlan_drop_rx_ctag_filter_info(dev);
9791 }
9792 }
9793
9794 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9795 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9796 dev->features = features;
9797 err |= vlan_get_rx_stag_filter_info(dev);
9798 } else {
9799 vlan_drop_rx_stag_filter_info(dev);
9800 }
9801 }
9802
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009803 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009804 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009805
Jarod Wilsone7868a82015-11-03 23:09:32 -05009806 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009807}
9808
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009809/**
9810 * netdev_update_features - recalculate device features
9811 * @dev: the device to check
9812 *
9813 * Recalculate dev->features set and send notifications if it
9814 * has changed. Should be called after driver or hardware dependent
9815 * conditions might have changed that influence the features.
9816 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009817void netdev_update_features(struct net_device *dev)
9818{
9819 if (__netdev_update_features(dev))
9820 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009821}
9822EXPORT_SYMBOL(netdev_update_features);
9823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009825 * netdev_change_features - recalculate device features
9826 * @dev: the device to check
9827 *
9828 * Recalculate dev->features set and send notifications even
9829 * if they have not changed. Should be called instead of
9830 * netdev_update_features() if also dev->vlan_features might
9831 * have changed to allow the changes to be propagated to stacked
9832 * VLAN devices.
9833 */
9834void netdev_change_features(struct net_device *dev)
9835{
9836 __netdev_update_features(dev);
9837 netdev_features_change(dev);
9838}
9839EXPORT_SYMBOL(netdev_change_features);
9840
9841/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009842 * netif_stacked_transfer_operstate - transfer operstate
9843 * @rootdev: the root or lower level device to transfer state from
9844 * @dev: the device to transfer operstate to
9845 *
9846 * Transfer operational state from root to device. This is normally
9847 * called when a stacking relationship exists between the root
9848 * device and the device(a leaf device).
9849 */
9850void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9851 struct net_device *dev)
9852{
9853 if (rootdev->operstate == IF_OPER_DORMANT)
9854 netif_dormant_on(dev);
9855 else
9856 netif_dormant_off(dev);
9857
Andrew Lunneec517cd2020-04-20 00:11:50 +02009858 if (rootdev->operstate == IF_OPER_TESTING)
9859 netif_testing_on(dev);
9860 else
9861 netif_testing_off(dev);
9862
Zhang Shengju0575c862017-04-26 17:49:38 +08009863 if (netif_carrier_ok(rootdev))
9864 netif_carrier_on(dev);
9865 else
9866 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009867}
9868EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9869
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009870static int netif_alloc_rx_queues(struct net_device *dev)
9871{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009872 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009873 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309874 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009875 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009876
Tom Herbertbd25fa72010-10-18 18:00:16 +00009877 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009878
Michal Hockodcda9b02017-07-12 14:36:45 -07009879 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009880 if (!rx)
9881 return -ENOMEM;
9882
Tom Herbertbd25fa72010-10-18 18:00:16 +00009883 dev->_rx = rx;
9884
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009885 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009886 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009887
9888 /* XDP RX-queue setup */
9889 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9890 if (err < 0)
9891 goto err_rxq_info;
9892 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009893 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009894
9895err_rxq_info:
9896 /* Rollback successful reg's and free other resources */
9897 while (i--)
9898 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009899 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009900 dev->_rx = NULL;
9901 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009902}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009903
9904static void netif_free_rx_queues(struct net_device *dev)
9905{
9906 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009907
9908 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9909 if (!dev->_rx)
9910 return;
9911
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009912 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009913 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9914
9915 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009916}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009917
Changli Gaoaa942102010-12-04 02:31:41 +00009918static void netdev_init_one_queue(struct net_device *dev,
9919 struct netdev_queue *queue, void *_unused)
9920{
9921 /* Initialize queue lock */
9922 spin_lock_init(&queue->_xmit_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07009923 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
Changli Gaoaa942102010-12-04 02:31:41 +00009924 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009925 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009926 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009927#ifdef CONFIG_BQL
9928 dql_init(&queue->dql, HZ);
9929#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009930}
9931
Eric Dumazet60877a32013-06-20 01:15:51 -07009932static void netif_free_tx_queues(struct net_device *dev)
9933{
WANG Cong4cb28972014-06-02 15:55:22 -07009934 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009935}
9936
Tom Herberte6484932010-10-18 18:04:39 +00009937static int netif_alloc_netdev_queues(struct net_device *dev)
9938{
9939 unsigned int count = dev->num_tx_queues;
9940 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009941 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009942
Eric Dumazetd3397272015-07-06 17:13:26 +02009943 if (count < 1 || count > 0xffff)
9944 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009945
Michal Hockodcda9b02017-07-12 14:36:45 -07009946 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009947 if (!tx)
9948 return -ENOMEM;
9949
Tom Herberte6484932010-10-18 18:04:39 +00009950 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009951
Tom Herberte6484932010-10-18 18:04:39 +00009952 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9953 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009954
9955 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009956}
9957
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009958void netif_tx_stop_all_queues(struct net_device *dev)
9959{
9960 unsigned int i;
9961
9962 for (i = 0; i < dev->num_tx_queues; i++) {
9963 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009964
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009965 netif_tx_stop_queue(txq);
9966 }
9967}
9968EXPORT_SYMBOL(netif_tx_stop_all_queues);
9969
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009970/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 * register_netdevice - register a network device
9972 * @dev: device to register
9973 *
9974 * Take a completed network device structure and add it to the kernel
9975 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9976 * chain. 0 is returned on success. A negative errno code is returned
9977 * on a failure to set up the device, or if the name is a duplicate.
9978 *
9979 * Callers must hold the rtnl semaphore. You may want
9980 * register_netdev() instead of this.
9981 *
9982 * BUGS:
9983 * The locking appears insufficient to guarantee two parallel registers
9984 * will not get the same name.
9985 */
9986
9987int register_netdevice(struct net_device *dev)
9988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009990 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991
Florian Fainellie283de32018-04-30 14:20:05 -07009992 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9993 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994 BUG_ON(dev_boot_phase);
9995 ASSERT_RTNL();
9996
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009997 might_sleep();
9998
Linus Torvalds1da177e2005-04-16 15:20:36 -07009999 /* When net_device's are persistent, this will be fatal. */
10000 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010001 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002
Jakub Kicinski9000edb2020-03-16 13:47:12 -070010003 ret = ethtool_check_ops(dev->ethtool_ops);
10004 if (ret)
10005 return ret;
10006
David S. Millerf1f28aa2008-07-15 00:08:33 -070010007 spin_lock_init(&dev->addr_list_lock);
Cong Wang845e0eb2020-06-08 14:53:01 -070010008 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009
Gao feng828de4f2012-09-13 20:58:27 +000010010 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +000010011 if (ret < 0)
10012 goto out;
10013
Eric Dumazet9077f052019-10-03 08:59:24 -070010014 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +020010015 dev->name_node = netdev_name_node_head_alloc(dev);
10016 if (!dev->name_node)
10017 goto out;
10018
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010020 if (dev->netdev_ops->ndo_init) {
10021 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 if (ret) {
10023 if (ret > 0)
10024 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010025 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 }
10027 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010028
Patrick McHardyf6469682013-04-19 02:04:27 +000010029 if (((dev->hw_features | dev->features) &
10030 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +000010031 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
10032 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
10033 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
10034 ret = -EINVAL;
10035 goto err_uninit;
10036 }
10037
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +000010038 ret = -EBUSY;
10039 if (!dev->ifindex)
10040 dev->ifindex = dev_new_index(net);
10041 else if (__dev_get_by_index(net, dev->ifindex))
10042 goto err_uninit;
10043
Michał Mirosław5455c692011-02-15 16:59:17 +000010044 /* Transfer changeable features to wanted_features and enable
10045 * software offloads (GSO and GRO).
10046 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +010010047 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +000010048 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +020010049
10050 if (dev->netdev_ops->ndo_udp_tunnel_add) {
10051 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10052 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10053 }
10054
Michał Mirosław14d12322011-02-22 16:52:28 +000010055 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010057 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +000010058 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010059
Alexander Duyck7f348a62016-04-20 16:51:00 -040010060 /* If IPv4 TCP segmentation offload is supported we should also
10061 * allow the device to enable segmenting the frame with the option
10062 * of ignoring a static IP ID value. This doesn't enable the
10063 * feature itself but allows the user to enable it later.
10064 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010065 if (dev->hw_features & NETIF_F_TSO)
10066 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -040010067 if (dev->vlan_features & NETIF_F_TSO)
10068 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
10069 if (dev->mpls_features & NETIF_F_TSO)
10070 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
10071 if (dev->hw_enc_features & NETIF_F_TSO)
10072 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -070010073
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010074 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +000010075 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010076 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +000010077
Pravin B Shelaree579672013-03-07 09:28:08 +000010078 /* Make NETIF_F_SG inheritable to tunnel devices.
10079 */
Alexander Duyck802ab552016-04-10 21:45:03 -040010080 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +000010081
Simon Horman0d89d202013-05-23 21:02:52 +000010082 /* Make NETIF_F_SG inheritable to MPLS.
10083 */
10084 dev->mpls_features |= NETIF_F_SG;
10085
Johannes Berg7ffbe3f2009-10-02 05:15:27 +000010086 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
10087 ret = notifier_to_errno(ret);
10088 if (ret)
10089 goto err_uninit;
10090
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010091 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010092 if (ret) {
10093 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010094 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010095 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010096 dev->reg_state = NETREG_REGISTERED;
10097
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010098 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +000010099
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 /*
10101 * Default initial state at registry is that the
10102 * device is present.
10103 */
10104
10105 set_bit(__LINK_STATE_PRESENT, &dev->state);
10106
Ben Hutchings8f4cccb2012-08-20 22:16:51 +010010107 linkwatch_init_dev(dev);
10108
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010111 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -040010112 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
Jiri Pirko948b3372013-01-08 01:38:25 +000010114 /* If the device has permanent device address, driver should
10115 * set dev_addr and also addr_assign_type should be set to
10116 * NET_ADDR_PERM (default value).
10117 */
10118 if (dev->addr_assign_type == NET_ADDR_PERM)
10119 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010122 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -070010123 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010124 if (ret) {
10125 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -060010126 rcu_barrier();
10127
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010128 dev->reg_state = NETREG_UNREGISTERED;
Yang Yingliang814152a2020-06-16 09:39:21 +000010129 /* We should put the kobject that hold in
10130 * netdev_unregister_kobject(), otherwise
10131 * the net device cannot be freed when
10132 * driver calls free_netdev(), because the
10133 * kobject is being hold.
10134 */
10135 kobject_put(&dev->dev.kobj);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010136 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010137 /*
10138 * Prevent userspace races by waiting until the network
10139 * device is fully setup before sending notifications.
10140 */
Patrick McHardya2835762010-02-26 06:34:51 +000010141 if (!dev->rtnl_link_ops ||
10142 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010143 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144
10145out:
10146 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010147
10148err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010149 if (dev->netdev_ops->ndo_uninit)
10150 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -040010151 if (dev->priv_destructor)
10152 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010153err_free_name:
10154 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010157EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158
10159/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010160 * init_dummy_netdev - init a dummy network device for NAPI
10161 * @dev: device to init
10162 *
10163 * This takes a network device structure and initialize the minimum
10164 * amount of fields so it can be used to schedule NAPI polls without
10165 * registering a full blown interface. This is to be used by drivers
10166 * that need to tie several hardware interfaces to a single NAPI
10167 * poll scheduler due to HW limitations.
10168 */
10169int init_dummy_netdev(struct net_device *dev)
10170{
10171 /* Clear everything. Note we don't initialize spinlocks
10172 * are they aren't supposed to be taken by any of the
10173 * NAPI code and this dummy netdev is supposed to be
10174 * only ever used for NAPI polls
10175 */
10176 memset(dev, 0, sizeof(struct net_device));
10177
10178 /* make sure we BUG if trying to hit standard
10179 * register/unregister code path
10180 */
10181 dev->reg_state = NETREG_DUMMY;
10182
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010183 /* NAPI wants this */
10184 INIT_LIST_HEAD(&dev->napi_list);
10185
10186 /* a dummy interface is started by default */
10187 set_bit(__LINK_STATE_PRESENT, &dev->state);
10188 set_bit(__LINK_STATE_START, &dev->state);
10189
Josh Elsasser35edfdc2019-01-26 14:38:33 -080010190 /* napi_busy_loop stats accounting wants this */
10191 dev_net_set(dev, &init_net);
10192
Eric Dumazet29b44332010-10-11 10:22:12 +000010193 /* Note : We dont allocate pcpu_refcnt for dummy devices,
10194 * because users of this 'device' dont need to change
10195 * its refcount.
10196 */
10197
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010198 return 0;
10199}
10200EXPORT_SYMBOL_GPL(init_dummy_netdev);
10201
10202
10203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204 * register_netdev - register a network device
10205 * @dev: device to register
10206 *
10207 * Take a completed network device structure and add it to the kernel
10208 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10209 * chain. 0 is returned on success. A negative errno code is returned
10210 * on a failure to set up the device, or if the name is a duplicate.
10211 *
Borislav Petkov38b4da32007-04-20 22:14:10 -070010212 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 * and expands the device name if you passed a format string to
10214 * alloc_netdev.
10215 */
10216int register_netdev(struct net_device *dev)
10217{
10218 int err;
10219
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +030010220 if (rtnl_lock_killable())
10221 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 rtnl_unlock();
10224 return err;
10225}
10226EXPORT_SYMBOL(register_netdev);
10227
Eric Dumazet29b44332010-10-11 10:22:12 +000010228int netdev_refcnt_read(const struct net_device *dev)
10229{
10230 int i, refcnt = 0;
10231
10232 for_each_possible_cpu(i)
10233 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10234 return refcnt;
10235}
10236EXPORT_SYMBOL(netdev_refcnt_read);
10237
Mauro Carvalho Chehabde2b5412020-09-22 13:22:52 +020010238#define WAIT_REFS_MIN_MSECS 1
10239#define WAIT_REFS_MAX_MSECS 250
Ben Hutchings2c530402012-07-10 10:55:09 +000010240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +000010242 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 *
10244 * This is called when unregistering network devices.
10245 *
10246 * Any protocol or device that holds a reference should register
10247 * for netdevice notification, and cleanup and put back the
10248 * reference if they receive an UNREGISTER event.
10249 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010250 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251 */
10252static void netdev_wait_allrefs(struct net_device *dev)
10253{
10254 unsigned long rebroadcast_time, warning_time;
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010255 int wait = 0, refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010256
Eric Dumazete014deb2009-11-17 05:59:21 +000010257 linkwatch_forget_dev(dev);
10258
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +000010260 refcnt = netdev_refcnt_read(dev);
10261
10262 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010264 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265
10266 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010267 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268
Eric Dumazet748e2d92012-08-22 21:50:59 +000010269 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010270 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +000010271 rtnl_lock();
10272
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10274 &dev->state)) {
10275 /* We must not have linkwatch events
10276 * pending on unregister. If this
10277 * happens, we simply run the queue
10278 * unscheduled, resulting in a noop
10279 * for this device.
10280 */
10281 linkwatch_run_queue();
10282 }
10283
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010284 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285
10286 rebroadcast_time = jiffies;
10287 }
10288
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010289 if (!wait) {
10290 rcu_barrier();
10291 wait = WAIT_REFS_MIN_MSECS;
10292 } else {
10293 msleep(wait);
10294 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296
Eric Dumazet29b44332010-10-11 10:22:12 +000010297 refcnt = netdev_refcnt_read(dev);
10298
Eric Dumazetd7c04b02019-05-16 08:09:57 -070010299 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010300 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10301 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 warning_time = jiffies;
10303 }
10304 }
10305}
10306
10307/* The sequence is:
10308 *
10309 * rtnl_lock();
10310 * ...
10311 * register_netdevice(x1);
10312 * register_netdevice(x2);
10313 * ...
10314 * unregister_netdevice(y1);
10315 * unregister_netdevice(y2);
10316 * ...
10317 * rtnl_unlock();
10318 * free_netdev(y1);
10319 * free_netdev(y2);
10320 *
Herbert Xu58ec3b42008-10-07 15:50:03 -070010321 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010323 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324 * without deadlocking with linkwatch via keventd.
10325 * 2) Since we run with the RTNL semaphore not held, we can sleep
10326 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -070010327 *
10328 * We must not return until all unregister events added during
10329 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331void netdev_run_todo(void)
10332{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010333 struct list_head list;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010334#ifdef CONFIG_LOCKDEP
10335 struct list_head unlink_list;
10336
10337 list_replace_init(&net_unlink_list, &unlink_list);
10338
10339 while (!list_empty(&unlink_list)) {
10340 struct net_device *dev = list_first_entry(&unlink_list,
10341 struct net_device,
10342 unlink_list);
Taehee Yoo0e8b8d62020-10-15 16:26:06 +000010343 list_del_init(&dev->unlink_list);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010344 dev->nested_level = dev->lower_level - 1;
10345 }
10346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010347
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010349 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -070010350
10351 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010352
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010353
10354 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +000010355 if (!list_empty(&list))
10356 rcu_barrier();
10357
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 while (!list_empty(&list)) {
10359 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +000010360 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361 list_del(&dev->todo_list);
10362
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010363 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010364 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070010365 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010366 dump_stack();
10367 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010369
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010370 dev->reg_state = NETREG_UNREGISTERED;
10371
10372 netdev_wait_allrefs(dev);
10373
10374 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +000010375 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -080010376 BUG_ON(!list_empty(&dev->ptype_all));
10377 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +000010378 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10379 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -080010380#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -070010381 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -080010382#endif
David S. Millercf124db2017-05-08 12:52:56 -040010383 if (dev->priv_destructor)
10384 dev->priv_destructor(dev);
10385 if (dev->needs_free_netdev)
10386 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010387
Eric W. Biederman50624c92013-09-23 21:19:49 -070010388 /* Report a network device has been unregistered */
10389 rtnl_lock();
10390 dev_net(dev)->dev_unreg_count--;
10391 __rtnl_unlock();
10392 wake_up(&netdev_unregistering_wq);
10393
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010394 /* Free network device */
10395 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397}
10398
Jarod Wilson92566452016-02-01 18:51:04 -050010399/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
10400 * all the same fields in the same order as net_device_stats, with only
10401 * the type differing, but rtnl_link_stats64 may have additional fields
10402 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +000010403 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010404void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
10405 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +000010406{
10407#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -050010408 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +020010409 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -050010410 /* zero out counters that only exist in rtnl_link_stats64 */
10411 memset((char *)stats64 + sizeof(*netdev_stats), 0,
10412 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010413#else
Jarod Wilson92566452016-02-01 18:51:04 -050010414 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010415 const unsigned long *src = (const unsigned long *)netdev_stats;
10416 u64 *dst = (u64 *)stats64;
10417
Jarod Wilson92566452016-02-01 18:51:04 -050010418 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010419 for (i = 0; i < n; i++)
10420 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -050010421 /* zero out counters that only exist in rtnl_link_stats64 */
10422 memset((char *)stats64 + n * sizeof(u64), 0,
10423 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010424#endif
10425}
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010426EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010427
Eric Dumazetd83345a2009-11-16 03:36:51 +000010428/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010429 * dev_get_stats - get network device statistics
10430 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -070010431 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010432 *
Ben Hutchingsd7753512010-07-09 09:12:41 +000010433 * Get network statistics from device. Return @storage.
10434 * The device driver may provide its own method by setting
10435 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10436 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010437 */
Ben Hutchingsd7753512010-07-09 09:12:41 +000010438struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10439 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +000010440{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010441 const struct net_device_ops *ops = dev->netdev_ops;
10442
Eric Dumazet28172732010-07-07 14:58:56 -070010443 if (ops->ndo_get_stats64) {
10444 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010445 ops->ndo_get_stats64(dev, storage);
10446 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +000010447 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010448 } else {
10449 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -070010450 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -070010451 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10452 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10453 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -070010454 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -070010455}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010456EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -070010457
Heiner Kallweit44fa32f2020-10-12 10:01:27 +020010458/**
10459 * dev_fetch_sw_netstats - get per-cpu network device statistics
10460 * @s: place to store stats
10461 * @netstats: per-cpu network stats to read from
10462 *
10463 * Read per-cpu network statistics and populate the related fields in @s.
10464 */
10465void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10466 const struct pcpu_sw_netstats __percpu *netstats)
10467{
10468 int cpu;
10469
10470 for_each_possible_cpu(cpu) {
10471 const struct pcpu_sw_netstats *stats;
10472 struct pcpu_sw_netstats tmp;
10473 unsigned int start;
10474
10475 stats = per_cpu_ptr(netstats, cpu);
10476 do {
10477 start = u64_stats_fetch_begin_irq(&stats->syncp);
10478 tmp.rx_packets = stats->rx_packets;
10479 tmp.rx_bytes = stats->rx_bytes;
10480 tmp.tx_packets = stats->tx_packets;
10481 tmp.tx_bytes = stats->tx_bytes;
10482 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10483
10484 s->rx_packets += tmp.rx_packets;
10485 s->rx_bytes += tmp.rx_bytes;
10486 s->tx_packets += tmp.tx_packets;
10487 s->tx_bytes += tmp.tx_bytes;
10488 }
10489}
10490EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10491
Eric Dumazet24824a02010-10-02 06:11:55 +000010492struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -070010493{
Eric Dumazet24824a02010-10-02 06:11:55 +000010494 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -070010495
Eric Dumazet24824a02010-10-02 06:11:55 +000010496#ifdef CONFIG_NET_CLS_ACT
10497 if (queue)
10498 return queue;
10499 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10500 if (!queue)
10501 return NULL;
10502 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -080010503 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +000010504 queue->qdisc_sleeping = &noop_qdisc;
10505 rcu_assign_pointer(dev->ingress_queue, queue);
10506#endif
10507 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -070010508}
10509
Eric Dumazet2c60db02012-09-16 09:17:26 +000010510static const struct ethtool_ops default_ethtool_ops;
10511
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +000010512void netdev_set_default_ethtool_ops(struct net_device *dev,
10513 const struct ethtool_ops *ops)
10514{
10515 if (dev->ethtool_ops == &default_ethtool_ops)
10516 dev->ethtool_ops = ops;
10517}
10518EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10519
Eric Dumazet74d332c2013-10-30 13:10:44 -070010520void netdev_freemem(struct net_device *dev)
10521{
10522 char *addr = (char *)dev - dev->padded;
10523
WANG Cong4cb28972014-06-02 15:55:22 -070010524 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010525}
10526
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527/**
tcharding722c9a02017-02-09 17:56:04 +110010528 * alloc_netdev_mqs - allocate network device
10529 * @sizeof_priv: size of private data to allocate space for
10530 * @name: device name format string
10531 * @name_assign_type: origin of device name
10532 * @setup: callback to initialize device
10533 * @txqs: the number of TX subqueues to allocate
10534 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535 *
tcharding722c9a02017-02-09 17:56:04 +110010536 * Allocates a struct net_device with private data area for driver use
10537 * and performs basic initialization. Also allocates subqueue structs
10538 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 */
Tom Herbert36909ea2011-01-09 19:36:31 +000010540struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +020010541 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +000010542 void (*setup)(struct net_device *),
10543 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010544{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +030010546 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010547 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -070010549 BUG_ON(strlen(name) >= sizeof(dev->name));
10550
Tom Herbert36909ea2011-01-09 19:36:31 +000010551 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010552 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +000010553 return NULL;
10554 }
10555
Tom Herbert36909ea2011-01-09 19:36:31 +000010556 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010557 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +000010558 return NULL;
10559 }
Tom Herbert36909ea2011-01-09 19:36:31 +000010560
David S. Millerfd2ea0a2008-07-17 01:56:23 -070010561 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010562 if (sizeof_priv) {
10563 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010564 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010565 alloc_size += sizeof_priv;
10566 }
10567 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010568 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010569
Michal Hockodcda9b02017-07-12 14:36:45 -070010570 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +000010571 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010574 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010575 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010576
Eric Dumazet29b44332010-10-11 10:22:12 +000010577 dev->pcpu_refcnt = alloc_percpu(int);
10578 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -070010579 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010580
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +000010582 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010583
Jiri Pirko22bedad32010-04-01 21:22:57 +000010584 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010585 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +000010586
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010587 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -070010589 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +000010590 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +000010591 dev->upper_level = 1;
10592 dev->lower_level = 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010593#ifdef CONFIG_LOCKDEP
10594 dev->nested_level = 0;
10595 INIT_LIST_HEAD(&dev->unlink_list);
10596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597
Herbert Xud565b0a2008-12-15 23:38:52 -080010598 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010599 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -070010600 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +000010601 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +020010602 INIT_LIST_HEAD(&dev->adj_list.upper);
10603 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -080010604 INIT_LIST_HEAD(&dev->ptype_all);
10605 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +010010606 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +020010607#ifdef CONFIG_NET_SCHED
10608 hash_init(dev->qdisc_hash);
10609#endif
Eric Dumazet02875872014-10-05 18:38:35 -070010610 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010612
Phil Suttera8131042016-02-17 15:37:43 +010010613 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +020010614 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +010010615 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +010010616 }
Phil Sutter906470c2015-08-18 10:30:48 +020010617
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010618 dev->num_tx_queues = txqs;
10619 dev->real_num_tx_queues = txqs;
10620 if (netif_alloc_netdev_queues(dev))
10621 goto free_all;
10622
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010623 dev->num_rx_queues = rxqs;
10624 dev->real_num_rx_queues = rxqs;
10625 if (netif_alloc_rx_queues(dev))
10626 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010627
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +020010629 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +000010630 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +000010631 if (!dev->ethtool_ops)
10632 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +020010633
Daniel Borkmann357b6cc2020-03-18 10:33:22 +010010634 nf_hook_ingress_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +020010635
Linus Torvalds1da177e2005-04-16 15:20:36 -070010636 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010637
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010638free_all:
10639 free_netdev(dev);
10640 return NULL;
10641
Eric Dumazet29b44332010-10-11 10:22:12 +000010642free_pcpu:
10643 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010644free_dev:
10645 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010646 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647}
Tom Herbert36909ea2011-01-09 19:36:31 +000010648EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010649
10650/**
tcharding722c9a02017-02-09 17:56:04 +110010651 * free_netdev - free network device
10652 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010653 *
tcharding722c9a02017-02-09 17:56:04 +110010654 * This function does the last stage of destroying an allocated device
10655 * interface. The reference to the device object is released. If this
10656 * is the last reference then it will be freed.Must be called in process
10657 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010658 */
10659void free_netdev(struct net_device *dev)
10660{
Herbert Xud565b0a2008-12-15 23:38:52 -080010661 struct napi_struct *p, *n;
10662
Eric Dumazet93d05d42015-11-18 06:31:03 -080010663 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -070010664 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010665 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -070010666
Eric Dumazet33d480c2011-08-11 19:30:52 +000010667 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +000010668
Jiri Pirkof001fde2009-05-05 02:48:28 +000010669 /* Flush device addresses */
10670 dev_addr_flush(dev);
10671
Herbert Xud565b0a2008-12-15 23:38:52 -080010672 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10673 netif_napi_del(p);
10674
Eric Dumazet29b44332010-10-11 10:22:12 +000010675 free_percpu(dev->pcpu_refcnt);
10676 dev->pcpu_refcnt = NULL;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010010677 free_percpu(dev->xdp_bulkq);
10678 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +000010679
Stephen Hemminger3041a062006-05-26 13:25:24 -070010680 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010681 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -070010682 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010683 return;
10684 }
10685
10686 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10687 dev->reg_state = NETREG_RELEASED;
10688
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070010689 /* will free via device release */
10690 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010692EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010693
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010694/**
10695 * synchronize_net - Synchronize with packet receive processing
10696 *
10697 * Wait for packets currently being received to be done.
10698 * Does not block later packets from starting.
10699 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010700void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010701{
10702 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +000010703 if (rtnl_is_locked())
10704 synchronize_rcu_expedited();
10705 else
10706 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010707}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010708EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709
10710/**
Eric Dumazet44a08732009-10-27 07:03:04 +000010711 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +000010713 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -080010714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010715 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010716 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +000010717 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718 *
10719 * Callers must hold the rtnl semaphore. You may want
10720 * unregister_netdev() instead of this.
10721 */
10722
Eric Dumazet44a08732009-10-27 07:03:04 +000010723void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724{
Herbert Xua6620712007-12-12 19:21:56 -080010725 ASSERT_RTNL();
10726
Eric Dumazet44a08732009-10-27 07:03:04 +000010727 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010728 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +000010729 } else {
10730 rollback_registered(dev);
10731 /* Finish processing unregister after unlock */
10732 net_set_todo(dev);
10733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010734}
Eric Dumazet44a08732009-10-27 07:03:04 +000010735EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736
10737/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010738 * unregister_netdevice_many - unregister many devices
10739 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -070010740 *
10741 * Note: As most callers use a stack allocated list_head,
10742 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010743 */
10744void unregister_netdevice_many(struct list_head *head)
10745{
10746 struct net_device *dev;
10747
10748 if (!list_empty(head)) {
10749 rollback_registered_many(head);
10750 list_for_each_entry(dev, head, unreg_list)
10751 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -070010752 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010753 }
10754}
Eric Dumazet63c80992009-10-27 07:06:49 +000010755EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010756
10757/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758 * unregister_netdev - remove device from the kernel
10759 * @dev: device
10760 *
10761 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010762 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010763 *
10764 * This is just a wrapper for unregister_netdevice that takes
10765 * the rtnl semaphore. In general you want to use this and not
10766 * unregister_netdevice.
10767 */
10768void unregister_netdev(struct net_device *dev)
10769{
10770 rtnl_lock();
10771 unregister_netdevice(dev);
10772 rtnl_unlock();
10773}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010774EXPORT_SYMBOL(unregister_netdev);
10775
Eric W. Biedermance286d32007-09-12 13:53:49 +020010776/**
10777 * dev_change_net_namespace - move device to different nethost namespace
10778 * @dev: device
10779 * @net: network namespace
10780 * @pat: If not NULL name pattern to try if the current device name
10781 * is already taken in the destination network namespace.
10782 *
10783 * This function shuts down a device interface and moves it
10784 * to a new network namespace. On success 0 is returned, on
10785 * a failure a netagive errno code is returned.
10786 *
10787 * Callers must hold the rtnl semaphore.
10788 */
10789
10790int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10791{
Christian Brauneref6a4c82020-02-27 04:37:19 +010010792 struct net *net_old = dev_net(dev);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010793 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010794
10795 ASSERT_RTNL();
10796
10797 /* Don't allow namespace local devices to be moved. */
10798 err = -EINVAL;
10799 if (dev->features & NETIF_F_NETNS_LOCAL)
10800 goto out;
10801
10802 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010803 if (dev->reg_state != NETREG_REGISTERED)
10804 goto out;
10805
10806 /* Get out if there is nothing todo */
10807 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010010808 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010809 goto out;
10810
10811 /* Pick the destination device name, and ensure
10812 * we can use it in the destination network namespace.
10813 */
10814 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000010815 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010816 /* We get here if we can't use the current device name */
10817 if (!pat)
10818 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010819 err = dev_get_valid_name(net, dev, pat);
10820 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010821 goto out;
10822 }
10823
10824 /*
10825 * And now a mini version of register_netdevice unregister_netdevice.
10826 */
10827
10828 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010829 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010830
10831 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010832 unlist_netdevice(dev);
10833
10834 synchronize_net();
10835
10836 /* Shutdown queueing discipline. */
10837 dev_shutdown(dev);
10838
10839 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010840 * this device. They should clean all the things.
10841 *
10842 * Note that dev->reg_state stays at NETREG_REGISTERED.
10843 * This is wanted because this way 8021q and macvlan know
10844 * the device is just moving and can keep their slaves up.
10845 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010846 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010847 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010848
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010849 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010850 /* If there is an ifindex conflict assign a new one */
10851 if (__dev_get_by_index(net, dev->ifindex))
10852 new_ifindex = dev_new_index(net);
10853 else
10854 new_ifindex = dev->ifindex;
10855
10856 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10857 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010858
10859 /*
10860 * Flush the unicast and multicast chains
10861 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010862 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010863 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010864
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010865 /* Send a netdev-removed uevent to the old namespace */
10866 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010867 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010868
Jiri Pirko93642e12020-01-25 12:17:08 +010010869 /* Move per-net netdevice notifiers that are following the netdevice */
10870 move_netdevice_notifiers_dev_net(dev, net);
10871
Eric W. Biedermance286d32007-09-12 13:53:49 +020010872 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010873 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010874 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010875
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010876 /* Send a netdev-add uevent to the new namespace */
10877 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010878 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010879
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010880 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010881 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010882 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010883
Christian Brauneref6a4c82020-02-27 04:37:19 +010010884 /* Adapt owner in case owning user namespace of target network
10885 * namespace is different from the original one.
10886 */
10887 err = netdev_change_owner(dev, net_old, net);
10888 WARN_ON(err);
10889
Eric W. Biedermance286d32007-09-12 13:53:49 +020010890 /* Add the device back in the hashes */
10891 list_netdevice(dev);
10892
10893 /* Notify protocols, that a new device appeared. */
10894 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10895
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010896 /*
10897 * Prevent userspace races by waiting until the network
10898 * device is fully setup before sending notifications.
10899 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010900 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010901
Eric W. Biedermance286d32007-09-12 13:53:49 +020010902 synchronize_net();
10903 err = 0;
10904out:
10905 return err;
10906}
Johannes Berg463d0182009-07-14 00:33:35 +020010907EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010908
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010909static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910{
10911 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010912 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010913 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010914 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010915
Linus Torvalds1da177e2005-04-16 15:20:36 -070010916 local_irq_disable();
10917 cpu = smp_processor_id();
10918 sd = &per_cpu(softnet_data, cpu);
10919 oldsd = &per_cpu(softnet_data, oldcpu);
10920
10921 /* Find end of our completion_queue. */
10922 list_skb = &sd->completion_queue;
10923 while (*list_skb)
10924 list_skb = &(*list_skb)->next;
10925 /* Append completion queue from offline CPU. */
10926 *list_skb = oldsd->completion_queue;
10927 oldsd->completion_queue = NULL;
10928
Linus Torvalds1da177e2005-04-16 15:20:36 -070010929 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010930 if (oldsd->output_queue) {
10931 *sd->output_queue_tailp = oldsd->output_queue;
10932 sd->output_queue_tailp = oldsd->output_queue_tailp;
10933 oldsd->output_queue = NULL;
10934 oldsd->output_queue_tailp = &oldsd->output_queue;
10935 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010936 /* Append NAPI poll list from offline CPU, with one exception :
10937 * process_backlog() must be called by cpu owning percpu backlog.
10938 * We properly handle process_queue & input_pkt_queue later.
10939 */
10940 while (!list_empty(&oldsd->poll_list)) {
10941 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10942 struct napi_struct,
10943 poll_list);
10944
10945 list_del_init(&napi->poll_list);
10946 if (napi->poll == process_backlog)
10947 napi->state = 0;
10948 else
10949 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010951
10952 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10953 local_irq_enable();
10954
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010955#ifdef CONFIG_RPS
10956 remsd = oldsd->rps_ipi_list;
10957 oldsd->rps_ipi_list = NULL;
10958#endif
10959 /* send out pending IPI's on offline CPU */
10960 net_rps_send_ipi(remsd);
10961
Linus Torvalds1da177e2005-04-16 15:20:36 -070010962 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010963 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010964 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010965 input_queue_head_incr(oldsd);
10966 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010967 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010968 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010969 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010971
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010973}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010974
Herbert Xu7f353bf2007-08-10 15:47:58 -070010975/**
Herbert Xub63365a2008-10-23 01:11:29 -070010976 * netdev_increment_features - increment feature set by one
10977 * @all: current feature set
10978 * @one: new feature set
10979 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010980 *
10981 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010982 * @one to the master device with current feature set @all. Will not
10983 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010984 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010985netdev_features_t netdev_increment_features(netdev_features_t all,
10986 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010987{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010988 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010989 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010990 mask |= NETIF_F_VLAN_CHALLENGED;
10991
Tom Herberta1882222015-12-14 11:19:43 -080010992 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010993 all &= one | ~NETIF_F_ALL_FOR_ALL;
10994
Michał Mirosław1742f182011-04-22 06:31:16 +000010995 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010996 if (all & NETIF_F_HW_CSUM)
10997 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010998
10999 return all;
11000}
Herbert Xub63365a2008-10-23 01:11:29 -070011001EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070011002
Baruch Siach430f03c2013-06-02 20:43:55 +000011003static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011004{
11005 int i;
11006 struct hlist_head *hash;
11007
Kees Cook6da2ec52018-06-12 13:55:00 -070011008 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011009 if (hash != NULL)
11010 for (i = 0; i < NETDEV_HASHENTRIES; i++)
11011 INIT_HLIST_HEAD(&hash[i]);
11012
11013 return hash;
11014}
11015
Eric W. Biederman881d9662007-09-17 11:56:21 -070011016/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070011017static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011018{
Li RongQingd9f37d02018-07-13 14:41:36 +080011019 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080011020 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080011021
Rustad, Mark D734b6542012-07-18 09:06:07 +000011022 if (net != &init_net)
11023 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070011024
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011025 net->dev_name_head = netdev_create_hash();
11026 if (net->dev_name_head == NULL)
11027 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011028
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011029 net->dev_index_head = netdev_create_hash();
11030 if (net->dev_index_head == NULL)
11031 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011032
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020011033 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
11034
Eric W. Biederman881d9662007-09-17 11:56:21 -070011035 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011036
11037err_idx:
11038 kfree(net->dev_name_head);
11039err_name:
11040 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011041}
11042
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011043/**
11044 * netdev_drivername - network driver for the device
11045 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011046 *
11047 * Determine network driver for device.
11048 */
David S. Miller3019de12011-06-06 16:41:33 -070011049const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070011050{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070011051 const struct device_driver *driver;
11052 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070011053 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070011054
11055 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011056 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070011057 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011058
11059 driver = parent->driver;
11060 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070011061 return driver->name;
11062 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011063}
11064
Joe Perches6ea754e2014-09-22 11:10:50 -070011065static void __netdev_printk(const char *level, const struct net_device *dev,
11066 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000011067{
Joe Perchesb004ff42012-09-12 20:12:19 -070011068 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011069 dev_printk_emit(level[1] - '0',
11070 dev->dev.parent,
11071 "%s %s %s%s: %pV",
11072 dev_driver_string(dev->dev.parent),
11073 dev_name(dev->dev.parent),
11074 netdev_name(dev), netdev_reg_state(dev),
11075 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011076 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011077 printk("%s%s%s: %pV",
11078 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011079 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070011080 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011081 }
Joe Perches256df2f2010-06-27 01:02:35 +000011082}
11083
Joe Perches6ea754e2014-09-22 11:10:50 -070011084void netdev_printk(const char *level, const struct net_device *dev,
11085 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000011086{
11087 struct va_format vaf;
11088 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000011089
11090 va_start(args, format);
11091
11092 vaf.fmt = format;
11093 vaf.va = &args;
11094
Joe Perches6ea754e2014-09-22 11:10:50 -070011095 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011096
Joe Perches256df2f2010-06-27 01:02:35 +000011097 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000011098}
11099EXPORT_SYMBOL(netdev_printk);
11100
11101#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070011102void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000011103{ \
Joe Perches256df2f2010-06-27 01:02:35 +000011104 struct va_format vaf; \
11105 va_list args; \
11106 \
11107 va_start(args, fmt); \
11108 \
11109 vaf.fmt = fmt; \
11110 vaf.va = &args; \
11111 \
Joe Perches6ea754e2014-09-22 11:10:50 -070011112 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070011113 \
Joe Perches256df2f2010-06-27 01:02:35 +000011114 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000011115} \
11116EXPORT_SYMBOL(func);
11117
11118define_netdev_printk_level(netdev_emerg, KERN_EMERG);
11119define_netdev_printk_level(netdev_alert, KERN_ALERT);
11120define_netdev_printk_level(netdev_crit, KERN_CRIT);
11121define_netdev_printk_level(netdev_err, KERN_ERR);
11122define_netdev_printk_level(netdev_warn, KERN_WARNING);
11123define_netdev_printk_level(netdev_notice, KERN_NOTICE);
11124define_netdev_printk_level(netdev_info, KERN_INFO);
11125
Pavel Emelyanov46650792007-10-08 20:38:39 -070011126static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011127{
11128 kfree(net->dev_name_head);
11129 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030011130 if (net != &init_net)
11131 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070011132}
11133
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011134static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070011135 .init = netdev_init,
11136 .exit = netdev_exit,
11137};
11138
Pavel Emelyanov46650792007-10-08 20:38:39 -070011139static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011140{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011141 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011142 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011143 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020011144 * initial network namespace
11145 */
11146 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011147 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011148 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011149 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020011150
11151 /* Ignore unmoveable devices (i.e. loopback) */
11152 if (dev->features & NETIF_F_NETNS_LOCAL)
11153 continue;
11154
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011155 /* Leave virtual devices for the generic cleanup */
Martin Willi8dc08a22021-03-02 13:24:23 +010011156 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011157 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080011158
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011159 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011160 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020011161 if (__dev_get_by_name(&init_net, fb_name))
11162 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011163 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011164 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000011165 pr_emerg("%s: failed to move %s to init_net: %d\n",
11166 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011167 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020011168 }
11169 }
11170 rtnl_unlock();
11171}
11172
Eric W. Biederman50624c92013-09-23 21:19:49 -070011173static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
11174{
11175 /* Return with the rtnl_lock held when there are no network
11176 * devices unregistering in any network namespace in net_list.
11177 */
11178 struct net *net;
11179 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010011180 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011181
Peter Zijlstraff960a72014-10-29 17:04:56 +010011182 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011183 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070011184 unregistering = false;
11185 rtnl_lock();
11186 list_for_each_entry(net, net_list, exit_list) {
11187 if (net->dev_unreg_count > 0) {
11188 unregistering = true;
11189 break;
11190 }
11191 }
11192 if (!unregistering)
11193 break;
11194 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010011195
11196 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011197 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010011198 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011199}
11200
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011201static void __net_exit default_device_exit_batch(struct list_head *net_list)
11202{
11203 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011204 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011205 * Do this across as many network namespaces as possible to
11206 * improve batching efficiency.
11207 */
11208 struct net_device *dev;
11209 struct net *net;
11210 LIST_HEAD(dev_kill_list);
11211
Eric W. Biederman50624c92013-09-23 21:19:49 -070011212 /* To prevent network device cleanup code from dereferencing
11213 * loopback devices or network devices that have been freed
11214 * wait here for all pending unregistrations to complete,
11215 * before unregistring the loopback device and allowing the
11216 * network namespace be freed.
11217 *
11218 * The netdev todo list containing all network devices
11219 * unregistrations that happen in default_device_exit_batch
11220 * will run in the rtnl_unlock() at the end of
11221 * default_device_exit_batch.
11222 */
11223 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011224 list_for_each_entry(net, net_list, exit_list) {
11225 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020011226 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011227 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
11228 else
11229 unregister_netdevice_queue(dev, &dev_kill_list);
11230 }
11231 }
11232 unregister_netdevice_many(&dev_kill_list);
11233 rtnl_unlock();
11234}
11235
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011236static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011237 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011238 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020011239};
11240
Linus Torvalds1da177e2005-04-16 15:20:36 -070011241/*
11242 * Initialize the DEV module. At boot time this walks the device list and
11243 * unhooks any devices that fail to initialise (normally hardware not
11244 * present) and leaves us with a valid list of present and active devices.
11245 *
11246 */
11247
11248/*
11249 * This is called single threaded during boot, so no need
11250 * to take the rtnl semaphore.
11251 */
11252static int __init net_dev_init(void)
11253{
11254 int i, rc = -ENOMEM;
11255
11256 BUG_ON(!dev_boot_phase);
11257
Linus Torvalds1da177e2005-04-16 15:20:36 -070011258 if (dev_proc_init())
11259 goto out;
11260
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011261 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262 goto out;
11263
11264 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080011265 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011266 INIT_LIST_HEAD(&ptype_base[i]);
11267
Vlad Yasevich62532da2012-11-15 08:49:10 +000011268 INIT_LIST_HEAD(&offload_base);
11269
Eric W. Biederman881d9662007-09-17 11:56:21 -070011270 if (register_pernet_subsys(&netdev_net_ops))
11271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011272
11273 /*
11274 * Initialise the packet receive queues.
11275 */
11276
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070011277 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070011278 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011279 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011280
Eric Dumazet41852492016-08-26 12:50:39 -070011281 INIT_WORK(flush, flush_backlog);
11282
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011283 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070011284 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010011285#ifdef CONFIG_XFRM_OFFLOAD
11286 skb_queue_head_init(&sd->xfrm_backlog);
11287#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011288 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000011289 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000011290#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011291 sd->csd.func = rps_trigger_softirq;
11292 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011293 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070011294#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000011295
David S. Miller7c4ec742018-07-20 23:37:55 -070011296 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011297 sd->backlog.poll = process_backlog;
11298 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011299 }
11300
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301 dev_boot_phase = 0;
11302
Eric W. Biederman505d4f72008-11-07 22:54:20 -080011303 /* The loopback device is special if any other network devices
11304 * is present in a network namespace the loopback device must
11305 * be present. Since we now dynamically allocate and free the
11306 * loopback device ensure this invariant is maintained by
11307 * keeping the loopback device as the first device on the
11308 * list of network devices. Ensuring the loopback devices
11309 * is the first device that appears and the last network device
11310 * that disappears.
11311 */
11312 if (register_pernet_device(&loopback_net_ops))
11313 goto out;
11314
11315 if (register_pernet_device(&default_device_ops))
11316 goto out;
11317
Carlos R. Mafra962cf362008-05-15 11:15:37 -030011318 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11319 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011320
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011321 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11322 NULL, dev_cpu_dead);
11323 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324 rc = 0;
11325out:
11326 return rc;
11327}
11328
11329subsys_initcall(net_dev_init);