blob: adde93cbca9f0b1934f45383d23cc61f899e0dd1 [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 Poplawski723e98b72007-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 Chen93b3cff92008-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 Chen93b3cff92008-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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001074 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001076 rcu_read_lock();
1077 for_each_netdev_rcu(net, dev)
1078 if (dev->type == type) {
1079 dev_hold(dev);
1080 ret = dev;
1081 break;
1082 }
1083 rcu_read_unlock();
1084 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086EXPORT_SYMBOL(dev_getfirstbyhwtype);
1087
1088/**
WANG Cong6c555492014-09-11 15:35:09 -07001089 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001090 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * @if_flags: IFF_* values
1092 * @mask: bitmask of bits in if_flags to check
1093 *
1094 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001095 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001096 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
1098
WANG Cong6c555492014-09-11 15:35:09 -07001099struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1100 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001102 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
WANG Cong6c555492014-09-11 15:35:09 -07001104 ASSERT_RTNL();
1105
Pavel Emelianov7562f872007-05-03 15:13:45 -07001106 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001107 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001109 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111 }
1112 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
WANG Cong6c555492014-09-11 15:35:09 -07001115EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/**
1118 * dev_valid_name - check if name is okay for network device
1119 * @name: name string
1120 *
1121 * Network device names need to be valid file names to
Randy Dunlap4250b752020-09-17 21:35:15 -07001122 * allow sysfs to work. We also disallow any kind of
David S. Millerc7fa9d12006-08-15 16:34:13 -07001123 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
David S. Miller95f050b2012-03-06 16:12:15 -05001125bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001127 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001128 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001129 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001130 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001131 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001132 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001133
1134 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001135 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001136 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001137 name++;
1138 }
David S. Miller95f050b2012-03-06 16:12:15 -05001139 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001141EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001144 * __dev_alloc_name - allocate a name for a device
1145 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001147 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
1149 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001150 * id. It scans list of devices to build up a free map, then chooses
1151 * the first empty slot. The caller must hold the dev_base or rtnl lock
1152 * while allocating the name and adding the device in order to avoid
1153 * duplicates.
1154 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1155 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
1157
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001158static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 const char *p;
1162 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001163 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct net_device *d;
1165
Rasmus Villemoes93809102017-11-13 00:15:08 +01001166 if (!dev_valid_name(name))
1167 return -EINVAL;
1168
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001169 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (p) {
1171 /*
1172 * Verify the string as this thing may have come from
1173 * the user. There must be either one "%d" and no other "%"
1174 * characters.
1175 */
1176 if (p[1] != 'd' || strchr(p + 2, '%'))
1177 return -EINVAL;
1178
1179 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001180 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!inuse)
1182 return -ENOMEM;
1183
Eric W. Biederman881d9662007-09-17 11:56:21 -07001184 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!sscanf(d->name, name, &i))
1186 continue;
1187 if (i < 0 || i >= max_netdevices)
1188 continue;
1189
1190 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001191 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!strncmp(buf, d->name, IFNAMSIZ))
1193 set_bit(i, inuse);
1194 }
1195
1196 i = find_first_zero_bit(inuse, max_netdevices);
1197 free_page((unsigned long) inuse);
1198 }
1199
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001200 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001201 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* It is possible to run out of possible slots
1205 * when the name is long and there isn't enough space left
1206 * for the digits, or if all bits are used.
1207 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001208 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001211static int dev_alloc_name_ns(struct net *net,
1212 struct net_device *dev,
1213 const char *name)
1214{
1215 char buf[IFNAMSIZ];
1216 int ret;
1217
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001218 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001219 ret = __dev_alloc_name(net, name, buf);
1220 if (ret >= 0)
1221 strlcpy(dev->name, buf, IFNAMSIZ);
1222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001225/**
1226 * dev_alloc_name - allocate a name for a device
1227 * @dev: device
1228 * @name: name format string
1229 *
1230 * Passed a format string - eg "lt%d" it will try and find a suitable
1231 * id. It scans list of devices to build up a free map, then chooses
1232 * the first empty slot. The caller must hold the dev_base or rtnl lock
1233 * while allocating the name and adding the device in order to avoid
1234 * duplicates.
1235 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1236 * Returns the number of the unit assigned or a negative errno code.
1237 */
1238
1239int dev_alloc_name(struct net_device *dev, const char *name)
1240{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001241 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001242}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001243EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001244
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001245static int dev_get_valid_name(struct net *net, struct net_device *dev,
1246 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001247{
David S. Miller55a5ec92018-01-02 11:45:07 -05001248 BUG_ON(!net);
1249
1250 if (!dev_valid_name(name))
1251 return -EINVAL;
1252
1253 if (strchr(name, '%'))
1254 return dev_alloc_name_ns(net, dev, name);
1255 else if (__dev_get_by_name(net, name))
1256 return -EEXIST;
1257 else if (dev->name != name)
1258 strlcpy(dev->name, name, IFNAMSIZ);
1259
1260 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263/**
1264 * dev_change_name - change name of a device
1265 * @dev: device
1266 * @newname: name (or format string) must be at least IFNAMSIZ
1267 *
1268 * Change name of a device, can pass format strings "eth%d".
1269 * for wildcarding.
1270 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001271int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Tom Gundersen238fa362014-07-14 16:37:23 +02001273 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001274 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001276 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001277 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001280 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001282 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001283
1284 /* Some auto-enslaved devices e.g. failover slaves are
1285 * special, as userspace might rename the device after
1286 * the interface had been brought up and running since
1287 * the point kernel initiated auto-enslavement. Allow
1288 * live name change even when these slave devices are
1289 * up and running.
1290 *
1291 * Typically, users of these auto-enslaving devices
1292 * don't actually care about slave name change, as
1293 * they are supposed to operate on master interface
1294 * directly.
1295 */
1296 if (dev->flags & IFF_UP &&
1297 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EBUSY;
1299
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001300 down_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001301
1302 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001303 up_write(&devnet_rename_sem);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001304 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001305 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001306
Herbert Xufcc5a032007-07-30 17:03:38 -07001307 memcpy(oldname, dev->name, IFNAMSIZ);
1308
Gao feng828de4f2012-09-13 20:58:27 +00001309 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001310 if (err < 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001311 up_write(&devnet_rename_sem);
Octavian Purdilad9031022009-11-18 02:36:59 +00001312 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001315 if (oldname[0] && !strchr(oldname, '%'))
1316 netdev_info(dev, "renamed from %s\n", oldname);
1317
Tom Gundersen238fa362014-07-14 16:37:23 +02001318 old_assign_type = dev->name_assign_type;
1319 dev->name_assign_type = NET_NAME_RENAMED;
1320
Herbert Xufcc5a032007-07-30 17:03:38 -07001321rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001322 ret = device_rename(&dev->dev, dev->name);
1323 if (ret) {
1324 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001325 dev->name_assign_type = old_assign_type;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001326 up_write(&devnet_rename_sem);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001327 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001328 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001329
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001330 up_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001331
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001332 netdev_adjacent_rename_links(dev, oldname);
1333
Herbert Xu7f988ea2007-07-30 16:35:46 -07001334 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001335 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001336 write_unlock_bh(&dev_base_lock);
1337
1338 synchronize_rcu();
1339
1340 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001341 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001342 write_unlock_bh(&dev_base_lock);
1343
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001344 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001345 ret = notifier_to_errno(ret);
1346
1347 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001348 /* err >= 0 after dev_alloc_name() or stores the first errno */
1349 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001350 err = ret;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001351 down_write(&devnet_rename_sem);
Herbert Xufcc5a032007-07-30 17:03:38 -07001352 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001353 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001354 dev->name_assign_type = old_assign_type;
1355 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001356 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001357 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001358 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001359 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001360 }
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 return err;
1364}
1365
1366/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001367 * dev_set_alias - change ifalias of a device
1368 * @dev: device
1369 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001370 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001371 *
1372 * Set ifalias for a device,
1373 */
1374int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1375{
Florian Westphal6c557002017-10-02 23:50:05 +02001376 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001377
1378 if (len >= IFALIASZ)
1379 return -EINVAL;
1380
Florian Westphal6c557002017-10-02 23:50:05 +02001381 if (len) {
1382 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1383 if (!new_alias)
1384 return -ENOMEM;
1385
1386 memcpy(new_alias->ifalias, alias, len);
1387 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001388 }
1389
Florian Westphal6c557002017-10-02 23:50:05 +02001390 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001391 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1392 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001393 mutex_unlock(&ifalias_mutex);
1394
1395 if (new_alias)
1396 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001397
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001398 return len;
1399}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001400EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001401
Florian Westphal6c557002017-10-02 23:50:05 +02001402/**
1403 * dev_get_alias - get ifalias of a device
1404 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001405 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001406 * @len: size of buffer
1407 *
1408 * get ifalias for a device. Caller must make sure dev cannot go
1409 * away, e.g. rcu read lock or own a reference count to device.
1410 */
1411int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1412{
1413 const struct dev_ifalias *alias;
1414 int ret = 0;
1415
1416 rcu_read_lock();
1417 alias = rcu_dereference(dev->ifalias);
1418 if (alias)
1419 ret = snprintf(name, len, "%s", alias->ifalias);
1420 rcu_read_unlock();
1421
1422 return ret;
1423}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001424
1425/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001426 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001427 * @dev: device to cause notification
1428 *
1429 * Called to indicate a device has changed features.
1430 */
1431void netdev_features_change(struct net_device *dev)
1432{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001433 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001434}
1435EXPORT_SYMBOL(netdev_features_change);
1436
1437/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * netdev_state_change - device changes state
1439 * @dev: device to cause notification
1440 *
1441 * Called to indicate a device has changed state. This function calls
1442 * the notifier chains for netdev_chain and sends a NEWLINK message
1443 * to the routing socket.
1444 */
1445void netdev_state_change(struct net_device *dev)
1446{
1447 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001448 struct netdev_notifier_change_info change_info = {
1449 .info.dev = dev,
1450 };
Loic Prylli54951192014-07-01 21:39:43 -07001451
David Ahern51d0c0472017-10-04 17:48:45 -07001452 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001453 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001454 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001457EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Amerigo Wangee89bab2012-08-09 22:14:56 +00001459/**
Lijun Pan7061eb82020-12-14 15:19:28 -06001460 * __netdev_notify_peers - notify network peers about existence of @dev,
1461 * to be called when rtnl lock is already held.
1462 * @dev: network device
1463 *
1464 * Generate traffic such that interested network peers are aware of
1465 * @dev, such as by generating a gratuitous ARP. This may be used when
1466 * a device wants to inform the rest of the network about some sort of
1467 * reconfiguration such as a failover event or virtual machine
1468 * migration.
1469 */
1470void __netdev_notify_peers(struct net_device *dev)
1471{
1472 ASSERT_RTNL();
1473 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1474 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1475}
1476EXPORT_SYMBOL(__netdev_notify_peers);
1477
1478/**
tcharding722c9a02017-02-09 17:56:04 +11001479 * netdev_notify_peers - notify network peers about existence of @dev
1480 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001481 *
1482 * Generate traffic such that interested network peers are aware of
1483 * @dev, such as by generating a gratuitous ARP. This may be used when
1484 * a device wants to inform the rest of the network about some sort of
1485 * reconfiguration such as a failover event or virtual machine
1486 * migration.
1487 */
1488void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001489{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001490 rtnl_lock();
Lijun Pan7061eb82020-12-14 15:19:28 -06001491 __netdev_notify_peers(dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001492 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001493}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001494EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001495
Petr Machata40c900a2018-12-06 17:05:47 +00001496static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001498 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001501 ASSERT_RTNL();
1502
Heiner Kallweitbd869242020-06-20 22:35:42 +02001503 if (!netif_device_present(dev)) {
1504 /* may be detached because parent is runtime-suspended */
1505 if (dev->dev.parent)
1506 pm_runtime_resume(dev->dev.parent);
1507 if (!netif_device_present(dev))
1508 return -ENODEV;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Neil Hormanca99ca12013-02-05 08:05:43 +00001511 /* Block netpoll from trying to do any rx path servicing.
1512 * If we don't do this there is a chance ndo_poll_controller
1513 * or ndo_poll may be running while we open the device
1514 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001515 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001516
Petr Machata40c900a2018-12-06 17:05:47 +00001517 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001518 ret = notifier_to_errno(ret);
1519 if (ret)
1520 return ret;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001523
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001524 if (ops->ndo_validate_addr)
1525 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001526
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001527 if (!ret && ops->ndo_open)
1528 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric W. Biederman66b55522014-03-27 15:39:03 -07001530 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001531
Jeff Garzikbada3392007-10-23 20:19:37 -07001532 if (ret)
1533 clear_bit(__LINK_STATE_START, &dev->state);
1534 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001536 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001538 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return ret;
1542}
Patrick McHardybd380812010-02-26 06:34:53 +00001543
1544/**
1545 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001546 * @dev: device to open
1547 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001548 *
1549 * Takes a device from down to up state. The device's private open
1550 * function is invoked and then the multicast lists are loaded. Finally
1551 * the device is moved into the up state and a %NETDEV_UP message is
1552 * sent to the netdev notifier chain.
1553 *
1554 * Calling this function on an active interface is a nop. On a failure
1555 * a negative errno code is returned.
1556 */
Petr Machata00f54e62018-12-06 17:05:36 +00001557int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001558{
1559 int ret;
1560
Patrick McHardybd380812010-02-26 06:34:53 +00001561 if (dev->flags & IFF_UP)
1562 return 0;
1563
Petr Machata40c900a2018-12-06 17:05:47 +00001564 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001565 if (ret < 0)
1566 return ret;
1567
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001568 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001569 call_netdevice_notifiers(NETDEV_UP, dev);
1570
1571 return ret;
1572}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001573EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
stephen hemminger7051b882017-07-18 15:59:27 -07001575static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Octavian Purdila44345722010-12-13 12:44:07 +00001577 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001578
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001579 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001580 might_sleep();
1581
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001582 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001583 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001584 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001585
Octavian Purdila44345722010-12-13 12:44:07 +00001586 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Octavian Purdila44345722010-12-13 12:44:07 +00001588 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Octavian Purdila44345722010-12-13 12:44:07 +00001590 /* Synchronize to scheduled poll. We cannot touch poll list, it
1591 * can be even on different cpu. So just clear netif_running().
1592 *
1593 * dev->stop() will invoke napi_disable() on all of it's
1594 * napi_struct instances on this device.
1595 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001596 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Octavian Purdila44345722010-12-13 12:44:07 +00001599 dev_deactivate_many(head);
1600
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001601 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001602 const struct net_device_ops *ops = dev->netdev_ops;
1603
1604 /*
1605 * Call the device specific close. This cannot fail.
1606 * Only if device is UP
1607 *
1608 * We allow it to be called even after a DETACH hot-plug
1609 * event.
1610 */
1611 if (ops->ndo_stop)
1612 ops->ndo_stop(dev);
1613
Octavian Purdila44345722010-12-13 12:44:07 +00001614 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001615 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001616 }
Octavian Purdila44345722010-12-13 12:44:07 +00001617}
1618
stephen hemminger7051b882017-07-18 15:59:27 -07001619static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001620{
1621 LIST_HEAD(single);
1622
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001623 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001624 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001625 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001626}
1627
stephen hemminger7051b882017-07-18 15:59:27 -07001628void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001629{
1630 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001631
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001632 /* Remove the devices that don't need to be closed */
1633 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001634 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001635 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001636
1637 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001638
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001639 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001640 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001641 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001642 if (unlink)
1643 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
David S. Miller99c4a262015-03-18 22:52:33 -04001646EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001647
1648/**
1649 * dev_close - shutdown an interface.
1650 * @dev: device to shutdown
1651 *
1652 * This function moves an active device into down state. A
1653 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1654 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1655 * chain.
1656 */
stephen hemminger7051b882017-07-18 15:59:27 -07001657void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001658{
Eric Dumazete14a5992011-05-10 12:26:06 -07001659 if (dev->flags & IFF_UP) {
1660 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001661
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001662 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001663 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001664 list_del(&single);
1665 }
Patrick McHardybd380812010-02-26 06:34:53 +00001666}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001667EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001670/**
1671 * dev_disable_lro - disable Large Receive Offload on a device
1672 * @dev: device
1673 *
1674 * Disable Large Receive Offload (LRO) on a net device. Must be
1675 * called under RTNL. This is needed if received packets may be
1676 * forwarded to another interface.
1677 */
1678void dev_disable_lro(struct net_device *dev)
1679{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001680 struct net_device *lower_dev;
1681 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001682
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001683 dev->wanted_features &= ~NETIF_F_LRO;
1684 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001685
Michał Mirosław22d59692011-04-21 12:42:15 +00001686 if (unlikely(dev->features & NETIF_F_LRO))
1687 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001688
1689 netdev_for_each_lower_dev(dev, lower_dev, iter)
1690 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001691}
1692EXPORT_SYMBOL(dev_disable_lro);
1693
Michael Chan56f5aa72017-12-16 03:09:41 -05001694/**
1695 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1696 * @dev: device
1697 *
1698 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1699 * called under RTNL. This is needed if Generic XDP is installed on
1700 * the device.
1701 */
1702static void dev_disable_gro_hw(struct net_device *dev)
1703{
1704 dev->wanted_features &= ~NETIF_F_GRO_HW;
1705 netdev_update_features(dev);
1706
1707 if (unlikely(dev->features & NETIF_F_GRO_HW))
1708 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1709}
1710
Kirill Tkhaiede27622018-03-23 19:47:19 +03001711const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1712{
1713#define N(val) \
1714 case NETDEV_##val: \
1715 return "NETDEV_" __stringify(val);
1716 switch (cmd) {
1717 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1718 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1719 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1720 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1721 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1722 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1723 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001724 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1725 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001726 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001727 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001728#undef N
1729 return "UNKNOWN_NETDEV_EVENT";
1730}
1731EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1732
Jiri Pirko351638e2013-05-28 01:30:21 +00001733static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1734 struct net_device *dev)
1735{
David Ahern51d0c0472017-10-04 17:48:45 -07001736 struct netdev_notifier_info info = {
1737 .dev = dev,
1738 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001739
Jiri Pirko351638e2013-05-28 01:30:21 +00001740 return nb->notifier_call(nb, val, &info);
1741}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001742
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001743static int call_netdevice_register_notifiers(struct notifier_block *nb,
1744 struct net_device *dev)
1745{
1746 int err;
1747
1748 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1749 err = notifier_to_errno(err);
1750 if (err)
1751 return err;
1752
1753 if (!(dev->flags & IFF_UP))
1754 return 0;
1755
1756 call_netdevice_notifier(nb, NETDEV_UP, dev);
1757 return 0;
1758}
1759
1760static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1761 struct net_device *dev)
1762{
1763 if (dev->flags & IFF_UP) {
1764 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1765 dev);
1766 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1767 }
1768 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1769}
1770
1771static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1772 struct net *net)
1773{
1774 struct net_device *dev;
1775 int err;
1776
1777 for_each_netdev(net, dev) {
1778 err = call_netdevice_register_notifiers(nb, dev);
1779 if (err)
1780 goto rollback;
1781 }
1782 return 0;
1783
1784rollback:
1785 for_each_netdev_continue_reverse(net, dev)
1786 call_netdevice_unregister_notifiers(nb, dev);
1787 return err;
1788}
1789
1790static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1791 struct net *net)
1792{
1793 struct net_device *dev;
1794
1795 for_each_netdev(net, dev)
1796 call_netdevice_unregister_notifiers(nb, dev);
1797}
1798
Eric W. Biederman881d9662007-09-17 11:56:21 -07001799static int dev_boot_phase = 1;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/**
tcharding722c9a02017-02-09 17:56:04 +11001802 * register_netdevice_notifier - register a network notifier block
1803 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *
tcharding722c9a02017-02-09 17:56:04 +11001805 * Register a notifier to be called when network device events occur.
1806 * The notifier passed is linked into the kernel structures and must
1807 * not be reused until it has been unregistered. A negative errno code
1808 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 *
tcharding722c9a02017-02-09 17:56:04 +11001810 * When registered all registration and up events are replayed
1811 * to the new notifier to allow device to have a race free
1812 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
1814
1815int register_netdevice_notifier(struct notifier_block *nb)
1816{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001817 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int err;
1819
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001820 /* Close race with setup_net() and cleanup_net() */
1821 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001823 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001824 if (err)
1825 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001826 if (dev_boot_phase)
1827 goto unlock;
1828 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001829 err = call_netdevice_register_net_notifiers(nb, net);
1830 if (err)
1831 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001833
1834unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001836 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001838
1839rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001840 for_each_net_continue_reverse(net)
1841 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001842
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001843 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001844 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001846EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848/**
tcharding722c9a02017-02-09 17:56:04 +11001849 * unregister_netdevice_notifier - unregister a network notifier block
1850 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *
tcharding722c9a02017-02-09 17:56:04 +11001852 * Unregister a notifier previously registered by
1853 * register_netdevice_notifier(). The notifier is unlinked into the
1854 * kernel structures and may then be reused. A negative errno code
1855 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001856 *
tcharding722c9a02017-02-09 17:56:04 +11001857 * After unregistering unregister and down device events are synthesized
1858 * for all devices on the device list to the removed notifier to remove
1859 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
1861
1862int unregister_netdevice_notifier(struct notifier_block *nb)
1863{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001864 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001865 int err;
1866
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001867 /* Close race with setup_net() and cleanup_net() */
1868 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001869 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001870 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001871 if (err)
1872 goto unlock;
1873
Jiri Pirko48b3a132020-01-25 12:17:06 +01001874 for_each_net(net)
1875 call_netdevice_unregister_net_notifiers(nb, net);
1876
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001877unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001878 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001879 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001880 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001882EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Jiri Pirko1f637702020-01-25 12:17:07 +01001884static int __register_netdevice_notifier_net(struct net *net,
1885 struct notifier_block *nb,
1886 bool ignore_call_fail)
1887{
1888 int err;
1889
1890 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1891 if (err)
1892 return err;
1893 if (dev_boot_phase)
1894 return 0;
1895
1896 err = call_netdevice_register_net_notifiers(nb, net);
1897 if (err && !ignore_call_fail)
1898 goto chain_unregister;
1899
1900 return 0;
1901
1902chain_unregister:
1903 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1904 return err;
1905}
1906
1907static int __unregister_netdevice_notifier_net(struct net *net,
1908 struct notifier_block *nb)
1909{
1910 int err;
1911
1912 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1913 if (err)
1914 return err;
1915
1916 call_netdevice_unregister_net_notifiers(nb, net);
1917 return 0;
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001921 * register_netdevice_notifier_net - register a per-netns network notifier block
1922 * @net: network namespace
1923 * @nb: notifier
1924 *
1925 * Register a notifier to be called when network device events occur.
1926 * The notifier passed is linked into the kernel structures and must
1927 * not be reused until it has been unregistered. A negative errno code
1928 * is returned on a failure.
1929 *
1930 * When registered all registration and up events are replayed
1931 * to the new notifier to allow device to have a race free
1932 * view of the network device list.
1933 */
1934
1935int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1936{
1937 int err;
1938
1939 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001940 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001941 rtnl_unlock();
1942 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001943}
1944EXPORT_SYMBOL(register_netdevice_notifier_net);
1945
1946/**
1947 * unregister_netdevice_notifier_net - unregister a per-netns
1948 * network notifier block
1949 * @net: network namespace
1950 * @nb: notifier
1951 *
1952 * Unregister a notifier previously registered by
1953 * register_netdevice_notifier(). The notifier is unlinked into the
1954 * kernel structures and may then be reused. A negative errno code
1955 * is returned on a failure.
1956 *
1957 * After unregistering unregister and down device events are synthesized
1958 * for all devices on the device list to the removed notifier to remove
1959 * the need for special case cleanup code.
1960 */
1961
1962int unregister_netdevice_notifier_net(struct net *net,
1963 struct notifier_block *nb)
1964{
1965 int err;
1966
1967 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001968 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001969 rtnl_unlock();
1970 return err;
1971}
1972EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1973
Jiri Pirko93642e12020-01-25 12:17:08 +01001974int register_netdevice_notifier_dev_net(struct net_device *dev,
1975 struct notifier_block *nb,
1976 struct netdev_net_notifier *nn)
1977{
1978 int err;
1979
1980 rtnl_lock();
1981 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1982 if (!err) {
1983 nn->nb = nb;
1984 list_add(&nn->list, &dev->net_notifier_list);
1985 }
1986 rtnl_unlock();
1987 return err;
1988}
1989EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1990
1991int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1992 struct notifier_block *nb,
1993 struct netdev_net_notifier *nn)
1994{
1995 int err;
1996
1997 rtnl_lock();
1998 list_del(&nn->list);
1999 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
2000 rtnl_unlock();
2001 return err;
2002}
2003EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
2004
2005static void move_netdevice_notifiers_dev_net(struct net_device *dev,
2006 struct net *net)
2007{
2008 struct netdev_net_notifier *nn;
2009
2010 list_for_each_entry(nn, &dev->net_notifier_list, list) {
2011 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
2012 __register_netdevice_notifier_net(net, nn->nb, true);
2013 }
2014}
2015
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002016/**
Jiri Pirko351638e2013-05-28 01:30:21 +00002017 * call_netdevice_notifiers_info - call all network notifier blocks
2018 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00002019 * @info: notifier information data
2020 *
2021 * Call all network notifier blocks. Parameters and return value
2022 * are as for raw_notifier_call_chain().
2023 */
2024
stephen hemminger1d143d92013-12-29 14:01:29 -08002025static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08002026 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00002027{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002028 struct net *net = dev_net(info->dev);
2029 int ret;
2030
Jiri Pirko351638e2013-05-28 01:30:21 +00002031 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002032
2033 /* Run per-netns notifier block chain first, then run the global one.
2034 * Hopefully, one day, the global one is going to be removed after
2035 * all notifier block registrators get converted to be per-netns.
2036 */
2037 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
2038 if (ret & NOTIFY_STOP_MASK)
2039 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00002040 return raw_notifier_call_chain(&netdev_chain, val, info);
2041}
Jiri Pirko351638e2013-05-28 01:30:21 +00002042
Petr Machata26372602018-12-06 17:05:45 +00002043static int call_netdevice_notifiers_extack(unsigned long val,
2044 struct net_device *dev,
2045 struct netlink_ext_ack *extack)
2046{
2047 struct netdev_notifier_info info = {
2048 .dev = dev,
2049 .extack = extack,
2050 };
2051
2052 return call_netdevice_notifiers_info(val, &info);
2053}
2054
Jiri Pirko351638e2013-05-28 01:30:21 +00002055/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 * call_netdevice_notifiers - call all network notifier blocks
2057 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002058 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 *
2060 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07002061 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
2063
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07002064int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Petr Machata26372602018-12-06 17:05:45 +00002066 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
stephen hemmingeredf947f2011-03-24 13:24:01 +00002068EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02002070/**
2071 * call_netdevice_notifiers_mtu - call all network notifier blocks
2072 * @val: value passed unmodified to notifier function
2073 * @dev: net_device pointer passed unmodified to notifier function
2074 * @arg: additional u32 argument passed to the notifier function
2075 *
2076 * Call all network notifier blocks. Parameters and return value
2077 * are as for raw_notifier_call_chain().
2078 */
2079static int call_netdevice_notifiers_mtu(unsigned long val,
2080 struct net_device *dev, u32 arg)
2081{
2082 struct netdev_notifier_info_ext info = {
2083 .info.dev = dev,
2084 .ext.mtu = arg,
2085 };
2086
2087 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2088
2089 return call_netdevice_notifiers_info(val, &info.info);
2090}
2091
Pablo Neira1cf519002015-05-13 18:19:37 +02002092#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002093static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002094
2095void net_inc_ingress_queue(void)
2096{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002097 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002098}
2099EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2100
2101void net_dec_ingress_queue(void)
2102{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002103 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002104}
2105EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2106#endif
2107
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002108#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002109static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002110
2111void net_inc_egress_queue(void)
2112{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002113 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002114}
2115EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2116
2117void net_dec_egress_queue(void)
2118{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002119 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002120}
2121EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2122#endif
2123
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002124static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002125#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002126static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002127static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002128static void netstamp_clear(struct work_struct *work)
2129{
2130 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002131 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002132
Eric Dumazet13baa002017-03-01 14:28:39 -08002133 wanted = atomic_add_return(deferred, &netstamp_wanted);
2134 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002135 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002136 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002137 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002138}
2139static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142void net_enable_timestamp(void)
2143{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002144#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002145 int wanted;
2146
2147 while (1) {
2148 wanted = atomic_read(&netstamp_wanted);
2149 if (wanted <= 0)
2150 break;
2151 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2152 return;
2153 }
2154 atomic_inc(&netstamp_needed_deferred);
2155 schedule_work(&netstamp_work);
2156#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002157 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002160EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162void net_disable_timestamp(void)
2163{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002164#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002165 int wanted;
2166
2167 while (1) {
2168 wanted = atomic_read(&netstamp_wanted);
2169 if (wanted <= 1)
2170 break;
2171 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2172 return;
2173 }
2174 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002175 schedule_work(&netstamp_work);
2176#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002177 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002180EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Eric Dumazet3b098e22010-05-15 23:57:10 -07002182static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002184 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002185 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002186 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002189#define net_timestamp_check(COND, SKB) \
2190 if (static_branch_unlikely(&netstamp_needed_key)) { \
2191 if ((COND) && !(SKB)->tstamp) \
2192 __net_timestamp(SKB); \
2193 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002194
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002195bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002196{
2197 unsigned int len;
2198
2199 if (!(dev->flags & IFF_UP))
2200 return false;
2201
2202 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2203 if (skb->len <= len)
2204 return true;
2205
2206 /* if TSO is enabled, we don't care about the length as the packet
2207 * could be forwarded without being segmented before
2208 */
2209 if (skb_is_gso(skb))
2210 return true;
2211
2212 return false;
2213}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002214EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002215
Herbert Xua0265d22014-04-17 13:45:03 +08002216int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2217{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002218 int ret = ____dev_forward_skb(dev, skb);
2219
2220 if (likely(!ret)) {
2221 skb->protocol = eth_type_trans(skb, dev);
2222 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002223 }
2224
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002225 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002226}
2227EXPORT_SYMBOL_GPL(__dev_forward_skb);
2228
Arnd Bergmann44540962009-11-26 06:07:08 +00002229/**
2230 * dev_forward_skb - loopback an skb to another netif
2231 *
2232 * @dev: destination network device
2233 * @skb: buffer to forward
2234 *
2235 * return values:
2236 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002237 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002238 *
2239 * dev_forward_skb can be used for injecting an skb from the
2240 * start_xmit function of one device into the receive queue
2241 * of another device.
2242 *
2243 * The receiving device may be in another namespace, so
2244 * we have to clear all information in the skb that could
2245 * impact namespace isolation.
2246 */
2247int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2248{
Herbert Xua0265d22014-04-17 13:45:03 +08002249 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002250}
2251EXPORT_SYMBOL_GPL(dev_forward_skb);
2252
Changli Gao71d9dec2010-12-15 19:57:25 +00002253static inline int deliver_skb(struct sk_buff *skb,
2254 struct packet_type *pt_prev,
2255 struct net_device *orig_dev)
2256{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002257 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002258 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002259 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002260 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2261}
2262
Salam Noureddine7866a622015-01-27 11:35:48 -08002263static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2264 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002265 struct net_device *orig_dev,
2266 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002267 struct list_head *ptype_list)
2268{
2269 struct packet_type *ptype, *pt_prev = *pt;
2270
2271 list_for_each_entry_rcu(ptype, ptype_list, list) {
2272 if (ptype->type != type)
2273 continue;
2274 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002275 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002276 pt_prev = ptype;
2277 }
2278 *pt = pt_prev;
2279}
2280
Eric Leblondc0de08d2012-08-16 22:02:58 +00002281static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2282{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002283 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002284 return false;
2285
2286 if (ptype->id_match)
2287 return ptype->id_match(ptype, skb->sk);
2288 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2289 return true;
2290
2291 return false;
2292}
2293
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002294/**
2295 * dev_nit_active - return true if any network interface taps are in use
2296 *
2297 * @dev: network device to check for the presence of taps
2298 */
2299bool dev_nit_active(struct net_device *dev)
2300{
2301 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2302}
2303EXPORT_SYMBOL_GPL(dev_nit_active);
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/*
2306 * Support routine. Sends outgoing frames to any network
2307 * taps currently in use.
2308 */
2309
David Ahern74b20582016-05-10 11:19:50 -07002310void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002313 struct sk_buff *skb2 = NULL;
2314 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002315 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002318again:
2319 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002320 if (ptype->ignore_outgoing)
2321 continue;
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /* Never send packets back to the socket
2324 * they originated from - MvS (miquels@drinkel.ow.org)
2325 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002326 if (skb_loop_sk(ptype, skb))
2327 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002328
Salam Noureddine7866a622015-01-27 11:35:48 -08002329 if (pt_prev) {
2330 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002331 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002332 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002334
2335 /* need to clone skb, done only once */
2336 skb2 = skb_clone(skb, GFP_ATOMIC);
2337 if (!skb2)
2338 goto out_unlock;
2339
2340 net_timestamp_set(skb2);
2341
2342 /* skb->nh should be correctly
2343 * set by sender, so that the second statement is
2344 * just protection against buggy protocols.
2345 */
2346 skb_reset_mac_header(skb2);
2347
2348 if (skb_network_header(skb2) < skb2->data ||
2349 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2350 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2351 ntohs(skb2->protocol),
2352 dev->name);
2353 skb_reset_network_header(skb2);
2354 }
2355
2356 skb2->transport_header = skb2->network_header;
2357 skb2->pkt_type = PACKET_OUTGOING;
2358 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002360
2361 if (ptype_list == &ptype_all) {
2362 ptype_list = &dev->ptype_all;
2363 goto again;
2364 }
2365out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002366 if (pt_prev) {
2367 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2368 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2369 else
2370 kfree_skb(skb2);
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 rcu_read_unlock();
2373}
David Ahern74b20582016-05-10 11:19:50 -07002374EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Ben Hutchings2c530402012-07-10 10:55:09 +00002376/**
2377 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002378 * @dev: Network device
2379 * @txq: number of queues available
2380 *
2381 * If real_num_tx_queues is changed the tc mappings may no longer be
2382 * valid. To resolve this verify the tc mapping remains valid and if
2383 * not NULL the mapping. With no priorities mapping to this
2384 * offset/count pair it will no longer be used. In the worst case TC0
2385 * is invalid nothing can be done so disable priority mappings. If is
2386 * expected that drivers will fix this mapping if they can before
2387 * calling netif_set_real_num_tx_queues.
2388 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002389static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002390{
2391 int i;
2392 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2393
2394 /* If TC0 is invalidated disable TC mapping */
2395 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002396 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002397 dev->num_tc = 0;
2398 return;
2399 }
2400
2401 /* Invalidated prio to tc mappings set to TC0 */
2402 for (i = 1; i < TC_BITMASK + 1; i++) {
2403 int q = netdev_get_prio_tc_map(dev, i);
2404
2405 tc = &dev->tc_to_txq[q];
2406 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002407 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2408 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002409 netdev_set_prio_tc_map(dev, i, 0);
2410 }
2411 }
2412}
2413
Alexander Duyck8d059b02016-10-28 11:43:49 -04002414int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2415{
2416 if (dev->num_tc) {
2417 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2418 int i;
2419
Alexander Duyckffcfe252018-07-09 12:19:38 -04002420 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002421 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2422 if ((txq - tc->offset) < tc->count)
2423 return i;
2424 }
2425
Alexander Duyckffcfe252018-07-09 12:19:38 -04002426 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002427 return -1;
2428 }
2429
2430 return 0;
2431}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002432EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002433
Alexander Duyck537c00d2013-01-10 08:57:02 +00002434#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002435struct static_key xps_needed __read_mostly;
2436EXPORT_SYMBOL(xps_needed);
2437struct static_key xps_rxqs_needed __read_mostly;
2438EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002439static DEFINE_MUTEX(xps_map_mutex);
2440#define xmap_dereference(P) \
2441 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2442
Alexander Duyck6234f872016-10-28 11:46:49 -04002443static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2444 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002445{
2446 struct xps_map *map = NULL;
2447 int pos;
2448
2449 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002450 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002451 if (!map)
2452 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002453
Alexander Duyck6234f872016-10-28 11:46:49 -04002454 for (pos = map->len; pos--;) {
2455 if (map->queues[pos] != index)
2456 continue;
2457
2458 if (map->len > 1) {
2459 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002460 break;
2461 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002462
Amritha Nambiar80d19662018-06-29 21:26:41 -07002463 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002464 kfree_rcu(map, rcu);
2465 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002466 }
2467
Alexander Duyck6234f872016-10-28 11:46:49 -04002468 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002469}
2470
Alexander Duyck6234f872016-10-28 11:46:49 -04002471static bool remove_xps_queue_cpu(struct net_device *dev,
2472 struct xps_dev_maps *dev_maps,
2473 int cpu, u16 offset, u16 count)
2474{
Alexander Duyck184c4492016-10-28 11:50:13 -04002475 int num_tc = dev->num_tc ? : 1;
2476 bool active = false;
2477 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002478
Alexander Duyck184c4492016-10-28 11:50:13 -04002479 for (tci = cpu * num_tc; num_tc--; tci++) {
2480 int i, j;
2481
2482 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002483 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002484 break;
2485 }
2486
2487 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002488 }
2489
Alexander Duyck184c4492016-10-28 11:50:13 -04002490 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002491}
2492
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002493static void reset_xps_maps(struct net_device *dev,
2494 struct xps_dev_maps *dev_maps,
2495 bool is_rxqs_map)
2496{
2497 if (is_rxqs_map) {
2498 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2499 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2500 } else {
2501 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2502 }
2503 static_key_slow_dec_cpuslocked(&xps_needed);
2504 kfree_rcu(dev_maps, rcu);
2505}
2506
Amritha Nambiar80d19662018-06-29 21:26:41 -07002507static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2508 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2509 u16 offset, u16 count, bool is_rxqs_map)
2510{
2511 bool active = false;
2512 int i, j;
2513
2514 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2515 j < nr_ids;)
2516 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2517 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002518 if (!active)
2519 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002520
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002521 if (!is_rxqs_map) {
2522 for (i = offset + (count - 1); count--; i--) {
2523 netdev_queue_numa_node_write(
2524 netdev_get_tx_queue(dev, i),
2525 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002526 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002527 }
2528}
2529
Alexander Duyck6234f872016-10-28 11:46:49 -04002530static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2531 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002532{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002533 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002534 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002535 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002536
Amritha Nambiar04157462018-06-29 21:26:46 -07002537 if (!static_key_false(&xps_needed))
2538 return;
2539
Andrei Vagin4d99f662018-08-08 20:07:35 -07002540 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002541 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002542
Amritha Nambiar04157462018-06-29 21:26:46 -07002543 if (static_key_false(&xps_rxqs_needed)) {
2544 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2545 if (dev_maps) {
2546 nr_ids = dev->num_rx_queues;
2547 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2548 offset, count, true);
2549 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002550 }
2551
2552 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002553 if (!dev_maps)
2554 goto out_no_maps;
2555
Amritha Nambiar80d19662018-06-29 21:26:41 -07002556 if (num_possible_cpus() > 1)
2557 possible_mask = cpumask_bits(cpu_possible_mask);
2558 nr_ids = nr_cpu_ids;
2559 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2560 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002561
Alexander Duyck537c00d2013-01-10 08:57:02 +00002562out_no_maps:
2563 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002564 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002565}
2566
Alexander Duyck6234f872016-10-28 11:46:49 -04002567static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2568{
2569 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2570}
2571
Amritha Nambiar80d19662018-06-29 21:26:41 -07002572static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2573 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002574{
2575 struct xps_map *new_map;
2576 int alloc_len = XPS_MIN_MAP_ALLOC;
2577 int i, pos;
2578
2579 for (pos = 0; map && pos < map->len; pos++) {
2580 if (map->queues[pos] != index)
2581 continue;
2582 return map;
2583 }
2584
Amritha Nambiar80d19662018-06-29 21:26:41 -07002585 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002586 if (map) {
2587 if (pos < map->alloc_len)
2588 return map;
2589
2590 alloc_len = map->alloc_len * 2;
2591 }
2592
Amritha Nambiar80d19662018-06-29 21:26:41 -07002593 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2594 * map
2595 */
2596 if (is_rxqs_map)
2597 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2598 else
2599 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2600 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002601 if (!new_map)
2602 return NULL;
2603
2604 for (i = 0; i < pos; i++)
2605 new_map->queues[i] = map->queues[i];
2606 new_map->alloc_len = alloc_len;
2607 new_map->len = pos;
2608
2609 return new_map;
2610}
2611
Andrei Vagin4d99f662018-08-08 20:07:35 -07002612/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002613int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2614 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002615{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002616 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002617 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002618 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002619 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002620 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002621 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002622 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002623
Alexander Duyck184c4492016-10-28 11:50:13 -04002624 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002625 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002626 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002627 if (num_tc < 0)
2628 return -EINVAL;
2629
2630 /* If queue belongs to subordinate dev use its map */
2631 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2632
Alexander Duyck184c4492016-10-28 11:50:13 -04002633 tc = netdev_txq_to_tc(dev, index);
2634 if (tc < 0)
2635 return -EINVAL;
2636 }
2637
Amritha Nambiar80d19662018-06-29 21:26:41 -07002638 mutex_lock(&xps_map_mutex);
2639 if (is_rxqs_map) {
2640 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2641 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2642 nr_ids = dev->num_rx_queues;
2643 } else {
2644 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2645 if (num_possible_cpus() > 1) {
2646 online_mask = cpumask_bits(cpu_online_mask);
2647 possible_mask = cpumask_bits(cpu_possible_mask);
2648 }
2649 dev_maps = xmap_dereference(dev->xps_cpus_map);
2650 nr_ids = nr_cpu_ids;
2651 }
2652
Alexander Duyck184c4492016-10-28 11:50:13 -04002653 if (maps_sz < L1_CACHE_BYTES)
2654 maps_sz = L1_CACHE_BYTES;
2655
Alexander Duyck01c5f862013-01-10 08:57:35 +00002656 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002657 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2658 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002659 if (!new_dev_maps)
2660 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002661 if (!new_dev_maps) {
2662 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002663 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002664 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002665
Amritha Nambiar80d19662018-06-29 21:26:41 -07002666 tci = j * num_tc + tc;
2667 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002668 NULL;
2669
Amritha Nambiar80d19662018-06-29 21:26:41 -07002670 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002671 if (!map)
2672 goto error;
2673
Amritha Nambiar80d19662018-06-29 21:26:41 -07002674 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002675 }
2676
2677 if (!new_dev_maps)
2678 goto out_no_new_maps;
2679
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002680 if (!dev_maps) {
2681 /* Increment static keys at most once per type */
2682 static_key_slow_inc_cpuslocked(&xps_needed);
2683 if (is_rxqs_map)
2684 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2685 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002686
Amritha Nambiar80d19662018-06-29 21:26:41 -07002687 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2688 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002689 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002690 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002691 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002692 map = xmap_dereference(dev_maps->attr_map[tci]);
2693 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002694 }
2695
2696 /* We need to explicitly update tci as prevous loop
2697 * could break out early if dev_maps is NULL.
2698 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002699 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002700
Amritha Nambiar80d19662018-06-29 21:26:41 -07002701 if (netif_attr_test_mask(j, mask, nr_ids) &&
2702 netif_attr_test_online(j, online_mask, nr_ids)) {
2703 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002704 int pos = 0;
2705
Amritha Nambiar80d19662018-06-29 21:26:41 -07002706 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002707 while ((pos < map->len) && (map->queues[pos] != index))
2708 pos++;
2709
2710 if (pos == map->len)
2711 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002712#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002713 if (!is_rxqs_map) {
2714 if (numa_node_id == -2)
2715 numa_node_id = cpu_to_node(j);
2716 else if (numa_node_id != cpu_to_node(j))
2717 numa_node_id = -1;
2718 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002719#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002720 } else if (dev_maps) {
2721 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002722 map = xmap_dereference(dev_maps->attr_map[tci]);
2723 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002724 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002725
Alexander Duyck184c4492016-10-28 11:50:13 -04002726 /* copy maps belonging to foreign traffic classes */
2727 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
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 Duyck184c4492016-10-28 11:50:13 -04002731 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002732 }
2733
Amritha Nambiar80d19662018-06-29 21:26:41 -07002734 if (is_rxqs_map)
2735 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2736 else
2737 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002738
Alexander Duyck537c00d2013-01-10 08:57:02 +00002739 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002740 if (!dev_maps)
2741 goto out_no_old_maps;
2742
Amritha Nambiar80d19662018-06-29 21:26:41 -07002743 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2744 j < nr_ids;) {
2745 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2746 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2747 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002748 if (map && map != new_map)
2749 kfree_rcu(map, rcu);
2750 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002751 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002752
Alexander Duyck184c4492016-10-28 11:50:13 -04002753 kfree_rcu(dev_maps, rcu);
2754
2755out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002756 dev_maps = new_dev_maps;
2757 active = true;
2758
2759out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002760 if (!is_rxqs_map) {
2761 /* update Tx queue numa node */
2762 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2763 (numa_node_id >= 0) ?
2764 numa_node_id : NUMA_NO_NODE);
2765 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002766
Alexander Duyck01c5f862013-01-10 08:57:35 +00002767 if (!dev_maps)
2768 goto out_no_maps;
2769
Amritha Nambiar80d19662018-06-29 21:26:41 -07002770 /* removes tx-queue from unused CPUs/rx-queues */
2771 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2772 j < nr_ids;) {
2773 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002774 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002775 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2776 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002777 active |= remove_xps_queue(dev_maps, tci, index);
2778 for (i = num_tc - tc, tci++; --i; tci++)
2779 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002780 }
2781
2782 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002783 if (!active)
2784 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002785
2786out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002787 mutex_unlock(&xps_map_mutex);
2788
2789 return 0;
2790error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002791 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002792 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2793 j < nr_ids;) {
2794 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2795 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002796 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002797 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002798 NULL;
2799 if (new_map && new_map != map)
2800 kfree(new_map);
2801 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002802 }
2803
Alexander Duyck537c00d2013-01-10 08:57:02 +00002804 mutex_unlock(&xps_map_mutex);
2805
Alexander Duyck537c00d2013-01-10 08:57:02 +00002806 kfree(new_dev_maps);
2807 return -ENOMEM;
2808}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002809EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002810
2811int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2812 u16 index)
2813{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002814 int ret;
2815
2816 cpus_read_lock();
2817 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2818 cpus_read_unlock();
2819
2820 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002821}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002822EXPORT_SYMBOL(netif_set_xps_queue);
2823
2824#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002825static void netdev_unbind_all_sb_channels(struct net_device *dev)
2826{
2827 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2828
2829 /* Unbind any subordinate channels */
2830 while (txq-- != &dev->_tx[0]) {
2831 if (txq->sb_dev)
2832 netdev_unbind_sb_channel(dev, txq->sb_dev);
2833 }
2834}
2835
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002836void netdev_reset_tc(struct net_device *dev)
2837{
Alexander Duyck6234f872016-10-28 11:46:49 -04002838#ifdef CONFIG_XPS
2839 netif_reset_xps_queues_gt(dev, 0);
2840#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002841 netdev_unbind_all_sb_channels(dev);
2842
2843 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002844 dev->num_tc = 0;
2845 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2846 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2847}
2848EXPORT_SYMBOL(netdev_reset_tc);
2849
2850int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2851{
2852 if (tc >= dev->num_tc)
2853 return -EINVAL;
2854
Alexander Duyck6234f872016-10-28 11:46:49 -04002855#ifdef CONFIG_XPS
2856 netif_reset_xps_queues(dev, offset, count);
2857#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002858 dev->tc_to_txq[tc].count = count;
2859 dev->tc_to_txq[tc].offset = offset;
2860 return 0;
2861}
2862EXPORT_SYMBOL(netdev_set_tc_queue);
2863
2864int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2865{
2866 if (num_tc > TC_MAX_QUEUE)
2867 return -EINVAL;
2868
Alexander Duyck6234f872016-10-28 11:46:49 -04002869#ifdef CONFIG_XPS
2870 netif_reset_xps_queues_gt(dev, 0);
2871#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002872 netdev_unbind_all_sb_channels(dev);
2873
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002874 dev->num_tc = num_tc;
2875 return 0;
2876}
2877EXPORT_SYMBOL(netdev_set_num_tc);
2878
Alexander Duyckffcfe252018-07-09 12:19:38 -04002879void netdev_unbind_sb_channel(struct net_device *dev,
2880 struct net_device *sb_dev)
2881{
2882 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2883
2884#ifdef CONFIG_XPS
2885 netif_reset_xps_queues_gt(sb_dev, 0);
2886#endif
2887 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2888 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2889
2890 while (txq-- != &dev->_tx[0]) {
2891 if (txq->sb_dev == sb_dev)
2892 txq->sb_dev = NULL;
2893 }
2894}
2895EXPORT_SYMBOL(netdev_unbind_sb_channel);
2896
2897int netdev_bind_sb_channel_queue(struct net_device *dev,
2898 struct net_device *sb_dev,
2899 u8 tc, u16 count, u16 offset)
2900{
2901 /* Make certain the sb_dev and dev are already configured */
2902 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2903 return -EINVAL;
2904
2905 /* We cannot hand out queues we don't have */
2906 if ((offset + count) > dev->real_num_tx_queues)
2907 return -EINVAL;
2908
2909 /* Record the mapping */
2910 sb_dev->tc_to_txq[tc].count = count;
2911 sb_dev->tc_to_txq[tc].offset = offset;
2912
2913 /* Provide a way for Tx queue to find the tc_to_txq map or
2914 * XPS map for itself.
2915 */
2916 while (count--)
2917 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2918
2919 return 0;
2920}
2921EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2922
2923int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2924{
2925 /* Do not use a multiqueue device to represent a subordinate channel */
2926 if (netif_is_multiqueue(dev))
2927 return -ENODEV;
2928
2929 /* We allow channels 1 - 32767 to be used for subordinate channels.
2930 * Channel 0 is meant to be "native" mode and used only to represent
2931 * the main root device. We allow writing 0 to reset the device back
2932 * to normal mode after being used as a subordinate channel.
2933 */
2934 if (channel > S16_MAX)
2935 return -EINVAL;
2936
2937 dev->num_tc = -channel;
2938
2939 return 0;
2940}
2941EXPORT_SYMBOL(netdev_set_sb_channel);
2942
John Fastabendf0796d52010-07-01 13:21:57 +00002943/*
2944 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002945 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002946 */
Tom Herberte6484932010-10-18 18:04:39 +00002947int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002948{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002949 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002950 int rc;
2951
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002952 disabling = txq < dev->real_num_tx_queues;
2953
Tom Herberte6484932010-10-18 18:04:39 +00002954 if (txq < 1 || txq > dev->num_tx_queues)
2955 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002956
Ben Hutchings5c565802011-02-15 19:39:21 +00002957 if (dev->reg_state == NETREG_REGISTERED ||
2958 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002959 ASSERT_RTNL();
2960
Tom Herbert1d24eb42010-11-21 13:17:27 +00002961 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2962 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002963 if (rc)
2964 return rc;
2965
John Fastabend4f57c082011-01-17 08:06:04 +00002966 if (dev->num_tc)
2967 netif_setup_tc(dev, txq);
2968
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002969 dev->real_num_tx_queues = txq;
2970
2971 if (disabling) {
2972 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002973 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002974#ifdef CONFIG_XPS
2975 netif_reset_xps_queues_gt(dev, txq);
2976#endif
2977 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002978 } else {
2979 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002980 }
Tom Herberte6484932010-10-18 18:04:39 +00002981
Tom Herberte6484932010-10-18 18:04:39 +00002982 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002983}
2984EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002985
Michael Daltona953be52014-01-16 22:23:28 -08002986#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002987/**
2988 * netif_set_real_num_rx_queues - set actual number of RX queues used
2989 * @dev: Network device
2990 * @rxq: Actual number of RX queues
2991 *
2992 * This must be called either with the rtnl_lock held or before
2993 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002994 * negative error code. If called before registration, it always
2995 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002996 */
2997int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2998{
2999 int rc;
3000
Tom Herbertbd25fa72010-10-18 18:00:16 +00003001 if (rxq < 1 || rxq > dev->num_rx_queues)
3002 return -EINVAL;
3003
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003004 if (dev->reg_state == NETREG_REGISTERED) {
3005 ASSERT_RTNL();
3006
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003007 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
3008 rxq);
3009 if (rc)
3010 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003011 }
3012
3013 dev->real_num_rx_queues = rxq;
3014 return 0;
3015}
3016EXPORT_SYMBOL(netif_set_real_num_rx_queues);
3017#endif
3018
Ben Hutchings2c530402012-07-10 10:55:09 +00003019/**
3020 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00003021 *
3022 * This routine should set an upper limit on the number of RSS queues
3023 * used by default by multiqueue devices.
3024 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00003025int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00003026{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05303027 return is_kdump_kernel() ?
3028 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00003029}
3030EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3031
Eric Dumazet3bcb8462016-06-04 20:02:28 -07003032static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003033{
3034 struct softnet_data *sd;
3035 unsigned long flags;
3036
3037 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05003038 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00003039 q->next_sched = NULL;
3040 *sd->output_queue_tailp = q;
3041 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003042 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3043 local_irq_restore(flags);
3044}
3045
David S. Miller37437bb2008-07-16 02:15:04 -07003046void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08003047{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003048 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3049 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08003050}
3051EXPORT_SYMBOL(__netif_schedule);
3052
Eric Dumazete6247022013-12-05 04:45:08 -08003053struct dev_kfree_skb_cb {
3054 enum skb_free_reason reason;
3055};
3056
3057static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08003058{
Eric Dumazete6247022013-12-05 04:45:08 -08003059 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08003060}
Denis Vlasenko56079432006-03-29 15:57:29 -08003061
John Fastabend46e5da40a2014-09-12 20:04:52 -07003062void netif_schedule_queue(struct netdev_queue *txq)
3063{
3064 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03003065 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07003066 struct Qdisc *q = rcu_dereference(txq->qdisc);
3067
3068 __netif_schedule(q);
3069 }
3070 rcu_read_unlock();
3071}
3072EXPORT_SYMBOL(netif_schedule_queue);
3073
John Fastabend46e5da40a2014-09-12 20:04:52 -07003074void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3075{
3076 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3077 struct Qdisc *q;
3078
3079 rcu_read_lock();
3080 q = rcu_dereference(dev_queue->qdisc);
3081 __netif_schedule(q);
3082 rcu_read_unlock();
3083 }
3084}
3085EXPORT_SYMBOL(netif_tx_wake_queue);
3086
Eric Dumazete6247022013-12-05 04:45:08 -08003087void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3088{
3089 unsigned long flags;
3090
Myungho Jung98998862017-04-25 11:58:15 -07003091 if (unlikely(!skb))
3092 return;
3093
Reshetova, Elena63354792017-06-30 13:07:58 +03003094 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003095 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003096 refcount_set(&skb->users, 0);
3097 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003098 return;
3099 }
3100 get_kfree_skb_cb(skb)->reason = reason;
3101 local_irq_save(flags);
3102 skb->next = __this_cpu_read(softnet_data.completion_queue);
3103 __this_cpu_write(softnet_data.completion_queue, skb);
3104 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3105 local_irq_restore(flags);
3106}
3107EXPORT_SYMBOL(__dev_kfree_skb_irq);
3108
3109void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003110{
3111 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003112 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003113 else
3114 dev_kfree_skb(skb);
3115}
Eric Dumazete6247022013-12-05 04:45:08 -08003116EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003117
3118
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003119/**
3120 * netif_device_detach - mark device as removed
3121 * @dev: network device
3122 *
3123 * Mark device as removed from system and therefore no longer available.
3124 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003125void netif_device_detach(struct net_device *dev)
3126{
3127 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3128 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003129 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003130 }
3131}
3132EXPORT_SYMBOL(netif_device_detach);
3133
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003134/**
3135 * netif_device_attach - mark device as attached
3136 * @dev: network device
3137 *
3138 * Mark device as attached from system and restart if needed.
3139 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003140void netif_device_attach(struct net_device *dev)
3141{
3142 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3143 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003144 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003145 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003146 }
3147}
3148EXPORT_SYMBOL(netif_device_attach);
3149
Jiri Pirko5605c762015-05-12 14:56:12 +02003150/*
3151 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3152 * to be used as a distribution range.
3153 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003154static u16 skb_tx_hash(const struct net_device *dev,
3155 const struct net_device *sb_dev,
3156 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003157{
3158 u32 hash;
3159 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003160 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003161
Alexander Duyckeadec8772018-07-09 12:19:48 -04003162 if (dev->num_tc) {
3163 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3164
3165 qoffset = sb_dev->tc_to_txq[tc].offset;
3166 qcount = sb_dev->tc_to_txq[tc].count;
3167 }
3168
Jiri Pirko5605c762015-05-12 14:56:12 +02003169 if (skb_rx_queue_recorded(skb)) {
3170 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003171 if (hash >= qoffset)
3172 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003173 while (unlikely(hash >= qcount))
3174 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003175 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003176 }
3177
3178 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3179}
Jiri Pirko5605c762015-05-12 14:56:12 +02003180
Ben Hutchings36c92472012-01-17 07:57:56 +00003181static void skb_warn_bad_offload(const struct sk_buff *skb)
3182{
Wei Tang84d15ae2016-06-16 21:17:49 +08003183 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003184 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003185 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003186
Ben Greearc846ad92013-04-19 10:45:52 +00003187 if (!net_ratelimit())
3188 return;
3189
Bjørn Mork88ad4172015-11-16 19:16:40 +01003190 if (dev) {
3191 if (dev->dev.parent)
3192 name = dev_driver_string(dev->dev.parent);
3193 else
3194 name = netdev_name(dev);
3195 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003196 skb_dump(KERN_WARNING, skb, false);
3197 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003198 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003199 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202/*
3203 * Invalidate hardware checksum when packet is to be mangled, and
3204 * complete checksum manually on outgoing path.
3205 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003206int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Al Virod3bc23e2006-11-14 21:24:49 -08003208 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003209 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Patrick McHardy84fa7932006-08-29 16:44:56 -07003211 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003212 goto out_set_summed;
3213
Yi Li3aefd7d2020-10-27 13:59:04 +08003214 if (unlikely(skb_is_gso(skb))) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003215 skb_warn_bad_offload(skb);
3216 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218
Eric Dumazetcef401d2013-01-25 20:34:37 +00003219 /* Before computing a checksum, we should make sure no frag could
3220 * be modified by an external entity : checksum could be wrong.
3221 */
3222 if (skb_has_shared_frag(skb)) {
3223 ret = __skb_linearize(skb);
3224 if (ret)
3225 goto out;
3226 }
3227
Michał Mirosław55508d62010-12-14 15:24:08 +00003228 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003229 BUG_ON(offset >= skb_headlen(skb));
3230 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3231
3232 offset += skb->csum_offset;
3233 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3234
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003235 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3236 if (ret)
3237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003239 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003240out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return ret;
3244}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003245EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Davide Carattib72b5bf2017-05-18 15:44:38 +02003247int skb_crc32c_csum_help(struct sk_buff *skb)
3248{
3249 __le32 crc32c_csum;
3250 int ret = 0, offset, start;
3251
3252 if (skb->ip_summed != CHECKSUM_PARTIAL)
3253 goto out;
3254
3255 if (unlikely(skb_is_gso(skb)))
3256 goto out;
3257
3258 /* Before computing a checksum, we should make sure no frag could
3259 * be modified by an external entity : checksum could be wrong.
3260 */
3261 if (unlikely(skb_has_shared_frag(skb))) {
3262 ret = __skb_linearize(skb);
3263 if (ret)
3264 goto out;
3265 }
3266 start = skb_checksum_start_offset(skb);
3267 offset = start + offsetof(struct sctphdr, checksum);
3268 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3269 ret = -EINVAL;
3270 goto out;
3271 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003272
3273 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3274 if (ret)
3275 goto out;
3276
Davide Carattib72b5bf2017-05-18 15:44:38 +02003277 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3278 skb->len - start, ~(__u32)0,
3279 crc32c_csum_stub));
3280 *(__le32 *)(skb->data + offset) = crc32c_csum;
3281 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003282 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003283out:
3284 return ret;
3285}
3286
Vlad Yasevich53d64712014-03-27 17:26:18 -04003287__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003288{
3289 __be16 type = skb->protocol;
3290
Pravin B Shelar19acc322013-05-07 20:41:07 +00003291 /* Tunnel gso handlers can set protocol to ethernet. */
3292 if (type == htons(ETH_P_TEB)) {
3293 struct ethhdr *eth;
3294
3295 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3296 return 0;
3297
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003298 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003299 type = eth->h_proto;
3300 }
3301
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003302 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003303}
3304
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003305/**
3306 * skb_mac_gso_segment - mac layer segmentation handler.
3307 * @skb: buffer to segment
3308 * @features: features for the output path (see dev->features)
3309 */
3310struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3311 netdev_features_t features)
3312{
3313 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3314 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003315 int vlan_depth = skb->mac_len;
3316 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003317
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003318 if (unlikely(!type))
3319 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003320
Vlad Yasevich53d64712014-03-27 17:26:18 -04003321 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003322
3323 rcu_read_lock();
3324 list_for_each_entry_rcu(ptype, &offload_base, list) {
3325 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003326 segs = ptype->callbacks.gso_segment(skb, features);
3327 break;
3328 }
3329 }
3330 rcu_read_unlock();
3331
3332 __skb_push(skb, skb->data - skb_mac_header(skb));
3333
3334 return segs;
3335}
3336EXPORT_SYMBOL(skb_mac_gso_segment);
3337
3338
Cong Wang12b00042013-02-05 16:36:38 +00003339/* openvswitch calls this on rx path, so we need a different check.
3340 */
3341static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3342{
3343 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003344 return skb->ip_summed != CHECKSUM_PARTIAL &&
3345 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003346
3347 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003348}
3349
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003350/**
Cong Wang12b00042013-02-05 16:36:38 +00003351 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003352 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003353 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003354 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003355 *
3356 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003357 *
3358 * It may return NULL if the skb requires no segmentation. This is
3359 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003360 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003361 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003362 */
Cong Wang12b00042013-02-05 16:36:38 +00003363struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3364 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003365{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003366 struct sk_buff *segs;
3367
Cong Wang12b00042013-02-05 16:36:38 +00003368 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003369 int err;
3370
Eric Dumazetb2504a52017-01-31 10:20:32 -08003371 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003372 err = skb_cow_head(skb, 0);
3373 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003374 return ERR_PTR(err);
3375 }
3376
Alexander Duyck802ab552016-04-10 21:45:03 -04003377 /* Only report GSO partial support if it will enable us to
3378 * support segmentation on this frame without needing additional
3379 * work.
3380 */
3381 if (features & NETIF_F_GSO_PARTIAL) {
3382 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3383 struct net_device *dev = skb->dev;
3384
3385 partial_features |= dev->features & dev->gso_partial_features;
3386 if (!skb_gso_ok(skb, features | partial_features))
3387 features &= ~NETIF_F_GSO_PARTIAL;
3388 }
3389
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003390 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003391 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3392
Pravin B Shelar68c33162013-02-14 14:02:41 +00003393 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003394 SKB_GSO_CB(skb)->encap_level = 0;
3395
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003396 skb_reset_mac_header(skb);
3397 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003398
Eric Dumazetb2504a52017-01-31 10:20:32 -08003399 segs = skb_mac_gso_segment(skb, features);
3400
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003401 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003402 skb_warn_bad_offload(skb);
3403
3404 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003405}
Cong Wang12b00042013-02-05 16:36:38 +00003406EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003407
Herbert Xufb286bb2005-11-10 13:01:24 -08003408/* Take action when hardware reception checksum errors are detected. */
3409#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003410void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003411{
3412 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003413 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Willem de Bruijn64131392019-07-07 05:51:55 -04003414 skb_dump(KERN_ERR, skb, true);
Herbert Xufb286bb2005-11-10 13:01:24 -08003415 dump_stack();
3416 }
3417}
3418EXPORT_SYMBOL(netdev_rx_csum_fault);
3419#endif
3420
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003421/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003422static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003424#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 int i;
tchardingf4563a72017-02-09 17:56:07 +11003426
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003427 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003428 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3429 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003430
Ian Campbellea2ab692011-08-22 23:44:58 +00003431 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003432 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003433 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003434 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return 0;
3437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Simon Horman3b392dd2014-06-04 08:53:17 +09003439/* If MPLS offload request, verify we are testing hardware MPLS features
3440 * instead of standard features for the netdev.
3441 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003442#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003443static netdev_features_t net_mpls_features(struct sk_buff *skb,
3444 netdev_features_t features,
3445 __be16 type)
3446{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003447 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003448 features &= skb->dev->mpls_features;
3449
3450 return features;
3451}
3452#else
3453static netdev_features_t net_mpls_features(struct sk_buff *skb,
3454 netdev_features_t features,
3455 __be16 type)
3456{
3457 return features;
3458}
3459#endif
3460
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003461static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003462 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003463{
Simon Horman3b392dd2014-06-04 08:53:17 +09003464 __be16 type;
3465
Miaohe Lin9fc95f52020-07-30 19:02:36 +08003466 type = skb_network_protocol(skb, NULL);
Simon Horman3b392dd2014-06-04 08:53:17 +09003467 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003468
Ed Cashinc0d680e2012-09-19 15:49:00 +00003469 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003470 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003471 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003472 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003473 if (illegal_highdma(skb->dev, skb))
3474 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003475
3476 return features;
3477}
3478
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003479netdev_features_t passthru_features_check(struct sk_buff *skb,
3480 struct net_device *dev,
3481 netdev_features_t features)
3482{
3483 return features;
3484}
3485EXPORT_SYMBOL(passthru_features_check);
3486
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003487static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003488 struct net_device *dev,
3489 netdev_features_t features)
3490{
3491 return vlan_features_check(skb, features);
3492}
3493
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003494static netdev_features_t gso_features_check(const struct sk_buff *skb,
3495 struct net_device *dev,
3496 netdev_features_t features)
3497{
3498 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3499
3500 if (gso_segs > dev->gso_max_segs)
3501 return features & ~NETIF_F_GSO_MASK;
3502
Heiner Kallweit1d155df2020-11-21 00:22:20 +01003503 if (!skb_shinfo(skb)->gso_type) {
3504 skb_warn_bad_offload(skb);
3505 return features & ~NETIF_F_GSO_MASK;
3506 }
3507
Alexander Duyck802ab552016-04-10 21:45:03 -04003508 /* Support for GSO partial features requires software
3509 * intervention before we can actually process the packets
3510 * so we need to strip support for any partial features now
3511 * and we can pull them back in after we have partially
3512 * segmented the frame.
3513 */
3514 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3515 features &= ~dev->gso_partial_features;
3516
3517 /* Make sure to clear the IPv4 ID mangling feature if the
3518 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003519 */
3520 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3521 struct iphdr *iph = skb->encapsulation ?
3522 inner_ip_hdr(skb) : ip_hdr(skb);
3523
3524 if (!(iph->frag_off & htons(IP_DF)))
3525 features &= ~NETIF_F_TSO_MANGLEID;
3526 }
3527
3528 return features;
3529}
3530
Florian Westphalc1e756b2014-05-05 15:00:44 +02003531netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003532{
Jesse Gross5f352272014-12-23 22:37:26 -08003533 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003534 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003535
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003536 if (skb_is_gso(skb))
3537 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003538
Jesse Gross5f352272014-12-23 22:37:26 -08003539 /* If encapsulation offload request, verify we are testing
3540 * hardware encapsulation features instead of standard
3541 * features for the netdev
3542 */
3543 if (skb->encapsulation)
3544 features &= dev->hw_enc_features;
3545
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003546 if (skb_vlan_tagged(skb))
3547 features = netdev_intersect_features(features,
3548 dev->vlan_features |
3549 NETIF_F_HW_VLAN_CTAG_TX |
3550 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003551
Jesse Gross5f352272014-12-23 22:37:26 -08003552 if (dev->netdev_ops->ndo_features_check)
3553 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3554 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003555 else
3556 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003557
Florian Westphalc1e756b2014-05-05 15:00:44 +02003558 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003559}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003560EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003561
David S. Miller2ea25512014-08-29 21:10:01 -07003562static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003563 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003564{
David S. Miller2ea25512014-08-29 21:10:01 -07003565 unsigned int len;
3566 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003567
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003568 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003569 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003570
David S. Miller2ea25512014-08-29 21:10:01 -07003571 len = skb->len;
Willy Tarreau37447412020-08-10 10:27:42 +02003572 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
David S. Miller2ea25512014-08-29 21:10:01 -07003573 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003574 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003575 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003576
Patrick McHardy572a9d72009-11-10 06:14:14 +00003577 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003578}
David S. Miller2ea25512014-08-29 21:10:01 -07003579
David S. Miller8dcda222014-09-01 15:06:40 -07003580struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3581 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003582{
3583 struct sk_buff *skb = first;
3584 int rc = NETDEV_TX_OK;
3585
3586 while (skb) {
3587 struct sk_buff *next = skb->next;
3588
David S. Millera8305bf2018-07-29 20:42:53 -07003589 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003590 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003591 if (unlikely(!dev_xmit_complete(rc))) {
3592 skb->next = next;
3593 goto out;
3594 }
3595
3596 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003597 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003598 rc = NETDEV_TX_BUSY;
3599 break;
3600 }
3601 }
3602
3603out:
3604 *ret = rc;
3605 return skb;
3606}
3607
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003608static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3609 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003610{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003611 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003612 !vlan_hw_offload_capable(features, skb->vlan_proto))
3613 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003614 return skb;
3615}
3616
Davide Caratti43c26a12017-05-18 15:44:41 +02003617int skb_csum_hwoffload_help(struct sk_buff *skb,
3618 const netdev_features_t features)
3619{
3620 if (unlikely(skb->csum_not_inet))
3621 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3622 skb_crc32c_csum_help(skb);
3623
3624 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3625}
3626EXPORT_SYMBOL(skb_csum_hwoffload_help);
3627
Steffen Klassertf53c7232017-12-20 10:41:36 +01003628static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003629{
3630 netdev_features_t features;
3631
David S. Millereae3f882014-08-30 15:17:13 -07003632 features = netif_skb_features(skb);
3633 skb = validate_xmit_vlan(skb, features);
3634 if (unlikely(!skb))
3635 goto out_null;
3636
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003637 skb = sk_validate_xmit_skb(skb, dev);
3638 if (unlikely(!skb))
3639 goto out_null;
3640
Johannes Berg8b86a612015-04-17 15:45:04 +02003641 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003642 struct sk_buff *segs;
3643
3644 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003645 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003646 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003647 } else if (segs) {
3648 consume_skb(skb);
3649 skb = segs;
3650 }
David S. Millereae3f882014-08-30 15:17:13 -07003651 } else {
3652 if (skb_needs_linearize(skb, features) &&
3653 __skb_linearize(skb))
3654 goto out_kfree_skb;
3655
3656 /* If packet is not checksummed and device does not
3657 * support checksumming for this protocol, complete
3658 * checksumming here.
3659 */
3660 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3661 if (skb->encapsulation)
3662 skb_set_inner_transport_header(skb,
3663 skb_checksum_start_offset(skb));
3664 else
3665 skb_set_transport_header(skb,
3666 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003667 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003668 goto out_kfree_skb;
3669 }
3670 }
3671
Steffen Klassertf53c7232017-12-20 10:41:36 +01003672 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003673
David S. Millereae3f882014-08-30 15:17:13 -07003674 return skb;
3675
3676out_kfree_skb:
3677 kfree_skb(skb);
3678out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003679 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003680 return NULL;
3681}
3682
Steffen Klassertf53c7232017-12-20 10:41:36 +01003683struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003684{
3685 struct sk_buff *next, *head = NULL, *tail;
3686
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003687 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003688 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003689 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003690
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003691 /* in case skb wont be segmented, point to itself */
3692 skb->prev = skb;
3693
Steffen Klassertf53c7232017-12-20 10:41:36 +01003694 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003695 if (!skb)
3696 continue;
3697
3698 if (!head)
3699 head = skb;
3700 else
3701 tail->next = skb;
3702 /* If skb was segmented, skb->prev points to
3703 * the last segment. If not, it still contains skb.
3704 */
3705 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003706 }
3707 return head;
3708}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003709EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003710
Eric Dumazet1def9232013-01-10 12:36:42 +00003711static void qdisc_pkt_len_init(struct sk_buff *skb)
3712{
3713 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3714
3715 qdisc_skb_cb(skb)->pkt_len = skb->len;
3716
3717 /* To get more precise estimation of bytes sent on wire,
3718 * we add to pkt_len the headers size of all segments
3719 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003720 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003721 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003722 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003723
Eric Dumazet757b8b12013-01-15 21:14:21 -08003724 /* mac layer + network layer */
3725 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3726
3727 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003728 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3729 const struct tcphdr *th;
3730 struct tcphdr _tcphdr;
3731
3732 th = skb_header_pointer(skb, skb_transport_offset(skb),
3733 sizeof(_tcphdr), &_tcphdr);
3734 if (likely(th))
3735 hdr_len += __tcp_hdrlen(th);
3736 } else {
3737 struct udphdr _udphdr;
3738
3739 if (skb_header_pointer(skb, skb_transport_offset(skb),
3740 sizeof(_udphdr), &_udphdr))
3741 hdr_len += sizeof(struct udphdr);
3742 }
Jason Wang15e5a032013-03-25 20:19:59 +00003743
3744 if (shinfo->gso_type & SKB_GSO_DODGY)
3745 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3746 shinfo->gso_size);
3747
3748 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003749 }
3750}
3751
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003752static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3753 struct net_device *dev,
3754 struct netdev_queue *txq)
3755{
3756 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003757 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003758 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003759 int rc;
3760
Eric Dumazeta2da5702011-01-20 03:48:19 +00003761 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003762
3763 if (q->flags & TCQ_F_NOLOCK) {
Petr Machataac5c66f2020-07-14 20:03:08 +03003764 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Paolo Abeni379349e2020-02-18 18:15:44 +01003765 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003766
3767 if (unlikely(to_free))
3768 kfree_skb_list(to_free);
3769 return rc;
3770 }
3771
Eric Dumazet79640a42010-06-02 05:09:29 -07003772 /*
3773 * Heuristic to force contended enqueues to serialize on a
3774 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003775 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003776 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003777 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003778 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003779 if (unlikely(contended))
3780 spin_lock(&q->busylock);
3781
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003782 spin_lock(root_lock);
3783 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003784 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003785 rc = NET_XMIT_DROP;
3786 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003787 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003788 /*
3789 * This is a work-conserving queue; there are no old skbs
3790 * waiting to be sent out; and the qdisc is not running -
3791 * xmit the skb directly.
3792 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003793
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003794 qdisc_bstats_update(q, skb);
3795
Eric Dumazet55a93b32014-10-03 15:31:07 -07003796 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003797 if (unlikely(contended)) {
3798 spin_unlock(&q->busylock);
3799 contended = false;
3800 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003801 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003802 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003803
John Fastabend6c148182017-12-07 09:54:06 -08003804 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003805 rc = NET_XMIT_SUCCESS;
3806 } else {
Petr Machataac5c66f2020-07-14 20:03:08 +03003807 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003808 if (qdisc_run_begin(q)) {
3809 if (unlikely(contended)) {
3810 spin_unlock(&q->busylock);
3811 contended = false;
3812 }
3813 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003814 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003815 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003816 }
3817 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003818 if (unlikely(to_free))
3819 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003820 if (unlikely(contended))
3821 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003822 return rc;
3823}
3824
Daniel Borkmann86f85152013-12-29 17:27:11 +01003825#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003826static void skb_update_prio(struct sk_buff *skb)
3827{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003828 const struct netprio_map *map;
3829 const struct sock *sk;
3830 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003831
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003832 if (skb->priority)
3833 return;
3834 map = rcu_dereference_bh(skb->dev->priomap);
3835 if (!map)
3836 return;
3837 sk = skb_to_full_sk(skb);
3838 if (!sk)
3839 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003840
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003841 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3842
3843 if (prioidx < map->priomap_len)
3844 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003845}
3846#else
3847#define skb_update_prio(skb)
3848#endif
3849
Dave Jonesd29f7492008-07-22 14:09:06 -07003850/**
Michel Machado95603e22012-06-12 10:16:35 +00003851 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003852 * @net: network namespace this loopback is happening in
3853 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003854 * @skb: buffer to transmit
3855 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003856int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003857{
3858 skb_reset_mac_header(skb);
3859 __skb_pull(skb, skb_network_offset(skb));
3860 skb->pkt_type = PACKET_LOOPBACK;
3861 skb->ip_summed = CHECKSUM_UNNECESSARY;
3862 WARN_ON(!skb_dst(skb));
3863 skb_dst_force(skb);
3864 netif_rx_ni(skb);
3865 return 0;
3866}
3867EXPORT_SYMBOL(dev_loopback_xmit);
3868
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003869#ifdef CONFIG_NET_EGRESS
3870static struct sk_buff *
3871sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3872{
Jiri Pirko46209402017-11-03 11:46:25 +01003873 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003874 struct tcf_result cl_res;
3875
Jiri Pirko46209402017-11-03 11:46:25 +01003876 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003877 return skb;
3878
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003879 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
wenxuaadaca92020-11-25 12:01:21 +08003880 qdisc_skb_cb(skb)->mru = 0;
Jiri Pirko46209402017-11-03 11:46:25 +01003881 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003882
Jiri Pirko46209402017-11-03 11:46:25 +01003883 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003884 case TC_ACT_OK:
3885 case TC_ACT_RECLASSIFY:
3886 skb->tc_index = TC_H_MIN(cl_res.classid);
3887 break;
3888 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003889 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003890 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003891 kfree_skb(skb);
3892 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003893 case TC_ACT_STOLEN:
3894 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003895 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003896 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003897 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003898 return NULL;
3899 case TC_ACT_REDIRECT:
3900 /* No need to push/pop skb's mac_header here on egress! */
3901 skb_do_redirect(skb);
3902 *ret = NET_XMIT_SUCCESS;
3903 return NULL;
3904 default:
3905 break;
3906 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003907
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003908 return skb;
3909}
3910#endif /* CONFIG_NET_EGRESS */
3911
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003912#ifdef CONFIG_XPS
3913static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3914 struct xps_dev_maps *dev_maps, unsigned int tci)
3915{
3916 struct xps_map *map;
3917 int queue_index = -1;
3918
3919 if (dev->num_tc) {
3920 tci *= dev->num_tc;
3921 tci += netdev_get_prio_tc_map(dev, skb->priority);
3922 }
3923
3924 map = rcu_dereference(dev_maps->attr_map[tci]);
3925 if (map) {
3926 if (map->len == 1)
3927 queue_index = map->queues[0];
3928 else
3929 queue_index = map->queues[reciprocal_scale(
3930 skb_get_hash(skb), map->len)];
3931 if (unlikely(queue_index >= dev->real_num_tx_queues))
3932 queue_index = -1;
3933 }
3934 return queue_index;
3935}
3936#endif
3937
Alexander Duyckeadec8772018-07-09 12:19:48 -04003938static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3939 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003940{
3941#ifdef CONFIG_XPS
3942 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003943 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003944 int queue_index = -1;
3945
Amritha Nambiar04157462018-06-29 21:26:46 -07003946 if (!static_key_false(&xps_needed))
3947 return -1;
3948
Jiri Pirko638b2a62015-05-12 14:56:13 +02003949 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003950 if (!static_key_false(&xps_rxqs_needed))
3951 goto get_cpus_map;
3952
Alexander Duyckeadec8772018-07-09 12:19:48 -04003953 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003954 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003955 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003956
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003957 if (tci >= 0 && tci < dev->num_rx_queues)
3958 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3959 tci);
3960 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003961
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003962get_cpus_map:
3963 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003964 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003965 if (dev_maps) {
3966 unsigned int tci = skb->sender_cpu - 1;
3967
3968 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3969 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003970 }
3971 }
3972 rcu_read_unlock();
3973
3974 return queue_index;
3975#else
3976 return -1;
3977#endif
3978}
3979
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003980u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003981 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003982{
3983 return 0;
3984}
3985EXPORT_SYMBOL(dev_pick_tx_zero);
3986
3987u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003988 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003989{
3990 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3991}
3992EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3993
Paolo Abenib71b5832019-03-20 11:02:05 +01003994u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3995 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003996{
3997 struct sock *sk = skb->sk;
3998 int queue_index = sk_tx_queue_get(sk);
3999
Alexander Duyckeadec8772018-07-09 12:19:48 -04004000 sb_dev = sb_dev ? : dev;
4001
Jiri Pirko638b2a62015-05-12 14:56:13 +02004002 if (queue_index < 0 || skb->ooo_okay ||
4003 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04004004 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11004005
Jiri Pirko638b2a62015-05-12 14:56:13 +02004006 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04004007 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004008
4009 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07004010 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02004011 rcu_access_pointer(sk->sk_dst_cache))
4012 sk_tx_queue_set(sk, new_index);
4013
4014 queue_index = new_index;
4015 }
4016
4017 return queue_index;
4018}
Paolo Abenib71b5832019-03-20 11:02:05 +01004019EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004020
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004021struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4022 struct sk_buff *skb,
4023 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004024{
4025 int queue_index = 0;
4026
4027#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08004028 u32 sender_cpu = skb->sender_cpu - 1;
4029
4030 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004031 skb->sender_cpu = raw_smp_processor_id() + 1;
4032#endif
4033
4034 if (dev->real_num_tx_queues != 1) {
4035 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11004036
Jiri Pirko638b2a62015-05-12 14:56:13 +02004037 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01004038 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004039 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004040 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004041
Alexander Duyckd5845272017-11-22 10:57:41 -08004042 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004043 }
4044
4045 skb_set_queue_mapping(skb, queue_index);
4046 return netdev_get_tx_queue(dev, queue_index);
4047}
4048
Michel Machado95603e22012-06-12 10:16:35 +00004049/**
Jason Wang9d08dd32014-01-20 11:25:13 +08004050 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07004051 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04004052 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07004053 *
4054 * Queue a buffer for transmission to a network device. The caller must
4055 * have set the device and priority and built the buffer before calling
4056 * this function. The function can be called from an interrupt.
4057 *
4058 * A negative errno code is returned on a failure. A success does not
4059 * guarantee the frame will be transmitted as it may be dropped due
4060 * to congestion or traffic shaping.
4061 *
4062 * -----------------------------------------------------------------------------------
4063 * I notice this method can also return errors from the queue disciplines,
4064 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4065 * be positive.
4066 *
4067 * Regardless of the return value, the skb is consumed, so it is currently
4068 * difficult to retry a send to this method. (You can bump the ref count
4069 * before sending to hold a reference for retry if you are careful.)
4070 *
4071 * When calling this method, interrupts MUST be enabled. This is because
4072 * the BH enable code must have IRQs enabled so that it will not deadlock.
4073 * --BLG
4074 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04004075static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
4077 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07004078 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 struct Qdisc *q;
4080 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01004081 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00004083 skb_reset_mac_header(skb);
4084
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004085 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4086 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
4087
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004088 /* Disable soft irqs for various locks below. Also
4089 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004091 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Neil Horman5bc14212011-11-22 05:10:51 +00004093 skb_update_prio(skb);
4094
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004095 qdisc_pkt_len_init(skb);
4096#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004097 skb->tc_at_ingress = 0;
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004098# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004099 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004100 skb = sch_handle_egress(skb, &rc, dev);
4101 if (!skb)
4102 goto out;
4103 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004104# endif
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004105#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004106 /* If device/qdisc don't need skb->dst, release it right now while
4107 * its hot in this cpu cache.
4108 */
4109 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4110 skb_dst_drop(skb);
4111 else
4112 skb_dst_force(skb);
4113
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004114 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004115 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004116
Koki Sanagicf66ba52010-08-23 18:45:02 +09004117 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004119 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 }
4122
4123 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004124 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
tchardingeb13da12017-02-09 17:56:06 +11004126 * Really, it is unlikely that netif_tx_lock protection is necessary
4127 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4128 * counters.)
4129 * However, it is possible, that they rely on protection
4130 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
tchardingeb13da12017-02-09 17:56:06 +11004132 * Check this and shot the lock. It is not prone from deadlocks.
4133 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 */
4135 if (dev->flags & IFF_UP) {
4136 int cpu = smp_processor_id(); /* ok because BHs are off */
4137
David S. Millerc773e842008-07-08 23:13:53 -07004138 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004139 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004140 goto recursion_alert;
4141
Steffen Klassertf53c7232017-12-20 10:41:36 +01004142 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004143 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004144 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004145
Willy Tarreau37447412020-08-10 10:27:42 +02004146 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
David S. Millerc773e842008-07-08 23:13:53 -07004147 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Tom Herbert734664982011-11-28 16:32:44 +00004149 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004150 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004151 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004152 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004153 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004154 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 goto out;
4156 }
4157 }
David S. Millerc773e842008-07-08 23:13:53 -07004158 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004159 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4160 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 } else {
4162 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004163 * unfortunately
4164 */
4165recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004166 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4167 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
4169 }
4170
4171 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004172 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Eric Dumazet015f0682014-03-27 08:45:56 -07004174 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004175 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return rc;
4177out:
Herbert Xud4828d82006-06-22 02:28:18 -07004178 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return rc;
4180}
Jason Wangf663dd92014-01-10 16:18:26 +08004181
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004182int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004183{
4184 return __dev_queue_xmit(skb, NULL);
4185}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004186EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Alexander Duyckeadec8772018-07-09 12:19:48 -04004188int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004189{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004190 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004191}
4192EXPORT_SYMBOL(dev_queue_xmit_accel);
4193
Björn Töpel36ccdf82020-11-23 18:56:00 +01004194int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004195{
4196 struct net_device *dev = skb->dev;
4197 struct sk_buff *orig_skb = skb;
4198 struct netdev_queue *txq;
4199 int ret = NETDEV_TX_BUSY;
4200 bool again = false;
4201
4202 if (unlikely(!netif_running(dev) ||
4203 !netif_carrier_ok(dev)))
4204 goto drop;
4205
4206 skb = validate_xmit_skb_list(skb, dev, &again);
4207 if (skb != orig_skb)
4208 goto drop;
4209
4210 skb_set_queue_mapping(skb, queue_id);
4211 txq = skb_get_tx_queue(dev, skb);
Willy Tarreau37447412020-08-10 10:27:42 +02004212 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004213
4214 local_bh_disable();
4215
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004216 dev_xmit_recursion_inc();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004217 HARD_TX_LOCK(dev, txq, smp_processor_id());
4218 if (!netif_xmit_frozen_or_drv_stopped(txq))
4219 ret = netdev_start_xmit(skb, dev, txq, false);
4220 HARD_TX_UNLOCK(dev, txq);
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004221 dev_xmit_recursion_dec();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004222
4223 local_bh_enable();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004224 return ret;
4225drop:
4226 atomic_long_inc(&dev->tx_dropped);
4227 kfree_skb_list(skb);
4228 return NET_XMIT_DROP;
4229}
Björn Töpel36ccdf82020-11-23 18:56:00 +01004230EXPORT_SYMBOL(__dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
tchardingeb13da12017-02-09 17:56:06 +11004232/*************************************************************************
4233 * Receiver routines
4234 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004236int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004237EXPORT_SYMBOL(netdev_max_backlog);
4238
Eric Dumazet3b098e22010-05-15 23:57:10 -07004239int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004240int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004241/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4242unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004243int weight_p __read_mostly = 64; /* old backlog weight */
4244int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4245int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4246int dev_rx_weight __read_mostly = 64;
4247int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004248/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4249int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004251/* Called with irq disabled */
4252static inline void ____napi_schedule(struct softnet_data *sd,
4253 struct napi_struct *napi)
4254{
4255 list_add_tail(&napi->poll_list, &sd->poll_list);
4256 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4257}
4258
Eric Dumazetdf334542010-03-24 19:13:54 +00004259#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004260
4261/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004262struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004263EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004264u32 rps_cpu_mask __read_mostly;
4265EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004266
Eric Dumazetdc053602019-03-22 08:56:38 -07004267struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004268EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004269struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004270EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004271
Ben Hutchingsc4454772011-01-19 11:03:53 +00004272static struct rps_dev_flow *
4273set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4274 struct rps_dev_flow *rflow, u16 next_cpu)
4275{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004276 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004277#ifdef CONFIG_RFS_ACCEL
4278 struct netdev_rx_queue *rxqueue;
4279 struct rps_dev_flow_table *flow_table;
4280 struct rps_dev_flow *old_rflow;
4281 u32 flow_id;
4282 u16 rxq_index;
4283 int rc;
4284
4285 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004286 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4287 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004288 goto out;
4289 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4290 if (rxq_index == skb_get_rx_queue(skb))
4291 goto out;
4292
4293 rxqueue = dev->_rx + rxq_index;
4294 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4295 if (!flow_table)
4296 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004297 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004298 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4299 rxq_index, flow_id);
4300 if (rc < 0)
4301 goto out;
4302 old_rflow = rflow;
4303 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004304 rflow->filter = rc;
4305 if (old_rflow->filter == rflow->filter)
4306 old_rflow->filter = RPS_NO_FILTER;
4307 out:
4308#endif
4309 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004310 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004311 }
4312
Ben Hutchings09994d12011-10-03 04:42:46 +00004313 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004314 return rflow;
4315}
4316
Tom Herbert0a9627f2010-03-16 08:03:29 +00004317/*
4318 * get_rps_cpu is called from netif_receive_skb and returns the target
4319 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004320 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004321 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004322static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4323 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004324{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004325 const struct rps_sock_flow_table *sock_flow_table;
4326 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004327 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004328 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004329 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004330 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004331 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004332
Tom Herbert0a9627f2010-03-16 08:03:29 +00004333 if (skb_rx_queue_recorded(skb)) {
4334 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004335
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004336 if (unlikely(index >= dev->real_num_rx_queues)) {
4337 WARN_ONCE(dev->real_num_rx_queues > 1,
4338 "%s received packet on queue %u, but number "
4339 "of RX queues is %u\n",
4340 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004341 goto done;
4342 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004343 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004344 }
4345
Eric Dumazet567e4b72015-02-06 12:59:01 -08004346 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4347
4348 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4349 map = rcu_dereference(rxqueue->rps_map);
4350 if (!flow_table && !map)
4351 goto done;
4352
Changli Gao2d47b452010-08-17 19:00:56 +00004353 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004354 hash = skb_get_hash(skb);
4355 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004356 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004357
Tom Herbertfec5e652010-04-16 16:01:27 -07004358 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4359 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004360 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004361 u32 next_cpu;
4362 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004363
Eric Dumazet567e4b72015-02-06 12:59:01 -08004364 /* First check into global flow table if there is a match */
4365 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4366 if ((ident ^ hash) & ~rps_cpu_mask)
4367 goto try_rps;
4368
4369 next_cpu = ident & rps_cpu_mask;
4370
4371 /* OK, now we know there is a match,
4372 * we can look at the local (per receive queue) flow table
4373 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004374 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004375 tcpu = rflow->cpu;
4376
Tom Herbertfec5e652010-04-16 16:01:27 -07004377 /*
4378 * If the desired CPU (where last recvmsg was done) is
4379 * different from current CPU (one in the rx-queue flow
4380 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004381 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004382 * - Current CPU is offline.
4383 * - The current CPU's queue tail has advanced beyond the
4384 * last packet that was enqueued using this table entry.
4385 * This guarantees that all previous packets for the flow
4386 * have been dequeued, thus preserving in order delivery.
4387 */
4388 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004389 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004390 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004391 rflow->last_qtail)) >= 0)) {
4392 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004393 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004394 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004395
Eric Dumazeta31196b2015-04-25 09:35:24 -07004396 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004397 *rflowp = rflow;
4398 cpu = tcpu;
4399 goto done;
4400 }
4401 }
4402
Eric Dumazet567e4b72015-02-06 12:59:01 -08004403try_rps:
4404
Tom Herbert0a9627f2010-03-16 08:03:29 +00004405 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004406 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004407 if (cpu_online(tcpu)) {
4408 cpu = tcpu;
4409 goto done;
4410 }
4411 }
4412
4413done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004414 return cpu;
4415}
4416
Ben Hutchingsc4454772011-01-19 11:03:53 +00004417#ifdef CONFIG_RFS_ACCEL
4418
4419/**
4420 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4421 * @dev: Device on which the filter was set
4422 * @rxq_index: RX queue index
4423 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4424 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4425 *
4426 * Drivers that implement ndo_rx_flow_steer() should periodically call
4427 * this function for each installed filter and remove the filters for
4428 * which it returns %true.
4429 */
4430bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4431 u32 flow_id, u16 filter_id)
4432{
4433 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4434 struct rps_dev_flow_table *flow_table;
4435 struct rps_dev_flow *rflow;
4436 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004437 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004438
4439 rcu_read_lock();
4440 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4441 if (flow_table && flow_id <= flow_table->mask) {
4442 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004443 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004444 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004445 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4446 rflow->last_qtail) <
4447 (int)(10 * flow_table->mask)))
4448 expire = false;
4449 }
4450 rcu_read_unlock();
4451 return expire;
4452}
4453EXPORT_SYMBOL(rps_may_expire_flow);
4454
4455#endif /* CONFIG_RFS_ACCEL */
4456
Tom Herbert0a9627f2010-03-16 08:03:29 +00004457/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004458static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004459{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004460 struct softnet_data *sd = data;
4461
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004462 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004463 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004464}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004465
Tom Herbertfec5e652010-04-16 16:01:27 -07004466#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004467
4468/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004469 * Check if this softnet_data structure is another cpu one
4470 * If yes, queue it to our IPI list and return 1
4471 * If no, return 0
4472 */
4473static int rps_ipi_queued(struct softnet_data *sd)
4474{
4475#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004476 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004477
4478 if (sd != mysd) {
4479 sd->rps_ipi_next = mysd->rps_ipi_list;
4480 mysd->rps_ipi_list = sd;
4481
4482 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4483 return 1;
4484 }
4485#endif /* CONFIG_RPS */
4486 return 0;
4487}
4488
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004489#ifdef CONFIG_NET_FLOW_LIMIT
4490int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4491#endif
4492
4493static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4494{
4495#ifdef CONFIG_NET_FLOW_LIMIT
4496 struct sd_flow_limit *fl;
4497 struct softnet_data *sd;
4498 unsigned int old_flow, new_flow;
4499
4500 if (qlen < (netdev_max_backlog >> 1))
4501 return false;
4502
Christoph Lameter903ceff2014-08-17 12:30:35 -05004503 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004504
4505 rcu_read_lock();
4506 fl = rcu_dereference(sd->flow_limit);
4507 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004508 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004509 old_flow = fl->history[fl->history_head];
4510 fl->history[fl->history_head] = new_flow;
4511
4512 fl->history_head++;
4513 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4514
4515 if (likely(fl->buckets[old_flow]))
4516 fl->buckets[old_flow]--;
4517
4518 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4519 fl->count++;
4520 rcu_read_unlock();
4521 return true;
4522 }
4523 }
4524 rcu_read_unlock();
4525#endif
4526 return false;
4527}
4528
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004529/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004530 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4531 * queue (may be a remote CPU queue).
4532 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004533static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4534 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004535{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004536 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004537 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004538 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004539
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004540 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004541
4542 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004543
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004544 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004545 if (!netif_running(skb->dev))
4546 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004547 qlen = skb_queue_len(&sd->input_pkt_queue);
4548 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004549 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004550enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004551 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004552 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004553 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004554 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004555 return NET_RX_SUCCESS;
4556 }
4557
Eric Dumazetebda37c22010-05-06 23:51:21 +00004558 /* Schedule NAPI for backlog device
4559 * We can use non atomic operation since we own the queue lock
4560 */
4561 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004562 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004563 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004564 }
4565 goto enqueue;
4566 }
4567
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004568drop:
Changli Gaodee42872010-05-02 05:42:16 +00004569 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004570 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004571
Tom Herbert0a9627f2010-03-16 08:03:29 +00004572 local_irq_restore(flags);
4573
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004574 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004575 kfree_skb(skb);
4576 return NET_RX_DROP;
4577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004579static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4580{
4581 struct net_device *dev = skb->dev;
4582 struct netdev_rx_queue *rxqueue;
4583
4584 rxqueue = dev->_rx;
4585
4586 if (skb_rx_queue_recorded(skb)) {
4587 u16 index = skb_get_rx_queue(skb);
4588
4589 if (unlikely(index >= dev->real_num_rx_queues)) {
4590 WARN_ONCE(dev->real_num_rx_queues > 1,
4591 "%s received packet on queue %u, but number "
4592 "of RX queues is %u\n",
4593 dev->name, index, dev->real_num_rx_queues);
4594
4595 return rxqueue; /* Return first rxqueue */
4596 }
4597 rxqueue += index;
4598 }
4599 return rxqueue;
4600}
4601
John Fastabendd4455162017-07-17 09:26:45 -07004602static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004603 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004604 struct bpf_prog *xdp_prog)
4605{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004606 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004607 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004608 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004609 __be16 orig_eth_type;
4610 struct ethhdr *eth;
4611 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004612 int hlen, off;
4613 u32 mac_len;
4614
4615 /* Reinjected packets coming from act_mirred or similar should
4616 * not get XDP generic processing.
4617 */
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01004618 if (skb_is_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004619 return XDP_PASS;
4620
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004621 /* XDP packets must be linear and must have sufficient headroom
4622 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4623 * native XDP provides, thus we need to do it here as well.
4624 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004625 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004626 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4627 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4628 int troom = skb->tail + skb->data_len - skb->end;
4629
4630 /* In case we have to go down the path and also linearize,
4631 * then lets do the pskb_expand_head() work just once here.
4632 */
4633 if (pskb_expand_head(skb,
4634 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4635 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4636 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004637 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004638 goto do_drop;
4639 }
John Fastabendd4455162017-07-17 09:26:45 -07004640
4641 /* The XDP program wants to see the packet starting at the MAC
4642 * header.
4643 */
4644 mac_len = skb->data - skb_mac_header(skb);
4645 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004646 xdp->data = skb->data - mac_len;
4647 xdp->data_meta = xdp->data;
4648 xdp->data_end = xdp->data + hlen;
4649 xdp->data_hard_start = skb->data - skb_headroom(skb);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004650
4651 /* SKB "head" area always have tailroom for skb_shared_info */
4652 xdp->frame_sz = (void *)skb_end_pointer(skb) - xdp->data_hard_start;
4653 xdp->frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4654
Björn Töpel02671e22018-05-02 13:01:30 +02004655 orig_data_end = xdp->data_end;
4656 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004657 eth = (struct ethhdr *)xdp->data;
4658 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4659 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004660
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004661 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004662 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004663
Björn Töpel02671e22018-05-02 13:01:30 +02004664 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004665
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004666 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004667 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004668 if (off) {
4669 if (off > 0)
4670 __skb_pull(skb, off);
4671 else if (off < 0)
4672 __skb_push(skb, -off);
4673
4674 skb->mac_header += off;
4675 skb_reset_network_header(skb);
4676 }
John Fastabendd4455162017-07-17 09:26:45 -07004677
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004678 /* check if bpf_xdp_adjust_tail was used */
4679 off = xdp->data_end - orig_data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004680 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004681 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004682 skb->len += off; /* positive on grow, negative on shrink */
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004683 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004684
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004685 /* check if XDP changed eth hdr such SKB needs update */
4686 eth = (struct ethhdr *)xdp->data;
4687 if ((orig_eth_type != eth->h_proto) ||
4688 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4689 __skb_push(skb, ETH_HLEN);
4690 skb->protocol = eth_type_trans(skb, skb->dev);
4691 }
4692
John Fastabendd4455162017-07-17 09:26:45 -07004693 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004694 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004695 case XDP_TX:
4696 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004697 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004698 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004699 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004700 if (metalen)
4701 skb_metadata_set(skb, metalen);
4702 break;
John Fastabendd4455162017-07-17 09:26:45 -07004703 default:
4704 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004705 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004706 case XDP_ABORTED:
4707 trace_xdp_exception(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004708 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004709 case XDP_DROP:
4710 do_drop:
4711 kfree_skb(skb);
4712 break;
4713 }
4714
4715 return act;
4716}
4717
4718/* When doing generic XDP we have to bypass the qdisc layer and the
4719 * network taps in order to match in-driver-XDP behavior.
4720 */
Jason Wang7c497472017-08-11 19:41:17 +08004721void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004722{
4723 struct net_device *dev = skb->dev;
4724 struct netdev_queue *txq;
4725 bool free_skb = true;
4726 int cpu, rc;
4727
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004728 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004729 cpu = smp_processor_id();
4730 HARD_TX_LOCK(dev, txq, cpu);
4731 if (!netif_xmit_stopped(txq)) {
4732 rc = netdev_start_xmit(skb, dev, txq, 0);
4733 if (dev_xmit_complete(rc))
4734 free_skb = false;
4735 }
4736 HARD_TX_UNLOCK(dev, txq);
4737 if (free_skb) {
4738 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4739 kfree_skb(skb);
4740 }
4741}
4742
Davidlohr Bueso02786472018-05-08 09:07:02 -07004743static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004744
Jason Wang7c497472017-08-11 19:41:17 +08004745int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004746{
John Fastabendd4455162017-07-17 09:26:45 -07004747 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004748 struct xdp_buff xdp;
4749 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004750 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004751
Björn Töpel02671e22018-05-02 13:01:30 +02004752 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004753 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004754 switch (act) {
4755 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004756 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004757 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004758 if (err)
4759 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004760 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004761 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004762 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004763 break;
4764 }
John Fastabendd4455162017-07-17 09:26:45 -07004765 return XDP_DROP;
4766 }
4767 }
4768 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004769out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004770 kfree_skb(skb);
4771 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004772}
Jason Wang7c497472017-08-11 19:41:17 +08004773EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004774
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004775static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004777 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Eric Dumazet588f0332011-11-15 04:12:55 +00004779 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Koki Sanagicf66ba52010-08-23 18:45:02 +09004781 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004782
Eric Dumazetdf334542010-03-24 19:13:54 +00004783#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004784 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004785 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004786 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Changli Gaocece1942010-08-07 20:35:43 -07004788 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004789 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004790
4791 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004792 if (cpu < 0)
4793 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004794
4795 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4796
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004797 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004798 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004799 } else
4800#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004801 {
4802 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004803
Tom Herbertfec5e652010-04-16 16:01:27 -07004804 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4805 put_cpu();
4806 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004807 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004809
4810/**
4811 * netif_rx - post buffer to the network code
4812 * @skb: buffer to post
4813 *
4814 * This function receives a packet from a device driver and queues it for
4815 * the upper (protocol) levels to process. It always succeeds. The buffer
4816 * may be dropped during processing for congestion control or by the
4817 * protocol layers.
4818 *
4819 * return values:
4820 * NET_RX_SUCCESS (no congestion)
4821 * NET_RX_DROP (packet was dropped)
4822 *
4823 */
4824
4825int netif_rx(struct sk_buff *skb)
4826{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004827 int ret;
4828
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004829 trace_netif_rx_entry(skb);
4830
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004831 ret = netif_rx_internal(skb);
4832 trace_netif_rx_exit(ret);
4833
4834 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004835}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004836EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838int netif_rx_ni(struct sk_buff *skb)
4839{
4840 int err;
4841
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004842 trace_netif_rx_ni_entry(skb);
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004845 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 if (local_softirq_pending())
4847 do_softirq();
4848 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004849 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
4851 return err;
4852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853EXPORT_SYMBOL(netif_rx_ni);
4854
Sebastian Andrzej Siewiorc11171a2020-09-29 22:25:12 +02004855int netif_rx_any_context(struct sk_buff *skb)
4856{
4857 /*
4858 * If invoked from contexts which do not invoke bottom half
4859 * processing either at return from interrupt or when softrqs are
4860 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4861 * directly.
4862 */
4863 if (in_interrupt())
4864 return netif_rx(skb);
4865 else
4866 return netif_rx_ni(skb);
4867}
4868EXPORT_SYMBOL(netif_rx_any_context);
4869
Emese Revfy0766f782016-06-20 20:42:34 +02004870static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004872 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (sd->completion_queue) {
4875 struct sk_buff *clist;
4876
4877 local_irq_disable();
4878 clist = sd->completion_queue;
4879 sd->completion_queue = NULL;
4880 local_irq_enable();
4881
4882 while (clist) {
4883 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 clist = clist->next;
4886
Reshetova, Elena63354792017-06-30 13:07:58 +03004887 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004888 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4889 trace_consume_skb(skb);
4890 else
4891 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004892
4893 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4894 __kfree_skb(skb);
4895 else
4896 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004898
4899 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 }
4901
4902 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004903 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 local_irq_disable();
4906 head = sd->output_queue;
4907 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004908 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 local_irq_enable();
4910
4911 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004912 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004913 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 head = head->next_sched;
4916
John Fastabend6b3ba912017-12-07 09:54:25 -08004917 if (!(q->flags & TCQ_F_NOLOCK)) {
4918 root_lock = qdisc_lock(q);
4919 spin_lock(root_lock);
4920 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004921 /* We need to make sure head->next_sched is read
4922 * before clearing __QDISC_STATE_SCHED
4923 */
4924 smp_mb__before_atomic();
4925 clear_bit(__QDISC_STATE_SCHED, &q->state);
4926 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004927 if (root_lock)
4928 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
4930 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004931
4932 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004935#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004936/* This hook is defined here for ATM LANE */
4937int (*br_fdb_test_addr_hook)(struct net_device *dev,
4938 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004939EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004942static inline struct sk_buff *
4943sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004944 struct net_device *orig_dev, bool *another)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004945{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004946#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004947 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004948 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004949
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004950 /* If there's at least one ingress present somewhere (so
4951 * we get here via enabled static key), remaining devices
4952 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004953 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004954 */
Jiri Pirko46209402017-11-03 11:46:25 +01004955 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004956 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004957
Herbert Xuf697c3e2007-10-14 00:38:47 -07004958 if (*pt_prev) {
4959 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4960 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004961 }
4962
Florian Westphal33654952015-05-14 00:36:28 +02004963 qdisc_skb_cb(skb)->pkt_len = skb->len;
wenxuaadaca92020-11-25 12:01:21 +08004964 qdisc_skb_cb(skb)->mru = 0;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004965 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004966 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004967
Paul Blakey7d17c542020-02-16 12:01:22 +02004968 switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list,
4969 &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004970 case TC_ACT_OK:
4971 case TC_ACT_RECLASSIFY:
4972 skb->tc_index = TC_H_MIN(cl_res.classid);
4973 break;
4974 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004975 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004976 kfree_skb(skb);
4977 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004978 case TC_ACT_STOLEN:
4979 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004980 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004981 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004982 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004983 case TC_ACT_REDIRECT:
4984 /* skb_mac_header check was done by cls/act_bpf, so
4985 * we can safely push the L2 header back before
4986 * redirecting to another netdev
4987 */
4988 __skb_push(skb, skb->mac_len);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004989 if (skb_do_redirect(skb) == -EAGAIN) {
4990 __skb_pull(skb, skb->mac_len);
4991 *another = true;
4992 break;
4993 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004994 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01004995 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02004996 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004997 default:
4998 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004999 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005000#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07005001 return skb;
5002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005004/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07005005 * netdev_is_rx_handler_busy - check if receive handler is registered
5006 * @dev: device to check
5007 *
5008 * Check if a receive handler is already registered for a given device.
5009 * Return true if there one.
5010 *
5011 * The caller must hold the rtnl_mutex.
5012 */
5013bool netdev_is_rx_handler_busy(struct net_device *dev)
5014{
5015 ASSERT_RTNL();
5016 return dev && rtnl_dereference(dev->rx_handler);
5017}
5018EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5019
5020/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005021 * netdev_rx_handler_register - register receive handler
5022 * @dev: device to register a handler for
5023 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00005024 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005025 *
Masanari Iidae2278672014-02-18 22:54:36 +09005026 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005027 * called from __netif_receive_skb. A negative errno code is returned
5028 * on a failure.
5029 *
5030 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005031 *
5032 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005033 */
5034int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00005035 rx_handler_func_t *rx_handler,
5036 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005037{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08005038 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005039 return -EBUSY;
5040
Paolo Abenif54262502018-03-09 10:39:24 +01005041 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5042 return -EINVAL;
5043
Eric Dumazet00cfec32013-03-29 03:01:22 +00005044 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00005045 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005046 rcu_assign_pointer(dev->rx_handler, rx_handler);
5047
5048 return 0;
5049}
5050EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5051
5052/**
5053 * netdev_rx_handler_unregister - unregister receive handler
5054 * @dev: device to unregister a handler from
5055 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00005056 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005057 *
5058 * The caller must hold the rtnl_mutex.
5059 */
5060void netdev_rx_handler_unregister(struct net_device *dev)
5061{
5062
5063 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005064 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00005065 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5066 * section has a guarantee to see a non NULL rx_handler_data
5067 * as well.
5068 */
5069 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005070 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005071}
5072EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5073
Mel Gormanb4b9e352012-07-31 16:44:26 -07005074/*
5075 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5076 * the special handling of PFMEMALLOC skbs.
5077 */
5078static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5079{
5080 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07005081 case htons(ETH_P_ARP):
5082 case htons(ETH_P_IP):
5083 case htons(ETH_P_IPV6):
5084 case htons(ETH_P_8021Q):
5085 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07005086 return true;
5087 default:
5088 return false;
5089 }
5090}
5091
Pablo Neirae687ad62015-05-13 18:19:38 +02005092static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5093 int *ret, struct net_device *orig_dev)
5094{
5095 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04005096 int ingress_retval;
5097
Pablo Neirae687ad62015-05-13 18:19:38 +02005098 if (*pt_prev) {
5099 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5100 *pt_prev = NULL;
5101 }
5102
Aaron Conole2c1e2702016-09-21 11:35:03 -04005103 rcu_read_lock();
5104 ingress_retval = nf_hook_ingress(skb);
5105 rcu_read_unlock();
5106 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02005107 }
5108 return 0;
5109}
Pablo Neirae687ad62015-05-13 18:19:38 +02005110
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005111static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
Edward Cree88eb1942018-07-02 16:13:56 +01005112 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
5114 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005115 rx_handler_func_t *rx_handler;
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005116 struct sk_buff *skb = *pskb;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07005117 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005118 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08005120 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
Eric Dumazet588f0332011-11-15 04:12:55 +00005122 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005123
Koki Sanagicf66ba52010-08-23 18:45:02 +09005124 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005125
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005126 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005127
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005128 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005129 if (!skb_transport_header_was_set(skb))
5130 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005131 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 pt_prev = NULL;
5134
David S. Miller63d8ea72011-02-28 10:48:59 -08005135another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005136 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005137
5138 __this_cpu_inc(softnet_data.processed);
5139
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005140 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5141 int ret2;
5142
5143 preempt_disable();
5144 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5145 preempt_enable();
5146
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005147 if (ret2 != XDP_PASS) {
5148 ret = NET_RX_DROP;
5149 goto out;
5150 }
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005151 skb_reset_mac_len(skb);
5152 }
5153
Patrick McHardy8ad227f2013-04-19 02:04:31 +00005154 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5155 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005156 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005157 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005158 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005159 }
5160
Willem de Bruijne7246e12017-01-07 17:06:35 -05005161 if (skb_skip_tc_classify(skb))
5162 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
David S. Miller9754e292013-02-14 15:57:38 -05005164 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005165 goto skip_taps;
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005168 if (pt_prev)
5169 ret = deliver_skb(skb, pt_prev, orig_dev);
5170 pt_prev = ptype;
5171 }
5172
5173 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5174 if (pt_prev)
5175 ret = deliver_skb(skb, pt_prev, orig_dev);
5176 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 }
5178
Mel Gormanb4b9e352012-07-31 16:44:26 -07005179skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005180#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005181 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005182 bool another = false;
5183
5184 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5185 &another);
5186 if (another)
5187 goto another_round;
Daniel Borkmann45771392015-04-10 23:07:54 +02005188 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005189 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005190
5191 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005192 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005193 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005194#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005195 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005196skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005197 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005198 goto drop;
5199
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005200 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005201 if (pt_prev) {
5202 ret = deliver_skb(skb, pt_prev, orig_dev);
5203 pt_prev = NULL;
5204 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005205 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005206 goto another_round;
5207 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005208 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005209 }
5210
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005211 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005212 if (rx_handler) {
5213 if (pt_prev) {
5214 ret = deliver_skb(skb, pt_prev, orig_dev);
5215 pt_prev = NULL;
5216 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005217 switch (rx_handler(&skb)) {
5218 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005219 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005220 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005221 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005222 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005223 case RX_HANDLER_EXACT:
5224 deliver_exact = true;
5225 case RX_HANDLER_PASS:
5226 break;
5227 default:
5228 BUG();
5229 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +03005232 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005233check_vlan_id:
5234 if (skb_vlan_tag_get_id(skb)) {
5235 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5236 * find vlan device.
5237 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005238 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005239 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5240 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5241 /* Outer header is 802.1P with vlan 0, inner header is
5242 * 802.1Q or 802.1AD and vlan_do_receive() above could
5243 * not find vlan dev for vlan id 0.
5244 */
5245 __vlan_hwaccel_clear_tag(skb);
5246 skb = skb_vlan_untag(skb);
5247 if (unlikely(!skb))
5248 goto out;
5249 if (vlan_do_receive(&skb))
5250 /* After stripping off 802.1P header with vlan 0
5251 * vlan dev is found for inner header.
5252 */
5253 goto another_round;
5254 else if (unlikely(!skb))
5255 goto out;
5256 else
5257 /* We have stripped outer 802.1P vlan 0 header.
5258 * But could not find vlan dev.
5259 * check again for vlan id to set OTHERHOST.
5260 */
5261 goto check_vlan_id;
5262 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005263 /* Note: we might in the future use prio bits
5264 * and set skb->priority like in vlan_do_receive()
5265 * For the time being, just ignore Priority Code Point
5266 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005267 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005268 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005271
5272 /* deliver only exact match when indicated */
5273 if (likely(!deliver_exact)) {
5274 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5275 &ptype_base[ntohs(type) &
5276 PTYPE_HASH_MASK]);
5277 }
5278
5279 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5280 &orig_dev->ptype_specific);
5281
5282 if (unlikely(skb->dev != orig_dev)) {
5283 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5284 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 }
5286
5287 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005288 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005289 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005290 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005292drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005293 if (!deliver_exact)
5294 atomic_long_inc(&skb->dev->rx_dropped);
5295 else
5296 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 kfree_skb(skb);
5298 /* Jamal, now you will not able to escape explaining
5299 * me how you were going to use this. :-)
5300 */
5301 ret = NET_RX_DROP;
5302 }
5303
Julian Anastasov2c17d272015-07-09 09:59:10 +03005304out:
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005305 /* The invariant here is that if *ppt_prev is not NULL
5306 * then skb should also be non-NULL.
5307 *
5308 * Apparently *ppt_prev assignment above holds this invariant due to
5309 * skb dereferencing near it.
5310 */
5311 *pskb = skb;
David S. Miller9754e292013-02-14 15:57:38 -05005312 return ret;
5313}
5314
Edward Cree88eb1942018-07-02 16:13:56 +01005315static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5316{
5317 struct net_device *orig_dev = skb->dev;
5318 struct packet_type *pt_prev = NULL;
5319 int ret;
5320
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005321 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree88eb1942018-07-02 16:13:56 +01005322 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005323 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5324 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005325 return ret;
5326}
5327
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005328/**
5329 * netif_receive_skb_core - special purpose version of netif_receive_skb
5330 * @skb: buffer to process
5331 *
5332 * More direct receive version of netif_receive_skb(). It should
5333 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005334 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005335 *
5336 * This function may only be called from softirq context and interrupts
5337 * should be enabled.
5338 *
5339 * Return values (usually ignored):
5340 * NET_RX_SUCCESS: no congestion
5341 * NET_RX_DROP: packet was dropped
5342 */
5343int netif_receive_skb_core(struct sk_buff *skb)
5344{
5345 int ret;
5346
5347 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005348 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005349 rcu_read_unlock();
5350
5351 return ret;
5352}
5353EXPORT_SYMBOL(netif_receive_skb_core);
5354
Edward Cree88eb1942018-07-02 16:13:56 +01005355static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5356 struct packet_type *pt_prev,
5357 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005358{
5359 struct sk_buff *skb, *next;
5360
Edward Cree88eb1942018-07-02 16:13:56 +01005361 if (!pt_prev)
5362 return;
5363 if (list_empty(head))
5364 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005365 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005366 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5367 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005368 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005369 list_for_each_entry_safe(skb, next, head, list) {
5370 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005371 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005372 }
Edward Cree88eb1942018-07-02 16:13:56 +01005373}
5374
5375static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5376{
5377 /* Fast-path assumptions:
5378 * - There is no RX handler.
5379 * - Only one packet_type matches.
5380 * If either of these fails, we will end up doing some per-packet
5381 * processing in-line, then handling the 'last ptype' for the whole
5382 * sublist. This can't cause out-of-order delivery to any single ptype,
5383 * because the 'last ptype' must be constant across the sublist, and all
5384 * other ptypes are handled per-packet.
5385 */
5386 /* Current (common) ptype of sublist */
5387 struct packet_type *pt_curr = NULL;
5388 /* Current (common) orig_dev of sublist */
5389 struct net_device *od_curr = NULL;
5390 struct list_head sublist;
5391 struct sk_buff *skb, *next;
5392
Edward Cree9af86f92018-07-09 18:10:19 +01005393 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005394 list_for_each_entry_safe(skb, next, head, list) {
5395 struct net_device *orig_dev = skb->dev;
5396 struct packet_type *pt_prev = NULL;
5397
Edward Cree22f6bbb2018-12-04 17:37:57 +00005398 skb_list_del_init(skb);
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005399 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005400 if (!pt_prev)
5401 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005402 if (pt_curr != pt_prev || od_curr != orig_dev) {
5403 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005404 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5405 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005406 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005407 pt_curr = pt_prev;
5408 od_curr = orig_dev;
5409 }
Edward Cree9af86f92018-07-09 18:10:19 +01005410 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005411 }
5412
5413 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005414 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005415}
5416
David S. Miller9754e292013-02-14 15:57:38 -05005417static int __netif_receive_skb(struct sk_buff *skb)
5418{
5419 int ret;
5420
5421 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005422 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005423
5424 /*
5425 * PFMEMALLOC skbs are special, they should
5426 * - be delivered to SOCK_MEMALLOC sockets only
5427 * - stay away from userspace
5428 * - have bounded memory usage
5429 *
5430 * Use PF_MEMALLOC as this saves us from propagating the allocation
5431 * context down to all allocation sites.
5432 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005433 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005434 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005435 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005436 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005437 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return ret;
5440}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005441
Edward Cree4ce00172018-07-02 16:13:40 +01005442static void __netif_receive_skb_list(struct list_head *head)
5443{
5444 unsigned long noreclaim_flag = 0;
5445 struct sk_buff *skb, *next;
5446 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5447
5448 list_for_each_entry_safe(skb, next, head, list) {
5449 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5450 struct list_head sublist;
5451
5452 /* Handle the previous sublist */
5453 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005454 if (!list_empty(&sublist))
5455 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005456 pfmemalloc = !pfmemalloc;
5457 /* See comments in __netif_receive_skb */
5458 if (pfmemalloc)
5459 noreclaim_flag = memalloc_noreclaim_save();
5460 else
5461 memalloc_noreclaim_restore(noreclaim_flag);
5462 }
5463 }
5464 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005465 if (!list_empty(head))
5466 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005467 /* Restore pflags */
5468 if (pfmemalloc)
5469 memalloc_noreclaim_restore(noreclaim_flag);
5470}
5471
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005472static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005473{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005474 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005475 struct bpf_prog *new = xdp->prog;
5476 int ret = 0;
5477
David Ahernfbee97f2020-05-29 16:07:13 -06005478 if (new) {
5479 u32 i;
5480
YiFei Zhu984fe942020-09-15 16:45:39 -07005481 mutex_lock(&new->aux->used_maps_mutex);
5482
David Ahernfbee97f2020-05-29 16:07:13 -06005483 /* generic XDP does not work with DEVMAPs that can
5484 * have a bpf_prog installed on an entry
5485 */
5486 for (i = 0; i < new->aux->used_map_cnt; i++) {
YiFei Zhu984fe942020-09-15 16:45:39 -07005487 if (dev_map_can_have_prog(new->aux->used_maps[i]) ||
5488 cpu_map_prog_allowed(new->aux->used_maps[i])) {
5489 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005490 return -EINVAL;
YiFei Zhu984fe942020-09-15 16:45:39 -07005491 }
David Ahernfbee97f2020-05-29 16:07:13 -06005492 }
YiFei Zhu984fe942020-09-15 16:45:39 -07005493
5494 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005495 }
5496
David S. Millerb5cdae32017-04-18 15:36:58 -04005497 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005498 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005499 rcu_assign_pointer(dev->xdp_prog, new);
5500 if (old)
5501 bpf_prog_put(old);
5502
5503 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005504 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005505 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005506 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005507 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005508 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005509 }
5510 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005511
David S. Millerb5cdae32017-04-18 15:36:58 -04005512 default:
5513 ret = -EINVAL;
5514 break;
5515 }
5516
5517 return ret;
5518}
5519
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005520static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005521{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005522 int ret;
5523
Eric Dumazet588f0332011-11-15 04:12:55 +00005524 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005525
Richard Cochranc1f19b52010-07-17 08:49:36 +00005526 if (skb_defer_rx_timestamp(skb))
5527 return NET_RX_SUCCESS;
5528
John Fastabendbbbe2112017-09-08 14:00:30 -07005529 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005530#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005531 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005532 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005533 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005534
Eric Dumazet3b098e22010-05-15 23:57:10 -07005535 if (cpu >= 0) {
5536 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5537 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005538 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005539 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005540 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005541#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005542 ret = __netif_receive_skb(skb);
5543 rcu_read_unlock();
5544 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005545}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005546
Edward Cree7da517a2018-07-02 16:13:24 +01005547static void netif_receive_skb_list_internal(struct list_head *head)
5548{
Edward Cree7da517a2018-07-02 16:13:24 +01005549 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005550 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005551
Edward Cree8c057ef2018-07-09 18:09:54 +01005552 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005553 list_for_each_entry_safe(skb, next, head, list) {
5554 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005555 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005556 if (!skb_defer_rx_timestamp(skb))
5557 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005558 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005559 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005560
Edward Cree7da517a2018-07-02 16:13:24 +01005561 rcu_read_lock();
5562#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005563 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005564 list_for_each_entry_safe(skb, next, head, list) {
5565 struct rps_dev_flow voidflow, *rflow = &voidflow;
5566 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5567
5568 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005569 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005570 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005571 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005572 }
5573 }
5574 }
5575#endif
5576 __netif_receive_skb_list(head);
5577 rcu_read_unlock();
5578}
5579
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005580/**
5581 * netif_receive_skb - process receive buffer from network
5582 * @skb: buffer to process
5583 *
5584 * netif_receive_skb() is the main receive data processing function.
5585 * It always succeeds. The buffer may be dropped during processing
5586 * for congestion control or by the protocol layers.
5587 *
5588 * This function may only be called from softirq context and interrupts
5589 * should be enabled.
5590 *
5591 * Return values (usually ignored):
5592 * NET_RX_SUCCESS: no congestion
5593 * NET_RX_DROP: packet was dropped
5594 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005595int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005596{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005597 int ret;
5598
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005599 trace_netif_receive_skb_entry(skb);
5600
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005601 ret = netif_receive_skb_internal(skb);
5602 trace_netif_receive_skb_exit(ret);
5603
5604 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005605}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005606EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Edward Creef6ad8c12018-07-02 16:12:45 +01005608/**
5609 * netif_receive_skb_list - process many receive buffers from network
5610 * @head: list of skbs to process.
5611 *
Edward Cree7da517a2018-07-02 16:13:24 +01005612 * Since return value of netif_receive_skb() is normally ignored, and
5613 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005614 *
5615 * This function may only be called from softirq context and interrupts
5616 * should be enabled.
5617 */
5618void netif_receive_skb_list(struct list_head *head)
5619{
Edward Cree7da517a2018-07-02 16:13:24 +01005620 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005621
Edward Creeb9f463d2018-07-02 16:14:44 +01005622 if (list_empty(head))
5623 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005624 if (trace_netif_receive_skb_list_entry_enabled()) {
5625 list_for_each_entry(skb, head, list)
5626 trace_netif_receive_skb_list_entry(skb);
5627 }
Edward Cree7da517a2018-07-02 16:13:24 +01005628 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005629 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005630}
5631EXPORT_SYMBOL(netif_receive_skb_list);
5632
Wei Yongjunce1e2a72020-07-13 22:23:44 +08005633static DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005634
5635/* Network device is going away, flush any packets still pending */
5636static void flush_backlog(struct work_struct *work)
5637{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005638 struct sk_buff *skb, *tmp;
5639 struct softnet_data *sd;
5640
5641 local_bh_disable();
5642 sd = this_cpu_ptr(&softnet_data);
5643
5644 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005645 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005646 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005647 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005648 __skb_unlink(skb, &sd->input_pkt_queue);
Subash Abhinov Kasiviswanathan7df5cb752020-07-23 11:31:48 -06005649 dev_kfree_skb_irq(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005650 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005651 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005652 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005653 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005654 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005655
5656 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005657 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005658 __skb_unlink(skb, &sd->process_queue);
5659 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005660 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005661 }
5662 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005663 local_bh_enable();
5664}
5665
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005666static bool flush_required(int cpu)
5667{
5668#if IS_ENABLED(CONFIG_RPS)
5669 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5670 bool do_flush;
5671
5672 local_irq_disable();
5673 rps_lock(sd);
5674
5675 /* as insertion into process_queue happens with the rps lock held,
5676 * process_queue access may race only with dequeue
5677 */
5678 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5679 !skb_queue_empty_lockless(&sd->process_queue);
5680 rps_unlock(sd);
5681 local_irq_enable();
5682
5683 return do_flush;
5684#endif
5685 /* without RPS we can't safely check input_pkt_queue: during a
5686 * concurrent remote skb_queue_splice() we can detect as empty both
5687 * input_pkt_queue and process_queue even if the latter could end-up
5688 * containing a lot of packets.
5689 */
5690 return true;
5691}
5692
Eric Dumazet41852492016-08-26 12:50:39 -07005693static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005694{
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005695 static cpumask_t flush_cpus;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005696 unsigned int cpu;
5697
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005698 /* since we are under rtnl lock protection we can use static data
5699 * for the cpumask and avoid allocating on stack the possibly
5700 * large mask
5701 */
5702 ASSERT_RTNL();
5703
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005704 get_online_cpus();
5705
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005706 cpumask_clear(&flush_cpus);
5707 for_each_online_cpu(cpu) {
5708 if (flush_required(cpu)) {
5709 queue_work_on(cpu, system_highpri_wq,
5710 per_cpu_ptr(&flush_works, cpu));
5711 cpumask_set_cpu(cpu, &flush_cpus);
5712 }
5713 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005714
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005715 /* we can have in flight packet[s] on the cpus we are not flushing,
5716 * synchronize_net() in rollback_registered_many() will take care of
5717 * them
5718 */
5719 for_each_cpu(cpu, &flush_cpus)
Eric Dumazet41852492016-08-26 12:50:39 -07005720 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005721
5722 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005723}
5724
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005725/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5726static void gro_normal_list(struct napi_struct *napi)
5727{
5728 if (!napi->rx_count)
5729 return;
5730 netif_receive_skb_list_internal(&napi->rx_list);
5731 INIT_LIST_HEAD(&napi->rx_list);
5732 napi->rx_count = 0;
5733}
5734
5735/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5736 * pass the whole batch up to the stack.
5737 */
5738static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5739{
5740 list_add_tail(&skb->list, &napi->rx_list);
5741 if (++napi->rx_count >= gro_normal_batch)
5742 gro_normal_list(napi);
5743}
5744
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005745INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5746INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005747static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005748{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005749 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005750 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005751 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005752 int err = -ENOENT;
5753
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005754 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5755
Herbert Xufc59f9a2009-04-14 15:11:06 -07005756 if (NAPI_GRO_CB(skb)->count == 1) {
5757 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005758 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005759 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005760
5761 rcu_read_lock();
5762 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005763 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005764 continue;
5765
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005766 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5767 ipv6_gro_complete, inet_gro_complete,
5768 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005769 break;
5770 }
5771 rcu_read_unlock();
5772
5773 if (err) {
5774 WARN_ON(&ptype->list == head);
5775 kfree_skb(skb);
5776 return NET_RX_SUCCESS;
5777 }
5778
5779out:
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005780 gro_normal_one(napi, skb);
5781 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005782}
5783
Li RongQing6312fe72018-07-05 14:34:32 +08005784static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005785 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005786{
Li RongQing6312fe72018-07-05 14:34:32 +08005787 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005788 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005789
David Miller07d78362018-06-24 14:14:02 +09005790 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005791 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5792 return;
David S. Miller992cba72018-07-31 15:27:56 -07005793 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005794 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005795 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005796 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005797
5798 if (!napi->gro_hash[index].count)
5799 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005800}
David Miller07d78362018-06-24 14:14:02 +09005801
Li RongQing6312fe72018-07-05 14:34:32 +08005802/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005803 * youngest packets at the head of it.
5804 * Complete skbs in reverse order to reduce latencies.
5805 */
5806void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5807{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005808 unsigned long bitmask = napi->gro_bitmask;
5809 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005810
Eric Dumazet42519ed2018-11-21 11:39:28 -08005811 while ((i = ffs(bitmask)) != 0) {
5812 bitmask >>= i;
5813 base += i;
5814 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005815 }
David Miller07d78362018-06-24 14:14:02 +09005816}
Eric Dumazet86cac582010-08-31 18:25:32 +00005817EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005818
David Miller07d78362018-06-24 14:14:02 +09005819static struct list_head *gro_list_prepare(struct napi_struct *napi,
5820 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005821{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005822 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005823 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005824 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005825 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005826
Li RongQing6312fe72018-07-05 14:34:32 +08005827 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005828 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005829 unsigned long diffs;
5830
Tom Herbert0b4cec82014-01-15 08:58:06 -08005831 NAPI_GRO_CB(p)->flush = 0;
5832
5833 if (hash != skb_get_hash_raw(p)) {
5834 NAPI_GRO_CB(p)->same_flow = 0;
5835 continue;
5836 }
5837
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005838 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005839 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5840 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005841 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005842 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005843 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005844 if (maclen == ETH_HLEN)
5845 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005846 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005847 else if (!diffs)
5848 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005849 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005850 maclen);
5851 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005852 }
David Miller07d78362018-06-24 14:14:02 +09005853
5854 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005855}
5856
Jerry Chu299603e82013-12-11 20:53:45 -08005857static void skb_gro_reset_offset(struct sk_buff *skb)
5858{
5859 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5860 const skb_frag_t *frag0 = &pinfo->frags[0];
5861
5862 NAPI_GRO_CB(skb)->data_offset = 0;
5863 NAPI_GRO_CB(skb)->frag0 = NULL;
5864 NAPI_GRO_CB(skb)->frag0_len = 0;
5865
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005866 if (!skb_headlen(skb) && pinfo->nr_frags &&
Jerry Chu299603e82013-12-11 20:53:45 -08005867 !PageHighMem(skb_frag_page(frag0))) {
5868 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005869 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5870 skb_frag_size(frag0),
5871 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005872 }
5873}
5874
Eric Dumazeta50e2332014-03-29 21:28:21 -07005875static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5876{
5877 struct skb_shared_info *pinfo = skb_shinfo(skb);
5878
5879 BUG_ON(skb->end - skb->tail < grow);
5880
5881 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5882
5883 skb->data_len -= grow;
5884 skb->tail += grow;
5885
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005886 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005887 skb_frag_size_sub(&pinfo->frags[0], grow);
5888
5889 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5890 skb_frag_unref(skb, 0);
5891 memmove(pinfo->frags, pinfo->frags + 1,
5892 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5893 }
5894}
5895
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005896static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005897{
Li RongQing6312fe72018-07-05 14:34:32 +08005898 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005899
Li RongQing6312fe72018-07-05 14:34:32 +08005900 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005901
Li RongQing6312fe72018-07-05 14:34:32 +08005902 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005903 * impossible.
5904 */
5905 if (WARN_ON_ONCE(!oldest))
5906 return;
5907
Li RongQingd9f37d02018-07-13 14:41:36 +08005908 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5909 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005910 */
David S. Millerece23712018-10-28 10:35:12 -07005911 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005912 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09005913}
5914
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005915INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5916 struct sk_buff *));
5917INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5918 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005919static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005920{
Li RongQing6312fe72018-07-05 14:34:32 +08005921 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005922 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005923 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005924 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005925 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005926 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005927 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005928 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005929 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005930
David S. Millerb5cdae32017-04-18 15:36:58 -04005931 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005932 goto normal;
5933
David Miller07d78362018-06-24 14:14:02 +09005934 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005935
Herbert Xud565b0a2008-12-15 23:38:52 -08005936 rcu_read_lock();
5937 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005938 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005939 continue;
5940
Herbert Xu86911732009-01-29 14:19:50 +00005941 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005942 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005943 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005944 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005945 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005946 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005947 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005948 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005949 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005950 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005951
Tom Herbert662880f2014-08-27 21:26:56 -07005952 /* Setup for GRO checksum validation */
5953 switch (skb->ip_summed) {
5954 case CHECKSUM_COMPLETE:
5955 NAPI_GRO_CB(skb)->csum = skb->csum;
5956 NAPI_GRO_CB(skb)->csum_valid = 1;
5957 NAPI_GRO_CB(skb)->csum_cnt = 0;
5958 break;
5959 case CHECKSUM_UNNECESSARY:
5960 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5961 NAPI_GRO_CB(skb)->csum_valid = 0;
5962 break;
5963 default:
5964 NAPI_GRO_CB(skb)->csum_cnt = 0;
5965 NAPI_GRO_CB(skb)->csum_valid = 0;
5966 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005967
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005968 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5969 ipv6_gro_receive, inet_gro_receive,
5970 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005971 break;
5972 }
5973 rcu_read_unlock();
5974
5975 if (&ptype->list == head)
5976 goto normal;
5977
Masahiro Yamada45586c72020-02-03 17:37:45 -08005978 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01005979 ret = GRO_CONSUMED;
5980 goto ok;
5981 }
5982
Herbert Xu0da2afd52008-12-26 14:57:42 -08005983 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005984 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005985
Herbert Xud565b0a2008-12-15 23:38:52 -08005986 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005987 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005988 napi_gro_complete(napi, pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005989 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005990 }
5991
Herbert Xu0da2afd52008-12-26 14:57:42 -08005992 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005993 goto ok;
5994
Eric Dumazet600adc12014-01-09 14:12:19 -08005995 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005996 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005997
Li RongQing6312fe72018-07-05 14:34:32 +08005998 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005999 gro_flush_oldest(napi, gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08006000 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08006001 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08006002 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006003 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00006004 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07006005 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00006006 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09006007 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006008 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08006009
Herbert Xuad0f99042009-02-01 01:24:55 -08006010pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006011 grow = skb_gro_offset(skb) - skb_headlen(skb);
6012 if (grow > 0)
6013 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08006014ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08006015 if (napi->gro_hash[hash].count) {
6016 if (!test_bit(hash, &napi->gro_bitmask))
6017 __set_bit(hash, &napi->gro_bitmask);
6018 } else if (test_bit(hash, &napi->gro_bitmask)) {
6019 __clear_bit(hash, &napi->gro_bitmask);
6020 }
6021
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006022 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006023
6024normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08006025 ret = GRO_NORMAL;
6026 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08006027}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006028
Jerry Chubf5a7552014-01-07 10:23:19 -08006029struct packet_offload *gro_find_receive_by_type(__be16 type)
6030{
6031 struct list_head *offload_head = &offload_base;
6032 struct packet_offload *ptype;
6033
6034 list_for_each_entry_rcu(ptype, offload_head, list) {
6035 if (ptype->type != type || !ptype->callbacks.gro_receive)
6036 continue;
6037 return ptype;
6038 }
6039 return NULL;
6040}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006041EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08006042
6043struct packet_offload *gro_find_complete_by_type(__be16 type)
6044{
6045 struct list_head *offload_head = &offload_base;
6046 struct packet_offload *ptype;
6047
6048 list_for_each_entry_rcu(ptype, offload_head, list) {
6049 if (ptype->type != type || !ptype->callbacks.gro_complete)
6050 continue;
6051 return ptype;
6052 }
6053 return NULL;
6054}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006055EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006056
Michal Kubečeke44699d2017-06-29 11:13:36 +02006057static void napi_skb_free_stolen_head(struct sk_buff *skb)
6058{
6059 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02006060 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02006061 kmem_cache_free(skbuff_head_cache, skb);
6062}
6063
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006064static gro_result_t napi_skb_finish(struct napi_struct *napi,
6065 struct sk_buff *skb,
6066 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08006067{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006068 switch (ret) {
6069 case GRO_NORMAL:
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006070 gro_normal_one(napi, skb);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006071 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006072
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006073 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08006074 kfree_skb(skb);
6075 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006076
Eric Dumazetdaa86542012-04-19 07:07:40 +00006077 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02006078 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6079 napi_skb_free_stolen_head(skb);
6080 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00006081 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00006082 break;
6083
Ben Hutchings5b252f02009-10-29 07:17:09 +00006084 case GRO_HELD:
6085 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006086 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006087 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006088 }
6089
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006090 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006091}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006092
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006093gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006094{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006095 gro_result_t ret;
6096
Eric Dumazet93f93a42015-11-18 06:30:59 -08006097 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006098 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00006099
Eric Dumazeta50e2332014-03-29 21:28:21 -07006100 skb_gro_reset_offset(skb);
6101
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006102 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006103 trace_napi_gro_receive_exit(ret);
6104
6105 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006106}
6107EXPORT_SYMBOL(napi_gro_receive);
6108
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00006109static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006110{
Eric Dumazet93a35f52014-10-23 06:30:30 -07006111 if (unlikely(skb->pfmemalloc)) {
6112 consume_skb(skb);
6113 return;
6114 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006115 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00006116 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
6117 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01006118 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08006119 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08006120 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08006121
6122 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
6123 skb->pkt_type = PACKET_HOST;
6124
Jerry Chuc3caf112014-07-14 15:54:46 -07006125 skb->encapsulation = 0;
6126 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07006127 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02006128 skb_ext_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006129
6130 napi->skb = skb;
6131}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006132
Herbert Xu76620aa2009-04-16 02:02:07 -07006133struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08006134{
Herbert Xu5d38a072009-01-04 16:13:40 -08006135 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08006136
6137 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08006138 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08006139 if (skb) {
6140 napi->skb = skb;
6141 skb_mark_napi_id(skb, napi);
6142 }
Herbert Xu5d38a072009-01-04 16:13:40 -08006143 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006144 return skb;
6145}
Herbert Xu76620aa2009-04-16 02:02:07 -07006146EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006147
Eric Dumazeta50e2332014-03-29 21:28:21 -07006148static gro_result_t napi_frags_finish(struct napi_struct *napi,
6149 struct sk_buff *skb,
6150 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006151{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006152 switch (ret) {
6153 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006154 case GRO_HELD:
6155 __skb_push(skb, ETH_HLEN);
6156 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01006157 if (ret == GRO_NORMAL)
6158 gro_normal_one(napi, skb);
Herbert Xu86911732009-01-29 14:19:50 +00006159 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006160
6161 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006162 napi_reuse_skb(napi, skb);
6163 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006164
Michal Kubečeke44699d2017-06-29 11:13:36 +02006165 case GRO_MERGED_FREE:
6166 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6167 napi_skb_free_stolen_head(skb);
6168 else
6169 napi_reuse_skb(napi, skb);
6170 break;
6171
Ben Hutchings5b252f02009-10-29 07:17:09 +00006172 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006173 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006174 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006175 }
6176
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006177 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006178}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006179
Eric Dumazeta50e2332014-03-29 21:28:21 -07006180/* Upper GRO stack assumes network header starts at gro_offset=0
6181 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6182 * We copy ethernet header into skb->data to have a common layout.
6183 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00006184static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006185{
Herbert Xu76620aa2009-04-16 02:02:07 -07006186 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006187 const struct ethhdr *eth;
6188 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07006189
6190 napi->skb = NULL;
6191
Eric Dumazeta50e2332014-03-29 21:28:21 -07006192 skb_reset_mac_header(skb);
6193 skb_gro_reset_offset(skb);
6194
Eric Dumazeta50e2332014-03-29 21:28:21 -07006195 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6196 eth = skb_gro_header_slow(skb, hlen, 0);
6197 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006198 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6199 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006200 napi_reuse_skb(napi, skb);
6201 return NULL;
6202 }
6203 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006204 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006205 gro_pull_from_frag0(skb, hlen);
6206 NAPI_GRO_CB(skb)->frag0 += hlen;
6207 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006208 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006209 __skb_pull(skb, hlen);
6210
6211 /*
6212 * This works because the only protocols we care about don't require
6213 * special handling.
6214 * We'll fix it up properly in napi_frags_finish()
6215 */
6216 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006217
Herbert Xu76620aa2009-04-16 02:02:07 -07006218 return skb;
6219}
Herbert Xu76620aa2009-04-16 02:02:07 -07006220
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006221gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006222{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006223 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006224 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006225
6226 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006227 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08006228
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006229 trace_napi_gro_frags_entry(skb);
6230
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006231 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6232 trace_napi_gro_frags_exit(ret);
6233
6234 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006235}
6236EXPORT_SYMBOL(napi_gro_frags);
6237
Tom Herbert573e8fc2014-08-22 13:33:47 -07006238/* Compute the checksum from gro_offset and return the folded value
6239 * after adding in any pseudo checksum.
6240 */
6241__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6242{
6243 __wsum wsum;
6244 __sum16 sum;
6245
6246 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6247
6248 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6249 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006250 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006251 if (likely(!sum)) {
6252 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6253 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006254 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006255 }
6256
6257 NAPI_GRO_CB(skb)->csum = wsum;
6258 NAPI_GRO_CB(skb)->csum_valid = 1;
6259
6260 return sum;
6261}
6262EXPORT_SYMBOL(__skb_gro_checksum_complete);
6263
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306264static void net_rps_send_ipi(struct softnet_data *remsd)
6265{
6266#ifdef CONFIG_RPS
6267 while (remsd) {
6268 struct softnet_data *next = remsd->rps_ipi_next;
6269
6270 if (cpu_online(remsd->cpu))
6271 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6272 remsd = next;
6273 }
6274#endif
6275}
6276
Eric Dumazete326bed2010-04-22 00:22:45 -07006277/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006278 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006279 * Note: called with local irq disabled, but exits with local irq enabled.
6280 */
6281static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6282{
6283#ifdef CONFIG_RPS
6284 struct softnet_data *remsd = sd->rps_ipi_list;
6285
6286 if (remsd) {
6287 sd->rps_ipi_list = NULL;
6288
6289 local_irq_enable();
6290
6291 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306292 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006293 } else
6294#endif
6295 local_irq_enable();
6296}
6297
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006298static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6299{
6300#ifdef CONFIG_RPS
6301 return sd->rps_ipi_list != NULL;
6302#else
6303 return false;
6304#endif
6305}
6306
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006307static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006309 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006310 bool again = true;
6311 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
Eric Dumazete326bed2010-04-22 00:22:45 -07006313 /* Check if we have pending ipi, its better to send them now,
6314 * not waiting net_rx_action() end.
6315 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006316 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006317 local_irq_disable();
6318 net_rps_action_and_irq_enable(sd);
6319 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006320
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006321 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006322 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
Changli Gao6e7676c2010-04-27 15:07:33 -07006325 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006326 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006327 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006328 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006329 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006330 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006331 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006332
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006335 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006336 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006337 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006338 /*
6339 * Inline a custom version of __napi_complete().
6340 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006341 * and NAPI_STATE_SCHED is the only possible flag set
6342 * on backlog.
6343 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006344 * and we dont need an smp_mb() memory barrier.
6345 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006346 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006347 again = false;
6348 } else {
6349 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6350 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006351 }
6352 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006353 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006356 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357}
6358
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006359/**
6360 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006361 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006362 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006363 * The entry's receive function will be scheduled to run.
6364 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006365 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006366void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006367{
6368 unsigned long flags;
6369
6370 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006371 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006372 local_irq_restore(flags);
6373}
6374EXPORT_SYMBOL(__napi_schedule);
6375
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006376/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006377 * napi_schedule_prep - check if napi can be scheduled
6378 * @n: napi context
6379 *
6380 * Test if NAPI routine is already running, and if not mark
Jonathan Neuschäferee1a4c82020-09-05 20:32:18 +02006381 * it as running. This is used as a condition variable to
Eric Dumazet39e6c822017-02-28 10:34:50 -08006382 * insure only one NAPI poll instance runs. We also make
6383 * sure there is no pending NAPI disable.
6384 */
6385bool napi_schedule_prep(struct napi_struct *n)
6386{
6387 unsigned long val, new;
6388
6389 do {
6390 val = READ_ONCE(n->state);
6391 if (unlikely(val & NAPIF_STATE_DISABLE))
6392 return false;
6393 new = val | NAPIF_STATE_SCHED;
6394
6395 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6396 * This was suggested by Alexander Duyck, as compiler
6397 * emits better code than :
6398 * if (val & NAPIF_STATE_SCHED)
6399 * new |= NAPIF_STATE_MISSED;
6400 */
6401 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6402 NAPIF_STATE_MISSED;
6403 } while (cmpxchg(&n->state, val, new) != val);
6404
6405 return !(val & NAPIF_STATE_SCHED);
6406}
6407EXPORT_SYMBOL(napi_schedule_prep);
6408
6409/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006410 * __napi_schedule_irqoff - schedule for receive
6411 * @n: entry to schedule
6412 *
6413 * Variant of __napi_schedule() assuming hard irqs are masked
6414 */
6415void __napi_schedule_irqoff(struct napi_struct *n)
6416{
6417 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6418}
6419EXPORT_SYMBOL(__napi_schedule_irqoff);
6420
Eric Dumazet364b6052016-11-15 10:15:13 -08006421bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006422{
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006423 unsigned long flags, val, new, timeout = 0;
6424 bool ret = true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006425
6426 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006427 * 1) Don't let napi dequeue from the cpu poll list
6428 * just in case its running on a different cpu.
6429 * 2) If we are busy polling, do nothing here, we have
6430 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006431 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006432 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6433 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006434 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006435
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006436 if (work_done) {
6437 if (n->gro_bitmask)
Eric Dumazet7e417a62020-04-22 09:13:28 -07006438 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6439 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006440 }
6441 if (n->defer_hard_irqs_count > 0) {
6442 n->defer_hard_irqs_count--;
Eric Dumazet7e417a62020-04-22 09:13:28 -07006443 timeout = READ_ONCE(n->dev->gro_flush_timeout);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006444 if (timeout)
6445 ret = false;
6446 }
6447 if (n->gro_bitmask) {
Paolo Abeni605108a2018-11-21 18:21:35 +01006448 /* When the NAPI instance uses a timeout and keeps postponing
6449 * it, we need to bound somehow the time packets are kept in
6450 * the GRO layer
6451 */
6452 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006453 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006454
6455 gro_normal_list(n);
6456
Eric Dumazet02c16022017-02-04 15:25:02 -08006457 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006458 /* If n->poll_list is not empty, we need to mask irqs */
6459 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006460 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006461 local_irq_restore(flags);
6462 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006463
6464 do {
6465 val = READ_ONCE(n->state);
6466
6467 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6468
Björn Töpel7fd32532020-11-30 19:51:56 +01006469 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
6470 NAPIF_STATE_PREFER_BUSY_POLL);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006471
6472 /* If STATE_MISSED was set, leave STATE_SCHED set,
6473 * because we will call napi->poll() one more time.
6474 * This C code was suggested by Alexander Duyck to help gcc.
6475 */
6476 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6477 NAPIF_STATE_SCHED;
6478 } while (cmpxchg(&n->state, val, new) != val);
6479
6480 if (unlikely(val & NAPIF_STATE_MISSED)) {
6481 __napi_schedule(n);
6482 return false;
6483 }
6484
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006485 if (timeout)
6486 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6487 HRTIMER_MODE_REL_PINNED);
6488 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006489}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006490EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006491
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006492/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006493static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006494{
6495 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6496 struct napi_struct *napi;
6497
6498 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6499 if (napi->napi_id == napi_id)
6500 return napi;
6501
6502 return NULL;
6503}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006504
6505#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006506
Björn Töpel7fd32532020-11-30 19:51:56 +01006507static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
Eric Dumazet217f6972016-11-15 10:15:11 -08006508{
Björn Töpel7fd32532020-11-30 19:51:56 +01006509 if (!skip_schedule) {
6510 gro_normal_list(napi);
6511 __napi_schedule(napi);
6512 return;
6513 }
6514
6515 if (napi->gro_bitmask) {
6516 /* flush too old packets
6517 * If HZ < 1000, flush all packets.
6518 */
6519 napi_gro_flush(napi, HZ >= 1000);
6520 }
6521
6522 gro_normal_list(napi);
6523 clear_bit(NAPI_STATE_SCHED, &napi->state);
6524}
6525
Björn Töpel7c951caf2020-11-30 19:51:57 +01006526static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll,
6527 u16 budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006528{
6529 bool skip_schedule = false;
6530 unsigned long timeout;
Eric Dumazet217f6972016-11-15 10:15:11 -08006531 int rc;
6532
Eric Dumazet39e6c822017-02-28 10:34:50 -08006533 /* Busy polling means there is a high chance device driver hard irq
6534 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6535 * set in napi_schedule_prep().
6536 * Since we are about to call napi->poll() once more, we can safely
6537 * clear NAPI_STATE_MISSED.
6538 *
6539 * Note: x86 could use a single "lock and ..." instruction
6540 * to perform these two clear_bit()
6541 */
6542 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006543 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6544
6545 local_bh_disable();
6546
Björn Töpel7fd32532020-11-30 19:51:56 +01006547 if (prefer_busy_poll) {
6548 napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
6549 timeout = READ_ONCE(napi->dev->gro_flush_timeout);
6550 if (napi->defer_hard_irqs_count && timeout) {
6551 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6552 skip_schedule = true;
6553 }
6554 }
6555
Eric Dumazet217f6972016-11-15 10:15:11 -08006556 /* All we really want here is to re-enable device interrupts.
6557 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6558 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006559 rc = napi->poll(napi, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006560 /* We can't gro_normal_list() here, because napi->poll() might have
6561 * rearmed the napi (napi_complete_done()) in which case it could
6562 * already be running on another CPU.
6563 */
Björn Töpel7c951caf2020-11-30 19:51:57 +01006564 trace_napi_poll(napi, rc, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006565 netpoll_poll_unlock(have_poll_lock);
Björn Töpel7c951caf2020-11-30 19:51:57 +01006566 if (rc == budget)
Björn Töpel7fd32532020-11-30 19:51:56 +01006567 __busy_poll_stop(napi, skip_schedule);
Eric Dumazet217f6972016-11-15 10:15:11 -08006568 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006569}
6570
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006571void napi_busy_loop(unsigned int napi_id,
6572 bool (*loop_end)(void *, unsigned long),
Björn Töpel7c951caf2020-11-30 19:51:57 +01006573 void *loop_end_arg, bool prefer_busy_poll, u16 budget)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006574{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006575 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006576 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006577 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006578 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006579
6580restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006581 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006582
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006583 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006584
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006585 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006586 if (!napi)
6587 goto out;
6588
Eric Dumazet217f6972016-11-15 10:15:11 -08006589 preempt_disable();
6590 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006591 int work = 0;
6592
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006593 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006594 if (!napi_poll) {
6595 unsigned long val = READ_ONCE(napi->state);
6596
6597 /* If multiple threads are competing for this napi,
6598 * we avoid dirtying napi->state as much as we can.
6599 */
6600 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
Björn Töpel7fd32532020-11-30 19:51:56 +01006601 NAPIF_STATE_IN_BUSY_POLL)) {
6602 if (prefer_busy_poll)
6603 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006604 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006605 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006606 if (cmpxchg(&napi->state, val,
6607 val | NAPIF_STATE_IN_BUSY_POLL |
Björn Töpel7fd32532020-11-30 19:51:56 +01006608 NAPIF_STATE_SCHED) != val) {
6609 if (prefer_busy_poll)
6610 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006611 goto count;
Björn Töpel7fd32532020-11-30 19:51:56 +01006612 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006613 have_poll_lock = netpoll_poll_lock(napi);
6614 napi_poll = napi->poll;
6615 }
Björn Töpel7c951caf2020-11-30 19:51:57 +01006616 work = napi_poll(napi, budget);
6617 trace_napi_poll(napi, work, budget);
Edward Cree323ebb62019-08-06 14:53:55 +01006618 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006619count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006620 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006621 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006622 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006623 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006624
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006625 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006626 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006627
Eric Dumazet217f6972016-11-15 10:15:11 -08006628 if (unlikely(need_resched())) {
6629 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006630 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006631 preempt_enable();
6632 rcu_read_unlock();
6633 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006634 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006635 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006636 goto restart;
6637 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006638 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006639 }
6640 if (napi_poll)
Björn Töpel7c951caf2020-11-30 19:51:57 +01006641 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006642 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006643out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006644 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006645}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006646EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006647
6648#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006649
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006650static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006651{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006652 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006653 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006654
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006655 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006656
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006657 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006658 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006659 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6660 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006661 } while (napi_by_id(napi_gen_id));
6662 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006663
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006664 hlist_add_head_rcu(&napi->napi_hash_node,
6665 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006666
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006667 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006668}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006669
6670/* Warning : caller is responsible to make sure rcu grace period
6671 * is respected before freeing memory containing @napi
6672 */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006673static void napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006674{
6675 spin_lock(&napi_hash_lock);
6676
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006677 hlist_del_init_rcu(&napi->napi_hash_node);
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006678
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006679 spin_unlock(&napi_hash_lock);
6680}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006681
Eric Dumazet3b47d302014-11-06 21:09:44 -08006682static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6683{
6684 struct napi_struct *napi;
6685
6686 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006687
6688 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6689 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6690 */
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006691 if (!napi_disable_pending(napi) &&
Björn Töpel7fd32532020-11-30 19:51:56 +01006692 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6693 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006694 __napi_schedule_irqoff(napi);
Björn Töpel7fd32532020-11-30 19:51:56 +01006695 }
Eric Dumazet3b47d302014-11-06 21:09:44 -08006696
6697 return HRTIMER_NORESTART;
6698}
6699
David S. Miller7c4ec742018-07-20 23:37:55 -07006700static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006701{
David Miller07d78362018-06-24 14:14:02 +09006702 int i;
6703
Li RongQing6312fe72018-07-05 14:34:32 +08006704 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6705 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6706 napi->gro_hash[i].count = 0;
6707 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006708 napi->gro_bitmask = 0;
6709}
6710
6711void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6712 int (*poll)(struct napi_struct *, int), int weight)
6713{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006714 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6715 return;
6716
David S. Miller7c4ec742018-07-20 23:37:55 -07006717 INIT_LIST_HEAD(&napi->poll_list);
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006718 INIT_HLIST_NODE(&napi->napi_hash_node);
David S. Miller7c4ec742018-07-20 23:37:55 -07006719 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6720 napi->timer.function = napi_watchdog;
6721 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006722 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006723 INIT_LIST_HEAD(&napi->rx_list);
6724 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006725 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006726 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006727 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6728 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006729 napi->weight = weight;
Herbert Xud565b0a2008-12-15 23:38:52 -08006730 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006731#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006732 napi->poll_owner = -1;
6733#endif
6734 set_bit(NAPI_STATE_SCHED, &napi->state);
Jakub Kicinski96e97bc2020-08-26 12:40:06 -07006735 set_bit(NAPI_STATE_NPSVC, &napi->state);
6736 list_add_rcu(&napi->dev_list, &dev->napi_list);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006737 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006738}
6739EXPORT_SYMBOL(netif_napi_add);
6740
Eric Dumazet3b47d302014-11-06 21:09:44 -08006741void napi_disable(struct napi_struct *n)
6742{
6743 might_sleep();
6744 set_bit(NAPI_STATE_DISABLE, &n->state);
6745
6746 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6747 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006748 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6749 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006750
6751 hrtimer_cancel(&n->timer);
6752
Björn Töpel7fd32532020-11-30 19:51:56 +01006753 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006754 clear_bit(NAPI_STATE_DISABLE, &n->state);
6755}
6756EXPORT_SYMBOL(napi_disable);
6757
David Miller07d78362018-06-24 14:14:02 +09006758static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006759{
David Miller07d78362018-06-24 14:14:02 +09006760 int i;
David Millerd4546c22018-06-24 14:13:49 +09006761
David Miller07d78362018-06-24 14:14:02 +09006762 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6763 struct sk_buff *skb, *n;
6764
Li RongQing6312fe72018-07-05 14:34:32 +08006765 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006766 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006767 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006768 }
David Millerd4546c22018-06-24 14:13:49 +09006769}
6770
Eric Dumazet93d05d42015-11-18 06:31:03 -08006771/* Must be called in process context */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006772void __netif_napi_del(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006773{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006774 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6775 return;
6776
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006777 napi_hash_del(napi);
Jakub Kicinski5251ef82020-09-09 10:37:53 -07006778 list_del_rcu(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006779 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006780
David Miller07d78362018-06-24 14:14:02 +09006781 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006782 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006783}
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006784EXPORT_SYMBOL(__netif_napi_del);
Herbert Xud565b0a2008-12-15 23:38:52 -08006785
Herbert Xu726ce702014-12-21 07:16:21 +11006786static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6787{
6788 void *have;
6789 int work, weight;
6790
6791 list_del_init(&n->poll_list);
6792
6793 have = netpoll_poll_lock(n);
6794
6795 weight = n->weight;
6796
6797 /* This NAPI_STATE_SCHED test is for avoiding a race
6798 * with netpoll's poll_napi(). Only the entity which
6799 * obtains the lock and sees NAPI_STATE_SCHED set will
6800 * actually make the ->poll() call. Therefore we avoid
6801 * accidentally calling ->poll() when NAPI is not scheduled.
6802 */
6803 work = 0;
6804 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6805 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006806 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006807 }
6808
Eric Dumazet427d5832020-06-17 09:40:51 -07006809 if (unlikely(work > weight))
6810 pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6811 n->poll, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006812
6813 if (likely(work < weight))
6814 goto out_unlock;
6815
6816 /* Drivers must not modify the NAPI state if they
6817 * consume the entire weight. In such cases this code
6818 * still "owns" the NAPI instance and therefore can
6819 * move the instance around on the list at-will.
6820 */
6821 if (unlikely(napi_disable_pending(n))) {
6822 napi_complete(n);
6823 goto out_unlock;
6824 }
6825
Björn Töpel7fd32532020-11-30 19:51:56 +01006826 /* The NAPI context has more processing work, but busy-polling
6827 * is preferred. Exit early.
6828 */
6829 if (napi_prefer_busy_poll(n)) {
6830 if (napi_complete_done(n, work)) {
6831 /* If timeout is not set, we need to make sure
6832 * that the NAPI is re-scheduled.
6833 */
6834 napi_schedule(n);
6835 }
6836 goto out_unlock;
6837 }
6838
Li RongQingd9f37d02018-07-13 14:41:36 +08006839 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006840 /* flush too old packets
6841 * If HZ < 1000, flush all packets.
6842 */
6843 napi_gro_flush(n, HZ >= 1000);
6844 }
6845
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006846 gro_normal_list(n);
6847
Herbert Xu001ce542014-12-21 07:16:22 +11006848 /* Some drivers may have called napi_schedule
6849 * prior to exhausting their budget.
6850 */
6851 if (unlikely(!list_empty(&n->poll_list))) {
6852 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6853 n->dev ? n->dev->name : "backlog");
6854 goto out_unlock;
6855 }
6856
Herbert Xu726ce702014-12-21 07:16:21 +11006857 list_add_tail(&n->poll_list, repoll);
6858
6859out_unlock:
6860 netpoll_poll_unlock(have);
6861
6862 return work;
6863}
6864
Emese Revfy0766f782016-06-20 20:42:34 +02006865static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006867 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006868 unsigned long time_limit = jiffies +
6869 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006870 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006871 LIST_HEAD(list);
6872 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006875 list_splice_init(&sd->poll_list, &list);
6876 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006878 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006879 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006881 if (list_empty(&list)) {
6882 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006883 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006884 break;
6885 }
6886
Herbert Xu6bd373e2014-12-21 07:16:24 +11006887 n = list_first_entry(&list, struct napi_struct, poll_list);
6888 budget -= napi_poll(n, &repoll);
6889
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006890 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006891 * Allow this to run for 2 jiffies since which will allow
6892 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006893 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006894 if (unlikely(budget <= 0 ||
6895 time_after_eq(jiffies, time_limit))) {
6896 sd->time_squeeze++;
6897 break;
6898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006900
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006901 local_irq_disable();
6902
6903 list_splice_tail_init(&sd->poll_list, &list);
6904 list_splice_tail(&repoll, &list);
6905 list_splice(&list, &sd->poll_list);
6906 if (!list_empty(&sd->poll_list))
6907 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6908
Eric Dumazete326bed2010-04-22 00:22:45 -07006909 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006910out:
6911 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
6913
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006914struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006915 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006916
6917 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006918 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006919
Taehee Yoo32b6d342019-10-21 18:47:56 +00006920 /* lookup ignore flag */
6921 bool ignore;
6922
Veaceslav Falico5d261912013-08-28 23:25:05 +02006923 /* counter for the number of times this device was added to us */
6924 u16 ref_nr;
6925
Veaceslav Falico402dae92013-09-25 09:20:09 +02006926 /* private field for the users */
6927 void *private;
6928
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006929 struct list_head list;
6930 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006931};
6932
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006933static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006934 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006935{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006936 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006937
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006938 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006939 if (adj->dev == adj_dev)
6940 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006941 }
6942 return NULL;
6943}
6944
Taehee Yooeff74232020-09-25 18:13:12 +00006945static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6946 struct netdev_nested_priv *priv)
David Ahernf1170fd2016-10-17 19:15:51 -07006947{
Taehee Yooeff74232020-09-25 18:13:12 +00006948 struct net_device *dev = (struct net_device *)priv->data;
David Ahernf1170fd2016-10-17 19:15:51 -07006949
6950 return upper_dev == dev;
6951}
6952
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006953/**
6954 * netdev_has_upper_dev - Check if device is linked to an upper device
6955 * @dev: device
6956 * @upper_dev: upper device to check
6957 *
6958 * Find out if a device is linked to specified upper device and return true
6959 * in case it is. Note that this checks only immediate upper device,
6960 * not through a complete stack of devices. The caller must hold the RTNL lock.
6961 */
6962bool netdev_has_upper_dev(struct net_device *dev,
6963 struct net_device *upper_dev)
6964{
Taehee Yooeff74232020-09-25 18:13:12 +00006965 struct netdev_nested_priv priv = {
6966 .data = (void *)upper_dev,
6967 };
6968
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006969 ASSERT_RTNL();
6970
Taehee Yoo32b6d342019-10-21 18:47:56 +00006971 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006972 &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006973}
6974EXPORT_SYMBOL(netdev_has_upper_dev);
6975
6976/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01006977 * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
David Ahern1a3f0602016-10-17 19:15:44 -07006978 * @dev: device
6979 * @upper_dev: upper device to check
6980 *
6981 * Find out if a device is linked to specified upper device and return true
6982 * in case it is. Note that this checks the entire upper device chain.
6983 * The caller must hold rcu lock.
6984 */
6985
David Ahern1a3f0602016-10-17 19:15:44 -07006986bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6987 struct net_device *upper_dev)
6988{
Taehee Yooeff74232020-09-25 18:13:12 +00006989 struct netdev_nested_priv priv = {
6990 .data = (void *)upper_dev,
6991 };
6992
Taehee Yoo32b6d342019-10-21 18:47:56 +00006993 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006994 &priv);
David Ahern1a3f0602016-10-17 19:15:44 -07006995}
6996EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6997
6998/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006999 * netdev_has_any_upper_dev - Check if device is linked to some device
7000 * @dev: device
7001 *
7002 * Find out if a device is linked to an upper device and return true in case
7003 * it is. The caller must hold the RTNL lock.
7004 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007005bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007006{
7007 ASSERT_RTNL();
7008
David Ahernf1170fd2016-10-17 19:15:51 -07007009 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007010}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02007011EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007012
7013/**
7014 * netdev_master_upper_dev_get - Get master upper device
7015 * @dev: device
7016 *
7017 * Find a master upper device and return pointer to it or NULL in case
7018 * it's not there. The caller must hold the RTNL lock.
7019 */
7020struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
7021{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007022 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007023
7024 ASSERT_RTNL();
7025
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007026 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007027 return NULL;
7028
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007029 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007030 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007031 if (likely(upper->master))
7032 return upper->dev;
7033 return NULL;
7034}
7035EXPORT_SYMBOL(netdev_master_upper_dev_get);
7036
Taehee Yoo32b6d342019-10-21 18:47:56 +00007037static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
7038{
7039 struct netdev_adjacent *upper;
7040
7041 ASSERT_RTNL();
7042
7043 if (list_empty(&dev->adj_list.upper))
7044 return NULL;
7045
7046 upper = list_first_entry(&dev->adj_list.upper,
7047 struct netdev_adjacent, list);
7048 if (likely(upper->master) && !upper->ignore)
7049 return upper->dev;
7050 return NULL;
7051}
7052
David Ahern0f524a82016-10-17 19:15:52 -07007053/**
7054 * netdev_has_any_lower_dev - Check if device is linked to some device
7055 * @dev: device
7056 *
7057 * Find out if a device is linked to a lower device and return true in case
7058 * it is. The caller must hold the RTNL lock.
7059 */
7060static bool netdev_has_any_lower_dev(struct net_device *dev)
7061{
7062 ASSERT_RTNL();
7063
7064 return !list_empty(&dev->adj_list.lower);
7065}
7066
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02007067void *netdev_adjacent_get_private(struct list_head *adj_list)
7068{
7069 struct netdev_adjacent *adj;
7070
7071 adj = list_entry(adj_list, struct netdev_adjacent, list);
7072
7073 return adj->private;
7074}
7075EXPORT_SYMBOL(netdev_adjacent_get_private);
7076
Veaceslav Falico31088a12013-09-25 09:20:12 +02007077/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04007078 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7079 * @dev: device
7080 * @iter: list_head ** of the current position
7081 *
7082 * Gets the next device from the dev's upper list, starting from iter
7083 * position. The caller must hold RCU read lock.
7084 */
7085struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7086 struct list_head **iter)
7087{
7088 struct netdev_adjacent *upper;
7089
7090 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7091
7092 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7093
7094 if (&upper->list == &dev->adj_list.upper)
7095 return NULL;
7096
7097 *iter = &upper->list;
7098
7099 return upper->dev;
7100}
7101EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7102
Taehee Yoo32b6d342019-10-21 18:47:56 +00007103static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7104 struct list_head **iter,
7105 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00007106{
7107 struct netdev_adjacent *upper;
7108
7109 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7110
7111 if (&upper->list == &dev->adj_list.upper)
7112 return NULL;
7113
7114 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007115 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007116
7117 return upper->dev;
7118}
7119
David Ahern1a3f0602016-10-17 19:15:44 -07007120static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7121 struct list_head **iter)
7122{
7123 struct netdev_adjacent *upper;
7124
7125 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7126
7127 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7128
7129 if (&upper->list == &dev->adj_list.upper)
7130 return NULL;
7131
7132 *iter = &upper->list;
7133
7134 return upper->dev;
7135}
7136
Taehee Yoo32b6d342019-10-21 18:47:56 +00007137static int __netdev_walk_all_upper_dev(struct net_device *dev,
7138 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007139 struct netdev_nested_priv *priv),
7140 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007141{
7142 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7143 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7144 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007145 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007146
7147 now = dev;
7148 iter = &dev->adj_list.upper;
7149
7150 while (1) {
7151 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007152 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007153 if (ret)
7154 return ret;
7155 }
7156
7157 next = NULL;
7158 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007159 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007160 if (!udev)
7161 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007162 if (ignore)
7163 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007164
7165 next = udev;
7166 niter = &udev->adj_list.upper;
7167 dev_stack[cur] = now;
7168 iter_stack[cur++] = iter;
7169 break;
7170 }
7171
7172 if (!next) {
7173 if (!cur)
7174 return 0;
7175 next = dev_stack[--cur];
7176 niter = iter_stack[cur];
7177 }
7178
7179 now = next;
7180 iter = niter;
7181 }
7182
7183 return 0;
7184}
7185
David Ahern1a3f0602016-10-17 19:15:44 -07007186int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7187 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007188 struct netdev_nested_priv *priv),
7189 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007190{
Taehee Yoo5343da42019-10-21 18:47:50 +00007191 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7192 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7193 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007194
Taehee Yoo5343da42019-10-21 18:47:50 +00007195 now = dev;
7196 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07007197
Taehee Yoo5343da42019-10-21 18:47:50 +00007198 while (1) {
7199 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007200 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007201 if (ret)
7202 return ret;
7203 }
7204
7205 next = NULL;
7206 while (1) {
7207 udev = netdev_next_upper_dev_rcu(now, &iter);
7208 if (!udev)
7209 break;
7210
7211 next = udev;
7212 niter = &udev->adj_list.upper;
7213 dev_stack[cur] = now;
7214 iter_stack[cur++] = iter;
7215 break;
7216 }
7217
7218 if (!next) {
7219 if (!cur)
7220 return 0;
7221 next = dev_stack[--cur];
7222 niter = iter_stack[cur];
7223 }
7224
7225 now = next;
7226 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007227 }
7228
7229 return 0;
7230}
7231EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7232
Taehee Yoo32b6d342019-10-21 18:47:56 +00007233static bool __netdev_has_upper_dev(struct net_device *dev,
7234 struct net_device *upper_dev)
7235{
Taehee Yooeff74232020-09-25 18:13:12 +00007236 struct netdev_nested_priv priv = {
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007237 .flags = 0,
Taehee Yooeff74232020-09-25 18:13:12 +00007238 .data = (void *)upper_dev,
7239 };
7240
Taehee Yoo32b6d342019-10-21 18:47:56 +00007241 ASSERT_RTNL();
7242
7243 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007244 &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007245}
7246
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007247/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02007248 * netdev_lower_get_next_private - Get the next ->private from the
7249 * lower neighbour list
7250 * @dev: device
7251 * @iter: list_head ** of the current position
7252 *
7253 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7254 * list, starting from iter position. The caller must hold either hold the
7255 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007256 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02007257 */
7258void *netdev_lower_get_next_private(struct net_device *dev,
7259 struct list_head **iter)
7260{
7261 struct netdev_adjacent *lower;
7262
7263 lower = list_entry(*iter, struct netdev_adjacent, list);
7264
7265 if (&lower->list == &dev->adj_list.lower)
7266 return NULL;
7267
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007268 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007269
7270 return lower->private;
7271}
7272EXPORT_SYMBOL(netdev_lower_get_next_private);
7273
7274/**
7275 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7276 * lower neighbour list, RCU
7277 * variant
7278 * @dev: device
7279 * @iter: list_head ** of the current position
7280 *
7281 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7282 * list, starting from iter position. The caller must hold RCU read lock.
7283 */
7284void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7285 struct list_head **iter)
7286{
7287 struct netdev_adjacent *lower;
7288
7289 WARN_ON_ONCE(!rcu_read_lock_held());
7290
7291 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7292
7293 if (&lower->list == &dev->adj_list.lower)
7294 return NULL;
7295
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007296 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007297
7298 return lower->private;
7299}
7300EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7301
7302/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007303 * netdev_lower_get_next - Get the next device from the lower neighbour
7304 * list
7305 * @dev: device
7306 * @iter: list_head ** of the current position
7307 *
7308 * Gets the next netdev_adjacent from the dev's lower neighbour
7309 * list, starting from iter position. The caller must hold RTNL lock or
7310 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007311 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007312 */
7313void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7314{
7315 struct netdev_adjacent *lower;
7316
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007317 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007318
7319 if (&lower->list == &dev->adj_list.lower)
7320 return NULL;
7321
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007322 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007323
7324 return lower->dev;
7325}
7326EXPORT_SYMBOL(netdev_lower_get_next);
7327
David Ahern1a3f0602016-10-17 19:15:44 -07007328static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7329 struct list_head **iter)
7330{
7331 struct netdev_adjacent *lower;
7332
David Ahern46b5ab12016-10-26 13:21:33 -07007333 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007334
7335 if (&lower->list == &dev->adj_list.lower)
7336 return NULL;
7337
David Ahern46b5ab12016-10-26 13:21:33 -07007338 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007339
7340 return lower->dev;
7341}
7342
Taehee Yoo32b6d342019-10-21 18:47:56 +00007343static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7344 struct list_head **iter,
7345 bool *ignore)
7346{
7347 struct netdev_adjacent *lower;
7348
7349 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7350
7351 if (&lower->list == &dev->adj_list.lower)
7352 return NULL;
7353
7354 *iter = &lower->list;
7355 *ignore = lower->ignore;
7356
7357 return lower->dev;
7358}
7359
David Ahern1a3f0602016-10-17 19:15:44 -07007360int netdev_walk_all_lower_dev(struct net_device *dev,
7361 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007362 struct netdev_nested_priv *priv),
7363 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007364{
Taehee Yoo5343da42019-10-21 18:47:50 +00007365 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7366 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7367 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007368
Taehee Yoo5343da42019-10-21 18:47:50 +00007369 now = dev;
7370 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007371
Taehee Yoo5343da42019-10-21 18:47:50 +00007372 while (1) {
7373 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007374 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007375 if (ret)
7376 return ret;
7377 }
7378
7379 next = NULL;
7380 while (1) {
7381 ldev = netdev_next_lower_dev(now, &iter);
7382 if (!ldev)
7383 break;
7384
7385 next = ldev;
7386 niter = &ldev->adj_list.lower;
7387 dev_stack[cur] = now;
7388 iter_stack[cur++] = iter;
7389 break;
7390 }
7391
7392 if (!next) {
7393 if (!cur)
7394 return 0;
7395 next = dev_stack[--cur];
7396 niter = iter_stack[cur];
7397 }
7398
7399 now = next;
7400 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007401 }
7402
7403 return 0;
7404}
7405EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7406
Taehee Yoo32b6d342019-10-21 18:47:56 +00007407static int __netdev_walk_all_lower_dev(struct net_device *dev,
7408 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007409 struct netdev_nested_priv *priv),
7410 struct netdev_nested_priv *priv)
Taehee Yoo32b6d342019-10-21 18:47:56 +00007411{
7412 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7413 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7414 int ret, cur = 0;
7415 bool ignore;
7416
7417 now = dev;
7418 iter = &dev->adj_list.lower;
7419
7420 while (1) {
7421 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007422 ret = fn(now, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007423 if (ret)
7424 return ret;
7425 }
7426
7427 next = NULL;
7428 while (1) {
7429 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7430 if (!ldev)
7431 break;
7432 if (ignore)
7433 continue;
7434
7435 next = ldev;
7436 niter = &ldev->adj_list.lower;
7437 dev_stack[cur] = now;
7438 iter_stack[cur++] = iter;
7439 break;
7440 }
7441
7442 if (!next) {
7443 if (!cur)
7444 return 0;
7445 next = dev_stack[--cur];
7446 niter = iter_stack[cur];
7447 }
7448
7449 now = next;
7450 iter = niter;
7451 }
7452
7453 return 0;
7454}
7455
Taehee Yoo7151aff2020-02-15 10:50:21 +00007456struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7457 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007458{
7459 struct netdev_adjacent *lower;
7460
7461 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7462 if (&lower->list == &dev->adj_list.lower)
7463 return NULL;
7464
7465 *iter = &lower->list;
7466
7467 return lower->dev;
7468}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007469EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007470
Taehee Yoo5343da42019-10-21 18:47:50 +00007471static u8 __netdev_upper_depth(struct net_device *dev)
7472{
7473 struct net_device *udev;
7474 struct list_head *iter;
7475 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007476 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007477
7478 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007479 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007480 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007481 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7482 if (ignore)
7483 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007484 if (max_depth < udev->upper_level)
7485 max_depth = udev->upper_level;
7486 }
7487
7488 return max_depth;
7489}
7490
7491static u8 __netdev_lower_depth(struct net_device *dev)
7492{
7493 struct net_device *ldev;
7494 struct list_head *iter;
7495 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007496 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007497
7498 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007499 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007500 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007501 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7502 if (ignore)
7503 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007504 if (max_depth < ldev->lower_level)
7505 max_depth = ldev->lower_level;
7506 }
7507
7508 return max_depth;
7509}
7510
Taehee Yooeff74232020-09-25 18:13:12 +00007511static int __netdev_update_upper_level(struct net_device *dev,
7512 struct netdev_nested_priv *__unused)
Taehee Yoo5343da42019-10-21 18:47:50 +00007513{
7514 dev->upper_level = __netdev_upper_depth(dev) + 1;
7515 return 0;
7516}
7517
Taehee Yooeff74232020-09-25 18:13:12 +00007518static int __netdev_update_lower_level(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007519 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007520{
7521 dev->lower_level = __netdev_lower_depth(dev) + 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007522
7523#ifdef CONFIG_LOCKDEP
7524 if (!priv)
7525 return 0;
7526
7527 if (priv->flags & NESTED_SYNC_IMM)
7528 dev->nested_level = dev->lower_level - 1;
7529 if (priv->flags & NESTED_SYNC_TODO)
7530 net_unlink_todo(dev);
7531#endif
Taehee Yoo5343da42019-10-21 18:47:50 +00007532 return 0;
7533}
7534
David Ahern1a3f0602016-10-17 19:15:44 -07007535int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7536 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007537 struct netdev_nested_priv *priv),
7538 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007539{
Taehee Yoo5343da42019-10-21 18:47:50 +00007540 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7541 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7542 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007543
Taehee Yoo5343da42019-10-21 18:47:50 +00007544 now = dev;
7545 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007546
Taehee Yoo5343da42019-10-21 18:47:50 +00007547 while (1) {
7548 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007549 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007550 if (ret)
7551 return ret;
7552 }
7553
7554 next = NULL;
7555 while (1) {
7556 ldev = netdev_next_lower_dev_rcu(now, &iter);
7557 if (!ldev)
7558 break;
7559
7560 next = ldev;
7561 niter = &ldev->adj_list.lower;
7562 dev_stack[cur] = now;
7563 iter_stack[cur++] = iter;
7564 break;
7565 }
7566
7567 if (!next) {
7568 if (!cur)
7569 return 0;
7570 next = dev_stack[--cur];
7571 niter = iter_stack[cur];
7572 }
7573
7574 now = next;
7575 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007576 }
7577
7578 return 0;
7579}
7580EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7581
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007582/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007583 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7584 * lower neighbour list, RCU
7585 * variant
7586 * @dev: device
7587 *
7588 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7589 * list. The caller must hold RCU read lock.
7590 */
7591void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7592{
7593 struct netdev_adjacent *lower;
7594
7595 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7596 struct netdev_adjacent, list);
7597 if (lower)
7598 return lower->private;
7599 return NULL;
7600}
7601EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7602
7603/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007604 * netdev_master_upper_dev_get_rcu - Get master upper device
7605 * @dev: device
7606 *
7607 * Find a master upper device and return pointer to it or NULL in case
7608 * it's not there. The caller must hold the RCU read lock.
7609 */
7610struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7611{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007612 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007613
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007614 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007615 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007616 if (upper && likely(upper->master))
7617 return upper->dev;
7618 return NULL;
7619}
7620EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7621
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307622static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007623 struct net_device *adj_dev,
7624 struct list_head *dev_list)
7625{
7626 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007627
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007628 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7629 "upper_%s" : "lower_%s", adj_dev->name);
7630 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7631 linkname);
7632}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307633static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007634 char *name,
7635 struct list_head *dev_list)
7636{
7637 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007638
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007639 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7640 "upper_%s" : "lower_%s", name);
7641 sysfs_remove_link(&(dev->dev.kobj), linkname);
7642}
7643
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007644static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7645 struct net_device *adj_dev,
7646 struct list_head *dev_list)
7647{
7648 return (dev_list == &dev->adj_list.upper ||
7649 dev_list == &dev->adj_list.lower) &&
7650 net_eq(dev_net(dev), dev_net(adj_dev));
7651}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007652
Veaceslav Falico5d261912013-08-28 23:25:05 +02007653static int __netdev_adjacent_dev_insert(struct net_device *dev,
7654 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007655 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007656 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007657{
7658 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007659 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007660
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007661 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007662
7663 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007664 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007665 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7666 dev->name, adj_dev->name, adj->ref_nr);
7667
Veaceslav Falico5d261912013-08-28 23:25:05 +02007668 return 0;
7669 }
7670
7671 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7672 if (!adj)
7673 return -ENOMEM;
7674
7675 adj->dev = adj_dev;
7676 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007677 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007678 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007679 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007680 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007681
David Ahern67b62f92016-10-17 19:15:53 -07007682 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7683 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007684
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007685 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007686 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007687 if (ret)
7688 goto free_adj;
7689 }
7690
Veaceslav Falico7863c052013-09-25 09:20:06 +02007691 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007692 if (master) {
7693 ret = sysfs_create_link(&(dev->dev.kobj),
7694 &(adj_dev->dev.kobj), "master");
7695 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007696 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007697
Veaceslav Falico7863c052013-09-25 09:20:06 +02007698 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007699 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007700 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007701 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007702
7703 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007704
Veaceslav Falico5831d662013-09-25 09:20:32 +02007705remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007706 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007707 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007708free_adj:
7709 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007710 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007711
7712 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007713}
7714
stephen hemminger1d143d92013-12-29 14:01:29 -08007715static void __netdev_adjacent_dev_remove(struct net_device *dev,
7716 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007717 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007718 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007719{
7720 struct netdev_adjacent *adj;
7721
David Ahern67b62f92016-10-17 19:15:53 -07007722 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7723 dev->name, adj_dev->name, ref_nr);
7724
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007725 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007726
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007727 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007728 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007729 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007730 WARN_ON(1);
7731 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007732 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007733
Andrew Collins93409032016-10-03 13:43:02 -06007734 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007735 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7736 dev->name, adj_dev->name, ref_nr,
7737 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007738 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007739 return;
7740 }
7741
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007742 if (adj->master)
7743 sysfs_remove_link(&(dev->dev.kobj), "master");
7744
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007745 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007746 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007747
Veaceslav Falico5d261912013-08-28 23:25:05 +02007748 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007749 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007750 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007751 dev_put(adj_dev);
7752 kfree_rcu(adj, rcu);
7753}
7754
stephen hemminger1d143d92013-12-29 14:01:29 -08007755static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7756 struct net_device *upper_dev,
7757 struct list_head *up_list,
7758 struct list_head *down_list,
7759 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007760{
7761 int ret;
7762
David Ahern790510d2016-10-17 19:15:43 -07007763 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007764 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007765 if (ret)
7766 return ret;
7767
David Ahern790510d2016-10-17 19:15:43 -07007768 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007769 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007770 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007771 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007772 return ret;
7773 }
7774
7775 return 0;
7776}
7777
stephen hemminger1d143d92013-12-29 14:01:29 -08007778static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7779 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007780 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007781 struct list_head *up_list,
7782 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007783{
Andrew Collins93409032016-10-03 13:43:02 -06007784 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7785 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007786}
7787
stephen hemminger1d143d92013-12-29 14:01:29 -08007788static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7789 struct net_device *upper_dev,
7790 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007791{
David Ahernf1170fd2016-10-17 19:15:51 -07007792 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7793 &dev->adj_list.upper,
7794 &upper_dev->adj_list.lower,
7795 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007796}
7797
stephen hemminger1d143d92013-12-29 14:01:29 -08007798static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7799 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007800{
Andrew Collins93409032016-10-03 13:43:02 -06007801 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007802 &dev->adj_list.upper,
7803 &upper_dev->adj_list.lower);
7804}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007805
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007806static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007807 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007808 void *upper_priv, void *upper_info,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007809 struct netdev_nested_priv *priv,
David Ahern42ab19e2017-10-04 17:48:47 -07007810 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007811{
David Ahern51d0c0472017-10-04 17:48:45 -07007812 struct netdev_notifier_changeupper_info changeupper_info = {
7813 .info = {
7814 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007815 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007816 },
7817 .upper_dev = upper_dev,
7818 .master = master,
7819 .linking = true,
7820 .upper_info = upper_info,
7821 };
Mike Manning50d629e2018-02-26 23:49:30 +00007822 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007823 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007824
7825 ASSERT_RTNL();
7826
7827 if (dev == upper_dev)
7828 return -EBUSY;
7829
7830 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007831 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007832 return -EBUSY;
7833
Taehee Yoo5343da42019-10-21 18:47:50 +00007834 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7835 return -EMLINK;
7836
Mike Manning50d629e2018-02-26 23:49:30 +00007837 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007838 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007839 return -EEXIST;
7840 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007841 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007842 if (master_dev)
7843 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7844 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007845
David Ahern51d0c0472017-10-04 17:48:45 -07007846 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007847 &changeupper_info.info);
7848 ret = notifier_to_errno(ret);
7849 if (ret)
7850 return ret;
7851
Jiri Pirko6dffb042015-12-03 12:12:10 +01007852 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007853 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007854 if (ret)
7855 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007856
David Ahern51d0c0472017-10-04 17:48:45 -07007857 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007858 &changeupper_info.info);
7859 ret = notifier_to_errno(ret);
7860 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007861 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007862
Taehee Yoo5343da42019-10-21 18:47:50 +00007863 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007864 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007865
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007866 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007867 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007868 priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007869
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007870 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007871
David Ahernf1170fd2016-10-17 19:15:51 -07007872rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007873 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007874
7875 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007876}
7877
7878/**
7879 * netdev_upper_dev_link - Add a link to the upper device
7880 * @dev: device
7881 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007882 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007883 *
7884 * Adds a link to device which is upper to this one. The caller must hold
7885 * the RTNL lock. On a failure a negative errno code is returned.
7886 * On success the reference counts are adjusted and the function
7887 * returns zero.
7888 */
7889int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007890 struct net_device *upper_dev,
7891 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007892{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007893 struct netdev_nested_priv priv = {
7894 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7895 .data = NULL,
7896 };
7897
David Ahern42ab19e2017-10-04 17:48:47 -07007898 return __netdev_upper_dev_link(dev, upper_dev, false,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007899 NULL, NULL, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007900}
7901EXPORT_SYMBOL(netdev_upper_dev_link);
7902
7903/**
7904 * netdev_master_upper_dev_link - Add a master link to the upper device
7905 * @dev: device
7906 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007907 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007908 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007909 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007910 *
7911 * Adds a link to device which is upper to this one. In this case, only
7912 * one master upper device can be linked, although other non-master devices
7913 * might be linked as well. The caller must hold the RTNL lock.
7914 * On a failure a negative errno code is returned. On success the reference
7915 * counts are adjusted and the function returns zero.
7916 */
7917int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007918 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007919 void *upper_priv, void *upper_info,
7920 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007921{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007922 struct netdev_nested_priv priv = {
7923 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7924 .data = NULL,
7925 };
7926
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007927 return __netdev_upper_dev_link(dev, upper_dev, true,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007928 upper_priv, upper_info, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007929}
7930EXPORT_SYMBOL(netdev_master_upper_dev_link);
7931
Taehee Yoofe8300f2020-09-25 18:13:02 +00007932static void __netdev_upper_dev_unlink(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007933 struct net_device *upper_dev,
7934 struct netdev_nested_priv *priv)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007935{
David Ahern51d0c0472017-10-04 17:48:45 -07007936 struct netdev_notifier_changeupper_info changeupper_info = {
7937 .info = {
7938 .dev = dev,
7939 },
7940 .upper_dev = upper_dev,
7941 .linking = false,
7942 };
tchardingf4563a72017-02-09 17:56:07 +11007943
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007944 ASSERT_RTNL();
7945
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007946 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007947
David Ahern51d0c0472017-10-04 17:48:45 -07007948 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007949 &changeupper_info.info);
7950
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007951 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007952
David Ahern51d0c0472017-10-04 17:48:45 -07007953 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007954 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007955
7956 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007957 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007958
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007959 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007960 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007961 priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007962}
Taehee Yoofe8300f2020-09-25 18:13:02 +00007963
7964/**
7965 * netdev_upper_dev_unlink - Removes a link to upper device
7966 * @dev: device
7967 * @upper_dev: new upper device
7968 *
7969 * Removes a link to device which is upper to this one. The caller must hold
7970 * the RTNL lock.
7971 */
7972void netdev_upper_dev_unlink(struct net_device *dev,
7973 struct net_device *upper_dev)
7974{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007975 struct netdev_nested_priv priv = {
7976 .flags = NESTED_SYNC_TODO,
7977 .data = NULL,
7978 };
7979
7980 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007981}
7982EXPORT_SYMBOL(netdev_upper_dev_unlink);
7983
Taehee Yoo32b6d342019-10-21 18:47:56 +00007984static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7985 struct net_device *lower_dev,
7986 bool val)
7987{
7988 struct netdev_adjacent *adj;
7989
7990 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7991 if (adj)
7992 adj->ignore = val;
7993
7994 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7995 if (adj)
7996 adj->ignore = val;
7997}
7998
7999static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
8000 struct net_device *lower_dev)
8001{
8002 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
8003}
8004
8005static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
8006 struct net_device *lower_dev)
8007{
8008 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
8009}
8010
8011int netdev_adjacent_change_prepare(struct net_device *old_dev,
8012 struct net_device *new_dev,
8013 struct net_device *dev,
8014 struct netlink_ext_ack *extack)
8015{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008016 struct netdev_nested_priv priv = {
8017 .flags = 0,
8018 .data = NULL,
8019 };
Taehee Yoo32b6d342019-10-21 18:47:56 +00008020 int err;
8021
8022 if (!new_dev)
8023 return 0;
8024
8025 if (old_dev && new_dev != old_dev)
8026 netdev_adjacent_dev_disable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008027 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
8028 extack);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008029 if (err) {
8030 if (old_dev && new_dev != old_dev)
8031 netdev_adjacent_dev_enable(dev, old_dev);
8032 return err;
8033 }
8034
8035 return 0;
8036}
8037EXPORT_SYMBOL(netdev_adjacent_change_prepare);
8038
8039void netdev_adjacent_change_commit(struct net_device *old_dev,
8040 struct net_device *new_dev,
8041 struct net_device *dev)
8042{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008043 struct netdev_nested_priv priv = {
8044 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8045 .data = NULL,
8046 };
8047
Taehee Yoo32b6d342019-10-21 18:47:56 +00008048 if (!new_dev || !old_dev)
8049 return;
8050
8051 if (new_dev == old_dev)
8052 return;
8053
8054 netdev_adjacent_dev_enable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008055 __netdev_upper_dev_unlink(old_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008056}
8057EXPORT_SYMBOL(netdev_adjacent_change_commit);
8058
8059void netdev_adjacent_change_abort(struct net_device *old_dev,
8060 struct net_device *new_dev,
8061 struct net_device *dev)
8062{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008063 struct netdev_nested_priv priv = {
8064 .flags = 0,
8065 .data = NULL,
8066 };
8067
Taehee Yoo32b6d342019-10-21 18:47:56 +00008068 if (!new_dev)
8069 return;
8070
8071 if (old_dev && new_dev != old_dev)
8072 netdev_adjacent_dev_enable(dev, old_dev);
8073
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008074 __netdev_upper_dev_unlink(new_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008075}
8076EXPORT_SYMBOL(netdev_adjacent_change_abort);
8077
Moni Shoua61bd3852015-02-03 16:48:29 +02008078/**
8079 * netdev_bonding_info_change - Dispatch event about slave change
8080 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09008081 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02008082 *
8083 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8084 * The caller must hold the RTNL lock.
8085 */
8086void netdev_bonding_info_change(struct net_device *dev,
8087 struct netdev_bonding_info *bonding_info)
8088{
David Ahern51d0c0472017-10-04 17:48:45 -07008089 struct netdev_notifier_bonding_info info = {
8090 .info.dev = dev,
8091 };
Moni Shoua61bd3852015-02-03 16:48:29 +02008092
8093 memcpy(&info.bonding_info, bonding_info,
8094 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07008095 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02008096 &info.info);
8097}
8098EXPORT_SYMBOL(netdev_bonding_info_change);
8099
Maor Gottliebcff9f122020-04-30 22:21:31 +03008100/**
8101 * netdev_get_xmit_slave - Get the xmit slave of master device
Andrew Lunn88425002020-07-13 01:14:59 +02008102 * @dev: device
Maor Gottliebcff9f122020-04-30 22:21:31 +03008103 * @skb: The packet
8104 * @all_slaves: assume all the slaves are active
8105 *
8106 * The reference counters are not incremented so the caller must be
8107 * careful with locks. The caller must hold RCU lock.
8108 * %NULL is returned if no slave is found.
8109 */
8110
8111struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8112 struct sk_buff *skb,
8113 bool all_slaves)
8114{
8115 const struct net_device_ops *ops = dev->netdev_ops;
8116
8117 if (!ops->ndo_get_xmit_slave)
8118 return NULL;
8119 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8120}
8121EXPORT_SYMBOL(netdev_get_xmit_slave);
8122
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008123static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008124{
8125 struct netdev_adjacent *iter;
8126
8127 struct net *net = dev_net(dev);
8128
8129 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008130 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008131 continue;
8132 netdev_adjacent_sysfs_add(iter->dev, dev,
8133 &iter->dev->adj_list.lower);
8134 netdev_adjacent_sysfs_add(dev, iter->dev,
8135 &dev->adj_list.upper);
8136 }
8137
8138 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008139 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008140 continue;
8141 netdev_adjacent_sysfs_add(iter->dev, dev,
8142 &iter->dev->adj_list.upper);
8143 netdev_adjacent_sysfs_add(dev, iter->dev,
8144 &dev->adj_list.lower);
8145 }
8146}
8147
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008148static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008149{
8150 struct netdev_adjacent *iter;
8151
8152 struct net *net = dev_net(dev);
8153
8154 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008155 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008156 continue;
8157 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8158 &iter->dev->adj_list.lower);
8159 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8160 &dev->adj_list.upper);
8161 }
8162
8163 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008164 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008165 continue;
8166 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8167 &iter->dev->adj_list.upper);
8168 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8169 &dev->adj_list.lower);
8170 }
8171}
8172
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008173void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02008174{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008175 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02008176
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008177 struct net *net = dev_net(dev);
8178
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008179 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008180 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008181 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008182 netdev_adjacent_sysfs_del(iter->dev, oldname,
8183 &iter->dev->adj_list.lower);
8184 netdev_adjacent_sysfs_add(iter->dev, dev,
8185 &iter->dev->adj_list.lower);
8186 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008187
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008188 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008189 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008190 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008191 netdev_adjacent_sysfs_del(iter->dev, oldname,
8192 &iter->dev->adj_list.upper);
8193 netdev_adjacent_sysfs_add(iter->dev, dev,
8194 &iter->dev->adj_list.upper);
8195 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008196}
Veaceslav Falico402dae92013-09-25 09:20:09 +02008197
8198void *netdev_lower_dev_get_private(struct net_device *dev,
8199 struct net_device *lower_dev)
8200{
8201 struct netdev_adjacent *lower;
8202
8203 if (!lower_dev)
8204 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02008205 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02008206 if (!lower)
8207 return NULL;
8208
8209 return lower->private;
8210}
8211EXPORT_SYMBOL(netdev_lower_dev_get_private);
8212
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04008213
Jiri Pirko04d48262015-12-03 12:12:15 +01008214/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +01008215 * netdev_lower_state_changed - Dispatch event about lower device state change
Jiri Pirko04d48262015-12-03 12:12:15 +01008216 * @lower_dev: device
8217 * @lower_state_info: state to dispatch
8218 *
8219 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8220 * The caller must hold the RTNL lock.
8221 */
8222void netdev_lower_state_changed(struct net_device *lower_dev,
8223 void *lower_state_info)
8224{
David Ahern51d0c0472017-10-04 17:48:45 -07008225 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8226 .info.dev = lower_dev,
8227 };
Jiri Pirko04d48262015-12-03 12:12:15 +01008228
8229 ASSERT_RTNL();
8230 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07008231 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01008232 &changelowerstate_info.info);
8233}
8234EXPORT_SYMBOL(netdev_lower_state_changed);
8235
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008236static void dev_change_rx_flags(struct net_device *dev, int flags)
8237{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008238 const struct net_device_ops *ops = dev->netdev_ops;
8239
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05008240 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008241 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008242}
8243
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008244static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07008245{
Eric Dumazetb536db92011-11-30 21:42:26 +00008246 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06008247 kuid_t uid;
8248 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07008249
Patrick McHardy24023452007-07-14 18:51:31 -07008250 ASSERT_RTNL();
8251
Wang Chendad9b332008-06-18 01:48:28 -07008252 dev->flags |= IFF_PROMISC;
8253 dev->promiscuity += inc;
8254 if (dev->promiscuity == 0) {
8255 /*
8256 * Avoid overflow.
8257 * If inc causes overflow, untouch promisc and return error.
8258 */
8259 if (inc < 0)
8260 dev->flags &= ~IFF_PROMISC;
8261 else {
8262 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008263 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
8264 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008265 return -EOVERFLOW;
8266 }
8267 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008268 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008269 pr_info("device %s %s promiscuous mode\n",
8270 dev->name,
8271 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11008272 if (audit_enabled) {
8273 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04008274 audit_log(audit_context(), GFP_ATOMIC,
8275 AUDIT_ANOM_PROMISCUOUS,
8276 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8277 dev->name, (dev->flags & IFF_PROMISC),
8278 (old_flags & IFF_PROMISC),
8279 from_kuid(&init_user_ns, audit_get_loginuid(current)),
8280 from_kuid(&init_user_ns, uid),
8281 from_kgid(&init_user_ns, gid),
8282 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11008283 }
Patrick McHardy24023452007-07-14 18:51:31 -07008284
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008285 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07008286 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008287 if (notify)
8288 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07008289 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008290}
8291
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292/**
8293 * dev_set_promiscuity - update promiscuity count on a device
8294 * @dev: device
8295 * @inc: modifier
8296 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07008297 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 * remains above zero the interface remains promiscuous. Once it hits zero
8299 * the device reverts back to normal filtering operation. A negative inc
8300 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07008301 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 */
Wang Chendad9b332008-06-18 01:48:28 -07008303int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304{
Eric Dumazetb536db92011-11-30 21:42:26 +00008305 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07008306 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008308 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07008309 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07008310 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07008311 if (dev->flags != old_flags)
8312 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07008313 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008315EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008317static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008319 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320
Patrick McHardy24023452007-07-14 18:51:31 -07008321 ASSERT_RTNL();
8322
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07008324 dev->allmulti += inc;
8325 if (dev->allmulti == 0) {
8326 /*
8327 * Avoid overflow.
8328 * If inc causes overflow, untouch allmulti and return error.
8329 */
8330 if (inc < 0)
8331 dev->flags &= ~IFF_ALLMULTI;
8332 else {
8333 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008334 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8335 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008336 return -EOVERFLOW;
8337 }
8338 }
Patrick McHardy24023452007-07-14 18:51:31 -07008339 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008340 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008341 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008342 if (notify)
8343 __dev_notify_flags(dev, old_flags,
8344 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008345 }
Wang Chendad9b332008-06-18 01:48:28 -07008346 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008347}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008348
8349/**
8350 * dev_set_allmulti - update allmulti count on a device
8351 * @dev: device
8352 * @inc: modifier
8353 *
8354 * Add or remove reception of all multicast frames to a device. While the
8355 * count in the device remains above zero the interface remains listening
8356 * to all interfaces. Once it hits zero the device reverts back to normal
8357 * filtering operation. A negative @inc value is used to drop the counter
8358 * when releasing a resource needing all multicasts.
8359 * Return 0 if successful or a negative errno code on error.
8360 */
8361
8362int dev_set_allmulti(struct net_device *dev, int inc)
8363{
8364 return __dev_set_allmulti(dev, inc, true);
8365}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008366EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008367
8368/*
8369 * Upload unicast and multicast address lists to device and
8370 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008371 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008372 * are present.
8373 */
8374void __dev_set_rx_mode(struct net_device *dev)
8375{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008376 const struct net_device_ops *ops = dev->netdev_ops;
8377
Patrick McHardy4417da62007-06-27 01:28:10 -07008378 /* dev_open will call this function so the list will stay sane. */
8379 if (!(dev->flags&IFF_UP))
8380 return;
8381
8382 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008383 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008384
Jiri Pirko01789342011-08-16 06:29:00 +00008385 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008386 /* Unicast addresses changes may only happen under the rtnl,
8387 * therefore calling __dev_set_promiscuity here is safe.
8388 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008389 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 = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008392 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008393 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008394 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008395 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008396 }
Jiri Pirko01789342011-08-16 06:29:00 +00008397
8398 if (ops->ndo_set_rx_mode)
8399 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008400}
8401
8402void dev_set_rx_mode(struct net_device *dev)
8403{
David S. Millerb9e40852008-07-15 00:15:08 -07008404 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008405 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008406 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407}
8408
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008409/**
8410 * dev_get_flags - get flags reported to userspace
8411 * @dev: device
8412 *
8413 * Get the combination of flag bits exported through APIs to userspace.
8414 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008415unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416{
Eric Dumazet95c96172012-04-15 05:58:06 +00008417 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418
8419 flags = (dev->flags & ~(IFF_PROMISC |
8420 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008421 IFF_RUNNING |
8422 IFF_LOWER_UP |
8423 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 (dev->gflags & (IFF_PROMISC |
8425 IFF_ALLMULTI));
8426
Stefan Rompfb00055a2006-03-20 17:09:11 -08008427 if (netif_running(dev)) {
8428 if (netif_oper_up(dev))
8429 flags |= IFF_RUNNING;
8430 if (netif_carrier_ok(dev))
8431 flags |= IFF_LOWER_UP;
8432 if (netif_dormant(dev))
8433 flags |= IFF_DORMANT;
8434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435
8436 return flags;
8437}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008438EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439
Petr Machata6d040322018-12-06 17:05:43 +00008440int __dev_change_flags(struct net_device *dev, unsigned int flags,
8441 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442{
Eric Dumazetb536db92011-11-30 21:42:26 +00008443 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008444 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
Patrick McHardy24023452007-07-14 18:51:31 -07008446 ASSERT_RTNL();
8447
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 /*
8449 * Set the flags on our device.
8450 */
8451
8452 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8453 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8454 IFF_AUTOMEDIA)) |
8455 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8456 IFF_ALLMULTI));
8457
8458 /*
8459 * Load in the correct multicast list now the flags have changed.
8460 */
8461
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008462 if ((old_flags ^ flags) & IFF_MULTICAST)
8463 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008464
Patrick McHardy4417da62007-06-27 01:28:10 -07008465 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466
8467 /*
8468 * Have we downed the interface. We handle IFF_UP ourselves
8469 * according to user attempts to set it, rather than blindly
8470 * setting it.
8471 */
8472
8473 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008474 if ((old_flags ^ flags) & IFF_UP) {
8475 if (old_flags & IFF_UP)
8476 __dev_close(dev);
8477 else
Petr Machata40c900a2018-12-06 17:05:47 +00008478 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008482 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008483 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008484
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008486
8487 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8488 if (dev->flags != old_flags)
8489 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 }
8491
8492 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008493 * is important. Some (broken) drivers set IFF_PROMISC, when
8494 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 */
8496 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008497 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008500 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 }
8502
Patrick McHardybd380812010-02-26 06:34:53 +00008503 return ret;
8504}
8505
Nicolas Dichtela528c212013-09-25 12:02:44 +02008506void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8507 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008508{
8509 unsigned int changes = dev->flags ^ old_flags;
8510
Nicolas Dichtela528c212013-09-25 12:02:44 +02008511 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008512 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008513
Patrick McHardybd380812010-02-26 06:34:53 +00008514 if (changes & IFF_UP) {
8515 if (dev->flags & IFF_UP)
8516 call_netdevice_notifiers(NETDEV_UP, dev);
8517 else
8518 call_netdevice_notifiers(NETDEV_DOWN, dev);
8519 }
8520
8521 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008522 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008523 struct netdev_notifier_change_info change_info = {
8524 .info = {
8525 .dev = dev,
8526 },
8527 .flags_changed = changes,
8528 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008529
David Ahern51d0c0472017-10-04 17:48:45 -07008530 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008531 }
Patrick McHardybd380812010-02-26 06:34:53 +00008532}
8533
8534/**
8535 * dev_change_flags - change device settings
8536 * @dev: device
8537 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008538 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008539 *
8540 * Change settings on device based state flags. The flags are
8541 * in the userspace exported format.
8542 */
Petr Machata567c5e12018-12-06 17:05:42 +00008543int dev_change_flags(struct net_device *dev, unsigned int flags,
8544 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008545{
Eric Dumazetb536db92011-11-30 21:42:26 +00008546 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008547 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008548
Petr Machata6d040322018-12-06 17:05:43 +00008549 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008550 if (ret < 0)
8551 return ret;
8552
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008553 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008554 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555 return ret;
8556}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008557EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558
WANG Congf51048c2017-07-06 15:01:57 -07008559int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008560{
8561 const struct net_device_ops *ops = dev->netdev_ops;
8562
8563 if (ops->ndo_change_mtu)
8564 return ops->ndo_change_mtu(dev, new_mtu);
8565
Eric Dumazet501a90c2019-12-05 20:43:46 -08008566 /* Pairs with all the lockless reads of dev->mtu in the stack */
8567 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008568 return 0;
8569}
WANG Congf51048c2017-07-06 15:01:57 -07008570EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008571
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008572int dev_validate_mtu(struct net_device *dev, int new_mtu,
8573 struct netlink_ext_ack *extack)
8574{
8575 /* MTU must be positive, and in range */
8576 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8577 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8578 return -EINVAL;
8579 }
8580
8581 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8582 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8583 return -EINVAL;
8584 }
8585 return 0;
8586}
8587
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008588/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008589 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008590 * @dev: device
8591 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008592 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008593 *
8594 * Change the maximum transfer size of the network device.
8595 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008596int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8597 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008599 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600
8601 if (new_mtu == dev->mtu)
8602 return 0;
8603
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008604 err = dev_validate_mtu(dev, new_mtu, extack);
8605 if (err)
8606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607
8608 if (!netif_device_present(dev))
8609 return -ENODEV;
8610
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008611 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8612 err = notifier_to_errno(err);
8613 if (err)
8614 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008615
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008616 orig_mtu = dev->mtu;
8617 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008619 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008620 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8621 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008622 err = notifier_to_errno(err);
8623 if (err) {
8624 /* setting mtu back and notifying everyone again,
8625 * so that they have a chance to revert changes.
8626 */
8627 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008628 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8629 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008630 }
8631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 return err;
8633}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008634
8635int dev_set_mtu(struct net_device *dev, int new_mtu)
8636{
8637 struct netlink_ext_ack extack;
8638 int err;
8639
Li RongQinga6bcfc82018-08-03 15:45:21 +08008640 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008641 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008642 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008643 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8644 return err;
8645}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008646EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008648/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008649 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8650 * @dev: device
8651 * @new_len: new tx queue length
8652 */
8653int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8654{
8655 unsigned int orig_len = dev->tx_queue_len;
8656 int res;
8657
8658 if (new_len != (unsigned int)new_len)
8659 return -ERANGE;
8660
8661 if (new_len != orig_len) {
8662 dev->tx_queue_len = new_len;
8663 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8664 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008665 if (res)
8666 goto err_rollback;
8667 res = dev_qdisc_change_tx_queue_len(dev);
8668 if (res)
8669 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008670 }
8671
8672 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008673
8674err_rollback:
8675 netdev_err(dev, "refused to change device tx_queue_len\n");
8676 dev->tx_queue_len = orig_len;
8677 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008678}
8679
8680/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008681 * dev_set_group - Change group this device belongs to
8682 * @dev: device
8683 * @new_group: group this device should belong to
8684 */
8685void dev_set_group(struct net_device *dev, int new_group)
8686{
8687 dev->group = new_group;
8688}
8689EXPORT_SYMBOL(dev_set_group);
8690
8691/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008692 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8693 * @dev: device
8694 * @addr: new address
8695 * @extack: netlink extended ack
8696 */
8697int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8698 struct netlink_ext_ack *extack)
8699{
8700 struct netdev_notifier_pre_changeaddr_info info = {
8701 .info.dev = dev,
8702 .info.extack = extack,
8703 .dev_addr = addr,
8704 };
8705 int rc;
8706
8707 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8708 return notifier_to_errno(rc);
8709}
8710EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8711
8712/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008713 * dev_set_mac_address - Change Media Access Control Address
8714 * @dev: device
8715 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008716 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008717 *
8718 * Change the hardware (MAC) address of the device
8719 */
Petr Machata3a37a962018-12-13 11:54:30 +00008720int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8721 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008723 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 int err;
8725
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008726 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 return -EOPNOTSUPP;
8728 if (sa->sa_family != dev->type)
8729 return -EINVAL;
8730 if (!netif_device_present(dev))
8731 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008732 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8733 if (err)
8734 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008735 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008736 if (err)
8737 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008738 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008739 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008740 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008743EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008745/**
8746 * dev_change_carrier - Change device carrier
8747 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008748 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008749 *
8750 * Change device carrier
8751 */
8752int dev_change_carrier(struct net_device *dev, bool new_carrier)
8753{
8754 const struct net_device_ops *ops = dev->netdev_ops;
8755
8756 if (!ops->ndo_change_carrier)
8757 return -EOPNOTSUPP;
8758 if (!netif_device_present(dev))
8759 return -ENODEV;
8760 return ops->ndo_change_carrier(dev, new_carrier);
8761}
8762EXPORT_SYMBOL(dev_change_carrier);
8763
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008765 * dev_get_phys_port_id - Get device physical port ID
8766 * @dev: device
8767 * @ppid: port ID
8768 *
8769 * Get device physical port ID
8770 */
8771int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008772 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008773{
8774 const struct net_device_ops *ops = dev->netdev_ops;
8775
8776 if (!ops->ndo_get_phys_port_id)
8777 return -EOPNOTSUPP;
8778 return ops->ndo_get_phys_port_id(dev, ppid);
8779}
8780EXPORT_SYMBOL(dev_get_phys_port_id);
8781
8782/**
David Aherndb24a902015-03-17 20:23:15 -06008783 * dev_get_phys_port_name - Get device physical port name
8784 * @dev: device
8785 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008786 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008787 *
8788 * Get device physical port name
8789 */
8790int dev_get_phys_port_name(struct net_device *dev,
8791 char *name, size_t len)
8792{
8793 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008794 int err;
David Aherndb24a902015-03-17 20:23:15 -06008795
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008796 if (ops->ndo_get_phys_port_name) {
8797 err = ops->ndo_get_phys_port_name(dev, name, len);
8798 if (err != -EOPNOTSUPP)
8799 return err;
8800 }
8801 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008802}
8803EXPORT_SYMBOL(dev_get_phys_port_name);
8804
8805/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008806 * dev_get_port_parent_id - Get the device's port parent identifier
8807 * @dev: network device
8808 * @ppid: pointer to a storage for the port's parent identifier
8809 * @recurse: allow/disallow recursion to lower devices
8810 *
8811 * Get the devices's port parent identifier
8812 */
8813int dev_get_port_parent_id(struct net_device *dev,
8814 struct netdev_phys_item_id *ppid,
8815 bool recurse)
8816{
8817 const struct net_device_ops *ops = dev->netdev_ops;
8818 struct netdev_phys_item_id first = { };
8819 struct net_device *lower_dev;
8820 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008821 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008822
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008823 if (ops->ndo_get_port_parent_id) {
8824 err = ops->ndo_get_port_parent_id(dev, ppid);
8825 if (err != -EOPNOTSUPP)
8826 return err;
8827 }
8828
8829 err = devlink_compat_switch_id_get(dev, ppid);
8830 if (!err || err != -EOPNOTSUPP)
8831 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008832
8833 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008834 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008835
8836 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8837 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8838 if (err)
8839 break;
8840 if (!first.id_len)
8841 first = *ppid;
8842 else if (memcmp(&first, ppid, sizeof(*ppid)))
Ido Schimmele1b9efe2020-09-10 14:01:26 +03008843 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008844 }
8845
8846 return err;
8847}
8848EXPORT_SYMBOL(dev_get_port_parent_id);
8849
8850/**
8851 * netdev_port_same_parent_id - Indicate if two network devices have
8852 * the same port parent identifier
8853 * @a: first network device
8854 * @b: second network device
8855 */
8856bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8857{
8858 struct netdev_phys_item_id a_id = { };
8859 struct netdev_phys_item_id b_id = { };
8860
8861 if (dev_get_port_parent_id(a, &a_id, true) ||
8862 dev_get_port_parent_id(b, &b_id, true))
8863 return false;
8864
8865 return netdev_phys_item_id_same(&a_id, &b_id);
8866}
8867EXPORT_SYMBOL(netdev_port_same_parent_id);
8868
8869/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008870 * dev_change_proto_down - update protocol port state information
8871 * @dev: device
8872 * @proto_down: new value
8873 *
8874 * This info can be used by switch drivers to set the phys state of the
8875 * port.
8876 */
8877int dev_change_proto_down(struct net_device *dev, bool proto_down)
8878{
8879 const struct net_device_ops *ops = dev->netdev_ops;
8880
8881 if (!ops->ndo_change_proto_down)
8882 return -EOPNOTSUPP;
8883 if (!netif_device_present(dev))
8884 return -ENODEV;
8885 return ops->ndo_change_proto_down(dev, proto_down);
8886}
8887EXPORT_SYMBOL(dev_change_proto_down);
8888
Andy Roulinb5899672019-02-22 18:06:36 +00008889/**
8890 * dev_change_proto_down_generic - generic implementation for
8891 * ndo_change_proto_down that sets carrier according to
8892 * proto_down.
8893 *
8894 * @dev: device
8895 * @proto_down: new value
8896 */
8897int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8898{
8899 if (proto_down)
8900 netif_carrier_off(dev);
8901 else
8902 netif_carrier_on(dev);
8903 dev->proto_down = proto_down;
8904 return 0;
8905}
8906EXPORT_SYMBOL(dev_change_proto_down_generic);
8907
Roopa Prabhu829eb202020-07-31 17:34:01 -07008908/**
8909 * dev_change_proto_down_reason - proto down reason
8910 *
8911 * @dev: device
8912 * @mask: proto down mask
8913 * @value: proto down value
8914 */
8915void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8916 u32 value)
8917{
8918 int b;
8919
8920 if (!mask) {
8921 dev->proto_down_reason = value;
8922 } else {
8923 for_each_set_bit(b, &mask, 32) {
8924 if (value & (1 << b))
8925 dev->proto_down_reason |= BIT(b);
8926 else
8927 dev->proto_down_reason &= ~BIT(b);
8928 }
8929 }
8930}
8931EXPORT_SYMBOL(dev_change_proto_down_reason);
8932
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008933struct bpf_xdp_link {
8934 struct bpf_link link;
8935 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
8936 int flags;
8937};
8938
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008939static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008940{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008941 if (flags & XDP_FLAGS_HW_MODE)
8942 return XDP_MODE_HW;
8943 if (flags & XDP_FLAGS_DRV_MODE)
8944 return XDP_MODE_DRV;
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008945 if (flags & XDP_FLAGS_SKB_MODE)
8946 return XDP_MODE_SKB;
8947 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008948}
8949
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008950static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008951{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008952 switch (mode) {
8953 case XDP_MODE_SKB:
8954 return generic_xdp_install;
8955 case XDP_MODE_DRV:
8956 case XDP_MODE_HW:
8957 return dev->netdev_ops->ndo_bpf;
8958 default:
8959 return NULL;
Tom Rix5d867242020-11-01 07:36:47 -08008960 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008961}
8962
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008963static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
8964 enum bpf_xdp_mode mode)
8965{
8966 return dev->xdp_state[mode].link;
8967}
8968
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008969static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
8970 enum bpf_xdp_mode mode)
8971{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008972 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
8973
8974 if (link)
8975 return link->link.prog;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008976 return dev->xdp_state[mode].prog;
8977}
8978
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008979static u8 dev_xdp_prog_count(struct net_device *dev)
8980{
8981 u8 count = 0;
8982 int i;
8983
8984 for (i = 0; i < __MAX_XDP_MODE; i++)
8985 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
8986 count++;
8987 return count;
8988}
8989
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008990u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
8991{
8992 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
8993
8994 return prog ? prog->aux->id : 0;
8995}
8996
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008997static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
8998 struct bpf_xdp_link *link)
8999{
9000 dev->xdp_state[mode].link = link;
9001 dev->xdp_state[mode].prog = NULL;
9002}
9003
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009004static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9005 struct bpf_prog *prog)
9006{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009007 dev->xdp_state[mode].link = NULL;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009008 dev->xdp_state[mode].prog = prog;
9009}
9010
9011static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9012 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9013 u32 flags, struct bpf_prog *prog)
9014{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009015 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01009016 int err;
9017
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009018 memset(&xdp, 0, sizeof(xdp));
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009019 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009020 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07009021 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009022 xdp.prog = prog;
9023
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009024 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
9025 * "moved" into driver), so they don't increment it on their own, but
9026 * they do decrement refcnt when program is detached or replaced.
9027 * Given net_device also owns link/prog, we need to bump refcnt here
9028 * to prevent drivers from underflowing it.
9029 */
9030 if (prog)
9031 bpf_prog_inc(prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009032 err = bpf_op(dev, &xdp);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009033 if (err) {
9034 if (prog)
9035 bpf_prog_put(prog);
9036 return err;
9037 }
Björn Töpel7e6897f2019-12-13 18:51:09 +01009038
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009039 if (mode != XDP_MODE_HW)
9040 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009041
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009042 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009043}
9044
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009045static void dev_xdp_uninstall(struct net_device *dev)
9046{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009047 struct bpf_xdp_link *link;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009048 struct bpf_prog *prog;
9049 enum bpf_xdp_mode mode;
9050 bpf_op_t bpf_op;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009051
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009052 ASSERT_RTNL();
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009053
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009054 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9055 prog = dev_xdp_prog(dev, mode);
9056 if (!prog)
9057 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009058
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009059 bpf_op = dev_xdp_bpf_op(dev, mode);
9060 if (!bpf_op)
9061 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009062
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009063 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9064
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009065 /* auto-detach link from net device */
9066 link = dev_xdp_link(dev, mode);
9067 if (link)
9068 link->dev = NULL;
9069 else
9070 bpf_prog_put(prog);
9071
9072 dev_xdp_set_link(dev, mode, NULL);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009073 }
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009074}
9075
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009076static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009077 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9078 struct bpf_prog *old_prog, u32 flags)
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009079{
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009080 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009081 struct bpf_prog *cur_prog;
9082 enum bpf_xdp_mode mode;
9083 bpf_op_t bpf_op;
9084 int err;
9085
9086 ASSERT_RTNL();
9087
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009088 /* either link or prog attachment, never both */
9089 if (link && (new_prog || old_prog))
9090 return -EINVAL;
9091 /* link supports only XDP mode flags */
9092 if (link && (flags & ~XDP_FLAGS_MODES)) {
9093 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9094 return -EINVAL;
9095 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009096 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9097 if (num_modes > 1) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009098 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9099 return -EINVAL;
9100 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009101 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9102 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9103 NL_SET_ERR_MSG(extack,
9104 "More than one program loaded, unset mode is ambiguous");
9105 return -EINVAL;
9106 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009107 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9108 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9109 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9110 return -EINVAL;
9111 }
9112
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009113 mode = dev_xdp_mode(dev, flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009114 /* can't replace attached link */
9115 if (dev_xdp_link(dev, mode)) {
9116 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9117 return -EBUSY;
9118 }
9119
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009120 cur_prog = dev_xdp_prog(dev, mode);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009121 /* can't replace attached prog with link */
9122 if (link && cur_prog) {
9123 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9124 return -EBUSY;
9125 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009126 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9127 NL_SET_ERR_MSG(extack, "Active program does not match expected");
9128 return -EEXIST;
9129 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009130
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009131 /* put effective new program into new_prog */
9132 if (link)
9133 new_prog = link->link.prog;
9134
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009135 if (new_prog) {
9136 bool offload = mode == XDP_MODE_HW;
9137 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9138 ? XDP_MODE_DRV : XDP_MODE_SKB;
9139
Andrii Nakryiko068d9d12020-08-11 19:29:23 -07009140 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9141 NL_SET_ERR_MSG(extack, "XDP program already attached");
9142 return -EBUSY;
9143 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009144 if (!offload && dev_xdp_prog(dev, other_mode)) {
9145 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
9146 return -EEXIST;
9147 }
9148 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
9149 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
9150 return -EINVAL;
9151 }
9152 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
9153 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
9154 return -EINVAL;
9155 }
9156 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9157 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
9158 return -EINVAL;
9159 }
9160 }
9161
9162 /* don't call drivers if the effective program didn't change */
9163 if (new_prog != cur_prog) {
9164 bpf_op = dev_xdp_bpf_op(dev, mode);
9165 if (!bpf_op) {
9166 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9167 return -EOPNOTSUPP;
9168 }
9169
9170 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9171 if (err)
9172 return err;
9173 }
9174
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009175 if (link)
9176 dev_xdp_set_link(dev, mode, link);
9177 else
9178 dev_xdp_set_prog(dev, mode, new_prog);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009179 if (cur_prog)
9180 bpf_prog_put(cur_prog);
9181
9182 return 0;
9183}
9184
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009185static int dev_xdp_attach_link(struct net_device *dev,
9186 struct netlink_ext_ack *extack,
9187 struct bpf_xdp_link *link)
9188{
9189 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9190}
9191
9192static int dev_xdp_detach_link(struct net_device *dev,
9193 struct netlink_ext_ack *extack,
9194 struct bpf_xdp_link *link)
9195{
9196 enum bpf_xdp_mode mode;
9197 bpf_op_t bpf_op;
9198
9199 ASSERT_RTNL();
9200
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009201 mode = dev_xdp_mode(dev, link->flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009202 if (dev_xdp_link(dev, mode) != link)
9203 return -EINVAL;
9204
9205 bpf_op = dev_xdp_bpf_op(dev, mode);
9206 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9207 dev_xdp_set_link(dev, mode, NULL);
9208 return 0;
9209}
9210
9211static void bpf_xdp_link_release(struct bpf_link *link)
9212{
9213 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9214
9215 rtnl_lock();
9216
9217 /* if racing with net_device's tear down, xdp_link->dev might be
9218 * already NULL, in which case link was already auto-detached
9219 */
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009220 if (xdp_link->dev) {
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009221 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009222 xdp_link->dev = NULL;
9223 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009224
9225 rtnl_unlock();
9226}
9227
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009228static int bpf_xdp_link_detach(struct bpf_link *link)
9229{
9230 bpf_xdp_link_release(link);
9231 return 0;
9232}
9233
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009234static void bpf_xdp_link_dealloc(struct bpf_link *link)
9235{
9236 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9237
9238 kfree(xdp_link);
9239}
9240
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009241static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9242 struct seq_file *seq)
9243{
9244 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9245 u32 ifindex = 0;
9246
9247 rtnl_lock();
9248 if (xdp_link->dev)
9249 ifindex = xdp_link->dev->ifindex;
9250 rtnl_unlock();
9251
9252 seq_printf(seq, "ifindex:\t%u\n", ifindex);
9253}
9254
9255static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9256 struct bpf_link_info *info)
9257{
9258 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9259 u32 ifindex = 0;
9260
9261 rtnl_lock();
9262 if (xdp_link->dev)
9263 ifindex = xdp_link->dev->ifindex;
9264 rtnl_unlock();
9265
9266 info->xdp.ifindex = ifindex;
9267 return 0;
9268}
9269
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009270static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9271 struct bpf_prog *old_prog)
9272{
9273 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9274 enum bpf_xdp_mode mode;
9275 bpf_op_t bpf_op;
9276 int err = 0;
9277
9278 rtnl_lock();
9279
9280 /* link might have been auto-released already, so fail */
9281 if (!xdp_link->dev) {
9282 err = -ENOLINK;
9283 goto out_unlock;
9284 }
9285
9286 if (old_prog && link->prog != old_prog) {
9287 err = -EPERM;
9288 goto out_unlock;
9289 }
9290 old_prog = link->prog;
9291 if (old_prog == new_prog) {
9292 /* no-op, don't disturb drivers */
9293 bpf_prog_put(new_prog);
9294 goto out_unlock;
9295 }
9296
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009297 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009298 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9299 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9300 xdp_link->flags, new_prog);
9301 if (err)
9302 goto out_unlock;
9303
9304 old_prog = xchg(&link->prog, new_prog);
9305 bpf_prog_put(old_prog);
9306
9307out_unlock:
9308 rtnl_unlock();
9309 return err;
9310}
9311
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009312static const struct bpf_link_ops bpf_xdp_link_lops = {
9313 .release = bpf_xdp_link_release,
9314 .dealloc = bpf_xdp_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009315 .detach = bpf_xdp_link_detach,
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009316 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9317 .fill_link_info = bpf_xdp_link_fill_link_info,
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009318 .update_prog = bpf_xdp_link_update,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009319};
9320
9321int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9322{
9323 struct net *net = current->nsproxy->net_ns;
9324 struct bpf_link_primer link_primer;
9325 struct bpf_xdp_link *link;
9326 struct net_device *dev;
9327 int err, fd;
9328
9329 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
9330 if (!dev)
9331 return -EINVAL;
9332
9333 link = kzalloc(sizeof(*link), GFP_USER);
9334 if (!link) {
9335 err = -ENOMEM;
9336 goto out_put_dev;
9337 }
9338
9339 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9340 link->dev = dev;
9341 link->flags = attr->link_create.flags;
9342
9343 err = bpf_link_prime(&link->link, &link_primer);
9344 if (err) {
9345 kfree(link);
9346 goto out_put_dev;
9347 }
9348
9349 rtnl_lock();
9350 err = dev_xdp_attach_link(dev, NULL, link);
9351 rtnl_unlock();
9352
9353 if (err) {
9354 bpf_link_cleanup(&link_primer);
9355 goto out_put_dev;
9356 }
9357
9358 fd = bpf_link_settle(&link_primer);
9359 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9360 dev_put(dev);
9361 return fd;
9362
9363out_put_dev:
9364 dev_put(dev);
9365 return err;
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009366}
9367
9368/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07009369 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9370 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07009371 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07009372 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009373 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01009374 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07009375 *
9376 * Set or clear a bpf program for a device
9377 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07009378int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009379 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07009380{
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009381 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009382 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07009383 int err;
9384
Daniel Borkmann85de8572016-11-28 23:16:54 +01009385 ASSERT_RTNL();
9386
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009387 if (fd >= 0) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009388 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9389 mode != XDP_MODE_SKB);
9390 if (IS_ERR(new_prog))
9391 return PTR_ERR(new_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009392 }
9393
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009394 if (expected_fd >= 0) {
9395 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9396 mode != XDP_MODE_SKB);
9397 if (IS_ERR(old_prog)) {
9398 err = PTR_ERR(old_prog);
9399 old_prog = NULL;
9400 goto err_out;
9401 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009402 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07009403
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009404 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009405
9406err_out:
9407 if (err && new_prog)
9408 bpf_prog_put(new_prog);
9409 if (old_prog)
9410 bpf_prog_put(old_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009411 return err;
9412}
Brenden Blancoa7862b42016-07-19 12:16:48 -07009413
9414/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07009416 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 *
9418 * Returns a suitable unique value for a new device interface
9419 * number. The caller must hold the rtnl semaphore or the
9420 * dev_base_lock to be sure it remains unique.
9421 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07009422static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009424 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11009425
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 for (;;) {
9427 if (++ifindex <= 0)
9428 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009429 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009430 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431 }
9432}
9433
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08009435static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07009436DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437
Stephen Hemminger6f05f622007-03-08 20:46:03 -08009438static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009441 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442}
9443
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009444static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009445{
Krishna Kumare93737b2009-12-08 22:26:02 +00009446 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009447 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009448
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009449 BUG_ON(dev_boot_phase);
9450 ASSERT_RTNL();
9451
Krishna Kumare93737b2009-12-08 22:26:02 +00009452 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009453 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00009454 * for initialization unwind. Remove those
9455 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009456 */
9457 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009458 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
9459 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009460
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009461 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00009462 list_del(&dev->unreg_list);
9463 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009464 }
Eric Dumazet449f4542011-05-19 12:24:16 +00009465 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009466 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00009467 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009468
Octavian Purdila44345722010-12-13 12:44:07 +00009469 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009470 list_for_each_entry(dev, head, unreg_list)
9471 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04009472 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009473
Octavian Purdila44345722010-12-13 12:44:07 +00009474 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009475 /* And unlink it from device chain. */
9476 unlist_netdevice(dev);
9477
9478 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009479 }
Eric Dumazet41852492016-08-26 12:50:39 -07009480 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009481
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009482 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009483
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009484 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009485 struct sk_buff *skb = NULL;
9486
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009487 /* Shutdown queueing discipline. */
9488 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009489
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009490 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009491
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009492 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009493 * this device. They should clean all the things.
9494 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009495 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
9496
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009497 if (!dev->rtnl_link_ops ||
9498 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04009499 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009500 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009501
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009502 /*
9503 * Flush the unicast and multicast chains
9504 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009505 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00009506 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009507
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02009508 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02009509 netdev_name_node_free(dev->name_node);
9510
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009511 if (dev->netdev_ops->ndo_uninit)
9512 dev->netdev_ops->ndo_uninit(dev);
9513
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009514 if (skb)
9515 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07009516
Jiri Pirko9ff162a2013-01-03 22:48:49 +00009517 /* Notifier chain MUST detach us all upper devices. */
9518 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07009519 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009520
9521 /* Remove entries from kobject tree */
9522 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00009523#ifdef CONFIG_XPS
9524 /* Remove XPS queueing entries */
9525 netif_reset_xps_queues_gt(dev, 0);
9526#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009527 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009528
Eric W. Biederman850a5452011-10-13 22:25:23 +00009529 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009530
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00009531 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009532 dev_put(dev);
9533}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009534
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009535static void rollback_registered(struct net_device *dev)
9536{
9537 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009538
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009539 list_add(&dev->unreg_list, &single);
9540 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00009541 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009542}
9543
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009544static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9545 struct net_device *upper, netdev_features_t features)
9546{
9547 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9548 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009549 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009550
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009551 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009552 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009553 if (!(upper->wanted_features & feature)
9554 && (features & feature)) {
9555 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9556 &feature, upper->name);
9557 features &= ~feature;
9558 }
9559 }
9560
9561 return features;
9562}
9563
9564static void netdev_sync_lower_features(struct net_device *upper,
9565 struct net_device *lower, netdev_features_t features)
9566{
9567 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9568 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009569 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009570
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009571 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009572 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009573 if (!(features & feature) && (lower->features & feature)) {
9574 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9575 &feature, lower->name);
9576 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07009577 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009578
9579 if (unlikely(lower->features & feature))
9580 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9581 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07009582 else
9583 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009584 }
9585 }
9586}
9587
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009588static netdev_features_t netdev_fix_features(struct net_device *dev,
9589 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07009590{
Michał Mirosław57422dc2011-01-22 12:14:12 +00009591 /* Fix illegal checksum combinations */
9592 if ((features & NETIF_F_HW_CSUM) &&
9593 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009594 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00009595 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9596 }
9597
Herbert Xub63365a2008-10-23 01:11:29 -07009598 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009599 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009600 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009601 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07009602 }
9603
Pravin B Shelarec5f0612013-03-07 09:28:01 +00009604 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9605 !(features & NETIF_F_IP_CSUM)) {
9606 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9607 features &= ~NETIF_F_TSO;
9608 features &= ~NETIF_F_TSO_ECN;
9609 }
9610
9611 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9612 !(features & NETIF_F_IPV6_CSUM)) {
9613 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9614 features &= ~NETIF_F_TSO6;
9615 }
9616
Alexander Duyckb1dc4972016-05-02 09:38:24 -07009617 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9618 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9619 features &= ~NETIF_F_TSO_MANGLEID;
9620
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00009621 /* TSO ECN requires that TSO is present as well. */
9622 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9623 features &= ~NETIF_F_TSO_ECN;
9624
Michał Mirosław212b5732011-02-15 16:59:16 +00009625 /* Software GSO depends on SG. */
9626 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009627 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00009628 features &= ~NETIF_F_GSO;
9629 }
9630
Alexander Duyck802ab552016-04-10 21:45:03 -04009631 /* GSO partial features require GSO partial be set */
9632 if ((features & dev->gso_partial_features) &&
9633 !(features & NETIF_F_GSO_PARTIAL)) {
9634 netdev_dbg(dev,
9635 "Dropping partially supported GSO features since no GSO partial.\n");
9636 features &= ~dev->gso_partial_features;
9637 }
9638
Michael Chanfb1f5f72017-12-16 03:09:40 -05009639 if (!(features & NETIF_F_RXCSUM)) {
9640 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9641 * successfully merged by hardware must also have the
9642 * checksum verified by hardware. If the user does not
9643 * want to enable RXCSUM, logically, we should disable GRO_HW.
9644 */
9645 if (features & NETIF_F_GRO_HW) {
9646 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9647 features &= ~NETIF_F_GRO_HW;
9648 }
9649 }
9650
Gal Pressmande8d5ab2018-03-12 11:48:49 +02009651 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9652 if (features & NETIF_F_RXFCS) {
9653 if (features & NETIF_F_LRO) {
9654 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9655 features &= ~NETIF_F_LRO;
9656 }
9657
9658 if (features & NETIF_F_GRO_HW) {
9659 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9660 features &= ~NETIF_F_GRO_HW;
9661 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02009662 }
9663
Tariq Toukanae0b04b2020-12-13 16:39:29 +02009664 if ((features & NETIF_F_HW_TLS_TX) && !(features & NETIF_F_HW_CSUM)) {
9665 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
9666 features &= ~NETIF_F_HW_TLS_TX;
9667 }
9668
Herbert Xub63365a2008-10-23 01:11:29 -07009669 return features;
9670}
Herbert Xub63365a2008-10-23 01:11:29 -07009671
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009672int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009673{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009674 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009675 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009676 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009677 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009678
Michał Mirosław87267482011-04-12 09:56:38 +00009679 ASSERT_RTNL();
9680
Michał Mirosław5455c692011-02-15 16:59:17 +00009681 features = netdev_get_wanted_features(dev);
9682
9683 if (dev->netdev_ops->ndo_fix_features)
9684 features = dev->netdev_ops->ndo_fix_features(dev, features);
9685
9686 /* driver might be less strict about feature dependencies */
9687 features = netdev_fix_features(dev, features);
9688
Randy Dunlap4250b752020-09-17 21:35:15 -07009689 /* some features can't be enabled if they're off on an upper device */
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009690 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9691 features = netdev_sync_upper_features(dev, upper, features);
9692
Michał Mirosław5455c692011-02-15 16:59:17 +00009693 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009694 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009695
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009696 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9697 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009698
9699 if (dev->netdev_ops->ndo_set_features)
9700 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009701 else
9702 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009703
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009704 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009705 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009706 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9707 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009708 /* return non-0 since some features might have changed and
9709 * it's better to fire a spurious notification than miss it
9710 */
9711 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009712 }
9713
Jarod Wilsone7868a82015-11-03 23:09:32 -05009714sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009715 /* some features must be disabled on lower devices when disabled
9716 * on an upper device (think: bonding master or bridge)
9717 */
9718 netdev_for_each_lower_dev(dev, lower, iter)
9719 netdev_sync_lower_features(dev, lower, features);
9720
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009721 if (!err) {
9722 netdev_features_t diff = features ^ dev->features;
9723
9724 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9725 /* udp_tunnel_{get,drop}_rx_info both need
9726 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9727 * device, or they won't do anything.
9728 * Thus we need to update dev->features
9729 * *before* calling udp_tunnel_get_rx_info,
9730 * but *after* calling udp_tunnel_drop_rx_info.
9731 */
9732 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9733 dev->features = features;
9734 udp_tunnel_get_rx_info(dev);
9735 } else {
9736 udp_tunnel_drop_rx_info(dev);
9737 }
9738 }
9739
Gal Pressman9daae9b2018-03-28 17:46:54 +03009740 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9741 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9742 dev->features = features;
9743 err |= vlan_get_rx_ctag_filter_info(dev);
9744 } else {
9745 vlan_drop_rx_ctag_filter_info(dev);
9746 }
9747 }
9748
9749 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9750 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9751 dev->features = features;
9752 err |= vlan_get_rx_stag_filter_info(dev);
9753 } else {
9754 vlan_drop_rx_stag_filter_info(dev);
9755 }
9756 }
9757
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009758 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009759 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009760
Jarod Wilsone7868a82015-11-03 23:09:32 -05009761 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009762}
9763
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009764/**
9765 * netdev_update_features - recalculate device features
9766 * @dev: the device to check
9767 *
9768 * Recalculate dev->features set and send notifications if it
9769 * has changed. Should be called after driver or hardware dependent
9770 * conditions might have changed that influence the features.
9771 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009772void netdev_update_features(struct net_device *dev)
9773{
9774 if (__netdev_update_features(dev))
9775 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009776}
9777EXPORT_SYMBOL(netdev_update_features);
9778
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009780 * netdev_change_features - recalculate device features
9781 * @dev: the device to check
9782 *
9783 * Recalculate dev->features set and send notifications even
9784 * if they have not changed. Should be called instead of
9785 * netdev_update_features() if also dev->vlan_features might
9786 * have changed to allow the changes to be propagated to stacked
9787 * VLAN devices.
9788 */
9789void netdev_change_features(struct net_device *dev)
9790{
9791 __netdev_update_features(dev);
9792 netdev_features_change(dev);
9793}
9794EXPORT_SYMBOL(netdev_change_features);
9795
9796/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009797 * netif_stacked_transfer_operstate - transfer operstate
9798 * @rootdev: the root or lower level device to transfer state from
9799 * @dev: the device to transfer operstate to
9800 *
9801 * Transfer operational state from root to device. This is normally
9802 * called when a stacking relationship exists between the root
9803 * device and the device(a leaf device).
9804 */
9805void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9806 struct net_device *dev)
9807{
9808 if (rootdev->operstate == IF_OPER_DORMANT)
9809 netif_dormant_on(dev);
9810 else
9811 netif_dormant_off(dev);
9812
Andrew Lunneec517cd2020-04-20 00:11:50 +02009813 if (rootdev->operstate == IF_OPER_TESTING)
9814 netif_testing_on(dev);
9815 else
9816 netif_testing_off(dev);
9817
Zhang Shengju0575c862017-04-26 17:49:38 +08009818 if (netif_carrier_ok(rootdev))
9819 netif_carrier_on(dev);
9820 else
9821 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009822}
9823EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9824
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009825static int netif_alloc_rx_queues(struct net_device *dev)
9826{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009827 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009828 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309829 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009830 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009831
Tom Herbertbd25fa72010-10-18 18:00:16 +00009832 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009833
Michal Hockodcda9b02017-07-12 14:36:45 -07009834 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009835 if (!rx)
9836 return -ENOMEM;
9837
Tom Herbertbd25fa72010-10-18 18:00:16 +00009838 dev->_rx = rx;
9839
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009840 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009841 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009842
9843 /* XDP RX-queue setup */
Björn Töpelb02e5a02020-11-30 19:52:01 +01009844 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009845 if (err < 0)
9846 goto err_rxq_info;
9847 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009848 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009849
9850err_rxq_info:
9851 /* Rollback successful reg's and free other resources */
9852 while (i--)
9853 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009854 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009855 dev->_rx = NULL;
9856 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009857}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009858
9859static void netif_free_rx_queues(struct net_device *dev)
9860{
9861 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009862
9863 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9864 if (!dev->_rx)
9865 return;
9866
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009867 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009868 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9869
9870 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009871}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009872
Changli Gaoaa942102010-12-04 02:31:41 +00009873static void netdev_init_one_queue(struct net_device *dev,
9874 struct netdev_queue *queue, void *_unused)
9875{
9876 /* Initialize queue lock */
9877 spin_lock_init(&queue->_xmit_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07009878 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
Changli Gaoaa942102010-12-04 02:31:41 +00009879 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009880 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009881 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009882#ifdef CONFIG_BQL
9883 dql_init(&queue->dql, HZ);
9884#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009885}
9886
Eric Dumazet60877a32013-06-20 01:15:51 -07009887static void netif_free_tx_queues(struct net_device *dev)
9888{
WANG Cong4cb28972014-06-02 15:55:22 -07009889 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009890}
9891
Tom Herberte6484932010-10-18 18:04:39 +00009892static int netif_alloc_netdev_queues(struct net_device *dev)
9893{
9894 unsigned int count = dev->num_tx_queues;
9895 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009896 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009897
Eric Dumazetd3397272015-07-06 17:13:26 +02009898 if (count < 1 || count > 0xffff)
9899 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009900
Michal Hockodcda9b02017-07-12 14:36:45 -07009901 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009902 if (!tx)
9903 return -ENOMEM;
9904
Tom Herberte6484932010-10-18 18:04:39 +00009905 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009906
Tom Herberte6484932010-10-18 18:04:39 +00009907 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9908 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009909
9910 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009911}
9912
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009913void netif_tx_stop_all_queues(struct net_device *dev)
9914{
9915 unsigned int i;
9916
9917 for (i = 0; i < dev->num_tx_queues; i++) {
9918 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009919
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009920 netif_tx_stop_queue(txq);
9921 }
9922}
9923EXPORT_SYMBOL(netif_tx_stop_all_queues);
9924
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009925/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 * register_netdevice - register a network device
9927 * @dev: device to register
9928 *
9929 * Take a completed network device structure and add it to the kernel
9930 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9931 * chain. 0 is returned on success. A negative errno code is returned
9932 * on a failure to set up the device, or if the name is a duplicate.
9933 *
9934 * Callers must hold the rtnl semaphore. You may want
9935 * register_netdev() instead of this.
9936 *
9937 * BUGS:
9938 * The locking appears insufficient to guarantee two parallel registers
9939 * will not get the same name.
9940 */
9941
9942int register_netdevice(struct net_device *dev)
9943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009945 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946
Florian Fainellie283de32018-04-30 14:20:05 -07009947 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9948 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949 BUG_ON(dev_boot_phase);
9950 ASSERT_RTNL();
9951
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009952 might_sleep();
9953
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954 /* When net_device's are persistent, this will be fatal. */
9955 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009956 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957
Jakub Kicinski9000edb2020-03-16 13:47:12 -07009958 ret = ethtool_check_ops(dev->ethtool_ops);
9959 if (ret)
9960 return ret;
9961
David S. Millerf1f28aa2008-07-15 00:08:33 -07009962 spin_lock_init(&dev->addr_list_lock);
Cong Wang845e0eb2020-06-08 14:53:01 -07009963 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964
Gao feng828de4f2012-09-13 20:58:27 +00009965 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009966 if (ret < 0)
9967 goto out;
9968
Eric Dumazet9077f052019-10-03 08:59:24 -07009969 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009970 dev->name_node = netdev_name_node_head_alloc(dev);
9971 if (!dev->name_node)
9972 goto out;
9973
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009975 if (dev->netdev_ops->ndo_init) {
9976 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 if (ret) {
9978 if (ret > 0)
9979 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009980 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009981 }
9982 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009983
Patrick McHardyf6469682013-04-19 02:04:27 +00009984 if (((dev->hw_features | dev->features) &
9985 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009986 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9987 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9988 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9989 ret = -EINVAL;
9990 goto err_uninit;
9991 }
9992
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009993 ret = -EBUSY;
9994 if (!dev->ifindex)
9995 dev->ifindex = dev_new_index(net);
9996 else if (__dev_get_by_index(net, dev->ifindex))
9997 goto err_uninit;
9998
Michał Mirosław5455c692011-02-15 16:59:17 +00009999 /* Transfer changeable features to wanted_features and enable
10000 * software offloads (GSO and GRO).
10001 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +010010002 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +000010003 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +020010004
10005 if (dev->netdev_ops->ndo_udp_tunnel_add) {
10006 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10007 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10008 }
10009
Michał Mirosław14d12322011-02-22 16:52:28 +000010010 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010012 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +000010013 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010014
Alexander Duyck7f348a62016-04-20 16:51:00 -040010015 /* If IPv4 TCP segmentation offload is supported we should also
10016 * allow the device to enable segmenting the frame with the option
10017 * of ignoring a static IP ID value. This doesn't enable the
10018 * feature itself but allows the user to enable it later.
10019 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010020 if (dev->hw_features & NETIF_F_TSO)
10021 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -040010022 if (dev->vlan_features & NETIF_F_TSO)
10023 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
10024 if (dev->mpls_features & NETIF_F_TSO)
10025 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
10026 if (dev->hw_enc_features & NETIF_F_TSO)
10027 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -070010028
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010029 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +000010030 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010031 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +000010032
Pravin B Shelaree579672013-03-07 09:28:08 +000010033 /* Make NETIF_F_SG inheritable to tunnel devices.
10034 */
Alexander Duyck802ab552016-04-10 21:45:03 -040010035 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +000010036
Simon Horman0d89d202013-05-23 21:02:52 +000010037 /* Make NETIF_F_SG inheritable to MPLS.
10038 */
10039 dev->mpls_features |= NETIF_F_SG;
10040
Johannes Berg7ffbe3f2009-10-02 05:15:27 +000010041 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
10042 ret = notifier_to_errno(ret);
10043 if (ret)
10044 goto err_uninit;
10045
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010046 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010047 if (ret) {
10048 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010049 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010050 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010051 dev->reg_state = NETREG_REGISTERED;
10052
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010053 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +000010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 /*
10056 * Default initial state at registry is that the
10057 * device is present.
10058 */
10059
10060 set_bit(__LINK_STATE_PRESENT, &dev->state);
10061
Ben Hutchings8f4cccb2012-08-20 22:16:51 +010010062 linkwatch_init_dev(dev);
10063
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010066 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -040010067 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068
Jiri Pirko948b3372013-01-08 01:38:25 +000010069 /* If the device has permanent device address, driver should
10070 * set dev_addr and also addr_assign_type should be set to
10071 * NET_ADDR_PERM (default value).
10072 */
10073 if (dev->addr_assign_type == NET_ADDR_PERM)
10074 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10075
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010077 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -070010078 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010079 if (ret) {
10080 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -060010081 rcu_barrier();
10082
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010083 dev->reg_state = NETREG_UNREGISTERED;
Yang Yingliang814152a2020-06-16 09:39:21 +000010084 /* We should put the kobject that hold in
10085 * netdev_unregister_kobject(), otherwise
10086 * the net device cannot be freed when
10087 * driver calls free_netdev(), because the
10088 * kobject is being hold.
10089 */
10090 kobject_put(&dev->dev.kobj);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010091 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010092 /*
10093 * Prevent userspace races by waiting until the network
10094 * device is fully setup before sending notifications.
10095 */
Patrick McHardya2835762010-02-26 06:34:51 +000010096 if (!dev->rtnl_link_ops ||
10097 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010098 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099
10100out:
10101 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010102
10103err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010104 if (dev->netdev_ops->ndo_uninit)
10105 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -040010106 if (dev->priv_destructor)
10107 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010108err_free_name:
10109 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010112EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
10114/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010115 * init_dummy_netdev - init a dummy network device for NAPI
10116 * @dev: device to init
10117 *
10118 * This takes a network device structure and initialize the minimum
10119 * amount of fields so it can be used to schedule NAPI polls without
10120 * registering a full blown interface. This is to be used by drivers
10121 * that need to tie several hardware interfaces to a single NAPI
10122 * poll scheduler due to HW limitations.
10123 */
10124int init_dummy_netdev(struct net_device *dev)
10125{
10126 /* Clear everything. Note we don't initialize spinlocks
10127 * are they aren't supposed to be taken by any of the
10128 * NAPI code and this dummy netdev is supposed to be
10129 * only ever used for NAPI polls
10130 */
10131 memset(dev, 0, sizeof(struct net_device));
10132
10133 /* make sure we BUG if trying to hit standard
10134 * register/unregister code path
10135 */
10136 dev->reg_state = NETREG_DUMMY;
10137
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010138 /* NAPI wants this */
10139 INIT_LIST_HEAD(&dev->napi_list);
10140
10141 /* a dummy interface is started by default */
10142 set_bit(__LINK_STATE_PRESENT, &dev->state);
10143 set_bit(__LINK_STATE_START, &dev->state);
10144
Josh Elsasser35edfdc2019-01-26 14:38:33 -080010145 /* napi_busy_loop stats accounting wants this */
10146 dev_net_set(dev, &init_net);
10147
Eric Dumazet29b44332010-10-11 10:22:12 +000010148 /* Note : We dont allocate pcpu_refcnt for dummy devices,
10149 * because users of this 'device' dont need to change
10150 * its refcount.
10151 */
10152
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010153 return 0;
10154}
10155EXPORT_SYMBOL_GPL(init_dummy_netdev);
10156
10157
10158/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 * register_netdev - register a network device
10160 * @dev: device to register
10161 *
10162 * Take a completed network device structure and add it to the kernel
10163 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10164 * chain. 0 is returned on success. A negative errno code is returned
10165 * on a failure to set up the device, or if the name is a duplicate.
10166 *
Borislav Petkov38b4da382007-04-20 22:14:10 -070010167 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168 * and expands the device name if you passed a format string to
10169 * alloc_netdev.
10170 */
10171int register_netdev(struct net_device *dev)
10172{
10173 int err;
10174
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +030010175 if (rtnl_lock_killable())
10176 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 rtnl_unlock();
10179 return err;
10180}
10181EXPORT_SYMBOL(register_netdev);
10182
Eric Dumazet29b44332010-10-11 10:22:12 +000010183int netdev_refcnt_read(const struct net_device *dev)
10184{
10185 int i, refcnt = 0;
10186
10187 for_each_possible_cpu(i)
10188 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10189 return refcnt;
10190}
10191EXPORT_SYMBOL(netdev_refcnt_read);
10192
Mauro Carvalho Chehabde2b5412020-09-22 13:22:52 +020010193#define WAIT_REFS_MIN_MSECS 1
10194#define WAIT_REFS_MAX_MSECS 250
Ben Hutchings2c530402012-07-10 10:55:09 +000010195/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +000010197 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 *
10199 * This is called when unregistering network devices.
10200 *
10201 * Any protocol or device that holds a reference should register
10202 * for netdevice notification, and cleanup and put back the
10203 * reference if they receive an UNREGISTER event.
10204 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010205 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 */
10207static void netdev_wait_allrefs(struct net_device *dev)
10208{
10209 unsigned long rebroadcast_time, warning_time;
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010210 int wait = 0, refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211
Eric Dumazete014deb2009-11-17 05:59:21 +000010212 linkwatch_forget_dev(dev);
10213
Linus Torvalds1da177e2005-04-16 15:20:36 -070010214 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +000010215 refcnt = netdev_refcnt_read(dev);
10216
10217 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010219 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
10221 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010222 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223
Eric Dumazet748e2d92012-08-22 21:50:59 +000010224 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010225 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +000010226 rtnl_lock();
10227
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10229 &dev->state)) {
10230 /* We must not have linkwatch events
10231 * pending on unregister. If this
10232 * happens, we simply run the queue
10233 * unscheduled, resulting in a noop
10234 * for this device.
10235 */
10236 linkwatch_run_queue();
10237 }
10238
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010239 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240
10241 rebroadcast_time = jiffies;
10242 }
10243
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010244 if (!wait) {
10245 rcu_barrier();
10246 wait = WAIT_REFS_MIN_MSECS;
10247 } else {
10248 msleep(wait);
10249 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251
Eric Dumazet29b44332010-10-11 10:22:12 +000010252 refcnt = netdev_refcnt_read(dev);
10253
Eric Dumazetd7c04b02019-05-16 08:09:57 -070010254 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010255 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10256 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257 warning_time = jiffies;
10258 }
10259 }
10260}
10261
10262/* The sequence is:
10263 *
10264 * rtnl_lock();
10265 * ...
10266 * register_netdevice(x1);
10267 * register_netdevice(x2);
10268 * ...
10269 * unregister_netdevice(y1);
10270 * unregister_netdevice(y2);
10271 * ...
10272 * rtnl_unlock();
10273 * free_netdev(y1);
10274 * free_netdev(y2);
10275 *
Herbert Xu58ec3b42008-10-07 15:50:03 -070010276 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010278 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279 * without deadlocking with linkwatch via keventd.
10280 * 2) Since we run with the RTNL semaphore not held, we can sleep
10281 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -070010282 *
10283 * We must not return until all unregister events added during
10284 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286void netdev_run_todo(void)
10287{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010288 struct list_head list;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010289#ifdef CONFIG_LOCKDEP
10290 struct list_head unlink_list;
10291
10292 list_replace_init(&net_unlink_list, &unlink_list);
10293
10294 while (!list_empty(&unlink_list)) {
10295 struct net_device *dev = list_first_entry(&unlink_list,
10296 struct net_device,
10297 unlink_list);
Taehee Yoo0e8b8d62020-10-15 16:26:06 +000010298 list_del_init(&dev->unlink_list);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010299 dev->nested_level = dev->lower_level - 1;
10300 }
10301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010304 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -070010305
10306 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010307
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010308
10309 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +000010310 if (!list_empty(&list))
10311 rcu_barrier();
10312
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 while (!list_empty(&list)) {
10314 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +000010315 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 list_del(&dev->todo_list);
10317
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010318 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010319 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010321 dump_stack();
10322 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010324
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010325 dev->reg_state = NETREG_UNREGISTERED;
10326
10327 netdev_wait_allrefs(dev);
10328
10329 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +000010330 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -080010331 BUG_ON(!list_empty(&dev->ptype_all));
10332 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +000010333 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10334 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -080010335#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -070010336 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -080010337#endif
David S. Millercf124db2017-05-08 12:52:56 -040010338 if (dev->priv_destructor)
10339 dev->priv_destructor(dev);
10340 if (dev->needs_free_netdev)
10341 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010342
Eric W. Biederman50624c92013-09-23 21:19:49 -070010343 /* Report a network device has been unregistered */
10344 rtnl_lock();
10345 dev_net(dev)->dev_unreg_count--;
10346 __rtnl_unlock();
10347 wake_up(&netdev_unregistering_wq);
10348
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010349 /* Free network device */
10350 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352}
10353
Jarod Wilson92566452016-02-01 18:51:04 -050010354/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
10355 * all the same fields in the same order as net_device_stats, with only
10356 * the type differing, but rtnl_link_stats64 may have additional fields
10357 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +000010358 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010359void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
10360 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +000010361{
10362#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -050010363 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +020010364 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -050010365 /* zero out counters that only exist in rtnl_link_stats64 */
10366 memset((char *)stats64 + sizeof(*netdev_stats), 0,
10367 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010368#else
Jarod Wilson92566452016-02-01 18:51:04 -050010369 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010370 const unsigned long *src = (const unsigned long *)netdev_stats;
10371 u64 *dst = (u64 *)stats64;
10372
Jarod Wilson92566452016-02-01 18:51:04 -050010373 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010374 for (i = 0; i < n; i++)
10375 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -050010376 /* zero out counters that only exist in rtnl_link_stats64 */
10377 memset((char *)stats64 + n * sizeof(u64), 0,
10378 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010379#endif
10380}
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010381EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010382
Eric Dumazetd83345a2009-11-16 03:36:51 +000010383/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010384 * dev_get_stats - get network device statistics
10385 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -070010386 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010387 *
Ben Hutchingsd7753512010-07-09 09:12:41 +000010388 * Get network statistics from device. Return @storage.
10389 * The device driver may provide its own method by setting
10390 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10391 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010392 */
Ben Hutchingsd7753512010-07-09 09:12:41 +000010393struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10394 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +000010395{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010396 const struct net_device_ops *ops = dev->netdev_ops;
10397
Eric Dumazet28172732010-07-07 14:58:56 -070010398 if (ops->ndo_get_stats64) {
10399 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010400 ops->ndo_get_stats64(dev, storage);
10401 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +000010402 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010403 } else {
10404 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -070010405 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -070010406 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10407 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10408 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -070010409 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -070010410}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010411EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -070010412
Heiner Kallweit44fa32f2020-10-12 10:01:27 +020010413/**
10414 * dev_fetch_sw_netstats - get per-cpu network device statistics
10415 * @s: place to store stats
10416 * @netstats: per-cpu network stats to read from
10417 *
10418 * Read per-cpu network statistics and populate the related fields in @s.
10419 */
10420void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10421 const struct pcpu_sw_netstats __percpu *netstats)
10422{
10423 int cpu;
10424
10425 for_each_possible_cpu(cpu) {
10426 const struct pcpu_sw_netstats *stats;
10427 struct pcpu_sw_netstats tmp;
10428 unsigned int start;
10429
10430 stats = per_cpu_ptr(netstats, cpu);
10431 do {
10432 start = u64_stats_fetch_begin_irq(&stats->syncp);
10433 tmp.rx_packets = stats->rx_packets;
10434 tmp.rx_bytes = stats->rx_bytes;
10435 tmp.tx_packets = stats->tx_packets;
10436 tmp.tx_bytes = stats->tx_bytes;
10437 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10438
10439 s->rx_packets += tmp.rx_packets;
10440 s->rx_bytes += tmp.rx_bytes;
10441 s->tx_packets += tmp.tx_packets;
10442 s->tx_bytes += tmp.tx_bytes;
10443 }
10444}
10445EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10446
Heiner Kallweita1839422020-11-07 21:49:07 +010010447/**
10448 * dev_get_tstats64 - ndo_get_stats64 implementation
10449 * @dev: device to get statistics from
10450 * @s: place to store stats
10451 *
10452 * Populate @s from dev->stats and dev->tstats. Can be used as
10453 * ndo_get_stats64() callback.
10454 */
10455void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
10456{
10457 netdev_stats_to_stats64(s, &dev->stats);
10458 dev_fetch_sw_netstats(s, dev->tstats);
10459}
10460EXPORT_SYMBOL_GPL(dev_get_tstats64);
10461
Eric Dumazet24824a02010-10-02 06:11:55 +000010462struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -070010463{
Eric Dumazet24824a02010-10-02 06:11:55 +000010464 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -070010465
Eric Dumazet24824a02010-10-02 06:11:55 +000010466#ifdef CONFIG_NET_CLS_ACT
10467 if (queue)
10468 return queue;
10469 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10470 if (!queue)
10471 return NULL;
10472 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -080010473 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +000010474 queue->qdisc_sleeping = &noop_qdisc;
10475 rcu_assign_pointer(dev->ingress_queue, queue);
10476#endif
10477 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -070010478}
10479
Eric Dumazet2c60db02012-09-16 09:17:26 +000010480static const struct ethtool_ops default_ethtool_ops;
10481
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +000010482void netdev_set_default_ethtool_ops(struct net_device *dev,
10483 const struct ethtool_ops *ops)
10484{
10485 if (dev->ethtool_ops == &default_ethtool_ops)
10486 dev->ethtool_ops = ops;
10487}
10488EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10489
Eric Dumazet74d332c2013-10-30 13:10:44 -070010490void netdev_freemem(struct net_device *dev)
10491{
10492 char *addr = (char *)dev - dev->padded;
10493
WANG Cong4cb28972014-06-02 15:55:22 -070010494 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010495}
10496
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497/**
tcharding722c9a02017-02-09 17:56:04 +110010498 * alloc_netdev_mqs - allocate network device
10499 * @sizeof_priv: size of private data to allocate space for
10500 * @name: device name format string
10501 * @name_assign_type: origin of device name
10502 * @setup: callback to initialize device
10503 * @txqs: the number of TX subqueues to allocate
10504 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505 *
tcharding722c9a02017-02-09 17:56:04 +110010506 * Allocates a struct net_device with private data area for driver use
10507 * and performs basic initialization. Also allocates subqueue structs
10508 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 */
Tom Herbert36909ea2011-01-09 19:36:31 +000010510struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +020010511 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +000010512 void (*setup)(struct net_device *),
10513 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010514{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010515 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +030010516 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010517 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -070010519 BUG_ON(strlen(name) >= sizeof(dev->name));
10520
Tom Herbert36909ea2011-01-09 19:36:31 +000010521 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010522 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +000010523 return NULL;
10524 }
10525
Tom Herbert36909ea2011-01-09 19:36:31 +000010526 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010527 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +000010528 return NULL;
10529 }
Tom Herbert36909ea2011-01-09 19:36:31 +000010530
David S. Millerfd2ea0a2008-07-17 01:56:23 -070010531 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010532 if (sizeof_priv) {
10533 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010534 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010535 alloc_size += sizeof_priv;
10536 }
10537 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010538 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539
Michal Hockodcda9b02017-07-12 14:36:45 -070010540 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +000010541 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010544 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010546
Eric Dumazet29b44332010-10-11 10:22:12 +000010547 dev->pcpu_refcnt = alloc_percpu(int);
10548 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -070010549 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010550
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +000010552 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553
Jiri Pirko22bedad32010-04-01 21:22:57 +000010554 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010555 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +000010556
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010557 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -070010559 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +000010560 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +000010561 dev->upper_level = 1;
10562 dev->lower_level = 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010563#ifdef CONFIG_LOCKDEP
10564 dev->nested_level = 0;
10565 INIT_LIST_HEAD(&dev->unlink_list);
10566#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567
Herbert Xud565b0a2008-12-15 23:38:52 -080010568 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010569 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -070010570 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +000010571 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +020010572 INIT_LIST_HEAD(&dev->adj_list.upper);
10573 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -080010574 INIT_LIST_HEAD(&dev->ptype_all);
10575 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +010010576 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +020010577#ifdef CONFIG_NET_SCHED
10578 hash_init(dev->qdisc_hash);
10579#endif
Eric Dumazet02875872014-10-05 18:38:35 -070010580 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010582
Phil Suttera8131042016-02-17 15:37:43 +010010583 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +020010584 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +010010585 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +010010586 }
Phil Sutter906470c2015-08-18 10:30:48 +020010587
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010588 dev->num_tx_queues = txqs;
10589 dev->real_num_tx_queues = txqs;
10590 if (netif_alloc_netdev_queues(dev))
10591 goto free_all;
10592
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010593 dev->num_rx_queues = rxqs;
10594 dev->real_num_rx_queues = rxqs;
10595 if (netif_alloc_rx_queues(dev))
10596 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010597
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +020010599 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +000010600 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +000010601 if (!dev->ethtool_ops)
10602 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +020010603
Daniel Borkmann357b6cc2020-03-18 10:33:22 +010010604 nf_hook_ingress_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +020010605
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010607
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010608free_all:
10609 free_netdev(dev);
10610 return NULL;
10611
Eric Dumazet29b44332010-10-11 10:22:12 +000010612free_pcpu:
10613 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010614free_dev:
10615 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010616 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617}
Tom Herbert36909ea2011-01-09 19:36:31 +000010618EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619
10620/**
tcharding722c9a02017-02-09 17:56:04 +110010621 * free_netdev - free network device
10622 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010623 *
tcharding722c9a02017-02-09 17:56:04 +110010624 * This function does the last stage of destroying an allocated device
10625 * interface. The reference to the device object is released. If this
10626 * is the last reference then it will be freed.Must be called in process
10627 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628 */
10629void free_netdev(struct net_device *dev)
10630{
Herbert Xud565b0a2008-12-15 23:38:52 -080010631 struct napi_struct *p, *n;
10632
Eric Dumazet93d05d42015-11-18 06:31:03 -080010633 might_sleep();
Jakub Kicinskic269a242021-01-06 10:40:06 -080010634
10635 /* When called immediately after register_netdevice() failed the unwind
10636 * handling may still be dismantling the device. Handle that case by
10637 * deferring the free.
10638 */
10639 if (dev->reg_state == NETREG_UNREGISTERING) {
10640 ASSERT_RTNL();
10641 dev->needs_free_netdev = true;
10642 return;
10643 }
10644
Eric Dumazet60877a32013-06-20 01:15:51 -070010645 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010646 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -070010647
Eric Dumazet33d480c2011-08-11 19:30:52 +000010648 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +000010649
Jiri Pirkof001fde2009-05-05 02:48:28 +000010650 /* Flush device addresses */
10651 dev_addr_flush(dev);
10652
Herbert Xud565b0a2008-12-15 23:38:52 -080010653 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10654 netif_napi_del(p);
10655
Eric Dumazet29b44332010-10-11 10:22:12 +000010656 free_percpu(dev->pcpu_refcnt);
10657 dev->pcpu_refcnt = NULL;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010010658 free_percpu(dev->xdp_bulkq);
10659 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +000010660
Stephen Hemminger3041a062006-05-26 13:25:24 -070010661 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010662 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -070010663 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664 return;
10665 }
10666
10667 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10668 dev->reg_state = NETREG_RELEASED;
10669
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070010670 /* will free via device release */
10671 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010672}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010673EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010674
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010675/**
10676 * synchronize_net - Synchronize with packet receive processing
10677 *
10678 * Wait for packets currently being received to be done.
10679 * Does not block later packets from starting.
10680 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010681void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682{
10683 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +000010684 if (rtnl_is_locked())
10685 synchronize_rcu_expedited();
10686 else
10687 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010689EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010690
10691/**
Eric Dumazet44a08732009-10-27 07:03:04 +000010692 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +000010694 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -080010695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010696 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010697 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +000010698 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699 *
10700 * Callers must hold the rtnl semaphore. You may want
10701 * unregister_netdev() instead of this.
10702 */
10703
Eric Dumazet44a08732009-10-27 07:03:04 +000010704void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010705{
Herbert Xua6620712007-12-12 19:21:56 -080010706 ASSERT_RTNL();
10707
Eric Dumazet44a08732009-10-27 07:03:04 +000010708 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010709 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +000010710 } else {
10711 rollback_registered(dev);
10712 /* Finish processing unregister after unlock */
10713 net_set_todo(dev);
10714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010715}
Eric Dumazet44a08732009-10-27 07:03:04 +000010716EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717
10718/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010719 * unregister_netdevice_many - unregister many devices
10720 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -070010721 *
10722 * Note: As most callers use a stack allocated list_head,
10723 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010724 */
10725void unregister_netdevice_many(struct list_head *head)
10726{
10727 struct net_device *dev;
10728
10729 if (!list_empty(head)) {
10730 rollback_registered_many(head);
10731 list_for_each_entry(dev, head, unreg_list)
10732 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -070010733 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010734 }
10735}
Eric Dumazet63c80992009-10-27 07:06:49 +000010736EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010737
10738/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739 * unregister_netdev - remove device from the kernel
10740 * @dev: device
10741 *
10742 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010743 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744 *
10745 * This is just a wrapper for unregister_netdevice that takes
10746 * the rtnl semaphore. In general you want to use this and not
10747 * unregister_netdevice.
10748 */
10749void unregister_netdev(struct net_device *dev)
10750{
10751 rtnl_lock();
10752 unregister_netdevice(dev);
10753 rtnl_unlock();
10754}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755EXPORT_SYMBOL(unregister_netdev);
10756
Eric W. Biedermance286d32007-09-12 13:53:49 +020010757/**
10758 * dev_change_net_namespace - move device to different nethost namespace
10759 * @dev: device
10760 * @net: network namespace
10761 * @pat: If not NULL name pattern to try if the current device name
10762 * is already taken in the destination network namespace.
10763 *
10764 * This function shuts down a device interface and moves it
10765 * to a new network namespace. On success 0 is returned, on
10766 * a failure a netagive errno code is returned.
10767 *
10768 * Callers must hold the rtnl semaphore.
10769 */
10770
10771int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10772{
Christian Brauneref6a4c82020-02-27 04:37:19 +010010773 struct net *net_old = dev_net(dev);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010774 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010775
10776 ASSERT_RTNL();
10777
10778 /* Don't allow namespace local devices to be moved. */
10779 err = -EINVAL;
10780 if (dev->features & NETIF_F_NETNS_LOCAL)
10781 goto out;
10782
10783 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010784 if (dev->reg_state != NETREG_REGISTERED)
10785 goto out;
10786
10787 /* Get out if there is nothing todo */
10788 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010010789 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010790 goto out;
10791
10792 /* Pick the destination device name, and ensure
10793 * we can use it in the destination network namespace.
10794 */
10795 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000010796 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010797 /* We get here if we can't use the current device name */
10798 if (!pat)
10799 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010800 err = dev_get_valid_name(net, dev, pat);
10801 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010802 goto out;
10803 }
10804
10805 /*
10806 * And now a mini version of register_netdevice unregister_netdevice.
10807 */
10808
10809 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010810 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010811
10812 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010813 unlist_netdevice(dev);
10814
10815 synchronize_net();
10816
10817 /* Shutdown queueing discipline. */
10818 dev_shutdown(dev);
10819
10820 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010821 * this device. They should clean all the things.
10822 *
10823 * Note that dev->reg_state stays at NETREG_REGISTERED.
10824 * This is wanted because this way 8021q and macvlan know
10825 * the device is just moving and can keep their slaves up.
10826 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010827 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010828 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010829
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010830 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010831 /* If there is an ifindex conflict assign a new one */
10832 if (__dev_get_by_index(net, dev->ifindex))
10833 new_ifindex = dev_new_index(net);
10834 else
10835 new_ifindex = dev->ifindex;
10836
10837 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10838 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010839
10840 /*
10841 * Flush the unicast and multicast chains
10842 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010843 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010844 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010845
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010846 /* Send a netdev-removed uevent to the old namespace */
10847 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010848 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010849
Jiri Pirko93642e12020-01-25 12:17:08 +010010850 /* Move per-net netdevice notifiers that are following the netdevice */
10851 move_netdevice_notifiers_dev_net(dev, net);
10852
Eric W. Biedermance286d32007-09-12 13:53:49 +020010853 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010854 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010855 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010856
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010857 /* Send a netdev-add uevent to the new namespace */
10858 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010859 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010860
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010861 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010862 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010863 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010864
Christian Brauneref6a4c82020-02-27 04:37:19 +010010865 /* Adapt owner in case owning user namespace of target network
10866 * namespace is different from the original one.
10867 */
10868 err = netdev_change_owner(dev, net_old, net);
10869 WARN_ON(err);
10870
Eric W. Biedermance286d32007-09-12 13:53:49 +020010871 /* Add the device back in the hashes */
10872 list_netdevice(dev);
10873
10874 /* Notify protocols, that a new device appeared. */
10875 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10876
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010877 /*
10878 * Prevent userspace races by waiting until the network
10879 * device is fully setup before sending notifications.
10880 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010881 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010882
Eric W. Biedermance286d32007-09-12 13:53:49 +020010883 synchronize_net();
10884 err = 0;
10885out:
10886 return err;
10887}
Johannes Berg463d0182009-07-14 00:33:35 +020010888EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010889
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010890static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010891{
10892 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010893 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010894 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010895 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010896
Linus Torvalds1da177e2005-04-16 15:20:36 -070010897 local_irq_disable();
10898 cpu = smp_processor_id();
10899 sd = &per_cpu(softnet_data, cpu);
10900 oldsd = &per_cpu(softnet_data, oldcpu);
10901
10902 /* Find end of our completion_queue. */
10903 list_skb = &sd->completion_queue;
10904 while (*list_skb)
10905 list_skb = &(*list_skb)->next;
10906 /* Append completion queue from offline CPU. */
10907 *list_skb = oldsd->completion_queue;
10908 oldsd->completion_queue = NULL;
10909
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010911 if (oldsd->output_queue) {
10912 *sd->output_queue_tailp = oldsd->output_queue;
10913 sd->output_queue_tailp = oldsd->output_queue_tailp;
10914 oldsd->output_queue = NULL;
10915 oldsd->output_queue_tailp = &oldsd->output_queue;
10916 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010917 /* Append NAPI poll list from offline CPU, with one exception :
10918 * process_backlog() must be called by cpu owning percpu backlog.
10919 * We properly handle process_queue & input_pkt_queue later.
10920 */
10921 while (!list_empty(&oldsd->poll_list)) {
10922 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10923 struct napi_struct,
10924 poll_list);
10925
10926 list_del_init(&napi->poll_list);
10927 if (napi->poll == process_backlog)
10928 napi->state = 0;
10929 else
10930 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010932
10933 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10934 local_irq_enable();
10935
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010936#ifdef CONFIG_RPS
10937 remsd = oldsd->rps_ipi_list;
10938 oldsd->rps_ipi_list = NULL;
10939#endif
10940 /* send out pending IPI's on offline CPU */
10941 net_rps_send_ipi(remsd);
10942
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010944 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010945 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010946 input_queue_head_incr(oldsd);
10947 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010948 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010949 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010950 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010954}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010955
Herbert Xu7f353bf2007-08-10 15:47:58 -070010956/**
Herbert Xub63365a2008-10-23 01:11:29 -070010957 * netdev_increment_features - increment feature set by one
10958 * @all: current feature set
10959 * @one: new feature set
10960 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010961 *
10962 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010963 * @one to the master device with current feature set @all. Will not
10964 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010965 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010966netdev_features_t netdev_increment_features(netdev_features_t all,
10967 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010968{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010969 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010970 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010971 mask |= NETIF_F_VLAN_CHALLENGED;
10972
Tom Herberta1882222015-12-14 11:19:43 -080010973 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010974 all &= one | ~NETIF_F_ALL_FOR_ALL;
10975
Michał Mirosław1742f182011-04-22 06:31:16 +000010976 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010977 if (all & NETIF_F_HW_CSUM)
10978 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010979
10980 return all;
10981}
Herbert Xub63365a2008-10-23 01:11:29 -070010982EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010983
Baruch Siach430f03c2013-06-02 20:43:55 +000010984static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010985{
10986 int i;
10987 struct hlist_head *hash;
10988
Kees Cook6da2ec52018-06-12 13:55:00 -070010989 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010990 if (hash != NULL)
10991 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10992 INIT_HLIST_HEAD(&hash[i]);
10993
10994 return hash;
10995}
10996
Eric W. Biederman881d9662007-09-17 11:56:21 -070010997/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010998static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010999{
Li RongQingd9f37d02018-07-13 14:41:36 +080011000 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080011001 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080011002
Rustad, Mark D734b6542012-07-18 09:06:07 +000011003 if (net != &init_net)
11004 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070011005
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011006 net->dev_name_head = netdev_create_hash();
11007 if (net->dev_name_head == NULL)
11008 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011009
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011010 net->dev_index_head = netdev_create_hash();
11011 if (net->dev_index_head == NULL)
11012 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011013
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020011014 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
11015
Eric W. Biederman881d9662007-09-17 11:56:21 -070011016 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070011017
11018err_idx:
11019 kfree(net->dev_name_head);
11020err_name:
11021 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070011022}
11023
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011024/**
11025 * netdev_drivername - network driver for the device
11026 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011027 *
11028 * Determine network driver for device.
11029 */
David S. Miller3019de12011-06-06 16:41:33 -070011030const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070011031{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070011032 const struct device_driver *driver;
11033 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070011034 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070011035
11036 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011037 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070011038 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011039
11040 driver = parent->driver;
11041 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070011042 return driver->name;
11043 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011044}
11045
Joe Perches6ea754e2014-09-22 11:10:50 -070011046static void __netdev_printk(const char *level, const struct net_device *dev,
11047 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000011048{
Joe Perchesb004ff42012-09-12 20:12:19 -070011049 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011050 dev_printk_emit(level[1] - '0',
11051 dev->dev.parent,
11052 "%s %s %s%s: %pV",
11053 dev_driver_string(dev->dev.parent),
11054 dev_name(dev->dev.parent),
11055 netdev_name(dev), netdev_reg_state(dev),
11056 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011057 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011058 printk("%s%s%s: %pV",
11059 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011060 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070011061 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011062 }
Joe Perches256df2f2010-06-27 01:02:35 +000011063}
11064
Joe Perches6ea754e2014-09-22 11:10:50 -070011065void netdev_printk(const char *level, const struct net_device *dev,
11066 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000011067{
11068 struct va_format vaf;
11069 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000011070
11071 va_start(args, format);
11072
11073 vaf.fmt = format;
11074 vaf.va = &args;
11075
Joe Perches6ea754e2014-09-22 11:10:50 -070011076 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011077
Joe Perches256df2f2010-06-27 01:02:35 +000011078 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000011079}
11080EXPORT_SYMBOL(netdev_printk);
11081
11082#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070011083void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000011084{ \
Joe Perches256df2f2010-06-27 01:02:35 +000011085 struct va_format vaf; \
11086 va_list args; \
11087 \
11088 va_start(args, fmt); \
11089 \
11090 vaf.fmt = fmt; \
11091 vaf.va = &args; \
11092 \
Joe Perches6ea754e2014-09-22 11:10:50 -070011093 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070011094 \
Joe Perches256df2f2010-06-27 01:02:35 +000011095 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000011096} \
11097EXPORT_SYMBOL(func);
11098
11099define_netdev_printk_level(netdev_emerg, KERN_EMERG);
11100define_netdev_printk_level(netdev_alert, KERN_ALERT);
11101define_netdev_printk_level(netdev_crit, KERN_CRIT);
11102define_netdev_printk_level(netdev_err, KERN_ERR);
11103define_netdev_printk_level(netdev_warn, KERN_WARNING);
11104define_netdev_printk_level(netdev_notice, KERN_NOTICE);
11105define_netdev_printk_level(netdev_info, KERN_INFO);
11106
Pavel Emelyanov46650792007-10-08 20:38:39 -070011107static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011108{
11109 kfree(net->dev_name_head);
11110 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030011111 if (net != &init_net)
11112 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070011113}
11114
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011115static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070011116 .init = netdev_init,
11117 .exit = netdev_exit,
11118};
11119
Pavel Emelyanov46650792007-10-08 20:38:39 -070011120static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011121{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011122 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011123 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011124 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020011125 * initial network namespace
11126 */
11127 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011128 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011129 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011130 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020011131
11132 /* Ignore unmoveable devices (i.e. loopback) */
11133 if (dev->features & NETIF_F_NETNS_LOCAL)
11134 continue;
11135
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011136 /* Leave virtual devices for the generic cleanup */
11137 if (dev->rtnl_link_ops)
11138 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080011139
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011140 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011141 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020011142 if (__dev_get_by_name(&init_net, fb_name))
11143 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011144 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011145 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000011146 pr_emerg("%s: failed to move %s to init_net: %d\n",
11147 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011148 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020011149 }
11150 }
11151 rtnl_unlock();
11152}
11153
Eric W. Biederman50624c92013-09-23 21:19:49 -070011154static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
11155{
11156 /* Return with the rtnl_lock held when there are no network
11157 * devices unregistering in any network namespace in net_list.
11158 */
11159 struct net *net;
11160 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010011161 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011162
Peter Zijlstraff960a72014-10-29 17:04:56 +010011163 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011164 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070011165 unregistering = false;
11166 rtnl_lock();
11167 list_for_each_entry(net, net_list, exit_list) {
11168 if (net->dev_unreg_count > 0) {
11169 unregistering = true;
11170 break;
11171 }
11172 }
11173 if (!unregistering)
11174 break;
11175 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010011176
11177 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011178 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010011179 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011180}
11181
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011182static void __net_exit default_device_exit_batch(struct list_head *net_list)
11183{
11184 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011185 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011186 * Do this across as many network namespaces as possible to
11187 * improve batching efficiency.
11188 */
11189 struct net_device *dev;
11190 struct net *net;
11191 LIST_HEAD(dev_kill_list);
11192
Eric W. Biederman50624c92013-09-23 21:19:49 -070011193 /* To prevent network device cleanup code from dereferencing
11194 * loopback devices or network devices that have been freed
11195 * wait here for all pending unregistrations to complete,
11196 * before unregistring the loopback device and allowing the
11197 * network namespace be freed.
11198 *
11199 * The netdev todo list containing all network devices
11200 * unregistrations that happen in default_device_exit_batch
11201 * will run in the rtnl_unlock() at the end of
11202 * default_device_exit_batch.
11203 */
11204 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011205 list_for_each_entry(net, net_list, exit_list) {
11206 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020011207 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011208 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
11209 else
11210 unregister_netdevice_queue(dev, &dev_kill_list);
11211 }
11212 }
11213 unregister_netdevice_many(&dev_kill_list);
11214 rtnl_unlock();
11215}
11216
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011217static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011218 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011219 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020011220};
11221
Linus Torvalds1da177e2005-04-16 15:20:36 -070011222/*
11223 * Initialize the DEV module. At boot time this walks the device list and
11224 * unhooks any devices that fail to initialise (normally hardware not
11225 * present) and leaves us with a valid list of present and active devices.
11226 *
11227 */
11228
11229/*
11230 * This is called single threaded during boot, so no need
11231 * to take the rtnl semaphore.
11232 */
11233static int __init net_dev_init(void)
11234{
11235 int i, rc = -ENOMEM;
11236
11237 BUG_ON(!dev_boot_phase);
11238
Linus Torvalds1da177e2005-04-16 15:20:36 -070011239 if (dev_proc_init())
11240 goto out;
11241
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011242 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070011243 goto out;
11244
11245 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080011246 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011247 INIT_LIST_HEAD(&ptype_base[i]);
11248
Vlad Yasevich62532da2012-11-15 08:49:10 +000011249 INIT_LIST_HEAD(&offload_base);
11250
Eric W. Biederman881d9662007-09-17 11:56:21 -070011251 if (register_pernet_subsys(&netdev_net_ops))
11252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253
11254 /*
11255 * Initialise the packet receive queues.
11256 */
11257
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070011258 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070011259 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011260 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011261
Eric Dumazet41852492016-08-26 12:50:39 -070011262 INIT_WORK(flush, flush_backlog);
11263
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011264 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070011265 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010011266#ifdef CONFIG_XFRM_OFFLOAD
11267 skb_queue_head_init(&sd->xfrm_backlog);
11268#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011269 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000011270 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000011271#ifdef CONFIG_RPS
Peter Zijlstra545b8c82020-06-15 11:29:31 +020011272 INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011273 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070011274#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000011275
David S. Miller7c4ec742018-07-20 23:37:55 -070011276 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011277 sd->backlog.poll = process_backlog;
11278 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011279 }
11280
Linus Torvalds1da177e2005-04-16 15:20:36 -070011281 dev_boot_phase = 0;
11282
Eric W. Biederman505d4f72008-11-07 22:54:20 -080011283 /* The loopback device is special if any other network devices
11284 * is present in a network namespace the loopback device must
11285 * be present. Since we now dynamically allocate and free the
11286 * loopback device ensure this invariant is maintained by
11287 * keeping the loopback device as the first device on the
11288 * list of network devices. Ensuring the loopback devices
11289 * is the first device that appears and the last network device
11290 * that disappears.
11291 */
11292 if (register_pernet_device(&loopback_net_ops))
11293 goto out;
11294
11295 if (register_pernet_device(&default_device_ops))
11296 goto out;
11297
Carlos R. Mafra962cf362008-05-15 11:15:37 -030011298 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11299 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011300
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011301 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11302 NULL, dev_cpu_dead);
11303 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011304 rc = 0;
11305out:
11306 return rc;
11307}
11308
11309subsys_initcall(net_dev_init);